5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
14 #include "my_assert.h"
17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
18 #define IS(w, y) !strcmp(w, y)
19 #define IS_START(w, y) !strncmp(w, y, strlen(y))
21 #include "protoparse.h"
23 static const char *asmfn;
27 #define anote(fmt, ...) \
28 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
29 #define awarn(fmt, ...) \
30 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
31 #define aerr(fmt, ...) do { \
32 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
37 #include "masm_tools.h"
40 OPF_RMD = (1 << 0), /* removed or optimized out */
41 OPF_DATA = (1 << 1), /* data processing - writes to dst opr */
42 OPF_FLAGS = (1 << 2), /* sets flags */
43 OPF_JMP = (1 << 3), /* branch, call */
44 OPF_CJMP = (1 << 4), /* cond. branch (cc or jecxz) */
45 OPF_CC = (1 << 5), /* uses flags */
46 OPF_TAIL = (1 << 6), /* ret or tail call */
47 OPF_RSAVE = (1 << 7), /* push/pop is local reg save/load */
48 OPF_REP = (1 << 8), /* prefixed by rep */
49 OPF_REPZ = (1 << 9), /* rep is repe/repz */
50 OPF_REPNZ = (1 << 10), /* rep is repne/repnz */
51 OPF_FARG = (1 << 11), /* push collected as func arg (no reuse) */
52 OPF_EBP_S = (1 << 12), /* ebp used as scratch here, not BP */
53 OPF_DF = (1 << 13), /* DF flag set */
54 OPF_ATAIL = (1 << 14), /* tail call with reused arg frame */
55 OPF_32BIT = (1 << 15), /* 32bit division */
56 OPF_LOCK = (1 << 16), /* op has lock prefix */
57 OPF_VAPUSH = (1 << 17), /* vararg ptr push (as call arg) */
121 // must be sorted (larger len must be further in enum)
129 #define MAX_OPERANDS 3
133 enum opr_lenmod lmod;
134 unsigned int is_ptr:1; // pointer in C
135 unsigned int is_array:1; // array in C
136 unsigned int type_from_var:1; // .. in header, sometimes wrong
137 unsigned int size_mismatch:1; // type override differs from C
138 unsigned int size_lt:1; // type override is larger than C
139 unsigned int had_ds:1; // had ds: prefix
140 const struct parsed_proto *pp; // for OPT_LABEL
148 struct parsed_opr operand[MAX_OPERANDS];
151 unsigned char pfo_inv;
152 unsigned char operand_cnt;
153 unsigned char p_argnum; // push: altered before call arg #
154 unsigned char p_argpass;// push: arg of host func
155 unsigned char pad[3];
156 int regmask_src; // all referensed regs
158 int pfomask; // flagop: parsed_flag_op that can't be delayed
159 int cc_scratch; // scratch storage during analysis
160 int bt_i; // branch target for branches
161 struct parsed_data *btj;// branch targets for jumptables
162 struct parsed_proto *pp;// parsed_proto for OP_CALL
168 // OP_CALL - parser proto hint (str)
169 // (OPF_CC) - point to one of (OPF_FLAGS) that affects cc op
170 // OP_POP - point to OP_PUSH in push/pop pair
174 enum opr_lenmod lmod;
181 enum opr_lenmod lmod;
195 struct label_ref *next;
199 IDAFA_BP_FRAME = (1 << 0),
200 IDAFA_LIB_FUNC = (1 << 1),
201 IDAFA_STATIC = (1 << 2),
202 IDAFA_NORETURN = (1 << 3),
203 IDAFA_THUNK = (1 << 4),
204 IDAFA_FPD = (1 << 5),
209 static struct parsed_op ops[MAX_OPS];
210 static struct parsed_equ *g_eqs;
212 static char g_labels[MAX_OPS][48];
213 static struct label_ref g_label_refs[MAX_OPS];
214 static const struct parsed_proto *g_func_pp;
215 static struct parsed_data *g_func_pd;
216 static int g_func_pd_cnt;
217 static char g_func[256];
218 static char g_comment[256];
219 static int g_bp_frame;
220 static int g_sp_frame;
221 static int g_stack_frame_used;
222 static int g_stack_fsz;
223 static int g_ida_func_attr;
224 static int g_allow_regfunc;
225 #define ferr(op_, fmt, ...) do { \
226 printf("%s:%d: error: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
227 dump_op(op_), ##__VA_ARGS__); \
231 #define fnote(op_, fmt, ...) \
232 printf("%s:%d: note: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
233 dump_op(op_), ##__VA_ARGS__)
237 const char *regs_r32[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp" };
238 const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
239 const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
240 const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
242 enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
244 // possible basic comparison types (without inversion)
245 enum parsed_flag_op {
249 PFO_BE, // 6 CF=1||ZF=1
253 PFO_LE, // e ZF=1||SF!=OF
256 static const char *parsed_flag_op_names[] = {
257 "o", "c", "z", "be", "s", "p", "l", "le"
260 static int char_array_i(const char *array[], size_t len, const char *s)
264 for (i = 0; i < len; i++)
271 static void printf_number(char *buf, size_t buf_size,
272 unsigned long number)
274 // output in C-friendly form
275 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
278 static int check_segment_prefix(const char *s)
280 if (s[0] == 0 || s[1] != 's' || s[2] != ':')
294 static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
298 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
300 *reg_lmod = OPLM_DWORD;
303 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
305 *reg_lmod = OPLM_WORD;
308 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
310 *reg_lmod = OPLM_BYTE;
313 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
315 *reg_lmod = OPLM_BYTE;
322 static int parse_indmode(char *name, int *regmask, int need_c_cvt)
324 enum opr_lenmod lmod;
337 while (my_isblank(*s))
339 for (; my_issep(*s); d++, s++)
341 while (my_isblank(*s))
345 // skip '?s:' prefixes
346 if (check_segment_prefix(s))
349 s = next_idt(w, sizeof(w), s);
354 reg = parse_reg(&lmod, w);
356 *regmask |= 1 << reg;
360 if ('0' <= w[0] && w[0] <= '9') {
361 number = parse_number(w);
362 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
366 // probably some label/identifier - pass
369 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
373 strcpy(name, cvtbuf);
378 static int is_reg_in_str(const char *s)
382 if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3])))
385 for (i = 0; i < ARRAY_SIZE(regs_r32); i++)
386 if (!strncmp(s, regs_r32[i], 3))
392 static const char *parse_stack_el(const char *name, char *extra_reg,
395 const char *p, *p2, *s;
401 if (g_bp_frame || early_try)
404 if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) {
406 if (extra_reg != NULL) {
407 strncpy(extra_reg, name, 3);
412 if (IS_START(p, "ebp+")) {
416 if (p2 != NULL && is_reg_in_str(p)) {
417 if (extra_reg != NULL) {
418 strncpy(extra_reg, p, p2 - p);
419 extra_reg[p2 - p] = 0;
424 if (!('0' <= *p && *p <= '9'))
431 if (!IS_START(name, "esp+"))
437 if (is_reg_in_str(s)) {
438 if (extra_reg != NULL) {
439 strncpy(extra_reg, s, p - s);
440 extra_reg[p - s] = 0;
445 aerr("%s IDA stackvar not set?\n", __func__);
447 if (!('0' <= *s && *s <= '9')) {
448 aerr("%s IDA stackvar offset not set?\n", __func__);
451 if (s[0] == '0' && s[1] == 'x')
454 if (len < sizeof(buf) - 1) {
455 strncpy(buf, s, len);
457 val = strtol(buf, &endp, 16);
458 if (val == 0 || *endp != 0) {
459 aerr("%s num parse fail for '%s'\n", __func__, buf);
468 if ('0' <= *p && *p <= '9')
474 static int guess_lmod_from_name(struct parsed_opr *opr)
476 if (!strncmp(opr->name, "dword_", 6)) {
477 opr->lmod = OPLM_DWORD;
480 if (!strncmp(opr->name, "word_", 5)) {
481 opr->lmod = OPLM_WORD;
484 if (!strncmp(opr->name, "byte_", 5)) {
485 opr->lmod = OPLM_BYTE;
491 static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
492 const struct parsed_type *c_type)
494 static const char *dword_types[] = {
495 "int", "_DWORD", "UINT_PTR", "DWORD",
496 "WPARAM", "LPARAM", "UINT", "__int32",
497 "LONG", "HIMC", "BOOL", "size_t",
499 static const char *word_types[] = {
500 "uint16_t", "int16_t", "_WORD", "WORD",
501 "unsigned __int16", "__int16",
503 static const char *byte_types[] = {
504 "uint8_t", "int8_t", "char",
505 "unsigned __int8", "__int8", "BYTE", "_BYTE",
507 // structures.. deal the same as with _UNKNOWN for now
513 if (c_type->is_ptr) {
518 n = skip_type_mod(c_type->name);
520 for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
521 if (IS(n, dword_types[i])) {
527 for (i = 0; i < ARRAY_SIZE(word_types); i++) {
528 if (IS(n, word_types[i])) {
534 for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
535 if (IS(n, byte_types[i])) {
544 static char *default_cast_to(char *buf, size_t buf_size,
545 struct parsed_opr *opr)
551 if (opr->pp == NULL || opr->pp->type.name == NULL
554 snprintf(buf, buf_size, "%s", "(void *)");
558 snprintf(buf, buf_size, "(%s)", opr->pp->type.name);
562 static enum opr_type lmod_from_directive(const char *d)
566 else if (IS(d, "dw"))
568 else if (IS(d, "db"))
571 aerr("unhandled directive: '%s'\n", d);
575 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
581 *regmask |= 1 << reg;
584 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
587 static int parse_operand(struct parsed_opr *opr,
588 int *regmask, int *regmask_indirect,
589 char words[16][256], int wordc, int w, unsigned int op_flags)
591 const struct parsed_proto *pp;
592 enum opr_lenmod tmplmod;
593 unsigned long number;
601 aerr("parse_operand w %d, wordc %d\n", w, wordc);
605 for (i = w; i < wordc; i++) {
606 len = strlen(words[i]);
607 if (words[i][len - 1] == ',') {
608 words[i][len - 1] = 0;
614 wordc_in = wordc - w;
616 if ((op_flags & OPF_JMP) && wordc_in > 0
617 && !('0' <= words[w][0] && words[w][0] <= '9'))
619 const char *label = NULL;
621 if (wordc_in == 3 && !strncmp(words[w], "near", 4)
622 && IS(words[w + 1], "ptr"))
623 label = words[w + 2];
624 else if (wordc_in == 2 && IS(words[w], "short"))
625 label = words[w + 1];
626 else if (wordc_in == 1
627 && strchr(words[w], '[') == NULL
628 && parse_reg(&tmplmod, words[w]) < 0)
632 opr->type = OPT_LABEL;
633 ret = check_segment_prefix(label);
636 aerr("fs/gs used\n");
640 strcpy(opr->name, label);
646 if (IS(words[w + 1], "ptr")) {
647 if (IS(words[w], "dword"))
648 opr->lmod = OPLM_DWORD;
649 else if (IS(words[w], "word"))
650 opr->lmod = OPLM_WORD;
651 else if (IS(words[w], "byte"))
652 opr->lmod = OPLM_BYTE;
654 aerr("type parsing failed\n");
656 wordc_in = wordc - w;
661 if (IS(words[w], "offset")) {
662 opr->type = OPT_OFFSET;
663 opr->lmod = OPLM_DWORD;
664 strcpy(opr->name, words[w + 1]);
665 pp = proto_parse(g_fhdr, opr->name, 1);
668 if (IS(words[w], "(offset")) {
669 p = strchr(words[w + 1], ')');
671 aerr("parse of bracketed offset failed\n");
673 opr->type = OPT_OFFSET;
674 strcpy(opr->name, words[w + 1]);
680 aerr("parse_operand 1 word expected\n");
682 ret = check_segment_prefix(words[w]);
685 aerr("fs/gs used\n");
687 memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
689 strcpy(opr->name, words[w]);
691 if (words[w][0] == '[') {
692 opr->type = OPT_REGMEM;
693 ret = sscanf(words[w], "[%[^]]]", opr->name);
695 aerr("[] parse failure\n");
697 parse_indmode(opr->name, regmask_indirect, 1);
698 if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL, 1))
701 struct parsed_equ *eq =
702 equ_find(NULL, parse_stack_el(opr->name, NULL, 1), &i);
704 opr->lmod = eq->lmod;
708 else if (strchr(words[w], '[')) {
710 p = strchr(words[w], '[');
711 opr->type = OPT_REGMEM;
712 parse_indmode(p, regmask_indirect, 0);
713 strncpy(buf, words[w], p - words[w]);
714 buf[p - words[w]] = 0;
715 pp = proto_parse(g_fhdr, buf, 1);
718 else if (('0' <= words[w][0] && words[w][0] <= '9')
719 || words[w][0] == '-')
721 number = parse_number(words[w]);
722 opr->type = OPT_CONST;
724 printf_number(opr->name, sizeof(opr->name), number);
728 ret = parse_reg(&tmplmod, opr->name);
730 setup_reg_opr(opr, ret, tmplmod, regmask);
734 // most likely var in data segment
735 opr->type = OPT_LABEL;
736 pp = proto_parse(g_fhdr, opr->name, 0);
740 if (pp->is_fptr || pp->is_func) {
741 opr->lmod = OPLM_DWORD;
745 tmplmod = OPLM_UNSPEC;
746 if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
747 anote("unhandled C type '%s' for '%s'\n",
748 pp->type.name, opr->name);
750 if (opr->lmod == OPLM_UNSPEC) {
752 opr->type_from_var = 1;
754 else if (opr->lmod != tmplmod) {
755 opr->size_mismatch = 1;
756 if (tmplmod < opr->lmod)
759 opr->is_ptr = pp->type.is_ptr;
761 opr->is_array = pp->type.is_array;
765 if (opr->lmod == OPLM_UNSPEC)
766 guess_lmod_from_name(opr);
770 static const struct {
775 { "repe", OPF_REP|OPF_REPZ },
776 { "repz", OPF_REP|OPF_REPZ },
777 { "repne", OPF_REP|OPF_REPNZ },
778 { "repnz", OPF_REP|OPF_REPNZ },
779 { "lock", OPF_LOCK }, // ignored for now..
782 #define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC)
784 static const struct {
787 unsigned short minopr;
788 unsigned short maxopr;
791 unsigned char pfo_inv;
793 { "nop", OP_NOP, 0, 0, 0 },
794 { "push", OP_PUSH, 1, 1, 0 },
795 { "pop", OP_POP, 1, 1, OPF_DATA },
796 { "leave",OP_LEAVE, 0, 0, OPF_DATA },
797 { "mov" , OP_MOV, 2, 2, OPF_DATA },
798 { "lea", OP_LEA, 2, 2, OPF_DATA },
799 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
800 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
801 { "xchg", OP_XCHG, 2, 2, OPF_DATA },
802 { "not", OP_NOT, 1, 1, OPF_DATA },
803 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
804 { "lodsb",OP_LODS, 0, 0, OPF_DATA },
805 { "lodsw",OP_LODS, 0, 0, OPF_DATA },
806 { "lodsd",OP_LODS, 0, 0, OPF_DATA },
807 { "stosb",OP_STOS, 0, 0, OPF_DATA },
808 { "stosw",OP_STOS, 0, 0, OPF_DATA },
809 { "stosd",OP_STOS, 0, 0, OPF_DATA },
810 { "movsb",OP_MOVS, 0, 0, OPF_DATA },
811 { "movsw",OP_MOVS, 0, 0, OPF_DATA },
812 { "movsd",OP_MOVS, 0, 0, OPF_DATA },
813 { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
814 { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
815 { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
816 { "scasb",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
817 { "scasw",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
818 { "scasd",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
819 { "std", OP_STD, 0, 0, OPF_DATA }, // special flag
820 { "cld", OP_CLD, 0, 0, OPF_DATA },
821 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
822 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
823 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
824 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
825 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
826 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
827 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
828 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
829 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
830 { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS },
831 { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS },
832 { "rcl", OP_RCL, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
833 { "rcr", OP_RCR, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
834 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
835 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
836 { "bsf", OP_BSF, 2, 2, OPF_DATA|OPF_FLAGS },
837 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
838 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
839 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
840 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
841 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
842 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
843 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
844 { "test", OP_TEST, 2, 2, OPF_FLAGS },
845 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
846 { "retn", OP_RET, 0, 1, OPF_TAIL },
847 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
848 { "jmp", OP_JMP, 1, 1, OPF_JMP },
849 { "jecxz",OP_JECXZ, 1, 1, OPF_JMP|OPF_CJMP },
850 { "jo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 0 }, // 70 OF=1
851 { "jno", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 1 }, // 71 OF=0
852 { "jc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 CF=1
853 { "jb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72
854 { "jnc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 CF=0
855 { "jnb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
856 { "jae", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
857 { "jz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74 ZF=1
858 { "je", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74
859 { "jnz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75 ZF=0
860 { "jne", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75
861 { "jbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 CF=1||ZF=1
862 { "jna", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76
863 { "ja", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 CF=0&&ZF=0
864 { "jnbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77
865 { "js", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 0 }, // 78 SF=1
866 { "jns", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 1 }, // 79 SF=0
867 { "jp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a PF=1
868 { "jpe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a
869 { "jnp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b PF=0
870 { "jpo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b
871 { "jl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c SF!=OF
872 { "jnge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c
873 { "jge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d SF=OF
874 { "jnl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d
875 { "jle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e ZF=1||SF!=OF
876 { "jng", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e
877 { "jg", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f ZF=0&&SF=OF
878 { "jnle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f
879 { "seto", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 0 },
880 { "setno", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 1 },
881 { "setc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
882 { "setb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
883 { "setnc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
884 { "setae", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
885 { "setnb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
886 { "setz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
887 { "sete", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
888 { "setnz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
889 { "setne", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
890 { "setbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
891 { "setna", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
892 { "seta", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
893 { "setnbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
894 { "sets", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 0 },
895 { "setns", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 1 },
896 { "setp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
897 { "setpe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
898 { "setnp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
899 { "setpo", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
900 { "setl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
901 { "setnge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
902 { "setge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
903 { "setnl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
904 { "setle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
905 { "setng", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
906 { "setg", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
907 { "setnle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
910 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
912 enum opr_lenmod lmod = OPLM_UNSPEC;
913 int prefix_flags = 0;
921 for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
922 if (IS(words[w], pref_table[i].name)) {
923 prefix_flags = pref_table[i].flags;
930 aerr("lone prefix: '%s'\n", words[0]);
935 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
936 if (IS(words[w], op_table[i].name))
940 if (i == ARRAY_SIZE(op_table))
941 aerr("unhandled op: '%s'\n", words[0]);
944 op->op = op_table[i].op;
945 op->flags = op_table[i].flags | prefix_flags;
946 op->pfo = op_table[i].pfo;
947 op->pfo_inv = op_table[i].pfo_inv;
948 op->regmask_src = op->regmask_dst = 0;
951 for (opr = 0; opr < op_table[i].minopr; opr++) {
952 regmask = regmask_ind = 0;
953 w = parse_operand(&op->operand[opr], ®mask, ®mask_ind,
954 words, wordc, w, op->flags);
956 if (opr == 0 && (op->flags & OPF_DATA))
957 op->regmask_dst = regmask;
958 // for now, mark dst as src too
959 op->regmask_src |= regmask | regmask_ind;
962 for (; w < wordc && opr < op_table[i].maxopr; opr++) {
963 w = parse_operand(&op->operand[opr],
964 &op->regmask_src, &op->regmask_src,
965 words, wordc, w, op->flags);
969 aerr("parse_op %s incomplete: %d/%d\n",
973 op->operand_cnt = opr;
974 if (!strncmp(op_table[i].name, "set", 3))
975 op->operand[0].lmod = OPLM_BYTE;
977 // ops with implicit argumets
981 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
982 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
988 if (op->operand_cnt != 0)
990 if (words[op_w][4] == 'b')
992 else if (words[op_w][4] == 'w')
994 else if (words[op_w][4] == 'd')
997 setup_reg_opr(&op->operand[0], op->op == OP_LODS ? xSI : xDI,
998 lmod, &op->regmask_src);
999 setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src);
1000 op->regmask_dst = op->regmask_src;
1001 setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD,
1002 op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src);
1007 if (op->operand_cnt != 0)
1009 if (words[op_w][4] == 'b')
1011 else if (words[op_w][4] == 'w')
1013 else if (words[op_w][4] == 'd')
1015 op->operand_cnt = 3;
1016 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
1017 setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src);
1018 setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src);
1019 op->regmask_dst = op->regmask_src;
1023 op->regmask_src |= op->regmask_dst;
1024 op->regmask_dst |= op->regmask_src;
1028 op->operand_cnt = 1;
1029 op->regmask_src = 1 << xCX;
1030 op->operand[0].type = OPT_REG;
1031 op->operand[0].reg = xCX;
1032 op->operand[0].lmod = OPLM_DWORD;
1036 if (op->operand_cnt != 1)
1041 op->regmask_dst = (1 << xDX) | (1 << xAX);
1042 op->regmask_src |= (1 << xAX);
1043 if (op->operand[0].lmod == OPLM_UNSPEC)
1044 op->operand[0].lmod = OPLM_DWORD;
1049 // we could set up operands for edx:eax, but there is no real need to
1050 // (see is_opr_modified())
1051 regmask = (1 << xDX) | (1 << xAX);
1052 op->regmask_dst = regmask;
1053 op->regmask_src |= regmask;
1054 if (op->operand[0].lmod == OPLM_UNSPEC)
1055 op->operand[0].lmod = OPLM_DWORD;
1063 if (op->operand[1].lmod == OPLM_UNSPEC)
1064 op->operand[1].lmod = OPLM_BYTE;
1068 if (op->operand[0].lmod == OPLM_UNSPEC
1069 && (op->operand[0].type == OPT_CONST
1070 || op->operand[0].type == OPT_OFFSET
1071 || op->operand[0].type == OPT_LABEL))
1072 op->operand[0].lmod = OPLM_DWORD;
1078 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1079 && op->operand[0].lmod == op->operand[1].lmod
1080 && op->operand[0].reg == op->operand[1].reg
1081 && IS(op->operand[0].name, op->operand[1].name)) // ah, al..
1083 op->flags |= OPF_RMD;
1084 op->regmask_src = op->regmask_dst = 0;
1089 if (op->operand[0].type == OPT_REG
1090 && op->operand[1].type == OPT_REGMEM)
1093 snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
1094 if (IS(buf, op->operand[1].name))
1095 op->flags |= OPF_RMD;
1100 // trashed regs must be explicitly detected later
1101 op->regmask_dst = 0;
1109 static const char *op_name(struct parsed_op *po)
1111 static char buf[16];
1115 if (po->op == OP_JCC || po->op == OP_SCC) {
1117 *p++ = (po->op == OP_JCC) ? 'j' : 's';
1120 strcpy(p, parsed_flag_op_names[po->pfo]);
1124 for (i = 0; i < ARRAY_SIZE(op_table); i++)
1125 if (op_table[i].op == po->op)
1126 return op_table[i].name;
1132 static const char *dump_op(struct parsed_op *po)
1134 static char out[128];
1141 snprintf(out, sizeof(out), "%s", op_name(po));
1142 for (i = 0; i < po->operand_cnt; i++) {
1146 snprintf(p, sizeof(out) - (p - out),
1147 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1148 po->operand[i].name);
1154 static const char *lmod_type_u(struct parsed_op *po,
1155 enum opr_lenmod lmod)
1165 ferr(po, "invalid lmod: %d\n", lmod);
1166 return "(_invalid_)";
1170 static const char *lmod_cast_u(struct parsed_op *po,
1171 enum opr_lenmod lmod)
1181 ferr(po, "invalid lmod: %d\n", lmod);
1182 return "(_invalid_)";
1186 static const char *lmod_cast_u_ptr(struct parsed_op *po,
1187 enum opr_lenmod lmod)
1197 ferr(po, "invalid lmod: %d\n", lmod);
1198 return "(_invalid_)";
1202 static const char *lmod_cast_s(struct parsed_op *po,
1203 enum opr_lenmod lmod)
1213 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1214 return "(_invalid_)";
1218 static const char *lmod_cast(struct parsed_op *po,
1219 enum opr_lenmod lmod, int is_signed)
1222 lmod_cast_s(po, lmod) :
1223 lmod_cast_u(po, lmod);
1226 static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1236 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1241 static const char *opr_name(struct parsed_op *po, int opr_num)
1243 if (opr_num >= po->operand_cnt)
1244 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1245 return po->operand[opr_num].name;
1248 static unsigned int opr_const(struct parsed_op *po, int opr_num)
1250 if (opr_num >= po->operand_cnt)
1251 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1252 if (po->operand[opr_num].type != OPT_CONST)
1253 ferr(po, "opr %d: const expected\n", opr_num);
1254 return po->operand[opr_num].val;
1257 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1259 if ((unsigned int)popr->reg >= MAX_REGS)
1260 ferr(po, "invalid reg: %d\n", popr->reg);
1261 return regs_r32[popr->reg];
1264 // cast1 is the "final" cast
1265 static const char *simplify_cast(const char *cast1, const char *cast2)
1267 static char buf[256];
1273 if (IS(cast1, cast2))
1275 if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
1277 if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
1279 if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
1281 if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
1283 if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1286 snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1290 static const char *simplify_cast_num(const char *cast, unsigned int val)
1292 if (IS(cast, "(u8)") && val < 0x100)
1294 if (IS(cast, "(s8)") && val < 0x80)
1296 if (IS(cast, "(u16)") && val < 0x10000)
1298 if (IS(cast, "(s16)") && val < 0x8000)
1300 if (IS(cast, "(s32)") && val < 0x80000000)
1306 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1315 namelen = strlen(name);
1317 p = strchr(name, '+');
1321 ferr(po, "equ parse failed for '%s'\n", name);
1323 if (IS_START(p, "0x"))
1325 *extra_offs = strtol(p, &endp, 16);
1327 ferr(po, "equ parse failed for '%s'\n", name);
1330 for (i = 0; i < g_eqcnt; i++)
1331 if (strncmp(g_eqs[i].name, name, namelen) == 0
1332 && g_eqs[i].name[namelen] == 0)
1336 ferr(po, "unresolved equ name: '%s'\n", name);
1343 static int is_stack_access(struct parsed_op *po,
1344 const struct parsed_opr *popr)
1346 return (parse_stack_el(popr->name, NULL, 0)
1347 || (g_bp_frame && !(po->flags & OPF_EBP_S)
1348 && IS_START(popr->name, "ebp")));
1351 static void parse_stack_access(struct parsed_op *po,
1352 const char *name, char *ofs_reg, int *offset_out,
1353 int *stack_ra_out, const char **bp_arg_out, int is_lea)
1355 const char *bp_arg = "";
1356 const char *p = NULL;
1357 struct parsed_equ *eq;
1364 if (IS_START(name, "ebp-")
1365 || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1368 if (IS_START(p, "0x"))
1370 offset = strtoul(p, &endp, 16);
1374 ferr(po, "ebp- parse of '%s' failed\n", name);
1377 bp_arg = parse_stack_el(name, ofs_reg, 0);
1378 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1379 eq = equ_find(po, bp_arg, &offset);
1381 ferr(po, "detected but missing eq\n");
1382 offset += eq->offset;
1385 if (!strncmp(name, "ebp", 3))
1388 // yes it sometimes LEAs ra for compares..
1389 if (!is_lea && ofs_reg[0] == 0
1390 && stack_ra <= offset && offset < stack_ra + 4)
1392 ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
1395 *offset_out = offset;
1396 *stack_ra_out = stack_ra;
1398 *bp_arg_out = bp_arg;
1401 static int stack_frame_access(struct parsed_op *po,
1402 struct parsed_opr *popr, char *buf, size_t buf_size,
1403 const char *name, const char *cast, int is_src, int is_lea)
1405 enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1406 const char *prefix = "";
1407 const char *bp_arg = NULL;
1408 char ofs_reg[16] = { 0, };
1409 int i, arg_i, arg_s;
1417 if (po->flags & OPF_EBP_S)
1418 ferr(po, "stack_frame_access while ebp is scratch\n");
1420 parse_stack_access(po, name, ofs_reg, &offset,
1421 &stack_ra, &bp_arg, is_lea);
1423 if (offset > stack_ra)
1425 arg_i = (offset - stack_ra - 4) / 4;
1426 if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
1428 if (g_func_pp->is_vararg
1429 && arg_i == g_func_pp->argc_stack && is_lea)
1431 // should be va_list
1434 snprintf(buf, buf_size, "%sap", cast);
1437 ferr(po, "offset %d (%s,%d) doesn't map to any arg\n",
1438 offset, bp_arg, arg_i);
1440 if (ofs_reg[0] != 0)
1441 ferr(po, "offset reg on arg access?\n");
1443 for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1444 if (g_func_pp->arg[i].reg != NULL)
1450 if (i == g_func_pp->argc)
1451 ferr(po, "arg %d not in prototype?\n", arg_i);
1453 popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
1460 ferr(po, "lea/byte to arg?\n");
1461 if (is_src && (offset & 3) == 0)
1462 snprintf(buf, buf_size, "%sa%d",
1463 simplify_cast(cast, "(u8)"), i + 1);
1465 snprintf(buf, buf_size, "%sBYTE%d(a%d)",
1466 cast, offset & 3, i + 1);
1471 ferr(po, "lea/word to arg?\n");
1476 ferr(po, "problematic arg store\n");
1477 snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
1478 simplify_cast(cast, "*(u16 *)"), i + 1);
1481 ferr(po, "unaligned arg word load\n");
1483 else if (is_src && (offset & 2) == 0)
1484 snprintf(buf, buf_size, "%sa%d",
1485 simplify_cast(cast, "(u16)"), i + 1);
1487 snprintf(buf, buf_size, "%s%sWORD(a%d)",
1488 cast, (offset & 2) ? "HI" : "LO", i + 1);
1500 snprintf(buf, buf_size, "(u32)&a%d + %d",
1503 ferr(po, "unaligned arg store\n");
1505 // mov edx, [ebp+arg_4+2]; movsx ecx, dx
1506 snprintf(buf, buf_size, "%s(a%d >> %d)",
1507 prefix, i + 1, (offset & 3) * 8);
1511 snprintf(buf, buf_size, "%s%sa%d",
1512 prefix, is_lea ? "&" : "", i + 1);
1517 ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
1521 snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
1524 guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
1525 if (tmp_lmod != OPLM_DWORD
1526 && (unaligned || (!is_src && lmod_bytes(po, tmp_lmod)
1527 < lmod_bytes(po, popr->lmod) + (offset & 3))))
1529 ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
1530 i + 1, offset, g_func_pp->arg[i].type.name);
1532 // can't check this because msvc likes to reuse
1533 // arg space for scratch..
1534 //if (popr->is_ptr && popr->lmod != OPLM_DWORD)
1535 // ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
1539 if (g_stack_fsz == 0)
1540 ferr(po, "stack var access without stackframe\n");
1541 g_stack_frame_used = 1;
1543 sf_ofs = g_stack_fsz + offset;
1544 lim = (ofs_reg[0] != 0) ? -4 : 0;
1545 if (offset > 0 || sf_ofs < lim)
1546 ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
1556 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1557 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1561 if ((sf_ofs & 1) || ofs_reg[0] != 0) {
1562 // known unaligned or possibly unaligned
1563 strcat(g_comment, " unaligned");
1565 prefix = "*(u16 *)&";
1566 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1567 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1570 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
1574 if ((sf_ofs & 3) || ofs_reg[0] != 0) {
1575 // known unaligned or possibly unaligned
1576 strcat(g_comment, " unaligned");
1578 prefix = "*(u32 *)&";
1579 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1580 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1583 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
1587 ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
1594 static void check_func_pp(struct parsed_op *po,
1595 const struct parsed_proto *pp, const char *pfx)
1597 enum opr_lenmod tmp_lmod;
1601 if (pp->argc_reg != 0) {
1602 if (/*!g_allow_regfunc &&*/ !pp->is_fastcall) {
1603 pp_print(buf, sizeof(buf), pp);
1604 ferr(po, "%s: unexpected reg arg in icall: %s\n", pfx, buf);
1606 if (pp->argc_stack > 0 && pp->argc_reg != 2)
1607 ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
1608 pfx, pp->argc_reg, pp->argc_stack);
1611 // fptrs must use 32bit args, callsite might have no information and
1612 // lack a cast to smaller types, which results in incorrectly masked
1613 // args passed (callee may assume masked args, it does on ARM)
1614 if (!pp->is_oslib) {
1615 for (i = 0; i < pp->argc; i++) {
1616 ret = guess_lmod_from_c_type(&tmp_lmod, &pp->arg[i].type);
1617 if (ret && tmp_lmod != OPLM_DWORD)
1618 ferr(po, "reference to %s with arg%d '%s'\n", pp->name,
1619 i + 1, pp->arg[i].type.name);
1624 static const char *check_label_read_ref(struct parsed_op *po,
1627 const struct parsed_proto *pp;
1629 pp = proto_parse(g_fhdr, name, 0);
1631 ferr(po, "proto_parse failed for ref '%s'\n", name);
1634 check_func_pp(po, pp, "ref");
1639 static char *out_src_opr(char *buf, size_t buf_size,
1640 struct parsed_op *po, struct parsed_opr *popr, const char *cast,
1643 char tmp1[256], tmp2[256];
1652 switch (popr->type) {
1655 ferr(po, "lea from reg?\n");
1657 switch (popr->lmod) {
1659 snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
1662 snprintf(buf, buf_size, "%s%s",
1663 simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
1666 if (popr->name[1] == 'h') // XXX..
1667 snprintf(buf, buf_size, "%s(%s >> 8)",
1668 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1670 snprintf(buf, buf_size, "%s%s",
1671 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1674 ferr(po, "invalid src lmod: %d\n", popr->lmod);
1679 if (is_stack_access(po, popr)) {
1680 stack_frame_access(po, popr, buf, buf_size,
1681 popr->name, cast, 1, is_lea);
1685 strcpy(expr, popr->name);
1686 if (strchr(expr, '[')) {
1687 // special case: '[' can only be left for label[reg] form
1688 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
1690 ferr(po, "parse failure for '%s'\n", expr);
1691 if (tmp1[0] == '(') {
1692 // (off_4FFF50+3)[eax]
1693 p = strchr(tmp1 + 1, ')');
1694 if (p == NULL || p[1] != 0)
1695 ferr(po, "parse failure (2) for '%s'\n", expr);
1697 memmove(tmp1, tmp1 + 1, strlen(tmp1));
1699 snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
1702 // XXX: do we need more parsing?
1704 snprintf(buf, buf_size, "%s", expr);
1708 snprintf(buf, buf_size, "%s(%s)",
1709 simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
1713 name = check_label_read_ref(po, popr->name);
1714 if (cast[0] == 0 && popr->is_ptr)
1718 snprintf(buf, buf_size, "(u32)&%s", name);
1719 else if (popr->size_lt)
1720 snprintf(buf, buf_size, "%s%s%s%s", cast,
1721 lmod_cast_u_ptr(po, popr->lmod),
1722 popr->is_array ? "" : "&", name);
1724 snprintf(buf, buf_size, "%s%s%s", cast, name,
1725 popr->is_array ? "[0]" : "");
1729 name = check_label_read_ref(po, popr->name);
1733 ferr(po, "lea an offset?\n");
1734 snprintf(buf, buf_size, "%s&%s", cast, name);
1739 ferr(po, "lea from const?\n");
1741 printf_number(tmp1, sizeof(tmp1), popr->val);
1742 if (popr->val == 0 && strchr(cast, '*'))
1743 snprintf(buf, buf_size, "NULL");
1745 snprintf(buf, buf_size, "%s%s",
1746 simplify_cast_num(cast, popr->val), tmp1);
1750 ferr(po, "invalid src type: %d\n", popr->type);
1756 // note: may set is_ptr (we find that out late for ebp frame..)
1757 static char *out_dst_opr(char *buf, size_t buf_size,
1758 struct parsed_op *po, struct parsed_opr *popr)
1760 switch (popr->type) {
1762 switch (popr->lmod) {
1764 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
1768 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
1772 if (popr->name[1] == 'h') // XXX..
1773 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
1775 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
1778 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
1783 if (is_stack_access(po, popr)) {
1784 stack_frame_access(po, popr, buf, buf_size,
1785 popr->name, "", 0, 0);
1789 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1792 if (popr->size_mismatch)
1793 snprintf(buf, buf_size, "%s%s%s",
1794 lmod_cast_u_ptr(po, popr->lmod),
1795 popr->is_array ? "" : "&", popr->name);
1797 snprintf(buf, buf_size, "%s%s", popr->name,
1798 popr->is_array ? "[0]" : "");
1802 ferr(po, "invalid dst type: %d\n", popr->type);
1808 static char *out_src_opr_u32(char *buf, size_t buf_size,
1809 struct parsed_op *po, struct parsed_opr *popr)
1811 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1814 static void out_test_for_cc(char *buf, size_t buf_size,
1815 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1816 enum opr_lenmod lmod, const char *expr)
1818 const char *cast, *scast;
1820 cast = lmod_cast_u(po, lmod);
1821 scast = lmod_cast_s(po, lmod);
1825 case PFO_BE: // CF=1||ZF=1; CF=0
1826 snprintf(buf, buf_size, "(%s%s %s 0)",
1827 cast, expr, is_inv ? "!=" : "==");
1831 case PFO_L: // SF!=OF; OF=0
1832 snprintf(buf, buf_size, "(%s%s %s 0)",
1833 scast, expr, is_inv ? ">=" : "<");
1836 case PFO_LE: // ZF=1||SF!=OF; OF=0
1837 snprintf(buf, buf_size, "(%s%s %s 0)",
1838 scast, expr, is_inv ? ">" : "<=");
1842 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1846 static void out_cmp_for_cc(char *buf, size_t buf_size,
1847 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1849 const char *cast, *scast, *cast_use;
1850 char buf1[256], buf2[256];
1851 enum opr_lenmod lmod;
1853 if (po->operand[0].lmod != po->operand[1].lmod)
1854 ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
1855 po->operand[0].lmod, po->operand[1].lmod);
1856 lmod = po->operand[0].lmod;
1858 cast = lmod_cast_u(po, lmod);
1859 scast = lmod_cast_s(po, lmod);
1875 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1878 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
1879 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
1883 // note: must be unsigned compare
1884 snprintf(buf, buf_size, "(%s %s %s)",
1885 buf1, is_inv ? ">=" : "<", buf2);
1889 snprintf(buf, buf_size, "(%s %s %s)",
1890 buf1, is_inv ? "!=" : "==", buf2);
1894 // note: must be unsigned compare
1895 snprintf(buf, buf_size, "(%s %s %s)",
1896 buf1, is_inv ? ">" : "<=", buf2);
1899 if (is_inv && lmod == OPLM_BYTE
1900 && po->operand[1].type == OPT_CONST
1901 && po->operand[1].val == 0xff)
1903 snprintf(g_comment, sizeof(g_comment), "if %s", buf);
1904 snprintf(buf, buf_size, "(0)");
1908 // note: must be signed compare
1910 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
1911 scast, buf1, buf2, is_inv ? ">=" : "<");
1915 snprintf(buf, buf_size, "(%s %s %s)",
1916 buf1, is_inv ? ">=" : "<", buf2);
1920 snprintf(buf, buf_size, "(%s %s %s)",
1921 buf1, is_inv ? ">" : "<=", buf2);
1929 static void out_cmp_test(char *buf, size_t buf_size,
1930 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1932 char buf1[256], buf2[256], buf3[256];
1934 if (po->op == OP_TEST) {
1935 if (IS(opr_name(po, 0), opr_name(po, 1))) {
1936 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
1939 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
1940 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
1941 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
1943 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
1944 po->operand[0].lmod, buf3);
1946 else if (po->op == OP_CMP) {
1947 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
1950 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
1953 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
1954 struct parsed_opr *popr2)
1956 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
1957 ferr(po, "missing lmod for both operands\n");
1959 if (popr1->lmod == OPLM_UNSPEC)
1960 popr1->lmod = popr2->lmod;
1961 else if (popr2->lmod == OPLM_UNSPEC)
1962 popr2->lmod = popr1->lmod;
1963 else if (popr1->lmod != popr2->lmod) {
1964 if (popr1->type_from_var) {
1965 popr1->size_mismatch = 1;
1966 if (popr1->lmod < popr2->lmod)
1968 popr1->lmod = popr2->lmod;
1970 else if (popr2->type_from_var) {
1971 popr2->size_mismatch = 1;
1972 if (popr2->lmod < popr1->lmod)
1974 popr2->lmod = popr1->lmod;
1977 ferr(po, "conflicting lmods: %d vs %d\n",
1978 popr1->lmod, popr2->lmod);
1982 static const char *op_to_c(struct parsed_op *po)
2006 ferr(po, "op_to_c was supplied with %d\n", po->op);
2010 static void op_set_clear_flag(struct parsed_op *po,
2011 enum op_flags flag_set, enum op_flags flag_clear)
2013 po->flags |= flag_set;
2014 po->flags &= ~flag_clear;
2017 // last op in stream - unconditional branch or ret
2018 #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
2019 || ((ops[_i].flags & (OPF_JMP|OPF_CJMP|OPF_RMD)) == OPF_JMP \
2020 && ops[_i].op != OP_CALL))
2022 static int scan_for_pop(int i, int opcnt, const char *reg,
2023 int magic, int depth, int *maxdepth, int do_flags)
2025 const struct parsed_proto *pp;
2026 struct parsed_op *po;
2030 for (; i < opcnt; i++) {
2032 if (po->cc_scratch == magic)
2033 break; // already checked
2034 po->cc_scratch = magic;
2036 if (po->flags & OPF_TAIL) {
2037 if (po->op == OP_CALL) {
2038 pp = proto_parse(g_fhdr, po->operand[0].name, 0);
2039 if (pp != NULL && pp->is_noreturn)
2040 // no stack cleanup for noreturn
2043 return -1; // deadend
2046 if ((po->flags & OPF_RMD)
2047 || (po->op == OP_PUSH && po->p_argnum != 0)) // arg push
2050 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2051 if (po->btj != NULL) {
2053 for (j = 0; j < po->btj->count; j++) {
2054 ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic,
2055 depth, maxdepth, do_flags);
2057 return ret; // dead end
2063 ferr(po, "dead branch\n");
2067 if (po->flags & OPF_CJMP) {
2068 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic,
2069 depth, maxdepth, do_flags);
2071 return ret; // dead end
2079 if ((po->op == OP_POP || po->op == OP_PUSH)
2080 && po->operand[0].type == OPT_REG
2081 && IS(po->operand[0].name, reg))
2083 if (po->op == OP_PUSH && !(po->flags & OPF_FARG)) {
2085 if (depth > *maxdepth)
2088 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2090 else if (po->op == OP_POP) {
2093 op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
2098 if (depth < 0) // should not happen
2099 ferr(po, "fail with depth\n");
2101 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2110 // scan for pop starting from 'ret' op (all paths)
2111 static int scan_for_pop_ret(int i, int opcnt, const char *reg,
2117 for (; i < opcnt; i++) {
2118 if (!(ops[i].flags & OPF_TAIL))
2121 for (j = i - 1; j >= 0; j--) {
2122 if (ops[j].flags & OPF_RMD)
2124 if (ops[j].flags & OPF_JMP)
2127 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
2128 && IS(ops[j].operand[0].name, reg))
2131 ops[j].flags |= flag_set;
2135 if (g_labels[j][0] != 0)
2140 return found ? 0 : -1;
2143 static void scan_propagate_df(int i, int opcnt)
2145 struct parsed_op *po = &ops[i];
2148 for (; i < opcnt; i++) {
2150 if (po->flags & OPF_DF)
2151 return; // already resolved
2152 po->flags |= OPF_DF;
2154 if (po->op == OP_CALL)
2155 ferr(po, "call with DF set?\n");
2157 if (po->flags & OPF_JMP) {
2158 if (po->btj != NULL) {
2160 for (j = 0; j < po->btj->count; j++)
2161 scan_propagate_df(po->btj->d[j].bt_i, opcnt);
2166 ferr(po, "dead branch\n");
2170 if (po->flags & OPF_CJMP)
2171 scan_propagate_df(po->bt_i, opcnt);
2177 if (po->flags & OPF_TAIL)
2180 if (po->op == OP_CLD) {
2181 po->flags |= OPF_RMD;
2186 ferr(po, "missing DF clear?\n");
2189 // is operand 'opr' modified by parsed_op 'po'?
2190 static int is_opr_modified(const struct parsed_opr *opr,
2191 const struct parsed_op *po)
2195 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2198 if (opr->type == OPT_REG) {
2199 if (po->op == OP_CALL) {
2200 mask = (1 << xAX) | (1 << xCX) | (1 << xDX);
2201 if ((1 << opr->reg) & mask)
2207 if (po->operand[0].type == OPT_REG) {
2208 if (po->regmask_dst & (1 << opr->reg))
2215 return IS(po->operand[0].name, opr->name);
2218 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
2219 static int is_any_opr_modified(const struct parsed_op *po_test,
2220 const struct parsed_op *po, int c_mode)
2225 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2228 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2231 if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
2234 // in reality, it can wreck any register, but in decompiled C
2235 // version it can only overwrite eax or edx:eax
2236 mask = (1 << xAX) | (1 << xDX);
2240 if (po->op == OP_CALL
2241 && ((po_test->regmask_src | po_test->regmask_dst) & mask))
2244 for (i = 0; i < po_test->operand_cnt; i++)
2245 if (IS(po_test->operand[i].name, po->operand[0].name))
2251 // scan for any po_test operand modification in range given
2252 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
2255 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2258 for (; i < opcnt; i++) {
2259 if (is_any_opr_modified(po_test, &ops[i], c_mode))
2266 // scan for po_test operand[0] modification in range given
2267 static int scan_for_mod_opr0(struct parsed_op *po_test,
2270 for (; i < opcnt; i++) {
2271 if (is_opr_modified(&po_test->operand[0], &ops[i]))
2278 static int scan_for_flag_set(int i, int magic, int *branched,
2279 int *setters, int *setter_cnt)
2281 struct label_ref *lr;
2285 if (ops[i].cc_scratch == magic) {
2286 ferr(&ops[i], "%s looped\n", __func__);
2289 ops[i].cc_scratch = magic;
2291 if (g_labels[i][0] != 0) {
2294 lr = &g_label_refs[i];
2295 for (; lr->next; lr = lr->next) {
2296 ret = scan_for_flag_set(lr->i, magic,
2297 branched, setters, setter_cnt);
2302 if (i > 0 && LAST_OP(i - 1)) {
2306 ret = scan_for_flag_set(lr->i, magic,
2307 branched, setters, setter_cnt);
2313 if (ops[i].flags & OPF_FLAGS) {
2314 setters[*setter_cnt] = i;
2319 if ((ops[i].flags & (OPF_JMP|OPF_CJMP)) == OPF_JMP)
2326 // scan back for cdq, if anything modifies edx, fail
2327 static int scan_for_cdq_edx(int i)
2330 if (g_labels[i][0] != 0) {
2331 if (g_label_refs[i].next != NULL)
2333 if (i > 0 && LAST_OP(i - 1)) {
2334 i = g_label_refs[i].i;
2341 if (ops[i].op == OP_CDQ)
2344 if (ops[i].regmask_dst & (1 << xDX))
2351 static int scan_for_reg_clear(int i, int reg)
2354 if (g_labels[i][0] != 0) {
2355 if (g_label_refs[i].next != NULL)
2357 if (i > 0 && LAST_OP(i - 1)) {
2358 i = g_label_refs[i].i;
2365 if (ops[i].op == OP_XOR
2366 && ops[i].operand[0].lmod == OPLM_DWORD
2367 && ops[i].operand[0].reg == ops[i].operand[1].reg
2368 && ops[i].operand[0].reg == reg)
2371 if (ops[i].regmask_dst & (1 << reg))
2378 // scan for positive, constant esp adjust
2379 static int scan_for_esp_adjust(int i, int opcnt, int *adj,
2382 struct parsed_op *po;
2385 *adj = *multipath = 0;
2387 for (; i < opcnt; i++) {
2390 if (g_labels[i][0] != 0)
2393 if (po->op == OP_ADD && po->operand[0].reg == xSP) {
2394 if (po->operand[1].type != OPT_CONST)
2395 ferr(&ops[i], "non-const esp adjust?\n");
2396 *adj += po->operand[1].val;
2398 ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
2401 else if (po->op == OP_PUSH && !(po->flags & OPF_RMD)) {
2402 //if (first_pop == -1)
2403 // first_pop = -2; // none
2404 *adj -= lmod_bytes(po, po->operand[0].lmod);
2406 else if (po->op == OP_POP && !(po->flags & OPF_RMD)) {
2407 // seems like msvc only uses 'pop ecx' for stack realignment..
2408 if (po->operand[0].type != OPT_REG || po->operand[0].reg != xCX)
2410 if (first_pop == -1 && *adj >= 0)
2412 *adj += lmod_bytes(po, po->operand[0].lmod);
2414 else if (po->flags & (OPF_JMP|OPF_TAIL)) {
2415 if (po->op == OP_JMP && po->btj == NULL) {
2419 if (po->op != OP_CALL)
2421 if (po->operand[0].type != OPT_LABEL)
2423 if (po->pp != NULL && po->pp->is_stdcall)
2428 if (first_pop >= 0) {
2429 // probably 'pop ecx' was used..
2436 static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
2438 struct parsed_op *po;
2442 ferr(ops, "%s: followed bad branch?\n", __func__);
2444 for (; i < opcnt; i++) {
2446 if (po->cc_scratch == magic)
2448 po->cc_scratch = magic;
2451 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2452 if (po->btj != NULL) {
2454 for (j = 0; j < po->btj->count; j++)
2455 scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
2459 scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
2460 if (!(po->flags & OPF_CJMP))
2463 if (po->flags & OPF_TAIL)
2468 static const struct parsed_proto *try_recover_pp(
2469 struct parsed_op *po, const struct parsed_opr *opr, int *search_instead)
2471 const struct parsed_proto *pp = NULL;
2475 // maybe an arg of g_func?
2476 if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
2478 char ofs_reg[16] = { 0, };
2479 int arg, arg_s, arg_i;
2483 parse_stack_access(po, opr->name, ofs_reg,
2484 &offset, &stack_ra, NULL, 0);
2485 if (ofs_reg[0] != 0)
2486 ferr(po, "offset reg on arg access?\n");
2487 if (offset <= stack_ra) {
2488 // search who set the stack var instead
2489 if (search_instead != NULL)
2490 *search_instead = 1;
2494 arg_i = (offset - stack_ra - 4) / 4;
2495 for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
2496 if (g_func_pp->arg[arg].reg != NULL)
2502 if (arg == g_func_pp->argc)
2503 ferr(po, "stack arg %d not in prototype?\n", arg_i);
2505 pp = g_func_pp->arg[arg].fptr;
2507 ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1);
2508 check_func_pp(po, pp, "icall arg");
2510 else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
2512 p = strchr(opr->name + 1, '[');
2513 memcpy(buf, opr->name, p - opr->name);
2514 buf[p - opr->name] = 0;
2515 pp = proto_parse(g_fhdr, buf, 0);
2517 else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
2518 pp = proto_parse(g_fhdr, opr->name, 0);
2520 ferr(po, "proto_parse failed for icall from '%s'\n", opr->name);
2521 check_func_pp(po, pp, "reg-fptr ref");
2527 static void scan_for_call_type(int i, const struct parsed_opr *opr,
2528 int magic, const struct parsed_proto **pp_found, int *multi)
2530 const struct parsed_proto *pp = NULL;
2531 struct parsed_op *po;
2532 struct label_ref *lr;
2534 ops[i].cc_scratch = magic;
2537 if (g_labels[i][0] != 0) {
2538 lr = &g_label_refs[i];
2539 for (; lr != NULL; lr = lr->next)
2540 scan_for_call_type(lr->i, opr, magic, pp_found, multi);
2541 if (i > 0 && LAST_OP(i - 1))
2549 if (ops[i].cc_scratch == magic)
2551 ops[i].cc_scratch = magic;
2553 if (!(ops[i].flags & OPF_DATA))
2555 if (!is_opr_modified(opr, &ops[i]))
2557 if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
2558 // most probably trashed by some processing
2563 opr = &ops[i].operand[1];
2564 if (opr->type != OPT_REG)
2568 po = (i >= 0) ? &ops[i] : ops;
2571 // reached the top - can only be an arg-reg
2572 if (opr->type != OPT_REG)
2575 for (i = 0; i < g_func_pp->argc; i++) {
2576 if (g_func_pp->arg[i].reg == NULL)
2578 if (IS(opr->name, g_func_pp->arg[i].reg))
2581 if (i == g_func_pp->argc)
2583 pp = g_func_pp->arg[i].fptr;
2585 ferr(po, "icall: arg%d (%s) is not a fptr?\n",
2586 i + 1, g_func_pp->arg[i].reg);
2587 check_func_pp(po, pp, "icall reg-arg");
2590 pp = try_recover_pp(po, opr, NULL);
2592 if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
2593 if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
2594 || (*pp_found)->is_stdcall != pp->is_stdcall
2595 || (*pp_found)->is_fptr != pp->is_fptr
2596 || (*pp_found)->argc != pp->argc
2597 || (*pp_found)->argc_reg != pp->argc_reg
2598 || (*pp_found)->argc_stack != pp->argc_stack)
2600 ferr(po, "icall: parsed_proto mismatch\n");
2608 static const struct parsed_proto *resolve_icall(int i, int opcnt,
2611 const struct parsed_proto *pp = NULL;
2612 int search_advice = 0;
2616 switch (ops[i].operand[0].type) {
2620 pp = try_recover_pp(&ops[i], &ops[i].operand[0], &search_advice);
2625 scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
2633 // find an instruction that changed opr before i op
2634 // *op_i must be set to -1
2635 static int resolve_origin(int i, const struct parsed_opr *opr,
2636 int magic, int *op_i)
2638 struct label_ref *lr;
2641 ops[i].cc_scratch = magic;
2644 if (g_labels[i][0] != 0) {
2645 lr = &g_label_refs[i];
2646 for (; lr != NULL; lr = lr->next)
2647 ret |= resolve_origin(lr->i, opr, magic, op_i);
2648 if (i > 0 && LAST_OP(i - 1))
2656 if (ops[i].cc_scratch == magic)
2658 ops[i].cc_scratch = magic;
2660 if (!(ops[i].flags & OPF_DATA))
2662 if (!is_opr_modified(opr, &ops[i]))
2668 // XXX: could check if the other op does the same
2677 static int try_resolve_const(int i, const struct parsed_opr *opr,
2678 int magic, unsigned int *val)
2683 ret = resolve_origin(i, opr, magic, &s_i);
2686 if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST)
2689 *val = ops[i].operand[1].val;
2696 static int collect_call_args_r(struct parsed_op *po, int i,
2697 struct parsed_proto *pp, int *regmask, int *save_arg_vars, int arg,
2698 int magic, int need_op_saving, int may_reuse)
2700 struct parsed_proto *pp_tmp;
2701 struct label_ref *lr;
2702 int need_to_save_current;
2710 ferr(po, "dead label encountered\n");
2714 for (; arg < pp->argc; arg++)
2715 if (pp->arg[arg].reg == NULL)
2717 magic = (magic & 0xffffff) | (arg << 24);
2719 for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
2721 if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
2722 if (ops[j].cc_scratch != magic) {
2723 ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
2727 // ok: have already been here
2730 ops[j].cc_scratch = magic;
2732 if (g_labels[j][0] != 0 && g_label_refs[j].i != -1) {
2733 lr = &g_label_refs[j];
2734 if (lr->next != NULL)
2736 for (; lr->next; lr = lr->next) {
2737 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
2739 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2740 arg, magic, need_op_saving, may_reuse);
2745 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
2747 if (j > 0 && LAST_OP(j - 1)) {
2748 // follow last branch in reverse
2753 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2754 arg, magic, need_op_saving, may_reuse);
2760 if (ops[j].op == OP_CALL)
2762 if (pp->is_unresolved)
2767 ferr(po, "arg collect hit unparsed call '%s'\n",
2768 ops[j].operand[0].name);
2769 if (may_reuse && pp_tmp->argc_stack > 0)
2770 ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
2771 arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
2773 // esp adjust of 0 means we collected it before
2774 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP
2775 && (ops[j].operand[1].type != OPT_CONST
2776 || ops[j].operand[1].val != 0))
2778 if (pp->is_unresolved)
2781 ferr(po, "arg collect %d/%d hit esp adjust of %d\n",
2782 arg, pp->argc, ops[j].operand[1].val);
2784 else if (ops[j].op == OP_POP) {
2785 if (pp->is_unresolved)
2788 ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc);
2790 else if (ops[j].flags & OPF_CJMP)
2792 if (pp->is_unresolved)
2797 else if (ops[j].op == OP_PUSH && !(ops[j].flags & OPF_FARG))
2799 if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
2802 pp->arg[arg].datap = &ops[j];
2803 need_to_save_current = 0;
2806 if (ops[j].operand[0].type == OPT_REG)
2807 reg = ops[j].operand[0].reg;
2809 if (!need_op_saving) {
2810 ret = scan_for_mod(&ops[j], j + 1, i, 1);
2811 need_to_save_current = (ret >= 0);
2813 if (need_op_saving || need_to_save_current) {
2814 // mark this push as one that needs operand saving
2815 ops[j].flags &= ~OPF_RMD;
2816 if (ops[j].p_argnum == 0) {
2817 ops[j].p_argnum = arg + 1;
2818 save_args |= 1 << arg;
2820 else if (ops[j].p_argnum < arg + 1) {
2821 // XXX: might kill valid var..
2822 //*save_arg_vars &= ~(1 << (ops[j].p_argnum - 1));
2823 ops[j].p_argnum = arg + 1;
2824 save_args |= 1 << arg;
2827 else if (ops[j].p_argnum == 0)
2828 ops[j].flags |= OPF_RMD;
2830 // some PUSHes are reused by different calls on other branches,
2831 // but that can't happen if we didn't branch, so they
2832 // can be removed from future searches (handles nested calls)
2834 ops[j].flags |= OPF_FARG;
2836 ops[j].flags &= ~OPF_RSAVE;
2838 // check for __VALIST
2839 if (!pp->is_unresolved && pp->arg[arg].type.is_va_list) {
2841 ret = resolve_origin(j, &ops[j].operand[0], magic + 1, &k);
2842 if (ret == 1 && k >= 0)
2844 if (ops[k].op == OP_LEA) {
2845 snprintf(buf, sizeof(buf), "arg_%X",
2846 g_func_pp->argc_stack * 4);
2847 if (!g_func_pp->is_vararg
2848 || strstr(ops[k].operand[1].name, buf))
2850 ops[k].flags |= OPF_RMD;
2851 ops[j].flags |= OPF_RMD | OPF_VAPUSH;
2852 save_args &= ~(1 << arg);
2856 ferr(&ops[j], "lea va_list used, but no vararg?\n");
2858 // check for va_list from g_func_pp arg too
2859 else if (ops[k].op == OP_MOV
2860 && is_stack_access(&ops[k], &ops[k].operand[1]))
2862 ret = stack_frame_access(&ops[k], &ops[k].operand[1],
2863 buf, sizeof(buf), ops[k].operand[1].name, "", 1, 0);
2865 ops[k].flags |= OPF_RMD;
2866 ops[j].flags |= OPF_RMD;
2867 ops[j].p_argpass = ret + 1;
2868 save_args &= ~(1 << arg);
2875 *save_arg_vars |= save_args;
2877 // tracking reg usage
2879 *regmask |= 1 << reg;
2882 if (!pp->is_unresolved) {
2884 for (; arg < pp->argc; arg++)
2885 if (pp->arg[arg].reg == NULL)
2888 magic = (magic & 0xffffff) | (arg << 24);
2892 if (arg < pp->argc) {
2893 ferr(po, "arg collect failed for '%s': %d/%d\n",
2894 pp->name, arg, pp->argc);
2901 static int collect_call_args(struct parsed_op *po, int i,
2902 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
2908 ret = collect_call_args_r(po, i, pp, regmask, save_arg_vars,
2913 if (pp->is_unresolved) {
2915 pp->argc_stack += ret;
2916 for (a = 0; a < pp->argc; a++)
2917 if (pp->arg[a].type.name == NULL)
2918 pp->arg[a].type.name = strdup("int");
2924 // early check for tail call or branch back
2925 static int is_like_tailjmp(int j)
2927 if (!(ops[j].flags & OPF_JMP))
2930 if (ops[j].op == OP_JMP && !ops[j].operand[0].had_ds)
2931 // probably local branch back..
2933 if (ops[j].op == OP_CALL)
2934 // probably noreturn call..
2940 static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
2944 for (i = 0; i < pp->argc; i++)
2945 if (pp->arg[i].reg == NULL)
2949 memmove(&pp->arg[i + 1], &pp->arg[i],
2950 sizeof(pp->arg[0]) * pp->argc_stack);
2951 memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
2952 pp->arg[i].reg = strdup(reg);
2953 pp->arg[i].type.name = strdup("int");
2958 static void add_label_ref(struct label_ref *lr, int op_i)
2960 struct label_ref *lr_new;
2967 lr_new = calloc(1, sizeof(*lr_new));
2969 lr_new->next = lr->next;
2973 static void output_std_flags(FILE *fout, struct parsed_op *po,
2974 int *pfomask, const char *dst_opr_text)
2976 if (*pfomask & (1 << PFO_Z)) {
2977 fprintf(fout, "\n cond_z = (%s%s == 0);",
2978 lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
2979 *pfomask &= ~(1 << PFO_Z);
2981 if (*pfomask & (1 << PFO_S)) {
2982 fprintf(fout, "\n cond_s = (%s%s < 0);",
2983 lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
2984 *pfomask &= ~(1 << PFO_S);
2988 static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp,
2991 if (pp->is_fastcall)
2992 fprintf(fout, "__fastcall ");
2993 else if (pp->is_stdcall && pp->argc_reg == 0)
2994 fprintf(fout, "__stdcall ");
2995 if (pp->is_noreturn || is_noreturn)
2996 fprintf(fout, "noreturn ");
2999 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
3001 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
3002 struct parsed_opr *last_arith_dst = NULL;
3003 char buf1[256], buf2[256], buf3[256], cast[64];
3004 const struct parsed_proto *pp_c;
3005 struct parsed_proto *pp, *pp_tmp;
3006 struct parsed_data *pd;
3007 const char *tmpname;
3009 int save_arg_vars = 0;
3011 int need_tmp_var = 0;
3014 int label_pending = 0;
3015 int regmask_save = 0;
3016 int regmask_arg = 0;
3017 int regmask_now = 0;
3018 int regmask_init = 0;
3029 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
3030 g_stack_frame_used = 0;
3032 g_func_pp = proto_parse(fhdr, funcn, 0);
3033 if (g_func_pp == NULL)
3034 ferr(ops, "proto_parse failed for '%s'\n", funcn);
3036 for (i = 0; i < g_func_pp->argc; i++) {
3037 if (g_func_pp->arg[i].reg != NULL) {
3038 reg = char_array_i(regs_r32,
3039 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
3041 ferr(ops, "arg '%s' is not a reg?\n", g_func_pp->arg[i].reg);
3042 regmask_arg |= 1 << reg;
3047 // - handle ebp/esp frame, remove ops related to it
3048 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
3049 && ops[1].op == OP_MOV
3050 && IS(opr_name(&ops[1], 0), "ebp")
3051 && IS(opr_name(&ops[1], 1), "esp"))
3056 ops[0].flags |= OPF_RMD;
3057 ops[1].flags |= OPF_RMD;
3060 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
3061 g_stack_fsz = opr_const(&ops[2], 1);
3062 ops[2].flags |= OPF_RMD;
3066 // another way msvc builds stack frame..
3068 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
3070 ops[i].flags |= OPF_RMD;
3074 // and another way..
3075 if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
3076 && ops[i].operand[1].type == OPT_CONST
3077 && ops[i + 1].op == OP_CALL
3078 && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
3080 g_stack_fsz += ops[i].operand[1].val;
3081 ops[i].flags |= OPF_RMD;
3083 ops[i].flags |= OPF_RMD;
3090 for (; i < opcnt; i++)
3091 if (ops[i].op == OP_RET)
3094 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
3095 if (found && is_like_tailjmp(j))
3100 if ((ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp"))
3101 || ops[j].op == OP_LEAVE)
3103 ops[j].flags |= OPF_RMD;
3105 else if (!(g_ida_func_attr & IDAFA_NORETURN))
3106 ferr(&ops[j], "'pop ebp' expected\n");
3108 if (g_stack_fsz != 0) {
3109 if (ops[j - 1].op == OP_MOV
3110 && IS(opr_name(&ops[j - 1], 0), "esp")
3111 && IS(opr_name(&ops[j - 1], 1), "ebp"))
3113 ops[j - 1].flags |= OPF_RMD;
3115 else if (ops[j].op != OP_LEAVE
3116 && !(g_ida_func_attr & IDAFA_NORETURN))
3118 ferr(&ops[j - 1], "esp restore expected\n");
3121 if (ecx_push && ops[j - 2].op == OP_POP
3122 && IS(opr_name(&ops[j - 2], 0), "ecx"))
3124 ferr(&ops[j - 2], "unexpected ecx pop\n");
3130 } while (i < opcnt);
3133 int ecx_push = 0, esp_sub = 0;
3136 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
3137 ops[i].flags |= OPF_RMD;
3143 for (; i < opcnt; i++) {
3144 if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
3146 if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
3147 && ops[i].operand[1].type == OPT_CONST)
3149 g_stack_fsz = ops[i].operand[1].val;
3150 ops[i].flags |= OPF_RMD;
3157 if (ecx_push || esp_sub)
3163 for (; i < opcnt; i++)
3164 if (ops[i].op == OP_RET)
3167 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
3168 if (found && is_like_tailjmp(j))
3174 for (l = 0; l < ecx_push; l++) {
3175 if (ops[j].op != OP_POP
3176 || !IS(opr_name(&ops[j], 0), "ecx"))
3178 ferr(&ops[j], "'pop ecx' expected\n");
3180 ops[j].flags |= OPF_RMD;
3188 if (ops[j].op != OP_ADD
3189 || !IS(opr_name(&ops[j], 0), "esp")
3190 || ops[j].operand[1].type != OPT_CONST
3191 || ops[j].operand[1].val != g_stack_fsz)
3192 ferr(&ops[j], "'add esp' expected\n");
3193 ops[j].flags |= OPF_RMD;
3199 } while (i < opcnt);
3204 // - parse calls with labels
3205 // - resolve all branches
3206 for (i = 0; i < opcnt; i++)
3212 if (po->flags & OPF_RMD)
3215 if (po->op == OP_CALL) {
3218 if (po->operand[0].type == OPT_LABEL) {
3219 tmpname = opr_name(po, 0);
3220 if (IS_START(tmpname, "loc_"))
3221 ferr(po, "call to loc_*\n");
3222 pp_c = proto_parse(fhdr, tmpname, 0);
3224 ferr(po, "proto_parse failed for call '%s'\n", tmpname);
3226 pp = proto_clone(pp_c);
3227 my_assert_not(pp, NULL);
3229 else if (po->datap != NULL) {
3230 pp = calloc(1, sizeof(*pp));
3231 my_assert_not(pp, NULL);
3233 ret = parse_protostr(po->datap, pp);
3235 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
3242 check_func_pp(po, pp, "fptr var call");
3243 if (pp->is_noreturn)
3244 po->flags |= OPF_TAIL;
3250 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
3253 if (po->operand[0].type == OPT_REGMEM) {
3254 char *p = strchr(po->operand[0].name, '[');
3257 ret = p - po->operand[0].name;
3258 strncpy(buf1, po->operand[0].name, ret);
3261 for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
3262 if (IS(g_func_pd[j].label, buf1)) {
3268 //ferr(po, "label '%s' not parsed?\n", buf1);
3270 if (pd->type != OPT_OFFSET)
3271 ferr(po, "label '%s' with non-offset data?\n", buf1);
3273 // find all labels, link
3274 for (j = 0; j < pd->count; j++) {
3275 for (l = 0; l < opcnt; l++) {
3276 if (g_labels[l][0] && IS(g_labels[l], pd->d[j].u.label)) {
3277 add_label_ref(&g_label_refs[l], i);
3288 for (l = 0; l < opcnt; l++) {
3289 if (g_labels[l][0] && IS(po->operand[0].name, g_labels[l])) {
3290 if (l == i + 1 && po->op == OP_JMP) {
3291 // yet another alignment type..
3292 po->flags |= OPF_RMD;
3295 add_label_ref(&g_label_refs[l], i);
3301 if (po->bt_i != -1 || (po->flags & OPF_RMD))
3304 if (po->operand[0].type == OPT_LABEL)
3308 ferr(po, "unhandled branch\n");
3312 po->flags |= OPF_TAIL;
3313 if (i > 0 && ops[i - 1].op == OP_POP)
3314 po->flags |= OPF_ATAIL;
3319 // - remove dead labels
3321 for (i = 0; i < opcnt; i++)
3323 if (g_labels[i][0] != 0 && g_label_refs[i].i == -1)
3327 if (po->flags & OPF_RMD)
3330 if (po->op == OP_CALL)
3332 tmpname = opr_name(po, 0);
3337 pp_c = resolve_icall(i, opcnt, &l);
3339 if (!pp_c->is_func && !pp_c->is_fptr)
3340 ferr(po, "call to non-func: %s\n", pp_c->name);
3341 pp = proto_clone(pp_c);
3342 my_assert_not(pp, NULL);
3344 // not resolved just to single func
3347 switch (po->operand[0].type) {
3349 // we resolved this call and no longer need the register
3350 po->regmask_src &= ~(1 << po->operand[0].reg);
3360 pp = calloc(1, sizeof(*pp));
3361 my_assert_not(pp, NULL);
3363 ret = scan_for_esp_adjust(i + 1, opcnt, &j, &l);
3365 if (!g_allow_regfunc)
3366 ferr(po, "non-__cdecl indirect call unhandled yet\n");
3367 pp->is_unresolved = 1;
3371 if (j > ARRAY_SIZE(pp->arg))
3372 ferr(po, "esp adjust too large: %d\n", j);
3373 pp->ret_type.name = strdup("int");
3374 pp->argc = pp->argc_stack = j;
3375 for (arg = 0; arg < pp->argc; arg++)
3376 pp->arg[arg].type.name = strdup("int");
3381 // look for and make use of esp adjust
3383 if (!pp->is_stdcall && pp->argc_stack > 0)
3384 ret = scan_for_esp_adjust(i + 1, opcnt, &j, &l);
3386 if (pp->is_vararg) {
3387 if (j / 4 < pp->argc_stack)
3388 ferr(po, "esp adjust is too small: %x < %x\n",
3389 j, pp->argc_stack * 4);
3390 // modify pp to make it have varargs as normal args
3392 pp->argc += j / 4 - pp->argc_stack;
3393 for (; arg < pp->argc; arg++) {
3394 pp->arg[arg].type.name = strdup("int");
3397 if (pp->argc > ARRAY_SIZE(pp->arg))
3398 ferr(po, "too many args for '%s'\n", tmpname);
3400 if (pp->argc_stack != j / 4)
3401 ferr(po, "stack tracking failed for '%s': %x %x\n",
3402 tmpname, pp->argc_stack * 4, j);
3404 ops[ret].flags |= OPF_RMD;
3405 if (ops[ret].op == OP_POP && j > 4) {
3406 // deal with multi-pop stack adjust
3408 while (ops[ret].op == OP_POP && j > 0 && ret < opcnt) {
3409 ops[ret].flags |= OPF_RMD;
3415 // a bit of a hack, but deals with use of
3416 // single adj for multiple calls
3417 ops[ret].operand[1].val -= j;
3420 else if (pp->is_vararg)
3421 ferr(po, "missing esp_adjust for vararg func '%s'\n",
3424 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
3425 // since we know the args, collect them
3426 collect_call_args(po, i, pp, ®mask, &save_arg_vars,
3430 if (strstr(pp->ret_type.name, "int64"))
3436 // - find POPs for PUSHes, rm both
3437 // - scan for STD/CLD, propagate DF
3438 // - scan for all used registers
3439 // - find flag set ops for their users
3440 // - do unreselved calls
3441 // - declare indirect functions
3442 for (i = 0; i < opcnt; i++) {
3444 if (po->flags & OPF_RMD)
3447 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
3448 reg = po->operand[0].reg;
3449 if (!(regmask & (1 << reg)))
3450 // not a reg save after all, rerun scan_for_pop
3451 po->flags &= ~OPF_RSAVE;
3453 regmask_save |= 1 << reg;
3456 if (po->op == OP_PUSH && po->p_argnum == 0
3457 && !(po->flags & OPF_RSAVE) && !g_func_pp->is_userstack)
3459 if (po->operand[0].type == OPT_REG)
3461 reg = po->operand[0].reg;
3463 ferr(po, "reg not set for push?\n");
3466 ret = scan_for_pop(i + 1, opcnt,
3467 po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
3470 ferr(po, "too much depth: %d\n", depth);
3472 po->flags |= OPF_RMD;
3473 scan_for_pop(i + 1, opcnt, po->operand[0].name,
3474 i + opcnt * 4, 0, &depth, 1);
3477 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
3480 if (regmask & (1 << reg)) {
3481 if (regmask_save & (1 << reg))
3482 ferr(po, "%s already saved?\n", po->operand[0].name);
3486 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg);
3490 else if (po->operand[0].type == OPT_CONST) {
3491 for (j = i + 1; j < opcnt; j++) {
3492 if ((ops[j].flags & (OPF_JMP|OPF_TAIL|OPF_RSAVE))
3493 || ops[j].op == OP_PUSH || g_labels[i][0] != 0)
3498 if (!(ops[j].flags & OPF_RMD) && ops[j].op == OP_POP)
3500 po->flags |= OPF_RMD;
3508 if (po->op == OP_STD) {
3509 po->flags |= OPF_DF | OPF_RMD;
3510 scan_propagate_df(i + 1, opcnt);
3513 regmask_now = po->regmask_src | po->regmask_dst;
3514 if (regmask_now & (1 << xBP)) {
3515 if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
3516 if (po->regmask_dst & (1 << xBP))
3517 // compiler decided to drop bp frame and use ebp as scratch
3518 scan_fwd_set_flags(i + 1, opcnt, i + opcnt * 5, OPF_EBP_S);
3520 regmask_now &= ~(1 << xBP);
3524 regmask |= regmask_now;
3526 if (po->flags & OPF_CC)
3528 int setters[16], cnt = 0, branched = 0;
3530 ret = scan_for_flag_set(i, i + opcnt * 6,
3531 &branched, setters, &cnt);
3532 if (ret < 0 || cnt <= 0)
3533 ferr(po, "unable to trace flag setter(s)\n");
3534 if (cnt > ARRAY_SIZE(setters))
3535 ferr(po, "too many flag setters\n");
3537 for (j = 0; j < cnt; j++)
3539 tmp_op = &ops[setters[j]]; // flag setter
3542 // to get nicer code, we try to delay test and cmp;
3543 // if we can't because of operand modification, or if we
3544 // have arith op, or branch, make it calculate flags explicitly
3545 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP)
3547 if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
3548 pfomask = 1 << po->pfo;
3550 else if (tmp_op->op == OP_CMPS || tmp_op->op == OP_SCAS) {
3551 pfomask = 1 << po->pfo;
3554 // see if we'll be able to handle based on op result
3555 if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
3556 && po->pfo != PFO_Z && po->pfo != PFO_S
3557 && po->pfo != PFO_P)
3559 || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
3561 pfomask = 1 << po->pfo;
3564 if (tmp_op->op == OP_ADD && po->pfo == PFO_C) {
3565 propagate_lmod(tmp_op, &tmp_op->operand[0],
3566 &tmp_op->operand[1]);
3567 if (tmp_op->operand[0].lmod == OPLM_DWORD)
3572 tmp_op->pfomask |= pfomask;
3573 cond_vars |= pfomask;
3575 // note: may overwrite, currently not a problem
3579 if (po->op == OP_RCL || po->op == OP_RCR
3580 || po->op == OP_ADC || po->op == OP_SBB)
3581 cond_vars |= 1 << PFO_C;
3584 if (po->op == OP_CMPS || po->op == OP_SCAS) {
3585 cond_vars |= 1 << PFO_Z;
3587 else if (po->op == OP_MUL
3588 || (po->op == OP_IMUL && po->operand_cnt == 1))
3590 if (po->operand[0].lmod == OPLM_DWORD)
3593 else if (po->op == OP_CALL) {
3596 ferr(po, "NULL pp\n");
3598 if (pp->is_unresolved) {
3599 int regmask_stack = 0;
3600 collect_call_args(po, i, pp, ®mask, &save_arg_vars,
3603 // this is pretty rough guess:
3604 // see ecx and edx were pushed (and not their saved versions)
3605 for (arg = 0; arg < pp->argc; arg++) {
3606 if (pp->arg[arg].reg != NULL)
3609 tmp_op = pp->arg[arg].datap;
3611 ferr(po, "parsed_op missing for arg%d\n", arg);
3612 if (tmp_op->p_argnum == 0 && tmp_op->operand[0].type == OPT_REG)
3613 regmask_stack |= 1 << tmp_op->operand[0].reg;
3616 if (!((regmask_stack & (1 << xCX))
3617 && (regmask_stack & (1 << xDX))))
3619 if (pp->argc_stack != 0
3620 || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX))))
3622 pp_insert_reg_arg(pp, "ecx");
3623 pp->is_fastcall = 1;
3624 regmask_init |= 1 << xCX;
3625 regmask |= 1 << xCX;
3627 if (pp->argc_stack != 0
3628 || ((regmask | regmask_arg) & (1 << xDX)))
3630 pp_insert_reg_arg(pp, "edx");
3631 regmask_init |= 1 << xDX;
3632 regmask |= 1 << xDX;
3636 // note: __cdecl doesn't fall into is_unresolved category
3637 if (pp->argc_stack > 0)
3641 for (arg = 0; arg < pp->argc; arg++) {
3642 if (pp->arg[arg].reg != NULL) {
3643 reg = char_array_i(regs_r32,
3644 ARRAY_SIZE(regs_r32), pp->arg[arg].reg);
3646 ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg);
3647 if (!(regmask & (1 << reg))) {
3648 regmask_init |= 1 << reg;
3649 regmask |= 1 << reg;
3654 else if (po->op == OP_MOV && po->operand[0].pp != NULL
3655 && po->operand[1].pp != NULL)
3657 // <var> = offset <something>
3658 if ((po->operand[1].pp->is_func || po->operand[1].pp->is_fptr)
3659 && !IS_START(po->operand[1].name, "off_"))
3661 if (!po->operand[0].pp->is_fptr)
3662 ferr(po, "%s not declared as fptr when it should be\n",
3663 po->operand[0].name);
3664 if (pp_cmp_func(po->operand[0].pp, po->operand[1].pp)) {
3665 pp_print(buf1, sizeof(buf1), po->operand[0].pp);
3666 pp_print(buf2, sizeof(buf2), po->operand[1].pp);
3667 fnote(po, "var: %s\n", buf1);
3668 fnote(po, "func: %s\n", buf2);
3669 ferr(po, "^ mismatch\n");
3673 else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void"))
3674 regmask |= 1 << xAX;
3675 else if (po->op == OP_DIV || po->op == OP_IDIV) {
3676 // 32bit division is common, look for it
3677 if (po->op == OP_DIV)
3678 ret = scan_for_reg_clear(i, xDX);
3680 ret = scan_for_cdq_edx(i);
3682 po->flags |= OPF_32BIT;
3686 else if (po->op == OP_CLD)
3687 po->flags |= OPF_RMD;
3689 if (po->op == OP_RCL || po->op == OP_RCR || po->op == OP_XCHG) {
3695 // - confirm regmask_save, it might have been reduced
3696 if (regmask_save != 0)
3699 for (i = 0; i < opcnt; i++) {
3701 if (po->flags & OPF_RMD)
3704 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE))
3705 regmask_save |= 1 << po->operand[0].reg;
3709 // output starts here
3711 // define userstack size
3712 if (g_func_pp->is_userstack) {
3713 fprintf(fout, "#ifndef US_SZ_%s\n", g_func_pp->name);
3714 fprintf(fout, "#define US_SZ_%s USERSTACK_SIZE\n", g_func_pp->name);
3715 fprintf(fout, "#endif\n");
3718 // the function itself
3719 fprintf(fout, "%s ", g_func_pp->ret_type.name);
3720 output_pp_attrs(fout, g_func_pp, g_ida_func_attr & IDAFA_NORETURN);
3721 fprintf(fout, "%s(", g_func_pp->name);
3723 for (i = 0; i < g_func_pp->argc; i++) {
3725 fprintf(fout, ", ");
3726 if (g_func_pp->arg[i].fptr != NULL) {
3728 pp = g_func_pp->arg[i].fptr;
3729 fprintf(fout, "%s (", pp->ret_type.name);
3730 output_pp_attrs(fout, pp, 0);
3731 fprintf(fout, "*a%d)(", i + 1);
3732 for (j = 0; j < pp->argc; j++) {
3734 fprintf(fout, ", ");
3735 if (pp->arg[j].fptr)
3736 ferr(ops, "nested fptr\n");
3737 fprintf(fout, "%s", pp->arg[j].type.name);
3739 if (pp->is_vararg) {
3741 fprintf(fout, ", ");
3742 fprintf(fout, "...");
3746 else if (g_func_pp->arg[i].type.is_retreg) {
3747 fprintf(fout, "u32 *r_%s", g_func_pp->arg[i].reg);
3750 fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1);
3753 if (g_func_pp->is_vararg) {
3755 fprintf(fout, ", ");
3756 fprintf(fout, "...");
3759 fprintf(fout, ")\n{\n");
3761 // declare indirect functions
3762 for (i = 0; i < opcnt; i++) {
3764 if (po->flags & OPF_RMD)
3767 if (po->op == OP_CALL) {
3770 ferr(po, "NULL pp\n");
3772 if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) {
3773 if (pp->name[0] != 0) {
3774 memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
3775 memcpy(pp->name, "i_", 2);
3777 // might be declared already
3779 for (j = 0; j < i; j++) {
3780 if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) {
3781 if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) {
3791 snprintf(pp->name, sizeof(pp->name), "icall%d", i);
3793 fprintf(fout, " %s (", pp->ret_type.name);
3794 output_pp_attrs(fout, pp, 0);
3795 fprintf(fout, "*%s)(", pp->name);
3796 for (j = 0; j < pp->argc; j++) {
3798 fprintf(fout, ", ");
3799 fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1);
3801 fprintf(fout, ");\n");
3806 // output LUTs/jumptables
3807 for (i = 0; i < g_func_pd_cnt; i++) {
3809 fprintf(fout, " static const ");
3810 if (pd->type == OPT_OFFSET) {
3811 fprintf(fout, "void *jt_%s[] =\n { ", pd->label);
3813 for (j = 0; j < pd->count; j++) {
3815 fprintf(fout, ", ");
3816 fprintf(fout, "&&%s", pd->d[j].u.label);
3820 fprintf(fout, "%s %s[] =\n { ",
3821 lmod_type_u(ops, pd->lmod), pd->label);
3823 for (j = 0; j < pd->count; j++) {
3825 fprintf(fout, ", ");
3826 fprintf(fout, "%u", pd->d[j].u.val);
3829 fprintf(fout, " };\n");
3833 // declare stack frame, va_arg
3835 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
3836 (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz);
3840 if (g_func_pp->is_userstack) {
3841 fprintf(fout, " u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name);
3842 fprintf(fout, " u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n");
3846 if (g_func_pp->is_vararg) {
3847 fprintf(fout, " va_list ap;\n");
3851 // declare arg-registers
3852 for (i = 0; i < g_func_pp->argc; i++) {
3853 if (g_func_pp->arg[i].reg != NULL) {
3854 reg = char_array_i(regs_r32,
3855 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
3856 if (regmask & (1 << reg)) {
3857 if (g_func_pp->arg[i].type.is_retreg)
3858 fprintf(fout, " u32 %s = *r_%s;\n",
3859 g_func_pp->arg[i].reg, g_func_pp->arg[i].reg);
3861 fprintf(fout, " u32 %s = (u32)a%d;\n",
3862 g_func_pp->arg[i].reg, i + 1);
3865 if (g_func_pp->arg[i].type.is_retreg)
3866 ferr(ops, "retreg '%s' is unused?\n",
3867 g_func_pp->arg[i].reg);
3868 fprintf(fout, " // %s = a%d; // unused\n",
3869 g_func_pp->arg[i].reg, i + 1);
3875 regmask_now = regmask & ~regmask_arg;
3876 regmask_now &= ~(1 << xSP);
3878 for (reg = 0; reg < 8; reg++) {
3879 if (regmask_now & (1 << reg)) {
3880 fprintf(fout, " u32 %s", regs_r32[reg]);
3881 if (regmask_init & (1 << reg))
3882 fprintf(fout, " = 0");
3883 fprintf(fout, ";\n");
3890 for (reg = 0; reg < 8; reg++) {
3891 if (regmask_save & (1 << reg)) {
3892 fprintf(fout, " u32 s_%s;\n", regs_r32[reg]);
3898 if (save_arg_vars) {
3899 for (reg = 0; reg < 32; reg++) {
3900 if (save_arg_vars & (1 << reg)) {
3901 fprintf(fout, " u32 s_a%d;\n", reg + 1);
3908 for (i = 0; i < 8; i++) {
3909 if (cond_vars & (1 << i)) {
3910 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
3917 fprintf(fout, " u32 tmp;\n");
3922 fprintf(fout, " u64 tmp64;\n");
3927 fprintf(fout, "\n");
3929 if (g_func_pp->is_vararg) {
3930 if (g_func_pp->argc_stack == 0)
3931 ferr(ops, "vararg func without stack args?\n");
3932 fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc);
3936 for (i = 0; i < opcnt; i++)
3938 if (g_labels[i][0] != 0) {
3939 fprintf(fout, "\n%s:\n", g_labels[i]);
3942 delayed_flag_op = NULL;
3943 last_arith_dst = NULL;
3947 if (po->flags & OPF_RMD)
3952 #define assert_operand_cnt(n_) \
3953 if (po->operand_cnt != n_) \
3954 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
3956 // conditional/flag using op?
3957 if (po->flags & OPF_CC)
3963 // we go through all this trouble to avoid using parsed_flag_op,
3964 // which makes generated code much nicer
3965 if (delayed_flag_op != NULL)
3967 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op,
3968 po->pfo, po->pfo_inv);
3971 else if (last_arith_dst != NULL
3972 && (po->pfo == PFO_Z || po->pfo == PFO_S || po->pfo == PFO_P
3973 || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
3976 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
3977 out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv,
3978 last_arith_dst->lmod, buf3);
3981 else if (tmp_op != NULL) {
3982 // use preprocessed flag calc results
3983 if (!(tmp_op->pfomask & (1 << po->pfo)))
3984 ferr(po, "not prepared for pfo %d\n", po->pfo);
3986 // note: pfo_inv was not yet applied
3987 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
3988 po->pfo_inv ? "!" : "", parsed_flag_op_names[po->pfo]);
3991 ferr(po, "all methods of finding comparison failed\n");
3994 if (po->flags & OPF_JMP) {
3995 fprintf(fout, " if %s", buf1);
3997 else if (po->op == OP_RCL || po->op == OP_RCR
3998 || po->op == OP_ADC || po->op == OP_SBB)
4001 fprintf(fout, " cond_%s = %s;\n",
4002 parsed_flag_op_names[po->pfo], buf1);
4004 else if (po->flags & OPF_DATA) { // SETcc
4005 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
4006 fprintf(fout, " %s = %s;", buf2, buf1);
4009 ferr(po, "unhandled conditional op\n");
4013 pfomask = po->pfomask;
4015 if (po->flags & (OPF_REPZ|OPF_REPNZ)) {
4016 struct parsed_opr opr = {0,};
4019 opr.lmod = OPLM_DWORD;
4020 ret = try_resolve_const(i, &opr, opcnt * 7 + i, &uval);
4022 if (ret != 1 || uval == 0) {
4023 // we need initial flags for ecx=0 case..
4024 if (i > 0 && ops[i - 1].op == OP_XOR
4025 && IS(ops[i - 1].operand[0].name,
4026 ops[i - 1].operand[1].name))
4028 fprintf(fout, " cond_z = ");
4029 if (pfomask & (1 << PFO_C))
4030 fprintf(fout, "cond_c = ");
4031 fprintf(fout, "0;\n");
4033 else if (last_arith_dst != NULL) {
4034 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
4035 out_test_for_cc(buf1, sizeof(buf1), po, PFO_Z, 0,
4036 last_arith_dst->lmod, buf3);
4037 fprintf(fout, " cond_z = %s;\n", buf1);
4040 ferr(po, "missing initial ZF\n");
4047 assert_operand_cnt(2);
4048 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4049 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4050 default_cast_to(buf3, sizeof(buf3), &po->operand[0]);
4051 fprintf(fout, " %s = %s;", buf1,
4052 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4057 assert_operand_cnt(2);
4058 po->operand[1].lmod = OPLM_DWORD; // always
4059 fprintf(fout, " %s = %s;",
4060 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4061 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4066 assert_operand_cnt(2);
4067 fprintf(fout, " %s = %s;",
4068 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4069 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4073 assert_operand_cnt(2);
4074 switch (po->operand[1].lmod) {
4076 strcpy(buf3, "(s8)");
4079 strcpy(buf3, "(s16)");
4082 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
4084 fprintf(fout, " %s = %s;",
4085 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4086 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4091 assert_operand_cnt(2);
4092 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4093 fprintf(fout, " tmp = %s;",
4094 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0));
4095 fprintf(fout, " %s = %s;",
4096 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4097 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4098 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
4099 fprintf(fout, " %s = %stmp;",
4100 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
4101 default_cast_to(buf3, sizeof(buf3), &po->operand[1]));
4102 snprintf(g_comment, sizeof(g_comment), "xchg");
4106 assert_operand_cnt(1);
4107 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4108 fprintf(fout, " %s = ~%s;", buf1, buf1);
4112 assert_operand_cnt(2);
4113 fprintf(fout, " %s = (s32)%s >> 31;",
4114 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4115 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4116 strcpy(g_comment, "cdq");
4120 assert_operand_cnt(3);
4121 if (po->flags & OPF_REP) {
4126 fprintf(fout, " eax = %sesi; esi %c= %d;",
4127 lmod_cast_u_ptr(po, po->operand[0].lmod),
4128 (po->flags & OPF_DF) ? '-' : '+',
4129 lmod_bytes(po, po->operand[0].lmod));
4130 strcpy(g_comment, "lods");
4135 assert_operand_cnt(3);
4136 if (po->flags & OPF_REP) {
4137 fprintf(fout, " for (; ecx != 0; ecx--, edi %c= %d)\n",
4138 (po->flags & OPF_DF) ? '-' : '+',
4139 lmod_bytes(po, po->operand[0].lmod));
4140 fprintf(fout, " %sedi = eax;",
4141 lmod_cast_u_ptr(po, po->operand[0].lmod));
4142 strcpy(g_comment, "rep stos");
4145 fprintf(fout, " %sedi = eax; edi %c= %d;",
4146 lmod_cast_u_ptr(po, po->operand[0].lmod),
4147 (po->flags & OPF_DF) ? '-' : '+',
4148 lmod_bytes(po, po->operand[0].lmod));
4149 strcpy(g_comment, "stos");
4154 assert_operand_cnt(3);
4155 j = lmod_bytes(po, po->operand[0].lmod);
4156 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4157 l = (po->flags & OPF_DF) ? '-' : '+';
4158 if (po->flags & OPF_REP) {
4160 " for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n",
4163 " %sedi = %sesi;", buf1, buf1);
4164 strcpy(g_comment, "rep movs");
4167 fprintf(fout, " %sedi = %sesi; edi %c= %d; esi %c= %d;",
4168 buf1, buf1, l, j, l, j);
4169 strcpy(g_comment, "movs");
4174 // repe ~ repeat while ZF=1
4175 assert_operand_cnt(3);
4176 j = lmod_bytes(po, po->operand[0].lmod);
4177 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4178 l = (po->flags & OPF_DF) ? '-' : '+';
4179 if (po->flags & OPF_REP) {
4181 " for (; ecx != 0; ecx--) {\n");
4182 if (pfomask & (1 << PFO_C)) {
4185 " cond_c = %sesi < %sedi;\n", buf1, buf1);
4186 pfomask &= ~(1 << PFO_C);
4189 " cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n",
4190 buf1, buf1, l, j, l, j);
4192 " if (cond_z %s 0) break;\n",
4193 (po->flags & OPF_REPZ) ? "==" : "!=");
4196 snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
4197 (po->flags & OPF_REPZ) ? "e" : "ne");
4201 " cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;",
4202 buf1, buf1, l, j, l, j);
4203 strcpy(g_comment, "cmps");
4205 pfomask &= ~(1 << PFO_Z);
4206 last_arith_dst = NULL;
4207 delayed_flag_op = NULL;
4211 // only does ZF (for now)
4212 // repe ~ repeat while ZF=1
4213 assert_operand_cnt(3);
4214 j = lmod_bytes(po, po->operand[0].lmod);
4215 l = (po->flags & OPF_DF) ? '-' : '+';
4216 if (po->flags & OPF_REP) {
4218 " for (; ecx != 0; ecx--) {\n");
4220 " cond_z = (%seax == %sedi); edi %c= %d;\n",
4221 lmod_cast_u(po, po->operand[0].lmod),
4222 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4224 " if (cond_z %s 0) break;\n",
4225 (po->flags & OPF_REPZ) ? "==" : "!=");
4228 snprintf(g_comment, sizeof(g_comment), "rep%s scas",
4229 (po->flags & OPF_REPZ) ? "e" : "ne");
4232 fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;",
4233 lmod_cast_u(po, po->operand[0].lmod),
4234 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4235 strcpy(g_comment, "scas");
4237 pfomask &= ~(1 << PFO_Z);
4238 last_arith_dst = NULL;
4239 delayed_flag_op = NULL;
4242 // arithmetic w/flags
4245 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4248 assert_operand_cnt(2);
4249 fprintf(fout, " %s %s= %s;",
4250 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4252 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4253 output_std_flags(fout, po, &pfomask, buf1);
4254 last_arith_dst = &po->operand[0];
4255 delayed_flag_op = NULL;
4260 assert_operand_cnt(2);
4261 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4262 if (pfomask & (1 << PFO_C)) {
4263 if (po->operand[1].type == OPT_CONST) {
4264 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4265 j = po->operand[1].val;
4268 if (po->op == OP_SHL)
4272 fprintf(fout, " cond_c = (%s >> %d) & 1;\n",
4276 ferr(po, "zero shift?\n");
4280 pfomask &= ~(1 << PFO_C);
4282 fprintf(fout, " %s %s= %s;", buf1, op_to_c(po),
4283 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4284 output_std_flags(fout, po, &pfomask, buf1);
4285 last_arith_dst = &po->operand[0];
4286 delayed_flag_op = NULL;
4290 assert_operand_cnt(2);
4291 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4292 fprintf(fout, " %s = %s%s >> %s;", buf1,
4293 lmod_cast_s(po, po->operand[0].lmod), buf1,
4294 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4295 output_std_flags(fout, po, &pfomask, buf1);
4296 last_arith_dst = &po->operand[0];
4297 delayed_flag_op = NULL;
4302 assert_operand_cnt(2);
4303 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4304 if (po->operand[1].type == OPT_CONST) {
4305 j = po->operand[1].val;
4306 j %= lmod_bytes(po, po->operand[0].lmod) * 8;
4307 fprintf(fout, po->op == OP_ROL ?
4308 " %s = (%s << %d) | (%s >> %d);" :
4309 " %s = (%s >> %d) | (%s << %d);",
4310 buf1, buf1, j, buf1,
4311 lmod_bytes(po, po->operand[0].lmod) * 8 - j);
4315 output_std_flags(fout, po, &pfomask, buf1);
4316 last_arith_dst = &po->operand[0];
4317 delayed_flag_op = NULL;
4322 assert_operand_cnt(2);
4323 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4324 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4325 if (po->operand[1].type == OPT_CONST) {
4326 j = po->operand[1].val % l;
4328 ferr(po, "zero rotate\n");
4329 fprintf(fout, " tmp = (%s >> %d) & 1;\n",
4330 buf1, (po->op == OP_RCL) ? (l - j) : (j - 1));
4331 if (po->op == OP_RCL) {
4333 " %s = (%s << %d) | (cond_c << %d)",
4334 buf1, buf1, j, j - 1);
4336 fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j);
4340 " %s = (%s >> %d) | (cond_c << %d)",
4341 buf1, buf1, j, l - j);
4343 fprintf(fout, " | (%s << %d)", buf1, l + 1 - j);
4345 fprintf(fout, ";\n");
4346 fprintf(fout, " cond_c = tmp;");
4350 strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr");
4351 output_std_flags(fout, po, &pfomask, buf1);
4352 last_arith_dst = &po->operand[0];
4353 delayed_flag_op = NULL;
4357 assert_operand_cnt(2);
4358 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4359 if (IS(opr_name(po, 0), opr_name(po, 1))) {
4360 // special case for XOR
4361 if (pfomask & (1 << PFO_BE)) { // weird, but it happens..
4362 fprintf(fout, " cond_be = 1;\n");
4363 pfomask &= ~(1 << PFO_BE);
4365 fprintf(fout, " %s = 0;",
4366 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4367 last_arith_dst = &po->operand[0];
4368 delayed_flag_op = NULL;
4374 assert_operand_cnt(2);
4375 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4376 if (pfomask & (1 << PFO_C)) {
4377 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4378 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4379 if (po->operand[0].lmod == OPLM_DWORD) {
4380 fprintf(fout, " tmp64 = (u64)%s + %s;\n", buf1, buf2);
4381 fprintf(fout, " cond_c = tmp64 >> 32;\n");
4382 fprintf(fout, " %s = (u32)tmp64;",
4383 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4384 strcat(g_comment, "add64");
4387 fprintf(fout, " cond_c = ((u32)%s + %s) >> %d;\n",
4388 buf1, buf2, lmod_bytes(po, po->operand[0].lmod) * 8);
4389 fprintf(fout, " %s += %s;",
4390 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4393 pfomask &= ~(1 << PFO_C);
4394 output_std_flags(fout, po, &pfomask, buf1);
4395 last_arith_dst = &po->operand[0];
4396 delayed_flag_op = NULL;
4402 assert_operand_cnt(2);
4403 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4404 if (pfomask & (1 << PFO_C)) {
4405 fprintf(fout, " cond_c = %s < %s;\n",
4406 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]),
4407 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4408 pfomask &= ~(1 << PFO_C);
4414 assert_operand_cnt(2);
4415 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4416 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4417 if (po->op == OP_SBB
4418 && IS(po->operand[0].name, po->operand[1].name))
4420 // avoid use of unitialized var
4421 fprintf(fout, " %s = -cond_c;", buf1);
4422 // carry remains what it was
4423 pfomask &= ~(1 << PFO_C);
4426 fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
4427 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4429 output_std_flags(fout, po, &pfomask, buf1);
4430 last_arith_dst = &po->operand[0];
4431 delayed_flag_op = NULL;
4435 assert_operand_cnt(2);
4436 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4437 fprintf(fout, " %s = %s ? __builtin_ffs(%s) - 1 : 0;",
4438 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4440 output_std_flags(fout, po, &pfomask, buf1);
4441 last_arith_dst = &po->operand[0];
4442 delayed_flag_op = NULL;
4443 strcat(g_comment, "bsf");
4448 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4449 if (po->operand[0].type == OPT_REG) {
4450 strcpy(buf2, po->op == OP_INC ? "++" : "--");
4451 fprintf(fout, " %s%s;", buf1, buf2);
4454 strcpy(buf2, po->op == OP_INC ? "+" : "-");
4455 fprintf(fout, " %s %s= 1;", buf1, buf2);
4457 output_std_flags(fout, po, &pfomask, buf1);
4458 last_arith_dst = &po->operand[0];
4459 delayed_flag_op = NULL;
4463 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4464 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
4465 fprintf(fout, " %s = -%s%s;", buf1,
4466 lmod_cast_s(po, po->operand[0].lmod), buf2);
4467 last_arith_dst = &po->operand[0];
4468 delayed_flag_op = NULL;
4469 if (pfomask & (1 << PFO_C)) {
4470 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
4471 pfomask &= ~(1 << PFO_C);
4476 if (po->operand_cnt == 2) {
4477 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4480 if (po->operand_cnt == 3)
4481 ferr(po, "TODO imul3\n");
4484 assert_operand_cnt(1);
4485 switch (po->operand[0].lmod) {
4487 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
4488 fprintf(fout, " tmp64 = %seax * %s%s;\n", buf1, buf1,
4489 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
4490 fprintf(fout, " edx = tmp64 >> 32;\n");
4491 fprintf(fout, " eax = tmp64;");
4494 strcpy(buf1, po->op == OP_IMUL ? "(s16)(s8)" : "(u16)(u8)");
4495 fprintf(fout, " LOWORD(eax) = %seax * %s;", buf1,
4496 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0],
4500 ferr(po, "TODO: unhandled mul type\n");
4503 last_arith_dst = NULL;
4504 delayed_flag_op = NULL;
4509 assert_operand_cnt(1);
4510 if (po->operand[0].lmod != OPLM_DWORD)
4511 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
4513 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4514 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
4515 po->op == OP_IDIV));
4516 switch (po->operand[0].lmod) {
4518 if (po->flags & OPF_32BIT)
4519 snprintf(buf3, sizeof(buf3), "%seax", buf2);
4521 fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n");
4522 snprintf(buf3, sizeof(buf3), "%stmp64",
4523 (po->op == OP_IDIV) ? "(s64)" : "");
4525 if (po->operand[0].type == OPT_REG
4526 && po->operand[0].reg == xDX)
4528 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4529 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4532 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4533 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4537 ferr(po, "unhandled division type\n");
4539 last_arith_dst = NULL;
4540 delayed_flag_op = NULL;
4545 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4547 for (j = 0; j < 8; j++) {
4548 if (pfomask & (1 << j)) {
4549 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
4550 fprintf(fout, " cond_%s = %s;",
4551 parsed_flag_op_names[j], buf1);
4558 last_arith_dst = NULL;
4559 delayed_flag_op = po;
4563 // SETcc - should already be handled
4566 // note: we reuse OP_Jcc for SETcc, only flags differ
4568 fprintf(fout, "\n goto %s;", po->operand[0].name);
4572 fprintf(fout, " if (ecx == 0)\n");
4573 fprintf(fout, " goto %s;", po->operand[0].name);
4574 strcat(g_comment, "jecxz");
4578 assert_operand_cnt(1);
4579 last_arith_dst = NULL;
4580 delayed_flag_op = NULL;
4582 if (po->operand[0].type == OPT_REGMEM) {
4583 ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
4586 ferr(po, "parse failure for jmp '%s'\n",
4587 po->operand[0].name);
4588 fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
4591 else if (po->operand[0].type != OPT_LABEL)
4592 ferr(po, "unhandled jmp type\n");
4594 fprintf(fout, " goto %s;", po->operand[0].name);
4598 assert_operand_cnt(1);
4600 my_assert_not(pp, NULL);
4603 if (po->flags & OPF_CC) {
4604 // we treat conditional branch to another func
4605 // (yes such code exists..) as conditional tailcall
4607 fprintf(fout, " {\n");
4610 if (pp->is_fptr && !pp->is_arg) {
4611 fprintf(fout, "%s%s = %s;\n", buf3, pp->name,
4612 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
4614 if (pp->is_unresolved)
4615 fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
4616 buf3, asmfn, po->asmln, pp->name);
4619 fprintf(fout, "%s", buf3);
4620 if (strstr(pp->ret_type.name, "int64")) {
4621 if (po->flags & OPF_TAIL)
4622 ferr(po, "int64 and tail?\n");
4623 fprintf(fout, "tmp64 = ");
4625 else if (!IS(pp->ret_type.name, "void")) {
4626 if (po->flags & OPF_TAIL) {
4627 if (!IS(g_func_pp->ret_type.name, "void")) {
4628 fprintf(fout, "return ");
4629 if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
4630 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
4633 else if (regmask & (1 << xAX)) {
4634 fprintf(fout, "eax = ");
4635 if (pp->ret_type.is_ptr)
4636 fprintf(fout, "(u32)");
4640 if (pp->name[0] == 0)
4641 ferr(po, "missing pp->name\n");
4642 fprintf(fout, "%s%s(", pp->name,
4643 pp->has_structarg ? "_sa" : "");
4645 if (po->flags & OPF_ATAIL) {
4646 if (pp->argc_stack != g_func_pp->argc_stack
4647 || (pp->argc_stack > 0
4648 && pp->is_stdcall != g_func_pp->is_stdcall))
4649 ferr(po, "incompatible tailcall\n");
4650 if (g_func_pp->has_retreg)
4651 ferr(po, "TODO: retreg+tailcall\n");
4653 for (arg = j = 0; arg < pp->argc; arg++) {
4655 fprintf(fout, ", ");
4658 if (pp->arg[arg].type.is_ptr)
4659 snprintf(cast, sizeof(cast), "(%s)",
4660 pp->arg[arg].type.name);
4662 if (pp->arg[arg].reg != NULL) {
4663 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
4667 for (; j < g_func_pp->argc; j++)
4668 if (g_func_pp->arg[j].reg == NULL)
4670 fprintf(fout, "%sa%d", cast, j + 1);
4675 for (arg = 0; arg < pp->argc; arg++) {
4677 fprintf(fout, ", ");
4680 if (pp->arg[arg].type.is_ptr)
4681 snprintf(cast, sizeof(cast), "(%s)",
4682 pp->arg[arg].type.name);
4684 if (pp->arg[arg].reg != NULL) {
4685 if (pp->arg[arg].type.is_retreg)
4686 fprintf(fout, "&%s", pp->arg[arg].reg);
4688 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
4693 tmp_op = pp->arg[arg].datap;
4695 ferr(po, "parsed_op missing for arg%d\n", arg);
4697 if (tmp_op->flags & OPF_VAPUSH) {
4698 fprintf(fout, "ap");
4700 else if (tmp_op->p_argpass != 0) {
4701 fprintf(fout, "a%d", tmp_op->p_argpass);
4703 else if (tmp_op->p_argnum != 0) {
4704 fprintf(fout, "%ss_a%d", cast, tmp_op->p_argnum);
4708 out_src_opr(buf1, sizeof(buf1),
4709 tmp_op, &tmp_op->operand[0], cast, 0));
4713 fprintf(fout, ");");
4715 if (strstr(pp->ret_type.name, "int64")) {
4716 fprintf(fout, "\n");
4717 fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3);
4718 fprintf(fout, "%seax = tmp64;", buf3);
4721 if (pp->is_unresolved) {
4722 snprintf(buf2, sizeof(buf2), " unresolved %dreg",
4724 strcat(g_comment, buf2);
4727 if (po->flags & OPF_TAIL) {
4729 if (i == opcnt - 1 || pp->is_noreturn)
4731 else if (IS(pp->ret_type.name, "void"))
4733 else if (IS(g_func_pp->ret_type.name, "void"))
4735 // else already handled as 'return f()'
4738 if (!IS(g_func_pp->ret_type.name, "void")) {
4739 ferr(po, "int func -> void func tailcall?\n");
4742 fprintf(fout, "\n%sreturn;", buf3);
4743 strcat(g_comment, " ^ tailcall");
4747 strcat(g_comment, " tailcall");
4749 if (pp->is_noreturn)
4750 strcat(g_comment, " noreturn");
4751 if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0)
4752 strcat(g_comment, " argframe");
4753 if (po->flags & OPF_CC)
4754 strcat(g_comment, " cond");
4756 if (po->flags & OPF_CC)
4757 fprintf(fout, "\n }");
4759 delayed_flag_op = NULL;
4760 last_arith_dst = NULL;
4764 if (g_func_pp->is_vararg)
4765 fprintf(fout, " va_end(ap);\n");
4766 if (g_func_pp->has_retreg) {
4767 for (arg = 0; arg < g_func_pp->argc; arg++)
4768 if (g_func_pp->arg[arg].type.is_retreg)
4769 fprintf(fout, " *r_%s = %s;\n",
4770 g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg);
4773 if (IS(g_func_pp->ret_type.name, "void")) {
4774 if (i != opcnt - 1 || label_pending)
4775 fprintf(fout, " return;");
4777 else if (g_func_pp->ret_type.is_ptr) {
4778 fprintf(fout, " return (%s)eax;",
4779 g_func_pp->ret_type.name);
4781 else if (IS(g_func_pp->ret_type.name, "__int64"))
4782 fprintf(fout, " return ((u64)edx << 32) | eax;");
4784 fprintf(fout, " return eax;");
4786 last_arith_dst = NULL;
4787 delayed_flag_op = NULL;
4791 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4792 if (po->p_argnum != 0) {
4793 // special case - saved func arg
4794 fprintf(fout, " s_a%d = %s;", po->p_argnum, buf1);
4797 else if (po->flags & OPF_RSAVE) {
4798 fprintf(fout, " s_%s = %s;", buf1, buf1);
4801 else if (g_func_pp->is_userstack) {
4802 fprintf(fout, " *(--esp) = %s;", buf1);
4805 if (!(g_ida_func_attr & IDAFA_NORETURN))
4806 ferr(po, "stray push encountered\n");
4811 if (po->flags & OPF_RSAVE) {
4812 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4813 fprintf(fout, " %s = s_%s;", buf1, buf1);
4816 else if (po->datap != NULL) {
4819 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4820 fprintf(fout, " %s = %s;", buf1,
4821 out_src_opr(buf2, sizeof(buf2),
4822 tmp_op, &tmp_op->operand[0],
4823 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
4826 else if (g_func_pp->is_userstack) {
4827 fprintf(fout, " %s = *esp++;",
4828 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4832 ferr(po, "stray pop encountered\n");
4841 ferr(po, "unhandled op type %d, flags %x\n",
4846 if (g_comment[0] != 0) {
4847 char *p = g_comment;
4848 while (my_isblank(*p))
4850 fprintf(fout, " // %s", p);
4855 fprintf(fout, "\n");
4857 // some sanity checking
4858 if (po->flags & OPF_REP) {
4859 if (po->op != OP_STOS && po->op != OP_MOVS
4860 && po->op != OP_CMPS && po->op != OP_SCAS)
4861 ferr(po, "unexpected rep\n");
4862 if (!(po->flags & (OPF_REPZ|OPF_REPNZ))
4863 && (po->op == OP_CMPS || po->op == OP_SCAS))
4864 ferr(po, "cmps/scas with plain rep\n");
4866 if ((po->flags & (OPF_REPZ|OPF_REPNZ))
4867 && po->op != OP_CMPS && po->op != OP_SCAS)
4868 ferr(po, "unexpected repz/repnz\n");
4871 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
4873 // see is delayed flag stuff is still valid
4874 if (delayed_flag_op != NULL && delayed_flag_op != po) {
4875 if (is_any_opr_modified(delayed_flag_op, po, 0))
4876 delayed_flag_op = NULL;
4879 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
4880 if (is_opr_modified(last_arith_dst, po))
4881 last_arith_dst = NULL;
4887 if (g_stack_fsz && !g_stack_frame_used)
4888 fprintf(fout, " (void)sf;\n");
4890 fprintf(fout, "}\n\n");
4893 for (i = 0; i < opcnt; i++) {
4894 struct label_ref *lr, *lr_del;
4896 lr = g_label_refs[i].next;
4897 while (lr != NULL) {
4902 g_label_refs[i].i = -1;
4903 g_label_refs[i].next = NULL;
4905 if (ops[i].op == OP_CALL) {
4907 proto_release(ops[i].pp);
4913 static void set_label(int i, const char *name)
4919 p = strchr(name, ':');
4923 if (len > sizeof(g_labels[0]) - 1)
4924 aerr("label '%s' too long: %d\n", name, len);
4925 if (g_labels[i][0] != 0 && !IS_START(g_labels[i], "algn_"))
4926 aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
4927 memcpy(g_labels[i], name, len);
4928 g_labels[i][len] = 0;
4931 // '=' needs special treatment..
4932 static char *next_word_s(char *w, size_t wsize, char *s)
4938 for (i = 0; i < wsize - 1; i++) {
4939 if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
4945 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
4946 printf("warning: '%s' truncated\n", w);
4957 static struct chunk_item *func_chunks;
4958 static int func_chunk_cnt;
4959 static int func_chunk_alloc;
4961 static void add_func_chunk(FILE *fasm, const char *name, int line)
4963 if (func_chunk_cnt >= func_chunk_alloc) {
4964 func_chunk_alloc *= 2;
4965 func_chunks = realloc(func_chunks,
4966 func_chunk_alloc * sizeof(func_chunks[0]));
4967 my_assert_not(func_chunks, NULL);
4969 func_chunks[func_chunk_cnt].fptr = ftell(fasm);
4970 func_chunks[func_chunk_cnt].name = strdup(name);
4971 func_chunks[func_chunk_cnt].asmln = line;
4975 static int cmp_chunks(const void *p1, const void *p2)
4977 const struct chunk_item *c1 = p1, *c2 = p2;
4978 return strcmp(c1->name, c2->name);
4981 static int cmpstringp(const void *p1, const void *p2)
4983 return strcmp(*(char * const *)p1, *(char * const *)p2);
4986 static void scan_ahead(FILE *fasm)
4996 oldpos = ftell(fasm);
4999 while (fgets(line, sizeof(line), fasm))
5010 // get rid of random tabs
5011 for (i = 0; line[i] != 0; i++)
5012 if (line[i] == '\t')
5015 if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
5018 next_word(words[0], sizeof(words[0]), p);
5019 if (words[0][0] == 0)
5020 aerr("missing name for func chunk?\n");
5022 add_func_chunk(fasm, words[0], asmln);
5024 else if (IS_START(p, "; sctend"))
5030 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
5031 words[wordc][0] = 0;
5032 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
5033 if (*p == 0 || *p == ';') {
5039 if (wordc == 2 && IS(words[1], "ends"))
5043 fseek(fasm, oldpos, SEEK_SET);
5047 int main(int argc, char *argv[])
5049 FILE *fout, *fasm, *frlist;
5050 struct parsed_data *pd = NULL;
5052 char **rlist = NULL;
5054 int rlist_alloc = 0;
5055 int func_chunks_used = 0;
5056 int func_chunks_sorted = 0;
5057 int func_chunk_i = -1;
5058 long func_chunk_ret = 0;
5059 int func_chunk_ret_ln = 0;
5060 int scanned_ahead = 0;
5062 char words[20][256];
5063 enum opr_lenmod lmod;
5064 char *sctproto = NULL;
5066 int pending_endp = 0;
5068 int skip_warned = 0;
5081 for (arg = 1; arg < argc; arg++) {
5082 if (IS(argv[arg], "-v"))
5084 else if (IS(argv[arg], "-rf"))
5085 g_allow_regfunc = 1;
5086 else if (IS(argv[arg], "-m"))
5092 if (argc < arg + 3) {
5093 printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdrf> [rlist]*\n",
5100 asmfn = argv[arg++];
5101 fasm = fopen(asmfn, "r");
5102 my_assert_not(fasm, NULL);
5104 hdrfn = argv[arg++];
5105 g_fhdr = fopen(hdrfn, "r");
5106 my_assert_not(g_fhdr, NULL);
5109 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
5110 my_assert_not(rlist, NULL);
5111 // needs special handling..
5112 rlist[rlist_len++] = "__alloca_probe";
5114 func_chunk_alloc = 32;
5115 func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
5116 my_assert_not(func_chunks, NULL);
5118 memset(words, 0, sizeof(words));
5120 for (; arg < argc; arg++) {
5121 frlist = fopen(argv[arg], "r");
5122 my_assert_not(frlist, NULL);
5124 while (fgets(line, sizeof(line), frlist)) {
5126 if (*p == 0 || *p == ';')
5129 if (IS_START(p, "#if 0")
5130 || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
5134 else if (IS_START(p, "#endif"))
5141 p = next_word(words[0], sizeof(words[0]), p);
5142 if (words[0][0] == 0)
5145 if (rlist_len >= rlist_alloc) {
5146 rlist_alloc = rlist_alloc * 2 + 64;
5147 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
5148 my_assert_not(rlist, NULL);
5150 rlist[rlist_len++] = strdup(words[0]);
5159 qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
5161 fout = fopen(argv[arg_out], "w");
5162 my_assert_not(fout, NULL);
5165 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
5166 my_assert_not(g_eqs, NULL);
5168 for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
5169 g_label_refs[i].i = -1;
5170 g_label_refs[i].next = NULL;
5173 while (fgets(line, sizeof(line), fasm))
5182 // get rid of random tabs
5183 for (i = 0; line[i] != 0; i++)
5184 if (line[i] == '\t')
5189 if (p[2] == '=' && IS_START(p, "; =============== S U B"))
5190 goto do_pending_endp; // eww..
5192 if (p[2] == 'A' && IS_START(p, "; Attributes:"))
5194 static const char *attrs[] = {
5203 // parse IDA's attribute-list comment
5204 g_ida_func_attr = 0;
5207 for (; *p != 0; p = sskip(p)) {
5208 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
5209 if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
5210 g_ida_func_attr |= 1 << i;
5211 p += strlen(attrs[i]);
5215 if (i == ARRAY_SIZE(attrs)) {
5216 anote("unparsed IDA attr: %s\n", p);
5219 if (IS(attrs[i], "fpd=")) {
5220 p = next_word(words[0], sizeof(words[0]), p);
5225 else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
5228 next_word(words[0], sizeof(words[0]), p);
5229 if (words[0][0] == 0)
5230 aerr("missing name for func chunk?\n");
5232 if (!scanned_ahead) {
5233 add_func_chunk(fasm, words[0], asmln);
5234 func_chunks_sorted = 0;
5237 else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
5239 if (func_chunk_i >= 0) {
5240 if (func_chunk_i < func_chunk_cnt
5241 && IS(func_chunks[func_chunk_i].name, g_func))
5243 // move on to next chunk
5244 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
5246 aerr("seek failed for '%s' chunk #%d\n",
5247 g_func, func_chunk_i);
5248 asmln = func_chunks[func_chunk_i].asmln;
5252 if (func_chunk_ret == 0)
5253 aerr("no return from chunk?\n");
5254 fseek(fasm, func_chunk_ret, SEEK_SET);
5255 asmln = func_chunk_ret_ln;
5261 else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
5262 func_chunks_used = 1;
5264 if (IS_START(g_func, "sub_")) {
5265 unsigned long addr = strtoul(p, NULL, 16);
5266 unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
5267 if (addr > f_addr && !scanned_ahead) {
5268 anote("scan_ahead caused by '%s', addr %lx\n",
5272 func_chunks_sorted = 0;
5280 for (i = wordc; i < ARRAY_SIZE(words); i++)
5282 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
5283 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
5284 if (*p == 0 || *p == ';') {
5289 if (*p != 0 && *p != ';')
5290 aerr("too many words\n");
5292 // alow asm patches in comments
5294 if (IS_START(p, "; sctpatch:")) {
5296 if (*p == 0 || *p == ';')
5298 goto parse_words; // lame
5300 if (IS_START(p, "; sctproto:")) {
5301 sctproto = strdup(p + 11);
5303 else if (IS_START(p, "; sctend")) {
5312 awarn("wordc == 0?\n");
5316 // don't care about this:
5317 if (words[0][0] == '.'
5318 || IS(words[0], "include")
5319 || IS(words[0], "assume") || IS(words[1], "segment")
5320 || IS(words[0], "align"))
5326 // do delayed endp processing to collect switch jumptables
5328 if (in_func && !skip_func && !end && wordc >= 2
5329 && ((words[0][0] == 'd' && words[0][2] == 0)
5330 || (words[1][0] == 'd' && words[1][2] == 0)))
5333 if (words[1][0] == 'd' && words[1][2] == 0) {
5335 if (g_func_pd_cnt >= pd_alloc) {
5336 pd_alloc = pd_alloc * 2 + 16;
5337 g_func_pd = realloc(g_func_pd,
5338 sizeof(g_func_pd[0]) * pd_alloc);
5339 my_assert_not(g_func_pd, NULL);
5341 pd = &g_func_pd[g_func_pd_cnt];
5343 memset(pd, 0, sizeof(*pd));
5344 strcpy(pd->label, words[0]);
5345 pd->type = OPT_CONST;
5346 pd->lmod = lmod_from_directive(words[1]);
5352 anote("skipping alignment byte?\n");
5355 lmod = lmod_from_directive(words[0]);
5356 if (lmod != pd->lmod)
5357 aerr("lmod change? %d->%d\n", pd->lmod, lmod);
5360 if (pd->count_alloc < pd->count + wordc) {
5361 pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
5362 pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
5363 my_assert_not(pd->d, NULL);
5365 for (; i < wordc; i++) {
5366 if (IS(words[i], "offset")) {
5367 pd->type = OPT_OFFSET;
5370 p = strchr(words[i], ',');
5373 if (pd->type == OPT_OFFSET)
5374 pd->d[pd->count].u.label = strdup(words[i]);
5376 pd->d[pd->count].u.val = parse_number(words[i]);
5377 pd->d[pd->count].bt_i = -1;
5383 if (in_func && !skip_func)
5384 gen_func(fout, g_fhdr, g_func, pi);
5388 g_ida_func_attr = 0;
5392 func_chunks_used = 0;
5395 memset(&ops, 0, pi * sizeof(ops[0]));
5396 memset(g_labels, 0, pi * sizeof(g_labels[0]));
5400 for (i = 0; i < g_func_pd_cnt; i++) {
5402 if (pd->type == OPT_OFFSET) {
5403 for (j = 0; j < pd->count; j++)
5404 free(pd->d[j].u.label);
5418 if (IS(words[1], "proc")) {
5420 aerr("proc '%s' while in_func '%s'?\n",
5423 if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
5425 strcpy(g_func, words[0]);
5426 set_label(0, words[0]);
5431 if (IS(words[1], "endp"))
5434 aerr("endp '%s' while not in_func?\n", words[0]);
5435 if (!IS(g_func, words[0]))
5436 aerr("endp '%s' while in_func '%s'?\n",
5439 if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1
5440 && ops[0].op == OP_JMP && ops[0].operand[0].had_ds)
5446 if (!skip_func && func_chunks_used) {
5447 // start processing chunks
5448 struct chunk_item *ci, key = { g_func, 0 };
5450 func_chunk_ret = ftell(fasm);
5451 func_chunk_ret_ln = asmln;
5452 if (!func_chunks_sorted) {
5453 qsort(func_chunks, func_chunk_cnt,
5454 sizeof(func_chunks[0]), cmp_chunks);
5455 func_chunks_sorted = 1;
5457 ci = bsearch(&key, func_chunks, func_chunk_cnt,
5458 sizeof(func_chunks[0]), cmp_chunks);
5460 aerr("'%s' needs chunks, but none found\n", g_func);
5461 func_chunk_i = ci - func_chunks;
5462 for (; func_chunk_i > 0; func_chunk_i--)
5463 if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
5466 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
5468 aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
5469 asmln = func_chunks[func_chunk_i].asmln;
5477 if (wordc == 2 && IS(words[1], "ends")) {
5481 goto do_pending_endp;
5485 // scan for next text segment
5486 while (fgets(line, sizeof(line), fasm)) {
5489 if (*p == 0 || *p == ';')
5492 if (strstr(p, "segment para public 'CODE' use32"))
5499 p = strchr(words[0], ':');
5501 set_label(pi, words[0]);
5505 if (!in_func || skip_func) {
5506 if (!skip_warned && !skip_func && g_labels[pi][0] != 0) {
5508 anote("skipping from '%s'\n", g_labels[pi]);
5511 g_labels[pi][0] = 0;
5515 if (wordc > 1 && IS(words[1], "="))
5518 aerr("unhandled equ, wc=%d\n", wordc);
5519 if (g_eqcnt >= eq_alloc) {
5521 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
5522 my_assert_not(g_eqs, NULL);
5525 len = strlen(words[0]);
5526 if (len > sizeof(g_eqs[0].name) - 1)
5527 aerr("equ name too long: %d\n", len);
5528 strcpy(g_eqs[g_eqcnt].name, words[0]);
5530 if (!IS(words[3], "ptr"))
5531 aerr("unhandled equ\n");
5532 if (IS(words[2], "dword"))
5533 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
5534 else if (IS(words[2], "word"))
5535 g_eqs[g_eqcnt].lmod = OPLM_WORD;
5536 else if (IS(words[2], "byte"))
5537 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
5539 aerr("bad lmod: '%s'\n", words[2]);
5541 g_eqs[g_eqcnt].offset = parse_number(words[4]);
5546 if (pi >= ARRAY_SIZE(ops))
5547 aerr("too many ops\n");
5549 parse_op(&ops[pi], words, wordc);
5551 if (sctproto != NULL) {
5552 if (ops[pi].op == OP_CALL || ops[pi].op == OP_JMP)
5553 ops[pi].datap = sctproto;
5566 // vim:ts=2:shiftwidth=2:expandtab