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