7637b6cc |
1 | /* |
2 | * ia32rtools |
2c10ea1f |
3 | * (C) notaz, 2013-2015 |
7637b6cc |
4 | * |
5 | * This work is licensed under the terms of 3-clause BSD license. |
6 | * See COPYING file in the top-level directory. |
7 | */ |
8 | |
33c35af6 |
9 | #define _GNU_SOURCE |
c36e914d |
10 | #include <stdio.h> |
11 | #include <stdlib.h> |
12 | #include <string.h> |
13 | |
14 | #include "my_assert.h" |
15 | #include "my_str.h" |
16 | |
17 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
18 | #define IS(w, y) !strcmp(w, y) |
39b168b8 |
19 | #define IS_START(w, y) !strncmp(w, y, strlen(y)) |
c36e914d |
20 | |
21 | #include "protoparse.h" |
22 | |
06c5d854 |
23 | static const char *asmfn; |
c36e914d |
24 | static int asmln; |
06c5d854 |
25 | static FILE *g_fhdr; |
c36e914d |
26 | |
940e8e66 |
27 | #define anote(fmt, ...) \ |
28 | printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__) |
c36e914d |
29 | #define awarn(fmt, ...) \ |
940e8e66 |
30 | printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__) |
c36e914d |
31 | #define aerr(fmt, ...) do { \ |
940e8e66 |
32 | printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \ |
33c35af6 |
33 | fcloseall(); \ |
c36e914d |
34 | exit(1); \ |
35 | } while (0) |
36 | |
054f95b2 |
37 | #include "masm_tools.h" |
38 | |
69a3cdfc |
39 | enum op_flags { |
5e49b270 |
40 | OPF_RMD = (1 << 0), /* removed from code generation */ |
87bf6cec |
41 | OPF_DATA = (1 << 1), /* data processing - writes to dst opr */ |
42 | OPF_FLAGS = (1 << 2), /* sets flags */ |
5c024ef7 |
43 | OPF_JMP = (1 << 3), /* branch, call */ |
44 | OPF_CJMP = (1 << 4), /* cond. branch (cc or jecxz) */ |
45 | OPF_CC = (1 << 5), /* uses flags */ |
46 | OPF_TAIL = (1 << 6), /* ret or tail call */ |
47 | OPF_RSAVE = (1 << 7), /* push/pop is local reg save/load */ |
48 | OPF_REP = (1 << 8), /* prefixed by rep */ |
49 | OPF_REPZ = (1 << 9), /* rep is repe/repz */ |
50 | OPF_REPNZ = (1 << 10), /* rep is repne/repnz */ |
9af2d373 |
51 | OPF_FARG = (1 << 11), /* push collected as func arg */ |
52 | OPF_FARGNR = (1 << 12), /* push collected as func arg (no reuse) */ |
53 | OPF_EBP_S = (1 << 13), /* ebp used as scratch here, not BP */ |
54 | OPF_DF = (1 << 14), /* DF flag set */ |
55 | OPF_ATAIL = (1 << 15), /* tail call with reused arg frame */ |
56 | OPF_32BIT = (1 << 16), /* 32bit division */ |
57 | OPF_LOCK = (1 << 17), /* op has lock prefix */ |
58 | OPF_VAPUSH = (1 << 18), /* vararg ptr push (as call arg) */ |
26677139 |
59 | OPF_DONE = (1 << 19), /* already fully handled by analysis */ |
c36e914d |
60 | }; |
61 | |
62 | enum op_op { |
63 | OP_INVAL, |
33c35af6 |
64 | OP_NOP, |
c36e914d |
65 | OP_PUSH, |
66 | OP_POP, |
591721d7 |
67 | OP_LEAVE, |
c36e914d |
68 | OP_MOV, |
850c9265 |
69 | OP_LEA, |
70 | OP_MOVZX, |
71 | OP_MOVSX, |
108e9fe3 |
72 | OP_XCHG, |
850c9265 |
73 | OP_NOT, |
5101a5f9 |
74 | OP_CDQ, |
092f64e1 |
75 | OP_LODS, |
33c35af6 |
76 | OP_STOS, |
d4e3b5db |
77 | OP_MOVS, |
7ba45c34 |
78 | OP_CMPS, |
591721d7 |
79 | OP_SCAS, |
80 | OP_STD, |
81 | OP_CLD, |
c36e914d |
82 | OP_RET, |
83 | OP_ADD, |
91977a1c |
84 | OP_SUB, |
850c9265 |
85 | OP_AND, |
86 | OP_OR, |
87 | OP_XOR, |
88 | OP_SHL, |
89 | OP_SHR, |
90 | OP_SAR, |
3b2f4044 |
91 | OP_SHRD, |
d4e3b5db |
92 | OP_ROL, |
93 | OP_ROR, |
cb090db0 |
94 | OP_RCL, |
95 | OP_RCR, |
69a3cdfc |
96 | OP_ADC, |
850c9265 |
97 | OP_SBB, |
1f84f6b3 |
98 | OP_BSF, |
850c9265 |
99 | OP_INC, |
100 | OP_DEC, |
5101a5f9 |
101 | OP_NEG, |
850c9265 |
102 | OP_MUL, |
103 | OP_IMUL, |
5101a5f9 |
104 | OP_DIV, |
105 | OP_IDIV, |
c36e914d |
106 | OP_TEST, |
107 | OP_CMP, |
108 | OP_CALL, |
109 | OP_JMP, |
5c024ef7 |
110 | OP_JECXZ, |
092f64e1 |
111 | OP_JCC, |
112 | OP_SCC, |
90307a99 |
113 | // x87 |
114 | // mmx |
115 | OP_EMMS, |
30c8c549 |
116 | // undefined |
2c10ea1f |
117 | OP_UD2, |
c36e914d |
118 | }; |
119 | |
120 | enum opr_type { |
87bf6cec |
121 | OPT_UNSPEC, |
122 | OPT_REG, |
123 | OPT_REGMEM, |
124 | OPT_LABEL, |
850c9265 |
125 | OPT_OFFSET, |
87bf6cec |
126 | OPT_CONST, |
c36e914d |
127 | }; |
128 | |
2b43685d |
129 | // must be sorted (larger len must be further in enum) |
c36e914d |
130 | enum opr_lenmod { |
91977a1c |
131 | OPLM_UNSPEC, |
132 | OPLM_BYTE, |
133 | OPLM_WORD, |
134 | OPLM_DWORD, |
90307a99 |
135 | OPLM_QWORD, |
c36e914d |
136 | }; |
137 | |
850c9265 |
138 | #define MAX_OPERANDS 3 |
92d715b6 |
139 | #define NAMELEN 112 |
c36e914d |
140 | |
141 | struct parsed_opr { |
91977a1c |
142 | enum opr_type type; |
143 | enum opr_lenmod lmod; |
7ba45c34 |
144 | unsigned int is_ptr:1; // pointer in C |
145 | unsigned int is_array:1; // array in C |
a3684be1 |
146 | unsigned int type_from_var:1; // .. in header, sometimes wrong |
2b43685d |
147 | unsigned int size_mismatch:1; // type override differs from C |
148 | unsigned int size_lt:1; // type override is larger than C |
ddaf8bd7 |
149 | unsigned int had_ds:1; // had ds: prefix |
c7ed83dd |
150 | const struct parsed_proto *pp; // for OPT_LABEL |
91977a1c |
151 | int reg; |
152 | unsigned int val; |
92d715b6 |
153 | char name[NAMELEN]; |
c36e914d |
154 | }; |
155 | |
156 | struct parsed_op { |
91977a1c |
157 | enum op_op op; |
158 | struct parsed_opr operand[MAX_OPERANDS]; |
69a3cdfc |
159 | unsigned int flags; |
092f64e1 |
160 | unsigned char pfo; |
161 | unsigned char pfo_inv; |
162 | unsigned char operand_cnt; |
3a5101d7 |
163 | unsigned char p_argnum; // arg push: altered before call arg # |
164 | unsigned char p_arggrp; // arg push: arg group # for above |
165 | unsigned char p_argpass;// arg push: arg of host func |
166 | short p_argnext;// arg push: same arg pushed elsewhere or -1 |
69a3cdfc |
167 | int regmask_src; // all referensed regs |
168 | int regmask_dst; |
940e8e66 |
169 | int pfomask; // flagop: parsed_flag_op that can't be delayed |
940e8e66 |
170 | int cc_scratch; // scratch storage during analysis |
4c45fa73 |
171 | int bt_i; // branch target for branches |
172 | struct parsed_data *btj;// branch targets for jumptables |
c7ed83dd |
173 | struct parsed_proto *pp;// parsed_proto for OP_CALL |
91977a1c |
174 | void *datap; |
8eb12e72 |
175 | int asmln; |
91977a1c |
176 | }; |
177 | |
69a3cdfc |
178 | // datap: |
092f64e1 |
179 | // OP_CALL - parser proto hint (str) |
2c10ea1f |
180 | // (OPF_CC) - points to one of (OPF_FLAGS) that affects cc op |
181 | // OP_POP - points to OP_PUSH in push/pop pair |
69a3cdfc |
182 | |
91977a1c |
183 | struct parsed_equ { |
184 | char name[64]; |
185 | enum opr_lenmod lmod; |
186 | int offset; |
c36e914d |
187 | }; |
188 | |
4c45fa73 |
189 | struct parsed_data { |
190 | char label[256]; |
191 | enum opr_type type; |
192 | enum opr_lenmod lmod; |
193 | int count; |
194 | int count_alloc; |
195 | struct { |
196 | union { |
197 | char *label; |
63df67be |
198 | unsigned int val; |
4c45fa73 |
199 | } u; |
200 | int bt_i; |
201 | } *d; |
202 | }; |
203 | |
204 | struct label_ref { |
205 | int i; |
206 | struct label_ref *next; |
207 | }; |
208 | |
1bafb621 |
209 | enum ida_func_attr { |
210 | IDAFA_BP_FRAME = (1 << 0), |
211 | IDAFA_LIB_FUNC = (1 << 1), |
212 | IDAFA_STATIC = (1 << 2), |
213 | IDAFA_NORETURN = (1 << 3), |
214 | IDAFA_THUNK = (1 << 4), |
215 | IDAFA_FPD = (1 << 5), |
216 | }; |
217 | |
3a5101d7 |
218 | // note: limited to 32k due to p_argnext |
219 | #define MAX_OPS 4096 |
220 | #define MAX_ARG_GRP 2 |
c36e914d |
221 | |
222 | static struct parsed_op ops[MAX_OPS]; |
91977a1c |
223 | static struct parsed_equ *g_eqs; |
224 | static int g_eqcnt; |
d7857c3a |
225 | static char *g_labels[MAX_OPS]; |
4c45fa73 |
226 | static struct label_ref g_label_refs[MAX_OPS]; |
bd96f656 |
227 | static const struct parsed_proto *g_func_pp; |
4c45fa73 |
228 | static struct parsed_data *g_func_pd; |
229 | static int g_func_pd_cnt; |
91977a1c |
230 | static char g_func[256]; |
231 | static char g_comment[256]; |
232 | static int g_bp_frame; |
1bafb621 |
233 | static int g_sp_frame; |
a2c1d768 |
234 | static int g_stack_frame_used; |
1bafb621 |
235 | static int g_stack_fsz; |
4c45fa73 |
236 | static int g_ida_func_attr; |
30c8c549 |
237 | static int g_skip_func; |
89ff3147 |
238 | static int g_allow_regfunc; |
92d715b6 |
239 | static int g_quiet_pp; |
9af2d373 |
240 | static int g_header_mode; |
92d715b6 |
241 | |
91977a1c |
242 | #define ferr(op_, fmt, ...) do { \ |
8eb12e72 |
243 | printf("%s:%d: error: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \ |
850c9265 |
244 | dump_op(op_), ##__VA_ARGS__); \ |
33c35af6 |
245 | fcloseall(); \ |
91977a1c |
246 | exit(1); \ |
247 | } while (0) |
de50b98b |
248 | #define fnote(op_, fmt, ...) \ |
8eb12e72 |
249 | printf("%s:%d: note: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \ |
de50b98b |
250 | dump_op(op_), ##__VA_ARGS__) |
91977a1c |
251 | |
26677139 |
252 | #define ferr_assert(op_, cond) do { \ |
253 | if (!(cond)) ferr(op_, "assertion '%s' failed on ln :%d\n", #cond, \ |
254 | __LINE__); \ |
255 | } while (0) |
256 | |
90307a99 |
257 | const char *regs_r32[] = { |
258 | "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp", |
259 | // not r32, but list here for easy parsing and printing |
260 | "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7", |
261 | }; |
91977a1c |
262 | const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" }; |
263 | const char *regs_r8l[] = { "al", "bl", "cl", "dl" }; |
264 | const char *regs_r8h[] = { "ah", "bh", "ch", "dh" }; |
265 | |
266 | enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP }; |
267 | |
69a3cdfc |
268 | // possible basic comparison types (without inversion) |
269 | enum parsed_flag_op { |
270 | PFO_O, // 0 OF=1 |
271 | PFO_C, // 2 CF=1 |
272 | PFO_Z, // 4 ZF=1 |
273 | PFO_BE, // 6 CF=1||ZF=1 |
274 | PFO_S, // 8 SF=1 |
275 | PFO_P, // a PF=1 |
276 | PFO_L, // c SF!=OF |
277 | PFO_LE, // e ZF=1||SF!=OF |
278 | }; |
279 | |
90307a99 |
280 | #define PFOB_O (1 << PFO_O) |
281 | #define PFOB_C (1 << PFO_C) |
282 | #define PFOB_Z (1 << PFO_Z) |
283 | #define PFOB_S (1 << PFO_S) |
284 | |
69a3cdfc |
285 | static const char *parsed_flag_op_names[] = { |
286 | "o", "c", "z", "be", "s", "p", "l", "le" |
287 | }; |
288 | |
91977a1c |
289 | static int char_array_i(const char *array[], size_t len, const char *s) |
290 | { |
291 | int i; |
c36e914d |
292 | |
91977a1c |
293 | for (i = 0; i < len; i++) |
294 | if (IS(s, array[i])) |
295 | return i; |
c36e914d |
296 | |
91977a1c |
297 | return -1; |
298 | } |
299 | |
63df67be |
300 | static void printf_number(char *buf, size_t buf_size, |
301 | unsigned long number) |
91977a1c |
302 | { |
5101a5f9 |
303 | // output in C-friendly form |
304 | snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number); |
305 | } |
91977a1c |
306 | |
fdd5548a |
307 | static int check_segment_prefix(const char *s) |
308 | { |
309 | if (s[0] == 0 || s[1] != 's' || s[2] != ':') |
310 | return 0; |
311 | |
312 | switch (s[0]) { |
313 | case 'c': return 1; |
314 | case 'd': return 2; |
315 | case 's': return 3; |
316 | case 'e': return 4; |
317 | case 'f': return 5; |
318 | case 'g': return 6; |
319 | default: return 0; |
320 | } |
321 | } |
322 | |
5101a5f9 |
323 | static int parse_reg(enum opr_lenmod *reg_lmod, const char *s) |
324 | { |
325 | int reg; |
91977a1c |
326 | |
5101a5f9 |
327 | reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s); |
90307a99 |
328 | if (reg >= 8) { |
329 | *reg_lmod = OPLM_QWORD; |
330 | return reg; |
331 | } |
5101a5f9 |
332 | if (reg >= 0) { |
333 | *reg_lmod = OPLM_DWORD; |
334 | return reg; |
91977a1c |
335 | } |
5101a5f9 |
336 | reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s); |
337 | if (reg >= 0) { |
338 | *reg_lmod = OPLM_WORD; |
339 | return reg; |
340 | } |
341 | reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s); |
342 | if (reg >= 0) { |
343 | *reg_lmod = OPLM_BYTE; |
344 | return reg; |
345 | } |
346 | reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s); |
347 | if (reg >= 0) { |
348 | *reg_lmod = OPLM_BYTE; |
349 | return reg; |
850c9265 |
350 | } |
351 | |
352 | return -1; |
91977a1c |
353 | } |
354 | |
5101a5f9 |
355 | static int parse_indmode(char *name, int *regmask, int need_c_cvt) |
356 | { |
357 | enum opr_lenmod lmod; |
358 | char cvtbuf[256]; |
359 | char *d = cvtbuf; |
360 | char *s = name; |
361 | char w[64]; |
362 | long number; |
363 | int reg; |
364 | int c = 0; |
365 | |
366 | *d = 0; |
367 | |
368 | while (*s != 0) { |
369 | d += strlen(d); |
370 | while (my_isblank(*s)) |
371 | s++; |
372 | for (; my_issep(*s); d++, s++) |
373 | *d = *s; |
374 | while (my_isblank(*s)) |
375 | s++; |
376 | *d = 0; |
377 | |
fdd5548a |
378 | // skip '?s:' prefixes |
379 | if (check_segment_prefix(s)) |
87bf6cec |
380 | s += 3; |
381 | |
5101a5f9 |
382 | s = next_idt(w, sizeof(w), s); |
383 | if (w[0] == 0) |
384 | break; |
385 | c++; |
386 | |
387 | reg = parse_reg(&lmod, w); |
388 | if (reg >= 0) { |
389 | *regmask |= 1 << reg; |
390 | goto pass; |
391 | } |
392 | |
393 | if ('0' <= w[0] && w[0] <= '9') { |
394 | number = parse_number(w); |
395 | printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number); |
396 | continue; |
397 | } |
398 | |
399 | // probably some label/identifier - pass |
400 | |
401 | pass: |
402 | snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w); |
403 | } |
404 | |
405 | if (need_c_cvt) |
406 | strcpy(name, cvtbuf); |
407 | |
408 | return c; |
409 | } |
410 | |
4c45fa73 |
411 | static int is_reg_in_str(const char *s) |
412 | { |
413 | int i; |
414 | |
415 | if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3]))) |
416 | return 0; |
417 | |
418 | for (i = 0; i < ARRAY_SIZE(regs_r32); i++) |
419 | if (!strncmp(s, regs_r32[i], 3)) |
420 | return 1; |
421 | |
422 | return 0; |
423 | } |
424 | |
09bc6fd5 |
425 | static const char *parse_stack_el(const char *name, char *extra_reg, |
426 | int early_try) |
d4e3b5db |
427 | { |
4c45fa73 |
428 | const char *p, *p2, *s; |
1bafb621 |
429 | char *endp = NULL; |
d4e3b5db |
430 | char buf[32]; |
1bafb621 |
431 | long val; |
d4e3b5db |
432 | int len; |
433 | |
09bc6fd5 |
434 | if (g_bp_frame || early_try) |
435 | { |
436 | p = name; |
437 | if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) { |
438 | p += 4; |
439 | if (extra_reg != NULL) { |
440 | strncpy(extra_reg, name, 3); |
441 | extra_reg[4] = 0; |
442 | } |
4c45fa73 |
443 | } |
4c45fa73 |
444 | |
09bc6fd5 |
445 | if (IS_START(p, "ebp+")) { |
446 | p += 4; |
4c45fa73 |
447 | |
09bc6fd5 |
448 | p2 = strchr(p, '+'); |
449 | if (p2 != NULL && is_reg_in_str(p)) { |
450 | if (extra_reg != NULL) { |
451 | strncpy(extra_reg, p, p2 - p); |
452 | extra_reg[p2 - p] = 0; |
453 | } |
454 | p = p2 + 1; |
4c45fa73 |
455 | } |
4c45fa73 |
456 | |
09bc6fd5 |
457 | if (!('0' <= *p && *p <= '9')) |
458 | return p; |
4c45fa73 |
459 | |
09bc6fd5 |
460 | return NULL; |
461 | } |
4c45fa73 |
462 | } |
463 | |
39b168b8 |
464 | if (!IS_START(name, "esp+")) |
d4e3b5db |
465 | return NULL; |
466 | |
037f4971 |
467 | s = name + 4; |
468 | p = strchr(s, '+'); |
d4e3b5db |
469 | if (p) { |
037f4971 |
470 | if (is_reg_in_str(s)) { |
471 | if (extra_reg != NULL) { |
472 | strncpy(extra_reg, s, p - s); |
473 | extra_reg[p - s] = 0; |
474 | } |
475 | s = p + 1; |
476 | p = strchr(s, '+'); |
477 | if (p == NULL) |
478 | aerr("%s IDA stackvar not set?\n", __func__); |
479 | } |
1bafb621 |
480 | if (!('0' <= *s && *s <= '9')) { |
037f4971 |
481 | aerr("%s IDA stackvar offset not set?\n", __func__); |
d4e3b5db |
482 | return NULL; |
1bafb621 |
483 | } |
484 | if (s[0] == '0' && s[1] == 'x') |
485 | s += 2; |
486 | len = p - s; |
d4e3b5db |
487 | if (len < sizeof(buf) - 1) { |
1bafb621 |
488 | strncpy(buf, s, len); |
d4e3b5db |
489 | buf[len] = 0; |
1bafb621 |
490 | val = strtol(buf, &endp, 16); |
491 | if (val == 0 || *endp != 0) { |
492 | aerr("%s num parse fail for '%s'\n", __func__, buf); |
493 | return NULL; |
494 | } |
d4e3b5db |
495 | } |
496 | p++; |
497 | } |
498 | else |
499 | p = name + 4; |
500 | |
501 | if ('0' <= *p && *p <= '9') |
502 | return NULL; |
503 | |
504 | return p; |
505 | } |
506 | |
850c9265 |
507 | static int guess_lmod_from_name(struct parsed_opr *opr) |
508 | { |
509 | if (!strncmp(opr->name, "dword_", 6)) { |
510 | opr->lmod = OPLM_DWORD; |
511 | return 1; |
512 | } |
513 | if (!strncmp(opr->name, "word_", 5)) { |
514 | opr->lmod = OPLM_WORD; |
515 | return 1; |
516 | } |
517 | if (!strncmp(opr->name, "byte_", 5)) { |
518 | opr->lmod = OPLM_BYTE; |
519 | return 1; |
520 | } |
90307a99 |
521 | if (!strncmp(opr->name, "qword_", 6)) { |
522 | opr->lmod = OPLM_QWORD; |
523 | return 1; |
524 | } |
850c9265 |
525 | return 0; |
526 | } |
527 | |
3ebea2cf |
528 | static int guess_lmod_from_c_type(enum opr_lenmod *lmod, |
529 | const struct parsed_type *c_type) |
06c5d854 |
530 | { |
06c5d854 |
531 | static const char *dword_types[] = { |
c0de9015 |
532 | "uint32_t", "int", "_DWORD", "UINT_PTR", "DWORD", |
4741fdfe |
533 | "WPARAM", "LPARAM", "UINT", "__int32", |
09bc6fd5 |
534 | "LONG", "HIMC", "BOOL", "size_t", |
feb0ee5d |
535 | "float", |
06c5d854 |
536 | }; |
537 | static const char *word_types[] = { |
fe18709a |
538 | "uint16_t", "int16_t", "_WORD", "WORD", |
2b43685d |
539 | "unsigned __int16", "__int16", |
06c5d854 |
540 | }; |
541 | static const char *byte_types[] = { |
2b43685d |
542 | "uint8_t", "int8_t", "char", |
89ff3147 |
543 | "unsigned __int8", "__int8", "BYTE", "_BYTE", |
4741fdfe |
544 | "CHAR", "_UNKNOWN", |
b4878d2b |
545 | // structures.. deal the same as with _UNKNOWN for now |
546 | "CRITICAL_SECTION", |
06c5d854 |
547 | }; |
3ebea2cf |
548 | const char *n; |
06c5d854 |
549 | int i; |
550 | |
3ebea2cf |
551 | if (c_type->is_ptr) { |
552 | *lmod = OPLM_DWORD; |
06c5d854 |
553 | return 1; |
554 | } |
555 | |
3ebea2cf |
556 | n = skip_type_mod(c_type->name); |
06c5d854 |
557 | |
3ebea2cf |
558 | for (i = 0; i < ARRAY_SIZE(dword_types); i++) { |
559 | if (IS(n, dword_types[i])) { |
560 | *lmod = OPLM_DWORD; |
06c5d854 |
561 | return 1; |
562 | } |
563 | } |
564 | |
565 | for (i = 0; i < ARRAY_SIZE(word_types); i++) { |
3ebea2cf |
566 | if (IS(n, word_types[i])) { |
567 | *lmod = OPLM_WORD; |
06c5d854 |
568 | return 1; |
569 | } |
570 | } |
571 | |
572 | for (i = 0; i < ARRAY_SIZE(byte_types); i++) { |
3ebea2cf |
573 | if (IS(n, byte_types[i])) { |
574 | *lmod = OPLM_BYTE; |
06c5d854 |
575 | return 1; |
576 | } |
577 | } |
578 | |
06c5d854 |
579 | return 0; |
580 | } |
581 | |
c7ed83dd |
582 | static char *default_cast_to(char *buf, size_t buf_size, |
583 | struct parsed_opr *opr) |
584 | { |
585 | buf[0] = 0; |
586 | |
587 | if (!opr->is_ptr) |
588 | return buf; |
589 | if (opr->pp == NULL || opr->pp->type.name == NULL |
590 | || opr->pp->is_fptr) |
591 | { |
592 | snprintf(buf, buf_size, "%s", "(void *)"); |
593 | return buf; |
594 | } |
595 | |
596 | snprintf(buf, buf_size, "(%s)", opr->pp->type.name); |
597 | return buf; |
598 | } |
599 | |
4c45fa73 |
600 | static enum opr_type lmod_from_directive(const char *d) |
601 | { |
602 | if (IS(d, "dd")) |
603 | return OPLM_DWORD; |
604 | else if (IS(d, "dw")) |
605 | return OPLM_WORD; |
606 | else if (IS(d, "db")) |
607 | return OPLM_BYTE; |
608 | |
609 | aerr("unhandled directive: '%s'\n", d); |
610 | return OPLM_UNSPEC; |
611 | } |
612 | |
5101a5f9 |
613 | static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod, |
614 | int *regmask) |
615 | { |
616 | opr->type = OPT_REG; |
617 | opr->reg = reg; |
618 | opr->lmod = lmod; |
619 | *regmask |= 1 << reg; |
620 | } |
621 | |
39b168b8 |
622 | static struct parsed_equ *equ_find(struct parsed_op *po, const char *name, |
623 | int *extra_offs); |
87bf6cec |
624 | |
69a3cdfc |
625 | static int parse_operand(struct parsed_opr *opr, |
626 | int *regmask, int *regmask_indirect, |
1bafb621 |
627 | char words[16][256], int wordc, int w, unsigned int op_flags) |
c36e914d |
628 | { |
92d715b6 |
629 | const struct parsed_proto *pp = NULL; |
850c9265 |
630 | enum opr_lenmod tmplmod; |
63df67be |
631 | unsigned long number; |
89ff3147 |
632 | char buf[256]; |
850c9265 |
633 | int ret, len; |
1bafb621 |
634 | int wordc_in; |
89ff3147 |
635 | char *p; |
850c9265 |
636 | int i; |
c36e914d |
637 | |
1bafb621 |
638 | if (w >= wordc) |
639 | aerr("parse_operand w %d, wordc %d\n", w, wordc); |
c36e914d |
640 | |
1bafb621 |
641 | opr->reg = xUNSPEC; |
91977a1c |
642 | |
1bafb621 |
643 | for (i = w; i < wordc; i++) { |
644 | len = strlen(words[i]); |
645 | if (words[i][len - 1] == ',') { |
646 | words[i][len - 1] = 0; |
647 | wordc = i + 1; |
648 | break; |
649 | } |
650 | } |
c36e914d |
651 | |
1bafb621 |
652 | wordc_in = wordc - w; |
c36e914d |
653 | |
1bafb621 |
654 | if ((op_flags & OPF_JMP) && wordc_in > 0 |
655 | && !('0' <= words[w][0] && words[w][0] <= '9')) |
656 | { |
657 | const char *label = NULL; |
658 | |
659 | if (wordc_in == 3 && !strncmp(words[w], "near", 4) |
660 | && IS(words[w + 1], "ptr")) |
661 | label = words[w + 2]; |
662 | else if (wordc_in == 2 && IS(words[w], "short")) |
663 | label = words[w + 1]; |
664 | else if (wordc_in == 1 |
665 | && strchr(words[w], '[') == NULL |
666 | && parse_reg(&tmplmod, words[w]) < 0) |
667 | label = words[w]; |
668 | |
669 | if (label != NULL) { |
670 | opr->type = OPT_LABEL; |
fdd5548a |
671 | ret = check_segment_prefix(label); |
672 | if (ret != 0) { |
673 | if (ret >= 5) |
674 | aerr("fs/gs used\n"); |
ddaf8bd7 |
675 | opr->had_ds = 1; |
39b168b8 |
676 | label += 3; |
ddaf8bd7 |
677 | } |
1bafb621 |
678 | strcpy(opr->name, label); |
679 | return wordc; |
680 | } |
681 | } |
c36e914d |
682 | |
1bafb621 |
683 | if (wordc_in >= 3) { |
684 | if (IS(words[w + 1], "ptr")) { |
685 | if (IS(words[w], "dword")) |
686 | opr->lmod = OPLM_DWORD; |
687 | else if (IS(words[w], "word")) |
688 | opr->lmod = OPLM_WORD; |
689 | else if (IS(words[w], "byte")) |
690 | opr->lmod = OPLM_BYTE; |
90307a99 |
691 | else if (IS(words[w], "qword")) |
692 | opr->lmod = OPLM_QWORD; |
1bafb621 |
693 | else |
694 | aerr("type parsing failed\n"); |
695 | w += 2; |
696 | wordc_in = wordc - w; |
697 | } |
698 | } |
699 | |
39b168b8 |
700 | if (wordc_in == 2) { |
701 | if (IS(words[w], "offset")) { |
702 | opr->type = OPT_OFFSET; |
27ebfaed |
703 | opr->lmod = OPLM_DWORD; |
39b168b8 |
704 | strcpy(opr->name, words[w + 1]); |
27ebfaed |
705 | pp = proto_parse(g_fhdr, opr->name, 1); |
706 | goto do_label; |
39b168b8 |
707 | } |
708 | if (IS(words[w], "(offset")) { |
89ff3147 |
709 | p = strchr(words[w + 1], ')'); |
39b168b8 |
710 | if (p == NULL) |
711 | aerr("parse of bracketed offset failed\n"); |
712 | *p = 0; |
713 | opr->type = OPT_OFFSET; |
714 | strcpy(opr->name, words[w + 1]); |
715 | return wordc; |
716 | } |
1bafb621 |
717 | } |
c36e914d |
718 | |
1bafb621 |
719 | if (wordc_in != 1) |
850c9265 |
720 | aerr("parse_operand 1 word expected\n"); |
c36e914d |
721 | |
fdd5548a |
722 | ret = check_segment_prefix(words[w]); |
723 | if (ret != 0) { |
724 | if (ret >= 5) |
725 | aerr("fs/gs used\n"); |
ddaf8bd7 |
726 | opr->had_ds = 1; |
89ff3147 |
727 | memmove(words[w], words[w] + 3, strlen(words[w]) - 2); |
ddaf8bd7 |
728 | } |
89ff3147 |
729 | strcpy(opr->name, words[w]); |
c36e914d |
730 | |
850c9265 |
731 | if (words[w][0] == '[') { |
732 | opr->type = OPT_REGMEM; |
733 | ret = sscanf(words[w], "[%[^]]]", opr->name); |
734 | if (ret != 1) |
735 | aerr("[] parse failure\n"); |
87bf6cec |
736 | |
5101a5f9 |
737 | parse_indmode(opr->name, regmask_indirect, 1); |
09bc6fd5 |
738 | if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL, 1)) |
739 | { |
87bf6cec |
740 | // might be an equ |
d4e3b5db |
741 | struct parsed_equ *eq = |
09bc6fd5 |
742 | equ_find(NULL, parse_stack_el(opr->name, NULL, 1), &i); |
87bf6cec |
743 | if (eq) |
744 | opr->lmod = eq->lmod; |
745 | } |
850c9265 |
746 | return wordc; |
747 | } |
748 | else if (strchr(words[w], '[')) { |
749 | // label[reg] form |
89ff3147 |
750 | p = strchr(words[w], '['); |
850c9265 |
751 | opr->type = OPT_REGMEM; |
89ff3147 |
752 | parse_indmode(p, regmask_indirect, 0); |
753 | strncpy(buf, words[w], p - words[w]); |
754 | buf[p - words[w]] = 0; |
755 | pp = proto_parse(g_fhdr, buf, 1); |
756 | goto do_label; |
850c9265 |
757 | } |
758 | else if (('0' <= words[w][0] && words[w][0] <= '9') |
759 | || words[w][0] == '-') |
760 | { |
5101a5f9 |
761 | number = parse_number(words[w]); |
91977a1c |
762 | opr->type = OPT_CONST; |
5101a5f9 |
763 | opr->val = number; |
764 | printf_number(opr->name, sizeof(opr->name), number); |
91977a1c |
765 | return wordc; |
850c9265 |
766 | } |
c36e914d |
767 | |
5101a5f9 |
768 | ret = parse_reg(&tmplmod, opr->name); |
769 | if (ret >= 0) { |
770 | setup_reg_opr(opr, ret, tmplmod, regmask); |
850c9265 |
771 | return wordc; |
772 | } |
773 | |
774 | // most likely var in data segment |
775 | opr->type = OPT_LABEL; |
92d715b6 |
776 | pp = proto_parse(g_fhdr, opr->name, g_quiet_pp); |
89ff3147 |
777 | |
778 | do_label: |
bd96f656 |
779 | if (pp != NULL) { |
1cd4a663 |
780 | if (pp->is_fptr || pp->is_func) { |
06c5d854 |
781 | opr->lmod = OPLM_DWORD; |
782 | opr->is_ptr = 1; |
783 | } |
2b43685d |
784 | else { |
840257f6 |
785 | tmplmod = OPLM_UNSPEC; |
bd96f656 |
786 | if (!guess_lmod_from_c_type(&tmplmod, &pp->type)) |
2b43685d |
787 | anote("unhandled C type '%s' for '%s'\n", |
bd96f656 |
788 | pp->type.name, opr->name); |
2b43685d |
789 | |
a3684be1 |
790 | if (opr->lmod == OPLM_UNSPEC) { |
2b43685d |
791 | opr->lmod = tmplmod; |
a3684be1 |
792 | opr->type_from_var = 1; |
793 | } |
2b43685d |
794 | else if (opr->lmod != tmplmod) { |
795 | opr->size_mismatch = 1; |
796 | if (tmplmod < opr->lmod) |
797 | opr->size_lt = 1; |
798 | } |
a652aa9f |
799 | opr->is_ptr = pp->type.is_ptr; |
3ebea2cf |
800 | } |
bd96f656 |
801 | opr->is_array = pp->type.is_array; |
06c5d854 |
802 | } |
c7ed83dd |
803 | opr->pp = pp; |
06c5d854 |
804 | |
850c9265 |
805 | if (opr->lmod == OPLM_UNSPEC) |
806 | guess_lmod_from_name(opr); |
850c9265 |
807 | return wordc; |
c36e914d |
808 | } |
809 | |
33c35af6 |
810 | static const struct { |
811 | const char *name; |
812 | unsigned int flags; |
813 | } pref_table[] = { |
814 | { "rep", OPF_REP }, |
7ba45c34 |
815 | { "repe", OPF_REP|OPF_REPZ }, |
816 | { "repz", OPF_REP|OPF_REPZ }, |
817 | { "repne", OPF_REP|OPF_REPNZ }, |
818 | { "repnz", OPF_REP|OPF_REPNZ }, |
037f4971 |
819 | { "lock", OPF_LOCK }, // ignored for now.. |
33c35af6 |
820 | }; |
821 | |
5c024ef7 |
822 | #define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC) |
823 | |
c36e914d |
824 | static const struct { |
850c9265 |
825 | const char *name; |
826 | enum op_op op; |
092f64e1 |
827 | unsigned short minopr; |
828 | unsigned short maxopr; |
69a3cdfc |
829 | unsigned int flags; |
092f64e1 |
830 | unsigned char pfo; |
831 | unsigned char pfo_inv; |
c36e914d |
832 | } op_table[] = { |
33c35af6 |
833 | { "nop", OP_NOP, 0, 0, 0 }, |
69a3cdfc |
834 | { "push", OP_PUSH, 1, 1, 0 }, |
835 | { "pop", OP_POP, 1, 1, OPF_DATA }, |
591721d7 |
836 | { "leave",OP_LEAVE, 0, 0, OPF_DATA }, |
69a3cdfc |
837 | { "mov" , OP_MOV, 2, 2, OPF_DATA }, |
838 | { "lea", OP_LEA, 2, 2, OPF_DATA }, |
839 | { "movzx",OP_MOVZX, 2, 2, OPF_DATA }, |
840 | { "movsx",OP_MOVSX, 2, 2, OPF_DATA }, |
108e9fe3 |
841 | { "xchg", OP_XCHG, 2, 2, OPF_DATA }, |
69a3cdfc |
842 | { "not", OP_NOT, 1, 1, OPF_DATA }, |
5101a5f9 |
843 | { "cdq", OP_CDQ, 0, 0, OPF_DATA }, |
092f64e1 |
844 | { "lodsb",OP_LODS, 0, 0, OPF_DATA }, |
845 | { "lodsw",OP_LODS, 0, 0, OPF_DATA }, |
846 | { "lodsd",OP_LODS, 0, 0, OPF_DATA }, |
33c35af6 |
847 | { "stosb",OP_STOS, 0, 0, OPF_DATA }, |
848 | { "stosw",OP_STOS, 0, 0, OPF_DATA }, |
849 | { "stosd",OP_STOS, 0, 0, OPF_DATA }, |
d4e3b5db |
850 | { "movsb",OP_MOVS, 0, 0, OPF_DATA }, |
851 | { "movsw",OP_MOVS, 0, 0, OPF_DATA }, |
852 | { "movsd",OP_MOVS, 0, 0, OPF_DATA }, |
7ba45c34 |
853 | { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS }, |
854 | { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS }, |
855 | { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS }, |
591721d7 |
856 | { "scasb",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS }, |
857 | { "scasw",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS }, |
858 | { "scasd",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS }, |
859 | { "std", OP_STD, 0, 0, OPF_DATA }, // special flag |
860 | { "cld", OP_CLD, 0, 0, OPF_DATA }, |
69a3cdfc |
861 | { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS }, |
862 | { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS }, |
863 | { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS }, |
864 | { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS }, |
865 | { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS }, |
866 | { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS }, |
867 | { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS }, |
868 | { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS }, |
869 | { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS }, |
3b2f4044 |
870 | { "shrd", OP_SHRD, 3, 3, OPF_DATA|OPF_FLAGS }, |
d4e3b5db |
871 | { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS }, |
872 | { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS }, |
092f64e1 |
873 | { "rcl", OP_RCL, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C }, |
874 | { "rcr", OP_RCR, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C }, |
875 | { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C }, |
876 | { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C }, |
1f84f6b3 |
877 | { "bsf", OP_BSF, 2, 2, OPF_DATA|OPF_FLAGS }, |
69a3cdfc |
878 | { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS }, |
879 | { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS }, |
5101a5f9 |
880 | { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS }, |
881 | { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS }, |
69a3cdfc |
882 | { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS }, |
5101a5f9 |
883 | { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS }, |
884 | { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS }, |
69a3cdfc |
885 | { "test", OP_TEST, 2, 2, OPF_FLAGS }, |
886 | { "cmp", OP_CMP, 2, 2, OPF_FLAGS }, |
a3684be1 |
887 | { "retn", OP_RET, 0, 1, OPF_TAIL }, |
de50b98b |
888 | { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS }, |
69a3cdfc |
889 | { "jmp", OP_JMP, 1, 1, OPF_JMP }, |
5c024ef7 |
890 | { "jecxz",OP_JECXZ, 1, 1, OPF_JMP|OPF_CJMP }, |
092f64e1 |
891 | { "jo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 0 }, // 70 OF=1 |
892 | { "jno", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 1 }, // 71 OF=0 |
893 | { "jc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 CF=1 |
894 | { "jb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 |
895 | { "jnc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 CF=0 |
896 | { "jnb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 |
897 | { "jae", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 |
898 | { "jz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74 ZF=1 |
899 | { "je", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74 |
900 | { "jnz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75 ZF=0 |
901 | { "jne", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75 |
902 | { "jbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 CF=1||ZF=1 |
903 | { "jna", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 |
904 | { "ja", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 CF=0&&ZF=0 |
905 | { "jnbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 |
906 | { "js", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 0 }, // 78 SF=1 |
907 | { "jns", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 1 }, // 79 SF=0 |
908 | { "jp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a PF=1 |
909 | { "jpe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a |
910 | { "jnp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b PF=0 |
911 | { "jpo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b |
912 | { "jl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c SF!=OF |
913 | { "jnge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c |
914 | { "jge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d SF=OF |
915 | { "jnl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d |
916 | { "jle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e ZF=1||SF!=OF |
917 | { "jng", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e |
918 | { "jg", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f ZF=0&&SF=OF |
919 | { "jnle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f |
920 | { "seto", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 0 }, |
921 | { "setno", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 1 }, |
922 | { "setc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 }, |
923 | { "setb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 }, |
924 | { "setnc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 }, |
925 | { "setae", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 }, |
926 | { "setnb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 }, |
927 | { "setz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 }, |
928 | { "sete", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 }, |
929 | { "setnz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 }, |
930 | { "setne", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 }, |
931 | { "setbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 }, |
932 | { "setna", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 }, |
933 | { "seta", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 }, |
934 | { "setnbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 }, |
935 | { "sets", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 0 }, |
936 | { "setns", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 1 }, |
937 | { "setp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 }, |
938 | { "setpe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 }, |
939 | { "setnp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 }, |
940 | { "setpo", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 }, |
941 | { "setl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 }, |
942 | { "setnge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 }, |
943 | { "setge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 }, |
944 | { "setnl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 }, |
945 | { "setle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 }, |
946 | { "setng", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 }, |
947 | { "setg", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 }, |
948 | { "setnle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 }, |
90307a99 |
949 | // x87 |
950 | // mmx |
951 | { "emms", OP_EMMS, 0, 0, OPF_DATA }, |
952 | { "movq", OP_MOV, 2, 2, OPF_DATA }, |
2c10ea1f |
953 | // must be last |
954 | { "ud2", OP_UD2 }, |
69a3cdfc |
955 | }; |
c36e914d |
956 | |
957 | static void parse_op(struct parsed_op *op, char words[16][256], int wordc) |
958 | { |
bfa4a6ee |
959 | enum opr_lenmod lmod = OPLM_UNSPEC; |
33c35af6 |
960 | int prefix_flags = 0; |
69a3cdfc |
961 | int regmask_ind; |
962 | int regmask; |
33c35af6 |
963 | int op_w = 0; |
91977a1c |
964 | int opr = 0; |
33c35af6 |
965 | int w = 0; |
91977a1c |
966 | int i; |
c36e914d |
967 | |
33c35af6 |
968 | for (i = 0; i < ARRAY_SIZE(pref_table); i++) { |
969 | if (IS(words[w], pref_table[i].name)) { |
970 | prefix_flags = pref_table[i].flags; |
971 | break; |
972 | } |
973 | } |
974 | |
975 | if (prefix_flags) { |
976 | if (wordc <= 1) |
977 | aerr("lone prefix: '%s'\n", words[0]); |
978 | w++; |
979 | } |
980 | |
981 | op_w = w; |
91977a1c |
982 | for (i = 0; i < ARRAY_SIZE(op_table); i++) { |
33c35af6 |
983 | if (IS(words[w], op_table[i].name)) |
69a3cdfc |
984 | break; |
985 | } |
c36e914d |
986 | |
2c10ea1f |
987 | if (i == ARRAY_SIZE(op_table)) { |
30c8c549 |
988 | if (!g_skip_func) |
989 | aerr("unhandled op: '%s'\n", words[0]); |
2c10ea1f |
990 | i--; // OP_UD2 |
991 | } |
33c35af6 |
992 | w++; |
c36e914d |
993 | |
69a3cdfc |
994 | op->op = op_table[i].op; |
33c35af6 |
995 | op->flags = op_table[i].flags | prefix_flags; |
092f64e1 |
996 | op->pfo = op_table[i].pfo; |
997 | op->pfo_inv = op_table[i].pfo_inv; |
69a3cdfc |
998 | op->regmask_src = op->regmask_dst = 0; |
8eb12e72 |
999 | op->asmln = asmln; |
69a3cdfc |
1000 | |
2c10ea1f |
1001 | if (op->op == OP_UD2) |
1002 | return; |
1003 | |
92d715b6 |
1004 | for (opr = 0; opr < op_table[i].maxopr; opr++) { |
1005 | if (opr >= op_table[i].minopr && w >= wordc) |
1006 | break; |
1007 | |
69a3cdfc |
1008 | regmask = regmask_ind = 0; |
1009 | w = parse_operand(&op->operand[opr], ®mask, ®mask_ind, |
1010 | words, wordc, w, op->flags); |
1011 | |
1012 | if (opr == 0 && (op->flags & OPF_DATA)) |
1013 | op->regmask_dst = regmask; |
92d715b6 |
1014 | else |
1015 | op->regmask_src |= regmask; |
1016 | op->regmask_src |= regmask_ind; |
91977a1c |
1017 | } |
c36e914d |
1018 | |
91977a1c |
1019 | if (w < wordc) |
1020 | aerr("parse_op %s incomplete: %d/%d\n", |
1021 | words[0], w, wordc); |
5101a5f9 |
1022 | |
1023 | // special cases |
1024 | op->operand_cnt = opr; |
1025 | if (!strncmp(op_table[i].name, "set", 3)) |
1026 | op->operand[0].lmod = OPLM_BYTE; |
1027 | |
5101a5f9 |
1028 | switch (op->op) { |
92d715b6 |
1029 | // first operand is not dst |
1030 | case OP_CMP: |
1031 | case OP_TEST: |
1032 | op->regmask_src |= op->regmask_dst; |
1033 | op->regmask_dst = 0; |
1034 | break; |
1035 | |
1036 | // first operand is src too |
1037 | case OP_NOT: |
1038 | case OP_ADD: |
1039 | case OP_AND: |
1040 | case OP_OR: |
1041 | case OP_RCL: |
1042 | case OP_RCR: |
1043 | case OP_ADC: |
1044 | case OP_INC: |
1045 | case OP_DEC: |
1046 | case OP_NEG: |
1047 | // more below.. |
1048 | op->regmask_src |= op->regmask_dst; |
1049 | break; |
1050 | |
1051 | // special |
1052 | case OP_XCHG: |
1053 | op->regmask_src |= op->regmask_dst; |
1054 | op->regmask_dst |= op->regmask_src; |
1055 | goto check_align; |
1056 | |
1057 | case OP_SUB: |
1058 | case OP_SBB: |
1059 | case OP_XOR: |
1060 | if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG |
1061 | && op->operand[0].lmod == op->operand[1].lmod |
1062 | && op->operand[0].reg == op->operand[1].reg |
1063 | && IS(op->operand[0].name, op->operand[1].name)) // ! ah, al.. |
1064 | { |
1065 | op->regmask_src = 0; |
1066 | } |
1067 | else |
1068 | op->regmask_src |= op->regmask_dst; |
1069 | break; |
1070 | |
1071 | // ops with implicit argumets |
5101a5f9 |
1072 | case OP_CDQ: |
1073 | op->operand_cnt = 2; |
1074 | setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst); |
1075 | setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src); |
1076 | break; |
1077 | |
092f64e1 |
1078 | case OP_LODS: |
33c35af6 |
1079 | case OP_STOS: |
591721d7 |
1080 | case OP_SCAS: |
33c35af6 |
1081 | if (op->operand_cnt != 0) |
1082 | break; |
591721d7 |
1083 | if (words[op_w][4] == 'b') |
33c35af6 |
1084 | lmod = OPLM_BYTE; |
591721d7 |
1085 | else if (words[op_w][4] == 'w') |
33c35af6 |
1086 | lmod = OPLM_WORD; |
591721d7 |
1087 | else if (words[op_w][4] == 'd') |
33c35af6 |
1088 | lmod = OPLM_DWORD; |
1089 | op->operand_cnt = 3; |
092f64e1 |
1090 | setup_reg_opr(&op->operand[0], op->op == OP_LODS ? xSI : xDI, |
1091 | lmod, &op->regmask_src); |
d4e3b5db |
1092 | setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src); |
1093 | op->regmask_dst = op->regmask_src; |
092f64e1 |
1094 | setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD, |
1095 | op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src); |
33c35af6 |
1096 | break; |
1097 | |
d4e3b5db |
1098 | case OP_MOVS: |
7ba45c34 |
1099 | case OP_CMPS: |
d4e3b5db |
1100 | if (op->operand_cnt != 0) |
1101 | break; |
7ba45c34 |
1102 | if (words[op_w][4] == 'b') |
d4e3b5db |
1103 | lmod = OPLM_BYTE; |
7ba45c34 |
1104 | else if (words[op_w][4] == 'w') |
d4e3b5db |
1105 | lmod = OPLM_WORD; |
7ba45c34 |
1106 | else if (words[op_w][4] == 'd') |
d4e3b5db |
1107 | lmod = OPLM_DWORD; |
1108 | op->operand_cnt = 3; |
1109 | setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src); |
1110 | setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src); |
1111 | setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src); |
1112 | op->regmask_dst = op->regmask_src; |
1113 | break; |
1114 | |
5c024ef7 |
1115 | case OP_JECXZ: |
1116 | op->operand_cnt = 1; |
1117 | op->regmask_src = 1 << xCX; |
1118 | op->operand[0].type = OPT_REG; |
1119 | op->operand[0].reg = xCX; |
1120 | op->operand[0].lmod = OPLM_DWORD; |
1121 | break; |
1122 | |
5101a5f9 |
1123 | case OP_IMUL: |
1124 | if (op->operand_cnt != 1) |
1125 | break; |
1126 | // fallthrough |
1127 | case OP_MUL: |
1128 | // singleop mul |
92d715b6 |
1129 | op->regmask_src |= op->regmask_dst; |
5101a5f9 |
1130 | op->regmask_dst = (1 << xDX) | (1 << xAX); |
5101a5f9 |
1131 | if (op->operand[0].lmod == OPLM_UNSPEC) |
1132 | op->operand[0].lmod = OPLM_DWORD; |
1133 | break; |
1134 | |
1135 | case OP_DIV: |
1136 | case OP_IDIV: |
1137 | // we could set up operands for edx:eax, but there is no real need to |
1138 | // (see is_opr_modified()) |
92d715b6 |
1139 | op->regmask_src |= op->regmask_dst; |
1140 | op->regmask_dst = (1 << xDX) | (1 << xAX); |
5101a5f9 |
1141 | if (op->operand[0].lmod == OPLM_UNSPEC) |
1142 | op->operand[0].lmod = OPLM_DWORD; |
1143 | break; |
1144 | |
1145 | case OP_SHL: |
1146 | case OP_SHR: |
1147 | case OP_SAR: |
d4e3b5db |
1148 | case OP_ROL: |
1149 | case OP_ROR: |
92d715b6 |
1150 | op->regmask_src |= op->regmask_dst; |
5101a5f9 |
1151 | if (op->operand[1].lmod == OPLM_UNSPEC) |
1152 | op->operand[1].lmod = OPLM_BYTE; |
1153 | break; |
1154 | |
3b2f4044 |
1155 | case OP_SHRD: |
92d715b6 |
1156 | op->regmask_src |= op->regmask_dst; |
3b2f4044 |
1157 | if (op->operand[2].lmod == OPLM_UNSPEC) |
1158 | op->operand[2].lmod = OPLM_BYTE; |
1159 | break; |
1160 | |
7ba45c34 |
1161 | case OP_PUSH: |
92d715b6 |
1162 | op->regmask_src |= op->regmask_dst; |
1163 | op->regmask_dst = 0; |
7ba45c34 |
1164 | if (op->operand[0].lmod == OPLM_UNSPEC |
1165 | && (op->operand[0].type == OPT_CONST |
1166 | || op->operand[0].type == OPT_OFFSET |
1167 | || op->operand[0].type == OPT_LABEL)) |
1168 | op->operand[0].lmod = OPLM_DWORD; |
1169 | break; |
1170 | |
3ebea2cf |
1171 | // alignment |
1172 | case OP_MOV: |
092f64e1 |
1173 | check_align: |
3ebea2cf |
1174 | if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG |
092f64e1 |
1175 | && op->operand[0].lmod == op->operand[1].lmod |
1176 | && op->operand[0].reg == op->operand[1].reg |
92d715b6 |
1177 | && IS(op->operand[0].name, op->operand[1].name)) // ! ah, al.. |
3ebea2cf |
1178 | { |
5e49b270 |
1179 | op->flags |= OPF_RMD | OPF_DONE; |
092f64e1 |
1180 | op->regmask_src = op->regmask_dst = 0; |
3ebea2cf |
1181 | } |
1182 | break; |
1183 | |
de50b98b |
1184 | case OP_LEA: |
1185 | if (op->operand[0].type == OPT_REG |
1186 | && op->operand[1].type == OPT_REGMEM) |
1187 | { |
1188 | char buf[16]; |
1189 | snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name); |
1190 | if (IS(buf, op->operand[1].name)) |
5e49b270 |
1191 | op->flags |= OPF_RMD | OPF_DONE; |
de50b98b |
1192 | } |
1193 | break; |
1194 | |
89ff3147 |
1195 | case OP_CALL: |
1196 | // trashed regs must be explicitly detected later |
1197 | op->regmask_dst = 0; |
1198 | break; |
1199 | |
92d715b6 |
1200 | case OP_LEAVE: |
1201 | op->regmask_dst = (1 << xBP) | (1 << xSP); |
1202 | op->regmask_src = 1 << xBP; |
1203 | break; |
1204 | |
5101a5f9 |
1205 | default: |
1206 | break; |
1207 | } |
c36e914d |
1208 | } |
1209 | |
092f64e1 |
1210 | static const char *op_name(struct parsed_op *po) |
850c9265 |
1211 | { |
092f64e1 |
1212 | static char buf[16]; |
1213 | char *p; |
850c9265 |
1214 | int i; |
1215 | |
092f64e1 |
1216 | if (po->op == OP_JCC || po->op == OP_SCC) { |
1217 | p = buf; |
1218 | *p++ = (po->op == OP_JCC) ? 'j' : 's'; |
1219 | if (po->pfo_inv) |
1220 | *p++ = 'n'; |
1221 | strcpy(p, parsed_flag_op_names[po->pfo]); |
1222 | return buf; |
1223 | } |
1224 | |
850c9265 |
1225 | for (i = 0; i < ARRAY_SIZE(op_table); i++) |
092f64e1 |
1226 | if (op_table[i].op == po->op) |
850c9265 |
1227 | return op_table[i].name; |
1228 | |
1229 | return "???"; |
1230 | } |
1231 | |
1232 | // debug |
1233 | static const char *dump_op(struct parsed_op *po) |
1234 | { |
1235 | static char out[128]; |
1236 | char *p = out; |
1237 | int i; |
1238 | |
4c45fa73 |
1239 | if (po == NULL) |
1240 | return "???"; |
1241 | |
092f64e1 |
1242 | snprintf(out, sizeof(out), "%s", op_name(po)); |
850c9265 |
1243 | for (i = 0; i < po->operand_cnt; i++) { |
1244 | p += strlen(p); |
1245 | if (i > 0) |
1246 | *p++ = ','; |
1247 | snprintf(p, sizeof(out) - (p - out), |
1248 | po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s", |
1249 | po->operand[i].name); |
1250 | } |
1251 | |
1252 | return out; |
1253 | } |
1254 | |
4c45fa73 |
1255 | static const char *lmod_type_u(struct parsed_op *po, |
1256 | enum opr_lenmod lmod) |
1257 | { |
1258 | switch (lmod) { |
90307a99 |
1259 | case OPLM_QWORD: |
1260 | return "u64"; |
4c45fa73 |
1261 | case OPLM_DWORD: |
1262 | return "u32"; |
1263 | case OPLM_WORD: |
1264 | return "u16"; |
1265 | case OPLM_BYTE: |
1266 | return "u8"; |
1267 | default: |
1268 | ferr(po, "invalid lmod: %d\n", lmod); |
1269 | return "(_invalid_)"; |
1270 | } |
1271 | } |
1272 | |
d4e3b5db |
1273 | static const char *lmod_cast_u(struct parsed_op *po, |
1274 | enum opr_lenmod lmod) |
1275 | { |
1276 | switch (lmod) { |
90307a99 |
1277 | case OPLM_QWORD: |
1278 | return ""; |
d4e3b5db |
1279 | case OPLM_DWORD: |
1280 | return ""; |
1281 | case OPLM_WORD: |
1282 | return "(u16)"; |
1283 | case OPLM_BYTE: |
1284 | return "(u8)"; |
1285 | default: |
1286 | ferr(po, "invalid lmod: %d\n", lmod); |
1287 | return "(_invalid_)"; |
1288 | } |
1289 | } |
1290 | |
1291 | static const char *lmod_cast_u_ptr(struct parsed_op *po, |
1292 | enum opr_lenmod lmod) |
1293 | { |
1294 | switch (lmod) { |
90307a99 |
1295 | case OPLM_QWORD: |
1296 | return "*(u64 *)"; |
d4e3b5db |
1297 | case OPLM_DWORD: |
1298 | return "*(u32 *)"; |
1299 | case OPLM_WORD: |
1300 | return "*(u16 *)"; |
1301 | case OPLM_BYTE: |
1302 | return "*(u8 *)"; |
1303 | default: |
1304 | ferr(po, "invalid lmod: %d\n", lmod); |
1305 | return "(_invalid_)"; |
1306 | } |
1307 | } |
1308 | |
1309 | static const char *lmod_cast_s(struct parsed_op *po, |
1310 | enum opr_lenmod lmod) |
1311 | { |
1312 | switch (lmod) { |
90307a99 |
1313 | case OPLM_QWORD: |
1314 | return "(s64)"; |
d4e3b5db |
1315 | case OPLM_DWORD: |
1316 | return "(s32)"; |
1317 | case OPLM_WORD: |
1318 | return "(s16)"; |
1319 | case OPLM_BYTE: |
1320 | return "(s8)"; |
1321 | default: |
1322 | ferr(po, "%s: invalid lmod: %d\n", __func__, lmod); |
1323 | return "(_invalid_)"; |
1324 | } |
1325 | } |
1326 | |
1327 | static const char *lmod_cast(struct parsed_op *po, |
1328 | enum opr_lenmod lmod, int is_signed) |
1329 | { |
1330 | return is_signed ? |
1331 | lmod_cast_s(po, lmod) : |
1332 | lmod_cast_u(po, lmod); |
1333 | } |
1334 | |
1335 | static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod) |
1336 | { |
1337 | switch (lmod) { |
90307a99 |
1338 | case OPLM_QWORD: |
1339 | return 8; |
d4e3b5db |
1340 | case OPLM_DWORD: |
1341 | return 4; |
1342 | case OPLM_WORD: |
1343 | return 2; |
1344 | case OPLM_BYTE: |
1345 | return 1; |
1346 | default: |
1347 | ferr(po, "%s: invalid lmod: %d\n", __func__, lmod); |
1348 | return 0; |
1349 | } |
1350 | } |
1351 | |
91977a1c |
1352 | static const char *opr_name(struct parsed_op *po, int opr_num) |
c36e914d |
1353 | { |
91977a1c |
1354 | if (opr_num >= po->operand_cnt) |
1355 | ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt); |
1356 | return po->operand[opr_num].name; |
c36e914d |
1357 | } |
1358 | |
91977a1c |
1359 | static unsigned int opr_const(struct parsed_op *po, int opr_num) |
c36e914d |
1360 | { |
91977a1c |
1361 | if (opr_num >= po->operand_cnt) |
1362 | ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt); |
1363 | if (po->operand[opr_num].type != OPT_CONST) |
1364 | ferr(po, "opr %d: const expected\n", opr_num); |
1365 | return po->operand[opr_num].val; |
1366 | } |
c36e914d |
1367 | |
91977a1c |
1368 | static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr) |
1369 | { |
90307a99 |
1370 | if ((unsigned int)popr->reg >= ARRAY_SIZE(regs_r32)) |
91977a1c |
1371 | ferr(po, "invalid reg: %d\n", popr->reg); |
1372 | return regs_r32[popr->reg]; |
1373 | } |
c36e914d |
1374 | |
a2c1d768 |
1375 | // cast1 is the "final" cast |
1376 | static const char *simplify_cast(const char *cast1, const char *cast2) |
1377 | { |
1378 | static char buf[256]; |
1379 | |
1380 | if (cast1[0] == 0) |
1381 | return cast2; |
1382 | if (cast2[0] == 0) |
1383 | return cast1; |
1384 | if (IS(cast1, cast2)) |
1385 | return cast1; |
1386 | if (IS(cast1, "(s8)") && IS(cast2, "(u8)")) |
1387 | return cast1; |
1388 | if (IS(cast1, "(s16)") && IS(cast2, "(u16)")) |
1389 | return cast1; |
1390 | if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)")) |
1391 | return cast2; |
1392 | if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)")) |
1393 | return cast2; |
1cd4a663 |
1394 | if (strchr(cast1, '*') && IS_START(cast2, "(u32)")) |
1395 | return cast1; |
a2c1d768 |
1396 | |
1397 | snprintf(buf, sizeof(buf), "%s%s", cast1, cast2); |
1398 | return buf; |
1399 | } |
1400 | |
1401 | static const char *simplify_cast_num(const char *cast, unsigned int val) |
1402 | { |
1403 | if (IS(cast, "(u8)") && val < 0x100) |
1404 | return ""; |
1405 | if (IS(cast, "(s8)") && val < 0x80) |
1406 | return ""; |
1407 | if (IS(cast, "(u16)") && val < 0x10000) |
1408 | return ""; |
1409 | if (IS(cast, "(s16)") && val < 0x8000) |
1410 | return ""; |
1411 | if (IS(cast, "(s32)") && val < 0x80000000) |
1412 | return ""; |
1413 | |
1414 | return cast; |
1415 | } |
1416 | |
39b168b8 |
1417 | static struct parsed_equ *equ_find(struct parsed_op *po, const char *name, |
1418 | int *extra_offs) |
91977a1c |
1419 | { |
39b168b8 |
1420 | const char *p; |
1421 | char *endp; |
1422 | int namelen; |
850c9265 |
1423 | int i; |
1424 | |
39b168b8 |
1425 | *extra_offs = 0; |
1426 | namelen = strlen(name); |
1427 | |
1428 | p = strchr(name, '+'); |
1429 | if (p != NULL) { |
1430 | namelen = p - name; |
1431 | if (namelen <= 0) |
1432 | ferr(po, "equ parse failed for '%s'\n", name); |
1433 | |
1434 | if (IS_START(p, "0x")) |
1435 | p += 2; |
1436 | *extra_offs = strtol(p, &endp, 16); |
1437 | if (*endp != 0) |
1438 | ferr(po, "equ parse failed for '%s'\n", name); |
1439 | } |
1440 | |
850c9265 |
1441 | for (i = 0; i < g_eqcnt; i++) |
39b168b8 |
1442 | if (strncmp(g_eqs[i].name, name, namelen) == 0 |
1443 | && g_eqs[i].name[namelen] == 0) |
850c9265 |
1444 | break; |
87bf6cec |
1445 | if (i >= g_eqcnt) { |
1446 | if (po != NULL) |
1447 | ferr(po, "unresolved equ name: '%s'\n", name); |
1448 | return NULL; |
1449 | } |
850c9265 |
1450 | |
1451 | return &g_eqs[i]; |
1452 | } |
1453 | |
1cd4a663 |
1454 | static int is_stack_access(struct parsed_op *po, |
1455 | const struct parsed_opr *popr) |
850c9265 |
1456 | { |
09bc6fd5 |
1457 | return (parse_stack_el(popr->name, NULL, 0) |
1cd4a663 |
1458 | || (g_bp_frame && !(po->flags & OPF_EBP_S) |
1459 | && IS_START(popr->name, "ebp"))); |
1460 | } |
1461 | |
1462 | static void parse_stack_access(struct parsed_op *po, |
1463 | const char *name, char *ofs_reg, int *offset_out, |
037f4971 |
1464 | int *stack_ra_out, const char **bp_arg_out, int is_lea) |
1cd4a663 |
1465 | { |
1466 | const char *bp_arg = ""; |
1467 | const char *p = NULL; |
91977a1c |
1468 | struct parsed_equ *eq; |
4f12f671 |
1469 | char *endp = NULL; |
d4e3b5db |
1470 | int stack_ra = 0; |
39b168b8 |
1471 | int offset = 0; |
91977a1c |
1472 | |
1cd4a663 |
1473 | ofs_reg[0] = 0; |
a3684be1 |
1474 | |
1475 | if (IS_START(name, "ebp-") |
1476 | || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9')) |
1477 | { |
1478 | p = name + 4; |
1479 | if (IS_START(p, "0x")) |
1480 | p += 2; |
1481 | offset = strtoul(p, &endp, 16); |
1482 | if (name[3] == '-') |
1483 | offset = -offset; |
1484 | if (*endp != 0) |
1485 | ferr(po, "ebp- parse of '%s' failed\n", name); |
1486 | } |
1487 | else { |
09bc6fd5 |
1488 | bp_arg = parse_stack_el(name, ofs_reg, 0); |
4f12f671 |
1489 | snprintf(g_comment, sizeof(g_comment), "%s", bp_arg); |
1490 | eq = equ_find(po, bp_arg, &offset); |
1491 | if (eq == NULL) |
1492 | ferr(po, "detected but missing eq\n"); |
1493 | offset += eq->offset; |
1494 | } |
39b168b8 |
1495 | |
d4e3b5db |
1496 | if (!strncmp(name, "ebp", 3)) |
1497 | stack_ra = 4; |
1498 | |
037f4971 |
1499 | // yes it sometimes LEAs ra for compares.. |
1500 | if (!is_lea && ofs_reg[0] == 0 |
1501 | && stack_ra <= offset && offset < stack_ra + 4) |
1502 | { |
39b168b8 |
1503 | ferr(po, "reference to ra? %d %d\n", offset, stack_ra); |
037f4971 |
1504 | } |
d4e3b5db |
1505 | |
1cd4a663 |
1506 | *offset_out = offset; |
1507 | *stack_ra_out = stack_ra; |
1508 | if (bp_arg_out) |
1509 | *bp_arg_out = bp_arg; |
1510 | } |
1511 | |
5f70a34f |
1512 | static int stack_frame_access(struct parsed_op *po, |
1cd4a663 |
1513 | struct parsed_opr *popr, char *buf, size_t buf_size, |
1514 | const char *name, const char *cast, int is_src, int is_lea) |
1515 | { |
1516 | enum opr_lenmod tmp_lmod = OPLM_UNSPEC; |
1517 | const char *prefix = ""; |
1518 | const char *bp_arg = NULL; |
1519 | char ofs_reg[16] = { 0, }; |
1520 | int i, arg_i, arg_s; |
1521 | int unaligned = 0; |
1522 | int stack_ra = 0; |
1523 | int offset = 0; |
5f70a34f |
1524 | int retval = -1; |
1cd4a663 |
1525 | int sf_ofs; |
1526 | int lim; |
1527 | |
1528 | if (po->flags & OPF_EBP_S) |
1529 | ferr(po, "stack_frame_access while ebp is scratch\n"); |
1530 | |
037f4971 |
1531 | parse_stack_access(po, name, ofs_reg, &offset, |
1532 | &stack_ra, &bp_arg, is_lea); |
1cd4a663 |
1533 | |
4f12f671 |
1534 | if (offset > stack_ra) |
1535 | { |
39b168b8 |
1536 | arg_i = (offset - stack_ra - 4) / 4; |
bd96f656 |
1537 | if (arg_i < 0 || arg_i >= g_func_pp->argc_stack) |
4f12f671 |
1538 | { |
bd96f656 |
1539 | if (g_func_pp->is_vararg |
1540 | && arg_i == g_func_pp->argc_stack && is_lea) |
1541 | { |
4f12f671 |
1542 | // should be va_list |
1543 | if (cast[0] == 0) |
1544 | cast = "(u32)"; |
1545 | snprintf(buf, buf_size, "%sap", cast); |
5f70a34f |
1546 | return -1; |
4f12f671 |
1547 | } |
1548 | ferr(po, "offset %d (%s,%d) doesn't map to any arg\n", |
1549 | offset, bp_arg, arg_i); |
1550 | } |
4c45fa73 |
1551 | if (ofs_reg[0] != 0) |
7ba45c34 |
1552 | ferr(po, "offset reg on arg access?\n"); |
91977a1c |
1553 | |
bd96f656 |
1554 | for (i = arg_s = 0; i < g_func_pp->argc; i++) { |
1555 | if (g_func_pp->arg[i].reg != NULL) |
91977a1c |
1556 | continue; |
1557 | if (arg_s == arg_i) |
1558 | break; |
1559 | arg_s++; |
1560 | } |
bd96f656 |
1561 | if (i == g_func_pp->argc) |
91977a1c |
1562 | ferr(po, "arg %d not in prototype?\n", arg_i); |
850c9265 |
1563 | |
bd96f656 |
1564 | popr->is_ptr = g_func_pp->arg[i].type.is_ptr; |
5f70a34f |
1565 | retval = i; |
de50b98b |
1566 | |
1567 | switch (popr->lmod) |
7ba45c34 |
1568 | { |
1569 | case OPLM_BYTE: |
4f12f671 |
1570 | if (is_lea) |
1571 | ferr(po, "lea/byte to arg?\n"); |
7ba45c34 |
1572 | if (is_src && (offset & 3) == 0) |
a2c1d768 |
1573 | snprintf(buf, buf_size, "%sa%d", |
1574 | simplify_cast(cast, "(u8)"), i + 1); |
7ba45c34 |
1575 | else |
a2c1d768 |
1576 | snprintf(buf, buf_size, "%sBYTE%d(a%d)", |
1577 | cast, offset & 3, i + 1); |
7ba45c34 |
1578 | break; |
1579 | |
1580 | case OPLM_WORD: |
4f12f671 |
1581 | if (is_lea) |
1582 | ferr(po, "lea/word to arg?\n"); |
a3684be1 |
1583 | if (offset & 1) { |
1584 | unaligned = 1; |
1585 | if (!is_src) { |
1586 | if (offset & 2) |
1587 | ferr(po, "problematic arg store\n"); |
a2c1d768 |
1588 | snprintf(buf, buf_size, "%s((char *)&a%d + 1)", |
1589 | simplify_cast(cast, "*(u16 *)"), i + 1); |
a3684be1 |
1590 | } |
1591 | else |
1592 | ferr(po, "unaligned arg word load\n"); |
1593 | } |
1594 | else if (is_src && (offset & 2) == 0) |
a2c1d768 |
1595 | snprintf(buf, buf_size, "%sa%d", |
1596 | simplify_cast(cast, "(u16)"), i + 1); |
7ba45c34 |
1597 | else |
a2c1d768 |
1598 | snprintf(buf, buf_size, "%s%sWORD(a%d)", |
1599 | cast, (offset & 2) ? "HI" : "LO", i + 1); |
7ba45c34 |
1600 | break; |
1601 | |
1602 | case OPLM_DWORD: |
3ebea2cf |
1603 | if (cast[0]) |
1604 | prefix = cast; |
1605 | else if (is_src) |
1606 | prefix = "(u32)"; |
a3684be1 |
1607 | |
de50b98b |
1608 | if (offset & 3) { |
a3684be1 |
1609 | unaligned = 1; |
2b43685d |
1610 | if (is_lea) |
1611 | snprintf(buf, buf_size, "(u32)&a%d + %d", |
1612 | i + 1, offset & 3); |
a3684be1 |
1613 | else if (!is_src) |
1614 | ferr(po, "unaligned arg store\n"); |
1615 | else { |
1616 | // mov edx, [ebp+arg_4+2]; movsx ecx, dx |
2b43685d |
1617 | snprintf(buf, buf_size, "%s(a%d >> %d)", |
1618 | prefix, i + 1, (offset & 3) * 8); |
a3684be1 |
1619 | } |
de50b98b |
1620 | } |
1621 | else { |
1622 | snprintf(buf, buf_size, "%s%sa%d", |
1623 | prefix, is_lea ? "&" : "", i + 1); |
1624 | } |
7ba45c34 |
1625 | break; |
1626 | |
1627 | default: |
de50b98b |
1628 | ferr(po, "bp_arg bad lmod: %d\n", popr->lmod); |
7ba45c34 |
1629 | } |
1630 | |
a3684be1 |
1631 | if (unaligned) |
1632 | snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg); |
1633 | |
7ba45c34 |
1634 | // common problem |
bd96f656 |
1635 | guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type); |
a2c1d768 |
1636 | if (tmp_lmod != OPLM_DWORD |
de041e5b |
1637 | && (unaligned || (!is_src && lmod_bytes(po, tmp_lmod) |
1638 | < lmod_bytes(po, popr->lmod) + (offset & 3)))) |
a2c1d768 |
1639 | { |
1640 | ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n", |
1641 | i + 1, offset, g_func_pp->arg[i].type.name); |
1642 | } |
4741fdfe |
1643 | // can't check this because msvc likes to reuse |
1644 | // arg space for scratch.. |
1645 | //if (popr->is_ptr && popr->lmod != OPLM_DWORD) |
1646 | // ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1); |
91977a1c |
1647 | } |
4f12f671 |
1648 | else |
1649 | { |
1bafb621 |
1650 | if (g_stack_fsz == 0) |
1651 | ferr(po, "stack var access without stackframe\n"); |
a2c1d768 |
1652 | g_stack_frame_used = 1; |
850c9265 |
1653 | |
39b168b8 |
1654 | sf_ofs = g_stack_fsz + offset; |
de50b98b |
1655 | lim = (ofs_reg[0] != 0) ? -4 : 0; |
1656 | if (offset > 0 || sf_ofs < lim) |
39b168b8 |
1657 | ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz); |
850c9265 |
1658 | |
1659 | if (is_lea) |
33c35af6 |
1660 | prefix = "(u32)&"; |
3ebea2cf |
1661 | else |
1662 | prefix = cast; |
850c9265 |
1663 | |
de50b98b |
1664 | switch (popr->lmod) |
850c9265 |
1665 | { |
1666 | case OPLM_BYTE: |
7ba45c34 |
1667 | snprintf(buf, buf_size, "%ssf.b[%d%s%s]", |
1668 | prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg); |
850c9265 |
1669 | break; |
7ba45c34 |
1670 | |
850c9265 |
1671 | case OPLM_WORD: |
7ba45c34 |
1672 | if ((sf_ofs & 1) || ofs_reg[0] != 0) { |
1673 | // known unaligned or possibly unaligned |
1674 | strcat(g_comment, " unaligned"); |
1675 | if (prefix[0] == 0) |
1676 | prefix = "*(u16 *)&"; |
1677 | snprintf(buf, buf_size, "%ssf.b[%d%s%s]", |
1678 | prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg); |
1679 | break; |
1680 | } |
1681 | snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2); |
850c9265 |
1682 | break; |
7ba45c34 |
1683 | |
850c9265 |
1684 | case OPLM_DWORD: |
7ba45c34 |
1685 | if ((sf_ofs & 3) || ofs_reg[0] != 0) { |
1686 | // known unaligned or possibly unaligned |
1687 | strcat(g_comment, " unaligned"); |
1688 | if (prefix[0] == 0) |
1689 | prefix = "*(u32 *)&"; |
1690 | snprintf(buf, buf_size, "%ssf.b[%d%s%s]", |
1691 | prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg); |
1692 | break; |
1693 | } |
1694 | snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4); |
850c9265 |
1695 | break; |
7ba45c34 |
1696 | |
850c9265 |
1697 | default: |
de50b98b |
1698 | ferr(po, "bp_stack bad lmod: %d\n", popr->lmod); |
850c9265 |
1699 | } |
91977a1c |
1700 | } |
5f70a34f |
1701 | |
1702 | return retval; |
91977a1c |
1703 | } |
c36e914d |
1704 | |
89ff3147 |
1705 | static void check_func_pp(struct parsed_op *po, |
1706 | const struct parsed_proto *pp, const char *pfx) |
1707 | { |
179b79a9 |
1708 | enum opr_lenmod tmp_lmod; |
b74c31e3 |
1709 | char buf[256]; |
179b79a9 |
1710 | int ret, i; |
b74c31e3 |
1711 | |
89ff3147 |
1712 | if (pp->argc_reg != 0) { |
b74c31e3 |
1713 | if (/*!g_allow_regfunc &&*/ !pp->is_fastcall) { |
1714 | pp_print(buf, sizeof(buf), pp); |
1715 | ferr(po, "%s: unexpected reg arg in icall: %s\n", pfx, buf); |
1716 | } |
89ff3147 |
1717 | if (pp->argc_stack > 0 && pp->argc_reg != 2) |
1718 | ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n", |
1719 | pfx, pp->argc_reg, pp->argc_stack); |
1720 | } |
179b79a9 |
1721 | |
1722 | // fptrs must use 32bit args, callsite might have no information and |
1723 | // lack a cast to smaller types, which results in incorrectly masked |
1724 | // args passed (callee may assume masked args, it does on ARM) |
61e29183 |
1725 | if (!pp->is_osinc) { |
179b79a9 |
1726 | for (i = 0; i < pp->argc; i++) { |
1727 | ret = guess_lmod_from_c_type(&tmp_lmod, &pp->arg[i].type); |
1728 | if (ret && tmp_lmod != OPLM_DWORD) |
1729 | ferr(po, "reference to %s with arg%d '%s'\n", pp->name, |
1730 | i + 1, pp->arg[i].type.name); |
1731 | } |
1732 | } |
89ff3147 |
1733 | } |
1734 | |
7aca4698 |
1735 | static const char *check_label_read_ref(struct parsed_op *po, |
1736 | const char *name) |
3ebea2cf |
1737 | { |
840257f6 |
1738 | const struct parsed_proto *pp; |
1739 | |
36595fd2 |
1740 | pp = proto_parse(g_fhdr, name, 0); |
840257f6 |
1741 | if (pp == NULL) |
1742 | ferr(po, "proto_parse failed for ref '%s'\n", name); |
1743 | |
89ff3147 |
1744 | if (pp->is_func) |
1745 | check_func_pp(po, pp, "ref"); |
7aca4698 |
1746 | |
1747 | return pp->name; |
3ebea2cf |
1748 | } |
1749 | |
91977a1c |
1750 | static char *out_src_opr(char *buf, size_t buf_size, |
591721d7 |
1751 | struct parsed_op *po, struct parsed_opr *popr, const char *cast, |
3ebea2cf |
1752 | int is_lea) |
91977a1c |
1753 | { |
850c9265 |
1754 | char tmp1[256], tmp2[256]; |
1755 | char expr[256]; |
7aca4698 |
1756 | const char *name; |
a2c1d768 |
1757 | char *p; |
850c9265 |
1758 | int ret; |
1759 | |
3ebea2cf |
1760 | if (cast == NULL) |
1761 | cast = ""; |
1762 | |
91977a1c |
1763 | switch (popr->type) { |
1764 | case OPT_REG: |
850c9265 |
1765 | if (is_lea) |
1766 | ferr(po, "lea from reg?\n"); |
1767 | |
91977a1c |
1768 | switch (popr->lmod) { |
90307a99 |
1769 | case OPLM_QWORD: |
1770 | snprintf(buf, buf_size, "%s%s.q", cast, opr_reg_p(po, popr)); |
1771 | break; |
91977a1c |
1772 | case OPLM_DWORD: |
3ebea2cf |
1773 | snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr)); |
91977a1c |
1774 | break; |
850c9265 |
1775 | case OPLM_WORD: |
a2c1d768 |
1776 | snprintf(buf, buf_size, "%s%s", |
1777 | simplify_cast(cast, "(u16)"), opr_reg_p(po, popr)); |
850c9265 |
1778 | break; |
1779 | case OPLM_BYTE: |
5101a5f9 |
1780 | if (popr->name[1] == 'h') // XXX.. |
a2c1d768 |
1781 | snprintf(buf, buf_size, "%s(%s >> 8)", |
1782 | simplify_cast(cast, "(u8)"), opr_reg_p(po, popr)); |
5101a5f9 |
1783 | else |
a2c1d768 |
1784 | snprintf(buf, buf_size, "%s%s", |
1785 | simplify_cast(cast, "(u8)"), opr_reg_p(po, popr)); |
850c9265 |
1786 | break; |
91977a1c |
1787 | default: |
1788 | ferr(po, "invalid src lmod: %d\n", popr->lmod); |
1789 | } |
1790 | break; |
850c9265 |
1791 | |
91977a1c |
1792 | case OPT_REGMEM: |
1cd4a663 |
1793 | if (is_stack_access(po, popr)) { |
de50b98b |
1794 | stack_frame_access(po, popr, buf, buf_size, |
3ebea2cf |
1795 | popr->name, cast, 1, is_lea); |
91977a1c |
1796 | break; |
1797 | } |
850c9265 |
1798 | |
1799 | strcpy(expr, popr->name); |
1800 | if (strchr(expr, '[')) { |
1801 | // special case: '[' can only be left for label[reg] form |
1802 | ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2); |
1803 | if (ret != 2) |
1804 | ferr(po, "parse failure for '%s'\n", expr); |
a2c1d768 |
1805 | if (tmp1[0] == '(') { |
1806 | // (off_4FFF50+3)[eax] |
1807 | p = strchr(tmp1 + 1, ')'); |
1808 | if (p == NULL || p[1] != 0) |
1809 | ferr(po, "parse failure (2) for '%s'\n", expr); |
1810 | *p = 0; |
1811 | memmove(tmp1, tmp1 + 1, strlen(tmp1)); |
1812 | } |
33c35af6 |
1813 | snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2); |
850c9265 |
1814 | } |
1815 | |
1816 | // XXX: do we need more parsing? |
1817 | if (is_lea) { |
1818 | snprintf(buf, buf_size, "%s", expr); |
1819 | break; |
1820 | } |
1821 | |
a2c1d768 |
1822 | snprintf(buf, buf_size, "%s(%s)", |
1823 | simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr); |
91977a1c |
1824 | break; |
850c9265 |
1825 | |
91977a1c |
1826 | case OPT_LABEL: |
7aca4698 |
1827 | name = check_label_read_ref(po, popr->name); |
3ebea2cf |
1828 | if (cast[0] == 0 && popr->is_ptr) |
1829 | cast = "(u32)"; |
2b43685d |
1830 | |
850c9265 |
1831 | if (is_lea) |
7aca4698 |
1832 | snprintf(buf, buf_size, "(u32)&%s", name); |
2b43685d |
1833 | else if (popr->size_lt) |
1834 | snprintf(buf, buf_size, "%s%s%s%s", cast, |
1835 | lmod_cast_u_ptr(po, popr->lmod), |
7aca4698 |
1836 | popr->is_array ? "" : "&", name); |
850c9265 |
1837 | else |
7aca4698 |
1838 | snprintf(buf, buf_size, "%s%s%s", cast, name, |
7ba45c34 |
1839 | popr->is_array ? "[0]" : ""); |
850c9265 |
1840 | break; |
1841 | |
1842 | case OPT_OFFSET: |
7aca4698 |
1843 | name = check_label_read_ref(po, popr->name); |
3ebea2cf |
1844 | if (cast[0] == 0) |
1845 | cast = "(u32)"; |
850c9265 |
1846 | if (is_lea) |
1847 | ferr(po, "lea an offset?\n"); |
7aca4698 |
1848 | snprintf(buf, buf_size, "%s&%s", cast, name); |
91977a1c |
1849 | break; |
850c9265 |
1850 | |
91977a1c |
1851 | case OPT_CONST: |
850c9265 |
1852 | if (is_lea) |
1853 | ferr(po, "lea from const?\n"); |
1854 | |
a2c1d768 |
1855 | printf_number(tmp1, sizeof(tmp1), popr->val); |
ddaf8bd7 |
1856 | if (popr->val == 0 && strchr(cast, '*')) |
1857 | snprintf(buf, buf_size, "NULL"); |
1858 | else |
1859 | snprintf(buf, buf_size, "%s%s", |
1860 | simplify_cast_num(cast, popr->val), tmp1); |
91977a1c |
1861 | break; |
850c9265 |
1862 | |
91977a1c |
1863 | default: |
1864 | ferr(po, "invalid src type: %d\n", popr->type); |
1865 | } |
1866 | |
1867 | return buf; |
1868 | } |
c36e914d |
1869 | |
de50b98b |
1870 | // note: may set is_ptr (we find that out late for ebp frame..) |
91977a1c |
1871 | static char *out_dst_opr(char *buf, size_t buf_size, |
1872 | struct parsed_op *po, struct parsed_opr *popr) |
1873 | { |
1874 | switch (popr->type) { |
1875 | case OPT_REG: |
1876 | switch (popr->lmod) { |
90307a99 |
1877 | case OPLM_QWORD: |
1878 | snprintf(buf, buf_size, "%s.q", opr_reg_p(po, popr)); |
1879 | break; |
91977a1c |
1880 | case OPLM_DWORD: |
1881 | snprintf(buf, buf_size, "%s", opr_reg_p(po, popr)); |
1882 | break; |
850c9265 |
1883 | case OPLM_WORD: |
1884 | // ugh.. |
1885 | snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr)); |
1886 | break; |
1887 | case OPLM_BYTE: |
1888 | // ugh.. |
5101a5f9 |
1889 | if (popr->name[1] == 'h') // XXX.. |
1890 | snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr)); |
1891 | else |
1892 | snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr)); |
850c9265 |
1893 | break; |
91977a1c |
1894 | default: |
1895 | ferr(po, "invalid dst lmod: %d\n", popr->lmod); |
1896 | } |
1897 | break; |
850c9265 |
1898 | |
1899 | case OPT_REGMEM: |
1cd4a663 |
1900 | if (is_stack_access(po, popr)) { |
de50b98b |
1901 | stack_frame_access(po, popr, buf, buf_size, |
3ebea2cf |
1902 | popr->name, "", 0, 0); |
850c9265 |
1903 | break; |
1904 | } |
1905 | |
3ebea2cf |
1906 | return out_src_opr(buf, buf_size, po, popr, NULL, 0); |
850c9265 |
1907 | |
bfa4a6ee |
1908 | case OPT_LABEL: |
2b43685d |
1909 | if (popr->size_mismatch) |
1910 | snprintf(buf, buf_size, "%s%s%s", |
1911 | lmod_cast_u_ptr(po, popr->lmod), |
1912 | popr->is_array ? "" : "&", popr->name); |
1913 | else |
1914 | snprintf(buf, buf_size, "%s%s", popr->name, |
1915 | popr->is_array ? "[0]" : ""); |
bfa4a6ee |
1916 | break; |
1917 | |
91977a1c |
1918 | default: |
1919 | ferr(po, "invalid dst type: %d\n", popr->type); |
1920 | } |
1921 | |
1922 | return buf; |
1923 | } |
c36e914d |
1924 | |
3ebea2cf |
1925 | static char *out_src_opr_u32(char *buf, size_t buf_size, |
1926 | struct parsed_op *po, struct parsed_opr *popr) |
1927 | { |
1928 | return out_src_opr(buf, buf_size, po, popr, NULL, 0); |
1929 | } |
1930 | |
91977a1c |
1931 | static void out_test_for_cc(char *buf, size_t buf_size, |
940e8e66 |
1932 | struct parsed_op *po, enum parsed_flag_op pfo, int is_inv, |
69a3cdfc |
1933 | enum opr_lenmod lmod, const char *expr) |
91977a1c |
1934 | { |
69a3cdfc |
1935 | const char *cast, *scast; |
91977a1c |
1936 | |
69a3cdfc |
1937 | cast = lmod_cast_u(po, lmod); |
1938 | scast = lmod_cast_s(po, lmod); |
1939 | |
1940 | switch (pfo) { |
1941 | case PFO_Z: |
87bf6cec |
1942 | case PFO_BE: // CF=1||ZF=1; CF=0 |
850c9265 |
1943 | snprintf(buf, buf_size, "(%s%s %s 0)", |
940e8e66 |
1944 | cast, expr, is_inv ? "!=" : "=="); |
91977a1c |
1945 | break; |
850c9265 |
1946 | |
5101a5f9 |
1947 | case PFO_S: |
1948 | case PFO_L: // SF!=OF; OF=0 |
1949 | snprintf(buf, buf_size, "(%s%s %s 0)", |
940e8e66 |
1950 | scast, expr, is_inv ? ">=" : "<"); |
5101a5f9 |
1951 | break; |
1952 | |
87bf6cec |
1953 | case PFO_LE: // ZF=1||SF!=OF; OF=0 |
69a3cdfc |
1954 | snprintf(buf, buf_size, "(%s%s %s 0)", |
940e8e66 |
1955 | scast, expr, is_inv ? ">" : "<="); |
850c9265 |
1956 | break; |
1957 | |
91977a1c |
1958 | default: |
69a3cdfc |
1959 | ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo); |
91977a1c |
1960 | } |
1961 | } |
c36e914d |
1962 | |
850c9265 |
1963 | static void out_cmp_for_cc(char *buf, size_t buf_size, |
a2c1d768 |
1964 | struct parsed_op *po, enum parsed_flag_op pfo, int is_inv) |
850c9265 |
1965 | { |
a2c1d768 |
1966 | const char *cast, *scast, *cast_use; |
1967 | char buf1[256], buf2[256]; |
1968 | enum opr_lenmod lmod; |
1969 | |
90307a99 |
1970 | if (po->op != OP_DEC && po->operand[0].lmod != po->operand[1].lmod) |
a2c1d768 |
1971 | ferr(po, "%s: lmod mismatch: %d %d\n", __func__, |
1972 | po->operand[0].lmod, po->operand[1].lmod); |
1973 | lmod = po->operand[0].lmod; |
850c9265 |
1974 | |
69a3cdfc |
1975 | cast = lmod_cast_u(po, lmod); |
1976 | scast = lmod_cast_s(po, lmod); |
850c9265 |
1977 | |
a2c1d768 |
1978 | switch (pfo) { |
1979 | case PFO_C: |
1980 | case PFO_Z: |
1981 | case PFO_BE: // !a |
1982 | cast_use = cast; |
1983 | break; |
1984 | |
1985 | case PFO_S: |
1986 | case PFO_L: // !ge |
1987 | case PFO_LE: |
1988 | cast_use = scast; |
1989 | break; |
1990 | |
1991 | default: |
1992 | ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo); |
1993 | } |
1994 | |
1995 | out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0); |
90307a99 |
1996 | if (po->op == OP_DEC) |
1997 | snprintf(buf2, sizeof(buf2), "1"); |
1998 | else |
1999 | out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0); |
a2c1d768 |
2000 | |
69a3cdfc |
2001 | switch (pfo) { |
5101a5f9 |
2002 | case PFO_C: |
2003 | // note: must be unsigned compare |
a2c1d768 |
2004 | snprintf(buf, buf_size, "(%s %s %s)", |
2005 | buf1, is_inv ? ">=" : "<", buf2); |
5101a5f9 |
2006 | break; |
2007 | |
69a3cdfc |
2008 | case PFO_Z: |
a2c1d768 |
2009 | snprintf(buf, buf_size, "(%s %s %s)", |
2010 | buf1, is_inv ? "!=" : "==", buf2); |
850c9265 |
2011 | break; |
2012 | |
5101a5f9 |
2013 | case PFO_BE: // !a |
850c9265 |
2014 | // note: must be unsigned compare |
a2c1d768 |
2015 | snprintf(buf, buf_size, "(%s %s %s)", |
2016 | buf1, is_inv ? ">" : "<=", buf2); |
2017 | |
2018 | // annoying case |
2019 | if (is_inv && lmod == OPLM_BYTE |
2020 | && po->operand[1].type == OPT_CONST |
2021 | && po->operand[1].val == 0xff) |
2022 | { |
2023 | snprintf(g_comment, sizeof(g_comment), "if %s", buf); |
2024 | snprintf(buf, buf_size, "(0)"); |
2025 | } |
5101a5f9 |
2026 | break; |
2027 | |
2028 | // note: must be signed compare |
2029 | case PFO_S: |
2030 | snprintf(buf, buf_size, "(%s(%s - %s) %s 0)", |
a2c1d768 |
2031 | scast, buf1, buf2, is_inv ? ">=" : "<"); |
850c9265 |
2032 | break; |
2033 | |
5101a5f9 |
2034 | case PFO_L: // !ge |
a2c1d768 |
2035 | snprintf(buf, buf_size, "(%s %s %s)", |
2036 | buf1, is_inv ? ">=" : "<", buf2); |
850c9265 |
2037 | break; |
2038 | |
90307a99 |
2039 | case PFO_LE: // !g |
a2c1d768 |
2040 | snprintf(buf, buf_size, "(%s %s %s)", |
2041 | buf1, is_inv ? ">" : "<=", buf2); |
5101a5f9 |
2042 | break; |
2043 | |
850c9265 |
2044 | default: |
a2c1d768 |
2045 | break; |
850c9265 |
2046 | } |
2047 | } |
2048 | |
69a3cdfc |
2049 | static void out_cmp_test(char *buf, size_t buf_size, |
940e8e66 |
2050 | struct parsed_op *po, enum parsed_flag_op pfo, int is_inv) |
69a3cdfc |
2051 | { |
2052 | char buf1[256], buf2[256], buf3[256]; |
2053 | |
2054 | if (po->op == OP_TEST) { |
2055 | if (IS(opr_name(po, 0), opr_name(po, 1))) { |
3ebea2cf |
2056 | out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]); |
69a3cdfc |
2057 | } |
2058 | else { |
3ebea2cf |
2059 | out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]); |
2060 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]); |
69a3cdfc |
2061 | snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2); |
2062 | } |
940e8e66 |
2063 | out_test_for_cc(buf, buf_size, po, pfo, is_inv, |
69a3cdfc |
2064 | po->operand[0].lmod, buf3); |
2065 | } |
2066 | else if (po->op == OP_CMP) { |
a2c1d768 |
2067 | out_cmp_for_cc(buf, buf_size, po, pfo, is_inv); |
69a3cdfc |
2068 | } |
2069 | else |
2070 | ferr(po, "%s: unhandled op: %d\n", __func__, po->op); |
2071 | } |
2072 | |
850c9265 |
2073 | static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1, |
91977a1c |
2074 | struct parsed_opr *popr2) |
2075 | { |
2076 | if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC) |
2077 | ferr(po, "missing lmod for both operands\n"); |
2078 | |
2079 | if (popr1->lmod == OPLM_UNSPEC) |
2080 | popr1->lmod = popr2->lmod; |
2081 | else if (popr2->lmod == OPLM_UNSPEC) |
2082 | popr2->lmod = popr1->lmod; |
a3684be1 |
2083 | else if (popr1->lmod != popr2->lmod) { |
2084 | if (popr1->type_from_var) { |
2085 | popr1->size_mismatch = 1; |
2086 | if (popr1->lmod < popr2->lmod) |
2087 | popr1->size_lt = 1; |
2088 | popr1->lmod = popr2->lmod; |
2089 | } |
2090 | else if (popr2->type_from_var) { |
2091 | popr2->size_mismatch = 1; |
2092 | if (popr2->lmod < popr1->lmod) |
2093 | popr2->size_lt = 1; |
2094 | popr2->lmod = popr1->lmod; |
2095 | } |
2096 | else |
2097 | ferr(po, "conflicting lmods: %d vs %d\n", |
2098 | popr1->lmod, popr2->lmod); |
2099 | } |
91977a1c |
2100 | } |
c36e914d |
2101 | |
850c9265 |
2102 | static const char *op_to_c(struct parsed_op *po) |
2103 | { |
2104 | switch (po->op) |
2105 | { |
2106 | case OP_ADD: |
5101a5f9 |
2107 | case OP_ADC: |
850c9265 |
2108 | return "+"; |
2109 | case OP_SUB: |
5101a5f9 |
2110 | case OP_SBB: |
850c9265 |
2111 | return "-"; |
2112 | case OP_AND: |
2113 | return "&"; |
2114 | case OP_OR: |
2115 | return "|"; |
2116 | case OP_XOR: |
2117 | return "^"; |
2118 | case OP_SHL: |
2119 | return "<<"; |
2120 | case OP_SHR: |
2121 | return ">>"; |
2122 | case OP_MUL: |
2123 | case OP_IMUL: |
2124 | return "*"; |
2125 | default: |
2126 | ferr(po, "op_to_c was supplied with %d\n", po->op); |
2127 | } |
2128 | } |
2129 | |
7ae48d73 |
2130 | static void op_set_clear_flag(struct parsed_op *po, |
2131 | enum op_flags flag_set, enum op_flags flag_clear) |
d4e3b5db |
2132 | { |
7ae48d73 |
2133 | po->flags |= flag_set; |
2134 | po->flags &= ~flag_clear; |
d4e3b5db |
2135 | } |
2136 | |
de50b98b |
2137 | // last op in stream - unconditional branch or ret |
2138 | #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \ |
092f64e1 |
2139 | || ((ops[_i].flags & (OPF_JMP|OPF_CJMP|OPF_RMD)) == OPF_JMP \ |
037f4971 |
2140 | && ops[_i].op != OP_CALL)) |
de50b98b |
2141 | |
87bf6cec |
2142 | static int scan_for_pop(int i, int opcnt, const char *reg, |
d4e3b5db |
2143 | int magic, int depth, int *maxdepth, int do_flags) |
850c9265 |
2144 | { |
89ff3147 |
2145 | const struct parsed_proto *pp; |
87bf6cec |
2146 | struct parsed_op *po; |
2147 | int ret = 0; |
4c45fa73 |
2148 | int j; |
87bf6cec |
2149 | |
850c9265 |
2150 | for (; i < opcnt; i++) { |
87bf6cec |
2151 | po = &ops[i]; |
2152 | if (po->cc_scratch == magic) |
2153 | break; // already checked |
2154 | po->cc_scratch = magic; |
2155 | |
89ff3147 |
2156 | if (po->flags & OPF_TAIL) { |
2157 | if (po->op == OP_CALL) { |
92d715b6 |
2158 | pp = proto_parse(g_fhdr, po->operand[0].name, g_quiet_pp); |
89ff3147 |
2159 | if (pp != NULL && pp->is_noreturn) |
2160 | // no stack cleanup for noreturn |
2161 | return ret; |
2162 | } |
87bf6cec |
2163 | return -1; // deadend |
89ff3147 |
2164 | } |
87bf6cec |
2165 | |
5e49b270 |
2166 | if ((po->flags & (OPF_RMD|OPF_DONE)) |
5f70a34f |
2167 | || (po->op == OP_PUSH && po->p_argnum != 0)) // arg push |
850c9265 |
2168 | continue; |
2169 | |
87bf6cec |
2170 | if ((po->flags & OPF_JMP) && po->op != OP_CALL) { |
4c45fa73 |
2171 | if (po->btj != NULL) { |
2172 | // jumptable |
da87ae38 |
2173 | for (j = 0; j < po->btj->count; j++) { |
4c45fa73 |
2174 | ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic, |
2175 | depth, maxdepth, do_flags); |
2176 | if (ret < 0) |
2177 | return ret; // dead end |
2178 | } |
da87ae38 |
2179 | return ret; |
4c45fa73 |
2180 | } |
2181 | |
87bf6cec |
2182 | if (po->bt_i < 0) { |
2183 | ferr(po, "dead branch\n"); |
2184 | return -1; |
2185 | } |
850c9265 |
2186 | |
5c024ef7 |
2187 | if (po->flags & OPF_CJMP) { |
d4e3b5db |
2188 | ret |= scan_for_pop(po->bt_i, opcnt, reg, magic, |
2189 | depth, maxdepth, do_flags); |
87bf6cec |
2190 | if (ret < 0) |
2191 | return ret; // dead end |
2192 | } |
2193 | else { |
2194 | i = po->bt_i - 1; |
2195 | } |
2196 | continue; |
2197 | } |
2198 | |
d4e3b5db |
2199 | if ((po->op == OP_POP || po->op == OP_PUSH) |
2200 | && po->operand[0].type == OPT_REG |
87bf6cec |
2201 | && IS(po->operand[0].name, reg)) |
2202 | { |
9af2d373 |
2203 | if (po->op == OP_PUSH && !(po->flags & OPF_FARGNR)) { |
d4e3b5db |
2204 | depth++; |
2205 | if (depth > *maxdepth) |
2206 | *maxdepth = depth; |
2207 | if (do_flags) |
7ae48d73 |
2208 | op_set_clear_flag(po, OPF_RSAVE, OPF_RMD); |
d4e3b5db |
2209 | } |
da87ae38 |
2210 | else if (po->op == OP_POP) { |
2211 | if (depth == 0) { |
2212 | if (do_flags) |
2213 | op_set_clear_flag(po, OPF_RMD, OPF_RSAVE); |
2214 | return 1; |
2215 | } |
2216 | else { |
2217 | depth--; |
2218 | if (depth < 0) // should not happen |
2219 | ferr(po, "fail with depth\n"); |
2220 | if (do_flags) |
2221 | op_set_clear_flag(po, OPF_RSAVE, OPF_RMD); |
2222 | } |
d4e3b5db |
2223 | } |
87bf6cec |
2224 | } |
850c9265 |
2225 | } |
2226 | |
87bf6cec |
2227 | return ret; |
850c9265 |
2228 | } |
2229 | |
69a3cdfc |
2230 | // scan for pop starting from 'ret' op (all paths) |
d4e3b5db |
2231 | static int scan_for_pop_ret(int i, int opcnt, const char *reg, |
2232 | int flag_set) |
850c9265 |
2233 | { |
2234 | int found = 0; |
2235 | int j; |
2236 | |
2237 | for (; i < opcnt; i++) { |
87bf6cec |
2238 | if (!(ops[i].flags & OPF_TAIL)) |
850c9265 |
2239 | continue; |
2240 | |
2241 | for (j = i - 1; j >= 0; j--) { |
5e49b270 |
2242 | if (ops[j].flags & (OPF_RMD|OPF_DONE)) |
69a3cdfc |
2243 | continue; |
2244 | if (ops[j].flags & OPF_JMP) |
850c9265 |
2245 | return -1; |
2246 | |
5e49b270 |
2247 | if (ops[j].op == OP_POP && ops[j].datap == NULL |
2248 | && ops[j].operand[0].type == OPT_REG |
850c9265 |
2249 | && IS(ops[j].operand[0].name, reg)) |
2250 | { |
2251 | found = 1; |
d4e3b5db |
2252 | ops[j].flags |= flag_set; |
850c9265 |
2253 | break; |
2254 | } |
2255 | |
d7857c3a |
2256 | if (g_labels[j] != NULL) |
850c9265 |
2257 | return -1; |
2258 | } |
2259 | } |
2260 | |
2261 | return found ? 0 : -1; |
2262 | } |
2263 | |
9af2d373 |
2264 | static void scan_for_pop_const(int i, int opcnt) |
2265 | { |
2266 | int j; |
2267 | |
2268 | for (j = i + 1; j < opcnt; j++) { |
2269 | if ((ops[j].flags & (OPF_JMP|OPF_TAIL|OPF_RSAVE)) |
2270 | || ops[j].op == OP_PUSH || g_labels[i] != NULL) |
2271 | { |
2272 | break; |
2273 | } |
2274 | |
5e49b270 |
2275 | if (ops[j].op == OP_POP && !(ops[j].flags & (OPF_RMD|OPF_DONE))) |
9af2d373 |
2276 | { |
5e49b270 |
2277 | ops[i].flags |= OPF_RMD | OPF_DONE; |
2278 | ops[j].flags |= OPF_DONE; |
9af2d373 |
2279 | ops[j].datap = &ops[i]; |
2280 | break; |
2281 | } |
2282 | } |
2283 | } |
2284 | |
591721d7 |
2285 | static void scan_propagate_df(int i, int opcnt) |
2286 | { |
2287 | struct parsed_op *po = &ops[i]; |
2288 | int j; |
2289 | |
2290 | for (; i < opcnt; i++) { |
2291 | po = &ops[i]; |
2292 | if (po->flags & OPF_DF) |
2293 | return; // already resolved |
2294 | po->flags |= OPF_DF; |
2295 | |
2296 | if (po->op == OP_CALL) |
2297 | ferr(po, "call with DF set?\n"); |
2298 | |
2299 | if (po->flags & OPF_JMP) { |
2300 | if (po->btj != NULL) { |
2301 | // jumptable |
2302 | for (j = 0; j < po->btj->count; j++) |
2303 | scan_propagate_df(po->btj->d[j].bt_i, opcnt); |
2304 | return; |
2305 | } |
2306 | |
2307 | if (po->bt_i < 0) { |
2308 | ferr(po, "dead branch\n"); |
2309 | return; |
2310 | } |
2311 | |
5c024ef7 |
2312 | if (po->flags & OPF_CJMP) |
591721d7 |
2313 | scan_propagate_df(po->bt_i, opcnt); |
2314 | else |
2315 | i = po->bt_i - 1; |
2316 | continue; |
2317 | } |
2318 | |
2319 | if (po->flags & OPF_TAIL) |
2320 | break; |
2321 | |
2322 | if (po->op == OP_CLD) { |
5e49b270 |
2323 | po->flags |= OPF_RMD | OPF_DONE; |
591721d7 |
2324 | return; |
2325 | } |
2326 | } |
2327 | |
2328 | ferr(po, "missing DF clear?\n"); |
2329 | } |
2330 | |
1cd4a663 |
2331 | // is operand 'opr' modified by parsed_op 'po'? |
5101a5f9 |
2332 | static int is_opr_modified(const struct parsed_opr *opr, |
69a3cdfc |
2333 | const struct parsed_op *po) |
2334 | { |
89ff3147 |
2335 | int mask; |
2336 | |
69a3cdfc |
2337 | if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA)) |
2338 | return 0; |
2339 | |
89ff3147 |
2340 | if (opr->type == OPT_REG) { |
2341 | if (po->op == OP_CALL) { |
2342 | mask = (1 << xAX) | (1 << xCX) | (1 << xDX); |
2343 | if ((1 << opr->reg) & mask) |
2344 | return 1; |
2345 | else |
2346 | return 0; |
2347 | } |
2348 | |
2349 | if (po->operand[0].type == OPT_REG) { |
2350 | if (po->regmask_dst & (1 << opr->reg)) |
2351 | return 1; |
2352 | else |
2353 | return 0; |
2354 | } |
69a3cdfc |
2355 | } |
2356 | |
2357 | return IS(po->operand[0].name, opr->name); |
2358 | } |
2359 | |
5101a5f9 |
2360 | // is any operand of parsed_op 'po_test' modified by parsed_op 'po'? |
2361 | static int is_any_opr_modified(const struct parsed_op *po_test, |
89ff3147 |
2362 | const struct parsed_op *po, int c_mode) |
5101a5f9 |
2363 | { |
89ff3147 |
2364 | int mask; |
5101a5f9 |
2365 | int i; |
2366 | |
2367 | if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA)) |
2368 | return 0; |
2369 | |
de50b98b |
2370 | if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST) |
2371 | return 0; |
2372 | |
2373 | if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst) |
2374 | return 1; |
2375 | |
2376 | // in reality, it can wreck any register, but in decompiled C |
2b43685d |
2377 | // version it can only overwrite eax or edx:eax |
89ff3147 |
2378 | mask = (1 << xAX) | (1 << xDX); |
2379 | if (!c_mode) |
2380 | mask |= 1 << xCX; |
2381 | |
de50b98b |
2382 | if (po->op == OP_CALL |
89ff3147 |
2383 | && ((po_test->regmask_src | po_test->regmask_dst) & mask)) |
5101a5f9 |
2384 | return 1; |
2385 | |
2386 | for (i = 0; i < po_test->operand_cnt; i++) |
2387 | if (IS(po_test->operand[i].name, po->operand[0].name)) |
2388 | return 1; |
2389 | |
2390 | return 0; |
2391 | } |
2392 | |
940e8e66 |
2393 | // scan for any po_test operand modification in range given |
89ff3147 |
2394 | static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt, |
2395 | int c_mode) |
69a3cdfc |
2396 | { |
2b43685d |
2397 | if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST) |
2398 | return -1; |
2399 | |
69a3cdfc |
2400 | for (; i < opcnt; i++) { |
89ff3147 |
2401 | if (is_any_opr_modified(po_test, &ops[i], c_mode)) |
69a3cdfc |
2402 | return i; |
2403 | } |
2404 | |
2405 | return -1; |
2406 | } |
2407 | |
940e8e66 |
2408 | // scan for po_test operand[0] modification in range given |
2409 | static int scan_for_mod_opr0(struct parsed_op *po_test, |
2410 | int i, int opcnt) |
2411 | { |
2412 | for (; i < opcnt; i++) { |
2413 | if (is_opr_modified(&po_test->operand[0], &ops[i])) |
2414 | return i; |
2415 | } |
2416 | |
2417 | return -1; |
2418 | } |
2419 | |
92d715b6 |
2420 | #define check_i(po, i) \ |
2421 | if ((i) < 0) \ |
2422 | ferr(po, "bad " #i ": %d\n", i) |
2423 | |
04f8a628 |
2424 | static int scan_for_flag_set(int i, int magic, int *branched, |
2425 | int *setters, int *setter_cnt) |
69a3cdfc |
2426 | { |
04f8a628 |
2427 | struct label_ref *lr; |
2b43685d |
2428 | int ret; |
de50b98b |
2429 | |
2430 | while (i >= 0) { |
04f8a628 |
2431 | if (ops[i].cc_scratch == magic) { |
2432 | ferr(&ops[i], "%s looped\n", __func__); |
2433 | return -1; |
2434 | } |
2435 | ops[i].cc_scratch = magic; |
2436 | |
d7857c3a |
2437 | if (g_labels[i] != NULL) { |
2b43685d |
2438 | *branched = 1; |
04f8a628 |
2439 | |
2440 | lr = &g_label_refs[i]; |
2441 | for (; lr->next; lr = lr->next) { |
92d715b6 |
2442 | check_i(&ops[i], lr->i); |
04f8a628 |
2443 | ret = scan_for_flag_set(lr->i, magic, |
2444 | branched, setters, setter_cnt); |
2445 | if (ret < 0) |
2446 | return ret; |
2447 | } |
2448 | |
92d715b6 |
2449 | check_i(&ops[i], lr->i); |
de50b98b |
2450 | if (i > 0 && LAST_OP(i - 1)) { |
94d447fb |
2451 | i = lr->i; |
de50b98b |
2452 | continue; |
2453 | } |
04f8a628 |
2454 | ret = scan_for_flag_set(lr->i, magic, |
2455 | branched, setters, setter_cnt); |
2b43685d |
2456 | if (ret < 0) |
2457 | return ret; |
de50b98b |
2458 | } |
2459 | i--; |
2460 | |
2b43685d |
2461 | if (ops[i].flags & OPF_FLAGS) { |
2462 | setters[*setter_cnt] = i; |
2463 | (*setter_cnt)++; |
2464 | return 0; |
2465 | } |
69a3cdfc |
2466 | |
5c024ef7 |
2467 | if ((ops[i].flags & (OPF_JMP|OPF_CJMP)) == OPF_JMP) |
69a3cdfc |
2468 | return -1; |
69a3cdfc |
2469 | } |
2470 | |
2471 | return -1; |
2472 | } |
2473 | |
5101a5f9 |
2474 | // scan back for cdq, if anything modifies edx, fail |
2475 | static int scan_for_cdq_edx(int i) |
2476 | { |
cdfaeed7 |
2477 | while (i >= 0) { |
d7857c3a |
2478 | if (g_labels[i] != NULL) { |
cdfaeed7 |
2479 | if (g_label_refs[i].next != NULL) |
2480 | return -1; |
2481 | if (i > 0 && LAST_OP(i - 1)) { |
2482 | i = g_label_refs[i].i; |
2483 | continue; |
2484 | } |
2485 | return -1; |
2486 | } |
2487 | i--; |
2488 | |
5101a5f9 |
2489 | if (ops[i].op == OP_CDQ) |
2490 | return i; |
2491 | |
2492 | if (ops[i].regmask_dst & (1 << xDX)) |
2493 | return -1; |
5101a5f9 |
2494 | } |
2495 | |
2496 | return -1; |
2497 | } |
2498 | |
64c59faf |
2499 | static int scan_for_reg_clear(int i, int reg) |
2500 | { |
cdfaeed7 |
2501 | while (i >= 0) { |
d7857c3a |
2502 | if (g_labels[i] != NULL) { |
cdfaeed7 |
2503 | if (g_label_refs[i].next != NULL) |
2504 | return -1; |
2505 | if (i > 0 && LAST_OP(i - 1)) { |
2506 | i = g_label_refs[i].i; |
2507 | continue; |
2508 | } |
2509 | return -1; |
2510 | } |
2511 | i--; |
2512 | |
64c59faf |
2513 | if (ops[i].op == OP_XOR |
2514 | && ops[i].operand[0].lmod == OPLM_DWORD |
2515 | && ops[i].operand[0].reg == ops[i].operand[1].reg |
2516 | && ops[i].operand[0].reg == reg) |
2517 | return i; |
2518 | |
2519 | if (ops[i].regmask_dst & (1 << reg)) |
2520 | return -1; |
64c59faf |
2521 | } |
2522 | |
2523 | return -1; |
2524 | } |
2525 | |
ee2361b9 |
2526 | static void patch_esp_adjust(struct parsed_op *po, int adj) |
2527 | { |
2528 | ferr_assert(po, po->op == OP_ADD); |
2529 | ferr_assert(po, IS(opr_name(po, 0), "esp")); |
2530 | ferr_assert(po, po->operand[1].type == OPT_CONST); |
2531 | |
2532 | // this is a bit of a hack, but deals with use of |
2533 | // single adj for multiple calls |
2534 | po->operand[1].val -= adj; |
2535 | po->flags |= OPF_RMD; |
2536 | if (po->operand[1].val == 0) |
2537 | po->flags |= OPF_DONE; |
2538 | ferr_assert(po, (int)po->operand[1].val >= 0); |
2539 | } |
2540 | |
1bafb621 |
2541 | // scan for positive, constant esp adjust |
ee2361b9 |
2542 | // multipath case is preliminary |
9af2d373 |
2543 | static int scan_for_esp_adjust(int i, int opcnt, |
ee2361b9 |
2544 | int adj_expect, int *adj, int *is_multipath, int do_update) |
1bafb621 |
2545 | { |
7ba45c34 |
2546 | struct parsed_op *po; |
46411e6c |
2547 | int first_pop = -1; |
4741fdfe |
2548 | |
ee2361b9 |
2549 | *adj = *is_multipath = 0; |
7ba45c34 |
2550 | |
9af2d373 |
2551 | for (; i < opcnt && *adj < adj_expect; i++) { |
d7857c3a |
2552 | if (g_labels[i] != NULL) |
ee2361b9 |
2553 | *is_multipath = 1; |
a2c1d768 |
2554 | |
5e49b270 |
2555 | po = &ops[i]; |
2556 | if (po->flags & OPF_DONE) |
2557 | continue; |
2558 | |
7ba45c34 |
2559 | if (po->op == OP_ADD && po->operand[0].reg == xSP) { |
2560 | if (po->operand[1].type != OPT_CONST) |
1bafb621 |
2561 | ferr(&ops[i], "non-const esp adjust?\n"); |
7ba45c34 |
2562 | *adj += po->operand[1].val; |
1bafb621 |
2563 | if (*adj & 3) |
2564 | ferr(&ops[i], "unaligned esp adjust: %x\n", *adj); |
ee2361b9 |
2565 | if (do_update) { |
2566 | if (!*is_multipath) |
2567 | patch_esp_adjust(po, adj_expect); |
2568 | else |
2569 | po->flags |= OPF_RMD; |
2570 | } |
1bafb621 |
2571 | return i; |
2572 | } |
5e49b270 |
2573 | else if (po->op == OP_PUSH) { |
5c024ef7 |
2574 | //if (first_pop == -1) |
2575 | // first_pop = -2; // none |
7ba45c34 |
2576 | *adj -= lmod_bytes(po, po->operand[0].lmod); |
46411e6c |
2577 | } |
5e49b270 |
2578 | else if (po->op == OP_POP) { |
91ca764a |
2579 | if (!(po->flags & OPF_DONE)) { |
2580 | // seems like msvc only uses 'pop ecx' for stack realignment.. |
2581 | if (po->operand[0].type != OPT_REG || po->operand[0].reg != xCX) |
2582 | break; |
2583 | if (first_pop == -1 && *adj >= 0) |
2584 | first_pop = i; |
2585 | } |
ee2361b9 |
2586 | if (do_update && *adj >= 0) { |
2587 | po->flags |= OPF_RMD; |
2588 | if (!*is_multipath) |
2589 | po->flags |= OPF_DONE; |
2590 | } |
2591 | |
7ba45c34 |
2592 | *adj += lmod_bytes(po, po->operand[0].lmod); |
46411e6c |
2593 | } |
e56ab892 |
2594 | else if (po->flags & (OPF_JMP|OPF_TAIL)) { |
4741fdfe |
2595 | if (po->op == OP_JMP && po->btj == NULL) { |
5e49b270 |
2596 | if (po->bt_i <= i) |
2597 | break; |
4741fdfe |
2598 | i = po->bt_i - 1; |
2599 | continue; |
2600 | } |
e56ab892 |
2601 | if (po->op != OP_CALL) |
a2c1d768 |
2602 | break; |
e56ab892 |
2603 | if (po->operand[0].type != OPT_LABEL) |
a2c1d768 |
2604 | break; |
91ca764a |
2605 | if (po->pp != NULL && po->pp->is_stdcall) |
89ff3147 |
2606 | break; |
91ca764a |
2607 | // assume it's another cdecl call |
e56ab892 |
2608 | } |
a2c1d768 |
2609 | } |
7ba45c34 |
2610 | |
108e9fe3 |
2611 | if (first_pop >= 0) { |
a2c1d768 |
2612 | // probably 'pop ecx' was used.. |
46411e6c |
2613 | return first_pop; |
1bafb621 |
2614 | } |
2615 | |
2616 | return -1; |
2617 | } |
2618 | |
a3684be1 |
2619 | static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags) |
2620 | { |
2621 | struct parsed_op *po; |
2622 | int j; |
2623 | |
2624 | if (i < 0) |
2625 | ferr(ops, "%s: followed bad branch?\n", __func__); |
2626 | |
2627 | for (; i < opcnt; i++) { |
2628 | po = &ops[i]; |
2629 | if (po->cc_scratch == magic) |
2630 | return; |
2631 | po->cc_scratch = magic; |
2632 | po->flags |= flags; |
2633 | |
2634 | if ((po->flags & OPF_JMP) && po->op != OP_CALL) { |
2635 | if (po->btj != NULL) { |
2636 | // jumptable |
2637 | for (j = 0; j < po->btj->count; j++) |
2638 | scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags); |
2639 | return; |
2640 | } |
2641 | |
2642 | scan_fwd_set_flags(po->bt_i, opcnt, magic, flags); |
5c024ef7 |
2643 | if (!(po->flags & OPF_CJMP)) |
a3684be1 |
2644 | return; |
2645 | } |
2646 | if (po->flags & OPF_TAIL) |
2647 | return; |
2648 | } |
2649 | } |
2650 | |
1cd4a663 |
2651 | static const struct parsed_proto *try_recover_pp( |
da87ae38 |
2652 | struct parsed_op *po, const struct parsed_opr *opr, int *search_instead) |
1cd4a663 |
2653 | { |
2654 | const struct parsed_proto *pp = NULL; |
89ff3147 |
2655 | char buf[256]; |
2656 | char *p; |
1cd4a663 |
2657 | |
2658 | // maybe an arg of g_func? |
2659 | if (opr->type == OPT_REGMEM && is_stack_access(po, opr)) |
2660 | { |
2661 | char ofs_reg[16] = { 0, }; |
2662 | int arg, arg_s, arg_i; |
2663 | int stack_ra = 0; |
2664 | int offset = 0; |
2665 | |
9af2d373 |
2666 | if (g_header_mode) |
2667 | return NULL; |
2668 | |
1cd4a663 |
2669 | parse_stack_access(po, opr->name, ofs_reg, |
037f4971 |
2670 | &offset, &stack_ra, NULL, 0); |
1cd4a663 |
2671 | if (ofs_reg[0] != 0) |
2672 | ferr(po, "offset reg on arg access?\n"); |
da87ae38 |
2673 | if (offset <= stack_ra) { |
2674 | // search who set the stack var instead |
2675 | if (search_instead != NULL) |
2676 | *search_instead = 1; |
2677 | return NULL; |
2678 | } |
1cd4a663 |
2679 | |
2680 | arg_i = (offset - stack_ra - 4) / 4; |
2681 | for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) { |
2682 | if (g_func_pp->arg[arg].reg != NULL) |
2683 | continue; |
2684 | if (arg_s == arg_i) |
2685 | break; |
2686 | arg_s++; |
2687 | } |
2688 | if (arg == g_func_pp->argc) |
2689 | ferr(po, "stack arg %d not in prototype?\n", arg_i); |
2690 | |
2691 | pp = g_func_pp->arg[arg].fptr; |
2692 | if (pp == NULL) |
46411e6c |
2693 | ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1); |
89ff3147 |
2694 | check_func_pp(po, pp, "icall arg"); |
2695 | } |
2696 | else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) { |
2697 | // label[index] |
2698 | p = strchr(opr->name + 1, '['); |
2699 | memcpy(buf, opr->name, p - opr->name); |
2700 | buf[p - opr->name] = 0; |
92d715b6 |
2701 | pp = proto_parse(g_fhdr, buf, g_quiet_pp); |
1cd4a663 |
2702 | } |
2703 | else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) { |
9af2d373 |
2704 | pp = proto_parse(g_fhdr, opr->name, g_quiet_pp); |
2705 | if (pp == NULL) { |
2706 | if (!g_header_mode) |
2707 | ferr(po, "proto_parse failed for icall to '%s'\n", opr->name); |
2708 | } |
2709 | else |
2710 | check_func_pp(po, pp, "reg-fptr ref"); |
1cd4a663 |
2711 | } |
2712 | |
2713 | return pp; |
2714 | } |
2715 | |
da87ae38 |
2716 | static void scan_for_call_type(int i, const struct parsed_opr *opr, |
89ff3147 |
2717 | int magic, const struct parsed_proto **pp_found, int *multi) |
1cd4a663 |
2718 | { |
2719 | const struct parsed_proto *pp = NULL; |
2720 | struct parsed_op *po; |
2721 | struct label_ref *lr; |
2722 | |
037f4971 |
2723 | ops[i].cc_scratch = magic; |
1cd4a663 |
2724 | |
037f4971 |
2725 | while (1) { |
d7857c3a |
2726 | if (g_labels[i] != NULL) { |
1cd4a663 |
2727 | lr = &g_label_refs[i]; |
92d715b6 |
2728 | for (; lr != NULL; lr = lr->next) { |
2729 | check_i(&ops[i], lr->i); |
89ff3147 |
2730 | scan_for_call_type(lr->i, opr, magic, pp_found, multi); |
92d715b6 |
2731 | } |
46411e6c |
2732 | if (i > 0 && LAST_OP(i - 1)) |
2733 | return; |
1cd4a663 |
2734 | } |
037f4971 |
2735 | |
1cd4a663 |
2736 | i--; |
037f4971 |
2737 | if (i < 0) |
2738 | break; |
2739 | |
2740 | if (ops[i].cc_scratch == magic) |
2741 | return; |
2742 | ops[i].cc_scratch = magic; |
1cd4a663 |
2743 | |
2744 | if (!(ops[i].flags & OPF_DATA)) |
2745 | continue; |
2746 | if (!is_opr_modified(opr, &ops[i])) |
2747 | continue; |
2748 | if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) { |
2749 | // most probably trashed by some processing |
2750 | *pp_found = NULL; |
2751 | return; |
2752 | } |
2753 | |
2754 | opr = &ops[i].operand[1]; |
2755 | if (opr->type != OPT_REG) |
2756 | break; |
2757 | } |
2758 | |
2759 | po = (i >= 0) ? &ops[i] : ops; |
2760 | |
2761 | if (i < 0) { |
2762 | // reached the top - can only be an arg-reg |
2763 | if (opr->type != OPT_REG) |
2764 | return; |
2765 | |
2766 | for (i = 0; i < g_func_pp->argc; i++) { |
2767 | if (g_func_pp->arg[i].reg == NULL) |
2768 | continue; |
2769 | if (IS(opr->name, g_func_pp->arg[i].reg)) |
2770 | break; |
2771 | } |
2772 | if (i == g_func_pp->argc) |
2773 | return; |
2774 | pp = g_func_pp->arg[i].fptr; |
2775 | if (pp == NULL) |
46411e6c |
2776 | ferr(po, "icall: arg%d (%s) is not a fptr?\n", |
2777 | i + 1, g_func_pp->arg[i].reg); |
89ff3147 |
2778 | check_func_pp(po, pp, "icall reg-arg"); |
1cd4a663 |
2779 | } |
2780 | else |
da87ae38 |
2781 | pp = try_recover_pp(po, opr, NULL); |
1cd4a663 |
2782 | |
89ff3147 |
2783 | if (*pp_found != NULL && pp != NULL && *pp_found != pp) { |
1cd4a663 |
2784 | if (!IS((*pp_found)->ret_type.name, pp->ret_type.name) |
2785 | || (*pp_found)->is_stdcall != pp->is_stdcall |
89ff3147 |
2786 | || (*pp_found)->is_fptr != pp->is_fptr |
1cd4a663 |
2787 | || (*pp_found)->argc != pp->argc |
2788 | || (*pp_found)->argc_reg != pp->argc_reg |
2789 | || (*pp_found)->argc_stack != pp->argc_stack) |
2790 | { |
2791 | ferr(po, "icall: parsed_proto mismatch\n"); |
2792 | } |
89ff3147 |
2793 | *multi = 1; |
1cd4a663 |
2794 | } |
2795 | if (pp != NULL) |
2796 | *pp_found = pp; |
2797 | } |
2798 | |
9af2d373 |
2799 | // early check for tail call or branch back |
2800 | static int is_like_tailjmp(int j) |
2801 | { |
2802 | if (!(ops[j].flags & OPF_JMP)) |
2803 | return 0; |
2804 | |
2805 | if (ops[j].op == OP_JMP && !ops[j].operand[0].had_ds) |
2806 | // probably local branch back.. |
2807 | return 1; |
2808 | if (ops[j].op == OP_CALL) |
2809 | // probably noreturn call.. |
2810 | return 1; |
2811 | |
2812 | return 0; |
2813 | } |
2814 | |
2815 | static void scan_prologue_epilogue(int opcnt) |
2816 | { |
2817 | int ecx_push = 0, esp_sub = 0; |
2818 | int found; |
2819 | int i, j, l; |
2820 | |
2821 | if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp") |
2822 | && ops[1].op == OP_MOV |
2823 | && IS(opr_name(&ops[1], 0), "ebp") |
2824 | && IS(opr_name(&ops[1], 1), "esp")) |
2825 | { |
2826 | g_bp_frame = 1; |
5e49b270 |
2827 | ops[0].flags |= OPF_RMD | OPF_DONE; |
2828 | ops[1].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
2829 | i = 2; |
2830 | |
2831 | if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) { |
2832 | g_stack_fsz = opr_const(&ops[2], 1); |
5e49b270 |
2833 | ops[2].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
2834 | i++; |
2835 | } |
2836 | else { |
2837 | // another way msvc builds stack frame.. |
2838 | i = 2; |
2839 | while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) { |
2840 | g_stack_fsz += 4; |
5e49b270 |
2841 | ops[i].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
2842 | ecx_push++; |
2843 | i++; |
2844 | } |
2845 | // and another way.. |
2846 | if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX |
2847 | && ops[i].operand[1].type == OPT_CONST |
2848 | && ops[i + 1].op == OP_CALL |
2849 | && IS(opr_name(&ops[i + 1], 0), "__alloca_probe")) |
2850 | { |
2851 | g_stack_fsz += ops[i].operand[1].val; |
5e49b270 |
2852 | ops[i].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
2853 | i++; |
5e49b270 |
2854 | ops[i].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
2855 | i++; |
2856 | } |
2857 | } |
2858 | |
2859 | found = 0; |
2860 | do { |
2861 | for (; i < opcnt; i++) |
2862 | if (ops[i].op == OP_RET) |
2863 | break; |
2864 | j = i - 1; |
2865 | if (i == opcnt && (ops[j].flags & OPF_JMP)) { |
2866 | if (found && is_like_tailjmp(j)) |
2867 | break; |
2868 | j--; |
2869 | } |
2870 | |
2871 | if ((ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp")) |
2872 | || ops[j].op == OP_LEAVE) |
2873 | { |
5e49b270 |
2874 | ops[j].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
2875 | } |
2876 | else if (!(g_ida_func_attr & IDAFA_NORETURN)) |
2877 | ferr(&ops[j], "'pop ebp' expected\n"); |
2878 | |
2879 | if (g_stack_fsz != 0) { |
2880 | if (ops[j - 1].op == OP_MOV |
2881 | && IS(opr_name(&ops[j - 1], 0), "esp") |
2882 | && IS(opr_name(&ops[j - 1], 1), "ebp")) |
2883 | { |
5e49b270 |
2884 | ops[j - 1].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
2885 | } |
2886 | else if (ops[j].op != OP_LEAVE |
2887 | && !(g_ida_func_attr & IDAFA_NORETURN)) |
2888 | { |
2889 | ferr(&ops[j - 1], "esp restore expected\n"); |
2890 | } |
2891 | |
2892 | if (ecx_push && ops[j - 2].op == OP_POP |
2893 | && IS(opr_name(&ops[j - 2], 0), "ecx")) |
2894 | { |
2895 | ferr(&ops[j - 2], "unexpected ecx pop\n"); |
2896 | } |
2897 | } |
2898 | |
2899 | found = 1; |
2900 | i++; |
2901 | } while (i < opcnt); |
2902 | |
2903 | return; |
2904 | } |
2905 | |
2906 | // non-bp frame |
2907 | i = 0; |
2908 | while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) { |
5e49b270 |
2909 | ops[i].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
2910 | g_stack_fsz += 4; |
2911 | ecx_push++; |
2912 | i++; |
2913 | } |
2914 | |
2915 | for (; i < opcnt; i++) { |
2916 | if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL))) |
2917 | break; |
2918 | if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP |
2919 | && ops[i].operand[1].type == OPT_CONST) |
2920 | { |
2921 | g_stack_fsz = ops[i].operand[1].val; |
5e49b270 |
2922 | ops[i].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
2923 | esp_sub = 1; |
2924 | break; |
2925 | } |
2926 | } |
2927 | |
2928 | if (ecx_push && !esp_sub) { |
2929 | // could actually be args for a call.. |
2930 | for (; i < opcnt; i++) |
2931 | if (ops[i].op != OP_PUSH) |
2932 | break; |
2933 | |
2934 | if (ops[i].op == OP_CALL && ops[i].operand[0].type == OPT_LABEL) { |
2935 | const struct parsed_proto *pp; |
2936 | pp = proto_parse(g_fhdr, opr_name(&ops[i], 0), 1); |
2937 | j = pp ? pp->argc_stack : 0; |
2938 | while (i > 0 && j > 0) { |
2939 | i--; |
2940 | if (ops[i].op == OP_PUSH) { |
5e49b270 |
2941 | ops[i].flags &= ~(OPF_RMD | OPF_DONE); |
9af2d373 |
2942 | j--; |
2943 | } |
2944 | } |
2945 | if (j != 0) |
2946 | ferr(&ops[i], "unhandled prologue\n"); |
2947 | |
2948 | // recheck |
2949 | i = g_stack_fsz = ecx_push = 0; |
2950 | while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) { |
2951 | if (!(ops[i].flags & OPF_RMD)) |
2952 | break; |
2953 | g_stack_fsz += 4; |
2954 | ecx_push++; |
2955 | i++; |
2956 | } |
2957 | } |
2958 | } |
2959 | |
2960 | found = 0; |
2961 | if (ecx_push || esp_sub) |
2962 | { |
2963 | g_sp_frame = 1; |
2964 | |
2965 | i++; |
2966 | do { |
2967 | for (; i < opcnt; i++) |
2968 | if (ops[i].op == OP_RET) |
2969 | break; |
2970 | j = i - 1; |
2971 | if (i == opcnt && (ops[j].flags & OPF_JMP)) { |
2972 | if (found && is_like_tailjmp(j)) |
2973 | break; |
2974 | j--; |
2975 | } |
2976 | |
2977 | if (ecx_push > 0) { |
2978 | for (l = 0; l < ecx_push; l++) { |
2979 | if (ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ecx")) |
2980 | /* pop ecx */; |
2981 | else if (ops[j].op == OP_ADD |
2982 | && IS(opr_name(&ops[j], 0), "esp") |
2983 | && ops[j].operand[1].type == OPT_CONST) |
2984 | { |
2985 | /* add esp, N */ |
5e49b270 |
2986 | l += ops[j].operand[1].val / 4 - 1; |
9af2d373 |
2987 | } |
2988 | else |
2989 | ferr(&ops[j], "'pop ecx' expected\n"); |
2990 | |
5e49b270 |
2991 | ops[j].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
2992 | j--; |
2993 | } |
2994 | if (l != ecx_push) |
2995 | ferr(&ops[j], "epilogue scan failed\n"); |
2996 | |
2997 | found = 1; |
2998 | } |
2999 | |
3000 | if (esp_sub) { |
3001 | if (ops[j].op != OP_ADD |
3002 | || !IS(opr_name(&ops[j], 0), "esp") |
3003 | || ops[j].operand[1].type != OPT_CONST |
3004 | || ops[j].operand[1].val != g_stack_fsz) |
3005 | ferr(&ops[j], "'add esp' expected\n"); |
3006 | |
5e49b270 |
3007 | ops[j].flags |= OPF_RMD | OPF_DONE; |
9af2d373 |
3008 | ops[j].operand[1].val = 0; // hack for stack arg scanner |
3009 | found = 1; |
3010 | } |
3011 | |
3012 | i++; |
3013 | } while (i < opcnt); |
3014 | } |
3015 | } |
3016 | |
89ff3147 |
3017 | static const struct parsed_proto *resolve_icall(int i, int opcnt, |
3018 | int *multi_src) |
1cd4a663 |
3019 | { |
3020 | const struct parsed_proto *pp = NULL; |
da87ae38 |
3021 | int search_advice = 0; |
1cd4a663 |
3022 | |
89ff3147 |
3023 | *multi_src = 0; |
3024 | |
1cd4a663 |
3025 | switch (ops[i].operand[0].type) { |
3026 | case OPT_REGMEM: |
3027 | case OPT_LABEL: |
3028 | case OPT_OFFSET: |
da87ae38 |
3029 | pp = try_recover_pp(&ops[i], &ops[i].operand[0], &search_advice); |
3030 | if (!search_advice) |
3031 | break; |
3032 | // fallthrough |
1cd4a663 |
3033 | default: |
89ff3147 |
3034 | scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp, |
3035 | multi_src); |
1cd4a663 |
3036 | break; |
3037 | } |
3038 | |
3039 | return pp; |
3040 | } |
3041 | |
23fd0b11 |
3042 | // find an instruction that changed opr before i op |
92d715b6 |
3043 | // *op_i must be set to -1 by caller |
3044 | // *entry is set to 1 if one source is determined to be the caller |
3045 | // returns 1 if found, *op_i is then set to origin |
23fd0b11 |
3046 | static int resolve_origin(int i, const struct parsed_opr *opr, |
92d715b6 |
3047 | int magic, int *op_i, int *is_caller) |
1f84f6b3 |
3048 | { |
3049 | struct label_ref *lr; |
3050 | int ret = 0; |
3051 | |
92d715b6 |
3052 | if (ops[i].cc_scratch == magic) |
3053 | return 0; |
1f84f6b3 |
3054 | ops[i].cc_scratch = magic; |
3055 | |
3056 | while (1) { |
d7857c3a |
3057 | if (g_labels[i] != NULL) { |
1f84f6b3 |
3058 | lr = &g_label_refs[i]; |
92d715b6 |
3059 | for (; lr != NULL; lr = lr->next) { |
3060 | check_i(&ops[i], lr->i); |
3061 | ret |= resolve_origin(lr->i, opr, magic, op_i, is_caller); |
3062 | } |
1f84f6b3 |
3063 | if (i > 0 && LAST_OP(i - 1)) |
3064 | return ret; |
3065 | } |
3066 | |
3067 | i--; |
92d715b6 |
3068 | if (i < 0) { |
3069 | if (is_caller != NULL) |
3070 | *is_caller = 1; |
1f84f6b3 |
3071 | return -1; |
92d715b6 |
3072 | } |
1f84f6b3 |
3073 | |
3074 | if (ops[i].cc_scratch == magic) |
3075 | return 0; |
3076 | ops[i].cc_scratch = magic; |
3077 | |
3078 | if (!(ops[i].flags & OPF_DATA)) |
3079 | continue; |
3080 | if (!is_opr_modified(opr, &ops[i])) |
3081 | continue; |
23fd0b11 |
3082 | |
3083 | if (*op_i >= 0) { |
3084 | if (*op_i == i) |
3085 | return 1; |
3086 | // XXX: could check if the other op does the same |
3087 | return -1; |
3088 | } |
3089 | |
3090 | *op_i = i; |
3091 | return 1; |
3092 | } |
3093 | } |
3094 | |
3095 | static int try_resolve_const(int i, const struct parsed_opr *opr, |
3096 | int magic, unsigned int *val) |
3097 | { |
3098 | int s_i = -1; |
92d715b6 |
3099 | int ret; |
23fd0b11 |
3100 | |
92d715b6 |
3101 | ret = resolve_origin(i, opr, magic, &s_i, NULL); |
23fd0b11 |
3102 | if (ret == 1) { |
3103 | i = s_i; |
1f84f6b3 |
3104 | if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST) |
3105 | return -1; |
3106 | |
3107 | *val = ops[i].operand[1].val; |
3108 | return 1; |
3109 | } |
23fd0b11 |
3110 | |
3111 | return -1; |
1f84f6b3 |
3112 | } |
3113 | |
26677139 |
3114 | static struct parsed_proto *process_call_early(int i, int opcnt, |
3115 | int *adj_i) |
3116 | { |
3117 | struct parsed_op *po = &ops[i]; |
3118 | struct parsed_proto *pp; |
3119 | int multipath = 0; |
3120 | int adj = 0; |
3121 | int ret; |
3122 | |
3123 | pp = po->pp; |
3124 | if (pp == NULL || pp->is_vararg || pp->argc_reg != 0) |
3125 | // leave for later |
3126 | return NULL; |
3127 | |
3128 | // look for and make use of esp adjust |
3129 | *adj_i = ret = -1; |
3130 | if (!pp->is_stdcall && pp->argc_stack > 0) |
3131 | ret = scan_for_esp_adjust(i + 1, opcnt, |
ee2361b9 |
3132 | pp->argc_stack * 4, &adj, &multipath, 0); |
26677139 |
3133 | if (ret >= 0) { |
3134 | if (pp->argc_stack > adj / 4) |
3135 | return NULL; |
3136 | if (multipath) |
3137 | return NULL; |
3138 | if (ops[ret].op == OP_POP && adj != 4) |
3139 | return NULL; |
3140 | } |
3141 | |
3142 | *adj_i = ret; |
3143 | return pp; |
3144 | } |
3145 | |
9af2d373 |
3146 | static struct parsed_proto *process_call(int i, int opcnt) |
3147 | { |
3148 | struct parsed_op *po = &ops[i]; |
3149 | const struct parsed_proto *pp_c; |
3150 | struct parsed_proto *pp; |
3151 | const char *tmpname; |
26677139 |
3152 | int adj = 0, multipath = 0; |
9af2d373 |
3153 | int ret, arg; |
3154 | |
3155 | tmpname = opr_name(po, 0); |
3156 | pp = po->pp; |
3157 | if (pp == NULL) |
3158 | { |
3159 | // indirect call |
26677139 |
3160 | pp_c = resolve_icall(i, opcnt, &multipath); |
9af2d373 |
3161 | if (pp_c != NULL) { |
3162 | if (!pp_c->is_func && !pp_c->is_fptr) |
3163 | ferr(po, "call to non-func: %s\n", pp_c->name); |
3164 | pp = proto_clone(pp_c); |
3165 | my_assert_not(pp, NULL); |
26677139 |
3166 | if (multipath) |
9af2d373 |
3167 | // not resolved just to single func |
3168 | pp->is_fptr = 1; |
3169 | |
3170 | switch (po->operand[0].type) { |
3171 | case OPT_REG: |
3172 | // we resolved this call and no longer need the register |
3173 | po->regmask_src &= ~(1 << po->operand[0].reg); |
3174 | break; |
3175 | case OPT_REGMEM: |
3176 | pp->is_fptr = 1; |
3177 | break; |
3178 | default: |
3179 | break; |
3180 | } |
3181 | } |
3182 | if (pp == NULL) { |
3183 | pp = calloc(1, sizeof(*pp)); |
3184 | my_assert_not(pp, NULL); |
3185 | |
3186 | pp->is_fptr = 1; |
ee2361b9 |
3187 | ret = scan_for_esp_adjust(i + 1, opcnt, |
3188 | 32*4, &adj, &multipath, 0); |
26677139 |
3189 | if (ret < 0 || adj < 0) { |
9af2d373 |
3190 | if (!g_allow_regfunc) |
3191 | ferr(po, "non-__cdecl indirect call unhandled yet\n"); |
3192 | pp->is_unresolved = 1; |
26677139 |
3193 | adj = 0; |
9af2d373 |
3194 | } |
26677139 |
3195 | adj /= 4; |
3196 | if (adj > ARRAY_SIZE(pp->arg)) |
3197 | ferr(po, "esp adjust too large: %d\n", adj); |
9af2d373 |
3198 | pp->ret_type.name = strdup("int"); |
26677139 |
3199 | pp->argc = pp->argc_stack = adj; |
9af2d373 |
3200 | for (arg = 0; arg < pp->argc; arg++) |
3201 | pp->arg[arg].type.name = strdup("int"); |
3202 | } |
3203 | po->pp = pp; |
3204 | } |
3205 | |
3206 | // look for and make use of esp adjust |
91ca764a |
3207 | multipath = 0; |
9af2d373 |
3208 | ret = -1; |
3209 | if (!pp->is_stdcall && pp->argc_stack > 0) |
3210 | ret = scan_for_esp_adjust(i + 1, opcnt, |
ee2361b9 |
3211 | pp->argc_stack * 4, &adj, &multipath, 0); |
9af2d373 |
3212 | if (ret >= 0) { |
3213 | if (pp->is_vararg) { |
26677139 |
3214 | if (adj / 4 < pp->argc_stack) { |
3215 | fnote(po, "(this call)\n"); |
3216 | ferr(&ops[ret], "esp adjust is too small: %x < %x\n", |
3217 | adj, pp->argc_stack * 4); |
3218 | } |
9af2d373 |
3219 | // modify pp to make it have varargs as normal args |
3220 | arg = pp->argc; |
26677139 |
3221 | pp->argc += adj / 4 - pp->argc_stack; |
9af2d373 |
3222 | for (; arg < pp->argc; arg++) { |
3223 | pp->arg[arg].type.name = strdup("int"); |
3224 | pp->argc_stack++; |
3225 | } |
3226 | if (pp->argc > ARRAY_SIZE(pp->arg)) |
3227 | ferr(po, "too many args for '%s'\n", tmpname); |
3228 | } |
26677139 |
3229 | if (pp->argc_stack > adj / 4) { |
9af2d373 |
3230 | fnote(po, "(this call)\n"); |
3231 | ferr(&ops[ret], "stack tracking failed for '%s': %x %x\n", |
26677139 |
3232 | tmpname, pp->argc_stack * 4, adj); |
9af2d373 |
3233 | } |
3234 | |
ee2361b9 |
3235 | scan_for_esp_adjust(i + 1, opcnt, |
3236 | pp->argc_stack * 4, &adj, &multipath, 1); |
9af2d373 |
3237 | } |
3238 | else if (pp->is_vararg) |
3239 | ferr(po, "missing esp_adjust for vararg func '%s'\n", |
3240 | pp->name); |
3241 | |
3242 | return pp; |
3243 | } |
3244 | |
26677139 |
3245 | static int collect_call_args_early(struct parsed_op *po, int i, |
3246 | struct parsed_proto *pp, int *regmask) |
3247 | { |
3248 | int arg, ret; |
3249 | int j; |
3250 | |
3251 | for (arg = 0; arg < pp->argc; arg++) |
3252 | if (pp->arg[arg].reg == NULL) |
3253 | break; |
3254 | |
3255 | // first see if it can be easily done |
3256 | for (j = i; j > 0 && arg < pp->argc; ) |
3257 | { |
3258 | if (g_labels[j] != NULL) |
3259 | return -1; |
3260 | j--; |
3261 | |
3262 | if (ops[j].op == OP_CALL) |
3263 | return -1; |
3264 | else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP) |
3265 | return -1; |
3266 | else if (ops[j].op == OP_POP) |
3267 | return -1; |
3268 | else if (ops[j].flags & OPF_CJMP) |
3269 | return -1; |
3270 | else if (ops[j].op == OP_PUSH) { |
3271 | if (ops[j].flags & (OPF_FARG|OPF_FARGNR)) |
3272 | return -1; |
3273 | ret = scan_for_mod(&ops[j], j + 1, i, 1); |
3274 | if (ret >= 0) |
3275 | return -1; |
3276 | |
3277 | if (pp->arg[arg].type.is_va_list) |
3278 | return -1; |
3279 | |
3280 | // next arg |
3281 | for (arg++; arg < pp->argc; arg++) |
3282 | if (pp->arg[arg].reg == NULL) |
3283 | break; |
3284 | } |
3285 | } |
3286 | |
3287 | if (arg < pp->argc) |
3288 | return -1; |
3289 | |
3290 | // now do it |
3291 | for (arg = 0; arg < pp->argc; arg++) |
3292 | if (pp->arg[arg].reg == NULL) |
3293 | break; |
3294 | |
3295 | for (j = i; j > 0 && arg < pp->argc; ) |
3296 | { |
3297 | j--; |
3298 | |
3299 | if (ops[j].op == OP_PUSH) |
3300 | { |
3301 | ops[j].p_argnext = -1; |
3302 | ferr_assert(&ops[j], pp->arg[arg].datap == NULL); |
3303 | pp->arg[arg].datap = &ops[j]; |
3304 | |
3305 | if (ops[j].operand[0].type == OPT_REG) |
3306 | *regmask |= 1 << ops[j].operand[0].reg; |
3307 | |
5e49b270 |
3308 | ops[j].flags |= OPF_RMD | OPF_DONE | OPF_FARGNR | OPF_FARG; |
26677139 |
3309 | ops[j].flags &= ~OPF_RSAVE; |
3310 | |
3311 | // next arg |
3312 | for (arg++; arg < pp->argc; arg++) |
3313 | if (pp->arg[arg].reg == NULL) |
3314 | break; |
3315 | } |
3316 | } |
3317 | |
3318 | return 0; |
3319 | } |
3320 | |
89ff3147 |
3321 | static int collect_call_args_r(struct parsed_op *po, int i, |
3a5101d7 |
3322 | struct parsed_proto *pp, int *regmask, int *save_arg_vars, |
3323 | int *arg_grp, int arg, int magic, int need_op_saving, int may_reuse) |
e56ab892 |
3324 | { |
3325 | struct parsed_proto *pp_tmp; |
3a5101d7 |
3326 | struct parsed_op *po_tmp; |
e56ab892 |
3327 | struct label_ref *lr; |
2b43685d |
3328 | int need_to_save_current; |
3a5101d7 |
3329 | int arg_grp_current = 0; |
3330 | int save_args_seen = 0; |
23fd0b11 |
3331 | int save_args; |
e56ab892 |
3332 | int ret = 0; |
5f70a34f |
3333 | int reg; |
23fd0b11 |
3334 | char buf[32]; |
3335 | int j, k; |
e56ab892 |
3336 | |
a3684be1 |
3337 | if (i < 0) { |
a2c1d768 |
3338 | ferr(po, "dead label encountered\n"); |
a3684be1 |
3339 | return -1; |
3340 | } |
e56ab892 |
3341 | |
de50b98b |
3342 | for (; arg < pp->argc; arg++) |
e56ab892 |
3343 | if (pp->arg[arg].reg == NULL) |
3344 | break; |
a3684be1 |
3345 | magic = (magic & 0xffffff) | (arg << 24); |
e56ab892 |
3346 | |
89ff3147 |
3347 | for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); ) |
e56ab892 |
3348 | { |
a3684be1 |
3349 | if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) { |
3350 | if (ops[j].cc_scratch != magic) { |
3351 | ferr(&ops[j], "arg collect hit same path with diff args for %s\n", |
3352 | pp->name); |
3353 | return -1; |
3354 | } |
3355 | // ok: have already been here |
3356 | return 0; |
3357 | } |
3358 | ops[j].cc_scratch = magic; |
3359 | |
d7857c3a |
3360 | if (g_labels[j] != NULL && g_label_refs[j].i != -1) { |
e56ab892 |
3361 | lr = &g_label_refs[j]; |
3362 | if (lr->next != NULL) |
3363 | need_op_saving = 1; |
a652aa9f |
3364 | for (; lr->next; lr = lr->next) { |
92d715b6 |
3365 | check_i(&ops[j], lr->i); |
5c024ef7 |
3366 | if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP) |
a652aa9f |
3367 | may_reuse = 1; |
89ff3147 |
3368 | ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars, |
3a5101d7 |
3369 | arg_grp, arg, magic, need_op_saving, may_reuse); |
a3684be1 |
3370 | if (ret < 0) |
3371 | return ret; |
a652aa9f |
3372 | } |
e56ab892 |
3373 | |
92d715b6 |
3374 | check_i(&ops[j], lr->i); |
5c024ef7 |
3375 | if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP) |
a652aa9f |
3376 | may_reuse = 1; |
de50b98b |
3377 | if (j > 0 && LAST_OP(j - 1)) { |
e56ab892 |
3378 | // follow last branch in reverse |
3379 | j = lr->i; |
3380 | continue; |
3381 | } |
3382 | need_op_saving = 1; |
89ff3147 |
3383 | ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars, |
3a5101d7 |
3384 | arg_grp, arg, magic, need_op_saving, may_reuse); |
a3684be1 |
3385 | if (ret < 0) |
3386 | return ret; |
e56ab892 |
3387 | } |
3388 | j--; |
3389 | |
3390 | if (ops[j].op == OP_CALL) |
3391 | { |
89ff3147 |
3392 | if (pp->is_unresolved) |
3393 | break; |
3394 | |
092f64e1 |
3395 | pp_tmp = ops[j].pp; |
e56ab892 |
3396 | if (pp_tmp == NULL) |
7ae48d73 |
3397 | ferr(po, "arg collect hit unparsed call '%s'\n", |
3398 | ops[j].operand[0].name); |
a652aa9f |
3399 | if (may_reuse && pp_tmp->argc_stack > 0) |
de50b98b |
3400 | ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n", |
3401 | arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack); |
e56ab892 |
3402 | } |
fdd5548a |
3403 | // esp adjust of 0 means we collected it before |
3404 | else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP |
3405 | && (ops[j].operand[1].type != OPT_CONST |
3406 | || ops[j].operand[1].val != 0)) |
3407 | { |
89ff3147 |
3408 | if (pp->is_unresolved) |
3409 | break; |
3410 | |
fdd5548a |
3411 | ferr(po, "arg collect %d/%d hit esp adjust of %d\n", |
3412 | arg, pp->argc, ops[j].operand[1].val); |
de50b98b |
3413 | } |
5e49b270 |
3414 | else if (ops[j].op == OP_POP && !(ops[j].flags & OPF_DONE)) |
9af2d373 |
3415 | { |
89ff3147 |
3416 | if (pp->is_unresolved) |
3417 | break; |
3418 | |
de50b98b |
3419 | ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc); |
3420 | } |
5c024ef7 |
3421 | else if (ops[j].flags & OPF_CJMP) |
de50b98b |
3422 | { |
89ff3147 |
3423 | if (pp->is_unresolved) |
3424 | break; |
3425 | |
a652aa9f |
3426 | may_reuse = 1; |
de50b98b |
3427 | } |
91ca764a |
3428 | else if (ops[j].op == OP_PUSH |
3429 | && !(ops[j].flags & (OPF_FARGNR|OPF_DONE))) |
e56ab892 |
3430 | { |
89ff3147 |
3431 | if (pp->is_unresolved && (ops[j].flags & OPF_RMD)) |
3432 | break; |
3433 | |
3a5101d7 |
3434 | ops[j].p_argnext = -1; |
3435 | po_tmp = pp->arg[arg].datap; |
3436 | if (po_tmp != NULL) |
3437 | ops[j].p_argnext = po_tmp - ops; |
e56ab892 |
3438 | pp->arg[arg].datap = &ops[j]; |
3a5101d7 |
3439 | |
2b43685d |
3440 | need_to_save_current = 0; |
23fd0b11 |
3441 | save_args = 0; |
5f70a34f |
3442 | reg = -1; |
3443 | if (ops[j].operand[0].type == OPT_REG) |
3444 | reg = ops[j].operand[0].reg; |
3445 | |
e56ab892 |
3446 | if (!need_op_saving) { |
89ff3147 |
3447 | ret = scan_for_mod(&ops[j], j + 1, i, 1); |
2b43685d |
3448 | need_to_save_current = (ret >= 0); |
e56ab892 |
3449 | } |
2b43685d |
3450 | if (need_op_saving || need_to_save_current) { |
e56ab892 |
3451 | // mark this push as one that needs operand saving |
3452 | ops[j].flags &= ~OPF_RMD; |
5f70a34f |
3453 | if (ops[j].p_argnum == 0) { |
3454 | ops[j].p_argnum = arg + 1; |
23fd0b11 |
3455 | save_args |= 1 << arg; |
de50b98b |
3456 | } |
fdd5548a |
3457 | else if (ops[j].p_argnum < arg + 1) { |
3458 | // XXX: might kill valid var.. |
3459 | //*save_arg_vars &= ~(1 << (ops[j].p_argnum - 1)); |
3460 | ops[j].p_argnum = arg + 1; |
3461 | save_args |= 1 << arg; |
3462 | } |
3a5101d7 |
3463 | |
3464 | if (save_args_seen & (1 << (ops[j].p_argnum - 1))) { |
3465 | save_args_seen = 0; |
3466 | arg_grp_current++; |
3467 | if (arg_grp_current >= MAX_ARG_GRP) |
3468 | ferr(&ops[j], "out of arg groups (arg%d), f %s\n", |
3469 | ops[j].p_argnum, pp->name); |
3470 | } |
e56ab892 |
3471 | } |
5f70a34f |
3472 | else if (ops[j].p_argnum == 0) |
e56ab892 |
3473 | ops[j].flags |= OPF_RMD; |
3474 | |
a652aa9f |
3475 | // some PUSHes are reused by different calls on other branches, |
de50b98b |
3476 | // but that can't happen if we didn't branch, so they |
3477 | // can be removed from future searches (handles nested calls) |
a652aa9f |
3478 | if (!may_reuse) |
9af2d373 |
3479 | ops[j].flags |= OPF_FARGNR; |
de50b98b |
3480 | |
9af2d373 |
3481 | ops[j].flags |= OPF_FARG; |
da87ae38 |
3482 | ops[j].flags &= ~OPF_RSAVE; |
3483 | |
23fd0b11 |
3484 | // check for __VALIST |
5f70a34f |
3485 | if (!pp->is_unresolved && pp->arg[arg].type.is_va_list) { |
23fd0b11 |
3486 | k = -1; |
92d715b6 |
3487 | ret = resolve_origin(j, &ops[j].operand[0], |
3488 | magic + 1, &k, NULL); |
5f70a34f |
3489 | if (ret == 1 && k >= 0) |
23fd0b11 |
3490 | { |
5f70a34f |
3491 | if (ops[k].op == OP_LEA) { |
3492 | snprintf(buf, sizeof(buf), "arg_%X", |
3493 | g_func_pp->argc_stack * 4); |
3494 | if (!g_func_pp->is_vararg |
3495 | || strstr(ops[k].operand[1].name, buf)) |
3496 | { |
5e49b270 |
3497 | ops[k].flags |= OPF_RMD | OPF_DONE; |
5f70a34f |
3498 | ops[j].flags |= OPF_RMD | OPF_VAPUSH; |
3499 | save_args &= ~(1 << arg); |
3500 | reg = -1; |
3501 | } |
3502 | else |
3503 | ferr(&ops[j], "lea va_list used, but no vararg?\n"); |
3504 | } |
3505 | // check for va_list from g_func_pp arg too |
3506 | else if (ops[k].op == OP_MOV |
3507 | && is_stack_access(&ops[k], &ops[k].operand[1])) |
3508 | { |
3509 | ret = stack_frame_access(&ops[k], &ops[k].operand[1], |
3510 | buf, sizeof(buf), ops[k].operand[1].name, "", 1, 0); |
3511 | if (ret >= 0) { |
5e49b270 |
3512 | ops[k].flags |= OPF_RMD | OPF_DONE; |
5f70a34f |
3513 | ops[j].flags |= OPF_RMD; |
3514 | ops[j].p_argpass = ret + 1; |
3515 | save_args &= ~(1 << arg); |
3516 | reg = -1; |
3517 | } |
3518 | } |
23fd0b11 |
3519 | } |
3520 | } |
3521 | |
3522 | *save_arg_vars |= save_args; |
3523 | |
3524 | // tracking reg usage |
5f70a34f |
3525 | if (reg >= 0) |
3526 | *regmask |= 1 << reg; |
23fd0b11 |
3527 | |
89ff3147 |
3528 | arg++; |
3529 | if (!pp->is_unresolved) { |
3530 | // next arg |
3531 | for (; arg < pp->argc; arg++) |
3532 | if (pp->arg[arg].reg == NULL) |
3533 | break; |
3534 | } |
a3684be1 |
3535 | magic = (magic & 0xffffff) | (arg << 24); |
e56ab892 |
3536 | } |
3a5101d7 |
3537 | |
3538 | if (ops[j].p_arggrp > arg_grp_current) { |
3539 | save_args_seen = 0; |
3540 | arg_grp_current = ops[j].p_arggrp; |
3541 | } |
3542 | if (ops[j].p_argnum > 0) |
3543 | save_args_seen |= 1 << (ops[j].p_argnum - 1); |
e56ab892 |
3544 | } |
3545 | |
3546 | if (arg < pp->argc) { |
3547 | ferr(po, "arg collect failed for '%s': %d/%d\n", |
3548 | pp->name, arg, pp->argc); |
89ff3147 |
3549 | return -1; |
e56ab892 |
3550 | } |
89ff3147 |
3551 | |
3a5101d7 |
3552 | if (arg_grp_current > *arg_grp) |
3553 | *arg_grp = arg_grp_current; |
3554 | |
89ff3147 |
3555 | return arg; |
3556 | } |
3557 | |
3558 | static int collect_call_args(struct parsed_op *po, int i, |
3559 | struct parsed_proto *pp, int *regmask, int *save_arg_vars, |
3560 | int magic) |
3561 | { |
3a5101d7 |
3562 | // arg group is for cases when pushes for |
3563 | // multiple funcs are going on |
3564 | struct parsed_op *po_tmp; |
3565 | int save_arg_vars_current = 0; |
3566 | int arg_grp = 0; |
89ff3147 |
3567 | int ret; |
3568 | int a; |
3569 | |
3a5101d7 |
3570 | ret = collect_call_args_r(po, i, pp, regmask, |
3571 | &save_arg_vars_current, &arg_grp, 0, magic, 0, 0); |
89ff3147 |
3572 | if (ret < 0) |
3573 | return ret; |
3574 | |
3a5101d7 |
3575 | if (arg_grp != 0) { |
3576 | // propagate arg_grp |
3577 | for (a = 0; a < pp->argc; a++) { |
3578 | if (pp->arg[a].reg != NULL) |
3579 | continue; |
3580 | |
3581 | po_tmp = pp->arg[a].datap; |
3582 | while (po_tmp != NULL) { |
3583 | po_tmp->p_arggrp = arg_grp; |
3584 | if (po_tmp->p_argnext > 0) |
3585 | po_tmp = &ops[po_tmp->p_argnext]; |
3586 | else |
3587 | po_tmp = NULL; |
3588 | } |
3589 | } |
3590 | } |
3591 | save_arg_vars[arg_grp] |= save_arg_vars_current; |
3592 | |
89ff3147 |
3593 | if (pp->is_unresolved) { |
3594 | pp->argc += ret; |
3595 | pp->argc_stack += ret; |
3596 | for (a = 0; a < pp->argc; a++) |
3597 | if (pp->arg[a].type.name == NULL) |
3598 | pp->arg[a].type.name = strdup("int"); |
3599 | } |
3600 | |
e56ab892 |
3601 | return ret; |
3602 | } |
3603 | |
89ff3147 |
3604 | static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg) |
3605 | { |
3606 | int i; |
3607 | |
3608 | for (i = 0; i < pp->argc; i++) |
3609 | if (pp->arg[i].reg == NULL) |
3610 | break; |
3611 | |
3612 | if (pp->argc_stack) |
3613 | memmove(&pp->arg[i + 1], &pp->arg[i], |
3614 | sizeof(pp->arg[0]) * pp->argc_stack); |
3615 | memset(&pp->arg[i], 0, sizeof(pp->arg[i])); |
3616 | pp->arg[i].reg = strdup(reg); |
3617 | pp->arg[i].type.name = strdup("int"); |
3618 | pp->argc++; |
3619 | pp->argc_reg++; |
3620 | } |
3621 | |
4c45fa73 |
3622 | static void add_label_ref(struct label_ref *lr, int op_i) |
3623 | { |
3624 | struct label_ref *lr_new; |
3625 | |
3626 | if (lr->i == -1) { |
3627 | lr->i = op_i; |
3628 | return; |
3629 | } |
3630 | |
3631 | lr_new = calloc(1, sizeof(*lr_new)); |
3632 | lr_new->i = op_i; |
a652aa9f |
3633 | lr_new->next = lr->next; |
4c45fa73 |
3634 | lr->next = lr_new; |
3635 | } |
3636 | |
92d715b6 |
3637 | static struct parsed_data *try_resolve_jumptab(int i, int opcnt) |
3638 | { |
3639 | struct parsed_op *po = &ops[i]; |
3640 | struct parsed_data *pd; |
3641 | char label[NAMELEN], *p; |
3642 | int len, j, l; |
3643 | |
3644 | p = strchr(po->operand[0].name, '['); |
3645 | if (p == NULL) |
3646 | return NULL; |
3647 | |
3648 | len = p - po->operand[0].name; |
3649 | strncpy(label, po->operand[0].name, len); |
3650 | label[len] = 0; |
3651 | |
3652 | for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) { |
3653 | if (IS(g_func_pd[j].label, label)) { |
3654 | pd = &g_func_pd[j]; |
3655 | break; |
3656 | } |
3657 | } |
3658 | if (pd == NULL) |
3659 | //ferr(po, "label '%s' not parsed?\n", label); |
3660 | return NULL; |
3661 | |
3662 | if (pd->type != OPT_OFFSET) |
3663 | ferr(po, "label '%s' with non-offset data?\n", label); |
3664 | |
3665 | // find all labels, link |
3666 | for (j = 0; j < pd->count; j++) { |
3667 | for (l = 0; l < opcnt; l++) { |
d7857c3a |
3668 | if (g_labels[l] != NULL && IS(g_labels[l], pd->d[j].u.label)) { |
92d715b6 |
3669 | add_label_ref(&g_label_refs[l], i); |
3670 | pd->d[j].bt_i = l; |
3671 | break; |
3672 | } |
3673 | } |
3674 | } |
3675 | |
3676 | return pd; |
3677 | } |
3678 | |
d7857c3a |
3679 | static void clear_labels(int count) |
3680 | { |
3681 | int i; |
3682 | |
3683 | for (i = 0; i < count; i++) { |
3684 | if (g_labels[i] != NULL) { |
3685 | free(g_labels[i]); |
3686 | g_labels[i] = NULL; |
3687 | } |
3688 | } |
3689 | } |
3690 | |
04f8a628 |
3691 | static void output_std_flags(FILE *fout, struct parsed_op *po, |
3692 | int *pfomask, const char *dst_opr_text) |
3693 | { |
3694 | if (*pfomask & (1 << PFO_Z)) { |
3695 | fprintf(fout, "\n cond_z = (%s%s == 0);", |
3696 | lmod_cast_u(po, po->operand[0].lmod), dst_opr_text); |
3697 | *pfomask &= ~(1 << PFO_Z); |
3698 | } |
3699 | if (*pfomask & (1 << PFO_S)) { |
3700 | fprintf(fout, "\n cond_s = (%s%s < 0);", |
3701 | lmod_cast_s(po, po->operand[0].lmod), dst_opr_text); |
3702 | *pfomask &= ~(1 << PFO_S); |
3703 | } |
3704 | } |
3705 | |
c0de9015 |
3706 | enum { |
3707 | OPP_FORCE_NORETURN = (1 << 0), |
3708 | OPP_SIMPLE_ARGS = (1 << 1), |
3709 | OPP_ALIGN = (1 << 2), |
3710 | }; |
3711 | |
c0050df6 |
3712 | static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp, |
c0de9015 |
3713 | int flags) |
c0050df6 |
3714 | { |
c0de9015 |
3715 | const char *cconv = ""; |
3716 | |
c0050df6 |
3717 | if (pp->is_fastcall) |
c0de9015 |
3718 | cconv = "__fastcall "; |
c0050df6 |
3719 | else if (pp->is_stdcall && pp->argc_reg == 0) |
c0de9015 |
3720 | cconv = "__stdcall "; |
3721 | |
3722 | fprintf(fout, (flags & OPP_ALIGN) ? "%-16s" : "%s", cconv); |
3723 | |
3724 | if (pp->is_noreturn || (flags & OPP_FORCE_NORETURN)) |
c0050df6 |
3725 | fprintf(fout, "noreturn "); |
3726 | } |
3727 | |
c0de9015 |
3728 | static void output_pp(FILE *fout, const struct parsed_proto *pp, |
3729 | int flags) |
3730 | { |
3731 | int i; |
3732 | |
3733 | fprintf(fout, (flags & OPP_ALIGN) ? "%-5s" : "%s ", |
3734 | pp->ret_type.name); |
3735 | if (pp->is_fptr) |
3736 | fprintf(fout, "("); |
3737 | output_pp_attrs(fout, pp, flags); |
3738 | if (pp->is_fptr) |
3739 | fprintf(fout, "*"); |
3740 | fprintf(fout, "%s", pp->name); |
3741 | if (pp->is_fptr) |
3742 | fprintf(fout, ")"); |
3743 | |
3744 | fprintf(fout, "("); |
3745 | for (i = 0; i < pp->argc; i++) { |
3746 | if (i > 0) |
3747 | fprintf(fout, ", "); |
3748 | if (pp->arg[i].fptr != NULL && !(flags & OPP_SIMPLE_ARGS)) { |
3749 | // func pointer |
3750 | output_pp(fout, pp->arg[i].fptr, 0); |
3751 | } |
3752 | else if (pp->arg[i].type.is_retreg) { |
3753 | fprintf(fout, "u32 *r_%s", pp->arg[i].reg); |
3754 | } |
3755 | else { |
3756 | fprintf(fout, "%s", pp->arg[i].type.name); |
3757 | if (!pp->is_fptr) |
3758 | fprintf(fout, " a%d", i + 1); |
3759 | } |
3760 | } |
3761 | if (pp->is_vararg) { |
3762 | if (i > 0) |
3763 | fprintf(fout, ", "); |
3764 | fprintf(fout, "..."); |
3765 | } |
3766 | fprintf(fout, ")"); |
3767 | } |
3768 | |
9af2d373 |
3769 | static int get_pp_arg_regmask(const struct parsed_proto *pp) |
3770 | { |
3771 | int regmask = 0; |
3772 | int i, reg; |
3773 | |
3774 | for (i = 0; i < pp->argc; i++) { |
3775 | if (pp->arg[i].reg != NULL) { |
3776 | reg = char_array_i(regs_r32, |
3777 | ARRAY_SIZE(regs_r32), pp->arg[i].reg); |
3778 | if (reg < 0) |
3779 | ferr(ops, "arg '%s' of func '%s' is not a reg?\n", |
3780 | pp->arg[i].reg, pp->name); |
3781 | regmask |= 1 << reg; |
3782 | } |
3783 | } |
3784 | |
3785 | return regmask; |
3786 | } |
3787 | |
3a5101d7 |
3788 | static char *saved_arg_name(char *buf, size_t buf_size, int grp, int num) |
3789 | { |
3790 | char buf1[16]; |
3791 | |
3792 | buf1[0] = 0; |
3793 | if (grp > 0) |
3794 | snprintf(buf1, sizeof(buf1), "%d", grp); |
3795 | snprintf(buf, buf_size, "s%s_a%d", buf1, num); |
3796 | |
3797 | return buf; |
3798 | } |
3799 | |
9af2d373 |
3800 | static void gen_x_cleanup(int opcnt); |
3801 | |
91977a1c |
3802 | static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) |
3803 | { |
69a3cdfc |
3804 | struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op; |
850c9265 |
3805 | struct parsed_opr *last_arith_dst = NULL; |
3ebea2cf |
3806 | char buf1[256], buf2[256], buf3[256], cast[64]; |
bd96f656 |
3807 | const struct parsed_proto *pp_c; |
ddaf8bd7 |
3808 | struct parsed_proto *pp, *pp_tmp; |
4c45fa73 |
3809 | struct parsed_data *pd; |
91977a1c |
3810 | const char *tmpname; |
1f84f6b3 |
3811 | unsigned int uval; |
3a5101d7 |
3812 | int save_arg_vars[MAX_ARG_GRP] = { 0, }; |
cb090db0 |
3813 | int cond_vars = 0; |
108e9fe3 |
3814 | int need_tmp_var = 0; |
2fe80fdb |
3815 | int need_tmp64 = 0; |
91977a1c |
3816 | int had_decl = 0; |
3ebea2cf |
3817 | int label_pending = 0; |
d4e3b5db |
3818 | int regmask_save = 0; |
91977a1c |
3819 | int regmask_arg = 0; |
a3684be1 |
3820 | int regmask_now = 0; |
ddaf8bd7 |
3821 | int regmask_init = 0; |
91977a1c |
3822 | int regmask = 0; |
940e8e66 |
3823 | int pfomask = 0; |
64c59faf |
3824 | int found = 0; |
d4e3b5db |
3825 | int depth = 0; |
91977a1c |
3826 | int no_output; |
4c45fa73 |
3827 | int i, j, l; |
91977a1c |
3828 | int arg; |
91977a1c |
3829 | int reg; |
3830 | int ret; |
3831 | |
1bafb621 |
3832 | g_bp_frame = g_sp_frame = g_stack_fsz = 0; |
a2c1d768 |
3833 | g_stack_frame_used = 0; |
91977a1c |
3834 | |
36595fd2 |
3835 | g_func_pp = proto_parse(fhdr, funcn, 0); |
bd96f656 |
3836 | if (g_func_pp == NULL) |
91977a1c |
3837 | ferr(ops, "proto_parse failed for '%s'\n", funcn); |
3838 | |
9af2d373 |
3839 | regmask_arg = get_pp_arg_regmask(g_func_pp); |
91977a1c |
3840 | |
3841 | // pass1: |
1bafb621 |
3842 | // - handle ebp/esp frame, remove ops related to it |
9af2d373 |
3843 | scan_prologue_epilogue(opcnt); |
91977a1c |
3844 | |
3845 | // pass2: |
7ae48d73 |
3846 | // - parse calls with labels |
87bf6cec |
3847 | // - resolve all branches |
de50b98b |
3848 | for (i = 0; i < opcnt; i++) |
3849 | { |
87bf6cec |
3850 | po = &ops[i]; |
3851 | po->bt_i = -1; |
4c45fa73 |
3852 | po->btj = NULL; |
87bf6cec |
3853 | |
5e49b270 |
3854 | if (po->flags & (OPF_RMD|OPF_DONE)) |
7ae48d73 |
3855 | continue; |
3856 | |
3857 | if (po->op == OP_CALL) { |
ddaf8bd7 |
3858 | pp = NULL; |
3859 | |
7ae48d73 |
3860 | if (po->operand[0].type == OPT_LABEL) { |
3861 | tmpname = opr_name(po, 0); |
46411e6c |
3862 | if (IS_START(tmpname, "loc_")) |
3863 | ferr(po, "call to loc_*\n"); |
36595fd2 |
3864 | pp_c = proto_parse(fhdr, tmpname, 0); |
7ae48d73 |
3865 | if (pp_c == NULL) |
3866 | ferr(po, "proto_parse failed for call '%s'\n", tmpname); |
da87ae38 |
3867 | |
7ae48d73 |
3868 | pp = proto_clone(pp_c); |
3869 | my_assert_not(pp, NULL); |
ddaf8bd7 |
3870 | } |
3871 | else if (po->datap != NULL) { |
3872 | pp = calloc(1, sizeof(*pp)); |
3873 | my_assert_not(pp, NULL); |
3874 | |
3875 | ret = parse_protostr(po->datap, pp); |
3876 | if (ret < 0) |
3877 | ferr(po, "bad protostr supplied: %s\n", (char *)po->datap); |
3878 | free(po->datap); |
3879 | po->datap = NULL; |
3880 | } |
3881 | |
3882 | if (pp != NULL) { |
3883 | if (pp->is_fptr) |
3884 | check_func_pp(po, pp, "fptr var call"); |
3885 | if (pp->is_noreturn) |
3886 | po->flags |= OPF_TAIL; |
7ae48d73 |
3887 | } |
092f64e1 |
3888 | po->pp = pp; |
7ae48d73 |
3889 | continue; |
3890 | } |
3891 | |
3892 | if (!(po->flags & OPF_JMP) || po->op == OP_RET) |
87bf6cec |
3893 | continue; |
3894 | |
4c45fa73 |
3895 | if (po->operand[0].type == OPT_REGMEM) { |
92d715b6 |
3896 | pd = try_resolve_jumptab(i, opcnt); |
4c45fa73 |
3897 | if (pd == NULL) |
840257f6 |
3898 | goto tailcall; |
4c45fa73 |
3899 | |
3900 | po->btj = pd; |
3901 | continue; |
3902 | } |
3903 | |
3904 | for (l = 0; l < opcnt; l++) { |
d7857c3a |
3905 | if (g_labels[l] != NULL |
3906 | && IS(po->operand[0].name, g_labels[l])) |
3907 | { |
092f64e1 |
3908 | if (l == i + 1 && po->op == OP_JMP) { |
3909 | // yet another alignment type.. |
5e49b270 |
3910 | po->flags |= OPF_RMD|OPF_DONE; |
092f64e1 |
3911 | break; |
3912 | } |
4c45fa73 |
3913 | add_label_ref(&g_label_refs[l], i); |
3914 | po->bt_i = l; |
87bf6cec |
3915 | break; |
3916 | } |
3917 | } |
3918 | |
092f64e1 |
3919 | if (po->bt_i != -1 || (po->flags & OPF_RMD)) |
4c45fa73 |
3920 | continue; |
3921 | |
840257f6 |
3922 | if (po->operand[0].type == OPT_LABEL) |
87bf6cec |
3923 | // assume tail call |
840257f6 |
3924 | goto tailcall; |
4c45fa73 |
3925 | |
3926 | ferr(po, "unhandled branch\n"); |
840257f6 |
3927 | |
3928 | tailcall: |
3929 | po->op = OP_CALL; |
3930 | po->flags |= OPF_TAIL; |
2fe80fdb |
3931 | if (i > 0 && ops[i - 1].op == OP_POP) |
3932 | po->flags |= OPF_ATAIL; |
7ae48d73 |
3933 | i--; // reprocess |
87bf6cec |
3934 | } |
3935 | |
3936 | // pass3: |
a2c1d768 |
3937 | // - remove dead labels |
26677139 |
3938 | // - process trivial calls |
1bafb621 |
3939 | for (i = 0; i < opcnt; i++) |
3940 | { |
d7857c3a |
3941 | if (g_labels[i] != NULL && g_label_refs[i].i == -1) { |
3942 | free(g_labels[i]); |
3943 | g_labels[i] = NULL; |
3944 | } |
a2c1d768 |
3945 | |
69a3cdfc |
3946 | po = &ops[i]; |
5e49b270 |
3947 | if (po->flags & (OPF_RMD|OPF_DONE)) |
91977a1c |
3948 | continue; |
850c9265 |
3949 | |
d4e3b5db |
3950 | if (po->op == OP_CALL) |
26677139 |
3951 | { |
3952 | pp = process_call_early(i, opcnt, &j); |
3953 | if (pp != NULL) { |
3954 | if (!(po->flags & OPF_ATAIL)) |
3955 | // since we know the args, try to collect them |
3956 | if (collect_call_args_early(po, i, pp, ®mask) != 0) |
3957 | pp = NULL; |
3958 | } |
3959 | |
3960 | if (pp != NULL) { |
3961 | if (j >= 0) { |
3962 | // commit esp adjust |
3963 | ops[j].flags |= OPF_RMD; |
5e49b270 |
3964 | if (ops[j].op != OP_POP) |
3965 | patch_esp_adjust(&ops[j], pp->argc_stack * 4); |
3966 | else |
3967 | ops[j].flags |= OPF_DONE; |
26677139 |
3968 | } |
3969 | |
3970 | if (strstr(pp->ret_type.name, "int64")) |
3971 | need_tmp64 = 1; |
3972 | |
3973 | po->flags |= OPF_DONE; |
3974 | } |
3975 | } |
3976 | } |
3977 | |
3978 | // pass4: |
3979 | // - process calls |
ee2361b9 |
3980 | // - handle push <const>/pop pairs |
26677139 |
3981 | for (i = 0; i < opcnt; i++) |
3982 | { |
3983 | po = &ops[i]; |
5e49b270 |
3984 | if (po->flags & (OPF_RMD|OPF_DONE)) |
26677139 |
3985 | continue; |
3986 | |
3987 | if (po->op == OP_CALL && !(po->flags & OPF_DONE)) |
69a3cdfc |
3988 | { |
9af2d373 |
3989 | pp = process_call(i, opcnt); |
91977a1c |
3990 | |
2fe80fdb |
3991 | if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) { |
89ff3147 |
3992 | // since we know the args, collect them |
3a5101d7 |
3993 | collect_call_args(po, i, pp, ®mask, save_arg_vars, |
89ff3147 |
3994 | i + opcnt * 2); |
3995 | } |
2b43685d |
3996 | |
3997 | if (strstr(pp->ret_type.name, "int64")) |
2fe80fdb |
3998 | need_tmp64 = 1; |
91977a1c |
3999 | } |
ee2361b9 |
4000 | else if (po->op == OP_PUSH && !(po->flags & OPF_FARG) |
4001 | && !(po->flags & OPF_RSAVE) && po->operand[0].type == OPT_CONST) |
4002 | scan_for_pop_const(i, opcnt); |
d4e3b5db |
4003 | } |
4004 | |
26677139 |
4005 | // pass5: |
d4e3b5db |
4006 | // - find POPs for PUSHes, rm both |
591721d7 |
4007 | // - scan for STD/CLD, propagate DF |
d4e3b5db |
4008 | // - scan for all used registers |
4009 | // - find flag set ops for their users |
89ff3147 |
4010 | // - do unreselved calls |
1bafb621 |
4011 | // - declare indirect functions |
26677139 |
4012 | for (i = 0; i < opcnt; i++) |
4013 | { |
d4e3b5db |
4014 | po = &ops[i]; |
5e49b270 |
4015 | if (po->flags & (OPF_RMD|OPF_DONE)) |
d4e3b5db |
4016 | continue; |
4017 | |
7ae48d73 |
4018 | if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) { |
4019 | reg = po->operand[0].reg; |
4020 | if (!(regmask & (1 << reg))) |
4021 | // not a reg save after all, rerun scan_for_pop |
4022 | po->flags &= ~OPF_RSAVE; |
4023 | else |
4024 | regmask_save |= 1 << reg; |
4025 | } |
4026 | |
9af2d373 |
4027 | if (po->op == OP_PUSH && !(po->flags & OPF_FARG) |
1f84f6b3 |
4028 | && !(po->flags & OPF_RSAVE) && !g_func_pp->is_userstack) |
d4e3b5db |
4029 | { |
5c024ef7 |
4030 | if (po->operand[0].type == OPT_REG) |
4031 | { |
4032 | reg = po->operand[0].reg; |
4033 | if (reg < 0) |
4034 | ferr(po, "reg not set for push?\n"); |
4035 | |
4036 | depth = 0; |
4037 | ret = scan_for_pop(i + 1, opcnt, |
4038 | po->operand[0].name, i + opcnt * 3, 0, &depth, 0); |
4039 | if (ret == 1) { |
4040 | if (depth > 1) |
4041 | ferr(po, "too much depth: %d\n", depth); |
4042 | |
4043 | po->flags |= OPF_RMD; |
4044 | scan_for_pop(i + 1, opcnt, po->operand[0].name, |
4045 | i + opcnt * 4, 0, &depth, 1); |
4046 | continue; |
4047 | } |
4048 | ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0); |
4049 | if (ret == 0) { |
4050 | arg = OPF_RMD; |
4051 | if (regmask & (1 << reg)) { |
4052 | if (regmask_save & (1 << reg)) |
4053 | ferr(po, "%s already saved?\n", po->operand[0].name); |
4054 | arg = OPF_RSAVE; |
4055 | } |
4056 | po->flags |= arg; |
4057 | scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg); |
4058 | continue; |
4059 | } |
d4e3b5db |
4060 | } |
d4e3b5db |
4061 | } |
4062 | |
591721d7 |
4063 | if (po->op == OP_STD) { |
5e49b270 |
4064 | po->flags |= OPF_DF | OPF_RMD | OPF_DONE; |
591721d7 |
4065 | scan_propagate_df(i + 1, opcnt); |
4066 | } |
4067 | |
a3684be1 |
4068 | regmask_now = po->regmask_src | po->regmask_dst; |
4069 | if (regmask_now & (1 << xBP)) { |
4070 | if (g_bp_frame && !(po->flags & OPF_EBP_S)) { |
4071 | if (po->regmask_dst & (1 << xBP)) |
4072 | // compiler decided to drop bp frame and use ebp as scratch |
037f4971 |
4073 | scan_fwd_set_flags(i + 1, opcnt, i + opcnt * 5, OPF_EBP_S); |
a3684be1 |
4074 | else |
4075 | regmask_now &= ~(1 << xBP); |
4076 | } |
4077 | } |
4078 | |
4079 | regmask |= regmask_now; |
d4e3b5db |
4080 | |
4081 | if (po->flags & OPF_CC) |
4082 | { |
2b43685d |
4083 | int setters[16], cnt = 0, branched = 0; |
4084 | |
04f8a628 |
4085 | ret = scan_for_flag_set(i, i + opcnt * 6, |
4086 | &branched, setters, &cnt); |
2b43685d |
4087 | if (ret < 0 || cnt <= 0) |
4088 | ferr(po, "unable to trace flag setter(s)\n"); |
4089 | if (cnt > ARRAY_SIZE(setters)) |
4090 | ferr(po, "too many flag setters\n"); |
d4e3b5db |
4091 | |
2b43685d |
4092 | for (j = 0; j < cnt; j++) |
4093 | { |
4094 | tmp_op = &ops[setters[j]]; // flag setter |
4095 | pfomask = 0; |
4096 | |
4097 | // to get nicer code, we try to delay test and cmp; |
4098 | // if we can't because of operand modification, or if we |
591721d7 |
4099 | // have arith op, or branch, make it calculate flags explicitly |
4100 | if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP) |
4101 | { |
89ff3147 |
4102 | if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0) |
092f64e1 |
4103 | pfomask = 1 << po->pfo; |
2b43685d |
4104 | } |
4741fdfe |
4105 | else if (tmp_op->op == OP_CMPS || tmp_op->op == OP_SCAS) { |
092f64e1 |
4106 | pfomask = 1 << po->pfo; |
591721d7 |
4107 | } |
2b43685d |
4108 | else { |
04f8a628 |
4109 | // see if we'll be able to handle based on op result |
4110 | if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR |
092f64e1 |
4111 | && po->pfo != PFO_Z && po->pfo != PFO_S |
4112 | && po->pfo != PFO_P) |
04f8a628 |
4113 | || branched |
2b43685d |
4114 | || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0) |
092f64e1 |
4115 | { |
4116 | pfomask = 1 << po->pfo; |
4117 | } |
2fe80fdb |
4118 | |
c8dbc5be |
4119 | if (tmp_op->op == OP_ADD && po->pfo == PFO_C) { |
4120 | propagate_lmod(tmp_op, &tmp_op->operand[0], |
4121 | &tmp_op->operand[1]); |
4122 | if (tmp_op->operand[0].lmod == OPLM_DWORD) |
4123 | need_tmp64 = 1; |
4124 | } |
2b43685d |
4125 | } |
4126 | if (pfomask) { |
4127 | tmp_op->pfomask |= pfomask; |
cb090db0 |
4128 | cond_vars |= pfomask; |
2b43685d |
4129 | } |
04f8a628 |
4130 | // note: may overwrite, currently not a problem |
4131 | po->datap = tmp_op; |
d4e3b5db |
4132 | } |
4133 | |
cb090db0 |
4134 | if (po->op == OP_RCL || po->op == OP_RCR |
4135 | || po->op == OP_ADC || po->op == OP_SBB) |
4136 | cond_vars |= 1 << PFO_C; |
d4e3b5db |
4137 | } |
092f64e1 |
4138 | |
4139 | if (po->op == OP_CMPS || po->op == OP_SCAS) { |
cb090db0 |
4140 | cond_vars |= 1 << PFO_Z; |
591721d7 |
4141 | } |
87bf6cec |
4142 | else if (po->op == OP_MUL |
4143 | || (po->op == OP_IMUL && po->operand_cnt == 1)) |
4144 | { |
c8dbc5be |
4145 | if (po->operand[0].lmod == OPLM_DWORD) |
4146 | need_tmp64 = 1; |
87bf6cec |
4147 | } |
89ff3147 |
4148 | else if (po->op == OP_CALL) { |
26677139 |
4149 | // note: resolved non-reg calls are OPF_DONE already |
092f64e1 |
4150 | pp = po->pp; |
89ff3147 |
4151 | if (pp == NULL) |
4152 | ferr(po, "NULL pp\n"); |
4153 | |
4154 | if (pp->is_unresolved) { |
ddaf8bd7 |
4155 | int regmask_stack = 0; |
3a5101d7 |
4156 | collect_call_args(po, i, pp, ®mask, save_arg_vars, |
89ff3147 |
4157 | i + opcnt * 2); |
4158 | |
b74c31e3 |
4159 | // this is pretty rough guess: |
4160 | // see ecx and edx were pushed (and not their saved versions) |
4161 | for (arg = 0; arg < pp->argc; arg++) { |
4162 | if (pp->arg[arg].reg != NULL) |
4163 | continue; |
4164 | |
4165 | tmp_op = pp->arg[arg].datap; |
4166 | if (tmp_op == NULL) |
4167 | ferr(po, "parsed_op missing for arg%d\n", arg); |
5f70a34f |
4168 | if (tmp_op->p_argnum == 0 && tmp_op->operand[0].type == OPT_REG) |
b74c31e3 |
4169 | regmask_stack |= 1 << tmp_op->operand[0].reg; |
4170 | } |
4171 | |
ddaf8bd7 |
4172 | if (!((regmask_stack & (1 << xCX)) |
4173 | && (regmask_stack & (1 << xDX)))) |
89ff3147 |
4174 | { |
4175 | if (pp->argc_stack != 0 |
c0050df6 |
4176 | || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX)))) |
89ff3147 |
4177 | { |
4178 | pp_insert_reg_arg(pp, "ecx"); |
c0050df6 |
4179 | pp->is_fastcall = 1; |
ddaf8bd7 |
4180 | regmask_init |= 1 << xCX; |
89ff3147 |
4181 | regmask |= 1 << xCX; |
4182 | } |
4183 | if (pp->argc_stack != 0 |
4184 | || ((regmask | regmask_arg) & (1 << xDX))) |
4185 | { |
4186 | pp_insert_reg_arg(pp, "edx"); |
ddaf8bd7 |
4187 | regmask_init |= 1 << xDX; |
89ff3147 |
4188 | regmask |= 1 << xDX; |
4189 | } |
4190 | } |
c0050df6 |
4191 | |
4192 | // note: __cdecl doesn't fall into is_unresolved category |
4193 | if (pp->argc_stack > 0) |
4194 | pp->is_stdcall = 1; |
ddaf8bd7 |
4195 | } |
4196 | |
4197 | for (arg = 0; arg < pp->argc; arg++) { |
4198 | if (pp->arg[arg].reg != NULL) { |
4199 | reg = char_array_i(regs_r32, |
4200 | ARRAY_SIZE(regs_r32), pp->arg[arg].reg); |
4201 | if (reg < 0) |
4202 | ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg); |
4203 | if (!(regmask & (1 << reg))) { |
4204 | regmask_init |= 1 << reg; |
4205 | regmask |= 1 << reg; |
4206 | } |
4207 | } |
89ff3147 |
4208 | } |
1bafb621 |
4209 | } |
27ebfaed |
4210 | else if (po->op == OP_MOV && po->operand[0].pp != NULL |
4211 | && po->operand[1].pp != NULL) |
4212 | { |
4213 | // <var> = offset <something> |
4214 | if ((po->operand[1].pp->is_func || po->operand[1].pp->is_fptr) |
4215 | && !IS_START(po->operand[1].name, "off_")) |
4216 | { |
4217 | if (!po->operand[0].pp->is_fptr) |
4218 | ferr(po, "%s not declared as fptr when it should be\n", |
4219 | po->operand[0].name); |
4220 | if (pp_cmp_func(po->operand[0].pp, po->operand[1].pp)) { |
4221 | pp_print(buf1, sizeof(buf1), po->operand[0].pp); |
4222 | pp_print(buf2, sizeof(buf2), po->operand[1].pp); |
4223 | fnote(po, "var: %s\n", buf1); |
4224 | fnote(po, "func: %s\n", buf2); |
4225 | ferr(po, "^ mismatch\n"); |
4226 | } |
4227 | } |
4228 | } |
75ad0378 |
4229 | else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void")) |
4230 | regmask |= 1 << xAX; |
cb090db0 |
4231 | else if (po->op == OP_DIV || po->op == OP_IDIV) { |
4232 | // 32bit division is common, look for it |
4233 | if (po->op == OP_DIV) |
4234 | ret = scan_for_reg_clear(i, xDX); |
4235 | else |
4236 | ret = scan_for_cdq_edx(i); |
4237 | if (ret >= 0) |
4238 | po->flags |= OPF_32BIT; |
4239 | else |
4240 | need_tmp64 = 1; |
4241 | } |
037f4971 |
4242 | else if (po->op == OP_CLD) |
5e49b270 |
4243 | po->flags |= OPF_RMD | OPF_DONE; |
cb090db0 |
4244 | |
4245 | if (po->op == OP_RCL || po->op == OP_RCR || po->op == OP_XCHG) { |
4246 | need_tmp_var = 1; |
4247 | } |
91977a1c |
4248 | } |
4249 | |
26677139 |
4250 | // pass6: |
da87ae38 |
4251 | // - confirm regmask_save, it might have been reduced |
4252 | if (regmask_save != 0) |
4253 | { |
4254 | regmask_save = 0; |
4255 | for (i = 0; i < opcnt; i++) { |
4256 | po = &ops[i]; |
4257 | if (po->flags & OPF_RMD) |
4258 | continue; |
4259 | |
4260 | if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) |
4261 | regmask_save |= 1 << po->operand[0].reg; |
4262 | } |
4263 | } |
4264 | |
60fe410c |
4265 | // output starts here |
4266 | |
4267 | // define userstack size |
4268 | if (g_func_pp->is_userstack) { |
4269 | fprintf(fout, "#ifndef US_SZ_%s\n", g_func_pp->name); |
4270 | fprintf(fout, "#define US_SZ_%s USERSTACK_SIZE\n", g_func_pp->name); |
4271 | fprintf(fout, "#endif\n"); |
4272 | } |
4273 | |
4274 | // the function itself |
c0de9015 |
4275 | ferr_assert(ops, !g_func_pp->is_fptr); |
4276 | output_pp(fout, g_func_pp, |
4277 | (g_ida_func_attr & IDAFA_NORETURN) ? OPP_FORCE_NORETURN : 0); |
4278 | fprintf(fout, "\n{\n"); |
60fe410c |
4279 | |
4280 | // declare indirect functions |
4281 | for (i = 0; i < opcnt; i++) { |
4282 | po = &ops[i]; |
4283 | if (po->flags & OPF_RMD) |
4284 | continue; |
4285 | |
4286 | if (po->op == OP_CALL) { |
4287 | pp = po->pp; |
4288 | if (pp == NULL) |
4289 | ferr(po, "NULL pp\n"); |
4290 | |
4291 | if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) { |
4292 | if (pp->name[0] != 0) { |
4293 | memmove(pp->name + 2, pp->name, strlen(pp->name) + 1); |
4294 | memcpy(pp->name, "i_", 2); |
4295 | |
4296 | // might be declared already |
4297 | found = 0; |
4298 | for (j = 0; j < i; j++) { |
4299 | if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) { |
4300 | if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) { |
4301 | found = 1; |
4302 | break; |
4303 | } |
4304 | } |
4305 | } |
4306 | if (found) |
4307 | continue; |
4308 | } |
4309 | else |
4310 | snprintf(pp->name, sizeof(pp->name), "icall%d", i); |
4311 | |
c0de9015 |
4312 | fprintf(fout, " "); |
4313 | output_pp(fout, pp, OPP_SIMPLE_ARGS); |
4314 | fprintf(fout, ";\n"); |
60fe410c |
4315 | } |
4316 | } |
4317 | } |
da87ae38 |
4318 | |
4c45fa73 |
4319 | // output LUTs/jumptables |
4320 | for (i = 0; i < g_func_pd_cnt; i++) { |
4321 | pd = &g_func_pd[i]; |
4322 | fprintf(fout, " static const "); |
4323 | if (pd->type == OPT_OFFSET) { |
4324 | fprintf(fout, "void *jt_%s[] =\n { ", pd->label); |
4325 | |
4326 | for (j = 0; j < pd->count; j++) { |
4327 | if (j > 0) |
4328 | fprintf(fout, ", "); |
4329 | fprintf(fout, "&&%s", pd->d[j].u.label); |
4330 | } |
4331 | } |
4332 | else { |
4333 | fprintf(fout, "%s %s[] =\n { ", |
4334 | lmod_type_u(ops, pd->lmod), pd->label); |
4335 | |
4336 | for (j = 0; j < pd->count; j++) { |
4337 | if (j > 0) |
4338 | fprintf(fout, ", "); |
4339 | fprintf(fout, "%u", pd->d[j].u.val); |
4340 | } |
4341 | } |
4342 | fprintf(fout, " };\n"); |
1f84f6b3 |
4343 | had_decl = 1; |
4c45fa73 |
4344 | } |
4345 | |
4f12f671 |
4346 | // declare stack frame, va_arg |
1f84f6b3 |
4347 | if (g_stack_fsz) { |
850c9265 |
4348 | fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n", |
1bafb621 |
4349 | (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz); |
1f84f6b3 |
4350 | had_decl = 1; |
4351 | } |
4352 | |
4353 | if (g_func_pp->is_userstack) { |
60fe410c |
4354 | fprintf(fout, " u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name); |
4355 | fprintf(fout, " u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n"); |
1f84f6b3 |
4356 | had_decl = 1; |
4357 | } |
850c9265 |
4358 | |
1f84f6b3 |
4359 | if (g_func_pp->is_vararg) { |
4f12f671 |
4360 | fprintf(fout, " va_list ap;\n"); |
1f84f6b3 |
4361 | had_decl = 1; |
4362 | } |
4f12f671 |
4363 | |
940e8e66 |
4364 | // declare arg-registers |
bd96f656 |
4365 | for (i = 0; i < g_func_pp->argc; i++) { |
4366 | if (g_func_pp->arg[i].reg != NULL) { |
91977a1c |
4367 | reg = char_array_i(regs_r32, |
bd96f656 |
4368 | ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg); |
75ad0378 |
4369 | if (regmask & (1 << reg)) { |
1f84f6b3 |
4370 | if (g_func_pp->arg[i].type.is_retreg) |
4371 | fprintf(fout, " u32 %s = *r_%s;\n", |
4372 | g_func_pp->arg[i].reg, g_func_pp->arg[i].reg); |
4373 | else |
4374 | fprintf(fout, " u32 %s = (u32)a%d;\n", |
4375 | g_func_pp->arg[i].reg, i + 1); |
75ad0378 |
4376 | } |
1f84f6b3 |
4377 | else { |
4378 | if (g_func_pp->arg[i].type.is_retreg) |
4379 | ferr(ops, "retreg '%s' is unused?\n", |
4380 | g_func_pp->arg[i].reg); |
75ad0378 |
4381 | fprintf(fout, " // %s = a%d; // unused\n", |
4382 | g_func_pp->arg[i].reg, i + 1); |
1f84f6b3 |
4383 | } |
91977a1c |
4384 | had_decl = 1; |
4385 | } |
4386 | } |
4387 | |
75ad0378 |
4388 | regmask_now = regmask & ~regmask_arg; |
4389 | regmask_now &= ~(1 << xSP); |
90307a99 |
4390 | if (regmask_now & 0x00ff) { |
91977a1c |
4391 | for (reg = 0; reg < 8; reg++) { |
75ad0378 |
4392 | if (regmask_now & (1 << reg)) { |
ddaf8bd7 |
4393 | fprintf(fout, " u32 %s", regs_r32[reg]); |
4394 | if (regmask_init & (1 << reg)) |
4395 | fprintf(fout, " = 0"); |
4396 | fprintf(fout, ";\n"); |
91977a1c |
4397 | had_decl = 1; |
4398 | } |
4399 | } |
4400 | } |
90307a99 |
4401 | if (regmask_now & 0xff00) { |
4402 | for (reg = 8; reg < 16; reg++) { |
4403 | if (regmask_now & (1 << reg)) { |
4404 | fprintf(fout, " mmxr %s", regs_r32[reg]); |
4405 | if (regmask_init & (1 << reg)) |
4406 | fprintf(fout, " = { 0, }"); |
4407 | fprintf(fout, ";\n"); |
4408 | had_decl = 1; |
4409 | } |
4410 | } |
4411 | } |
91977a1c |
4412 | |
d4e3b5db |
4413 | if (regmask_save) { |
4414 | for (reg = 0; reg < 8; reg++) { |
4415 | if (regmask_save & (1 << reg)) { |
4416 | fprintf(fout, " u32 s_%s;\n", regs_r32[reg]); |
4417 | had_decl = 1; |
4418 | } |
4419 | } |
4420 | } |
4421 | |
3a5101d7 |
4422 | for (i = 0; i < ARRAY_SIZE(save_arg_vars); i++) { |
4423 | if (save_arg_vars[i] == 0) |
4424 | continue; |
69a3cdfc |
4425 | for (reg = 0; reg < 32; reg++) { |
3a5101d7 |
4426 | if (save_arg_vars[i] & (1 << reg)) { |
4427 | fprintf(fout, " u32 %s;\n", |
4428 | saved_arg_name(buf1, sizeof(buf1), i, reg + 1)); |
69a3cdfc |
4429 | had_decl = 1; |
4430 | } |
4431 | } |
4432 | } |
4433 | |
cb090db0 |
4434 | if (cond_vars) { |
69a3cdfc |
4435 | for (i = 0; i < 8; i++) { |
cb090db0 |
4436 | if (cond_vars & (1 << i)) { |
69a3cdfc |
4437 | fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]); |
4438 | had_decl = 1; |
4439 | } |
4440 | } |
4441 | } |
4442 | |
108e9fe3 |
4443 | if (need_tmp_var) { |
4444 | fprintf(fout, " u32 tmp;\n"); |
4445 | had_decl = 1; |
4446 | } |
4447 | |
2fe80fdb |
4448 | if (need_tmp64) { |
4449 | fprintf(fout, " u64 tmp64;\n"); |
87bf6cec |
4450 | had_decl = 1; |
4451 | } |
4452 | |
91977a1c |
4453 | if (had_decl) |
4454 | fprintf(fout, "\n"); |
4455 | |
bd96f656 |
4456 | if (g_func_pp->is_vararg) { |
4457 | if (g_func_pp->argc_stack == 0) |
4f12f671 |
4458 | ferr(ops, "vararg func without stack args?\n"); |
bd96f656 |
4459 | fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc); |
4f12f671 |
4460 | } |
4461 | |
91977a1c |
4462 | // output ops |
69a3cdfc |
4463 | for (i = 0; i < opcnt; i++) |
4464 | { |
d7857c3a |
4465 | if (g_labels[i] != NULL) { |
91977a1c |
4466 | fprintf(fout, "\n%s:\n", g_labels[i]); |
3ebea2cf |
4467 | label_pending = 1; |
2b43685d |
4468 | |
4469 | delayed_flag_op = NULL; |
4470 | last_arith_dst = NULL; |
3ebea2cf |
4471 | } |
91977a1c |
4472 | |
69a3cdfc |
4473 | po = &ops[i]; |
4474 | if (po->flags & OPF_RMD) |
91977a1c |
4475 | continue; |
4476 | |
4477 | no_output = 0; |
4478 | |
91977a1c |
4479 | #define assert_operand_cnt(n_) \ |
850c9265 |
4480 | if (po->operand_cnt != n_) \ |
4481 | ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_) |
4482 | |
69a3cdfc |
4483 | // conditional/flag using op? |
4484 | if (po->flags & OPF_CC) |
850c9265 |
4485 | { |
940e8e66 |
4486 | int is_delayed = 0; |
69a3cdfc |
4487 | |
04f8a628 |
4488 | tmp_op = po->datap; |
850c9265 |
4489 | |
69a3cdfc |
4490 | // we go through all this trouble to avoid using parsed_flag_op, |
4491 | // which makes generated code much nicer |
4492 | if (delayed_flag_op != NULL) |
850c9265 |
4493 | { |
092f64e1 |
4494 | out_cmp_test(buf1, sizeof(buf1), delayed_flag_op, |
4495 | po->pfo, po->pfo_inv); |
940e8e66 |
4496 | is_delayed = 1; |
91977a1c |
4497 | } |
850c9265 |
4498 | else if (last_arith_dst != NULL |
092f64e1 |
4499 | && (po->pfo == PFO_Z || po->pfo == PFO_S || po->pfo == PFO_P |
04f8a628 |
4500 | || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR)) |
4501 | )) |
850c9265 |
4502 | { |
3ebea2cf |
4503 | out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst); |
092f64e1 |
4504 | out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv, |
850c9265 |
4505 | last_arith_dst->lmod, buf3); |
940e8e66 |
4506 | is_delayed = 1; |
850c9265 |
4507 | } |
04f8a628 |
4508 | else if (tmp_op != NULL) { |
7ba45c34 |
4509 | // use preprocessed flag calc results |
092f64e1 |
4510 | if (!(tmp_op->pfomask & (1 << po->pfo))) |
4511 | ferr(po, "not prepared for pfo %d\n", po->pfo); |
69a3cdfc |
4512 | |
092f64e1 |
4513 | // note: pfo_inv was not yet applied |
69a3cdfc |
4514 | snprintf(buf1, sizeof(buf1), "(%scond_%s)", |
092f64e1 |
4515 | po->pfo_inv ? "!" : "", parsed_flag_op_names[po->pfo]); |
69a3cdfc |
4516 | } |
4517 | else { |
4518 | ferr(po, "all methods of finding comparison failed\n"); |
4519 | } |
850c9265 |
4520 | |
69a3cdfc |
4521 | if (po->flags & OPF_JMP) { |
092f64e1 |
4522 | fprintf(fout, " if %s", buf1); |
850c9265 |
4523 | } |
cb090db0 |
4524 | else if (po->op == OP_RCL || po->op == OP_RCR |
4525 | || po->op == OP_ADC || po->op == OP_SBB) |
4526 | { |
940e8e66 |
4527 | if (is_delayed) |
4528 | fprintf(fout, " cond_%s = %s;\n", |
092f64e1 |
4529 | parsed_flag_op_names[po->pfo], buf1); |
850c9265 |
4530 | } |
5101a5f9 |
4531 | else if (po->flags & OPF_DATA) { // SETcc |
850c9265 |
4532 | out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]); |
4533 | fprintf(fout, " %s = %s;", buf2, buf1); |
91977a1c |
4534 | } |
69a3cdfc |
4535 | else { |
4536 | ferr(po, "unhandled conditional op\n"); |
4537 | } |
91977a1c |
4538 | } |
4539 | |
940e8e66 |
4540 | pfomask = po->pfomask; |
4541 | |
4741fdfe |
4542 | if (po->flags & (OPF_REPZ|OPF_REPNZ)) { |
1f84f6b3 |
4543 | struct parsed_opr opr = {0,}; |
4544 | opr.type = OPT_REG; |
4545 | opr.reg = xCX; |
4546 | opr.lmod = OPLM_DWORD; |
4547 | ret = try_resolve_const(i, &opr, opcnt * 7 + i, &uval); |
4548 | |
4549 | if (ret != 1 || uval == 0) { |
4550 | // we need initial flags for ecx=0 case.. |
4551 | if (i > 0 && ops[i - 1].op == OP_XOR |
4552 | && IS(ops[i - 1].operand[0].name, |
4553 | ops[i - 1].operand[1].name)) |
4554 | { |
4555 | fprintf(fout, " cond_z = "); |
4556 | if (pfomask & (1 << PFO_C)) |
4557 | fprintf(fout, "cond_c = "); |
4558 | fprintf(fout, "0;\n"); |
4559 | } |
4560 | else if (last_arith_dst != NULL) { |
4561 | out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst); |
4562 | out_test_for_cc(buf1, sizeof(buf1), po, PFO_Z, 0, |
4563 | last_arith_dst->lmod, buf3); |
4564 | fprintf(fout, " cond_z = %s;\n", buf1); |
4565 | } |
4566 | else |
4567 | ferr(po, "missing initial ZF\n"); |
4741fdfe |
4568 | } |
4741fdfe |
4569 | } |
4570 | |
850c9265 |
4571 | switch (po->op) |
91977a1c |
4572 | { |
4573 | case OP_MOV: |
4574 | assert_operand_cnt(2); |
850c9265 |
4575 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
de50b98b |
4576 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
c7ed83dd |
4577 | default_cast_to(buf3, sizeof(buf3), &po->operand[0]); |
de50b98b |
4578 | fprintf(fout, " %s = %s;", buf1, |
3ebea2cf |
4579 | out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], |
c7ed83dd |
4580 | buf3, 0)); |
850c9265 |
4581 | break; |
4582 | |
4583 | case OP_LEA: |
4584 | assert_operand_cnt(2); |
87bf6cec |
4585 | po->operand[1].lmod = OPLM_DWORD; // always |
850c9265 |
4586 | fprintf(fout, " %s = %s;", |
4587 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
3ebea2cf |
4588 | out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], |
4589 | NULL, 1)); |
850c9265 |
4590 | break; |
4591 | |
4592 | case OP_MOVZX: |
4593 | assert_operand_cnt(2); |
91977a1c |
4594 | fprintf(fout, " %s = %s;", |
850c9265 |
4595 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
3ebea2cf |
4596 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
850c9265 |
4597 | break; |
4598 | |
4599 | case OP_MOVSX: |
4600 | assert_operand_cnt(2); |
4601 | switch (po->operand[1].lmod) { |
4602 | case OPLM_BYTE: |
4603 | strcpy(buf3, "(s8)"); |
4604 | break; |
4605 | case OPLM_WORD: |
4606 | strcpy(buf3, "(s16)"); |
4607 | break; |
4608 | default: |
4609 | ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod); |
4610 | } |
a2c1d768 |
4611 | fprintf(fout, " %s = %s;", |
850c9265 |
4612 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
a2c1d768 |
4613 | out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], |
4614 | buf3, 0)); |
850c9265 |
4615 | break; |
4616 | |
108e9fe3 |
4617 | case OP_XCHG: |
4618 | assert_operand_cnt(2); |
4619 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
4620 | fprintf(fout, " tmp = %s;", |
4621 | out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0)); |
4622 | fprintf(fout, " %s = %s;", |
4623 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
c7ed83dd |
4624 | out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], |
4625 | default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0)); |
4626 | fprintf(fout, " %s = %stmp;", |
4627 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]), |
4628 | default_cast_to(buf3, sizeof(buf3), &po->operand[1])); |
108e9fe3 |
4629 | snprintf(g_comment, sizeof(g_comment), "xchg"); |
4630 | break; |
4631 | |
850c9265 |
4632 | case OP_NOT: |
4633 | assert_operand_cnt(1); |
4634 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
4635 | fprintf(fout, " %s = ~%s;", buf1, buf1); |
4636 | break; |
4637 | |
5101a5f9 |
4638 | case OP_CDQ: |
4639 | assert_operand_cnt(2); |
4640 | fprintf(fout, " %s = (s32)%s >> 31;", |
4641 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
3ebea2cf |
4642 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
5101a5f9 |
4643 | strcpy(g_comment, "cdq"); |
4644 | break; |
4645 | |
092f64e1 |
4646 | case OP_LODS: |
4647 | assert_operand_cnt(3); |
4648 | if (po->flags & OPF_REP) { |
4649 | // hmh.. |
4650 | ferr(po, "TODO\n"); |
4651 | } |
4652 | else { |
05381e4a |
4653 | fprintf(fout, " eax = %sesi; esi %c= %d;", |
092f64e1 |
4654 | lmod_cast_u_ptr(po, po->operand[0].lmod), |
4655 | (po->flags & OPF_DF) ? '-' : '+', |
4656 | lmod_bytes(po, po->operand[0].lmod)); |
4657 | strcpy(g_comment, "lods"); |
4658 | } |
4659 | break; |
4660 | |
33c35af6 |
4661 | case OP_STOS: |
33c35af6 |
4662 | assert_operand_cnt(3); |
4663 | if (po->flags & OPF_REP) { |
591721d7 |
4664 | fprintf(fout, " for (; ecx != 0; ecx--, edi %c= %d)\n", |
4665 | (po->flags & OPF_DF) ? '-' : '+', |
33c35af6 |
4666 | lmod_bytes(po, po->operand[0].lmod)); |
d4e3b5db |
4667 | fprintf(fout, " %sedi = eax;", |
4668 | lmod_cast_u_ptr(po, po->operand[0].lmod)); |
33c35af6 |
4669 | strcpy(g_comment, "rep stos"); |
4670 | } |
4671 | else { |
092f64e1 |
4672 | fprintf(fout, " %sedi = eax; edi %c= %d;", |
d4e3b5db |
4673 | lmod_cast_u_ptr(po, po->operand[0].lmod), |
591721d7 |
4674 | (po->flags & OPF_DF) ? '-' : '+', |
33c35af6 |
4675 | lmod_bytes(po, po->operand[0].lmod)); |
4676 | strcpy(g_comment, "stos"); |
4677 | } |
4678 | break; |
4679 | |
d4e3b5db |
4680 | case OP_MOVS: |
d4e3b5db |
4681 | assert_operand_cnt(3); |
4682 | j = lmod_bytes(po, po->operand[0].lmod); |
4683 | strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod)); |
591721d7 |
4684 | l = (po->flags & OPF_DF) ? '-' : '+'; |
d4e3b5db |
4685 | if (po->flags & OPF_REP) { |
4686 | fprintf(fout, |
591721d7 |
4687 | " for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n", |
4688 | l, j, l, j); |
d4e3b5db |
4689 | fprintf(fout, |
4690 | " %sedi = %sesi;", buf1, buf1); |
4691 | strcpy(g_comment, "rep movs"); |
4692 | } |
4693 | else { |
092f64e1 |
4694 | fprintf(fout, " %sedi = %sesi; edi %c= %d; esi %c= %d;", |
591721d7 |
4695 | buf1, buf1, l, j, l, j); |
d4e3b5db |
4696 | strcpy(g_comment, "movs"); |
4697 | } |
4698 | break; |
4699 | |
7ba45c34 |
4700 | case OP_CMPS: |
7ba45c34 |
4701 | // repe ~ repeat while ZF=1 |
4702 | assert_operand_cnt(3); |
4703 | j = lmod_bytes(po, po->operand[0].lmod); |
4704 | strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod)); |
591721d7 |
4705 | l = (po->flags & OPF_DF) ? '-' : '+'; |
7ba45c34 |
4706 | if (po->flags & OPF_REP) { |
4707 | fprintf(fout, |
1f84f6b3 |
4708 | " for (; ecx != 0; ecx--) {\n"); |
4741fdfe |
4709 | if (pfomask & (1 << PFO_C)) { |
4710 | // ugh.. |
4711 | fprintf(fout, |
05381e4a |
4712 | " cond_c = %sesi < %sedi;\n", buf1, buf1); |
4741fdfe |
4713 | pfomask &= ~(1 << PFO_C); |
4714 | } |
7ba45c34 |
4715 | fprintf(fout, |
05381e4a |
4716 | " cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n", |
1f84f6b3 |
4717 | buf1, buf1, l, j, l, j); |
4718 | fprintf(fout, |
4719 | " if (cond_z %s 0) break;\n", |
4720 | (po->flags & OPF_REPZ) ? "==" : "!="); |
7ba45c34 |
4721 | fprintf(fout, |
4741fdfe |
4722 | " }"); |
7ba45c34 |
4723 | snprintf(g_comment, sizeof(g_comment), "rep%s cmps", |
4724 | (po->flags & OPF_REPZ) ? "e" : "ne"); |
4725 | } |
4726 | else { |
4727 | fprintf(fout, |
05381e4a |
4728 | " cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;", |
591721d7 |
4729 | buf1, buf1, l, j, l, j); |
7ba45c34 |
4730 | strcpy(g_comment, "cmps"); |
4731 | } |
4732 | pfomask &= ~(1 << PFO_Z); |
4733 | last_arith_dst = NULL; |
4734 | delayed_flag_op = NULL; |
4735 | break; |
4736 | |
591721d7 |
4737 | case OP_SCAS: |
4738 | // only does ZF (for now) |
4739 | // repe ~ repeat while ZF=1 |
4740 | assert_operand_cnt(3); |
4741 | j = lmod_bytes(po, po->operand[0].lmod); |
4742 | l = (po->flags & OPF_DF) ? '-' : '+'; |
4743 | if (po->flags & OPF_REP) { |
4744 | fprintf(fout, |
1f84f6b3 |
4745 | " for (; ecx != 0; ecx--) {\n"); |
591721d7 |
4746 | fprintf(fout, |
1f84f6b3 |
4747 | " cond_z = (%seax == %sedi); edi %c= %d;\n", |
591721d7 |
4748 | lmod_cast_u(po, po->operand[0].lmod), |
1f84f6b3 |
4749 | lmod_cast_u_ptr(po, po->operand[0].lmod), l, j); |
4750 | fprintf(fout, |
4751 | " if (cond_z %s 0) break;\n", |
591721d7 |
4752 | (po->flags & OPF_REPZ) ? "==" : "!="); |
4753 | fprintf(fout, |
1f84f6b3 |
4754 | " }"); |
591721d7 |
4755 | snprintf(g_comment, sizeof(g_comment), "rep%s scas", |
4756 | (po->flags & OPF_REPZ) ? "e" : "ne"); |
4757 | } |
4758 | else { |
05381e4a |
4759 | fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;", |
591721d7 |
4760 | lmod_cast_u(po, po->operand[0].lmod), |
4761 | lmod_cast_u_ptr(po, po->operand[0].lmod), l, j); |
4762 | strcpy(g_comment, "scas"); |
4763 | } |
4764 | pfomask &= ~(1 << PFO_Z); |
4765 | last_arith_dst = NULL; |
4766 | delayed_flag_op = NULL; |
4767 | break; |
4768 | |
850c9265 |
4769 | // arithmetic w/flags |
850c9265 |
4770 | case OP_AND: |
4771 | case OP_OR: |
5101a5f9 |
4772 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
4773 | // fallthrough |
850c9265 |
4774 | dualop_arith: |
4775 | assert_operand_cnt(2); |
850c9265 |
4776 | fprintf(fout, " %s %s= %s;", |
4777 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
4778 | op_to_c(po), |
3ebea2cf |
4779 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
04f8a628 |
4780 | output_std_flags(fout, po, &pfomask, buf1); |
4781 | last_arith_dst = &po->operand[0]; |
4782 | delayed_flag_op = NULL; |
4783 | break; |
4784 | |
4785 | case OP_SHL: |
4786 | case OP_SHR: |
4787 | assert_operand_cnt(2); |
4788 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
4789 | if (pfomask & (1 << PFO_C)) { |
4790 | if (po->operand[1].type == OPT_CONST) { |
4791 | l = lmod_bytes(po, po->operand[0].lmod) * 8; |
4792 | j = po->operand[1].val; |
4793 | j %= l; |
4794 | if (j != 0) { |
4795 | if (po->op == OP_SHL) |
4796 | j = l - j; |
4797 | else |
4798 | j -= 1; |
cb090db0 |
4799 | fprintf(fout, " cond_c = (%s >> %d) & 1;\n", |
4800 | buf1, j); |
04f8a628 |
4801 | } |
4802 | else |
4803 | ferr(po, "zero shift?\n"); |
4804 | } |
4805 | else |
4806 | ferr(po, "TODO\n"); |
4807 | pfomask &= ~(1 << PFO_C); |
840257f6 |
4808 | } |
04f8a628 |
4809 | fprintf(fout, " %s %s= %s;", buf1, op_to_c(po), |
4810 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
4811 | output_std_flags(fout, po, &pfomask, buf1); |
850c9265 |
4812 | last_arith_dst = &po->operand[0]; |
69a3cdfc |
4813 | delayed_flag_op = NULL; |
850c9265 |
4814 | break; |
4815 | |
d4e3b5db |
4816 | case OP_SAR: |
4817 | assert_operand_cnt(2); |
4818 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
4819 | fprintf(fout, " %s = %s%s >> %s;", buf1, |
4820 | lmod_cast_s(po, po->operand[0].lmod), buf1, |
3ebea2cf |
4821 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
04f8a628 |
4822 | output_std_flags(fout, po, &pfomask, buf1); |
d4e3b5db |
4823 | last_arith_dst = &po->operand[0]; |
4824 | delayed_flag_op = NULL; |
4825 | break; |
4826 | |
3b2f4044 |
4827 | case OP_SHRD: |
4828 | assert_operand_cnt(3); |
4829 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
4830 | l = lmod_bytes(po, po->operand[0].lmod) * 8; |
4831 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
4832 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]); |
4833 | out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[2]); |
4834 | fprintf(fout, " %s >>= %s; %s |= %s << (%d - %s);", |
4835 | buf1, buf3, buf1, buf2, l, buf3); |
4836 | strcpy(g_comment, "shrd"); |
4837 | output_std_flags(fout, po, &pfomask, buf1); |
4838 | last_arith_dst = &po->operand[0]; |
4839 | delayed_flag_op = NULL; |
4840 | break; |
4841 | |
d4e3b5db |
4842 | case OP_ROL: |
4843 | case OP_ROR: |
4844 | assert_operand_cnt(2); |
4845 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
4846 | if (po->operand[1].type == OPT_CONST) { |
4847 | j = po->operand[1].val; |
4848 | j %= lmod_bytes(po, po->operand[0].lmod) * 8; |
4849 | fprintf(fout, po->op == OP_ROL ? |
4850 | " %s = (%s << %d) | (%s >> %d);" : |
4851 | " %s = (%s >> %d) | (%s << %d);", |
4852 | buf1, buf1, j, buf1, |
4853 | lmod_bytes(po, po->operand[0].lmod) * 8 - j); |
4854 | } |
4855 | else |
4856 | ferr(po, "TODO\n"); |
04f8a628 |
4857 | output_std_flags(fout, po, &pfomask, buf1); |
d4e3b5db |
4858 | last_arith_dst = &po->operand[0]; |
4859 | delayed_flag_op = NULL; |
4860 | break; |
4861 | |
cb090db0 |
4862 | case OP_RCL: |
4863 | case OP_RCR: |
4864 | assert_operand_cnt(2); |
4865 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
4866 | l = lmod_bytes(po, po->operand[0].lmod) * 8; |
4867 | if (po->operand[1].type == OPT_CONST) { |
4868 | j = po->operand[1].val % l; |
4869 | if (j == 0) |
4870 | ferr(po, "zero rotate\n"); |
4871 | fprintf(fout, " tmp = (%s >> %d) & 1;\n", |
4872 | buf1, (po->op == OP_RCL) ? (l - j) : (j - 1)); |
4873 | if (po->op == OP_RCL) { |
4874 | fprintf(fout, |
4875 | " %s = (%s << %d) | (cond_c << %d)", |
4876 | buf1, buf1, j, j - 1); |
4877 | if (j != 1) |
4878 | fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j); |
4879 | } |
4880 | else { |
4881 | fprintf(fout, |
4882 | " %s = (%s >> %d) | (cond_c << %d)", |
4883 | buf1, buf1, j, l - j); |
4884 | if (j != 1) |
4885 | fprintf(fout, " | (%s << %d)", buf1, l + 1 - j); |
4886 | } |
4887 | fprintf(fout, ";\n"); |
4888 | fprintf(fout, " cond_c = tmp;"); |
4889 | } |
4890 | else |
4891 | ferr(po, "TODO\n"); |
4892 | strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr"); |
4893 | output_std_flags(fout, po, &pfomask, buf1); |
4894 | last_arith_dst = &po->operand[0]; |
4895 | delayed_flag_op = NULL; |
4896 | break; |
4897 | |
5101a5f9 |
4898 | case OP_XOR: |
850c9265 |
4899 | assert_operand_cnt(2); |
4900 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
5101a5f9 |
4901 | if (IS(opr_name(po, 0), opr_name(po, 1))) { |
4902 | // special case for XOR |
2fe80fdb |
4903 | if (pfomask & (1 << PFO_BE)) { // weird, but it happens.. |
4904 | fprintf(fout, " cond_be = 1;\n"); |
4905 | pfomask &= ~(1 << PFO_BE); |
4906 | } |
5101a5f9 |
4907 | fprintf(fout, " %s = 0;", |
4908 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0])); |
4909 | last_arith_dst = &po->operand[0]; |
4910 | delayed_flag_op = NULL; |
850c9265 |
4911 | break; |
850c9265 |
4912 | } |
5101a5f9 |
4913 | goto dualop_arith; |
4914 | |
2fe80fdb |
4915 | case OP_ADD: |
4916 | assert_operand_cnt(2); |
4917 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
4918 | if (pfomask & (1 << PFO_C)) { |
c8dbc5be |
4919 | out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]); |
4920 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]); |
4921 | if (po->operand[0].lmod == OPLM_DWORD) { |
4922 | fprintf(fout, " tmp64 = (u64)%s + %s;\n", buf1, buf2); |
4923 | fprintf(fout, " cond_c = tmp64 >> 32;\n"); |
4924 | fprintf(fout, " %s = (u32)tmp64;", |
4925 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0])); |
4926 | strcat(g_comment, "add64"); |
4927 | } |
4928 | else { |
4929 | fprintf(fout, " cond_c = ((u32)%s + %s) >> %d;\n", |
4930 | buf1, buf2, lmod_bytes(po, po->operand[0].lmod) * 8); |
4931 | fprintf(fout, " %s += %s;", |
4932 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
4933 | buf2); |
4934 | } |
2fe80fdb |
4935 | pfomask &= ~(1 << PFO_C); |
4936 | output_std_flags(fout, po, &pfomask, buf1); |
4937 | last_arith_dst = &po->operand[0]; |
4938 | delayed_flag_op = NULL; |
4939 | break; |
4940 | } |
4941 | goto dualop_arith; |
4942 | |
4943 | case OP_SUB: |
4944 | assert_operand_cnt(2); |
4945 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
3b2f4044 |
4946 | if (pfomask & ~((1 << PFO_Z) | (1 << PFO_S))) { |
4947 | for (j = 0; j <= PFO_LE; j++) { |
4948 | if (!(pfomask & (1 << j))) |
4949 | continue; |
4950 | if (j == PFO_Z || j == PFO_S) |
4951 | continue; |
4952 | |
4953 | out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0); |
4954 | fprintf(fout, " cond_%s = %s;\n", |
4955 | parsed_flag_op_names[j], buf1); |
4956 | pfomask &= ~(1 << j); |
4957 | } |
2fe80fdb |
4958 | } |
4959 | goto dualop_arith; |
4960 | |
5101a5f9 |
4961 | case OP_ADC: |
850c9265 |
4962 | case OP_SBB: |
5101a5f9 |
4963 | assert_operand_cnt(2); |
4964 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
a2c1d768 |
4965 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
840257f6 |
4966 | if (po->op == OP_SBB |
4967 | && IS(po->operand[0].name, po->operand[1].name)) |
4968 | { |
4969 | // avoid use of unitialized var |
a2c1d768 |
4970 | fprintf(fout, " %s = -cond_c;", buf1); |
94d447fb |
4971 | // carry remains what it was |
4972 | pfomask &= ~(1 << PFO_C); |
840257f6 |
4973 | } |
4974 | else { |
a2c1d768 |
4975 | fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po), |
3ebea2cf |
4976 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); |
840257f6 |
4977 | } |
a2c1d768 |
4978 | output_std_flags(fout, po, &pfomask, buf1); |
5101a5f9 |
4979 | last_arith_dst = &po->operand[0]; |
4980 | delayed_flag_op = NULL; |
850c9265 |
4981 | break; |
4982 | |
1f84f6b3 |
4983 | case OP_BSF: |
4984 | assert_operand_cnt(2); |
4985 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]); |
4986 | fprintf(fout, " %s = %s ? __builtin_ffs(%s) - 1 : 0;", |
4987 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), |
4988 | buf2, buf2); |
4989 | output_std_flags(fout, po, &pfomask, buf1); |
4990 | last_arith_dst = &po->operand[0]; |
4991 | delayed_flag_op = NULL; |
4992 | strcat(g_comment, "bsf"); |
4993 | break; |
4994 | |
850c9265 |
4995 | case OP_DEC: |
90307a99 |
4996 | if (pfomask & ~(PFOB_S | PFOB_S | PFOB_C)) { |
4997 | for (j = 0; j <= PFO_LE; j++) { |
4998 | if (!(pfomask & (1 << j))) |
4999 | continue; |
5000 | if (j == PFO_Z || j == PFO_S || j == PFO_C) |
5001 | continue; |
5002 | |
5003 | out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0); |
5004 | fprintf(fout, " cond_%s = %s;\n", |
5005 | parsed_flag_op_names[j], buf1); |
5006 | pfomask &= ~(1 << j); |
5007 | } |
5008 | } |
5009 | // fallthrough |
5010 | |
5011 | case OP_INC: |
5012 | if (pfomask & (1 << PFO_C)) |
5013 | // carry is unaffected by inc/dec.. wtf? |
5014 | ferr(po, "carry propagation needed\n"); |
5015 | |
850c9265 |
5016 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
1bafb621 |
5017 | if (po->operand[0].type == OPT_REG) { |
5018 | strcpy(buf2, po->op == OP_INC ? "++" : "--"); |
5019 | fprintf(fout, " %s%s;", buf1, buf2); |
5020 | } |
5021 | else { |
5022 | strcpy(buf2, po->op == OP_INC ? "+" : "-"); |
5023 | fprintf(fout, " %s %s= 1;", buf1, buf2); |
5024 | } |
a2c1d768 |
5025 | output_std_flags(fout, po, &pfomask, buf1); |
5101a5f9 |
5026 | last_arith_dst = &po->operand[0]; |
5027 | delayed_flag_op = NULL; |
5028 | break; |
5029 | |
5030 | case OP_NEG: |
5031 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
3ebea2cf |
5032 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]); |
5101a5f9 |
5033 | fprintf(fout, " %s = -%s%s;", buf1, |
5034 | lmod_cast_s(po, po->operand[0].lmod), buf2); |
850c9265 |
5035 | last_arith_dst = &po->operand[0]; |
69a3cdfc |
5036 | delayed_flag_op = NULL; |
940e8e66 |
5037 | if (pfomask & (1 << PFO_C)) { |
5038 | fprintf(fout, "\n cond_c = (%s != 0);", buf1); |
5039 | pfomask &= ~(1 << PFO_C); |
5040 | } |
850c9265 |
5041 | break; |
5042 | |
5043 | case OP_IMUL: |
de50b98b |
5044 | if (po->operand_cnt == 2) { |
5045 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
850c9265 |
5046 | goto dualop_arith; |
de50b98b |
5047 | } |
87bf6cec |
5048 | if (po->operand_cnt == 3) |
5049 | ferr(po, "TODO imul3\n"); |
5050 | // fallthrough |
5051 | case OP_MUL: |
5052 | assert_operand_cnt(1); |
c8dbc5be |
5053 | switch (po->operand[0].lmod) { |
5054 | case OPLM_DWORD: |
5055 | strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)"); |
5056 | fprintf(fout, " tmp64 = %seax * %s%s;\n", buf1, buf1, |
5057 | out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0])); |
5058 | fprintf(fout, " edx = tmp64 >> 32;\n"); |
5059 | fprintf(fout, " eax = tmp64;"); |
5060 | break; |
5061 | case OPLM_BYTE: |
5062 | strcpy(buf1, po->op == OP_IMUL ? "(s16)(s8)" : "(u16)(u8)"); |
5063 | fprintf(fout, " LOWORD(eax) = %seax * %s;", buf1, |
5064 | out_src_opr(buf2, sizeof(buf2), po, &po->operand[0], |
5065 | buf1, 0)); |
5066 | break; |
5067 | default: |
5068 | ferr(po, "TODO: unhandled mul type\n"); |
5069 | break; |
5070 | } |
87bf6cec |
5071 | last_arith_dst = NULL; |
69a3cdfc |
5072 | delayed_flag_op = NULL; |
91977a1c |
5073 | break; |
5074 | |
5101a5f9 |
5075 | case OP_DIV: |
5076 | case OP_IDIV: |
5077 | assert_operand_cnt(1); |
5078 | if (po->operand[0].lmod != OPLM_DWORD) |
5079 | ferr(po, "unhandled lmod %d\n", po->operand[0].lmod); |
5080 | |
cb090db0 |
5081 | out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]); |
5082 | strcpy(buf2, lmod_cast(po, po->operand[0].lmod, |
5083 | po->op == OP_IDIV)); |
5084 | switch (po->operand[0].lmod) { |
5085 | case OPLM_DWORD: |
5086 | if (po->flags & OPF_32BIT) |
5087 | snprintf(buf3, sizeof(buf3), "%seax", buf2); |
5088 | else { |
5089 | fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n"); |
5090 | snprintf(buf3, sizeof(buf3), "%stmp64", |
5091 | (po->op == OP_IDIV) ? "(s64)" : ""); |
5092 | } |
5093 | if (po->operand[0].type == OPT_REG |
5094 | && po->operand[0].reg == xDX) |
5095 | { |
5096 | fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1); |
5097 | fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1); |
5098 | } |
5099 | else { |
5100 | fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1); |
5101 | fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1); |
5102 | } |
5103 | break; |
5104 | default: |
5105 | ferr(po, "unhandled division type\n"); |
5101a5f9 |
5106 | } |
87bf6cec |
5107 | last_arith_dst = NULL; |
5108 | delayed_flag_op = NULL; |
5101a5f9 |
5109 | break; |
5110 | |
91977a1c |
5111 | case OP_TEST: |
5112 | case OP_CMP: |
850c9265 |
5113 | propagate_lmod(po, &po->operand[0], &po->operand[1]); |
940e8e66 |
5114 | if (pfomask != 0) { |
69a3cdfc |
5115 | for (j = 0; j < 8; j++) { |
940e8e66 |
5116 | if (pfomask & (1 << j)) { |
69a3cdfc |
5117 | out_cmp_test(buf1, sizeof(buf1), po, j, 0); |
5118 | fprintf(fout, " cond_%s = %s;", |
5119 | parsed_flag_op_names[j], buf1); |
5120 | } |
5121 | } |
940e8e66 |
5122 | pfomask = 0; |
69a3cdfc |
5123 | } |
5124 | else |
5125 | no_output = 1; |
7ba45c34 |
5126 | last_arith_dst = NULL; |
69a3cdfc |
5127 | delayed_flag_op = po; |
91977a1c |
5128 | break; |
5129 | |
092f64e1 |
5130 | case OP_SCC: |
5131 | // SETcc - should already be handled |
5132 | break; |
5133 | |
69a3cdfc |
5134 | // note: we reuse OP_Jcc for SETcc, only flags differ |
092f64e1 |
5135 | case OP_JCC: |
5136 | fprintf(fout, "\n goto %s;", po->operand[0].name); |
850c9265 |
5137 | break; |
5138 | |
5c024ef7 |
5139 | case OP_JECXZ: |
5140 | fprintf(fout, " if (ecx == 0)\n"); |
5141 | fprintf(fout, " goto %s;", po->operand[0].name); |
5142 | strcat(g_comment, "jecxz"); |
5143 | break; |
5144 | |
850c9265 |
5145 | case OP_JMP: |
87bf6cec |
5146 | assert_operand_cnt(1); |
de50b98b |
5147 | last_arith_dst = NULL; |
5148 | delayed_flag_op = NULL; |
5149 | |
4c45fa73 |
5150 | if (po->operand[0].type == OPT_REGMEM) { |
5151 | ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]", |
5152 | buf1, buf2); |
5153 | if (ret != 2) |
5154 | ferr(po, "parse failure for jmp '%s'\n", |
5155 | po->operand[0].name); |
5156 | fprintf(fout, " goto *jt_%s[%s];", buf1, buf2); |
5157 | break; |
5158 | } |
5159 | else if (po->operand[0].type != OPT_LABEL) |
5160 | ferr(po, "unhandled jmp type\n"); |
87bf6cec |
5161 | |
850c9265 |
5162 | fprintf(fout, " goto %s;", po->operand[0].name); |
91977a1c |
5163 | break; |
5164 | |
5165 | case OP_CALL: |
5101a5f9 |
5166 | assert_operand_cnt(1); |
092f64e1 |
5167 | pp = po->pp; |
89ff3147 |
5168 | my_assert_not(pp, NULL); |
91977a1c |
5169 | |
092f64e1 |
5170 | strcpy(buf3, " "); |
5171 | if (po->flags & OPF_CC) { |
5172 | // we treat conditional branch to another func |
5173 | // (yes such code exists..) as conditional tailcall |
5174 | strcat(buf3, " "); |
5175 | fprintf(fout, " {\n"); |
5176 | } |
5177 | |
8eb12e72 |
5178 | if (pp->is_fptr && !pp->is_arg) { |
092f64e1 |
5179 | fprintf(fout, "%s%s = %s;\n", buf3, pp->name, |
1cd4a663 |
5180 | out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], |
5181 | "(void *)", 0)); |
8eb12e72 |
5182 | if (pp->is_unresolved) |
5183 | fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n", |
5184 | buf3, asmfn, po->asmln, pp->name); |
5185 | } |
1bafb621 |
5186 | |
092f64e1 |
5187 | fprintf(fout, "%s", buf3); |
2b43685d |
5188 | if (strstr(pp->ret_type.name, "int64")) { |
87bf6cec |
5189 | if (po->flags & OPF_TAIL) |
2b43685d |
5190 | ferr(po, "int64 and tail?\n"); |
2fe80fdb |
5191 | fprintf(fout, "tmp64 = "); |
2b43685d |
5192 | } |
5193 | else if (!IS(pp->ret_type.name, "void")) { |
5194 | if (po->flags & OPF_TAIL) { |
840257f6 |
5195 | if (!IS(g_func_pp->ret_type.name, "void")) { |
5196 | fprintf(fout, "return "); |
5197 | if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr) |
5198 | fprintf(fout, "(%s)", g_func_pp->ret_type.name); |
5199 | } |
2b43685d |
5200 | } |
75ad0378 |
5201 | else if (regmask & (1 << xAX)) { |
87bf6cec |
5202 | fprintf(fout, "eax = "); |
2b43685d |
5203 | if (pp->ret_type.is_ptr) |
5204 | fprintf(fout, "(u32)"); |
5205 | } |
91977a1c |
5206 | } |
87bf6cec |
5207 | |
ddaf8bd7 |
5208 | if (pp->name[0] == 0) |
5209 | ferr(po, "missing pp->name\n"); |
5210 | fprintf(fout, "%s%s(", pp->name, |
5211 | pp->has_structarg ? "_sa" : ""); |
39b168b8 |
5212 | |
2fe80fdb |
5213 | if (po->flags & OPF_ATAIL) { |
5214 | if (pp->argc_stack != g_func_pp->argc_stack |
5215 | || (pp->argc_stack > 0 |
5216 | && pp->is_stdcall != g_func_pp->is_stdcall)) |
5217 | ferr(po, "incompatible tailcall\n"); |
1f84f6b3 |
5218 | if (g_func_pp->has_retreg) |
5219 | ferr(po, "TODO: retreg+tailcall\n"); |
87bf6cec |
5220 | |
2fe80fdb |
5221 | for (arg = j = 0; arg < pp->argc; arg++) { |
5222 | if (arg > 0) |
5223 | fprintf(fout, ", "); |
87bf6cec |
5224 | |
2fe80fdb |
5225 | cast[0] = 0; |
5226 | if (pp->arg[arg].type.is_ptr) |
5227 | snprintf(cast, sizeof(cast), "(%s)", |
5228 | pp->arg[arg].type.name); |
91977a1c |
5229 | |
2fe80fdb |
5230 | if (pp->arg[arg].reg != NULL) { |
5231 | fprintf(fout, "%s%s", cast, pp->arg[arg].reg); |
5232 | continue; |
5233 | } |
5234 | // stack arg |
5235 | for (; j < g_func_pp->argc; j++) |
5236 | if (g_func_pp->arg[j].reg == NULL) |
5237 | break; |
5238 | fprintf(fout, "%sa%d", cast, j + 1); |
5239 | j++; |
69a3cdfc |
5240 | } |
2fe80fdb |
5241 | } |
5242 | else { |
5243 | for (arg = 0; arg < pp->argc; arg++) { |
5244 | if (arg > 0) |
5245 | fprintf(fout, ", "); |
5246 | |
5247 | cast[0] = 0; |
5248 | if (pp->arg[arg].type.is_ptr) |
5249 | snprintf(cast, sizeof(cast), "(%s)", |
5250 | pp->arg[arg].type.name); |
5251 | |
5252 | if (pp->arg[arg].reg != NULL) { |
1f84f6b3 |
5253 | if (pp->arg[arg].type.is_retreg) |
5254 | fprintf(fout, "&%s", pp->arg[arg].reg); |
5255 | else |
5256 | fprintf(fout, "%s%s", cast, pp->arg[arg].reg); |
2fe80fdb |
5257 | continue; |
5258 | } |
5259 | |
5260 | // stack arg |
5261 | tmp_op = pp->arg[arg].datap; |
5262 | if (tmp_op == NULL) |
5263 | ferr(po, "parsed_op missing for arg%d\n", arg); |
23fd0b11 |
5264 | |
5265 | if (tmp_op->flags & OPF_VAPUSH) { |
5266 | fprintf(fout, "ap"); |
5267 | } |
5f70a34f |
5268 | else if (tmp_op->p_argpass != 0) { |
5269 | fprintf(fout, "a%d", tmp_op->p_argpass); |
5270 | } |
5271 | else if (tmp_op->p_argnum != 0) { |
3a5101d7 |
5272 | fprintf(fout, "%s%s", cast, |
5273 | saved_arg_name(buf1, sizeof(buf1), |
5274 | tmp_op->p_arggrp, tmp_op->p_argnum)); |
2fe80fdb |
5275 | } |
5276 | else { |
5277 | fprintf(fout, "%s", |
5278 | out_src_opr(buf1, sizeof(buf1), |
5279 | tmp_op, &tmp_op->operand[0], cast, 0)); |
5280 | } |
69a3cdfc |
5281 | } |
91977a1c |
5282 | } |
5283 | fprintf(fout, ");"); |
87bf6cec |
5284 | |
2b43685d |
5285 | if (strstr(pp->ret_type.name, "int64")) { |
5286 | fprintf(fout, "\n"); |
092f64e1 |
5287 | fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3); |
5288 | fprintf(fout, "%seax = tmp64;", buf3); |
2b43685d |
5289 | } |
5290 | |
89ff3147 |
5291 | if (pp->is_unresolved) { |
8eb12e72 |
5292 | snprintf(buf2, sizeof(buf2), " unresolved %dreg", |
89ff3147 |
5293 | pp->argc_reg); |
092f64e1 |
5294 | strcat(g_comment, buf2); |
89ff3147 |
5295 | } |
5296 | |
87bf6cec |
5297 | if (po->flags & OPF_TAIL) { |
840257f6 |
5298 | ret = 0; |
ddaf8bd7 |
5299 | if (i == opcnt - 1 || pp->is_noreturn) |
840257f6 |
5300 | ret = 0; |
5301 | else if (IS(pp->ret_type.name, "void")) |
5302 | ret = 1; |
5303 | else if (IS(g_func_pp->ret_type.name, "void")) |
5304 | ret = 1; |
5305 | // else already handled as 'return f()' |
5306 | |
5307 | if (ret) { |
da87ae38 |
5308 | if (!IS(g_func_pp->ret_type.name, "void")) { |
ddaf8bd7 |
5309 | ferr(po, "int func -> void func tailcall?\n"); |
da87ae38 |
5310 | } |
5311 | else { |
092f64e1 |
5312 | fprintf(fout, "\n%sreturn;", buf3); |
ddaf8bd7 |
5313 | strcat(g_comment, " ^ tailcall"); |
da87ae38 |
5314 | } |
3ebea2cf |
5315 | } |
89ff3147 |
5316 | else |
ddaf8bd7 |
5317 | strcat(g_comment, " tailcall"); |
87bf6cec |
5318 | } |
ddaf8bd7 |
5319 | if (pp->is_noreturn) |
5320 | strcat(g_comment, " noreturn"); |
2fe80fdb |
5321 | if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0) |
5322 | strcat(g_comment, " argframe"); |
092f64e1 |
5323 | if (po->flags & OPF_CC) |
5324 | strcat(g_comment, " cond"); |
5325 | |
5326 | if (po->flags & OPF_CC) |
5327 | fprintf(fout, "\n }"); |
5328 | |
87bf6cec |
5329 | delayed_flag_op = NULL; |
5330 | last_arith_dst = NULL; |
91977a1c |
5331 | break; |
5332 | |
5333 | case OP_RET: |
bd96f656 |
5334 | if (g_func_pp->is_vararg) |
4f12f671 |
5335 | fprintf(fout, " va_end(ap);\n"); |
1f84f6b3 |
5336 | if (g_func_pp->has_retreg) { |
5337 | for (arg = 0; arg < g_func_pp->argc; arg++) |
5338 | if (g_func_pp->arg[arg].type.is_retreg) |
5339 | fprintf(fout, " *r_%s = %s;\n", |
5340 | g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg); |
5341 | } |
4f12f671 |
5342 | |
bd96f656 |
5343 | if (IS(g_func_pp->ret_type.name, "void")) { |
3ebea2cf |
5344 | if (i != opcnt - 1 || label_pending) |
5345 | fprintf(fout, " return;"); |
5346 | } |
bd96f656 |
5347 | else if (g_func_pp->ret_type.is_ptr) { |
d4e3b5db |
5348 | fprintf(fout, " return (%s)eax;", |
bd96f656 |
5349 | g_func_pp->ret_type.name); |
3ebea2cf |
5350 | } |
2fe80fdb |
5351 | else if (IS(g_func_pp->ret_type.name, "__int64")) |
5352 | fprintf(fout, " return ((u64)edx << 32) | eax;"); |
91977a1c |
5353 | else |
5354 | fprintf(fout, " return eax;"); |
de50b98b |
5355 | |
5356 | last_arith_dst = NULL; |
5357 | delayed_flag_op = NULL; |
91977a1c |
5358 | break; |
5359 | |
5360 | case OP_PUSH: |
1f84f6b3 |
5361 | out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]); |
5f70a34f |
5362 | if (po->p_argnum != 0) { |
69a3cdfc |
5363 | // special case - saved func arg |
3a5101d7 |
5364 | fprintf(fout, " %s = %s;", |
5365 | saved_arg_name(buf2, sizeof(buf2), |
5366 | po->p_arggrp, po->p_argnum), buf1); |
69a3cdfc |
5367 | break; |
5368 | } |
d4e3b5db |
5369 | else if (po->flags & OPF_RSAVE) { |
d4e3b5db |
5370 | fprintf(fout, " s_%s = %s;", buf1, buf1); |
5371 | break; |
5372 | } |
1f84f6b3 |
5373 | else if (g_func_pp->is_userstack) { |
5374 | fprintf(fout, " *(--esp) = %s;", buf1); |
5375 | break; |
5376 | } |
e56ab892 |
5377 | if (!(g_ida_func_attr & IDAFA_NORETURN)) |
5378 | ferr(po, "stray push encountered\n"); |
5379 | no_output = 1; |
91977a1c |
5380 | break; |
5381 | |
5382 | case OP_POP: |
d4e3b5db |
5383 | if (po->flags & OPF_RSAVE) { |
3ebea2cf |
5384 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
d4e3b5db |
5385 | fprintf(fout, " %s = s_%s;", buf1, buf1); |
5386 | break; |
5387 | } |
5c024ef7 |
5388 | else if (po->datap != NULL) { |
5389 | // push/pop pair |
5390 | tmp_op = po->datap; |
5391 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); |
5392 | fprintf(fout, " %s = %s;", buf1, |
5393 | out_src_opr(buf2, sizeof(buf2), |
5394 | tmp_op, &tmp_op->operand[0], |
c7ed83dd |
5395 | default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0)); |
5c024ef7 |
5396 | break; |
5397 | } |
1f84f6b3 |
5398 | else if (g_func_pp->is_userstack) { |
5399 | fprintf(fout, " %s = *esp++;", |
5400 | out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0])); |
5401 | break; |
5402 | } |
5403 | else |
5404 | ferr(po, "stray pop encountered\n"); |
91977a1c |
5405 | break; |
5406 | |
33c35af6 |
5407 | case OP_NOP: |
2b43685d |
5408 | no_output = 1; |
33c35af6 |
5409 | break; |
5410 | |
90307a99 |
5411 | // mmx |
5412 | case OP_EMMS: |
5413 | strcpy(g_comment, "(emms)"); |
5414 | break; |
5415 | |
91977a1c |
5416 | default: |
5417 | no_output = 1; |
69a3cdfc |
5418 | ferr(po, "unhandled op type %d, flags %x\n", |
5419 | po->op, po->flags); |
91977a1c |
5420 | break; |
5421 | } |
5422 | |
5423 | if (g_comment[0] != 0) { |
ddaf8bd7 |
5424 | char *p = g_comment; |
5425 | while (my_isblank(*p)) |
5426 | p++; |
5427 | fprintf(fout, " // %s", p); |
91977a1c |
5428 | g_comment[0] = 0; |
5429 | no_output = 0; |
5430 | } |
5431 | if (!no_output) |
5432 | fprintf(fout, "\n"); |
5101a5f9 |
5433 | |
2b43685d |
5434 | // some sanity checking |
591721d7 |
5435 | if (po->flags & OPF_REP) { |
5436 | if (po->op != OP_STOS && po->op != OP_MOVS |
5437 | && po->op != OP_CMPS && po->op != OP_SCAS) |
5438 | ferr(po, "unexpected rep\n"); |
5439 | if (!(po->flags & (OPF_REPZ|OPF_REPNZ)) |
5440 | && (po->op == OP_CMPS || po->op == OP_SCAS)) |
5441 | ferr(po, "cmps/scas with plain rep\n"); |
5442 | } |
5443 | if ((po->flags & (OPF_REPZ|OPF_REPNZ)) |
5444 | && po->op != OP_CMPS && po->op != OP_SCAS) |
2b43685d |
5445 | ferr(po, "unexpected repz/repnz\n"); |
5446 | |
940e8e66 |
5447 | if (pfomask != 0) |
7ba45c34 |
5448 | ferr(po, "missed flag calc, pfomask=%x\n", pfomask); |
940e8e66 |
5449 | |
5101a5f9 |
5450 | // see is delayed flag stuff is still valid |
5451 | if (delayed_flag_op != NULL && delayed_flag_op != po) { |
89ff3147 |
5452 | if (is_any_opr_modified(delayed_flag_op, po, 0)) |
5101a5f9 |
5453 | delayed_flag_op = NULL; |
5454 | } |
5455 | |
5456 | if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) { |
5457 | if (is_opr_modified(last_arith_dst, po)) |
5458 | last_arith_dst = NULL; |
5459 | } |
3ebea2cf |
5460 | |
5461 | label_pending = 0; |
91977a1c |
5462 | } |
5463 | |
a2c1d768 |
5464 | if (g_stack_fsz && !g_stack_frame_used) |
5465 | fprintf(fout, " (void)sf;\n"); |
5466 | |
91977a1c |
5467 | fprintf(fout, "}\n\n"); |
5468 | |
9af2d373 |
5469 | gen_x_cleanup(opcnt); |
5470 | } |
5471 | |
5472 | static void gen_x_cleanup(int opcnt) |
5473 | { |
5474 | int i; |
5475 | |
91977a1c |
5476 | for (i = 0; i < opcnt; i++) { |
4c45fa73 |
5477 | struct label_ref *lr, *lr_del; |
5478 | |
5479 | lr = g_label_refs[i].next; |
5480 | while (lr != NULL) { |
5481 | lr_del = lr; |
5482 | lr = lr->next; |
5483 | free(lr_del); |
5484 | } |
5485 | g_label_refs[i].i = -1; |
5486 | g_label_refs[i].next = NULL; |
5487 | |
91977a1c |
5488 | if (ops[i].op == OP_CALL) { |
092f64e1 |
5489 | if (ops[i].pp) |
5490 | proto_release(ops[i].pp); |
91977a1c |
5491 | } |
5492 | } |
bd96f656 |
5493 | g_func_pp = NULL; |
91977a1c |
5494 | } |
c36e914d |
5495 | |
92d715b6 |
5496 | struct func_proto_dep; |
5497 | |
5498 | struct func_prototype { |
5499 | char name[NAMELEN]; |
5500 | int id; |
5501 | int argc_stack; |
5502 | int regmask_dep; |
91ca764a |
5503 | int has_ret:3; // -1, 0, 1: unresolved, no, yes |
92d715b6 |
5504 | unsigned int dep_resolved:1; |
5505 | unsigned int is_stdcall:1; |
5506 | struct func_proto_dep *dep_func; |
5507 | int dep_func_cnt; |
91ca764a |
5508 | const struct parsed_proto *pp; // seed pp, if any |
92d715b6 |
5509 | }; |
5510 | |
5511 | struct func_proto_dep { |
5512 | char *name; |
5513 | struct func_prototype *proto; |
5514 | int regmask_live; // .. at the time of call |
5515 | unsigned int ret_dep:1; // return from this is caller's return |
5516 | }; |
5517 | |
5518 | static struct func_prototype *hg_fp; |
5519 | static int hg_fp_cnt; |
5520 | |
5fa1256f |
5521 | static struct scanned_var { |
5522 | char name[NAMELEN]; |
5523 | enum opr_lenmod lmod; |
5524 | unsigned int is_seeded:1; |
61e29183 |
5525 | unsigned int is_c_str:1; |
c0de9015 |
5526 | const struct parsed_proto *pp; // seed pp, if any |
5fa1256f |
5527 | } *hg_vars; |
5528 | static int hg_var_cnt; |
5529 | |
9af2d373 |
5530 | static void output_hdr_fp(FILE *fout, const struct func_prototype *fp, |
5531 | int count); |
5532 | |
92d715b6 |
5533 | static struct func_proto_dep *hg_fp_find_dep(struct func_prototype *fp, |
5534 | const char *name) |
5535 | { |
5536 | int i; |
5537 | |
5538 | for (i = 0; i < fp->dep_func_cnt; i++) |
5539 | if (IS(fp->dep_func[i].name, name)) |
5540 | return &fp->dep_func[i]; |
5541 | |
5542 | return NULL; |
5543 | } |
5544 | |
5545 | static void hg_fp_add_dep(struct func_prototype *fp, const char *name) |
5546 | { |
5547 | // is it a dupe? |
5548 | if (hg_fp_find_dep(fp, name)) |
5549 | return; |
5550 | |
5551 | if ((fp->dep_func_cnt & 0xff) == 0) { |
5552 | fp->dep_func = realloc(fp->dep_func, |
5553 | sizeof(fp->dep_func[0]) * (fp->dep_func_cnt + 0x100)); |
5554 | my_assert_not(fp->dep_func, NULL); |
5555 | memset(&fp->dep_func[fp->dep_func_cnt], 0, |
5556 | sizeof(fp->dep_func[0]) * 0x100); |
5557 | } |
5558 | fp->dep_func[fp->dep_func_cnt].name = strdup(name); |
5559 | fp->dep_func_cnt++; |
5560 | } |
5561 | |
5562 | static int hg_fp_cmp_name(const void *p1_, const void *p2_) |
5563 | { |
5564 | const struct func_prototype *p1 = p1_, *p2 = p2_; |
5565 | return strcmp(p1->name, p2->name); |
5566 | } |
5567 | |
5568 | #if 0 |
5569 | static int hg_fp_cmp_id(const void *p1_, const void *p2_) |
5570 | { |
5571 | const struct func_prototype *p1 = p1_, *p2 = p2_; |
5572 | return p1->id - p2->id; |
5573 | } |
5574 | #endif |
5575 | |
91ca764a |
5576 | // recursive register dep pass |
5577 | // - track saved regs (part 2) |
5578 | // - try to figure out arg-regs |
5579 | // - calculate reg deps |
5580 | static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits, |
5581 | struct func_prototype *fp, int regmask_save, int regmask_dst, |
5582 | int *regmask_dep, int *has_ret) |
5583 | { |
5584 | struct func_proto_dep *dep; |
5585 | struct parsed_op *po; |
5586 | int from_caller = 0; |
5587 | int depth; |
5588 | int j, l; |
5589 | int reg; |
5590 | int ret; |
5591 | |
5592 | for (; i < opcnt; i++) |
5593 | { |
5594 | if (cbits[i >> 3] & (1 << (i & 7))) |
5595 | return; |
5596 | cbits[i >> 3] |= (1 << (i & 7)); |
5597 | |
5598 | po = &ops[i]; |
5599 | |
5600 | if ((po->flags & OPF_JMP) && po->op != OP_CALL) { |
5601 | if (po->btj != NULL) { |
5602 | // jumptable |
5603 | for (j = 0; j < po->btj->count; j++) { |
5604 | gen_hdr_dep_pass(po->btj->d[j].bt_i, opcnt, cbits, fp, |
5605 | regmask_save, regmask_dst, regmask_dep, has_ret); |
5606 | } |
5607 | return; |
5608 | } |
5609 | |
5610 | if (po->bt_i < 0) { |
5611 | ferr(po, "dead branch\n"); |
5612 | return; |
5613 | } |
5614 | |
5615 | if (po->flags & OPF_CJMP) { |
5616 | gen_hdr_dep_pass(po->bt_i, opcnt, cbits, fp, |
5617 | regmask_save, regmask_dst, regmask_dep, has_ret); |
5618 | } |
5619 | else { |
5620 | i = po->bt_i - 1; |
5621 | } |
5622 | continue; |
5623 | } |
5624 | |
5625 | if (po->flags & OPF_FARG) |
5626 | /* (just calculate register deps) */; |
5627 | else if (po->op == OP_PUSH && po->operand[0].type == OPT_REG) |
5628 | { |
5629 | reg = po->operand[0].reg; |
5630 | if (reg < 0) |
5631 | ferr(po, "reg not set for push?\n"); |
5632 | |
5633 | if (po->flags & OPF_RSAVE) { |
5634 | regmask_save |= 1 << reg; |
5635 | continue; |
5636 | } |
5637 | if (po->flags & OPF_DONE) |
5638 | continue; |
5639 | |
5640 | depth = 0; |
5641 | ret = scan_for_pop(i + 1, opcnt, |
5642 | po->operand[0].name, i + opcnt * 2, 0, &depth, 0); |
5643 | if (ret == 1) { |
5644 | regmask_save |= 1 << reg; |
5645 | po->flags |= OPF_RMD; |
5646 | scan_for_pop(i + 1, opcnt, |
5647 | po->operand[0].name, i + opcnt * 3, 0, &depth, 1); |
5648 | continue; |
5649 | } |
5650 | } |
5651 | else if (po->flags & OPF_RMD) |
5652 | continue; |
5653 | else if (po->op == OP_CALL) { |
5654 | po->regmask_dst |= 1 << xAX; |
5655 | |
5656 | dep = hg_fp_find_dep(fp, po->operand[0].name); |
5657 | if (dep != NULL) |
5658 | dep->regmask_live = regmask_save | regmask_dst; |
5659 | } |
5660 | else if (po->op == OP_RET) { |
5661 | if (po->operand_cnt > 0) { |
5662 | fp->is_stdcall = 1; |
5663 | if (fp->argc_stack >= 0 |
5664 | && fp->argc_stack != po->operand[0].val / 4) |
5665 | ferr(po, "ret mismatch? (%d)\n", fp->argc_stack * 4); |
5666 | fp->argc_stack = po->operand[0].val / 4; |
5667 | } |
5668 | } |
5669 | |
5670 | if (*has_ret != 0 && (po->flags & OPF_TAIL)) { |
5671 | if (po->op == OP_CALL) { |
5672 | j = i; |
5673 | ret = 1; |
5674 | } |
5675 | else { |
5676 | struct parsed_opr opr = { 0, }; |
5677 | opr.type = OPT_REG; |
5678 | opr.reg = xAX; |
5679 | j = -1; |
5680 | from_caller = 0; |
5681 | ret = resolve_origin(i, &opr, i + opcnt * 4, &j, &from_caller); |
5682 | } |
5683 | |
5684 | if (ret == -1 && from_caller) { |
5685 | // unresolved eax - probably void func |
5686 | *has_ret = 0; |
5687 | } |
5688 | else { |
5689 | if (ops[j].op == OP_CALL) { |
5690 | dep = hg_fp_find_dep(fp, po->operand[0].name); |
5691 | if (dep != NULL) |
5692 | dep->ret_dep = 1; |
5693 | else |
5694 | *has_ret = 1; |
5695 | } |
5696 | else |
5697 | *has_ret = 1; |
5698 | } |
5699 | } |
5700 | |
5701 | l = regmask_save | regmask_dst; |
5702 | if (g_bp_frame && !(po->flags & OPF_EBP_S)) |
5703 | l |= 1 << xBP; |
5704 | |
5705 | l = po->regmask_src & ~l; |
5706 | #if 0 |
5707 | if (l) |
5708 | fnote(po, "dep |= %04x, dst %04x, save %04x (f %x)\n", |
5709 | l, regmask_dst, regmask_save, po->flags); |
5710 | #endif |
5711 | *regmask_dep |= l; |
5712 | regmask_dst |= po->regmask_dst; |
5713 | |
5714 | if (po->flags & OPF_TAIL) |
5715 | return; |
5716 | } |
5717 | } |
5718 | |
92d715b6 |
5719 | static void gen_hdr(const char *funcn, int opcnt) |
5720 | { |
26677139 |
5721 | int save_arg_vars[MAX_ARG_GRP] = { 0, }; |
91ca764a |
5722 | unsigned char cbits[MAX_OPS / 8]; |
9af2d373 |
5723 | const struct parsed_proto *pp_c; |
5724 | struct parsed_proto *pp; |
92d715b6 |
5725 | struct func_prototype *fp; |
92d715b6 |
5726 | struct parsed_data *pd; |
5727 | struct parsed_op *po; |
9af2d373 |
5728 | const char *tmpname; |
26677139 |
5729 | int regmask_dummy = 0; |
91ca764a |
5730 | int regmask_dep; |
92d715b6 |
5731 | int max_bp_offset = 0; |
91ca764a |
5732 | int has_ret; |
92d715b6 |
5733 | int i, j, l, ret; |
92d715b6 |
5734 | |
5735 | if ((hg_fp_cnt & 0xff) == 0) { |
5736 | hg_fp = realloc(hg_fp, sizeof(hg_fp[0]) * (hg_fp_cnt + 0x100)); |
5737 | my_assert_not(hg_fp, NULL); |
5738 | memset(hg_fp + hg_fp_cnt, 0, sizeof(hg_fp[0]) * 0x100); |
5739 | } |
5740 | |
5741 | fp = &hg_fp[hg_fp_cnt]; |
5742 | snprintf(fp->name, sizeof(fp->name), "%s", funcn); |
5743 | fp->id = hg_fp_cnt; |
9af2d373 |
5744 | fp->argc_stack = -1; |
92d715b6 |
5745 | hg_fp_cnt++; |
5746 | |
9af2d373 |
5747 | // perhaps already in seed header? |
91ca764a |
5748 | fp->pp = proto_parse(g_fhdr, funcn, 1); |
5749 | if (fp->pp != NULL) { |
5750 | fp->argc_stack = fp->pp->argc_stack; |
5751 | fp->is_stdcall = fp->pp->is_stdcall; |
5752 | fp->regmask_dep = get_pp_arg_regmask(fp->pp); |
5753 | fp->has_ret = !IS(fp->pp->ret_type.name, "void"); |
9af2d373 |
5754 | return; |
5755 | } |
5756 | |
5757 | g_bp_frame = g_sp_frame = g_stack_fsz = 0; |
5758 | g_stack_frame_used = 0; |
5759 | |
92d715b6 |
5760 | // pass1: |
9af2d373 |
5761 | // - handle ebp/esp frame, remove ops related to it |
5762 | scan_prologue_epilogue(opcnt); |
5763 | |
5764 | // pass2: |
92d715b6 |
5765 | // - collect calls |
5766 | // - resolve all branches |
5767 | for (i = 0; i < opcnt; i++) |
5768 | { |
5769 | po = &ops[i]; |
5770 | po->bt_i = -1; |
5771 | po->btj = NULL; |
5772 | |
5e49b270 |
5773 | if (po->flags & (OPF_RMD|OPF_DONE)) |
92d715b6 |
5774 | continue; |
5775 | |
5776 | if (po->op == OP_CALL) { |
9af2d373 |
5777 | tmpname = opr_name(po, 0); |
5778 | pp = NULL; |
5779 | if (po->operand[0].type == OPT_LABEL) { |
5780 | hg_fp_add_dep(fp, tmpname); |
5781 | |
5782 | // perhaps a call to already known func? |
5783 | pp_c = proto_parse(g_fhdr, tmpname, 1); |
5784 | if (pp_c != NULL) |
5785 | pp = proto_clone(pp_c); |
5786 | } |
5787 | else if (po->datap != NULL) { |
5788 | pp = calloc(1, sizeof(*pp)); |
5789 | my_assert_not(pp, NULL); |
92d715b6 |
5790 | |
9af2d373 |
5791 | ret = parse_protostr(po->datap, pp); |
5792 | if (ret < 0) |
5793 | ferr(po, "bad protostr supplied: %s\n", (char *)po->datap); |
5794 | free(po->datap); |
5795 | po->datap = NULL; |
5796 | } |
5797 | if (pp != NULL && pp->is_noreturn) |
5798 | po->flags |= OPF_TAIL; |
5799 | |
5800 | po->pp = pp; |
92d715b6 |
5801 | continue; |
5802 | } |
5803 | |
5804 | if (!(po->flags & OPF_JMP) || po->op == OP_RET) |
5805 | continue; |
5806 | |
5807 | if (po->operand[0].type == OPT_REGMEM) { |
5808 | pd = try_resolve_jumptab(i, opcnt); |
5809 | if (pd == NULL) |
5810 | goto tailcall; |
5811 | |
5812 | po->btj = pd; |
5813 | continue; |
5814 | } |
5815 | |
5816 | for (l = 0; l < opcnt; l++) { |
d7857c3a |
5817 | if (g_labels[l] != NULL |
5818 | && IS(po->operand[0].name, g_labels[l])) |
5819 | { |
92d715b6 |
5820 | add_label_ref(&g_label_refs[l], i); |
5821 | po->bt_i = l; |
5822 | break; |
5823 | } |
5824 | } |
5825 | |
5826 | if (po->bt_i != -1 || (po->flags & OPF_RMD)) |
5827 | continue; |
5828 | |
5829 | if (po->operand[0].type == OPT_LABEL) |
5830 | // assume tail call |
5831 | goto tailcall; |
5832 | |
5833 | ferr(po, "unhandled branch\n"); |
5834 | |
5835 | tailcall: |
5836 | po->op = OP_CALL; |
5837 | po->flags |= OPF_TAIL; |
5838 | if (i > 0 && ops[i - 1].op == OP_POP) |
5839 | po->flags |= OPF_ATAIL; |
5840 | i--; // reprocess |
5841 | } |
5842 | |
9af2d373 |
5843 | // pass3: |
92d715b6 |
5844 | // - remove dead labels |
9af2d373 |
5845 | // - handle push <const>/pop pairs |
92d715b6 |
5846 | for (i = 0; i < opcnt; i++) |
5847 | { |
d7857c3a |
5848 | if (g_labels[i] != NULL && g_label_refs[i].i == -1) { |
5849 | free(g_labels[i]); |
5850 | g_labels[i] = NULL; |
5851 | } |
9af2d373 |
5852 | |
91ca764a |
5853 | po = &ops[i]; |
5854 | if (po->flags & (OPF_RMD|OPF_DONE)) |
5855 | continue; |
5856 | |
5857 | if (po->op == OP_PUSH && po->operand[0].type == OPT_CONST) |
5858 | scan_for_pop_const(i, opcnt); |
5859 | } |
5860 | |
5861 | // pass4: |
5862 | // - process trivial calls |
5863 | for (i = 0; i < opcnt; i++) |
5864 | { |
9af2d373 |
5865 | po = &ops[i]; |
5e49b270 |
5866 | if (po->flags & (OPF_RMD|OPF_DONE)) |
9af2d373 |
5867 | continue; |
5868 | |
26677139 |
5869 | if (po->op == OP_CALL) |
5870 | { |
5871 | pp = process_call_early(i, opcnt, &j); |
5872 | if (pp != NULL) { |
5873 | if (!(po->flags & OPF_ATAIL)) |
5874 | // since we know the args, try to collect them |
5875 | if (collect_call_args_early(po, i, pp, ®mask_dummy) != 0) |
5876 | pp = NULL; |
5877 | } |
5878 | |
5879 | if (pp != NULL) { |
5880 | if (j >= 0) { |
5881 | // commit esp adjust |
5882 | ops[j].flags |= OPF_RMD; |
5e49b270 |
5883 | if (ops[j].op != OP_POP) |
5884 | patch_esp_adjust(&ops[j], pp->argc_stack * 4); |
5885 | else |
5886 | ops[j].flags |= OPF_DONE; |
26677139 |
5887 | } |
5888 | |
5889 | po->flags |= OPF_DONE; |
5890 | } |
5891 | } |
26677139 |
5892 | } |
5893 | |
91ca764a |
5894 | // pass5: |
5e49b270 |
5895 | // - track saved regs (simple) |
26677139 |
5896 | // - process calls |
5897 | for (i = 0; i < opcnt; i++) |
5898 | { |
5899 | po = &ops[i]; |
5e49b270 |
5900 | if (po->flags & (OPF_RMD|OPF_DONE)) |
26677139 |
5901 | continue; |
5902 | |
5e49b270 |
5903 | if (po->op == OP_PUSH && po->operand[0].type == OPT_REG) |
5904 | { |
5905 | ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0); |
5906 | if (ret == 0) { |
91ca764a |
5907 | // regmask_save |= 1 << po->operand[0].reg; // do it later |
5908 | po->flags |= OPF_RSAVE | OPF_RMD | OPF_DONE; |
5e49b270 |
5909 | scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, OPF_RMD); |
5910 | } |
5911 | } |
5912 | else if (po->op == OP_CALL && !(po->flags & OPF_DONE)) |
26677139 |
5913 | { |
9af2d373 |
5914 | pp = process_call(i, opcnt); |
5915 | |
5916 | if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) { |
9af2d373 |
5917 | // since we know the args, collect them |
91ca764a |
5918 | ret = collect_call_args(po, i, pp, ®mask_dummy, save_arg_vars, |
5e49b270 |
5919 | i + opcnt * 1); |
9af2d373 |
5920 | } |
5921 | } |
92d715b6 |
5922 | } |
5923 | |
91ca764a |
5924 | // pass6 |
5925 | memset(cbits, 0, sizeof(cbits)); |
5926 | regmask_dep = 0; |
5927 | has_ret = -1; |
5928 | |
5929 | gen_hdr_dep_pass(0, opcnt, cbits, fp, 0, 0, ®mask_dep, &has_ret); |
5930 | |
5931 | // find unreachable code - must be fixed in IDA |
92d715b6 |
5932 | for (i = 0; i < opcnt; i++) |
5933 | { |
91ca764a |
5934 | if (cbits[i >> 3] & (1 << (i & 7))) |
9af2d373 |
5935 | continue; |
92d715b6 |
5936 | |
91ca764a |
5937 | if (ops[i].op != OP_NOP) |
5938 | ferr(&ops[i], "unreachable code\n"); |
92d715b6 |
5939 | } |
5940 | |
5941 | if (has_ret == -1 && (regmask_dep & (1 << xAX))) |
5942 | has_ret = 1; |
5943 | |
9af2d373 |
5944 | for (i = 0; i < g_eqcnt; i++) { |
92d715b6 |
5945 | if (g_eqs[i].offset > max_bp_offset && g_eqs[i].offset < 4*32) |
5946 | max_bp_offset = g_eqs[i].offset; |
9af2d373 |
5947 | } |
92d715b6 |
5948 | |
9af2d373 |
5949 | if (fp->argc_stack < 0) { |
92d715b6 |
5950 | max_bp_offset = (max_bp_offset + 3) & ~3; |
9af2d373 |
5951 | fp->argc_stack = max_bp_offset / 4; |
5952 | if ((g_ida_func_attr & IDAFA_BP_FRAME) && fp->argc_stack > 0) |
92d715b6 |
5953 | fp->argc_stack--; |
5954 | } |
5955 | |
5956 | fp->regmask_dep = regmask_dep & ~(1 << xSP); |
5957 | fp->has_ret = has_ret; |
91ca764a |
5958 | #if 0 |
5959 | printf("// has_ret %d, regmask_dep %x\n", |
5960 | fp->has_ret, fp->regmask_dep); |
5961 | output_hdr_fp(stdout, fp, 1); |
5962 | if (IS(funcn, "sub_100073FD")) exit(1); |
5963 | #endif |
9af2d373 |
5964 | |
5965 | gen_x_cleanup(opcnt); |
92d715b6 |
5966 | } |
5967 | |
5968 | static void hg_fp_resolve_deps(struct func_prototype *fp) |
5969 | { |
5970 | struct func_prototype fp_s; |
91ca764a |
5971 | int dep; |
92d715b6 |
5972 | int i; |
5973 | |
5974 | // this thing is recursive, so mark first.. |
5975 | fp->dep_resolved = 1; |
5976 | |
5977 | for (i = 0; i < fp->dep_func_cnt; i++) { |
5978 | strcpy(fp_s.name, fp->dep_func[i].name); |
5979 | fp->dep_func[i].proto = bsearch(&fp_s, hg_fp, hg_fp_cnt, |
5980 | sizeof(hg_fp[0]), hg_fp_cmp_name); |
5981 | if (fp->dep_func[i].proto != NULL) { |
5982 | if (!fp->dep_func[i].proto->dep_resolved) |
5983 | hg_fp_resolve_deps(fp->dep_func[i].proto); |
5984 | |
91ca764a |
5985 | dep = ~fp->dep_func[i].regmask_live |
5986 | & fp->dep_func[i].proto->regmask_dep; |
5987 | fp->regmask_dep |= dep; |
5988 | // printf("dep %s %s |= %x\n", fp->name, |
5989 | // fp->dep_func[i].name, dep); |
92d715b6 |
5990 | |
5991 | if (fp->has_ret == -1) |
5992 | fp->has_ret = fp->dep_func[i].proto->has_ret; |
5993 | } |
5994 | } |
5995 | } |
5996 | |
9af2d373 |
5997 | static void output_hdr_fp(FILE *fout, const struct func_prototype *fp, |
5998 | int count) |
92d715b6 |
5999 | { |
61e29183 |
6000 | const struct parsed_proto *pp; |
6001 | char *p, namebuf[NAMELEN]; |
6002 | const char *name; |
92d715b6 |
6003 | int regmask_dep; |
6004 | int argc_stack; |
9af2d373 |
6005 | int j, arg; |
92d715b6 |
6006 | |
9af2d373 |
6007 | for (; count > 0; count--, fp++) { |
92d715b6 |
6008 | if (fp->has_ret == -1) |
6009 | fprintf(fout, "// ret unresolved\n"); |
6010 | #if 0 |
6011 | fprintf(fout, "// dep:"); |
6012 | for (j = 0; j < fp->dep_func_cnt; j++) { |
6013 | fprintf(fout, " %s/", fp->dep_func[j].name); |
6014 | if (fp->dep_func[j].proto != NULL) |
6015 | fprintf(fout, "%04x/%d", fp->dep_func[j].proto->regmask_dep, |
6016 | fp->dep_func[j].proto->has_ret); |
6017 | } |
6018 | fprintf(fout, "\n"); |
6019 | #endif |
6020 | |
61e29183 |
6021 | p = strchr(fp->name, '@'); |
6022 | if (p != NULL) { |
6023 | memcpy(namebuf, fp->name, p - fp->name); |
6024 | namebuf[p - fp->name] = 0; |
6025 | name = namebuf; |
6026 | } |
6027 | else |
6028 | name = fp->name; |
6029 | if (name[0] == '_') |
6030 | name++; |
6031 | |
6032 | pp = proto_parse(g_fhdr, name, 1); |
6033 | if (pp != NULL && pp->is_include) |
6034 | continue; |
6035 | |
c0de9015 |
6036 | if (fp->pp != NULL) { |
6037 | // prefer fp for common style, |
6038 | // only use output_pp if args are complex |
6039 | for (j = 0; j < fp->pp->argc; j++) { |
6040 | if (fp->pp->arg[j].fptr != NULL) |
6041 | break; |
6042 | } |
6043 | if (j != fp->pp->argc) { |
6044 | output_pp(fout, fp->pp, OPP_ALIGN); |
6045 | fprintf(fout, ";\n"); |
6046 | continue; |
6047 | } |
6048 | } |
6049 | |
92d715b6 |
6050 | regmask_dep = fp->regmask_dep; |
6051 | argc_stack = fp->argc_stack; |
6052 | |
91ca764a |
6053 | fprintf(fout, "%-5s", fp->pp ? fp->pp->ret_type.name : |
6054 | (fp->has_ret ? "int" : "void")); |
92d715b6 |
6055 | if (regmask_dep && (fp->is_stdcall || argc_stack == 0) |
6056 | && (regmask_dep & ~((1 << xCX) | (1 << xDX))) == 0) |
6057 | { |
9af2d373 |
6058 | fprintf(fout, " __fastcall "); |
92d715b6 |
6059 | if (!(regmask_dep & (1 << xDX)) && argc_stack == 0) |
6060 | argc_stack = 1; |
6061 | else |
6062 | argc_stack += 2; |
6063 | regmask_dep = 0; |
6064 | } |
6065 | else if (regmask_dep && !fp->is_stdcall) { |
6066 | fprintf(fout, "/*__usercall*/ "); |
92d715b6 |
6067 | } |
6068 | else if (regmask_dep) { |
6069 | fprintf(fout, "/*__userpurge*/ "); |
92d715b6 |
6070 | } |
6071 | else if (fp->is_stdcall) |
9af2d373 |
6072 | fprintf(fout, " __stdcall "); |
92d715b6 |
6073 | else |
9af2d373 |
6074 | fprintf(fout, " __cdecl "); |
92d715b6 |
6075 | |
61e29183 |
6076 | fprintf(fout, "%s(", name); |
92d715b6 |
6077 | |
6078 | arg = 0; |
6079 | for (j = 0; j < xSP; j++) { |
6080 | if (regmask_dep & (1 << j)) { |
6081 | arg++; |
6082 | if (arg != 1) |
6083 | fprintf(fout, ", "); |
91ca764a |
6084 | if (fp->pp != NULL) |
6085 | fprintf(fout, "%s", fp->pp->arg[arg - 1].type.name); |
6086 | else |
6087 | fprintf(fout, "int"); |
6088 | fprintf(fout, " a%d/*<%s>*/", arg, regs_r32[j]); |
92d715b6 |
6089 | } |
6090 | } |
6091 | |
6092 | for (j = 0; j < argc_stack; j++) { |
6093 | arg++; |
6094 | if (arg != 1) |
6095 | fprintf(fout, ", "); |
91ca764a |
6096 | if (fp->pp != NULL) { |
6097 | fprintf(fout, "%s", fp->pp->arg[arg - 1].type.name); |
6098 | if (!fp->pp->arg[arg - 1].type.is_ptr) |
6099 | fprintf(fout, " "); |
6100 | } |
6101 | else |
6102 | fprintf(fout, "int "); |
6103 | fprintf(fout, "a%d", arg); |
92d715b6 |
6104 | } |
6105 | |
6106 | fprintf(fout, ");\n"); |
6107 | } |
6108 | } |
6109 | |
9af2d373 |
6110 | static void output_hdr(FILE *fout) |
6111 | { |
5fa1256f |
6112 | static const char *lmod_c_names[] = { |
6113 | [OPLM_UNSPEC] = "???", |
6114 | [OPLM_BYTE] = "uint8_t", |
6115 | [OPLM_WORD] = "uint16_t", |
6116 | [OPLM_DWORD] = "uint32_t", |
6117 | [OPLM_QWORD] = "uint64_t", |
6118 | }; |
6119 | const struct scanned_var *var; |
c0de9015 |
6120 | char line[256] = { 0, }; |
9af2d373 |
6121 | int i; |
6122 | |
6123 | // resolve deps |
6124 | qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_name); |
6125 | for (i = 0; i < hg_fp_cnt; i++) |
6126 | hg_fp_resolve_deps(&hg_fp[i]); |
6127 | |
6128 | // note: messes up .proto ptr, don't use |
6129 | //qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_id); |
6130 | |
5fa1256f |
6131 | // output variables |
6132 | for (i = 0; i < hg_var_cnt; i++) { |
6133 | var = &hg_vars[i]; |
6134 | |
c0de9015 |
6135 | if (var->pp != NULL && var->pp->is_fptr) { |
6136 | fprintf(fout, "extern "); |
6137 | output_pp(fout, var->pp, 0); |
6138 | fprintf(fout, ";"); |
6139 | } |
6140 | else if (var->is_c_str) |
61e29183 |
6141 | fprintf(fout, "extern %-8s %s[];", "char", var->name); |
6142 | else |
6143 | fprintf(fout, "extern %-8s %s;", |
6144 | lmod_c_names[var->lmod], var->name); |
5fa1256f |
6145 | |
6146 | if (var->is_seeded) |
6147 | fprintf(fout, " // seeded"); |
6148 | fprintf(fout, "\n"); |
6149 | } |
6150 | |
6151 | fprintf(fout, "\n"); |
6152 | |
6153 | // output function prototypes |
9af2d373 |
6154 | output_hdr_fp(fout, hg_fp, hg_fp_cnt); |
c0de9015 |
6155 | |
6156 | // include passthrough |
6157 | fprintf(fout, "\n// for translate\n"); |
6158 | |
6159 | rewind(g_fhdr); |
6160 | while (fgets(line, sizeof(line), g_fhdr)) { |
6161 | if (IS_START(line, "//#")) |
6162 | fwrite(line, 1, strlen(line), fout); |
6163 | } |
9af2d373 |
6164 | } |
6165 | |
5fa1256f |
6166 | // read a line, truncating it if it doesn't fit |
6167 | static char *my_fgets(char *s, size_t size, FILE *stream) |
d4e3b5db |
6168 | { |
5fa1256f |
6169 | char *ret, *ret2; |
6170 | char buf[64]; |
6171 | int p; |
d4e3b5db |
6172 | |
5fa1256f |
6173 | p = size - 2; |
6174 | if (p >= 0) |
6175 | s[p] = 0; |
d4e3b5db |
6176 | |
5fa1256f |
6177 | ret = fgets(s, size, stream); |
6178 | if (ret != NULL && p >= 0 && s[p] != 0 && s[p] != '\n') { |
6179 | p = sizeof(buf) - 2; |
6180 | do { |
6181 | buf[p] = 0; |
6182 | ret2 = fgets(buf, sizeof(buf), stream); |
6183 | } |
6184 | while (ret2 != NULL && buf[p] != 0 && buf[p] != '\n'); |
6185 | } |
6186 | |
6187 | return ret; |
d4e3b5db |
6188 | } |
6189 | |
61e29183 |
6190 | // '=' needs special treatment |
6191 | // also ' quote |
bfa4a6ee |
6192 | static char *next_word_s(char *w, size_t wsize, char *s) |
6193 | { |
61e29183 |
6194 | size_t i; |
bfa4a6ee |
6195 | |
61e29183 |
6196 | s = sskip(s); |
bfa4a6ee |
6197 | |
61e29183 |
6198 | i = 0; |
6199 | if (*s == '\'') { |
6200 | w[0] = s[0]; |
6201 | for (i = 1; i < wsize - 1; i++) { |
6202 | if (s[i] == 0) { |
6203 | printf("warning: missing closing quote: \"%s\"\n", s); |
6204 | break; |
6205 | } |
6206 | if (s[i] == '\'') |
6207 | break; |
6208 | w[i] = s[i]; |
6209 | } |
6210 | } |
bfa4a6ee |
6211 | |
61e29183 |
6212 | for (; i < wsize - 1; i++) { |
6213 | if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0)) |
6214 | break; |
6215 | w[i] = s[i]; |
6216 | } |
6217 | w[i] = 0; |
6218 | |
6219 | if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=') |
6220 | printf("warning: '%s' truncated\n", w); |
bfa4a6ee |
6221 | |
61e29183 |
6222 | return s + i; |
bfa4a6ee |
6223 | } |
6224 | |
5fa1256f |
6225 | static void scan_variables(FILE *fasm) |
6226 | { |
5fa1256f |
6227 | struct scanned_var *var; |
6228 | char line[256] = { 0, }; |
61e29183 |
6229 | char words[3][256]; |
5fa1256f |
6230 | char *p = NULL; |
6231 | int wordc; |
61e29183 |
6232 | int l; |
5fa1256f |
6233 | |
6234 | while (!feof(fasm)) |
6235 | { |
6236 | // skip to next data section |
6237 | while (my_fgets(line, sizeof(line), fasm)) |
6238 | { |
6239 | asmln++; |
6240 | |
6241 | p = sskip(line); |
6242 | if (*p == 0 || *p == ';') |
6243 | continue; |
6244 | |
6245 | p = sskip(next_word_s(words[0], sizeof(words[0]), p)); |
6246 | if (*p == 0 || *p == ';') |
6247 | continue; |
6248 | |
6249 | if (*p != 's' || !IS_START(p, "segment para public")) |
6250 | continue; |
6251 | |
6252 | break; |
6253 | } |
6254 | |
6255 | if (p == NULL || !IS_START(p, "segment para public")) |
6256 | break; |
6257 | p = sskip(p + 19); |
6258 | |
6259 | if (!IS_START(p, "'DATA'")) |
6260 | continue; |
6261 | |
6262 | // now process it |
6263 | while (my_fgets(line, sizeof(line), fasm)) |
6264 | { |
6265 | asmln++; |
6266 | |
6267 | p = line; |
6268 | if (my_isblank(*p)) |
6269 | continue; |
6270 | |
6271 | p = sskip(p); |
6272 | if (*p == 0 || *p == ';') |
6273 | continue; |
6274 | |
6275 | for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) { |
6276 | words[wordc][0] = 0; |
6277 | p = sskip(next_word_s(words[wordc], sizeof(words[0]), p)); |
6278 | if (*p == 0 || *p == ';') { |
6279 | wordc++; |
6280 | break; |
6281 | } |
6282 | } |
6283 | |
6284 | if (wordc == 2 && IS(words[1], "ends")) |
6285 | break; |
61e29183 |
6286 | if (wordc < 2) |
6287 | continue; |
5fa1256f |
6288 | |
6289 | if ((hg_var_cnt & 0xff) == 0) { |
6290 | hg_vars = realloc(hg_vars, sizeof(hg_vars[0]) |
6291 | * (hg_var_cnt + 0x100)); |
6292 | my_assert_not(hg_vars, NULL); |
6293 | memset(hg_vars + hg_var_cnt, 0, sizeof(hg_vars[0]) * 0x100); |
6294 | } |
6295 | |
6296 | var = &hg_vars[hg_var_cnt++]; |
6297 | snprintf(var->name, sizeof(var->name), "%s", words[0]); |
6298 | |
6299 | // maybe already in seed header? |
c0de9015 |
6300 | var->pp = proto_parse(g_fhdr, var->name, 1); |
6301 | if (var->pp != NULL) { |
6302 | if (var->pp->is_fptr) { |
5fa1256f |
6303 | var->lmod = OPLM_DWORD; |
6304 | //var->is_ptr = 1; |
6305 | } |
c0de9015 |
6306 | else if (var->pp->is_func) |
6307 | aerr("func?\n"); |
6308 | else if (!guess_lmod_from_c_type(&var->lmod, &var->pp->type)) |
5fa1256f |
6309 | aerr("unhandled C type '%s' for '%s'\n", |
c0de9015 |
6310 | var->pp->type.name, var->name); |
5fa1256f |
6311 | |
6312 | var->is_seeded = 1; |
6313 | continue; |
6314 | } |
6315 | |
6316 | if (IS(words[1], "dd")) |
6317 | var->lmod = OPLM_DWORD; |
6318 | else if (IS(words[1], "dw")) |
6319 | var->lmod = OPLM_WORD; |
61e29183 |
6320 | else if (IS(words[1], "db")) { |
5fa1256f |
6321 | var->lmod = OPLM_BYTE; |
61e29183 |
6322 | if (wordc >= 3 && (l = strlen(words[2])) > 4) { |
6323 | if (words[2][0] == '\'' && IS(words[2] + l - 2, ",0")) |
6324 | var->is_c_str = 1; |
6325 | } |
6326 | } |
5fa1256f |
6327 | else if (IS(words[1], "dq")) |
6328 | var->lmod = OPLM_QWORD; |
6329 | //else if (IS(words[1], "dt")) |
6330 | else |
6331 | aerr("type '%s' not known\n", words[1]); |
6332 | } |
6333 | } |
6334 | |
6335 | rewind(fasm); |
6336 | asmln = 0; |
6337 | } |
6338 | |
6339 | static void set_label(int i, const char *name) |
6340 | { |
6341 | const char *p; |
6342 | int len; |
6343 | |
6344 | len = strlen(name); |
6345 | p = strchr(name, ':'); |
6346 | if (p != NULL) |
6347 | len = p - name; |
6348 | |
6349 | if (g_labels[i] != NULL && !IS_START(g_labels[i], "algn_")) |
6350 | aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]); |
6351 | g_labels[i] = realloc(g_labels[i], len + 1); |
6352 | my_assert_not(g_labels[i], NULL); |
6353 | memcpy(g_labels[i], name, len); |
6354 | g_labels[i][len] = 0; |
6355 | } |
6356 | |
e56ab892 |
6357 | struct chunk_item { |
6358 | char *name; |
6359 | long fptr; |
de50b98b |
6360 | int asmln; |
e56ab892 |
6361 | }; |
6362 | |
cdfaeed7 |
6363 | static struct chunk_item *func_chunks; |
6364 | static int func_chunk_cnt; |
6365 | static int func_chunk_alloc; |
6366 | |
6367 | static void add_func_chunk(FILE *fasm, const char *name, int line) |
6368 | { |
6369 | if (func_chunk_cnt >= func_chunk_alloc) { |
6370 | func_chunk_alloc *= 2; |
6371 | func_chunks = realloc(func_chunks, |
6372 | func_chunk_alloc * sizeof(func_chunks[0])); |
6373 | my_assert_not(func_chunks, NULL); |
6374 | } |
6375 | func_chunks[func_chunk_cnt].fptr = ftell(fasm); |
6376 | func_chunks[func_chunk_cnt].name = strdup(name); |
6377 | func_chunks[func_chunk_cnt].asmln = line; |
6378 | func_chunk_cnt++; |
6379 | } |
6380 | |
e56ab892 |
6381 | static int cmp_chunks(const void *p1, const void *p2) |
6382 | { |
6383 | const struct chunk_item *c1 = p1, *c2 = p2; |
6384 | return strcmp(c1->name, c2->name); |
6385 | } |
6386 | |
bfa4a6ee |
6387 | static int cmpstringp(const void *p1, const void *p2) |
6388 | { |
6389 | return strcmp(*(char * const *)p1, *(char * const *)p2); |
6390 | } |
6391 | |
cdfaeed7 |
6392 | static void scan_ahead(FILE *fasm) |
6393 | { |
6394 | char words[2][256]; |
6395 | char line[256]; |
6396 | long oldpos; |
6397 | int oldasmln; |
6398 | int wordc; |
6399 | char *p; |
6400 | int i; |
6401 | |
6402 | oldpos = ftell(fasm); |
6403 | oldasmln = asmln; |
6404 | |
5fa1256f |
6405 | while (my_fgets(line, sizeof(line), fasm)) |
cdfaeed7 |
6406 | { |
6407 | wordc = 0; |
6408 | asmln++; |
6409 | |
6410 | p = sskip(line); |
6411 | if (*p == 0) |
6412 | continue; |
6413 | |
6414 | if (*p == ';') |
6415 | { |
6416 | // get rid of random tabs |
6417 | for (i = 0; line[i] != 0; i++) |
6418 | if (line[i] == '\t') |
6419 | line[i] = ' '; |
6420 | |
6421 | if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR ")) |
6422 | { |
6423 | p += 30; |
6424 | next_word(words[0], sizeof(words[0]), p); |
6425 | if (words[0][0] == 0) |
6426 | aerr("missing name for func chunk?\n"); |
6427 | |
6428 | add_func_chunk(fasm, words[0], asmln); |
6429 | } |
46b388c2 |
6430 | else if (IS_START(p, "; sctend")) |
6431 | break; |
6432 | |
cdfaeed7 |
6433 | continue; |
6434 | } // *p == ';' |
6435 | |
6436 | for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) { |
6437 | words[wordc][0] = 0; |
6438 | p = sskip(next_word_s(words[wordc], sizeof(words[0]), p)); |
6439 | if (*p == 0 || *p == ';') { |
6440 | wordc++; |
6441 | break; |
6442 | } |
6443 | } |
6444 | |
6445 | if (wordc == 2 && IS(words[1], "ends")) |
6446 | break; |
6447 | } |
6448 | |
6449 | fseek(fasm, oldpos, SEEK_SET); |
6450 | asmln = oldasmln; |
6451 | } |
6452 | |
91977a1c |
6453 | int main(int argc, char *argv[]) |
6454 | { |
06c5d854 |
6455 | FILE *fout, *fasm, *frlist; |
4c45fa73 |
6456 | struct parsed_data *pd = NULL; |
6457 | int pd_alloc = 0; |
6458 | char **rlist = NULL; |
6459 | int rlist_len = 0; |
6460 | int rlist_alloc = 0; |
e56ab892 |
6461 | int func_chunks_used = 0; |
6462 | int func_chunks_sorted = 0; |
e56ab892 |
6463 | int func_chunk_i = -1; |
6464 | long func_chunk_ret = 0; |
de50b98b |
6465 | int func_chunk_ret_ln = 0; |
cdfaeed7 |
6466 | int scanned_ahead = 0; |
91977a1c |
6467 | char line[256]; |
a2c1d768 |
6468 | char words[20][256]; |
4c45fa73 |
6469 | enum opr_lenmod lmod; |
ddaf8bd7 |
6470 | char *sctproto = NULL; |
91977a1c |
6471 | int in_func = 0; |
4c45fa73 |
6472 | int pending_endp = 0; |
bfa4a6ee |
6473 | int skip_func = 0; |
940e8e66 |
6474 | int skip_warned = 0; |
91977a1c |
6475 | int eq_alloc; |
bfa4a6ee |
6476 | int verbose = 0; |
1f84f6b3 |
6477 | int multi_seg = 0; |
46b388c2 |
6478 | int end = 0; |
bfa4a6ee |
6479 | int arg_out; |
89ff3147 |
6480 | int arg; |
91977a1c |
6481 | int pi = 0; |
e56ab892 |
6482 | int i, j; |
6483 | int ret, len; |
91977a1c |
6484 | char *p; |
6485 | int wordc; |
6486 | |
89ff3147 |
6487 | for (arg = 1; arg < argc; arg++) { |
6488 | if (IS(argv[arg], "-v")) |
6489 | verbose = 1; |
6490 | else if (IS(argv[arg], "-rf")) |
6491 | g_allow_regfunc = 1; |
1f84f6b3 |
6492 | else if (IS(argv[arg], "-m")) |
6493 | multi_seg = 1; |
92d715b6 |
6494 | else if (IS(argv[arg], "-hdr")) |
9af2d373 |
6495 | g_header_mode = g_quiet_pp = g_allow_regfunc = 1; |
89ff3147 |
6496 | else |
6497 | break; |
bfa4a6ee |
6498 | } |
6499 | |
6500 | if (argc < arg + 3) { |
315b77eb |
6501 | printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdr.h> [rlist]*\n" |
6502 | "%s -hdr <out.h> <.asm> <seed.h> [rlist]*\n" |
6503 | "options:\n" |
6504 | " -hdr - header generation mode\n" |
6505 | " -rf - allow unannotated indirect calls\n" |
6506 | " -m - allow multiple .text sections\n" |
6507 | "[rlist] is a file with function names to skip," |
6508 | " one per line\n", |
92d715b6 |
6509 | argv[0], argv[0]); |
91977a1c |
6510 | return 1; |
6511 | } |
6512 | |
bfa4a6ee |
6513 | arg_out = arg++; |
91977a1c |
6514 | |
bfa4a6ee |
6515 | asmfn = argv[arg++]; |
91977a1c |
6516 | fasm = fopen(asmfn, "r"); |
6517 | my_assert_not(fasm, NULL); |
6518 | |
bfa4a6ee |
6519 | hdrfn = argv[arg++]; |
06c5d854 |
6520 | g_fhdr = fopen(hdrfn, "r"); |
6521 | my_assert_not(g_fhdr, NULL); |
bfa4a6ee |
6522 | |
6523 | rlist_alloc = 64; |
6524 | rlist = malloc(rlist_alloc * sizeof(rlist[0])); |
6525 | my_assert_not(rlist, NULL); |
6526 | // needs special handling.. |
6527 | rlist[rlist_len++] = "__alloca_probe"; |
6528 | |
e56ab892 |
6529 | func_chunk_alloc = 32; |
6530 | func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0])); |
6531 | my_assert_not(func_chunks, NULL); |
6532 | |
a2c1d768 |
6533 | memset(words, 0, sizeof(words)); |
6534 | |
bfa4a6ee |
6535 | for (; arg < argc; arg++) { |
6536 | frlist = fopen(argv[arg], "r"); |
6537 | my_assert_not(frlist, NULL); |
6538 | |
5fa1256f |
6539 | while (my_fgets(line, sizeof(line), frlist)) { |
bfa4a6ee |
6540 | p = sskip(line); |
1cd4a663 |
6541 | if (*p == 0 || *p == ';') |
6542 | continue; |
6543 | if (*p == '#') { |
89ff3147 |
6544 | if (IS_START(p, "#if 0") |
6545 | || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC"))) |
6546 | { |
1cd4a663 |
6547 | skip_func = 1; |
89ff3147 |
6548 | } |
1cd4a663 |
6549 | else if (IS_START(p, "#endif")) |
6550 | skip_func = 0; |
6551 | continue; |
6552 | } |
6553 | if (skip_func) |
bfa4a6ee |
6554 | continue; |
6555 | |
6556 | p = next_word(words[0], sizeof(words[0]), p); |
6557 | if (words[0][0] == 0) |
6558 | continue; |
6559 | |
6560 | if (rlist_len >= rlist_alloc) { |
6561 | rlist_alloc = rlist_alloc * 2 + 64; |
6562 | rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0])); |
6563 | my_assert_not(rlist, NULL); |
6564 | } |
6565 | rlist[rlist_len++] = strdup(words[0]); |
6566 | } |
1cd4a663 |
6567 | skip_func = 0; |
bfa4a6ee |
6568 | |
6569 | fclose(frlist); |
6570 | frlist = NULL; |
6571 | } |
6572 | |
6573 | if (rlist_len > 0) |
6574 | qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp); |
6575 | |
6576 | fout = fopen(argv[arg_out], "w"); |
91977a1c |
6577 | my_assert_not(fout, NULL); |
6578 | |
6579 | eq_alloc = 128; |
6580 | g_eqs = malloc(eq_alloc * sizeof(g_eqs[0])); |
6581 | my_assert_not(g_eqs, NULL); |
6582 | |
4c45fa73 |
6583 | for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) { |
6584 | g_label_refs[i].i = -1; |
6585 | g_label_refs[i].next = NULL; |
6586 | } |
6587 | |
5fa1256f |
6588 | if (g_header_mode) |
6589 | scan_variables(fasm); |
6590 | |
6591 | while (my_fgets(line, sizeof(line), fasm)) |
91977a1c |
6592 | { |
4c45fa73 |
6593 | wordc = 0; |
91977a1c |
6594 | asmln++; |
6595 | |
6596 | p = sskip(line); |
1bafb621 |
6597 | if (*p == 0) |
91977a1c |
6598 | continue; |
6599 | |
de50b98b |
6600 | // get rid of random tabs |
6601 | for (i = 0; line[i] != 0; i++) |
6602 | if (line[i] == '\t') |
6603 | line[i] = ' '; |
6604 | |
e56ab892 |
6605 | if (*p == ';') |
6606 | { |
e56ab892 |
6607 | if (p[2] == '=' && IS_START(p, "; =============== S U B")) |
6608 | goto do_pending_endp; // eww.. |
6609 | |
6610 | if (p[2] == 'A' && IS_START(p, "; Attributes:")) |
6611 | { |
6612 | static const char *attrs[] = { |
6613 | "bp-based frame", |
6614 | "library function", |
6615 | "static", |
6616 | "noreturn", |
6617 | "thunk", |
6618 | "fpd=", |
6619 | }; |
6620 | |
6621 | // parse IDA's attribute-list comment |
6622 | g_ida_func_attr = 0; |
6623 | p = sskip(p + 13); |
6624 | |
6625 | for (; *p != 0; p = sskip(p)) { |
6626 | for (i = 0; i < ARRAY_SIZE(attrs); i++) { |
6627 | if (!strncmp(p, attrs[i], strlen(attrs[i]))) { |
6628 | g_ida_func_attr |= 1 << i; |
6629 | p += strlen(attrs[i]); |
6630 | break; |
6631 | } |
6632 | } |
6633 | if (i == ARRAY_SIZE(attrs)) { |
6634 | anote("unparsed IDA attr: %s\n", p); |
1bafb621 |
6635 | break; |
6636 | } |
e56ab892 |
6637 | if (IS(attrs[i], "fpd=")) { |
6638 | p = next_word(words[0], sizeof(words[0]), p); |
6639 | // ignore for now.. |
6640 | } |
1bafb621 |
6641 | } |
e56ab892 |
6642 | } |
6643 | else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR ")) |
6644 | { |
6645 | p += 30; |
6646 | next_word(words[0], sizeof(words[0]), p); |
6647 | if (words[0][0] == 0) |
cdfaeed7 |
6648 | aerr("missing name for func chunk?\n"); |
6649 | |
6650 | if (!scanned_ahead) { |
6651 | add_func_chunk(fasm, words[0], asmln); |
6652 | func_chunks_sorted = 0; |
e56ab892 |
6653 | } |
e56ab892 |
6654 | } |
6655 | else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK")) |
6656 | { |
6657 | if (func_chunk_i >= 0) { |
6658 | if (func_chunk_i < func_chunk_cnt |
6659 | && IS(func_chunks[func_chunk_i].name, g_func)) |
6660 | { |
6661 | // move on to next chunk |
6662 | ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET); |
6663 | if (ret) |
6664 | aerr("seek failed for '%s' chunk #%d\n", |
6665 | g_func, func_chunk_i); |
de50b98b |
6666 | asmln = func_chunks[func_chunk_i].asmln; |
e56ab892 |
6667 | func_chunk_i++; |
6668 | } |
6669 | else { |
6670 | if (func_chunk_ret == 0) |
6671 | aerr("no return from chunk?\n"); |
6672 | fseek(fasm, func_chunk_ret, SEEK_SET); |
de50b98b |
6673 | asmln = func_chunk_ret_ln; |
e56ab892 |
6674 | func_chunk_ret = 0; |
6675 | pending_endp = 1; |
6676 | } |
1bafb621 |
6677 | } |
e56ab892 |
6678 | } |
6679 | else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) { |
6680 | func_chunks_used = 1; |
6681 | p += 20; |
6682 | if (IS_START(g_func, "sub_")) { |
6683 | unsigned long addr = strtoul(p, NULL, 16); |
6684 | unsigned long f_addr = strtoul(g_func + 4, NULL, 16); |
cdfaeed7 |
6685 | if (addr > f_addr && !scanned_ahead) { |
94d447fb |
6686 | anote("scan_ahead caused by '%s', addr %lx\n", |
6687 | g_func, addr); |
cdfaeed7 |
6688 | scan_ahead(fasm); |
6689 | scanned_ahead = 1; |
6690 | func_chunks_sorted = 0; |
6691 | } |
1bafb621 |
6692 | } |
6693 | } |
6694 | continue; |
e56ab892 |
6695 | } // *p == ';' |
1bafb621 |
6696 | |
06c5d854 |
6697 | parse_words: |
a2c1d768 |
6698 | for (i = wordc; i < ARRAY_SIZE(words); i++) |
6699 | words[i][0] = 0; |
cdfaeed7 |
6700 | for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) { |
bfa4a6ee |
6701 | p = sskip(next_word_s(words[wordc], sizeof(words[0]), p)); |
91977a1c |
6702 | if (*p == 0 || *p == ';') { |
6703 | wordc++; |
6704 | break; |
6705 | } |
6706 | } |
a2c1d768 |
6707 | if (*p != 0 && *p != ';') |
6708 | aerr("too many words\n"); |
91977a1c |
6709 | |
06c5d854 |
6710 | // alow asm patches in comments |
ddaf8bd7 |
6711 | if (*p == ';') { |
6712 | if (IS_START(p, "; sctpatch:")) { |
6713 | p = sskip(p + 11); |
6714 | if (*p == 0 || *p == ';') |
6715 | continue; |
6716 | goto parse_words; // lame |
6717 | } |
6718 | if (IS_START(p, "; sctproto:")) { |
6719 | sctproto = strdup(p + 11); |
6720 | } |
46b388c2 |
6721 | else if (IS_START(p, "; sctend")) { |
6722 | end = 1; |
6723 | if (!pending_endp) |
6724 | break; |
6725 | } |
06c5d854 |
6726 | } |
6727 | |
91977a1c |
6728 | if (wordc == 0) { |
6729 | // shouldn't happen |
6730 | awarn("wordc == 0?\n"); |
6731 | continue; |
6732 | } |
6733 | |
6734 | // don't care about this: |
6735 | if (words[0][0] == '.' |
6736 | || IS(words[0], "include") |
6737 | || IS(words[0], "assume") || IS(words[1], "segment") |
6738 | || IS(words[0], "align")) |
6739 | { |
6740 | continue; |
6741 | } |
6742 | |
4c45fa73 |
6743 | do_pending_endp: |
6744 | // do delayed endp processing to collect switch jumptables |
6745 | if (pending_endp) { |
30c8c549 |
6746 | if (in_func && !g_skip_func && !end && wordc >= 2 |
4c45fa73 |
6747 | && ((words[0][0] == 'd' && words[0][2] == 0) |
6748 | || (words[1][0] == 'd' && words[1][2] == 0))) |
6749 | { |
6750 | i = 1; |
6751 | if (words[1][0] == 'd' && words[1][2] == 0) { |
6752 | // label |
6753 | if (g_func_pd_cnt >= pd_alloc) { |
6754 | pd_alloc = pd_alloc * 2 + 16; |
6755 | g_func_pd = realloc(g_func_pd, |
6756 | sizeof(g_func_pd[0]) * pd_alloc); |
6757 | my_assert_not(g_func_pd, NULL); |
6758 | } |
6759 | pd = &g_func_pd[g_func_pd_cnt]; |
6760 | g_func_pd_cnt++; |
6761 | memset(pd, 0, sizeof(*pd)); |
6762 | strcpy(pd->label, words[0]); |
6763 | pd->type = OPT_CONST; |
6764 | pd->lmod = lmod_from_directive(words[1]); |
6765 | i = 2; |
6766 | } |
6767 | else { |
da87ae38 |
6768 | if (pd == NULL) { |
6769 | if (verbose) |
6770 | anote("skipping alignment byte?\n"); |
6771 | continue; |
6772 | } |
4c45fa73 |
6773 | lmod = lmod_from_directive(words[0]); |
6774 | if (lmod != pd->lmod) |
6775 | aerr("lmod change? %d->%d\n", pd->lmod, lmod); |
6776 | } |
6777 | |
6778 | if (pd->count_alloc < pd->count + wordc) { |
6779 | pd->count_alloc = pd->count_alloc * 2 + 14 + wordc; |
6780 | pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc); |
6781 | my_assert_not(pd->d, NULL); |
6782 | } |
6783 | for (; i < wordc; i++) { |
6784 | if (IS(words[i], "offset")) { |
6785 | pd->type = OPT_OFFSET; |
6786 | i++; |
6787 | } |
6788 | p = strchr(words[i], ','); |
6789 | if (p != NULL) |
6790 | *p = 0; |
6791 | if (pd->type == OPT_OFFSET) |
6792 | pd->d[pd->count].u.label = strdup(words[i]); |
6793 | else |
6794 | pd->d[pd->count].u.val = parse_number(words[i]); |
6795 | pd->d[pd->count].bt_i = -1; |
6796 | pd->count++; |
6797 | } |
6798 | continue; |
6799 | } |
6800 | |
30c8c549 |
6801 | if (in_func && !g_skip_func) { |
9af2d373 |
6802 | if (g_header_mode) |
92d715b6 |
6803 | gen_hdr(g_func, pi); |
6804 | else |
6805 | gen_func(fout, g_fhdr, g_func, pi); |
6806 | } |
4c45fa73 |
6807 | |
6808 | pending_endp = 0; |
6809 | in_func = 0; |
6810 | g_ida_func_attr = 0; |
6811 | skip_warned = 0; |
30c8c549 |
6812 | g_skip_func = 0; |
4c45fa73 |
6813 | g_func[0] = 0; |
e56ab892 |
6814 | func_chunks_used = 0; |
6815 | func_chunk_i = -1; |
4c45fa73 |
6816 | if (pi != 0) { |
6817 | memset(&ops, 0, pi * sizeof(ops[0])); |
d7857c3a |
6818 | clear_labels(pi); |
4c45fa73 |
6819 | pi = 0; |
6820 | } |
6821 | g_eqcnt = 0; |
6822 | for (i = 0; i < g_func_pd_cnt; i++) { |
6823 | pd = &g_func_pd[i]; |
6824 | if (pd->type == OPT_OFFSET) { |
6825 | for (j = 0; j < pd->count; j++) |
6826 | free(pd->d[j].u.label); |
6827 | } |
6828 | free(pd->d); |
6829 | pd->d = NULL; |
6830 | } |
6831 | g_func_pd_cnt = 0; |
6832 | pd = NULL; |
46b388c2 |
6833 | |
6834 | if (end) |
6835 | break; |
4c45fa73 |
6836 | if (wordc == 0) |
6837 | continue; |
6838 | } |
6839 | |
91977a1c |
6840 | if (IS(words[1], "proc")) { |
6841 | if (in_func) |
6842 | aerr("proc '%s' while in_func '%s'?\n", |
6843 | words[0], g_func); |
bfa4a6ee |
6844 | p = words[0]; |
ddaf8bd7 |
6845 | if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp)) |
30c8c549 |
6846 | g_skip_func = 1; |
91977a1c |
6847 | strcpy(g_func, words[0]); |
d4e3b5db |
6848 | set_label(0, words[0]); |
91977a1c |
6849 | in_func = 1; |
6850 | continue; |
6851 | } |
6852 | |
e56ab892 |
6853 | if (IS(words[1], "endp")) |
6854 | { |
91977a1c |
6855 | if (!in_func) |
6856 | aerr("endp '%s' while not in_func?\n", words[0]); |
6857 | if (!IS(g_func, words[0])) |
6858 | aerr("endp '%s' while in_func '%s'?\n", |
6859 | words[0], g_func); |
bfa4a6ee |
6860 | |
ddaf8bd7 |
6861 | if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1 |
6862 | && ops[0].op == OP_JMP && ops[0].operand[0].had_ds) |
6863 | { |
6864 | // import jump |
30c8c549 |
6865 | g_skip_func = 1; |
ddaf8bd7 |
6866 | } |
6867 | |
30c8c549 |
6868 | if (!g_skip_func && func_chunks_used) { |
e56ab892 |
6869 | // start processing chunks |
6870 | struct chunk_item *ci, key = { g_func, 0 }; |
6871 | |
6872 | func_chunk_ret = ftell(fasm); |
de50b98b |
6873 | func_chunk_ret_ln = asmln; |
e56ab892 |
6874 | if (!func_chunks_sorted) { |
6875 | qsort(func_chunks, func_chunk_cnt, |
6876 | sizeof(func_chunks[0]), cmp_chunks); |
6877 | func_chunks_sorted = 1; |
6878 | } |
6879 | ci = bsearch(&key, func_chunks, func_chunk_cnt, |
6880 | sizeof(func_chunks[0]), cmp_chunks); |
6881 | if (ci == NULL) |
6882 | aerr("'%s' needs chunks, but none found\n", g_func); |
6883 | func_chunk_i = ci - func_chunks; |
6884 | for (; func_chunk_i > 0; func_chunk_i--) |
6885 | if (!IS(func_chunks[func_chunk_i - 1].name, g_func)) |
6886 | break; |
6887 | |
6888 | ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET); |
6889 | if (ret) |
6890 | aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i); |
de50b98b |
6891 | asmln = func_chunks[func_chunk_i].asmln; |
e56ab892 |
6892 | func_chunk_i++; |
6893 | continue; |
6894 | } |
4c45fa73 |
6895 | pending_endp = 1; |
91977a1c |
6896 | continue; |
6897 | } |
6898 | |
1f84f6b3 |
6899 | if (wordc == 2 && IS(words[1], "ends")) { |
46b388c2 |
6900 | if (!multi_seg) { |
6901 | end = 1; |
6902 | if (pending_endp) |
6903 | goto do_pending_endp; |
1f84f6b3 |
6904 | break; |
46b388c2 |
6905 | } |
1f84f6b3 |
6906 | |
6907 | // scan for next text segment |
5fa1256f |
6908 | while (my_fgets(line, sizeof(line), fasm)) { |
1f84f6b3 |
6909 | asmln++; |
6910 | p = sskip(line); |
6911 | if (*p == 0 || *p == ';') |
6912 | continue; |
6913 | |
6914 | if (strstr(p, "segment para public 'CODE' use32")) |
6915 | break; |
6916 | } |
6917 | |
6918 | continue; |
6919 | } |
a2c1d768 |
6920 | |
bfa4a6ee |
6921 | p = strchr(words[0], ':'); |
6922 | if (p != NULL) { |
d4e3b5db |
6923 | set_label(pi, words[0]); |
bfa4a6ee |
6924 | continue; |
6925 | } |
6926 | |
30c8c549 |
6927 | if (!in_func || g_skip_func) { |
6928 | if (!skip_warned && !g_skip_func && g_labels[pi] != NULL) { |
bfa4a6ee |
6929 | if (verbose) |
6930 | anote("skipping from '%s'\n", g_labels[pi]); |
6931 | skip_warned = 1; |
6932 | } |
d7857c3a |
6933 | free(g_labels[pi]); |
6934 | g_labels[pi] = NULL; |
bfa4a6ee |
6935 | continue; |
6936 | } |
6937 | |
ddaf8bd7 |
6938 | if (wordc > 1 && IS(words[1], "=")) |
6939 | { |
91977a1c |
6940 | if (wordc != 5) |
6941 | aerr("unhandled equ, wc=%d\n", wordc); |
6942 | if (g_eqcnt >= eq_alloc) { |
6943 | eq_alloc *= 2; |
6944 | g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0])); |
6945 | my_assert_not(g_eqs, NULL); |
6946 | } |
6947 | |
6948 | len = strlen(words[0]); |
6949 | if (len > sizeof(g_eqs[0].name) - 1) |
6950 | aerr("equ name too long: %d\n", len); |
6951 | strcpy(g_eqs[g_eqcnt].name, words[0]); |
6952 | |
6953 | if (!IS(words[3], "ptr")) |
6954 | aerr("unhandled equ\n"); |
6955 | if (IS(words[2], "dword")) |
6956 | g_eqs[g_eqcnt].lmod = OPLM_DWORD; |
6957 | else if (IS(words[2], "word")) |
6958 | g_eqs[g_eqcnt].lmod = OPLM_WORD; |
6959 | else if (IS(words[2], "byte")) |
6960 | g_eqs[g_eqcnt].lmod = OPLM_BYTE; |
90307a99 |
6961 | else if (IS(words[2], "qword")) |
6962 | g_eqs[g_eqcnt].lmod = OPLM_QWORD; |
91977a1c |
6963 | else |
6964 | aerr("bad lmod: '%s'\n", words[2]); |
6965 | |
6966 | g_eqs[g_eqcnt].offset = parse_number(words[4]); |
6967 | g_eqcnt++; |
6968 | continue; |
6969 | } |
6970 | |
6971 | if (pi >= ARRAY_SIZE(ops)) |
6972 | aerr("too many ops\n"); |
6973 | |
91977a1c |
6974 | parse_op(&ops[pi], words, wordc); |
ddaf8bd7 |
6975 | |
6976 | if (sctproto != NULL) { |
8eb12e72 |
6977 | if (ops[pi].op == OP_CALL || ops[pi].op == OP_JMP) |
ddaf8bd7 |
6978 | ops[pi].datap = sctproto; |
6979 | sctproto = NULL; |
6980 | } |
91977a1c |
6981 | pi++; |
91977a1c |
6982 | } |
6983 | |
9af2d373 |
6984 | if (g_header_mode) |
92d715b6 |
6985 | output_hdr(fout); |
6986 | |
91977a1c |
6987 | fclose(fout); |
6988 | fclose(fasm); |
06c5d854 |
6989 | fclose(g_fhdr); |
91977a1c |
6990 | |
6991 | return 0; |
c36e914d |
6992 | } |
91977a1c |
6993 | |
6994 | // vim:ts=2:shiftwidth=2:expandtab |