1 |
dpavlin |
1 |
/* |
2 |
|
|
* HT Editor |
3 |
|
|
* asm.cc |
4 |
|
|
* |
5 |
|
|
* Copyright (C) 1999-2002 Stefan Weyergraf |
6 |
|
|
* |
7 |
|
|
* This program is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License version 2 as |
9 |
|
|
* published by the Free Software Foundation. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <cstring> |
22 |
|
|
#include <cstdio> |
23 |
|
|
#include <cstdarg> |
24 |
|
|
|
25 |
|
|
#include "asm.h" |
26 |
|
|
#include "tools/data.h" |
27 |
|
|
#include "system/types.h" |
28 |
|
|
#include "tools/atom.h" |
29 |
|
|
#include "tools/debug.h" |
30 |
|
|
|
31 |
|
|
/* FIX8 |
32 |
|
|
#include "alphadis.h" |
33 |
|
|
#include "ia64dis.h" |
34 |
|
|
#include "ildis.h" |
35 |
|
|
#include "javadis.h" |
36 |
|
|
#include "x86dis.h" |
37 |
|
|
*/ |
38 |
|
|
|
39 |
|
|
/* |
40 |
|
|
* CLASS Assembler |
41 |
|
|
*/ |
42 |
|
|
|
43 |
|
|
Assembler::Assembler(bool b) |
44 |
|
|
{ |
45 |
|
|
codes = 0; |
46 |
|
|
bigendian = b; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
Assembler::~Assembler() |
50 |
|
|
{ |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
asm_insn *Assembler::alloc_insn() |
54 |
|
|
{ |
55 |
|
|
return NULL; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
void Assembler::deletecode(asm_code *code) |
59 |
|
|
{ |
60 |
|
|
asm_code **p=&codes, *c=codes; |
61 |
|
|
while (c) { |
62 |
|
|
if (c==code) { |
63 |
|
|
*p=c->next; |
64 |
|
|
delete c; |
65 |
|
|
return; |
66 |
|
|
} |
67 |
|
|
c=c->next; |
68 |
|
|
p=&(*p)->next; |
69 |
|
|
} |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
asm_code *Assembler::encode(asm_insn *asm_insn, int _options, CPU_ADDR cur_address) |
73 |
|
|
{ |
74 |
|
|
free_asm_codes(); |
75 |
|
|
error=0; |
76 |
|
|
options=_options; |
77 |
|
|
return 0; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
void Assembler::clearcode() |
81 |
|
|
{ |
82 |
|
|
code.size=0; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
void Assembler::emitbyte(byte b) |
86 |
|
|
{ |
87 |
|
|
code.data[code.size] = b; |
88 |
|
|
code.size++; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
void Assembler::emitword(uint16 w) |
92 |
|
|
{ |
93 |
|
|
if (bigendian) { |
94 |
|
|
code.data[code.size+1] = (byte)w; |
95 |
|
|
code.data[code.size+0] = (byte)(w>>8); |
96 |
|
|
} else { |
97 |
|
|
code.data[code.size+0] = (byte)w; |
98 |
|
|
code.data[code.size+1] = (byte)(w>>8); |
99 |
|
|
} |
100 |
|
|
code.size += 2; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
void Assembler::emitdword(uint32 d) |
104 |
|
|
{ |
105 |
|
|
if (bigendian) { |
106 |
|
|
code.data[code.size+3] = (byte)d; |
107 |
|
|
code.data[code.size+2] = (byte)(d>>8); |
108 |
|
|
code.data[code.size+1] = (byte)(d>>16); |
109 |
|
|
code.data[code.size+0] = (byte)(d>>24); |
110 |
|
|
} else { |
111 |
|
|
code.data[code.size+0] = (byte)d; |
112 |
|
|
code.data[code.size+1] = (byte)(d>>8); |
113 |
|
|
code.data[code.size+2] = (byte)(d>>16); |
114 |
|
|
code.data[code.size+3] = (byte)(d>>24); |
115 |
|
|
} |
116 |
|
|
code.size += 4; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
void Assembler::free_asm_codes() |
120 |
|
|
{ |
121 |
|
|
while (codes) { |
122 |
|
|
asm_code *t=codes->next; |
123 |
|
|
delete codes; |
124 |
|
|
codes=t; |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
char *Assembler::get_error_msg() |
129 |
|
|
{ |
130 |
|
|
return error_msg; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
char *Assembler::get_name() |
134 |
|
|
{ |
135 |
|
|
return "generic asm"; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
void Assembler::newcode() |
139 |
|
|
{ |
140 |
|
|
code.size=0; |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
asm_code *Assembler::shortest(asm_code *codes) |
144 |
|
|
{ |
145 |
|
|
asm_code *best=0; |
146 |
|
|
uint32 bestv=0xffffffff; |
147 |
|
|
while (codes) { |
148 |
|
|
if (codes->size<bestv) { |
149 |
|
|
best=codes; |
150 |
|
|
bestv=codes->size; |
151 |
|
|
} |
152 |
|
|
codes=codes->next; |
153 |
|
|
}; |
154 |
|
|
return best; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
void Assembler::pushcode() |
158 |
|
|
{ |
159 |
|
|
asm_code **t=&codes; |
160 |
|
|
while (*t) { |
161 |
|
|
t=&(*t)->next; |
162 |
|
|
} |
163 |
|
|
*t=new asm_code; |
164 |
|
|
|
165 |
|
|
memmove(*t, &code, sizeof code); |
166 |
|
|
(*t)->next=0; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
int Assembler::translate_str(asm_insn *asm_insn, const char *s) |
170 |
|
|
{ |
171 |
|
|
return 0; |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
void Assembler::set_error_msg(char *format, ...) |
175 |
|
|
{ |
176 |
|
|
va_list arg; |
177 |
|
|
va_start(arg, format); |
178 |
|
|
vsprintf(error_msg, format, arg); |
179 |
|
|
va_end(arg); |
180 |
|
|
error=1; |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
void Assembler::set_imm_eval_proc(int (*p)(void *context, char **s, uint32 *v), void *c) |
184 |
|
|
{ |
185 |
|
|
imm_eval_proc=p; |
186 |
|
|
imm_eval_context=c; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
/* |
190 |
|
|
* CLASS disassembler |
191 |
|
|
*/ |
192 |
|
|
|
193 |
|
|
Disassembler::Disassembler() |
194 |
|
|
{ |
195 |
|
|
disable_highlighting(); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
Disassembler::~Disassembler() |
199 |
|
|
{ |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
char* (*addr_sym_func)(CPU_ADDR addr, int *symstrlen, void *context) = NULL; |
203 |
|
|
void* addr_sym_func_context = NULL; |
204 |
|
|
|
205 |
|
|
dis_insn *Disassembler::createInvalidInsn() |
206 |
|
|
{ |
207 |
|
|
return NULL; |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
void Disassembler::hexd(char **s, int size, int options, int imm) |
211 |
|
|
{ |
212 |
|
|
char ff[16]; |
213 |
|
|
char *f = (char*)&ff; |
214 |
|
|
char *t = *s; |
215 |
|
|
*f++ = '%'; |
216 |
|
|
if ((imm>=0) && (imm<=9)) { |
217 |
|
|
*s += sprintf(*s, "%d", imm); |
218 |
|
|
} else if (options & DIS_STYLE_SIGNED) { |
219 |
|
|
if (!(options & DIS_STYLE_HEX_NOZEROPAD)) f += sprintf(f, "0%d", size); |
220 |
|
|
*f++ = 'd'; |
221 |
|
|
*f = 0; |
222 |
|
|
*s += sprintf(*s, ff, imm); |
223 |
|
|
} else { |
224 |
|
|
if (options & DIS_STYLE_HEX_CSTYLE) *f++ = '#'; |
225 |
|
|
if (!(options & DIS_STYLE_HEX_NOZEROPAD)) f += sprintf(f, "0%d", size); |
226 |
|
|
if (options & DIS_STYLE_HEX_UPPERCASE) *f++ = 'X'; else |
227 |
|
|
*f++ = 'x'; |
228 |
|
|
if (options & DIS_STYLE_HEX_ASMSTYLE) *f++ = 'h'; |
229 |
|
|
*f = 0; |
230 |
|
|
*s += sprintf(*s, ff, imm); |
231 |
|
|
if ((options & DIS_STYLE_HEX_NOZEROPAD) && (*t-'0'>9)) { |
232 |
|
|
memmove(t+1, t, strlen(t)+1); |
233 |
|
|
*t = '0'; |
234 |
|
|
(*s)++; |
235 |
|
|
} |
236 |
|
|
} |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
bool Disassembler::selectNext(dis_insn *disasm_insn) |
240 |
|
|
{ |
241 |
|
|
return false; |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
char *Disassembler::str(dis_insn *disasm_insn, int style) |
245 |
|
|
{ |
246 |
|
|
return strf(disasm_insn, style, DISASM_STRF_DEFAULT_FORMAT); |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
const char *Disassembler::get_cs(AsmSyntaxHighlightEnum style) |
250 |
|
|
{ |
251 |
|
|
const char *highlights[] = { |
252 |
|
|
ASM_SYNTAX_DEFAULT, |
253 |
|
|
ASM_SYNTAX_COMMENT, |
254 |
|
|
ASM_SYNTAX_NUMBER, |
255 |
|
|
ASM_SYNTAX_SYMBOL, |
256 |
|
|
ASM_SYNTAX_STRING |
257 |
|
|
}; |
258 |
|
|
return (highlight) ? highlights[(int)style] : ""; |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
void Disassembler::enable_highlighting() |
262 |
|
|
{ |
263 |
|
|
highlight = true; |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
void Disassembler::disable_highlighting() |
267 |
|
|
{ |
268 |
|
|
highlight = false; |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
/* FIX8 |
272 |
|
|
BUILDER(ATOM_DISASM_X86, x86dis) |
273 |
|
|
BUILDER(ATOM_DISASM_X86_VXD, x86dis_vxd) |
274 |
|
|
BUILDER(ATOM_DISASM_ALPHA, Alphadis) |
275 |
|
|
BUILDER(ATOM_DISASM_JAVA, javadis) |
276 |
|
|
BUILDER(ATOM_DISASM_IA64, IA64Disassembler) |
277 |
|
|
*/ |
278 |
|
|
|
279 |
|
|
bool init_asm() |
280 |
|
|
{ |
281 |
|
|
/* FIX8 |
282 |
|
|
REGISTER(ATOM_DISASM_X86, x86dis) |
283 |
|
|
REGISTER(ATOM_DISASM_X86_VXD, x86dis_vxd) |
284 |
|
|
REGISTER(ATOM_DISASM_ALPHA, Alphadis) |
285 |
|
|
REGISTER(ATOM_DISASM_JAVA, javadis) |
286 |
|
|
REGISTER(ATOM_DISASM_IA64, IA64Disassembler) |
287 |
|
|
*/ |
288 |
|
|
return true; |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
void done_asm() |
292 |
|
|
{ |
293 |
|
|
/* FIX8 |
294 |
|
|
UNREGISTER(ATOM_DISASM_IA64, IA64Disassembler) |
295 |
|
|
UNREGISTER(ATOM_DISASM_JAVA, javadis) |
296 |
|
|
UNREGISTER(ATOM_DISASM_ALPHA, Alphadis) |
297 |
|
|
UNREGISTER(ATOM_DISASM_X86_VXD, x86dis_vxd) |
298 |
|
|
UNREGISTER(ATOM_DISASM_X86, x86dis) |
299 |
|
|
*/ |
300 |
|
|
} |
301 |
|
|
|