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), /* branch, call */
36 OPF_CJMP = (1 << 4), /* cond. branch (cc or jecxz) */
37 OPF_CC = (1 << 5), /* uses flags */
38 OPF_TAIL = (1 << 6), /* ret or tail call */
39 OPF_RSAVE = (1 << 7), /* push/pop is local reg save/load */
40 OPF_REP = (1 << 8), /* prefixed by rep */
41 OPF_REPZ = (1 << 9), /* rep is repe/repz */
42 OPF_REPNZ = (1 << 10), /* rep is repne/repnz */
43 OPF_FARG = (1 << 11), /* push collected as func arg (no reuse) */
44 OPF_EBP_S = (1 << 12), /* ebp used as scratch here, not BP */
45 OPF_DF = (1 << 13), /* DF flag set */
46 OPF_ATAIL = (1 << 14), /* tail call with reused arg frame */
47 OPF_32BIT = (1 << 15), /* 32bit division */
48 OPF_LOCK = (1 << 16), /* op has lock prefix */
112 // must be sorted (larger len must be further in enum)
120 #define MAX_OPERANDS 3
124 enum opr_lenmod lmod;
125 unsigned int is_ptr:1; // pointer in C
126 unsigned int is_array:1; // array in C
127 unsigned int type_from_var:1; // .. in header, sometimes wrong
128 unsigned int size_mismatch:1; // type override differs from C
129 unsigned int size_lt:1; // type override is larger than C
130 unsigned int had_ds:1; // had ds: prefix
131 const struct parsed_proto *pp; // for OPT_LABEL
139 struct parsed_opr operand[MAX_OPERANDS];
142 unsigned char pfo_inv;
143 unsigned char operand_cnt;
145 int regmask_src; // all referensed regs
147 int pfomask; // flagop: parsed_flag_op that can't be delayed
148 int argnum; // push: altered before call arg #
149 int cc_scratch; // scratch storage during analysis
150 int bt_i; // branch target for branches
151 struct parsed_data *btj;// branch targets for jumptables
152 struct parsed_proto *pp;// parsed_proto for OP_CALL
158 // OP_CALL - parser proto hint (str)
159 // (OPF_CC) - point to one of (OPF_FLAGS) that affects cc op
160 // OP_POP - point to OP_PUSH in push/pop pair
164 enum opr_lenmod lmod;
171 enum opr_lenmod lmod;
185 struct label_ref *next;
189 IDAFA_BP_FRAME = (1 << 0),
190 IDAFA_LIB_FUNC = (1 << 1),
191 IDAFA_STATIC = (1 << 2),
192 IDAFA_NORETURN = (1 << 3),
193 IDAFA_THUNK = (1 << 4),
194 IDAFA_FPD = (1 << 5),
199 static struct parsed_op ops[MAX_OPS];
200 static struct parsed_equ *g_eqs;
202 static char g_labels[MAX_OPS][48];
203 static struct label_ref g_label_refs[MAX_OPS];
204 static const struct parsed_proto *g_func_pp;
205 static struct parsed_data *g_func_pd;
206 static int g_func_pd_cnt;
207 static char g_func[256];
208 static char g_comment[256];
209 static int g_bp_frame;
210 static int g_sp_frame;
211 static int g_stack_frame_used;
212 static int g_stack_fsz;
213 static int g_ida_func_attr;
214 static int g_allow_regfunc;
215 #define ferr(op_, fmt, ...) do { \
216 printf("%s:%d: error: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
217 dump_op(op_), ##__VA_ARGS__); \
221 #define fnote(op_, fmt, ...) \
222 printf("%s:%d: note: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
223 dump_op(op_), ##__VA_ARGS__)
227 const char *regs_r32[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp" };
228 const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
229 const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
230 const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
232 enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
234 // possible basic comparison types (without inversion)
235 enum parsed_flag_op {
239 PFO_BE, // 6 CF=1||ZF=1
243 PFO_LE, // e ZF=1||SF!=OF
246 static const char *parsed_flag_op_names[] = {
247 "o", "c", "z", "be", "s", "p", "l", "le"
250 static int char_array_i(const char *array[], size_t len, const char *s)
254 for (i = 0; i < len; i++)
261 static void printf_number(char *buf, size_t buf_size,
262 unsigned long number)
264 // output in C-friendly form
265 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
268 static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
272 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
274 *reg_lmod = OPLM_DWORD;
277 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
279 *reg_lmod = OPLM_WORD;
282 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
284 *reg_lmod = OPLM_BYTE;
287 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
289 *reg_lmod = OPLM_BYTE;
296 static int parse_indmode(char *name, int *regmask, int need_c_cvt)
298 enum opr_lenmod lmod;
311 while (my_isblank(*s))
313 for (; my_issep(*s); d++, s++)
315 while (my_isblank(*s))
320 if (IS_START(s, "ds:"))
323 s = next_idt(w, sizeof(w), s);
328 reg = parse_reg(&lmod, w);
330 *regmask |= 1 << reg;
334 if ('0' <= w[0] && w[0] <= '9') {
335 number = parse_number(w);
336 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
340 // probably some label/identifier - pass
343 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
347 strcpy(name, cvtbuf);
352 static int is_reg_in_str(const char *s)
356 if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3])))
359 for (i = 0; i < ARRAY_SIZE(regs_r32); i++)
360 if (!strncmp(s, regs_r32[i], 3))
366 static const char *parse_stack_el(const char *name, char *extra_reg)
368 const char *p, *p2, *s;
375 if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) {
377 if (extra_reg != NULL) {
378 strncpy(extra_reg, name, 3);
383 if (IS_START(p, "ebp+")) {
387 if (p2 != NULL && is_reg_in_str(p)) {
388 if (extra_reg != NULL) {
389 strncpy(extra_reg, p, p2 - p);
390 extra_reg[p2 - p] = 0;
395 if (!('0' <= *p && *p <= '9'))
401 if (!IS_START(name, "esp+"))
407 if (is_reg_in_str(s)) {
408 if (extra_reg != NULL) {
409 strncpy(extra_reg, s, p - s);
410 extra_reg[p - s] = 0;
415 aerr("%s IDA stackvar not set?\n", __func__);
417 if (!('0' <= *s && *s <= '9')) {
418 aerr("%s IDA stackvar offset not set?\n", __func__);
421 if (s[0] == '0' && s[1] == 'x')
424 if (len < sizeof(buf) - 1) {
425 strncpy(buf, s, len);
427 val = strtol(buf, &endp, 16);
428 if (val == 0 || *endp != 0) {
429 aerr("%s num parse fail for '%s'\n", __func__, buf);
438 if ('0' <= *p && *p <= '9')
444 static int guess_lmod_from_name(struct parsed_opr *opr)
446 if (!strncmp(opr->name, "dword_", 6)) {
447 opr->lmod = OPLM_DWORD;
450 if (!strncmp(opr->name, "word_", 5)) {
451 opr->lmod = OPLM_WORD;
454 if (!strncmp(opr->name, "byte_", 5)) {
455 opr->lmod = OPLM_BYTE;
461 static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
462 const struct parsed_type *c_type)
464 static const char *dword_types[] = {
465 "int", "_DWORD", "UINT_PTR", "DWORD",
466 "WPARAM", "LPARAM", "UINT", "__int32",
469 static const char *word_types[] = {
470 "uint16_t", "int16_t", "_WORD",
471 "unsigned __int16", "__int16",
473 static const char *byte_types[] = {
474 "uint8_t", "int8_t", "char",
475 "unsigned __int8", "__int8", "BYTE", "_BYTE",
477 // structures.. deal the same as with _UNKNOWN for now
483 if (c_type->is_ptr) {
488 n = skip_type_mod(c_type->name);
490 for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
491 if (IS(n, dword_types[i])) {
497 for (i = 0; i < ARRAY_SIZE(word_types); i++) {
498 if (IS(n, word_types[i])) {
504 for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
505 if (IS(n, byte_types[i])) {
514 static char *default_cast_to(char *buf, size_t buf_size,
515 struct parsed_opr *opr)
521 if (opr->pp == NULL || opr->pp->type.name == NULL
524 snprintf(buf, buf_size, "%s", "(void *)");
528 snprintf(buf, buf_size, "(%s)", opr->pp->type.name);
532 static enum opr_type lmod_from_directive(const char *d)
536 else if (IS(d, "dw"))
538 else if (IS(d, "db"))
541 aerr("unhandled directive: '%s'\n", d);
545 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
551 *regmask |= 1 << reg;
554 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
557 static int parse_operand(struct parsed_opr *opr,
558 int *regmask, int *regmask_indirect,
559 char words[16][256], int wordc, int w, unsigned int op_flags)
561 const struct parsed_proto *pp;
562 enum opr_lenmod tmplmod;
563 unsigned long number;
571 aerr("parse_operand w %d, wordc %d\n", w, wordc);
575 for (i = w; i < wordc; i++) {
576 len = strlen(words[i]);
577 if (words[i][len - 1] == ',') {
578 words[i][len - 1] = 0;
584 wordc_in = wordc - w;
586 if ((op_flags & OPF_JMP) && wordc_in > 0
587 && !('0' <= words[w][0] && words[w][0] <= '9'))
589 const char *label = NULL;
591 if (wordc_in == 3 && !strncmp(words[w], "near", 4)
592 && IS(words[w + 1], "ptr"))
593 label = words[w + 2];
594 else if (wordc_in == 2 && IS(words[w], "short"))
595 label = words[w + 1];
596 else if (wordc_in == 1
597 && strchr(words[w], '[') == NULL
598 && parse_reg(&tmplmod, words[w]) < 0)
602 opr->type = OPT_LABEL;
603 if (IS_START(label, "ds:")) {
607 strcpy(opr->name, label);
613 if (IS(words[w + 1], "ptr")) {
614 if (IS(words[w], "dword"))
615 opr->lmod = OPLM_DWORD;
616 else if (IS(words[w], "word"))
617 opr->lmod = OPLM_WORD;
618 else if (IS(words[w], "byte"))
619 opr->lmod = OPLM_BYTE;
621 aerr("type parsing failed\n");
623 wordc_in = wordc - w;
628 if (IS(words[w], "offset")) {
629 opr->type = OPT_OFFSET;
630 opr->lmod = OPLM_DWORD;
631 strcpy(opr->name, words[w + 1]);
632 pp = proto_parse(g_fhdr, opr->name, 1);
635 if (IS(words[w], "(offset")) {
636 p = strchr(words[w + 1], ')');
638 aerr("parse of bracketed offset failed\n");
640 opr->type = OPT_OFFSET;
641 strcpy(opr->name, words[w + 1]);
647 aerr("parse_operand 1 word expected\n");
649 if (IS_START(words[w], "ds:")) {
651 memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
653 strcpy(opr->name, words[w]);
655 if (words[w][0] == '[') {
656 opr->type = OPT_REGMEM;
657 ret = sscanf(words[w], "[%[^]]]", opr->name);
659 aerr("[] parse failure\n");
661 parse_indmode(opr->name, regmask_indirect, 1);
662 if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL)) {
664 struct parsed_equ *eq =
665 equ_find(NULL, parse_stack_el(opr->name, NULL), &i);
667 opr->lmod = eq->lmod;
671 else if (strchr(words[w], '[')) {
673 p = strchr(words[w], '[');
674 opr->type = OPT_REGMEM;
675 parse_indmode(p, regmask_indirect, 0);
676 strncpy(buf, words[w], p - words[w]);
677 buf[p - words[w]] = 0;
678 pp = proto_parse(g_fhdr, buf, 1);
681 else if (('0' <= words[w][0] && words[w][0] <= '9')
682 || words[w][0] == '-')
684 number = parse_number(words[w]);
685 opr->type = OPT_CONST;
687 printf_number(opr->name, sizeof(opr->name), number);
691 ret = parse_reg(&tmplmod, opr->name);
693 setup_reg_opr(opr, ret, tmplmod, regmask);
697 // most likely var in data segment
698 opr->type = OPT_LABEL;
699 pp = proto_parse(g_fhdr, opr->name, 0);
703 if (pp->is_fptr || pp->is_func) {
704 opr->lmod = OPLM_DWORD;
708 tmplmod = OPLM_UNSPEC;
709 if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
710 anote("unhandled C type '%s' for '%s'\n",
711 pp->type.name, opr->name);
713 if (opr->lmod == OPLM_UNSPEC) {
715 opr->type_from_var = 1;
717 else if (opr->lmod != tmplmod) {
718 opr->size_mismatch = 1;
719 if (tmplmod < opr->lmod)
722 opr->is_ptr = pp->type.is_ptr;
724 opr->is_array = pp->type.is_array;
728 if (opr->lmod == OPLM_UNSPEC)
729 guess_lmod_from_name(opr);
733 static const struct {
738 { "repe", OPF_REP|OPF_REPZ },
739 { "repz", OPF_REP|OPF_REPZ },
740 { "repne", OPF_REP|OPF_REPNZ },
741 { "repnz", OPF_REP|OPF_REPNZ },
742 { "lock", OPF_LOCK }, // ignored for now..
745 #define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC)
747 static const struct {
750 unsigned short minopr;
751 unsigned short maxopr;
754 unsigned char pfo_inv;
756 { "nop", OP_NOP, 0, 0, 0 },
757 { "push", OP_PUSH, 1, 1, 0 },
758 { "pop", OP_POP, 1, 1, OPF_DATA },
759 { "leave",OP_LEAVE, 0, 0, OPF_DATA },
760 { "mov" , OP_MOV, 2, 2, OPF_DATA },
761 { "lea", OP_LEA, 2, 2, OPF_DATA },
762 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
763 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
764 { "xchg", OP_XCHG, 2, 2, OPF_DATA },
765 { "not", OP_NOT, 1, 1, OPF_DATA },
766 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
767 { "lodsb",OP_LODS, 0, 0, OPF_DATA },
768 { "lodsw",OP_LODS, 0, 0, OPF_DATA },
769 { "lodsd",OP_LODS, 0, 0, OPF_DATA },
770 { "stosb",OP_STOS, 0, 0, OPF_DATA },
771 { "stosw",OP_STOS, 0, 0, OPF_DATA },
772 { "stosd",OP_STOS, 0, 0, OPF_DATA },
773 { "movsb",OP_MOVS, 0, 0, OPF_DATA },
774 { "movsw",OP_MOVS, 0, 0, OPF_DATA },
775 { "movsd",OP_MOVS, 0, 0, OPF_DATA },
776 { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
777 { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
778 { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
779 { "scasb",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
780 { "scasw",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
781 { "scasd",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
782 { "std", OP_STD, 0, 0, OPF_DATA }, // special flag
783 { "cld", OP_CLD, 0, 0, OPF_DATA },
784 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
785 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
786 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
787 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
788 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
789 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
790 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
791 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
792 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
793 { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS },
794 { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS },
795 { "rcl", OP_RCL, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
796 { "rcr", OP_RCR, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
797 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
798 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
799 { "bsf", OP_BSF, 2, 2, OPF_DATA|OPF_FLAGS },
800 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
801 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
802 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
803 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
804 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
805 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
806 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
807 { "test", OP_TEST, 2, 2, OPF_FLAGS },
808 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
809 { "retn", OP_RET, 0, 1, OPF_TAIL },
810 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
811 { "jmp", OP_JMP, 1, 1, OPF_JMP },
812 { "jecxz",OP_JECXZ, 1, 1, OPF_JMP|OPF_CJMP },
813 { "jo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 0 }, // 70 OF=1
814 { "jno", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 1 }, // 71 OF=0
815 { "jc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 CF=1
816 { "jb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72
817 { "jnc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 CF=0
818 { "jnb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
819 { "jae", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
820 { "jz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74 ZF=1
821 { "je", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74
822 { "jnz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75 ZF=0
823 { "jne", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75
824 { "jbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 CF=1||ZF=1
825 { "jna", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76
826 { "ja", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 CF=0&&ZF=0
827 { "jnbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77
828 { "js", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 0 }, // 78 SF=1
829 { "jns", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 1 }, // 79 SF=0
830 { "jp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a PF=1
831 { "jpe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a
832 { "jnp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b PF=0
833 { "jpo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b
834 { "jl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c SF!=OF
835 { "jnge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c
836 { "jge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d SF=OF
837 { "jnl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d
838 { "jle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e ZF=1||SF!=OF
839 { "jng", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e
840 { "jg", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f ZF=0&&SF=OF
841 { "jnle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f
842 { "seto", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 0 },
843 { "setno", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 1 },
844 { "setc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
845 { "setb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
846 { "setnc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
847 { "setae", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
848 { "setnb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
849 { "setz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
850 { "sete", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
851 { "setnz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
852 { "setne", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
853 { "setbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
854 { "setna", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
855 { "seta", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
856 { "setnbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
857 { "sets", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 0 },
858 { "setns", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 1 },
859 { "setp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
860 { "setpe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
861 { "setnp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
862 { "setpo", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
863 { "setl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
864 { "setnge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
865 { "setge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
866 { "setnl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
867 { "setle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
868 { "setng", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
869 { "setg", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
870 { "setnle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
873 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
875 enum opr_lenmod lmod = OPLM_UNSPEC;
876 int prefix_flags = 0;
884 for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
885 if (IS(words[w], pref_table[i].name)) {
886 prefix_flags = pref_table[i].flags;
893 aerr("lone prefix: '%s'\n", words[0]);
898 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
899 if (IS(words[w], op_table[i].name))
903 if (i == ARRAY_SIZE(op_table))
904 aerr("unhandled op: '%s'\n", words[0]);
907 op->op = op_table[i].op;
908 op->flags = op_table[i].flags | prefix_flags;
909 op->pfo = op_table[i].pfo;
910 op->pfo_inv = op_table[i].pfo_inv;
911 op->regmask_src = op->regmask_dst = 0;
914 for (opr = 0; opr < op_table[i].minopr; opr++) {
915 regmask = regmask_ind = 0;
916 w = parse_operand(&op->operand[opr], ®mask, ®mask_ind,
917 words, wordc, w, op->flags);
919 if (opr == 0 && (op->flags & OPF_DATA))
920 op->regmask_dst = regmask;
921 // for now, mark dst as src too
922 op->regmask_src |= regmask | regmask_ind;
925 for (; w < wordc && opr < op_table[i].maxopr; opr++) {
926 w = parse_operand(&op->operand[opr],
927 &op->regmask_src, &op->regmask_src,
928 words, wordc, w, op->flags);
932 aerr("parse_op %s incomplete: %d/%d\n",
936 op->operand_cnt = opr;
937 if (!strncmp(op_table[i].name, "set", 3))
938 op->operand[0].lmod = OPLM_BYTE;
940 // ops with implicit argumets
944 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
945 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
951 if (op->operand_cnt != 0)
953 if (words[op_w][4] == 'b')
955 else if (words[op_w][4] == 'w')
957 else if (words[op_w][4] == 'd')
960 setup_reg_opr(&op->operand[0], op->op == OP_LODS ? xSI : xDI,
961 lmod, &op->regmask_src);
962 setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src);
963 op->regmask_dst = op->regmask_src;
964 setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD,
965 op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src);
970 if (op->operand_cnt != 0)
972 if (words[op_w][4] == 'b')
974 else if (words[op_w][4] == 'w')
976 else if (words[op_w][4] == 'd')
979 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
980 setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src);
981 setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src);
982 op->regmask_dst = op->regmask_src;
986 op->regmask_src |= op->regmask_dst;
987 op->regmask_dst |= op->regmask_src;
992 op->regmask_src = 1 << xCX;
993 op->operand[0].type = OPT_REG;
994 op->operand[0].reg = xCX;
995 op->operand[0].lmod = OPLM_DWORD;
999 if (op->operand_cnt != 1)
1004 op->regmask_dst = (1 << xDX) | (1 << xAX);
1005 op->regmask_src |= (1 << xAX);
1006 if (op->operand[0].lmod == OPLM_UNSPEC)
1007 op->operand[0].lmod = OPLM_DWORD;
1012 // we could set up operands for edx:eax, but there is no real need to
1013 // (see is_opr_modified())
1014 regmask = (1 << xDX) | (1 << xAX);
1015 op->regmask_dst = regmask;
1016 op->regmask_src |= regmask;
1017 if (op->operand[0].lmod == OPLM_UNSPEC)
1018 op->operand[0].lmod = OPLM_DWORD;
1026 if (op->operand[1].lmod == OPLM_UNSPEC)
1027 op->operand[1].lmod = OPLM_BYTE;
1031 if (op->operand[0].lmod == OPLM_UNSPEC
1032 && (op->operand[0].type == OPT_CONST
1033 || op->operand[0].type == OPT_OFFSET
1034 || op->operand[0].type == OPT_LABEL))
1035 op->operand[0].lmod = OPLM_DWORD;
1041 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1042 && op->operand[0].lmod == op->operand[1].lmod
1043 && op->operand[0].reg == op->operand[1].reg
1044 && IS(op->operand[0].name, op->operand[1].name)) // ah, al..
1046 op->flags |= OPF_RMD;
1047 op->regmask_src = op->regmask_dst = 0;
1052 if (op->operand[0].type == OPT_REG
1053 && op->operand[1].type == OPT_REGMEM)
1056 snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
1057 if (IS(buf, op->operand[1].name))
1058 op->flags |= OPF_RMD;
1063 // trashed regs must be explicitly detected later
1064 op->regmask_dst = 0;
1072 static const char *op_name(struct parsed_op *po)
1074 static char buf[16];
1078 if (po->op == OP_JCC || po->op == OP_SCC) {
1080 *p++ = (po->op == OP_JCC) ? 'j' : 's';
1083 strcpy(p, parsed_flag_op_names[po->pfo]);
1087 for (i = 0; i < ARRAY_SIZE(op_table); i++)
1088 if (op_table[i].op == po->op)
1089 return op_table[i].name;
1095 static const char *dump_op(struct parsed_op *po)
1097 static char out[128];
1104 snprintf(out, sizeof(out), "%s", op_name(po));
1105 for (i = 0; i < po->operand_cnt; i++) {
1109 snprintf(p, sizeof(out) - (p - out),
1110 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1111 po->operand[i].name);
1117 static const char *lmod_type_u(struct parsed_op *po,
1118 enum opr_lenmod lmod)
1128 ferr(po, "invalid lmod: %d\n", lmod);
1129 return "(_invalid_)";
1133 static const char *lmod_cast_u(struct parsed_op *po,
1134 enum opr_lenmod lmod)
1144 ferr(po, "invalid lmod: %d\n", lmod);
1145 return "(_invalid_)";
1149 static const char *lmod_cast_u_ptr(struct parsed_op *po,
1150 enum opr_lenmod lmod)
1160 ferr(po, "invalid lmod: %d\n", lmod);
1161 return "(_invalid_)";
1165 static const char *lmod_cast_s(struct parsed_op *po,
1166 enum opr_lenmod lmod)
1176 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1177 return "(_invalid_)";
1181 static const char *lmod_cast(struct parsed_op *po,
1182 enum opr_lenmod lmod, int is_signed)
1185 lmod_cast_s(po, lmod) :
1186 lmod_cast_u(po, lmod);
1189 static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1199 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1204 static const char *opr_name(struct parsed_op *po, int opr_num)
1206 if (opr_num >= po->operand_cnt)
1207 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1208 return po->operand[opr_num].name;
1211 static unsigned int opr_const(struct parsed_op *po, int opr_num)
1213 if (opr_num >= po->operand_cnt)
1214 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1215 if (po->operand[opr_num].type != OPT_CONST)
1216 ferr(po, "opr %d: const expected\n", opr_num);
1217 return po->operand[opr_num].val;
1220 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1222 if ((unsigned int)popr->reg >= MAX_REGS)
1223 ferr(po, "invalid reg: %d\n", popr->reg);
1224 return regs_r32[popr->reg];
1227 // cast1 is the "final" cast
1228 static const char *simplify_cast(const char *cast1, const char *cast2)
1230 static char buf[256];
1236 if (IS(cast1, cast2))
1238 if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
1240 if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
1242 if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
1244 if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
1246 if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1249 snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1253 static const char *simplify_cast_num(const char *cast, unsigned int val)
1255 if (IS(cast, "(u8)") && val < 0x100)
1257 if (IS(cast, "(s8)") && val < 0x80)
1259 if (IS(cast, "(u16)") && val < 0x10000)
1261 if (IS(cast, "(s16)") && val < 0x8000)
1263 if (IS(cast, "(s32)") && val < 0x80000000)
1269 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1278 namelen = strlen(name);
1280 p = strchr(name, '+');
1284 ferr(po, "equ parse failed for '%s'\n", name);
1286 if (IS_START(p, "0x"))
1288 *extra_offs = strtol(p, &endp, 16);
1290 ferr(po, "equ parse failed for '%s'\n", name);
1293 for (i = 0; i < g_eqcnt; i++)
1294 if (strncmp(g_eqs[i].name, name, namelen) == 0
1295 && g_eqs[i].name[namelen] == 0)
1299 ferr(po, "unresolved equ name: '%s'\n", name);
1306 static int is_stack_access(struct parsed_op *po,
1307 const struct parsed_opr *popr)
1309 return (parse_stack_el(popr->name, NULL)
1310 || (g_bp_frame && !(po->flags & OPF_EBP_S)
1311 && IS_START(popr->name, "ebp")));
1314 static void parse_stack_access(struct parsed_op *po,
1315 const char *name, char *ofs_reg, int *offset_out,
1316 int *stack_ra_out, const char **bp_arg_out, int is_lea)
1318 const char *bp_arg = "";
1319 const char *p = NULL;
1320 struct parsed_equ *eq;
1327 if (IS_START(name, "ebp-")
1328 || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1331 if (IS_START(p, "0x"))
1333 offset = strtoul(p, &endp, 16);
1337 ferr(po, "ebp- parse of '%s' failed\n", name);
1340 bp_arg = parse_stack_el(name, ofs_reg);
1341 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1342 eq = equ_find(po, bp_arg, &offset);
1344 ferr(po, "detected but missing eq\n");
1345 offset += eq->offset;
1348 if (!strncmp(name, "ebp", 3))
1351 // yes it sometimes LEAs ra for compares..
1352 if (!is_lea && ofs_reg[0] == 0
1353 && stack_ra <= offset && offset < stack_ra + 4)
1355 ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
1358 *offset_out = offset;
1359 *stack_ra_out = stack_ra;
1361 *bp_arg_out = bp_arg;
1364 static void stack_frame_access(struct parsed_op *po,
1365 struct parsed_opr *popr, char *buf, size_t buf_size,
1366 const char *name, const char *cast, int is_src, int is_lea)
1368 enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1369 const char *prefix = "";
1370 const char *bp_arg = NULL;
1371 char ofs_reg[16] = { 0, };
1372 int i, arg_i, arg_s;
1379 if (po->flags & OPF_EBP_S)
1380 ferr(po, "stack_frame_access while ebp is scratch\n");
1382 parse_stack_access(po, name, ofs_reg, &offset,
1383 &stack_ra, &bp_arg, is_lea);
1385 if (offset > stack_ra)
1387 arg_i = (offset - stack_ra - 4) / 4;
1388 if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
1390 if (g_func_pp->is_vararg
1391 && arg_i == g_func_pp->argc_stack && is_lea)
1393 // should be va_list
1396 snprintf(buf, buf_size, "%sap", cast);
1399 ferr(po, "offset %d (%s,%d) doesn't map to any arg\n",
1400 offset, bp_arg, arg_i);
1402 if (ofs_reg[0] != 0)
1403 ferr(po, "offset reg on arg access?\n");
1405 for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1406 if (g_func_pp->arg[i].reg != NULL)
1412 if (i == g_func_pp->argc)
1413 ferr(po, "arg %d not in prototype?\n", arg_i);
1415 popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
1421 ferr(po, "lea/byte to arg?\n");
1422 if (is_src && (offset & 3) == 0)
1423 snprintf(buf, buf_size, "%sa%d",
1424 simplify_cast(cast, "(u8)"), i + 1);
1426 snprintf(buf, buf_size, "%sBYTE%d(a%d)",
1427 cast, offset & 3, i + 1);
1432 ferr(po, "lea/word to arg?\n");
1437 ferr(po, "problematic arg store\n");
1438 snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
1439 simplify_cast(cast, "*(u16 *)"), i + 1);
1442 ferr(po, "unaligned arg word load\n");
1444 else if (is_src && (offset & 2) == 0)
1445 snprintf(buf, buf_size, "%sa%d",
1446 simplify_cast(cast, "(u16)"), i + 1);
1448 snprintf(buf, buf_size, "%s%sWORD(a%d)",
1449 cast, (offset & 2) ? "HI" : "LO", i + 1);
1461 snprintf(buf, buf_size, "(u32)&a%d + %d",
1464 ferr(po, "unaligned arg store\n");
1466 // mov edx, [ebp+arg_4+2]; movsx ecx, dx
1467 snprintf(buf, buf_size, "%s(a%d >> %d)",
1468 prefix, i + 1, (offset & 3) * 8);
1472 snprintf(buf, buf_size, "%s%sa%d",
1473 prefix, is_lea ? "&" : "", i + 1);
1478 ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
1482 snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
1485 guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
1486 if (tmp_lmod != OPLM_DWORD
1487 && (unaligned || (!is_src && tmp_lmod < popr->lmod)))
1489 ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
1490 i + 1, offset, g_func_pp->arg[i].type.name);
1492 // can't check this because msvc likes to reuse
1493 // arg space for scratch..
1494 //if (popr->is_ptr && popr->lmod != OPLM_DWORD)
1495 // ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
1499 if (g_stack_fsz == 0)
1500 ferr(po, "stack var access without stackframe\n");
1501 g_stack_frame_used = 1;
1503 sf_ofs = g_stack_fsz + offset;
1504 lim = (ofs_reg[0] != 0) ? -4 : 0;
1505 if (offset > 0 || sf_ofs < lim)
1506 ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
1516 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1517 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1521 if ((sf_ofs & 1) || ofs_reg[0] != 0) {
1522 // known unaligned or possibly unaligned
1523 strcat(g_comment, " unaligned");
1525 prefix = "*(u16 *)&";
1526 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1527 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1530 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
1534 if ((sf_ofs & 3) || ofs_reg[0] != 0) {
1535 // known unaligned or possibly unaligned
1536 strcat(g_comment, " unaligned");
1538 prefix = "*(u32 *)&";
1539 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1540 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1543 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
1547 ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
1552 static void check_func_pp(struct parsed_op *po,
1553 const struct parsed_proto *pp, const char *pfx)
1557 if (pp->argc_reg != 0) {
1558 if (/*!g_allow_regfunc &&*/ !pp->is_fastcall) {
1559 pp_print(buf, sizeof(buf), pp);
1560 ferr(po, "%s: unexpected reg arg in icall: %s\n", pfx, buf);
1562 if (pp->argc_stack > 0 && pp->argc_reg != 2)
1563 ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
1564 pfx, pp->argc_reg, pp->argc_stack);
1568 static const char *check_label_read_ref(struct parsed_op *po,
1571 const struct parsed_proto *pp;
1573 pp = proto_parse(g_fhdr, name, 0);
1575 ferr(po, "proto_parse failed for ref '%s'\n", name);
1578 check_func_pp(po, pp, "ref");
1583 static char *out_src_opr(char *buf, size_t buf_size,
1584 struct parsed_op *po, struct parsed_opr *popr, const char *cast,
1587 char tmp1[256], tmp2[256];
1596 switch (popr->type) {
1599 ferr(po, "lea from reg?\n");
1601 switch (popr->lmod) {
1603 snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
1606 snprintf(buf, buf_size, "%s%s",
1607 simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
1610 if (popr->name[1] == 'h') // XXX..
1611 snprintf(buf, buf_size, "%s(%s >> 8)",
1612 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1614 snprintf(buf, buf_size, "%s%s",
1615 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1618 ferr(po, "invalid src lmod: %d\n", popr->lmod);
1623 if (is_stack_access(po, popr)) {
1624 stack_frame_access(po, popr, buf, buf_size,
1625 popr->name, cast, 1, is_lea);
1629 strcpy(expr, popr->name);
1630 if (strchr(expr, '[')) {
1631 // special case: '[' can only be left for label[reg] form
1632 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
1634 ferr(po, "parse failure for '%s'\n", expr);
1635 if (tmp1[0] == '(') {
1636 // (off_4FFF50+3)[eax]
1637 p = strchr(tmp1 + 1, ')');
1638 if (p == NULL || p[1] != 0)
1639 ferr(po, "parse failure (2) for '%s'\n", expr);
1641 memmove(tmp1, tmp1 + 1, strlen(tmp1));
1643 snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
1646 // XXX: do we need more parsing?
1648 snprintf(buf, buf_size, "%s", expr);
1652 snprintf(buf, buf_size, "%s(%s)",
1653 simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
1657 name = check_label_read_ref(po, popr->name);
1658 if (cast[0] == 0 && popr->is_ptr)
1662 snprintf(buf, buf_size, "(u32)&%s", name);
1663 else if (popr->size_lt)
1664 snprintf(buf, buf_size, "%s%s%s%s", cast,
1665 lmod_cast_u_ptr(po, popr->lmod),
1666 popr->is_array ? "" : "&", name);
1668 snprintf(buf, buf_size, "%s%s%s", cast, name,
1669 popr->is_array ? "[0]" : "");
1673 name = check_label_read_ref(po, popr->name);
1677 ferr(po, "lea an offset?\n");
1678 snprintf(buf, buf_size, "%s&%s", cast, name);
1683 ferr(po, "lea from const?\n");
1685 printf_number(tmp1, sizeof(tmp1), popr->val);
1686 if (popr->val == 0 && strchr(cast, '*'))
1687 snprintf(buf, buf_size, "NULL");
1689 snprintf(buf, buf_size, "%s%s",
1690 simplify_cast_num(cast, popr->val), tmp1);
1694 ferr(po, "invalid src type: %d\n", popr->type);
1700 // note: may set is_ptr (we find that out late for ebp frame..)
1701 static char *out_dst_opr(char *buf, size_t buf_size,
1702 struct parsed_op *po, struct parsed_opr *popr)
1704 switch (popr->type) {
1706 switch (popr->lmod) {
1708 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
1712 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
1716 if (popr->name[1] == 'h') // XXX..
1717 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
1719 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
1722 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
1727 if (is_stack_access(po, popr)) {
1728 stack_frame_access(po, popr, buf, buf_size,
1729 popr->name, "", 0, 0);
1733 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1736 if (popr->size_mismatch)
1737 snprintf(buf, buf_size, "%s%s%s",
1738 lmod_cast_u_ptr(po, popr->lmod),
1739 popr->is_array ? "" : "&", popr->name);
1741 snprintf(buf, buf_size, "%s%s", popr->name,
1742 popr->is_array ? "[0]" : "");
1746 ferr(po, "invalid dst type: %d\n", popr->type);
1752 static char *out_src_opr_u32(char *buf, size_t buf_size,
1753 struct parsed_op *po, struct parsed_opr *popr)
1755 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1758 static void out_test_for_cc(char *buf, size_t buf_size,
1759 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1760 enum opr_lenmod lmod, const char *expr)
1762 const char *cast, *scast;
1764 cast = lmod_cast_u(po, lmod);
1765 scast = lmod_cast_s(po, lmod);
1769 case PFO_BE: // CF=1||ZF=1; CF=0
1770 snprintf(buf, buf_size, "(%s%s %s 0)",
1771 cast, expr, is_inv ? "!=" : "==");
1775 case PFO_L: // SF!=OF; OF=0
1776 snprintf(buf, buf_size, "(%s%s %s 0)",
1777 scast, expr, is_inv ? ">=" : "<");
1780 case PFO_LE: // ZF=1||SF!=OF; OF=0
1781 snprintf(buf, buf_size, "(%s%s %s 0)",
1782 scast, expr, is_inv ? ">" : "<=");
1786 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1790 static void out_cmp_for_cc(char *buf, size_t buf_size,
1791 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1793 const char *cast, *scast, *cast_use;
1794 char buf1[256], buf2[256];
1795 enum opr_lenmod lmod;
1797 if (po->operand[0].lmod != po->operand[1].lmod)
1798 ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
1799 po->operand[0].lmod, po->operand[1].lmod);
1800 lmod = po->operand[0].lmod;
1802 cast = lmod_cast_u(po, lmod);
1803 scast = lmod_cast_s(po, lmod);
1819 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1822 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
1823 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
1827 // note: must be unsigned compare
1828 snprintf(buf, buf_size, "(%s %s %s)",
1829 buf1, is_inv ? ">=" : "<", buf2);
1833 snprintf(buf, buf_size, "(%s %s %s)",
1834 buf1, is_inv ? "!=" : "==", buf2);
1838 // note: must be unsigned compare
1839 snprintf(buf, buf_size, "(%s %s %s)",
1840 buf1, is_inv ? ">" : "<=", buf2);
1843 if (is_inv && lmod == OPLM_BYTE
1844 && po->operand[1].type == OPT_CONST
1845 && po->operand[1].val == 0xff)
1847 snprintf(g_comment, sizeof(g_comment), "if %s", buf);
1848 snprintf(buf, buf_size, "(0)");
1852 // note: must be signed compare
1854 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
1855 scast, buf1, buf2, is_inv ? ">=" : "<");
1859 snprintf(buf, buf_size, "(%s %s %s)",
1860 buf1, is_inv ? ">=" : "<", buf2);
1864 snprintf(buf, buf_size, "(%s %s %s)",
1865 buf1, is_inv ? ">" : "<=", buf2);
1873 static void out_cmp_test(char *buf, size_t buf_size,
1874 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1876 char buf1[256], buf2[256], buf3[256];
1878 if (po->op == OP_TEST) {
1879 if (IS(opr_name(po, 0), opr_name(po, 1))) {
1880 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
1883 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
1884 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
1885 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
1887 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
1888 po->operand[0].lmod, buf3);
1890 else if (po->op == OP_CMP) {
1891 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
1894 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
1897 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
1898 struct parsed_opr *popr2)
1900 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
1901 ferr(po, "missing lmod for both operands\n");
1903 if (popr1->lmod == OPLM_UNSPEC)
1904 popr1->lmod = popr2->lmod;
1905 else if (popr2->lmod == OPLM_UNSPEC)
1906 popr2->lmod = popr1->lmod;
1907 else if (popr1->lmod != popr2->lmod) {
1908 if (popr1->type_from_var) {
1909 popr1->size_mismatch = 1;
1910 if (popr1->lmod < popr2->lmod)
1912 popr1->lmod = popr2->lmod;
1914 else if (popr2->type_from_var) {
1915 popr2->size_mismatch = 1;
1916 if (popr2->lmod < popr1->lmod)
1918 popr2->lmod = popr1->lmod;
1921 ferr(po, "conflicting lmods: %d vs %d\n",
1922 popr1->lmod, popr2->lmod);
1926 static const char *op_to_c(struct parsed_op *po)
1950 ferr(po, "op_to_c was supplied with %d\n", po->op);
1954 static void op_set_clear_flag(struct parsed_op *po,
1955 enum op_flags flag_set, enum op_flags flag_clear)
1957 po->flags |= flag_set;
1958 po->flags &= ~flag_clear;
1961 // last op in stream - unconditional branch or ret
1962 #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
1963 || ((ops[_i].flags & (OPF_JMP|OPF_CJMP|OPF_RMD)) == OPF_JMP \
1964 && ops[_i].op != OP_CALL))
1966 static int scan_for_pop(int i, int opcnt, const char *reg,
1967 int magic, int depth, int *maxdepth, int do_flags)
1969 const struct parsed_proto *pp;
1970 struct parsed_op *po;
1974 for (; i < opcnt; i++) {
1976 if (po->cc_scratch == magic)
1977 break; // already checked
1978 po->cc_scratch = magic;
1980 if (po->flags & OPF_TAIL) {
1981 if (po->op == OP_CALL) {
1982 pp = proto_parse(g_fhdr, po->operand[0].name, 0);
1983 if (pp != NULL && pp->is_noreturn)
1984 // no stack cleanup for noreturn
1987 return -1; // deadend
1990 if ((po->flags & OPF_RMD)
1991 || (po->op == OP_PUSH && po->argnum != 0)) // arg push
1994 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
1995 if (po->btj != NULL) {
1997 for (j = 0; j < po->btj->count; j++) {
1998 ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic,
1999 depth, maxdepth, do_flags);
2001 return ret; // dead end
2007 ferr(po, "dead branch\n");
2011 if (po->flags & OPF_CJMP) {
2012 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic,
2013 depth, maxdepth, do_flags);
2015 return ret; // dead end
2023 if ((po->op == OP_POP || po->op == OP_PUSH)
2024 && po->operand[0].type == OPT_REG
2025 && IS(po->operand[0].name, reg))
2027 if (po->op == OP_PUSH && !(po->flags & OPF_FARG)) {
2029 if (depth > *maxdepth)
2032 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2034 else if (po->op == OP_POP) {
2037 op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
2042 if (depth < 0) // should not happen
2043 ferr(po, "fail with depth\n");
2045 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2054 // scan for pop starting from 'ret' op (all paths)
2055 static int scan_for_pop_ret(int i, int opcnt, const char *reg,
2061 for (; i < opcnt; i++) {
2062 if (!(ops[i].flags & OPF_TAIL))
2065 for (j = i - 1; j >= 0; j--) {
2066 if (ops[j].flags & OPF_RMD)
2068 if (ops[j].flags & OPF_JMP)
2071 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
2072 && IS(ops[j].operand[0].name, reg))
2075 ops[j].flags |= flag_set;
2079 if (g_labels[j][0] != 0)
2084 return found ? 0 : -1;
2087 static void scan_propagate_df(int i, int opcnt)
2089 struct parsed_op *po = &ops[i];
2092 for (; i < opcnt; i++) {
2094 if (po->flags & OPF_DF)
2095 return; // already resolved
2096 po->flags |= OPF_DF;
2098 if (po->op == OP_CALL)
2099 ferr(po, "call with DF set?\n");
2101 if (po->flags & OPF_JMP) {
2102 if (po->btj != NULL) {
2104 for (j = 0; j < po->btj->count; j++)
2105 scan_propagate_df(po->btj->d[j].bt_i, opcnt);
2110 ferr(po, "dead branch\n");
2114 if (po->flags & OPF_CJMP)
2115 scan_propagate_df(po->bt_i, opcnt);
2121 if (po->flags & OPF_TAIL)
2124 if (po->op == OP_CLD) {
2125 po->flags |= OPF_RMD;
2130 ferr(po, "missing DF clear?\n");
2133 // is operand 'opr' modified by parsed_op 'po'?
2134 static int is_opr_modified(const struct parsed_opr *opr,
2135 const struct parsed_op *po)
2139 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2142 if (opr->type == OPT_REG) {
2143 if (po->op == OP_CALL) {
2144 mask = (1 << xAX) | (1 << xCX) | (1 << xDX);
2145 if ((1 << opr->reg) & mask)
2151 if (po->operand[0].type == OPT_REG) {
2152 if (po->regmask_dst & (1 << opr->reg))
2159 return IS(po->operand[0].name, opr->name);
2162 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
2163 static int is_any_opr_modified(const struct parsed_op *po_test,
2164 const struct parsed_op *po, int c_mode)
2169 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2172 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2175 if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
2178 // in reality, it can wreck any register, but in decompiled C
2179 // version it can only overwrite eax or edx:eax
2180 mask = (1 << xAX) | (1 << xDX);
2184 if (po->op == OP_CALL
2185 && ((po_test->regmask_src | po_test->regmask_dst) & mask))
2188 for (i = 0; i < po_test->operand_cnt; i++)
2189 if (IS(po_test->operand[i].name, po->operand[0].name))
2195 // scan for any po_test operand modification in range given
2196 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
2199 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2202 for (; i < opcnt; i++) {
2203 if (is_any_opr_modified(po_test, &ops[i], c_mode))
2210 // scan for po_test operand[0] modification in range given
2211 static int scan_for_mod_opr0(struct parsed_op *po_test,
2214 for (; i < opcnt; i++) {
2215 if (is_opr_modified(&po_test->operand[0], &ops[i]))
2222 static int scan_for_flag_set(int i, int magic, int *branched,
2223 int *setters, int *setter_cnt)
2225 struct label_ref *lr;
2229 if (ops[i].cc_scratch == magic) {
2230 ferr(&ops[i], "%s looped\n", __func__);
2233 ops[i].cc_scratch = magic;
2235 if (g_labels[i][0] != 0) {
2238 lr = &g_label_refs[i];
2239 for (; lr->next; lr = lr->next) {
2240 ret = scan_for_flag_set(lr->i, magic,
2241 branched, setters, setter_cnt);
2246 if (i > 0 && LAST_OP(i - 1)) {
2250 ret = scan_for_flag_set(lr->i, magic,
2251 branched, setters, setter_cnt);
2257 if (ops[i].flags & OPF_FLAGS) {
2258 setters[*setter_cnt] = i;
2263 if ((ops[i].flags & (OPF_JMP|OPF_CJMP)) == OPF_JMP)
2270 // scan back for cdq, if anything modifies edx, fail
2271 static int scan_for_cdq_edx(int i)
2274 if (g_labels[i][0] != 0) {
2275 if (g_label_refs[i].next != NULL)
2277 if (i > 0 && LAST_OP(i - 1)) {
2278 i = g_label_refs[i].i;
2285 if (ops[i].op == OP_CDQ)
2288 if (ops[i].regmask_dst & (1 << xDX))
2295 static int scan_for_reg_clear(int i, int reg)
2298 if (g_labels[i][0] != 0) {
2299 if (g_label_refs[i].next != NULL)
2301 if (i > 0 && LAST_OP(i - 1)) {
2302 i = g_label_refs[i].i;
2309 if (ops[i].op == OP_XOR
2310 && ops[i].operand[0].lmod == OPLM_DWORD
2311 && ops[i].operand[0].reg == ops[i].operand[1].reg
2312 && ops[i].operand[0].reg == reg)
2315 if (ops[i].regmask_dst & (1 << reg))
2322 // scan for positive, constant esp adjust
2323 static int scan_for_esp_adjust(int i, int opcnt, int *adj,
2326 struct parsed_op *po;
2329 *adj = *multipath = 0;
2331 for (; i < opcnt; i++) {
2334 if (g_labels[i][0] != 0)
2337 if (po->op == OP_ADD && po->operand[0].reg == xSP) {
2338 if (po->operand[1].type != OPT_CONST)
2339 ferr(&ops[i], "non-const esp adjust?\n");
2340 *adj += po->operand[1].val;
2342 ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
2345 else if (po->op == OP_PUSH && !(po->flags & OPF_RMD)) {
2346 //if (first_pop == -1)
2347 // first_pop = -2; // none
2348 *adj -= lmod_bytes(po, po->operand[0].lmod);
2350 else if (po->op == OP_POP && !(po->flags & OPF_RMD)) {
2351 // seems like msvc only uses 'pop ecx' for stack realignment..
2352 if (po->operand[0].type != OPT_REG || po->operand[0].reg != xCX)
2354 if (first_pop == -1 && *adj >= 0)
2356 *adj += lmod_bytes(po, po->operand[0].lmod);
2358 else if (po->flags & (OPF_JMP|OPF_TAIL)) {
2359 if (po->op == OP_JMP && po->btj == NULL) {
2363 if (po->op != OP_CALL)
2365 if (po->operand[0].type != OPT_LABEL)
2367 if (po->pp != NULL && po->pp->is_stdcall)
2372 if (first_pop >= 0) {
2373 // probably 'pop ecx' was used..
2380 static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
2382 struct parsed_op *po;
2386 ferr(ops, "%s: followed bad branch?\n", __func__);
2388 for (; i < opcnt; i++) {
2390 if (po->cc_scratch == magic)
2392 po->cc_scratch = magic;
2395 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2396 if (po->btj != NULL) {
2398 for (j = 0; j < po->btj->count; j++)
2399 scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
2403 scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
2404 if (!(po->flags & OPF_CJMP))
2407 if (po->flags & OPF_TAIL)
2412 static const struct parsed_proto *try_recover_pp(
2413 struct parsed_op *po, const struct parsed_opr *opr, int *search_instead)
2415 const struct parsed_proto *pp = NULL;
2419 // maybe an arg of g_func?
2420 if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
2422 char ofs_reg[16] = { 0, };
2423 int arg, arg_s, arg_i;
2427 parse_stack_access(po, opr->name, ofs_reg,
2428 &offset, &stack_ra, NULL, 0);
2429 if (ofs_reg[0] != 0)
2430 ferr(po, "offset reg on arg access?\n");
2431 if (offset <= stack_ra) {
2432 // search who set the stack var instead
2433 if (search_instead != NULL)
2434 *search_instead = 1;
2438 arg_i = (offset - stack_ra - 4) / 4;
2439 for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
2440 if (g_func_pp->arg[arg].reg != NULL)
2446 if (arg == g_func_pp->argc)
2447 ferr(po, "stack arg %d not in prototype?\n", arg_i);
2449 pp = g_func_pp->arg[arg].fptr;
2451 ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1);
2452 check_func_pp(po, pp, "icall arg");
2454 else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
2456 p = strchr(opr->name + 1, '[');
2457 memcpy(buf, opr->name, p - opr->name);
2458 buf[p - opr->name] = 0;
2459 pp = proto_parse(g_fhdr, buf, 0);
2461 else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
2462 pp = proto_parse(g_fhdr, opr->name, 0);
2464 ferr(po, "proto_parse failed for icall from '%s'\n", opr->name);
2465 check_func_pp(po, pp, "reg-fptr ref");
2471 static void scan_for_call_type(int i, const struct parsed_opr *opr,
2472 int magic, const struct parsed_proto **pp_found, int *multi)
2474 const struct parsed_proto *pp = NULL;
2475 struct parsed_op *po;
2476 struct label_ref *lr;
2478 ops[i].cc_scratch = magic;
2481 if (g_labels[i][0] != 0) {
2482 lr = &g_label_refs[i];
2483 for (; lr != NULL; lr = lr->next)
2484 scan_for_call_type(lr->i, opr, magic, pp_found, multi);
2485 if (i > 0 && LAST_OP(i - 1))
2493 if (ops[i].cc_scratch == magic)
2495 ops[i].cc_scratch = magic;
2497 if (!(ops[i].flags & OPF_DATA))
2499 if (!is_opr_modified(opr, &ops[i]))
2501 if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
2502 // most probably trashed by some processing
2507 opr = &ops[i].operand[1];
2508 if (opr->type != OPT_REG)
2512 po = (i >= 0) ? &ops[i] : ops;
2515 // reached the top - can only be an arg-reg
2516 if (opr->type != OPT_REG)
2519 for (i = 0; i < g_func_pp->argc; i++) {
2520 if (g_func_pp->arg[i].reg == NULL)
2522 if (IS(opr->name, g_func_pp->arg[i].reg))
2525 if (i == g_func_pp->argc)
2527 pp = g_func_pp->arg[i].fptr;
2529 ferr(po, "icall: arg%d (%s) is not a fptr?\n",
2530 i + 1, g_func_pp->arg[i].reg);
2531 check_func_pp(po, pp, "icall reg-arg");
2534 pp = try_recover_pp(po, opr, NULL);
2536 if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
2537 if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
2538 || (*pp_found)->is_stdcall != pp->is_stdcall
2539 || (*pp_found)->is_fptr != pp->is_fptr
2540 || (*pp_found)->argc != pp->argc
2541 || (*pp_found)->argc_reg != pp->argc_reg
2542 || (*pp_found)->argc_stack != pp->argc_stack)
2544 ferr(po, "icall: parsed_proto mismatch\n");
2552 static const struct parsed_proto *resolve_icall(int i, int opcnt,
2555 const struct parsed_proto *pp = NULL;
2556 int search_advice = 0;
2560 switch (ops[i].operand[0].type) {
2564 pp = try_recover_pp(&ops[i], &ops[i].operand[0], &search_advice);
2569 scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
2577 static int try_resolve_const(int i, const struct parsed_opr *opr,
2578 int magic, unsigned int *val)
2580 struct label_ref *lr;
2583 ops[i].cc_scratch = magic;
2586 if (g_labels[i][0] != 0) {
2587 lr = &g_label_refs[i];
2588 for (; lr != NULL; lr = lr->next)
2589 ret |= try_resolve_const(lr->i, opr, magic, val);
2590 if (i > 0 && LAST_OP(i - 1))
2598 if (ops[i].cc_scratch == magic)
2600 ops[i].cc_scratch = magic;
2602 if (!(ops[i].flags & OPF_DATA))
2604 if (!is_opr_modified(opr, &ops[i]))
2606 if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST)
2609 *val = ops[i].operand[1].val;
2614 static int collect_call_args_r(struct parsed_op *po, int i,
2615 struct parsed_proto *pp, int *regmask, int *save_arg_vars, int arg,
2616 int magic, int need_op_saving, int may_reuse)
2618 struct parsed_proto *pp_tmp;
2619 struct label_ref *lr;
2620 int need_to_save_current;
2625 ferr(po, "dead label encountered\n");
2629 for (; arg < pp->argc; arg++)
2630 if (pp->arg[arg].reg == NULL)
2632 magic = (magic & 0xffffff) | (arg << 24);
2634 for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
2636 if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
2637 if (ops[j].cc_scratch != magic) {
2638 ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
2642 // ok: have already been here
2645 ops[j].cc_scratch = magic;
2647 if (g_labels[j][0] != 0 && g_label_refs[j].i != -1) {
2648 lr = &g_label_refs[j];
2649 if (lr->next != NULL)
2651 for (; lr->next; lr = lr->next) {
2652 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
2654 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2655 arg, magic, need_op_saving, may_reuse);
2660 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
2662 if (j > 0 && LAST_OP(j - 1)) {
2663 // follow last branch in reverse
2668 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2669 arg, magic, need_op_saving, may_reuse);
2675 if (ops[j].op == OP_CALL)
2677 if (pp->is_unresolved)
2682 ferr(po, "arg collect hit unparsed call '%s'\n",
2683 ops[j].operand[0].name);
2684 if (may_reuse && pp_tmp->argc_stack > 0)
2685 ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
2686 arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
2688 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP) {
2689 if (pp->is_unresolved)
2692 ferr(po, "arg collect %d/%d hit esp adjust\n",
2695 else if (ops[j].op == OP_POP) {
2696 if (pp->is_unresolved)
2699 ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc);
2701 else if (ops[j].flags & OPF_CJMP)
2703 if (pp->is_unresolved)
2708 else if (ops[j].op == OP_PUSH && !(ops[j].flags & OPF_FARG))
2710 if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
2713 pp->arg[arg].datap = &ops[j];
2714 need_to_save_current = 0;
2715 if (!need_op_saving) {
2716 ret = scan_for_mod(&ops[j], j + 1, i, 1);
2717 need_to_save_current = (ret >= 0);
2719 if (need_op_saving || need_to_save_current) {
2720 // mark this push as one that needs operand saving
2721 ops[j].flags &= ~OPF_RMD;
2722 if (ops[j].argnum == 0) {
2723 ops[j].argnum = arg + 1;
2724 *save_arg_vars |= 1 << arg;
2726 else if (ops[j].argnum < arg + 1)
2727 ferr(&ops[j], "argnum conflict (%d<%d) for '%s'\n",
2728 ops[j].argnum, arg + 1, pp->name);
2730 else if (ops[j].argnum == 0)
2731 ops[j].flags |= OPF_RMD;
2733 // some PUSHes are reused by different calls on other branches,
2734 // but that can't happen if we didn't branch, so they
2735 // can be removed from future searches (handles nested calls)
2737 ops[j].flags |= OPF_FARG;
2739 ops[j].flags &= ~OPF_RSAVE;
2742 if (!pp->is_unresolved) {
2744 for (; arg < pp->argc; arg++)
2745 if (pp->arg[arg].reg == NULL)
2748 magic = (magic & 0xffffff) | (arg << 24);
2750 // tracking reg usage
2751 if (ops[j].operand[0].type == OPT_REG)
2752 *regmask |= 1 << ops[j].operand[0].reg;
2756 if (arg < pp->argc) {
2757 ferr(po, "arg collect failed for '%s': %d/%d\n",
2758 pp->name, arg, pp->argc);
2765 static int collect_call_args(struct parsed_op *po, int i,
2766 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
2772 ret = collect_call_args_r(po, i, pp, regmask, save_arg_vars,
2777 if (pp->is_unresolved) {
2779 pp->argc_stack += ret;
2780 for (a = 0; a < pp->argc; a++)
2781 if (pp->arg[a].type.name == NULL)
2782 pp->arg[a].type.name = strdup("int");
2788 // early check for tail call or branch back
2789 static int is_like_tailjmp(int j)
2791 if (!(ops[j].flags & OPF_JMP))
2794 if (ops[j].op == OP_JMP && !ops[j].operand[0].had_ds)
2795 // probably local branch back..
2797 if (ops[j].op == OP_CALL)
2798 // probably noreturn call..
2804 static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
2808 for (i = 0; i < pp->argc; i++)
2809 if (pp->arg[i].reg == NULL)
2813 memmove(&pp->arg[i + 1], &pp->arg[i],
2814 sizeof(pp->arg[0]) * pp->argc_stack);
2815 memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
2816 pp->arg[i].reg = strdup(reg);
2817 pp->arg[i].type.name = strdup("int");
2822 static void add_label_ref(struct label_ref *lr, int op_i)
2824 struct label_ref *lr_new;
2831 lr_new = calloc(1, sizeof(*lr_new));
2833 lr_new->next = lr->next;
2837 static void output_std_flags(FILE *fout, struct parsed_op *po,
2838 int *pfomask, const char *dst_opr_text)
2840 if (*pfomask & (1 << PFO_Z)) {
2841 fprintf(fout, "\n cond_z = (%s%s == 0);",
2842 lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
2843 *pfomask &= ~(1 << PFO_Z);
2845 if (*pfomask & (1 << PFO_S)) {
2846 fprintf(fout, "\n cond_s = (%s%s < 0);",
2847 lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
2848 *pfomask &= ~(1 << PFO_S);
2852 static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp,
2855 if (pp->is_fastcall)
2856 fprintf(fout, "__fastcall ");
2857 else if (pp->is_stdcall && pp->argc_reg == 0)
2858 fprintf(fout, "__stdcall ");
2859 if (pp->is_noreturn || is_noreturn)
2860 fprintf(fout, "noreturn ");
2863 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
2865 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
2866 struct parsed_opr *last_arith_dst = NULL;
2867 char buf1[256], buf2[256], buf3[256], cast[64];
2868 const struct parsed_proto *pp_c;
2869 struct parsed_proto *pp, *pp_tmp;
2870 struct parsed_data *pd;
2871 const char *tmpname;
2873 int save_arg_vars = 0;
2875 int need_tmp_var = 0;
2878 int label_pending = 0;
2879 int regmask_save = 0;
2880 int regmask_arg = 0;
2881 int regmask_now = 0;
2882 int regmask_init = 0;
2893 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
2894 g_stack_frame_used = 0;
2896 g_func_pp = proto_parse(fhdr, funcn, 0);
2897 if (g_func_pp == NULL)
2898 ferr(ops, "proto_parse failed for '%s'\n", funcn);
2900 for (i = 0; i < g_func_pp->argc; i++) {
2901 if (g_func_pp->arg[i].reg != NULL) {
2902 reg = char_array_i(regs_r32,
2903 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
2905 ferr(ops, "arg '%s' is not a reg?\n", g_func_pp->arg[i].reg);
2906 regmask_arg |= 1 << reg;
2911 // - handle ebp/esp frame, remove ops related to it
2912 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
2913 && ops[1].op == OP_MOV
2914 && IS(opr_name(&ops[1], 0), "ebp")
2915 && IS(opr_name(&ops[1], 1), "esp"))
2920 ops[0].flags |= OPF_RMD;
2921 ops[1].flags |= OPF_RMD;
2924 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
2925 g_stack_fsz = opr_const(&ops[2], 1);
2926 ops[2].flags |= OPF_RMD;
2930 // another way msvc builds stack frame..
2932 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
2934 ops[i].flags |= OPF_RMD;
2938 // and another way..
2939 if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
2940 && ops[i].operand[1].type == OPT_CONST
2941 && ops[i + 1].op == OP_CALL
2942 && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
2944 g_stack_fsz += ops[i].operand[1].val;
2945 ops[i].flags |= OPF_RMD;
2947 ops[i].flags |= OPF_RMD;
2954 for (; i < opcnt; i++)
2955 if (ops[i].op == OP_RET)
2958 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
2959 if (found && is_like_tailjmp(j))
2964 if ((ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp"))
2965 || ops[j].op == OP_LEAVE)
2967 ops[j].flags |= OPF_RMD;
2969 else if (!(g_ida_func_attr & IDAFA_NORETURN))
2970 ferr(&ops[j], "'pop ebp' expected\n");
2972 if (g_stack_fsz != 0) {
2973 if (ops[j - 1].op == OP_MOV
2974 && IS(opr_name(&ops[j - 1], 0), "esp")
2975 && IS(opr_name(&ops[j - 1], 1), "ebp"))
2977 ops[j - 1].flags |= OPF_RMD;
2979 else if (ops[j].op != OP_LEAVE
2980 && !(g_ida_func_attr & IDAFA_NORETURN))
2982 ferr(&ops[j - 1], "esp restore expected\n");
2985 if (ecx_push && ops[j - 2].op == OP_POP
2986 && IS(opr_name(&ops[j - 2], 0), "ecx"))
2988 ferr(&ops[j - 2], "unexpected ecx pop\n");
2994 } while (i < opcnt);
2997 for (i = 0; i < opcnt; i++) {
2998 if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
3000 if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
3001 && ops[i].operand[1].type == OPT_CONST)
3011 g_stack_fsz = ops[i].operand[1].val;
3012 ops[i].flags |= OPF_RMD;
3016 for (; i < opcnt; i++)
3017 if (ops[i].op == OP_RET)
3020 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
3021 if (found && is_like_tailjmp(j))
3026 if (ops[j].op != OP_ADD
3027 || !IS(opr_name(&ops[j], 0), "esp")
3028 || ops[j].operand[1].type != OPT_CONST
3029 || ops[j].operand[1].val != g_stack_fsz)
3030 ferr(&ops[j], "'add esp' expected\n");
3031 ops[j].flags |= OPF_RMD;
3035 } while (i < opcnt);
3040 // - parse calls with labels
3041 // - resolve all branches
3042 for (i = 0; i < opcnt; i++)
3048 if (po->flags & OPF_RMD)
3051 if (po->op == OP_CALL) {
3054 if (po->operand[0].type == OPT_LABEL) {
3055 tmpname = opr_name(po, 0);
3056 if (IS_START(tmpname, "loc_"))
3057 ferr(po, "call to loc_*\n");
3058 pp_c = proto_parse(fhdr, tmpname, 0);
3060 ferr(po, "proto_parse failed for call '%s'\n", tmpname);
3062 pp = proto_clone(pp_c);
3063 my_assert_not(pp, NULL);
3065 else if (po->datap != NULL) {
3066 pp = calloc(1, sizeof(*pp));
3067 my_assert_not(pp, NULL);
3069 ret = parse_protostr(po->datap, pp);
3071 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
3078 check_func_pp(po, pp, "fptr var call");
3079 if (pp->is_noreturn)
3080 po->flags |= OPF_TAIL;
3086 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
3089 if (po->operand[0].type == OPT_REGMEM) {
3090 char *p = strchr(po->operand[0].name, '[');
3093 ret = p - po->operand[0].name;
3094 strncpy(buf1, po->operand[0].name, ret);
3097 for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
3098 if (IS(g_func_pd[j].label, buf1)) {
3104 //ferr(po, "label '%s' not parsed?\n", buf1);
3106 if (pd->type != OPT_OFFSET)
3107 ferr(po, "label '%s' with non-offset data?\n", buf1);
3109 // find all labels, link
3110 for (j = 0; j < pd->count; j++) {
3111 for (l = 0; l < opcnt; l++) {
3112 if (g_labels[l][0] && IS(g_labels[l], pd->d[j].u.label)) {
3113 add_label_ref(&g_label_refs[l], i);
3124 for (l = 0; l < opcnt; l++) {
3125 if (g_labels[l][0] && IS(po->operand[0].name, g_labels[l])) {
3126 if (l == i + 1 && po->op == OP_JMP) {
3127 // yet another alignment type..
3128 po->flags |= OPF_RMD;
3131 add_label_ref(&g_label_refs[l], i);
3137 if (po->bt_i != -1 || (po->flags & OPF_RMD))
3140 if (po->operand[0].type == OPT_LABEL)
3144 ferr(po, "unhandled branch\n");
3148 po->flags |= OPF_TAIL;
3149 if (i > 0 && ops[i - 1].op == OP_POP)
3150 po->flags |= OPF_ATAIL;
3155 // - remove dead labels
3157 for (i = 0; i < opcnt; i++)
3159 if (g_labels[i][0] != 0 && g_label_refs[i].i == -1)
3163 if (po->flags & OPF_RMD)
3166 if (po->op == OP_CALL)
3168 tmpname = opr_name(po, 0);
3173 pp_c = resolve_icall(i, opcnt, &l);
3175 pp = proto_clone(pp_c);
3176 my_assert_not(pp, NULL);
3178 // not resolved just to single func
3181 switch (po->operand[0].type) {
3183 // we resolved this call and no longer need the register
3184 po->regmask_src &= ~(1 << po->operand[0].reg);
3194 pp = calloc(1, sizeof(*pp));
3195 my_assert_not(pp, NULL);
3197 ret = scan_for_esp_adjust(i + 1, opcnt, &j, &l);
3199 if (!g_allow_regfunc)
3200 ferr(po, "non-__cdecl indirect call unhandled yet\n");
3201 pp->is_unresolved = 1;
3205 if (j > ARRAY_SIZE(pp->arg))
3206 ferr(po, "esp adjust too large: %d\n", j);
3207 pp->ret_type.name = strdup("int");
3208 pp->argc = pp->argc_stack = j;
3209 for (arg = 0; arg < pp->argc; arg++)
3210 pp->arg[arg].type.name = strdup("int");
3215 // look for and make use of esp adjust
3217 if (!pp->is_stdcall && pp->argc_stack > 0)
3218 ret = scan_for_esp_adjust(i + 1, opcnt, &j, &l);
3220 if (pp->is_vararg) {
3221 if (j / 4 < pp->argc_stack)
3222 ferr(po, "esp adjust is too small: %x < %x\n",
3223 j, pp->argc_stack * 4);
3224 // modify pp to make it have varargs as normal args
3226 pp->argc += j / 4 - pp->argc_stack;
3227 for (; arg < pp->argc; arg++) {
3228 pp->arg[arg].type.name = strdup("int");
3231 if (pp->argc > ARRAY_SIZE(pp->arg))
3232 ferr(po, "too many args for '%s'\n", tmpname);
3234 if (pp->argc_stack != j / 4)
3235 ferr(po, "stack tracking failed for '%s': %x %x\n",
3236 tmpname, pp->argc_stack * 4, j);
3238 ops[ret].flags |= OPF_RMD;
3239 if (ops[ret].op == OP_POP && j > 4) {
3240 // deal with multi-pop stack adjust
3242 while (ops[ret].op == OP_POP && j > 0 && ret < opcnt) {
3243 ops[ret].flags |= OPF_RMD;
3249 // a bit of a hack, but deals with use of
3250 // single adj for multiple calls
3251 ops[ret].operand[1].val -= j;
3254 else if (pp->is_vararg)
3255 ferr(po, "missing esp_adjust for vararg func '%s'\n",
3258 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
3259 // since we know the args, collect them
3260 collect_call_args(po, i, pp, ®mask, &save_arg_vars,
3264 if (strstr(pp->ret_type.name, "int64"))
3270 // - find POPs for PUSHes, rm both
3271 // - scan for STD/CLD, propagate DF
3272 // - scan for all used registers
3273 // - find flag set ops for their users
3274 // - do unreselved calls
3275 // - declare indirect functions
3276 for (i = 0; i < opcnt; i++) {
3278 if (po->flags & OPF_RMD)
3281 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
3282 reg = po->operand[0].reg;
3283 if (!(regmask & (1 << reg)))
3284 // not a reg save after all, rerun scan_for_pop
3285 po->flags &= ~OPF_RSAVE;
3287 regmask_save |= 1 << reg;
3290 if (po->op == OP_PUSH && po->argnum == 0
3291 && !(po->flags & OPF_RSAVE) && !g_func_pp->is_userstack)
3293 if (po->operand[0].type == OPT_REG)
3295 reg = po->operand[0].reg;
3297 ferr(po, "reg not set for push?\n");
3300 ret = scan_for_pop(i + 1, opcnt,
3301 po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
3304 ferr(po, "too much depth: %d\n", depth);
3306 po->flags |= OPF_RMD;
3307 scan_for_pop(i + 1, opcnt, po->operand[0].name,
3308 i + opcnt * 4, 0, &depth, 1);
3311 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
3314 if (regmask & (1 << reg)) {
3315 if (regmask_save & (1 << reg))
3316 ferr(po, "%s already saved?\n", po->operand[0].name);
3320 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg);
3324 else if (po->operand[0].type == OPT_CONST) {
3325 for (j = i + 1; j < opcnt; j++) {
3326 if ((ops[j].flags & (OPF_JMP|OPF_TAIL|OPF_RSAVE))
3327 || ops[j].op == OP_PUSH || g_labels[i][0] != 0)
3332 if (!(ops[j].flags & OPF_RMD) && ops[j].op == OP_POP)
3334 po->flags |= OPF_RMD;
3342 if (po->op == OP_STD) {
3343 po->flags |= OPF_DF | OPF_RMD;
3344 scan_propagate_df(i + 1, opcnt);
3347 regmask_now = po->regmask_src | po->regmask_dst;
3348 if (regmask_now & (1 << xBP)) {
3349 if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
3350 if (po->regmask_dst & (1 << xBP))
3351 // compiler decided to drop bp frame and use ebp as scratch
3352 scan_fwd_set_flags(i + 1, opcnt, i + opcnt * 5, OPF_EBP_S);
3354 regmask_now &= ~(1 << xBP);
3358 regmask |= regmask_now;
3360 if (po->flags & OPF_CC)
3362 int setters[16], cnt = 0, branched = 0;
3364 ret = scan_for_flag_set(i, i + opcnt * 6,
3365 &branched, setters, &cnt);
3366 if (ret < 0 || cnt <= 0)
3367 ferr(po, "unable to trace flag setter(s)\n");
3368 if (cnt > ARRAY_SIZE(setters))
3369 ferr(po, "too many flag setters\n");
3371 for (j = 0; j < cnt; j++)
3373 tmp_op = &ops[setters[j]]; // flag setter
3376 // to get nicer code, we try to delay test and cmp;
3377 // if we can't because of operand modification, or if we
3378 // have arith op, or branch, make it calculate flags explicitly
3379 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP)
3381 if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
3382 pfomask = 1 << po->pfo;
3384 else if (tmp_op->op == OP_CMPS || tmp_op->op == OP_SCAS) {
3385 pfomask = 1 << po->pfo;
3388 // see if we'll be able to handle based on op result
3389 if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
3390 && po->pfo != PFO_Z && po->pfo != PFO_S
3391 && po->pfo != PFO_P)
3393 || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
3395 pfomask = 1 << po->pfo;
3398 if (tmp_op->op == OP_ADD && po->pfo == PFO_C) {
3399 propagate_lmod(tmp_op, &tmp_op->operand[0],
3400 &tmp_op->operand[1]);
3401 if (tmp_op->operand[0].lmod == OPLM_DWORD)
3406 tmp_op->pfomask |= pfomask;
3407 cond_vars |= pfomask;
3409 // note: may overwrite, currently not a problem
3413 if (po->op == OP_RCL || po->op == OP_RCR
3414 || po->op == OP_ADC || po->op == OP_SBB)
3415 cond_vars |= 1 << PFO_C;
3418 if (po->op == OP_CMPS || po->op == OP_SCAS) {
3419 cond_vars |= 1 << PFO_Z;
3421 else if (po->op == OP_MUL
3422 || (po->op == OP_IMUL && po->operand_cnt == 1))
3424 if (po->operand[0].lmod == OPLM_DWORD)
3427 else if (po->op == OP_CALL) {
3430 ferr(po, "NULL pp\n");
3432 if (pp->is_unresolved) {
3433 int regmask_stack = 0;
3434 collect_call_args(po, i, pp, ®mask, &save_arg_vars,
3437 // this is pretty rough guess:
3438 // see ecx and edx were pushed (and not their saved versions)
3439 for (arg = 0; arg < pp->argc; arg++) {
3440 if (pp->arg[arg].reg != NULL)
3443 tmp_op = pp->arg[arg].datap;
3445 ferr(po, "parsed_op missing for arg%d\n", arg);
3446 if (tmp_op->argnum == 0 && tmp_op->operand[0].type == OPT_REG)
3447 regmask_stack |= 1 << tmp_op->operand[0].reg;
3450 if (!((regmask_stack & (1 << xCX))
3451 && (regmask_stack & (1 << xDX))))
3453 if (pp->argc_stack != 0
3454 || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX))))
3456 pp_insert_reg_arg(pp, "ecx");
3457 pp->is_fastcall = 1;
3458 regmask_init |= 1 << xCX;
3459 regmask |= 1 << xCX;
3461 if (pp->argc_stack != 0
3462 || ((regmask | regmask_arg) & (1 << xDX)))
3464 pp_insert_reg_arg(pp, "edx");
3465 regmask_init |= 1 << xDX;
3466 regmask |= 1 << xDX;
3470 // note: __cdecl doesn't fall into is_unresolved category
3471 if (pp->argc_stack > 0)
3475 for (arg = 0; arg < pp->argc; arg++) {
3476 if (pp->arg[arg].reg != NULL) {
3477 reg = char_array_i(regs_r32,
3478 ARRAY_SIZE(regs_r32), pp->arg[arg].reg);
3480 ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg);
3481 if (!(regmask & (1 << reg))) {
3482 regmask_init |= 1 << reg;
3483 regmask |= 1 << reg;
3488 else if (po->op == OP_MOV && po->operand[0].pp != NULL
3489 && po->operand[1].pp != NULL)
3491 // <var> = offset <something>
3492 if ((po->operand[1].pp->is_func || po->operand[1].pp->is_fptr)
3493 && !IS_START(po->operand[1].name, "off_"))
3495 if (!po->operand[0].pp->is_fptr)
3496 ferr(po, "%s not declared as fptr when it should be\n",
3497 po->operand[0].name);
3498 if (pp_cmp_func(po->operand[0].pp, po->operand[1].pp)) {
3499 pp_print(buf1, sizeof(buf1), po->operand[0].pp);
3500 pp_print(buf2, sizeof(buf2), po->operand[1].pp);
3501 fnote(po, "var: %s\n", buf1);
3502 fnote(po, "func: %s\n", buf2);
3503 ferr(po, "^ mismatch\n");
3507 else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void"))
3508 regmask |= 1 << xAX;
3509 else if (po->op == OP_DIV || po->op == OP_IDIV) {
3510 // 32bit division is common, look for it
3511 if (po->op == OP_DIV)
3512 ret = scan_for_reg_clear(i, xDX);
3514 ret = scan_for_cdq_edx(i);
3516 po->flags |= OPF_32BIT;
3520 else if (po->op == OP_CLD)
3521 po->flags |= OPF_RMD;
3523 if (po->op == OP_RCL || po->op == OP_RCR || po->op == OP_XCHG) {
3529 // - confirm regmask_save, it might have been reduced
3530 if (regmask_save != 0)
3533 for (i = 0; i < opcnt; i++) {
3535 if (po->flags & OPF_RMD)
3538 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE))
3539 regmask_save |= 1 << po->operand[0].reg;
3543 // output starts here
3545 // define userstack size
3546 if (g_func_pp->is_userstack) {
3547 fprintf(fout, "#ifndef US_SZ_%s\n", g_func_pp->name);
3548 fprintf(fout, "#define US_SZ_%s USERSTACK_SIZE\n", g_func_pp->name);
3549 fprintf(fout, "#endif\n");
3552 // the function itself
3553 fprintf(fout, "%s ", g_func_pp->ret_type.name);
3554 output_pp_attrs(fout, g_func_pp, g_ida_func_attr & IDAFA_NORETURN);
3555 fprintf(fout, "%s(", g_func_pp->name);
3557 for (i = 0; i < g_func_pp->argc; i++) {
3559 fprintf(fout, ", ");
3560 if (g_func_pp->arg[i].fptr != NULL) {
3562 pp = g_func_pp->arg[i].fptr;
3563 fprintf(fout, "%s (", pp->ret_type.name);
3564 output_pp_attrs(fout, pp, 0);
3565 fprintf(fout, "*a%d)(", i + 1);
3566 for (j = 0; j < pp->argc; j++) {
3568 fprintf(fout, ", ");
3569 if (pp->arg[j].fptr)
3570 ferr(ops, "nested fptr\n");
3571 fprintf(fout, "%s", pp->arg[j].type.name);
3573 if (pp->is_vararg) {
3575 fprintf(fout, ", ");
3576 fprintf(fout, "...");
3580 else if (g_func_pp->arg[i].type.is_retreg) {
3581 fprintf(fout, "u32 *r_%s", g_func_pp->arg[i].reg);
3584 fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1);
3587 if (g_func_pp->is_vararg) {
3589 fprintf(fout, ", ");
3590 fprintf(fout, "...");
3593 fprintf(fout, ")\n{\n");
3595 // declare indirect functions
3596 for (i = 0; i < opcnt; i++) {
3598 if (po->flags & OPF_RMD)
3601 if (po->op == OP_CALL) {
3604 ferr(po, "NULL pp\n");
3606 if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) {
3607 if (pp->name[0] != 0) {
3608 memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
3609 memcpy(pp->name, "i_", 2);
3611 // might be declared already
3613 for (j = 0; j < i; j++) {
3614 if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) {
3615 if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) {
3625 snprintf(pp->name, sizeof(pp->name), "icall%d", i);
3627 fprintf(fout, " %s (", pp->ret_type.name);
3628 output_pp_attrs(fout, pp, 0);
3629 fprintf(fout, "*%s)(", pp->name);
3630 for (j = 0; j < pp->argc; j++) {
3632 fprintf(fout, ", ");
3633 fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1);
3635 fprintf(fout, ");\n");
3640 // output LUTs/jumptables
3641 for (i = 0; i < g_func_pd_cnt; i++) {
3643 fprintf(fout, " static const ");
3644 if (pd->type == OPT_OFFSET) {
3645 fprintf(fout, "void *jt_%s[] =\n { ", pd->label);
3647 for (j = 0; j < pd->count; j++) {
3649 fprintf(fout, ", ");
3650 fprintf(fout, "&&%s", pd->d[j].u.label);
3654 fprintf(fout, "%s %s[] =\n { ",
3655 lmod_type_u(ops, pd->lmod), pd->label);
3657 for (j = 0; j < pd->count; j++) {
3659 fprintf(fout, ", ");
3660 fprintf(fout, "%u", pd->d[j].u.val);
3663 fprintf(fout, " };\n");
3667 // declare stack frame, va_arg
3669 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
3670 (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz);
3674 if (g_func_pp->is_userstack) {
3675 fprintf(fout, " u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name);
3676 fprintf(fout, " u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n");
3680 if (g_func_pp->is_vararg) {
3681 fprintf(fout, " va_list ap;\n");
3685 // declare arg-registers
3686 for (i = 0; i < g_func_pp->argc; i++) {
3687 if (g_func_pp->arg[i].reg != NULL) {
3688 reg = char_array_i(regs_r32,
3689 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
3690 if (regmask & (1 << reg)) {
3691 if (g_func_pp->arg[i].type.is_retreg)
3692 fprintf(fout, " u32 %s = *r_%s;\n",
3693 g_func_pp->arg[i].reg, g_func_pp->arg[i].reg);
3695 fprintf(fout, " u32 %s = (u32)a%d;\n",
3696 g_func_pp->arg[i].reg, i + 1);
3699 if (g_func_pp->arg[i].type.is_retreg)
3700 ferr(ops, "retreg '%s' is unused?\n",
3701 g_func_pp->arg[i].reg);
3702 fprintf(fout, " // %s = a%d; // unused\n",
3703 g_func_pp->arg[i].reg, i + 1);
3709 regmask_now = regmask & ~regmask_arg;
3710 regmask_now &= ~(1 << xSP);
3712 for (reg = 0; reg < 8; reg++) {
3713 if (regmask_now & (1 << reg)) {
3714 fprintf(fout, " u32 %s", regs_r32[reg]);
3715 if (regmask_init & (1 << reg))
3716 fprintf(fout, " = 0");
3717 fprintf(fout, ";\n");
3724 for (reg = 0; reg < 8; reg++) {
3725 if (regmask_save & (1 << reg)) {
3726 fprintf(fout, " u32 s_%s;\n", regs_r32[reg]);
3732 if (save_arg_vars) {
3733 for (reg = 0; reg < 32; reg++) {
3734 if (save_arg_vars & (1 << reg)) {
3735 fprintf(fout, " u32 s_a%d;\n", reg + 1);
3742 for (i = 0; i < 8; i++) {
3743 if (cond_vars & (1 << i)) {
3744 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
3751 fprintf(fout, " u32 tmp;\n");
3756 fprintf(fout, " u64 tmp64;\n");
3761 fprintf(fout, "\n");
3763 if (g_func_pp->is_vararg) {
3764 if (g_func_pp->argc_stack == 0)
3765 ferr(ops, "vararg func without stack args?\n");
3766 fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc);
3770 for (i = 0; i < opcnt; i++)
3772 if (g_labels[i][0] != 0) {
3773 fprintf(fout, "\n%s:\n", g_labels[i]);
3776 delayed_flag_op = NULL;
3777 last_arith_dst = NULL;
3781 if (po->flags & OPF_RMD)
3786 #define assert_operand_cnt(n_) \
3787 if (po->operand_cnt != n_) \
3788 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
3790 // conditional/flag using op?
3791 if (po->flags & OPF_CC)
3797 // we go through all this trouble to avoid using parsed_flag_op,
3798 // which makes generated code much nicer
3799 if (delayed_flag_op != NULL)
3801 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op,
3802 po->pfo, po->pfo_inv);
3805 else if (last_arith_dst != NULL
3806 && (po->pfo == PFO_Z || po->pfo == PFO_S || po->pfo == PFO_P
3807 || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
3810 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
3811 out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv,
3812 last_arith_dst->lmod, buf3);
3815 else if (tmp_op != NULL) {
3816 // use preprocessed flag calc results
3817 if (!(tmp_op->pfomask & (1 << po->pfo)))
3818 ferr(po, "not prepared for pfo %d\n", po->pfo);
3820 // note: pfo_inv was not yet applied
3821 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
3822 po->pfo_inv ? "!" : "", parsed_flag_op_names[po->pfo]);
3825 ferr(po, "all methods of finding comparison failed\n");
3828 if (po->flags & OPF_JMP) {
3829 fprintf(fout, " if %s", buf1);
3831 else if (po->op == OP_RCL || po->op == OP_RCR
3832 || po->op == OP_ADC || po->op == OP_SBB)
3835 fprintf(fout, " cond_%s = %s;\n",
3836 parsed_flag_op_names[po->pfo], buf1);
3838 else if (po->flags & OPF_DATA) { // SETcc
3839 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
3840 fprintf(fout, " %s = %s;", buf2, buf1);
3843 ferr(po, "unhandled conditional op\n");
3847 pfomask = po->pfomask;
3849 if (po->flags & (OPF_REPZ|OPF_REPNZ)) {
3850 struct parsed_opr opr = {0,};
3853 opr.lmod = OPLM_DWORD;
3854 ret = try_resolve_const(i, &opr, opcnt * 7 + i, &uval);
3856 if (ret != 1 || uval == 0) {
3857 // we need initial flags for ecx=0 case..
3858 if (i > 0 && ops[i - 1].op == OP_XOR
3859 && IS(ops[i - 1].operand[0].name,
3860 ops[i - 1].operand[1].name))
3862 fprintf(fout, " cond_z = ");
3863 if (pfomask & (1 << PFO_C))
3864 fprintf(fout, "cond_c = ");
3865 fprintf(fout, "0;\n");
3867 else if (last_arith_dst != NULL) {
3868 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
3869 out_test_for_cc(buf1, sizeof(buf1), po, PFO_Z, 0,
3870 last_arith_dst->lmod, buf3);
3871 fprintf(fout, " cond_z = %s;\n", buf1);
3874 ferr(po, "missing initial ZF\n");
3881 assert_operand_cnt(2);
3882 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3883 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3884 default_cast_to(buf3, sizeof(buf3), &po->operand[0]);
3885 fprintf(fout, " %s = %s;", buf1,
3886 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3891 assert_operand_cnt(2);
3892 po->operand[1].lmod = OPLM_DWORD; // always
3893 fprintf(fout, " %s = %s;",
3894 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3895 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3900 assert_operand_cnt(2);
3901 fprintf(fout, " %s = %s;",
3902 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3903 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3907 assert_operand_cnt(2);
3908 switch (po->operand[1].lmod) {
3910 strcpy(buf3, "(s8)");
3913 strcpy(buf3, "(s16)");
3916 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
3918 fprintf(fout, " %s = %s;",
3919 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3920 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3925 assert_operand_cnt(2);
3926 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3927 fprintf(fout, " tmp = %s;",
3928 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0));
3929 fprintf(fout, " %s = %s;",
3930 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3931 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3932 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
3933 fprintf(fout, " %s = %stmp;",
3934 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
3935 default_cast_to(buf3, sizeof(buf3), &po->operand[1]));
3936 snprintf(g_comment, sizeof(g_comment), "xchg");
3940 assert_operand_cnt(1);
3941 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3942 fprintf(fout, " %s = ~%s;", buf1, buf1);
3946 assert_operand_cnt(2);
3947 fprintf(fout, " %s = (s32)%s >> 31;",
3948 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3949 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3950 strcpy(g_comment, "cdq");
3954 assert_operand_cnt(3);
3955 if (po->flags & OPF_REP) {
3960 fprintf(fout, " eax = %sesi; esi %c= %d;",
3961 lmod_cast_u_ptr(po, po->operand[0].lmod),
3962 (po->flags & OPF_DF) ? '-' : '+',
3963 lmod_bytes(po, po->operand[0].lmod));
3964 strcpy(g_comment, "lods");
3969 assert_operand_cnt(3);
3970 if (po->flags & OPF_REP) {
3971 fprintf(fout, " for (; ecx != 0; ecx--, edi %c= %d)\n",
3972 (po->flags & OPF_DF) ? '-' : '+',
3973 lmod_bytes(po, po->operand[0].lmod));
3974 fprintf(fout, " %sedi = eax;",
3975 lmod_cast_u_ptr(po, po->operand[0].lmod));
3976 strcpy(g_comment, "rep stos");
3979 fprintf(fout, " %sedi = eax; edi %c= %d;",
3980 lmod_cast_u_ptr(po, po->operand[0].lmod),
3981 (po->flags & OPF_DF) ? '-' : '+',
3982 lmod_bytes(po, po->operand[0].lmod));
3983 strcpy(g_comment, "stos");
3988 assert_operand_cnt(3);
3989 j = lmod_bytes(po, po->operand[0].lmod);
3990 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
3991 l = (po->flags & OPF_DF) ? '-' : '+';
3992 if (po->flags & OPF_REP) {
3994 " for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n",
3997 " %sedi = %sesi;", buf1, buf1);
3998 strcpy(g_comment, "rep movs");
4001 fprintf(fout, " %sedi = %sesi; edi %c= %d; esi %c= %d;",
4002 buf1, buf1, l, j, l, j);
4003 strcpy(g_comment, "movs");
4008 // repe ~ repeat while ZF=1
4009 assert_operand_cnt(3);
4010 j = lmod_bytes(po, po->operand[0].lmod);
4011 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4012 l = (po->flags & OPF_DF) ? '-' : '+';
4013 if (po->flags & OPF_REP) {
4015 " for (; ecx != 0; ecx--) {\n");
4016 if (pfomask & (1 << PFO_C)) {
4019 " cond_c = %sesi < %sedi;\n", buf1, buf1);
4020 pfomask &= ~(1 << PFO_C);
4023 " cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n",
4024 buf1, buf1, l, j, l, j);
4026 " if (cond_z %s 0) break;\n",
4027 (po->flags & OPF_REPZ) ? "==" : "!=");
4030 snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
4031 (po->flags & OPF_REPZ) ? "e" : "ne");
4035 " cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;",
4036 buf1, buf1, l, j, l, j);
4037 strcpy(g_comment, "cmps");
4039 pfomask &= ~(1 << PFO_Z);
4040 last_arith_dst = NULL;
4041 delayed_flag_op = NULL;
4045 // only does ZF (for now)
4046 // repe ~ repeat while ZF=1
4047 assert_operand_cnt(3);
4048 j = lmod_bytes(po, po->operand[0].lmod);
4049 l = (po->flags & OPF_DF) ? '-' : '+';
4050 if (po->flags & OPF_REP) {
4052 " for (; ecx != 0; ecx--) {\n");
4054 " cond_z = (%seax == %sedi); edi %c= %d;\n",
4055 lmod_cast_u(po, po->operand[0].lmod),
4056 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4058 " if (cond_z %s 0) break;\n",
4059 (po->flags & OPF_REPZ) ? "==" : "!=");
4062 snprintf(g_comment, sizeof(g_comment), "rep%s scas",
4063 (po->flags & OPF_REPZ) ? "e" : "ne");
4066 fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;",
4067 lmod_cast_u(po, po->operand[0].lmod),
4068 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4069 strcpy(g_comment, "scas");
4071 pfomask &= ~(1 << PFO_Z);
4072 last_arith_dst = NULL;
4073 delayed_flag_op = NULL;
4076 // arithmetic w/flags
4079 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4082 assert_operand_cnt(2);
4083 fprintf(fout, " %s %s= %s;",
4084 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4086 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4087 output_std_flags(fout, po, &pfomask, buf1);
4088 last_arith_dst = &po->operand[0];
4089 delayed_flag_op = NULL;
4094 assert_operand_cnt(2);
4095 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4096 if (pfomask & (1 << PFO_C)) {
4097 if (po->operand[1].type == OPT_CONST) {
4098 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4099 j = po->operand[1].val;
4102 if (po->op == OP_SHL)
4106 fprintf(fout, " cond_c = (%s >> %d) & 1;\n",
4110 ferr(po, "zero shift?\n");
4114 pfomask &= ~(1 << PFO_C);
4116 fprintf(fout, " %s %s= %s;", buf1, op_to_c(po),
4117 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4118 output_std_flags(fout, po, &pfomask, buf1);
4119 last_arith_dst = &po->operand[0];
4120 delayed_flag_op = NULL;
4124 assert_operand_cnt(2);
4125 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4126 fprintf(fout, " %s = %s%s >> %s;", buf1,
4127 lmod_cast_s(po, po->operand[0].lmod), buf1,
4128 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4129 output_std_flags(fout, po, &pfomask, buf1);
4130 last_arith_dst = &po->operand[0];
4131 delayed_flag_op = NULL;
4136 assert_operand_cnt(2);
4137 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4138 if (po->operand[1].type == OPT_CONST) {
4139 j = po->operand[1].val;
4140 j %= lmod_bytes(po, po->operand[0].lmod) * 8;
4141 fprintf(fout, po->op == OP_ROL ?
4142 " %s = (%s << %d) | (%s >> %d);" :
4143 " %s = (%s >> %d) | (%s << %d);",
4144 buf1, buf1, j, buf1,
4145 lmod_bytes(po, po->operand[0].lmod) * 8 - j);
4149 output_std_flags(fout, po, &pfomask, buf1);
4150 last_arith_dst = &po->operand[0];
4151 delayed_flag_op = NULL;
4156 assert_operand_cnt(2);
4157 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4158 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4159 if (po->operand[1].type == OPT_CONST) {
4160 j = po->operand[1].val % l;
4162 ferr(po, "zero rotate\n");
4163 fprintf(fout, " tmp = (%s >> %d) & 1;\n",
4164 buf1, (po->op == OP_RCL) ? (l - j) : (j - 1));
4165 if (po->op == OP_RCL) {
4167 " %s = (%s << %d) | (cond_c << %d)",
4168 buf1, buf1, j, j - 1);
4170 fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j);
4174 " %s = (%s >> %d) | (cond_c << %d)",
4175 buf1, buf1, j, l - j);
4177 fprintf(fout, " | (%s << %d)", buf1, l + 1 - j);
4179 fprintf(fout, ";\n");
4180 fprintf(fout, " cond_c = tmp;");
4184 strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr");
4185 output_std_flags(fout, po, &pfomask, buf1);
4186 last_arith_dst = &po->operand[0];
4187 delayed_flag_op = NULL;
4191 assert_operand_cnt(2);
4192 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4193 if (IS(opr_name(po, 0), opr_name(po, 1))) {
4194 // special case for XOR
4195 if (pfomask & (1 << PFO_BE)) { // weird, but it happens..
4196 fprintf(fout, " cond_be = 1;\n");
4197 pfomask &= ~(1 << PFO_BE);
4199 fprintf(fout, " %s = 0;",
4200 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4201 last_arith_dst = &po->operand[0];
4202 delayed_flag_op = NULL;
4208 assert_operand_cnt(2);
4209 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4210 if (pfomask & (1 << PFO_C)) {
4211 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4212 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4213 if (po->operand[0].lmod == OPLM_DWORD) {
4214 fprintf(fout, " tmp64 = (u64)%s + %s;\n", buf1, buf2);
4215 fprintf(fout, " cond_c = tmp64 >> 32;\n");
4216 fprintf(fout, " %s = (u32)tmp64;",
4217 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4218 strcat(g_comment, "add64");
4221 fprintf(fout, " cond_c = ((u32)%s + %s) >> %d;\n",
4222 buf1, buf2, lmod_bytes(po, po->operand[0].lmod) * 8);
4223 fprintf(fout, " %s += %s;",
4224 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4227 pfomask &= ~(1 << PFO_C);
4228 output_std_flags(fout, po, &pfomask, buf1);
4229 last_arith_dst = &po->operand[0];
4230 delayed_flag_op = NULL;
4236 assert_operand_cnt(2);
4237 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4238 if (pfomask & (1 << PFO_C)) {
4239 fprintf(fout, " cond_c = %s < %s;\n",
4240 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]),
4241 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4242 pfomask &= ~(1 << PFO_C);
4248 assert_operand_cnt(2);
4249 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4250 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4251 if (po->op == OP_SBB
4252 && IS(po->operand[0].name, po->operand[1].name))
4254 // avoid use of unitialized var
4255 fprintf(fout, " %s = -cond_c;", buf1);
4256 // carry remains what it was
4257 pfomask &= ~(1 << PFO_C);
4260 fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
4261 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4263 output_std_flags(fout, po, &pfomask, buf1);
4264 last_arith_dst = &po->operand[0];
4265 delayed_flag_op = NULL;
4269 assert_operand_cnt(2);
4270 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4271 fprintf(fout, " %s = %s ? __builtin_ffs(%s) - 1 : 0;",
4272 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4274 output_std_flags(fout, po, &pfomask, buf1);
4275 last_arith_dst = &po->operand[0];
4276 delayed_flag_op = NULL;
4277 strcat(g_comment, "bsf");
4282 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4283 if (po->operand[0].type == OPT_REG) {
4284 strcpy(buf2, po->op == OP_INC ? "++" : "--");
4285 fprintf(fout, " %s%s;", buf1, buf2);
4288 strcpy(buf2, po->op == OP_INC ? "+" : "-");
4289 fprintf(fout, " %s %s= 1;", buf1, buf2);
4291 output_std_flags(fout, po, &pfomask, buf1);
4292 last_arith_dst = &po->operand[0];
4293 delayed_flag_op = NULL;
4297 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4298 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
4299 fprintf(fout, " %s = -%s%s;", buf1,
4300 lmod_cast_s(po, po->operand[0].lmod), buf2);
4301 last_arith_dst = &po->operand[0];
4302 delayed_flag_op = NULL;
4303 if (pfomask & (1 << PFO_C)) {
4304 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
4305 pfomask &= ~(1 << PFO_C);
4310 if (po->operand_cnt == 2) {
4311 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4314 if (po->operand_cnt == 3)
4315 ferr(po, "TODO imul3\n");
4318 assert_operand_cnt(1);
4319 switch (po->operand[0].lmod) {
4321 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
4322 fprintf(fout, " tmp64 = %seax * %s%s;\n", buf1, buf1,
4323 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
4324 fprintf(fout, " edx = tmp64 >> 32;\n");
4325 fprintf(fout, " eax = tmp64;");
4328 strcpy(buf1, po->op == OP_IMUL ? "(s16)(s8)" : "(u16)(u8)");
4329 fprintf(fout, " LOWORD(eax) = %seax * %s;", buf1,
4330 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0],
4334 ferr(po, "TODO: unhandled mul type\n");
4337 last_arith_dst = NULL;
4338 delayed_flag_op = NULL;
4343 assert_operand_cnt(1);
4344 if (po->operand[0].lmod != OPLM_DWORD)
4345 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
4347 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4348 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
4349 po->op == OP_IDIV));
4350 switch (po->operand[0].lmod) {
4352 if (po->flags & OPF_32BIT)
4353 snprintf(buf3, sizeof(buf3), "%seax", buf2);
4355 fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n");
4356 snprintf(buf3, sizeof(buf3), "%stmp64",
4357 (po->op == OP_IDIV) ? "(s64)" : "");
4359 if (po->operand[0].type == OPT_REG
4360 && po->operand[0].reg == xDX)
4362 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4363 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4366 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4367 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4371 ferr(po, "unhandled division type\n");
4373 last_arith_dst = NULL;
4374 delayed_flag_op = NULL;
4379 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4381 for (j = 0; j < 8; j++) {
4382 if (pfomask & (1 << j)) {
4383 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
4384 fprintf(fout, " cond_%s = %s;",
4385 parsed_flag_op_names[j], buf1);
4392 last_arith_dst = NULL;
4393 delayed_flag_op = po;
4397 // SETcc - should already be handled
4400 // note: we reuse OP_Jcc for SETcc, only flags differ
4402 fprintf(fout, "\n goto %s;", po->operand[0].name);
4406 fprintf(fout, " if (ecx == 0)\n");
4407 fprintf(fout, " goto %s;", po->operand[0].name);
4408 strcat(g_comment, "jecxz");
4412 assert_operand_cnt(1);
4413 last_arith_dst = NULL;
4414 delayed_flag_op = NULL;
4416 if (po->operand[0].type == OPT_REGMEM) {
4417 ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
4420 ferr(po, "parse failure for jmp '%s'\n",
4421 po->operand[0].name);
4422 fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
4425 else if (po->operand[0].type != OPT_LABEL)
4426 ferr(po, "unhandled jmp type\n");
4428 fprintf(fout, " goto %s;", po->operand[0].name);
4432 assert_operand_cnt(1);
4434 my_assert_not(pp, NULL);
4437 if (po->flags & OPF_CC) {
4438 // we treat conditional branch to another func
4439 // (yes such code exists..) as conditional tailcall
4441 fprintf(fout, " {\n");
4444 if (pp->is_fptr && !pp->is_arg) {
4445 fprintf(fout, "%s%s = %s;\n", buf3, pp->name,
4446 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
4448 if (pp->is_unresolved)
4449 fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
4450 buf3, asmfn, po->asmln, pp->name);
4453 fprintf(fout, "%s", buf3);
4454 if (strstr(pp->ret_type.name, "int64")) {
4455 if (po->flags & OPF_TAIL)
4456 ferr(po, "int64 and tail?\n");
4457 fprintf(fout, "tmp64 = ");
4459 else if (!IS(pp->ret_type.name, "void")) {
4460 if (po->flags & OPF_TAIL) {
4461 if (!IS(g_func_pp->ret_type.name, "void")) {
4462 fprintf(fout, "return ");
4463 if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
4464 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
4467 else if (regmask & (1 << xAX)) {
4468 fprintf(fout, "eax = ");
4469 if (pp->ret_type.is_ptr)
4470 fprintf(fout, "(u32)");
4474 if (pp->name[0] == 0)
4475 ferr(po, "missing pp->name\n");
4476 fprintf(fout, "%s%s(", pp->name,
4477 pp->has_structarg ? "_sa" : "");
4479 if (po->flags & OPF_ATAIL) {
4480 if (pp->argc_stack != g_func_pp->argc_stack
4481 || (pp->argc_stack > 0
4482 && pp->is_stdcall != g_func_pp->is_stdcall))
4483 ferr(po, "incompatible tailcall\n");
4484 if (g_func_pp->has_retreg)
4485 ferr(po, "TODO: retreg+tailcall\n");
4487 for (arg = j = 0; arg < pp->argc; arg++) {
4489 fprintf(fout, ", ");
4492 if (pp->arg[arg].type.is_ptr)
4493 snprintf(cast, sizeof(cast), "(%s)",
4494 pp->arg[arg].type.name);
4496 if (pp->arg[arg].reg != NULL) {
4497 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
4501 for (; j < g_func_pp->argc; j++)
4502 if (g_func_pp->arg[j].reg == NULL)
4504 fprintf(fout, "%sa%d", cast, j + 1);
4509 for (arg = 0; arg < pp->argc; arg++) {
4511 fprintf(fout, ", ");
4514 if (pp->arg[arg].type.is_ptr)
4515 snprintf(cast, sizeof(cast), "(%s)",
4516 pp->arg[arg].type.name);
4518 if (pp->arg[arg].reg != NULL) {
4519 if (pp->arg[arg].type.is_retreg)
4520 fprintf(fout, "&%s", pp->arg[arg].reg);
4522 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
4527 tmp_op = pp->arg[arg].datap;
4529 ferr(po, "parsed_op missing for arg%d\n", arg);
4530 if (tmp_op->argnum != 0) {
4531 fprintf(fout, "%ss_a%d", cast, tmp_op->argnum);
4535 out_src_opr(buf1, sizeof(buf1),
4536 tmp_op, &tmp_op->operand[0], cast, 0));
4540 fprintf(fout, ");");
4542 if (strstr(pp->ret_type.name, "int64")) {
4543 fprintf(fout, "\n");
4544 fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3);
4545 fprintf(fout, "%seax = tmp64;", buf3);
4548 if (pp->is_unresolved) {
4549 snprintf(buf2, sizeof(buf2), " unresolved %dreg",
4551 strcat(g_comment, buf2);
4554 if (po->flags & OPF_TAIL) {
4556 if (i == opcnt - 1 || pp->is_noreturn)
4558 else if (IS(pp->ret_type.name, "void"))
4560 else if (IS(g_func_pp->ret_type.name, "void"))
4562 // else already handled as 'return f()'
4565 if (!IS(g_func_pp->ret_type.name, "void")) {
4566 ferr(po, "int func -> void func tailcall?\n");
4569 fprintf(fout, "\n%sreturn;", buf3);
4570 strcat(g_comment, " ^ tailcall");
4574 strcat(g_comment, " tailcall");
4576 if (pp->is_noreturn)
4577 strcat(g_comment, " noreturn");
4578 if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0)
4579 strcat(g_comment, " argframe");
4580 if (po->flags & OPF_CC)
4581 strcat(g_comment, " cond");
4583 if (po->flags & OPF_CC)
4584 fprintf(fout, "\n }");
4586 delayed_flag_op = NULL;
4587 last_arith_dst = NULL;
4591 if (g_func_pp->is_vararg)
4592 fprintf(fout, " va_end(ap);\n");
4593 if (g_func_pp->has_retreg) {
4594 for (arg = 0; arg < g_func_pp->argc; arg++)
4595 if (g_func_pp->arg[arg].type.is_retreg)
4596 fprintf(fout, " *r_%s = %s;\n",
4597 g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg);
4600 if (IS(g_func_pp->ret_type.name, "void")) {
4601 if (i != opcnt - 1 || label_pending)
4602 fprintf(fout, " return;");
4604 else if (g_func_pp->ret_type.is_ptr) {
4605 fprintf(fout, " return (%s)eax;",
4606 g_func_pp->ret_type.name);
4608 else if (IS(g_func_pp->ret_type.name, "__int64"))
4609 fprintf(fout, " return ((u64)edx << 32) | eax;");
4611 fprintf(fout, " return eax;");
4613 last_arith_dst = NULL;
4614 delayed_flag_op = NULL;
4618 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4619 if (po->argnum != 0) {
4620 // special case - saved func arg
4621 fprintf(fout, " s_a%d = %s;", po->argnum, buf1);
4624 else if (po->flags & OPF_RSAVE) {
4625 fprintf(fout, " s_%s = %s;", buf1, buf1);
4628 else if (g_func_pp->is_userstack) {
4629 fprintf(fout, " *(--esp) = %s;", buf1);
4632 if (!(g_ida_func_attr & IDAFA_NORETURN))
4633 ferr(po, "stray push encountered\n");
4638 if (po->flags & OPF_RSAVE) {
4639 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4640 fprintf(fout, " %s = s_%s;", buf1, buf1);
4643 else if (po->datap != NULL) {
4646 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4647 fprintf(fout, " %s = %s;", buf1,
4648 out_src_opr(buf2, sizeof(buf2),
4649 tmp_op, &tmp_op->operand[0],
4650 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
4653 else if (g_func_pp->is_userstack) {
4654 fprintf(fout, " %s = *esp++;",
4655 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4659 ferr(po, "stray pop encountered\n");
4668 ferr(po, "unhandled op type %d, flags %x\n",
4673 if (g_comment[0] != 0) {
4674 char *p = g_comment;
4675 while (my_isblank(*p))
4677 fprintf(fout, " // %s", p);
4682 fprintf(fout, "\n");
4684 // some sanity checking
4685 if (po->flags & OPF_REP) {
4686 if (po->op != OP_STOS && po->op != OP_MOVS
4687 && po->op != OP_CMPS && po->op != OP_SCAS)
4688 ferr(po, "unexpected rep\n");
4689 if (!(po->flags & (OPF_REPZ|OPF_REPNZ))
4690 && (po->op == OP_CMPS || po->op == OP_SCAS))
4691 ferr(po, "cmps/scas with plain rep\n");
4693 if ((po->flags & (OPF_REPZ|OPF_REPNZ))
4694 && po->op != OP_CMPS && po->op != OP_SCAS)
4695 ferr(po, "unexpected repz/repnz\n");
4698 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
4700 // see is delayed flag stuff is still valid
4701 if (delayed_flag_op != NULL && delayed_flag_op != po) {
4702 if (is_any_opr_modified(delayed_flag_op, po, 0))
4703 delayed_flag_op = NULL;
4706 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
4707 if (is_opr_modified(last_arith_dst, po))
4708 last_arith_dst = NULL;
4714 if (g_stack_fsz && !g_stack_frame_used)
4715 fprintf(fout, " (void)sf;\n");
4717 fprintf(fout, "}\n\n");
4720 for (i = 0; i < opcnt; i++) {
4721 struct label_ref *lr, *lr_del;
4723 lr = g_label_refs[i].next;
4724 while (lr != NULL) {
4729 g_label_refs[i].i = -1;
4730 g_label_refs[i].next = NULL;
4732 if (ops[i].op == OP_CALL) {
4734 proto_release(ops[i].pp);
4740 static void set_label(int i, const char *name)
4746 p = strchr(name, ':');
4750 if (len > sizeof(g_labels[0]) - 1)
4751 aerr("label '%s' too long: %d\n", name, len);
4752 if (g_labels[i][0] != 0 && !IS_START(g_labels[i], "algn_"))
4753 aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
4754 memcpy(g_labels[i], name, len);
4755 g_labels[i][len] = 0;
4758 // '=' needs special treatment..
4759 static char *next_word_s(char *w, size_t wsize, char *s)
4765 for (i = 0; i < wsize - 1; i++) {
4766 if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
4772 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
4773 printf("warning: '%s' truncated\n", w);
4784 static struct chunk_item *func_chunks;
4785 static int func_chunk_cnt;
4786 static int func_chunk_alloc;
4788 static void add_func_chunk(FILE *fasm, const char *name, int line)
4790 if (func_chunk_cnt >= func_chunk_alloc) {
4791 func_chunk_alloc *= 2;
4792 func_chunks = realloc(func_chunks,
4793 func_chunk_alloc * sizeof(func_chunks[0]));
4794 my_assert_not(func_chunks, NULL);
4796 func_chunks[func_chunk_cnt].fptr = ftell(fasm);
4797 func_chunks[func_chunk_cnt].name = strdup(name);
4798 func_chunks[func_chunk_cnt].asmln = line;
4802 static int cmp_chunks(const void *p1, const void *p2)
4804 const struct chunk_item *c1 = p1, *c2 = p2;
4805 return strcmp(c1->name, c2->name);
4808 static int cmpstringp(const void *p1, const void *p2)
4810 return strcmp(*(char * const *)p1, *(char * const *)p2);
4813 static void scan_ahead(FILE *fasm)
4823 oldpos = ftell(fasm);
4826 while (fgets(line, sizeof(line), fasm))
4837 // get rid of random tabs
4838 for (i = 0; line[i] != 0; i++)
4839 if (line[i] == '\t')
4842 if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
4845 next_word(words[0], sizeof(words[0]), p);
4846 if (words[0][0] == 0)
4847 aerr("missing name for func chunk?\n");
4849 add_func_chunk(fasm, words[0], asmln);
4851 else if (IS_START(p, "; sctend"))
4857 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
4858 words[wordc][0] = 0;
4859 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
4860 if (*p == 0 || *p == ';') {
4866 if (wordc == 2 && IS(words[1], "ends"))
4870 fseek(fasm, oldpos, SEEK_SET);
4874 int main(int argc, char *argv[])
4876 FILE *fout, *fasm, *frlist;
4877 struct parsed_data *pd = NULL;
4879 char **rlist = NULL;
4881 int rlist_alloc = 0;
4882 int func_chunks_used = 0;
4883 int func_chunks_sorted = 0;
4884 int func_chunk_i = -1;
4885 long func_chunk_ret = 0;
4886 int func_chunk_ret_ln = 0;
4887 int scanned_ahead = 0;
4889 char words[20][256];
4890 enum opr_lenmod lmod;
4891 char *sctproto = NULL;
4893 int pending_endp = 0;
4895 int skip_warned = 0;
4908 for (arg = 1; arg < argc; arg++) {
4909 if (IS(argv[arg], "-v"))
4911 else if (IS(argv[arg], "-rf"))
4912 g_allow_regfunc = 1;
4913 else if (IS(argv[arg], "-m"))
4919 if (argc < arg + 3) {
4920 printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdrf> [rlist]*\n",
4927 asmfn = argv[arg++];
4928 fasm = fopen(asmfn, "r");
4929 my_assert_not(fasm, NULL);
4931 hdrfn = argv[arg++];
4932 g_fhdr = fopen(hdrfn, "r");
4933 my_assert_not(g_fhdr, NULL);
4936 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
4937 my_assert_not(rlist, NULL);
4938 // needs special handling..
4939 rlist[rlist_len++] = "__alloca_probe";
4941 func_chunk_alloc = 32;
4942 func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
4943 my_assert_not(func_chunks, NULL);
4945 memset(words, 0, sizeof(words));
4947 for (; arg < argc; arg++) {
4948 frlist = fopen(argv[arg], "r");
4949 my_assert_not(frlist, NULL);
4951 while (fgets(line, sizeof(line), frlist)) {
4953 if (*p == 0 || *p == ';')
4956 if (IS_START(p, "#if 0")
4957 || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
4961 else if (IS_START(p, "#endif"))
4968 p = next_word(words[0], sizeof(words[0]), p);
4969 if (words[0][0] == 0)
4972 if (rlist_len >= rlist_alloc) {
4973 rlist_alloc = rlist_alloc * 2 + 64;
4974 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
4975 my_assert_not(rlist, NULL);
4977 rlist[rlist_len++] = strdup(words[0]);
4986 qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
4988 fout = fopen(argv[arg_out], "w");
4989 my_assert_not(fout, NULL);
4992 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
4993 my_assert_not(g_eqs, NULL);
4995 for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
4996 g_label_refs[i].i = -1;
4997 g_label_refs[i].next = NULL;
5000 while (fgets(line, sizeof(line), fasm))
5009 // get rid of random tabs
5010 for (i = 0; line[i] != 0; i++)
5011 if (line[i] == '\t')
5016 if (p[2] == '=' && IS_START(p, "; =============== S U B"))
5017 goto do_pending_endp; // eww..
5019 if (p[2] == 'A' && IS_START(p, "; Attributes:"))
5021 static const char *attrs[] = {
5030 // parse IDA's attribute-list comment
5031 g_ida_func_attr = 0;
5034 for (; *p != 0; p = sskip(p)) {
5035 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
5036 if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
5037 g_ida_func_attr |= 1 << i;
5038 p += strlen(attrs[i]);
5042 if (i == ARRAY_SIZE(attrs)) {
5043 anote("unparsed IDA attr: %s\n", p);
5046 if (IS(attrs[i], "fpd=")) {
5047 p = next_word(words[0], sizeof(words[0]), p);
5052 else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
5055 next_word(words[0], sizeof(words[0]), p);
5056 if (words[0][0] == 0)
5057 aerr("missing name for func chunk?\n");
5059 if (!scanned_ahead) {
5060 add_func_chunk(fasm, words[0], asmln);
5061 func_chunks_sorted = 0;
5064 else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
5066 if (func_chunk_i >= 0) {
5067 if (func_chunk_i < func_chunk_cnt
5068 && IS(func_chunks[func_chunk_i].name, g_func))
5070 // move on to next chunk
5071 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
5073 aerr("seek failed for '%s' chunk #%d\n",
5074 g_func, func_chunk_i);
5075 asmln = func_chunks[func_chunk_i].asmln;
5079 if (func_chunk_ret == 0)
5080 aerr("no return from chunk?\n");
5081 fseek(fasm, func_chunk_ret, SEEK_SET);
5082 asmln = func_chunk_ret_ln;
5088 else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
5089 func_chunks_used = 1;
5091 if (IS_START(g_func, "sub_")) {
5092 unsigned long addr = strtoul(p, NULL, 16);
5093 unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
5094 if (addr > f_addr && !scanned_ahead) {
5095 anote("scan_ahead caused by '%s', addr %lx\n",
5099 func_chunks_sorted = 0;
5107 for (i = wordc; i < ARRAY_SIZE(words); i++)
5109 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
5110 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
5111 if (*p == 0 || *p == ';') {
5116 if (*p != 0 && *p != ';')
5117 aerr("too many words\n");
5119 // alow asm patches in comments
5121 if (IS_START(p, "; sctpatch:")) {
5123 if (*p == 0 || *p == ';')
5125 goto parse_words; // lame
5127 if (IS_START(p, "; sctproto:")) {
5128 sctproto = strdup(p + 11);
5130 else if (IS_START(p, "; sctend")) {
5139 awarn("wordc == 0?\n");
5143 // don't care about this:
5144 if (words[0][0] == '.'
5145 || IS(words[0], "include")
5146 || IS(words[0], "assume") || IS(words[1], "segment")
5147 || IS(words[0], "align"))
5153 // do delayed endp processing to collect switch jumptables
5155 if (in_func && !skip_func && !end && wordc >= 2
5156 && ((words[0][0] == 'd' && words[0][2] == 0)
5157 || (words[1][0] == 'd' && words[1][2] == 0)))
5160 if (words[1][0] == 'd' && words[1][2] == 0) {
5162 if (g_func_pd_cnt >= pd_alloc) {
5163 pd_alloc = pd_alloc * 2 + 16;
5164 g_func_pd = realloc(g_func_pd,
5165 sizeof(g_func_pd[0]) * pd_alloc);
5166 my_assert_not(g_func_pd, NULL);
5168 pd = &g_func_pd[g_func_pd_cnt];
5170 memset(pd, 0, sizeof(*pd));
5171 strcpy(pd->label, words[0]);
5172 pd->type = OPT_CONST;
5173 pd->lmod = lmod_from_directive(words[1]);
5179 anote("skipping alignment byte?\n");
5182 lmod = lmod_from_directive(words[0]);
5183 if (lmod != pd->lmod)
5184 aerr("lmod change? %d->%d\n", pd->lmod, lmod);
5187 if (pd->count_alloc < pd->count + wordc) {
5188 pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
5189 pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
5190 my_assert_not(pd->d, NULL);
5192 for (; i < wordc; i++) {
5193 if (IS(words[i], "offset")) {
5194 pd->type = OPT_OFFSET;
5197 p = strchr(words[i], ',');
5200 if (pd->type == OPT_OFFSET)
5201 pd->d[pd->count].u.label = strdup(words[i]);
5203 pd->d[pd->count].u.val = parse_number(words[i]);
5204 pd->d[pd->count].bt_i = -1;
5210 if (in_func && !skip_func)
5211 gen_func(fout, g_fhdr, g_func, pi);
5215 g_ida_func_attr = 0;
5219 func_chunks_used = 0;
5222 memset(&ops, 0, pi * sizeof(ops[0]));
5223 memset(g_labels, 0, pi * sizeof(g_labels[0]));
5227 for (i = 0; i < g_func_pd_cnt; i++) {
5229 if (pd->type == OPT_OFFSET) {
5230 for (j = 0; j < pd->count; j++)
5231 free(pd->d[j].u.label);
5245 if (IS(words[1], "proc")) {
5247 aerr("proc '%s' while in_func '%s'?\n",
5250 if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
5252 strcpy(g_func, words[0]);
5253 set_label(0, words[0]);
5258 if (IS(words[1], "endp"))
5261 aerr("endp '%s' while not in_func?\n", words[0]);
5262 if (!IS(g_func, words[0]))
5263 aerr("endp '%s' while in_func '%s'?\n",
5266 if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1
5267 && ops[0].op == OP_JMP && ops[0].operand[0].had_ds)
5273 if (!skip_func && func_chunks_used) {
5274 // start processing chunks
5275 struct chunk_item *ci, key = { g_func, 0 };
5277 func_chunk_ret = ftell(fasm);
5278 func_chunk_ret_ln = asmln;
5279 if (!func_chunks_sorted) {
5280 qsort(func_chunks, func_chunk_cnt,
5281 sizeof(func_chunks[0]), cmp_chunks);
5282 func_chunks_sorted = 1;
5284 ci = bsearch(&key, func_chunks, func_chunk_cnt,
5285 sizeof(func_chunks[0]), cmp_chunks);
5287 aerr("'%s' needs chunks, but none found\n", g_func);
5288 func_chunk_i = ci - func_chunks;
5289 for (; func_chunk_i > 0; func_chunk_i--)
5290 if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
5293 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
5295 aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
5296 asmln = func_chunks[func_chunk_i].asmln;
5304 if (wordc == 2 && IS(words[1], "ends")) {
5308 goto do_pending_endp;
5312 // scan for next text segment
5313 while (fgets(line, sizeof(line), fasm)) {
5316 if (*p == 0 || *p == ';')
5319 if (strstr(p, "segment para public 'CODE' use32"))
5326 p = strchr(words[0], ':');
5328 set_label(pi, words[0]);
5332 if (!in_func || skip_func) {
5333 if (!skip_warned && !skip_func && g_labels[pi][0] != 0) {
5335 anote("skipping from '%s'\n", g_labels[pi]);
5338 g_labels[pi][0] = 0;
5342 if (wordc > 1 && IS(words[1], "="))
5345 aerr("unhandled equ, wc=%d\n", wordc);
5346 if (g_eqcnt >= eq_alloc) {
5348 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
5349 my_assert_not(g_eqs, NULL);
5352 len = strlen(words[0]);
5353 if (len > sizeof(g_eqs[0].name) - 1)
5354 aerr("equ name too long: %d\n", len);
5355 strcpy(g_eqs[g_eqcnt].name, words[0]);
5357 if (!IS(words[3], "ptr"))
5358 aerr("unhandled equ\n");
5359 if (IS(words[2], "dword"))
5360 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
5361 else if (IS(words[2], "word"))
5362 g_eqs[g_eqcnt].lmod = OPLM_WORD;
5363 else if (IS(words[2], "byte"))
5364 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
5366 aerr("bad lmod: '%s'\n", words[2]);
5368 g_eqs[g_eqcnt].offset = parse_number(words[4]);
5373 if (pi >= ARRAY_SIZE(ops))
5374 aerr("too many ops\n");
5376 parse_op(&ops[pi], words, wordc);
5378 if (sctproto != NULL) {
5379 if (ops[pi].op == OP_CALL || ops[pi].op == OP_JMP)
5380 ops[pi].datap = sctproto;
5393 // vim:ts=2:shiftwidth=2:expandtab