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