-
Notifications
You must be signed in to change notification settings - Fork 1
/
dis_base.cpp
249 lines (223 loc) · 8.13 KB
/
dis_base.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright 2020 Tadashi G. Takaoka
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dis_base.h"
namespace libasm {
namespace {
constexpr char OPT_BOOL_UPPER_HEX[] PROGMEM = "upper-hex";
constexpr char OPT_DESC_UPPER_HEX[] PROGMEM = "use upper case letter for hexadecimal";
constexpr char OPT_BOOL_UPPERCASE[] PROGMEM = "upper-case";
constexpr char OPT_DESC_UPPERCASE[] PROGMEM = "use upper case letter for output";
constexpr char OPT_INT_LIST_RADIX[] PROGMEM = "list-radix";
constexpr char OPT_DESC_LIST_RADIX[] PROGMEM = "set listing radix (8, 16)";
constexpr char OPT_BOOL_RELATIVE[] PROGMEM = "relative";
constexpr char OPT_DESC_RELATIVE[] PROGMEM = "program counter relative branch target";
constexpr char OPT_BOOL_CSTYLE[] PROGMEM = "c-style";
constexpr char OPT_DESC_CSTYLE[] PROGMEM = "C language style number constant";
constexpr char OPT_BOOL_INTELHEX[] PROGMEM = "intel-hex";
constexpr char OPT_DESC_INTELHEX[] PROGMEM = "Intel style hexadecimal";
constexpr char OPT_CHAR_ORIGIN[] PROGMEM = "origin-char";
constexpr char OPT_DESC_ORIGIN[] PROGMEM = "letter for origin symbol";
} // namespace
Disassembler::Disassembler(const ValueFormatter::Plugins &plugins, const OptionBase *option)
: _formatter(plugins),
_commonOptions(&_opt_upperHex),
_options(option),
_opt_upperHex(this, &Disassembler::setUpperHex, OPT_BOOL_UPPER_HEX, OPT_DESC_UPPER_HEX,
&_opt_uppercase),
_opt_uppercase(this, &Disassembler::setUppercase, OPT_BOOL_UPPERCASE, OPT_DESC_UPPERCASE,
&_opt_listRadix),
_opt_listRadix(this, &Disassembler::setListRadix, OPT_INT_LIST_RADIX, OPT_DESC_LIST_RADIX,
&_opt_relative),
_opt_relative(this, &Disassembler::setRelativeTarget, OPT_BOOL_RELATIVE, OPT_DESC_RELATIVE,
&_opt_cstyle),
_opt_cstyle(this, &Disassembler::setCStyle, OPT_BOOL_CSTYLE, OPT_DESC_CSTYLE, &_opt_intelhex),
_opt_intelhex(
this, &Disassembler::setIntelHex, OPT_BOOL_INTELHEX, OPT_DESC_INTELHEX, &_opt_curSym),
_opt_curSym(this, &Disassembler::setCurSym, OPT_CHAR_ORIGIN, OPT_DESC_ORIGIN) {}
void Disassembler::reset() {
setUpperHex(true);
setUppercase(true);
setListRadix(RADIX_16);
setRelativeTarget(false);
setCStyle(false);
setIntelHex(false);
setCurSym(0);
}
const ValueFormatter &Disassembler::formatter() const {
if (_cstyle) {
static ValueFormatter formatter{ValueFormatter::Plugins::cstyle()};
return formatter;
}
if (_intelHex) {
static ValueFormatter formatter{ValueFormatter::Plugins::intel()};
return formatter;
}
return _formatter;
}
Error Disassembler::setUpperHex(bool enable) {
_upperHex = enable;
return OK;
}
Error Disassembler::setUppercase(bool enable) {
_uppercase = enable;
return OK;
}
Error Disassembler::setListRadix(int32_t radix) {
_listRadix = static_cast<Radix>(radix == 8 ? RADIX_8 : RADIX_16);
return OK;
}
Error Disassembler::setRelativeTarget(bool enable) {
_relativeTarget = enable;
return OK;
}
Error Disassembler::setCStyle(bool enable) {
_cstyle = enable;
if (_cstyle && _intelHex)
_intelHex = false;
return OK;
}
Error Disassembler::setIntelHex(bool enable) {
_intelHex = enable;
if (_intelHex && _cstyle)
_cstyle = false;
return OK;
}
Error Disassembler::setCurSym(char curSym) {
_curSym = curSym ? curSym : _formatter.locationSymbol();
return OK;
}
bool Disassembler::setCpu(const char *name) {
StrScanner scan{name};
return setCpu(scan) == OK;
}
Error Disassembler::setCpu(StrScanner &scan) {
return configSetter().setCpuName(scan);
}
Error Disassembler::setOption(const char *name, const char *text) {
StrScanner scan{text};
if (_commonOptions.setOption(name, scan) == OK)
return OK;
return options().setOption(name, scan);
}
Error Disassembler::decode(
DisMemory &memory, Insn &insn, char *operands, size_t size, SymbolTable *symtab) {
_symtab = symtab;
// This setError also reset error of Disassembler.
if (insn.setError(config().checkAddr(insn.address())))
return insn.getError();
StrCaseBuffer out(operands, size, _uppercase);
insn.nameBuffer().reset();
decodeImpl(memory, insn, out);
if (insn.isOK())
insn.setError(out);
return insn.getError();
}
const char *Disassembler::lookup(uint32_t addr, uint8_t addrWidth) const {
const char *symbol = nullptr;
if (_symtab) {
Value val;
val.setUnsigned(addr);
symbol = _symtab->lookupValue(val);
if (!symbol) {
val.setSigned(config().signExtend(addr, addrWidth));
symbol = _symtab->lookupValue(val);
}
}
return symbol;
}
StrBuffer &Disassembler::outDec(StrBuffer &out, uint32_t val, int8_t bits) const {
const auto bw = bits >= 0 ? bits : -bits;
const char *label = lookup(val, bw);
if (label)
return out.rtext(label);
StrCaseBuffer caseOut(out, _upperHex);
if (_listRadix == RADIX_8)
return formatter().formatOct(caseOut, val, bits).over(out);
return formatter().formatDec(caseOut, val, bits).over(out);
}
/**
* Convert |val| as |bits| octal integer. Treat |val| as signed
* integer when |bits| is negative. Output symbol label when |val| is
* in symbol table.
*/
StrBuffer &Disassembler::outHex(StrBuffer &out, uint32_t val, int8_t bits, bool relax) const {
const auto bw = bits >= 0 ? bits : -bits;
const char *label = lookup(val, bw);
if (label)
return out.rtext(label);
StrCaseBuffer caseOut(out, _upperHex);
if (_listRadix == RADIX_8)
return formatter().formatOct(caseOut, val, bits).over(out);
if (relax) {
auto abs = val;
ValueFormatter::absolute(abs, bits);
if (abs <= 32)
return formatter().formatDec(caseOut, val, bits).over(out);
}
return formatter().formatHex(caseOut, val, bits).over(out);
}
/**
* Convert |val| as |addrWidth| bit absolute address. Use default
* configured address width when |addrWdith| is ommitted. Output
* symbol label when |val| is in symbol table.
*/
StrBuffer &Disassembler::outAbsAddr(StrBuffer &out, uint32_t val, uint8_t addrWidth) const {
const char *label = lookup(val, addrWidth);
if (label)
return out.rtext(label);
if (addrWidth == 0)
addrWidth = uint8_t(config().addressWidth());
StrCaseBuffer caseOut(out, _upperHex);
if (_listRadix == RADIX_8)
return formatter().formatOct(caseOut, val, addrWidth).over(out);
return formatter().formatHex(caseOut, val, addrWidth).over(out);
}
/**
* Convert |target| as relative |deltaBits| offset from |origin|.
*/
StrBuffer &Disassembler::outRelAddr(
StrBuffer &out, uint32_t target, uint32_t origin, uint8_t deltaBits) const {
if (!_relativeTarget)
return outAbsAddr(out, target);
const char *label = lookup(target, config().addressWidth());
if (label)
return out.rtext(label);
out.letter(_curSym);
const auto delta = static_cast<int32_t>(target - origin);
if (delta == 0)
return out;
uint32_t val;
if (delta < 0) {
out.letter('-');
val = static_cast<uint32_t>(-delta);
} else {
out.letter('+');
val = static_cast<uint32_t>(delta);
}
StrCaseBuffer caseOut(out, _upperHex);
if (_listRadix == RADIX_8)
return formatter().formatOct(caseOut, val, deltaBits).over(out);
if (deltaBits <= 14)
return formatter().formatDec(caseOut, val, deltaBits).over(out);
return outHex(caseOut, val, deltaBits, true).over(out);
}
} // namespace libasm
// Local Variables:
// mode: c++
// c-basic-offset: 4
// tab-width: 4
// End:
// vim: set ft=cpp et ts=4 sw=4: