33c35af6 |
1 | #define _GNU_SOURCE |
c36e914d |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | #include <string.h> |
5 | |
6 | #include "my_assert.h" |
7 | #include "my_str.h" |
8 | |
9 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
10 | #define IS(w, y) !strcmp(w, y) |
39b168b8 |
11 | #define IS_START(w, y) !strncmp(w, y, strlen(y)) |
c36e914d |
12 | |
13 | #include "protoparse.h" |
14 | |
06c5d854 |
15 | static const char *asmfn; |
c36e914d |
16 | static int asmln; |
06c5d854 |
17 | static FILE *g_fhdr; |
c36e914d |
18 | |
940e8e66 |
19 | #define anote(fmt, ...) \ |
20 | printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__) |
c36e914d |
21 | #define awarn(fmt, ...) \ |
940e8e66 |
22 | printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__) |
c36e914d |
23 | #define aerr(fmt, ...) do { \ |
940e8e66 |
24 | printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \ |
33c35af6 |
25 | fcloseall(); \ |
c36e914d |
26 | exit(1); \ |
27 | } while (0) |
28 | |
69a3cdfc |
29 | enum op_flags { |
87bf6cec |
30 | OPF_RMD = (1 << 0), /* removed or optimized out */ |
31 | OPF_DATA = (1 << 1), /* data processing - writes to dst opr */ |
32 | OPF_FLAGS = (1 << 2), /* sets flags */ |
33 | OPF_JMP = (1 << 3), /* branches, ret and call */ |
34 | OPF_CC = (1 << 4), /* uses flags */ |
35 | OPF_TAIL = (1 << 5), /* ret or tail call */ |
7ba45c34 |
36 | OPF_RSAVE = (1 << 6), /* push/pop is local reg save/load */ |
37 | OPF_REP = (1 << 7), /* prefixed by rep */ |
38 | OPF_REPZ = (1 << 8), /* rep is repe/repz */ |
39 | OPF_REPNZ = (1 << 9), /* rep is repne/repnz */ |
de50b98b |
40 | OPF_FARG = (1 << 10), /* push collected as func arg (no reuse) */ |
a3684be1 |
41 | OPF_EBP_S = (1 << 11), /* ebp used as scratch, not BP */ |
c36e914d |
42 | }; |
43 | |
44 | enum op_op { |
45 | OP_INVAL, |
33c35af6 |
46 | OP_NOP, |
c36e914d |
47 | OP_PUSH, |
48 | OP_POP, |
49 | OP_MOV, |
850c9265 |
50 | OP_LEA, |
51 | OP_MOVZX, |
52 | OP_MOVSX, |
53 | OP_NOT, |
5101a5f9 |
54 | OP_CDQ, |
33c35af6 |
55 | OP_STOS, |
d4e3b5db |
56 | OP_MOVS, |
7ba45c34 |
57 | OP_CMPS, |
c36e914d |
58 | OP_RET, |
59 | OP_ADD, |
91977a1c |
60 | OP_SUB, |
850c9265 |
61 | OP_AND, |
62 | OP_OR, |
63 | OP_XOR, |
64 | OP_SHL, |
65 | OP_SHR, |
66 | OP_SAR, |
d4e3b5db |
67 | OP_ROL, |
68 | OP_ROR, |
69a3cdfc |
69 | OP_ADC, |
850c9265 |
70 | OP_SBB, |
71 | OP_INC, |
72 | OP_DEC, |
5101a5f9 |
73 | OP_NEG, |
850c9265 |
74 | OP_MUL, |
75 | OP_IMUL, |
5101a5f9 |
76 | OP_DIV, |
77 | OP_IDIV, |
c36e914d |
78 | OP_TEST, |
79 | OP_CMP, |
80 | OP_CALL, |
81 | OP_JMP, |
82 | OP_JO, |
83 | OP_JNO, |
84 | OP_JC, |
85 | OP_JNC, |
86 | OP_JZ, |
87 | OP_JNZ, |
88 | OP_JBE, |
89 | OP_JA, |
90 | OP_JS, |
91 | OP_JNS, |
92 | OP_JP, |
93 | OP_JNP, |
94 | OP_JL, |
95 | OP_JGE, |
96 | OP_JLE, |
97 | OP_JG, |
98 | }; |
99 | |
100 | enum opr_type { |
87bf6cec |
101 | OPT_UNSPEC, |
102 | OPT_REG, |
103 | OPT_REGMEM, |
104 | OPT_LABEL, |
850c9265 |
105 | OPT_OFFSET, |
87bf6cec |
106 | OPT_CONST, |
c36e914d |
107 | }; |
108 | |
2b43685d |
109 | // must be sorted (larger len must be further in enum) |
c36e914d |
110 | enum opr_lenmod { |
91977a1c |
111 | OPLM_UNSPEC, |
112 | OPLM_BYTE, |
113 | OPLM_WORD, |
114 | OPLM_DWORD, |
c36e914d |
115 | }; |
116 | |
850c9265 |
117 | #define MAX_OPERANDS 3 |
c36e914d |
118 | |
119 | struct parsed_opr { |
91977a1c |
120 | enum opr_type type; |
121 | enum opr_lenmod lmod; |
7ba45c34 |
122 | unsigned int is_ptr:1; // pointer in C |
123 | unsigned int is_array:1; // array in C |
a3684be1 |
124 | unsigned int type_from_var:1; // .. in header, sometimes wrong |
2b43685d |
125 | unsigned int size_mismatch:1; // type override differs from C |
126 | unsigned int size_lt:1; // type override is larger than C |
91977a1c |
127 | int reg; |
128 | unsigned int val; |
129 | char name[256]; |
c36e914d |
130 | }; |
131 | |
132 | struct parsed_op { |
91977a1c |
133 | enum op_op op; |
134 | struct parsed_opr operand[MAX_OPERANDS]; |
69a3cdfc |
135 | unsigned int flags; |
91977a1c |
136 | int operand_cnt; |
69a3cdfc |
137 | int regmask_src; // all referensed regs |
138 | int regmask_dst; |
940e8e66 |
139 | int pfomask; // flagop: parsed_flag_op that can't be delayed |
de50b98b |
140 | int argnum; // push: altered before call arg # |
940e8e66 |
141 | int cc_scratch; // scratch storage during analysis |
4c45fa73 |
142 | int bt_i; // branch target for branches |
143 | struct parsed_data *btj;// branch targets for jumptables |
91977a1c |
144 | void *datap; |
145 | }; |
146 | |
69a3cdfc |
147 | // datap: |
69a3cdfc |
148 | // OP_CALL - ptr to parsed_proto |
2b43685d |
149 | // (OPF_CC) - point to one of (OPF_FLAGS) that affects cc op |
69a3cdfc |
150 | |
91977a1c |
151 | struct parsed_equ { |
152 | char name[64]; |
153 | enum opr_lenmod lmod; |
154 | int offset; |
c36e914d |
155 | }; |
156 | |
4c45fa73 |
157 | struct parsed_data { |
158 | char label[256]; |
159 | enum opr_type type; |
160 | enum opr_lenmod lmod; |
161 | int count; |
162 | int count_alloc; |
163 | struct { |
164 | union { |
165 | char *label; |
63df67be |
166 | unsigned int val; |
4c45fa73 |
167 | } u; |
168 | int bt_i; |
169 | } *d; |
170 | }; |
171 | |
172 | struct label_ref { |
173 | int i; |
174 | struct label_ref *next; |
175 | }; |
176 | |
1bafb621 |
177 | enum ida_func_attr { |
178 | IDAFA_BP_FRAME = (1 << 0), |
179 | IDAFA_LIB_FUNC = (1 << 1), |
180 | IDAFA_STATIC = (1 << 2), |
181 | IDAFA_NORETURN = (1 << 3), |
182 | IDAFA_THUNK = (1 << 4), |
183 | IDAFA_FPD = (1 << 5), |
184 | }; |
185 | |
186 | #define MAX_OPS 4096 |
c36e914d |
187 | |
188 | static struct parsed_op ops[MAX_OPS]; |
91977a1c |
189 | static struct parsed_equ *g_eqs; |
190 | static int g_eqcnt; |
191 | static char g_labels[MAX_OPS][32]; |
4c45fa73 |
192 | static struct label_ref g_label_refs[MAX_OPS]; |
bd96f656 |
193 | static const struct parsed_proto *g_func_pp; |
4c45fa73 |
194 | static struct parsed_data *g_func_pd; |
195 | static int g_func_pd_cnt; |
91977a1c |
196 | static char g_func[256]; |
197 | static char g_comment[256]; |
198 | static int g_bp_frame; |
1bafb621 |
199 | static int g_sp_frame; |
200 | static int g_stack_fsz; |
4c45fa73 |
201 | static int g_ida_func_attr; |
91977a1c |
202 | #define ferr(op_, fmt, ...) do { \ |
63df67be |
203 | printf("error:%s:#%zd: '%s': " fmt, g_func, (op_) - ops, \ |
850c9265 |
204 | dump_op(op_), ##__VA_ARGS__); \ |
33c35af6 |
205 | fcloseall(); \ |
91977a1c |
206 | exit(1); \ |
207 | } while (0) |
de50b98b |
208 | #define fnote(op_, fmt, ...) \ |
209 | printf("error:%s:#%zd: '%s': " fmt, g_func, (op_) - ops, \ |
210 | dump_op(op_), ##__VA_ARGS__) |
91977a1c |
211 | |
212 | #define MAX_REGS 8 |
213 | |
214 | const char *regs_r32[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp" }; |
215 | const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" }; |
216 | const char *regs_r8l[] = { "al", "bl", "cl", "dl" }; |
217 | const char *regs_r8h[] = { "ah", "bh", "ch", "dh" }; |
218 | |
219 | enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP }; |
220 | |
69a3cdfc |
221 | // possible basic comparison types (without inversion) |
222 | enum parsed_flag_op { |
223 | PFO_O, // 0 OF=1 |
224 | PFO_C, // 2 CF=1 |
225 | PFO_Z, // 4 ZF=1 |
226 | PFO_BE, // 6 CF=1||ZF=1 |
227 | PFO_S, // 8 SF=1 |
228 | PFO_P, // a PF=1 |
229 | PFO_L, // c SF!=OF |
230 | PFO_LE, // e ZF=1||SF!=OF |
231 | }; |
232 | |
233 | static const char *parsed_flag_op_names[] = { |
234 | "o", "c", "z", "be", "s", "p", "l", "le" |
235 | }; |
236 | |
91977a1c |
237 | static int char_array_i(const char *array[], size_t len, const char *s) |
238 | { |
239 | int i; |
c36e914d |
240 | |
91977a1c |
241 | for (i = 0; i < len; i++) |
242 | if (IS(s, array[i])) |
243 | return i; |
c36e914d |
244 | |
91977a1c |
245 | return -1; |
246 | } |
247 | |
63df67be |
248 | static void printf_number(char *buf, size_t buf_size, |
249 | unsigned long number) |
91977a1c |
250 | { |
5101a5f9 |
251 | // output in C-friendly form |
252 | snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number); |
253 | } |
91977a1c |
254 | |
5101a5f9 |
255 | static int parse_reg(enum opr_lenmod *reg_lmod, const char *s) |
256 | { |
257 | int reg; |
91977a1c |
258 | |
5101a5f9 |
259 | reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s); |
260 | if (reg >= 0) { |
261 | *reg_lmod = OPLM_DWORD; |
262 | return reg; |
91977a1c |
263 | } |
5101a5f9 |
264 | reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s); |
265 | if (reg >= 0) { |
266 | *reg_lmod = OPLM_WORD; |
267 | return reg; |
268 | } |
269 | reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s); |
270 | if (reg >= 0) { |
271 | *reg_lmod = OPLM_BYTE; |
272 | return reg; |
273 | } |
274 | reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s); |
275 | if (reg >= 0) { |
276 | *reg_lmod = OPLM_BYTE; |
277 | return reg; |
850c9265 |
278 | } |
279 | |
280 | return -1; |
91977a1c |
281 | } |
282 | |
63df67be |
283 | static unsigned long parse_number(const char *number) |
91977a1c |
284 | { |
285 | int len = strlen(number); |
286 | const char *p = number; |
287 | char *endp = NULL; |
63df67be |
288 | unsigned long ret; |
91977a1c |
289 | int neg = 0; |
290 | int bad; |
91977a1c |
291 | |
292 | if (*p == '-') { |
293 | neg = 1; |
294 | p++; |
295 | } |
296 | if (len > 1 && *p == '0') |
297 | p++; |
298 | if (number[len - 1] == 'h') { |
63df67be |
299 | ret = strtoul(p, &endp, 16); |
91977a1c |
300 | bad = (*endp != 'h'); |
301 | } |
302 | else { |
63df67be |
303 | ret = strtoul(p, &endp, 10); |
91977a1c |
304 | bad = (*endp != 0); |
305 | } |
306 | if (bad) |
307 | aerr("number parsing failed\n"); |
308 | if (neg) |
309 | ret = -ret; |
310 | return ret; |
311 | } |
312 | |
5101a5f9 |
313 | static int parse_indmode(char *name, int *regmask, int need_c_cvt) |
314 | { |
315 | enum opr_lenmod lmod; |
316 | char cvtbuf[256]; |
317 | char *d = cvtbuf; |
318 | char *s = name; |
319 | char w[64]; |
320 | long number; |
321 | int reg; |
322 | int c = 0; |
323 | |
324 | *d = 0; |
325 | |
326 | while (*s != 0) { |
327 | d += strlen(d); |
328 | while (my_isblank(*s)) |
329 | s++; |
330 | for (; my_issep(*s); d++, s++) |
331 | *d = *s; |
332 | while (my_isblank(*s)) |
333 | s++; |
334 | *d = 0; |
335 | |
87bf6cec |
336 | // skip 'ds:' prefix |
39b168b8 |
337 | if (IS_START(s, "ds:")) |
87bf6cec |
338 | s += 3; |
339 | |
5101a5f9 |
340 | s = next_idt(w, sizeof(w), s); |
341 | if (w[0] == 0) |
342 | break; |
343 | c++; |
344 | |
345 | reg = parse_reg(&lmod, w); |
346 | if (reg >= 0) { |
347 | *regmask |= 1 << reg; |
348 | goto pass; |
349 | } |
350 | |
351 | if ('0' <= w[0] && w[0] <= '9') { |
352 | number = parse_number(w); |
353 | printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number); |
354 | continue; |
355 | } |
356 | |
357 | // probably some label/identifier - pass |
358 | |
359 | pass: |
360 | snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w); |
361 | } |
362 | |
363 | if (need_c_cvt) |
364 | strcpy(name, cvtbuf); |
365 | |
366 | return c; |
367 | } |
368 | |
4c45fa73 |
369 | static int is_reg_in_str(const char *s) |
370 | { |
371 | int i; |
372 | |
373 | if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3]))) |
374 | return 0; |
375 | |
376 | for (i = 0; i < ARRAY_SIZE(regs_r32); i++) |
377 | if (!strncmp(s, regs_r32[i], 3)) |
378 | return 1; |
379 | |
380 | return 0; |
381 | } |
382 | |
383 | static const char *parse_stack_el(const char *name, char *extra_reg) |
d4e3b5db |
384 | { |
4c45fa73 |
385 | const char *p, *p2, *s; |
1bafb621 |
386 | char *endp = NULL; |
d4e3b5db |
387 | char buf[32]; |
1bafb621 |
388 | long val; |
d4e3b5db |
389 | int len; |
390 | |
4c45fa73 |
391 | p = name; |
392 | if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) { |
393 | p += 4; |
394 | if (extra_reg != NULL) { |
395 | strncpy(extra_reg, name, 3); |
396 | extra_reg[4] = 0; |
397 | } |
d4e3b5db |
398 | } |
4c45fa73 |
399 | |
400 | if (IS_START(p, "ebp+")) { |
401 | p += 4; |
402 | |
403 | p2 = strchr(p, '+'); |
404 | if (p2 != NULL && is_reg_in_str(p)) { |
405 | if (extra_reg != NULL) { |
406 | strncpy(extra_reg, p, p2 - p); |
407 | extra_reg[p2 - p] = 0; |
408 | } |
409 | p = p2 + 1; |
410 | } |
411 | |
412 | if (!('0' <= *p && *p <= '9')) |
413 | return p; |
414 | |
415 | return NULL; |
416 | } |
417 | |
39b168b8 |
418 | if (!IS_START(name, "esp+")) |
d4e3b5db |
419 | return NULL; |
420 | |
421 | p = strchr(name + 4, '+'); |
422 | if (p) { |
1bafb621 |
423 | // must be a number after esp+, already converted to 0x.. |
424 | s = name + 4; |
425 | if (!('0' <= *s && *s <= '9')) { |
426 | aerr("%s nan?\n", __func__); |
d4e3b5db |
427 | return NULL; |
1bafb621 |
428 | } |
429 | if (s[0] == '0' && s[1] == 'x') |
430 | s += 2; |
431 | len = p - s; |
d4e3b5db |
432 | if (len < sizeof(buf) - 1) { |
1bafb621 |
433 | strncpy(buf, s, len); |
d4e3b5db |
434 | buf[len] = 0; |
1bafb621 |
435 | val = strtol(buf, &endp, 16); |
436 | if (val == 0 || *endp != 0) { |
437 | aerr("%s num parse fail for '%s'\n", __func__, buf); |
438 | return NULL; |
439 | } |
d4e3b5db |
440 | } |
441 | p++; |
442 | } |
443 | else |
444 | p = name + 4; |
445 | |
446 | if ('0' <= *p && *p <= '9') |
447 | return NULL; |
448 | |
449 | return p; |
450 | } |
451 | |
850c9265 |
452 | static int guess_lmod_from_name(struct parsed_opr *opr) |
453 | { |
454 | if (!strncmp(opr->name, "dword_", 6)) { |
455 | opr->lmod = OPLM_DWORD; |
456 | return 1; |
457 | } |
458 | if (!strncmp(opr->name, "word_", 5)) { |
459 | opr->lmod = OPLM_WORD; |
460 | return 1; |
461 | } |
462 | if (!strncmp(opr->name, "byte_", 5)) { |
463 | opr->lmod = OPLM_BYTE; |
464 | return 1; |
465 | } |
466 | return 0; |
467 | } |
468 | |
3ebea2cf |
469 | static int guess_lmod_from_c_type(enum opr_lenmod *lmod, |
470 | const struct parsed_type *c_type) |
06c5d854 |
471 | { |
06c5d854 |
472 | static const char *dword_types[] = { |
a3684be1 |
473 | "int", "_DWORD", "UINT_PTR", |
474 | "DWORD", "HANDLE", "HWND", "HMODULE", |
840257f6 |
475 | "WPARAM", "LPARAM", "UINT", |
06c5d854 |
476 | }; |
477 | static const char *word_types[] = { |
2b43685d |
478 | "uint16_t", "int16_t", |
479 | "unsigned __int16", "__int16", |
06c5d854 |
480 | }; |
481 | static const char *byte_types[] = { |
2b43685d |
482 | "uint8_t", "int8_t", "char", |
483 | "unsigned __int8", "__int8", "BYTE", |
a3684be1 |
484 | "_UNKNOWN", |
06c5d854 |
485 | }; |
3ebea2cf |
486 | const char *n; |
06c5d854 |
487 | int i; |
488 | |
3ebea2cf |
489 | if (c_type->is_ptr) { |
490 | *lmod = OPLM_DWORD; |
06c5d854 |
491 | return 1; |
492 | } |
493 | |
3ebea2cf |
494 | n = skip_type_mod(c_type->name); |
06c5d854 |
495 | |
3ebea2cf |
496 | for (i = 0; i < ARRAY_SIZE(dword_types); i++) { |
497 | if (IS(n, dword_types[i])) { |
498 | *lmod = OPLM_DWORD; |
06c5d854 |
499 | return 1; |
500 | } |
501 | } |
502 | |
503 | for (i = 0; i < ARRAY_SIZE(word_types); i++) { |
3ebea2cf |
504 | if (IS(n, word_types[i])) { |
505 | *lmod = OPLM_WORD; |
06c5d854 |
506 | return 1; |
507 | } |
508 | } |
509 | |
510 | for (i = 0; i < ARRAY_SIZE(byte_types); i++) { |
3ebea2cf |
511 | if (IS(n, byte_types[i])) { |
512 | *lmod = OPLM_BYTE; |
06c5d854 |
513 | return 1; |
514 | } |
515 | } |
516 | |
06c5d854 |
517 | return 0; |
518 | } |
519 | |
4c45fa73 |
520 | static enum opr_type lmod_from_directive(const char *d) |
521 | { |
522 | if (IS(d, "dd")) |
523 | return OPLM_DWORD; |
524 | else if (IS(d, "dw")) |
525 | return OPLM_WORD; |
526 | else if (IS(d, "db")) |
527 | return OPLM_BYTE; |
528 | |
529 | aerr("unhandled directive: '%s'\n", d); |
530 | return OPLM_UNSPEC; |
531 | } |
532 | |
5101a5f9 |
533 | static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod, |
534 | int *regmask) |
535 | { |
536 | opr->type = OPT_REG; |
537 | opr->reg = reg; |
538 | opr->lmod = lmod; |
539 | *regmask |= 1 << reg; |
540 | } |
541 | |
39b168b8 |
542 | static struct parsed_equ *equ_find(struct parsed_op *po, const char *name, |
543 | int *extra_offs); |
87bf6cec |
544 | |
69a3cdfc |
545 | static int parse_operand(struct parsed_opr *opr, |
546 | int *regmask, int *regmask_indirect, |
1bafb621 |
547 | char words[16][256], int wordc, int w, unsigned int op_flags) |
c36e914d |
548 | { |
bd96f656 |
549 | const struct parsed_proto *pp; |
850c9265 |
550 | enum opr_lenmod tmplmod; |
63df67be |
551 | unsigned long number; |
850c9265 |
552 | int ret, len; |
1bafb621 |
553 | int wordc_in; |
39b168b8 |
554 | char *tmp; |
850c9265 |
555 | int i; |
c36e914d |
556 | |
1bafb621 |
557 | if (w >= wordc) |
558 | aerr("parse_operand w %d, wordc %d\n", w, wordc); |
c36e914d |
559 | |
1bafb621 |
560 | opr->reg = xUNSPEC; |
91977a1c |
561 | |
1bafb621 |
562 | for (i = w; i < wordc; i++) { |
563 | len = strlen(words[i]); |
564 | if (words[i][len - 1] == ',') { |
565 | words[i][len - 1] = 0; |
566 | wordc = i + 1; |
567 | break; |
568 | } |
569 | } |
c36e914d |
570 | |
1bafb621 |
571 | wordc_in = wordc - w; |
c36e914d |
572 | |
1bafb621 |
573 | if ((op_flags & OPF_JMP) && wordc_in > 0 |
574 | && !('0' <= words[w][0] && words[w][0] <= '9')) |
575 | { |
576 | const char *label = NULL; |
577 | |
578 | if (wordc_in == 3 && !strncmp(words[w], "near", 4) |
579 | && IS(words[w + 1], "ptr")) |
580 | label = words[w + 2]; |
581 | else if (wordc_in == 2 && IS(words[w], "short")) |
582 | label = words[w + 1]; |
583 | else if (wordc_in == 1 |
584 | && strchr(words[w], '[') == NULL |
585 | && parse_reg(&tmplmod, words[w]) < 0) |
586 | label = words[w]; |
587 | |
588 | if (label != NULL) { |
589 | opr->type = OPT_LABEL; |
39b168b8 |
590 | if (IS_START(label, "ds:")) |
591 | label += 3; |
1bafb621 |
592 | strcpy(opr->name, label); |
593 | return wordc; |
594 | } |
595 | } |
c36e914d |
596 | |
1bafb621 |
597 | if (wordc_in >= 3) { |
598 | if (IS(words[w + 1], "ptr")) { |
599 | if (IS(words[w], "dword")) |
600 | opr->lmod = OPLM_DWORD; |
601 | else if (IS(words[w], "word")) |
602 | opr->lmod = OPLM_WORD; |
603 | else if (IS(words[w], "byte")) |
604 | opr->lmod = OPLM_BYTE; |
605 | else |
606 | aerr("type parsing failed\n"); |
607 | w += 2; |
608 | wordc_in = wordc - w; |
609 | } |
610 | } |
611 | |
39b168b8 |
612 | if (wordc_in == 2) { |
613 | if (IS(words[w], "offset")) { |
614 | opr->type = OPT_OFFSET; |
615 | strcpy(opr->name, words[w + 1]); |
616 | return wordc; |
617 | } |
618 | if (IS(words[w], "(offset")) { |
619 | char *p = strchr(words[w + 1], ')'); |
620 | if (p == NULL) |
621 | aerr("parse of bracketed offset failed\n"); |
622 | *p = 0; |
623 | opr->type = OPT_OFFSET; |
624 | strcpy(opr->name, words[w + 1]); |
625 | return wordc; |
626 | } |
1bafb621 |
627 | } |
c36e914d |
628 | |
1bafb621 |
629 | if (wordc_in != 1) |
850c9265 |
630 | aerr("parse_operand 1 word expected\n"); |
c36e914d |
631 | |
39b168b8 |
632 | tmp = words[w]; |
633 | if (IS_START(tmp, "ds:")) |
634 | tmp += 3; |
635 | strcpy(opr->name, tmp); |
c36e914d |
636 | |
850c9265 |
637 | if (words[w][0] == '[') { |
638 | opr->type = OPT_REGMEM; |
639 | ret = sscanf(words[w], "[%[^]]]", opr->name); |
640 | if (ret != 1) |
641 | aerr("[] parse failure\n"); |
87bf6cec |
642 | |
5101a5f9 |
643 | parse_indmode(opr->name, regmask_indirect, 1); |
4c45fa73 |
644 | if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL)) { |
87bf6cec |
645 | // might be an equ |
d4e3b5db |
646 | struct parsed_equ *eq = |
4c45fa73 |
647 | equ_find(NULL, parse_stack_el(opr->name, NULL), &i); |
87bf6cec |
648 | if (eq) |
649 | opr->lmod = eq->lmod; |
650 | } |
850c9265 |
651 | return wordc; |
652 | } |
653 | else if (strchr(words[w], '[')) { |
654 | // label[reg] form |
655 | opr->type = OPT_REGMEM; |
656 | if (opr->lmod == OPLM_UNSPEC) |
657 | guess_lmod_from_name(opr); |
5101a5f9 |
658 | parse_indmode(strchr(words[w], '['), regmask_indirect, 0); |
850c9265 |
659 | return wordc; |
660 | } |
661 | else if (('0' <= words[w][0] && words[w][0] <= '9') |
662 | || words[w][0] == '-') |
663 | { |
5101a5f9 |
664 | number = parse_number(words[w]); |
91977a1c |
665 | opr->type = OPT_CONST; |
5101a5f9 |
666 | opr->val = number; |
667 | printf_number(opr->name, sizeof(opr->name), number); |
91977a1c |
668 | return wordc; |
850c9265 |
669 | } |
c36e914d |
670 | |
5101a5f9 |
671 | ret = parse_reg(&tmplmod, opr->name); |
672 | if (ret >= 0) { |
673 | setup_reg_opr(opr, ret, tmplmod, regmask); |
850c9265 |
674 | return wordc; |
675 | } |
676 | |
677 | // most likely var in data segment |
678 | opr->type = OPT_LABEL; |
06c5d854 |
679 | |
bd96f656 |
680 | pp = proto_parse(g_fhdr, opr->name); |
681 | if (pp != NULL) { |
682 | if (pp->is_fptr) { |
06c5d854 |
683 | opr->lmod = OPLM_DWORD; |
684 | opr->is_ptr = 1; |
685 | } |
2b43685d |
686 | else { |
840257f6 |
687 | tmplmod = OPLM_UNSPEC; |
bd96f656 |
688 | if (!guess_lmod_from_c_type(&tmplmod, &pp->type)) |
2b43685d |
689 | anote("unhandled C type '%s' for '%s'\n", |
bd96f656 |
690 | pp->type.name, opr->name); |
2b43685d |
691 | |
a3684be1 |
692 | if (opr->lmod == OPLM_UNSPEC) { |
2b43685d |
693 | opr->lmod = tmplmod; |
a3684be1 |
694 | opr->type_from_var = 1; |
695 | } |
2b43685d |
696 | else if (opr->lmod != tmplmod) { |
697 | opr->size_mismatch = 1; |
698 | if (tmplmod < opr->lmod) |
699 | opr->size_lt = 1; |
700 | } |
a652aa9f |
701 | opr->is_ptr = pp->type.is_ptr; |
3ebea2cf |
702 | } |
bd96f656 |
703 | opr->is_array = pp->type.is_array; |
06c5d854 |
704 | } |
705 | |
850c9265 |
706 | if (opr->lmod == OPLM_UNSPEC) |
707 | guess_lmod_from_name(opr); |
850c9265 |
708 | return wordc; |
c36e914d |
709 | } |
710 | |
33c35af6 |
711 | static const struct { |
712 | const char *name; |
713 | unsigned int flags; |
714 | } pref_table[] = { |
715 | { "rep", OPF_REP }, |
7ba45c34 |
716 | { "repe", OPF_REP|OPF_REPZ }, |
717 | { "repz", OPF_REP|OPF_REPZ }, |
718 | { "repne", OPF_REP|OPF_REPNZ }, |
719 | { "repnz", OPF_REP|OPF_REPNZ }, |
33c35af6 |
720 | }; |
721 | |
c36e914d |
722 | static const struct { |
850c9265 |
723 | const char *name; |
724 | enum op_op op; |
69a3cdfc |
725 | unsigned int minopr; |
726 | unsigned int maxopr; |
727 | unsigned int flags; |
c36e914d |
728 | } op_table[] = { |
33c35af6 |
729 | { "nop", OP_NOP, 0, 0, 0 }, |
69a3cdfc |
730 | { "push", OP_PUSH, 1, 1, 0 }, |
731 | { "pop", OP_POP, 1, 1, OPF_DATA }, |
732 | { "mov" , OP_MOV, 2, 2, OPF_DATA }, |
733 | { "lea", OP_LEA, 2, 2, OPF_DATA }, |
734 | { "movzx",OP_MOVZX, 2, 2, OPF_DATA }, |
735 | { "movsx",OP_MOVSX, 2, 2, OPF_DATA }, |
736 | { "not", OP_NOT, 1, 1, OPF_DATA }, |
5101a5f9 |
737 | { "cdq", OP_CDQ, 0, 0, OPF_DATA }, |
33c35af6 |
738 | { "stosb",OP_STOS, 0, 0, OPF_DATA }, |
739 | { "stosw",OP_STOS, 0, 0, OPF_DATA }, |
740 | { "stosd",OP_STOS, 0, 0, OPF_DATA }, |
d4e3b5db |
741 | { "movsb",OP_MOVS, 0, 0, OPF_DATA }, |
742 | { "movsw",OP_MOVS, 0, 0, OPF_DATA }, |
743 | { "movsd",OP_MOVS, 0, 0, OPF_DATA }, |
7ba45c34 |
744 | { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS }, |
745 | { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS }, |
746 | { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS }, |
69a3cdfc |
747 | { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS }, |
748 | { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS }, |
749 | { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS }, |
750 | { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS }, |
751 | { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS }, |
752 | { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS }, |
753 | { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS }, |
754 | { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS }, |
755 | { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS }, |
d4e3b5db |
756 | { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS }, |
757 | { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS }, |
5101a5f9 |
758 | { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC }, |
69a3cdfc |
759 | { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC }, |
760 | { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS }, |
761 | { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS }, |
5101a5f9 |
762 | { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS }, |
763 | { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS }, |
69a3cdfc |
764 | { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS }, |
5101a5f9 |
765 | { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS }, |
766 | { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS }, |
69a3cdfc |
767 | { "test", OP_TEST, 2, 2, OPF_FLAGS }, |
768 | { "cmp", OP_CMP, 2, 2, OPF_FLAGS }, |
a3684be1 |
769 | { "retn", OP_RET, 0, 1, OPF_TAIL }, |
de50b98b |
770 | { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS }, |
69a3cdfc |
771 | { "jmp", OP_JMP, 1, 1, OPF_JMP }, |
772 | { "jo", OP_JO, 1, 1, OPF_JMP|OPF_CC }, // 70 OF=1 |
773 | { "jno", OP_JNO, 1, 1, OPF_JMP|OPF_CC }, // 71 OF=0 |
774 | { "jc", OP_JC, 1, 1, OPF_JMP|OPF_CC }, // 72 CF=1 |
775 | { "jb", OP_JC, 1, 1, OPF_JMP|OPF_CC }, // 72 |
776 | { "jnc", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73 CF=0 |
33c35af6 |
777 | { "jnb", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73 |
69a3cdfc |
778 | { "jae", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73 |
779 | { "jz", OP_JZ, 1, 1, OPF_JMP|OPF_CC }, // 74 ZF=1 |
780 | { "je", OP_JZ, 1, 1, OPF_JMP|OPF_CC }, // 74 |
781 | { "jnz", OP_JNZ, 1, 1, OPF_JMP|OPF_CC }, // 75 ZF=0 |
782 | { "jne", OP_JNZ, 1, 1, OPF_JMP|OPF_CC }, // 75 |
783 | { "jbe", OP_JBE, 1, 1, OPF_JMP|OPF_CC }, // 76 CF=1 || ZF=1 |
784 | { "jna", OP_JBE, 1, 1, OPF_JMP|OPF_CC }, // 76 |
785 | { "ja", OP_JA, 1, 1, OPF_JMP|OPF_CC }, // 77 CF=0 && ZF=0 |
786 | { "jnbe", OP_JA, 1, 1, OPF_JMP|OPF_CC }, // 77 |
787 | { "js", OP_JS, 1, 1, OPF_JMP|OPF_CC }, // 78 SF=1 |
788 | { "jns", OP_JNS, 1, 1, OPF_JMP|OPF_CC }, // 79 SF=0 |
789 | { "jp", OP_JP, 1, 1, OPF_JMP|OPF_CC }, // 7a PF=1 |
790 | { "jpe", OP_JP, 1, 1, OPF_JMP|OPF_CC }, // 7a |
791 | { "jnp", OP_JNP, 1, 1, OPF_JMP|OPF_CC }, // 7b PF=0 |
792 | { "jpo", OP_JNP, 1, 1, OPF_JMP|OPF_CC }, // 7b |
793 | { "jl", OP_JL, 1, 1, OPF_JMP|OPF_CC }, // 7c SF!=OF |
794 | { "jnge", OP_JL, 1, 1, OPF_JMP|OPF_CC }, // 7c |
795 | { "jge", OP_JGE, 1, 1, OPF_JMP|OPF_CC }, // 7d SF=OF |
796 | { "jnl", OP_JGE, 1, 1, OPF_JMP|OPF_CC }, // 7d |
797 | { "jle", OP_JLE, 1, 1, OPF_JMP|OPF_CC }, // 7e ZF=1 || SF!=OF |
798 | { "jng", OP_JLE, 1, 1, OPF_JMP|OPF_CC }, // 7e |
799 | { "jg", OP_JG, 1, 1, OPF_JMP|OPF_CC }, // 7f ZF=0 && SF=OF |
800 | { "jnle", OP_JG, 1, 1, OPF_JMP|OPF_CC }, // 7f |
5101a5f9 |
801 | { "seto", OP_JO, 1, 1, OPF_DATA|OPF_CC }, |
802 | { "setno", OP_JNO, 1, 1, OPF_DATA|OPF_CC }, |
803 | { "setc", OP_JC, 1, 1, OPF_DATA|OPF_CC }, |
804 | { "setb", OP_JC, 1, 1, OPF_DATA|OPF_CC }, |
805 | { "setnc", OP_JNC, 1, 1, OPF_DATA|OPF_CC }, |
806 | { "setae", OP_JNC, 1, 1, OPF_DATA|OPF_CC }, |
807 | { "setz", OP_JZ, 1, 1, OPF_DATA|OPF_CC }, |
808 | { "sete", OP_JZ, 1, 1, OPF_DATA|OPF_CC }, |
809 | { "setnz", OP_JNZ, 1, 1, OPF_DATA|OPF_CC }, |
810 | { "setne", OP_JNZ, 1, 1, OPF_DATA|OPF_CC }, |
811 | { "setbe", OP_JBE, 1, 1, OPF_DATA|OPF_CC }, |
812 | { "setna", OP_JBE, 1, 1, OPF_DATA|OPF_CC }, |
813 | { "seta", OP_JA, 1, 1, OPF_DATA|OPF_CC }, |
814 | { "setnbe", OP_JA, 1, 1, OPF_DATA|OPF_CC }, |
815 | { "sets", OP_JS, 1, 1, OPF_DATA|OPF_CC }, |
816 | { "setns", OP_JNS, 1, 1, OPF_DATA|OPF_CC }, |
817 | { "setp", OP_JP, 1, 1, OPF_DATA|OPF_CC }, |
818 | { "setpe", OP_JP, 1, 1, OPF_DATA|OPF_CC }, |
819 | { "setnp", OP_JNP, 1, 1, OPF_DATA|OPF_CC }, |
820 | { "setpo", OP_JNP, 1, 1, OPF_DATA|OPF_CC }, |
821 | { "setl", OP_JL, 1, 1, OPF_DATA|OPF_CC }, |
822 | { "setnge", OP_JL, 1, 1, OPF_DATA|OPF_CC }, |
823 | { "setge", OP_JGE, 1, 1, OPF_DATA|OPF_CC }, |
824 | { "setnl", OP_JGE, 1, 1, OPF_DATA|OPF_CC }, |
825 | { "setle", OP_JLE, 1, 1, OPF_DATA|OPF_CC }, |
826 | { "setng", OP_JLE, 1, 1, OPF_DATA|OPF_CC }, |
827 | { "setg", OP_JG, 1, 1, OPF_DATA|OPF_CC }, |
828 | { "setnle", OP_JG, 1, 1, OPF_DATA|OPF_CC }, |
69a3cdfc |
829 | }; |
c36e914d |
830 | |
831 | static void parse_op(struct parsed_op *op, char words[16][256], int wordc) |
832 | { |
bfa4a6ee |
833 | enum opr_lenmod lmod = OPLM_UNSPEC; |
33c35af6 |
834 | int prefix_flags = 0; |
69a3cdfc |
835 | int regmask_ind; |
836 | int regmask; |
33c35af6 |
837 | int op_w = 0; |
91977a1c |
838 | int opr = 0; |
33c35af6 |
839 | int w = 0; |
91977a1c |
840 | int i; |
c36e914d |
841 | |
33c35af6 |
842 | for (i = 0; i < ARRAY_SIZE(pref_table); i++) { |
843 | if (IS(words[w], pref_table[i].name)) { |
844 | prefix_flags = pref_table[i].flags; |
845 | break; |
846 | } |
847 | } |
848 | |
849 | if (prefix_flags) { |
850 | if (wordc <= 1) |
851 | aerr("lone prefix: '%s'\n", words[0]); |
852 | w++; |
853 | } |
854 | |
855 | op_w = w; |
91977a1c |
856 | for (i = 0; i < ARRAY_SIZE(op_table); i++) { |
33c35af6 |
857 | if (IS(words[w], op_table[i].name)) |
69a3cdfc |
858 | break; |
859 | } |
c36e914d |
860 | |
69a3cdfc |
861 | if (i == ARRAY_SIZE(op_table)) |
862 | aerr("unhandled op: '%s'\n", words[0]); |
33c35af6 |
863 | w++; |
c36e914d |
864 | |
69a3cdfc |
865 | op->op = op_table[i].op; |
33c35af6 |
866 | op->flags = op_table[i].flags | prefix_flags; |
69a3cdfc |
867 | op->regmask_src = op->regmask_dst = 0; |
868 | |
869 | for (opr = 0; opr < op_table[i].minopr; opr++) { |
870 | regmask = regmask_ind = 0; |
871 | w = parse_operand(&op->operand[opr], ®mask, ®mask_ind, |
872 | words, wordc, w, op->flags); |
873 | |
874 | if (opr == 0 && (op->flags & OPF_DATA)) |
875 | op->regmask_dst = regmask; |
876 | // for now, mark dst as src too |
877 | op->regmask_src |= regmask | regmask_ind; |
878 | } |
c36e914d |
879 | |
69a3cdfc |
880 | for (; w < wordc && opr < op_table[i].maxopr; opr++) { |
881 | w = parse_operand(&op->operand[opr], |
882 | &op->regmask_src, &op->regmask_src, |
883 | words, wordc, w, op->flags); |
91977a1c |
884 | } |
c36e914d |
885 | |
91977a1c |
886 | if (w < wordc) |
887 | aerr("parse_op %s incomplete: %d/%d\n", |
888 | words[0], w, wordc); |
5101a5f9 |
889 | |
890 | // special cases |
891 | op->operand_cnt = opr; |
892 | if (!strncmp(op_table[i].name, "set", 3)) |
893 | op->operand[0].lmod = OPLM_BYTE; |
894 | |
895 | // ops with implicit argumets |
896 | switch (op->op) { |
897 | case OP_CDQ: |
898 | op->operand_cnt = 2; |
899 | setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst); |
900 | setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src); |
901 | break; |
902 | |
33c35af6 |
903 | case OP_STOS: |
904 | if (op->operand_cnt != 0) |
905 | break; |
906 | if (IS(words[op_w], "stosb")) |
907 | lmod = OPLM_BYTE; |
908 | else if (IS(words[op_w], "stosw")) |
909 | lmod = OPLM_WORD; |
910 | else if (IS(words[op_w], "stosd")) |
911 | lmod = OPLM_DWORD; |
912 | op->operand_cnt = 3; |
d4e3b5db |
913 | setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src); |
914 | setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src); |
915 | op->regmask_dst = op->regmask_src; |
33c35af6 |
916 | setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD, &op->regmask_src); |
917 | break; |
918 | |
d4e3b5db |
919 | case OP_MOVS: |
7ba45c34 |
920 | case OP_CMPS: |
d4e3b5db |
921 | if (op->operand_cnt != 0) |
922 | break; |
7ba45c34 |
923 | if (words[op_w][4] == 'b') |
d4e3b5db |
924 | lmod = OPLM_BYTE; |
7ba45c34 |
925 | else if (words[op_w][4] == 'w') |
d4e3b5db |
926 | lmod = OPLM_WORD; |
7ba45c34 |
927 | else if (words[op_w][4] == 'd') |
d4e3b5db |
928 | lmod = OPLM_DWORD; |
929 | op->operand_cnt = 3; |
930 | setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src); |
931 | setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src); |
932 | setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src); |
933 | op->regmask_dst = op->regmask_src; |
934 | break; |
935 | |
5101a5f9 |
936 | case OP_IMUL: |
937 | if (op->operand_cnt != 1) |
938 | break; |
939 | // fallthrough |
940 | case OP_MUL: |
941 | // singleop mul |
942 | op->regmask_dst = (1 << xDX) | (1 << xAX); |
943 | op->regmask_src |= (1 << xAX); |
944 | if (op->operand[0].lmod == OPLM_UNSPEC) |
945 | op->operand[0].lmod = OPLM_DWORD; |
946 | break; |
947 | |
948 | case OP_DIV: |
949 | case OP_IDIV: |
950 | // we could set up operands for edx:eax, but there is no real need to |
951 | // (see is_opr_modified()) |
952 | regmask = (1 << xDX) | (1 << xAX); |
953 | op->regmask_dst = regmask; |
954 | op->regmask_src |= regmask; |
955 | if (op->operand[0].lmod == OPLM_UNSPEC) |
956 | op->operand[0].lmod = OPLM_DWORD; |
957 | break; |
958 | |
959 | case OP_SHL: |
960 | case OP_SHR: |
961 | case OP_SAR: |
d4e3b5db |
962 | case OP_ROL: |
963 | case OP_ROR: |
5101a5f9 |
964 | if (op->operand[1].lmod == OPLM_UNSPEC) |
965 | op->operand[1].lmod = OPLM_BYTE; |
966 | break; |
967 | |
7ba45c34 |
968 | case OP_PUSH: |
969 | if (op->operand[0].lmod == OPLM_UNSPEC |
970 | && (op->operand[0].type == OPT_CONST |
971 | || op->operand[0].type == OPT_OFFSET |
972 | || op->operand[0].type == OPT_LABEL)) |
973 | op->operand[0].lmod = OPLM_DWORD; |
974 | break; |
975 | |
3ebea2cf |
976 | // alignment |
977 | case OP_MOV: |
978 | if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG |
979 | && op->operand[0].reg == xDI && op->operand[1].reg == xDI) |
980 | { |
981 | op->flags |= OPF_RMD; |
982 | } |
983 | break; |
984 | |
de50b98b |
985 | case OP_LEA: |
986 | if (op->operand[0].type == OPT_REG |
987 | && op->operand[1].type == OPT_REGMEM) |
988 | { |
989 | char buf[16]; |
990 | snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name); |
991 | if (IS(buf, op->operand[1].name)) |
992 | op->flags |= OPF_RMD; |
993 | } |
994 | break; |
995 | |
5101a5f9 |
996 | default: |
997 | break; |
998 | } |
c36e914d |
999 | } |
1000 | |
850c9265 |
1001 | static const char *op_name(enum op_op op) |
1002 | { |
1003 | int i; |
1004 | |
1005 | for (i = 0; i < ARRAY_SIZE(op_table); i++) |
1006 | if (op_table[i].op == op) |
1007 | return op_table[i].name; |
1008 | |
1009 | return "???"; |
1010 | } |
1011 | |
1012 | // debug |
1013 | static const char *dump_op(struct parsed_op *po) |
1014 | { |
1015 | static char out[128]; |
1016 | char *p = out; |
1017 | int i; |
1018 | |
4c45fa73 |
1019 | if (po == NULL) |
1020 | return "???"; |
1021 | |
850c9265 |
1022 | snprintf(out, sizeof(out), "%s", op_name(po->op)); |
1023 | for (i = 0; i < po->operand_cnt; i++) { |
1024 | p += strlen(p); |
1025 | if (i > 0) |
1026 | *p++ = ','; |
1027 | snprintf(p, sizeof(out) - (p - out), |
1028 | po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s", |
1029 | po->operand[i].name); |
1030 | } |
1031 | |
1032 | return out; |
1033 | } |
1034 | |
4c45fa73 |
1035 | static const char *lmod_type_u(struct parsed_op *po, |
1036 | enum opr_lenmod lmod) |
1037 | { |
1038 | switch (lmod) { |
1039 | case OPLM_DWORD: |
1040 | return "u32"; |
1041 | case OPLM_WORD: |
1042 | return "u16"; |
1043 | case OPLM_BYTE: |
1044 | return "u8"; |
1045 | default: |
1046 | ferr(po, "invalid lmod: %d\n", lmod); |
1047 | return "(_invalid_)"; |
1048 | } |
1049 | } |
1050 | |
d4e3b5db |
1051 | static const char *lmod_cast_u(struct parsed_op *po, |
1052 | enum opr_lenmod lmod) |
1053 | { |
1054 | switch (lmod) { |
1055 | case OPLM_DWORD: |
1056 | return ""; |
1057 | case OPLM_WORD: |
1058 | return "(u16)"; |
1059 | case OPLM_BYTE: |
1060 | return "(u8)"; |
1061 | default: |
1062 | ferr(po, "invalid lmod: %d\n", lmod); |
1063 | return "(_invalid_)"; |
1064 | } |
1065 | } |
1066 | |
1067 | static const char *lmod_cast_u_ptr(struct parsed_op *po, |
1068 | enum opr_lenmod lmod) |
1069 | { |
1070 | switch (lmod) { |
1071 | case OPLM_DWORD: |
1072 | return "*(u32 *)"; |
1073 | case OPLM_WORD: |
1074 | return "*(u16 *)"; |
1075 | case OPLM_BYTE: |
1076 | return "*(u8 *)"; |
1077 | default: |
1078 | ferr(po, "invalid lmod: %d\n", lmod); |
1079 | return "(_invalid_)"; |
1080 | } |
1081 | } |
1082 | |
1083 | static const char *lmod_cast_s(struct parsed_op *po, |
1084 | enum opr_lenmod lmod) |
1085 | { |
1086 | switch (lmod) { |
1087 | case OPLM_DWORD: |
1088 | return "(s32)"; |
1089 | case OPLM_WORD: |
1090 | return "(s16)"; |
1091 | case OPLM_BYTE: |
1092 | return "(s8)"; |
1093 | default: |
1094 | ferr(po, "%s: invalid lmod: %d\n", __func__, lmod); |
1095 | return "(_invalid_)"; |
1096 | } |
1097 | } |
1098 | |
1099 | static const char *lmod_cast(struct parsed_op *po, |
1100 | enum opr_lenmod lmod, int is_signed) |
1101 | { |
1102 | return is_signed ? |
1103 | lmod_cast_s(po, lmod) : |
1104 | lmod_cast_u(po, lmod); |
1105 | } |
1106 | |
1107 | static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod) |
1108 | { |
1109 | switch (lmod) { |
1110 | case OPLM_DWORD: |
1111 | return 4; |
1112 | case OPLM_WORD: |
1113 | return 2; |
1114 | case OPLM_BYTE: |
1115 | return 1; |
1116 | default: |
1117 | ferr(po, "%s: invalid lmod: %d\n", __func__, lmod); |
1118 | return 0; |
1119 | } |
1120 | } |
1121 | |
91977a1c |
1122 | static const char *opr_name(struct parsed_op *po, int opr_num) |
c36e914d |
1123 | { |
91977a1c |
1124 | if (opr_num >= po->operand_cnt) |
1125 | ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt); |
1126 | return po->operand[opr_num].name; |
c36e914d |
1127 | } |
1128 | |
91977a1c |
1129 | static unsigned int opr_const(struct parsed_op *po, int opr_num) |
c36e914d |
1130 | { |
91977a1c |
1131 | if (opr_num >= po->operand_cnt) |
1132 | ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt); |
1133 | if (po->operand[opr_num].type != OPT_CONST) |
1134 | ferr(po, "opr %d: const expected\n", opr_num); |
1135 | return po->operand[opr_num].val; |
1136 | } |
c36e914d |
1137 | |
91977a1c |
1138 | static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr) |
1139 | { |
1140 | if ((unsigned int)popr->reg >= MAX_REGS) |
1141 | ferr(po, "invalid reg: %d\n", popr->reg); |
1142 | return regs_r32[popr->reg]; |
1143 | } |
c36e914d |
1144 | |
39b168b8 |
1145 | static struct parsed_equ *equ_find(struct parsed_op *po, const char *name, |
1146 | int *extra_offs) |
91977a1c |
1147 | { |
39b168b8 |
1148 | const char *p; |
1149 | char *endp; |
1150 | int namelen; |
850c9265 |
1151 | int i; |
1152 | |
39b168b8 |
1153 | *extra_offs = 0; |
1154 | namelen = strlen(name); |
1155 | |
1156 | p = strchr(name, '+'); |
1157 | if (p != NULL) { |
1158 | namelen = p - name; |
1159 | if (namelen <= 0) |
1160 | ferr(po, "equ parse failed for '%s'\n", name); |
1161 | |
1162 | if (IS_START(p, "0x")) |
1163 | p += 2; |
1164 | *extra_offs = strtol(p, &endp, 16); |
1165 | if (*endp != 0) |
1166 | ferr(po, "equ parse failed for '%s'\n", name); |
1167 | } |
1168 | |
850c9265 |
1169 | for (i = 0; i < g_eqcnt; i++) |
39b168b8 |
1170 | if (strncmp(g_eqs[i].name, name, namelen) == 0 |
1171 | && g_eqs[i].name[namelen] == 0) |
850c9265 |
1172 | break; |
87bf6cec |
1173 | if (i >= g_eqcnt) { |
1174 | if (po != NULL) |
1175 | ferr(po, "unresolved equ name: '%s'\n", name); |
1176 | return NULL; |
1177 | } |
850c9265 |
1178 | |
1179 | return &g_eqs[i]; |
1180 | } |
1181 | |
1bafb621 |
1182 | static void stack_frame_access(struct parsed_op *po, |
de50b98b |
1183 | struct parsed_opr *popr, char *buf, size_t buf_size, |
3ebea2cf |
1184 | const char *name, const char *cast, int is_src, int is_lea) |
850c9265 |
1185 | { |
3ebea2cf |
1186 | enum opr_lenmod tmp_lmod = OPLM_UNSPEC; |
850c9265 |
1187 | const char *prefix = ""; |
4f12f671 |
1188 | const char *bp_arg = NULL; |
4c45fa73 |
1189 | char ofs_reg[16] = { 0, }; |
91977a1c |
1190 | struct parsed_equ *eq; |
4f12f671 |
1191 | const char *p; |
1192 | char *endp = NULL; |
91977a1c |
1193 | int i, arg_i, arg_s; |
a3684be1 |
1194 | int unaligned = 0; |
d4e3b5db |
1195 | int stack_ra = 0; |
39b168b8 |
1196 | int offset = 0; |
850c9265 |
1197 | int sf_ofs; |
de50b98b |
1198 | int lim; |
91977a1c |
1199 | |
a3684be1 |
1200 | if (po->flags & OPF_EBP_S) |
1201 | ferr(po, "stack_frame_access while ebp is scratch\n"); |
1202 | |
1203 | if (IS_START(name, "ebp-") |
1204 | || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9')) |
1205 | { |
1206 | p = name + 4; |
1207 | if (IS_START(p, "0x")) |
1208 | p += 2; |
1209 | offset = strtoul(p, &endp, 16); |
1210 | if (name[3] == '-') |
1211 | offset = -offset; |
1212 | if (*endp != 0) |
1213 | ferr(po, "ebp- parse of '%s' failed\n", name); |
1214 | } |
1215 | else { |
4f12f671 |
1216 | bp_arg = parse_stack_el(name, ofs_reg); |
1217 | snprintf(g_comment, sizeof(g_comment), "%s", bp_arg); |
1218 | eq = equ_find(po, bp_arg, &offset); |
1219 | if (eq == NULL) |
1220 | ferr(po, "detected but missing eq\n"); |
1221 | offset += eq->offset; |
1222 | } |
39b168b8 |
1223 | |
d4e3b5db |
1224 | if (!strncmp(name, "ebp", 3)) |
1225 | stack_ra = 4; |
1226 | |
39b168b8 |
1227 | if (stack_ra <= offset && offset < stack_ra + 4) |
1228 | ferr(po, "reference to ra? %d %d\n", offset, stack_ra); |
d4e3b5db |
1229 | |
4f12f671 |
1230 | if (offset > stack_ra) |
1231 | { |
39b168b8 |
1232 | arg_i = (offset - stack_ra - 4) / 4; |
bd96f656 |
1233 | if (arg_i < 0 || arg_i >= g_func_pp->argc_stack) |
4f12f671 |
1234 | { |
bd96f656 |
1235 | if (g_func_pp->is_vararg |
1236 | && arg_i == g_func_pp->argc_stack && is_lea) |
1237 | { |
4f12f671 |
1238 | // should be va_list |
1239 | if (cast[0] == 0) |
1240 | cast = "(u32)"; |
1241 | snprintf(buf, buf_size, "%sap", cast); |
1242 | return; |
1243 | } |
1244 | ferr(po, "offset %d (%s,%d) doesn't map to any arg\n", |
1245 | offset, bp_arg, arg_i); |
1246 | } |
4c45fa73 |
1247 | if (ofs_reg[0] != 0) |
7ba45c34 |
1248 | ferr(po, "offset reg on arg access?\n"); |
91977a1c |
1249 | |
bd96f656 |
1250 | for (i = arg_s = 0; i < g_func_pp->argc; i++) { |
1251 | if (g_func_pp->arg[i].reg != NULL) |
91977a1c |
1252 | continue; |
1253 | if (arg_s == arg_i) |
1254 | break; |
1255 | arg_s++; |
1256 | } |
bd96f656 |
1257 | if (i == g_func_pp->argc) |
91977a1c |
1258 | ferr(po, "arg %d not in prototype?\n", arg_i); |
850c9265 |
1259 | |
bd96f656 |
1260 | popr->is_ptr = g_func_pp->arg[i].type.is_ptr; |
de50b98b |
1261 | |
1262 | switch (popr->lmod) |
7ba45c34 |
1263 | { |
1264 | case OPLM_BYTE: |
4f12f671 |
1265 | if (is_lea) |
1266 | ferr(po, "lea/byte to arg?\n"); |
7ba45c34 |
1267 | if (is_src && (offset & 3) == 0) |
1268 | snprintf(buf, buf_size, "(u8)a%d", i + 1); |
1269 | else |
1270 | snprintf(buf, buf_size, "BYTE%d(a%d)", offset & 3, i + 1); |
1271 | break; |
1272 | |
1273 | case OPLM_WORD: |
4f12f671 |
1274 | if (is_lea) |
1275 | ferr(po, "lea/word to arg?\n"); |
a3684be1 |
1276 | if (offset & 1) { |
1277 | unaligned = 1; |
1278 | if (!is_src) { |
1279 | if (offset & 2) |
1280 | ferr(po, "problematic arg store\n"); |
1281 | snprintf(buf, buf_size, "*(u16 *)((char *)&a%d + 1)", i + 1); |
1282 | } |
1283 | else |
1284 | ferr(po, "unaligned arg word load\n"); |
1285 | } |
1286 | else if (is_src && (offset & 2) == 0) |
7ba45c34 |
1287 | snprintf(buf, buf_size, "(u16)a%d", i + 1); |
1288 | else |
1289 | snprintf(buf, buf_size, "%sWORD(a%d)", |
1290 | (offset & 2) ? "HI" : "LO", i + 1); |
1291 | break; |
1292 | |
1293 | case OPLM_DWORD: |
3ebea2cf |
1294 | if (cast[0]) |
1295 | prefix = cast; |
1296 | else if (is_src) |
1297 | prefix = "(u32)"; |
a3684be1 |
1298 | |
de50b98b |
1299 | if (offset & 3) { |
a3684be1 |
1300 | unaligned = 1; |
2b43685d |
1301 | if (is_lea) |
1302 | snprintf(buf, buf_size, "(u32)&a%d + %d", |
1303 | i + 1, offset & 3); |
a3684be1 |
1304 | else if (!is_src) |
1305 | ferr(po, "unaligned arg store\n"); |
1306 | else { |
1307 | // mov edx, [ebp+arg_4+2]; movsx ecx, dx |
2b43685d |
1308 | snprintf(buf, buf_size, "%s(a%d >> %d)", |
1309 | prefix, i + 1, (offset & 3) * 8); |
a3684be1 |
1310 | } |
de50b98b |
1311 | } |
1312 | else { |
1313 | snprintf(buf, buf_size, "%s%sa%d", |
1314 | prefix, is_lea ? "&" : "", i + 1); |
1315 | } |
7ba45c34 |
1316 | break; |
1317 | |
1318 | default: |
de50b98b |
1319 | ferr(po, "bp_arg bad lmod: %d\n", popr->lmod); |
7ba45c34 |
1320 | } |
1321 | |
a3684be1 |
1322 | if (unaligned) |
1323 | snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg); |
1324 | |
7ba45c34 |
1325 | // common problem |
bd96f656 |
1326 | guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type); |
a3684be1 |
1327 | if (unaligned && tmp_lmod != OPLM_DWORD) |
7ba45c34 |
1328 | ferr(po, "bp_arg arg/w offset %d and type '%s'\n", |
bd96f656 |
1329 | offset, g_func_pp->arg[i].type.name); |
91977a1c |
1330 | } |
4f12f671 |
1331 | else |
1332 | { |
1bafb621 |
1333 | if (g_stack_fsz == 0) |
1334 | ferr(po, "stack var access without stackframe\n"); |
850c9265 |
1335 | |
39b168b8 |
1336 | sf_ofs = g_stack_fsz + offset; |
de50b98b |
1337 | lim = (ofs_reg[0] != 0) ? -4 : 0; |
1338 | if (offset > 0 || sf_ofs < lim) |
39b168b8 |
1339 | ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz); |
850c9265 |
1340 | |
1341 | if (is_lea) |
33c35af6 |
1342 | prefix = "(u32)&"; |
3ebea2cf |
1343 | else |
1344 | prefix = cast; |
850c9265 |
1345 | |
de50b98b |
1346 | switch (popr->lmod) |
850c9265 |
1347 | { |
1348 | case OPLM_BYTE: |
7ba45c34 |
1349 | snprintf(buf, buf_size, "%ssf.b[%d%s%s]", |
1350 | prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg); |
850c9265 |
1351 | break; |
7ba45c34 |
1352 | |
850c9265 |
1353 | case OPLM_WORD: |
7ba45c34 |
1354 | if ((sf_ofs & 1) || ofs_reg[0] != 0) { |
1355 | // known unaligned or possibly unaligned |
1356 | strcat(g_comment, " unaligned"); |
1357 | if (prefix[0] == 0) |
1358 | prefix = "*(u16 *)&"; |
1359 | snprintf(buf, buf_size, "%ssf.b[%d%s%s]", |
1360 | prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg); |
1361 | break; |
1362 | } |
1363 | snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2); |
850c9265 |
1364 | break; |
7ba45c34 |
1365 | |
850c9265 |
1366 | case OPLM_DWORD: |
7ba45c34 |
1367 | if ((sf_ofs & 3) || ofs_reg[0] != 0) { |
1368 | // known unaligned or possibly unaligned |
1369 | strcat(g_comment, " unaligned"); |
1370 | if (prefix[0] == 0) |
1371 | prefix = "*(u32 *)&"; |
1372 | snprintf(buf, buf_size, "%ssf.b[%d%s%s]", |
1373 | prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg); |
1374 | break; |
1375 | } |
1376 | snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4); |
850c9265 |
1377 | break; |
7ba45c34 |
1378 | |
850c9265 |
1379 | default: |
de50b98b |
1380 | ferr(po, "bp_stack bad lmod: %d\n", popr->lmod); |
850c9265 |
1381 | } |
91977a1c |
1382 | } |
1383 | } |
c36e914d |
1384 | |
3ebea2cf |
1385 | static void check_label_read_ref(struct parsed_op *po, const char *name) |
1386 | { |
840257f6 |
1387 | const struct parsed_proto *pp; |
1388 | |
1389 | pp = proto_parse(g_fhdr, name); |
1390 | if (pp == NULL) |
1391 | ferr(po, "proto_parse failed for ref '%s'\n", name); |
1392 | |
1393 | // currently we can take __cdecl and __stdcall |
1394 | if (pp->is_func && pp->argc_reg != 0) |
1395 | ferr(po, "reg-arg func reference?\n"); |
3ebea2cf |
1396 | } |
1397 | |
91977a1c |
1398 | static char *out_src_opr(char *buf, size_t buf_size, |
3ebea2cf |
1399 | struct parsed_op *po, struct parsed_opr *popr, const char *cast, |
1400 | int is_lea) |
91977a1c |
1401 | { |
850c9265 |
1402 | char tmp1[256], tmp2[256]; |
1403 | char expr[256]; |
1404 | int ret; |
1405 | |
3ebea2cf |
1406 | if (cast == NULL) |
1407 | cast = ""; |
1408 | |
91977a1c |
1409 | switch (popr->type) { |
1410 | case OPT_REG: |
850c9265 |
1411 | if (is_lea) |
1412 | ferr(po, "lea from reg?\n"); |
1413 | |
91977a1c |
1414 | switch (popr->lmod) { |
1415 | case OPLM_DWORD: |
3ebea2cf |
1416 | snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr)); |
91977a1c |
1417 | break; |
850c9265 |
1418 | case OPLM_WORD: |
1419 | snprintf(buf, buf_size, "(u16)%s", opr_reg_p(po, popr)); |
1420 | break; |
1421 | case OPLM_BYTE: |
5101a5f9 |
1422 | if (popr->name[1] == 'h') // XXX.. |
1423 | snprintf(buf, buf_size, "(u8)(%s >> 8)", opr_reg_p(po, popr)); |
1424 | else |
1425 | snprintf(buf, buf_size, "(u8)%s", opr_reg_p(po, popr)); |
850c9265 |
1426 | break; |
91977a1c |
1427 | default: |
1428 | ferr(po, "invalid src lmod: %d\n", popr->lmod); |
1429 | } |
1430 | break; |
850c9265 |
1431 | |
91977a1c |
1432 | case OPT_REGMEM: |
4f12f671 |
1433 | if (parse_stack_el(popr->name, NULL) |
a3684be1 |
1434 | || (g_bp_frame && !(po->flags & OPF_EBP_S) |
1435 | && IS_START(popr->name, "ebp"))) |
4f12f671 |
1436 | { |
de50b98b |
1437 | stack_frame_access(po, popr, buf, buf_size, |
3ebea2cf |
1438 | popr->name, cast, 1, is_lea); |
91977a1c |
1439 | break; |
1440 | } |
850c9265 |
1441 | |
1442 | strcpy(expr, popr->name); |
1443 | if (strchr(expr, '[')) { |
1444 | // special case: '[' can only be left for label[reg] form |
1445 | ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2); |
1446 | if (ret != 2) |
1447 | ferr(po, "parse failure for '%s'\n", expr); |
33c35af6 |
1448 | snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2); |
850c9265 |
1449 | } |
1450 | |
1451 | // XXX: do we need more parsing? |
1452 | if (is_lea) { |
1453 | snprintf(buf, buf_size, "%s", expr); |
1454 | break; |
1455 | } |
1456 | |
3ebea2cf |
1457 | if (cast[0] == 0) |
1458 | cast = lmod_cast_u_ptr(po, popr->lmod); |
1459 | snprintf(buf, buf_size, "%s(%s)", cast, expr); |
91977a1c |
1460 | break; |
850c9265 |
1461 | |
91977a1c |
1462 | case OPT_LABEL: |
3ebea2cf |
1463 | check_label_read_ref(po, popr->name); |
1464 | if (cast[0] == 0 && popr->is_ptr) |
1465 | cast = "(u32)"; |
2b43685d |
1466 | |
850c9265 |
1467 | if (is_lea) |
1468 | snprintf(buf, buf_size, "(u32)&%s", popr->name); |
2b43685d |
1469 | else if (popr->size_lt) |
1470 | snprintf(buf, buf_size, "%s%s%s%s", cast, |
1471 | lmod_cast_u_ptr(po, popr->lmod), |
1472 | popr->is_array ? "" : "&", |
1473 | popr->name); |
850c9265 |
1474 | else |
3ebea2cf |
1475 | snprintf(buf, buf_size, "%s%s%s", cast, popr->name, |
7ba45c34 |
1476 | popr->is_array ? "[0]" : ""); |
850c9265 |
1477 | break; |
1478 | |
1479 | case OPT_OFFSET: |
3ebea2cf |
1480 | check_label_read_ref(po, popr->name); |
1481 | if (cast[0] == 0) |
1482 | cast = "(u32)"; |
850c9265 |
1483 | if (is_lea) |
1484 | ferr(po, "lea an offset?\n"); |
3ebea2cf |
1485 | snprintf(buf, buf_size, "%s&%s", cast, popr->name); |
91977a1c |
1486 | break; |
850c9265 |
1487 | |
91977a1c |
1488 | case OPT_CONST: |
850c9265 |
1489 | if (is_lea) |
1490 | ferr(po, "lea from const?\n"); |
1491 | |
3ebea2cf |
1492 | snprintf(buf, buf_size, "%s", cast); |
1493 | ret = strlen(buf); |
1494 | printf_number(buf + ret, buf_size - ret, popr->val); |
91977a1c |
1495 | break; |
850c9265 |
1496 | |
91977a1c |
1497 | default: |
1498 | ferr(po, "invalid src type: %d\n", popr->type); |
1499 | } |
1500 | |
1501 | return buf; |
1502 | } |
c36e914d |
1503 | |
de50b98b |
1504 | // note: may set is_ptr (we find that out late for ebp frame..) |
91977a1c |
1505 | static char *out_dst_opr(char *buf, size_t buf_size, |
1506 | struct parsed_op *po, struct parsed_opr *popr) |
1507 | { |
1508 | switch (popr->type) { |
1509 | case OPT_REG: |
1510 | switch (popr->lmod) { |
1511 | case OPLM_DWORD: |
1512 | snprintf(buf, buf_size, "%s", opr_reg_p(po, popr)); |
1513 | break; |
850c9265 |
1514 | case OPLM_WORD: |
1515 | // ugh.. |
1516 | snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr)); |
1517 | break; |
1518 | case OPLM_BYTE: |
1519 | // ugh.. |
5101a5f9 |
1520 | if (popr->name[1] == 'h') // XXX.. |
1521 | snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr)); |
1522 | else |
1523 | snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr)); |
850c9265 |
1524 | break; |
91977a1c |
1525 | default: |
1526 | ferr(po, "invalid dst lmod: %d\n", popr->lmod); |
1527 | } |
1528 | break; |
850c9265 |
1529 | |
1530 | case OPT_REGMEM: |
4f12f671 |
1531 | if (parse_stack_el(popr->name, NULL) |
a3684be1 |
1532 | || (g_bp_frame && !(po->flags & OPF_EBP_S) |
1533 | && IS_START(popr->name, "ebp"))) |
4f12f671 |
1534 | { |
de50b98b |
1535 | stack_frame_access(po, popr, buf, buf_size, |
3ebea2cf |
1536 | popr->name, "", 0, 0); |
850c9265 |
1537 | break; |
1538 | } |
1539 | |
3ebea2cf |
1540 | return out_src_opr(buf, buf_size, po, popr, NULL, 0); |
850c9265 |
1541 | |
bfa4a6ee |
1542 | case OPT_LABEL: |
2b43685d |
1543 | if (popr->size_mismatch) |
1544 | snprintf(buf, buf_size, "%s%s%s", |
1545 | lmod_cast_u_ptr(po, popr->lmod), |
1546 | popr->is_array ? "" : "&", popr->name); |
1547 | else |
1548 | snprintf(buf, buf_size, "%s%s", popr->name, |
1549 | popr->is_array ? "[0]" : ""); |
bfa4a6ee |
1550 | break; |
1551 | |
91977a1c |
1552 | default: |
1553 | ferr(po, "invalid dst type: %d\n", popr->type); |
1554 | } |
1555 | |
1556 | return buf; |
1557 | } |
c36e914d |
1558 | |
3ebea2cf |
1559 | static char *out_src_opr_u32(char *buf, size_t buf_size, |
1560 | struct parsed_op *po, struct parsed_opr *popr) |
1561 | { |
1562 | return out_src_opr(buf, buf_size, po, popr, NULL, 0); |
1563 | } |
1564 | |
69a3cdfc |
1565 | static enum parsed_flag_op split_cond(struct parsed_op *po, |
940e8e66 |
1566 | enum op_op op, int *is_inv) |
91977a1c |
1567 | { |
940e8e66 |
1568 | *is_inv = 0; |
91977a1c |
1569 | |
69a3cdfc |
1570 | switch (op) { |
1571 | case OP_JO: |
1572 | return PFO_O; |
1573 | case OP_JC: |
1574 | return PFO_C; |
1575 | case OP_JZ: |
1576 | return PFO_Z; |
1577 | case OP_JBE: |
1578 | return PFO_BE; |
1579 | case OP_JS: |
1580 | return PFO_S; |
1581 | case OP_JP: |
1582 | return PFO_P; |
1583 | case OP_JL: |
1584 | return PFO_L; |
1585 | case OP_JLE: |
1586 | return PFO_LE; |
1587 | |
91977a1c |
1588 | case OP_JNO: |
940e8e66 |
1589 | *is_inv = 1; |
69a3cdfc |
1590 | return PFO_O; |
91977a1c |
1591 | case OP_JNC: |
940e8e66 |
1592 | *is_inv = 1; |
69a3cdfc |
1593 | return PFO_C; |
91977a1c |
1594 | case OP_JNZ: |
940e8e66 |
1595 | *is_inv = 1; |
69a3cdfc |
1596 | return PFO_Z; |
1597 | case OP_JA: |
940e8e66 |
1598 | *is_inv = 1; |
69a3cdfc |
1599 | return PFO_BE; |
91977a1c |
1600 | case OP_JNS: |
940e8e66 |
1601 | *is_inv = 1; |
69a3cdfc |
1602 | return PFO_S; |
91977a1c |
1603 | case OP_JNP: |
940e8e66 |
1604 | *is_inv = 1; |
69a3cdfc |
1605 | return PFO_P; |
850c9265 |
1606 | case OP_JGE: |
940e8e66 |
1607 | *is_inv = 1; |
69a3cdfc |
1608 | return PFO_L; |
91977a1c |
1609 | case OP_JG: |
940e8e66 |
1610 | *is_inv = 1; |
69a3cdfc |
1611 | return PFO_LE; |
1612 | |
1613 | case OP_ADC: |
1614 | case OP_SBB: |
1615 | return PFO_C; |
1616 | |
91977a1c |
1617 | default: |
69a3cdfc |
1618 | ferr(po, "split_cond: bad op %d\n", op); |
1619 | return -1; |
91977a1c |
1620 | } |
1621 | } |
c36e914d |
1622 | |
91977a1c |
1623 | static void out_test_for_cc(char *buf, size_t buf_size, |
940e8e66 |
1624 | struct parsed_op *po, enum parsed_flag_op pfo, int is_inv, |
69a3cdfc |
1625 | enum opr_lenmod lmod, const char *expr) |
91977a1c |
1626 | { |
69a3cdfc |
1627 | const char *cast, *scast; |
91977a1c |
1628 | |
69a3cdfc |
1629 | cast = lmod_cast_u(po, lmod); |
1630 | scast = lmod_cast_s(po, lmod); |
1631 | |
1632 | switch (pfo) { |
1633 | case PFO_Z: |
87bf6cec |
1634 | case PFO_BE: // CF=1||ZF=1; CF=0 |
850c9265 |
1635 | snprintf(buf, buf_size, "(%s%s %s 0)", |
940e8e66 |
1636 | cast, expr, is_inv ? "!=" : "=="); |
91977a1c |
1637 | break; |
850c9265 |
1638 | |
5101a5f9 |
1639 | case PFO_S: |
1640 | case PFO_L: // SF!=OF; OF=0 |
1641 | snprintf(buf, buf_size, "(%s%s %s 0)", |
940e8e66 |
1642 | scast, expr, is_inv ? ">=" : "<"); |
5101a5f9 |
1643 | break; |
1644 | |
87bf6cec |
1645 | case PFO_LE: // ZF=1||SF!=OF; OF=0 |
69a3cdfc |
1646 | snprintf(buf, buf_size, "(%s%s %s 0)", |
940e8e66 |
1647 | scast, expr, is_inv ? ">" : "<="); |
850c9265 |
1648 | break; |
1649 | |
91977a1c |
1650 | default: |
69a3cdfc |
1651 | ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo); |
91977a1c |
1652 | } |
1653 | } |
c36e914d |
1654 | |
850c9265 |
1655 | static void out_cmp_for_cc(char *buf, size_t buf_size, |
940e8e66 |
1656 | struct parsed_op *po, enum parsed_flag_op pfo, int is_inv, |
69a3cdfc |
1657 | enum opr_lenmod lmod, const char *expr1, const char *expr2) |
850c9265 |
1658 | { |
69a3cdfc |
1659 | const char *cast, *scast; |
850c9265 |
1660 | |
69a3cdfc |
1661 | cast = lmod_cast_u(po, lmod); |
1662 | scast = lmod_cast_s(po, lmod); |
850c9265 |
1663 | |
69a3cdfc |
1664 | switch (pfo) { |
5101a5f9 |
1665 | case PFO_C: |
1666 | // note: must be unsigned compare |
1667 | snprintf(buf, buf_size, "(%s%s %s %s%s)", |
940e8e66 |
1668 | cast, expr1, is_inv ? ">=" : "<", cast, expr2); |
5101a5f9 |
1669 | break; |
1670 | |
69a3cdfc |
1671 | case PFO_Z: |
850c9265 |
1672 | snprintf(buf, buf_size, "(%s%s %s %s%s)", |
940e8e66 |
1673 | cast, expr1, is_inv ? "!=" : "==", cast, expr2); |
850c9265 |
1674 | break; |
1675 | |
5101a5f9 |
1676 | case PFO_BE: // !a |
850c9265 |
1677 | // note: must be unsigned compare |
1678 | snprintf(buf, buf_size, "(%s%s %s %s%s)", |
940e8e66 |
1679 | cast, expr1, is_inv ? ">" : "<=", cast, expr2); |
5101a5f9 |
1680 | break; |
1681 | |
1682 | // note: must be signed compare |
1683 | case PFO_S: |
1684 | snprintf(buf, buf_size, "(%s(%s - %s) %s 0)", |
940e8e66 |
1685 | scast, expr1, expr2, is_inv ? ">=" : "<"); |
850c9265 |
1686 | break; |
1687 | |
5101a5f9 |
1688 | case PFO_L: // !ge |
850c9265 |
1689 | snprintf(buf, buf_size, "(%s%s %s %s%s)", |
940e8e66 |
1690 | scast, expr1, is_inv ? ">=" : "<", scast, expr2); |
850c9265 |
1691 | break; |
1692 | |
5101a5f9 |
1693 | case PFO_LE: |
1694 | snprintf(buf, buf_size, "(%s%s %s %s%s)", |
940e8e66 |
1695 | scast, expr1, is_inv ? ">" : "<=", scast, expr2); |
5101a5f9 |
1696 | break; |
1697 | |
850c9265 |
1698 | default: |
69a3cdfc |
1699 | ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo); |
850c9265 |
1700 | } |
1701 | } |
1702 | |
69a3cdfc |
1703 | static void out_cmp_test(char *buf, size_t buf_size, |
940e8e66 |
1704 | struct parsed_op *po, enum parsed_flag_op pfo, int is_inv) |
69a3cdfc |
1705 | { |
1706 | char buf1[256], buf2[256], buf3[256]; |
1707 | |
1708 | if (po->op == OP_TEST) { |
1709 | if (IS(opr_name(po, 0), opr_name(po, 1))) { |
3ebea2cf |
1710 | out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]); |
69a3cdfc |
1711 | } |
1712 | else { |
3ebea2cf |
1713 | out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]); |
1714 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]); |
69a3cdfc |
1715 | snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2); |
1716 | } |
940e8e66 |
1717 | out_test_for_cc(buf, buf_size, po, pfo, is_inv, |
69a3cdfc |
1718 | po->operand[0].lmod, buf3); |
1719 | } |
1720 | else if (po->op == OP_CMP) { |
3ebea2cf |
1721 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]); |
1722 | out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[1]); |
940e8e66 |
1723 | out_cmp_for_cc(buf, buf_size, po, pfo, is_inv, |
69a3cdfc |
1724 | po->operand[0].lmod, buf2, buf3); |
1725 | } |
1726 | else |
1727 | ferr(po, "%s: unhandled op: %d\n", __func__, po->op); |
1728 | } |
1729 | |
850c9265 |
1730 | static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1, |
91977a1c |
1731 | struct parsed_opr *popr2) |
1732 | { |
1733 | if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC) |
1734 | ferr(po, "missing lmod for both operands\n"); |
1735 | |
1736 | if (popr1->lmod == OPLM_UNSPEC) |
1737 | popr1->lmod = popr2->lmod; |
1738 | else if (popr2->lmod == OPLM_UNSPEC) |
1739 | popr2->lmod = popr1->lmod; |
a3684be1 |
1740 | else if (popr1->lmod != popr2->lmod) { |
1741 | if (popr1->type_from_var) { |
1742 | popr1->size_mismatch = 1; |
1743 | if (popr1->lmod < popr2->lmod) |
1744 | popr1->size_lt = 1; |
1745 | popr1->lmod = popr2->lmod; |
1746 | } |
1747 | else if (popr2->type_from_var) { |
1748 | popr2->size_mismatch = 1; |
1749 | if (popr2->lmod < popr1->lmod) |
1750 | popr2->size_lt = 1; |
1751 | popr2->lmod = popr1->lmod; |
1752 | } |
1753 | else |
1754 | ferr(po, "conflicting lmods: %d vs %d\n", |
1755 | popr1->lmod, popr2->lmod); |
1756 | } |
91977a1c |
1757 | } |
c36e914d |
1758 | |
850c9265 |
1759 | static const char *op_to_c(struct parsed_op *po) |
1760 | { |
1761 | switch (po->op) |
1762 | { |
1763 | case OP_ADD: |
5101a5f9 |
1764 | case OP_ADC: |
850c9265 |
1765 | return "+"; |
1766 | case OP_SUB: |
5101a5f9 |
1767 | case OP_SBB: |
850c9265 |
1768 | return "-"; |
1769 | case OP_AND: |
1770 | return "&"; |
1771 | case OP_OR: |
1772 | return "|"; |
1773 | case OP_XOR: |
1774 | return "^"; |
1775 | case OP_SHL: |
1776 | return "<<"; |
1777 | case OP_SHR: |
1778 | return ">>"; |
1779 | case OP_MUL: |
1780 | case OP_IMUL: |
1781 | return "*"; |
1782 | default: |
1783 | ferr(po, "op_to_c was supplied with %d\n", po->op); |
1784 | } |
1785 | } |
1786 | |
d4e3b5db |
1787 | static void set_flag_no_dup(struct parsed_op *po, enum op_flags flag, |
1788 | enum op_flags flag_check) |
1789 | { |
1790 | if (po->flags & flag) |
1791 | ferr(po, "flag %x already set\n", flag); |
1792 | if (po->flags & flag_check) |
1793 | ferr(po, "flag_check %x already set\n", flag_check); |
1794 | |
1795 | po->flags |= flag; |
1796 | } |
1797 | |
de50b98b |
1798 | // last op in stream - unconditional branch or ret |
1799 | #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \ |
1800 | || (ops[_i].flags & (OPF_JMP|OPF_CC)) == OPF_JMP) |
1801 | |
87bf6cec |
1802 | static int scan_for_pop(int i, int opcnt, const char *reg, |
d4e3b5db |
1803 | int magic, int depth, int *maxdepth, int do_flags) |
850c9265 |
1804 | { |
87bf6cec |
1805 | struct parsed_op *po; |
1806 | int ret = 0; |
4c45fa73 |
1807 | int j; |
87bf6cec |
1808 | |
850c9265 |
1809 | for (; i < opcnt; i++) { |
87bf6cec |
1810 | po = &ops[i]; |
1811 | if (po->cc_scratch == magic) |
1812 | break; // already checked |
1813 | po->cc_scratch = magic; |
1814 | |
1815 | if (po->flags & OPF_TAIL) |
1816 | return -1; // deadend |
1817 | |
1bafb621 |
1818 | if ((po->flags & OPF_RMD) |
de50b98b |
1819 | || (po->op == OP_PUSH && po->argnum != 0)) // arg push |
850c9265 |
1820 | continue; |
1821 | |
87bf6cec |
1822 | if ((po->flags & OPF_JMP) && po->op != OP_CALL) { |
4c45fa73 |
1823 | if (po->btj != NULL) { |
1824 | // jumptable |
1825 | for (j = 0; j < po->btj->count - 1; j++) { |
1826 | ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic, |
1827 | depth, maxdepth, do_flags); |
1828 | if (ret < 0) |
1829 | return ret; // dead end |
1830 | } |
1831 | // follow last jumptable entry |
1832 | i = po->btj->d[j].bt_i - 1; |
1833 | continue; |
1834 | } |
1835 | |
87bf6cec |
1836 | if (po->bt_i < 0) { |
1837 | ferr(po, "dead branch\n"); |
1838 | return -1; |
1839 | } |
850c9265 |
1840 | |
87bf6cec |
1841 | if (po->flags & OPF_CC) { |
d4e3b5db |
1842 | ret |= scan_for_pop(po->bt_i, opcnt, reg, magic, |
1843 | depth, maxdepth, do_flags); |
87bf6cec |
1844 | if (ret < 0) |
1845 | return ret; // dead end |
1846 | } |
1847 | else { |
1848 | i = po->bt_i - 1; |
1849 | } |
1850 | continue; |
1851 | } |
1852 | |
d4e3b5db |
1853 | if ((po->op == OP_POP || po->op == OP_PUSH) |
1854 | && po->operand[0].type == OPT_REG |
87bf6cec |
1855 | && IS(po->operand[0].name, reg)) |
1856 | { |
d4e3b5db |
1857 | if (po->op == OP_PUSH) { |
1858 | depth++; |
1859 | if (depth > *maxdepth) |
1860 | *maxdepth = depth; |
1861 | if (do_flags) |
1862 | set_flag_no_dup(po, OPF_RSAVE, OPF_RMD); |
1863 | } |
1864 | else if (depth == 0) { |
1865 | if (do_flags) |
1866 | set_flag_no_dup(po, OPF_RMD, OPF_RSAVE); |
1867 | return 1; |
1868 | } |
1869 | else { |
1870 | depth--; |
1871 | if (depth < 0) // should not happen |
1872 | ferr(po, "fail with depth\n"); |
1873 | if (do_flags) |
1874 | set_flag_no_dup(po, OPF_RSAVE, OPF_RMD); |
1875 | } |
87bf6cec |
1876 | } |
850c9265 |
1877 | } |
1878 | |
87bf6cec |
1879 | return ret; |
850c9265 |
1880 | } |
1881 | |
69a3cdfc |
1882 | // scan for pop starting from 'ret' op (all paths) |
d4e3b5db |
1883 | static int scan_for_pop_ret(int i, int opcnt, const char *reg, |
1884 | int flag_set) |
850c9265 |
1885 | { |
1886 | int found = 0; |
1887 | int j; |
1888 | |
1889 | for (; i < opcnt; i++) { |
87bf6cec |
1890 | if (!(ops[i].flags & OPF_TAIL)) |
850c9265 |
1891 | continue; |
1892 | |
1893 | for (j = i - 1; j >= 0; j--) { |
69a3cdfc |
1894 | if (ops[j].flags & OPF_RMD) |
1895 | continue; |
1896 | if (ops[j].flags & OPF_JMP) |
850c9265 |
1897 | return -1; |
1898 | |
1899 | if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG |
1900 | && IS(ops[j].operand[0].name, reg)) |
1901 | { |
1902 | found = 1; |
d4e3b5db |
1903 | ops[j].flags |= flag_set; |
850c9265 |
1904 | break; |
1905 | } |
1906 | |
1907 | if (g_labels[j][0] != 0) |
1908 | return -1; |
1909 | } |
1910 | } |
1911 | |
1912 | return found ? 0 : -1; |
1913 | } |
1914 | |
5101a5f9 |
1915 | // is operand 'opr modified' by parsed_op 'po'? |
1916 | static int is_opr_modified(const struct parsed_opr *opr, |
69a3cdfc |
1917 | const struct parsed_op *po) |
1918 | { |
1919 | if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA)) |
1920 | return 0; |
1921 | |
1922 | if (opr->type == OPT_REG && po->operand[0].type == OPT_REG) { |
1923 | if (po->regmask_dst & (1 << opr->reg)) |
1924 | return 1; |
1925 | else |
1926 | return 0; |
1927 | } |
1928 | |
1929 | return IS(po->operand[0].name, opr->name); |
1930 | } |
1931 | |
5101a5f9 |
1932 | // is any operand of parsed_op 'po_test' modified by parsed_op 'po'? |
1933 | static int is_any_opr_modified(const struct parsed_op *po_test, |
1934 | const struct parsed_op *po) |
1935 | { |
1936 | int i; |
1937 | |
1938 | if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA)) |
1939 | return 0; |
1940 | |
de50b98b |
1941 | if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST) |
1942 | return 0; |
1943 | |
1944 | if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst) |
1945 | return 1; |
1946 | |
1947 | // in reality, it can wreck any register, but in decompiled C |
2b43685d |
1948 | // version it can only overwrite eax or edx:eax |
de50b98b |
1949 | if (po->op == OP_CALL |
2b43685d |
1950 | && ((po_test->regmask_src | po_test->regmask_dst) |
1951 | & ((1 << xAX)|(1 << xDX)))) |
5101a5f9 |
1952 | return 1; |
1953 | |
1954 | for (i = 0; i < po_test->operand_cnt; i++) |
1955 | if (IS(po_test->operand[i].name, po->operand[0].name)) |
1956 | return 1; |
1957 | |
1958 | return 0; |
1959 | } |
1960 | |
940e8e66 |
1961 | // scan for any po_test operand modification in range given |
5101a5f9 |
1962 | static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt) |
69a3cdfc |
1963 | { |
2b43685d |
1964 | if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST) |
1965 | return -1; |
1966 | |
69a3cdfc |
1967 | for (; i < opcnt; i++) { |
5101a5f9 |
1968 | if (is_any_opr_modified(po_test, &ops[i])) |
69a3cdfc |
1969 | return i; |
1970 | } |
1971 | |
1972 | return -1; |
1973 | } |
1974 | |
940e8e66 |
1975 | // scan for po_test operand[0] modification in range given |
1976 | static int scan_for_mod_opr0(struct parsed_op *po_test, |
1977 | int i, int opcnt) |
1978 | { |
1979 | for (; i < opcnt; i++) { |
1980 | if (is_opr_modified(&po_test->operand[0], &ops[i])) |
1981 | return i; |
1982 | } |
1983 | |
1984 | return -1; |
1985 | } |
1986 | |
04f8a628 |
1987 | static int scan_for_flag_set(int i, int magic, int *branched, |
1988 | int *setters, int *setter_cnt) |
69a3cdfc |
1989 | { |
04f8a628 |
1990 | struct label_ref *lr; |
2b43685d |
1991 | int ret; |
de50b98b |
1992 | |
1993 | while (i >= 0) { |
04f8a628 |
1994 | if (ops[i].cc_scratch == magic) { |
1995 | ferr(&ops[i], "%s looped\n", __func__); |
1996 | return -1; |
1997 | } |
1998 | ops[i].cc_scratch = magic; |
1999 | |
de50b98b |
2000 | if (g_labels[i][0] != 0) { |
2b43685d |
2001 | *branched = 1; |
04f8a628 |
2002 | |
2003 | lr = &g_label_refs[i]; |
2004 | for (; lr->next; lr = lr->next) { |
2005 | ret = scan_for_flag_set(lr->i, magic, |
2006 | branched, setters, setter_cnt); |
2007 | if (ret < 0) |
2008 | return ret; |
2009 | } |
2010 | |
de50b98b |
2011 | if (i > 0 && LAST_OP(i - 1)) { |
2012 | i = g_label_refs[i].i; |
de50b98b |
2013 | continue; |
2014 | } |
04f8a628 |
2015 | ret = scan_for_flag_set(lr->i, magic, |
2016 | branched, setters, setter_cnt); |
2b43685d |
2017 | if (ret < 0) |
2018 | return ret; |
de50b98b |
2019 | } |
2020 | i--; |
2021 | |
2b43685d |
2022 | if (ops[i].flags & OPF_FLAGS) { |
2023 | setters[*setter_cnt] = i; |
2024 | (*setter_cnt)++; |
2025 | return 0; |
2026 | } |
69a3cdfc |
2027 | |
2028 | if ((ops[i].flags & OPF_JMP) && !(ops[i].flags & OPF_CC)) |
2029 | return -1; |
69a3cdfc |
2030 | } |
2031 | |
2032 | return -1; |
2033 | } |
2034 | |
5101a5f9 |
2035 | // scan back for cdq, if anything modifies edx, fail |
2036 | static int scan_for_cdq_edx(int i) |
2037 | { |
cdfaeed7 |
2038 | while (i >= 0) { |
2039 | if (g_labels[i][0] != 0) { |
2040 | if (g_label_refs[i].next != NULL) |
2041 | return -1; |
2042 | if (i > 0 && LAST_OP(i - 1)) { |
2043 | i = g_label_refs[i].i; |
2044 | continue; |
2045 | } |
2046 | return -1; |
2047 | } |
2048 | i--; |
2049 | |
5101a5f9 |
2050 | if (ops[i].op == OP_CDQ) |
2051 | return i; |
2052 | |
2053 | if (ops[i].regmask_dst & (1 << xDX)) |
2054 | return -1; |
2055 | if (g_labels[i][0] != 0) |
2056 | return -1; |
2057 | } |
2058 | |
2059 | return -1; |
2060 | } |
2061 | |
64c59faf |
2062 | static int scan_for_reg_clear(int i, int reg) |
2063 | { |
cdfaeed7 |
2064 | while (i >= 0) { |
2065 | if (g_labels[i][0] != 0) { |
2066 | if (g_label_refs[i].next != NULL) |
2067 | return -1; |
2068 | if (i > 0 && LAST_OP(i - 1)) { |
2069 | i = g_label_refs[i].i; |
2070 | continue; |
2071 | } |
2072 | return -1; |
2073 | } |
2074 | i--; |
2075 | |
64c59faf |
2076 | if (ops[i].op == OP_XOR |
2077 | && ops[i].operand[0].lmod == OPLM_DWORD |
2078 | && ops[i].operand[0].reg == ops[i].operand[1].reg |
2079 | && ops[i].operand[0].reg == reg) |
2080 | return i; |
2081 | |
2082 | if (ops[i].regmask_dst & (1 << reg)) |
2083 | return -1; |
2084 | if (g_labels[i][0] != 0) |
2085 | return -1; |
2086 | } |
2087 | |
2088 | return -1; |
2089 | } |
2090 | |
1bafb621 |
2091 | // scan for positive, constant esp adjust |
2092 | static int scan_for_esp_adjust(int i, int opcnt, int *adj) |
2093 | { |
7ba45c34 |
2094 | struct parsed_op *po; |
2095 | *adj = 0; |
2096 | |
1bafb621 |
2097 | for (; i < opcnt; i++) { |
7ba45c34 |
2098 | po = &ops[i]; |
2099 | |
2100 | if (po->op == OP_ADD && po->operand[0].reg == xSP) { |
2101 | if (po->operand[1].type != OPT_CONST) |
1bafb621 |
2102 | ferr(&ops[i], "non-const esp adjust?\n"); |
7ba45c34 |
2103 | *adj += po->operand[1].val; |
1bafb621 |
2104 | if (*adj & 3) |
2105 | ferr(&ops[i], "unaligned esp adjust: %x\n", *adj); |
2106 | return i; |
2107 | } |
7ba45c34 |
2108 | else if (po->op == OP_PUSH) |
2109 | *adj -= lmod_bytes(po, po->operand[0].lmod); |
2110 | else if (po->op == OP_POP) |
2111 | *adj += lmod_bytes(po, po->operand[0].lmod); |
e56ab892 |
2112 | else if (po->flags & (OPF_JMP|OPF_TAIL)) { |
2113 | if (po->op != OP_CALL) |
2114 | return -1; |
2115 | if (po->operand[0].type != OPT_LABEL) |
2116 | return -1; |
2117 | // TODO: should only allow combining __cdecl calls.. |
2118 | } |
7ba45c34 |
2119 | |
1bafb621 |
2120 | if (g_labels[i][0] != 0) |
2121 | return -1; |
2122 | } |
2123 | |
2124 | return -1; |
2125 | } |
2126 | |
a3684be1 |
2127 | static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags) |
2128 | { |
2129 | struct parsed_op *po; |
2130 | int j; |
2131 | |
2132 | if (i < 0) |
2133 | ferr(ops, "%s: followed bad branch?\n", __func__); |
2134 | |
2135 | for (; i < opcnt; i++) { |
2136 | po = &ops[i]; |
2137 | if (po->cc_scratch == magic) |
2138 | return; |
2139 | po->cc_scratch = magic; |
2140 | po->flags |= flags; |
2141 | |
2142 | if ((po->flags & OPF_JMP) && po->op != OP_CALL) { |
2143 | if (po->btj != NULL) { |
2144 | // jumptable |
2145 | for (j = 0; j < po->btj->count; j++) |
2146 | scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags); |
2147 | return; |
2148 | } |
2149 | |
2150 | scan_fwd_set_flags(po->bt_i, opcnt, magic, flags); |
2151 | if (!(po->flags & OPF_CC)) |
2152 | return; |
2153 | } |
2154 | if (po->flags & OPF_TAIL) |
2155 | return; |
2156 | } |
2157 | } |
2158 | |
e56ab892 |
2159 | static int collect_call_args(struct parsed_op *po, int i, |
2160 | struct parsed_proto *pp, int *save_arg_vars, int arg, |
a3684be1 |
2161 | int magic, int need_op_saving, int may_reuse) |
e56ab892 |
2162 | { |
2163 | struct parsed_proto *pp_tmp; |
2164 | struct label_ref *lr; |
2b43685d |
2165 | int need_to_save_current; |
e56ab892 |
2166 | int ret = 0; |
2167 | int j; |
2168 | |
a3684be1 |
2169 | if (i < 0) { |
e56ab892 |
2170 | ferr(po, "no refs for '%s'?\n", g_labels[i]); |
a3684be1 |
2171 | return -1; |
2172 | } |
e56ab892 |
2173 | |
de50b98b |
2174 | for (; arg < pp->argc; arg++) |
e56ab892 |
2175 | if (pp->arg[arg].reg == NULL) |
2176 | break; |
a3684be1 |
2177 | magic = (magic & 0xffffff) | (arg << 24); |
e56ab892 |
2178 | |
2179 | for (j = i; j >= 0 && arg < pp->argc; ) |
2180 | { |
a3684be1 |
2181 | if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) { |
2182 | if (ops[j].cc_scratch != magic) { |
2183 | ferr(&ops[j], "arg collect hit same path with diff args for %s\n", |
2184 | pp->name); |
2185 | return -1; |
2186 | } |
2187 | // ok: have already been here |
2188 | return 0; |
2189 | } |
2190 | ops[j].cc_scratch = magic; |
2191 | |
e56ab892 |
2192 | if (g_labels[j][0] != 0) { |
2193 | lr = &g_label_refs[j]; |
2194 | if (lr->next != NULL) |
2195 | need_op_saving = 1; |
a652aa9f |
2196 | for (; lr->next; lr = lr->next) { |
2197 | if ((ops[lr->i].flags & (OPF_JMP|OPF_CC)) != OPF_JMP) |
2198 | may_reuse = 1; |
a3684be1 |
2199 | ret = collect_call_args(po, lr->i, pp, save_arg_vars, |
2200 | arg, magic, need_op_saving, may_reuse); |
2201 | if (ret < 0) |
2202 | return ret; |
a652aa9f |
2203 | } |
e56ab892 |
2204 | |
a652aa9f |
2205 | if ((ops[lr->i].flags & (OPF_JMP|OPF_CC)) != OPF_JMP) |
2206 | may_reuse = 1; |
de50b98b |
2207 | if (j > 0 && LAST_OP(j - 1)) { |
e56ab892 |
2208 | // follow last branch in reverse |
2209 | j = lr->i; |
2210 | continue; |
2211 | } |
2212 | need_op_saving = 1; |
a3684be1 |
2213 | ret = collect_call_args(po, lr->i, pp, save_arg_vars, |
2214 | arg, magic, need_op_saving, may_reuse); |
2215 | if (ret < 0) |
2216 | return ret; |
e56ab892 |
2217 | } |
2218 | j--; |
2219 | |
2220 | if (ops[j].op == OP_CALL) |
2221 | { |
2222 | pp_tmp = ops[j].datap; |
2223 | if (pp_tmp == NULL) |
2224 | ferr(po, "arg collect hit unparsed call\n"); |
a652aa9f |
2225 | if (may_reuse && pp_tmp->argc_stack > 0) |
de50b98b |
2226 | ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n", |
2227 | arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack); |
e56ab892 |
2228 | } |
de50b98b |
2229 | else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP) { |
2230 | ferr(po, "arg collect %d/%d hit esp adjust\n", |
2231 | arg, pp->argc); |
2232 | } |
2233 | else if (ops[j].op == OP_POP) { |
2234 | ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc); |
2235 | } |
de50b98b |
2236 | else if ((ops[j].flags & (OPF_JMP|OPF_CC)) == (OPF_JMP|OPF_CC)) |
2237 | { |
a652aa9f |
2238 | may_reuse = 1; |
de50b98b |
2239 | } |
2240 | else if (ops[j].op == OP_PUSH && !(ops[j].flags & OPF_FARG)) |
e56ab892 |
2241 | { |
2242 | pp->arg[arg].datap = &ops[j]; |
2b43685d |
2243 | need_to_save_current = 0; |
e56ab892 |
2244 | if (!need_op_saving) { |
2245 | ret = scan_for_mod(&ops[j], j + 1, i); |
2b43685d |
2246 | need_to_save_current = (ret >= 0); |
e56ab892 |
2247 | } |
2b43685d |
2248 | if (need_op_saving || need_to_save_current) { |
e56ab892 |
2249 | // mark this push as one that needs operand saving |
2250 | ops[j].flags &= ~OPF_RMD; |
de50b98b |
2251 | if (ops[j].argnum == 0) { |
2252 | ops[j].argnum = arg + 1; |
2253 | *save_arg_vars |= 1 << arg; |
2254 | } |
2255 | else if (ops[j].argnum < arg + 1) |
2256 | ferr(&ops[j], "argnum conflict (%d<%d) for '%s'\n", |
2257 | ops[j].argnum, arg + 1, pp->name); |
e56ab892 |
2258 | } |
de50b98b |
2259 | else if (ops[j].argnum == 0) |
e56ab892 |
2260 | ops[j].flags |= OPF_RMD; |
2261 | |
a652aa9f |
2262 | // some PUSHes are reused by different calls on other branches, |
de50b98b |
2263 | // but that can't happen if we didn't branch, so they |
2264 | // can be removed from future searches (handles nested calls) |
a652aa9f |
2265 | if (!may_reuse) |
de50b98b |
2266 | ops[j].flags |= OPF_FARG; |
2267 | |
e56ab892 |
2268 | // next arg |
2269 | for (arg++; arg < pp->argc; arg++) |
2270 | if (pp->arg[arg].reg == NULL) |
2271 | break; |
a3684be1 |
2272 | magic = (magic & 0xffffff) | (arg << 24); |
e56ab892 |
2273 | } |
2274 | } |
2275 | |
2276 | if (arg < pp->argc) { |
2277 | ferr(po, "arg collect failed for '%s': %d/%d\n", |
2278 | pp->name, arg, pp->argc); |
2279 | ret = -1; |
2280 | } |
2281 | return ret; |
2282 | } |
2283 | |
4c45fa73 |
2284 | static void add_label_ref(struct label_ref *lr, int op_i) |
2285 | { |
2286 | struct label_ref *lr_new; |
2287 | |
2288 | if (lr->i == -1) { |
2289 | lr->i = op_i; |
2290 | return; |
2291 | } |
2292 | |
2293 | lr_new = calloc(1, sizeof(*lr_new)); |
2294 | lr_new->i = op_i; |
a652aa9f |
2295 | lr_new->next = lr->next; |
4c45fa73 |
2296 | lr->next = lr_new; |
2297 | } |
2298 | |
04f8a628 |
2299 | static void output_std_flags(FILE *fout, struct parsed_op *po, |
2300 | int *pfomask, const char *dst_opr_text) |
2301 | { |
2302 | if (*pfomask & (1 << PFO_Z)) { |
2303 | fprintf(fout, "\n cond_z = (%s%s == 0);", |
2304 | lmod_cast_u(po, po->operand[0].lmod), dst_opr_text); |
2305 | *pfomask &= ~(1 << PFO_Z); |
2306 | } |
2307 | if (*pfomask & (1 << PFO_S)) { |
2308 | fprintf(fout, "\n cond_s = (%s%s < 0);", |
2309 | lmod_cast_s(po, po->operand[0].lmod), dst_opr_text); |
2310 | *pfomask &= ~(1 << PFO_S); |
2311 | } |
2312 | } |
2313 | |
91977a1c |
2314 | static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) |
2315 | { |
69a3cdfc |
2316 | struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op; |
850c9265 |
2317 | struct parsed_opr *last_arith_dst = NULL; |
3ebea2cf |
2318 | char buf1[256], buf2[256], buf3[256], cast[64]; |
bd96f656 |
2319 | const struct parsed_proto *pp_c; |
940e8e66 |
2320 | struct parsed_proto *pp, *pp_tmp; |
4c45fa73 |
2321 | struct parsed_data *pd; |
91977a1c |
2322 | const char *tmpname; |
940e8e66 |
2323 | enum parsed_flag_op pfo; |
69a3cdfc |
2324 | int save_arg_vars = 0; |
2325 | int cmp_result_vars = 0; |
87bf6cec |
2326 | int need_mul_var = 0; |
a3684be1 |
2327 | int have_func_ret = 0; |
91977a1c |
2328 | int had_decl = 0; |
3ebea2cf |
2329 | int label_pending = 0; |
d4e3b5db |
2330 | int regmask_save = 0; |
91977a1c |
2331 | int regmask_arg = 0; |
a3684be1 |
2332 | int regmask_now = 0; |
91977a1c |
2333 | int regmask = 0; |
940e8e66 |
2334 | int pfomask = 0; |
64c59faf |
2335 | int found = 0; |
d4e3b5db |
2336 | int depth = 0; |
91977a1c |
2337 | int no_output; |
4c45fa73 |
2338 | int i, j, l; |
69a3cdfc |
2339 | int dummy; |
91977a1c |
2340 | int arg; |
91977a1c |
2341 | int reg; |
2342 | int ret; |
2343 | |
1bafb621 |
2344 | g_bp_frame = g_sp_frame = g_stack_fsz = 0; |
91977a1c |
2345 | |
bd96f656 |
2346 | g_func_pp = proto_parse(fhdr, funcn); |
2347 | if (g_func_pp == NULL) |
91977a1c |
2348 | ferr(ops, "proto_parse failed for '%s'\n", funcn); |
2349 | |
bd96f656 |
2350 | fprintf(fout, "%s ", g_func_pp->ret_type.name); |
54e763a1 |
2351 | if (g_func_pp->is_stdcall && g_func_pp->argc_reg == 0) |
2352 | fprintf(fout, "__stdcall "); |
e56ab892 |
2353 | if (g_ida_func_attr & IDAFA_NORETURN) |
2354 | fprintf(fout, "noreturn "); |
2355 | fprintf(fout, "%s(", funcn); |
54e763a1 |
2356 | |
bd96f656 |
2357 | for (i = 0; i < g_func_pp->argc; i++) { |
91977a1c |
2358 | if (i > 0) |
2359 | fprintf(fout, ", "); |
54e763a1 |
2360 | if (g_func_pp->arg[i].fptr != NULL) { |
2361 | // func pointer.. |
2362 | pp = g_func_pp->arg[i].fptr; |
2363 | fprintf(fout, "%s (", pp->ret_type.name); |
2364 | if (pp->is_stdcall && pp->argc_reg == 0) |
2365 | fprintf(fout, "__stdcall "); |
2366 | fprintf(fout, "*a%d)(", i + 1); |
2367 | for (j = 0; j < pp->argc; j++) { |
2368 | if (j > 0) |
2369 | fprintf(fout, ", "); |
2370 | if (pp->arg[j].fptr) |
2371 | ferr(ops, "nested fptr\n"); |
2372 | fprintf(fout, "%s", pp->arg[j].type.name); |
2373 | } |
2374 | fprintf(fout, ")"); |
2375 | } |
2376 | else { |
2377 | fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1); |
2378 | } |
91977a1c |
2379 | } |
bd96f656 |
2380 | if (g_func_pp->is_vararg) { |
4f12f671 |
2381 | if (i > 0) |
2382 | fprintf(fout, ", "); |
2383 | fprintf(fout, "..."); |
2384 | } |
54e763a1 |
2385 | |
91977a1c |
2386 | fprintf(fout, ")\n{\n"); |
2387 | |
2388 | // pass1: |
1bafb621 |
2389 | // - handle ebp/esp frame, remove ops related to it |
91977a1c |
2390 | if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp") |
2391 | && ops[1].op == OP_MOV |
2392 | && IS(opr_name(&ops[1], 0), "ebp") |
2393 | && IS(opr_name(&ops[1], 1), "esp")) |
2394 | { |
87bf6cec |
2395 | int ecx_push = 0; |
2396 | |
91977a1c |
2397 | g_bp_frame = 1; |
69a3cdfc |
2398 | ops[0].flags |= OPF_RMD; |
2399 | ops[1].flags |= OPF_RMD; |
4c45fa73 |
2400 | i = 2; |
91977a1c |
2401 | |
2402 | if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) { |
1bafb621 |
2403 | g_stack_fsz = opr_const(&ops[2], 1); |
69a3cdfc |
2404 | ops[2].flags |= OPF_RMD; |
4c45fa73 |
2405 | i++; |
91977a1c |
2406 | } |
87bf6cec |
2407 | else { |
2408 | // another way msvc builds stack frame.. |
2409 | i = 2; |
2410 | while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) { |
1bafb621 |
2411 | g_stack_fsz += 4; |
87bf6cec |
2412 | ops[i].flags |= OPF_RMD; |
2413 | ecx_push++; |
2414 | i++; |
2415 | } |
4c45fa73 |
2416 | // and another way.. |
2417 | if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX |
2418 | && ops[i].operand[1].type == OPT_CONST |
2419 | && ops[i + 1].op == OP_CALL |
2420 | && IS(opr_name(&ops[i + 1], 0), "__alloca_probe")) |
2421 | { |
2422 | g_stack_fsz += ops[i].operand[1].val; |
2423 | ops[i].flags |= OPF_RMD; |
2424 | i++; |
2425 | ops[i].flags |= OPF_RMD; |
2426 | i++; |
2427 | } |
87bf6cec |
2428 | } |
91977a1c |
2429 | |
64c59faf |
2430 | found = 0; |
91977a1c |
2431 | do { |
2432 | for (; i < opcnt; i++) |
2433 | if (ops[i].op == OP_RET) |
2434 | break; |
64c59faf |
2435 | if (i == opcnt && (ops[i - 1].flags & OPF_JMP) && found) |
2436 | break; |
2437 | |
e56ab892 |
2438 | if (ops[i - 1].op == OP_POP && IS(opr_name(&ops[i - 1], 0), "ebp")) |
2439 | ops[i - 1].flags |= OPF_RMD; |
2440 | else if (!(g_ida_func_attr & IDAFA_NORETURN)) |
91977a1c |
2441 | ferr(&ops[i - 1], "'pop ebp' expected\n"); |
91977a1c |
2442 | |
1bafb621 |
2443 | if (g_stack_fsz != 0) { |
e56ab892 |
2444 | if (ops[i - 2].op == OP_MOV |
2445 | && IS(opr_name(&ops[i - 2], 0), "esp") |
2446 | && IS(opr_name(&ops[i - 2], 1), "ebp")) |
91977a1c |
2447 | { |
e56ab892 |
2448 | ops[i - 2].flags |= OPF_RMD; |
91977a1c |
2449 | } |
e56ab892 |
2450 | else if (!(g_ida_func_attr & IDAFA_NORETURN)) |
2451 | ferr(&ops[i - 2], "esp restore expected\n"); |
87bf6cec |
2452 | |
2453 | if (ecx_push && ops[i - 3].op == OP_POP |
2454 | && IS(opr_name(&ops[i - 3], 0), "ecx")) |
2455 | { |
2456 | ferr(&ops[i - 3], "unexpected ecx pop\n"); |
2457 | } |
91977a1c |
2458 | } |
87bf6cec |
2459 | |
64c59faf |
2460 | found = 1; |
91977a1c |
2461 | i++; |
2462 | } while (i < opcnt); |
2463 | } |
1bafb621 |
2464 | else { |
2465 | for (i = 0; i < opcnt; i++) { |
2466 | if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL))) |
2467 | break; |
2468 | if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP |
2469 | && ops[i].operand[1].type == OPT_CONST) |
2470 | { |
2471 | g_sp_frame = 1; |
2472 | break; |
2473 | } |
2474 | } |
2475 | |
2476 | if (g_sp_frame) |
2477 | { |
2478 | g_stack_fsz = ops[i].operand[1].val; |
2479 | ops[i].flags |= OPF_RMD; |
2480 | |
2481 | i++; |
2482 | do { |
2483 | for (; i < opcnt; i++) |
2484 | if (ops[i].op == OP_RET) |
2485 | break; |
2486 | if (ops[i - 1].op != OP_ADD |
2487 | || !IS(opr_name(&ops[i - 1], 0), "esp") |
2488 | || ops[i - 1].operand[1].type != OPT_CONST |
2489 | || ops[i - 1].operand[1].val != g_stack_fsz) |
2490 | ferr(&ops[i - 1], "'add esp' expected\n"); |
2491 | ops[i - 1].flags |= OPF_RMD; |
2492 | |
2493 | i++; |
2494 | } while (i < opcnt); |
2495 | } |
2496 | } |
91977a1c |
2497 | |
2498 | // pass2: |
87bf6cec |
2499 | // - resolve all branches |
de50b98b |
2500 | for (i = 0; i < opcnt; i++) |
2501 | { |
87bf6cec |
2502 | po = &ops[i]; |
2503 | po->bt_i = -1; |
4c45fa73 |
2504 | po->btj = NULL; |
87bf6cec |
2505 | |
2506 | if ((po->flags & OPF_RMD) || !(po->flags & OPF_JMP) |
2507 | || po->op == OP_CALL || po->op == OP_RET) |
2508 | continue; |
2509 | |
4c45fa73 |
2510 | if (po->operand[0].type == OPT_REGMEM) { |
2511 | char *p = strchr(po->operand[0].name, '['); |
2512 | if (p == NULL) |
2513 | ferr(po, "unhandled indirect branch\n"); |
2514 | ret = p - po->operand[0].name; |
2515 | strncpy(buf1, po->operand[0].name, ret); |
2516 | buf1[ret] = 0; |
2517 | |
2518 | for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) { |
2519 | if (IS(g_func_pd[j].label, buf1)) { |
2520 | pd = &g_func_pd[j]; |
2521 | break; |
2522 | } |
2523 | } |
2524 | if (pd == NULL) |
840257f6 |
2525 | //ferr(po, "label '%s' not parsed?\n", buf1); |
2526 | goto tailcall; |
4c45fa73 |
2527 | if (pd->type != OPT_OFFSET) |
2528 | ferr(po, "label '%s' with non-offset data?\n", buf1); |
2529 | |
2530 | // find all labels, link |
2531 | for (j = 0; j < pd->count; j++) { |
2532 | for (l = 0; l < opcnt; l++) { |
2533 | if (g_labels[l][0] && IS(g_labels[l], pd->d[j].u.label)) { |
2534 | add_label_ref(&g_label_refs[l], i); |
2535 | pd->d[j].bt_i = l; |
2536 | break; |
2537 | } |
2538 | } |
2539 | } |
2540 | |
2541 | po->btj = pd; |
2542 | continue; |
2543 | } |
2544 | |
2545 | for (l = 0; l < opcnt; l++) { |
2546 | if (g_labels[l][0] && IS(po->operand[0].name, g_labels[l])) { |
2547 | add_label_ref(&g_label_refs[l], i); |
2548 | po->bt_i = l; |
87bf6cec |
2549 | break; |
2550 | } |
2551 | } |
2552 | |
4c45fa73 |
2553 | if (po->bt_i != -1) |
2554 | continue; |
2555 | |
840257f6 |
2556 | if (po->operand[0].type == OPT_LABEL) |
87bf6cec |
2557 | // assume tail call |
840257f6 |
2558 | goto tailcall; |
4c45fa73 |
2559 | |
2560 | ferr(po, "unhandled branch\n"); |
840257f6 |
2561 | |
2562 | tailcall: |
2563 | po->op = OP_CALL; |
2564 | po->flags |= OPF_TAIL; |
87bf6cec |
2565 | } |
2566 | |
2567 | // pass3: |
91977a1c |
2568 | // - process calls |
1bafb621 |
2569 | for (i = 0; i < opcnt; i++) |
2570 | { |
69a3cdfc |
2571 | po = &ops[i]; |
2572 | if (po->flags & OPF_RMD) |
91977a1c |
2573 | continue; |
850c9265 |
2574 | |
d4e3b5db |
2575 | if (po->op == OP_CALL) |
69a3cdfc |
2576 | { |
1bafb621 |
2577 | pp = calloc(1, sizeof(*pp)); |
91977a1c |
2578 | my_assert_not(pp, NULL); |
1bafb621 |
2579 | tmpname = opr_name(po, 0); |
2580 | if (po->operand[0].type != OPT_LABEL) |
2581 | { |
2582 | ret = scan_for_esp_adjust(i + 1, opcnt, &j); |
2583 | if (ret < 0) |
2584 | ferr(po, "non-__cdecl indirect call unhandled yet\n"); |
2585 | j /= 4; |
2586 | if (j > ARRAY_SIZE(pp->arg)) |
2587 | ferr(po, "esp adjust too large?\n"); |
3ebea2cf |
2588 | pp->ret_type.name = strdup("int"); |
1bafb621 |
2589 | pp->argc = pp->argc_stack = j; |
2590 | for (arg = 0; arg < pp->argc; arg++) |
3ebea2cf |
2591 | pp->arg[arg].type.name = strdup("int"); |
1bafb621 |
2592 | } |
2593 | else { |
bd96f656 |
2594 | pp_c = proto_parse(fhdr, tmpname); |
2595 | if (pp_c == NULL) |
1bafb621 |
2596 | ferr(po, "proto_parse failed for call '%s'\n", tmpname); |
cdfaeed7 |
2597 | if (pp_c->is_fptr && pp_c->argc_reg != 0) |
2598 | ferr(po, "fptr call with reg arg\n"); |
bd96f656 |
2599 | pp = proto_clone(pp_c); |
1bafb621 |
2600 | } |
2601 | |
7ba45c34 |
2602 | // look for and make use of esp adjust |
2603 | ret = -1; |
e56ab892 |
2604 | if (!pp->is_stdcall && pp->argc_stack > 0) |
7ba45c34 |
2605 | ret = scan_for_esp_adjust(i + 1, opcnt, &j); |
1bafb621 |
2606 | if (ret >= 0) { |
7ba45c34 |
2607 | if (pp->is_vararg) { |
2608 | if (j / 4 < pp->argc_stack) |
2609 | ferr(po, "esp adjust is too small: %x < %x\n", |
2610 | j, pp->argc_stack * 4); |
2611 | // modify pp to make it have varargs as normal args |
2612 | arg = pp->argc; |
2613 | pp->argc += j / 4 - pp->argc_stack; |
2614 | for (; arg < pp->argc; arg++) { |
3ebea2cf |
2615 | pp->arg[arg].type.name = strdup("int"); |
7ba45c34 |
2616 | pp->argc_stack++; |
2617 | } |
2618 | if (pp->argc > ARRAY_SIZE(pp->arg)) |
e56ab892 |
2619 | ferr(po, "too many args for '%s'\n", tmpname); |
7ba45c34 |
2620 | } |
1bafb621 |
2621 | if (pp->argc_stack != j / 4) |
e56ab892 |
2622 | ferr(po, "stack tracking failed for '%s': %x %x\n", |
2623 | tmpname, pp->argc_stack * 4, j); |
7ba45c34 |
2624 | |
1bafb621 |
2625 | ops[ret].flags |= OPF_RMD; |
7ba45c34 |
2626 | // a bit of a hack, but deals with use of |
2627 | // single adj for multiple calls |
2628 | ops[ret].operand[1].val -= j; |
1bafb621 |
2629 | } |
7ba45c34 |
2630 | else if (pp->is_vararg) |
2631 | ferr(po, "missing esp_adjust for vararg func '%s'\n", |
2632 | pp->name); |
91977a1c |
2633 | |
39b168b8 |
2634 | // can't call functions with non-__cdecl callbacks yet |
2635 | for (arg = 0; arg < pp->argc; arg++) { |
2636 | if (pp->arg[arg].fptr != NULL) { |
2637 | pp_tmp = pp->arg[arg].fptr; |
2638 | if (pp_tmp->is_stdcall || pp_tmp->argc != pp_tmp->argc_stack) |
2639 | ferr(po, "'%s' has a non-__cdecl callback\n", tmpname); |
2640 | } |
2641 | } |
2642 | |
a3684be1 |
2643 | collect_call_args(po, i, pp, &save_arg_vars, |
2644 | 0, i + opcnt * 2, 0, 0); |
2b43685d |
2645 | |
2646 | if (strstr(pp->ret_type.name, "int64")) |
2647 | need_mul_var = 1; |
a3684be1 |
2648 | if (!(po->flags & OPF_TAIL) && !IS(pp->ret_type.name, "void")) |
2649 | have_func_ret = 1; |
69a3cdfc |
2650 | po->datap = pp; |
91977a1c |
2651 | } |
d4e3b5db |
2652 | } |
2653 | |
2654 | // pass4: |
2655 | // - find POPs for PUSHes, rm both |
2656 | // - scan for all used registers |
2657 | // - find flag set ops for their users |
1bafb621 |
2658 | // - declare indirect functions |
d4e3b5db |
2659 | for (i = 0; i < opcnt; i++) { |
2660 | po = &ops[i]; |
2661 | if (po->flags & OPF_RMD) |
2662 | continue; |
2663 | |
2664 | if (po->op == OP_PUSH |
de50b98b |
2665 | && po->argnum == 0 && !(po->flags & OPF_RSAVE) |
d4e3b5db |
2666 | && po->operand[0].type == OPT_REG) |
2667 | { |
2668 | reg = po->operand[0].reg; |
2669 | if (reg < 0) |
2670 | ferr(po, "reg not set for push?\n"); |
2671 | |
2672 | depth = 0; |
2673 | ret = scan_for_pop(i + 1, opcnt, |
a3684be1 |
2674 | po->operand[0].name, i + opcnt * 3, 0, &depth, 0); |
d4e3b5db |
2675 | if (ret == 1) { |
2676 | if (depth > 1) |
2677 | ferr(po, "too much depth: %d\n", depth); |
2678 | if (depth > 0) |
2679 | regmask_save |= 1 << reg; |
2680 | |
2681 | po->flags |= OPF_RMD; |
2682 | scan_for_pop(i + 1, opcnt, po->operand[0].name, |
a3684be1 |
2683 | i + opcnt * 4, 0, &depth, 1); |
d4e3b5db |
2684 | continue; |
2685 | } |
2686 | ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0); |
2687 | if (ret == 0) { |
2688 | arg = OPF_RMD; |
2689 | if (regmask & (1 << reg)) { |
2690 | if (regmask_save & (1 << reg)) |
2691 | ferr(po, "%s already saved?\n", po->operand[0].name); |
2692 | arg = OPF_RSAVE; |
2693 | } |
2694 | po->flags |= arg; |
2695 | scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg); |
2696 | continue; |
2697 | } |
2698 | } |
2699 | |
a3684be1 |
2700 | regmask_now = po->regmask_src | po->regmask_dst; |
2701 | if (regmask_now & (1 << xBP)) { |
2702 | if (g_bp_frame && !(po->flags & OPF_EBP_S)) { |
2703 | if (po->regmask_dst & (1 << xBP)) |
2704 | // compiler decided to drop bp frame and use ebp as scratch |
2705 | scan_fwd_set_flags(i, opcnt, i + opcnt * 5, OPF_EBP_S); |
2706 | else |
2707 | regmask_now &= ~(1 << xBP); |
2708 | } |
2709 | } |
2710 | |
2711 | regmask |= regmask_now; |
d4e3b5db |
2712 | |
2713 | if (po->flags & OPF_CC) |
2714 | { |
2b43685d |
2715 | int setters[16], cnt = 0, branched = 0; |
2716 | |
04f8a628 |
2717 | ret = scan_for_flag_set(i, i + opcnt * 6, |
2718 | &branched, setters, &cnt); |
2b43685d |
2719 | if (ret < 0 || cnt <= 0) |
2720 | ferr(po, "unable to trace flag setter(s)\n"); |
2721 | if (cnt > ARRAY_SIZE(setters)) |
2722 | ferr(po, "too many flag setters\n"); |
d4e3b5db |
2723 | |
d4e3b5db |
2724 | pfo = split_cond(po, po->op, &dummy); |
2b43685d |
2725 | for (j = 0; j < cnt; j++) |
2726 | { |
2727 | tmp_op = &ops[setters[j]]; // flag setter |
2728 | pfomask = 0; |
2729 | |
2730 | // to get nicer code, we try to delay test and cmp; |
2731 | // if we can't because of operand modification, or if we |
2732 | // have math op, or branch, make it calculate flags explicitly |
2733 | if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP) { |
2734 | if (branched || scan_for_mod(tmp_op, setters[j] + 1, i) >= 0) |
2735 | pfomask = 1 << pfo; |
2736 | } |
2737 | else if (tmp_op->op == OP_CMPS) { |
2738 | pfomask = 1 << PFO_Z; |
2739 | } |
2740 | else { |
04f8a628 |
2741 | // see if we'll be able to handle based on op result |
2742 | if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR |
2743 | && pfo != PFO_Z && pfo != PFO_S && pfo != PFO_P) |
2744 | || branched |
2b43685d |
2745 | || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0) |
2746 | pfomask = 1 << pfo; |
2747 | } |
2748 | if (pfomask) { |
2749 | tmp_op->pfomask |= pfomask; |
2750 | cmp_result_vars |= pfomask; |
2b43685d |
2751 | } |
04f8a628 |
2752 | // note: may overwrite, currently not a problem |
2753 | po->datap = tmp_op; |
d4e3b5db |
2754 | } |
2755 | |
2756 | if (po->op == OP_ADC || po->op == OP_SBB) |
2757 | cmp_result_vars |= 1 << PFO_C; |
2758 | } |
87bf6cec |
2759 | else if (po->op == OP_MUL |
2760 | || (po->op == OP_IMUL && po->operand_cnt == 1)) |
2761 | { |
2762 | need_mul_var = 1; |
2763 | } |
1bafb621 |
2764 | else if (po->op == OP_CALL && po->operand[0].type != OPT_LABEL) { |
2765 | pp = po->datap; |
2766 | my_assert_not(pp, NULL); |
3ebea2cf |
2767 | fprintf(fout, " %s (*icall%d)(", pp->ret_type.name, i); |
1bafb621 |
2768 | for (j = 0; j < pp->argc; j++) { |
2769 | if (j > 0) |
2770 | fprintf(fout, ", "); |
3ebea2cf |
2771 | fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1); |
1bafb621 |
2772 | } |
2773 | fprintf(fout, ");\n"); |
2774 | } |
91977a1c |
2775 | } |
2776 | |
4c45fa73 |
2777 | // output LUTs/jumptables |
2778 | for (i = 0; i < g_func_pd_cnt; i++) { |
2779 | pd = &g_func_pd[i]; |
2780 | fprintf(fout, " static const "); |
2781 | if (pd->type == OPT_OFFSET) { |
2782 | fprintf(fout, "void *jt_%s[] =\n { ", pd->label); |
2783 | |
2784 | for (j = 0; j < pd->count; j++) { |
2785 | if (j > 0) |
2786 | fprintf(fout, ", "); |
2787 | fprintf(fout, "&&%s", pd->d[j].u.label); |
2788 | } |
2789 | } |
2790 | else { |
2791 | fprintf(fout, "%s %s[] =\n { ", |
2792 | lmod_type_u(ops, pd->lmod), pd->label); |
2793 | |
2794 | for (j = 0; j < pd->count; j++) { |
2795 | if (j > 0) |
2796 | fprintf(fout, ", "); |
2797 | fprintf(fout, "%u", pd->d[j].u.val); |
2798 | } |
2799 | } |
2800 | fprintf(fout, " };\n"); |
2801 | } |
2802 | |
4f12f671 |
2803 | // declare stack frame, va_arg |
1bafb621 |
2804 | if (g_stack_fsz) |
850c9265 |
2805 | fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n", |
1bafb621 |
2806 | (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz); |
850c9265 |
2807 | |
bd96f656 |
2808 | if (g_func_pp->is_vararg) |
4f12f671 |
2809 | fprintf(fout, " va_list ap;\n"); |
2810 | |
940e8e66 |
2811 | // declare arg-registers |
bd96f656 |
2812 | for (i = 0; i < g_func_pp->argc; i++) { |
2813 | if (g_func_pp->arg[i].reg != NULL) { |
91977a1c |
2814 | reg = char_array_i(regs_r32, |
bd96f656 |
2815 | ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg); |
91977a1c |
2816 | if (reg < 0) |
bd96f656 |
2817 | ferr(ops, "arg '%s' is not a reg?\n", g_func_pp->arg[i].reg); |
91977a1c |
2818 | |
2819 | regmask_arg |= 1 << reg; |
2820 | fprintf(fout, " u32 %s = (u32)a%d;\n", |
bd96f656 |
2821 | g_func_pp->arg[i].reg, i + 1); |
91977a1c |
2822 | had_decl = 1; |
2823 | } |
2824 | } |
2825 | |
940e8e66 |
2826 | // declare other regs - special case for eax |
a3684be1 |
2827 | if (!((regmask | regmask_arg) & 1) && have_func_ret |
bd96f656 |
2828 | && !IS(g_func_pp->ret_type.name, "void")) |
3ebea2cf |
2829 | { |
91977a1c |
2830 | fprintf(fout, " u32 eax = 0;\n"); |
2831 | had_decl = 1; |
2832 | } |
2833 | |
2834 | regmask &= ~regmask_arg; |
d4e3b5db |
2835 | regmask &= ~(1 << xSP); |
91977a1c |
2836 | if (regmask) { |
2837 | for (reg = 0; reg < 8; reg++) { |
2838 | if (regmask & (1 << reg)) { |
2839 | fprintf(fout, " u32 %s;\n", regs_r32[reg]); |
2840 | had_decl = 1; |
2841 | } |
2842 | } |
2843 | } |
2844 | |
d4e3b5db |
2845 | if (regmask_save) { |
2846 | for (reg = 0; reg < 8; reg++) { |
2847 | if (regmask_save & (1 << reg)) { |
2848 | fprintf(fout, " u32 s_%s;\n", regs_r32[reg]); |
2849 | had_decl = 1; |
2850 | } |
2851 | } |
2852 | } |
2853 | |
69a3cdfc |
2854 | if (save_arg_vars) { |
2855 | for (reg = 0; reg < 32; reg++) { |
2856 | if (save_arg_vars & (1 << reg)) { |
2857 | fprintf(fout, " u32 s_a%d;\n", reg + 1); |
2858 | had_decl = 1; |
2859 | } |
2860 | } |
2861 | } |
2862 | |
2863 | if (cmp_result_vars) { |
2864 | for (i = 0; i < 8; i++) { |
2865 | if (cmp_result_vars & (1 << i)) { |
2866 | fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]); |
2867 | had_decl = 1; |
2868 | } |
2869 | } |
2870 | } |
2871 | |
87bf6cec |
2872 | if (need_mul_var) { |
2873 | fprintf(fout, " u64 mul_tmp;\n"); |
2874 | had_decl = 1; |
2875 | } |
2876 | |
91977a1c |
2877 | if (had_decl) |
2878 | fprintf(fout, "\n"); |
2879 | |
bd96f656 |
2880 | if (g_func_pp->is_vararg) { |
2881 | if (g_func_pp->argc_stack == 0) |
4f12f671 |
2882 | ferr(ops, "vararg func without stack args?\n"); |
bd96f656 |
2883 | fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc); |
4f12f671 |
2884 | } |
2885 | |
91977a1c |
2886 | // output ops |
69a3cdfc |
2887 | for (i = 0; i < opcnt; i++) |
2888 | { |
3ebea2cf |
2889 | if (g_labels[i][0] != 0 && g_label_refs[i].i != -1) { |
91977a1c |
2890 | fprintf(fout, "\n%s:\n", g_labels[i]); |
3ebea2cf |
2891 | label_pending = 1; |
2b43685d |
2892 | |
2893 | delayed_flag_op = NULL; |
2894 | last_arith_dst = NULL; |
3ebea2cf |
2895 | } |
91977a1c |
2896 | |
69a3cdfc |
2897 | po = &ops[i]; |
2898 | if (po->flags & OPF_RMD) |
91977a1c |
2899 | continue; |
2900 | |
2901 | no_output = 0; |
2902 | |
91977a1c |
2903 | #define assert_operand_cnt(n_) \ |
850c9265 |
2904 | if (po->operand_cnt != n_) \ |
2905 | ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_) |
2906 | |
69a3cdfc |
2907 | // conditional/flag using op? |
2908 | if (po->flags & OPF_CC) |
850c9265 |
2909 | { |
940e8e66 |
2910 | int is_delayed = 0; |
2911 | int is_inv = 0; |
69a3cdfc |
2912 | |
940e8e66 |
2913 | pfo = split_cond(po, po->op, &is_inv); |
04f8a628 |
2914 | tmp_op = po->datap; |
850c9265 |
2915 | |
69a3cdfc |
2916 | // we go through all this trouble to avoid using parsed_flag_op, |
2917 | // which makes generated code much nicer |
2918 | if (delayed_flag_op != NULL) |
850c9265 |
2919 | { |
940e8e66 |
2920 | out_cmp_test(buf1, sizeof(buf1), delayed_flag_op, pfo, is_inv); |
2921 | is_delayed = 1; |
91977a1c |
2922 | } |
850c9265 |
2923 | else if (last_arith_dst != NULL |
04f8a628 |
2924 | && (pfo == PFO_Z || pfo == PFO_S || pfo == PFO_P |
2925 | || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR)) |
2926 | )) |
850c9265 |
2927 | { |
3ebea2cf |
2928 | out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst); |
940e8e66 |
2929 | out_test_for_cc(buf1, sizeof(buf1), po, pfo, is_inv, |
850c9265 |
2930 | last_arith_dst->lmod, buf3); |
940e8e66 |
2931 | is_delayed = 1; |
850c9265 |
2932 | } |
04f8a628 |
2933 | else if (tmp_op != NULL) { |
7ba45c34 |
2934 | // use preprocessed flag calc results |
04f8a628 |
2935 | if (!(tmp_op->pfomask & (1 << pfo))) |
69a3cdfc |
2936 | ferr(po, "not prepared for pfo %d\n", pfo); |
2937 | |
940e8e66 |
2938 | // note: is_inv was not yet applied |
69a3cdfc |
2939 | snprintf(buf1, sizeof(buf1), "(%scond_%s)", |
940e8e66 |
2940 | is_inv ? "!" : "", parsed_flag_op_names[pfo]); |
69a3cdfc |
2941 | } |
2942 | else { |
2943 | ferr(po, "all methods of finding comparison failed\n"); |
2944 | } |
850c9265 |
2945 | |
69a3cdfc |
2946 | if (po->flags & OPF_JMP) { |
850c9265 |
2947 | fprintf(fout, " if %s\n", buf1); |
2948 | } |
5101a5f9 |
2949 | else if (po->op == OP_ADC || po->op == OP_SBB) { |
940e8e66 |
2950 | if (is_delayed) |
2951 | fprintf(fout, " cond_%s = %s;\n", |
2952 | parsed_flag_op_names[pfo], buf1); |
850c9265 |
2953 | } |
5101a5f9 |
2954 | else if (po->flags & OPF_DATA) { // SETcc |
850c9265 |
2955 | out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]); |
2956 | fprintf(fout, " %s = %s;", buf2, buf1); |
91977a1c |
2957 | } |
69a3cdfc |
2958 | else { |
2959 | ferr(po, "unhandled conditional op\n"); |
2960 | } |
91977a1c |
2961 | } |
2962 | |
940e8e66 |
2963 | pfomask = po->pfomask; |
2964 | |
850c9265 |
2965 | switch (po->op) |
91977a1c |
2966 | { |
2967 | case OP_MOV: |
2968 | assert_operand_cnt(2); |
850c9265 |
2969 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
de50b98b |
2970 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
2971 | fprintf(fout, " %s = %s;", buf1, |
3ebea2cf |
2972 | out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], |
2973 | po->operand[0].is_ptr ? "(void *)" : "", 0)); |
850c9265 |
2974 | break; |
2975 | |
2976 | case OP_LEA: |
2977 | assert_operand_cnt(2); |
87bf6cec |
2978 | po->operand[1].lmod = OPLM_DWORD; // always |
850c9265 |
2979 | fprintf(fout, " %s = %s;", |
2980 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
3ebea2cf |
2981 | out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], |
2982 | NULL, 1)); |
850c9265 |
2983 | break; |
2984 | |
2985 | case OP_MOVZX: |
2986 | assert_operand_cnt(2); |
91977a1c |
2987 | fprintf(fout, " %s = %s;", |
850c9265 |
2988 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
3ebea2cf |
2989 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
850c9265 |
2990 | break; |
2991 | |
2992 | case OP_MOVSX: |
2993 | assert_operand_cnt(2); |
2994 | switch (po->operand[1].lmod) { |
2995 | case OPLM_BYTE: |
2996 | strcpy(buf3, "(s8)"); |
2997 | break; |
2998 | case OPLM_WORD: |
2999 | strcpy(buf3, "(s16)"); |
3000 | break; |
3001 | default: |
3002 | ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod); |
3003 | } |
3004 | fprintf(fout, " %s = %s%s;", |
3005 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
3006 | buf3, |
3ebea2cf |
3007 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
850c9265 |
3008 | break; |
3009 | |
3010 | case OP_NOT: |
3011 | assert_operand_cnt(1); |
3012 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
3013 | fprintf(fout, " %s = ~%s;", buf1, buf1); |
3014 | break; |
3015 | |
5101a5f9 |
3016 | case OP_CDQ: |
3017 | assert_operand_cnt(2); |
3018 | fprintf(fout, " %s = (s32)%s >> 31;", |
3019 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
3ebea2cf |
3020 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
5101a5f9 |
3021 | strcpy(g_comment, "cdq"); |
3022 | break; |
3023 | |
33c35af6 |
3024 | case OP_STOS: |
3025 | // assumes DF=0 |
3026 | assert_operand_cnt(3); |
3027 | if (po->flags & OPF_REP) { |
3028 | fprintf(fout, " for (; ecx != 0; ecx--, edi += %d)\n", |
3029 | lmod_bytes(po, po->operand[0].lmod)); |
d4e3b5db |
3030 | fprintf(fout, " %sedi = eax;", |
3031 | lmod_cast_u_ptr(po, po->operand[0].lmod)); |
33c35af6 |
3032 | strcpy(g_comment, "rep stos"); |
3033 | } |
3034 | else { |
d4e3b5db |
3035 | fprintf(fout, " %sedi = eax; edi += %d;", |
3036 | lmod_cast_u_ptr(po, po->operand[0].lmod), |
33c35af6 |
3037 | lmod_bytes(po, po->operand[0].lmod)); |
3038 | strcpy(g_comment, "stos"); |
3039 | } |
3040 | break; |
3041 | |
d4e3b5db |
3042 | case OP_MOVS: |
3043 | // assumes DF=0 |
3044 | assert_operand_cnt(3); |
3045 | j = lmod_bytes(po, po->operand[0].lmod); |
3046 | strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod)); |
3047 | if (po->flags & OPF_REP) { |
3048 | fprintf(fout, |
3049 | " for (; ecx != 0; ecx--, edi += %d, esi += %d)\n", |
3050 | j, j); |
3051 | fprintf(fout, |
3052 | " %sedi = %sesi;", buf1, buf1); |
3053 | strcpy(g_comment, "rep movs"); |
3054 | } |
3055 | else { |
3056 | fprintf(fout, " %sedi = %sesi; edi += %d; esi += %d;", |
3057 | buf1, buf1, j, j); |
3058 | strcpy(g_comment, "movs"); |
3059 | } |
3060 | break; |
3061 | |
7ba45c34 |
3062 | case OP_CMPS: |
3063 | // assumes DF=0 |
3064 | // repe ~ repeat while ZF=1 |
3065 | assert_operand_cnt(3); |
3066 | j = lmod_bytes(po, po->operand[0].lmod); |
3067 | strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod)); |
3068 | if (po->flags & OPF_REP) { |
3069 | fprintf(fout, |
3070 | " for (; ecx != 0; ecx--, edi += %d, esi += %d)\n", |
3071 | j, j); |
3072 | fprintf(fout, |
3073 | " if ((cond_z = (%sedi == %sesi)) %s 0)\n", |
3074 | buf1, buf1, (po->flags & OPF_REPZ) ? "==" : "!="); |
3075 | fprintf(fout, |
3076 | " break;"); |
3077 | snprintf(g_comment, sizeof(g_comment), "rep%s cmps", |
3078 | (po->flags & OPF_REPZ) ? "e" : "ne"); |
3079 | } |
3080 | else { |
3081 | fprintf(fout, |
3082 | " cond_z = (%sedi = %sesi); edi += %d; esi += %d;", |
3083 | buf1, buf1, j, j); |
3084 | strcpy(g_comment, "cmps"); |
3085 | } |
3086 | pfomask &= ~(1 << PFO_Z); |
3087 | last_arith_dst = NULL; |
3088 | delayed_flag_op = NULL; |
3089 | break; |
3090 | |
850c9265 |
3091 | // arithmetic w/flags |
3092 | case OP_ADD: |
3093 | case OP_SUB: |
3094 | case OP_AND: |
3095 | case OP_OR: |
5101a5f9 |
3096 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
3097 | // fallthrough |
850c9265 |
3098 | dualop_arith: |
3099 | assert_operand_cnt(2); |
850c9265 |
3100 | fprintf(fout, " %s %s= %s;", |
3101 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
3102 | op_to_c(po), |
3ebea2cf |
3103 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
04f8a628 |
3104 | output_std_flags(fout, po, &pfomask, buf1); |
3105 | last_arith_dst = &po->operand[0]; |
3106 | delayed_flag_op = NULL; |
3107 | break; |
3108 | |
3109 | case OP_SHL: |
3110 | case OP_SHR: |
3111 | assert_operand_cnt(2); |
3112 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
3113 | if (pfomask & (1 << PFO_C)) { |
3114 | if (po->operand[1].type == OPT_CONST) { |
3115 | l = lmod_bytes(po, po->operand[0].lmod) * 8; |
3116 | j = po->operand[1].val; |
3117 | j %= l; |
3118 | if (j != 0) { |
3119 | if (po->op == OP_SHL) |
3120 | j = l - j; |
3121 | else |
3122 | j -= 1; |
3123 | fprintf(fout, " cond_c = (%s & 0x%02x) ? 1 : 0;\n", |
3124 | buf1, 1 << j); |
3125 | } |
3126 | else |
3127 | ferr(po, "zero shift?\n"); |
3128 | } |
3129 | else |
3130 | ferr(po, "TODO\n"); |
3131 | pfomask &= ~(1 << PFO_C); |
840257f6 |
3132 | } |
04f8a628 |
3133 | fprintf(fout, " %s %s= %s;", buf1, op_to_c(po), |
3134 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
3135 | output_std_flags(fout, po, &pfomask, buf1); |
850c9265 |
3136 | last_arith_dst = &po->operand[0]; |
69a3cdfc |
3137 | delayed_flag_op = NULL; |
850c9265 |
3138 | break; |
3139 | |
d4e3b5db |
3140 | case OP_SAR: |
3141 | assert_operand_cnt(2); |
3142 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
3143 | fprintf(fout, " %s = %s%s >> %s;", buf1, |
3144 | lmod_cast_s(po, po->operand[0].lmod), buf1, |
3ebea2cf |
3145 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
04f8a628 |
3146 | output_std_flags(fout, po, &pfomask, buf1); |
d4e3b5db |
3147 | last_arith_dst = &po->operand[0]; |
3148 | delayed_flag_op = NULL; |
3149 | break; |
3150 | |
3151 | case OP_ROL: |
3152 | case OP_ROR: |
3153 | assert_operand_cnt(2); |
3154 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
3155 | if (po->operand[1].type == OPT_CONST) { |
3156 | j = po->operand[1].val; |
3157 | j %= lmod_bytes(po, po->operand[0].lmod) * 8; |
3158 | fprintf(fout, po->op == OP_ROL ? |
3159 | " %s = (%s << %d) | (%s >> %d);" : |
3160 | " %s = (%s >> %d) | (%s << %d);", |
3161 | buf1, buf1, j, buf1, |
3162 | lmod_bytes(po, po->operand[0].lmod) * 8 - j); |
3163 | } |
3164 | else |
3165 | ferr(po, "TODO\n"); |
04f8a628 |
3166 | output_std_flags(fout, po, &pfomask, buf1); |
d4e3b5db |
3167 | last_arith_dst = &po->operand[0]; |
3168 | delayed_flag_op = NULL; |
3169 | break; |
3170 | |
5101a5f9 |
3171 | case OP_XOR: |
850c9265 |
3172 | assert_operand_cnt(2); |
3173 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
5101a5f9 |
3174 | if (IS(opr_name(po, 0), opr_name(po, 1))) { |
3175 | // special case for XOR |
3176 | fprintf(fout, " %s = 0;", |
3177 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0])); |
3178 | last_arith_dst = &po->operand[0]; |
3179 | delayed_flag_op = NULL; |
850c9265 |
3180 | break; |
850c9265 |
3181 | } |
5101a5f9 |
3182 | goto dualop_arith; |
3183 | |
5101a5f9 |
3184 | case OP_ADC: |
850c9265 |
3185 | case OP_SBB: |
5101a5f9 |
3186 | assert_operand_cnt(2); |
3187 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
840257f6 |
3188 | if (po->op == OP_SBB |
3189 | && IS(po->operand[0].name, po->operand[1].name)) |
3190 | { |
3191 | // avoid use of unitialized var |
3192 | fprintf(fout, " %s = -cond_c;", |
3193 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0])); |
3194 | } |
3195 | else { |
3196 | fprintf(fout, " %s %s= %s + cond_c;", |
5101a5f9 |
3197 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
3198 | op_to_c(po), |
3ebea2cf |
3199 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
840257f6 |
3200 | } |
5101a5f9 |
3201 | last_arith_dst = &po->operand[0]; |
3202 | delayed_flag_op = NULL; |
850c9265 |
3203 | break; |
3204 | |
3205 | case OP_INC: |
3206 | case OP_DEC: |
3207 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
1bafb621 |
3208 | if (po->operand[0].type == OPT_REG) { |
3209 | strcpy(buf2, po->op == OP_INC ? "++" : "--"); |
3210 | fprintf(fout, " %s%s;", buf1, buf2); |
3211 | } |
3212 | else { |
3213 | strcpy(buf2, po->op == OP_INC ? "+" : "-"); |
3214 | fprintf(fout, " %s %s= 1;", buf1, buf2); |
3215 | } |
5101a5f9 |
3216 | last_arith_dst = &po->operand[0]; |
3217 | delayed_flag_op = NULL; |
3218 | break; |
3219 | |
3220 | case OP_NEG: |
3221 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
3ebea2cf |
3222 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]); |
5101a5f9 |
3223 | fprintf(fout, " %s = -%s%s;", buf1, |
3224 | lmod_cast_s(po, po->operand[0].lmod), buf2); |
850c9265 |
3225 | last_arith_dst = &po->operand[0]; |
69a3cdfc |
3226 | delayed_flag_op = NULL; |
940e8e66 |
3227 | if (pfomask & (1 << PFO_C)) { |
3228 | fprintf(fout, "\n cond_c = (%s != 0);", buf1); |
3229 | pfomask &= ~(1 << PFO_C); |
3230 | } |
850c9265 |
3231 | break; |
3232 | |
3233 | case OP_IMUL: |
de50b98b |
3234 | if (po->operand_cnt == 2) { |
3235 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
850c9265 |
3236 | goto dualop_arith; |
de50b98b |
3237 | } |
87bf6cec |
3238 | if (po->operand_cnt == 3) |
3239 | ferr(po, "TODO imul3\n"); |
3240 | // fallthrough |
3241 | case OP_MUL: |
3242 | assert_operand_cnt(1); |
3243 | strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)"); |
3244 | fprintf(fout, " mul_tmp = %seax * %s%s;\n", buf1, buf1, |
3ebea2cf |
3245 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0])); |
87bf6cec |
3246 | fprintf(fout, " edx = mul_tmp >> 32;\n"); |
3247 | fprintf(fout, " eax = mul_tmp;"); |
3248 | last_arith_dst = NULL; |
69a3cdfc |
3249 | delayed_flag_op = NULL; |
91977a1c |
3250 | break; |
3251 | |
5101a5f9 |
3252 | case OP_DIV: |
3253 | case OP_IDIV: |
3254 | assert_operand_cnt(1); |
3255 | if (po->operand[0].lmod != OPLM_DWORD) |
3256 | ferr(po, "unhandled lmod %d\n", po->operand[0].lmod); |
3257 | |
3258 | // 32bit division is common, look for it |
64c59faf |
3259 | if (po->op == OP_DIV) |
cdfaeed7 |
3260 | ret = scan_for_reg_clear(i, xDX); |
64c59faf |
3261 | else |
cdfaeed7 |
3262 | ret = scan_for_cdq_edx(i); |
64c59faf |
3263 | if (ret >= 0) { |
3ebea2cf |
3264 | out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]); |
5101a5f9 |
3265 | strcpy(buf2, lmod_cast(po, po->operand[0].lmod, |
3266 | po->op == OP_IDIV)); |
3267 | fprintf(fout, " edx = %seax %% %s%s;\n", buf2, buf2, buf1); |
3268 | fprintf(fout, " eax = %seax / %s%s;", buf2, buf2, buf1); |
3269 | } |
3270 | else |
3271 | ferr(po, "TODO 64bit divident\n"); |
87bf6cec |
3272 | last_arith_dst = NULL; |
3273 | delayed_flag_op = NULL; |
5101a5f9 |
3274 | break; |
3275 | |
91977a1c |
3276 | case OP_TEST: |
3277 | case OP_CMP: |
850c9265 |
3278 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
940e8e66 |
3279 | if (pfomask != 0) { |
69a3cdfc |
3280 | for (j = 0; j < 8; j++) { |
940e8e66 |
3281 | if (pfomask & (1 << j)) { |
69a3cdfc |
3282 | out_cmp_test(buf1, sizeof(buf1), po, j, 0); |
3283 | fprintf(fout, " cond_%s = %s;", |
3284 | parsed_flag_op_names[j], buf1); |
3285 | } |
3286 | } |
940e8e66 |
3287 | pfomask = 0; |
69a3cdfc |
3288 | } |
3289 | else |
3290 | no_output = 1; |
7ba45c34 |
3291 | last_arith_dst = NULL; |
69a3cdfc |
3292 | delayed_flag_op = po; |
91977a1c |
3293 | break; |
3294 | |
69a3cdfc |
3295 | // note: we reuse OP_Jcc for SETcc, only flags differ |
91977a1c |
3296 | case OP_JO ... OP_JG: |
5101a5f9 |
3297 | if (po->flags & OPF_JMP) |
850c9265 |
3298 | fprintf(fout, " goto %s;", po->operand[0].name); |
5101a5f9 |
3299 | // else SETcc - should already be handled |
850c9265 |
3300 | break; |
3301 | |
3302 | case OP_JMP: |
87bf6cec |
3303 | assert_operand_cnt(1); |
de50b98b |
3304 | last_arith_dst = NULL; |
3305 | delayed_flag_op = NULL; |
3306 | |
4c45fa73 |
3307 | if (po->operand[0].type == OPT_REGMEM) { |
3308 | ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]", |
3309 | buf1, buf2); |
3310 | if (ret != 2) |
3311 | ferr(po, "parse failure for jmp '%s'\n", |
3312 | po->operand[0].name); |
3313 | fprintf(fout, " goto *jt_%s[%s];", buf1, buf2); |
3314 | break; |
3315 | } |
3316 | else if (po->operand[0].type != OPT_LABEL) |
3317 | ferr(po, "unhandled jmp type\n"); |
87bf6cec |
3318 | |
850c9265 |
3319 | fprintf(fout, " goto %s;", po->operand[0].name); |
91977a1c |
3320 | break; |
3321 | |
3322 | case OP_CALL: |
5101a5f9 |
3323 | assert_operand_cnt(1); |
850c9265 |
3324 | pp = po->datap; |
91977a1c |
3325 | if (pp == NULL) |
850c9265 |
3326 | ferr(po, "NULL pp\n"); |
91977a1c |
3327 | |
1bafb621 |
3328 | if (po->operand[0].type != OPT_LABEL) |
3329 | fprintf(fout, " icall%d = (void *)%s;\n", i, |
3ebea2cf |
3330 | out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0])); |
1bafb621 |
3331 | |
91977a1c |
3332 | fprintf(fout, " "); |
2b43685d |
3333 | if (strstr(pp->ret_type.name, "int64")) { |
87bf6cec |
3334 | if (po->flags & OPF_TAIL) |
2b43685d |
3335 | ferr(po, "int64 and tail?\n"); |
3336 | fprintf(fout, "mul_tmp = "); |
3337 | } |
3338 | else if (!IS(pp->ret_type.name, "void")) { |
3339 | if (po->flags & OPF_TAIL) { |
840257f6 |
3340 | if (!IS(g_func_pp->ret_type.name, "void")) { |
3341 | fprintf(fout, "return "); |
3342 | if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr) |
3343 | fprintf(fout, "(%s)", g_func_pp->ret_type.name); |
3344 | } |
2b43685d |
3345 | } |
3346 | else { |
87bf6cec |
3347 | fprintf(fout, "eax = "); |
2b43685d |
3348 | if (pp->ret_type.is_ptr) |
3349 | fprintf(fout, "(u32)"); |
3350 | } |
91977a1c |
3351 | } |
87bf6cec |
3352 | |
39b168b8 |
3353 | if (po->operand[0].type != OPT_LABEL) { |
1bafb621 |
3354 | fprintf(fout, "icall%d(", i); |
39b168b8 |
3355 | } |
3356 | else { |
3357 | if (pp->name[0] == 0) |
3358 | ferr(po, "missing pp->name\n"); |
a652aa9f |
3359 | fprintf(fout, "%s%s(", pp->name, |
3360 | pp->has_structarg ? "_sa" : ""); |
39b168b8 |
3361 | } |
3362 | |
91977a1c |
3363 | for (arg = 0; arg < pp->argc; arg++) { |
3364 | if (arg > 0) |
3365 | fprintf(fout, ", "); |
87bf6cec |
3366 | |
3ebea2cf |
3367 | cast[0] = 0; |
3368 | if (pp->arg[arg].type.is_ptr) |
3369 | snprintf(cast, sizeof(cast), "(%s)", pp->arg[arg].type.name); |
87bf6cec |
3370 | |
91977a1c |
3371 | if (pp->arg[arg].reg != NULL) { |
3ebea2cf |
3372 | fprintf(fout, "%s%s", cast, pp->arg[arg].reg); |
91977a1c |
3373 | continue; |
3374 | } |
3375 | |
3376 | // stack arg |
3377 | tmp_op = pp->arg[arg].datap; |
3378 | if (tmp_op == NULL) |
850c9265 |
3379 | ferr(po, "parsed_op missing for arg%d\n", arg); |
de50b98b |
3380 | if (tmp_op->argnum != 0) { |
3381 | fprintf(fout, "%ss_a%d", cast, tmp_op->argnum); |
69a3cdfc |
3382 | } |
3383 | else { |
3384 | fprintf(fout, "%s", |
3385 | out_src_opr(buf1, sizeof(buf1), |
3ebea2cf |
3386 | tmp_op, &tmp_op->operand[0], cast, 0)); |
69a3cdfc |
3387 | } |
91977a1c |
3388 | } |
3389 | fprintf(fout, ");"); |
87bf6cec |
3390 | |
2b43685d |
3391 | if (strstr(pp->ret_type.name, "int64")) { |
3392 | fprintf(fout, "\n"); |
3393 | fprintf(fout, " edx = mul_tmp >> 32;\n"); |
3394 | fprintf(fout, " eax = mul_tmp;"); |
3395 | } |
3396 | |
87bf6cec |
3397 | if (po->flags & OPF_TAIL) { |
3398 | strcpy(g_comment, "tailcall"); |
840257f6 |
3399 | ret = 0; |
3400 | if (i == opcnt - 1) |
3401 | ret = 0; |
3402 | else if (IS(pp->ret_type.name, "void")) |
3403 | ret = 1; |
3404 | else if (IS(g_func_pp->ret_type.name, "void")) |
3405 | ret = 1; |
3406 | // else already handled as 'return f()' |
3407 | |
3408 | if (ret) { |
87bf6cec |
3409 | fprintf(fout, "\n return;"); |
3ebea2cf |
3410 | strcpy(g_comment, "^ tailcall"); |
3411 | } |
87bf6cec |
3412 | } |
3413 | delayed_flag_op = NULL; |
3414 | last_arith_dst = NULL; |
91977a1c |
3415 | break; |
3416 | |
3417 | case OP_RET: |
bd96f656 |
3418 | if (g_func_pp->is_vararg) |
4f12f671 |
3419 | fprintf(fout, " va_end(ap);\n"); |
3420 | |
bd96f656 |
3421 | if (IS(g_func_pp->ret_type.name, "void")) { |
3ebea2cf |
3422 | if (i != opcnt - 1 || label_pending) |
3423 | fprintf(fout, " return;"); |
3424 | } |
bd96f656 |
3425 | else if (g_func_pp->ret_type.is_ptr) { |
d4e3b5db |
3426 | fprintf(fout, " return (%s)eax;", |
bd96f656 |
3427 | g_func_pp->ret_type.name); |
3ebea2cf |
3428 | } |
91977a1c |
3429 | else |
3430 | fprintf(fout, " return eax;"); |
de50b98b |
3431 | |
3432 | last_arith_dst = NULL; |
3433 | delayed_flag_op = NULL; |
91977a1c |
3434 | break; |
3435 | |
3436 | case OP_PUSH: |
de50b98b |
3437 | if (po->argnum != 0) { |
69a3cdfc |
3438 | // special case - saved func arg |
3ebea2cf |
3439 | out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]); |
de50b98b |
3440 | fprintf(fout, " s_a%d = %s;", po->argnum, buf1); |
69a3cdfc |
3441 | break; |
3442 | } |
d4e3b5db |
3443 | else if (po->flags & OPF_RSAVE) { |
3ebea2cf |
3444 | out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]); |
d4e3b5db |
3445 | fprintf(fout, " s_%s = %s;", buf1, buf1); |
3446 | break; |
3447 | } |
e56ab892 |
3448 | if (!(g_ida_func_attr & IDAFA_NORETURN)) |
3449 | ferr(po, "stray push encountered\n"); |
3450 | no_output = 1; |
91977a1c |
3451 | break; |
3452 | |
3453 | case OP_POP: |
d4e3b5db |
3454 | if (po->flags & OPF_RSAVE) { |
3ebea2cf |
3455 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
d4e3b5db |
3456 | fprintf(fout, " %s = s_%s;", buf1, buf1); |
3457 | break; |
3458 | } |
3459 | ferr(po, "stray pop encountered\n"); |
91977a1c |
3460 | break; |
3461 | |
33c35af6 |
3462 | case OP_NOP: |
2b43685d |
3463 | no_output = 1; |
33c35af6 |
3464 | break; |
3465 | |
91977a1c |
3466 | default: |
3467 | no_output = 1; |
69a3cdfc |
3468 | ferr(po, "unhandled op type %d, flags %x\n", |
3469 | po->op, po->flags); |
91977a1c |
3470 | break; |
3471 | } |
3472 | |
3473 | if (g_comment[0] != 0) { |
3474 | fprintf(fout, " // %s", g_comment); |
3475 | g_comment[0] = 0; |
3476 | no_output = 0; |
3477 | } |
3478 | if (!no_output) |
3479 | fprintf(fout, "\n"); |
5101a5f9 |
3480 | |
2b43685d |
3481 | // some sanity checking |
3482 | if ((po->flags & OPF_REP) && po->op != OP_STOS |
3483 | && po->op != OP_MOVS && po->op != OP_CMPS) |
3484 | ferr(po, "unexpected rep\n"); |
3485 | if ((po->flags & (OPF_REPZ|OPF_REPNZ)) && po->op != OP_CMPS) |
3486 | ferr(po, "unexpected repz/repnz\n"); |
3487 | |
940e8e66 |
3488 | if (pfomask != 0) |
7ba45c34 |
3489 | ferr(po, "missed flag calc, pfomask=%x\n", pfomask); |
940e8e66 |
3490 | |
5101a5f9 |
3491 | // see is delayed flag stuff is still valid |
3492 | if (delayed_flag_op != NULL && delayed_flag_op != po) { |
3493 | if (is_any_opr_modified(delayed_flag_op, po)) |
3494 | delayed_flag_op = NULL; |
3495 | } |
3496 | |
3497 | if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) { |
3498 | if (is_opr_modified(last_arith_dst, po)) |
3499 | last_arith_dst = NULL; |
3500 | } |
3ebea2cf |
3501 | |
3502 | label_pending = 0; |
91977a1c |
3503 | } |
3504 | |
3505 | fprintf(fout, "}\n\n"); |
3506 | |
3507 | // cleanup |
3508 | for (i = 0; i < opcnt; i++) { |
4c45fa73 |
3509 | struct label_ref *lr, *lr_del; |
3510 | |
3511 | lr = g_label_refs[i].next; |
3512 | while (lr != NULL) { |
3513 | lr_del = lr; |
3514 | lr = lr->next; |
3515 | free(lr_del); |
3516 | } |
3517 | g_label_refs[i].i = -1; |
3518 | g_label_refs[i].next = NULL; |
3519 | |
91977a1c |
3520 | if (ops[i].op == OP_CALL) { |
3521 | pp = ops[i].datap; |
bd96f656 |
3522 | if (pp) |
91977a1c |
3523 | proto_release(pp); |
91977a1c |
3524 | } |
3525 | } |
bd96f656 |
3526 | g_func_pp = NULL; |
91977a1c |
3527 | } |
c36e914d |
3528 | |
d4e3b5db |
3529 | static void set_label(int i, const char *name) |
3530 | { |
3531 | const char *p; |
3532 | int len; |
3533 | |
3534 | len = strlen(name); |
3535 | p = strchr(name, ':'); |
3536 | if (p != NULL) |
3537 | len = p - name; |
3538 | |
3539 | if (len > sizeof(g_labels[0]) - 1) |
3540 | aerr("label '%s' too long: %d\n", name, len); |
e56ab892 |
3541 | if (g_labels[i][0] != 0 && !IS_START(g_labels[i], "algn_")) |
3542 | aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]); |
d4e3b5db |
3543 | memcpy(g_labels[i], name, len); |
3544 | g_labels[i][len] = 0; |
3545 | } |
3546 | |
bfa4a6ee |
3547 | // '=' needs special treatment.. |
3548 | static char *next_word_s(char *w, size_t wsize, char *s) |
3549 | { |
3550 | size_t i; |
3551 | |
3552 | s = sskip(s); |
3553 | |
3554 | for (i = 0; i < wsize - 1; i++) { |
3555 | if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0)) |
3556 | break; |
3557 | w[i] = s[i]; |
3558 | } |
3559 | w[i] = 0; |
3560 | |
3561 | if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=') |
3562 | printf("warning: '%s' truncated\n", w); |
3563 | |
3564 | return s + i; |
3565 | } |
3566 | |
e56ab892 |
3567 | struct chunk_item { |
3568 | char *name; |
3569 | long fptr; |
de50b98b |
3570 | int asmln; |
e56ab892 |
3571 | }; |
3572 | |
cdfaeed7 |
3573 | static struct chunk_item *func_chunks; |
3574 | static int func_chunk_cnt; |
3575 | static int func_chunk_alloc; |
3576 | |
3577 | static void add_func_chunk(FILE *fasm, const char *name, int line) |
3578 | { |
3579 | if (func_chunk_cnt >= func_chunk_alloc) { |
3580 | func_chunk_alloc *= 2; |
3581 | func_chunks = realloc(func_chunks, |
3582 | func_chunk_alloc * sizeof(func_chunks[0])); |
3583 | my_assert_not(func_chunks, NULL); |
3584 | } |
3585 | func_chunks[func_chunk_cnt].fptr = ftell(fasm); |
3586 | func_chunks[func_chunk_cnt].name = strdup(name); |
3587 | func_chunks[func_chunk_cnt].asmln = line; |
3588 | func_chunk_cnt++; |
3589 | } |
3590 | |
e56ab892 |
3591 | static int cmp_chunks(const void *p1, const void *p2) |
3592 | { |
3593 | const struct chunk_item *c1 = p1, *c2 = p2; |
3594 | return strcmp(c1->name, c2->name); |
3595 | } |
3596 | |
bfa4a6ee |
3597 | static int cmpstringp(const void *p1, const void *p2) |
3598 | { |
3599 | return strcmp(*(char * const *)p1, *(char * const *)p2); |
3600 | } |
3601 | |
cdfaeed7 |
3602 | static void scan_ahead(FILE *fasm) |
3603 | { |
3604 | char words[2][256]; |
3605 | char line[256]; |
3606 | long oldpos; |
3607 | int oldasmln; |
3608 | int wordc; |
3609 | char *p; |
3610 | int i; |
3611 | |
3612 | oldpos = ftell(fasm); |
3613 | oldasmln = asmln; |
3614 | |
3615 | while (fgets(line, sizeof(line), fasm)) |
3616 | { |
3617 | wordc = 0; |
3618 | asmln++; |
3619 | |
3620 | p = sskip(line); |
3621 | if (*p == 0) |
3622 | continue; |
3623 | |
3624 | if (*p == ';') |
3625 | { |
3626 | // get rid of random tabs |
3627 | for (i = 0; line[i] != 0; i++) |
3628 | if (line[i] == '\t') |
3629 | line[i] = ' '; |
3630 | |
3631 | if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR ")) |
3632 | { |
3633 | p += 30; |
3634 | next_word(words[0], sizeof(words[0]), p); |
3635 | if (words[0][0] == 0) |
3636 | aerr("missing name for func chunk?\n"); |
3637 | |
3638 | add_func_chunk(fasm, words[0], asmln); |
3639 | } |
3640 | continue; |
3641 | } // *p == ';' |
3642 | |
3643 | for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) { |
3644 | words[wordc][0] = 0; |
3645 | p = sskip(next_word_s(words[wordc], sizeof(words[0]), p)); |
3646 | if (*p == 0 || *p == ';') { |
3647 | wordc++; |
3648 | break; |
3649 | } |
3650 | } |
3651 | |
3652 | if (wordc == 2 && IS(words[1], "ends")) |
3653 | break; |
3654 | } |
3655 | |
3656 | fseek(fasm, oldpos, SEEK_SET); |
3657 | asmln = oldasmln; |
3658 | } |
3659 | |
91977a1c |
3660 | int main(int argc, char *argv[]) |
3661 | { |
06c5d854 |
3662 | FILE *fout, *fasm, *frlist; |
4c45fa73 |
3663 | struct parsed_data *pd = NULL; |
3664 | int pd_alloc = 0; |
3665 | char **rlist = NULL; |
3666 | int rlist_len = 0; |
3667 | int rlist_alloc = 0; |
e56ab892 |
3668 | int func_chunks_used = 0; |
3669 | int func_chunks_sorted = 0; |
e56ab892 |
3670 | int func_chunk_i = -1; |
3671 | long func_chunk_ret = 0; |
de50b98b |
3672 | int func_chunk_ret_ln = 0; |
cdfaeed7 |
3673 | int scanned_ahead = 0; |
91977a1c |
3674 | char line[256]; |
3675 | char words[16][256]; |
4c45fa73 |
3676 | enum opr_lenmod lmod; |
91977a1c |
3677 | int in_func = 0; |
4c45fa73 |
3678 | int pending_endp = 0; |
bfa4a6ee |
3679 | int skip_func = 0; |
940e8e66 |
3680 | int skip_warned = 0; |
91977a1c |
3681 | int eq_alloc; |
bfa4a6ee |
3682 | int verbose = 0; |
3683 | int arg_out; |
3684 | int arg = 1; |
91977a1c |
3685 | int pi = 0; |
e56ab892 |
3686 | int i, j; |
3687 | int ret, len; |
91977a1c |
3688 | char *p; |
3689 | int wordc; |
3690 | |
bfa4a6ee |
3691 | if (argv[1] && IS(argv[1], "-v")) { |
3692 | verbose = 1; |
3693 | arg++; |
3694 | } |
3695 | |
3696 | if (argc < arg + 3) { |
3697 | printf("usage:\n%s [-v] <.c> <.asm> <hdrf> [rlist]*\n", |
91977a1c |
3698 | argv[0]); |
3699 | return 1; |
3700 | } |
3701 | |
bfa4a6ee |
3702 | arg_out = arg++; |
91977a1c |
3703 | |
bfa4a6ee |
3704 | asmfn = argv[arg++]; |
91977a1c |
3705 | fasm = fopen(asmfn, "r"); |
3706 | my_assert_not(fasm, NULL); |
3707 | |
bfa4a6ee |
3708 | hdrfn = argv[arg++]; |
06c5d854 |
3709 | g_fhdr = fopen(hdrfn, "r"); |
3710 | my_assert_not(g_fhdr, NULL); |
bfa4a6ee |
3711 | |
3712 | rlist_alloc = 64; |
3713 | rlist = malloc(rlist_alloc * sizeof(rlist[0])); |
3714 | my_assert_not(rlist, NULL); |
3715 | // needs special handling.. |
3716 | rlist[rlist_len++] = "__alloca_probe"; |
3717 | |
e56ab892 |
3718 | func_chunk_alloc = 32; |
3719 | func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0])); |
3720 | my_assert_not(func_chunks, NULL); |
3721 | |
bfa4a6ee |
3722 | for (; arg < argc; arg++) { |
3723 | frlist = fopen(argv[arg], "r"); |
3724 | my_assert_not(frlist, NULL); |
3725 | |
3726 | while (fgets(line, sizeof(line), frlist)) { |
3727 | p = sskip(line); |
3728 | if (*p == 0 || *p == ';' || *p == '#') |
3729 | continue; |
3730 | |
3731 | p = next_word(words[0], sizeof(words[0]), p); |
3732 | if (words[0][0] == 0) |
3733 | continue; |
3734 | |
3735 | if (rlist_len >= rlist_alloc) { |
3736 | rlist_alloc = rlist_alloc * 2 + 64; |
3737 | rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0])); |
3738 | my_assert_not(rlist, NULL); |
3739 | } |
3740 | rlist[rlist_len++] = strdup(words[0]); |
3741 | } |
3742 | |
3743 | fclose(frlist); |
3744 | frlist = NULL; |
3745 | } |
3746 | |
3747 | if (rlist_len > 0) |
3748 | qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp); |
3749 | |
3750 | fout = fopen(argv[arg_out], "w"); |
91977a1c |
3751 | my_assert_not(fout, NULL); |
3752 | |
3753 | eq_alloc = 128; |
3754 | g_eqs = malloc(eq_alloc * sizeof(g_eqs[0])); |
3755 | my_assert_not(g_eqs, NULL); |
3756 | |
4c45fa73 |
3757 | for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) { |
3758 | g_label_refs[i].i = -1; |
3759 | g_label_refs[i].next = NULL; |
3760 | } |
3761 | |
91977a1c |
3762 | while (fgets(line, sizeof(line), fasm)) |
3763 | { |
4c45fa73 |
3764 | wordc = 0; |
91977a1c |
3765 | asmln++; |
3766 | |
3767 | p = sskip(line); |
1bafb621 |
3768 | if (*p == 0) |
91977a1c |
3769 | continue; |
3770 | |
de50b98b |
3771 | // get rid of random tabs |
3772 | for (i = 0; line[i] != 0; i++) |
3773 | if (line[i] == '\t') |
3774 | line[i] = ' '; |
3775 | |
e56ab892 |
3776 | if (*p == ';') |
3777 | { |
e56ab892 |
3778 | if (p[2] == '=' && IS_START(p, "; =============== S U B")) |
3779 | goto do_pending_endp; // eww.. |
3780 | |
3781 | if (p[2] == 'A' && IS_START(p, "; Attributes:")) |
3782 | { |
3783 | static const char *attrs[] = { |
3784 | "bp-based frame", |
3785 | "library function", |
3786 | "static", |
3787 | "noreturn", |
3788 | "thunk", |
3789 | "fpd=", |
3790 | }; |
3791 | |
3792 | // parse IDA's attribute-list comment |
3793 | g_ida_func_attr = 0; |
3794 | p = sskip(p + 13); |
3795 | |
3796 | for (; *p != 0; p = sskip(p)) { |
3797 | for (i = 0; i < ARRAY_SIZE(attrs); i++) { |
3798 | if (!strncmp(p, attrs[i], strlen(attrs[i]))) { |
3799 | g_ida_func_attr |= 1 << i; |
3800 | p += strlen(attrs[i]); |
3801 | break; |
3802 | } |
3803 | } |
3804 | if (i == ARRAY_SIZE(attrs)) { |
3805 | anote("unparsed IDA attr: %s\n", p); |
1bafb621 |
3806 | break; |
3807 | } |
e56ab892 |
3808 | if (IS(attrs[i], "fpd=")) { |
3809 | p = next_word(words[0], sizeof(words[0]), p); |
3810 | // ignore for now.. |
3811 | } |
1bafb621 |
3812 | } |
e56ab892 |
3813 | } |
3814 | else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR ")) |
3815 | { |
3816 | p += 30; |
3817 | next_word(words[0], sizeof(words[0]), p); |
3818 | if (words[0][0] == 0) |
cdfaeed7 |
3819 | aerr("missing name for func chunk?\n"); |
3820 | |
3821 | if (!scanned_ahead) { |
3822 | add_func_chunk(fasm, words[0], asmln); |
3823 | func_chunks_sorted = 0; |
e56ab892 |
3824 | } |
e56ab892 |
3825 | } |
3826 | else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK")) |
3827 | { |
3828 | if (func_chunk_i >= 0) { |
3829 | if (func_chunk_i < func_chunk_cnt |
3830 | && IS(func_chunks[func_chunk_i].name, g_func)) |
3831 | { |
3832 | // move on to next chunk |
3833 | ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET); |
3834 | if (ret) |
3835 | aerr("seek failed for '%s' chunk #%d\n", |
3836 | g_func, func_chunk_i); |
de50b98b |
3837 | asmln = func_chunks[func_chunk_i].asmln; |
e56ab892 |
3838 | func_chunk_i++; |
3839 | } |
3840 | else { |
3841 | if (func_chunk_ret == 0) |
3842 | aerr("no return from chunk?\n"); |
3843 | fseek(fasm, func_chunk_ret, SEEK_SET); |
de50b98b |
3844 | asmln = func_chunk_ret_ln; |
e56ab892 |
3845 | func_chunk_ret = 0; |
3846 | pending_endp = 1; |
3847 | } |
1bafb621 |
3848 | } |
e56ab892 |
3849 | } |
3850 | else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) { |
3851 | func_chunks_used = 1; |
3852 | p += 20; |
3853 | if (IS_START(g_func, "sub_")) { |
3854 | unsigned long addr = strtoul(p, NULL, 16); |
3855 | unsigned long f_addr = strtoul(g_func + 4, NULL, 16); |
cdfaeed7 |
3856 | if (addr > f_addr && !scanned_ahead) { |
3857 | anote("scan_ahead caused by '%s', f_addr %lx\n", |
3858 | g_func, f_addr); |
3859 | scan_ahead(fasm); |
3860 | scanned_ahead = 1; |
3861 | func_chunks_sorted = 0; |
3862 | } |
1bafb621 |
3863 | } |
3864 | } |
3865 | continue; |
e56ab892 |
3866 | } // *p == ';' |
1bafb621 |
3867 | |
06c5d854 |
3868 | parse_words: |
91977a1c |
3869 | memset(words, 0, sizeof(words)); |
cdfaeed7 |
3870 | for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) { |
bfa4a6ee |
3871 | p = sskip(next_word_s(words[wordc], sizeof(words[0]), p)); |
91977a1c |
3872 | if (*p == 0 || *p == ';') { |
3873 | wordc++; |
3874 | break; |
3875 | } |
3876 | } |
3877 | |
06c5d854 |
3878 | // alow asm patches in comments |
3ebea2cf |
3879 | if (*p == ';' && IS_START(p, "; sctpatch:")) { |
3880 | p = sskip(p + 11); |
06c5d854 |
3881 | if (*p == 0 || *p == ';') |
3882 | continue; |
3883 | goto parse_words; // lame |
3884 | } |
3885 | |
91977a1c |
3886 | if (wordc == 0) { |
3887 | // shouldn't happen |
3888 | awarn("wordc == 0?\n"); |
3889 | continue; |
3890 | } |
3891 | |
3892 | // don't care about this: |
3893 | if (words[0][0] == '.' |
3894 | || IS(words[0], "include") |
3895 | || IS(words[0], "assume") || IS(words[1], "segment") |
3896 | || IS(words[0], "align")) |
3897 | { |
3898 | continue; |
3899 | } |
3900 | |
4c45fa73 |
3901 | do_pending_endp: |
3902 | // do delayed endp processing to collect switch jumptables |
3903 | if (pending_endp) { |
3904 | if (in_func && !skip_func && wordc >= 2 |
3905 | && ((words[0][0] == 'd' && words[0][2] == 0) |
3906 | || (words[1][0] == 'd' && words[1][2] == 0))) |
3907 | { |
3908 | i = 1; |
3909 | if (words[1][0] == 'd' && words[1][2] == 0) { |
3910 | // label |
3911 | if (g_func_pd_cnt >= pd_alloc) { |
3912 | pd_alloc = pd_alloc * 2 + 16; |
3913 | g_func_pd = realloc(g_func_pd, |
3914 | sizeof(g_func_pd[0]) * pd_alloc); |
3915 | my_assert_not(g_func_pd, NULL); |
3916 | } |
3917 | pd = &g_func_pd[g_func_pd_cnt]; |
3918 | g_func_pd_cnt++; |
3919 | memset(pd, 0, sizeof(*pd)); |
3920 | strcpy(pd->label, words[0]); |
3921 | pd->type = OPT_CONST; |
3922 | pd->lmod = lmod_from_directive(words[1]); |
3923 | i = 2; |
3924 | } |
3925 | else { |
3926 | lmod = lmod_from_directive(words[0]); |
3927 | if (lmod != pd->lmod) |
3928 | aerr("lmod change? %d->%d\n", pd->lmod, lmod); |
3929 | } |
3930 | |
3931 | if (pd->count_alloc < pd->count + wordc) { |
3932 | pd->count_alloc = pd->count_alloc * 2 + 14 + wordc; |
3933 | pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc); |
3934 | my_assert_not(pd->d, NULL); |
3935 | } |
3936 | for (; i < wordc; i++) { |
3937 | if (IS(words[i], "offset")) { |
3938 | pd->type = OPT_OFFSET; |
3939 | i++; |
3940 | } |
3941 | p = strchr(words[i], ','); |
3942 | if (p != NULL) |
3943 | *p = 0; |
3944 | if (pd->type == OPT_OFFSET) |
3945 | pd->d[pd->count].u.label = strdup(words[i]); |
3946 | else |
3947 | pd->d[pd->count].u.val = parse_number(words[i]); |
3948 | pd->d[pd->count].bt_i = -1; |
3949 | pd->count++; |
3950 | } |
3951 | continue; |
3952 | } |
3953 | |
3954 | if (in_func && !skip_func) |
3955 | gen_func(fout, g_fhdr, g_func, pi); |
3956 | |
3957 | pending_endp = 0; |
3958 | in_func = 0; |
3959 | g_ida_func_attr = 0; |
3960 | skip_warned = 0; |
3961 | skip_func = 0; |
3962 | g_func[0] = 0; |
e56ab892 |
3963 | func_chunks_used = 0; |
3964 | func_chunk_i = -1; |
4c45fa73 |
3965 | if (pi != 0) { |
3966 | memset(&ops, 0, pi * sizeof(ops[0])); |
3967 | memset(g_labels, 0, pi * sizeof(g_labels[0])); |
3968 | pi = 0; |
3969 | } |
3970 | g_eqcnt = 0; |
3971 | for (i = 0; i < g_func_pd_cnt; i++) { |
3972 | pd = &g_func_pd[i]; |
3973 | if (pd->type == OPT_OFFSET) { |
3974 | for (j = 0; j < pd->count; j++) |
3975 | free(pd->d[j].u.label); |
3976 | } |
3977 | free(pd->d); |
3978 | pd->d = NULL; |
3979 | } |
3980 | g_func_pd_cnt = 0; |
3981 | pd = NULL; |
3982 | if (wordc == 0) |
3983 | continue; |
3984 | } |
3985 | |
91977a1c |
3986 | if (IS(words[1], "proc")) { |
3987 | if (in_func) |
3988 | aerr("proc '%s' while in_func '%s'?\n", |
3989 | words[0], g_func); |
bfa4a6ee |
3990 | p = words[0]; |
4c45fa73 |
3991 | if ((g_ida_func_attr & IDAFA_THUNK) |
1bafb621 |
3992 | || bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp)) |
bfa4a6ee |
3993 | skip_func = 1; |
91977a1c |
3994 | strcpy(g_func, words[0]); |
d4e3b5db |
3995 | set_label(0, words[0]); |
91977a1c |
3996 | in_func = 1; |
3997 | continue; |
3998 | } |
3999 | |
e56ab892 |
4000 | if (IS(words[1], "endp")) |
4001 | { |
91977a1c |
4002 | if (!in_func) |
4003 | aerr("endp '%s' while not in_func?\n", words[0]); |
4004 | if (!IS(g_func, words[0])) |
4005 | aerr("endp '%s' while in_func '%s'?\n", |
4006 | words[0], g_func); |
bfa4a6ee |
4007 | |
e56ab892 |
4008 | if (!skip_func && func_chunks_used) { |
4009 | // start processing chunks |
4010 | struct chunk_item *ci, key = { g_func, 0 }; |
4011 | |
4012 | func_chunk_ret = ftell(fasm); |
de50b98b |
4013 | func_chunk_ret_ln = asmln; |
e56ab892 |
4014 | if (!func_chunks_sorted) { |
4015 | qsort(func_chunks, func_chunk_cnt, |
4016 | sizeof(func_chunks[0]), cmp_chunks); |
4017 | func_chunks_sorted = 1; |
4018 | } |
4019 | ci = bsearch(&key, func_chunks, func_chunk_cnt, |
4020 | sizeof(func_chunks[0]), cmp_chunks); |
4021 | if (ci == NULL) |
4022 | aerr("'%s' needs chunks, but none found\n", g_func); |
4023 | func_chunk_i = ci - func_chunks; |
4024 | for (; func_chunk_i > 0; func_chunk_i--) |
4025 | if (!IS(func_chunks[func_chunk_i - 1].name, g_func)) |
4026 | break; |
4027 | |
4028 | ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET); |
4029 | if (ret) |
4030 | aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i); |
de50b98b |
4031 | asmln = func_chunks[func_chunk_i].asmln; |
e56ab892 |
4032 | func_chunk_i++; |
4033 | continue; |
4034 | } |
4c45fa73 |
4035 | pending_endp = 1; |
91977a1c |
4036 | continue; |
4037 | } |
4038 | |
bfa4a6ee |
4039 | p = strchr(words[0], ':'); |
4040 | if (p != NULL) { |
d4e3b5db |
4041 | set_label(pi, words[0]); |
bfa4a6ee |
4042 | continue; |
4043 | } |
4044 | |
4045 | if (!in_func || skip_func) { |
4046 | if (!skip_warned && !skip_func && g_labels[pi][0] != 0) { |
4047 | if (verbose) |
4048 | anote("skipping from '%s'\n", g_labels[pi]); |
4049 | skip_warned = 1; |
4050 | } |
4051 | g_labels[pi][0] = 0; |
4052 | continue; |
4053 | } |
4054 | |
4c45fa73 |
4055 | if (wordc > 1 && IS(words[1], "=")) { |
91977a1c |
4056 | if (wordc != 5) |
4057 | aerr("unhandled equ, wc=%d\n", wordc); |
4058 | if (g_eqcnt >= eq_alloc) { |
4059 | eq_alloc *= 2; |
4060 | g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0])); |
4061 | my_assert_not(g_eqs, NULL); |
4062 | } |
4063 | |
4064 | len = strlen(words[0]); |
4065 | if (len > sizeof(g_eqs[0].name) - 1) |
4066 | aerr("equ name too long: %d\n", len); |
4067 | strcpy(g_eqs[g_eqcnt].name, words[0]); |
4068 | |
4069 | if (!IS(words[3], "ptr")) |
4070 | aerr("unhandled equ\n"); |
4071 | if (IS(words[2], "dword")) |
4072 | g_eqs[g_eqcnt].lmod = OPLM_DWORD; |
4073 | else if (IS(words[2], "word")) |
4074 | g_eqs[g_eqcnt].lmod = OPLM_WORD; |
4075 | else if (IS(words[2], "byte")) |
4076 | g_eqs[g_eqcnt].lmod = OPLM_BYTE; |
4077 | else |
4078 | aerr("bad lmod: '%s'\n", words[2]); |
4079 | |
4080 | g_eqs[g_eqcnt].offset = parse_number(words[4]); |
4081 | g_eqcnt++; |
4082 | continue; |
4083 | } |
4084 | |
4085 | if (pi >= ARRAY_SIZE(ops)) |
4086 | aerr("too many ops\n"); |
4087 | |
91977a1c |
4088 | parse_op(&ops[pi], words, wordc); |
4089 | pi++; |
91977a1c |
4090 | } |
4091 | |
4092 | fclose(fout); |
4093 | fclose(fasm); |
06c5d854 |
4094 | fclose(g_fhdr); |
91977a1c |
4095 | |
4096 | return 0; |
c36e914d |
4097 | } |
91977a1c |
4098 | |
4099 | // vim:ts=2:shiftwidth=2:expandtab |