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
|
use ::{MSP430, Operand, Opcode, Instruction, Width, DecodeError};
use std::fmt::{self, Display, Formatter};
use std;
use yaxpeax_arch::{Arch, Colorize, NoColors, ShowContextual, YaxColors};
impl Display for Instruction {
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
let mut s = String::new();
self.contextualize(&NoColors, 0, Some(&NoContext), &mut s).unwrap();
write!(f, "{}", s)
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
DecodeError::ExhaustedInput => write!(f, "exhausted input"),
DecodeError::InvalidOpcode => write!(f, "invalid opcode"),
DecodeError::InvalidOperand => write!(f, "invalid operand"),
}
}
}
/// No per-operand when contextualizing an instruction.
pub struct NoContext;
impl <T: std::fmt::Write, Color: fmt::Display, Y: YaxColors<Color>> ShowContextual<<MSP430 as Arch>::Address, NoContext, Color, T, Y> for Instruction {
fn contextualize(&self, _colors: &Y, _address: <MSP430 as Arch>::Address, _context: Option<&NoContext>, out: &mut T) -> std::fmt::Result {
write!(out, "{}", self.opcode)?;
match self.op_width {
Width::B => { write!(out, ".b")? },
Width::W => { }
};
match self.operands[0] {
Operand::Nothing => return Ok(()),
x @ _ => {
write!(out, " {}", x)?;
}
};
match self.operands[1] {
Operand::Nothing => return Ok(()),
x @ _ => {
write!(out, ", {}", x)?;
}
};
Ok(())
}
}
#[cfg(feature="std")]
impl <T: std::fmt::Write, Color: fmt::Display, Y: YaxColors<Color>> ShowContextual<<MSP430 as Arch>::Address, [Option<String>], Color, T, Y> for Instruction {
fn contextualize(&self, _colors: &Y, _address: <MSP430 as Arch>::Address, _context: Option<&[Option<String>]>, out: &mut T) -> std::fmt::Result {
write!(out, "{}", self.opcode)?;
match self.op_width {
Width::B => { write!(out, ".b")? },
Width::W => { }
};
match self.operands[0] {
Operand::Nothing => return Ok(()),
x @ _ => {
write!(out, " {}", x)?;
}
};
match self.operands[1] {
Operand::Nothing => return Ok(()),
x @ _ => {
write!(out, ", {}", x)?;
}
};
Ok(())
}
}
impl Display for Opcode {
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
match self {
Opcode::Invalid(a) => { write!(f, "invalid({:04x})", a) },
Opcode::RRC => { write!(f, "rrc") },
Opcode::SWPB => { write!(f, "swpb") },
Opcode::RRA => { write!(f, "rra") },
Opcode::SXT => { write!(f, "sxt") },
Opcode::PUSH => { write!(f, "push") },
Opcode::CALL => { write!(f, "call") },
Opcode::RETI => { write!(f, "reti") },
Opcode::JNE => { write!(f, "jne") },
Opcode::JEQ => { write!(f, "jeq") },
Opcode::JNC => { write!(f, "jnc") },
Opcode::JC => { write!(f, "jc") },
Opcode::JN => { write!(f, "jn") },
Opcode::JGE => { write!(f, "jge") },
Opcode::JL => { write!(f, "jl") },
Opcode::JMP => { write!(f, "jmp") },
Opcode::MOV => { write!(f, "mov") },
Opcode::ADD => { write!(f, "add") },
Opcode::ADDC => { write!(f, "addc") },
Opcode::SUBC => { write!(f, "subc") },
Opcode::SUB => { write!(f, "sub") },
Opcode::CMP => { write!(f, "cmp") },
Opcode::DADD => { write!(f, "dadd") },
Opcode::BIT => { write!(f, "bit") },
Opcode::BIC => { write!(f, "bic") },
Opcode::BIS => { write!(f, "bis") },
Opcode::XOR => { write!(f, "xor") },
Opcode::AND => { write!(f, "and") }
}
}
}
impl Display for Operand {
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
let mut s = String::new();
self.colorize(&NoColors, &mut s).unwrap();
write!(f, "{}", s)
}
}
impl <T: std::fmt::Write, Color: fmt::Display, Y: YaxColors<Color>> Colorize<T, Color, Y> for Operand {
fn colorize(&self, _colors: &Y, out: &mut T) -> std::fmt::Result {
fn signed_hex(num: i16) -> String {
if num >= 0 {
format!("+{:#x}", num)
} else {
format!("-{:#x}", -num)
}
}
match self {
Operand::Register(reg) => {
write!(out, "R{}", reg)
},
Operand::Indexed(reg, offset) => {
write!(out, "{}(R{})", signed_hex(*offset as i16), reg)
},
Operand::RegisterIndirect(reg) => {
write!(out, "@R{}", reg)
},
Operand::IndirectAutoinc(reg) => {
write!(out, "@R{}+", reg)
},
Operand::Offset(offset) => {
write!(out, "${}", signed_hex(*offset as i16))
},
Operand::Symbolic(offset) => {
write!(out, "{}(PC)", signed_hex(*offset as i16))
},
Operand::Immediate(imm) => {
write!(out, "#0x{:x}", imm)
},
Operand::Absolute(offset) => {
write!(out, "&0x{:x}", offset)
},
Operand::Const4 => {
write!(out, "4")
},
Operand::Const8 => {
write!(out, "8")
},
Operand::Const0 => {
write!(out, "0")
},
Operand::Const1 => {
write!(out, "1")
},
Operand::Const2 => {
write!(out, "2")
},
Operand::ConstNeg1 => {
write!(out, "-1")
},
Operand::Nothing => {
write!(out, "<No Operand>")
}
}
}
}
|