9 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
10 #define IS(w, y) !strcmp(w, y)
11 #define IS_START(w, y) !strncmp(w, y, strlen(y))
13 #include "protoparse.h"
15 static const char *asmfn;
19 #define anote(fmt, ...) \
20 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
21 #define awarn(fmt, ...) \
22 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
23 #define aerr(fmt, ...) do { \
24 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
29 #include "masm_tools.h"
32 OPF_RMD = (1 << 0), /* removed or optimized out */
33 OPF_DATA = (1 << 1), /* data processing - writes to dst opr */
34 OPF_FLAGS = (1 << 2), /* sets flags */
35 OPF_JMP = (1 << 3), /* branch, call */
36 OPF_CJMP = (1 << 4), /* cond. branch (cc or jecxz) */
37 OPF_CC = (1 << 5), /* uses flags */
38 OPF_TAIL = (1 << 6), /* ret or tail call */
39 OPF_RSAVE = (1 << 7), /* push/pop is local reg save/load */
40 OPF_REP = (1 << 8), /* prefixed by rep */
41 OPF_REPZ = (1 << 9), /* rep is repe/repz */
42 OPF_REPNZ = (1 << 10), /* rep is repne/repnz */
43 OPF_FARG = (1 << 11), /* push collected as func arg (no reuse) */
44 OPF_EBP_S = (1 << 12), /* ebp used as scratch here, not BP */
45 OPF_DF = (1 << 13), /* DF flag set */
46 OPF_ATAIL = (1 << 14), /* tail call with reused arg frame */
47 OPF_32BIT = (1 << 15), /* 32bit division */
48 OPF_LOCK = (1 << 16), /* op has lock prefix */
112 // must be sorted (larger len must be further in enum)
120 #define MAX_OPERANDS 3
124 enum opr_lenmod lmod;
125 unsigned int is_ptr:1; // pointer in C
126 unsigned int is_array:1; // array in C
127 unsigned int type_from_var:1; // .. in header, sometimes wrong
128 unsigned int size_mismatch:1; // type override differs from C
129 unsigned int size_lt:1; // type override is larger than C
130 unsigned int had_ds:1; // had ds: prefix
131 const struct parsed_proto *pp; // for OPT_LABEL
139 struct parsed_opr operand[MAX_OPERANDS];
142 unsigned char pfo_inv;
143 unsigned char operand_cnt;
145 int regmask_src; // all referensed regs
147 int pfomask; // flagop: parsed_flag_op that can't be delayed
148 int argnum; // push: altered before call arg #
149 int cc_scratch; // scratch storage during analysis
150 int bt_i; // branch target for branches
151 struct parsed_data *btj;// branch targets for jumptables
152 struct parsed_proto *pp;// parsed_proto for OP_CALL
157 // OP_CALL - parser proto hint (str)
158 // (OPF_CC) - point to one of (OPF_FLAGS) that affects cc op
159 // OP_POP - point to OP_PUSH in push/pop pair
163 enum opr_lenmod lmod;
170 enum opr_lenmod lmod;
184 struct label_ref *next;
188 IDAFA_BP_FRAME = (1 << 0),
189 IDAFA_LIB_FUNC = (1 << 1),
190 IDAFA_STATIC = (1 << 2),
191 IDAFA_NORETURN = (1 << 3),
192 IDAFA_THUNK = (1 << 4),
193 IDAFA_FPD = (1 << 5),
198 static struct parsed_op ops[MAX_OPS];
199 static struct parsed_equ *g_eqs;
201 static char g_labels[MAX_OPS][48];
202 static struct label_ref g_label_refs[MAX_OPS];
203 static const struct parsed_proto *g_func_pp;
204 static struct parsed_data *g_func_pd;
205 static int g_func_pd_cnt;
206 static char g_func[256];
207 static char g_comment[256];
208 static int g_bp_frame;
209 static int g_sp_frame;
210 static int g_stack_frame_used;
211 static int g_stack_fsz;
212 static int g_ida_func_attr;
213 static int g_allow_regfunc;
214 #define ferr(op_, fmt, ...) do { \
215 printf("error:%s:#%zd: '%s': " fmt, g_func, (op_) - ops, \
216 dump_op(op_), ##__VA_ARGS__); \
220 #define fnote(op_, fmt, ...) \
221 printf("error:%s:#%zd: '%s': " fmt, g_func, (op_) - ops, \
222 dump_op(op_), ##__VA_ARGS__)
226 const char *regs_r32[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp" };
227 const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
228 const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
229 const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
231 enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
233 // possible basic comparison types (without inversion)
234 enum parsed_flag_op {
238 PFO_BE, // 6 CF=1||ZF=1
242 PFO_LE, // e ZF=1||SF!=OF
245 static const char *parsed_flag_op_names[] = {
246 "o", "c", "z", "be", "s", "p", "l", "le"
249 static int char_array_i(const char *array[], size_t len, const char *s)
253 for (i = 0; i < len; i++)
260 static void printf_number(char *buf, size_t buf_size,
261 unsigned long number)
263 // output in C-friendly form
264 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
267 static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
271 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
273 *reg_lmod = OPLM_DWORD;
276 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
278 *reg_lmod = OPLM_WORD;
281 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
283 *reg_lmod = OPLM_BYTE;
286 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
288 *reg_lmod = OPLM_BYTE;
295 static int parse_indmode(char *name, int *regmask, int need_c_cvt)
297 enum opr_lenmod lmod;
310 while (my_isblank(*s))
312 for (; my_issep(*s); d++, s++)
314 while (my_isblank(*s))
319 if (IS_START(s, "ds:"))
322 s = next_idt(w, sizeof(w), s);
327 reg = parse_reg(&lmod, w);
329 *regmask |= 1 << reg;
333 if ('0' <= w[0] && w[0] <= '9') {
334 number = parse_number(w);
335 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
339 // probably some label/identifier - pass
342 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
346 strcpy(name, cvtbuf);
351 static int is_reg_in_str(const char *s)
355 if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3])))
358 for (i = 0; i < ARRAY_SIZE(regs_r32); i++)
359 if (!strncmp(s, regs_r32[i], 3))
365 static const char *parse_stack_el(const char *name, char *extra_reg)
367 const char *p, *p2, *s;
374 if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) {
376 if (extra_reg != NULL) {
377 strncpy(extra_reg, name, 3);
382 if (IS_START(p, "ebp+")) {
386 if (p2 != NULL && is_reg_in_str(p)) {
387 if (extra_reg != NULL) {
388 strncpy(extra_reg, p, p2 - p);
389 extra_reg[p2 - p] = 0;
394 if (!('0' <= *p && *p <= '9'))
400 if (!IS_START(name, "esp+"))
406 if (is_reg_in_str(s)) {
407 if (extra_reg != NULL) {
408 strncpy(extra_reg, s, p - s);
409 extra_reg[p - s] = 0;
414 aerr("%s IDA stackvar not set?\n", __func__);
416 if (!('0' <= *s && *s <= '9')) {
417 aerr("%s IDA stackvar offset not set?\n", __func__);
420 if (s[0] == '0' && s[1] == 'x')
423 if (len < sizeof(buf) - 1) {
424 strncpy(buf, s, len);
426 val = strtol(buf, &endp, 16);
427 if (val == 0 || *endp != 0) {
428 aerr("%s num parse fail for '%s'\n", __func__, buf);
437 if ('0' <= *p && *p <= '9')
443 static int guess_lmod_from_name(struct parsed_opr *opr)
445 if (!strncmp(opr->name, "dword_", 6)) {
446 opr->lmod = OPLM_DWORD;
449 if (!strncmp(opr->name, "word_", 5)) {
450 opr->lmod = OPLM_WORD;
453 if (!strncmp(opr->name, "byte_", 5)) {
454 opr->lmod = OPLM_BYTE;
460 static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
461 const struct parsed_type *c_type)
463 static const char *dword_types[] = {
464 "int", "_DWORD", "UINT_PTR", "DWORD",
465 "WPARAM", "LPARAM", "UINT", "__int32",
468 static const char *word_types[] = {
469 "uint16_t", "int16_t", "_WORD",
470 "unsigned __int16", "__int16",
472 static const char *byte_types[] = {
473 "uint8_t", "int8_t", "char",
474 "unsigned __int8", "__int8", "BYTE", "_BYTE",
480 if (c_type->is_ptr) {
485 n = skip_type_mod(c_type->name);
487 for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
488 if (IS(n, dword_types[i])) {
494 for (i = 0; i < ARRAY_SIZE(word_types); i++) {
495 if (IS(n, word_types[i])) {
501 for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
502 if (IS(n, byte_types[i])) {
511 static char *default_cast_to(char *buf, size_t buf_size,
512 struct parsed_opr *opr)
518 if (opr->pp == NULL || opr->pp->type.name == NULL
521 snprintf(buf, buf_size, "%s", "(void *)");
525 snprintf(buf, buf_size, "(%s)", opr->pp->type.name);
529 static enum opr_type lmod_from_directive(const char *d)
533 else if (IS(d, "dw"))
535 else if (IS(d, "db"))
538 aerr("unhandled directive: '%s'\n", d);
542 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
548 *regmask |= 1 << reg;
551 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
554 static int parse_operand(struct parsed_opr *opr,
555 int *regmask, int *regmask_indirect,
556 char words[16][256], int wordc, int w, unsigned int op_flags)
558 const struct parsed_proto *pp;
559 enum opr_lenmod tmplmod;
560 unsigned long number;
568 aerr("parse_operand w %d, wordc %d\n", w, wordc);
572 for (i = w; i < wordc; i++) {
573 len = strlen(words[i]);
574 if (words[i][len - 1] == ',') {
575 words[i][len - 1] = 0;
581 wordc_in = wordc - w;
583 if ((op_flags & OPF_JMP) && wordc_in > 0
584 && !('0' <= words[w][0] && words[w][0] <= '9'))
586 const char *label = NULL;
588 if (wordc_in == 3 && !strncmp(words[w], "near", 4)
589 && IS(words[w + 1], "ptr"))
590 label = words[w + 2];
591 else if (wordc_in == 2 && IS(words[w], "short"))
592 label = words[w + 1];
593 else if (wordc_in == 1
594 && strchr(words[w], '[') == NULL
595 && parse_reg(&tmplmod, words[w]) < 0)
599 opr->type = OPT_LABEL;
600 if (IS_START(label, "ds:")) {
604 strcpy(opr->name, label);
610 if (IS(words[w + 1], "ptr")) {
611 if (IS(words[w], "dword"))
612 opr->lmod = OPLM_DWORD;
613 else if (IS(words[w], "word"))
614 opr->lmod = OPLM_WORD;
615 else if (IS(words[w], "byte"))
616 opr->lmod = OPLM_BYTE;
618 aerr("type parsing failed\n");
620 wordc_in = wordc - w;
625 if (IS(words[w], "offset")) {
626 opr->type = OPT_OFFSET;
627 strcpy(opr->name, words[w + 1]);
630 if (IS(words[w], "(offset")) {
631 p = strchr(words[w + 1], ')');
633 aerr("parse of bracketed offset failed\n");
635 opr->type = OPT_OFFSET;
636 strcpy(opr->name, words[w + 1]);
642 aerr("parse_operand 1 word expected\n");
644 if (IS_START(words[w], "ds:")) {
646 memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
648 strcpy(opr->name, words[w]);
650 if (words[w][0] == '[') {
651 opr->type = OPT_REGMEM;
652 ret = sscanf(words[w], "[%[^]]]", opr->name);
654 aerr("[] parse failure\n");
656 parse_indmode(opr->name, regmask_indirect, 1);
657 if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL)) {
659 struct parsed_equ *eq =
660 equ_find(NULL, parse_stack_el(opr->name, NULL), &i);
662 opr->lmod = eq->lmod;
666 else if (strchr(words[w], '[')) {
668 p = strchr(words[w], '[');
669 opr->type = OPT_REGMEM;
670 parse_indmode(p, regmask_indirect, 0);
671 strncpy(buf, words[w], p - words[w]);
672 buf[p - words[w]] = 0;
673 pp = proto_parse(g_fhdr, buf, 1);
676 else if (('0' <= words[w][0] && words[w][0] <= '9')
677 || words[w][0] == '-')
679 number = parse_number(words[w]);
680 opr->type = OPT_CONST;
682 printf_number(opr->name, sizeof(opr->name), number);
686 ret = parse_reg(&tmplmod, opr->name);
688 setup_reg_opr(opr, ret, tmplmod, regmask);
692 // most likely var in data segment
693 opr->type = OPT_LABEL;
694 pp = proto_parse(g_fhdr, opr->name, 0);
698 if (pp->is_fptr || pp->is_func) {
699 opr->lmod = OPLM_DWORD;
703 tmplmod = OPLM_UNSPEC;
704 if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
705 anote("unhandled C type '%s' for '%s'\n",
706 pp->type.name, opr->name);
708 if (opr->lmod == OPLM_UNSPEC) {
710 opr->type_from_var = 1;
712 else if (opr->lmod != tmplmod) {
713 opr->size_mismatch = 1;
714 if (tmplmod < opr->lmod)
717 opr->is_ptr = pp->type.is_ptr;
719 opr->is_array = pp->type.is_array;
723 if (opr->lmod == OPLM_UNSPEC)
724 guess_lmod_from_name(opr);
728 static const struct {
733 { "repe", OPF_REP|OPF_REPZ },
734 { "repz", OPF_REP|OPF_REPZ },
735 { "repne", OPF_REP|OPF_REPNZ },
736 { "repnz", OPF_REP|OPF_REPNZ },
737 { "lock", OPF_LOCK }, // ignored for now..
740 #define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC)
742 static const struct {
745 unsigned short minopr;
746 unsigned short maxopr;
749 unsigned char pfo_inv;
751 { "nop", OP_NOP, 0, 0, 0 },
752 { "push", OP_PUSH, 1, 1, 0 },
753 { "pop", OP_POP, 1, 1, OPF_DATA },
754 { "leave",OP_LEAVE, 0, 0, OPF_DATA },
755 { "mov" , OP_MOV, 2, 2, OPF_DATA },
756 { "lea", OP_LEA, 2, 2, OPF_DATA },
757 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
758 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
759 { "xchg", OP_XCHG, 2, 2, OPF_DATA },
760 { "not", OP_NOT, 1, 1, OPF_DATA },
761 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
762 { "lodsb",OP_LODS, 0, 0, OPF_DATA },
763 { "lodsw",OP_LODS, 0, 0, OPF_DATA },
764 { "lodsd",OP_LODS, 0, 0, OPF_DATA },
765 { "stosb",OP_STOS, 0, 0, OPF_DATA },
766 { "stosw",OP_STOS, 0, 0, OPF_DATA },
767 { "stosd",OP_STOS, 0, 0, OPF_DATA },
768 { "movsb",OP_MOVS, 0, 0, OPF_DATA },
769 { "movsw",OP_MOVS, 0, 0, OPF_DATA },
770 { "movsd",OP_MOVS, 0, 0, OPF_DATA },
771 { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
772 { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
773 { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
774 { "scasb",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
775 { "scasw",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
776 { "scasd",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
777 { "std", OP_STD, 0, 0, OPF_DATA }, // special flag
778 { "cld", OP_CLD, 0, 0, OPF_DATA },
779 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
780 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
781 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
782 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
783 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
784 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
785 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
786 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
787 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
788 { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS },
789 { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS },
790 { "rcl", OP_RCL, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
791 { "rcr", OP_RCR, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
792 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
793 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
794 { "bsf", OP_BSF, 2, 2, OPF_DATA|OPF_FLAGS },
795 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
796 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
797 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
798 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
799 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
800 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
801 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
802 { "test", OP_TEST, 2, 2, OPF_FLAGS },
803 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
804 { "retn", OP_RET, 0, 1, OPF_TAIL },
805 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
806 { "jmp", OP_JMP, 1, 1, OPF_JMP },
807 { "jecxz",OP_JECXZ, 1, 1, OPF_JMP|OPF_CJMP },
808 { "jo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 0 }, // 70 OF=1
809 { "jno", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 1 }, // 71 OF=0
810 { "jc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 CF=1
811 { "jb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72
812 { "jnc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 CF=0
813 { "jnb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
814 { "jae", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
815 { "jz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74 ZF=1
816 { "je", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74
817 { "jnz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75 ZF=0
818 { "jne", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75
819 { "jbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 CF=1||ZF=1
820 { "jna", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76
821 { "ja", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 CF=0&&ZF=0
822 { "jnbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77
823 { "js", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 0 }, // 78 SF=1
824 { "jns", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 1 }, // 79 SF=0
825 { "jp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a PF=1
826 { "jpe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a
827 { "jnp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b PF=0
828 { "jpo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b
829 { "jl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c SF!=OF
830 { "jnge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c
831 { "jge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d SF=OF
832 { "jnl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d
833 { "jle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e ZF=1||SF!=OF
834 { "jng", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e
835 { "jg", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f ZF=0&&SF=OF
836 { "jnle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f
837 { "seto", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 0 },
838 { "setno", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 1 },
839 { "setc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
840 { "setb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
841 { "setnc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
842 { "setae", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
843 { "setnb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
844 { "setz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
845 { "sete", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
846 { "setnz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
847 { "setne", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
848 { "setbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
849 { "setna", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
850 { "seta", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
851 { "setnbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
852 { "sets", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 0 },
853 { "setns", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 1 },
854 { "setp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
855 { "setpe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
856 { "setnp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
857 { "setpo", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
858 { "setl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
859 { "setnge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
860 { "setge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
861 { "setnl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
862 { "setle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
863 { "setng", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
864 { "setg", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
865 { "setnle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
868 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
870 enum opr_lenmod lmod = OPLM_UNSPEC;
871 int prefix_flags = 0;
879 for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
880 if (IS(words[w], pref_table[i].name)) {
881 prefix_flags = pref_table[i].flags;
888 aerr("lone prefix: '%s'\n", words[0]);
893 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
894 if (IS(words[w], op_table[i].name))
898 if (i == ARRAY_SIZE(op_table))
899 aerr("unhandled op: '%s'\n", words[0]);
902 op->op = op_table[i].op;
903 op->flags = op_table[i].flags | prefix_flags;
904 op->pfo = op_table[i].pfo;
905 op->pfo_inv = op_table[i].pfo_inv;
906 op->regmask_src = op->regmask_dst = 0;
908 for (opr = 0; opr < op_table[i].minopr; opr++) {
909 regmask = regmask_ind = 0;
910 w = parse_operand(&op->operand[opr], ®mask, ®mask_ind,
911 words, wordc, w, op->flags);
913 if (opr == 0 && (op->flags & OPF_DATA))
914 op->regmask_dst = regmask;
915 // for now, mark dst as src too
916 op->regmask_src |= regmask | regmask_ind;
919 for (; w < wordc && opr < op_table[i].maxopr; opr++) {
920 w = parse_operand(&op->operand[opr],
921 &op->regmask_src, &op->regmask_src,
922 words, wordc, w, op->flags);
926 aerr("parse_op %s incomplete: %d/%d\n",
930 op->operand_cnt = opr;
931 if (!strncmp(op_table[i].name, "set", 3))
932 op->operand[0].lmod = OPLM_BYTE;
934 // ops with implicit argumets
938 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
939 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
945 if (op->operand_cnt != 0)
947 if (words[op_w][4] == 'b')
949 else if (words[op_w][4] == 'w')
951 else if (words[op_w][4] == 'd')
954 setup_reg_opr(&op->operand[0], op->op == OP_LODS ? xSI : xDI,
955 lmod, &op->regmask_src);
956 setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src);
957 op->regmask_dst = op->regmask_src;
958 setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD,
959 op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src);
964 if (op->operand_cnt != 0)
966 if (words[op_w][4] == 'b')
968 else if (words[op_w][4] == 'w')
970 else if (words[op_w][4] == 'd')
973 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
974 setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src);
975 setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src);
976 op->regmask_dst = op->regmask_src;
980 op->regmask_src |= op->regmask_dst;
981 op->regmask_dst |= op->regmask_src;
986 op->regmask_src = 1 << xCX;
987 op->operand[0].type = OPT_REG;
988 op->operand[0].reg = xCX;
989 op->operand[0].lmod = OPLM_DWORD;
993 if (op->operand_cnt != 1)
998 op->regmask_dst = (1 << xDX) | (1 << xAX);
999 op->regmask_src |= (1 << xAX);
1000 if (op->operand[0].lmod == OPLM_UNSPEC)
1001 op->operand[0].lmod = OPLM_DWORD;
1006 // we could set up operands for edx:eax, but there is no real need to
1007 // (see is_opr_modified())
1008 regmask = (1 << xDX) | (1 << xAX);
1009 op->regmask_dst = regmask;
1010 op->regmask_src |= regmask;
1011 if (op->operand[0].lmod == OPLM_UNSPEC)
1012 op->operand[0].lmod = OPLM_DWORD;
1020 if (op->operand[1].lmod == OPLM_UNSPEC)
1021 op->operand[1].lmod = OPLM_BYTE;
1025 if (op->operand[0].lmod == OPLM_UNSPEC
1026 && (op->operand[0].type == OPT_CONST
1027 || op->operand[0].type == OPT_OFFSET
1028 || op->operand[0].type == OPT_LABEL))
1029 op->operand[0].lmod = OPLM_DWORD;
1035 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1036 && op->operand[0].lmod == op->operand[1].lmod
1037 && op->operand[0].reg == op->operand[1].reg
1038 && IS(op->operand[0].name, op->operand[1].name)) // ah, al..
1040 op->flags |= OPF_RMD;
1041 op->regmask_src = op->regmask_dst = 0;
1046 if (op->operand[0].type == OPT_REG
1047 && op->operand[1].type == OPT_REGMEM)
1050 snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
1051 if (IS(buf, op->operand[1].name))
1052 op->flags |= OPF_RMD;
1057 // trashed regs must be explicitly detected later
1058 op->regmask_dst = 0;
1066 static const char *op_name(struct parsed_op *po)
1068 static char buf[16];
1072 if (po->op == OP_JCC || po->op == OP_SCC) {
1074 *p++ = (po->op == OP_JCC) ? 'j' : 's';
1077 strcpy(p, parsed_flag_op_names[po->pfo]);
1081 for (i = 0; i < ARRAY_SIZE(op_table); i++)
1082 if (op_table[i].op == po->op)
1083 return op_table[i].name;
1089 static const char *dump_op(struct parsed_op *po)
1091 static char out[128];
1098 snprintf(out, sizeof(out), "%s", op_name(po));
1099 for (i = 0; i < po->operand_cnt; i++) {
1103 snprintf(p, sizeof(out) - (p - out),
1104 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1105 po->operand[i].name);
1111 static const char *lmod_type_u(struct parsed_op *po,
1112 enum opr_lenmod lmod)
1122 ferr(po, "invalid lmod: %d\n", lmod);
1123 return "(_invalid_)";
1127 static const char *lmod_cast_u(struct parsed_op *po,
1128 enum opr_lenmod lmod)
1138 ferr(po, "invalid lmod: %d\n", lmod);
1139 return "(_invalid_)";
1143 static const char *lmod_cast_u_ptr(struct parsed_op *po,
1144 enum opr_lenmod lmod)
1154 ferr(po, "invalid lmod: %d\n", lmod);
1155 return "(_invalid_)";
1159 static const char *lmod_cast_s(struct parsed_op *po,
1160 enum opr_lenmod lmod)
1170 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1171 return "(_invalid_)";
1175 static const char *lmod_cast(struct parsed_op *po,
1176 enum opr_lenmod lmod, int is_signed)
1179 lmod_cast_s(po, lmod) :
1180 lmod_cast_u(po, lmod);
1183 static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1193 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1198 static const char *opr_name(struct parsed_op *po, int opr_num)
1200 if (opr_num >= po->operand_cnt)
1201 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1202 return po->operand[opr_num].name;
1205 static unsigned int opr_const(struct parsed_op *po, int opr_num)
1207 if (opr_num >= po->operand_cnt)
1208 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1209 if (po->operand[opr_num].type != OPT_CONST)
1210 ferr(po, "opr %d: const expected\n", opr_num);
1211 return po->operand[opr_num].val;
1214 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1216 if ((unsigned int)popr->reg >= MAX_REGS)
1217 ferr(po, "invalid reg: %d\n", popr->reg);
1218 return regs_r32[popr->reg];
1221 // cast1 is the "final" cast
1222 static const char *simplify_cast(const char *cast1, const char *cast2)
1224 static char buf[256];
1230 if (IS(cast1, cast2))
1232 if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
1234 if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
1236 if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
1238 if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
1240 if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1243 snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1247 static const char *simplify_cast_num(const char *cast, unsigned int val)
1249 if (IS(cast, "(u8)") && val < 0x100)
1251 if (IS(cast, "(s8)") && val < 0x80)
1253 if (IS(cast, "(u16)") && val < 0x10000)
1255 if (IS(cast, "(s16)") && val < 0x8000)
1257 if (IS(cast, "(s32)") && val < 0x80000000)
1263 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1272 namelen = strlen(name);
1274 p = strchr(name, '+');
1278 ferr(po, "equ parse failed for '%s'\n", name);
1280 if (IS_START(p, "0x"))
1282 *extra_offs = strtol(p, &endp, 16);
1284 ferr(po, "equ parse failed for '%s'\n", name);
1287 for (i = 0; i < g_eqcnt; i++)
1288 if (strncmp(g_eqs[i].name, name, namelen) == 0
1289 && g_eqs[i].name[namelen] == 0)
1293 ferr(po, "unresolved equ name: '%s'\n", name);
1300 static int is_stack_access(struct parsed_op *po,
1301 const struct parsed_opr *popr)
1303 return (parse_stack_el(popr->name, NULL)
1304 || (g_bp_frame && !(po->flags & OPF_EBP_S)
1305 && IS_START(popr->name, "ebp")));
1308 static void parse_stack_access(struct parsed_op *po,
1309 const char *name, char *ofs_reg, int *offset_out,
1310 int *stack_ra_out, const char **bp_arg_out, int is_lea)
1312 const char *bp_arg = "";
1313 const char *p = NULL;
1314 struct parsed_equ *eq;
1321 if (IS_START(name, "ebp-")
1322 || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1325 if (IS_START(p, "0x"))
1327 offset = strtoul(p, &endp, 16);
1331 ferr(po, "ebp- parse of '%s' failed\n", name);
1334 bp_arg = parse_stack_el(name, ofs_reg);
1335 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1336 eq = equ_find(po, bp_arg, &offset);
1338 ferr(po, "detected but missing eq\n");
1339 offset += eq->offset;
1342 if (!strncmp(name, "ebp", 3))
1345 // yes it sometimes LEAs ra for compares..
1346 if (!is_lea && ofs_reg[0] == 0
1347 && stack_ra <= offset && offset < stack_ra + 4)
1349 ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
1352 *offset_out = offset;
1353 *stack_ra_out = stack_ra;
1355 *bp_arg_out = bp_arg;
1358 static void stack_frame_access(struct parsed_op *po,
1359 struct parsed_opr *popr, char *buf, size_t buf_size,
1360 const char *name, const char *cast, int is_src, int is_lea)
1362 enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1363 const char *prefix = "";
1364 const char *bp_arg = NULL;
1365 char ofs_reg[16] = { 0, };
1366 int i, arg_i, arg_s;
1373 if (po->flags & OPF_EBP_S)
1374 ferr(po, "stack_frame_access while ebp is scratch\n");
1376 parse_stack_access(po, name, ofs_reg, &offset,
1377 &stack_ra, &bp_arg, is_lea);
1379 if (offset > stack_ra)
1381 arg_i = (offset - stack_ra - 4) / 4;
1382 if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
1384 if (g_func_pp->is_vararg
1385 && arg_i == g_func_pp->argc_stack && is_lea)
1387 // should be va_list
1390 snprintf(buf, buf_size, "%sap", cast);
1393 ferr(po, "offset %d (%s,%d) doesn't map to any arg\n",
1394 offset, bp_arg, arg_i);
1396 if (ofs_reg[0] != 0)
1397 ferr(po, "offset reg on arg access?\n");
1399 for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1400 if (g_func_pp->arg[i].reg != NULL)
1406 if (i == g_func_pp->argc)
1407 ferr(po, "arg %d not in prototype?\n", arg_i);
1409 popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
1415 ferr(po, "lea/byte to arg?\n");
1416 if (is_src && (offset & 3) == 0)
1417 snprintf(buf, buf_size, "%sa%d",
1418 simplify_cast(cast, "(u8)"), i + 1);
1420 snprintf(buf, buf_size, "%sBYTE%d(a%d)",
1421 cast, offset & 3, i + 1);
1426 ferr(po, "lea/word to arg?\n");
1431 ferr(po, "problematic arg store\n");
1432 snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
1433 simplify_cast(cast, "*(u16 *)"), i + 1);
1436 ferr(po, "unaligned arg word load\n");
1438 else if (is_src && (offset & 2) == 0)
1439 snprintf(buf, buf_size, "%sa%d",
1440 simplify_cast(cast, "(u16)"), i + 1);
1442 snprintf(buf, buf_size, "%s%sWORD(a%d)",
1443 cast, (offset & 2) ? "HI" : "LO", i + 1);
1455 snprintf(buf, buf_size, "(u32)&a%d + %d",
1458 ferr(po, "unaligned arg store\n");
1460 // mov edx, [ebp+arg_4+2]; movsx ecx, dx
1461 snprintf(buf, buf_size, "%s(a%d >> %d)",
1462 prefix, i + 1, (offset & 3) * 8);
1466 snprintf(buf, buf_size, "%s%sa%d",
1467 prefix, is_lea ? "&" : "", i + 1);
1472 ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
1476 snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
1479 guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
1480 if (tmp_lmod != OPLM_DWORD
1481 && (unaligned || (!is_src && tmp_lmod < popr->lmod)))
1483 ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
1484 i + 1, offset, g_func_pp->arg[i].type.name);
1486 // can't check this because msvc likes to reuse
1487 // arg space for scratch..
1488 //if (popr->is_ptr && popr->lmod != OPLM_DWORD)
1489 // ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
1493 if (g_stack_fsz == 0)
1494 ferr(po, "stack var access without stackframe\n");
1495 g_stack_frame_used = 1;
1497 sf_ofs = g_stack_fsz + offset;
1498 lim = (ofs_reg[0] != 0) ? -4 : 0;
1499 if (offset > 0 || sf_ofs < lim)
1500 ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
1510 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1511 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1515 if ((sf_ofs & 1) || ofs_reg[0] != 0) {
1516 // known unaligned or possibly unaligned
1517 strcat(g_comment, " unaligned");
1519 prefix = "*(u16 *)&";
1520 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1521 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1524 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
1528 if ((sf_ofs & 3) || ofs_reg[0] != 0) {
1529 // known unaligned or possibly unaligned
1530 strcat(g_comment, " unaligned");
1532 prefix = "*(u32 *)&";
1533 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1534 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1537 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
1541 ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
1546 static void check_func_pp(struct parsed_op *po,
1547 const struct parsed_proto *pp, const char *pfx)
1551 if (pp->argc_reg != 0) {
1552 if (/*!g_allow_regfunc &&*/ !pp->is_fastcall) {
1553 pp_print(buf, sizeof(buf), pp);
1554 ferr(po, "%s: unexpected reg arg in icall: %s\n", pfx, buf);
1556 if (pp->argc_stack > 0 && pp->argc_reg != 2)
1557 ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
1558 pfx, pp->argc_reg, pp->argc_stack);
1562 static const char *check_label_read_ref(struct parsed_op *po,
1565 const struct parsed_proto *pp;
1567 pp = proto_parse(g_fhdr, name, 0);
1569 ferr(po, "proto_parse failed for ref '%s'\n", name);
1572 check_func_pp(po, pp, "ref");
1577 static char *out_src_opr(char *buf, size_t buf_size,
1578 struct parsed_op *po, struct parsed_opr *popr, const char *cast,
1581 char tmp1[256], tmp2[256];
1590 switch (popr->type) {
1593 ferr(po, "lea from reg?\n");
1595 switch (popr->lmod) {
1597 snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
1600 snprintf(buf, buf_size, "%s%s",
1601 simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
1604 if (popr->name[1] == 'h') // XXX..
1605 snprintf(buf, buf_size, "%s(%s >> 8)",
1606 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1608 snprintf(buf, buf_size, "%s%s",
1609 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1612 ferr(po, "invalid src lmod: %d\n", popr->lmod);
1617 if (is_stack_access(po, popr)) {
1618 stack_frame_access(po, popr, buf, buf_size,
1619 popr->name, cast, 1, is_lea);
1623 strcpy(expr, popr->name);
1624 if (strchr(expr, '[')) {
1625 // special case: '[' can only be left for label[reg] form
1626 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
1628 ferr(po, "parse failure for '%s'\n", expr);
1629 if (tmp1[0] == '(') {
1630 // (off_4FFF50+3)[eax]
1631 p = strchr(tmp1 + 1, ')');
1632 if (p == NULL || p[1] != 0)
1633 ferr(po, "parse failure (2) for '%s'\n", expr);
1635 memmove(tmp1, tmp1 + 1, strlen(tmp1));
1637 snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
1640 // XXX: do we need more parsing?
1642 snprintf(buf, buf_size, "%s", expr);
1646 snprintf(buf, buf_size, "%s(%s)",
1647 simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
1651 name = check_label_read_ref(po, popr->name);
1652 if (cast[0] == 0 && popr->is_ptr)
1656 snprintf(buf, buf_size, "(u32)&%s", name);
1657 else if (popr->size_lt)
1658 snprintf(buf, buf_size, "%s%s%s%s", cast,
1659 lmod_cast_u_ptr(po, popr->lmod),
1660 popr->is_array ? "" : "&", name);
1662 snprintf(buf, buf_size, "%s%s%s", cast, name,
1663 popr->is_array ? "[0]" : "");
1667 name = check_label_read_ref(po, popr->name);
1671 ferr(po, "lea an offset?\n");
1672 snprintf(buf, buf_size, "%s&%s", cast, name);
1677 ferr(po, "lea from const?\n");
1679 printf_number(tmp1, sizeof(tmp1), popr->val);
1680 if (popr->val == 0 && strchr(cast, '*'))
1681 snprintf(buf, buf_size, "NULL");
1683 snprintf(buf, buf_size, "%s%s",
1684 simplify_cast_num(cast, popr->val), tmp1);
1688 ferr(po, "invalid src type: %d\n", popr->type);
1694 // note: may set is_ptr (we find that out late for ebp frame..)
1695 static char *out_dst_opr(char *buf, size_t buf_size,
1696 struct parsed_op *po, struct parsed_opr *popr)
1698 switch (popr->type) {
1700 switch (popr->lmod) {
1702 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
1706 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
1710 if (popr->name[1] == 'h') // XXX..
1711 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
1713 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
1716 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
1721 if (is_stack_access(po, popr)) {
1722 stack_frame_access(po, popr, buf, buf_size,
1723 popr->name, "", 0, 0);
1727 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1730 if (popr->size_mismatch)
1731 snprintf(buf, buf_size, "%s%s%s",
1732 lmod_cast_u_ptr(po, popr->lmod),
1733 popr->is_array ? "" : "&", popr->name);
1735 snprintf(buf, buf_size, "%s%s", popr->name,
1736 popr->is_array ? "[0]" : "");
1740 ferr(po, "invalid dst type: %d\n", popr->type);
1746 static char *out_src_opr_u32(char *buf, size_t buf_size,
1747 struct parsed_op *po, struct parsed_opr *popr)
1749 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1752 static void out_test_for_cc(char *buf, size_t buf_size,
1753 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1754 enum opr_lenmod lmod, const char *expr)
1756 const char *cast, *scast;
1758 cast = lmod_cast_u(po, lmod);
1759 scast = lmod_cast_s(po, lmod);
1763 case PFO_BE: // CF=1||ZF=1; CF=0
1764 snprintf(buf, buf_size, "(%s%s %s 0)",
1765 cast, expr, is_inv ? "!=" : "==");
1769 case PFO_L: // SF!=OF; OF=0
1770 snprintf(buf, buf_size, "(%s%s %s 0)",
1771 scast, expr, is_inv ? ">=" : "<");
1774 case PFO_LE: // ZF=1||SF!=OF; OF=0
1775 snprintf(buf, buf_size, "(%s%s %s 0)",
1776 scast, expr, is_inv ? ">" : "<=");
1780 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1784 static void out_cmp_for_cc(char *buf, size_t buf_size,
1785 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1787 const char *cast, *scast, *cast_use;
1788 char buf1[256], buf2[256];
1789 enum opr_lenmod lmod;
1791 if (po->operand[0].lmod != po->operand[1].lmod)
1792 ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
1793 po->operand[0].lmod, po->operand[1].lmod);
1794 lmod = po->operand[0].lmod;
1796 cast = lmod_cast_u(po, lmod);
1797 scast = lmod_cast_s(po, lmod);
1813 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1816 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
1817 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
1821 // note: must be unsigned compare
1822 snprintf(buf, buf_size, "(%s %s %s)",
1823 buf1, is_inv ? ">=" : "<", buf2);
1827 snprintf(buf, buf_size, "(%s %s %s)",
1828 buf1, is_inv ? "!=" : "==", buf2);
1832 // note: must be unsigned compare
1833 snprintf(buf, buf_size, "(%s %s %s)",
1834 buf1, is_inv ? ">" : "<=", buf2);
1837 if (is_inv && lmod == OPLM_BYTE
1838 && po->operand[1].type == OPT_CONST
1839 && po->operand[1].val == 0xff)
1841 snprintf(g_comment, sizeof(g_comment), "if %s", buf);
1842 snprintf(buf, buf_size, "(0)");
1846 // note: must be signed compare
1848 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
1849 scast, buf1, buf2, is_inv ? ">=" : "<");
1853 snprintf(buf, buf_size, "(%s %s %s)",
1854 buf1, is_inv ? ">=" : "<", buf2);
1858 snprintf(buf, buf_size, "(%s %s %s)",
1859 buf1, is_inv ? ">" : "<=", buf2);
1867 static void out_cmp_test(char *buf, size_t buf_size,
1868 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1870 char buf1[256], buf2[256], buf3[256];
1872 if (po->op == OP_TEST) {
1873 if (IS(opr_name(po, 0), opr_name(po, 1))) {
1874 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
1877 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
1878 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
1879 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
1881 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
1882 po->operand[0].lmod, buf3);
1884 else if (po->op == OP_CMP) {
1885 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
1888 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
1891 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
1892 struct parsed_opr *popr2)
1894 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
1895 ferr(po, "missing lmod for both operands\n");
1897 if (popr1->lmod == OPLM_UNSPEC)
1898 popr1->lmod = popr2->lmod;
1899 else if (popr2->lmod == OPLM_UNSPEC)
1900 popr2->lmod = popr1->lmod;
1901 else if (popr1->lmod != popr2->lmod) {
1902 if (popr1->type_from_var) {
1903 popr1->size_mismatch = 1;
1904 if (popr1->lmod < popr2->lmod)
1906 popr1->lmod = popr2->lmod;
1908 else if (popr2->type_from_var) {
1909 popr2->size_mismatch = 1;
1910 if (popr2->lmod < popr1->lmod)
1912 popr2->lmod = popr1->lmod;
1915 ferr(po, "conflicting lmods: %d vs %d\n",
1916 popr1->lmod, popr2->lmod);
1920 static const char *op_to_c(struct parsed_op *po)
1944 ferr(po, "op_to_c was supplied with %d\n", po->op);
1948 static void op_set_clear_flag(struct parsed_op *po,
1949 enum op_flags flag_set, enum op_flags flag_clear)
1951 po->flags |= flag_set;
1952 po->flags &= ~flag_clear;
1955 // last op in stream - unconditional branch or ret
1956 #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
1957 || ((ops[_i].flags & (OPF_JMP|OPF_CJMP|OPF_RMD)) == OPF_JMP \
1958 && ops[_i].op != OP_CALL))
1960 static int scan_for_pop(int i, int opcnt, const char *reg,
1961 int magic, int depth, int *maxdepth, int do_flags)
1963 const struct parsed_proto *pp;
1964 struct parsed_op *po;
1968 for (; i < opcnt; i++) {
1970 if (po->cc_scratch == magic)
1971 break; // already checked
1972 po->cc_scratch = magic;
1974 if (po->flags & OPF_TAIL) {
1975 if (po->op == OP_CALL) {
1976 pp = proto_parse(g_fhdr, po->operand[0].name, 0);
1977 if (pp != NULL && pp->is_noreturn)
1978 // no stack cleanup for noreturn
1981 return -1; // deadend
1984 if ((po->flags & OPF_RMD)
1985 || (po->op == OP_PUSH && po->argnum != 0)) // arg push
1988 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
1989 if (po->btj != NULL) {
1991 for (j = 0; j < po->btj->count; j++) {
1992 ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic,
1993 depth, maxdepth, do_flags);
1995 return ret; // dead end
2001 ferr(po, "dead branch\n");
2005 if (po->flags & OPF_CJMP) {
2006 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic,
2007 depth, maxdepth, do_flags);
2009 return ret; // dead end
2017 if ((po->op == OP_POP || po->op == OP_PUSH)
2018 && po->operand[0].type == OPT_REG
2019 && IS(po->operand[0].name, reg))
2021 if (po->op == OP_PUSH && !(po->flags & OPF_FARG)) {
2023 if (depth > *maxdepth)
2026 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2028 else if (po->op == OP_POP) {
2031 op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
2036 if (depth < 0) // should not happen
2037 ferr(po, "fail with depth\n");
2039 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2048 // scan for pop starting from 'ret' op (all paths)
2049 static int scan_for_pop_ret(int i, int opcnt, const char *reg,
2055 for (; i < opcnt; i++) {
2056 if (!(ops[i].flags & OPF_TAIL))
2059 for (j = i - 1; j >= 0; j--) {
2060 if (ops[j].flags & OPF_RMD)
2062 if (ops[j].flags & OPF_JMP)
2065 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
2066 && IS(ops[j].operand[0].name, reg))
2069 ops[j].flags |= flag_set;
2073 if (g_labels[j][0] != 0)
2078 return found ? 0 : -1;
2081 static void scan_propagate_df(int i, int opcnt)
2083 struct parsed_op *po = &ops[i];
2086 for (; i < opcnt; i++) {
2088 if (po->flags & OPF_DF)
2089 return; // already resolved
2090 po->flags |= OPF_DF;
2092 if (po->op == OP_CALL)
2093 ferr(po, "call with DF set?\n");
2095 if (po->flags & OPF_JMP) {
2096 if (po->btj != NULL) {
2098 for (j = 0; j < po->btj->count; j++)
2099 scan_propagate_df(po->btj->d[j].bt_i, opcnt);
2104 ferr(po, "dead branch\n");
2108 if (po->flags & OPF_CJMP)
2109 scan_propagate_df(po->bt_i, opcnt);
2115 if (po->flags & OPF_TAIL)
2118 if (po->op == OP_CLD) {
2119 po->flags |= OPF_RMD;
2124 ferr(po, "missing DF clear?\n");
2127 // is operand 'opr' modified by parsed_op 'po'?
2128 static int is_opr_modified(const struct parsed_opr *opr,
2129 const struct parsed_op *po)
2133 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2136 if (opr->type == OPT_REG) {
2137 if (po->op == OP_CALL) {
2138 mask = (1 << xAX) | (1 << xCX) | (1 << xDX);
2139 if ((1 << opr->reg) & mask)
2145 if (po->operand[0].type == OPT_REG) {
2146 if (po->regmask_dst & (1 << opr->reg))
2153 return IS(po->operand[0].name, opr->name);
2156 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
2157 static int is_any_opr_modified(const struct parsed_op *po_test,
2158 const struct parsed_op *po, int c_mode)
2163 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2166 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2169 if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
2172 // in reality, it can wreck any register, but in decompiled C
2173 // version it can only overwrite eax or edx:eax
2174 mask = (1 << xAX) | (1 << xDX);
2178 if (po->op == OP_CALL
2179 && ((po_test->regmask_src | po_test->regmask_dst) & mask))
2182 for (i = 0; i < po_test->operand_cnt; i++)
2183 if (IS(po_test->operand[i].name, po->operand[0].name))
2189 // scan for any po_test operand modification in range given
2190 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
2193 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2196 for (; i < opcnt; i++) {
2197 if (is_any_opr_modified(po_test, &ops[i], c_mode))
2204 // scan for po_test operand[0] modification in range given
2205 static int scan_for_mod_opr0(struct parsed_op *po_test,
2208 for (; i < opcnt; i++) {
2209 if (is_opr_modified(&po_test->operand[0], &ops[i]))
2216 static int scan_for_flag_set(int i, int magic, int *branched,
2217 int *setters, int *setter_cnt)
2219 struct label_ref *lr;
2223 if (ops[i].cc_scratch == magic) {
2224 ferr(&ops[i], "%s looped\n", __func__);
2227 ops[i].cc_scratch = magic;
2229 if (g_labels[i][0] != 0) {
2232 lr = &g_label_refs[i];
2233 for (; lr->next; lr = lr->next) {
2234 ret = scan_for_flag_set(lr->i, magic,
2235 branched, setters, setter_cnt);
2240 if (i > 0 && LAST_OP(i - 1)) {
2244 ret = scan_for_flag_set(lr->i, magic,
2245 branched, setters, setter_cnt);
2251 if (ops[i].flags & OPF_FLAGS) {
2252 setters[*setter_cnt] = i;
2257 if ((ops[i].flags & (OPF_JMP|OPF_CJMP)) == OPF_JMP)
2264 // scan back for cdq, if anything modifies edx, fail
2265 static int scan_for_cdq_edx(int i)
2268 if (g_labels[i][0] != 0) {
2269 if (g_label_refs[i].next != NULL)
2271 if (i > 0 && LAST_OP(i - 1)) {
2272 i = g_label_refs[i].i;
2279 if (ops[i].op == OP_CDQ)
2282 if (ops[i].regmask_dst & (1 << xDX))
2289 static int scan_for_reg_clear(int i, int reg)
2292 if (g_labels[i][0] != 0) {
2293 if (g_label_refs[i].next != NULL)
2295 if (i > 0 && LAST_OP(i - 1)) {
2296 i = g_label_refs[i].i;
2303 if (ops[i].op == OP_XOR
2304 && ops[i].operand[0].lmod == OPLM_DWORD
2305 && ops[i].operand[0].reg == ops[i].operand[1].reg
2306 && ops[i].operand[0].reg == reg)
2309 if (ops[i].regmask_dst & (1 << reg))
2316 // scan for positive, constant esp adjust
2317 static int scan_for_esp_adjust(int i, int opcnt, int *adj,
2320 struct parsed_op *po;
2323 *adj = *multipath = 0;
2325 for (; i < opcnt; i++) {
2328 if (g_labels[i][0] != 0)
2331 if (po->op == OP_ADD && po->operand[0].reg == xSP) {
2332 if (po->operand[1].type != OPT_CONST)
2333 ferr(&ops[i], "non-const esp adjust?\n");
2334 *adj += po->operand[1].val;
2336 ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
2339 else if (po->op == OP_PUSH && !(po->flags & OPF_RMD)) {
2340 //if (first_pop == -1)
2341 // first_pop = -2; // none
2342 *adj -= lmod_bytes(po, po->operand[0].lmod);
2344 else if (po->op == OP_POP && !(po->flags & OPF_RMD)) {
2345 // seems like msvc only uses 'pop ecx' for stack realignment..
2346 if (po->operand[0].type != OPT_REG || po->operand[0].reg != xCX)
2348 if (first_pop == -1 && *adj >= 0)
2350 *adj += lmod_bytes(po, po->operand[0].lmod);
2352 else if (po->flags & (OPF_JMP|OPF_TAIL)) {
2353 if (po->op == OP_JMP && po->btj == NULL) {
2357 if (po->op != OP_CALL)
2359 if (po->operand[0].type != OPT_LABEL)
2361 if (po->pp != NULL && po->pp->is_stdcall)
2366 if (first_pop >= 0) {
2367 // probably 'pop ecx' was used..
2374 static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
2376 struct parsed_op *po;
2380 ferr(ops, "%s: followed bad branch?\n", __func__);
2382 for (; i < opcnt; i++) {
2384 if (po->cc_scratch == magic)
2386 po->cc_scratch = magic;
2389 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2390 if (po->btj != NULL) {
2392 for (j = 0; j < po->btj->count; j++)
2393 scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
2397 scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
2398 if (!(po->flags & OPF_CJMP))
2401 if (po->flags & OPF_TAIL)
2406 static const struct parsed_proto *try_recover_pp(
2407 struct parsed_op *po, const struct parsed_opr *opr, int *search_instead)
2409 const struct parsed_proto *pp = NULL;
2413 // maybe an arg of g_func?
2414 if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
2416 char ofs_reg[16] = { 0, };
2417 int arg, arg_s, arg_i;
2421 parse_stack_access(po, opr->name, ofs_reg,
2422 &offset, &stack_ra, NULL, 0);
2423 if (ofs_reg[0] != 0)
2424 ferr(po, "offset reg on arg access?\n");
2425 if (offset <= stack_ra) {
2426 // search who set the stack var instead
2427 if (search_instead != NULL)
2428 *search_instead = 1;
2432 arg_i = (offset - stack_ra - 4) / 4;
2433 for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
2434 if (g_func_pp->arg[arg].reg != NULL)
2440 if (arg == g_func_pp->argc)
2441 ferr(po, "stack arg %d not in prototype?\n", arg_i);
2443 pp = g_func_pp->arg[arg].fptr;
2445 ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1);
2446 check_func_pp(po, pp, "icall arg");
2448 else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
2450 p = strchr(opr->name + 1, '[');
2451 memcpy(buf, opr->name, p - opr->name);
2452 buf[p - opr->name] = 0;
2453 pp = proto_parse(g_fhdr, buf, 0);
2455 else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
2456 pp = proto_parse(g_fhdr, opr->name, 0);
2458 ferr(po, "proto_parse failed for icall from '%s'\n", opr->name);
2459 check_func_pp(po, pp, "reg-fptr ref");
2465 static void scan_for_call_type(int i, const struct parsed_opr *opr,
2466 int magic, const struct parsed_proto **pp_found, int *multi)
2468 const struct parsed_proto *pp = NULL;
2469 struct parsed_op *po;
2470 struct label_ref *lr;
2472 ops[i].cc_scratch = magic;
2475 if (g_labels[i][0] != 0) {
2476 lr = &g_label_refs[i];
2477 for (; lr != NULL; lr = lr->next)
2478 scan_for_call_type(lr->i, opr, magic, pp_found, multi);
2479 if (i > 0 && LAST_OP(i - 1))
2487 if (ops[i].cc_scratch == magic)
2489 ops[i].cc_scratch = magic;
2491 if (!(ops[i].flags & OPF_DATA))
2493 if (!is_opr_modified(opr, &ops[i]))
2495 if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
2496 // most probably trashed by some processing
2501 opr = &ops[i].operand[1];
2502 if (opr->type != OPT_REG)
2506 po = (i >= 0) ? &ops[i] : ops;
2509 // reached the top - can only be an arg-reg
2510 if (opr->type != OPT_REG)
2513 for (i = 0; i < g_func_pp->argc; i++) {
2514 if (g_func_pp->arg[i].reg == NULL)
2516 if (IS(opr->name, g_func_pp->arg[i].reg))
2519 if (i == g_func_pp->argc)
2521 pp = g_func_pp->arg[i].fptr;
2523 ferr(po, "icall: arg%d (%s) is not a fptr?\n",
2524 i + 1, g_func_pp->arg[i].reg);
2525 check_func_pp(po, pp, "icall reg-arg");
2528 pp = try_recover_pp(po, opr, NULL);
2530 if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
2531 if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
2532 || (*pp_found)->is_stdcall != pp->is_stdcall
2533 || (*pp_found)->is_fptr != pp->is_fptr
2534 || (*pp_found)->argc != pp->argc
2535 || (*pp_found)->argc_reg != pp->argc_reg
2536 || (*pp_found)->argc_stack != pp->argc_stack)
2538 ferr(po, "icall: parsed_proto mismatch\n");
2546 static const struct parsed_proto *resolve_icall(int i, int opcnt,
2549 const struct parsed_proto *pp = NULL;
2550 int search_advice = 0;
2554 switch (ops[i].operand[0].type) {
2558 pp = try_recover_pp(&ops[i], &ops[i].operand[0], &search_advice);
2563 scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
2571 static int try_resolve_const(int i, const struct parsed_opr *opr,
2572 int magic, unsigned int *val)
2574 struct label_ref *lr;
2577 ops[i].cc_scratch = magic;
2580 if (g_labels[i][0] != 0) {
2581 lr = &g_label_refs[i];
2582 for (; lr != NULL; lr = lr->next)
2583 ret |= try_resolve_const(lr->i, opr, magic, val);
2584 if (i > 0 && LAST_OP(i - 1))
2592 if (ops[i].cc_scratch == magic)
2594 ops[i].cc_scratch = magic;
2596 if (!(ops[i].flags & OPF_DATA))
2598 if (!is_opr_modified(opr, &ops[i]))
2600 if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST)
2603 *val = ops[i].operand[1].val;
2608 static int collect_call_args_r(struct parsed_op *po, int i,
2609 struct parsed_proto *pp, int *regmask, int *save_arg_vars, int arg,
2610 int magic, int need_op_saving, int may_reuse)
2612 struct parsed_proto *pp_tmp;
2613 struct label_ref *lr;
2614 int need_to_save_current;
2619 ferr(po, "dead label encountered\n");
2623 for (; arg < pp->argc; arg++)
2624 if (pp->arg[arg].reg == NULL)
2626 magic = (magic & 0xffffff) | (arg << 24);
2628 for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
2630 if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
2631 if (ops[j].cc_scratch != magic) {
2632 ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
2636 // ok: have already been here
2639 ops[j].cc_scratch = magic;
2641 if (g_labels[j][0] != 0 && g_label_refs[j].i != -1) {
2642 lr = &g_label_refs[j];
2643 if (lr->next != NULL)
2645 for (; lr->next; lr = lr->next) {
2646 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
2648 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2649 arg, magic, need_op_saving, may_reuse);
2654 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
2656 if (j > 0 && LAST_OP(j - 1)) {
2657 // follow last branch in reverse
2662 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2663 arg, magic, need_op_saving, may_reuse);
2669 if (ops[j].op == OP_CALL)
2671 if (pp->is_unresolved)
2676 ferr(po, "arg collect hit unparsed call '%s'\n",
2677 ops[j].operand[0].name);
2678 if (may_reuse && pp_tmp->argc_stack > 0)
2679 ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
2680 arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
2682 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP) {
2683 if (pp->is_unresolved)
2686 ferr(po, "arg collect %d/%d hit esp adjust\n",
2689 else if (ops[j].op == OP_POP) {
2690 if (pp->is_unresolved)
2693 ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc);
2695 else if (ops[j].flags & OPF_CJMP)
2697 if (pp->is_unresolved)
2702 else if (ops[j].op == OP_PUSH && !(ops[j].flags & OPF_FARG))
2704 if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
2707 pp->arg[arg].datap = &ops[j];
2708 need_to_save_current = 0;
2709 if (!need_op_saving) {
2710 ret = scan_for_mod(&ops[j], j + 1, i, 1);
2711 need_to_save_current = (ret >= 0);
2713 if (need_op_saving || need_to_save_current) {
2714 // mark this push as one that needs operand saving
2715 ops[j].flags &= ~OPF_RMD;
2716 if (ops[j].argnum == 0) {
2717 ops[j].argnum = arg + 1;
2718 *save_arg_vars |= 1 << arg;
2720 else if (ops[j].argnum < arg + 1)
2721 ferr(&ops[j], "argnum conflict (%d<%d) for '%s'\n",
2722 ops[j].argnum, arg + 1, pp->name);
2724 else if (ops[j].argnum == 0)
2725 ops[j].flags |= OPF_RMD;
2727 // some PUSHes are reused by different calls on other branches,
2728 // but that can't happen if we didn't branch, so they
2729 // can be removed from future searches (handles nested calls)
2731 ops[j].flags |= OPF_FARG;
2733 ops[j].flags &= ~OPF_RSAVE;
2736 if (!pp->is_unresolved) {
2738 for (; arg < pp->argc; arg++)
2739 if (pp->arg[arg].reg == NULL)
2742 magic = (magic & 0xffffff) | (arg << 24);
2744 // tracking reg usage
2745 if (ops[j].operand[0].type == OPT_REG)
2746 *regmask |= 1 << ops[j].operand[0].reg;
2750 if (arg < pp->argc) {
2751 ferr(po, "arg collect failed for '%s': %d/%d\n",
2752 pp->name, arg, pp->argc);
2759 static int collect_call_args(struct parsed_op *po, int i,
2760 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
2766 ret = collect_call_args_r(po, i, pp, regmask, save_arg_vars,
2771 if (pp->is_unresolved) {
2773 pp->argc_stack += ret;
2774 for (a = 0; a < pp->argc; a++)
2775 if (pp->arg[a].type.name == NULL)
2776 pp->arg[a].type.name = strdup("int");
2782 // early check for tail call or branch back
2783 static int is_like_tailjmp(int j)
2785 if (!(ops[j].flags & OPF_JMP))
2788 if (ops[j].op == OP_JMP && !ops[j].operand[0].had_ds)
2789 // probably local branch back..
2791 if (ops[j].op == OP_CALL)
2792 // probably noreturn call..
2798 static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
2802 for (i = 0; i < pp->argc; i++)
2803 if (pp->arg[i].reg == NULL)
2807 memmove(&pp->arg[i + 1], &pp->arg[i],
2808 sizeof(pp->arg[0]) * pp->argc_stack);
2809 memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
2810 pp->arg[i].reg = strdup(reg);
2811 pp->arg[i].type.name = strdup("int");
2816 static void add_label_ref(struct label_ref *lr, int op_i)
2818 struct label_ref *lr_new;
2825 lr_new = calloc(1, sizeof(*lr_new));
2827 lr_new->next = lr->next;
2831 static void output_std_flags(FILE *fout, struct parsed_op *po,
2832 int *pfomask, const char *dst_opr_text)
2834 if (*pfomask & (1 << PFO_Z)) {
2835 fprintf(fout, "\n cond_z = (%s%s == 0);",
2836 lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
2837 *pfomask &= ~(1 << PFO_Z);
2839 if (*pfomask & (1 << PFO_S)) {
2840 fprintf(fout, "\n cond_s = (%s%s < 0);",
2841 lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
2842 *pfomask &= ~(1 << PFO_S);
2846 static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp,
2849 if (pp->is_fastcall)
2850 fprintf(fout, "__fastcall ");
2851 else if (pp->is_stdcall && pp->argc_reg == 0)
2852 fprintf(fout, "__stdcall ");
2853 if (pp->is_noreturn || is_noreturn)
2854 fprintf(fout, "noreturn ");
2857 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
2859 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
2860 struct parsed_opr *last_arith_dst = NULL;
2861 char buf1[256], buf2[256], buf3[256], cast[64];
2862 const struct parsed_proto *pp_c;
2863 struct parsed_proto *pp, *pp_tmp;
2864 struct parsed_data *pd;
2865 const char *tmpname;
2867 int save_arg_vars = 0;
2869 int need_tmp_var = 0;
2872 int label_pending = 0;
2873 int regmask_save = 0;
2874 int regmask_arg = 0;
2875 int regmask_now = 0;
2876 int regmask_init = 0;
2887 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
2888 g_stack_frame_used = 0;
2890 g_func_pp = proto_parse(fhdr, funcn, 0);
2891 if (g_func_pp == NULL)
2892 ferr(ops, "proto_parse failed for '%s'\n", funcn);
2894 fprintf(fout, "%s ", g_func_pp->ret_type.name);
2895 output_pp_attrs(fout, g_func_pp, g_ida_func_attr & IDAFA_NORETURN);
2896 fprintf(fout, "%s(", g_func_pp->name);
2898 for (i = 0; i < g_func_pp->argc; i++) {
2900 fprintf(fout, ", ");
2901 if (g_func_pp->arg[i].fptr != NULL) {
2903 pp = g_func_pp->arg[i].fptr;
2904 fprintf(fout, "%s (", pp->ret_type.name);
2905 output_pp_attrs(fout, pp, 0);
2906 fprintf(fout, "*a%d)(", i + 1);
2907 for (j = 0; j < pp->argc; j++) {
2909 fprintf(fout, ", ");
2910 if (pp->arg[j].fptr)
2911 ferr(ops, "nested fptr\n");
2912 fprintf(fout, "%s", pp->arg[j].type.name);
2914 if (pp->is_vararg) {
2916 fprintf(fout, ", ");
2917 fprintf(fout, "...");
2921 else if (g_func_pp->arg[i].type.is_retreg) {
2922 fprintf(fout, "u32 *r_%s", g_func_pp->arg[i].reg);
2925 fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1);
2927 if (g_func_pp->arg[i].reg != NULL) {
2928 reg = char_array_i(regs_r32,
2929 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
2931 ferr(ops, "arg '%s' is not a reg?\n", g_func_pp->arg[i].reg);
2932 regmask_arg |= 1 << reg;
2935 if (g_func_pp->is_vararg) {
2937 fprintf(fout, ", ");
2938 fprintf(fout, "...");
2941 fprintf(fout, ")\n{\n");
2944 // - handle ebp/esp frame, remove ops related to it
2945 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
2946 && ops[1].op == OP_MOV
2947 && IS(opr_name(&ops[1], 0), "ebp")
2948 && IS(opr_name(&ops[1], 1), "esp"))
2953 ops[0].flags |= OPF_RMD;
2954 ops[1].flags |= OPF_RMD;
2957 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
2958 g_stack_fsz = opr_const(&ops[2], 1);
2959 ops[2].flags |= OPF_RMD;
2963 // another way msvc builds stack frame..
2965 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
2967 ops[i].flags |= OPF_RMD;
2971 // and another way..
2972 if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
2973 && ops[i].operand[1].type == OPT_CONST
2974 && ops[i + 1].op == OP_CALL
2975 && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
2977 g_stack_fsz += ops[i].operand[1].val;
2978 ops[i].flags |= OPF_RMD;
2980 ops[i].flags |= OPF_RMD;
2987 for (; i < opcnt; i++)
2988 if (ops[i].op == OP_RET)
2991 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
2992 if (found && is_like_tailjmp(j))
2997 if ((ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp"))
2998 || ops[j].op == OP_LEAVE)
3000 ops[j].flags |= OPF_RMD;
3002 else if (!(g_ida_func_attr & IDAFA_NORETURN))
3003 ferr(&ops[j], "'pop ebp' expected\n");
3005 if (g_stack_fsz != 0) {
3006 if (ops[j - 1].op == OP_MOV
3007 && IS(opr_name(&ops[j - 1], 0), "esp")
3008 && IS(opr_name(&ops[j - 1], 1), "ebp"))
3010 ops[j - 1].flags |= OPF_RMD;
3012 else if (ops[j].op != OP_LEAVE
3013 && !(g_ida_func_attr & IDAFA_NORETURN))
3015 ferr(&ops[j - 1], "esp restore expected\n");
3018 if (ecx_push && ops[j - 2].op == OP_POP
3019 && IS(opr_name(&ops[j - 2], 0), "ecx"))
3021 ferr(&ops[j - 2], "unexpected ecx pop\n");
3027 } while (i < opcnt);
3030 for (i = 0; i < opcnt; i++) {
3031 if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
3033 if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
3034 && ops[i].operand[1].type == OPT_CONST)
3044 g_stack_fsz = ops[i].operand[1].val;
3045 ops[i].flags |= OPF_RMD;
3049 for (; i < opcnt; i++)
3050 if (ops[i].op == OP_RET)
3053 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
3054 if (found && is_like_tailjmp(j))
3059 if (ops[j].op != OP_ADD
3060 || !IS(opr_name(&ops[j], 0), "esp")
3061 || ops[j].operand[1].type != OPT_CONST
3062 || ops[j].operand[1].val != g_stack_fsz)
3063 ferr(&ops[j], "'add esp' expected\n");
3064 ops[j].flags |= OPF_RMD;
3068 } while (i < opcnt);
3073 // - parse calls with labels
3074 // - resolve all branches
3075 for (i = 0; i < opcnt; i++)
3081 if (po->flags & OPF_RMD)
3084 if (po->op == OP_CALL) {
3087 if (po->operand[0].type == OPT_LABEL) {
3088 tmpname = opr_name(po, 0);
3089 if (IS_START(tmpname, "loc_"))
3090 ferr(po, "call to loc_*\n");
3091 pp_c = proto_parse(fhdr, tmpname, 0);
3093 ferr(po, "proto_parse failed for call '%s'\n", tmpname);
3095 pp = proto_clone(pp_c);
3096 my_assert_not(pp, NULL);
3098 else if (po->datap != NULL) {
3099 pp = calloc(1, sizeof(*pp));
3100 my_assert_not(pp, NULL);
3102 ret = parse_protostr(po->datap, pp);
3104 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
3111 check_func_pp(po, pp, "fptr var call");
3112 if (pp->is_noreturn)
3113 po->flags |= OPF_TAIL;
3119 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
3122 if (po->operand[0].type == OPT_REGMEM) {
3123 char *p = strchr(po->operand[0].name, '[');
3126 ret = p - po->operand[0].name;
3127 strncpy(buf1, po->operand[0].name, ret);
3130 for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
3131 if (IS(g_func_pd[j].label, buf1)) {
3137 //ferr(po, "label '%s' not parsed?\n", buf1);
3139 if (pd->type != OPT_OFFSET)
3140 ferr(po, "label '%s' with non-offset data?\n", buf1);
3142 // find all labels, link
3143 for (j = 0; j < pd->count; j++) {
3144 for (l = 0; l < opcnt; l++) {
3145 if (g_labels[l][0] && IS(g_labels[l], pd->d[j].u.label)) {
3146 add_label_ref(&g_label_refs[l], i);
3157 for (l = 0; l < opcnt; l++) {
3158 if (g_labels[l][0] && IS(po->operand[0].name, g_labels[l])) {
3159 if (l == i + 1 && po->op == OP_JMP) {
3160 // yet another alignment type..
3161 po->flags |= OPF_RMD;
3164 add_label_ref(&g_label_refs[l], i);
3170 if (po->bt_i != -1 || (po->flags & OPF_RMD))
3173 if (po->operand[0].type == OPT_LABEL)
3177 ferr(po, "unhandled branch\n");
3181 po->flags |= OPF_TAIL;
3182 if (i > 0 && ops[i - 1].op == OP_POP)
3183 po->flags |= OPF_ATAIL;
3188 // - remove dead labels
3190 for (i = 0; i < opcnt; i++)
3192 if (g_labels[i][0] != 0 && g_label_refs[i].i == -1)
3196 if (po->flags & OPF_RMD)
3199 if (po->op == OP_CALL)
3201 tmpname = opr_name(po, 0);
3206 pp_c = resolve_icall(i, opcnt, &l);
3208 pp = proto_clone(pp_c);
3209 my_assert_not(pp, NULL);
3211 // not resolved just to single func
3214 switch (po->operand[0].type) {
3216 // we resolved this call and no longer need the register
3217 po->regmask_src &= ~(1 << po->operand[0].reg);
3227 pp = calloc(1, sizeof(*pp));
3228 my_assert_not(pp, NULL);
3230 ret = scan_for_esp_adjust(i + 1, opcnt, &j, &l);
3232 if (!g_allow_regfunc)
3233 ferr(po, "non-__cdecl indirect call unhandled yet\n");
3234 pp->is_unresolved = 1;
3238 if (j > ARRAY_SIZE(pp->arg))
3239 ferr(po, "esp adjust too large: %d\n", j);
3240 pp->ret_type.name = strdup("int");
3241 pp->argc = pp->argc_stack = j;
3242 for (arg = 0; arg < pp->argc; arg++)
3243 pp->arg[arg].type.name = strdup("int");
3248 // look for and make use of esp adjust
3250 if (!pp->is_stdcall && pp->argc_stack > 0)
3251 ret = scan_for_esp_adjust(i + 1, opcnt, &j, &l);
3253 if (pp->is_vararg) {
3254 if (j / 4 < pp->argc_stack)
3255 ferr(po, "esp adjust is too small: %x < %x\n",
3256 j, pp->argc_stack * 4);
3257 // modify pp to make it have varargs as normal args
3259 pp->argc += j / 4 - pp->argc_stack;
3260 for (; arg < pp->argc; arg++) {
3261 pp->arg[arg].type.name = strdup("int");
3264 if (pp->argc > ARRAY_SIZE(pp->arg))
3265 ferr(po, "too many args for '%s'\n", tmpname);
3267 if (pp->argc_stack != j / 4)
3268 ferr(po, "stack tracking failed for '%s': %x %x\n",
3269 tmpname, pp->argc_stack * 4, j);
3271 ops[ret].flags |= OPF_RMD;
3272 if (ops[ret].op == OP_POP && j > 4) {
3273 // deal with multi-pop stack adjust
3275 while (ops[ret].op == OP_POP && j > 0 && ret < opcnt) {
3276 ops[ret].flags |= OPF_RMD;
3282 // a bit of a hack, but deals with use of
3283 // single adj for multiple calls
3284 ops[ret].operand[1].val -= j;
3287 else if (pp->is_vararg)
3288 ferr(po, "missing esp_adjust for vararg func '%s'\n",
3291 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
3292 // since we know the args, collect them
3293 collect_call_args(po, i, pp, ®mask, &save_arg_vars,
3297 if (strstr(pp->ret_type.name, "int64"))
3303 // - find POPs for PUSHes, rm both
3304 // - scan for STD/CLD, propagate DF
3305 // - scan for all used registers
3306 // - find flag set ops for their users
3307 // - do unreselved calls
3308 // - declare indirect functions
3309 for (i = 0; i < opcnt; i++) {
3311 if (po->flags & OPF_RMD)
3314 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
3315 reg = po->operand[0].reg;
3316 if (!(regmask & (1 << reg)))
3317 // not a reg save after all, rerun scan_for_pop
3318 po->flags &= ~OPF_RSAVE;
3320 regmask_save |= 1 << reg;
3323 if (po->op == OP_PUSH && po->argnum == 0
3324 && !(po->flags & OPF_RSAVE) && !g_func_pp->is_userstack)
3326 if (po->operand[0].type == OPT_REG)
3328 reg = po->operand[0].reg;
3330 ferr(po, "reg not set for push?\n");
3333 ret = scan_for_pop(i + 1, opcnt,
3334 po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
3337 ferr(po, "too much depth: %d\n", depth);
3339 po->flags |= OPF_RMD;
3340 scan_for_pop(i + 1, opcnt, po->operand[0].name,
3341 i + opcnt * 4, 0, &depth, 1);
3344 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
3347 if (regmask & (1 << reg)) {
3348 if (regmask_save & (1 << reg))
3349 ferr(po, "%s already saved?\n", po->operand[0].name);
3353 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg);
3357 else if (po->operand[0].type == OPT_CONST) {
3358 for (j = i + 1; j < opcnt; j++) {
3359 if ((ops[j].flags & (OPF_JMP|OPF_TAIL|OPF_RSAVE))
3360 || ops[j].op == OP_PUSH || g_labels[i][0] != 0)
3365 if (!(ops[j].flags & OPF_RMD) && ops[j].op == OP_POP)
3367 po->flags |= OPF_RMD;
3375 if (po->op == OP_STD) {
3376 po->flags |= OPF_DF | OPF_RMD;
3377 scan_propagate_df(i + 1, opcnt);
3380 regmask_now = po->regmask_src | po->regmask_dst;
3381 if (regmask_now & (1 << xBP)) {
3382 if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
3383 if (po->regmask_dst & (1 << xBP))
3384 // compiler decided to drop bp frame and use ebp as scratch
3385 scan_fwd_set_flags(i + 1, opcnt, i + opcnt * 5, OPF_EBP_S);
3387 regmask_now &= ~(1 << xBP);
3391 regmask |= regmask_now;
3393 if (po->flags & OPF_CC)
3395 int setters[16], cnt = 0, branched = 0;
3397 ret = scan_for_flag_set(i, i + opcnt * 6,
3398 &branched, setters, &cnt);
3399 if (ret < 0 || cnt <= 0)
3400 ferr(po, "unable to trace flag setter(s)\n");
3401 if (cnt > ARRAY_SIZE(setters))
3402 ferr(po, "too many flag setters\n");
3404 for (j = 0; j < cnt; j++)
3406 tmp_op = &ops[setters[j]]; // flag setter
3409 // to get nicer code, we try to delay test and cmp;
3410 // if we can't because of operand modification, or if we
3411 // have arith op, or branch, make it calculate flags explicitly
3412 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP)
3414 if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
3415 pfomask = 1 << po->pfo;
3417 else if (tmp_op->op == OP_CMPS || tmp_op->op == OP_SCAS) {
3418 pfomask = 1 << po->pfo;
3421 // see if we'll be able to handle based on op result
3422 if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
3423 && po->pfo != PFO_Z && po->pfo != PFO_S
3424 && po->pfo != PFO_P)
3426 || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
3428 pfomask = 1 << po->pfo;
3431 if (tmp_op->op == OP_ADD && po->pfo == PFO_C)
3435 tmp_op->pfomask |= pfomask;
3436 cond_vars |= pfomask;
3438 // note: may overwrite, currently not a problem
3442 if (po->op == OP_RCL || po->op == OP_RCR
3443 || po->op == OP_ADC || po->op == OP_SBB)
3444 cond_vars |= 1 << PFO_C;
3447 if (po->op == OP_CMPS || po->op == OP_SCAS) {
3448 cond_vars |= 1 << PFO_Z;
3450 else if (po->op == OP_MUL
3451 || (po->op == OP_IMUL && po->operand_cnt == 1))
3455 else if (po->op == OP_CALL) {
3458 ferr(po, "NULL pp\n");
3460 if (pp->is_unresolved) {
3461 int regmask_stack = 0;
3462 collect_call_args(po, i, pp, ®mask, &save_arg_vars,
3465 // this is pretty rough guess:
3466 // see ecx and edx were pushed (and not their saved versions)
3467 for (arg = 0; arg < pp->argc; arg++) {
3468 if (pp->arg[arg].reg != NULL)
3471 tmp_op = pp->arg[arg].datap;
3473 ferr(po, "parsed_op missing for arg%d\n", arg);
3474 if (tmp_op->argnum == 0 && tmp_op->operand[0].type == OPT_REG)
3475 regmask_stack |= 1 << tmp_op->operand[0].reg;
3478 if (!((regmask_stack & (1 << xCX))
3479 && (regmask_stack & (1 << xDX))))
3481 if (pp->argc_stack != 0
3482 || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX))))
3484 pp_insert_reg_arg(pp, "ecx");
3485 pp->is_fastcall = 1;
3486 regmask_init |= 1 << xCX;
3487 regmask |= 1 << xCX;
3489 if (pp->argc_stack != 0
3490 || ((regmask | regmask_arg) & (1 << xDX)))
3492 pp_insert_reg_arg(pp, "edx");
3493 regmask_init |= 1 << xDX;
3494 regmask |= 1 << xDX;
3498 // note: __cdecl doesn't fall into is_unresolved category
3499 if (pp->argc_stack > 0)
3503 for (arg = 0; arg < pp->argc; arg++) {
3504 if (pp->arg[arg].reg != NULL) {
3505 reg = char_array_i(regs_r32,
3506 ARRAY_SIZE(regs_r32), pp->arg[arg].reg);
3508 ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg);
3509 if (!(regmask & (1 << reg))) {
3510 regmask_init |= 1 << reg;
3511 regmask |= 1 << reg;
3516 // declare indirect funcs
3517 if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) {
3518 if (pp->name[0] != 0) {
3519 memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
3520 memcpy(pp->name, "i_", 2);
3522 // might be declared already
3524 for (j = 0; j < i; j++) {
3525 if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) {
3526 if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) {
3536 snprintf(pp->name, sizeof(pp->name), "icall%d", i);
3538 fprintf(fout, " %s (", pp->ret_type.name);
3539 output_pp_attrs(fout, pp, 0);
3540 fprintf(fout, "*%s)(", pp->name);
3541 for (j = 0; j < pp->argc; j++) {
3543 fprintf(fout, ", ");
3544 fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1);
3546 fprintf(fout, ");\n");
3549 else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void"))
3550 regmask |= 1 << xAX;
3551 else if (po->op == OP_DIV || po->op == OP_IDIV) {
3552 // 32bit division is common, look for it
3553 if (po->op == OP_DIV)
3554 ret = scan_for_reg_clear(i, xDX);
3556 ret = scan_for_cdq_edx(i);
3558 po->flags |= OPF_32BIT;
3562 else if (po->op == OP_CLD)
3563 po->flags |= OPF_RMD;
3565 if (po->op == OP_RCL || po->op == OP_RCR || po->op == OP_XCHG) {
3571 // - confirm regmask_save, it might have been reduced
3572 if (regmask_save != 0)
3575 for (i = 0; i < opcnt; i++) {
3577 if (po->flags & OPF_RMD)
3580 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE))
3581 regmask_save |= 1 << po->operand[0].reg;
3586 // output LUTs/jumptables
3587 for (i = 0; i < g_func_pd_cnt; i++) {
3589 fprintf(fout, " static const ");
3590 if (pd->type == OPT_OFFSET) {
3591 fprintf(fout, "void *jt_%s[] =\n { ", pd->label);
3593 for (j = 0; j < pd->count; j++) {
3595 fprintf(fout, ", ");
3596 fprintf(fout, "&&%s", pd->d[j].u.label);
3600 fprintf(fout, "%s %s[] =\n { ",
3601 lmod_type_u(ops, pd->lmod), pd->label);
3603 for (j = 0; j < pd->count; j++) {
3605 fprintf(fout, ", ");
3606 fprintf(fout, "%u", pd->d[j].u.val);
3609 fprintf(fout, " };\n");
3613 // declare stack frame, va_arg
3615 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
3616 (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz);
3620 if (g_func_pp->is_userstack) {
3621 fprintf(fout, " u32 fake_sf[1024];\n");
3622 fprintf(fout, " u32 *esp = &fake_sf[1024];\n");
3626 if (g_func_pp->is_vararg) {
3627 fprintf(fout, " va_list ap;\n");
3631 // declare arg-registers
3632 for (i = 0; i < g_func_pp->argc; i++) {
3633 if (g_func_pp->arg[i].reg != NULL) {
3634 reg = char_array_i(regs_r32,
3635 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
3636 if (regmask & (1 << reg)) {
3637 if (g_func_pp->arg[i].type.is_retreg)
3638 fprintf(fout, " u32 %s = *r_%s;\n",
3639 g_func_pp->arg[i].reg, g_func_pp->arg[i].reg);
3641 fprintf(fout, " u32 %s = (u32)a%d;\n",
3642 g_func_pp->arg[i].reg, i + 1);
3645 if (g_func_pp->arg[i].type.is_retreg)
3646 ferr(ops, "retreg '%s' is unused?\n",
3647 g_func_pp->arg[i].reg);
3648 fprintf(fout, " // %s = a%d; // unused\n",
3649 g_func_pp->arg[i].reg, i + 1);
3655 regmask_now = regmask & ~regmask_arg;
3656 regmask_now &= ~(1 << xSP);
3658 for (reg = 0; reg < 8; reg++) {
3659 if (regmask_now & (1 << reg)) {
3660 fprintf(fout, " u32 %s", regs_r32[reg]);
3661 if (regmask_init & (1 << reg))
3662 fprintf(fout, " = 0");
3663 fprintf(fout, ";\n");
3670 for (reg = 0; reg < 8; reg++) {
3671 if (regmask_save & (1 << reg)) {
3672 fprintf(fout, " u32 s_%s;\n", regs_r32[reg]);
3678 if (save_arg_vars) {
3679 for (reg = 0; reg < 32; reg++) {
3680 if (save_arg_vars & (1 << reg)) {
3681 fprintf(fout, " u32 s_a%d;\n", reg + 1);
3688 for (i = 0; i < 8; i++) {
3689 if (cond_vars & (1 << i)) {
3690 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
3697 fprintf(fout, " u32 tmp;\n");
3702 fprintf(fout, " u64 tmp64;\n");
3707 fprintf(fout, "\n");
3709 if (g_func_pp->is_vararg) {
3710 if (g_func_pp->argc_stack == 0)
3711 ferr(ops, "vararg func without stack args?\n");
3712 fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc);
3716 for (i = 0; i < opcnt; i++)
3718 if (g_labels[i][0] != 0) {
3719 fprintf(fout, "\n%s:\n", g_labels[i]);
3722 delayed_flag_op = NULL;
3723 last_arith_dst = NULL;
3727 if (po->flags & OPF_RMD)
3732 #define assert_operand_cnt(n_) \
3733 if (po->operand_cnt != n_) \
3734 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
3736 // conditional/flag using op?
3737 if (po->flags & OPF_CC)
3743 // we go through all this trouble to avoid using parsed_flag_op,
3744 // which makes generated code much nicer
3745 if (delayed_flag_op != NULL)
3747 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op,
3748 po->pfo, po->pfo_inv);
3751 else if (last_arith_dst != NULL
3752 && (po->pfo == PFO_Z || po->pfo == PFO_S || po->pfo == PFO_P
3753 || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
3756 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
3757 out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv,
3758 last_arith_dst->lmod, buf3);
3761 else if (tmp_op != NULL) {
3762 // use preprocessed flag calc results
3763 if (!(tmp_op->pfomask & (1 << po->pfo)))
3764 ferr(po, "not prepared for pfo %d\n", po->pfo);
3766 // note: pfo_inv was not yet applied
3767 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
3768 po->pfo_inv ? "!" : "", parsed_flag_op_names[po->pfo]);
3771 ferr(po, "all methods of finding comparison failed\n");
3774 if (po->flags & OPF_JMP) {
3775 fprintf(fout, " if %s", buf1);
3777 else if (po->op == OP_RCL || po->op == OP_RCR
3778 || po->op == OP_ADC || po->op == OP_SBB)
3781 fprintf(fout, " cond_%s = %s;\n",
3782 parsed_flag_op_names[po->pfo], buf1);
3784 else if (po->flags & OPF_DATA) { // SETcc
3785 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
3786 fprintf(fout, " %s = %s;", buf2, buf1);
3789 ferr(po, "unhandled conditional op\n");
3793 pfomask = po->pfomask;
3795 if (po->flags & (OPF_REPZ|OPF_REPNZ)) {
3796 struct parsed_opr opr = {0,};
3799 opr.lmod = OPLM_DWORD;
3800 ret = try_resolve_const(i, &opr, opcnt * 7 + i, &uval);
3802 if (ret != 1 || uval == 0) {
3803 // we need initial flags for ecx=0 case..
3804 if (i > 0 && ops[i - 1].op == OP_XOR
3805 && IS(ops[i - 1].operand[0].name,
3806 ops[i - 1].operand[1].name))
3808 fprintf(fout, " cond_z = ");
3809 if (pfomask & (1 << PFO_C))
3810 fprintf(fout, "cond_c = ");
3811 fprintf(fout, "0;\n");
3813 else if (last_arith_dst != NULL) {
3814 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
3815 out_test_for_cc(buf1, sizeof(buf1), po, PFO_Z, 0,
3816 last_arith_dst->lmod, buf3);
3817 fprintf(fout, " cond_z = %s;\n", buf1);
3820 ferr(po, "missing initial ZF\n");
3827 assert_operand_cnt(2);
3828 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3829 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3830 default_cast_to(buf3, sizeof(buf3), &po->operand[0]);
3831 fprintf(fout, " %s = %s;", buf1,
3832 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3837 assert_operand_cnt(2);
3838 po->operand[1].lmod = OPLM_DWORD; // always
3839 fprintf(fout, " %s = %s;",
3840 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3841 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3846 assert_operand_cnt(2);
3847 fprintf(fout, " %s = %s;",
3848 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3849 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3853 assert_operand_cnt(2);
3854 switch (po->operand[1].lmod) {
3856 strcpy(buf3, "(s8)");
3859 strcpy(buf3, "(s16)");
3862 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
3864 fprintf(fout, " %s = %s;",
3865 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3866 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3871 assert_operand_cnt(2);
3872 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3873 fprintf(fout, " tmp = %s;",
3874 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0));
3875 fprintf(fout, " %s = %s;",
3876 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3877 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3878 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
3879 fprintf(fout, " %s = %stmp;",
3880 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
3881 default_cast_to(buf3, sizeof(buf3), &po->operand[1]));
3882 snprintf(g_comment, sizeof(g_comment), "xchg");
3886 assert_operand_cnt(1);
3887 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3888 fprintf(fout, " %s = ~%s;", buf1, buf1);
3892 assert_operand_cnt(2);
3893 fprintf(fout, " %s = (s32)%s >> 31;",
3894 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3895 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3896 strcpy(g_comment, "cdq");
3900 assert_operand_cnt(3);
3901 if (po->flags & OPF_REP) {
3906 fprintf(fout, " eax = %sesi; esi %c= %d;",
3907 lmod_cast_u_ptr(po, po->operand[0].lmod),
3908 (po->flags & OPF_DF) ? '-' : '+',
3909 lmod_bytes(po, po->operand[0].lmod));
3910 strcpy(g_comment, "lods");
3915 assert_operand_cnt(3);
3916 if (po->flags & OPF_REP) {
3917 fprintf(fout, " for (; ecx != 0; ecx--, edi %c= %d)\n",
3918 (po->flags & OPF_DF) ? '-' : '+',
3919 lmod_bytes(po, po->operand[0].lmod));
3920 fprintf(fout, " %sedi = eax;",
3921 lmod_cast_u_ptr(po, po->operand[0].lmod));
3922 strcpy(g_comment, "rep stos");
3925 fprintf(fout, " %sedi = eax; edi %c= %d;",
3926 lmod_cast_u_ptr(po, po->operand[0].lmod),
3927 (po->flags & OPF_DF) ? '-' : '+',
3928 lmod_bytes(po, po->operand[0].lmod));
3929 strcpy(g_comment, "stos");
3934 assert_operand_cnt(3);
3935 j = lmod_bytes(po, po->operand[0].lmod);
3936 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
3937 l = (po->flags & OPF_DF) ? '-' : '+';
3938 if (po->flags & OPF_REP) {
3940 " for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n",
3943 " %sedi = %sesi;", buf1, buf1);
3944 strcpy(g_comment, "rep movs");
3947 fprintf(fout, " %sedi = %sesi; edi %c= %d; esi %c= %d;",
3948 buf1, buf1, l, j, l, j);
3949 strcpy(g_comment, "movs");
3954 // repe ~ repeat while ZF=1
3955 assert_operand_cnt(3);
3956 j = lmod_bytes(po, po->operand[0].lmod);
3957 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
3958 l = (po->flags & OPF_DF) ? '-' : '+';
3959 if (po->flags & OPF_REP) {
3961 " for (; ecx != 0; ecx--) {\n");
3962 if (pfomask & (1 << PFO_C)) {
3965 " cond_c = %sesi < %sedi;\n", buf1, buf1);
3966 pfomask &= ~(1 << PFO_C);
3969 " cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n",
3970 buf1, buf1, l, j, l, j);
3972 " if (cond_z %s 0) break;\n",
3973 (po->flags & OPF_REPZ) ? "==" : "!=");
3976 snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
3977 (po->flags & OPF_REPZ) ? "e" : "ne");
3981 " cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;",
3982 buf1, buf1, l, j, l, j);
3983 strcpy(g_comment, "cmps");
3985 pfomask &= ~(1 << PFO_Z);
3986 last_arith_dst = NULL;
3987 delayed_flag_op = NULL;
3991 // only does ZF (for now)
3992 // repe ~ repeat while ZF=1
3993 assert_operand_cnt(3);
3994 j = lmod_bytes(po, po->operand[0].lmod);
3995 l = (po->flags & OPF_DF) ? '-' : '+';
3996 if (po->flags & OPF_REP) {
3998 " for (; ecx != 0; ecx--) {\n");
4000 " cond_z = (%seax == %sedi); edi %c= %d;\n",
4001 lmod_cast_u(po, po->operand[0].lmod),
4002 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4004 " if (cond_z %s 0) break;\n",
4005 (po->flags & OPF_REPZ) ? "==" : "!=");
4008 snprintf(g_comment, sizeof(g_comment), "rep%s scas",
4009 (po->flags & OPF_REPZ) ? "e" : "ne");
4012 fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;",
4013 lmod_cast_u(po, po->operand[0].lmod),
4014 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4015 strcpy(g_comment, "scas");
4017 pfomask &= ~(1 << PFO_Z);
4018 last_arith_dst = NULL;
4019 delayed_flag_op = NULL;
4022 // arithmetic w/flags
4025 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4028 assert_operand_cnt(2);
4029 fprintf(fout, " %s %s= %s;",
4030 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4032 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4033 output_std_flags(fout, po, &pfomask, buf1);
4034 last_arith_dst = &po->operand[0];
4035 delayed_flag_op = NULL;
4040 assert_operand_cnt(2);
4041 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4042 if (pfomask & (1 << PFO_C)) {
4043 if (po->operand[1].type == OPT_CONST) {
4044 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4045 j = po->operand[1].val;
4048 if (po->op == OP_SHL)
4052 fprintf(fout, " cond_c = (%s >> %d) & 1;\n",
4056 ferr(po, "zero shift?\n");
4060 pfomask &= ~(1 << PFO_C);
4062 fprintf(fout, " %s %s= %s;", buf1, op_to_c(po),
4063 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4064 output_std_flags(fout, po, &pfomask, buf1);
4065 last_arith_dst = &po->operand[0];
4066 delayed_flag_op = NULL;
4070 assert_operand_cnt(2);
4071 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4072 fprintf(fout, " %s = %s%s >> %s;", buf1,
4073 lmod_cast_s(po, po->operand[0].lmod), buf1,
4074 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4075 output_std_flags(fout, po, &pfomask, buf1);
4076 last_arith_dst = &po->operand[0];
4077 delayed_flag_op = NULL;
4082 assert_operand_cnt(2);
4083 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4084 if (po->operand[1].type == OPT_CONST) {
4085 j = po->operand[1].val;
4086 j %= lmod_bytes(po, po->operand[0].lmod) * 8;
4087 fprintf(fout, po->op == OP_ROL ?
4088 " %s = (%s << %d) | (%s >> %d);" :
4089 " %s = (%s >> %d) | (%s << %d);",
4090 buf1, buf1, j, buf1,
4091 lmod_bytes(po, po->operand[0].lmod) * 8 - j);
4095 output_std_flags(fout, po, &pfomask, buf1);
4096 last_arith_dst = &po->operand[0];
4097 delayed_flag_op = NULL;
4102 assert_operand_cnt(2);
4103 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4104 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4105 if (po->operand[1].type == OPT_CONST) {
4106 j = po->operand[1].val % l;
4108 ferr(po, "zero rotate\n");
4109 fprintf(fout, " tmp = (%s >> %d) & 1;\n",
4110 buf1, (po->op == OP_RCL) ? (l - j) : (j - 1));
4111 if (po->op == OP_RCL) {
4113 " %s = (%s << %d) | (cond_c << %d)",
4114 buf1, buf1, j, j - 1);
4116 fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j);
4120 " %s = (%s >> %d) | (cond_c << %d)",
4121 buf1, buf1, j, l - j);
4123 fprintf(fout, " | (%s << %d)", buf1, l + 1 - j);
4125 fprintf(fout, ";\n");
4126 fprintf(fout, " cond_c = tmp;");
4130 strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr");
4131 output_std_flags(fout, po, &pfomask, buf1);
4132 last_arith_dst = &po->operand[0];
4133 delayed_flag_op = NULL;
4137 assert_operand_cnt(2);
4138 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4139 if (IS(opr_name(po, 0), opr_name(po, 1))) {
4140 // special case for XOR
4141 if (pfomask & (1 << PFO_BE)) { // weird, but it happens..
4142 fprintf(fout, " cond_be = 1;\n");
4143 pfomask &= ~(1 << PFO_BE);
4145 fprintf(fout, " %s = 0;",
4146 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4147 last_arith_dst = &po->operand[0];
4148 delayed_flag_op = NULL;
4154 assert_operand_cnt(2);
4155 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4156 if (pfomask & (1 << PFO_C)) {
4157 fprintf(fout, " tmp64 = (u64)%s + %s;\n",
4158 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]),
4159 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4160 fprintf(fout, " cond_c = tmp64 >> 32;\n");
4161 fprintf(fout, " %s = (u32)tmp64;",
4162 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4163 strcat(g_comment, "add64");
4164 pfomask &= ~(1 << PFO_C);
4165 output_std_flags(fout, po, &pfomask, buf1);
4166 last_arith_dst = &po->operand[0];
4167 delayed_flag_op = NULL;
4173 assert_operand_cnt(2);
4174 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4175 if (pfomask & (1 << PFO_C)) {
4176 fprintf(fout, " cond_c = %s < %s;\n",
4177 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]),
4178 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4179 pfomask &= ~(1 << PFO_C);
4185 assert_operand_cnt(2);
4186 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4187 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4188 if (po->op == OP_SBB
4189 && IS(po->operand[0].name, po->operand[1].name))
4191 // avoid use of unitialized var
4192 fprintf(fout, " %s = -cond_c;", buf1);
4193 // carry remains what it was
4194 pfomask &= ~(1 << PFO_C);
4197 fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
4198 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4200 output_std_flags(fout, po, &pfomask, buf1);
4201 last_arith_dst = &po->operand[0];
4202 delayed_flag_op = NULL;
4206 assert_operand_cnt(2);
4207 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4208 fprintf(fout, " %s = %s ? __builtin_ffs(%s) - 1 : 0;",
4209 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4211 output_std_flags(fout, po, &pfomask, buf1);
4212 last_arith_dst = &po->operand[0];
4213 delayed_flag_op = NULL;
4214 strcat(g_comment, "bsf");
4219 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4220 if (po->operand[0].type == OPT_REG) {
4221 strcpy(buf2, po->op == OP_INC ? "++" : "--");
4222 fprintf(fout, " %s%s;", buf1, buf2);
4225 strcpy(buf2, po->op == OP_INC ? "+" : "-");
4226 fprintf(fout, " %s %s= 1;", buf1, buf2);
4228 output_std_flags(fout, po, &pfomask, buf1);
4229 last_arith_dst = &po->operand[0];
4230 delayed_flag_op = NULL;
4234 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4235 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
4236 fprintf(fout, " %s = -%s%s;", buf1,
4237 lmod_cast_s(po, po->operand[0].lmod), buf2);
4238 last_arith_dst = &po->operand[0];
4239 delayed_flag_op = NULL;
4240 if (pfomask & (1 << PFO_C)) {
4241 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
4242 pfomask &= ~(1 << PFO_C);
4247 if (po->operand_cnt == 2) {
4248 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4251 if (po->operand_cnt == 3)
4252 ferr(po, "TODO imul3\n");
4255 assert_operand_cnt(1);
4256 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
4257 fprintf(fout, " tmp64 = %seax * %s%s;\n", buf1, buf1,
4258 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
4259 fprintf(fout, " edx = tmp64 >> 32;\n");
4260 fprintf(fout, " eax = tmp64;");
4261 last_arith_dst = NULL;
4262 delayed_flag_op = NULL;
4267 assert_operand_cnt(1);
4268 if (po->operand[0].lmod != OPLM_DWORD)
4269 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
4271 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4272 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
4273 po->op == OP_IDIV));
4274 switch (po->operand[0].lmod) {
4276 if (po->flags & OPF_32BIT)
4277 snprintf(buf3, sizeof(buf3), "%seax", buf2);
4279 fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n");
4280 snprintf(buf3, sizeof(buf3), "%stmp64",
4281 (po->op == OP_IDIV) ? "(s64)" : "");
4283 if (po->operand[0].type == OPT_REG
4284 && po->operand[0].reg == xDX)
4286 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4287 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4290 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4291 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4295 ferr(po, "unhandled division type\n");
4297 last_arith_dst = NULL;
4298 delayed_flag_op = NULL;
4303 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4305 for (j = 0; j < 8; j++) {
4306 if (pfomask & (1 << j)) {
4307 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
4308 fprintf(fout, " cond_%s = %s;",
4309 parsed_flag_op_names[j], buf1);
4316 last_arith_dst = NULL;
4317 delayed_flag_op = po;
4321 // SETcc - should already be handled
4324 // note: we reuse OP_Jcc for SETcc, only flags differ
4326 fprintf(fout, "\n goto %s;", po->operand[0].name);
4330 fprintf(fout, " if (ecx == 0)\n");
4331 fprintf(fout, " goto %s;", po->operand[0].name);
4332 strcat(g_comment, "jecxz");
4336 assert_operand_cnt(1);
4337 last_arith_dst = NULL;
4338 delayed_flag_op = NULL;
4340 if (po->operand[0].type == OPT_REGMEM) {
4341 ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
4344 ferr(po, "parse failure for jmp '%s'\n",
4345 po->operand[0].name);
4346 fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
4349 else if (po->operand[0].type != OPT_LABEL)
4350 ferr(po, "unhandled jmp type\n");
4352 fprintf(fout, " goto %s;", po->operand[0].name);
4356 assert_operand_cnt(1);
4358 my_assert_not(pp, NULL);
4361 if (po->flags & OPF_CC) {
4362 // we treat conditional branch to another func
4363 // (yes such code exists..) as conditional tailcall
4365 fprintf(fout, " {\n");
4368 if (pp->is_fptr && !pp->is_arg)
4369 fprintf(fout, "%s%s = %s;\n", buf3, pp->name,
4370 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
4373 fprintf(fout, "%s", buf3);
4374 if (strstr(pp->ret_type.name, "int64")) {
4375 if (po->flags & OPF_TAIL)
4376 ferr(po, "int64 and tail?\n");
4377 fprintf(fout, "tmp64 = ");
4379 else if (!IS(pp->ret_type.name, "void")) {
4380 if (po->flags & OPF_TAIL) {
4381 if (!IS(g_func_pp->ret_type.name, "void")) {
4382 fprintf(fout, "return ");
4383 if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
4384 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
4387 else if (regmask & (1 << xAX)) {
4388 fprintf(fout, "eax = ");
4389 if (pp->ret_type.is_ptr)
4390 fprintf(fout, "(u32)");
4394 if (pp->name[0] == 0)
4395 ferr(po, "missing pp->name\n");
4396 fprintf(fout, "%s%s(", pp->name,
4397 pp->has_structarg ? "_sa" : "");
4399 if (po->flags & OPF_ATAIL) {
4400 if (pp->argc_stack != g_func_pp->argc_stack
4401 || (pp->argc_stack > 0
4402 && pp->is_stdcall != g_func_pp->is_stdcall))
4403 ferr(po, "incompatible tailcall\n");
4404 if (g_func_pp->has_retreg)
4405 ferr(po, "TODO: retreg+tailcall\n");
4407 for (arg = j = 0; arg < pp->argc; arg++) {
4409 fprintf(fout, ", ");
4412 if (pp->arg[arg].type.is_ptr)
4413 snprintf(cast, sizeof(cast), "(%s)",
4414 pp->arg[arg].type.name);
4416 if (pp->arg[arg].reg != NULL) {
4417 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
4421 for (; j < g_func_pp->argc; j++)
4422 if (g_func_pp->arg[j].reg == NULL)
4424 fprintf(fout, "%sa%d", cast, j + 1);
4429 for (arg = 0; arg < pp->argc; arg++) {
4431 fprintf(fout, ", ");
4434 if (pp->arg[arg].type.is_ptr)
4435 snprintf(cast, sizeof(cast), "(%s)",
4436 pp->arg[arg].type.name);
4438 if (pp->arg[arg].reg != NULL) {
4439 if (pp->arg[arg].type.is_retreg)
4440 fprintf(fout, "&%s", pp->arg[arg].reg);
4442 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
4447 tmp_op = pp->arg[arg].datap;
4449 ferr(po, "parsed_op missing for arg%d\n", arg);
4450 if (tmp_op->argnum != 0) {
4451 fprintf(fout, "%ss_a%d", cast, tmp_op->argnum);
4455 out_src_opr(buf1, sizeof(buf1),
4456 tmp_op, &tmp_op->operand[0], cast, 0));
4460 fprintf(fout, ");");
4462 if (strstr(pp->ret_type.name, "int64")) {
4463 fprintf(fout, "\n");
4464 fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3);
4465 fprintf(fout, "%seax = tmp64;", buf3);
4468 if (pp->is_unresolved) {
4469 snprintf(buf2, sizeof(buf2), " unresoved %dreg",
4471 strcat(g_comment, buf2);
4474 if (po->flags & OPF_TAIL) {
4476 if (i == opcnt - 1 || pp->is_noreturn)
4478 else if (IS(pp->ret_type.name, "void"))
4480 else if (IS(g_func_pp->ret_type.name, "void"))
4482 // else already handled as 'return f()'
4485 if (!IS(g_func_pp->ret_type.name, "void")) {
4486 ferr(po, "int func -> void func tailcall?\n");
4489 fprintf(fout, "\n%sreturn;", buf3);
4490 strcat(g_comment, " ^ tailcall");
4494 strcat(g_comment, " tailcall");
4496 if (pp->is_noreturn)
4497 strcat(g_comment, " noreturn");
4498 if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0)
4499 strcat(g_comment, " argframe");
4500 if (po->flags & OPF_CC)
4501 strcat(g_comment, " cond");
4503 if (po->flags & OPF_CC)
4504 fprintf(fout, "\n }");
4506 delayed_flag_op = NULL;
4507 last_arith_dst = NULL;
4511 if (g_func_pp->is_vararg)
4512 fprintf(fout, " va_end(ap);\n");
4513 if (g_func_pp->has_retreg) {
4514 for (arg = 0; arg < g_func_pp->argc; arg++)
4515 if (g_func_pp->arg[arg].type.is_retreg)
4516 fprintf(fout, " *r_%s = %s;\n",
4517 g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg);
4520 if (IS(g_func_pp->ret_type.name, "void")) {
4521 if (i != opcnt - 1 || label_pending)
4522 fprintf(fout, " return;");
4524 else if (g_func_pp->ret_type.is_ptr) {
4525 fprintf(fout, " return (%s)eax;",
4526 g_func_pp->ret_type.name);
4528 else if (IS(g_func_pp->ret_type.name, "__int64"))
4529 fprintf(fout, " return ((u64)edx << 32) | eax;");
4531 fprintf(fout, " return eax;");
4533 last_arith_dst = NULL;
4534 delayed_flag_op = NULL;
4538 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4539 if (po->argnum != 0) {
4540 // special case - saved func arg
4541 fprintf(fout, " s_a%d = %s;", po->argnum, buf1);
4544 else if (po->flags & OPF_RSAVE) {
4545 fprintf(fout, " s_%s = %s;", buf1, buf1);
4548 else if (g_func_pp->is_userstack) {
4549 fprintf(fout, " *(--esp) = %s;", buf1);
4552 if (!(g_ida_func_attr & IDAFA_NORETURN))
4553 ferr(po, "stray push encountered\n");
4558 if (po->flags & OPF_RSAVE) {
4559 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4560 fprintf(fout, " %s = s_%s;", buf1, buf1);
4563 else if (po->datap != NULL) {
4566 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4567 fprintf(fout, " %s = %s;", buf1,
4568 out_src_opr(buf2, sizeof(buf2),
4569 tmp_op, &tmp_op->operand[0],
4570 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
4573 else if (g_func_pp->is_userstack) {
4574 fprintf(fout, " %s = *esp++;",
4575 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4579 ferr(po, "stray pop encountered\n");
4588 ferr(po, "unhandled op type %d, flags %x\n",
4593 if (g_comment[0] != 0) {
4594 char *p = g_comment;
4595 while (my_isblank(*p))
4597 fprintf(fout, " // %s", p);
4602 fprintf(fout, "\n");
4604 // some sanity checking
4605 if (po->flags & OPF_REP) {
4606 if (po->op != OP_STOS && po->op != OP_MOVS
4607 && po->op != OP_CMPS && po->op != OP_SCAS)
4608 ferr(po, "unexpected rep\n");
4609 if (!(po->flags & (OPF_REPZ|OPF_REPNZ))
4610 && (po->op == OP_CMPS || po->op == OP_SCAS))
4611 ferr(po, "cmps/scas with plain rep\n");
4613 if ((po->flags & (OPF_REPZ|OPF_REPNZ))
4614 && po->op != OP_CMPS && po->op != OP_SCAS)
4615 ferr(po, "unexpected repz/repnz\n");
4618 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
4620 // see is delayed flag stuff is still valid
4621 if (delayed_flag_op != NULL && delayed_flag_op != po) {
4622 if (is_any_opr_modified(delayed_flag_op, po, 0))
4623 delayed_flag_op = NULL;
4626 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
4627 if (is_opr_modified(last_arith_dst, po))
4628 last_arith_dst = NULL;
4634 if (g_stack_fsz && !g_stack_frame_used)
4635 fprintf(fout, " (void)sf;\n");
4637 fprintf(fout, "}\n\n");
4640 for (i = 0; i < opcnt; i++) {
4641 struct label_ref *lr, *lr_del;
4643 lr = g_label_refs[i].next;
4644 while (lr != NULL) {
4649 g_label_refs[i].i = -1;
4650 g_label_refs[i].next = NULL;
4652 if (ops[i].op == OP_CALL) {
4654 proto_release(ops[i].pp);
4660 static void set_label(int i, const char *name)
4666 p = strchr(name, ':');
4670 if (len > sizeof(g_labels[0]) - 1)
4671 aerr("label '%s' too long: %d\n", name, len);
4672 if (g_labels[i][0] != 0 && !IS_START(g_labels[i], "algn_"))
4673 aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
4674 memcpy(g_labels[i], name, len);
4675 g_labels[i][len] = 0;
4678 // '=' needs special treatment..
4679 static char *next_word_s(char *w, size_t wsize, char *s)
4685 for (i = 0; i < wsize - 1; i++) {
4686 if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
4692 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
4693 printf("warning: '%s' truncated\n", w);
4704 static struct chunk_item *func_chunks;
4705 static int func_chunk_cnt;
4706 static int func_chunk_alloc;
4708 static void add_func_chunk(FILE *fasm, const char *name, int line)
4710 if (func_chunk_cnt >= func_chunk_alloc) {
4711 func_chunk_alloc *= 2;
4712 func_chunks = realloc(func_chunks,
4713 func_chunk_alloc * sizeof(func_chunks[0]));
4714 my_assert_not(func_chunks, NULL);
4716 func_chunks[func_chunk_cnt].fptr = ftell(fasm);
4717 func_chunks[func_chunk_cnt].name = strdup(name);
4718 func_chunks[func_chunk_cnt].asmln = line;
4722 static int cmp_chunks(const void *p1, const void *p2)
4724 const struct chunk_item *c1 = p1, *c2 = p2;
4725 return strcmp(c1->name, c2->name);
4728 static int cmpstringp(const void *p1, const void *p2)
4730 return strcmp(*(char * const *)p1, *(char * const *)p2);
4733 static void scan_ahead(FILE *fasm)
4743 oldpos = ftell(fasm);
4746 while (fgets(line, sizeof(line), fasm))
4757 // get rid of random tabs
4758 for (i = 0; line[i] != 0; i++)
4759 if (line[i] == '\t')
4762 if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
4765 next_word(words[0], sizeof(words[0]), p);
4766 if (words[0][0] == 0)
4767 aerr("missing name for func chunk?\n");
4769 add_func_chunk(fasm, words[0], asmln);
4771 else if (IS_START(p, "; sctend"))
4777 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
4778 words[wordc][0] = 0;
4779 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
4780 if (*p == 0 || *p == ';') {
4786 if (wordc == 2 && IS(words[1], "ends"))
4790 fseek(fasm, oldpos, SEEK_SET);
4794 int main(int argc, char *argv[])
4796 FILE *fout, *fasm, *frlist;
4797 struct parsed_data *pd = NULL;
4799 char **rlist = NULL;
4801 int rlist_alloc = 0;
4802 int func_chunks_used = 0;
4803 int func_chunks_sorted = 0;
4804 int func_chunk_i = -1;
4805 long func_chunk_ret = 0;
4806 int func_chunk_ret_ln = 0;
4807 int scanned_ahead = 0;
4809 char words[20][256];
4810 enum opr_lenmod lmod;
4811 char *sctproto = NULL;
4813 int pending_endp = 0;
4815 int skip_warned = 0;
4828 for (arg = 1; arg < argc; arg++) {
4829 if (IS(argv[arg], "-v"))
4831 else if (IS(argv[arg], "-rf"))
4832 g_allow_regfunc = 1;
4833 else if (IS(argv[arg], "-m"))
4839 if (argc < arg + 3) {
4840 printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdrf> [rlist]*\n",
4847 asmfn = argv[arg++];
4848 fasm = fopen(asmfn, "r");
4849 my_assert_not(fasm, NULL);
4851 hdrfn = argv[arg++];
4852 g_fhdr = fopen(hdrfn, "r");
4853 my_assert_not(g_fhdr, NULL);
4856 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
4857 my_assert_not(rlist, NULL);
4858 // needs special handling..
4859 rlist[rlist_len++] = "__alloca_probe";
4861 func_chunk_alloc = 32;
4862 func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
4863 my_assert_not(func_chunks, NULL);
4865 memset(words, 0, sizeof(words));
4867 for (; arg < argc; arg++) {
4868 frlist = fopen(argv[arg], "r");
4869 my_assert_not(frlist, NULL);
4871 while (fgets(line, sizeof(line), frlist)) {
4873 if (*p == 0 || *p == ';')
4876 if (IS_START(p, "#if 0")
4877 || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
4881 else if (IS_START(p, "#endif"))
4888 p = next_word(words[0], sizeof(words[0]), p);
4889 if (words[0][0] == 0)
4892 if (rlist_len >= rlist_alloc) {
4893 rlist_alloc = rlist_alloc * 2 + 64;
4894 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
4895 my_assert_not(rlist, NULL);
4897 rlist[rlist_len++] = strdup(words[0]);
4906 qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
4908 fout = fopen(argv[arg_out], "w");
4909 my_assert_not(fout, NULL);
4912 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
4913 my_assert_not(g_eqs, NULL);
4915 for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
4916 g_label_refs[i].i = -1;
4917 g_label_refs[i].next = NULL;
4920 while (fgets(line, sizeof(line), fasm))
4929 // get rid of random tabs
4930 for (i = 0; line[i] != 0; i++)
4931 if (line[i] == '\t')
4936 if (p[2] == '=' && IS_START(p, "; =============== S U B"))
4937 goto do_pending_endp; // eww..
4939 if (p[2] == 'A' && IS_START(p, "; Attributes:"))
4941 static const char *attrs[] = {
4950 // parse IDA's attribute-list comment
4951 g_ida_func_attr = 0;
4954 for (; *p != 0; p = sskip(p)) {
4955 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
4956 if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
4957 g_ida_func_attr |= 1 << i;
4958 p += strlen(attrs[i]);
4962 if (i == ARRAY_SIZE(attrs)) {
4963 anote("unparsed IDA attr: %s\n", p);
4966 if (IS(attrs[i], "fpd=")) {
4967 p = next_word(words[0], sizeof(words[0]), p);
4972 else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
4975 next_word(words[0], sizeof(words[0]), p);
4976 if (words[0][0] == 0)
4977 aerr("missing name for func chunk?\n");
4979 if (!scanned_ahead) {
4980 add_func_chunk(fasm, words[0], asmln);
4981 func_chunks_sorted = 0;
4984 else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
4986 if (func_chunk_i >= 0) {
4987 if (func_chunk_i < func_chunk_cnt
4988 && IS(func_chunks[func_chunk_i].name, g_func))
4990 // move on to next chunk
4991 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
4993 aerr("seek failed for '%s' chunk #%d\n",
4994 g_func, func_chunk_i);
4995 asmln = func_chunks[func_chunk_i].asmln;
4999 if (func_chunk_ret == 0)
5000 aerr("no return from chunk?\n");
5001 fseek(fasm, func_chunk_ret, SEEK_SET);
5002 asmln = func_chunk_ret_ln;
5008 else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
5009 func_chunks_used = 1;
5011 if (IS_START(g_func, "sub_")) {
5012 unsigned long addr = strtoul(p, NULL, 16);
5013 unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
5014 if (addr > f_addr && !scanned_ahead) {
5015 anote("scan_ahead caused by '%s', addr %lx\n",
5019 func_chunks_sorted = 0;
5027 for (i = wordc; i < ARRAY_SIZE(words); i++)
5029 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
5030 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
5031 if (*p == 0 || *p == ';') {
5036 if (*p != 0 && *p != ';')
5037 aerr("too many words\n");
5039 // alow asm patches in comments
5041 if (IS_START(p, "; sctpatch:")) {
5043 if (*p == 0 || *p == ';')
5045 goto parse_words; // lame
5047 if (IS_START(p, "; sctproto:")) {
5048 sctproto = strdup(p + 11);
5050 else if (IS_START(p, "; sctend")) {
5059 awarn("wordc == 0?\n");
5063 // don't care about this:
5064 if (words[0][0] == '.'
5065 || IS(words[0], "include")
5066 || IS(words[0], "assume") || IS(words[1], "segment")
5067 || IS(words[0], "align"))
5073 // do delayed endp processing to collect switch jumptables
5075 if (in_func && !skip_func && !end && wordc >= 2
5076 && ((words[0][0] == 'd' && words[0][2] == 0)
5077 || (words[1][0] == 'd' && words[1][2] == 0)))
5080 if (words[1][0] == 'd' && words[1][2] == 0) {
5082 if (g_func_pd_cnt >= pd_alloc) {
5083 pd_alloc = pd_alloc * 2 + 16;
5084 g_func_pd = realloc(g_func_pd,
5085 sizeof(g_func_pd[0]) * pd_alloc);
5086 my_assert_not(g_func_pd, NULL);
5088 pd = &g_func_pd[g_func_pd_cnt];
5090 memset(pd, 0, sizeof(*pd));
5091 strcpy(pd->label, words[0]);
5092 pd->type = OPT_CONST;
5093 pd->lmod = lmod_from_directive(words[1]);
5099 anote("skipping alignment byte?\n");
5102 lmod = lmod_from_directive(words[0]);
5103 if (lmod != pd->lmod)
5104 aerr("lmod change? %d->%d\n", pd->lmod, lmod);
5107 if (pd->count_alloc < pd->count + wordc) {
5108 pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
5109 pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
5110 my_assert_not(pd->d, NULL);
5112 for (; i < wordc; i++) {
5113 if (IS(words[i], "offset")) {
5114 pd->type = OPT_OFFSET;
5117 p = strchr(words[i], ',');
5120 if (pd->type == OPT_OFFSET)
5121 pd->d[pd->count].u.label = strdup(words[i]);
5123 pd->d[pd->count].u.val = parse_number(words[i]);
5124 pd->d[pd->count].bt_i = -1;
5130 if (in_func && !skip_func)
5131 gen_func(fout, g_fhdr, g_func, pi);
5135 g_ida_func_attr = 0;
5139 func_chunks_used = 0;
5142 memset(&ops, 0, pi * sizeof(ops[0]));
5143 memset(g_labels, 0, pi * sizeof(g_labels[0]));
5147 for (i = 0; i < g_func_pd_cnt; i++) {
5149 if (pd->type == OPT_OFFSET) {
5150 for (j = 0; j < pd->count; j++)
5151 free(pd->d[j].u.label);
5165 if (IS(words[1], "proc")) {
5167 aerr("proc '%s' while in_func '%s'?\n",
5170 if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
5172 strcpy(g_func, words[0]);
5173 set_label(0, words[0]);
5178 if (IS(words[1], "endp"))
5181 aerr("endp '%s' while not in_func?\n", words[0]);
5182 if (!IS(g_func, words[0]))
5183 aerr("endp '%s' while in_func '%s'?\n",
5186 if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1
5187 && ops[0].op == OP_JMP && ops[0].operand[0].had_ds)
5193 if (!skip_func && func_chunks_used) {
5194 // start processing chunks
5195 struct chunk_item *ci, key = { g_func, 0 };
5197 func_chunk_ret = ftell(fasm);
5198 func_chunk_ret_ln = asmln;
5199 if (!func_chunks_sorted) {
5200 qsort(func_chunks, func_chunk_cnt,
5201 sizeof(func_chunks[0]), cmp_chunks);
5202 func_chunks_sorted = 1;
5204 ci = bsearch(&key, func_chunks, func_chunk_cnt,
5205 sizeof(func_chunks[0]), cmp_chunks);
5207 aerr("'%s' needs chunks, but none found\n", g_func);
5208 func_chunk_i = ci - func_chunks;
5209 for (; func_chunk_i > 0; func_chunk_i--)
5210 if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
5213 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
5215 aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
5216 asmln = func_chunks[func_chunk_i].asmln;
5224 if (wordc == 2 && IS(words[1], "ends")) {
5228 goto do_pending_endp;
5232 // scan for next text segment
5233 while (fgets(line, sizeof(line), fasm)) {
5236 if (*p == 0 || *p == ';')
5239 if (strstr(p, "segment para public 'CODE' use32"))
5246 p = strchr(words[0], ':');
5248 set_label(pi, words[0]);
5252 if (!in_func || skip_func) {
5253 if (!skip_warned && !skip_func && g_labels[pi][0] != 0) {
5255 anote("skipping from '%s'\n", g_labels[pi]);
5258 g_labels[pi][0] = 0;
5262 if (wordc > 1 && IS(words[1], "="))
5265 aerr("unhandled equ, wc=%d\n", wordc);
5266 if (g_eqcnt >= eq_alloc) {
5268 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
5269 my_assert_not(g_eqs, NULL);
5272 len = strlen(words[0]);
5273 if (len > sizeof(g_eqs[0].name) - 1)
5274 aerr("equ name too long: %d\n", len);
5275 strcpy(g_eqs[g_eqcnt].name, words[0]);
5277 if (!IS(words[3], "ptr"))
5278 aerr("unhandled equ\n");
5279 if (IS(words[2], "dword"))
5280 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
5281 else if (IS(words[2], "word"))
5282 g_eqs[g_eqcnt].lmod = OPLM_WORD;
5283 else if (IS(words[2], "byte"))
5284 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
5286 aerr("bad lmod: '%s'\n", words[2]);
5288 g_eqs[g_eqcnt].offset = parse_number(words[4]);
5293 if (pi >= ARRAY_SIZE(ops))
5294 aerr("too many ops\n");
5296 parse_op(&ops[pi], words, wordc);
5298 if (sctproto != NULL) {
5299 if (ops[pi].op == OP_CALL)
5300 ops[pi].datap = sctproto;
5313 // vim:ts=2:shiftwidth=2:expandtab