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",
500 static const char *word_types[] = {
501 "uint16_t", "int16_t", "_WORD", "WORD",
502 "unsigned __int16", "__int16",
504 static const char *byte_types[] = {
505 "uint8_t", "int8_t", "char",
506 "unsigned __int8", "__int8", "BYTE", "_BYTE",
508 // structures.. deal the same as with _UNKNOWN for now
514 if (c_type->is_ptr) {
519 n = skip_type_mod(c_type->name);
521 for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
522 if (IS(n, dword_types[i])) {
528 for (i = 0; i < ARRAY_SIZE(word_types); i++) {
529 if (IS(n, word_types[i])) {
535 for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
536 if (IS(n, byte_types[i])) {
545 static char *default_cast_to(char *buf, size_t buf_size,
546 struct parsed_opr *opr)
552 if (opr->pp == NULL || opr->pp->type.name == NULL
555 snprintf(buf, buf_size, "%s", "(void *)");
559 snprintf(buf, buf_size, "(%s)", opr->pp->type.name);
563 static enum opr_type lmod_from_directive(const char *d)
567 else if (IS(d, "dw"))
569 else if (IS(d, "db"))
572 aerr("unhandled directive: '%s'\n", d);
576 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
582 *regmask |= 1 << reg;
585 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
588 static int parse_operand(struct parsed_opr *opr,
589 int *regmask, int *regmask_indirect,
590 char words[16][256], int wordc, int w, unsigned int op_flags)
592 const struct parsed_proto *pp;
593 enum opr_lenmod tmplmod;
594 unsigned long number;
602 aerr("parse_operand w %d, wordc %d\n", w, wordc);
606 for (i = w; i < wordc; i++) {
607 len = strlen(words[i]);
608 if (words[i][len - 1] == ',') {
609 words[i][len - 1] = 0;
615 wordc_in = wordc - w;
617 if ((op_flags & OPF_JMP) && wordc_in > 0
618 && !('0' <= words[w][0] && words[w][0] <= '9'))
620 const char *label = NULL;
622 if (wordc_in == 3 && !strncmp(words[w], "near", 4)
623 && IS(words[w + 1], "ptr"))
624 label = words[w + 2];
625 else if (wordc_in == 2 && IS(words[w], "short"))
626 label = words[w + 1];
627 else if (wordc_in == 1
628 && strchr(words[w], '[') == NULL
629 && parse_reg(&tmplmod, words[w]) < 0)
633 opr->type = OPT_LABEL;
634 ret = check_segment_prefix(label);
637 aerr("fs/gs used\n");
641 strcpy(opr->name, label);
647 if (IS(words[w + 1], "ptr")) {
648 if (IS(words[w], "dword"))
649 opr->lmod = OPLM_DWORD;
650 else if (IS(words[w], "word"))
651 opr->lmod = OPLM_WORD;
652 else if (IS(words[w], "byte"))
653 opr->lmod = OPLM_BYTE;
655 aerr("type parsing failed\n");
657 wordc_in = wordc - w;
662 if (IS(words[w], "offset")) {
663 opr->type = OPT_OFFSET;
664 opr->lmod = OPLM_DWORD;
665 strcpy(opr->name, words[w + 1]);
666 pp = proto_parse(g_fhdr, opr->name, 1);
669 if (IS(words[w], "(offset")) {
670 p = strchr(words[w + 1], ')');
672 aerr("parse of bracketed offset failed\n");
674 opr->type = OPT_OFFSET;
675 strcpy(opr->name, words[w + 1]);
681 aerr("parse_operand 1 word expected\n");
683 ret = check_segment_prefix(words[w]);
686 aerr("fs/gs used\n");
688 memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
690 strcpy(opr->name, words[w]);
692 if (words[w][0] == '[') {
693 opr->type = OPT_REGMEM;
694 ret = sscanf(words[w], "[%[^]]]", opr->name);
696 aerr("[] parse failure\n");
698 parse_indmode(opr->name, regmask_indirect, 1);
699 if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL, 1))
702 struct parsed_equ *eq =
703 equ_find(NULL, parse_stack_el(opr->name, NULL, 1), &i);
705 opr->lmod = eq->lmod;
709 else if (strchr(words[w], '[')) {
711 p = strchr(words[w], '[');
712 opr->type = OPT_REGMEM;
713 parse_indmode(p, regmask_indirect, 0);
714 strncpy(buf, words[w], p - words[w]);
715 buf[p - words[w]] = 0;
716 pp = proto_parse(g_fhdr, buf, 1);
719 else if (('0' <= words[w][0] && words[w][0] <= '9')
720 || words[w][0] == '-')
722 number = parse_number(words[w]);
723 opr->type = OPT_CONST;
725 printf_number(opr->name, sizeof(opr->name), number);
729 ret = parse_reg(&tmplmod, opr->name);
731 setup_reg_opr(opr, ret, tmplmod, regmask);
735 // most likely var in data segment
736 opr->type = OPT_LABEL;
737 pp = proto_parse(g_fhdr, opr->name, 0);
741 if (pp->is_fptr || pp->is_func) {
742 opr->lmod = OPLM_DWORD;
746 tmplmod = OPLM_UNSPEC;
747 if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
748 anote("unhandled C type '%s' for '%s'\n",
749 pp->type.name, opr->name);
751 if (opr->lmod == OPLM_UNSPEC) {
753 opr->type_from_var = 1;
755 else if (opr->lmod != tmplmod) {
756 opr->size_mismatch = 1;
757 if (tmplmod < opr->lmod)
760 opr->is_ptr = pp->type.is_ptr;
762 opr->is_array = pp->type.is_array;
766 if (opr->lmod == OPLM_UNSPEC)
767 guess_lmod_from_name(opr);
771 static const struct {
776 { "repe", OPF_REP|OPF_REPZ },
777 { "repz", OPF_REP|OPF_REPZ },
778 { "repne", OPF_REP|OPF_REPNZ },
779 { "repnz", OPF_REP|OPF_REPNZ },
780 { "lock", OPF_LOCK }, // ignored for now..
783 #define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC)
785 static const struct {
788 unsigned short minopr;
789 unsigned short maxopr;
792 unsigned char pfo_inv;
794 { "nop", OP_NOP, 0, 0, 0 },
795 { "push", OP_PUSH, 1, 1, 0 },
796 { "pop", OP_POP, 1, 1, OPF_DATA },
797 { "leave",OP_LEAVE, 0, 0, OPF_DATA },
798 { "mov" , OP_MOV, 2, 2, OPF_DATA },
799 { "lea", OP_LEA, 2, 2, OPF_DATA },
800 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
801 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
802 { "xchg", OP_XCHG, 2, 2, OPF_DATA },
803 { "not", OP_NOT, 1, 1, OPF_DATA },
804 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
805 { "lodsb",OP_LODS, 0, 0, OPF_DATA },
806 { "lodsw",OP_LODS, 0, 0, OPF_DATA },
807 { "lodsd",OP_LODS, 0, 0, OPF_DATA },
808 { "stosb",OP_STOS, 0, 0, OPF_DATA },
809 { "stosw",OP_STOS, 0, 0, OPF_DATA },
810 { "stosd",OP_STOS, 0, 0, OPF_DATA },
811 { "movsb",OP_MOVS, 0, 0, OPF_DATA },
812 { "movsw",OP_MOVS, 0, 0, OPF_DATA },
813 { "movsd",OP_MOVS, 0, 0, OPF_DATA },
814 { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
815 { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
816 { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
817 { "scasb",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
818 { "scasw",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
819 { "scasd",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
820 { "std", OP_STD, 0, 0, OPF_DATA }, // special flag
821 { "cld", OP_CLD, 0, 0, OPF_DATA },
822 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
823 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
824 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
825 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
826 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
827 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
828 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
829 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
830 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
831 { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS },
832 { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS },
833 { "rcl", OP_RCL, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
834 { "rcr", OP_RCR, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
835 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
836 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
837 { "bsf", OP_BSF, 2, 2, OPF_DATA|OPF_FLAGS },
838 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
839 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
840 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
841 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
842 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
843 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
844 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
845 { "test", OP_TEST, 2, 2, OPF_FLAGS },
846 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
847 { "retn", OP_RET, 0, 1, OPF_TAIL },
848 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
849 { "jmp", OP_JMP, 1, 1, OPF_JMP },
850 { "jecxz",OP_JECXZ, 1, 1, OPF_JMP|OPF_CJMP },
851 { "jo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 0 }, // 70 OF=1
852 { "jno", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 1 }, // 71 OF=0
853 { "jc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 CF=1
854 { "jb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72
855 { "jnc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 CF=0
856 { "jnb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
857 { "jae", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
858 { "jz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74 ZF=1
859 { "je", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74
860 { "jnz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75 ZF=0
861 { "jne", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75
862 { "jbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 CF=1||ZF=1
863 { "jna", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76
864 { "ja", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 CF=0&&ZF=0
865 { "jnbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77
866 { "js", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 0 }, // 78 SF=1
867 { "jns", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 1 }, // 79 SF=0
868 { "jp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a PF=1
869 { "jpe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a
870 { "jnp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b PF=0
871 { "jpo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b
872 { "jl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c SF!=OF
873 { "jnge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c
874 { "jge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d SF=OF
875 { "jnl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d
876 { "jle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e ZF=1||SF!=OF
877 { "jng", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e
878 { "jg", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f ZF=0&&SF=OF
879 { "jnle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f
880 { "seto", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 0 },
881 { "setno", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 1 },
882 { "setc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
883 { "setb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
884 { "setnc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
885 { "setae", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
886 { "setnb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
887 { "setz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
888 { "sete", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
889 { "setnz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
890 { "setne", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
891 { "setbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
892 { "setna", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
893 { "seta", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
894 { "setnbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
895 { "sets", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 0 },
896 { "setns", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 1 },
897 { "setp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
898 { "setpe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
899 { "setnp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
900 { "setpo", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
901 { "setl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
902 { "setnge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
903 { "setge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
904 { "setnl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
905 { "setle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
906 { "setng", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
907 { "setg", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
908 { "setnle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
911 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
913 enum opr_lenmod lmod = OPLM_UNSPEC;
914 int prefix_flags = 0;
922 for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
923 if (IS(words[w], pref_table[i].name)) {
924 prefix_flags = pref_table[i].flags;
931 aerr("lone prefix: '%s'\n", words[0]);
936 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
937 if (IS(words[w], op_table[i].name))
941 if (i == ARRAY_SIZE(op_table))
942 aerr("unhandled op: '%s'\n", words[0]);
945 op->op = op_table[i].op;
946 op->flags = op_table[i].flags | prefix_flags;
947 op->pfo = op_table[i].pfo;
948 op->pfo_inv = op_table[i].pfo_inv;
949 op->regmask_src = op->regmask_dst = 0;
952 for (opr = 0; opr < op_table[i].minopr; opr++) {
953 regmask = regmask_ind = 0;
954 w = parse_operand(&op->operand[opr], ®mask, ®mask_ind,
955 words, wordc, w, op->flags);
957 if (opr == 0 && (op->flags & OPF_DATA))
958 op->regmask_dst = regmask;
959 // for now, mark dst as src too
960 op->regmask_src |= regmask | regmask_ind;
963 for (; w < wordc && opr < op_table[i].maxopr; opr++) {
964 w = parse_operand(&op->operand[opr],
965 &op->regmask_src, &op->regmask_src,
966 words, wordc, w, op->flags);
970 aerr("parse_op %s incomplete: %d/%d\n",
974 op->operand_cnt = opr;
975 if (!strncmp(op_table[i].name, "set", 3))
976 op->operand[0].lmod = OPLM_BYTE;
978 // ops with implicit argumets
982 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
983 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
989 if (op->operand_cnt != 0)
991 if (words[op_w][4] == 'b')
993 else if (words[op_w][4] == 'w')
995 else if (words[op_w][4] == 'd')
998 setup_reg_opr(&op->operand[0], op->op == OP_LODS ? xSI : xDI,
999 lmod, &op->regmask_src);
1000 setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src);
1001 op->regmask_dst = op->regmask_src;
1002 setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD,
1003 op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src);
1008 if (op->operand_cnt != 0)
1010 if (words[op_w][4] == 'b')
1012 else if (words[op_w][4] == 'w')
1014 else if (words[op_w][4] == 'd')
1016 op->operand_cnt = 3;
1017 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
1018 setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src);
1019 setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src);
1020 op->regmask_dst = op->regmask_src;
1024 op->regmask_src |= op->regmask_dst;
1025 op->regmask_dst |= op->regmask_src;
1029 op->operand_cnt = 1;
1030 op->regmask_src = 1 << xCX;
1031 op->operand[0].type = OPT_REG;
1032 op->operand[0].reg = xCX;
1033 op->operand[0].lmod = OPLM_DWORD;
1037 if (op->operand_cnt != 1)
1042 op->regmask_dst = (1 << xDX) | (1 << xAX);
1043 op->regmask_src |= (1 << xAX);
1044 if (op->operand[0].lmod == OPLM_UNSPEC)
1045 op->operand[0].lmod = OPLM_DWORD;
1050 // we could set up operands for edx:eax, but there is no real need to
1051 // (see is_opr_modified())
1052 regmask = (1 << xDX) | (1 << xAX);
1053 op->regmask_dst = regmask;
1054 op->regmask_src |= regmask;
1055 if (op->operand[0].lmod == OPLM_UNSPEC)
1056 op->operand[0].lmod = OPLM_DWORD;
1064 if (op->operand[1].lmod == OPLM_UNSPEC)
1065 op->operand[1].lmod = OPLM_BYTE;
1069 if (op->operand[0].lmod == OPLM_UNSPEC
1070 && (op->operand[0].type == OPT_CONST
1071 || op->operand[0].type == OPT_OFFSET
1072 || op->operand[0].type == OPT_LABEL))
1073 op->operand[0].lmod = OPLM_DWORD;
1079 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1080 && op->operand[0].lmod == op->operand[1].lmod
1081 && op->operand[0].reg == op->operand[1].reg
1082 && IS(op->operand[0].name, op->operand[1].name)) // ah, al..
1084 op->flags |= OPF_RMD;
1085 op->regmask_src = op->regmask_dst = 0;
1090 if (op->operand[0].type == OPT_REG
1091 && op->operand[1].type == OPT_REGMEM)
1094 snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
1095 if (IS(buf, op->operand[1].name))
1096 op->flags |= OPF_RMD;
1101 // trashed regs must be explicitly detected later
1102 op->regmask_dst = 0;
1110 static const char *op_name(struct parsed_op *po)
1112 static char buf[16];
1116 if (po->op == OP_JCC || po->op == OP_SCC) {
1118 *p++ = (po->op == OP_JCC) ? 'j' : 's';
1121 strcpy(p, parsed_flag_op_names[po->pfo]);
1125 for (i = 0; i < ARRAY_SIZE(op_table); i++)
1126 if (op_table[i].op == po->op)
1127 return op_table[i].name;
1133 static const char *dump_op(struct parsed_op *po)
1135 static char out[128];
1142 snprintf(out, sizeof(out), "%s", op_name(po));
1143 for (i = 0; i < po->operand_cnt; i++) {
1147 snprintf(p, sizeof(out) - (p - out),
1148 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1149 po->operand[i].name);
1155 static const char *lmod_type_u(struct parsed_op *po,
1156 enum opr_lenmod lmod)
1166 ferr(po, "invalid lmod: %d\n", lmod);
1167 return "(_invalid_)";
1171 static const char *lmod_cast_u(struct parsed_op *po,
1172 enum opr_lenmod lmod)
1182 ferr(po, "invalid lmod: %d\n", lmod);
1183 return "(_invalid_)";
1187 static const char *lmod_cast_u_ptr(struct parsed_op *po,
1188 enum opr_lenmod lmod)
1198 ferr(po, "invalid lmod: %d\n", lmod);
1199 return "(_invalid_)";
1203 static const char *lmod_cast_s(struct parsed_op *po,
1204 enum opr_lenmod lmod)
1214 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1215 return "(_invalid_)";
1219 static const char *lmod_cast(struct parsed_op *po,
1220 enum opr_lenmod lmod, int is_signed)
1223 lmod_cast_s(po, lmod) :
1224 lmod_cast_u(po, lmod);
1227 static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1237 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1242 static const char *opr_name(struct parsed_op *po, int opr_num)
1244 if (opr_num >= po->operand_cnt)
1245 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1246 return po->operand[opr_num].name;
1249 static unsigned int opr_const(struct parsed_op *po, int opr_num)
1251 if (opr_num >= po->operand_cnt)
1252 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1253 if (po->operand[opr_num].type != OPT_CONST)
1254 ferr(po, "opr %d: const expected\n", opr_num);
1255 return po->operand[opr_num].val;
1258 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1260 if ((unsigned int)popr->reg >= MAX_REGS)
1261 ferr(po, "invalid reg: %d\n", popr->reg);
1262 return regs_r32[popr->reg];
1265 // cast1 is the "final" cast
1266 static const char *simplify_cast(const char *cast1, const char *cast2)
1268 static char buf[256];
1274 if (IS(cast1, cast2))
1276 if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
1278 if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
1280 if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
1282 if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
1284 if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1287 snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1291 static const char *simplify_cast_num(const char *cast, unsigned int val)
1293 if (IS(cast, "(u8)") && val < 0x100)
1295 if (IS(cast, "(s8)") && val < 0x80)
1297 if (IS(cast, "(u16)") && val < 0x10000)
1299 if (IS(cast, "(s16)") && val < 0x8000)
1301 if (IS(cast, "(s32)") && val < 0x80000000)
1307 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1316 namelen = strlen(name);
1318 p = strchr(name, '+');
1322 ferr(po, "equ parse failed for '%s'\n", name);
1324 if (IS_START(p, "0x"))
1326 *extra_offs = strtol(p, &endp, 16);
1328 ferr(po, "equ parse failed for '%s'\n", name);
1331 for (i = 0; i < g_eqcnt; i++)
1332 if (strncmp(g_eqs[i].name, name, namelen) == 0
1333 && g_eqs[i].name[namelen] == 0)
1337 ferr(po, "unresolved equ name: '%s'\n", name);
1344 static int is_stack_access(struct parsed_op *po,
1345 const struct parsed_opr *popr)
1347 return (parse_stack_el(popr->name, NULL, 0)
1348 || (g_bp_frame && !(po->flags & OPF_EBP_S)
1349 && IS_START(popr->name, "ebp")));
1352 static void parse_stack_access(struct parsed_op *po,
1353 const char *name, char *ofs_reg, int *offset_out,
1354 int *stack_ra_out, const char **bp_arg_out, int is_lea)
1356 const char *bp_arg = "";
1357 const char *p = NULL;
1358 struct parsed_equ *eq;
1365 if (IS_START(name, "ebp-")
1366 || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1369 if (IS_START(p, "0x"))
1371 offset = strtoul(p, &endp, 16);
1375 ferr(po, "ebp- parse of '%s' failed\n", name);
1378 bp_arg = parse_stack_el(name, ofs_reg, 0);
1379 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1380 eq = equ_find(po, bp_arg, &offset);
1382 ferr(po, "detected but missing eq\n");
1383 offset += eq->offset;
1386 if (!strncmp(name, "ebp", 3))
1389 // yes it sometimes LEAs ra for compares..
1390 if (!is_lea && ofs_reg[0] == 0
1391 && stack_ra <= offset && offset < stack_ra + 4)
1393 ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
1396 *offset_out = offset;
1397 *stack_ra_out = stack_ra;
1399 *bp_arg_out = bp_arg;
1402 static int stack_frame_access(struct parsed_op *po,
1403 struct parsed_opr *popr, char *buf, size_t buf_size,
1404 const char *name, const char *cast, int is_src, int is_lea)
1406 enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1407 const char *prefix = "";
1408 const char *bp_arg = NULL;
1409 char ofs_reg[16] = { 0, };
1410 int i, arg_i, arg_s;
1418 if (po->flags & OPF_EBP_S)
1419 ferr(po, "stack_frame_access while ebp is scratch\n");
1421 parse_stack_access(po, name, ofs_reg, &offset,
1422 &stack_ra, &bp_arg, is_lea);
1424 if (offset > stack_ra)
1426 arg_i = (offset - stack_ra - 4) / 4;
1427 if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
1429 if (g_func_pp->is_vararg
1430 && arg_i == g_func_pp->argc_stack && is_lea)
1432 // should be va_list
1435 snprintf(buf, buf_size, "%sap", cast);
1438 ferr(po, "offset %d (%s,%d) doesn't map to any arg\n",
1439 offset, bp_arg, arg_i);
1441 if (ofs_reg[0] != 0)
1442 ferr(po, "offset reg on arg access?\n");
1444 for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1445 if (g_func_pp->arg[i].reg != NULL)
1451 if (i == g_func_pp->argc)
1452 ferr(po, "arg %d not in prototype?\n", arg_i);
1454 popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
1461 ferr(po, "lea/byte to arg?\n");
1462 if (is_src && (offset & 3) == 0)
1463 snprintf(buf, buf_size, "%sa%d",
1464 simplify_cast(cast, "(u8)"), i + 1);
1466 snprintf(buf, buf_size, "%sBYTE%d(a%d)",
1467 cast, offset & 3, i + 1);
1472 ferr(po, "lea/word to arg?\n");
1477 ferr(po, "problematic arg store\n");
1478 snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
1479 simplify_cast(cast, "*(u16 *)"), i + 1);
1482 ferr(po, "unaligned arg word load\n");
1484 else if (is_src && (offset & 2) == 0)
1485 snprintf(buf, buf_size, "%sa%d",
1486 simplify_cast(cast, "(u16)"), i + 1);
1488 snprintf(buf, buf_size, "%s%sWORD(a%d)",
1489 cast, (offset & 2) ? "HI" : "LO", i + 1);
1501 snprintf(buf, buf_size, "(u32)&a%d + %d",
1504 ferr(po, "unaligned arg store\n");
1506 // mov edx, [ebp+arg_4+2]; movsx ecx, dx
1507 snprintf(buf, buf_size, "%s(a%d >> %d)",
1508 prefix, i + 1, (offset & 3) * 8);
1512 snprintf(buf, buf_size, "%s%sa%d",
1513 prefix, is_lea ? "&" : "", i + 1);
1518 ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
1522 snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
1525 guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
1526 if (tmp_lmod != OPLM_DWORD
1527 && (unaligned || (!is_src && lmod_bytes(po, tmp_lmod)
1528 < lmod_bytes(po, popr->lmod) + (offset & 3))))
1530 ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
1531 i + 1, offset, g_func_pp->arg[i].type.name);
1533 // can't check this because msvc likes to reuse
1534 // arg space for scratch..
1535 //if (popr->is_ptr && popr->lmod != OPLM_DWORD)
1536 // ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
1540 if (g_stack_fsz == 0)
1541 ferr(po, "stack var access without stackframe\n");
1542 g_stack_frame_used = 1;
1544 sf_ofs = g_stack_fsz + offset;
1545 lim = (ofs_reg[0] != 0) ? -4 : 0;
1546 if (offset > 0 || sf_ofs < lim)
1547 ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
1557 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1558 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1562 if ((sf_ofs & 1) || ofs_reg[0] != 0) {
1563 // known unaligned or possibly unaligned
1564 strcat(g_comment, " unaligned");
1566 prefix = "*(u16 *)&";
1567 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1568 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1571 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
1575 if ((sf_ofs & 3) || ofs_reg[0] != 0) {
1576 // known unaligned or possibly unaligned
1577 strcat(g_comment, " unaligned");
1579 prefix = "*(u32 *)&";
1580 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1581 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1584 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
1588 ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
1595 static void check_func_pp(struct parsed_op *po,
1596 const struct parsed_proto *pp, const char *pfx)
1598 enum opr_lenmod tmp_lmod;
1602 if (pp->argc_reg != 0) {
1603 if (/*!g_allow_regfunc &&*/ !pp->is_fastcall) {
1604 pp_print(buf, sizeof(buf), pp);
1605 ferr(po, "%s: unexpected reg arg in icall: %s\n", pfx, buf);
1607 if (pp->argc_stack > 0 && pp->argc_reg != 2)
1608 ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
1609 pfx, pp->argc_reg, pp->argc_stack);
1612 // fptrs must use 32bit args, callsite might have no information and
1613 // lack a cast to smaller types, which results in incorrectly masked
1614 // args passed (callee may assume masked args, it does on ARM)
1615 if (!pp->is_oslib) {
1616 for (i = 0; i < pp->argc; i++) {
1617 ret = guess_lmod_from_c_type(&tmp_lmod, &pp->arg[i].type);
1618 if (ret && tmp_lmod != OPLM_DWORD)
1619 ferr(po, "reference to %s with arg%d '%s'\n", pp->name,
1620 i + 1, pp->arg[i].type.name);
1625 static const char *check_label_read_ref(struct parsed_op *po,
1628 const struct parsed_proto *pp;
1630 pp = proto_parse(g_fhdr, name, 0);
1632 ferr(po, "proto_parse failed for ref '%s'\n", name);
1635 check_func_pp(po, pp, "ref");
1640 static char *out_src_opr(char *buf, size_t buf_size,
1641 struct parsed_op *po, struct parsed_opr *popr, const char *cast,
1644 char tmp1[256], tmp2[256];
1653 switch (popr->type) {
1656 ferr(po, "lea from reg?\n");
1658 switch (popr->lmod) {
1660 snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
1663 snprintf(buf, buf_size, "%s%s",
1664 simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
1667 if (popr->name[1] == 'h') // XXX..
1668 snprintf(buf, buf_size, "%s(%s >> 8)",
1669 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1671 snprintf(buf, buf_size, "%s%s",
1672 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1675 ferr(po, "invalid src lmod: %d\n", popr->lmod);
1680 if (is_stack_access(po, popr)) {
1681 stack_frame_access(po, popr, buf, buf_size,
1682 popr->name, cast, 1, is_lea);
1686 strcpy(expr, popr->name);
1687 if (strchr(expr, '[')) {
1688 // special case: '[' can only be left for label[reg] form
1689 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
1691 ferr(po, "parse failure for '%s'\n", expr);
1692 if (tmp1[0] == '(') {
1693 // (off_4FFF50+3)[eax]
1694 p = strchr(tmp1 + 1, ')');
1695 if (p == NULL || p[1] != 0)
1696 ferr(po, "parse failure (2) for '%s'\n", expr);
1698 memmove(tmp1, tmp1 + 1, strlen(tmp1));
1700 snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
1703 // XXX: do we need more parsing?
1705 snprintf(buf, buf_size, "%s", expr);
1709 snprintf(buf, buf_size, "%s(%s)",
1710 simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
1714 name = check_label_read_ref(po, popr->name);
1715 if (cast[0] == 0 && popr->is_ptr)
1719 snprintf(buf, buf_size, "(u32)&%s", name);
1720 else if (popr->size_lt)
1721 snprintf(buf, buf_size, "%s%s%s%s", cast,
1722 lmod_cast_u_ptr(po, popr->lmod),
1723 popr->is_array ? "" : "&", name);
1725 snprintf(buf, buf_size, "%s%s%s", cast, name,
1726 popr->is_array ? "[0]" : "");
1730 name = check_label_read_ref(po, popr->name);
1734 ferr(po, "lea an offset?\n");
1735 snprintf(buf, buf_size, "%s&%s", cast, name);
1740 ferr(po, "lea from const?\n");
1742 printf_number(tmp1, sizeof(tmp1), popr->val);
1743 if (popr->val == 0 && strchr(cast, '*'))
1744 snprintf(buf, buf_size, "NULL");
1746 snprintf(buf, buf_size, "%s%s",
1747 simplify_cast_num(cast, popr->val), tmp1);
1751 ferr(po, "invalid src type: %d\n", popr->type);
1757 // note: may set is_ptr (we find that out late for ebp frame..)
1758 static char *out_dst_opr(char *buf, size_t buf_size,
1759 struct parsed_op *po, struct parsed_opr *popr)
1761 switch (popr->type) {
1763 switch (popr->lmod) {
1765 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
1769 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
1773 if (popr->name[1] == 'h') // XXX..
1774 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
1776 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
1779 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
1784 if (is_stack_access(po, popr)) {
1785 stack_frame_access(po, popr, buf, buf_size,
1786 popr->name, "", 0, 0);
1790 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1793 if (popr->size_mismatch)
1794 snprintf(buf, buf_size, "%s%s%s",
1795 lmod_cast_u_ptr(po, popr->lmod),
1796 popr->is_array ? "" : "&", popr->name);
1798 snprintf(buf, buf_size, "%s%s", popr->name,
1799 popr->is_array ? "[0]" : "");
1803 ferr(po, "invalid dst type: %d\n", popr->type);
1809 static char *out_src_opr_u32(char *buf, size_t buf_size,
1810 struct parsed_op *po, struct parsed_opr *popr)
1812 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1815 static void out_test_for_cc(char *buf, size_t buf_size,
1816 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1817 enum opr_lenmod lmod, const char *expr)
1819 const char *cast, *scast;
1821 cast = lmod_cast_u(po, lmod);
1822 scast = lmod_cast_s(po, lmod);
1826 case PFO_BE: // CF=1||ZF=1; CF=0
1827 snprintf(buf, buf_size, "(%s%s %s 0)",
1828 cast, expr, is_inv ? "!=" : "==");
1832 case PFO_L: // SF!=OF; OF=0
1833 snprintf(buf, buf_size, "(%s%s %s 0)",
1834 scast, expr, is_inv ? ">=" : "<");
1837 case PFO_LE: // ZF=1||SF!=OF; OF=0
1838 snprintf(buf, buf_size, "(%s%s %s 0)",
1839 scast, expr, is_inv ? ">" : "<=");
1843 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1847 static void out_cmp_for_cc(char *buf, size_t buf_size,
1848 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1850 const char *cast, *scast, *cast_use;
1851 char buf1[256], buf2[256];
1852 enum opr_lenmod lmod;
1854 if (po->operand[0].lmod != po->operand[1].lmod)
1855 ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
1856 po->operand[0].lmod, po->operand[1].lmod);
1857 lmod = po->operand[0].lmod;
1859 cast = lmod_cast_u(po, lmod);
1860 scast = lmod_cast_s(po, lmod);
1876 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1879 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
1880 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
1884 // note: must be unsigned compare
1885 snprintf(buf, buf_size, "(%s %s %s)",
1886 buf1, is_inv ? ">=" : "<", buf2);
1890 snprintf(buf, buf_size, "(%s %s %s)",
1891 buf1, is_inv ? "!=" : "==", buf2);
1895 // note: must be unsigned compare
1896 snprintf(buf, buf_size, "(%s %s %s)",
1897 buf1, is_inv ? ">" : "<=", buf2);
1900 if (is_inv && lmod == OPLM_BYTE
1901 && po->operand[1].type == OPT_CONST
1902 && po->operand[1].val == 0xff)
1904 snprintf(g_comment, sizeof(g_comment), "if %s", buf);
1905 snprintf(buf, buf_size, "(0)");
1909 // note: must be signed compare
1911 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
1912 scast, buf1, buf2, is_inv ? ">=" : "<");
1916 snprintf(buf, buf_size, "(%s %s %s)",
1917 buf1, is_inv ? ">=" : "<", buf2);
1921 snprintf(buf, buf_size, "(%s %s %s)",
1922 buf1, is_inv ? ">" : "<=", buf2);
1930 static void out_cmp_test(char *buf, size_t buf_size,
1931 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1933 char buf1[256], buf2[256], buf3[256];
1935 if (po->op == OP_TEST) {
1936 if (IS(opr_name(po, 0), opr_name(po, 1))) {
1937 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
1940 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
1941 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
1942 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
1944 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
1945 po->operand[0].lmod, buf3);
1947 else if (po->op == OP_CMP) {
1948 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
1951 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
1954 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
1955 struct parsed_opr *popr2)
1957 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
1958 ferr(po, "missing lmod for both operands\n");
1960 if (popr1->lmod == OPLM_UNSPEC)
1961 popr1->lmod = popr2->lmod;
1962 else if (popr2->lmod == OPLM_UNSPEC)
1963 popr2->lmod = popr1->lmod;
1964 else if (popr1->lmod != popr2->lmod) {
1965 if (popr1->type_from_var) {
1966 popr1->size_mismatch = 1;
1967 if (popr1->lmod < popr2->lmod)
1969 popr1->lmod = popr2->lmod;
1971 else if (popr2->type_from_var) {
1972 popr2->size_mismatch = 1;
1973 if (popr2->lmod < popr1->lmod)
1975 popr2->lmod = popr1->lmod;
1978 ferr(po, "conflicting lmods: %d vs %d\n",
1979 popr1->lmod, popr2->lmod);
1983 static const char *op_to_c(struct parsed_op *po)
2007 ferr(po, "op_to_c was supplied with %d\n", po->op);
2011 static void op_set_clear_flag(struct parsed_op *po,
2012 enum op_flags flag_set, enum op_flags flag_clear)
2014 po->flags |= flag_set;
2015 po->flags &= ~flag_clear;
2018 // last op in stream - unconditional branch or ret
2019 #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
2020 || ((ops[_i].flags & (OPF_JMP|OPF_CJMP|OPF_RMD)) == OPF_JMP \
2021 && ops[_i].op != OP_CALL))
2023 static int scan_for_pop(int i, int opcnt, const char *reg,
2024 int magic, int depth, int *maxdepth, int do_flags)
2026 const struct parsed_proto *pp;
2027 struct parsed_op *po;
2031 for (; i < opcnt; i++) {
2033 if (po->cc_scratch == magic)
2034 break; // already checked
2035 po->cc_scratch = magic;
2037 if (po->flags & OPF_TAIL) {
2038 if (po->op == OP_CALL) {
2039 pp = proto_parse(g_fhdr, po->operand[0].name, 0);
2040 if (pp != NULL && pp->is_noreturn)
2041 // no stack cleanup for noreturn
2044 return -1; // deadend
2047 if ((po->flags & OPF_RMD)
2048 || (po->op == OP_PUSH && po->p_argnum != 0)) // arg push
2051 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2052 if (po->btj != NULL) {
2054 for (j = 0; j < po->btj->count; j++) {
2055 ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic,
2056 depth, maxdepth, do_flags);
2058 return ret; // dead end
2064 ferr(po, "dead branch\n");
2068 if (po->flags & OPF_CJMP) {
2069 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic,
2070 depth, maxdepth, do_flags);
2072 return ret; // dead end
2080 if ((po->op == OP_POP || po->op == OP_PUSH)
2081 && po->operand[0].type == OPT_REG
2082 && IS(po->operand[0].name, reg))
2084 if (po->op == OP_PUSH && !(po->flags & OPF_FARG)) {
2086 if (depth > *maxdepth)
2089 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2091 else if (po->op == OP_POP) {
2094 op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
2099 if (depth < 0) // should not happen
2100 ferr(po, "fail with depth\n");
2102 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2111 // scan for pop starting from 'ret' op (all paths)
2112 static int scan_for_pop_ret(int i, int opcnt, const char *reg,
2118 for (; i < opcnt; i++) {
2119 if (!(ops[i].flags & OPF_TAIL))
2122 for (j = i - 1; j >= 0; j--) {
2123 if (ops[j].flags & OPF_RMD)
2125 if (ops[j].flags & OPF_JMP)
2128 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
2129 && IS(ops[j].operand[0].name, reg))
2132 ops[j].flags |= flag_set;
2136 if (g_labels[j][0] != 0)
2141 return found ? 0 : -1;
2144 static void scan_propagate_df(int i, int opcnt)
2146 struct parsed_op *po = &ops[i];
2149 for (; i < opcnt; i++) {
2151 if (po->flags & OPF_DF)
2152 return; // already resolved
2153 po->flags |= OPF_DF;
2155 if (po->op == OP_CALL)
2156 ferr(po, "call with DF set?\n");
2158 if (po->flags & OPF_JMP) {
2159 if (po->btj != NULL) {
2161 for (j = 0; j < po->btj->count; j++)
2162 scan_propagate_df(po->btj->d[j].bt_i, opcnt);
2167 ferr(po, "dead branch\n");
2171 if (po->flags & OPF_CJMP)
2172 scan_propagate_df(po->bt_i, opcnt);
2178 if (po->flags & OPF_TAIL)
2181 if (po->op == OP_CLD) {
2182 po->flags |= OPF_RMD;
2187 ferr(po, "missing DF clear?\n");
2190 // is operand 'opr' modified by parsed_op 'po'?
2191 static int is_opr_modified(const struct parsed_opr *opr,
2192 const struct parsed_op *po)
2196 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2199 if (opr->type == OPT_REG) {
2200 if (po->op == OP_CALL) {
2201 mask = (1 << xAX) | (1 << xCX) | (1 << xDX);
2202 if ((1 << opr->reg) & mask)
2208 if (po->operand[0].type == OPT_REG) {
2209 if (po->regmask_dst & (1 << opr->reg))
2216 return IS(po->operand[0].name, opr->name);
2219 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
2220 static int is_any_opr_modified(const struct parsed_op *po_test,
2221 const struct parsed_op *po, int c_mode)
2226 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2229 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2232 if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
2235 // in reality, it can wreck any register, but in decompiled C
2236 // version it can only overwrite eax or edx:eax
2237 mask = (1 << xAX) | (1 << xDX);
2241 if (po->op == OP_CALL
2242 && ((po_test->regmask_src | po_test->regmask_dst) & mask))
2245 for (i = 0; i < po_test->operand_cnt; i++)
2246 if (IS(po_test->operand[i].name, po->operand[0].name))
2252 // scan for any po_test operand modification in range given
2253 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
2256 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2259 for (; i < opcnt; i++) {
2260 if (is_any_opr_modified(po_test, &ops[i], c_mode))
2267 // scan for po_test operand[0] modification in range given
2268 static int scan_for_mod_opr0(struct parsed_op *po_test,
2271 for (; i < opcnt; i++) {
2272 if (is_opr_modified(&po_test->operand[0], &ops[i]))
2279 static int scan_for_flag_set(int i, int magic, int *branched,
2280 int *setters, int *setter_cnt)
2282 struct label_ref *lr;
2286 if (ops[i].cc_scratch == magic) {
2287 ferr(&ops[i], "%s looped\n", __func__);
2290 ops[i].cc_scratch = magic;
2292 if (g_labels[i][0] != 0) {
2295 lr = &g_label_refs[i];
2296 for (; lr->next; lr = lr->next) {
2297 ret = scan_for_flag_set(lr->i, magic,
2298 branched, setters, setter_cnt);
2303 if (i > 0 && LAST_OP(i - 1)) {
2307 ret = scan_for_flag_set(lr->i, magic,
2308 branched, setters, setter_cnt);
2314 if (ops[i].flags & OPF_FLAGS) {
2315 setters[*setter_cnt] = i;
2320 if ((ops[i].flags & (OPF_JMP|OPF_CJMP)) == OPF_JMP)
2327 // scan back for cdq, if anything modifies edx, fail
2328 static int scan_for_cdq_edx(int i)
2331 if (g_labels[i][0] != 0) {
2332 if (g_label_refs[i].next != NULL)
2334 if (i > 0 && LAST_OP(i - 1)) {
2335 i = g_label_refs[i].i;
2342 if (ops[i].op == OP_CDQ)
2345 if (ops[i].regmask_dst & (1 << xDX))
2352 static int scan_for_reg_clear(int i, int reg)
2355 if (g_labels[i][0] != 0) {
2356 if (g_label_refs[i].next != NULL)
2358 if (i > 0 && LAST_OP(i - 1)) {
2359 i = g_label_refs[i].i;
2366 if (ops[i].op == OP_XOR
2367 && ops[i].operand[0].lmod == OPLM_DWORD
2368 && ops[i].operand[0].reg == ops[i].operand[1].reg
2369 && ops[i].operand[0].reg == reg)
2372 if (ops[i].regmask_dst & (1 << reg))
2379 // scan for positive, constant esp adjust
2380 static int scan_for_esp_adjust(int i, int opcnt, int *adj,
2383 struct parsed_op *po;
2386 *adj = *multipath = 0;
2388 for (; i < opcnt; i++) {
2391 if (g_labels[i][0] != 0)
2394 if (po->op == OP_ADD && po->operand[0].reg == xSP) {
2395 if (po->operand[1].type != OPT_CONST)
2396 ferr(&ops[i], "non-const esp adjust?\n");
2397 *adj += po->operand[1].val;
2399 ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
2402 else if (po->op == OP_PUSH && !(po->flags & OPF_RMD)) {
2403 //if (first_pop == -1)
2404 // first_pop = -2; // none
2405 *adj -= lmod_bytes(po, po->operand[0].lmod);
2407 else if (po->op == OP_POP && !(po->flags & OPF_RMD)) {
2408 // seems like msvc only uses 'pop ecx' for stack realignment..
2409 if (po->operand[0].type != OPT_REG || po->operand[0].reg != xCX)
2411 if (first_pop == -1 && *adj >= 0)
2413 *adj += lmod_bytes(po, po->operand[0].lmod);
2415 else if (po->flags & (OPF_JMP|OPF_TAIL)) {
2416 if (po->op == OP_JMP && po->btj == NULL) {
2420 if (po->op != OP_CALL)
2422 if (po->operand[0].type != OPT_LABEL)
2424 if (po->pp != NULL && po->pp->is_stdcall)
2429 if (first_pop >= 0) {
2430 // probably 'pop ecx' was used..
2437 static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
2439 struct parsed_op *po;
2443 ferr(ops, "%s: followed bad branch?\n", __func__);
2445 for (; i < opcnt; i++) {
2447 if (po->cc_scratch == magic)
2449 po->cc_scratch = magic;
2452 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2453 if (po->btj != NULL) {
2455 for (j = 0; j < po->btj->count; j++)
2456 scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
2460 scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
2461 if (!(po->flags & OPF_CJMP))
2464 if (po->flags & OPF_TAIL)
2469 static const struct parsed_proto *try_recover_pp(
2470 struct parsed_op *po, const struct parsed_opr *opr, int *search_instead)
2472 const struct parsed_proto *pp = NULL;
2476 // maybe an arg of g_func?
2477 if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
2479 char ofs_reg[16] = { 0, };
2480 int arg, arg_s, arg_i;
2484 parse_stack_access(po, opr->name, ofs_reg,
2485 &offset, &stack_ra, NULL, 0);
2486 if (ofs_reg[0] != 0)
2487 ferr(po, "offset reg on arg access?\n");
2488 if (offset <= stack_ra) {
2489 // search who set the stack var instead
2490 if (search_instead != NULL)
2491 *search_instead = 1;
2495 arg_i = (offset - stack_ra - 4) / 4;
2496 for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
2497 if (g_func_pp->arg[arg].reg != NULL)
2503 if (arg == g_func_pp->argc)
2504 ferr(po, "stack arg %d not in prototype?\n", arg_i);
2506 pp = g_func_pp->arg[arg].fptr;
2508 ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1);
2509 check_func_pp(po, pp, "icall arg");
2511 else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
2513 p = strchr(opr->name + 1, '[');
2514 memcpy(buf, opr->name, p - opr->name);
2515 buf[p - opr->name] = 0;
2516 pp = proto_parse(g_fhdr, buf, 0);
2518 else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
2519 pp = proto_parse(g_fhdr, opr->name, 0);
2521 ferr(po, "proto_parse failed for icall from '%s'\n", opr->name);
2522 check_func_pp(po, pp, "reg-fptr ref");
2528 static void scan_for_call_type(int i, const struct parsed_opr *opr,
2529 int magic, const struct parsed_proto **pp_found, int *multi)
2531 const struct parsed_proto *pp = NULL;
2532 struct parsed_op *po;
2533 struct label_ref *lr;
2535 ops[i].cc_scratch = magic;
2538 if (g_labels[i][0] != 0) {
2539 lr = &g_label_refs[i];
2540 for (; lr != NULL; lr = lr->next)
2541 scan_for_call_type(lr->i, opr, magic, pp_found, multi);
2542 if (i > 0 && LAST_OP(i - 1))
2550 if (ops[i].cc_scratch == magic)
2552 ops[i].cc_scratch = magic;
2554 if (!(ops[i].flags & OPF_DATA))
2556 if (!is_opr_modified(opr, &ops[i]))
2558 if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
2559 // most probably trashed by some processing
2564 opr = &ops[i].operand[1];
2565 if (opr->type != OPT_REG)
2569 po = (i >= 0) ? &ops[i] : ops;
2572 // reached the top - can only be an arg-reg
2573 if (opr->type != OPT_REG)
2576 for (i = 0; i < g_func_pp->argc; i++) {
2577 if (g_func_pp->arg[i].reg == NULL)
2579 if (IS(opr->name, g_func_pp->arg[i].reg))
2582 if (i == g_func_pp->argc)
2584 pp = g_func_pp->arg[i].fptr;
2586 ferr(po, "icall: arg%d (%s) is not a fptr?\n",
2587 i + 1, g_func_pp->arg[i].reg);
2588 check_func_pp(po, pp, "icall reg-arg");
2591 pp = try_recover_pp(po, opr, NULL);
2593 if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
2594 if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
2595 || (*pp_found)->is_stdcall != pp->is_stdcall
2596 || (*pp_found)->is_fptr != pp->is_fptr
2597 || (*pp_found)->argc != pp->argc
2598 || (*pp_found)->argc_reg != pp->argc_reg
2599 || (*pp_found)->argc_stack != pp->argc_stack)
2601 ferr(po, "icall: parsed_proto mismatch\n");
2609 static const struct parsed_proto *resolve_icall(int i, int opcnt,
2612 const struct parsed_proto *pp = NULL;
2613 int search_advice = 0;
2617 switch (ops[i].operand[0].type) {
2621 pp = try_recover_pp(&ops[i], &ops[i].operand[0], &search_advice);
2626 scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
2634 // find an instruction that changed opr before i op
2635 // *op_i must be set to -1
2636 static int resolve_origin(int i, const struct parsed_opr *opr,
2637 int magic, int *op_i)
2639 struct label_ref *lr;
2642 ops[i].cc_scratch = magic;
2645 if (g_labels[i][0] != 0) {
2646 lr = &g_label_refs[i];
2647 for (; lr != NULL; lr = lr->next)
2648 ret |= resolve_origin(lr->i, opr, magic, op_i);
2649 if (i > 0 && LAST_OP(i - 1))
2657 if (ops[i].cc_scratch == magic)
2659 ops[i].cc_scratch = magic;
2661 if (!(ops[i].flags & OPF_DATA))
2663 if (!is_opr_modified(opr, &ops[i]))
2669 // XXX: could check if the other op does the same
2678 static int try_resolve_const(int i, const struct parsed_opr *opr,
2679 int magic, unsigned int *val)
2684 ret = resolve_origin(i, opr, magic, &s_i);
2687 if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST)
2690 *val = ops[i].operand[1].val;
2697 static int collect_call_args_r(struct parsed_op *po, int i,
2698 struct parsed_proto *pp, int *regmask, int *save_arg_vars, int arg,
2699 int magic, int need_op_saving, int may_reuse)
2701 struct parsed_proto *pp_tmp;
2702 struct label_ref *lr;
2703 int need_to_save_current;
2711 ferr(po, "dead label encountered\n");
2715 for (; arg < pp->argc; arg++)
2716 if (pp->arg[arg].reg == NULL)
2718 magic = (magic & 0xffffff) | (arg << 24);
2720 for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
2722 if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
2723 if (ops[j].cc_scratch != magic) {
2724 ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
2728 // ok: have already been here
2731 ops[j].cc_scratch = magic;
2733 if (g_labels[j][0] != 0 && g_label_refs[j].i != -1) {
2734 lr = &g_label_refs[j];
2735 if (lr->next != NULL)
2737 for (; lr->next; lr = lr->next) {
2738 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
2740 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2741 arg, magic, need_op_saving, may_reuse);
2746 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
2748 if (j > 0 && LAST_OP(j - 1)) {
2749 // follow last branch in reverse
2754 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2755 arg, magic, need_op_saving, may_reuse);
2761 if (ops[j].op == OP_CALL)
2763 if (pp->is_unresolved)
2768 ferr(po, "arg collect hit unparsed call '%s'\n",
2769 ops[j].operand[0].name);
2770 if (may_reuse && pp_tmp->argc_stack > 0)
2771 ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
2772 arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
2774 // esp adjust of 0 means we collected it before
2775 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP
2776 && (ops[j].operand[1].type != OPT_CONST
2777 || ops[j].operand[1].val != 0))
2779 if (pp->is_unresolved)
2782 ferr(po, "arg collect %d/%d hit esp adjust of %d\n",
2783 arg, pp->argc, ops[j].operand[1].val);
2785 else if (ops[j].op == OP_POP) {
2786 if (pp->is_unresolved)
2789 ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc);
2791 else if (ops[j].flags & OPF_CJMP)
2793 if (pp->is_unresolved)
2798 else if (ops[j].op == OP_PUSH && !(ops[j].flags & OPF_FARG))
2800 if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
2803 pp->arg[arg].datap = &ops[j];
2804 need_to_save_current = 0;
2807 if (ops[j].operand[0].type == OPT_REG)
2808 reg = ops[j].operand[0].reg;
2810 if (!need_op_saving) {
2811 ret = scan_for_mod(&ops[j], j + 1, i, 1);
2812 need_to_save_current = (ret >= 0);
2814 if (need_op_saving || need_to_save_current) {
2815 // mark this push as one that needs operand saving
2816 ops[j].flags &= ~OPF_RMD;
2817 if (ops[j].p_argnum == 0) {
2818 ops[j].p_argnum = arg + 1;
2819 save_args |= 1 << arg;
2821 else if (ops[j].p_argnum < arg + 1) {
2822 // XXX: might kill valid var..
2823 //*save_arg_vars &= ~(1 << (ops[j].p_argnum - 1));
2824 ops[j].p_argnum = arg + 1;
2825 save_args |= 1 << arg;
2828 else if (ops[j].p_argnum == 0)
2829 ops[j].flags |= OPF_RMD;
2831 // some PUSHes are reused by different calls on other branches,
2832 // but that can't happen if we didn't branch, so they
2833 // can be removed from future searches (handles nested calls)
2835 ops[j].flags |= OPF_FARG;
2837 ops[j].flags &= ~OPF_RSAVE;
2839 // check for __VALIST
2840 if (!pp->is_unresolved && pp->arg[arg].type.is_va_list) {
2842 ret = resolve_origin(j, &ops[j].operand[0], magic + 1, &k);
2843 if (ret == 1 && k >= 0)
2845 if (ops[k].op == OP_LEA) {
2846 snprintf(buf, sizeof(buf), "arg_%X",
2847 g_func_pp->argc_stack * 4);
2848 if (!g_func_pp->is_vararg
2849 || strstr(ops[k].operand[1].name, buf))
2851 ops[k].flags |= OPF_RMD;
2852 ops[j].flags |= OPF_RMD | OPF_VAPUSH;
2853 save_args &= ~(1 << arg);
2857 ferr(&ops[j], "lea va_list used, but no vararg?\n");
2859 // check for va_list from g_func_pp arg too
2860 else if (ops[k].op == OP_MOV
2861 && is_stack_access(&ops[k], &ops[k].operand[1]))
2863 ret = stack_frame_access(&ops[k], &ops[k].operand[1],
2864 buf, sizeof(buf), ops[k].operand[1].name, "", 1, 0);
2866 ops[k].flags |= OPF_RMD;
2867 ops[j].flags |= OPF_RMD;
2868 ops[j].p_argpass = ret + 1;
2869 save_args &= ~(1 << arg);
2876 *save_arg_vars |= save_args;
2878 // tracking reg usage
2880 *regmask |= 1 << reg;
2883 if (!pp->is_unresolved) {
2885 for (; arg < pp->argc; arg++)
2886 if (pp->arg[arg].reg == NULL)
2889 magic = (magic & 0xffffff) | (arg << 24);
2893 if (arg < pp->argc) {
2894 ferr(po, "arg collect failed for '%s': %d/%d\n",
2895 pp->name, arg, pp->argc);
2902 static int collect_call_args(struct parsed_op *po, int i,
2903 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
2909 ret = collect_call_args_r(po, i, pp, regmask, save_arg_vars,
2914 if (pp->is_unresolved) {
2916 pp->argc_stack += ret;
2917 for (a = 0; a < pp->argc; a++)
2918 if (pp->arg[a].type.name == NULL)
2919 pp->arg[a].type.name = strdup("int");
2925 // early check for tail call or branch back
2926 static int is_like_tailjmp(int j)
2928 if (!(ops[j].flags & OPF_JMP))
2931 if (ops[j].op == OP_JMP && !ops[j].operand[0].had_ds)
2932 // probably local branch back..
2934 if (ops[j].op == OP_CALL)
2935 // probably noreturn call..
2941 static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
2945 for (i = 0; i < pp->argc; i++)
2946 if (pp->arg[i].reg == NULL)
2950 memmove(&pp->arg[i + 1], &pp->arg[i],
2951 sizeof(pp->arg[0]) * pp->argc_stack);
2952 memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
2953 pp->arg[i].reg = strdup(reg);
2954 pp->arg[i].type.name = strdup("int");
2959 static void add_label_ref(struct label_ref *lr, int op_i)
2961 struct label_ref *lr_new;
2968 lr_new = calloc(1, sizeof(*lr_new));
2970 lr_new->next = lr->next;
2974 static void output_std_flags(FILE *fout, struct parsed_op *po,
2975 int *pfomask, const char *dst_opr_text)
2977 if (*pfomask & (1 << PFO_Z)) {
2978 fprintf(fout, "\n cond_z = (%s%s == 0);",
2979 lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
2980 *pfomask &= ~(1 << PFO_Z);
2982 if (*pfomask & (1 << PFO_S)) {
2983 fprintf(fout, "\n cond_s = (%s%s < 0);",
2984 lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
2985 *pfomask &= ~(1 << PFO_S);
2989 static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp,
2992 if (pp->is_fastcall)
2993 fprintf(fout, "__fastcall ");
2994 else if (pp->is_stdcall && pp->argc_reg == 0)
2995 fprintf(fout, "__stdcall ");
2996 if (pp->is_noreturn || is_noreturn)
2997 fprintf(fout, "noreturn ");
3000 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
3002 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
3003 struct parsed_opr *last_arith_dst = NULL;
3004 char buf1[256], buf2[256], buf3[256], cast[64];
3005 const struct parsed_proto *pp_c;
3006 struct parsed_proto *pp, *pp_tmp;
3007 struct parsed_data *pd;
3008 const char *tmpname;
3010 int save_arg_vars = 0;
3012 int need_tmp_var = 0;
3015 int label_pending = 0;
3016 int regmask_save = 0;
3017 int regmask_arg = 0;
3018 int regmask_now = 0;
3019 int regmask_init = 0;
3030 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
3031 g_stack_frame_used = 0;
3033 g_func_pp = proto_parse(fhdr, funcn, 0);
3034 if (g_func_pp == NULL)
3035 ferr(ops, "proto_parse failed for '%s'\n", funcn);
3037 for (i = 0; i < g_func_pp->argc; i++) {
3038 if (g_func_pp->arg[i].reg != NULL) {
3039 reg = char_array_i(regs_r32,
3040 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
3042 ferr(ops, "arg '%s' is not a reg?\n", g_func_pp->arg[i].reg);
3043 regmask_arg |= 1 << reg;
3048 // - handle ebp/esp frame, remove ops related to it
3049 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
3050 && ops[1].op == OP_MOV
3051 && IS(opr_name(&ops[1], 0), "ebp")
3052 && IS(opr_name(&ops[1], 1), "esp"))
3057 ops[0].flags |= OPF_RMD;
3058 ops[1].flags |= OPF_RMD;
3061 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
3062 g_stack_fsz = opr_const(&ops[2], 1);
3063 ops[2].flags |= OPF_RMD;
3067 // another way msvc builds stack frame..
3069 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
3071 ops[i].flags |= OPF_RMD;
3075 // and another way..
3076 if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
3077 && ops[i].operand[1].type == OPT_CONST
3078 && ops[i + 1].op == OP_CALL
3079 && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
3081 g_stack_fsz += ops[i].operand[1].val;
3082 ops[i].flags |= OPF_RMD;
3084 ops[i].flags |= OPF_RMD;
3091 for (; i < opcnt; i++)
3092 if (ops[i].op == OP_RET)
3095 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
3096 if (found && is_like_tailjmp(j))
3101 if ((ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp"))
3102 || ops[j].op == OP_LEAVE)
3104 ops[j].flags |= OPF_RMD;
3106 else if (!(g_ida_func_attr & IDAFA_NORETURN))
3107 ferr(&ops[j], "'pop ebp' expected\n");
3109 if (g_stack_fsz != 0) {
3110 if (ops[j - 1].op == OP_MOV
3111 && IS(opr_name(&ops[j - 1], 0), "esp")
3112 && IS(opr_name(&ops[j - 1], 1), "ebp"))
3114 ops[j - 1].flags |= OPF_RMD;
3116 else if (ops[j].op != OP_LEAVE
3117 && !(g_ida_func_attr & IDAFA_NORETURN))
3119 ferr(&ops[j - 1], "esp restore expected\n");
3122 if (ecx_push && ops[j - 2].op == OP_POP
3123 && IS(opr_name(&ops[j - 2], 0), "ecx"))
3125 ferr(&ops[j - 2], "unexpected ecx pop\n");
3131 } while (i < opcnt);
3134 int ecx_push = 0, esp_sub = 0;
3137 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
3138 ops[i].flags |= OPF_RMD;
3144 for (; i < opcnt; i++) {
3145 if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
3147 if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
3148 && ops[i].operand[1].type == OPT_CONST)
3150 g_stack_fsz = ops[i].operand[1].val;
3151 ops[i].flags |= OPF_RMD;
3158 if (ecx_push || esp_sub)
3164 for (; i < opcnt; i++)
3165 if (ops[i].op == OP_RET)
3168 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
3169 if (found && is_like_tailjmp(j))
3175 for (l = 0; l < ecx_push; l++) {
3176 if (ops[j].op != OP_POP
3177 || !IS(opr_name(&ops[j], 0), "ecx"))
3179 ferr(&ops[j], "'pop ecx' expected\n");
3181 ops[j].flags |= OPF_RMD;
3189 if (ops[j].op != OP_ADD
3190 || !IS(opr_name(&ops[j], 0), "esp")
3191 || ops[j].operand[1].type != OPT_CONST
3192 || ops[j].operand[1].val != g_stack_fsz)
3193 ferr(&ops[j], "'add esp' expected\n");
3194 ops[j].flags |= OPF_RMD;
3200 } while (i < opcnt);
3205 // - parse calls with labels
3206 // - resolve all branches
3207 for (i = 0; i < opcnt; i++)
3213 if (po->flags & OPF_RMD)
3216 if (po->op == OP_CALL) {
3219 if (po->operand[0].type == OPT_LABEL) {
3220 tmpname = opr_name(po, 0);
3221 if (IS_START(tmpname, "loc_"))
3222 ferr(po, "call to loc_*\n");
3223 pp_c = proto_parse(fhdr, tmpname, 0);
3225 ferr(po, "proto_parse failed for call '%s'\n", tmpname);
3227 pp = proto_clone(pp_c);
3228 my_assert_not(pp, NULL);
3230 else if (po->datap != NULL) {
3231 pp = calloc(1, sizeof(*pp));
3232 my_assert_not(pp, NULL);
3234 ret = parse_protostr(po->datap, pp);
3236 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
3243 check_func_pp(po, pp, "fptr var call");
3244 if (pp->is_noreturn)
3245 po->flags |= OPF_TAIL;
3251 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
3254 if (po->operand[0].type == OPT_REGMEM) {
3255 char *p = strchr(po->operand[0].name, '[');
3258 ret = p - po->operand[0].name;
3259 strncpy(buf1, po->operand[0].name, ret);
3262 for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
3263 if (IS(g_func_pd[j].label, buf1)) {
3269 //ferr(po, "label '%s' not parsed?\n", buf1);
3271 if (pd->type != OPT_OFFSET)
3272 ferr(po, "label '%s' with non-offset data?\n", buf1);
3274 // find all labels, link
3275 for (j = 0; j < pd->count; j++) {
3276 for (l = 0; l < opcnt; l++) {
3277 if (g_labels[l][0] && IS(g_labels[l], pd->d[j].u.label)) {
3278 add_label_ref(&g_label_refs[l], i);
3289 for (l = 0; l < opcnt; l++) {
3290 if (g_labels[l][0] && IS(po->operand[0].name, g_labels[l])) {
3291 if (l == i + 1 && po->op == OP_JMP) {
3292 // yet another alignment type..
3293 po->flags |= OPF_RMD;
3296 add_label_ref(&g_label_refs[l], i);
3302 if (po->bt_i != -1 || (po->flags & OPF_RMD))
3305 if (po->operand[0].type == OPT_LABEL)
3309 ferr(po, "unhandled branch\n");
3313 po->flags |= OPF_TAIL;
3314 if (i > 0 && ops[i - 1].op == OP_POP)
3315 po->flags |= OPF_ATAIL;
3320 // - remove dead labels
3322 for (i = 0; i < opcnt; i++)
3324 if (g_labels[i][0] != 0 && g_label_refs[i].i == -1)
3328 if (po->flags & OPF_RMD)
3331 if (po->op == OP_CALL)
3333 tmpname = opr_name(po, 0);
3338 pp_c = resolve_icall(i, opcnt, &l);
3340 if (!pp_c->is_func && !pp_c->is_fptr)
3341 ferr(po, "call to non-func: %s\n", pp_c->name);
3342 pp = proto_clone(pp_c);
3343 my_assert_not(pp, NULL);
3345 // not resolved just to single func
3348 switch (po->operand[0].type) {
3350 // we resolved this call and no longer need the register
3351 po->regmask_src &= ~(1 << po->operand[0].reg);
3361 pp = calloc(1, sizeof(*pp));
3362 my_assert_not(pp, NULL);
3364 ret = scan_for_esp_adjust(i + 1, opcnt, &j, &l);
3366 if (!g_allow_regfunc)
3367 ferr(po, "non-__cdecl indirect call unhandled yet\n");
3368 pp->is_unresolved = 1;
3372 if (j > ARRAY_SIZE(pp->arg))
3373 ferr(po, "esp adjust too large: %d\n", j);
3374 pp->ret_type.name = strdup("int");
3375 pp->argc = pp->argc_stack = j;
3376 for (arg = 0; arg < pp->argc; arg++)
3377 pp->arg[arg].type.name = strdup("int");
3382 // look for and make use of esp adjust
3384 if (!pp->is_stdcall && pp->argc_stack > 0)
3385 ret = scan_for_esp_adjust(i + 1, opcnt, &j, &l);
3387 if (pp->is_vararg) {
3388 if (j / 4 < pp->argc_stack)
3389 ferr(po, "esp adjust is too small: %x < %x\n",
3390 j, pp->argc_stack * 4);
3391 // modify pp to make it have varargs as normal args
3393 pp->argc += j / 4 - pp->argc_stack;
3394 for (; arg < pp->argc; arg++) {
3395 pp->arg[arg].type.name = strdup("int");
3398 if (pp->argc > ARRAY_SIZE(pp->arg))
3399 ferr(po, "too many args for '%s'\n", tmpname);
3401 if (pp->argc_stack != j / 4)
3402 ferr(po, "stack tracking failed for '%s': %x %x\n",
3403 tmpname, pp->argc_stack * 4, j);
3405 ops[ret].flags |= OPF_RMD;
3406 if (ops[ret].op == OP_POP && j > 4) {
3407 // deal with multi-pop stack adjust
3409 while (ops[ret].op == OP_POP && j > 0 && ret < opcnt) {
3410 ops[ret].flags |= OPF_RMD;
3416 // a bit of a hack, but deals with use of
3417 // single adj for multiple calls
3418 ops[ret].operand[1].val -= j;
3421 else if (pp->is_vararg)
3422 ferr(po, "missing esp_adjust for vararg func '%s'\n",
3425 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
3426 // since we know the args, collect them
3427 collect_call_args(po, i, pp, ®mask, &save_arg_vars,
3431 if (strstr(pp->ret_type.name, "int64"))
3437 // - find POPs for PUSHes, rm both
3438 // - scan for STD/CLD, propagate DF
3439 // - scan for all used registers
3440 // - find flag set ops for their users
3441 // - do unreselved calls
3442 // - declare indirect functions
3443 for (i = 0; i < opcnt; i++) {
3445 if (po->flags & OPF_RMD)
3448 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
3449 reg = po->operand[0].reg;
3450 if (!(regmask & (1 << reg)))
3451 // not a reg save after all, rerun scan_for_pop
3452 po->flags &= ~OPF_RSAVE;
3454 regmask_save |= 1 << reg;
3457 if (po->op == OP_PUSH && po->p_argnum == 0
3458 && !(po->flags & OPF_RSAVE) && !g_func_pp->is_userstack)
3460 if (po->operand[0].type == OPT_REG)
3462 reg = po->operand[0].reg;
3464 ferr(po, "reg not set for push?\n");
3467 ret = scan_for_pop(i + 1, opcnt,
3468 po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
3471 ferr(po, "too much depth: %d\n", depth);
3473 po->flags |= OPF_RMD;
3474 scan_for_pop(i + 1, opcnt, po->operand[0].name,
3475 i + opcnt * 4, 0, &depth, 1);
3478 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
3481 if (regmask & (1 << reg)) {
3482 if (regmask_save & (1 << reg))
3483 ferr(po, "%s already saved?\n", po->operand[0].name);
3487 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg);
3491 else if (po->operand[0].type == OPT_CONST) {
3492 for (j = i + 1; j < opcnt; j++) {
3493 if ((ops[j].flags & (OPF_JMP|OPF_TAIL|OPF_RSAVE))
3494 || ops[j].op == OP_PUSH || g_labels[i][0] != 0)
3499 if (!(ops[j].flags & OPF_RMD) && ops[j].op == OP_POP)
3501 po->flags |= OPF_RMD;
3509 if (po->op == OP_STD) {
3510 po->flags |= OPF_DF | OPF_RMD;
3511 scan_propagate_df(i + 1, opcnt);
3514 regmask_now = po->regmask_src | po->regmask_dst;
3515 if (regmask_now & (1 << xBP)) {
3516 if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
3517 if (po->regmask_dst & (1 << xBP))
3518 // compiler decided to drop bp frame and use ebp as scratch
3519 scan_fwd_set_flags(i + 1, opcnt, i + opcnt * 5, OPF_EBP_S);
3521 regmask_now &= ~(1 << xBP);
3525 regmask |= regmask_now;
3527 if (po->flags & OPF_CC)
3529 int setters[16], cnt = 0, branched = 0;
3531 ret = scan_for_flag_set(i, i + opcnt * 6,
3532 &branched, setters, &cnt);
3533 if (ret < 0 || cnt <= 0)
3534 ferr(po, "unable to trace flag setter(s)\n");
3535 if (cnt > ARRAY_SIZE(setters))
3536 ferr(po, "too many flag setters\n");
3538 for (j = 0; j < cnt; j++)
3540 tmp_op = &ops[setters[j]]; // flag setter
3543 // to get nicer code, we try to delay test and cmp;
3544 // if we can't because of operand modification, or if we
3545 // have arith op, or branch, make it calculate flags explicitly
3546 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP)
3548 if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
3549 pfomask = 1 << po->pfo;
3551 else if (tmp_op->op == OP_CMPS || tmp_op->op == OP_SCAS) {
3552 pfomask = 1 << po->pfo;
3555 // see if we'll be able to handle based on op result
3556 if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
3557 && po->pfo != PFO_Z && po->pfo != PFO_S
3558 && po->pfo != PFO_P)
3560 || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
3562 pfomask = 1 << po->pfo;
3565 if (tmp_op->op == OP_ADD && po->pfo == PFO_C) {
3566 propagate_lmod(tmp_op, &tmp_op->operand[0],
3567 &tmp_op->operand[1]);
3568 if (tmp_op->operand[0].lmod == OPLM_DWORD)
3573 tmp_op->pfomask |= pfomask;
3574 cond_vars |= pfomask;
3576 // note: may overwrite, currently not a problem
3580 if (po->op == OP_RCL || po->op == OP_RCR
3581 || po->op == OP_ADC || po->op == OP_SBB)
3582 cond_vars |= 1 << PFO_C;
3585 if (po->op == OP_CMPS || po->op == OP_SCAS) {
3586 cond_vars |= 1 << PFO_Z;
3588 else if (po->op == OP_MUL
3589 || (po->op == OP_IMUL && po->operand_cnt == 1))
3591 if (po->operand[0].lmod == OPLM_DWORD)
3594 else if (po->op == OP_CALL) {
3597 ferr(po, "NULL pp\n");
3599 if (pp->is_unresolved) {
3600 int regmask_stack = 0;
3601 collect_call_args(po, i, pp, ®mask, &save_arg_vars,
3604 // this is pretty rough guess:
3605 // see ecx and edx were pushed (and not their saved versions)
3606 for (arg = 0; arg < pp->argc; arg++) {
3607 if (pp->arg[arg].reg != NULL)
3610 tmp_op = pp->arg[arg].datap;
3612 ferr(po, "parsed_op missing for arg%d\n", arg);
3613 if (tmp_op->p_argnum == 0 && tmp_op->operand[0].type == OPT_REG)
3614 regmask_stack |= 1 << tmp_op->operand[0].reg;
3617 if (!((regmask_stack & (1 << xCX))
3618 && (regmask_stack & (1 << xDX))))
3620 if (pp->argc_stack != 0
3621 || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX))))
3623 pp_insert_reg_arg(pp, "ecx");
3624 pp->is_fastcall = 1;
3625 regmask_init |= 1 << xCX;
3626 regmask |= 1 << xCX;
3628 if (pp->argc_stack != 0
3629 || ((regmask | regmask_arg) & (1 << xDX)))
3631 pp_insert_reg_arg(pp, "edx");
3632 regmask_init |= 1 << xDX;
3633 regmask |= 1 << xDX;
3637 // note: __cdecl doesn't fall into is_unresolved category
3638 if (pp->argc_stack > 0)
3642 for (arg = 0; arg < pp->argc; arg++) {
3643 if (pp->arg[arg].reg != NULL) {
3644 reg = char_array_i(regs_r32,
3645 ARRAY_SIZE(regs_r32), pp->arg[arg].reg);
3647 ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg);
3648 if (!(regmask & (1 << reg))) {
3649 regmask_init |= 1 << reg;
3650 regmask |= 1 << reg;
3655 else if (po->op == OP_MOV && po->operand[0].pp != NULL
3656 && po->operand[1].pp != NULL)
3658 // <var> = offset <something>
3659 if ((po->operand[1].pp->is_func || po->operand[1].pp->is_fptr)
3660 && !IS_START(po->operand[1].name, "off_"))
3662 if (!po->operand[0].pp->is_fptr)
3663 ferr(po, "%s not declared as fptr when it should be\n",
3664 po->operand[0].name);
3665 if (pp_cmp_func(po->operand[0].pp, po->operand[1].pp)) {
3666 pp_print(buf1, sizeof(buf1), po->operand[0].pp);
3667 pp_print(buf2, sizeof(buf2), po->operand[1].pp);
3668 fnote(po, "var: %s\n", buf1);
3669 fnote(po, "func: %s\n", buf2);
3670 ferr(po, "^ mismatch\n");
3674 else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void"))
3675 regmask |= 1 << xAX;
3676 else if (po->op == OP_DIV || po->op == OP_IDIV) {
3677 // 32bit division is common, look for it
3678 if (po->op == OP_DIV)
3679 ret = scan_for_reg_clear(i, xDX);
3681 ret = scan_for_cdq_edx(i);
3683 po->flags |= OPF_32BIT;
3687 else if (po->op == OP_CLD)
3688 po->flags |= OPF_RMD;
3690 if (po->op == OP_RCL || po->op == OP_RCR || po->op == OP_XCHG) {
3696 // - confirm regmask_save, it might have been reduced
3697 if (regmask_save != 0)
3700 for (i = 0; i < opcnt; i++) {
3702 if (po->flags & OPF_RMD)
3705 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE))
3706 regmask_save |= 1 << po->operand[0].reg;
3710 // output starts here
3712 // define userstack size
3713 if (g_func_pp->is_userstack) {
3714 fprintf(fout, "#ifndef US_SZ_%s\n", g_func_pp->name);
3715 fprintf(fout, "#define US_SZ_%s USERSTACK_SIZE\n", g_func_pp->name);
3716 fprintf(fout, "#endif\n");
3719 // the function itself
3720 fprintf(fout, "%s ", g_func_pp->ret_type.name);
3721 output_pp_attrs(fout, g_func_pp, g_ida_func_attr & IDAFA_NORETURN);
3722 fprintf(fout, "%s(", g_func_pp->name);
3724 for (i = 0; i < g_func_pp->argc; i++) {
3726 fprintf(fout, ", ");
3727 if (g_func_pp->arg[i].fptr != NULL) {
3729 pp = g_func_pp->arg[i].fptr;
3730 fprintf(fout, "%s (", pp->ret_type.name);
3731 output_pp_attrs(fout, pp, 0);
3732 fprintf(fout, "*a%d)(", i + 1);
3733 for (j = 0; j < pp->argc; j++) {
3735 fprintf(fout, ", ");
3736 if (pp->arg[j].fptr)
3737 ferr(ops, "nested fptr\n");
3738 fprintf(fout, "%s", pp->arg[j].type.name);
3740 if (pp->is_vararg) {
3742 fprintf(fout, ", ");
3743 fprintf(fout, "...");
3747 else if (g_func_pp->arg[i].type.is_retreg) {
3748 fprintf(fout, "u32 *r_%s", g_func_pp->arg[i].reg);
3751 fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1);
3754 if (g_func_pp->is_vararg) {
3756 fprintf(fout, ", ");
3757 fprintf(fout, "...");
3760 fprintf(fout, ")\n{\n");
3762 // declare indirect functions
3763 for (i = 0; i < opcnt; i++) {
3765 if (po->flags & OPF_RMD)
3768 if (po->op == OP_CALL) {
3771 ferr(po, "NULL pp\n");
3773 if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) {
3774 if (pp->name[0] != 0) {
3775 memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
3776 memcpy(pp->name, "i_", 2);
3778 // might be declared already
3780 for (j = 0; j < i; j++) {
3781 if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) {
3782 if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) {
3792 snprintf(pp->name, sizeof(pp->name), "icall%d", i);
3794 fprintf(fout, " %s (", pp->ret_type.name);
3795 output_pp_attrs(fout, pp, 0);
3796 fprintf(fout, "*%s)(", pp->name);
3797 for (j = 0; j < pp->argc; j++) {
3799 fprintf(fout, ", ");
3800 fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1);
3802 fprintf(fout, ");\n");
3807 // output LUTs/jumptables
3808 for (i = 0; i < g_func_pd_cnt; i++) {
3810 fprintf(fout, " static const ");
3811 if (pd->type == OPT_OFFSET) {
3812 fprintf(fout, "void *jt_%s[] =\n { ", pd->label);
3814 for (j = 0; j < pd->count; j++) {
3816 fprintf(fout, ", ");
3817 fprintf(fout, "&&%s", pd->d[j].u.label);
3821 fprintf(fout, "%s %s[] =\n { ",
3822 lmod_type_u(ops, pd->lmod), pd->label);
3824 for (j = 0; j < pd->count; j++) {
3826 fprintf(fout, ", ");
3827 fprintf(fout, "%u", pd->d[j].u.val);
3830 fprintf(fout, " };\n");
3834 // declare stack frame, va_arg
3836 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
3837 (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz);
3841 if (g_func_pp->is_userstack) {
3842 fprintf(fout, " u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name);
3843 fprintf(fout, " u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n");
3847 if (g_func_pp->is_vararg) {
3848 fprintf(fout, " va_list ap;\n");
3852 // declare arg-registers
3853 for (i = 0; i < g_func_pp->argc; i++) {
3854 if (g_func_pp->arg[i].reg != NULL) {
3855 reg = char_array_i(regs_r32,
3856 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
3857 if (regmask & (1 << reg)) {
3858 if (g_func_pp->arg[i].type.is_retreg)
3859 fprintf(fout, " u32 %s = *r_%s;\n",
3860 g_func_pp->arg[i].reg, g_func_pp->arg[i].reg);
3862 fprintf(fout, " u32 %s = (u32)a%d;\n",
3863 g_func_pp->arg[i].reg, i + 1);
3866 if (g_func_pp->arg[i].type.is_retreg)
3867 ferr(ops, "retreg '%s' is unused?\n",
3868 g_func_pp->arg[i].reg);
3869 fprintf(fout, " // %s = a%d; // unused\n",
3870 g_func_pp->arg[i].reg, i + 1);
3876 regmask_now = regmask & ~regmask_arg;
3877 regmask_now &= ~(1 << xSP);
3879 for (reg = 0; reg < 8; reg++) {
3880 if (regmask_now & (1 << reg)) {
3881 fprintf(fout, " u32 %s", regs_r32[reg]);
3882 if (regmask_init & (1 << reg))
3883 fprintf(fout, " = 0");
3884 fprintf(fout, ";\n");
3891 for (reg = 0; reg < 8; reg++) {
3892 if (regmask_save & (1 << reg)) {
3893 fprintf(fout, " u32 s_%s;\n", regs_r32[reg]);
3899 if (save_arg_vars) {
3900 for (reg = 0; reg < 32; reg++) {
3901 if (save_arg_vars & (1 << reg)) {
3902 fprintf(fout, " u32 s_a%d;\n", reg + 1);
3909 for (i = 0; i < 8; i++) {
3910 if (cond_vars & (1 << i)) {
3911 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
3918 fprintf(fout, " u32 tmp;\n");
3923 fprintf(fout, " u64 tmp64;\n");
3928 fprintf(fout, "\n");
3930 if (g_func_pp->is_vararg) {
3931 if (g_func_pp->argc_stack == 0)
3932 ferr(ops, "vararg func without stack args?\n");
3933 fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc);
3937 for (i = 0; i < opcnt; i++)
3939 if (g_labels[i][0] != 0) {
3940 fprintf(fout, "\n%s:\n", g_labels[i]);
3943 delayed_flag_op = NULL;
3944 last_arith_dst = NULL;
3948 if (po->flags & OPF_RMD)
3953 #define assert_operand_cnt(n_) \
3954 if (po->operand_cnt != n_) \
3955 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
3957 // conditional/flag using op?
3958 if (po->flags & OPF_CC)
3964 // we go through all this trouble to avoid using parsed_flag_op,
3965 // which makes generated code much nicer
3966 if (delayed_flag_op != NULL)
3968 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op,
3969 po->pfo, po->pfo_inv);
3972 else if (last_arith_dst != NULL
3973 && (po->pfo == PFO_Z || po->pfo == PFO_S || po->pfo == PFO_P
3974 || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
3977 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
3978 out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv,
3979 last_arith_dst->lmod, buf3);
3982 else if (tmp_op != NULL) {
3983 // use preprocessed flag calc results
3984 if (!(tmp_op->pfomask & (1 << po->pfo)))
3985 ferr(po, "not prepared for pfo %d\n", po->pfo);
3987 // note: pfo_inv was not yet applied
3988 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
3989 po->pfo_inv ? "!" : "", parsed_flag_op_names[po->pfo]);
3992 ferr(po, "all methods of finding comparison failed\n");
3995 if (po->flags & OPF_JMP) {
3996 fprintf(fout, " if %s", buf1);
3998 else if (po->op == OP_RCL || po->op == OP_RCR
3999 || po->op == OP_ADC || po->op == OP_SBB)
4002 fprintf(fout, " cond_%s = %s;\n",
4003 parsed_flag_op_names[po->pfo], buf1);
4005 else if (po->flags & OPF_DATA) { // SETcc
4006 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
4007 fprintf(fout, " %s = %s;", buf2, buf1);
4010 ferr(po, "unhandled conditional op\n");
4014 pfomask = po->pfomask;
4016 if (po->flags & (OPF_REPZ|OPF_REPNZ)) {
4017 struct parsed_opr opr = {0,};
4020 opr.lmod = OPLM_DWORD;
4021 ret = try_resolve_const(i, &opr, opcnt * 7 + i, &uval);
4023 if (ret != 1 || uval == 0) {
4024 // we need initial flags for ecx=0 case..
4025 if (i > 0 && ops[i - 1].op == OP_XOR
4026 && IS(ops[i - 1].operand[0].name,
4027 ops[i - 1].operand[1].name))
4029 fprintf(fout, " cond_z = ");
4030 if (pfomask & (1 << PFO_C))
4031 fprintf(fout, "cond_c = ");
4032 fprintf(fout, "0;\n");
4034 else if (last_arith_dst != NULL) {
4035 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
4036 out_test_for_cc(buf1, sizeof(buf1), po, PFO_Z, 0,
4037 last_arith_dst->lmod, buf3);
4038 fprintf(fout, " cond_z = %s;\n", buf1);
4041 ferr(po, "missing initial ZF\n");
4048 assert_operand_cnt(2);
4049 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4050 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4051 default_cast_to(buf3, sizeof(buf3), &po->operand[0]);
4052 fprintf(fout, " %s = %s;", buf1,
4053 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4058 assert_operand_cnt(2);
4059 po->operand[1].lmod = OPLM_DWORD; // always
4060 fprintf(fout, " %s = %s;",
4061 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4062 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4067 assert_operand_cnt(2);
4068 fprintf(fout, " %s = %s;",
4069 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4070 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4074 assert_operand_cnt(2);
4075 switch (po->operand[1].lmod) {
4077 strcpy(buf3, "(s8)");
4080 strcpy(buf3, "(s16)");
4083 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
4085 fprintf(fout, " %s = %s;",
4086 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4087 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4092 assert_operand_cnt(2);
4093 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4094 fprintf(fout, " tmp = %s;",
4095 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0));
4096 fprintf(fout, " %s = %s;",
4097 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4098 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4099 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
4100 fprintf(fout, " %s = %stmp;",
4101 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
4102 default_cast_to(buf3, sizeof(buf3), &po->operand[1]));
4103 snprintf(g_comment, sizeof(g_comment), "xchg");
4107 assert_operand_cnt(1);
4108 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4109 fprintf(fout, " %s = ~%s;", buf1, buf1);
4113 assert_operand_cnt(2);
4114 fprintf(fout, " %s = (s32)%s >> 31;",
4115 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4116 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4117 strcpy(g_comment, "cdq");
4121 assert_operand_cnt(3);
4122 if (po->flags & OPF_REP) {
4127 fprintf(fout, " eax = %sesi; esi %c= %d;",
4128 lmod_cast_u_ptr(po, po->operand[0].lmod),
4129 (po->flags & OPF_DF) ? '-' : '+',
4130 lmod_bytes(po, po->operand[0].lmod));
4131 strcpy(g_comment, "lods");
4136 assert_operand_cnt(3);
4137 if (po->flags & OPF_REP) {
4138 fprintf(fout, " for (; ecx != 0; ecx--, edi %c= %d)\n",
4139 (po->flags & OPF_DF) ? '-' : '+',
4140 lmod_bytes(po, po->operand[0].lmod));
4141 fprintf(fout, " %sedi = eax;",
4142 lmod_cast_u_ptr(po, po->operand[0].lmod));
4143 strcpy(g_comment, "rep stos");
4146 fprintf(fout, " %sedi = eax; edi %c= %d;",
4147 lmod_cast_u_ptr(po, po->operand[0].lmod),
4148 (po->flags & OPF_DF) ? '-' : '+',
4149 lmod_bytes(po, po->operand[0].lmod));
4150 strcpy(g_comment, "stos");
4155 assert_operand_cnt(3);
4156 j = lmod_bytes(po, po->operand[0].lmod);
4157 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4158 l = (po->flags & OPF_DF) ? '-' : '+';
4159 if (po->flags & OPF_REP) {
4161 " for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n",
4164 " %sedi = %sesi;", buf1, buf1);
4165 strcpy(g_comment, "rep movs");
4168 fprintf(fout, " %sedi = %sesi; edi %c= %d; esi %c= %d;",
4169 buf1, buf1, l, j, l, j);
4170 strcpy(g_comment, "movs");
4175 // repe ~ repeat while ZF=1
4176 assert_operand_cnt(3);
4177 j = lmod_bytes(po, po->operand[0].lmod);
4178 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4179 l = (po->flags & OPF_DF) ? '-' : '+';
4180 if (po->flags & OPF_REP) {
4182 " for (; ecx != 0; ecx--) {\n");
4183 if (pfomask & (1 << PFO_C)) {
4186 " cond_c = %sesi < %sedi;\n", buf1, buf1);
4187 pfomask &= ~(1 << PFO_C);
4190 " cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n",
4191 buf1, buf1, l, j, l, j);
4193 " if (cond_z %s 0) break;\n",
4194 (po->flags & OPF_REPZ) ? "==" : "!=");
4197 snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
4198 (po->flags & OPF_REPZ) ? "e" : "ne");
4202 " cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;",
4203 buf1, buf1, l, j, l, j);
4204 strcpy(g_comment, "cmps");
4206 pfomask &= ~(1 << PFO_Z);
4207 last_arith_dst = NULL;
4208 delayed_flag_op = NULL;
4212 // only does ZF (for now)
4213 // repe ~ repeat while ZF=1
4214 assert_operand_cnt(3);
4215 j = lmod_bytes(po, po->operand[0].lmod);
4216 l = (po->flags & OPF_DF) ? '-' : '+';
4217 if (po->flags & OPF_REP) {
4219 " for (; ecx != 0; ecx--) {\n");
4221 " cond_z = (%seax == %sedi); edi %c= %d;\n",
4222 lmod_cast_u(po, po->operand[0].lmod),
4223 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4225 " if (cond_z %s 0) break;\n",
4226 (po->flags & OPF_REPZ) ? "==" : "!=");
4229 snprintf(g_comment, sizeof(g_comment), "rep%s scas",
4230 (po->flags & OPF_REPZ) ? "e" : "ne");
4233 fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;",
4234 lmod_cast_u(po, po->operand[0].lmod),
4235 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4236 strcpy(g_comment, "scas");
4238 pfomask &= ~(1 << PFO_Z);
4239 last_arith_dst = NULL;
4240 delayed_flag_op = NULL;
4243 // arithmetic w/flags
4246 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4249 assert_operand_cnt(2);
4250 fprintf(fout, " %s %s= %s;",
4251 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4253 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4254 output_std_flags(fout, po, &pfomask, buf1);
4255 last_arith_dst = &po->operand[0];
4256 delayed_flag_op = NULL;
4261 assert_operand_cnt(2);
4262 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4263 if (pfomask & (1 << PFO_C)) {
4264 if (po->operand[1].type == OPT_CONST) {
4265 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4266 j = po->operand[1].val;
4269 if (po->op == OP_SHL)
4273 fprintf(fout, " cond_c = (%s >> %d) & 1;\n",
4277 ferr(po, "zero shift?\n");
4281 pfomask &= ~(1 << PFO_C);
4283 fprintf(fout, " %s %s= %s;", buf1, op_to_c(po),
4284 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4285 output_std_flags(fout, po, &pfomask, buf1);
4286 last_arith_dst = &po->operand[0];
4287 delayed_flag_op = NULL;
4291 assert_operand_cnt(2);
4292 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4293 fprintf(fout, " %s = %s%s >> %s;", buf1,
4294 lmod_cast_s(po, po->operand[0].lmod), buf1,
4295 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4296 output_std_flags(fout, po, &pfomask, buf1);
4297 last_arith_dst = &po->operand[0];
4298 delayed_flag_op = NULL;
4303 assert_operand_cnt(2);
4304 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4305 if (po->operand[1].type == OPT_CONST) {
4306 j = po->operand[1].val;
4307 j %= lmod_bytes(po, po->operand[0].lmod) * 8;
4308 fprintf(fout, po->op == OP_ROL ?
4309 " %s = (%s << %d) | (%s >> %d);" :
4310 " %s = (%s >> %d) | (%s << %d);",
4311 buf1, buf1, j, buf1,
4312 lmod_bytes(po, po->operand[0].lmod) * 8 - j);
4316 output_std_flags(fout, po, &pfomask, buf1);
4317 last_arith_dst = &po->operand[0];
4318 delayed_flag_op = NULL;
4323 assert_operand_cnt(2);
4324 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4325 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4326 if (po->operand[1].type == OPT_CONST) {
4327 j = po->operand[1].val % l;
4329 ferr(po, "zero rotate\n");
4330 fprintf(fout, " tmp = (%s >> %d) & 1;\n",
4331 buf1, (po->op == OP_RCL) ? (l - j) : (j - 1));
4332 if (po->op == OP_RCL) {
4334 " %s = (%s << %d) | (cond_c << %d)",
4335 buf1, buf1, j, j - 1);
4337 fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j);
4341 " %s = (%s >> %d) | (cond_c << %d)",
4342 buf1, buf1, j, l - j);
4344 fprintf(fout, " | (%s << %d)", buf1, l + 1 - j);
4346 fprintf(fout, ";\n");
4347 fprintf(fout, " cond_c = tmp;");
4351 strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr");
4352 output_std_flags(fout, po, &pfomask, buf1);
4353 last_arith_dst = &po->operand[0];
4354 delayed_flag_op = NULL;
4358 assert_operand_cnt(2);
4359 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4360 if (IS(opr_name(po, 0), opr_name(po, 1))) {
4361 // special case for XOR
4362 if (pfomask & (1 << PFO_BE)) { // weird, but it happens..
4363 fprintf(fout, " cond_be = 1;\n");
4364 pfomask &= ~(1 << PFO_BE);
4366 fprintf(fout, " %s = 0;",
4367 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4368 last_arith_dst = &po->operand[0];
4369 delayed_flag_op = NULL;
4375 assert_operand_cnt(2);
4376 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4377 if (pfomask & (1 << PFO_C)) {
4378 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4379 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4380 if (po->operand[0].lmod == OPLM_DWORD) {
4381 fprintf(fout, " tmp64 = (u64)%s + %s;\n", buf1, buf2);
4382 fprintf(fout, " cond_c = tmp64 >> 32;\n");
4383 fprintf(fout, " %s = (u32)tmp64;",
4384 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4385 strcat(g_comment, "add64");
4388 fprintf(fout, " cond_c = ((u32)%s + %s) >> %d;\n",
4389 buf1, buf2, lmod_bytes(po, po->operand[0].lmod) * 8);
4390 fprintf(fout, " %s += %s;",
4391 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4394 pfomask &= ~(1 << PFO_C);
4395 output_std_flags(fout, po, &pfomask, buf1);
4396 last_arith_dst = &po->operand[0];
4397 delayed_flag_op = NULL;
4403 assert_operand_cnt(2);
4404 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4405 if (pfomask & (1 << PFO_C)) {
4406 fprintf(fout, " cond_c = %s < %s;\n",
4407 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]),
4408 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4409 pfomask &= ~(1 << PFO_C);
4415 assert_operand_cnt(2);
4416 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4417 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4418 if (po->op == OP_SBB
4419 && IS(po->operand[0].name, po->operand[1].name))
4421 // avoid use of unitialized var
4422 fprintf(fout, " %s = -cond_c;", buf1);
4423 // carry remains what it was
4424 pfomask &= ~(1 << PFO_C);
4427 fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
4428 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4430 output_std_flags(fout, po, &pfomask, buf1);
4431 last_arith_dst = &po->operand[0];
4432 delayed_flag_op = NULL;
4436 assert_operand_cnt(2);
4437 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4438 fprintf(fout, " %s = %s ? __builtin_ffs(%s) - 1 : 0;",
4439 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4441 output_std_flags(fout, po, &pfomask, buf1);
4442 last_arith_dst = &po->operand[0];
4443 delayed_flag_op = NULL;
4444 strcat(g_comment, "bsf");
4449 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4450 if (po->operand[0].type == OPT_REG) {
4451 strcpy(buf2, po->op == OP_INC ? "++" : "--");
4452 fprintf(fout, " %s%s;", buf1, buf2);
4455 strcpy(buf2, po->op == OP_INC ? "+" : "-");
4456 fprintf(fout, " %s %s= 1;", buf1, buf2);
4458 output_std_flags(fout, po, &pfomask, buf1);
4459 last_arith_dst = &po->operand[0];
4460 delayed_flag_op = NULL;
4464 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4465 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
4466 fprintf(fout, " %s = -%s%s;", buf1,
4467 lmod_cast_s(po, po->operand[0].lmod), buf2);
4468 last_arith_dst = &po->operand[0];
4469 delayed_flag_op = NULL;
4470 if (pfomask & (1 << PFO_C)) {
4471 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
4472 pfomask &= ~(1 << PFO_C);
4477 if (po->operand_cnt == 2) {
4478 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4481 if (po->operand_cnt == 3)
4482 ferr(po, "TODO imul3\n");
4485 assert_operand_cnt(1);
4486 switch (po->operand[0].lmod) {
4488 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
4489 fprintf(fout, " tmp64 = %seax * %s%s;\n", buf1, buf1,
4490 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
4491 fprintf(fout, " edx = tmp64 >> 32;\n");
4492 fprintf(fout, " eax = tmp64;");
4495 strcpy(buf1, po->op == OP_IMUL ? "(s16)(s8)" : "(u16)(u8)");
4496 fprintf(fout, " LOWORD(eax) = %seax * %s;", buf1,
4497 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0],
4501 ferr(po, "TODO: unhandled mul type\n");
4504 last_arith_dst = NULL;
4505 delayed_flag_op = NULL;
4510 assert_operand_cnt(1);
4511 if (po->operand[0].lmod != OPLM_DWORD)
4512 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
4514 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4515 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
4516 po->op == OP_IDIV));
4517 switch (po->operand[0].lmod) {
4519 if (po->flags & OPF_32BIT)
4520 snprintf(buf3, sizeof(buf3), "%seax", buf2);
4522 fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n");
4523 snprintf(buf3, sizeof(buf3), "%stmp64",
4524 (po->op == OP_IDIV) ? "(s64)" : "");
4526 if (po->operand[0].type == OPT_REG
4527 && po->operand[0].reg == xDX)
4529 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4530 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4533 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4534 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4538 ferr(po, "unhandled division type\n");
4540 last_arith_dst = NULL;
4541 delayed_flag_op = NULL;
4546 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4548 for (j = 0; j < 8; j++) {
4549 if (pfomask & (1 << j)) {
4550 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
4551 fprintf(fout, " cond_%s = %s;",
4552 parsed_flag_op_names[j], buf1);
4559 last_arith_dst = NULL;
4560 delayed_flag_op = po;
4564 // SETcc - should already be handled
4567 // note: we reuse OP_Jcc for SETcc, only flags differ
4569 fprintf(fout, "\n goto %s;", po->operand[0].name);
4573 fprintf(fout, " if (ecx == 0)\n");
4574 fprintf(fout, " goto %s;", po->operand[0].name);
4575 strcat(g_comment, "jecxz");
4579 assert_operand_cnt(1);
4580 last_arith_dst = NULL;
4581 delayed_flag_op = NULL;
4583 if (po->operand[0].type == OPT_REGMEM) {
4584 ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
4587 ferr(po, "parse failure for jmp '%s'\n",
4588 po->operand[0].name);
4589 fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
4592 else if (po->operand[0].type != OPT_LABEL)
4593 ferr(po, "unhandled jmp type\n");
4595 fprintf(fout, " goto %s;", po->operand[0].name);
4599 assert_operand_cnt(1);
4601 my_assert_not(pp, NULL);
4604 if (po->flags & OPF_CC) {
4605 // we treat conditional branch to another func
4606 // (yes such code exists..) as conditional tailcall
4608 fprintf(fout, " {\n");
4611 if (pp->is_fptr && !pp->is_arg) {
4612 fprintf(fout, "%s%s = %s;\n", buf3, pp->name,
4613 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
4615 if (pp->is_unresolved)
4616 fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
4617 buf3, asmfn, po->asmln, pp->name);
4620 fprintf(fout, "%s", buf3);
4621 if (strstr(pp->ret_type.name, "int64")) {
4622 if (po->flags & OPF_TAIL)
4623 ferr(po, "int64 and tail?\n");
4624 fprintf(fout, "tmp64 = ");
4626 else if (!IS(pp->ret_type.name, "void")) {
4627 if (po->flags & OPF_TAIL) {
4628 if (!IS(g_func_pp->ret_type.name, "void")) {
4629 fprintf(fout, "return ");
4630 if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
4631 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
4634 else if (regmask & (1 << xAX)) {
4635 fprintf(fout, "eax = ");
4636 if (pp->ret_type.is_ptr)
4637 fprintf(fout, "(u32)");
4641 if (pp->name[0] == 0)
4642 ferr(po, "missing pp->name\n");
4643 fprintf(fout, "%s%s(", pp->name,
4644 pp->has_structarg ? "_sa" : "");
4646 if (po->flags & OPF_ATAIL) {
4647 if (pp->argc_stack != g_func_pp->argc_stack
4648 || (pp->argc_stack > 0
4649 && pp->is_stdcall != g_func_pp->is_stdcall))
4650 ferr(po, "incompatible tailcall\n");
4651 if (g_func_pp->has_retreg)
4652 ferr(po, "TODO: retreg+tailcall\n");
4654 for (arg = j = 0; arg < pp->argc; arg++) {
4656 fprintf(fout, ", ");
4659 if (pp->arg[arg].type.is_ptr)
4660 snprintf(cast, sizeof(cast), "(%s)",
4661 pp->arg[arg].type.name);
4663 if (pp->arg[arg].reg != NULL) {
4664 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
4668 for (; j < g_func_pp->argc; j++)
4669 if (g_func_pp->arg[j].reg == NULL)
4671 fprintf(fout, "%sa%d", cast, j + 1);
4676 for (arg = 0; arg < pp->argc; arg++) {
4678 fprintf(fout, ", ");
4681 if (pp->arg[arg].type.is_ptr)
4682 snprintf(cast, sizeof(cast), "(%s)",
4683 pp->arg[arg].type.name);
4685 if (pp->arg[arg].reg != NULL) {
4686 if (pp->arg[arg].type.is_retreg)
4687 fprintf(fout, "&%s", pp->arg[arg].reg);
4689 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
4694 tmp_op = pp->arg[arg].datap;
4696 ferr(po, "parsed_op missing for arg%d\n", arg);
4698 if (tmp_op->flags & OPF_VAPUSH) {
4699 fprintf(fout, "ap");
4701 else if (tmp_op->p_argpass != 0) {
4702 fprintf(fout, "a%d", tmp_op->p_argpass);
4704 else if (tmp_op->p_argnum != 0) {
4705 fprintf(fout, "%ss_a%d", cast, tmp_op->p_argnum);
4709 out_src_opr(buf1, sizeof(buf1),
4710 tmp_op, &tmp_op->operand[0], cast, 0));
4714 fprintf(fout, ");");
4716 if (strstr(pp->ret_type.name, "int64")) {
4717 fprintf(fout, "\n");
4718 fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3);
4719 fprintf(fout, "%seax = tmp64;", buf3);
4722 if (pp->is_unresolved) {
4723 snprintf(buf2, sizeof(buf2), " unresolved %dreg",
4725 strcat(g_comment, buf2);
4728 if (po->flags & OPF_TAIL) {
4730 if (i == opcnt - 1 || pp->is_noreturn)
4732 else if (IS(pp->ret_type.name, "void"))
4734 else if (IS(g_func_pp->ret_type.name, "void"))
4736 // else already handled as 'return f()'
4739 if (!IS(g_func_pp->ret_type.name, "void")) {
4740 ferr(po, "int func -> void func tailcall?\n");
4743 fprintf(fout, "\n%sreturn;", buf3);
4744 strcat(g_comment, " ^ tailcall");
4748 strcat(g_comment, " tailcall");
4750 if (pp->is_noreturn)
4751 strcat(g_comment, " noreturn");
4752 if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0)
4753 strcat(g_comment, " argframe");
4754 if (po->flags & OPF_CC)
4755 strcat(g_comment, " cond");
4757 if (po->flags & OPF_CC)
4758 fprintf(fout, "\n }");
4760 delayed_flag_op = NULL;
4761 last_arith_dst = NULL;
4765 if (g_func_pp->is_vararg)
4766 fprintf(fout, " va_end(ap);\n");
4767 if (g_func_pp->has_retreg) {
4768 for (arg = 0; arg < g_func_pp->argc; arg++)
4769 if (g_func_pp->arg[arg].type.is_retreg)
4770 fprintf(fout, " *r_%s = %s;\n",
4771 g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg);
4774 if (IS(g_func_pp->ret_type.name, "void")) {
4775 if (i != opcnt - 1 || label_pending)
4776 fprintf(fout, " return;");
4778 else if (g_func_pp->ret_type.is_ptr) {
4779 fprintf(fout, " return (%s)eax;",
4780 g_func_pp->ret_type.name);
4782 else if (IS(g_func_pp->ret_type.name, "__int64"))
4783 fprintf(fout, " return ((u64)edx << 32) | eax;");
4785 fprintf(fout, " return eax;");
4787 last_arith_dst = NULL;
4788 delayed_flag_op = NULL;
4792 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4793 if (po->p_argnum != 0) {
4794 // special case - saved func arg
4795 fprintf(fout, " s_a%d = %s;", po->p_argnum, buf1);
4798 else if (po->flags & OPF_RSAVE) {
4799 fprintf(fout, " s_%s = %s;", buf1, buf1);
4802 else if (g_func_pp->is_userstack) {
4803 fprintf(fout, " *(--esp) = %s;", buf1);
4806 if (!(g_ida_func_attr & IDAFA_NORETURN))
4807 ferr(po, "stray push encountered\n");
4812 if (po->flags & OPF_RSAVE) {
4813 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4814 fprintf(fout, " %s = s_%s;", buf1, buf1);
4817 else if (po->datap != NULL) {
4820 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4821 fprintf(fout, " %s = %s;", buf1,
4822 out_src_opr(buf2, sizeof(buf2),
4823 tmp_op, &tmp_op->operand[0],
4824 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
4827 else if (g_func_pp->is_userstack) {
4828 fprintf(fout, " %s = *esp++;",
4829 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4833 ferr(po, "stray pop encountered\n");
4842 ferr(po, "unhandled op type %d, flags %x\n",
4847 if (g_comment[0] != 0) {
4848 char *p = g_comment;
4849 while (my_isblank(*p))
4851 fprintf(fout, " // %s", p);
4856 fprintf(fout, "\n");
4858 // some sanity checking
4859 if (po->flags & OPF_REP) {
4860 if (po->op != OP_STOS && po->op != OP_MOVS
4861 && po->op != OP_CMPS && po->op != OP_SCAS)
4862 ferr(po, "unexpected rep\n");
4863 if (!(po->flags & (OPF_REPZ|OPF_REPNZ))
4864 && (po->op == OP_CMPS || po->op == OP_SCAS))
4865 ferr(po, "cmps/scas with plain rep\n");
4867 if ((po->flags & (OPF_REPZ|OPF_REPNZ))
4868 && po->op != OP_CMPS && po->op != OP_SCAS)
4869 ferr(po, "unexpected repz/repnz\n");
4872 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
4874 // see is delayed flag stuff is still valid
4875 if (delayed_flag_op != NULL && delayed_flag_op != po) {
4876 if (is_any_opr_modified(delayed_flag_op, po, 0))
4877 delayed_flag_op = NULL;
4880 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
4881 if (is_opr_modified(last_arith_dst, po))
4882 last_arith_dst = NULL;
4888 if (g_stack_fsz && !g_stack_frame_used)
4889 fprintf(fout, " (void)sf;\n");
4891 fprintf(fout, "}\n\n");
4894 for (i = 0; i < opcnt; i++) {
4895 struct label_ref *lr, *lr_del;
4897 lr = g_label_refs[i].next;
4898 while (lr != NULL) {
4903 g_label_refs[i].i = -1;
4904 g_label_refs[i].next = NULL;
4906 if (ops[i].op == OP_CALL) {
4908 proto_release(ops[i].pp);
4914 static void set_label(int i, const char *name)
4920 p = strchr(name, ':');
4924 if (len > sizeof(g_labels[0]) - 1)
4925 aerr("label '%s' too long: %d\n", name, len);
4926 if (g_labels[i][0] != 0 && !IS_START(g_labels[i], "algn_"))
4927 aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
4928 memcpy(g_labels[i], name, len);
4929 g_labels[i][len] = 0;
4932 // '=' needs special treatment..
4933 static char *next_word_s(char *w, size_t wsize, char *s)
4939 for (i = 0; i < wsize - 1; i++) {
4940 if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
4946 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
4947 printf("warning: '%s' truncated\n", w);
4958 static struct chunk_item *func_chunks;
4959 static int func_chunk_cnt;
4960 static int func_chunk_alloc;
4962 static void add_func_chunk(FILE *fasm, const char *name, int line)
4964 if (func_chunk_cnt >= func_chunk_alloc) {
4965 func_chunk_alloc *= 2;
4966 func_chunks = realloc(func_chunks,
4967 func_chunk_alloc * sizeof(func_chunks[0]));
4968 my_assert_not(func_chunks, NULL);
4970 func_chunks[func_chunk_cnt].fptr = ftell(fasm);
4971 func_chunks[func_chunk_cnt].name = strdup(name);
4972 func_chunks[func_chunk_cnt].asmln = line;
4976 static int cmp_chunks(const void *p1, const void *p2)
4978 const struct chunk_item *c1 = p1, *c2 = p2;
4979 return strcmp(c1->name, c2->name);
4982 static int cmpstringp(const void *p1, const void *p2)
4984 return strcmp(*(char * const *)p1, *(char * const *)p2);
4987 static void scan_ahead(FILE *fasm)
4997 oldpos = ftell(fasm);
5000 while (fgets(line, sizeof(line), fasm))
5011 // get rid of random tabs
5012 for (i = 0; line[i] != 0; i++)
5013 if (line[i] == '\t')
5016 if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
5019 next_word(words[0], sizeof(words[0]), p);
5020 if (words[0][0] == 0)
5021 aerr("missing name for func chunk?\n");
5023 add_func_chunk(fasm, words[0], asmln);
5025 else if (IS_START(p, "; sctend"))
5031 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
5032 words[wordc][0] = 0;
5033 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
5034 if (*p == 0 || *p == ';') {
5040 if (wordc == 2 && IS(words[1], "ends"))
5044 fseek(fasm, oldpos, SEEK_SET);
5048 int main(int argc, char *argv[])
5050 FILE *fout, *fasm, *frlist;
5051 struct parsed_data *pd = NULL;
5053 char **rlist = NULL;
5055 int rlist_alloc = 0;
5056 int func_chunks_used = 0;
5057 int func_chunks_sorted = 0;
5058 int func_chunk_i = -1;
5059 long func_chunk_ret = 0;
5060 int func_chunk_ret_ln = 0;
5061 int scanned_ahead = 0;
5063 char words[20][256];
5064 enum opr_lenmod lmod;
5065 char *sctproto = NULL;
5067 int pending_endp = 0;
5069 int skip_warned = 0;
5082 for (arg = 1; arg < argc; arg++) {
5083 if (IS(argv[arg], "-v"))
5085 else if (IS(argv[arg], "-rf"))
5086 g_allow_regfunc = 1;
5087 else if (IS(argv[arg], "-m"))
5093 if (argc < arg + 3) {
5094 printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdrf> [rlist]*\n",
5101 asmfn = argv[arg++];
5102 fasm = fopen(asmfn, "r");
5103 my_assert_not(fasm, NULL);
5105 hdrfn = argv[arg++];
5106 g_fhdr = fopen(hdrfn, "r");
5107 my_assert_not(g_fhdr, NULL);
5110 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
5111 my_assert_not(rlist, NULL);
5112 // needs special handling..
5113 rlist[rlist_len++] = "__alloca_probe";
5115 func_chunk_alloc = 32;
5116 func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
5117 my_assert_not(func_chunks, NULL);
5119 memset(words, 0, sizeof(words));
5121 for (; arg < argc; arg++) {
5122 frlist = fopen(argv[arg], "r");
5123 my_assert_not(frlist, NULL);
5125 while (fgets(line, sizeof(line), frlist)) {
5127 if (*p == 0 || *p == ';')
5130 if (IS_START(p, "#if 0")
5131 || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
5135 else if (IS_START(p, "#endif"))
5142 p = next_word(words[0], sizeof(words[0]), p);
5143 if (words[0][0] == 0)
5146 if (rlist_len >= rlist_alloc) {
5147 rlist_alloc = rlist_alloc * 2 + 64;
5148 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
5149 my_assert_not(rlist, NULL);
5151 rlist[rlist_len++] = strdup(words[0]);
5160 qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
5162 fout = fopen(argv[arg_out], "w");
5163 my_assert_not(fout, NULL);
5166 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
5167 my_assert_not(g_eqs, NULL);
5169 for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
5170 g_label_refs[i].i = -1;
5171 g_label_refs[i].next = NULL;
5174 while (fgets(line, sizeof(line), fasm))
5183 // get rid of random tabs
5184 for (i = 0; line[i] != 0; i++)
5185 if (line[i] == '\t')
5190 if (p[2] == '=' && IS_START(p, "; =============== S U B"))
5191 goto do_pending_endp; // eww..
5193 if (p[2] == 'A' && IS_START(p, "; Attributes:"))
5195 static const char *attrs[] = {
5204 // parse IDA's attribute-list comment
5205 g_ida_func_attr = 0;
5208 for (; *p != 0; p = sskip(p)) {
5209 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
5210 if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
5211 g_ida_func_attr |= 1 << i;
5212 p += strlen(attrs[i]);
5216 if (i == ARRAY_SIZE(attrs)) {
5217 anote("unparsed IDA attr: %s\n", p);
5220 if (IS(attrs[i], "fpd=")) {
5221 p = next_word(words[0], sizeof(words[0]), p);
5226 else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
5229 next_word(words[0], sizeof(words[0]), p);
5230 if (words[0][0] == 0)
5231 aerr("missing name for func chunk?\n");
5233 if (!scanned_ahead) {
5234 add_func_chunk(fasm, words[0], asmln);
5235 func_chunks_sorted = 0;
5238 else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
5240 if (func_chunk_i >= 0) {
5241 if (func_chunk_i < func_chunk_cnt
5242 && IS(func_chunks[func_chunk_i].name, g_func))
5244 // move on to next chunk
5245 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
5247 aerr("seek failed for '%s' chunk #%d\n",
5248 g_func, func_chunk_i);
5249 asmln = func_chunks[func_chunk_i].asmln;
5253 if (func_chunk_ret == 0)
5254 aerr("no return from chunk?\n");
5255 fseek(fasm, func_chunk_ret, SEEK_SET);
5256 asmln = func_chunk_ret_ln;
5262 else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
5263 func_chunks_used = 1;
5265 if (IS_START(g_func, "sub_")) {
5266 unsigned long addr = strtoul(p, NULL, 16);
5267 unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
5268 if (addr > f_addr && !scanned_ahead) {
5269 anote("scan_ahead caused by '%s', addr %lx\n",
5273 func_chunks_sorted = 0;
5281 for (i = wordc; i < ARRAY_SIZE(words); i++)
5283 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
5284 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
5285 if (*p == 0 || *p == ';') {
5290 if (*p != 0 && *p != ';')
5291 aerr("too many words\n");
5293 // alow asm patches in comments
5295 if (IS_START(p, "; sctpatch:")) {
5297 if (*p == 0 || *p == ';')
5299 goto parse_words; // lame
5301 if (IS_START(p, "; sctproto:")) {
5302 sctproto = strdup(p + 11);
5304 else if (IS_START(p, "; sctend")) {
5313 awarn("wordc == 0?\n");
5317 // don't care about this:
5318 if (words[0][0] == '.'
5319 || IS(words[0], "include")
5320 || IS(words[0], "assume") || IS(words[1], "segment")
5321 || IS(words[0], "align"))
5327 // do delayed endp processing to collect switch jumptables
5329 if (in_func && !skip_func && !end && wordc >= 2
5330 && ((words[0][0] == 'd' && words[0][2] == 0)
5331 || (words[1][0] == 'd' && words[1][2] == 0)))
5334 if (words[1][0] == 'd' && words[1][2] == 0) {
5336 if (g_func_pd_cnt >= pd_alloc) {
5337 pd_alloc = pd_alloc * 2 + 16;
5338 g_func_pd = realloc(g_func_pd,
5339 sizeof(g_func_pd[0]) * pd_alloc);
5340 my_assert_not(g_func_pd, NULL);
5342 pd = &g_func_pd[g_func_pd_cnt];
5344 memset(pd, 0, sizeof(*pd));
5345 strcpy(pd->label, words[0]);
5346 pd->type = OPT_CONST;
5347 pd->lmod = lmod_from_directive(words[1]);
5353 anote("skipping alignment byte?\n");
5356 lmod = lmod_from_directive(words[0]);
5357 if (lmod != pd->lmod)
5358 aerr("lmod change? %d->%d\n", pd->lmod, lmod);
5361 if (pd->count_alloc < pd->count + wordc) {
5362 pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
5363 pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
5364 my_assert_not(pd->d, NULL);
5366 for (; i < wordc; i++) {
5367 if (IS(words[i], "offset")) {
5368 pd->type = OPT_OFFSET;
5371 p = strchr(words[i], ',');
5374 if (pd->type == OPT_OFFSET)
5375 pd->d[pd->count].u.label = strdup(words[i]);
5377 pd->d[pd->count].u.val = parse_number(words[i]);
5378 pd->d[pd->count].bt_i = -1;
5384 if (in_func && !skip_func)
5385 gen_func(fout, g_fhdr, g_func, pi);
5389 g_ida_func_attr = 0;
5393 func_chunks_used = 0;
5396 memset(&ops, 0, pi * sizeof(ops[0]));
5397 memset(g_labels, 0, pi * sizeof(g_labels[0]));
5401 for (i = 0; i < g_func_pd_cnt; i++) {
5403 if (pd->type == OPT_OFFSET) {
5404 for (j = 0; j < pd->count; j++)
5405 free(pd->d[j].u.label);
5419 if (IS(words[1], "proc")) {
5421 aerr("proc '%s' while in_func '%s'?\n",
5424 if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
5426 strcpy(g_func, words[0]);
5427 set_label(0, words[0]);
5432 if (IS(words[1], "endp"))
5435 aerr("endp '%s' while not in_func?\n", words[0]);
5436 if (!IS(g_func, words[0]))
5437 aerr("endp '%s' while in_func '%s'?\n",
5440 if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1
5441 && ops[0].op == OP_JMP && ops[0].operand[0].had_ds)
5447 if (!skip_func && func_chunks_used) {
5448 // start processing chunks
5449 struct chunk_item *ci, key = { g_func, 0 };
5451 func_chunk_ret = ftell(fasm);
5452 func_chunk_ret_ln = asmln;
5453 if (!func_chunks_sorted) {
5454 qsort(func_chunks, func_chunk_cnt,
5455 sizeof(func_chunks[0]), cmp_chunks);
5456 func_chunks_sorted = 1;
5458 ci = bsearch(&key, func_chunks, func_chunk_cnt,
5459 sizeof(func_chunks[0]), cmp_chunks);
5461 aerr("'%s' needs chunks, but none found\n", g_func);
5462 func_chunk_i = ci - func_chunks;
5463 for (; func_chunk_i > 0; func_chunk_i--)
5464 if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
5467 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
5469 aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
5470 asmln = func_chunks[func_chunk_i].asmln;
5478 if (wordc == 2 && IS(words[1], "ends")) {
5482 goto do_pending_endp;
5486 // scan for next text segment
5487 while (fgets(line, sizeof(line), fasm)) {
5490 if (*p == 0 || *p == ';')
5493 if (strstr(p, "segment para public 'CODE' use32"))
5500 p = strchr(words[0], ':');
5502 set_label(pi, words[0]);
5506 if (!in_func || skip_func) {
5507 if (!skip_warned && !skip_func && g_labels[pi][0] != 0) {
5509 anote("skipping from '%s'\n", g_labels[pi]);
5512 g_labels[pi][0] = 0;
5516 if (wordc > 1 && IS(words[1], "="))
5519 aerr("unhandled equ, wc=%d\n", wordc);
5520 if (g_eqcnt >= eq_alloc) {
5522 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
5523 my_assert_not(g_eqs, NULL);
5526 len = strlen(words[0]);
5527 if (len > sizeof(g_eqs[0].name) - 1)
5528 aerr("equ name too long: %d\n", len);
5529 strcpy(g_eqs[g_eqcnt].name, words[0]);
5531 if (!IS(words[3], "ptr"))
5532 aerr("unhandled equ\n");
5533 if (IS(words[2], "dword"))
5534 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
5535 else if (IS(words[2], "word"))
5536 g_eqs[g_eqcnt].lmod = OPLM_WORD;
5537 else if (IS(words[2], "byte"))
5538 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
5540 aerr("bad lmod: '%s'\n", words[2]);
5542 g_eqs[g_eqcnt].offset = parse_number(words[4]);
5547 if (pi >= ARRAY_SIZE(ops))
5548 aerr("too many ops\n");
5550 parse_op(&ops[pi], words, wordc);
5552 if (sctproto != NULL) {
5553 if (ops[pi].op == OP_CALL || ops[pi].op == OP_JMP)
5554 ops[pi].datap = sctproto;
5567 // vim:ts=2:shiftwidth=2:expandtab