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