9 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
10 #define IS(w, y) !strcmp(w, y)
11 #define IS_START(w, y) !strncmp(w, y, strlen(y))
13 #include "protoparse.h"
15 static const char *asmfn;
19 #define anote(fmt, ...) \
20 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
21 #define awarn(fmt, ...) \
22 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
23 #define aerr(fmt, ...) do { \
24 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
29 #include "masm_tools.h"
32 OPF_RMD = (1 << 0), /* removed or optimized out */
33 OPF_DATA = (1 << 1), /* data processing - writes to dst opr */
34 OPF_FLAGS = (1 << 2), /* sets flags */
35 OPF_JMP = (1 << 3), /* branches, ret and call */
36 OPF_CC = (1 << 4), /* uses flags */
37 OPF_TAIL = (1 << 5), /* ret or tail call */
38 OPF_RSAVE = (1 << 6), /* push/pop is local reg save/load */
39 OPF_REP = (1 << 7), /* prefixed by rep */
40 OPF_REPZ = (1 << 8), /* rep is repe/repz */
41 OPF_REPNZ = (1 << 9), /* rep is repne/repnz */
42 OPF_FARG = (1 << 10), /* push collected as func arg (no reuse) */
43 OPF_EBP_S = (1 << 11), /* ebp used as scratch, not BP */
111 // must be sorted (larger len must be further in enum)
119 #define MAX_OPERANDS 3
123 enum opr_lenmod lmod;
124 unsigned int is_ptr:1; // pointer in C
125 unsigned int is_array:1; // array in C
126 unsigned int type_from_var:1; // .. in header, sometimes wrong
127 unsigned int size_mismatch:1; // type override differs from C
128 unsigned int size_lt:1; // type override is larger than C
136 struct parsed_opr operand[MAX_OPERANDS];
139 int regmask_src; // all referensed regs
141 int pfomask; // flagop: parsed_flag_op that can't be delayed
142 int argnum; // push: altered before call arg #
143 int cc_scratch; // scratch storage during analysis
144 int bt_i; // branch target for branches
145 struct parsed_data *btj;// branch targets for jumptables
150 // OP_CALL - ptr to parsed_proto
151 // (OPF_CC) - point to one of (OPF_FLAGS) that affects cc op
155 enum opr_lenmod lmod;
162 enum opr_lenmod lmod;
176 struct label_ref *next;
180 IDAFA_BP_FRAME = (1 << 0),
181 IDAFA_LIB_FUNC = (1 << 1),
182 IDAFA_STATIC = (1 << 2),
183 IDAFA_NORETURN = (1 << 3),
184 IDAFA_THUNK = (1 << 4),
185 IDAFA_FPD = (1 << 5),
190 static struct parsed_op ops[MAX_OPS];
191 static struct parsed_equ *g_eqs;
193 static char g_labels[MAX_OPS][32];
194 static struct label_ref g_label_refs[MAX_OPS];
195 static const struct parsed_proto *g_func_pp;
196 static struct parsed_data *g_func_pd;
197 static int g_func_pd_cnt;
198 static char g_func[256];
199 static char g_comment[256];
200 static int g_bp_frame;
201 static int g_sp_frame;
202 static int g_stack_frame_used;
203 static int g_stack_fsz;
204 static int g_ida_func_attr;
205 static int g_allow_regfunc;
206 #define ferr(op_, fmt, ...) do { \
207 printf("error:%s:#%zd: '%s': " fmt, g_func, (op_) - ops, \
208 dump_op(op_), ##__VA_ARGS__); \
212 #define fnote(op_, fmt, ...) \
213 printf("error:%s:#%zd: '%s': " fmt, g_func, (op_) - ops, \
214 dump_op(op_), ##__VA_ARGS__)
218 const char *regs_r32[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp" };
219 const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
220 const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
221 const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
223 enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
225 // possible basic comparison types (without inversion)
226 enum parsed_flag_op {
230 PFO_BE, // 6 CF=1||ZF=1
234 PFO_LE, // e ZF=1||SF!=OF
237 static const char *parsed_flag_op_names[] = {
238 "o", "c", "z", "be", "s", "p", "l", "le"
241 static int char_array_i(const char *array[], size_t len, const char *s)
245 for (i = 0; i < len; i++)
252 static void printf_number(char *buf, size_t buf_size,
253 unsigned long number)
255 // output in C-friendly form
256 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
259 static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
263 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
265 *reg_lmod = OPLM_DWORD;
268 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
270 *reg_lmod = OPLM_WORD;
273 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
275 *reg_lmod = OPLM_BYTE;
278 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
280 *reg_lmod = OPLM_BYTE;
287 static int parse_indmode(char *name, int *regmask, int need_c_cvt)
289 enum opr_lenmod lmod;
302 while (my_isblank(*s))
304 for (; my_issep(*s); d++, s++)
306 while (my_isblank(*s))
311 if (IS_START(s, "ds:"))
314 s = next_idt(w, sizeof(w), s);
319 reg = parse_reg(&lmod, w);
321 *regmask |= 1 << reg;
325 if ('0' <= w[0] && w[0] <= '9') {
326 number = parse_number(w);
327 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
331 // probably some label/identifier - pass
334 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
338 strcpy(name, cvtbuf);
343 static int is_reg_in_str(const char *s)
347 if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3])))
350 for (i = 0; i < ARRAY_SIZE(regs_r32); i++)
351 if (!strncmp(s, regs_r32[i], 3))
357 static const char *parse_stack_el(const char *name, char *extra_reg)
359 const char *p, *p2, *s;
366 if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) {
368 if (extra_reg != NULL) {
369 strncpy(extra_reg, name, 3);
374 if (IS_START(p, "ebp+")) {
378 if (p2 != NULL && is_reg_in_str(p)) {
379 if (extra_reg != NULL) {
380 strncpy(extra_reg, p, p2 - p);
381 extra_reg[p2 - p] = 0;
386 if (!('0' <= *p && *p <= '9'))
392 if (!IS_START(name, "esp+"))
395 p = strchr(name + 4, '+');
397 // must be a number after esp+, already converted to 0x..
399 if (!('0' <= *s && *s <= '9')) {
400 aerr("%s nan?\n", __func__);
403 if (s[0] == '0' && s[1] == 'x')
406 if (len < sizeof(buf) - 1) {
407 strncpy(buf, s, len);
409 val = strtol(buf, &endp, 16);
410 if (val == 0 || *endp != 0) {
411 aerr("%s num parse fail for '%s'\n", __func__, buf);
420 if ('0' <= *p && *p <= '9')
426 static int guess_lmod_from_name(struct parsed_opr *opr)
428 if (!strncmp(opr->name, "dword_", 6)) {
429 opr->lmod = OPLM_DWORD;
432 if (!strncmp(opr->name, "word_", 5)) {
433 opr->lmod = OPLM_WORD;
436 if (!strncmp(opr->name, "byte_", 5)) {
437 opr->lmod = OPLM_BYTE;
443 static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
444 const struct parsed_type *c_type)
446 static const char *dword_types[] = {
447 "int", "_DWORD", "UINT_PTR",
448 "DWORD", "HANDLE", "HWND", "HMODULE",
449 "WPARAM", "LPARAM", "UINT",
451 static const char *word_types[] = {
452 "uint16_t", "int16_t", "_WORD",
453 "unsigned __int16", "__int16",
455 static const char *byte_types[] = {
456 "uint8_t", "int8_t", "char",
457 "unsigned __int8", "__int8", "BYTE", "_BYTE",
463 if (c_type->is_ptr) {
468 n = skip_type_mod(c_type->name);
470 for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
471 if (IS(n, dword_types[i])) {
477 for (i = 0; i < ARRAY_SIZE(word_types); i++) {
478 if (IS(n, word_types[i])) {
484 for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
485 if (IS(n, byte_types[i])) {
494 static enum opr_type lmod_from_directive(const char *d)
498 else if (IS(d, "dw"))
500 else if (IS(d, "db"))
503 aerr("unhandled directive: '%s'\n", d);
507 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
513 *regmask |= 1 << reg;
516 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
519 static int parse_operand(struct parsed_opr *opr,
520 int *regmask, int *regmask_indirect,
521 char words[16][256], int wordc, int w, unsigned int op_flags)
523 const struct parsed_proto *pp;
524 enum opr_lenmod tmplmod;
525 unsigned long number;
533 aerr("parse_operand w %d, wordc %d\n", w, wordc);
537 for (i = w; i < wordc; i++) {
538 len = strlen(words[i]);
539 if (words[i][len - 1] == ',') {
540 words[i][len - 1] = 0;
546 wordc_in = wordc - w;
548 if ((op_flags & OPF_JMP) && wordc_in > 0
549 && !('0' <= words[w][0] && words[w][0] <= '9'))
551 const char *label = NULL;
553 if (wordc_in == 3 && !strncmp(words[w], "near", 4)
554 && IS(words[w + 1], "ptr"))
555 label = words[w + 2];
556 else if (wordc_in == 2 && IS(words[w], "short"))
557 label = words[w + 1];
558 else if (wordc_in == 1
559 && strchr(words[w], '[') == NULL
560 && parse_reg(&tmplmod, words[w]) < 0)
564 opr->type = OPT_LABEL;
565 if (IS_START(label, "ds:"))
567 strcpy(opr->name, label);
573 if (IS(words[w + 1], "ptr")) {
574 if (IS(words[w], "dword"))
575 opr->lmod = OPLM_DWORD;
576 else if (IS(words[w], "word"))
577 opr->lmod = OPLM_WORD;
578 else if (IS(words[w], "byte"))
579 opr->lmod = OPLM_BYTE;
581 aerr("type parsing failed\n");
583 wordc_in = wordc - w;
588 if (IS(words[w], "offset")) {
589 opr->type = OPT_OFFSET;
590 strcpy(opr->name, words[w + 1]);
593 if (IS(words[w], "(offset")) {
594 p = strchr(words[w + 1], ')');
596 aerr("parse of bracketed offset failed\n");
598 opr->type = OPT_OFFSET;
599 strcpy(opr->name, words[w + 1]);
605 aerr("parse_operand 1 word expected\n");
607 if (IS_START(words[w], "ds:"))
608 memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
609 strcpy(opr->name, words[w]);
611 if (words[w][0] == '[') {
612 opr->type = OPT_REGMEM;
613 ret = sscanf(words[w], "[%[^]]]", opr->name);
615 aerr("[] parse failure\n");
617 parse_indmode(opr->name, regmask_indirect, 1);
618 if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL)) {
620 struct parsed_equ *eq =
621 equ_find(NULL, parse_stack_el(opr->name, NULL), &i);
623 opr->lmod = eq->lmod;
627 else if (strchr(words[w], '[')) {
629 p = strchr(words[w], '[');
630 opr->type = OPT_REGMEM;
631 parse_indmode(p, regmask_indirect, 0);
632 strncpy(buf, words[w], p - words[w]);
633 buf[p - words[w]] = 0;
634 pp = proto_parse(g_fhdr, buf, 1);
637 else if (('0' <= words[w][0] && words[w][0] <= '9')
638 || words[w][0] == '-')
640 number = parse_number(words[w]);
641 opr->type = OPT_CONST;
643 printf_number(opr->name, sizeof(opr->name), number);
647 ret = parse_reg(&tmplmod, opr->name);
649 setup_reg_opr(opr, ret, tmplmod, regmask);
653 // most likely var in data segment
654 opr->type = OPT_LABEL;
655 pp = proto_parse(g_fhdr, opr->name, 0);
659 if (pp->is_fptr || pp->is_func) {
660 opr->lmod = OPLM_DWORD;
664 tmplmod = OPLM_UNSPEC;
665 if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
666 anote("unhandled C type '%s' for '%s'\n",
667 pp->type.name, opr->name);
669 if (opr->lmod == OPLM_UNSPEC) {
671 opr->type_from_var = 1;
673 else if (opr->lmod != tmplmod) {
674 opr->size_mismatch = 1;
675 if (tmplmod < opr->lmod)
678 opr->is_ptr = pp->type.is_ptr;
680 opr->is_array = pp->type.is_array;
683 if (opr->lmod == OPLM_UNSPEC)
684 guess_lmod_from_name(opr);
688 static const struct {
693 { "repe", OPF_REP|OPF_REPZ },
694 { "repz", OPF_REP|OPF_REPZ },
695 { "repne", OPF_REP|OPF_REPNZ },
696 { "repnz", OPF_REP|OPF_REPNZ },
699 static const struct {
706 { "nop", OP_NOP, 0, 0, 0 },
707 { "push", OP_PUSH, 1, 1, 0 },
708 { "pop", OP_POP, 1, 1, OPF_DATA },
709 { "mov" , OP_MOV, 2, 2, OPF_DATA },
710 { "lea", OP_LEA, 2, 2, OPF_DATA },
711 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
712 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
713 { "not", OP_NOT, 1, 1, OPF_DATA },
714 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
715 { "stosb",OP_STOS, 0, 0, OPF_DATA },
716 { "stosw",OP_STOS, 0, 0, OPF_DATA },
717 { "stosd",OP_STOS, 0, 0, OPF_DATA },
718 { "movsb",OP_MOVS, 0, 0, OPF_DATA },
719 { "movsw",OP_MOVS, 0, 0, OPF_DATA },
720 { "movsd",OP_MOVS, 0, 0, OPF_DATA },
721 { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
722 { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
723 { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
724 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
725 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
726 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
727 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
728 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
729 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
730 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
731 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
732 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
733 { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS },
734 { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS },
735 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC },
736 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC },
737 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
738 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
739 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
740 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
741 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
742 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
743 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
744 { "test", OP_TEST, 2, 2, OPF_FLAGS },
745 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
746 { "retn", OP_RET, 0, 1, OPF_TAIL },
747 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
748 { "jmp", OP_JMP, 1, 1, OPF_JMP },
749 { "jo", OP_JO, 1, 1, OPF_JMP|OPF_CC }, // 70 OF=1
750 { "jno", OP_JNO, 1, 1, OPF_JMP|OPF_CC }, // 71 OF=0
751 { "jc", OP_JC, 1, 1, OPF_JMP|OPF_CC }, // 72 CF=1
752 { "jb", OP_JC, 1, 1, OPF_JMP|OPF_CC }, // 72
753 { "jnc", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73 CF=0
754 { "jnb", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73
755 { "jae", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73
756 { "jz", OP_JZ, 1, 1, OPF_JMP|OPF_CC }, // 74 ZF=1
757 { "je", OP_JZ, 1, 1, OPF_JMP|OPF_CC }, // 74
758 { "jnz", OP_JNZ, 1, 1, OPF_JMP|OPF_CC }, // 75 ZF=0
759 { "jne", OP_JNZ, 1, 1, OPF_JMP|OPF_CC }, // 75
760 { "jbe", OP_JBE, 1, 1, OPF_JMP|OPF_CC }, // 76 CF=1 || ZF=1
761 { "jna", OP_JBE, 1, 1, OPF_JMP|OPF_CC }, // 76
762 { "ja", OP_JA, 1, 1, OPF_JMP|OPF_CC }, // 77 CF=0 && ZF=0
763 { "jnbe", OP_JA, 1, 1, OPF_JMP|OPF_CC }, // 77
764 { "js", OP_JS, 1, 1, OPF_JMP|OPF_CC }, // 78 SF=1
765 { "jns", OP_JNS, 1, 1, OPF_JMP|OPF_CC }, // 79 SF=0
766 { "jp", OP_JP, 1, 1, OPF_JMP|OPF_CC }, // 7a PF=1
767 { "jpe", OP_JP, 1, 1, OPF_JMP|OPF_CC }, // 7a
768 { "jnp", OP_JNP, 1, 1, OPF_JMP|OPF_CC }, // 7b PF=0
769 { "jpo", OP_JNP, 1, 1, OPF_JMP|OPF_CC }, // 7b
770 { "jl", OP_JL, 1, 1, OPF_JMP|OPF_CC }, // 7c SF!=OF
771 { "jnge", OP_JL, 1, 1, OPF_JMP|OPF_CC }, // 7c
772 { "jge", OP_JGE, 1, 1, OPF_JMP|OPF_CC }, // 7d SF=OF
773 { "jnl", OP_JGE, 1, 1, OPF_JMP|OPF_CC }, // 7d
774 { "jle", OP_JLE, 1, 1, OPF_JMP|OPF_CC }, // 7e ZF=1 || SF!=OF
775 { "jng", OP_JLE, 1, 1, OPF_JMP|OPF_CC }, // 7e
776 { "jg", OP_JG, 1, 1, OPF_JMP|OPF_CC }, // 7f ZF=0 && SF=OF
777 { "jnle", OP_JG, 1, 1, OPF_JMP|OPF_CC }, // 7f
778 { "seto", OP_JO, 1, 1, OPF_DATA|OPF_CC },
779 { "setno", OP_JNO, 1, 1, OPF_DATA|OPF_CC },
780 { "setc", OP_JC, 1, 1, OPF_DATA|OPF_CC },
781 { "setb", OP_JC, 1, 1, OPF_DATA|OPF_CC },
782 { "setnc", OP_JNC, 1, 1, OPF_DATA|OPF_CC },
783 { "setae", OP_JNC, 1, 1, OPF_DATA|OPF_CC },
784 { "setz", OP_JZ, 1, 1, OPF_DATA|OPF_CC },
785 { "sete", OP_JZ, 1, 1, OPF_DATA|OPF_CC },
786 { "setnz", OP_JNZ, 1, 1, OPF_DATA|OPF_CC },
787 { "setne", OP_JNZ, 1, 1, OPF_DATA|OPF_CC },
788 { "setbe", OP_JBE, 1, 1, OPF_DATA|OPF_CC },
789 { "setna", OP_JBE, 1, 1, OPF_DATA|OPF_CC },
790 { "seta", OP_JA, 1, 1, OPF_DATA|OPF_CC },
791 { "setnbe", OP_JA, 1, 1, OPF_DATA|OPF_CC },
792 { "sets", OP_JS, 1, 1, OPF_DATA|OPF_CC },
793 { "setns", OP_JNS, 1, 1, OPF_DATA|OPF_CC },
794 { "setp", OP_JP, 1, 1, OPF_DATA|OPF_CC },
795 { "setpe", OP_JP, 1, 1, OPF_DATA|OPF_CC },
796 { "setnp", OP_JNP, 1, 1, OPF_DATA|OPF_CC },
797 { "setpo", OP_JNP, 1, 1, OPF_DATA|OPF_CC },
798 { "setl", OP_JL, 1, 1, OPF_DATA|OPF_CC },
799 { "setnge", OP_JL, 1, 1, OPF_DATA|OPF_CC },
800 { "setge", OP_JGE, 1, 1, OPF_DATA|OPF_CC },
801 { "setnl", OP_JGE, 1, 1, OPF_DATA|OPF_CC },
802 { "setle", OP_JLE, 1, 1, OPF_DATA|OPF_CC },
803 { "setng", OP_JLE, 1, 1, OPF_DATA|OPF_CC },
804 { "setg", OP_JG, 1, 1, OPF_DATA|OPF_CC },
805 { "setnle", OP_JG, 1, 1, OPF_DATA|OPF_CC },
808 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
810 enum opr_lenmod lmod = OPLM_UNSPEC;
811 int prefix_flags = 0;
819 for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
820 if (IS(words[w], pref_table[i].name)) {
821 prefix_flags = pref_table[i].flags;
828 aerr("lone prefix: '%s'\n", words[0]);
833 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
834 if (IS(words[w], op_table[i].name))
838 if (i == ARRAY_SIZE(op_table))
839 aerr("unhandled op: '%s'\n", words[0]);
842 op->op = op_table[i].op;
843 op->flags = op_table[i].flags | prefix_flags;
844 op->regmask_src = op->regmask_dst = 0;
846 for (opr = 0; opr < op_table[i].minopr; opr++) {
847 regmask = regmask_ind = 0;
848 w = parse_operand(&op->operand[opr], ®mask, ®mask_ind,
849 words, wordc, w, op->flags);
851 if (opr == 0 && (op->flags & OPF_DATA))
852 op->regmask_dst = regmask;
853 // for now, mark dst as src too
854 op->regmask_src |= regmask | regmask_ind;
857 for (; w < wordc && opr < op_table[i].maxopr; opr++) {
858 w = parse_operand(&op->operand[opr],
859 &op->regmask_src, &op->regmask_src,
860 words, wordc, w, op->flags);
864 aerr("parse_op %s incomplete: %d/%d\n",
868 op->operand_cnt = opr;
869 if (!strncmp(op_table[i].name, "set", 3))
870 op->operand[0].lmod = OPLM_BYTE;
872 // ops with implicit argumets
876 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
877 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
881 if (op->operand_cnt != 0)
883 if (IS(words[op_w], "stosb"))
885 else if (IS(words[op_w], "stosw"))
887 else if (IS(words[op_w], "stosd"))
890 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
891 setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src);
892 op->regmask_dst = op->regmask_src;
893 setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD, &op->regmask_src);
898 if (op->operand_cnt != 0)
900 if (words[op_w][4] == 'b')
902 else if (words[op_w][4] == 'w')
904 else if (words[op_w][4] == 'd')
907 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
908 setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src);
909 setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src);
910 op->regmask_dst = op->regmask_src;
914 if (op->operand_cnt != 1)
919 op->regmask_dst = (1 << xDX) | (1 << xAX);
920 op->regmask_src |= (1 << xAX);
921 if (op->operand[0].lmod == OPLM_UNSPEC)
922 op->operand[0].lmod = OPLM_DWORD;
927 // we could set up operands for edx:eax, but there is no real need to
928 // (see is_opr_modified())
929 regmask = (1 << xDX) | (1 << xAX);
930 op->regmask_dst = regmask;
931 op->regmask_src |= regmask;
932 if (op->operand[0].lmod == OPLM_UNSPEC)
933 op->operand[0].lmod = OPLM_DWORD;
941 if (op->operand[1].lmod == OPLM_UNSPEC)
942 op->operand[1].lmod = OPLM_BYTE;
946 if (op->operand[0].lmod == OPLM_UNSPEC
947 && (op->operand[0].type == OPT_CONST
948 || op->operand[0].type == OPT_OFFSET
949 || op->operand[0].type == OPT_LABEL))
950 op->operand[0].lmod = OPLM_DWORD;
955 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
956 && op->operand[0].reg == xDI && op->operand[1].reg == xDI)
958 op->flags |= OPF_RMD;
963 if (op->operand[0].type == OPT_REG
964 && op->operand[1].type == OPT_REGMEM)
967 snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
968 if (IS(buf, op->operand[1].name))
969 op->flags |= OPF_RMD;
974 // trashed regs must be explicitly detected later
983 static const char *op_name(enum op_op op)
987 for (i = 0; i < ARRAY_SIZE(op_table); i++)
988 if (op_table[i].op == op)
989 return op_table[i].name;
995 static const char *dump_op(struct parsed_op *po)
997 static char out[128];
1004 snprintf(out, sizeof(out), "%s", op_name(po->op));
1005 for (i = 0; i < po->operand_cnt; i++) {
1009 snprintf(p, sizeof(out) - (p - out),
1010 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1011 po->operand[i].name);
1017 static const char *lmod_type_u(struct parsed_op *po,
1018 enum opr_lenmod lmod)
1028 ferr(po, "invalid lmod: %d\n", lmod);
1029 return "(_invalid_)";
1033 static const char *lmod_cast_u(struct parsed_op *po,
1034 enum opr_lenmod lmod)
1044 ferr(po, "invalid lmod: %d\n", lmod);
1045 return "(_invalid_)";
1049 static const char *lmod_cast_u_ptr(struct parsed_op *po,
1050 enum opr_lenmod lmod)
1060 ferr(po, "invalid lmod: %d\n", lmod);
1061 return "(_invalid_)";
1065 static const char *lmod_cast_s(struct parsed_op *po,
1066 enum opr_lenmod lmod)
1076 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1077 return "(_invalid_)";
1081 static const char *lmod_cast(struct parsed_op *po,
1082 enum opr_lenmod lmod, int is_signed)
1085 lmod_cast_s(po, lmod) :
1086 lmod_cast_u(po, lmod);
1089 static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1099 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1104 static const char *opr_name(struct parsed_op *po, int opr_num)
1106 if (opr_num >= po->operand_cnt)
1107 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1108 return po->operand[opr_num].name;
1111 static unsigned int opr_const(struct parsed_op *po, int opr_num)
1113 if (opr_num >= po->operand_cnt)
1114 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1115 if (po->operand[opr_num].type != OPT_CONST)
1116 ferr(po, "opr %d: const expected\n", opr_num);
1117 return po->operand[opr_num].val;
1120 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1122 if ((unsigned int)popr->reg >= MAX_REGS)
1123 ferr(po, "invalid reg: %d\n", popr->reg);
1124 return regs_r32[popr->reg];
1127 // cast1 is the "final" cast
1128 static const char *simplify_cast(const char *cast1, const char *cast2)
1130 static char buf[256];
1136 if (IS(cast1, cast2))
1138 if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
1140 if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
1142 if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
1144 if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
1146 if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1149 snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1153 static const char *simplify_cast_num(const char *cast, unsigned int val)
1155 if (IS(cast, "(u8)") && val < 0x100)
1157 if (IS(cast, "(s8)") && val < 0x80)
1159 if (IS(cast, "(u16)") && val < 0x10000)
1161 if (IS(cast, "(s16)") && val < 0x8000)
1163 if (IS(cast, "(s32)") && val < 0x80000000)
1169 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1178 namelen = strlen(name);
1180 p = strchr(name, '+');
1184 ferr(po, "equ parse failed for '%s'\n", name);
1186 if (IS_START(p, "0x"))
1188 *extra_offs = strtol(p, &endp, 16);
1190 ferr(po, "equ parse failed for '%s'\n", name);
1193 for (i = 0; i < g_eqcnt; i++)
1194 if (strncmp(g_eqs[i].name, name, namelen) == 0
1195 && g_eqs[i].name[namelen] == 0)
1199 ferr(po, "unresolved equ name: '%s'\n", name);
1206 static int is_stack_access(struct parsed_op *po,
1207 const struct parsed_opr *popr)
1209 return (parse_stack_el(popr->name, NULL)
1210 || (g_bp_frame && !(po->flags & OPF_EBP_S)
1211 && IS_START(popr->name, "ebp")));
1214 static void parse_stack_access(struct parsed_op *po,
1215 const char *name, char *ofs_reg, int *offset_out,
1216 int *stack_ra_out, const char **bp_arg_out)
1218 const char *bp_arg = "";
1219 const char *p = NULL;
1220 struct parsed_equ *eq;
1227 if (IS_START(name, "ebp-")
1228 || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1231 if (IS_START(p, "0x"))
1233 offset = strtoul(p, &endp, 16);
1237 ferr(po, "ebp- parse of '%s' failed\n", name);
1240 bp_arg = parse_stack_el(name, ofs_reg);
1241 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1242 eq = equ_find(po, bp_arg, &offset);
1244 ferr(po, "detected but missing eq\n");
1245 offset += eq->offset;
1248 if (!strncmp(name, "ebp", 3))
1251 if (ofs_reg[0] == 0 && stack_ra <= offset && offset < stack_ra + 4)
1252 ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
1254 *offset_out = offset;
1255 *stack_ra_out = stack_ra;
1257 *bp_arg_out = bp_arg;
1260 static void stack_frame_access(struct parsed_op *po,
1261 struct parsed_opr *popr, char *buf, size_t buf_size,
1262 const char *name, const char *cast, int is_src, int is_lea)
1264 enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1265 const char *prefix = "";
1266 const char *bp_arg = NULL;
1267 char ofs_reg[16] = { 0, };
1268 int i, arg_i, arg_s;
1275 if (po->flags & OPF_EBP_S)
1276 ferr(po, "stack_frame_access while ebp is scratch\n");
1278 parse_stack_access(po, name, ofs_reg, &offset, &stack_ra, &bp_arg);
1280 if (offset > stack_ra)
1282 arg_i = (offset - stack_ra - 4) / 4;
1283 if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
1285 if (g_func_pp->is_vararg
1286 && arg_i == g_func_pp->argc_stack && is_lea)
1288 // should be va_list
1291 snprintf(buf, buf_size, "%sap", cast);
1294 ferr(po, "offset %d (%s,%d) doesn't map to any arg\n",
1295 offset, bp_arg, arg_i);
1297 if (ofs_reg[0] != 0)
1298 ferr(po, "offset reg on arg access?\n");
1300 for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1301 if (g_func_pp->arg[i].reg != NULL)
1307 if (i == g_func_pp->argc)
1308 ferr(po, "arg %d not in prototype?\n", arg_i);
1310 popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
1316 ferr(po, "lea/byte to arg?\n");
1317 if (is_src && (offset & 3) == 0)
1318 snprintf(buf, buf_size, "%sa%d",
1319 simplify_cast(cast, "(u8)"), i + 1);
1321 snprintf(buf, buf_size, "%sBYTE%d(a%d)",
1322 cast, offset & 3, i + 1);
1327 ferr(po, "lea/word to arg?\n");
1332 ferr(po, "problematic arg store\n");
1333 snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
1334 simplify_cast(cast, "*(u16 *)"), i + 1);
1337 ferr(po, "unaligned arg word load\n");
1339 else if (is_src && (offset & 2) == 0)
1340 snprintf(buf, buf_size, "%sa%d",
1341 simplify_cast(cast, "(u16)"), i + 1);
1343 snprintf(buf, buf_size, "%s%sWORD(a%d)",
1344 cast, (offset & 2) ? "HI" : "LO", i + 1);
1356 snprintf(buf, buf_size, "(u32)&a%d + %d",
1359 ferr(po, "unaligned arg store\n");
1361 // mov edx, [ebp+arg_4+2]; movsx ecx, dx
1362 snprintf(buf, buf_size, "%s(a%d >> %d)",
1363 prefix, i + 1, (offset & 3) * 8);
1367 snprintf(buf, buf_size, "%s%sa%d",
1368 prefix, is_lea ? "&" : "", i + 1);
1373 ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
1377 snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
1380 guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
1381 if (tmp_lmod != OPLM_DWORD
1382 && (unaligned || (!is_src && tmp_lmod < popr->lmod)))
1384 ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
1385 i + 1, offset, g_func_pp->arg[i].type.name);
1387 if (popr->is_ptr && popr->lmod != OPLM_DWORD)
1388 ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
1392 if (g_stack_fsz == 0)
1393 ferr(po, "stack var access without stackframe\n");
1394 g_stack_frame_used = 1;
1396 sf_ofs = g_stack_fsz + offset;
1397 lim = (ofs_reg[0] != 0) ? -4 : 0;
1398 if (offset > 0 || sf_ofs < lim)
1399 ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
1409 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1410 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1414 if ((sf_ofs & 1) || ofs_reg[0] != 0) {
1415 // known unaligned or possibly unaligned
1416 strcat(g_comment, " unaligned");
1418 prefix = "*(u16 *)&";
1419 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1420 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1423 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
1427 if ((sf_ofs & 3) || ofs_reg[0] != 0) {
1428 // known unaligned or possibly unaligned
1429 strcat(g_comment, " unaligned");
1431 prefix = "*(u32 *)&";
1432 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1433 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1436 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
1440 ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
1445 static void check_func_pp(struct parsed_op *po,
1446 const struct parsed_proto *pp, const char *pfx)
1448 if (pp->argc_reg != 0) {
1449 if (!g_allow_regfunc)
1450 ferr(po, "%s: reg arg in arg-call unhandled yet\n", pfx);
1451 if (pp->argc_stack > 0 && pp->argc_reg != 2)
1452 ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
1453 pfx, pp->argc_reg, pp->argc_stack);
1457 static void check_label_read_ref(struct parsed_op *po, const char *name)
1459 const struct parsed_proto *pp;
1461 pp = proto_parse(g_fhdr, name, 0);
1463 ferr(po, "proto_parse failed for ref '%s'\n", name);
1466 check_func_pp(po, pp, "ref");
1469 static char *out_src_opr(char *buf, size_t buf_size,
1470 struct parsed_op *po, struct parsed_opr *popr, const char *cast,
1473 char tmp1[256], tmp2[256];
1481 switch (popr->type) {
1484 ferr(po, "lea from reg?\n");
1486 switch (popr->lmod) {
1488 snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
1491 snprintf(buf, buf_size, "%s%s",
1492 simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
1495 if (popr->name[1] == 'h') // XXX..
1496 snprintf(buf, buf_size, "%s(%s >> 8)",
1497 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1499 snprintf(buf, buf_size, "%s%s",
1500 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1503 ferr(po, "invalid src lmod: %d\n", popr->lmod);
1508 if (is_stack_access(po, popr)) {
1509 stack_frame_access(po, popr, buf, buf_size,
1510 popr->name, cast, 1, is_lea);
1514 strcpy(expr, popr->name);
1515 if (strchr(expr, '[')) {
1516 // special case: '[' can only be left for label[reg] form
1517 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
1519 ferr(po, "parse failure for '%s'\n", expr);
1520 if (tmp1[0] == '(') {
1521 // (off_4FFF50+3)[eax]
1522 p = strchr(tmp1 + 1, ')');
1523 if (p == NULL || p[1] != 0)
1524 ferr(po, "parse failure (2) for '%s'\n", expr);
1526 memmove(tmp1, tmp1 + 1, strlen(tmp1));
1528 snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
1531 // XXX: do we need more parsing?
1533 snprintf(buf, buf_size, "%s", expr);
1537 snprintf(buf, buf_size, "%s(%s)",
1538 simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
1542 check_label_read_ref(po, popr->name);
1543 if (cast[0] == 0 && popr->is_ptr)
1547 snprintf(buf, buf_size, "(u32)&%s", popr->name);
1548 else if (popr->size_lt)
1549 snprintf(buf, buf_size, "%s%s%s%s", cast,
1550 lmod_cast_u_ptr(po, popr->lmod),
1551 popr->is_array ? "" : "&",
1554 snprintf(buf, buf_size, "%s%s%s", cast, popr->name,
1555 popr->is_array ? "[0]" : "");
1559 check_label_read_ref(po, popr->name);
1563 ferr(po, "lea an offset?\n");
1564 snprintf(buf, buf_size, "%s&%s", cast, popr->name);
1569 ferr(po, "lea from const?\n");
1571 printf_number(tmp1, sizeof(tmp1), popr->val);
1572 snprintf(buf, buf_size, "%s%s",
1573 simplify_cast_num(cast, popr->val), tmp1);
1577 ferr(po, "invalid src type: %d\n", popr->type);
1583 // note: may set is_ptr (we find that out late for ebp frame..)
1584 static char *out_dst_opr(char *buf, size_t buf_size,
1585 struct parsed_op *po, struct parsed_opr *popr)
1587 switch (popr->type) {
1589 switch (popr->lmod) {
1591 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
1595 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
1599 if (popr->name[1] == 'h') // XXX..
1600 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
1602 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
1605 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
1610 if (is_stack_access(po, popr)) {
1611 stack_frame_access(po, popr, buf, buf_size,
1612 popr->name, "", 0, 0);
1616 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1619 if (popr->size_mismatch)
1620 snprintf(buf, buf_size, "%s%s%s",
1621 lmod_cast_u_ptr(po, popr->lmod),
1622 popr->is_array ? "" : "&", popr->name);
1624 snprintf(buf, buf_size, "%s%s", popr->name,
1625 popr->is_array ? "[0]" : "");
1629 ferr(po, "invalid dst type: %d\n", popr->type);
1635 static char *out_src_opr_u32(char *buf, size_t buf_size,
1636 struct parsed_op *po, struct parsed_opr *popr)
1638 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1641 static enum parsed_flag_op split_cond(struct parsed_op *po,
1642 enum op_op op, int *is_inv)
1694 ferr(po, "split_cond: bad op %d\n", op);
1699 static void out_test_for_cc(char *buf, size_t buf_size,
1700 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1701 enum opr_lenmod lmod, const char *expr)
1703 const char *cast, *scast;
1705 cast = lmod_cast_u(po, lmod);
1706 scast = lmod_cast_s(po, lmod);
1710 case PFO_BE: // CF=1||ZF=1; CF=0
1711 snprintf(buf, buf_size, "(%s%s %s 0)",
1712 cast, expr, is_inv ? "!=" : "==");
1716 case PFO_L: // SF!=OF; OF=0
1717 snprintf(buf, buf_size, "(%s%s %s 0)",
1718 scast, expr, is_inv ? ">=" : "<");
1721 case PFO_LE: // ZF=1||SF!=OF; OF=0
1722 snprintf(buf, buf_size, "(%s%s %s 0)",
1723 scast, expr, is_inv ? ">" : "<=");
1727 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1731 static void out_cmp_for_cc(char *buf, size_t buf_size,
1732 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1734 const char *cast, *scast, *cast_use;
1735 char buf1[256], buf2[256];
1736 enum opr_lenmod lmod;
1738 if (po->operand[0].lmod != po->operand[1].lmod)
1739 ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
1740 po->operand[0].lmod, po->operand[1].lmod);
1741 lmod = po->operand[0].lmod;
1743 cast = lmod_cast_u(po, lmod);
1744 scast = lmod_cast_s(po, lmod);
1760 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1763 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
1764 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
1768 // note: must be unsigned compare
1769 snprintf(buf, buf_size, "(%s %s %s)",
1770 buf1, is_inv ? ">=" : "<", buf2);
1774 snprintf(buf, buf_size, "(%s %s %s)",
1775 buf1, is_inv ? "!=" : "==", buf2);
1779 // note: must be unsigned compare
1780 snprintf(buf, buf_size, "(%s %s %s)",
1781 buf1, is_inv ? ">" : "<=", buf2);
1784 if (is_inv && lmod == OPLM_BYTE
1785 && po->operand[1].type == OPT_CONST
1786 && po->operand[1].val == 0xff)
1788 snprintf(g_comment, sizeof(g_comment), "if %s", buf);
1789 snprintf(buf, buf_size, "(0)");
1793 // note: must be signed compare
1795 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
1796 scast, buf1, buf2, is_inv ? ">=" : "<");
1800 snprintf(buf, buf_size, "(%s %s %s)",
1801 buf1, is_inv ? ">=" : "<", buf2);
1805 snprintf(buf, buf_size, "(%s %s %s)",
1806 buf1, is_inv ? ">" : "<=", buf2);
1814 static void out_cmp_test(char *buf, size_t buf_size,
1815 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1817 char buf1[256], buf2[256], buf3[256];
1819 if (po->op == OP_TEST) {
1820 if (IS(opr_name(po, 0), opr_name(po, 1))) {
1821 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
1824 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
1825 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
1826 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
1828 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
1829 po->operand[0].lmod, buf3);
1831 else if (po->op == OP_CMP) {
1832 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
1835 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
1838 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
1839 struct parsed_opr *popr2)
1841 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
1842 ferr(po, "missing lmod for both operands\n");
1844 if (popr1->lmod == OPLM_UNSPEC)
1845 popr1->lmod = popr2->lmod;
1846 else if (popr2->lmod == OPLM_UNSPEC)
1847 popr2->lmod = popr1->lmod;
1848 else if (popr1->lmod != popr2->lmod) {
1849 if (popr1->type_from_var) {
1850 popr1->size_mismatch = 1;
1851 if (popr1->lmod < popr2->lmod)
1853 popr1->lmod = popr2->lmod;
1855 else if (popr2->type_from_var) {
1856 popr2->size_mismatch = 1;
1857 if (popr2->lmod < popr1->lmod)
1859 popr2->lmod = popr1->lmod;
1862 ferr(po, "conflicting lmods: %d vs %d\n",
1863 popr1->lmod, popr2->lmod);
1867 static const char *op_to_c(struct parsed_op *po)
1891 ferr(po, "op_to_c was supplied with %d\n", po->op);
1895 static void op_set_clear_flag(struct parsed_op *po,
1896 enum op_flags flag_set, enum op_flags flag_clear)
1898 po->flags |= flag_set;
1899 po->flags &= ~flag_clear;
1902 // last op in stream - unconditional branch or ret
1903 #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
1904 || (ops[_i].flags & (OPF_JMP|OPF_CC)) == OPF_JMP)
1906 static int scan_for_pop(int i, int opcnt, const char *reg,
1907 int magic, int depth, int *maxdepth, int do_flags)
1909 const struct parsed_proto *pp;
1910 struct parsed_op *po;
1914 for (; i < opcnt; i++) {
1916 if (po->cc_scratch == magic)
1917 break; // already checked
1918 po->cc_scratch = magic;
1920 if (po->flags & OPF_TAIL) {
1921 if (po->op == OP_CALL) {
1922 pp = proto_parse(g_fhdr, po->operand[0].name, 0);
1923 if (pp != NULL && pp->is_noreturn)
1924 // no stack cleanup for noreturn
1927 return -1; // deadend
1930 if ((po->flags & OPF_RMD)
1931 || (po->op == OP_PUSH && po->argnum != 0)) // arg push
1934 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
1935 if (po->btj != NULL) {
1937 for (j = 0; j < po->btj->count - 1; j++) {
1938 ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic,
1939 depth, maxdepth, do_flags);
1941 return ret; // dead end
1943 // follow last jumptable entry
1944 i = po->btj->d[j].bt_i - 1;
1949 ferr(po, "dead branch\n");
1953 if (po->flags & OPF_CC) {
1954 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic,
1955 depth, maxdepth, do_flags);
1957 return ret; // dead end
1965 if ((po->op == OP_POP || po->op == OP_PUSH)
1966 && po->operand[0].type == OPT_REG
1967 && IS(po->operand[0].name, reg))
1969 if (po->op == OP_PUSH) {
1971 if (depth > *maxdepth)
1974 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
1976 else if (depth == 0) {
1978 op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
1983 if (depth < 0) // should not happen
1984 ferr(po, "fail with depth\n");
1986 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
1994 // scan for pop starting from 'ret' op (all paths)
1995 static int scan_for_pop_ret(int i, int opcnt, const char *reg,
2001 for (; i < opcnt; i++) {
2002 if (!(ops[i].flags & OPF_TAIL))
2005 for (j = i - 1; j >= 0; j--) {
2006 if (ops[j].flags & OPF_RMD)
2008 if (ops[j].flags & OPF_JMP)
2011 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
2012 && IS(ops[j].operand[0].name, reg))
2015 ops[j].flags |= flag_set;
2019 if (g_labels[j][0] != 0)
2024 return found ? 0 : -1;
2027 // is operand 'opr' modified by parsed_op 'po'?
2028 static int is_opr_modified(const struct parsed_opr *opr,
2029 const struct parsed_op *po)
2033 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2036 if (opr->type == OPT_REG) {
2037 if (po->op == OP_CALL) {
2038 mask = (1 << xAX) | (1 << xCX) | (1 << xDX);
2039 if ((1 << opr->reg) & mask)
2045 if (po->operand[0].type == OPT_REG) {
2046 if (po->regmask_dst & (1 << opr->reg))
2053 return IS(po->operand[0].name, opr->name);
2056 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
2057 static int is_any_opr_modified(const struct parsed_op *po_test,
2058 const struct parsed_op *po, int c_mode)
2063 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2066 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2069 if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
2072 // in reality, it can wreck any register, but in decompiled C
2073 // version it can only overwrite eax or edx:eax
2074 mask = (1 << xAX) | (1 << xDX);
2078 if (po->op == OP_CALL
2079 && ((po_test->regmask_src | po_test->regmask_dst) & mask))
2082 for (i = 0; i < po_test->operand_cnt; i++)
2083 if (IS(po_test->operand[i].name, po->operand[0].name))
2089 // scan for any po_test operand modification in range given
2090 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
2093 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2096 for (; i < opcnt; i++) {
2097 if (is_any_opr_modified(po_test, &ops[i], c_mode))
2104 // scan for po_test operand[0] modification in range given
2105 static int scan_for_mod_opr0(struct parsed_op *po_test,
2108 for (; i < opcnt; i++) {
2109 if (is_opr_modified(&po_test->operand[0], &ops[i]))
2116 static int scan_for_flag_set(int i, int magic, int *branched,
2117 int *setters, int *setter_cnt)
2119 struct label_ref *lr;
2123 if (ops[i].cc_scratch == magic) {
2124 ferr(&ops[i], "%s looped\n", __func__);
2127 ops[i].cc_scratch = magic;
2129 if (g_labels[i][0] != 0) {
2132 lr = &g_label_refs[i];
2133 for (; lr->next; lr = lr->next) {
2134 ret = scan_for_flag_set(lr->i, magic,
2135 branched, setters, setter_cnt);
2140 if (i > 0 && LAST_OP(i - 1)) {
2144 ret = scan_for_flag_set(lr->i, magic,
2145 branched, setters, setter_cnt);
2151 if (ops[i].flags & OPF_FLAGS) {
2152 setters[*setter_cnt] = i;
2157 if ((ops[i].flags & OPF_JMP) && !(ops[i].flags & OPF_CC))
2164 // scan back for cdq, if anything modifies edx, fail
2165 static int scan_for_cdq_edx(int i)
2168 if (g_labels[i][0] != 0) {
2169 if (g_label_refs[i].next != NULL)
2171 if (i > 0 && LAST_OP(i - 1)) {
2172 i = g_label_refs[i].i;
2179 if (ops[i].op == OP_CDQ)
2182 if (ops[i].regmask_dst & (1 << xDX))
2184 if (g_labels[i][0] != 0)
2191 static int scan_for_reg_clear(int i, int reg)
2194 if (g_labels[i][0] != 0) {
2195 if (g_label_refs[i].next != NULL)
2197 if (i > 0 && LAST_OP(i - 1)) {
2198 i = g_label_refs[i].i;
2205 if (ops[i].op == OP_XOR
2206 && ops[i].operand[0].lmod == OPLM_DWORD
2207 && ops[i].operand[0].reg == ops[i].operand[1].reg
2208 && ops[i].operand[0].reg == reg)
2211 if (ops[i].regmask_dst & (1 << reg))
2213 if (g_labels[i][0] != 0)
2220 // scan for positive, constant esp adjust
2221 static int scan_for_esp_adjust(int i, int opcnt, int *adj)
2223 const struct parsed_proto *pp;
2224 struct parsed_op *po;
2228 for (; i < opcnt; i++) {
2231 if (g_labels[i][0] != 0)
2234 if (po->op == OP_ADD && po->operand[0].reg == xSP) {
2235 if (po->operand[1].type != OPT_CONST)
2236 ferr(&ops[i], "non-const esp adjust?\n");
2237 *adj += po->operand[1].val;
2239 ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
2242 else if (po->op == OP_PUSH) {
2243 if (first_pop == -1)
2244 first_pop = -2; // none
2245 *adj -= lmod_bytes(po, po->operand[0].lmod);
2247 else if (po->op == OP_POP) {
2248 if (first_pop == -1)
2250 *adj += lmod_bytes(po, po->operand[0].lmod);
2252 else if (po->flags & (OPF_JMP|OPF_TAIL)) {
2253 if (po->op != OP_CALL)
2255 if (po->operand[0].type != OPT_LABEL)
2258 if (pp != NULL && pp->is_stdcall)
2263 if (*adj == 4 && first_pop >= 0 && ops[first_pop].op == OP_POP
2264 && ops[first_pop].operand[0].type == OPT_REG
2265 && ops[first_pop].operand[0].reg == xCX)
2267 // probably 'pop ecx' was used..
2274 static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
2276 struct parsed_op *po;
2280 ferr(ops, "%s: followed bad branch?\n", __func__);
2282 for (; i < opcnt; i++) {
2284 if (po->cc_scratch == magic)
2286 po->cc_scratch = magic;
2289 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2290 if (po->btj != NULL) {
2292 for (j = 0; j < po->btj->count; j++)
2293 scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
2297 scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
2298 if (!(po->flags & OPF_CC))
2301 if (po->flags & OPF_TAIL)
2306 static const struct parsed_proto *try_recover_pp(
2307 struct parsed_op *po, const struct parsed_opr *opr)
2309 const struct parsed_proto *pp = NULL;
2313 // maybe an arg of g_func?
2314 if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
2316 char ofs_reg[16] = { 0, };
2317 int arg, arg_s, arg_i;
2321 parse_stack_access(po, opr->name, ofs_reg,
2322 &offset, &stack_ra, NULL);
2323 if (ofs_reg[0] != 0)
2324 ferr(po, "offset reg on arg access?\n");
2325 if (offset <= stack_ra)
2326 ferr(po, "stack var call unhandled yet\n");
2328 arg_i = (offset - stack_ra - 4) / 4;
2329 for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
2330 if (g_func_pp->arg[arg].reg != NULL)
2336 if (arg == g_func_pp->argc)
2337 ferr(po, "stack arg %d not in prototype?\n", arg_i);
2339 pp = g_func_pp->arg[arg].fptr;
2341 ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1);
2342 check_func_pp(po, pp, "icall arg");
2344 else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
2346 p = strchr(opr->name + 1, '[');
2347 memcpy(buf, opr->name, p - opr->name);
2348 buf[p - opr->name] = 0;
2349 pp = proto_parse(g_fhdr, buf, 0);
2351 else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
2352 pp = proto_parse(g_fhdr, opr->name, 0);
2354 ferr(po, "proto_parse failed for icall from '%s'\n", opr->name);
2355 check_func_pp(po, pp, "reg-fptr ref");
2361 static void scan_for_call_type(int i, struct parsed_opr *opr,
2362 int magic, const struct parsed_proto **pp_found, int *multi)
2364 const struct parsed_proto *pp = NULL;
2365 struct parsed_op *po;
2366 struct label_ref *lr;
2369 if (ops[i].cc_scratch == magic)
2371 ops[i].cc_scratch = magic;
2373 if (g_labels[i][0] != 0) {
2374 lr = &g_label_refs[i];
2375 for (; lr != NULL; lr = lr->next)
2376 scan_for_call_type(lr->i, opr, magic, pp_found, multi);
2377 if (i > 0 && LAST_OP(i - 1))
2382 if (!(ops[i].flags & OPF_DATA))
2384 if (!is_opr_modified(opr, &ops[i]))
2386 if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
2387 // most probably trashed by some processing
2392 opr = &ops[i].operand[1];
2393 if (opr->type != OPT_REG)
2397 po = (i >= 0) ? &ops[i] : ops;
2400 // reached the top - can only be an arg-reg
2401 if (opr->type != OPT_REG)
2404 for (i = 0; i < g_func_pp->argc; i++) {
2405 if (g_func_pp->arg[i].reg == NULL)
2407 if (IS(opr->name, g_func_pp->arg[i].reg))
2410 if (i == g_func_pp->argc)
2412 pp = g_func_pp->arg[i].fptr;
2414 ferr(po, "icall: arg%d (%s) is not a fptr?\n",
2415 i + 1, g_func_pp->arg[i].reg);
2416 check_func_pp(po, pp, "icall reg-arg");
2419 pp = try_recover_pp(po, opr);
2421 if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
2422 if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
2423 || (*pp_found)->is_stdcall != pp->is_stdcall
2424 || (*pp_found)->is_fptr != pp->is_fptr
2425 || (*pp_found)->argc != pp->argc
2426 || (*pp_found)->argc_reg != pp->argc_reg
2427 || (*pp_found)->argc_stack != pp->argc_stack)
2429 ferr(po, "icall: parsed_proto mismatch\n");
2437 static const struct parsed_proto *resolve_icall(int i, int opcnt,
2440 const struct parsed_proto *pp = NULL;
2444 switch (ops[i].operand[0].type) {
2448 pp = try_recover_pp(&ops[i], &ops[i].operand[0]);
2451 scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
2459 static int collect_call_args_r(struct parsed_op *po, int i,
2460 struct parsed_proto *pp, int *regmask, int *save_arg_vars, int arg,
2461 int magic, int need_op_saving, int may_reuse)
2463 struct parsed_proto *pp_tmp;
2464 struct label_ref *lr;
2465 int need_to_save_current;
2470 ferr(po, "dead label encountered\n");
2474 for (; arg < pp->argc; arg++)
2475 if (pp->arg[arg].reg == NULL)
2477 magic = (magic & 0xffffff) | (arg << 24);
2479 for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
2481 if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
2482 if (ops[j].cc_scratch != magic) {
2483 ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
2487 // ok: have already been here
2490 ops[j].cc_scratch = magic;
2492 if (g_labels[j][0] != 0 && g_label_refs[j].i != -1) {
2493 lr = &g_label_refs[j];
2494 if (lr->next != NULL)
2496 for (; lr->next; lr = lr->next) {
2497 if ((ops[lr->i].flags & (OPF_JMP|OPF_CC)) != OPF_JMP)
2499 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2500 arg, magic, need_op_saving, may_reuse);
2505 if ((ops[lr->i].flags & (OPF_JMP|OPF_CC)) != OPF_JMP)
2507 if (j > 0 && LAST_OP(j - 1)) {
2508 // follow last branch in reverse
2513 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2514 arg, magic, need_op_saving, may_reuse);
2520 if (ops[j].op == OP_CALL)
2522 if (pp->is_unresolved)
2525 pp_tmp = ops[j].datap;
2527 ferr(po, "arg collect hit unparsed call '%s'\n",
2528 ops[j].operand[0].name);
2529 if (may_reuse && pp_tmp->argc_stack > 0)
2530 ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
2531 arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
2533 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP) {
2534 if (pp->is_unresolved)
2537 ferr(po, "arg collect %d/%d hit esp adjust\n",
2540 else if (ops[j].op == OP_POP) {
2541 if (pp->is_unresolved)
2544 ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc);
2546 else if ((ops[j].flags & (OPF_JMP|OPF_CC)) == (OPF_JMP|OPF_CC))
2548 if (pp->is_unresolved)
2553 else if (ops[j].op == OP_PUSH && !(ops[j].flags & OPF_FARG))
2555 if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
2558 pp->arg[arg].datap = &ops[j];
2559 need_to_save_current = 0;
2560 if (!need_op_saving) {
2561 ret = scan_for_mod(&ops[j], j + 1, i, 1);
2562 need_to_save_current = (ret >= 0);
2564 if (need_op_saving || need_to_save_current) {
2565 // mark this push as one that needs operand saving
2566 ops[j].flags &= ~OPF_RMD;
2567 if (ops[j].argnum == 0) {
2568 ops[j].argnum = arg + 1;
2569 *save_arg_vars |= 1 << arg;
2571 else if (ops[j].argnum < arg + 1)
2572 ferr(&ops[j], "argnum conflict (%d<%d) for '%s'\n",
2573 ops[j].argnum, arg + 1, pp->name);
2575 else if (ops[j].argnum == 0)
2576 ops[j].flags |= OPF_RMD;
2578 // some PUSHes are reused by different calls on other branches,
2579 // but that can't happen if we didn't branch, so they
2580 // can be removed from future searches (handles nested calls)
2582 ops[j].flags |= OPF_FARG;
2585 if (!pp->is_unresolved) {
2587 for (; arg < pp->argc; arg++)
2588 if (pp->arg[arg].reg == NULL)
2591 magic = (magic & 0xffffff) | (arg << 24);
2593 // tracking reg usage
2594 if (ops[j].operand[0].type == OPT_REG)
2595 *regmask |= 1 << ops[j].operand[0].reg;
2599 if (arg < pp->argc) {
2600 ferr(po, "arg collect failed for '%s': %d/%d\n",
2601 pp->name, arg, pp->argc);
2608 static int collect_call_args(struct parsed_op *po, int i,
2609 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
2615 ret = collect_call_args_r(po, i, pp, regmask, save_arg_vars,
2620 if (pp->is_unresolved) {
2622 pp->argc_stack += ret;
2623 for (a = 0; a < pp->argc; a++)
2624 if (pp->arg[a].type.name == NULL)
2625 pp->arg[a].type.name = strdup("int");
2631 static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
2635 for (i = 0; i < pp->argc; i++)
2636 if (pp->arg[i].reg == NULL)
2640 memmove(&pp->arg[i + 1], &pp->arg[i],
2641 sizeof(pp->arg[0]) * pp->argc_stack);
2642 memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
2643 pp->arg[i].reg = strdup(reg);
2644 pp->arg[i].type.name = strdup("int");
2649 static void add_label_ref(struct label_ref *lr, int op_i)
2651 struct label_ref *lr_new;
2658 lr_new = calloc(1, sizeof(*lr_new));
2660 lr_new->next = lr->next;
2664 static void output_std_flags(FILE *fout, struct parsed_op *po,
2665 int *pfomask, const char *dst_opr_text)
2667 if (*pfomask & (1 << PFO_Z)) {
2668 fprintf(fout, "\n cond_z = (%s%s == 0);",
2669 lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
2670 *pfomask &= ~(1 << PFO_Z);
2672 if (*pfomask & (1 << PFO_S)) {
2673 fprintf(fout, "\n cond_s = (%s%s < 0);",
2674 lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
2675 *pfomask &= ~(1 << PFO_S);
2679 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
2681 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
2682 struct parsed_opr *last_arith_dst = NULL;
2683 char buf1[256], buf2[256], buf3[256], cast[64];
2684 const struct parsed_proto *pp_c;
2685 struct parsed_proto *pp;
2686 struct parsed_data *pd;
2687 const char *tmpname;
2688 enum parsed_flag_op pfo;
2689 int save_arg_vars = 0;
2690 int cmp_result_vars = 0;
2691 int need_mul_var = 0;
2693 int label_pending = 0;
2694 int regmask_save = 0;
2695 int regmask_arg = 0;
2696 int regmask_now = 0;
2708 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
2709 g_stack_frame_used = 0;
2711 g_func_pp = proto_parse(fhdr, funcn, 0);
2712 if (g_func_pp == NULL)
2713 ferr(ops, "proto_parse failed for '%s'\n", funcn);
2715 fprintf(fout, "%s ", g_func_pp->ret_type.name);
2716 if (g_func_pp->is_stdcall && g_func_pp->argc_reg == 0)
2717 fprintf(fout, "__stdcall ");
2718 if (g_ida_func_attr & IDAFA_NORETURN)
2719 fprintf(fout, "noreturn ");
2720 fprintf(fout, "%s(", funcn);
2722 for (i = 0; i < g_func_pp->argc; i++) {
2724 fprintf(fout, ", ");
2725 if (g_func_pp->arg[i].fptr != NULL) {
2727 pp = g_func_pp->arg[i].fptr;
2728 fprintf(fout, "%s (", pp->ret_type.name);
2729 if (pp->is_stdcall && pp->argc_reg == 0)
2730 fprintf(fout, "__stdcall ");
2731 fprintf(fout, "*a%d)(", i + 1);
2732 for (j = 0; j < pp->argc; j++) {
2734 fprintf(fout, ", ");
2735 if (pp->arg[j].fptr)
2736 ferr(ops, "nested fptr\n");
2737 fprintf(fout, "%s", pp->arg[j].type.name);
2742 fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1);
2744 if (g_func_pp->arg[i].reg != NULL) {
2745 reg = char_array_i(regs_r32,
2746 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
2748 ferr(ops, "arg '%s' is not a reg?\n", g_func_pp->arg[i].reg);
2749 regmask_arg |= 1 << reg;
2752 if (g_func_pp->is_vararg) {
2754 fprintf(fout, ", ");
2755 fprintf(fout, "...");
2758 fprintf(fout, ")\n{\n");
2761 // - handle ebp/esp frame, remove ops related to it
2762 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
2763 && ops[1].op == OP_MOV
2764 && IS(opr_name(&ops[1], 0), "ebp")
2765 && IS(opr_name(&ops[1], 1), "esp"))
2770 ops[0].flags |= OPF_RMD;
2771 ops[1].flags |= OPF_RMD;
2774 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
2775 g_stack_fsz = opr_const(&ops[2], 1);
2776 ops[2].flags |= OPF_RMD;
2780 // another way msvc builds stack frame..
2782 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
2784 ops[i].flags |= OPF_RMD;
2788 // and another way..
2789 if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
2790 && ops[i].operand[1].type == OPT_CONST
2791 && ops[i + 1].op == OP_CALL
2792 && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
2794 g_stack_fsz += ops[i].operand[1].val;
2795 ops[i].flags |= OPF_RMD;
2797 ops[i].flags |= OPF_RMD;
2804 for (; i < opcnt; i++)
2805 if (ops[i].op == OP_RET)
2807 if (i == opcnt && (ops[i - 1].flags & OPF_JMP) && found)
2810 if (ops[i - 1].op == OP_POP && IS(opr_name(&ops[i - 1], 0), "ebp"))
2811 ops[i - 1].flags |= OPF_RMD;
2812 else if (!(g_ida_func_attr & IDAFA_NORETURN))
2813 ferr(&ops[i - 1], "'pop ebp' expected\n");
2815 if (g_stack_fsz != 0) {
2816 if (ops[i - 2].op == OP_MOV
2817 && IS(opr_name(&ops[i - 2], 0), "esp")
2818 && IS(opr_name(&ops[i - 2], 1), "ebp"))
2820 ops[i - 2].flags |= OPF_RMD;
2822 else if (!(g_ida_func_attr & IDAFA_NORETURN))
2823 ferr(&ops[i - 2], "esp restore expected\n");
2825 if (ecx_push && ops[i - 3].op == OP_POP
2826 && IS(opr_name(&ops[i - 3], 0), "ecx"))
2828 ferr(&ops[i - 3], "unexpected ecx pop\n");
2834 } while (i < opcnt);
2837 for (i = 0; i < opcnt; i++) {
2838 if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
2840 if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
2841 && ops[i].operand[1].type == OPT_CONST)
2850 g_stack_fsz = ops[i].operand[1].val;
2851 ops[i].flags |= OPF_RMD;
2855 for (; i < opcnt; i++)
2856 if (ops[i].op == OP_RET)
2858 if (ops[i - 1].op != OP_ADD
2859 || !IS(opr_name(&ops[i - 1], 0), "esp")
2860 || ops[i - 1].operand[1].type != OPT_CONST
2861 || ops[i - 1].operand[1].val != g_stack_fsz)
2862 ferr(&ops[i - 1], "'add esp' expected\n");
2863 ops[i - 1].flags |= OPF_RMD;
2866 } while (i < opcnt);
2871 // - parse calls with labels
2872 // - resolve all branches
2873 for (i = 0; i < opcnt; i++)
2879 if (po->flags & OPF_RMD)
2882 if (po->op == OP_CALL) {
2883 if (po->operand[0].type == OPT_LABEL) {
2884 tmpname = opr_name(po, 0);
2885 if (IS_START(tmpname, "loc_"))
2886 ferr(po, "call to loc_*\n");
2887 pp_c = proto_parse(fhdr, tmpname, 0);
2889 ferr(po, "proto_parse failed for call '%s'\n", tmpname);
2890 if (pp_c->is_fptr && pp_c->argc_reg != 0)
2891 ferr(po, "fptr call with reg arg\n");
2892 pp = proto_clone(pp_c);
2893 my_assert_not(pp, NULL);
2899 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
2902 if (po->operand[0].type == OPT_REGMEM) {
2903 char *p = strchr(po->operand[0].name, '[');
2906 ret = p - po->operand[0].name;
2907 strncpy(buf1, po->operand[0].name, ret);
2910 for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
2911 if (IS(g_func_pd[j].label, buf1)) {
2917 //ferr(po, "label '%s' not parsed?\n", buf1);
2919 if (pd->type != OPT_OFFSET)
2920 ferr(po, "label '%s' with non-offset data?\n", buf1);
2922 // find all labels, link
2923 for (j = 0; j < pd->count; j++) {
2924 for (l = 0; l < opcnt; l++) {
2925 if (g_labels[l][0] && IS(g_labels[l], pd->d[j].u.label)) {
2926 add_label_ref(&g_label_refs[l], i);
2937 for (l = 0; l < opcnt; l++) {
2938 if (g_labels[l][0] && IS(po->operand[0].name, g_labels[l])) {
2939 add_label_ref(&g_label_refs[l], i);
2948 if (po->operand[0].type == OPT_LABEL)
2952 ferr(po, "unhandled branch\n");
2956 po->flags |= OPF_TAIL;
2961 // - remove dead labels
2963 for (i = 0; i < opcnt; i++)
2965 if (g_labels[i][0] != 0 && g_label_refs[i].i == -1)
2969 if (po->flags & OPF_RMD)
2972 if (po->op == OP_CALL)
2974 tmpname = opr_name(po, 0);
2979 pp_c = resolve_icall(i, opcnt, &l);
2981 pp = proto_clone(pp_c);
2982 my_assert_not(pp, NULL);
2984 // not resolved just to single func
2988 pp = calloc(1, sizeof(*pp));
2989 my_assert_not(pp, NULL);
2991 ret = scan_for_esp_adjust(i + 1, opcnt, &j);
2993 if (!g_allow_regfunc)
2994 ferr(po, "non-__cdecl indirect call unhandled yet\n");
2995 pp->is_unresolved = 1;
2999 if (j > ARRAY_SIZE(pp->arg))
3000 ferr(po, "esp adjust too large: %d\n", j);
3001 pp->ret_type.name = strdup("int");
3002 pp->argc = pp->argc_stack = j;
3003 for (arg = 0; arg < pp->argc; arg++)
3004 pp->arg[arg].type.name = strdup("int");
3009 // look for and make use of esp adjust
3011 if (!pp->is_stdcall && pp->argc_stack > 0)
3012 ret = scan_for_esp_adjust(i + 1, opcnt, &j);
3014 if (pp->is_vararg) {
3015 if (j / 4 < pp->argc_stack)
3016 ferr(po, "esp adjust is too small: %x < %x\n",
3017 j, pp->argc_stack * 4);
3018 // modify pp to make it have varargs as normal args
3020 pp->argc += j / 4 - pp->argc_stack;
3021 for (; arg < pp->argc; arg++) {
3022 pp->arg[arg].type.name = strdup("int");
3025 if (pp->argc > ARRAY_SIZE(pp->arg))
3026 ferr(po, "too many args for '%s'\n", tmpname);
3028 if (pp->argc_stack != j / 4)
3029 ferr(po, "stack tracking failed for '%s': %x %x\n",
3030 tmpname, pp->argc_stack * 4, j);
3032 ops[ret].flags |= OPF_RMD;
3033 // a bit of a hack, but deals with use of
3034 // single adj for multiple calls
3035 ops[ret].operand[1].val -= j;
3037 else if (pp->is_vararg)
3038 ferr(po, "missing esp_adjust for vararg func '%s'\n",
3041 for (arg = 0; arg < pp->argc; arg++) {
3042 if (pp->arg[arg].reg != NULL) {
3043 reg = char_array_i(regs_r32,
3044 ARRAY_SIZE(regs_r32), pp->arg[arg].reg);
3046 ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg);
3047 regmask |= 1 << reg;
3050 if (pp->arg[arg].fptr != NULL) {
3051 // can't call functions with non-__cdecl callbacks yet
3052 struct parsed_proto *pp_tmp;
3053 pp_tmp = pp->arg[arg].fptr;
3054 if (pp_tmp->argc_reg != 0)
3055 ferr(po, "'%s' has a callback with reg-args\n", tmpname);
3060 if (!pp->is_unresolved) {
3061 // since we know the args, collect them
3062 collect_call_args(po, i, pp, ®mask, &save_arg_vars,
3066 if (strstr(pp->ret_type.name, "int64"))
3072 // - find POPs for PUSHes, rm both
3073 // - scan for all used registers
3074 // - find flag set ops for their users
3075 // - do unreselved calls
3076 // - declare indirect functions
3077 for (i = 0; i < opcnt; i++) {
3079 if (po->flags & OPF_RMD)
3082 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
3083 reg = po->operand[0].reg;
3084 if (!(regmask & (1 << reg)))
3085 // not a reg save after all, rerun scan_for_pop
3086 po->flags &= ~OPF_RSAVE;
3088 regmask_save |= 1 << reg;
3091 if (po->op == OP_PUSH
3092 && po->argnum == 0 && !(po->flags & OPF_RSAVE)
3093 && po->operand[0].type == OPT_REG)
3095 reg = po->operand[0].reg;
3097 ferr(po, "reg not set for push?\n");
3100 ret = scan_for_pop(i + 1, opcnt,
3101 po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
3104 ferr(po, "too much depth: %d\n", depth);
3106 po->flags |= OPF_RMD;
3107 scan_for_pop(i + 1, opcnt, po->operand[0].name,
3108 i + opcnt * 4, 0, &depth, 1);
3111 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
3114 if (regmask & (1 << reg)) {
3115 if (regmask_save & (1 << reg))
3116 ferr(po, "%s already saved?\n", po->operand[0].name);
3120 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg);
3125 regmask_now = po->regmask_src | po->regmask_dst;
3126 if (regmask_now & (1 << xBP)) {
3127 if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
3128 if (po->regmask_dst & (1 << xBP))
3129 // compiler decided to drop bp frame and use ebp as scratch
3130 scan_fwd_set_flags(i, opcnt, i + opcnt * 5, OPF_EBP_S);
3132 regmask_now &= ~(1 << xBP);
3136 regmask |= regmask_now;
3138 if (po->flags & OPF_CC)
3140 int setters[16], cnt = 0, branched = 0;
3142 ret = scan_for_flag_set(i, i + opcnt * 6,
3143 &branched, setters, &cnt);
3144 if (ret < 0 || cnt <= 0)
3145 ferr(po, "unable to trace flag setter(s)\n");
3146 if (cnt > ARRAY_SIZE(setters))
3147 ferr(po, "too many flag setters\n");
3149 pfo = split_cond(po, po->op, &dummy);
3150 for (j = 0; j < cnt; j++)
3152 tmp_op = &ops[setters[j]]; // flag setter
3155 // to get nicer code, we try to delay test and cmp;
3156 // if we can't because of operand modification, or if we
3157 // have math op, or branch, make it calculate flags explicitly
3158 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP) {
3159 if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
3162 else if (tmp_op->op == OP_CMPS) {
3163 pfomask = 1 << PFO_Z;
3166 // see if we'll be able to handle based on op result
3167 if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
3168 && pfo != PFO_Z && pfo != PFO_S && pfo != PFO_P)
3170 || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
3174 tmp_op->pfomask |= pfomask;
3175 cmp_result_vars |= pfomask;
3177 // note: may overwrite, currently not a problem
3181 if (po->op == OP_ADC || po->op == OP_SBB)
3182 cmp_result_vars |= 1 << PFO_C;
3184 else if (po->op == OP_MUL
3185 || (po->op == OP_IMUL && po->operand_cnt == 1))
3189 else if (po->op == OP_CALL) {
3192 ferr(po, "NULL pp\n");
3194 if (pp->is_unresolved) {
3195 int regmask_push = 0;
3196 collect_call_args(po, i, pp, ®mask_push, &save_arg_vars,
3199 if (!((regmask_push & (1 << xCX))
3200 && (regmask_push & (1 << xDX))))
3202 if (pp->argc_stack != 0
3203 || ((regmask | regmask_arg) & (1 << xCX)))
3205 pp_insert_reg_arg(pp, "ecx");
3206 regmask |= 1 << xCX;
3208 if (pp->argc_stack != 0
3209 || ((regmask | regmask_arg) & (1 << xDX)))
3211 pp_insert_reg_arg(pp, "edx");
3212 regmask |= 1 << xDX;
3215 regmask |= regmask_push;
3219 fprintf(fout, " %s (", pp->ret_type.name);
3220 if (pp->is_stdcall && pp->argc_reg == 0)
3221 fprintf(fout, "__stdcall ");
3222 fprintf(fout, "*icall%d)(", i);
3223 for (j = 0; j < pp->argc; j++) {
3225 fprintf(fout, ", ");
3226 fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1);
3228 fprintf(fout, ");\n");
3231 else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void"))
3232 regmask |= 1 << xAX;
3235 // output LUTs/jumptables
3236 for (i = 0; i < g_func_pd_cnt; i++) {
3238 fprintf(fout, " static const ");
3239 if (pd->type == OPT_OFFSET) {
3240 fprintf(fout, "void *jt_%s[] =\n { ", pd->label);
3242 for (j = 0; j < pd->count; j++) {
3244 fprintf(fout, ", ");
3245 fprintf(fout, "&&%s", pd->d[j].u.label);
3249 fprintf(fout, "%s %s[] =\n { ",
3250 lmod_type_u(ops, pd->lmod), pd->label);
3252 for (j = 0; j < pd->count; j++) {
3254 fprintf(fout, ", ");
3255 fprintf(fout, "%u", pd->d[j].u.val);
3258 fprintf(fout, " };\n");
3261 // declare stack frame, va_arg
3263 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
3264 (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz);
3266 if (g_func_pp->is_vararg)
3267 fprintf(fout, " va_list ap;\n");
3269 // declare arg-registers
3270 for (i = 0; i < g_func_pp->argc; i++) {
3271 if (g_func_pp->arg[i].reg != NULL) {
3272 reg = char_array_i(regs_r32,
3273 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
3274 if (regmask & (1 << reg)) {
3275 fprintf(fout, " u32 %s = (u32)a%d;\n",
3276 g_func_pp->arg[i].reg, i + 1);
3279 fprintf(fout, " // %s = a%d; // unused\n",
3280 g_func_pp->arg[i].reg, i + 1);
3285 regmask_now = regmask & ~regmask_arg;
3286 regmask_now &= ~(1 << xSP);
3288 for (reg = 0; reg < 8; reg++) {
3289 if (regmask_now & (1 << reg)) {
3290 fprintf(fout, " u32 %s;\n", regs_r32[reg]);
3297 for (reg = 0; reg < 8; reg++) {
3298 if (regmask_save & (1 << reg)) {
3299 fprintf(fout, " u32 s_%s;\n", regs_r32[reg]);
3305 if (save_arg_vars) {
3306 for (reg = 0; reg < 32; reg++) {
3307 if (save_arg_vars & (1 << reg)) {
3308 fprintf(fout, " u32 s_a%d;\n", reg + 1);
3314 if (cmp_result_vars) {
3315 for (i = 0; i < 8; i++) {
3316 if (cmp_result_vars & (1 << i)) {
3317 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
3324 fprintf(fout, " u64 mul_tmp;\n");
3329 fprintf(fout, "\n");
3331 if (g_func_pp->is_vararg) {
3332 if (g_func_pp->argc_stack == 0)
3333 ferr(ops, "vararg func without stack args?\n");
3334 fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc);
3338 for (i = 0; i < opcnt; i++)
3340 if (g_labels[i][0] != 0) {
3341 fprintf(fout, "\n%s:\n", g_labels[i]);
3344 delayed_flag_op = NULL;
3345 last_arith_dst = NULL;
3349 if (po->flags & OPF_RMD)
3354 #define assert_operand_cnt(n_) \
3355 if (po->operand_cnt != n_) \
3356 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
3358 // conditional/flag using op?
3359 if (po->flags & OPF_CC)
3364 pfo = split_cond(po, po->op, &is_inv);
3367 // we go through all this trouble to avoid using parsed_flag_op,
3368 // which makes generated code much nicer
3369 if (delayed_flag_op != NULL)
3371 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op, pfo, is_inv);
3374 else if (last_arith_dst != NULL
3375 && (pfo == PFO_Z || pfo == PFO_S || pfo == PFO_P
3376 || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
3379 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
3380 out_test_for_cc(buf1, sizeof(buf1), po, pfo, is_inv,
3381 last_arith_dst->lmod, buf3);
3384 else if (tmp_op != NULL) {
3385 // use preprocessed flag calc results
3386 if (!(tmp_op->pfomask & (1 << pfo)))
3387 ferr(po, "not prepared for pfo %d\n", pfo);
3389 // note: is_inv was not yet applied
3390 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
3391 is_inv ? "!" : "", parsed_flag_op_names[pfo]);
3394 ferr(po, "all methods of finding comparison failed\n");
3397 if (po->flags & OPF_JMP) {
3398 fprintf(fout, " if %s\n", buf1);
3400 else if (po->op == OP_ADC || po->op == OP_SBB) {
3402 fprintf(fout, " cond_%s = %s;\n",
3403 parsed_flag_op_names[pfo], buf1);
3405 else if (po->flags & OPF_DATA) { // SETcc
3406 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
3407 fprintf(fout, " %s = %s;", buf2, buf1);
3410 ferr(po, "unhandled conditional op\n");
3414 pfomask = po->pfomask;
3419 assert_operand_cnt(2);
3420 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3421 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3422 fprintf(fout, " %s = %s;", buf1,
3423 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3424 po->operand[0].is_ptr ? "(void *)" : "", 0));
3428 assert_operand_cnt(2);
3429 po->operand[1].lmod = OPLM_DWORD; // always
3430 fprintf(fout, " %s = %s;",
3431 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3432 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3437 assert_operand_cnt(2);
3438 fprintf(fout, " %s = %s;",
3439 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3440 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3444 assert_operand_cnt(2);
3445 switch (po->operand[1].lmod) {
3447 strcpy(buf3, "(s8)");
3450 strcpy(buf3, "(s16)");
3453 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
3455 fprintf(fout, " %s = %s;",
3456 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3457 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3462 assert_operand_cnt(1);
3463 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3464 fprintf(fout, " %s = ~%s;", buf1, buf1);
3468 assert_operand_cnt(2);
3469 fprintf(fout, " %s = (s32)%s >> 31;",
3470 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3471 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3472 strcpy(g_comment, "cdq");
3477 assert_operand_cnt(3);
3478 if (po->flags & OPF_REP) {
3479 fprintf(fout, " for (; ecx != 0; ecx--, edi += %d)\n",
3480 lmod_bytes(po, po->operand[0].lmod));
3481 fprintf(fout, " %sedi = eax;",
3482 lmod_cast_u_ptr(po, po->operand[0].lmod));
3483 strcpy(g_comment, "rep stos");
3486 fprintf(fout, " %sedi = eax; edi += %d;",
3487 lmod_cast_u_ptr(po, po->operand[0].lmod),
3488 lmod_bytes(po, po->operand[0].lmod));
3489 strcpy(g_comment, "stos");
3495 assert_operand_cnt(3);
3496 j = lmod_bytes(po, po->operand[0].lmod);
3497 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
3498 if (po->flags & OPF_REP) {
3500 " for (; ecx != 0; ecx--, edi += %d, esi += %d)\n",
3503 " %sedi = %sesi;", buf1, buf1);
3504 strcpy(g_comment, "rep movs");
3507 fprintf(fout, " %sedi = %sesi; edi += %d; esi += %d;",
3509 strcpy(g_comment, "movs");
3515 // repe ~ repeat while ZF=1
3516 assert_operand_cnt(3);
3517 j = lmod_bytes(po, po->operand[0].lmod);
3518 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
3519 if (po->flags & OPF_REP) {
3521 " for (; ecx != 0; ecx--, edi += %d, esi += %d)\n",
3524 " if ((cond_z = (%sedi == %sesi)) %s 0)\n",
3525 buf1, buf1, (po->flags & OPF_REPZ) ? "==" : "!=");
3528 snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
3529 (po->flags & OPF_REPZ) ? "e" : "ne");
3533 " cond_z = (%sedi = %sesi); edi += %d; esi += %d;",
3535 strcpy(g_comment, "cmps");
3537 pfomask &= ~(1 << PFO_Z);
3538 last_arith_dst = NULL;
3539 delayed_flag_op = NULL;
3542 // arithmetic w/flags
3547 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3550 assert_operand_cnt(2);
3551 fprintf(fout, " %s %s= %s;",
3552 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3554 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3555 output_std_flags(fout, po, &pfomask, buf1);
3556 last_arith_dst = &po->operand[0];
3557 delayed_flag_op = NULL;
3562 assert_operand_cnt(2);
3563 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3564 if (pfomask & (1 << PFO_C)) {
3565 if (po->operand[1].type == OPT_CONST) {
3566 l = lmod_bytes(po, po->operand[0].lmod) * 8;
3567 j = po->operand[1].val;
3570 if (po->op == OP_SHL)
3574 fprintf(fout, " cond_c = (%s & 0x%02x) ? 1 : 0;\n",
3578 ferr(po, "zero shift?\n");
3582 pfomask &= ~(1 << PFO_C);
3584 fprintf(fout, " %s %s= %s;", buf1, op_to_c(po),
3585 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3586 output_std_flags(fout, po, &pfomask, buf1);
3587 last_arith_dst = &po->operand[0];
3588 delayed_flag_op = NULL;
3592 assert_operand_cnt(2);
3593 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3594 fprintf(fout, " %s = %s%s >> %s;", buf1,
3595 lmod_cast_s(po, po->operand[0].lmod), buf1,
3596 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3597 output_std_flags(fout, po, &pfomask, buf1);
3598 last_arith_dst = &po->operand[0];
3599 delayed_flag_op = NULL;
3604 assert_operand_cnt(2);
3605 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3606 if (po->operand[1].type == OPT_CONST) {
3607 j = po->operand[1].val;
3608 j %= lmod_bytes(po, po->operand[0].lmod) * 8;
3609 fprintf(fout, po->op == OP_ROL ?
3610 " %s = (%s << %d) | (%s >> %d);" :
3611 " %s = (%s >> %d) | (%s << %d);",
3612 buf1, buf1, j, buf1,
3613 lmod_bytes(po, po->operand[0].lmod) * 8 - j);
3617 output_std_flags(fout, po, &pfomask, buf1);
3618 last_arith_dst = &po->operand[0];
3619 delayed_flag_op = NULL;
3623 assert_operand_cnt(2);
3624 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3625 if (IS(opr_name(po, 0), opr_name(po, 1))) {
3626 // special case for XOR
3627 fprintf(fout, " %s = 0;",
3628 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
3629 last_arith_dst = &po->operand[0];
3630 delayed_flag_op = NULL;
3637 assert_operand_cnt(2);
3638 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3639 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3640 if (po->op == OP_SBB
3641 && IS(po->operand[0].name, po->operand[1].name))
3643 // avoid use of unitialized var
3644 fprintf(fout, " %s = -cond_c;", buf1);
3645 // carry remains what it was
3646 pfomask &= ~(1 << PFO_C);
3649 fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
3650 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3652 output_std_flags(fout, po, &pfomask, buf1);
3653 last_arith_dst = &po->operand[0];
3654 delayed_flag_op = NULL;
3659 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3660 if (po->operand[0].type == OPT_REG) {
3661 strcpy(buf2, po->op == OP_INC ? "++" : "--");
3662 fprintf(fout, " %s%s;", buf1, buf2);
3665 strcpy(buf2, po->op == OP_INC ? "+" : "-");
3666 fprintf(fout, " %s %s= 1;", buf1, buf2);
3668 output_std_flags(fout, po, &pfomask, buf1);
3669 last_arith_dst = &po->operand[0];
3670 delayed_flag_op = NULL;
3674 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3675 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
3676 fprintf(fout, " %s = -%s%s;", buf1,
3677 lmod_cast_s(po, po->operand[0].lmod), buf2);
3678 last_arith_dst = &po->operand[0];
3679 delayed_flag_op = NULL;
3680 if (pfomask & (1 << PFO_C)) {
3681 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
3682 pfomask &= ~(1 << PFO_C);
3687 if (po->operand_cnt == 2) {
3688 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3691 if (po->operand_cnt == 3)
3692 ferr(po, "TODO imul3\n");
3695 assert_operand_cnt(1);
3696 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
3697 fprintf(fout, " mul_tmp = %seax * %s%s;\n", buf1, buf1,
3698 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
3699 fprintf(fout, " edx = mul_tmp >> 32;\n");
3700 fprintf(fout, " eax = mul_tmp;");
3701 last_arith_dst = NULL;
3702 delayed_flag_op = NULL;
3707 assert_operand_cnt(1);
3708 if (po->operand[0].lmod != OPLM_DWORD)
3709 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
3711 // 32bit division is common, look for it
3712 if (po->op == OP_DIV)
3713 ret = scan_for_reg_clear(i, xDX);
3715 ret = scan_for_cdq_edx(i);
3717 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
3718 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
3719 po->op == OP_IDIV));
3720 fprintf(fout, " edx = %seax %% %s%s;\n", buf2, buf2, buf1);
3721 fprintf(fout, " eax = %seax / %s%s;", buf2, buf2, buf1);
3724 ferr(po, "TODO 64bit divident\n");
3725 last_arith_dst = NULL;
3726 delayed_flag_op = NULL;
3731 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3733 for (j = 0; j < 8; j++) {
3734 if (pfomask & (1 << j)) {
3735 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
3736 fprintf(fout, " cond_%s = %s;",
3737 parsed_flag_op_names[j], buf1);
3744 last_arith_dst = NULL;
3745 delayed_flag_op = po;
3748 // note: we reuse OP_Jcc for SETcc, only flags differ
3749 case OP_JO ... OP_JG:
3750 if (po->flags & OPF_JMP)
3751 fprintf(fout, " goto %s;", po->operand[0].name);
3752 // else SETcc - should already be handled
3756 assert_operand_cnt(1);
3757 last_arith_dst = NULL;
3758 delayed_flag_op = NULL;
3760 if (po->operand[0].type == OPT_REGMEM) {
3761 ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
3764 ferr(po, "parse failure for jmp '%s'\n",
3765 po->operand[0].name);
3766 fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
3769 else if (po->operand[0].type != OPT_LABEL)
3770 ferr(po, "unhandled jmp type\n");
3772 fprintf(fout, " goto %s;", po->operand[0].name);
3776 assert_operand_cnt(1);
3778 my_assert_not(pp, NULL);
3781 fprintf(fout, " icall%d = %s;\n", i,
3782 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
3786 if (strstr(pp->ret_type.name, "int64")) {
3787 if (po->flags & OPF_TAIL)
3788 ferr(po, "int64 and tail?\n");
3789 fprintf(fout, "mul_tmp = ");
3791 else if (!IS(pp->ret_type.name, "void")) {
3792 if (po->flags & OPF_TAIL) {
3793 if (!IS(g_func_pp->ret_type.name, "void")) {
3794 fprintf(fout, "return ");
3795 if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
3796 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
3799 else if (regmask & (1 << xAX)) {
3800 fprintf(fout, "eax = ");
3801 if (pp->ret_type.is_ptr)
3802 fprintf(fout, "(u32)");
3807 fprintf(fout, "icall%d(", i);
3810 if (pp->name[0] == 0)
3811 ferr(po, "missing pp->name\n");
3812 fprintf(fout, "%s%s(", pp->name,
3813 pp->has_structarg ? "_sa" : "");
3816 for (arg = 0; arg < pp->argc; arg++) {
3818 fprintf(fout, ", ");
3821 if (pp->arg[arg].type.is_ptr)
3822 snprintf(cast, sizeof(cast), "(%s)", pp->arg[arg].type.name);
3824 if (pp->arg[arg].reg != NULL) {
3825 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
3830 tmp_op = pp->arg[arg].datap;
3832 ferr(po, "parsed_op missing for arg%d\n", arg);
3833 if (tmp_op->argnum != 0) {
3834 fprintf(fout, "%ss_a%d", cast, tmp_op->argnum);
3838 out_src_opr(buf1, sizeof(buf1),
3839 tmp_op, &tmp_op->operand[0], cast, 0));
3842 fprintf(fout, ");");
3844 if (strstr(pp->ret_type.name, "int64")) {
3845 fprintf(fout, "\n");
3846 fprintf(fout, " edx = mul_tmp >> 32;\n");
3847 fprintf(fout, " eax = mul_tmp;");
3850 if (pp->is_unresolved) {
3851 snprintf(buf3, sizeof(buf3), "unresoved %dreg ",
3853 strcat(g_comment, buf3);
3856 if (po->flags & OPF_TAIL) {
3860 else if (IS(pp->ret_type.name, "void"))
3862 else if (IS(g_func_pp->ret_type.name, "void"))
3864 // else already handled as 'return f()'
3867 if (!IS(g_func_pp->ret_type.name, "void"))
3868 ferr(po, "int func -> void func tailcall?\n");
3869 fprintf(fout, "\n return;");
3870 strcat(g_comment, "^ tailcall");
3873 strcat(g_comment, "tailcall");
3875 delayed_flag_op = NULL;
3876 last_arith_dst = NULL;
3880 if (g_func_pp->is_vararg)
3881 fprintf(fout, " va_end(ap);\n");
3883 if (IS(g_func_pp->ret_type.name, "void")) {
3884 if (i != opcnt - 1 || label_pending)
3885 fprintf(fout, " return;");
3887 else if (g_func_pp->ret_type.is_ptr) {
3888 fprintf(fout, " return (%s)eax;",
3889 g_func_pp->ret_type.name);
3892 fprintf(fout, " return eax;");
3894 last_arith_dst = NULL;
3895 delayed_flag_op = NULL;
3899 if (po->argnum != 0) {
3900 // special case - saved func arg
3901 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
3902 fprintf(fout, " s_a%d = %s;", po->argnum, buf1);
3905 else if (po->flags & OPF_RSAVE) {
3906 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
3907 fprintf(fout, " s_%s = %s;", buf1, buf1);
3910 if (!(g_ida_func_attr & IDAFA_NORETURN))
3911 ferr(po, "stray push encountered\n");
3916 if (po->flags & OPF_RSAVE) {
3917 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3918 fprintf(fout, " %s = s_%s;", buf1, buf1);
3921 ferr(po, "stray pop encountered\n");
3930 ferr(po, "unhandled op type %d, flags %x\n",
3935 if (g_comment[0] != 0) {
3936 fprintf(fout, " // %s", g_comment);
3941 fprintf(fout, "\n");
3943 // some sanity checking
3944 if ((po->flags & OPF_REP) && po->op != OP_STOS
3945 && po->op != OP_MOVS && po->op != OP_CMPS)
3946 ferr(po, "unexpected rep\n");
3947 if ((po->flags & (OPF_REPZ|OPF_REPNZ)) && po->op != OP_CMPS)
3948 ferr(po, "unexpected repz/repnz\n");
3951 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
3953 // see is delayed flag stuff is still valid
3954 if (delayed_flag_op != NULL && delayed_flag_op != po) {
3955 if (is_any_opr_modified(delayed_flag_op, po, 0))
3956 delayed_flag_op = NULL;
3959 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
3960 if (is_opr_modified(last_arith_dst, po))
3961 last_arith_dst = NULL;
3967 if (g_stack_fsz && !g_stack_frame_used)
3968 fprintf(fout, " (void)sf;\n");
3970 fprintf(fout, "}\n\n");
3973 for (i = 0; i < opcnt; i++) {
3974 struct label_ref *lr, *lr_del;
3976 lr = g_label_refs[i].next;
3977 while (lr != NULL) {
3982 g_label_refs[i].i = -1;
3983 g_label_refs[i].next = NULL;
3985 if (ops[i].op == OP_CALL) {
3994 static void set_label(int i, const char *name)
4000 p = strchr(name, ':');
4004 if (len > sizeof(g_labels[0]) - 1)
4005 aerr("label '%s' too long: %d\n", name, len);
4006 if (g_labels[i][0] != 0 && !IS_START(g_labels[i], "algn_"))
4007 aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
4008 memcpy(g_labels[i], name, len);
4009 g_labels[i][len] = 0;
4012 // '=' needs special treatment..
4013 static char *next_word_s(char *w, size_t wsize, char *s)
4019 for (i = 0; i < wsize - 1; i++) {
4020 if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
4026 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
4027 printf("warning: '%s' truncated\n", w);
4038 static struct chunk_item *func_chunks;
4039 static int func_chunk_cnt;
4040 static int func_chunk_alloc;
4042 static void add_func_chunk(FILE *fasm, const char *name, int line)
4044 if (func_chunk_cnt >= func_chunk_alloc) {
4045 func_chunk_alloc *= 2;
4046 func_chunks = realloc(func_chunks,
4047 func_chunk_alloc * sizeof(func_chunks[0]));
4048 my_assert_not(func_chunks, NULL);
4050 func_chunks[func_chunk_cnt].fptr = ftell(fasm);
4051 func_chunks[func_chunk_cnt].name = strdup(name);
4052 func_chunks[func_chunk_cnt].asmln = line;
4056 static int cmp_chunks(const void *p1, const void *p2)
4058 const struct chunk_item *c1 = p1, *c2 = p2;
4059 return strcmp(c1->name, c2->name);
4062 static int cmpstringp(const void *p1, const void *p2)
4064 return strcmp(*(char * const *)p1, *(char * const *)p2);
4067 static void scan_ahead(FILE *fasm)
4077 oldpos = ftell(fasm);
4080 while (fgets(line, sizeof(line), fasm))
4091 // get rid of random tabs
4092 for (i = 0; line[i] != 0; i++)
4093 if (line[i] == '\t')
4096 if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
4099 next_word(words[0], sizeof(words[0]), p);
4100 if (words[0][0] == 0)
4101 aerr("missing name for func chunk?\n");
4103 add_func_chunk(fasm, words[0], asmln);
4108 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
4109 words[wordc][0] = 0;
4110 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
4111 if (*p == 0 || *p == ';') {
4117 if (wordc == 2 && IS(words[1], "ends"))
4121 fseek(fasm, oldpos, SEEK_SET);
4125 int main(int argc, char *argv[])
4127 FILE *fout, *fasm, *frlist;
4128 struct parsed_data *pd = NULL;
4130 char **rlist = NULL;
4132 int rlist_alloc = 0;
4133 int func_chunks_used = 0;
4134 int func_chunks_sorted = 0;
4135 int func_chunk_i = -1;
4136 long func_chunk_ret = 0;
4137 int func_chunk_ret_ln = 0;
4138 int scanned_ahead = 0;
4140 char words[20][256];
4141 enum opr_lenmod lmod;
4143 int pending_endp = 0;
4145 int skip_warned = 0;
4156 for (arg = 1; arg < argc; arg++) {
4157 if (IS(argv[arg], "-v"))
4159 else if (IS(argv[arg], "-rf"))
4160 g_allow_regfunc = 1;
4165 if (argc < arg + 3) {
4166 printf("usage:\n%s [-v] [-rf] <.c> <.asm> <hdrf> [rlist]*\n",
4173 asmfn = argv[arg++];
4174 fasm = fopen(asmfn, "r");
4175 my_assert_not(fasm, NULL);
4177 hdrfn = argv[arg++];
4178 g_fhdr = fopen(hdrfn, "r");
4179 my_assert_not(g_fhdr, NULL);
4182 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
4183 my_assert_not(rlist, NULL);
4184 // needs special handling..
4185 rlist[rlist_len++] = "__alloca_probe";
4187 func_chunk_alloc = 32;
4188 func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
4189 my_assert_not(func_chunks, NULL);
4191 memset(words, 0, sizeof(words));
4193 for (; arg < argc; arg++) {
4194 frlist = fopen(argv[arg], "r");
4195 my_assert_not(frlist, NULL);
4197 while (fgets(line, sizeof(line), frlist)) {
4199 if (*p == 0 || *p == ';')
4202 if (IS_START(p, "#if 0")
4203 || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
4207 else if (IS_START(p, "#endif"))
4214 p = next_word(words[0], sizeof(words[0]), p);
4215 if (words[0][0] == 0)
4218 if (rlist_len >= rlist_alloc) {
4219 rlist_alloc = rlist_alloc * 2 + 64;
4220 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
4221 my_assert_not(rlist, NULL);
4223 rlist[rlist_len++] = strdup(words[0]);
4232 qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
4234 fout = fopen(argv[arg_out], "w");
4235 my_assert_not(fout, NULL);
4238 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
4239 my_assert_not(g_eqs, NULL);
4241 for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
4242 g_label_refs[i].i = -1;
4243 g_label_refs[i].next = NULL;
4246 while (fgets(line, sizeof(line), fasm))
4255 // get rid of random tabs
4256 for (i = 0; line[i] != 0; i++)
4257 if (line[i] == '\t')
4262 if (p[2] == '=' && IS_START(p, "; =============== S U B"))
4263 goto do_pending_endp; // eww..
4265 if (p[2] == 'A' && IS_START(p, "; Attributes:"))
4267 static const char *attrs[] = {
4276 // parse IDA's attribute-list comment
4277 g_ida_func_attr = 0;
4280 for (; *p != 0; p = sskip(p)) {
4281 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
4282 if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
4283 g_ida_func_attr |= 1 << i;
4284 p += strlen(attrs[i]);
4288 if (i == ARRAY_SIZE(attrs)) {
4289 anote("unparsed IDA attr: %s\n", p);
4292 if (IS(attrs[i], "fpd=")) {
4293 p = next_word(words[0], sizeof(words[0]), p);
4298 else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
4301 next_word(words[0], sizeof(words[0]), p);
4302 if (words[0][0] == 0)
4303 aerr("missing name for func chunk?\n");
4305 if (!scanned_ahead) {
4306 add_func_chunk(fasm, words[0], asmln);
4307 func_chunks_sorted = 0;
4310 else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
4312 if (func_chunk_i >= 0) {
4313 if (func_chunk_i < func_chunk_cnt
4314 && IS(func_chunks[func_chunk_i].name, g_func))
4316 // move on to next chunk
4317 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
4319 aerr("seek failed for '%s' chunk #%d\n",
4320 g_func, func_chunk_i);
4321 asmln = func_chunks[func_chunk_i].asmln;
4325 if (func_chunk_ret == 0)
4326 aerr("no return from chunk?\n");
4327 fseek(fasm, func_chunk_ret, SEEK_SET);
4328 asmln = func_chunk_ret_ln;
4334 else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
4335 func_chunks_used = 1;
4337 if (IS_START(g_func, "sub_")) {
4338 unsigned long addr = strtoul(p, NULL, 16);
4339 unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
4340 if (addr > f_addr && !scanned_ahead) {
4341 anote("scan_ahead caused by '%s', addr %lx\n",
4345 func_chunks_sorted = 0;
4353 for (i = wordc; i < ARRAY_SIZE(words); i++)
4355 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
4356 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
4357 if (*p == 0 || *p == ';') {
4362 if (*p != 0 && *p != ';')
4363 aerr("too many words\n");
4365 // alow asm patches in comments
4366 if (*p == ';' && IS_START(p, "; sctpatch:")) {
4368 if (*p == 0 || *p == ';')
4370 goto parse_words; // lame
4375 awarn("wordc == 0?\n");
4379 // don't care about this:
4380 if (words[0][0] == '.'
4381 || IS(words[0], "include")
4382 || IS(words[0], "assume") || IS(words[1], "segment")
4383 || IS(words[0], "align"))
4389 // do delayed endp processing to collect switch jumptables
4391 if (in_func && !skip_func && wordc >= 2
4392 && ((words[0][0] == 'd' && words[0][2] == 0)
4393 || (words[1][0] == 'd' && words[1][2] == 0)))
4396 if (words[1][0] == 'd' && words[1][2] == 0) {
4398 if (g_func_pd_cnt >= pd_alloc) {
4399 pd_alloc = pd_alloc * 2 + 16;
4400 g_func_pd = realloc(g_func_pd,
4401 sizeof(g_func_pd[0]) * pd_alloc);
4402 my_assert_not(g_func_pd, NULL);
4404 pd = &g_func_pd[g_func_pd_cnt];
4406 memset(pd, 0, sizeof(*pd));
4407 strcpy(pd->label, words[0]);
4408 pd->type = OPT_CONST;
4409 pd->lmod = lmod_from_directive(words[1]);
4413 lmod = lmod_from_directive(words[0]);
4414 if (lmod != pd->lmod)
4415 aerr("lmod change? %d->%d\n", pd->lmod, lmod);
4418 if (pd->count_alloc < pd->count + wordc) {
4419 pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
4420 pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
4421 my_assert_not(pd->d, NULL);
4423 for (; i < wordc; i++) {
4424 if (IS(words[i], "offset")) {
4425 pd->type = OPT_OFFSET;
4428 p = strchr(words[i], ',');
4431 if (pd->type == OPT_OFFSET)
4432 pd->d[pd->count].u.label = strdup(words[i]);
4434 pd->d[pd->count].u.val = parse_number(words[i]);
4435 pd->d[pd->count].bt_i = -1;
4441 if (in_func && !skip_func)
4442 gen_func(fout, g_fhdr, g_func, pi);
4446 g_ida_func_attr = 0;
4450 func_chunks_used = 0;
4453 memset(&ops, 0, pi * sizeof(ops[0]));
4454 memset(g_labels, 0, pi * sizeof(g_labels[0]));
4458 for (i = 0; i < g_func_pd_cnt; i++) {
4460 if (pd->type == OPT_OFFSET) {
4461 for (j = 0; j < pd->count; j++)
4462 free(pd->d[j].u.label);
4473 if (IS(words[1], "proc")) {
4475 aerr("proc '%s' while in_func '%s'?\n",
4478 if ((g_ida_func_attr & IDAFA_THUNK)
4479 || bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
4481 strcpy(g_func, words[0]);
4482 set_label(0, words[0]);
4487 if (IS(words[1], "endp"))
4490 aerr("endp '%s' while not in_func?\n", words[0]);
4491 if (!IS(g_func, words[0]))
4492 aerr("endp '%s' while in_func '%s'?\n",
4495 if (!skip_func && func_chunks_used) {
4496 // start processing chunks
4497 struct chunk_item *ci, key = { g_func, 0 };
4499 func_chunk_ret = ftell(fasm);
4500 func_chunk_ret_ln = asmln;
4501 if (!func_chunks_sorted) {
4502 qsort(func_chunks, func_chunk_cnt,
4503 sizeof(func_chunks[0]), cmp_chunks);
4504 func_chunks_sorted = 1;
4506 ci = bsearch(&key, func_chunks, func_chunk_cnt,
4507 sizeof(func_chunks[0]), cmp_chunks);
4509 aerr("'%s' needs chunks, but none found\n", g_func);
4510 func_chunk_i = ci - func_chunks;
4511 for (; func_chunk_i > 0; func_chunk_i--)
4512 if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
4515 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
4517 aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
4518 asmln = func_chunks[func_chunk_i].asmln;
4526 if (wordc == 2 && IS(words[1], "ends"))
4529 p = strchr(words[0], ':');
4531 set_label(pi, words[0]);
4535 if (!in_func || skip_func) {
4536 if (!skip_warned && !skip_func && g_labels[pi][0] != 0) {
4538 anote("skipping from '%s'\n", g_labels[pi]);
4541 g_labels[pi][0] = 0;
4545 if (wordc > 1 && IS(words[1], "=")) {
4547 aerr("unhandled equ, wc=%d\n", wordc);
4548 if (g_eqcnt >= eq_alloc) {
4550 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
4551 my_assert_not(g_eqs, NULL);
4554 len = strlen(words[0]);
4555 if (len > sizeof(g_eqs[0].name) - 1)
4556 aerr("equ name too long: %d\n", len);
4557 strcpy(g_eqs[g_eqcnt].name, words[0]);
4559 if (!IS(words[3], "ptr"))
4560 aerr("unhandled equ\n");
4561 if (IS(words[2], "dword"))
4562 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
4563 else if (IS(words[2], "word"))
4564 g_eqs[g_eqcnt].lmod = OPLM_WORD;
4565 else if (IS(words[2], "byte"))
4566 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
4568 aerr("bad lmod: '%s'\n", words[2]);
4570 g_eqs[g_eqcnt].offset = parse_number(words[4]);
4575 if (pi >= ARRAY_SIZE(ops))
4576 aerr("too many ops\n");
4578 parse_op(&ops[pi], words, wordc);
4589 // vim:ts=2:shiftwidth=2:expandtab