1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use termion::color;
use yaxpeax_msp430::{Opcode, Operand, Instruction, Width};
use std::collections::HashMap;

pub fn opcode_color(opcode: Opcode) -> &'static color::Fg<&'static dyn color::Color> {
    match opcode {
        Opcode::Invalid(_) => { &color::Fg(&color::Red) }
        Opcode::CALL |
        Opcode::RETI => { &color::Fg(&color::LightGreen) }
        Opcode::JNE |
        Opcode::JEQ |
        Opcode::JNC |
        Opcode::JC |
        Opcode::JN |
        Opcode::JGE |
        Opcode::JL |
        Opcode::JMP => { &color::Fg(&color::Green) }
        Opcode::MOV => { &color::Fg(&color::LightMagenta) }
        Opcode::RRA |
        Opcode::SXT |
        Opcode::PUSH |
        Opcode::AND |
        Opcode::XOR |
        Opcode::BIT |
        Opcode::BIC |
        Opcode::RRC |
        Opcode::SWPB |
        Opcode::BIS => { &color::Fg(&color::LightYellow) }
        Opcode::ADD |
        Opcode::ADDC |
        Opcode::SUBC |
        Opcode::SUB |
        Opcode::DADD |
        Opcode::CMP => { &color::Fg(&color::Yellow) }
    }
}

use arch::msp430::PartialInstructionContext;
impl <T> ::SyntaxedRender<u16, T, ()> for Instruction where T: PartialInstructionContext {
    fn render(&self, context: Option<&T>, _function_table: &HashMap<u16, ()>) -> String {
        fn render_operand<T: PartialInstructionContext>(operand: &Operand, context: Option<&T>) -> String {
            fn signed_hex(num: i16) -> String {
                if num >= 0 {
                    format!("+{:#x}", num)
                } else {
                    format!("-{:#x}", -num)
                }
            }
            match operand {
                Operand::Register(0) => { "pc".to_owned() },
                Operand::Register(1) => { "sp".to_owned() },
                Operand::Register(2) => { "sr".to_owned() },
                Operand::Register(3) => { "cg".to_owned() }, // this really shouldn't come up, since any read/write to CG should normalize to const
                Operand::Register(reg) => {
                    format!("r{}", reg)
                },
                Operand::Indexed(0, offset) => {
                    format!("{}(pc)", signed_hex(*offset as i16))
                },
                Operand::Indexed(1, offset) => {
                    format!("{}(sp)", signed_hex(*offset as i16))
                },
                Operand::Indexed(2, offset) => {
                    format!("{}(sr)", signed_hex(*offset as i16))
                },
                Operand::Indexed(3, offset) => {
                    format!("{}(cg)", signed_hex(*offset as i16))
                },
                Operand::Indexed(reg, offset) => {
                    format!("{}(r{})", signed_hex(*offset as i16), reg)
                },
                Operand::RegisterIndirect(0) => { "@pc".to_owned() }
                Operand::RegisterIndirect(1) => { "@sp".to_owned() }
                Operand::RegisterIndirect(2) => { "@sr".to_owned() }
                Operand::RegisterIndirect(3) => { "@cg".to_owned() }
                Operand::RegisterIndirect(reg) => {
                    format!("@r{}", reg)
                },
                Operand::IndirectAutoinc(0) => { "@pc+".to_owned() }
                Operand::IndirectAutoinc(1) => { "@sp+".to_owned() }
                Operand::IndirectAutoinc(2) => { "@sr+".to_owned() }
                Operand::IndirectAutoinc(3) => { "@cg+".to_owned() } // this one just makes no sense
                Operand::IndirectAutoinc(reg) => {
                    format!("@r{}+", reg)
                },
                Operand::Offset(offset) => {
                    match context.and_then(|ctx| ctx.address()) {
                        Some(address) => {
                            // TODO: Uhhhh.. is this supposed to be instr len, not 2?
                            format!("{:#x}", address.wrapping_add((*offset as u16).wrapping_mul(2)).wrapping_add(2))
                        },
                        None => {
                            format!("{}(pc)", signed_hex(*offset as i16))
                        }
                    }
                },
                Operand::Symbolic(offset) => {
                    match context.and_then(|ctx| ctx.address()) {
                        Some(address) => {
                            format!("{:#x}", address.wrapping_add(*offset))
                        },
                        None => {
                            format!("{}(pc)", signed_hex(*offset as i16))
                        }
                    }
                },
                Operand::Immediate(imm) => {
                    format!("#{:#x}", imm)
                },
                Operand::Absolute(offset) => {
                    format!("&{:#x}", offset)
                },
                Operand::Const4 => {
                    "4".to_owned()
                },
                Operand::Const8 => {
                    "8".to_owned()
                },
                Operand::Const0 => {
                    "0".to_owned()
                },
                Operand::Const1 => {
                    "1".to_owned()
                },
                Operand::Const2 => {
                    "2".to_owned()
                },
                Operand::ConstNeg1 => {
                    "-1".to_owned()
                },
                Operand::Nothing => {
                    "<No Operand>".to_owned()
                }
            }
        }

        // try to recover some of the "emulated" instructions... fall back with a naive render
        match self {
            Instruction { opcode: Opcode::MOV, operands: [Operand::Const0, Operand::Const0], op_width: _ } => {
                format!("{}{}{}", color::Fg(color::Blue), "nop", color::Fg(color::Reset))
            },
            Instruction { opcode: Opcode::MOV, operands: [Operand::Const0, dest], op_width: _ } => {
                let start_color = opcode_color(Opcode::MOV);
                format!("{}{}{} {}", start_color, "clr", color::Fg(color::Reset), render_operand(&dest, context))
            },
            Instruction { opcode: Opcode::MOV, operands: [Operand::IndirectAutoinc(1), Operand::Register(0)], op_width: Width::W } => {
                // this is a pop
                let start_color = opcode_color(Opcode::CALL);
                format!("{}{}{}", start_color, "ret", color::Fg(color::Reset))
            },
            Instruction { opcode: Opcode::MOV, operands: [Operand::IndirectAutoinc(1), dest], op_width: Width::W } => {
                // this is a pop
                let start_color = opcode_color(Opcode::PUSH);
                format!("{}{}{} {}", start_color, "pop", color::Fg(color::Reset), render_operand(&dest, context))
            },
            Instruction { opcode: Opcode::MOV, operands: [src, Operand::Register(0)], op_width: Width::W } => {
                // br [src]
                let start_color = opcode_color(Opcode::JMP);
                format!("{}{}{} {}", start_color, "br", color::Fg(color::Reset), render_operand(&src, context))
            }
            _ => {
                let start_color = opcode_color(self.opcode);
                let mut result = format!("{}{}{}{}", start_color, self.opcode, match self.op_width {
                    Width::W => "",
                    Width::B => ".b"
                }, color::Fg(color::Reset));

                match self.operands[0] {
                    Operand::Nothing => { return result; },
                    x @ _ => {
                        result.push(' ');
                        result.push_str(&render_operand(&x, context));
                    }
                };
                match self.operands[1] {
                    Operand::Nothing => { return result; },
                    x @ _ => {
                        result.push(',');
                        result.push(' ');
                        result.push_str(&render_operand(&x, context));
                    }
                };
                result
            }
        }
    }
}