9 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
10 #define IS(w, y) !strcmp(w, y)
11 #define IS_START(w, y) !strncmp(w, y, strlen(y))
13 #include "protoparse.h"
15 static const char *asmfn;
19 #define anote(fmt, ...) \
20 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
21 #define awarn(fmt, ...) \
22 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
23 #define aerr(fmt, ...) do { \
24 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
29 #include "masm_tools.h"
32 OPF_RMD = (1 << 0), /* removed or optimized out */
33 OPF_DATA = (1 << 1), /* data processing - writes to dst opr */
34 OPF_FLAGS = (1 << 2), /* sets flags */
35 OPF_JMP = (1 << 3), /* branches, ret and call */
36 OPF_CC = (1 << 4), /* uses flags */
37 OPF_TAIL = (1 << 5), /* ret or tail call */
38 OPF_RSAVE = (1 << 6), /* push/pop is local reg save/load */
39 OPF_REP = (1 << 7), /* prefixed by rep */
40 OPF_REPZ = (1 << 8), /* rep is repe/repz */
41 OPF_REPNZ = (1 << 9), /* rep is repne/repnz */
42 OPF_FARG = (1 << 10), /* push collected as func arg (no reuse) */
43 OPF_EBP_S = (1 << 11), /* ebp used as scratch here, not BP */
111 // must be sorted (larger len must be further in enum)
119 #define MAX_OPERANDS 3
123 enum opr_lenmod lmod;
124 unsigned int is_ptr:1; // pointer in C
125 unsigned int is_array:1; // array in C
126 unsigned int type_from_var:1; // .. in header, sometimes wrong
127 unsigned int size_mismatch:1; // type override differs from C
128 unsigned int size_lt:1; // type override is larger than C
129 unsigned int had_ds:1; // had ds: prefix
137 struct parsed_opr operand[MAX_OPERANDS];
140 int regmask_src; // all referensed regs
142 int pfomask; // flagop: parsed_flag_op that can't be delayed
143 int argnum; // push: altered before call arg #
144 int cc_scratch; // scratch storage during analysis
145 int bt_i; // branch target for branches
146 struct parsed_data *btj;// branch targets for jumptables
151 // OP_CALL - parser proto hint (str), ptr to struct parsed_proto
152 // (OPF_CC) - point to one of (OPF_FLAGS) that affects cc op
156 enum opr_lenmod lmod;
163 enum opr_lenmod lmod;
177 struct label_ref *next;
181 IDAFA_BP_FRAME = (1 << 0),
182 IDAFA_LIB_FUNC = (1 << 1),
183 IDAFA_STATIC = (1 << 2),
184 IDAFA_NORETURN = (1 << 3),
185 IDAFA_THUNK = (1 << 4),
186 IDAFA_FPD = (1 << 5),
191 static struct parsed_op ops[MAX_OPS];
192 static struct parsed_equ *g_eqs;
194 static char g_labels[MAX_OPS][32];
195 static struct label_ref g_label_refs[MAX_OPS];
196 static const struct parsed_proto *g_func_pp;
197 static struct parsed_data *g_func_pd;
198 static int g_func_pd_cnt;
199 static char g_func[256];
200 static char g_comment[256];
201 static int g_bp_frame;
202 static int g_sp_frame;
203 static int g_stack_frame_used;
204 static int g_stack_fsz;
205 static int g_ida_func_attr;
206 static int g_allow_regfunc;
207 #define ferr(op_, fmt, ...) do { \
208 printf("error:%s:#%zd: '%s': " fmt, g_func, (op_) - ops, \
209 dump_op(op_), ##__VA_ARGS__); \
213 #define fnote(op_, fmt, ...) \
214 printf("error:%s:#%zd: '%s': " fmt, g_func, (op_) - ops, \
215 dump_op(op_), ##__VA_ARGS__)
219 const char *regs_r32[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp" };
220 const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
221 const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
222 const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
224 enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
226 // possible basic comparison types (without inversion)
227 enum parsed_flag_op {
231 PFO_BE, // 6 CF=1||ZF=1
235 PFO_LE, // e ZF=1||SF!=OF
238 static const char *parsed_flag_op_names[] = {
239 "o", "c", "z", "be", "s", "p", "l", "le"
242 static int char_array_i(const char *array[], size_t len, const char *s)
246 for (i = 0; i < len; i++)
253 static void printf_number(char *buf, size_t buf_size,
254 unsigned long number)
256 // output in C-friendly form
257 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
260 static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
264 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
266 *reg_lmod = OPLM_DWORD;
269 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
271 *reg_lmod = OPLM_WORD;
274 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
276 *reg_lmod = OPLM_BYTE;
279 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
281 *reg_lmod = OPLM_BYTE;
288 static int parse_indmode(char *name, int *regmask, int need_c_cvt)
290 enum opr_lenmod lmod;
303 while (my_isblank(*s))
305 for (; my_issep(*s); d++, s++)
307 while (my_isblank(*s))
312 if (IS_START(s, "ds:"))
315 s = next_idt(w, sizeof(w), s);
320 reg = parse_reg(&lmod, w);
322 *regmask |= 1 << reg;
326 if ('0' <= w[0] && w[0] <= '9') {
327 number = parse_number(w);
328 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
332 // probably some label/identifier - pass
335 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
339 strcpy(name, cvtbuf);
344 static int is_reg_in_str(const char *s)
348 if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3])))
351 for (i = 0; i < ARRAY_SIZE(regs_r32); i++)
352 if (!strncmp(s, regs_r32[i], 3))
358 static const char *parse_stack_el(const char *name, char *extra_reg)
360 const char *p, *p2, *s;
367 if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) {
369 if (extra_reg != NULL) {
370 strncpy(extra_reg, name, 3);
375 if (IS_START(p, "ebp+")) {
379 if (p2 != NULL && is_reg_in_str(p)) {
380 if (extra_reg != NULL) {
381 strncpy(extra_reg, p, p2 - p);
382 extra_reg[p2 - p] = 0;
387 if (!('0' <= *p && *p <= '9'))
393 if (!IS_START(name, "esp+"))
396 p = strchr(name + 4, '+');
398 // must be a number after esp+, already converted to 0x..
400 if (!('0' <= *s && *s <= '9')) {
401 aerr("%s nan?\n", __func__);
404 if (s[0] == '0' && s[1] == 'x')
407 if (len < sizeof(buf) - 1) {
408 strncpy(buf, s, len);
410 val = strtol(buf, &endp, 16);
411 if (val == 0 || *endp != 0) {
412 aerr("%s num parse fail for '%s'\n", __func__, buf);
421 if ('0' <= *p && *p <= '9')
427 static int guess_lmod_from_name(struct parsed_opr *opr)
429 if (!strncmp(opr->name, "dword_", 6)) {
430 opr->lmod = OPLM_DWORD;
433 if (!strncmp(opr->name, "word_", 5)) {
434 opr->lmod = OPLM_WORD;
437 if (!strncmp(opr->name, "byte_", 5)) {
438 opr->lmod = OPLM_BYTE;
444 static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
445 const struct parsed_type *c_type)
447 static const char *dword_types[] = {
448 "int", "_DWORD", "UINT_PTR", "DWORD",
449 "WPARAM", "LPARAM", "UINT",
452 static const char *word_types[] = {
453 "uint16_t", "int16_t", "_WORD",
454 "unsigned __int16", "__int16",
456 static const char *byte_types[] = {
457 "uint8_t", "int8_t", "char",
458 "unsigned __int8", "__int8", "BYTE", "_BYTE",
464 if (c_type->is_ptr) {
469 n = skip_type_mod(c_type->name);
471 for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
472 if (IS(n, dword_types[i])) {
478 for (i = 0; i < ARRAY_SIZE(word_types); i++) {
479 if (IS(n, word_types[i])) {
485 for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
486 if (IS(n, byte_types[i])) {
495 static enum opr_type lmod_from_directive(const char *d)
499 else if (IS(d, "dw"))
501 else if (IS(d, "db"))
504 aerr("unhandled directive: '%s'\n", d);
508 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
514 *regmask |= 1 << reg;
517 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
520 static int parse_operand(struct parsed_opr *opr,
521 int *regmask, int *regmask_indirect,
522 char words[16][256], int wordc, int w, unsigned int op_flags)
524 const struct parsed_proto *pp;
525 enum opr_lenmod tmplmod;
526 unsigned long number;
534 aerr("parse_operand w %d, wordc %d\n", w, wordc);
538 for (i = w; i < wordc; i++) {
539 len = strlen(words[i]);
540 if (words[i][len - 1] == ',') {
541 words[i][len - 1] = 0;
547 wordc_in = wordc - w;
549 if ((op_flags & OPF_JMP) && wordc_in > 0
550 && !('0' <= words[w][0] && words[w][0] <= '9'))
552 const char *label = NULL;
554 if (wordc_in == 3 && !strncmp(words[w], "near", 4)
555 && IS(words[w + 1], "ptr"))
556 label = words[w + 2];
557 else if (wordc_in == 2 && IS(words[w], "short"))
558 label = words[w + 1];
559 else if (wordc_in == 1
560 && strchr(words[w], '[') == NULL
561 && parse_reg(&tmplmod, words[w]) < 0)
565 opr->type = OPT_LABEL;
566 if (IS_START(label, "ds:")) {
570 strcpy(opr->name, label);
576 if (IS(words[w + 1], "ptr")) {
577 if (IS(words[w], "dword"))
578 opr->lmod = OPLM_DWORD;
579 else if (IS(words[w], "word"))
580 opr->lmod = OPLM_WORD;
581 else if (IS(words[w], "byte"))
582 opr->lmod = OPLM_BYTE;
584 aerr("type parsing failed\n");
586 wordc_in = wordc - w;
591 if (IS(words[w], "offset")) {
592 opr->type = OPT_OFFSET;
593 strcpy(opr->name, words[w + 1]);
596 if (IS(words[w], "(offset")) {
597 p = strchr(words[w + 1], ')');
599 aerr("parse of bracketed offset failed\n");
601 opr->type = OPT_OFFSET;
602 strcpy(opr->name, words[w + 1]);
608 aerr("parse_operand 1 word expected\n");
610 if (IS_START(words[w], "ds:")) {
612 memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
614 strcpy(opr->name, words[w]);
616 if (words[w][0] == '[') {
617 opr->type = OPT_REGMEM;
618 ret = sscanf(words[w], "[%[^]]]", opr->name);
620 aerr("[] parse failure\n");
622 parse_indmode(opr->name, regmask_indirect, 1);
623 if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL)) {
625 struct parsed_equ *eq =
626 equ_find(NULL, parse_stack_el(opr->name, NULL), &i);
628 opr->lmod = eq->lmod;
632 else if (strchr(words[w], '[')) {
634 p = strchr(words[w], '[');
635 opr->type = OPT_REGMEM;
636 parse_indmode(p, regmask_indirect, 0);
637 strncpy(buf, words[w], p - words[w]);
638 buf[p - words[w]] = 0;
639 pp = proto_parse(g_fhdr, buf, 1);
642 else if (('0' <= words[w][0] && words[w][0] <= '9')
643 || words[w][0] == '-')
645 number = parse_number(words[w]);
646 opr->type = OPT_CONST;
648 printf_number(opr->name, sizeof(opr->name), number);
652 ret = parse_reg(&tmplmod, opr->name);
654 setup_reg_opr(opr, ret, tmplmod, regmask);
658 // most likely var in data segment
659 opr->type = OPT_LABEL;
660 pp = proto_parse(g_fhdr, opr->name, 0);
664 if (pp->is_fptr || pp->is_func) {
665 opr->lmod = OPLM_DWORD;
669 tmplmod = OPLM_UNSPEC;
670 if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
671 anote("unhandled C type '%s' for '%s'\n",
672 pp->type.name, opr->name);
674 if (opr->lmod == OPLM_UNSPEC) {
676 opr->type_from_var = 1;
678 else if (opr->lmod != tmplmod) {
679 opr->size_mismatch = 1;
680 if (tmplmod < opr->lmod)
683 opr->is_ptr = pp->type.is_ptr;
685 opr->is_array = pp->type.is_array;
688 if (opr->lmod == OPLM_UNSPEC)
689 guess_lmod_from_name(opr);
693 static const struct {
698 { "repe", OPF_REP|OPF_REPZ },
699 { "repz", OPF_REP|OPF_REPZ },
700 { "repne", OPF_REP|OPF_REPNZ },
701 { "repnz", OPF_REP|OPF_REPNZ },
704 static const struct {
711 { "nop", OP_NOP, 0, 0, 0 },
712 { "push", OP_PUSH, 1, 1, 0 },
713 { "pop", OP_POP, 1, 1, OPF_DATA },
714 { "mov" , OP_MOV, 2, 2, OPF_DATA },
715 { "lea", OP_LEA, 2, 2, OPF_DATA },
716 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
717 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
718 { "not", OP_NOT, 1, 1, OPF_DATA },
719 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
720 { "stosb",OP_STOS, 0, 0, OPF_DATA },
721 { "stosw",OP_STOS, 0, 0, OPF_DATA },
722 { "stosd",OP_STOS, 0, 0, OPF_DATA },
723 { "movsb",OP_MOVS, 0, 0, OPF_DATA },
724 { "movsw",OP_MOVS, 0, 0, OPF_DATA },
725 { "movsd",OP_MOVS, 0, 0, OPF_DATA },
726 { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
727 { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
728 { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
729 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
730 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
731 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
732 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
733 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
734 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
735 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
736 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
737 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
738 { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS },
739 { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS },
740 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC },
741 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC },
742 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
743 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
744 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
745 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
746 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
747 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
748 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
749 { "test", OP_TEST, 2, 2, OPF_FLAGS },
750 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
751 { "retn", OP_RET, 0, 1, OPF_TAIL },
752 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
753 { "jmp", OP_JMP, 1, 1, OPF_JMP },
754 { "jo", OP_JO, 1, 1, OPF_JMP|OPF_CC }, // 70 OF=1
755 { "jno", OP_JNO, 1, 1, OPF_JMP|OPF_CC }, // 71 OF=0
756 { "jc", OP_JC, 1, 1, OPF_JMP|OPF_CC }, // 72 CF=1
757 { "jb", OP_JC, 1, 1, OPF_JMP|OPF_CC }, // 72
758 { "jnc", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73 CF=0
759 { "jnb", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73
760 { "jae", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73
761 { "jz", OP_JZ, 1, 1, OPF_JMP|OPF_CC }, // 74 ZF=1
762 { "je", OP_JZ, 1, 1, OPF_JMP|OPF_CC }, // 74
763 { "jnz", OP_JNZ, 1, 1, OPF_JMP|OPF_CC }, // 75 ZF=0
764 { "jne", OP_JNZ, 1, 1, OPF_JMP|OPF_CC }, // 75
765 { "jbe", OP_JBE, 1, 1, OPF_JMP|OPF_CC }, // 76 CF=1 || ZF=1
766 { "jna", OP_JBE, 1, 1, OPF_JMP|OPF_CC }, // 76
767 { "ja", OP_JA, 1, 1, OPF_JMP|OPF_CC }, // 77 CF=0 && ZF=0
768 { "jnbe", OP_JA, 1, 1, OPF_JMP|OPF_CC }, // 77
769 { "js", OP_JS, 1, 1, OPF_JMP|OPF_CC }, // 78 SF=1
770 { "jns", OP_JNS, 1, 1, OPF_JMP|OPF_CC }, // 79 SF=0
771 { "jp", OP_JP, 1, 1, OPF_JMP|OPF_CC }, // 7a PF=1
772 { "jpe", OP_JP, 1, 1, OPF_JMP|OPF_CC }, // 7a
773 { "jnp", OP_JNP, 1, 1, OPF_JMP|OPF_CC }, // 7b PF=0
774 { "jpo", OP_JNP, 1, 1, OPF_JMP|OPF_CC }, // 7b
775 { "jl", OP_JL, 1, 1, OPF_JMP|OPF_CC }, // 7c SF!=OF
776 { "jnge", OP_JL, 1, 1, OPF_JMP|OPF_CC }, // 7c
777 { "jge", OP_JGE, 1, 1, OPF_JMP|OPF_CC }, // 7d SF=OF
778 { "jnl", OP_JGE, 1, 1, OPF_JMP|OPF_CC }, // 7d
779 { "jle", OP_JLE, 1, 1, OPF_JMP|OPF_CC }, // 7e ZF=1 || SF!=OF
780 { "jng", OP_JLE, 1, 1, OPF_JMP|OPF_CC }, // 7e
781 { "jg", OP_JG, 1, 1, OPF_JMP|OPF_CC }, // 7f ZF=0 && SF=OF
782 { "jnle", OP_JG, 1, 1, OPF_JMP|OPF_CC }, // 7f
783 { "seto", OP_JO, 1, 1, OPF_DATA|OPF_CC },
784 { "setno", OP_JNO, 1, 1, OPF_DATA|OPF_CC },
785 { "setc", OP_JC, 1, 1, OPF_DATA|OPF_CC },
786 { "setb", OP_JC, 1, 1, OPF_DATA|OPF_CC },
787 { "setnc", OP_JNC, 1, 1, OPF_DATA|OPF_CC },
788 { "setae", OP_JNC, 1, 1, OPF_DATA|OPF_CC },
789 { "setz", OP_JZ, 1, 1, OPF_DATA|OPF_CC },
790 { "sete", OP_JZ, 1, 1, OPF_DATA|OPF_CC },
791 { "setnz", OP_JNZ, 1, 1, OPF_DATA|OPF_CC },
792 { "setne", OP_JNZ, 1, 1, OPF_DATA|OPF_CC },
793 { "setbe", OP_JBE, 1, 1, OPF_DATA|OPF_CC },
794 { "setna", OP_JBE, 1, 1, OPF_DATA|OPF_CC },
795 { "seta", OP_JA, 1, 1, OPF_DATA|OPF_CC },
796 { "setnbe", OP_JA, 1, 1, OPF_DATA|OPF_CC },
797 { "sets", OP_JS, 1, 1, OPF_DATA|OPF_CC },
798 { "setns", OP_JNS, 1, 1, OPF_DATA|OPF_CC },
799 { "setp", OP_JP, 1, 1, OPF_DATA|OPF_CC },
800 { "setpe", OP_JP, 1, 1, OPF_DATA|OPF_CC },
801 { "setnp", OP_JNP, 1, 1, OPF_DATA|OPF_CC },
802 { "setpo", OP_JNP, 1, 1, OPF_DATA|OPF_CC },
803 { "setl", OP_JL, 1, 1, OPF_DATA|OPF_CC },
804 { "setnge", OP_JL, 1, 1, OPF_DATA|OPF_CC },
805 { "setge", OP_JGE, 1, 1, OPF_DATA|OPF_CC },
806 { "setnl", OP_JGE, 1, 1, OPF_DATA|OPF_CC },
807 { "setle", OP_JLE, 1, 1, OPF_DATA|OPF_CC },
808 { "setng", OP_JLE, 1, 1, OPF_DATA|OPF_CC },
809 { "setg", OP_JG, 1, 1, OPF_DATA|OPF_CC },
810 { "setnle", OP_JG, 1, 1, OPF_DATA|OPF_CC },
813 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
815 enum opr_lenmod lmod = OPLM_UNSPEC;
816 int prefix_flags = 0;
824 for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
825 if (IS(words[w], pref_table[i].name)) {
826 prefix_flags = pref_table[i].flags;
833 aerr("lone prefix: '%s'\n", words[0]);
838 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
839 if (IS(words[w], op_table[i].name))
843 if (i == ARRAY_SIZE(op_table))
844 aerr("unhandled op: '%s'\n", words[0]);
847 op->op = op_table[i].op;
848 op->flags = op_table[i].flags | prefix_flags;
849 op->regmask_src = op->regmask_dst = 0;
851 for (opr = 0; opr < op_table[i].minopr; opr++) {
852 regmask = regmask_ind = 0;
853 w = parse_operand(&op->operand[opr], ®mask, ®mask_ind,
854 words, wordc, w, op->flags);
856 if (opr == 0 && (op->flags & OPF_DATA))
857 op->regmask_dst = regmask;
858 // for now, mark dst as src too
859 op->regmask_src |= regmask | regmask_ind;
862 for (; w < wordc && opr < op_table[i].maxopr; opr++) {
863 w = parse_operand(&op->operand[opr],
864 &op->regmask_src, &op->regmask_src,
865 words, wordc, w, op->flags);
869 aerr("parse_op %s incomplete: %d/%d\n",
873 op->operand_cnt = opr;
874 if (!strncmp(op_table[i].name, "set", 3))
875 op->operand[0].lmod = OPLM_BYTE;
877 // ops with implicit argumets
881 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
882 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
886 if (op->operand_cnt != 0)
888 if (IS(words[op_w], "stosb"))
890 else if (IS(words[op_w], "stosw"))
892 else if (IS(words[op_w], "stosd"))
895 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
896 setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src);
897 op->regmask_dst = op->regmask_src;
898 setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD, &op->regmask_src);
903 if (op->operand_cnt != 0)
905 if (words[op_w][4] == 'b')
907 else if (words[op_w][4] == 'w')
909 else if (words[op_w][4] == 'd')
912 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
913 setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src);
914 setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src);
915 op->regmask_dst = op->regmask_src;
919 if (op->operand_cnt != 1)
924 op->regmask_dst = (1 << xDX) | (1 << xAX);
925 op->regmask_src |= (1 << xAX);
926 if (op->operand[0].lmod == OPLM_UNSPEC)
927 op->operand[0].lmod = OPLM_DWORD;
932 // we could set up operands for edx:eax, but there is no real need to
933 // (see is_opr_modified())
934 regmask = (1 << xDX) | (1 << xAX);
935 op->regmask_dst = regmask;
936 op->regmask_src |= regmask;
937 if (op->operand[0].lmod == OPLM_UNSPEC)
938 op->operand[0].lmod = OPLM_DWORD;
946 if (op->operand[1].lmod == OPLM_UNSPEC)
947 op->operand[1].lmod = OPLM_BYTE;
951 if (op->operand[0].lmod == OPLM_UNSPEC
952 && (op->operand[0].type == OPT_CONST
953 || op->operand[0].type == OPT_OFFSET
954 || op->operand[0].type == OPT_LABEL))
955 op->operand[0].lmod = OPLM_DWORD;
960 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
961 && op->operand[0].reg == xDI && op->operand[1].reg == xDI)
963 op->flags |= OPF_RMD;
968 if (op->operand[0].type == OPT_REG
969 && op->operand[1].type == OPT_REGMEM)
972 snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
973 if (IS(buf, op->operand[1].name))
974 op->flags |= OPF_RMD;
979 // trashed regs must be explicitly detected later
988 static const char *op_name(enum op_op op)
992 for (i = 0; i < ARRAY_SIZE(op_table); i++)
993 if (op_table[i].op == op)
994 return op_table[i].name;
1000 static const char *dump_op(struct parsed_op *po)
1002 static char out[128];
1009 snprintf(out, sizeof(out), "%s", op_name(po->op));
1010 for (i = 0; i < po->operand_cnt; i++) {
1014 snprintf(p, sizeof(out) - (p - out),
1015 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1016 po->operand[i].name);
1022 static const char *lmod_type_u(struct parsed_op *po,
1023 enum opr_lenmod lmod)
1033 ferr(po, "invalid lmod: %d\n", lmod);
1034 return "(_invalid_)";
1038 static const char *lmod_cast_u(struct parsed_op *po,
1039 enum opr_lenmod lmod)
1049 ferr(po, "invalid lmod: %d\n", lmod);
1050 return "(_invalid_)";
1054 static const char *lmod_cast_u_ptr(struct parsed_op *po,
1055 enum opr_lenmod lmod)
1065 ferr(po, "invalid lmod: %d\n", lmod);
1066 return "(_invalid_)";
1070 static const char *lmod_cast_s(struct parsed_op *po,
1071 enum opr_lenmod lmod)
1081 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1082 return "(_invalid_)";
1086 static const char *lmod_cast(struct parsed_op *po,
1087 enum opr_lenmod lmod, int is_signed)
1090 lmod_cast_s(po, lmod) :
1091 lmod_cast_u(po, lmod);
1094 static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1104 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1109 static const char *opr_name(struct parsed_op *po, int opr_num)
1111 if (opr_num >= po->operand_cnt)
1112 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1113 return po->operand[opr_num].name;
1116 static unsigned int opr_const(struct parsed_op *po, int opr_num)
1118 if (opr_num >= po->operand_cnt)
1119 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1120 if (po->operand[opr_num].type != OPT_CONST)
1121 ferr(po, "opr %d: const expected\n", opr_num);
1122 return po->operand[opr_num].val;
1125 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1127 if ((unsigned int)popr->reg >= MAX_REGS)
1128 ferr(po, "invalid reg: %d\n", popr->reg);
1129 return regs_r32[popr->reg];
1132 // cast1 is the "final" cast
1133 static const char *simplify_cast(const char *cast1, const char *cast2)
1135 static char buf[256];
1141 if (IS(cast1, cast2))
1143 if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
1145 if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
1147 if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
1149 if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
1151 if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1154 snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1158 static const char *simplify_cast_num(const char *cast, unsigned int val)
1160 if (IS(cast, "(u8)") && val < 0x100)
1162 if (IS(cast, "(s8)") && val < 0x80)
1164 if (IS(cast, "(u16)") && val < 0x10000)
1166 if (IS(cast, "(s16)") && val < 0x8000)
1168 if (IS(cast, "(s32)") && val < 0x80000000)
1174 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1183 namelen = strlen(name);
1185 p = strchr(name, '+');
1189 ferr(po, "equ parse failed for '%s'\n", name);
1191 if (IS_START(p, "0x"))
1193 *extra_offs = strtol(p, &endp, 16);
1195 ferr(po, "equ parse failed for '%s'\n", name);
1198 for (i = 0; i < g_eqcnt; i++)
1199 if (strncmp(g_eqs[i].name, name, namelen) == 0
1200 && g_eqs[i].name[namelen] == 0)
1204 ferr(po, "unresolved equ name: '%s'\n", name);
1211 static int is_stack_access(struct parsed_op *po,
1212 const struct parsed_opr *popr)
1214 return (parse_stack_el(popr->name, NULL)
1215 || (g_bp_frame && !(po->flags & OPF_EBP_S)
1216 && IS_START(popr->name, "ebp")));
1219 static void parse_stack_access(struct parsed_op *po,
1220 const char *name, char *ofs_reg, int *offset_out,
1221 int *stack_ra_out, const char **bp_arg_out)
1223 const char *bp_arg = "";
1224 const char *p = NULL;
1225 struct parsed_equ *eq;
1232 if (IS_START(name, "ebp-")
1233 || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1236 if (IS_START(p, "0x"))
1238 offset = strtoul(p, &endp, 16);
1242 ferr(po, "ebp- parse of '%s' failed\n", name);
1245 bp_arg = parse_stack_el(name, ofs_reg);
1246 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1247 eq = equ_find(po, bp_arg, &offset);
1249 ferr(po, "detected but missing eq\n");
1250 offset += eq->offset;
1253 if (!strncmp(name, "ebp", 3))
1256 if (ofs_reg[0] == 0 && stack_ra <= offset && offset < stack_ra + 4)
1257 ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
1259 *offset_out = offset;
1260 *stack_ra_out = stack_ra;
1262 *bp_arg_out = bp_arg;
1265 static void stack_frame_access(struct parsed_op *po,
1266 struct parsed_opr *popr, char *buf, size_t buf_size,
1267 const char *name, const char *cast, int is_src, int is_lea)
1269 enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1270 const char *prefix = "";
1271 const char *bp_arg = NULL;
1272 char ofs_reg[16] = { 0, };
1273 int i, arg_i, arg_s;
1280 if (po->flags & OPF_EBP_S)
1281 ferr(po, "stack_frame_access while ebp is scratch\n");
1283 parse_stack_access(po, name, ofs_reg, &offset, &stack_ra, &bp_arg);
1285 if (offset > stack_ra)
1287 arg_i = (offset - stack_ra - 4) / 4;
1288 if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
1290 if (g_func_pp->is_vararg
1291 && arg_i == g_func_pp->argc_stack && is_lea)
1293 // should be va_list
1296 snprintf(buf, buf_size, "%sap", cast);
1299 ferr(po, "offset %d (%s,%d) doesn't map to any arg\n",
1300 offset, bp_arg, arg_i);
1302 if (ofs_reg[0] != 0)
1303 ferr(po, "offset reg on arg access?\n");
1305 for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1306 if (g_func_pp->arg[i].reg != NULL)
1312 if (i == g_func_pp->argc)
1313 ferr(po, "arg %d not in prototype?\n", arg_i);
1315 popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
1321 ferr(po, "lea/byte to arg?\n");
1322 if (is_src && (offset & 3) == 0)
1323 snprintf(buf, buf_size, "%sa%d",
1324 simplify_cast(cast, "(u8)"), i + 1);
1326 snprintf(buf, buf_size, "%sBYTE%d(a%d)",
1327 cast, offset & 3, i + 1);
1332 ferr(po, "lea/word to arg?\n");
1337 ferr(po, "problematic arg store\n");
1338 snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
1339 simplify_cast(cast, "*(u16 *)"), i + 1);
1342 ferr(po, "unaligned arg word load\n");
1344 else if (is_src && (offset & 2) == 0)
1345 snprintf(buf, buf_size, "%sa%d",
1346 simplify_cast(cast, "(u16)"), i + 1);
1348 snprintf(buf, buf_size, "%s%sWORD(a%d)",
1349 cast, (offset & 2) ? "HI" : "LO", i + 1);
1361 snprintf(buf, buf_size, "(u32)&a%d + %d",
1364 ferr(po, "unaligned arg store\n");
1366 // mov edx, [ebp+arg_4+2]; movsx ecx, dx
1367 snprintf(buf, buf_size, "%s(a%d >> %d)",
1368 prefix, i + 1, (offset & 3) * 8);
1372 snprintf(buf, buf_size, "%s%sa%d",
1373 prefix, is_lea ? "&" : "", i + 1);
1378 ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
1382 snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
1385 guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
1386 if (tmp_lmod != OPLM_DWORD
1387 && (unaligned || (!is_src && tmp_lmod < popr->lmod)))
1389 ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
1390 i + 1, offset, g_func_pp->arg[i].type.name);
1392 if (popr->is_ptr && popr->lmod != OPLM_DWORD)
1393 ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
1397 if (g_stack_fsz == 0)
1398 ferr(po, "stack var access without stackframe\n");
1399 g_stack_frame_used = 1;
1401 sf_ofs = g_stack_fsz + offset;
1402 lim = (ofs_reg[0] != 0) ? -4 : 0;
1403 if (offset > 0 || sf_ofs < lim)
1404 ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
1414 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1415 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1419 if ((sf_ofs & 1) || ofs_reg[0] != 0) {
1420 // known unaligned or possibly unaligned
1421 strcat(g_comment, " unaligned");
1423 prefix = "*(u16 *)&";
1424 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1425 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1428 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
1432 if ((sf_ofs & 3) || ofs_reg[0] != 0) {
1433 // known unaligned or possibly unaligned
1434 strcat(g_comment, " unaligned");
1436 prefix = "*(u32 *)&";
1437 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1438 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1441 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
1445 ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
1450 static void check_func_pp(struct parsed_op *po,
1451 const struct parsed_proto *pp, const char *pfx)
1453 if (pp->argc_reg != 0) {
1454 if (!g_allow_regfunc && !pp->is_fastcall)
1455 ferr(po, "%s: reg arg in arg-call unhandled yet\n", pfx);
1456 if (pp->argc_stack > 0 && pp->argc_reg != 2)
1457 ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
1458 pfx, pp->argc_reg, pp->argc_stack);
1462 static void check_label_read_ref(struct parsed_op *po, const char *name)
1464 const struct parsed_proto *pp;
1466 pp = proto_parse(g_fhdr, name, 0);
1468 ferr(po, "proto_parse failed for ref '%s'\n", name);
1471 check_func_pp(po, pp, "ref");
1474 static char *out_src_opr(char *buf, size_t buf_size,
1475 struct parsed_op *po, struct parsed_opr *popr, const char *cast,
1478 char tmp1[256], tmp2[256];
1486 switch (popr->type) {
1489 ferr(po, "lea from reg?\n");
1491 switch (popr->lmod) {
1493 snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
1496 snprintf(buf, buf_size, "%s%s",
1497 simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
1500 if (popr->name[1] == 'h') // XXX..
1501 snprintf(buf, buf_size, "%s(%s >> 8)",
1502 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1504 snprintf(buf, buf_size, "%s%s",
1505 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1508 ferr(po, "invalid src lmod: %d\n", popr->lmod);
1513 if (is_stack_access(po, popr)) {
1514 stack_frame_access(po, popr, buf, buf_size,
1515 popr->name, cast, 1, is_lea);
1519 strcpy(expr, popr->name);
1520 if (strchr(expr, '[')) {
1521 // special case: '[' can only be left for label[reg] form
1522 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
1524 ferr(po, "parse failure for '%s'\n", expr);
1525 if (tmp1[0] == '(') {
1526 // (off_4FFF50+3)[eax]
1527 p = strchr(tmp1 + 1, ')');
1528 if (p == NULL || p[1] != 0)
1529 ferr(po, "parse failure (2) for '%s'\n", expr);
1531 memmove(tmp1, tmp1 + 1, strlen(tmp1));
1533 snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
1536 // XXX: do we need more parsing?
1538 snprintf(buf, buf_size, "%s", expr);
1542 snprintf(buf, buf_size, "%s(%s)",
1543 simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
1547 check_label_read_ref(po, popr->name);
1548 if (cast[0] == 0 && popr->is_ptr)
1552 snprintf(buf, buf_size, "(u32)&%s", popr->name);
1553 else if (popr->size_lt)
1554 snprintf(buf, buf_size, "%s%s%s%s", cast,
1555 lmod_cast_u_ptr(po, popr->lmod),
1556 popr->is_array ? "" : "&",
1559 snprintf(buf, buf_size, "%s%s%s", cast, popr->name,
1560 popr->is_array ? "[0]" : "");
1564 check_label_read_ref(po, popr->name);
1568 ferr(po, "lea an offset?\n");
1569 snprintf(buf, buf_size, "%s&%s", cast, popr->name);
1574 ferr(po, "lea from const?\n");
1576 printf_number(tmp1, sizeof(tmp1), popr->val);
1577 if (popr->val == 0 && strchr(cast, '*'))
1578 snprintf(buf, buf_size, "NULL");
1580 snprintf(buf, buf_size, "%s%s",
1581 simplify_cast_num(cast, popr->val), tmp1);
1585 ferr(po, "invalid src type: %d\n", popr->type);
1591 // note: may set is_ptr (we find that out late for ebp frame..)
1592 static char *out_dst_opr(char *buf, size_t buf_size,
1593 struct parsed_op *po, struct parsed_opr *popr)
1595 switch (popr->type) {
1597 switch (popr->lmod) {
1599 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
1603 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
1607 if (popr->name[1] == 'h') // XXX..
1608 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
1610 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
1613 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
1618 if (is_stack_access(po, popr)) {
1619 stack_frame_access(po, popr, buf, buf_size,
1620 popr->name, "", 0, 0);
1624 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1627 if (popr->size_mismatch)
1628 snprintf(buf, buf_size, "%s%s%s",
1629 lmod_cast_u_ptr(po, popr->lmod),
1630 popr->is_array ? "" : "&", popr->name);
1632 snprintf(buf, buf_size, "%s%s", popr->name,
1633 popr->is_array ? "[0]" : "");
1637 ferr(po, "invalid dst type: %d\n", popr->type);
1643 static char *out_src_opr_u32(char *buf, size_t buf_size,
1644 struct parsed_op *po, struct parsed_opr *popr)
1646 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1649 static enum parsed_flag_op split_cond(struct parsed_op *po,
1650 enum op_op op, int *is_inv)
1702 ferr(po, "split_cond: bad op %d\n", op);
1707 static void out_test_for_cc(char *buf, size_t buf_size,
1708 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1709 enum opr_lenmod lmod, const char *expr)
1711 const char *cast, *scast;
1713 cast = lmod_cast_u(po, lmod);
1714 scast = lmod_cast_s(po, lmod);
1718 case PFO_BE: // CF=1||ZF=1; CF=0
1719 snprintf(buf, buf_size, "(%s%s %s 0)",
1720 cast, expr, is_inv ? "!=" : "==");
1724 case PFO_L: // SF!=OF; OF=0
1725 snprintf(buf, buf_size, "(%s%s %s 0)",
1726 scast, expr, is_inv ? ">=" : "<");
1729 case PFO_LE: // ZF=1||SF!=OF; OF=0
1730 snprintf(buf, buf_size, "(%s%s %s 0)",
1731 scast, expr, is_inv ? ">" : "<=");
1735 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1739 static void out_cmp_for_cc(char *buf, size_t buf_size,
1740 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1742 const char *cast, *scast, *cast_use;
1743 char buf1[256], buf2[256];
1744 enum opr_lenmod lmod;
1746 if (po->operand[0].lmod != po->operand[1].lmod)
1747 ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
1748 po->operand[0].lmod, po->operand[1].lmod);
1749 lmod = po->operand[0].lmod;
1751 cast = lmod_cast_u(po, lmod);
1752 scast = lmod_cast_s(po, lmod);
1768 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1771 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
1772 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
1776 // note: must be unsigned compare
1777 snprintf(buf, buf_size, "(%s %s %s)",
1778 buf1, is_inv ? ">=" : "<", buf2);
1782 snprintf(buf, buf_size, "(%s %s %s)",
1783 buf1, is_inv ? "!=" : "==", buf2);
1787 // note: must be unsigned compare
1788 snprintf(buf, buf_size, "(%s %s %s)",
1789 buf1, is_inv ? ">" : "<=", buf2);
1792 if (is_inv && lmod == OPLM_BYTE
1793 && po->operand[1].type == OPT_CONST
1794 && po->operand[1].val == 0xff)
1796 snprintf(g_comment, sizeof(g_comment), "if %s", buf);
1797 snprintf(buf, buf_size, "(0)");
1801 // note: must be signed compare
1803 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
1804 scast, buf1, buf2, is_inv ? ">=" : "<");
1808 snprintf(buf, buf_size, "(%s %s %s)",
1809 buf1, is_inv ? ">=" : "<", buf2);
1813 snprintf(buf, buf_size, "(%s %s %s)",
1814 buf1, is_inv ? ">" : "<=", buf2);
1822 static void out_cmp_test(char *buf, size_t buf_size,
1823 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1825 char buf1[256], buf2[256], buf3[256];
1827 if (po->op == OP_TEST) {
1828 if (IS(opr_name(po, 0), opr_name(po, 1))) {
1829 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
1832 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
1833 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
1834 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
1836 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
1837 po->operand[0].lmod, buf3);
1839 else if (po->op == OP_CMP) {
1840 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
1843 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
1846 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
1847 struct parsed_opr *popr2)
1849 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
1850 ferr(po, "missing lmod for both operands\n");
1852 if (popr1->lmod == OPLM_UNSPEC)
1853 popr1->lmod = popr2->lmod;
1854 else if (popr2->lmod == OPLM_UNSPEC)
1855 popr2->lmod = popr1->lmod;
1856 else if (popr1->lmod != popr2->lmod) {
1857 if (popr1->type_from_var) {
1858 popr1->size_mismatch = 1;
1859 if (popr1->lmod < popr2->lmod)
1861 popr1->lmod = popr2->lmod;
1863 else if (popr2->type_from_var) {
1864 popr2->size_mismatch = 1;
1865 if (popr2->lmod < popr1->lmod)
1867 popr2->lmod = popr1->lmod;
1870 ferr(po, "conflicting lmods: %d vs %d\n",
1871 popr1->lmod, popr2->lmod);
1875 static const char *op_to_c(struct parsed_op *po)
1899 ferr(po, "op_to_c was supplied with %d\n", po->op);
1903 static void op_set_clear_flag(struct parsed_op *po,
1904 enum op_flags flag_set, enum op_flags flag_clear)
1906 po->flags |= flag_set;
1907 po->flags &= ~flag_clear;
1910 // last op in stream - unconditional branch or ret
1911 #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
1912 || (ops[_i].flags & (OPF_JMP|OPF_CC)) == OPF_JMP)
1914 static int scan_for_pop(int i, int opcnt, const char *reg,
1915 int magic, int depth, int *maxdepth, int do_flags)
1917 const struct parsed_proto *pp;
1918 struct parsed_op *po;
1922 for (; i < opcnt; i++) {
1924 if (po->cc_scratch == magic)
1925 break; // already checked
1926 po->cc_scratch = magic;
1928 if (po->flags & OPF_TAIL) {
1929 if (po->op == OP_CALL) {
1930 pp = proto_parse(g_fhdr, po->operand[0].name, 0);
1931 if (pp != NULL && pp->is_noreturn)
1932 // no stack cleanup for noreturn
1935 return -1; // deadend
1938 if ((po->flags & OPF_RMD)
1939 || (po->op == OP_PUSH && po->argnum != 0)) // arg push
1942 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
1943 if (po->btj != NULL) {
1945 for (j = 0; j < po->btj->count; j++) {
1946 ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic,
1947 depth, maxdepth, do_flags);
1949 return ret; // dead end
1955 ferr(po, "dead branch\n");
1959 if (po->flags & OPF_CC) {
1960 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic,
1961 depth, maxdepth, do_flags);
1963 return ret; // dead end
1971 if ((po->op == OP_POP || po->op == OP_PUSH)
1972 && po->operand[0].type == OPT_REG
1973 && IS(po->operand[0].name, reg))
1975 if (po->op == OP_PUSH && !(po->flags & OPF_FARG)) {
1977 if (depth > *maxdepth)
1980 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
1982 else if (po->op == OP_POP) {
1985 op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
1990 if (depth < 0) // should not happen
1991 ferr(po, "fail with depth\n");
1993 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2002 // scan for pop starting from 'ret' op (all paths)
2003 static int scan_for_pop_ret(int i, int opcnt, const char *reg,
2009 for (; i < opcnt; i++) {
2010 if (!(ops[i].flags & OPF_TAIL))
2013 for (j = i - 1; j >= 0; j--) {
2014 if (ops[j].flags & OPF_RMD)
2016 if (ops[j].flags & OPF_JMP)
2019 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
2020 && IS(ops[j].operand[0].name, reg))
2023 ops[j].flags |= flag_set;
2027 if (g_labels[j][0] != 0)
2032 return found ? 0 : -1;
2035 // is operand 'opr' modified by parsed_op 'po'?
2036 static int is_opr_modified(const struct parsed_opr *opr,
2037 const struct parsed_op *po)
2041 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2044 if (opr->type == OPT_REG) {
2045 if (po->op == OP_CALL) {
2046 mask = (1 << xAX) | (1 << xCX) | (1 << xDX);
2047 if ((1 << opr->reg) & mask)
2053 if (po->operand[0].type == OPT_REG) {
2054 if (po->regmask_dst & (1 << opr->reg))
2061 return IS(po->operand[0].name, opr->name);
2064 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
2065 static int is_any_opr_modified(const struct parsed_op *po_test,
2066 const struct parsed_op *po, int c_mode)
2071 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2074 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2077 if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
2080 // in reality, it can wreck any register, but in decompiled C
2081 // version it can only overwrite eax or edx:eax
2082 mask = (1 << xAX) | (1 << xDX);
2086 if (po->op == OP_CALL
2087 && ((po_test->regmask_src | po_test->regmask_dst) & mask))
2090 for (i = 0; i < po_test->operand_cnt; i++)
2091 if (IS(po_test->operand[i].name, po->operand[0].name))
2097 // scan for any po_test operand modification in range given
2098 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
2101 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2104 for (; i < opcnt; i++) {
2105 if (is_any_opr_modified(po_test, &ops[i], c_mode))
2112 // scan for po_test operand[0] modification in range given
2113 static int scan_for_mod_opr0(struct parsed_op *po_test,
2116 for (; i < opcnt; i++) {
2117 if (is_opr_modified(&po_test->operand[0], &ops[i]))
2124 static int scan_for_flag_set(int i, int magic, int *branched,
2125 int *setters, int *setter_cnt)
2127 struct label_ref *lr;
2131 if (ops[i].cc_scratch == magic) {
2132 ferr(&ops[i], "%s looped\n", __func__);
2135 ops[i].cc_scratch = magic;
2137 if (g_labels[i][0] != 0) {
2140 lr = &g_label_refs[i];
2141 for (; lr->next; lr = lr->next) {
2142 ret = scan_for_flag_set(lr->i, magic,
2143 branched, setters, setter_cnt);
2148 if (i > 0 && LAST_OP(i - 1)) {
2152 ret = scan_for_flag_set(lr->i, magic,
2153 branched, setters, setter_cnt);
2159 if (ops[i].flags & OPF_FLAGS) {
2160 setters[*setter_cnt] = i;
2165 if ((ops[i].flags & OPF_JMP) && !(ops[i].flags & OPF_CC))
2172 // scan back for cdq, if anything modifies edx, fail
2173 static int scan_for_cdq_edx(int i)
2176 if (g_labels[i][0] != 0) {
2177 if (g_label_refs[i].next != NULL)
2179 if (i > 0 && LAST_OP(i - 1)) {
2180 i = g_label_refs[i].i;
2187 if (ops[i].op == OP_CDQ)
2190 if (ops[i].regmask_dst & (1 << xDX))
2197 static int scan_for_reg_clear(int i, int reg)
2200 if (g_labels[i][0] != 0) {
2201 if (g_label_refs[i].next != NULL)
2203 if (i > 0 && LAST_OP(i - 1)) {
2204 i = g_label_refs[i].i;
2211 if (ops[i].op == OP_XOR
2212 && ops[i].operand[0].lmod == OPLM_DWORD
2213 && ops[i].operand[0].reg == ops[i].operand[1].reg
2214 && ops[i].operand[0].reg == reg)
2217 if (ops[i].regmask_dst & (1 << reg))
2224 // scan for positive, constant esp adjust
2225 static int scan_for_esp_adjust(int i, int opcnt, int *adj)
2227 const struct parsed_proto *pp;
2228 struct parsed_op *po;
2232 for (; i < opcnt; i++) {
2235 if (g_labels[i][0] != 0)
2238 if (po->op == OP_ADD && po->operand[0].reg == xSP) {
2239 if (po->operand[1].type != OPT_CONST)
2240 ferr(&ops[i], "non-const esp adjust?\n");
2241 *adj += po->operand[1].val;
2243 ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
2246 else if (po->op == OP_PUSH) {
2247 if (first_pop == -1)
2248 first_pop = -2; // none
2249 *adj -= lmod_bytes(po, po->operand[0].lmod);
2251 else if (po->op == OP_POP) {
2252 if (first_pop == -1)
2254 *adj += lmod_bytes(po, po->operand[0].lmod);
2256 else if (po->flags & (OPF_JMP|OPF_TAIL)) {
2257 if (po->op != OP_CALL)
2259 if (po->operand[0].type != OPT_LABEL)
2262 if (pp != NULL && pp->is_stdcall)
2267 if (*adj == 4 && first_pop >= 0 && ops[first_pop].op == OP_POP
2268 && ops[first_pop].operand[0].type == OPT_REG
2269 && ops[first_pop].operand[0].reg == xCX)
2271 // probably 'pop ecx' was used..
2278 static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
2280 struct parsed_op *po;
2284 ferr(ops, "%s: followed bad branch?\n", __func__);
2286 for (; i < opcnt; i++) {
2288 if (po->cc_scratch == magic)
2290 po->cc_scratch = magic;
2293 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2294 if (po->btj != NULL) {
2296 for (j = 0; j < po->btj->count; j++)
2297 scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
2301 scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
2302 if (!(po->flags & OPF_CC))
2305 if (po->flags & OPF_TAIL)
2310 static const struct parsed_proto *try_recover_pp(
2311 struct parsed_op *po, const struct parsed_opr *opr, int *search_instead)
2313 const struct parsed_proto *pp = NULL;
2317 // maybe an arg of g_func?
2318 if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
2320 char ofs_reg[16] = { 0, };
2321 int arg, arg_s, arg_i;
2325 parse_stack_access(po, opr->name, ofs_reg,
2326 &offset, &stack_ra, NULL);
2327 if (ofs_reg[0] != 0)
2328 ferr(po, "offset reg on arg access?\n");
2329 if (offset <= stack_ra) {
2330 // search who set the stack var instead
2331 if (search_instead != NULL)
2332 *search_instead = 1;
2336 arg_i = (offset - stack_ra - 4) / 4;
2337 for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
2338 if (g_func_pp->arg[arg].reg != NULL)
2344 if (arg == g_func_pp->argc)
2345 ferr(po, "stack arg %d not in prototype?\n", arg_i);
2347 pp = g_func_pp->arg[arg].fptr;
2349 ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1);
2350 check_func_pp(po, pp, "icall arg");
2352 else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
2354 p = strchr(opr->name + 1, '[');
2355 memcpy(buf, opr->name, p - opr->name);
2356 buf[p - opr->name] = 0;
2357 pp = proto_parse(g_fhdr, buf, 0);
2359 else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
2360 pp = proto_parse(g_fhdr, opr->name, 0);
2362 ferr(po, "proto_parse failed for icall from '%s'\n", opr->name);
2363 check_func_pp(po, pp, "reg-fptr ref");
2369 static void scan_for_call_type(int i, const struct parsed_opr *opr,
2370 int magic, const struct parsed_proto **pp_found, int *multi)
2372 const struct parsed_proto *pp = NULL;
2373 struct parsed_op *po;
2374 struct label_ref *lr;
2377 if (ops[i].cc_scratch == magic)
2379 ops[i].cc_scratch = magic;
2381 if (g_labels[i][0] != 0) {
2382 lr = &g_label_refs[i];
2383 for (; lr != NULL; lr = lr->next)
2384 scan_for_call_type(lr->i, opr, magic, pp_found, multi);
2385 if (i > 0 && LAST_OP(i - 1))
2390 if (!(ops[i].flags & OPF_DATA))
2392 if (!is_opr_modified(opr, &ops[i]))
2394 if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
2395 // most probably trashed by some processing
2400 opr = &ops[i].operand[1];
2401 if (opr->type != OPT_REG)
2405 po = (i >= 0) ? &ops[i] : ops;
2408 // reached the top - can only be an arg-reg
2409 if (opr->type != OPT_REG)
2412 for (i = 0; i < g_func_pp->argc; i++) {
2413 if (g_func_pp->arg[i].reg == NULL)
2415 if (IS(opr->name, g_func_pp->arg[i].reg))
2418 if (i == g_func_pp->argc)
2420 pp = g_func_pp->arg[i].fptr;
2422 ferr(po, "icall: arg%d (%s) is not a fptr?\n",
2423 i + 1, g_func_pp->arg[i].reg);
2424 check_func_pp(po, pp, "icall reg-arg");
2427 pp = try_recover_pp(po, opr, NULL);
2429 if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
2430 if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
2431 || (*pp_found)->is_stdcall != pp->is_stdcall
2432 || (*pp_found)->is_fptr != pp->is_fptr
2433 || (*pp_found)->argc != pp->argc
2434 || (*pp_found)->argc_reg != pp->argc_reg
2435 || (*pp_found)->argc_stack != pp->argc_stack)
2437 ferr(po, "icall: parsed_proto mismatch\n");
2445 static const struct parsed_proto *resolve_icall(int i, int opcnt,
2448 const struct parsed_proto *pp = NULL;
2449 int search_advice = 0;
2453 switch (ops[i].operand[0].type) {
2457 pp = try_recover_pp(&ops[i], &ops[i].operand[0], &search_advice);
2462 scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
2470 static int collect_call_args_r(struct parsed_op *po, int i,
2471 struct parsed_proto *pp, int *regmask, int *save_arg_vars, int arg,
2472 int magic, int need_op_saving, int may_reuse)
2474 struct parsed_proto *pp_tmp;
2475 struct label_ref *lr;
2476 int need_to_save_current;
2481 ferr(po, "dead label encountered\n");
2485 for (; arg < pp->argc; arg++)
2486 if (pp->arg[arg].reg == NULL)
2488 magic = (magic & 0xffffff) | (arg << 24);
2490 for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
2492 if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
2493 if (ops[j].cc_scratch != magic) {
2494 ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
2498 // ok: have already been here
2501 ops[j].cc_scratch = magic;
2503 if (g_labels[j][0] != 0 && g_label_refs[j].i != -1) {
2504 lr = &g_label_refs[j];
2505 if (lr->next != NULL)
2507 for (; lr->next; lr = lr->next) {
2508 if ((ops[lr->i].flags & (OPF_JMP|OPF_CC)) != OPF_JMP)
2510 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2511 arg, magic, need_op_saving, may_reuse);
2516 if ((ops[lr->i].flags & (OPF_JMP|OPF_CC)) != OPF_JMP)
2518 if (j > 0 && LAST_OP(j - 1)) {
2519 // follow last branch in reverse
2524 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
2525 arg, magic, need_op_saving, may_reuse);
2531 if (ops[j].op == OP_CALL)
2533 if (pp->is_unresolved)
2536 pp_tmp = ops[j].datap;
2538 ferr(po, "arg collect hit unparsed call '%s'\n",
2539 ops[j].operand[0].name);
2540 if (may_reuse && pp_tmp->argc_stack > 0)
2541 ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
2542 arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
2544 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP) {
2545 if (pp->is_unresolved)
2548 ferr(po, "arg collect %d/%d hit esp adjust\n",
2551 else if (ops[j].op == OP_POP) {
2552 if (pp->is_unresolved)
2555 ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc);
2557 else if ((ops[j].flags & (OPF_JMP|OPF_CC)) == (OPF_JMP|OPF_CC))
2559 if (pp->is_unresolved)
2564 else if (ops[j].op == OP_PUSH && !(ops[j].flags & OPF_FARG))
2566 if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
2569 pp->arg[arg].datap = &ops[j];
2570 need_to_save_current = 0;
2571 if (!need_op_saving) {
2572 ret = scan_for_mod(&ops[j], j + 1, i, 1);
2573 need_to_save_current = (ret >= 0);
2575 if (need_op_saving || need_to_save_current) {
2576 // mark this push as one that needs operand saving
2577 ops[j].flags &= ~OPF_RMD;
2578 if (ops[j].argnum == 0) {
2579 ops[j].argnum = arg + 1;
2580 *save_arg_vars |= 1 << arg;
2582 else if (ops[j].argnum < arg + 1)
2583 ferr(&ops[j], "argnum conflict (%d<%d) for '%s'\n",
2584 ops[j].argnum, arg + 1, pp->name);
2586 else if (ops[j].argnum == 0)
2587 ops[j].flags |= OPF_RMD;
2589 // some PUSHes are reused by different calls on other branches,
2590 // but that can't happen if we didn't branch, so they
2591 // can be removed from future searches (handles nested calls)
2593 ops[j].flags |= OPF_FARG;
2595 ops[j].flags &= ~OPF_RSAVE;
2598 if (!pp->is_unresolved) {
2600 for (; arg < pp->argc; arg++)
2601 if (pp->arg[arg].reg == NULL)
2604 magic = (magic & 0xffffff) | (arg << 24);
2606 // tracking reg usage
2607 if (ops[j].operand[0].type == OPT_REG)
2608 *regmask |= 1 << ops[j].operand[0].reg;
2612 if (arg < pp->argc) {
2613 ferr(po, "arg collect failed for '%s': %d/%d\n",
2614 pp->name, arg, pp->argc);
2621 static int collect_call_args(struct parsed_op *po, int i,
2622 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
2628 ret = collect_call_args_r(po, i, pp, regmask, save_arg_vars,
2633 if (pp->is_unresolved) {
2635 pp->argc_stack += ret;
2636 for (a = 0; a < pp->argc; a++)
2637 if (pp->arg[a].type.name == NULL)
2638 pp->arg[a].type.name = strdup("int");
2644 static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
2648 for (i = 0; i < pp->argc; i++)
2649 if (pp->arg[i].reg == NULL)
2653 memmove(&pp->arg[i + 1], &pp->arg[i],
2654 sizeof(pp->arg[0]) * pp->argc_stack);
2655 memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
2656 pp->arg[i].reg = strdup(reg);
2657 pp->arg[i].type.name = strdup("int");
2662 static void add_label_ref(struct label_ref *lr, int op_i)
2664 struct label_ref *lr_new;
2671 lr_new = calloc(1, sizeof(*lr_new));
2673 lr_new->next = lr->next;
2677 static void output_std_flags(FILE *fout, struct parsed_op *po,
2678 int *pfomask, const char *dst_opr_text)
2680 if (*pfomask & (1 << PFO_Z)) {
2681 fprintf(fout, "\n cond_z = (%s%s == 0);",
2682 lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
2683 *pfomask &= ~(1 << PFO_Z);
2685 if (*pfomask & (1 << PFO_S)) {
2686 fprintf(fout, "\n cond_s = (%s%s < 0);",
2687 lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
2688 *pfomask &= ~(1 << PFO_S);
2692 static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp,
2695 if (pp->is_fastcall)
2696 fprintf(fout, "__fastcall ");
2697 else if (pp->is_stdcall && pp->argc_reg == 0)
2698 fprintf(fout, "__stdcall ");
2699 if (pp->is_noreturn || is_noreturn)
2700 fprintf(fout, "noreturn ");
2703 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
2705 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
2706 struct parsed_opr *last_arith_dst = NULL;
2707 char buf1[256], buf2[256], buf3[256], cast[64];
2708 const struct parsed_proto *pp_c;
2709 struct parsed_proto *pp, *pp_tmp;
2710 struct parsed_data *pd;
2711 const char *tmpname;
2712 enum parsed_flag_op pfo;
2713 int save_arg_vars = 0;
2714 int cmp_result_vars = 0;
2715 int need_mul_var = 0;
2717 int label_pending = 0;
2718 int regmask_save = 0;
2719 int regmask_arg = 0;
2720 int regmask_now = 0;
2721 int regmask_init = 0;
2733 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
2734 g_stack_frame_used = 0;
2736 g_func_pp = proto_parse(fhdr, funcn, 0);
2737 if (g_func_pp == NULL)
2738 ferr(ops, "proto_parse failed for '%s'\n", funcn);
2740 fprintf(fout, "%s ", g_func_pp->ret_type.name);
2741 output_pp_attrs(fout, g_func_pp, g_ida_func_attr & IDAFA_NORETURN);
2742 fprintf(fout, "%s(", funcn);
2744 for (i = 0; i < g_func_pp->argc; i++) {
2746 fprintf(fout, ", ");
2747 if (g_func_pp->arg[i].fptr != NULL) {
2749 pp = g_func_pp->arg[i].fptr;
2750 fprintf(fout, "%s (", pp->ret_type.name);
2751 output_pp_attrs(fout, pp, 0);
2752 fprintf(fout, "*a%d)(", i + 1);
2753 for (j = 0; j < pp->argc; j++) {
2755 fprintf(fout, ", ");
2756 if (pp->arg[j].fptr)
2757 ferr(ops, "nested fptr\n");
2758 fprintf(fout, "%s", pp->arg[j].type.name);
2763 fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1);
2765 if (g_func_pp->arg[i].reg != NULL) {
2766 reg = char_array_i(regs_r32,
2767 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
2769 ferr(ops, "arg '%s' is not a reg?\n", g_func_pp->arg[i].reg);
2770 regmask_arg |= 1 << reg;
2773 if (g_func_pp->is_vararg) {
2775 fprintf(fout, ", ");
2776 fprintf(fout, "...");
2779 fprintf(fout, ")\n{\n");
2782 // - handle ebp/esp frame, remove ops related to it
2783 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
2784 && ops[1].op == OP_MOV
2785 && IS(opr_name(&ops[1], 0), "ebp")
2786 && IS(opr_name(&ops[1], 1), "esp"))
2791 ops[0].flags |= OPF_RMD;
2792 ops[1].flags |= OPF_RMD;
2795 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
2796 g_stack_fsz = opr_const(&ops[2], 1);
2797 ops[2].flags |= OPF_RMD;
2801 // another way msvc builds stack frame..
2803 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
2805 ops[i].flags |= OPF_RMD;
2809 // and another way..
2810 if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
2811 && ops[i].operand[1].type == OPT_CONST
2812 && ops[i + 1].op == OP_CALL
2813 && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
2815 g_stack_fsz += ops[i].operand[1].val;
2816 ops[i].flags |= OPF_RMD;
2818 ops[i].flags |= OPF_RMD;
2825 for (; i < opcnt; i++)
2826 if (ops[i].op == OP_RET)
2828 if (i == opcnt && (ops[i - 1].flags & OPF_JMP) && found)
2831 if (ops[i - 1].op == OP_POP && IS(opr_name(&ops[i - 1], 0), "ebp"))
2832 ops[i - 1].flags |= OPF_RMD;
2833 else if (!(g_ida_func_attr & IDAFA_NORETURN))
2834 ferr(&ops[i - 1], "'pop ebp' expected\n");
2836 if (g_stack_fsz != 0) {
2837 if (ops[i - 2].op == OP_MOV
2838 && IS(opr_name(&ops[i - 2], 0), "esp")
2839 && IS(opr_name(&ops[i - 2], 1), "ebp"))
2841 ops[i - 2].flags |= OPF_RMD;
2843 else if (!(g_ida_func_attr & IDAFA_NORETURN))
2844 ferr(&ops[i - 2], "esp restore expected\n");
2846 if (ecx_push && ops[i - 3].op == OP_POP
2847 && IS(opr_name(&ops[i - 3], 0), "ecx"))
2849 ferr(&ops[i - 3], "unexpected ecx pop\n");
2855 } while (i < opcnt);
2858 for (i = 0; i < opcnt; i++) {
2859 if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
2861 if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
2862 && ops[i].operand[1].type == OPT_CONST)
2871 g_stack_fsz = ops[i].operand[1].val;
2872 ops[i].flags |= OPF_RMD;
2876 for (; i < opcnt; i++)
2877 if (ops[i].op == OP_RET)
2879 if (ops[i - 1].op != OP_ADD
2880 || !IS(opr_name(&ops[i - 1], 0), "esp")
2881 || ops[i - 1].operand[1].type != OPT_CONST
2882 || ops[i - 1].operand[1].val != g_stack_fsz)
2883 ferr(&ops[i - 1], "'add esp' expected\n");
2884 ops[i - 1].flags |= OPF_RMD;
2887 } while (i < opcnt);
2892 // - parse calls with labels
2893 // - resolve all branches
2894 for (i = 0; i < opcnt; i++)
2900 if (po->flags & OPF_RMD)
2903 if (po->op == OP_CALL) {
2906 if (po->operand[0].type == OPT_LABEL) {
2907 tmpname = opr_name(po, 0);
2908 if (IS_START(tmpname, "loc_"))
2909 ferr(po, "call to loc_*\n");
2910 pp_c = proto_parse(fhdr, tmpname, 0);
2912 ferr(po, "proto_parse failed for call '%s'\n", tmpname);
2914 pp = proto_clone(pp_c);
2915 my_assert_not(pp, NULL);
2917 else if (po->datap != NULL) {
2918 pp = calloc(1, sizeof(*pp));
2919 my_assert_not(pp, NULL);
2921 ret = parse_protostr(po->datap, pp);
2923 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
2930 check_func_pp(po, pp, "fptr var call");
2931 if (pp->is_noreturn)
2932 po->flags |= OPF_TAIL;
2938 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
2941 if (po->operand[0].type == OPT_REGMEM) {
2942 char *p = strchr(po->operand[0].name, '[');
2945 ret = p - po->operand[0].name;
2946 strncpy(buf1, po->operand[0].name, ret);
2949 for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
2950 if (IS(g_func_pd[j].label, buf1)) {
2956 //ferr(po, "label '%s' not parsed?\n", buf1);
2958 if (pd->type != OPT_OFFSET)
2959 ferr(po, "label '%s' with non-offset data?\n", buf1);
2961 // find all labels, link
2962 for (j = 0; j < pd->count; j++) {
2963 for (l = 0; l < opcnt; l++) {
2964 if (g_labels[l][0] && IS(g_labels[l], pd->d[j].u.label)) {
2965 add_label_ref(&g_label_refs[l], i);
2976 for (l = 0; l < opcnt; l++) {
2977 if (g_labels[l][0] && IS(po->operand[0].name, g_labels[l])) {
2978 add_label_ref(&g_label_refs[l], i);
2987 if (po->operand[0].type == OPT_LABEL)
2991 ferr(po, "unhandled branch\n");
2995 po->flags |= OPF_TAIL;
3000 // - remove dead labels
3002 for (i = 0; i < opcnt; i++)
3004 if (g_labels[i][0] != 0 && g_label_refs[i].i == -1)
3008 if (po->flags & OPF_RMD)
3011 if (po->op == OP_CALL)
3013 tmpname = opr_name(po, 0);
3018 pp_c = resolve_icall(i, opcnt, &l);
3020 pp = proto_clone(pp_c);
3021 my_assert_not(pp, NULL);
3023 // not resolved just to single func
3027 pp = calloc(1, sizeof(*pp));
3028 my_assert_not(pp, NULL);
3030 ret = scan_for_esp_adjust(i + 1, opcnt, &j);
3032 if (!g_allow_regfunc)
3033 ferr(po, "non-__cdecl indirect call unhandled yet\n");
3034 pp->is_unresolved = 1;
3038 if (j > ARRAY_SIZE(pp->arg))
3039 ferr(po, "esp adjust too large: %d\n", j);
3040 pp->ret_type.name = strdup("int");
3041 pp->argc = pp->argc_stack = j;
3042 for (arg = 0; arg < pp->argc; arg++)
3043 pp->arg[arg].type.name = strdup("int");
3048 // look for and make use of esp adjust
3050 if (!pp->is_stdcall && pp->argc_stack > 0)
3051 ret = scan_for_esp_adjust(i + 1, opcnt, &j);
3053 if (pp->is_vararg) {
3054 if (j / 4 < pp->argc_stack)
3055 ferr(po, "esp adjust is too small: %x < %x\n",
3056 j, pp->argc_stack * 4);
3057 // modify pp to make it have varargs as normal args
3059 pp->argc += j / 4 - pp->argc_stack;
3060 for (; arg < pp->argc; arg++) {
3061 pp->arg[arg].type.name = strdup("int");
3064 if (pp->argc > ARRAY_SIZE(pp->arg))
3065 ferr(po, "too many args for '%s'\n", tmpname);
3067 if (pp->argc_stack != j / 4)
3068 ferr(po, "stack tracking failed for '%s': %x %x\n",
3069 tmpname, pp->argc_stack * 4, j);
3071 ops[ret].flags |= OPF_RMD;
3072 // a bit of a hack, but deals with use of
3073 // single adj for multiple calls
3074 ops[ret].operand[1].val -= j;
3076 else if (pp->is_vararg)
3077 ferr(po, "missing esp_adjust for vararg func '%s'\n",
3080 if (!pp->is_unresolved) {
3081 // since we know the args, collect them
3082 collect_call_args(po, i, pp, ®mask, &save_arg_vars,
3086 if (strstr(pp->ret_type.name, "int64"))
3092 // - find POPs for PUSHes, rm both
3093 // - scan for all used registers
3094 // - find flag set ops for their users
3095 // - do unreselved calls
3096 // - declare indirect functions
3097 for (i = 0; i < opcnt; i++) {
3099 if (po->flags & OPF_RMD)
3102 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
3103 reg = po->operand[0].reg;
3104 if (!(regmask & (1 << reg)))
3105 // not a reg save after all, rerun scan_for_pop
3106 po->flags &= ~OPF_RSAVE;
3108 regmask_save |= 1 << reg;
3111 if (po->op == OP_PUSH
3112 && po->argnum == 0 && !(po->flags & OPF_RSAVE)
3113 && po->operand[0].type == OPT_REG)
3115 reg = po->operand[0].reg;
3117 ferr(po, "reg not set for push?\n");
3120 ret = scan_for_pop(i + 1, opcnt,
3121 po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
3124 ferr(po, "too much depth: %d\n", depth);
3126 po->flags |= OPF_RMD;
3127 scan_for_pop(i + 1, opcnt, po->operand[0].name,
3128 i + opcnt * 4, 0, &depth, 1);
3131 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
3134 if (regmask & (1 << reg)) {
3135 if (regmask_save & (1 << reg))
3136 ferr(po, "%s already saved?\n", po->operand[0].name);
3140 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg);
3145 regmask_now = po->regmask_src | po->regmask_dst;
3146 if (regmask_now & (1 << xBP)) {
3147 if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
3148 if (po->regmask_dst & (1 << xBP))
3149 // compiler decided to drop bp frame and use ebp as scratch
3150 scan_fwd_set_flags(i, opcnt, i + opcnt * 5, OPF_EBP_S);
3152 regmask_now &= ~(1 << xBP);
3156 regmask |= regmask_now;
3158 if (po->flags & OPF_CC)
3160 int setters[16], cnt = 0, branched = 0;
3162 ret = scan_for_flag_set(i, i + opcnt * 6,
3163 &branched, setters, &cnt);
3164 if (ret < 0 || cnt <= 0)
3165 ferr(po, "unable to trace flag setter(s)\n");
3166 if (cnt > ARRAY_SIZE(setters))
3167 ferr(po, "too many flag setters\n");
3169 pfo = split_cond(po, po->op, &dummy);
3170 for (j = 0; j < cnt; j++)
3172 tmp_op = &ops[setters[j]]; // flag setter
3175 // to get nicer code, we try to delay test and cmp;
3176 // if we can't because of operand modification, or if we
3177 // have math op, or branch, make it calculate flags explicitly
3178 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP) {
3179 if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
3182 else if (tmp_op->op == OP_CMPS) {
3183 pfomask = 1 << PFO_Z;
3186 // see if we'll be able to handle based on op result
3187 if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
3188 && pfo != PFO_Z && pfo != PFO_S && pfo != PFO_P)
3190 || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
3194 tmp_op->pfomask |= pfomask;
3195 cmp_result_vars |= pfomask;
3197 // note: may overwrite, currently not a problem
3201 if (po->op == OP_ADC || po->op == OP_SBB)
3202 cmp_result_vars |= 1 << PFO_C;
3204 else if (po->op == OP_MUL
3205 || (po->op == OP_IMUL && po->operand_cnt == 1))
3209 else if (po->op == OP_CALL) {
3212 ferr(po, "NULL pp\n");
3214 if (pp->is_unresolved) {
3215 int regmask_stack = 0;
3216 collect_call_args(po, i, pp, ®mask_stack, &save_arg_vars,
3219 if (!((regmask_stack & (1 << xCX))
3220 && (regmask_stack & (1 << xDX))))
3222 if (pp->argc_stack != 0
3223 || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX))))
3225 pp_insert_reg_arg(pp, "ecx");
3226 pp->is_fastcall = 1;
3227 regmask_init |= 1 << xCX;
3228 regmask |= 1 << xCX;
3230 if (pp->argc_stack != 0
3231 || ((regmask | regmask_arg) & (1 << xDX)))
3233 pp_insert_reg_arg(pp, "edx");
3234 regmask_init |= 1 << xDX;
3235 regmask |= 1 << xDX;
3238 regmask |= regmask_stack;
3240 // note: __cdecl doesn't fall into is_unresolved category
3241 if (pp->argc_stack > 0)
3245 for (arg = 0; arg < pp->argc; arg++) {
3246 if (pp->arg[arg].reg != NULL) {
3247 reg = char_array_i(regs_r32,
3248 ARRAY_SIZE(regs_r32), pp->arg[arg].reg);
3250 ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg);
3251 if (!(regmask & (1 << reg))) {
3252 regmask_init |= 1 << reg;
3253 regmask |= 1 << reg;
3259 if (pp->name[0] != 0) {
3260 memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
3261 memcpy(pp->name, "i_", 2);
3263 // might be declared already
3265 for (j = 0; j < i; j++) {
3266 if (ops[j].op == OP_CALL && (pp_tmp = ops[j].datap)) {
3267 if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) {
3277 snprintf(pp->name, sizeof(pp->name), "icall%d", i);
3279 fprintf(fout, " %s (", pp->ret_type.name);
3280 output_pp_attrs(fout, pp, 0);
3281 fprintf(fout, "*%s)(", pp->name);
3282 for (j = 0; j < pp->argc; j++) {
3284 fprintf(fout, ", ");
3285 fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1);
3287 fprintf(fout, ");\n");
3290 else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void"))
3291 regmask |= 1 << xAX;
3295 // - confirm regmask_save, it might have been reduced
3296 if (regmask_save != 0)
3299 for (i = 0; i < opcnt; i++) {
3301 if (po->flags & OPF_RMD)
3304 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE))
3305 regmask_save |= 1 << po->operand[0].reg;
3310 // output LUTs/jumptables
3311 for (i = 0; i < g_func_pd_cnt; i++) {
3313 fprintf(fout, " static const ");
3314 if (pd->type == OPT_OFFSET) {
3315 fprintf(fout, "void *jt_%s[] =\n { ", pd->label);
3317 for (j = 0; j < pd->count; j++) {
3319 fprintf(fout, ", ");
3320 fprintf(fout, "&&%s", pd->d[j].u.label);
3324 fprintf(fout, "%s %s[] =\n { ",
3325 lmod_type_u(ops, pd->lmod), pd->label);
3327 for (j = 0; j < pd->count; j++) {
3329 fprintf(fout, ", ");
3330 fprintf(fout, "%u", pd->d[j].u.val);
3333 fprintf(fout, " };\n");
3336 // declare stack frame, va_arg
3338 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
3339 (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz);
3341 if (g_func_pp->is_vararg)
3342 fprintf(fout, " va_list ap;\n");
3344 // declare arg-registers
3345 for (i = 0; i < g_func_pp->argc; i++) {
3346 if (g_func_pp->arg[i].reg != NULL) {
3347 reg = char_array_i(regs_r32,
3348 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
3349 if (regmask & (1 << reg)) {
3350 fprintf(fout, " u32 %s = (u32)a%d;\n",
3351 g_func_pp->arg[i].reg, i + 1);
3354 fprintf(fout, " // %s = a%d; // unused\n",
3355 g_func_pp->arg[i].reg, i + 1);
3360 regmask_now = regmask & ~regmask_arg;
3361 regmask_now &= ~(1 << xSP);
3363 for (reg = 0; reg < 8; reg++) {
3364 if (regmask_now & (1 << reg)) {
3365 fprintf(fout, " u32 %s", regs_r32[reg]);
3366 if (regmask_init & (1 << reg))
3367 fprintf(fout, " = 0");
3368 fprintf(fout, ";\n");
3375 for (reg = 0; reg < 8; reg++) {
3376 if (regmask_save & (1 << reg)) {
3377 fprintf(fout, " u32 s_%s;\n", regs_r32[reg]);
3383 if (save_arg_vars) {
3384 for (reg = 0; reg < 32; reg++) {
3385 if (save_arg_vars & (1 << reg)) {
3386 fprintf(fout, " u32 s_a%d;\n", reg + 1);
3392 if (cmp_result_vars) {
3393 for (i = 0; i < 8; i++) {
3394 if (cmp_result_vars & (1 << i)) {
3395 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
3402 fprintf(fout, " u64 mul_tmp;\n");
3407 fprintf(fout, "\n");
3409 if (g_func_pp->is_vararg) {
3410 if (g_func_pp->argc_stack == 0)
3411 ferr(ops, "vararg func without stack args?\n");
3412 fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc);
3416 for (i = 0; i < opcnt; i++)
3418 if (g_labels[i][0] != 0) {
3419 fprintf(fout, "\n%s:\n", g_labels[i]);
3422 delayed_flag_op = NULL;
3423 last_arith_dst = NULL;
3427 if (po->flags & OPF_RMD)
3432 #define assert_operand_cnt(n_) \
3433 if (po->operand_cnt != n_) \
3434 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
3436 // conditional/flag using op?
3437 if (po->flags & OPF_CC)
3442 pfo = split_cond(po, po->op, &is_inv);
3445 // we go through all this trouble to avoid using parsed_flag_op,
3446 // which makes generated code much nicer
3447 if (delayed_flag_op != NULL)
3449 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op, pfo, is_inv);
3452 else if (last_arith_dst != NULL
3453 && (pfo == PFO_Z || pfo == PFO_S || pfo == PFO_P
3454 || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
3457 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
3458 out_test_for_cc(buf1, sizeof(buf1), po, pfo, is_inv,
3459 last_arith_dst->lmod, buf3);
3462 else if (tmp_op != NULL) {
3463 // use preprocessed flag calc results
3464 if (!(tmp_op->pfomask & (1 << pfo)))
3465 ferr(po, "not prepared for pfo %d\n", pfo);
3467 // note: is_inv was not yet applied
3468 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
3469 is_inv ? "!" : "", parsed_flag_op_names[pfo]);
3472 ferr(po, "all methods of finding comparison failed\n");
3475 if (po->flags & OPF_JMP) {
3476 fprintf(fout, " if %s\n", buf1);
3478 else if (po->op == OP_ADC || po->op == OP_SBB) {
3480 fprintf(fout, " cond_%s = %s;\n",
3481 parsed_flag_op_names[pfo], buf1);
3483 else if (po->flags & OPF_DATA) { // SETcc
3484 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
3485 fprintf(fout, " %s = %s;", buf2, buf1);
3488 ferr(po, "unhandled conditional op\n");
3492 pfomask = po->pfomask;
3497 assert_operand_cnt(2);
3498 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3499 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3500 fprintf(fout, " %s = %s;", buf1,
3501 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3502 po->operand[0].is_ptr ? "(void *)" : "", 0));
3506 assert_operand_cnt(2);
3507 po->operand[1].lmod = OPLM_DWORD; // always
3508 fprintf(fout, " %s = %s;",
3509 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3510 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3515 assert_operand_cnt(2);
3516 fprintf(fout, " %s = %s;",
3517 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3518 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3522 assert_operand_cnt(2);
3523 switch (po->operand[1].lmod) {
3525 strcpy(buf3, "(s8)");
3528 strcpy(buf3, "(s16)");
3531 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
3533 fprintf(fout, " %s = %s;",
3534 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3535 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
3540 assert_operand_cnt(1);
3541 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3542 fprintf(fout, " %s = ~%s;", buf1, buf1);
3546 assert_operand_cnt(2);
3547 fprintf(fout, " %s = (s32)%s >> 31;",
3548 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3549 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3550 strcpy(g_comment, "cdq");
3555 assert_operand_cnt(3);
3556 if (po->flags & OPF_REP) {
3557 fprintf(fout, " for (; ecx != 0; ecx--, edi += %d)\n",
3558 lmod_bytes(po, po->operand[0].lmod));
3559 fprintf(fout, " %sedi = eax;",
3560 lmod_cast_u_ptr(po, po->operand[0].lmod));
3561 strcpy(g_comment, "rep stos");
3564 fprintf(fout, " %sedi = eax; edi += %d;",
3565 lmod_cast_u_ptr(po, po->operand[0].lmod),
3566 lmod_bytes(po, po->operand[0].lmod));
3567 strcpy(g_comment, "stos");
3573 assert_operand_cnt(3);
3574 j = lmod_bytes(po, po->operand[0].lmod);
3575 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
3576 if (po->flags & OPF_REP) {
3578 " for (; ecx != 0; ecx--, edi += %d, esi += %d)\n",
3581 " %sedi = %sesi;", buf1, buf1);
3582 strcpy(g_comment, "rep movs");
3585 fprintf(fout, " %sedi = %sesi; edi += %d; esi += %d;",
3587 strcpy(g_comment, "movs");
3593 // repe ~ repeat while ZF=1
3594 assert_operand_cnt(3);
3595 j = lmod_bytes(po, po->operand[0].lmod);
3596 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
3597 if (po->flags & OPF_REP) {
3599 " for (; ecx != 0; ecx--, edi += %d, esi += %d)\n",
3602 " if ((cond_z = (%sedi == %sesi)) %s 0)\n",
3603 buf1, buf1, (po->flags & OPF_REPZ) ? "==" : "!=");
3606 snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
3607 (po->flags & OPF_REPZ) ? "e" : "ne");
3611 " cond_z = (%sedi = %sesi); edi += %d; esi += %d;",
3613 strcpy(g_comment, "cmps");
3615 pfomask &= ~(1 << PFO_Z);
3616 last_arith_dst = NULL;
3617 delayed_flag_op = NULL;
3620 // arithmetic w/flags
3625 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3628 assert_operand_cnt(2);
3629 fprintf(fout, " %s %s= %s;",
3630 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
3632 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3633 output_std_flags(fout, po, &pfomask, buf1);
3634 last_arith_dst = &po->operand[0];
3635 delayed_flag_op = NULL;
3640 assert_operand_cnt(2);
3641 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3642 if (pfomask & (1 << PFO_C)) {
3643 if (po->operand[1].type == OPT_CONST) {
3644 l = lmod_bytes(po, po->operand[0].lmod) * 8;
3645 j = po->operand[1].val;
3648 if (po->op == OP_SHL)
3652 fprintf(fout, " cond_c = (%s & 0x%02x) ? 1 : 0;\n",
3656 ferr(po, "zero shift?\n");
3660 pfomask &= ~(1 << PFO_C);
3662 fprintf(fout, " %s %s= %s;", buf1, op_to_c(po),
3663 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3664 output_std_flags(fout, po, &pfomask, buf1);
3665 last_arith_dst = &po->operand[0];
3666 delayed_flag_op = NULL;
3670 assert_operand_cnt(2);
3671 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3672 fprintf(fout, " %s = %s%s >> %s;", buf1,
3673 lmod_cast_s(po, po->operand[0].lmod), buf1,
3674 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3675 output_std_flags(fout, po, &pfomask, buf1);
3676 last_arith_dst = &po->operand[0];
3677 delayed_flag_op = NULL;
3682 assert_operand_cnt(2);
3683 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3684 if (po->operand[1].type == OPT_CONST) {
3685 j = po->operand[1].val;
3686 j %= lmod_bytes(po, po->operand[0].lmod) * 8;
3687 fprintf(fout, po->op == OP_ROL ?
3688 " %s = (%s << %d) | (%s >> %d);" :
3689 " %s = (%s >> %d) | (%s << %d);",
3690 buf1, buf1, j, buf1,
3691 lmod_bytes(po, po->operand[0].lmod) * 8 - j);
3695 output_std_flags(fout, po, &pfomask, buf1);
3696 last_arith_dst = &po->operand[0];
3697 delayed_flag_op = NULL;
3701 assert_operand_cnt(2);
3702 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3703 if (IS(opr_name(po, 0), opr_name(po, 1))) {
3704 // special case for XOR
3705 fprintf(fout, " %s = 0;",
3706 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
3707 last_arith_dst = &po->operand[0];
3708 delayed_flag_op = NULL;
3715 assert_operand_cnt(2);
3716 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3717 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3718 if (po->op == OP_SBB
3719 && IS(po->operand[0].name, po->operand[1].name))
3721 // avoid use of unitialized var
3722 fprintf(fout, " %s = -cond_c;", buf1);
3723 // carry remains what it was
3724 pfomask &= ~(1 << PFO_C);
3727 fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
3728 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
3730 output_std_flags(fout, po, &pfomask, buf1);
3731 last_arith_dst = &po->operand[0];
3732 delayed_flag_op = NULL;
3737 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3738 if (po->operand[0].type == OPT_REG) {
3739 strcpy(buf2, po->op == OP_INC ? "++" : "--");
3740 fprintf(fout, " %s%s;", buf1, buf2);
3743 strcpy(buf2, po->op == OP_INC ? "+" : "-");
3744 fprintf(fout, " %s %s= 1;", buf1, buf2);
3746 output_std_flags(fout, po, &pfomask, buf1);
3747 last_arith_dst = &po->operand[0];
3748 delayed_flag_op = NULL;
3752 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3753 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
3754 fprintf(fout, " %s = -%s%s;", buf1,
3755 lmod_cast_s(po, po->operand[0].lmod), buf2);
3756 last_arith_dst = &po->operand[0];
3757 delayed_flag_op = NULL;
3758 if (pfomask & (1 << PFO_C)) {
3759 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
3760 pfomask &= ~(1 << PFO_C);
3765 if (po->operand_cnt == 2) {
3766 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3769 if (po->operand_cnt == 3)
3770 ferr(po, "TODO imul3\n");
3773 assert_operand_cnt(1);
3774 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
3775 fprintf(fout, " mul_tmp = %seax * %s%s;\n", buf1, buf1,
3776 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
3777 fprintf(fout, " edx = mul_tmp >> 32;\n");
3778 fprintf(fout, " eax = mul_tmp;");
3779 last_arith_dst = NULL;
3780 delayed_flag_op = NULL;
3785 assert_operand_cnt(1);
3786 if (po->operand[0].lmod != OPLM_DWORD)
3787 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
3789 // 32bit division is common, look for it
3790 if (po->op == OP_DIV)
3791 ret = scan_for_reg_clear(i, xDX);
3793 ret = scan_for_cdq_edx(i);
3795 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
3796 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
3797 po->op == OP_IDIV));
3798 fprintf(fout, " edx = %seax %% %s%s;\n", buf2, buf2, buf1);
3799 fprintf(fout, " eax = %seax / %s%s;", buf2, buf2, buf1);
3802 ferr(po, "TODO 64bit divident\n");
3803 last_arith_dst = NULL;
3804 delayed_flag_op = NULL;
3809 propagate_lmod(po, &po->operand[0], &po->operand[1]);
3811 for (j = 0; j < 8; j++) {
3812 if (pfomask & (1 << j)) {
3813 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
3814 fprintf(fout, " cond_%s = %s;",
3815 parsed_flag_op_names[j], buf1);
3822 last_arith_dst = NULL;
3823 delayed_flag_op = po;
3826 // note: we reuse OP_Jcc for SETcc, only flags differ
3827 case OP_JO ... OP_JG:
3828 if (po->flags & OPF_JMP)
3829 fprintf(fout, " goto %s;", po->operand[0].name);
3830 // else SETcc - should already be handled
3834 assert_operand_cnt(1);
3835 last_arith_dst = NULL;
3836 delayed_flag_op = NULL;
3838 if (po->operand[0].type == OPT_REGMEM) {
3839 ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
3842 ferr(po, "parse failure for jmp '%s'\n",
3843 po->operand[0].name);
3844 fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
3847 else if (po->operand[0].type != OPT_LABEL)
3848 ferr(po, "unhandled jmp type\n");
3850 fprintf(fout, " goto %s;", po->operand[0].name);
3854 assert_operand_cnt(1);
3856 my_assert_not(pp, NULL);
3859 fprintf(fout, " %s = %s;\n", pp->name,
3860 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
3864 if (strstr(pp->ret_type.name, "int64")) {
3865 if (po->flags & OPF_TAIL)
3866 ferr(po, "int64 and tail?\n");
3867 fprintf(fout, "mul_tmp = ");
3869 else if (!IS(pp->ret_type.name, "void")) {
3870 if (po->flags & OPF_TAIL) {
3871 if (!IS(g_func_pp->ret_type.name, "void")) {
3872 fprintf(fout, "return ");
3873 if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
3874 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
3877 else if (regmask & (1 << xAX)) {
3878 fprintf(fout, "eax = ");
3879 if (pp->ret_type.is_ptr)
3880 fprintf(fout, "(u32)");
3884 if (pp->name[0] == 0)
3885 ferr(po, "missing pp->name\n");
3886 fprintf(fout, "%s%s(", pp->name,
3887 pp->has_structarg ? "_sa" : "");
3889 for (arg = 0; arg < pp->argc; arg++) {
3891 fprintf(fout, ", ");
3894 if (pp->arg[arg].type.is_ptr)
3895 snprintf(cast, sizeof(cast), "(%s)", pp->arg[arg].type.name);
3897 if (pp->arg[arg].reg != NULL) {
3898 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
3903 tmp_op = pp->arg[arg].datap;
3905 ferr(po, "parsed_op missing for arg%d\n", arg);
3906 if (tmp_op->argnum != 0) {
3907 fprintf(fout, "%ss_a%d", cast, tmp_op->argnum);
3911 out_src_opr(buf1, sizeof(buf1),
3912 tmp_op, &tmp_op->operand[0], cast, 0));
3915 fprintf(fout, ");");
3917 if (strstr(pp->ret_type.name, "int64")) {
3918 fprintf(fout, "\n");
3919 fprintf(fout, " edx = mul_tmp >> 32;\n");
3920 fprintf(fout, " eax = mul_tmp;");
3923 if (pp->is_unresolved) {
3924 snprintf(buf3, sizeof(buf3), " unresoved %dreg",
3926 strcat(g_comment, buf3);
3929 if (po->flags & OPF_TAIL) {
3931 if (i == opcnt - 1 || pp->is_noreturn)
3933 else if (IS(pp->ret_type.name, "void"))
3935 else if (IS(g_func_pp->ret_type.name, "void"))
3937 // else already handled as 'return f()'
3940 if (!IS(g_func_pp->ret_type.name, "void")) {
3941 ferr(po, "int func -> void func tailcall?\n");
3944 fprintf(fout, "\n return;");
3945 strcat(g_comment, " ^ tailcall");
3949 strcat(g_comment, " tailcall");
3951 if (pp->is_noreturn)
3952 strcat(g_comment, " noreturn");
3953 delayed_flag_op = NULL;
3954 last_arith_dst = NULL;
3958 if (g_func_pp->is_vararg)
3959 fprintf(fout, " va_end(ap);\n");
3961 if (IS(g_func_pp->ret_type.name, "void")) {
3962 if (i != opcnt - 1 || label_pending)
3963 fprintf(fout, " return;");
3965 else if (g_func_pp->ret_type.is_ptr) {
3966 fprintf(fout, " return (%s)eax;",
3967 g_func_pp->ret_type.name);
3970 fprintf(fout, " return eax;");
3972 last_arith_dst = NULL;
3973 delayed_flag_op = NULL;
3977 if (po->argnum != 0) {
3978 // special case - saved func arg
3979 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
3980 fprintf(fout, " s_a%d = %s;", po->argnum, buf1);
3983 else if (po->flags & OPF_RSAVE) {
3984 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
3985 fprintf(fout, " s_%s = %s;", buf1, buf1);
3988 if (!(g_ida_func_attr & IDAFA_NORETURN))
3989 ferr(po, "stray push encountered\n");
3994 if (po->flags & OPF_RSAVE) {
3995 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
3996 fprintf(fout, " %s = s_%s;", buf1, buf1);
3999 ferr(po, "stray pop encountered\n");
4008 ferr(po, "unhandled op type %d, flags %x\n",
4013 if (g_comment[0] != 0) {
4014 char *p = g_comment;
4015 while (my_isblank(*p))
4017 fprintf(fout, " // %s", p);
4022 fprintf(fout, "\n");
4024 // some sanity checking
4025 if ((po->flags & OPF_REP) && po->op != OP_STOS
4026 && po->op != OP_MOVS && po->op != OP_CMPS)
4027 ferr(po, "unexpected rep\n");
4028 if ((po->flags & (OPF_REPZ|OPF_REPNZ)) && po->op != OP_CMPS)
4029 ferr(po, "unexpected repz/repnz\n");
4032 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
4034 // see is delayed flag stuff is still valid
4035 if (delayed_flag_op != NULL && delayed_flag_op != po) {
4036 if (is_any_opr_modified(delayed_flag_op, po, 0))
4037 delayed_flag_op = NULL;
4040 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
4041 if (is_opr_modified(last_arith_dst, po))
4042 last_arith_dst = NULL;
4048 if (g_stack_fsz && !g_stack_frame_used)
4049 fprintf(fout, " (void)sf;\n");
4051 fprintf(fout, "}\n\n");
4054 for (i = 0; i < opcnt; i++) {
4055 struct label_ref *lr, *lr_del;
4057 lr = g_label_refs[i].next;
4058 while (lr != NULL) {
4063 g_label_refs[i].i = -1;
4064 g_label_refs[i].next = NULL;
4066 if (ops[i].op == OP_CALL) {
4075 static void set_label(int i, const char *name)
4081 p = strchr(name, ':');
4085 if (len > sizeof(g_labels[0]) - 1)
4086 aerr("label '%s' too long: %d\n", name, len);
4087 if (g_labels[i][0] != 0 && !IS_START(g_labels[i], "algn_"))
4088 aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
4089 memcpy(g_labels[i], name, len);
4090 g_labels[i][len] = 0;
4093 // '=' needs special treatment..
4094 static char *next_word_s(char *w, size_t wsize, char *s)
4100 for (i = 0; i < wsize - 1; i++) {
4101 if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
4107 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
4108 printf("warning: '%s' truncated\n", w);
4119 static struct chunk_item *func_chunks;
4120 static int func_chunk_cnt;
4121 static int func_chunk_alloc;
4123 static void add_func_chunk(FILE *fasm, const char *name, int line)
4125 if (func_chunk_cnt >= func_chunk_alloc) {
4126 func_chunk_alloc *= 2;
4127 func_chunks = realloc(func_chunks,
4128 func_chunk_alloc * sizeof(func_chunks[0]));
4129 my_assert_not(func_chunks, NULL);
4131 func_chunks[func_chunk_cnt].fptr = ftell(fasm);
4132 func_chunks[func_chunk_cnt].name = strdup(name);
4133 func_chunks[func_chunk_cnt].asmln = line;
4137 static int cmp_chunks(const void *p1, const void *p2)
4139 const struct chunk_item *c1 = p1, *c2 = p2;
4140 return strcmp(c1->name, c2->name);
4143 static int cmpstringp(const void *p1, const void *p2)
4145 return strcmp(*(char * const *)p1, *(char * const *)p2);
4148 static void scan_ahead(FILE *fasm)
4158 oldpos = ftell(fasm);
4161 while (fgets(line, sizeof(line), fasm))
4172 // get rid of random tabs
4173 for (i = 0; line[i] != 0; i++)
4174 if (line[i] == '\t')
4177 if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
4180 next_word(words[0], sizeof(words[0]), p);
4181 if (words[0][0] == 0)
4182 aerr("missing name for func chunk?\n");
4184 add_func_chunk(fasm, words[0], asmln);
4189 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
4190 words[wordc][0] = 0;
4191 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
4192 if (*p == 0 || *p == ';') {
4198 if (wordc == 2 && IS(words[1], "ends"))
4202 fseek(fasm, oldpos, SEEK_SET);
4206 int main(int argc, char *argv[])
4208 FILE *fout, *fasm, *frlist;
4209 struct parsed_data *pd = NULL;
4211 char **rlist = NULL;
4213 int rlist_alloc = 0;
4214 int func_chunks_used = 0;
4215 int func_chunks_sorted = 0;
4216 int func_chunk_i = -1;
4217 long func_chunk_ret = 0;
4218 int func_chunk_ret_ln = 0;
4219 int scanned_ahead = 0;
4221 char words[20][256];
4222 enum opr_lenmod lmod;
4223 char *sctproto = NULL;
4225 int pending_endp = 0;
4227 int skip_warned = 0;
4238 for (arg = 1; arg < argc; arg++) {
4239 if (IS(argv[arg], "-v"))
4241 else if (IS(argv[arg], "-rf"))
4242 g_allow_regfunc = 1;
4247 if (argc < arg + 3) {
4248 printf("usage:\n%s [-v] [-rf] <.c> <.asm> <hdrf> [rlist]*\n",
4255 asmfn = argv[arg++];
4256 fasm = fopen(asmfn, "r");
4257 my_assert_not(fasm, NULL);
4259 hdrfn = argv[arg++];
4260 g_fhdr = fopen(hdrfn, "r");
4261 my_assert_not(g_fhdr, NULL);
4264 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
4265 my_assert_not(rlist, NULL);
4266 // needs special handling..
4267 rlist[rlist_len++] = "__alloca_probe";
4269 func_chunk_alloc = 32;
4270 func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
4271 my_assert_not(func_chunks, NULL);
4273 memset(words, 0, sizeof(words));
4275 for (; arg < argc; arg++) {
4276 frlist = fopen(argv[arg], "r");
4277 my_assert_not(frlist, NULL);
4279 while (fgets(line, sizeof(line), frlist)) {
4281 if (*p == 0 || *p == ';')
4284 if (IS_START(p, "#if 0")
4285 || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
4289 else if (IS_START(p, "#endif"))
4296 p = next_word(words[0], sizeof(words[0]), p);
4297 if (words[0][0] == 0)
4300 if (rlist_len >= rlist_alloc) {
4301 rlist_alloc = rlist_alloc * 2 + 64;
4302 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
4303 my_assert_not(rlist, NULL);
4305 rlist[rlist_len++] = strdup(words[0]);
4314 qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
4316 fout = fopen(argv[arg_out], "w");
4317 my_assert_not(fout, NULL);
4320 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
4321 my_assert_not(g_eqs, NULL);
4323 for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
4324 g_label_refs[i].i = -1;
4325 g_label_refs[i].next = NULL;
4328 while (fgets(line, sizeof(line), fasm))
4337 // get rid of random tabs
4338 for (i = 0; line[i] != 0; i++)
4339 if (line[i] == '\t')
4344 if (p[2] == '=' && IS_START(p, "; =============== S U B"))
4345 goto do_pending_endp; // eww..
4347 if (p[2] == 'A' && IS_START(p, "; Attributes:"))
4349 static const char *attrs[] = {
4358 // parse IDA's attribute-list comment
4359 g_ida_func_attr = 0;
4362 for (; *p != 0; p = sskip(p)) {
4363 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
4364 if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
4365 g_ida_func_attr |= 1 << i;
4366 p += strlen(attrs[i]);
4370 if (i == ARRAY_SIZE(attrs)) {
4371 anote("unparsed IDA attr: %s\n", p);
4374 if (IS(attrs[i], "fpd=")) {
4375 p = next_word(words[0], sizeof(words[0]), p);
4380 else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
4383 next_word(words[0], sizeof(words[0]), p);
4384 if (words[0][0] == 0)
4385 aerr("missing name for func chunk?\n");
4387 if (!scanned_ahead) {
4388 add_func_chunk(fasm, words[0], asmln);
4389 func_chunks_sorted = 0;
4392 else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
4394 if (func_chunk_i >= 0) {
4395 if (func_chunk_i < func_chunk_cnt
4396 && IS(func_chunks[func_chunk_i].name, g_func))
4398 // move on to next chunk
4399 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
4401 aerr("seek failed for '%s' chunk #%d\n",
4402 g_func, func_chunk_i);
4403 asmln = func_chunks[func_chunk_i].asmln;
4407 if (func_chunk_ret == 0)
4408 aerr("no return from chunk?\n");
4409 fseek(fasm, func_chunk_ret, SEEK_SET);
4410 asmln = func_chunk_ret_ln;
4416 else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
4417 func_chunks_used = 1;
4419 if (IS_START(g_func, "sub_")) {
4420 unsigned long addr = strtoul(p, NULL, 16);
4421 unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
4422 if (addr > f_addr && !scanned_ahead) {
4423 anote("scan_ahead caused by '%s', addr %lx\n",
4427 func_chunks_sorted = 0;
4435 for (i = wordc; i < ARRAY_SIZE(words); i++)
4437 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
4438 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
4439 if (*p == 0 || *p == ';') {
4444 if (*p != 0 && *p != ';')
4445 aerr("too many words\n");
4447 // alow asm patches in comments
4449 if (IS_START(p, "; sctpatch:")) {
4451 if (*p == 0 || *p == ';')
4453 goto parse_words; // lame
4455 if (IS_START(p, "; sctproto:")) {
4456 sctproto = strdup(p + 11);
4462 awarn("wordc == 0?\n");
4466 // don't care about this:
4467 if (words[0][0] == '.'
4468 || IS(words[0], "include")
4469 || IS(words[0], "assume") || IS(words[1], "segment")
4470 || IS(words[0], "align"))
4476 // do delayed endp processing to collect switch jumptables
4478 if (in_func && !skip_func && wordc >= 2
4479 && ((words[0][0] == 'd' && words[0][2] == 0)
4480 || (words[1][0] == 'd' && words[1][2] == 0)))
4483 if (words[1][0] == 'd' && words[1][2] == 0) {
4485 if (g_func_pd_cnt >= pd_alloc) {
4486 pd_alloc = pd_alloc * 2 + 16;
4487 g_func_pd = realloc(g_func_pd,
4488 sizeof(g_func_pd[0]) * pd_alloc);
4489 my_assert_not(g_func_pd, NULL);
4491 pd = &g_func_pd[g_func_pd_cnt];
4493 memset(pd, 0, sizeof(*pd));
4494 strcpy(pd->label, words[0]);
4495 pd->type = OPT_CONST;
4496 pd->lmod = lmod_from_directive(words[1]);
4502 anote("skipping alignment byte?\n");
4505 lmod = lmod_from_directive(words[0]);
4506 if (lmod != pd->lmod)
4507 aerr("lmod change? %d->%d\n", pd->lmod, lmod);
4510 if (pd->count_alloc < pd->count + wordc) {
4511 pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
4512 pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
4513 my_assert_not(pd->d, NULL);
4515 for (; i < wordc; i++) {
4516 if (IS(words[i], "offset")) {
4517 pd->type = OPT_OFFSET;
4520 p = strchr(words[i], ',');
4523 if (pd->type == OPT_OFFSET)
4524 pd->d[pd->count].u.label = strdup(words[i]);
4526 pd->d[pd->count].u.val = parse_number(words[i]);
4527 pd->d[pd->count].bt_i = -1;
4533 if (in_func && !skip_func)
4534 gen_func(fout, g_fhdr, g_func, pi);
4538 g_ida_func_attr = 0;
4542 func_chunks_used = 0;
4545 memset(&ops, 0, pi * sizeof(ops[0]));
4546 memset(g_labels, 0, pi * sizeof(g_labels[0]));
4550 for (i = 0; i < g_func_pd_cnt; i++) {
4552 if (pd->type == OPT_OFFSET) {
4553 for (j = 0; j < pd->count; j++)
4554 free(pd->d[j].u.label);
4565 if (IS(words[1], "proc")) {
4567 aerr("proc '%s' while in_func '%s'?\n",
4570 if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
4572 strcpy(g_func, words[0]);
4573 set_label(0, words[0]);
4578 if (IS(words[1], "endp"))
4581 aerr("endp '%s' while not in_func?\n", words[0]);
4582 if (!IS(g_func, words[0]))
4583 aerr("endp '%s' while in_func '%s'?\n",
4586 if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1
4587 && ops[0].op == OP_JMP && ops[0].operand[0].had_ds)
4593 if (!skip_func && func_chunks_used) {
4594 // start processing chunks
4595 struct chunk_item *ci, key = { g_func, 0 };
4597 func_chunk_ret = ftell(fasm);
4598 func_chunk_ret_ln = asmln;
4599 if (!func_chunks_sorted) {
4600 qsort(func_chunks, func_chunk_cnt,
4601 sizeof(func_chunks[0]), cmp_chunks);
4602 func_chunks_sorted = 1;
4604 ci = bsearch(&key, func_chunks, func_chunk_cnt,
4605 sizeof(func_chunks[0]), cmp_chunks);
4607 aerr("'%s' needs chunks, but none found\n", g_func);
4608 func_chunk_i = ci - func_chunks;
4609 for (; func_chunk_i > 0; func_chunk_i--)
4610 if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
4613 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
4615 aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
4616 asmln = func_chunks[func_chunk_i].asmln;
4624 if (wordc == 2 && IS(words[1], "ends"))
4627 p = strchr(words[0], ':');
4629 set_label(pi, words[0]);
4633 if (!in_func || skip_func) {
4634 if (!skip_warned && !skip_func && g_labels[pi][0] != 0) {
4636 anote("skipping from '%s'\n", g_labels[pi]);
4639 g_labels[pi][0] = 0;
4643 if (wordc > 1 && IS(words[1], "="))
4646 aerr("unhandled equ, wc=%d\n", wordc);
4647 if (g_eqcnt >= eq_alloc) {
4649 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
4650 my_assert_not(g_eqs, NULL);
4653 len = strlen(words[0]);
4654 if (len > sizeof(g_eqs[0].name) - 1)
4655 aerr("equ name too long: %d\n", len);
4656 strcpy(g_eqs[g_eqcnt].name, words[0]);
4658 if (!IS(words[3], "ptr"))
4659 aerr("unhandled equ\n");
4660 if (IS(words[2], "dword"))
4661 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
4662 else if (IS(words[2], "word"))
4663 g_eqs[g_eqcnt].lmod = OPLM_WORD;
4664 else if (IS(words[2], "byte"))
4665 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
4667 aerr("bad lmod: '%s'\n", words[2]);
4669 g_eqs[g_eqcnt].offset = parse_number(words[4]);
4674 if (pi >= ARRAY_SIZE(ops))
4675 aerr("too many ops\n");
4677 parse_op(&ops[pi], words, wordc);
4679 if (sctproto != NULL) {
4680 if (ops[pi].op == OP_CALL)
4681 ops[pi].datap = sctproto;
4694 // vim:ts=2:shiftwidth=2:expandtab