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