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