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