5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
14 #include "my_assert.h"
17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
18 #define IS(w, y) !strcmp(w, y)
19 #define IS_START(w, y) !strncmp(w, y, strlen(y))
21 #include "protoparse.h"
23 static const char *asmfn;
27 #define anote(fmt, ...) \
28 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
29 #define awarn(fmt, ...) \
30 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
31 #define aerr(fmt, ...) do { \
32 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
37 #include "masm_tools.h"
40 OPF_RMD = (1 << 0), /* removed from code generation */
41 OPF_DATA = (1 << 1), /* data processing - writes to dst opr */
42 OPF_FLAGS = (1 << 2), /* sets flags */
43 OPF_JMP = (1 << 3), /* branch, call */
44 OPF_CJMP = (1 << 4), /* cond. branch (cc or jecxz) */
45 OPF_CC = (1 << 5), /* uses flags */
46 OPF_TAIL = (1 << 6), /* ret or tail call */
47 OPF_RSAVE = (1 << 7), /* push/pop is local reg save/load */
48 OPF_REP = (1 << 8), /* prefixed by rep */
49 OPF_REPZ = (1 << 9), /* rep is repe/repz */
50 OPF_REPNZ = (1 << 10), /* rep is repne/repnz */
51 OPF_FARG = (1 << 11), /* push collected as func arg */
52 OPF_FARGNR = (1 << 12), /* push collected as func arg (no reuse) */
53 OPF_EBP_S = (1 << 13), /* ebp used as scratch here, not BP */
54 OPF_DF = (1 << 14), /* DF flag set */
55 OPF_ATAIL = (1 << 15), /* tail call with reused arg frame */
56 OPF_32BIT = (1 << 16), /* 32bit division */
57 OPF_LOCK = (1 << 17), /* op has lock prefix */
58 OPF_VAPUSH = (1 << 18), /* vararg ptr push (as call arg) */
59 OPF_DONE = (1 << 19), /* already fully handled by analysis */
60 OPF_PPUSH = (1 << 20), /* part of complex push-pop graph */
130 // must be sorted (larger len must be further in enum)
139 #define MAX_OPERANDS 3
144 enum opr_lenmod lmod;
145 unsigned int is_ptr:1; // pointer in C
146 unsigned int is_array:1; // array in C
147 unsigned int type_from_var:1; // .. in header, sometimes wrong
148 unsigned int size_mismatch:1; // type override differs from C
149 unsigned int size_lt:1; // type override is larger than C
150 unsigned int had_ds:1; // had ds: prefix
151 const struct parsed_proto *pp; // for OPT_LABEL
159 struct parsed_opr operand[MAX_OPERANDS];
162 unsigned char pfo_inv;
163 unsigned char operand_cnt;
164 unsigned char p_argnum; // arg push: altered before call arg #
165 unsigned char p_arggrp; // arg push: arg group # for above
166 unsigned char p_argpass;// arg push: arg of host func
167 short p_argnext;// arg push: same arg pushed elsewhere or -1
168 int regmask_src; // all referensed regs
170 int pfomask; // flagop: parsed_flag_op that can't be delayed
171 int cc_scratch; // scratch storage during analysis
172 int bt_i; // branch target for branches
173 struct parsed_data *btj;// branch targets for jumptables
174 struct parsed_proto *pp;// parsed_proto for OP_CALL
180 // OP_CALL - parser proto hint (str)
181 // (OPF_CC) - points to one of (OPF_FLAGS) that affects cc op
182 // OP_PUSH - points to OP_POP in complex push/pop graph
183 // OP_POP - points to OP_PUSH in simple push/pop pair
187 enum opr_lenmod lmod;
194 enum opr_lenmod lmod;
208 struct label_ref *next;
212 IDAFA_BP_FRAME = (1 << 0),
213 IDAFA_LIB_FUNC = (1 << 1),
214 IDAFA_STATIC = (1 << 2),
215 IDAFA_NORETURN = (1 << 3),
216 IDAFA_THUNK = (1 << 4),
217 IDAFA_FPD = (1 << 5),
220 // note: limited to 32k due to p_argnext
222 #define MAX_ARG_GRP 2
224 static struct parsed_op ops[MAX_OPS];
225 static struct parsed_equ *g_eqs;
227 static char *g_labels[MAX_OPS];
228 static struct label_ref g_label_refs[MAX_OPS];
229 static const struct parsed_proto *g_func_pp;
230 static struct parsed_data *g_func_pd;
231 static int g_func_pd_cnt;
232 static char g_func[256];
233 static char g_comment[256];
234 static int g_bp_frame;
235 static int g_sp_frame;
236 static int g_stack_frame_used;
237 static int g_stack_fsz;
238 static int g_ida_func_attr;
239 static int g_skip_func;
240 static int g_allow_regfunc;
241 static int g_quiet_pp;
242 static int g_header_mode;
244 #define ferr(op_, fmt, ...) do { \
245 printf("%s:%d: error: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
246 dump_op(op_), ##__VA_ARGS__); \
250 #define fnote(op_, fmt, ...) \
251 printf("%s:%d: note: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
252 dump_op(op_), ##__VA_ARGS__)
254 #define ferr_assert(op_, cond) do { \
255 if (!(cond)) ferr(op_, "assertion '%s' failed on ln :%d\n", #cond, \
259 const char *regs_r32[] = {
260 "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
261 // not r32, but list here for easy parsing and printing
262 "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
264 const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
265 const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
266 const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
268 enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
270 // possible basic comparison types (without inversion)
271 enum parsed_flag_op {
275 PFO_BE, // 6 CF=1||ZF=1
279 PFO_LE, // e ZF=1||SF!=OF
282 #define PFOB_O (1 << PFO_O)
283 #define PFOB_C (1 << PFO_C)
284 #define PFOB_Z (1 << PFO_Z)
285 #define PFOB_S (1 << PFO_S)
287 static const char *parsed_flag_op_names[] = {
288 "o", "c", "z", "be", "s", "p", "l", "le"
291 static int char_array_i(const char *array[], size_t len, const char *s)
295 for (i = 0; i < len; i++)
302 static void printf_number(char *buf, size_t buf_size,
303 unsigned long number)
305 // output in C-friendly form
306 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
309 static int check_segment_prefix(const char *s)
311 if (s[0] == 0 || s[1] != 's' || s[2] != ':')
325 static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
329 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
331 *reg_lmod = OPLM_QWORD;
335 *reg_lmod = OPLM_DWORD;
338 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
340 *reg_lmod = OPLM_WORD;
343 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
345 *reg_lmod = OPLM_BYTE;
348 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
350 *reg_lmod = OPLM_BYTE;
357 static int parse_indmode(char *name, int *regmask, int need_c_cvt)
359 enum opr_lenmod lmod;
372 while (my_isblank(*s))
374 for (; my_issep(*s); d++, s++)
376 while (my_isblank(*s))
380 // skip '?s:' prefixes
381 if (check_segment_prefix(s))
384 s = next_idt(w, sizeof(w), s);
389 reg = parse_reg(&lmod, w);
391 *regmask |= 1 << reg;
395 if ('0' <= w[0] && w[0] <= '9') {
396 number = parse_number(w);
397 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
401 // probably some label/identifier - pass
404 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
408 strcpy(name, cvtbuf);
413 static int is_reg_in_str(const char *s)
417 if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3])))
420 for (i = 0; i < ARRAY_SIZE(regs_r32); i++)
421 if (!strncmp(s, regs_r32[i], 3))
427 static const char *parse_stack_el(const char *name, char *extra_reg,
430 const char *p, *p2, *s;
436 if (g_bp_frame || early_try)
439 if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) {
441 if (extra_reg != NULL) {
442 strncpy(extra_reg, name, 3);
447 if (IS_START(p, "ebp+")) {
451 if (p2 != NULL && is_reg_in_str(p)) {
452 if (extra_reg != NULL) {
453 strncpy(extra_reg, p, p2 - p);
454 extra_reg[p2 - p] = 0;
459 if (!('0' <= *p && *p <= '9'))
466 if (!IS_START(name, "esp+"))
472 if (is_reg_in_str(s)) {
473 if (extra_reg != NULL) {
474 strncpy(extra_reg, s, p - s);
475 extra_reg[p - s] = 0;
480 aerr("%s IDA stackvar not set?\n", __func__);
482 if (!('0' <= *s && *s <= '9')) {
483 aerr("%s IDA stackvar offset not set?\n", __func__);
486 if (s[0] == '0' && s[1] == 'x')
489 if (len < sizeof(buf) - 1) {
490 strncpy(buf, s, len);
492 val = strtol(buf, &endp, 16);
493 if (val == 0 || *endp != 0) {
494 aerr("%s num parse fail for '%s'\n", __func__, buf);
503 if ('0' <= *p && *p <= '9')
509 static int guess_lmod_from_name(struct parsed_opr *opr)
511 if (!strncmp(opr->name, "dword_", 6)) {
512 opr->lmod = OPLM_DWORD;
515 if (!strncmp(opr->name, "word_", 5)) {
516 opr->lmod = OPLM_WORD;
519 if (!strncmp(opr->name, "byte_", 5)) {
520 opr->lmod = OPLM_BYTE;
523 if (!strncmp(opr->name, "qword_", 6)) {
524 opr->lmod = OPLM_QWORD;
530 static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
531 const struct parsed_type *c_type)
533 static const char *dword_types[] = {
534 "uint32_t", "int", "_DWORD", "UINT_PTR", "DWORD",
535 "WPARAM", "LPARAM", "UINT", "__int32",
536 "LONG", "HIMC", "BOOL", "size_t",
539 static const char *word_types[] = {
540 "uint16_t", "int16_t", "_WORD", "WORD",
541 "unsigned __int16", "__int16",
543 static const char *byte_types[] = {
544 "uint8_t", "int8_t", "char",
545 "unsigned __int8", "__int8", "BYTE", "_BYTE",
547 // structures.. deal the same as with _UNKNOWN for now
553 if (c_type->is_ptr) {
558 n = skip_type_mod(c_type->name);
560 for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
561 if (IS(n, dword_types[i])) {
567 for (i = 0; i < ARRAY_SIZE(word_types); i++) {
568 if (IS(n, word_types[i])) {
574 for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
575 if (IS(n, byte_types[i])) {
584 static char *default_cast_to(char *buf, size_t buf_size,
585 struct parsed_opr *opr)
591 if (opr->pp == NULL || opr->pp->type.name == NULL
594 snprintf(buf, buf_size, "%s", "(void *)");
598 snprintf(buf, buf_size, "(%s)", opr->pp->type.name);
602 static enum opr_type lmod_from_directive(const char *d)
606 else if (IS(d, "dw"))
608 else if (IS(d, "db"))
611 aerr("unhandled directive: '%s'\n", d);
615 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
621 *regmask |= 1 << reg;
624 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
627 static int parse_operand(struct parsed_opr *opr,
628 int *regmask, int *regmask_indirect,
629 char words[16][256], int wordc, int w, unsigned int op_flags)
631 const struct parsed_proto *pp = NULL;
632 enum opr_lenmod tmplmod;
633 unsigned long number;
641 aerr("parse_operand w %d, wordc %d\n", w, wordc);
645 for (i = w; i < wordc; i++) {
646 len = strlen(words[i]);
647 if (words[i][len - 1] == ',') {
648 words[i][len - 1] = 0;
654 wordc_in = wordc - w;
656 if ((op_flags & OPF_JMP) && wordc_in > 0
657 && !('0' <= words[w][0] && words[w][0] <= '9'))
659 const char *label = NULL;
661 if (wordc_in == 3 && !strncmp(words[w], "near", 4)
662 && IS(words[w + 1], "ptr"))
663 label = words[w + 2];
664 else if (wordc_in == 2 && IS(words[w], "short"))
665 label = words[w + 1];
666 else if (wordc_in == 1
667 && strchr(words[w], '[') == NULL
668 && parse_reg(&tmplmod, words[w]) < 0)
672 opr->type = OPT_LABEL;
673 ret = check_segment_prefix(label);
676 aerr("fs/gs used\n");
680 strcpy(opr->name, label);
686 if (IS(words[w + 1], "ptr")) {
687 if (IS(words[w], "dword"))
688 opr->lmod = OPLM_DWORD;
689 else if (IS(words[w], "word"))
690 opr->lmod = OPLM_WORD;
691 else if (IS(words[w], "byte"))
692 opr->lmod = OPLM_BYTE;
693 else if (IS(words[w], "qword"))
694 opr->lmod = OPLM_QWORD;
696 aerr("type parsing failed\n");
698 wordc_in = wordc - w;
703 if (IS(words[w], "offset")) {
704 opr->type = OPT_OFFSET;
705 opr->lmod = OPLM_DWORD;
706 strcpy(opr->name, words[w + 1]);
707 pp = proto_parse(g_fhdr, opr->name, 1);
710 if (IS(words[w], "(offset")) {
711 p = strchr(words[w + 1], ')');
713 aerr("parse of bracketed offset failed\n");
715 opr->type = OPT_OFFSET;
716 strcpy(opr->name, words[w + 1]);
722 aerr("parse_operand 1 word expected\n");
724 ret = check_segment_prefix(words[w]);
727 aerr("fs/gs used\n");
729 memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
731 strcpy(opr->name, words[w]);
733 if (words[w][0] == '[') {
734 opr->type = OPT_REGMEM;
735 ret = sscanf(words[w], "[%[^]]]", opr->name);
737 aerr("[] parse failure\n");
739 parse_indmode(opr->name, regmask_indirect, 1);
740 if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL, 1))
743 struct parsed_equ *eq =
744 equ_find(NULL, parse_stack_el(opr->name, NULL, 1), &i);
746 opr->lmod = eq->lmod;
750 else if (strchr(words[w], '[')) {
752 p = strchr(words[w], '[');
753 opr->type = OPT_REGMEM;
754 parse_indmode(p, regmask_indirect, 0);
755 strncpy(buf, words[w], p - words[w]);
756 buf[p - words[w]] = 0;
757 pp = proto_parse(g_fhdr, buf, 1);
760 else if (('0' <= words[w][0] && words[w][0] <= '9')
761 || words[w][0] == '-')
763 number = parse_number(words[w]);
764 opr->type = OPT_CONST;
766 printf_number(opr->name, sizeof(opr->name), number);
770 ret = parse_reg(&tmplmod, opr->name);
772 setup_reg_opr(opr, ret, tmplmod, regmask);
776 // most likely var in data segment
777 opr->type = OPT_LABEL;
778 pp = proto_parse(g_fhdr, opr->name, g_quiet_pp);
782 if (pp->is_fptr || pp->is_func) {
783 opr->lmod = OPLM_DWORD;
787 tmplmod = OPLM_UNSPEC;
788 if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
789 anote("unhandled C type '%s' for '%s'\n",
790 pp->type.name, opr->name);
792 if (opr->lmod == OPLM_UNSPEC) {
794 opr->type_from_var = 1;
796 else if (opr->lmod != tmplmod) {
797 opr->size_mismatch = 1;
798 if (tmplmod < opr->lmod)
801 opr->is_ptr = pp->type.is_ptr;
803 opr->is_array = pp->type.is_array;
807 if (opr->lmod == OPLM_UNSPEC)
808 guess_lmod_from_name(opr);
812 static const struct {
817 { "repe", OPF_REP|OPF_REPZ },
818 { "repz", OPF_REP|OPF_REPZ },
819 { "repne", OPF_REP|OPF_REPNZ },
820 { "repnz", OPF_REP|OPF_REPNZ },
821 { "lock", OPF_LOCK }, // ignored for now..
824 #define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC)
826 static const struct {
829 unsigned short minopr;
830 unsigned short maxopr;
833 unsigned char pfo_inv;
835 { "nop", OP_NOP, 0, 0, 0 },
836 { "push", OP_PUSH, 1, 1, 0 },
837 { "pop", OP_POP, 1, 1, OPF_DATA },
838 { "leave",OP_LEAVE, 0, 0, OPF_DATA },
839 { "mov" , OP_MOV, 2, 2, OPF_DATA },
840 { "lea", OP_LEA, 2, 2, OPF_DATA },
841 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
842 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
843 { "xchg", OP_XCHG, 2, 2, OPF_DATA },
844 { "not", OP_NOT, 1, 1, OPF_DATA },
845 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
846 { "lodsb",OP_LODS, 0, 0, OPF_DATA },
847 { "lodsw",OP_LODS, 0, 0, OPF_DATA },
848 { "lodsd",OP_LODS, 0, 0, OPF_DATA },
849 { "stosb",OP_STOS, 0, 0, OPF_DATA },
850 { "stosw",OP_STOS, 0, 0, OPF_DATA },
851 { "stosd",OP_STOS, 0, 0, OPF_DATA },
852 { "movsb",OP_MOVS, 0, 0, OPF_DATA },
853 { "movsw",OP_MOVS, 0, 0, OPF_DATA },
854 { "movsd",OP_MOVS, 0, 0, OPF_DATA },
855 { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
856 { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
857 { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
858 { "scasb",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
859 { "scasw",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
860 { "scasd",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
861 { "std", OP_STD, 0, 0, OPF_DATA }, // special flag
862 { "cld", OP_CLD, 0, 0, OPF_DATA },
863 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
864 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
865 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
866 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
867 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
868 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
869 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
870 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
871 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
872 { "shrd", OP_SHRD, 3, 3, OPF_DATA|OPF_FLAGS },
873 { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS },
874 { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS },
875 { "rcl", OP_RCL, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
876 { "rcr", OP_RCR, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
877 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
878 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
879 { "bsf", OP_BSF, 2, 2, OPF_DATA|OPF_FLAGS },
880 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
881 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
882 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
883 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
884 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
885 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
886 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
887 { "test", OP_TEST, 2, 2, OPF_FLAGS },
888 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
889 { "retn", OP_RET, 0, 1, OPF_TAIL },
890 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
891 { "jmp", OP_JMP, 1, 1, OPF_JMP },
892 { "jecxz",OP_JECXZ, 1, 1, OPF_JMP|OPF_CJMP },
893 { "jo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 0 }, // 70 OF=1
894 { "jno", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 1 }, // 71 OF=0
895 { "jc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 CF=1
896 { "jb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72
897 { "jnc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 CF=0
898 { "jnb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
899 { "jae", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
900 { "jz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74 ZF=1
901 { "je", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74
902 { "jnz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75 ZF=0
903 { "jne", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75
904 { "jbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 CF=1||ZF=1
905 { "jna", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76
906 { "ja", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 CF=0&&ZF=0
907 { "jnbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77
908 { "js", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 0 }, // 78 SF=1
909 { "jns", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 1 }, // 79 SF=0
910 { "jp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a PF=1
911 { "jpe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a
912 { "jnp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b PF=0
913 { "jpo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b
914 { "jl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c SF!=OF
915 { "jnge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c
916 { "jge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d SF=OF
917 { "jnl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d
918 { "jle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e ZF=1||SF!=OF
919 { "jng", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e
920 { "jg", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f ZF=0&&SF=OF
921 { "jnle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f
922 { "seto", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 0 },
923 { "setno", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 1 },
924 { "setc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
925 { "setb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
926 { "setnc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
927 { "setae", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
928 { "setnb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
929 { "setz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
930 { "sete", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
931 { "setnz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
932 { "setne", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
933 { "setbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
934 { "setna", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
935 { "seta", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
936 { "setnbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
937 { "sets", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 0 },
938 { "setns", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 1 },
939 { "setp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
940 { "setpe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
941 { "setnp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
942 { "setpo", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
943 { "setl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
944 { "setnge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
945 { "setge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
946 { "setnl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
947 { "setle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
948 { "setng", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
949 { "setg", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
950 { "setnle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
953 { "emms", OP_EMMS, 0, 0, OPF_DATA },
954 { "movq", OP_MOV, 2, 2, OPF_DATA },
959 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
961 enum opr_lenmod lmod = OPLM_UNSPEC;
962 int prefix_flags = 0;
970 for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
971 if (IS(words[w], pref_table[i].name)) {
972 prefix_flags = pref_table[i].flags;
979 aerr("lone prefix: '%s'\n", words[0]);
984 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
985 if (IS(words[w], op_table[i].name))
989 if (i == ARRAY_SIZE(op_table)) {
991 aerr("unhandled op: '%s'\n", words[0]);
996 op->op = op_table[i].op;
997 op->flags = op_table[i].flags | prefix_flags;
998 op->pfo = op_table[i].pfo;
999 op->pfo_inv = op_table[i].pfo_inv;
1000 op->regmask_src = op->regmask_dst = 0;
1003 if (op->op == OP_UD2)
1006 for (opr = 0; opr < op_table[i].maxopr; opr++) {
1007 if (opr >= op_table[i].minopr && w >= wordc)
1010 regmask = regmask_ind = 0;
1011 w = parse_operand(&op->operand[opr], ®mask, ®mask_ind,
1012 words, wordc, w, op->flags);
1014 if (opr == 0 && (op->flags & OPF_DATA))
1015 op->regmask_dst = regmask;
1017 op->regmask_src |= regmask;
1018 op->regmask_src |= regmask_ind;
1022 aerr("parse_op %s incomplete: %d/%d\n",
1023 words[0], w, wordc);
1026 op->operand_cnt = opr;
1027 if (!strncmp(op_table[i].name, "set", 3))
1028 op->operand[0].lmod = OPLM_BYTE;
1031 // first operand is not dst
1034 op->regmask_src |= op->regmask_dst;
1035 op->regmask_dst = 0;
1038 // first operand is src too
1050 op->regmask_src |= op->regmask_dst;
1055 op->regmask_src |= op->regmask_dst;
1056 op->regmask_dst |= op->regmask_src;
1062 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1063 && op->operand[0].lmod == op->operand[1].lmod
1064 && op->operand[0].reg == op->operand[1].reg
1065 && IS(op->operand[0].name, op->operand[1].name)) // ! ah, al..
1067 op->regmask_src = 0;
1070 op->regmask_src |= op->regmask_dst;
1073 // ops with implicit argumets
1075 op->operand_cnt = 2;
1076 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
1077 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
1083 if (op->operand_cnt != 0)
1085 if (words[op_w][4] == 'b')
1087 else if (words[op_w][4] == 'w')
1089 else if (words[op_w][4] == 'd')
1091 op->operand_cnt = 3;
1092 setup_reg_opr(&op->operand[0], op->op == OP_LODS ? xSI : xDI,
1093 lmod, &op->regmask_src);
1094 setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src);
1095 op->regmask_dst = op->regmask_src;
1096 setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD,
1097 op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src);
1102 if (op->operand_cnt != 0)
1104 if (words[op_w][4] == 'b')
1106 else if (words[op_w][4] == 'w')
1108 else if (words[op_w][4] == 'd')
1110 op->operand_cnt = 3;
1111 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
1112 setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src);
1113 setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src);
1114 op->regmask_dst = op->regmask_src;
1118 op->operand_cnt = 1;
1119 op->regmask_src = 1 << xCX;
1120 op->operand[0].type = OPT_REG;
1121 op->operand[0].reg = xCX;
1122 op->operand[0].lmod = OPLM_DWORD;
1126 if (op->operand_cnt != 1)
1131 op->regmask_src |= op->regmask_dst;
1132 op->regmask_dst = (1 << xDX) | (1 << xAX);
1133 if (op->operand[0].lmod == OPLM_UNSPEC)
1134 op->operand[0].lmod = OPLM_DWORD;
1139 // we could set up operands for edx:eax, but there is no real need to
1140 // (see is_opr_modified())
1141 op->regmask_src |= op->regmask_dst;
1142 op->regmask_dst = (1 << xDX) | (1 << xAX);
1143 if (op->operand[0].lmod == OPLM_UNSPEC)
1144 op->operand[0].lmod = OPLM_DWORD;
1152 op->regmask_src |= op->regmask_dst;
1153 if (op->operand[1].lmod == OPLM_UNSPEC)
1154 op->operand[1].lmod = OPLM_BYTE;
1158 op->regmask_src |= op->regmask_dst;
1159 if (op->operand[2].lmod == OPLM_UNSPEC)
1160 op->operand[2].lmod = OPLM_BYTE;
1164 op->regmask_src |= op->regmask_dst;
1165 op->regmask_dst = 0;
1166 if (op->operand[0].lmod == OPLM_UNSPEC
1167 && (op->operand[0].type == OPT_CONST
1168 || op->operand[0].type == OPT_OFFSET
1169 || op->operand[0].type == OPT_LABEL))
1170 op->operand[0].lmod = OPLM_DWORD;
1176 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1177 && op->operand[0].lmod == op->operand[1].lmod
1178 && op->operand[0].reg == op->operand[1].reg
1179 && IS(op->operand[0].name, op->operand[1].name)) // ! ah, al..
1181 op->flags |= OPF_RMD | OPF_DONE;
1182 op->regmask_src = op->regmask_dst = 0;
1187 if (op->operand[0].type == OPT_REG
1188 && op->operand[1].type == OPT_REGMEM)
1191 snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
1192 if (IS(buf, op->operand[1].name))
1193 op->flags |= OPF_RMD | OPF_DONE;
1198 // trashed regs must be explicitly detected later
1199 op->regmask_dst = 0;
1203 op->regmask_dst = (1 << xBP) | (1 << xSP);
1204 op->regmask_src = 1 << xBP;
1212 static const char *op_name(struct parsed_op *po)
1214 static char buf[16];
1218 if (po->op == OP_JCC || po->op == OP_SCC) {
1220 *p++ = (po->op == OP_JCC) ? 'j' : 's';
1223 strcpy(p, parsed_flag_op_names[po->pfo]);
1227 for (i = 0; i < ARRAY_SIZE(op_table); i++)
1228 if (op_table[i].op == po->op)
1229 return op_table[i].name;
1235 static const char *dump_op(struct parsed_op *po)
1237 static char out[128];
1244 snprintf(out, sizeof(out), "%s", op_name(po));
1245 for (i = 0; i < po->operand_cnt; i++) {
1249 snprintf(p, sizeof(out) - (p - out),
1250 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1251 po->operand[i].name);
1257 static const char *lmod_type_u(struct parsed_op *po,
1258 enum opr_lenmod lmod)
1270 ferr(po, "invalid lmod: %d\n", lmod);
1271 return "(_invalid_)";
1275 static const char *lmod_cast_u(struct parsed_op *po,
1276 enum opr_lenmod lmod)
1288 ferr(po, "invalid lmod: %d\n", lmod);
1289 return "(_invalid_)";
1293 static const char *lmod_cast_u_ptr(struct parsed_op *po,
1294 enum opr_lenmod lmod)
1306 ferr(po, "invalid lmod: %d\n", lmod);
1307 return "(_invalid_)";
1311 static const char *lmod_cast_s(struct parsed_op *po,
1312 enum opr_lenmod lmod)
1324 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1325 return "(_invalid_)";
1329 static const char *lmod_cast(struct parsed_op *po,
1330 enum opr_lenmod lmod, int is_signed)
1333 lmod_cast_s(po, lmod) :
1334 lmod_cast_u(po, lmod);
1337 static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1349 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1354 static const char *opr_name(struct parsed_op *po, int opr_num)
1356 if (opr_num >= po->operand_cnt)
1357 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1358 return po->operand[opr_num].name;
1361 static unsigned int opr_const(struct parsed_op *po, int opr_num)
1363 if (opr_num >= po->operand_cnt)
1364 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1365 if (po->operand[opr_num].type != OPT_CONST)
1366 ferr(po, "opr %d: const expected\n", opr_num);
1367 return po->operand[opr_num].val;
1370 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1372 if ((unsigned int)popr->reg >= ARRAY_SIZE(regs_r32))
1373 ferr(po, "invalid reg: %d\n", popr->reg);
1374 return regs_r32[popr->reg];
1377 // cast1 is the "final" cast
1378 static const char *simplify_cast(const char *cast1, const char *cast2)
1380 static char buf[256];
1386 if (IS(cast1, cast2))
1388 if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
1390 if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
1392 if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
1394 if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
1396 if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1399 snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1403 static const char *simplify_cast_num(const char *cast, unsigned int val)
1405 if (IS(cast, "(u8)") && val < 0x100)
1407 if (IS(cast, "(s8)") && val < 0x80)
1409 if (IS(cast, "(u16)") && val < 0x10000)
1411 if (IS(cast, "(s16)") && val < 0x8000)
1413 if (IS(cast, "(s32)") && val < 0x80000000)
1419 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1428 namelen = strlen(name);
1430 p = strchr(name, '+');
1434 ferr(po, "equ parse failed for '%s'\n", name);
1436 if (IS_START(p, "0x"))
1438 *extra_offs = strtol(p, &endp, 16);
1440 ferr(po, "equ parse failed for '%s'\n", name);
1443 for (i = 0; i < g_eqcnt; i++)
1444 if (strncmp(g_eqs[i].name, name, namelen) == 0
1445 && g_eqs[i].name[namelen] == 0)
1449 ferr(po, "unresolved equ name: '%s'\n", name);
1456 static int is_stack_access(struct parsed_op *po,
1457 const struct parsed_opr *popr)
1459 return (parse_stack_el(popr->name, NULL, 0)
1460 || (g_bp_frame && !(po->flags & OPF_EBP_S)
1461 && IS_START(popr->name, "ebp")));
1464 static void parse_stack_access(struct parsed_op *po,
1465 const char *name, char *ofs_reg, int *offset_out,
1466 int *stack_ra_out, const char **bp_arg_out, int is_lea)
1468 const char *bp_arg = "";
1469 const char *p = NULL;
1470 struct parsed_equ *eq;
1477 if (IS_START(name, "ebp-")
1478 || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1481 if (IS_START(p, "0x"))
1483 offset = strtoul(p, &endp, 16);
1487 ferr(po, "ebp- parse of '%s' failed\n", name);
1490 bp_arg = parse_stack_el(name, ofs_reg, 0);
1491 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1492 eq = equ_find(po, bp_arg, &offset);
1494 ferr(po, "detected but missing eq\n");
1495 offset += eq->offset;
1498 if (!strncmp(name, "ebp", 3))
1501 // yes it sometimes LEAs ra for compares..
1502 if (!is_lea && ofs_reg[0] == 0
1503 && stack_ra <= offset && offset < stack_ra + 4)
1505 ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
1508 *offset_out = offset;
1509 *stack_ra_out = stack_ra;
1511 *bp_arg_out = bp_arg;
1514 static int stack_frame_access(struct parsed_op *po,
1515 struct parsed_opr *popr, char *buf, size_t buf_size,
1516 const char *name, const char *cast, int is_src, int is_lea)
1518 enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1519 const char *prefix = "";
1520 const char *bp_arg = NULL;
1521 char ofs_reg[16] = { 0, };
1522 int i, arg_i, arg_s;
1530 if (po->flags & OPF_EBP_S)
1531 ferr(po, "stack_frame_access while ebp is scratch\n");
1533 parse_stack_access(po, name, ofs_reg, &offset,
1534 &stack_ra, &bp_arg, is_lea);
1536 if (offset > stack_ra)
1538 arg_i = (offset - stack_ra - 4) / 4;
1539 if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
1541 if (g_func_pp->is_vararg
1542 && arg_i == g_func_pp->argc_stack && is_lea)
1544 // should be va_list
1547 snprintf(buf, buf_size, "%sap", cast);
1550 ferr(po, "offset %d (%s,%d) doesn't map to any arg\n",
1551 offset, bp_arg, arg_i);
1553 if (ofs_reg[0] != 0)
1554 ferr(po, "offset reg on arg access?\n");
1556 for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1557 if (g_func_pp->arg[i].reg != NULL)
1563 if (i == g_func_pp->argc)
1564 ferr(po, "arg %d not in prototype?\n", arg_i);
1566 popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
1573 ferr(po, "lea/byte to arg?\n");
1574 if (is_src && (offset & 3) == 0)
1575 snprintf(buf, buf_size, "%sa%d",
1576 simplify_cast(cast, "(u8)"), i + 1);
1578 snprintf(buf, buf_size, "%sBYTE%d(a%d)",
1579 cast, offset & 3, i + 1);
1584 ferr(po, "lea/word to arg?\n");
1589 ferr(po, "problematic arg store\n");
1590 snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
1591 simplify_cast(cast, "*(u16 *)"), i + 1);
1594 ferr(po, "unaligned arg word load\n");
1596 else if (is_src && (offset & 2) == 0)
1597 snprintf(buf, buf_size, "%sa%d",
1598 simplify_cast(cast, "(u16)"), i + 1);
1600 snprintf(buf, buf_size, "%s%sWORD(a%d)",
1601 cast, (offset & 2) ? "HI" : "LO", i + 1);
1613 snprintf(buf, buf_size, "(u32)&a%d + %d",
1616 ferr(po, "unaligned arg store\n");
1618 // mov edx, [ebp+arg_4+2]; movsx ecx, dx
1619 snprintf(buf, buf_size, "%s(a%d >> %d)",
1620 prefix, i + 1, (offset & 3) * 8);
1624 snprintf(buf, buf_size, "%s%sa%d",
1625 prefix, is_lea ? "&" : "", i + 1);
1630 ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
1634 snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
1637 guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
1638 if (tmp_lmod != OPLM_DWORD
1639 && (unaligned || (!is_src && lmod_bytes(po, tmp_lmod)
1640 < lmod_bytes(po, popr->lmod) + (offset & 3))))
1642 ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
1643 i + 1, offset, g_func_pp->arg[i].type.name);
1645 // can't check this because msvc likes to reuse
1646 // arg space for scratch..
1647 //if (popr->is_ptr && popr->lmod != OPLM_DWORD)
1648 // ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
1652 if (g_stack_fsz == 0)
1653 ferr(po, "stack var access without stackframe\n");
1654 g_stack_frame_used = 1;
1656 sf_ofs = g_stack_fsz + offset;
1657 lim = (ofs_reg[0] != 0) ? -4 : 0;
1658 if (offset > 0 || sf_ofs < lim)
1659 ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
1669 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1670 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1674 if ((sf_ofs & 1) || ofs_reg[0] != 0) {
1675 // known unaligned or possibly unaligned
1676 strcat(g_comment, " unaligned");
1678 prefix = "*(u16 *)&";
1679 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1680 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1683 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
1687 if ((sf_ofs & 3) || ofs_reg[0] != 0) {
1688 // known unaligned or possibly unaligned
1689 strcat(g_comment, " unaligned");
1691 prefix = "*(u32 *)&";
1692 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1693 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1696 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
1700 ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
1707 static void check_func_pp(struct parsed_op *po,
1708 const struct parsed_proto *pp, const char *pfx)
1710 enum opr_lenmod tmp_lmod;
1714 if (pp->argc_reg != 0) {
1715 if (/*!g_allow_regfunc &&*/ !pp->is_fastcall) {
1716 pp_print(buf, sizeof(buf), pp);
1717 ferr(po, "%s: unexpected reg arg in icall: %s\n", pfx, buf);
1719 if (pp->argc_stack > 0 && pp->argc_reg != 2)
1720 ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
1721 pfx, pp->argc_reg, pp->argc_stack);
1724 // fptrs must use 32bit args, callsite might have no information and
1725 // lack a cast to smaller types, which results in incorrectly masked
1726 // args passed (callee may assume masked args, it does on ARM)
1727 if (!pp->is_osinc) {
1728 for (i = 0; i < pp->argc; i++) {
1729 ret = guess_lmod_from_c_type(&tmp_lmod, &pp->arg[i].type);
1730 if (ret && tmp_lmod != OPLM_DWORD)
1731 ferr(po, "reference to %s with arg%d '%s'\n", pp->name,
1732 i + 1, pp->arg[i].type.name);
1737 static const char *check_label_read_ref(struct parsed_op *po,
1740 const struct parsed_proto *pp;
1742 pp = proto_parse(g_fhdr, name, 0);
1744 ferr(po, "proto_parse failed for ref '%s'\n", name);
1747 check_func_pp(po, pp, "ref");
1752 static char *out_src_opr(char *buf, size_t buf_size,
1753 struct parsed_op *po, struct parsed_opr *popr, const char *cast,
1756 char tmp1[256], tmp2[256];
1765 switch (popr->type) {
1768 ferr(po, "lea from reg?\n");
1770 switch (popr->lmod) {
1772 snprintf(buf, buf_size, "%s%s.q", cast, opr_reg_p(po, popr));
1775 snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
1778 snprintf(buf, buf_size, "%s%s",
1779 simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
1782 if (popr->name[1] == 'h') // XXX..
1783 snprintf(buf, buf_size, "%s(%s >> 8)",
1784 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1786 snprintf(buf, buf_size, "%s%s",
1787 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1790 ferr(po, "invalid src lmod: %d\n", popr->lmod);
1795 if (is_stack_access(po, popr)) {
1796 stack_frame_access(po, popr, buf, buf_size,
1797 popr->name, cast, 1, is_lea);
1801 strcpy(expr, popr->name);
1802 if (strchr(expr, '[')) {
1803 // special case: '[' can only be left for label[reg] form
1804 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
1806 ferr(po, "parse failure for '%s'\n", expr);
1807 if (tmp1[0] == '(') {
1808 // (off_4FFF50+3)[eax]
1809 p = strchr(tmp1 + 1, ')');
1810 if (p == NULL || p[1] != 0)
1811 ferr(po, "parse failure (2) for '%s'\n", expr);
1813 memmove(tmp1, tmp1 + 1, strlen(tmp1));
1815 snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
1818 // XXX: do we need more parsing?
1820 snprintf(buf, buf_size, "%s", expr);
1824 snprintf(buf, buf_size, "%s(%s)",
1825 simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
1829 name = check_label_read_ref(po, popr->name);
1830 if (cast[0] == 0 && popr->is_ptr)
1834 snprintf(buf, buf_size, "(u32)&%s", name);
1835 else if (popr->size_lt)
1836 snprintf(buf, buf_size, "%s%s%s%s", cast,
1837 lmod_cast_u_ptr(po, popr->lmod),
1838 popr->is_array ? "" : "&", name);
1840 snprintf(buf, buf_size, "%s%s%s", cast, name,
1841 popr->is_array ? "[0]" : "");
1845 name = check_label_read_ref(po, popr->name);
1849 ferr(po, "lea an offset?\n");
1850 snprintf(buf, buf_size, "%s&%s", cast, name);
1855 ferr(po, "lea from const?\n");
1857 printf_number(tmp1, sizeof(tmp1), popr->val);
1858 if (popr->val == 0 && strchr(cast, '*'))
1859 snprintf(buf, buf_size, "NULL");
1861 snprintf(buf, buf_size, "%s%s",
1862 simplify_cast_num(cast, popr->val), tmp1);
1866 ferr(po, "invalid src type: %d\n", popr->type);
1872 // note: may set is_ptr (we find that out late for ebp frame..)
1873 static char *out_dst_opr(char *buf, size_t buf_size,
1874 struct parsed_op *po, struct parsed_opr *popr)
1876 switch (popr->type) {
1878 switch (popr->lmod) {
1880 snprintf(buf, buf_size, "%s.q", opr_reg_p(po, popr));
1883 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
1887 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
1891 if (popr->name[1] == 'h') // XXX..
1892 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
1894 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
1897 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
1902 if (is_stack_access(po, popr)) {
1903 stack_frame_access(po, popr, buf, buf_size,
1904 popr->name, "", 0, 0);
1908 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1911 if (popr->size_mismatch)
1912 snprintf(buf, buf_size, "%s%s%s",
1913 lmod_cast_u_ptr(po, popr->lmod),
1914 popr->is_array ? "" : "&", popr->name);
1916 snprintf(buf, buf_size, "%s%s", popr->name,
1917 popr->is_array ? "[0]" : "");
1921 ferr(po, "invalid dst type: %d\n", popr->type);
1927 static char *out_src_opr_u32(char *buf, size_t buf_size,
1928 struct parsed_op *po, struct parsed_opr *popr)
1930 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1933 static void out_test_for_cc(char *buf, size_t buf_size,
1934 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1935 enum opr_lenmod lmod, const char *expr)
1937 const char *cast, *scast;
1939 cast = lmod_cast_u(po, lmod);
1940 scast = lmod_cast_s(po, lmod);
1944 case PFO_BE: // CF=1||ZF=1; CF=0
1945 snprintf(buf, buf_size, "(%s%s %s 0)",
1946 cast, expr, is_inv ? "!=" : "==");
1950 case PFO_L: // SF!=OF; OF=0
1951 snprintf(buf, buf_size, "(%s%s %s 0)",
1952 scast, expr, is_inv ? ">=" : "<");
1955 case PFO_LE: // ZF=1||SF!=OF; OF=0
1956 snprintf(buf, buf_size, "(%s%s %s 0)",
1957 scast, expr, is_inv ? ">" : "<=");
1961 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1965 static void out_cmp_for_cc(char *buf, size_t buf_size,
1966 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1968 const char *cast, *scast, *cast_use;
1969 char buf1[256], buf2[256];
1970 enum opr_lenmod lmod;
1972 if (po->op != OP_DEC && po->operand[0].lmod != po->operand[1].lmod)
1973 ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
1974 po->operand[0].lmod, po->operand[1].lmod);
1975 lmod = po->operand[0].lmod;
1977 cast = lmod_cast_u(po, lmod);
1978 scast = lmod_cast_s(po, lmod);
1994 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1997 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
1998 if (po->op == OP_DEC)
1999 snprintf(buf2, sizeof(buf2), "1");
2001 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
2005 // note: must be unsigned compare
2006 snprintf(buf, buf_size, "(%s %s %s)",
2007 buf1, is_inv ? ">=" : "<", buf2);
2011 snprintf(buf, buf_size, "(%s %s %s)",
2012 buf1, is_inv ? "!=" : "==", buf2);
2016 // note: must be unsigned compare
2017 snprintf(buf, buf_size, "(%s %s %s)",
2018 buf1, is_inv ? ">" : "<=", buf2);
2021 if (is_inv && lmod == OPLM_BYTE
2022 && po->operand[1].type == OPT_CONST
2023 && po->operand[1].val == 0xff)
2025 snprintf(g_comment, sizeof(g_comment), "if %s", buf);
2026 snprintf(buf, buf_size, "(0)");
2030 // note: must be signed compare
2032 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
2033 scast, buf1, buf2, is_inv ? ">=" : "<");
2037 snprintf(buf, buf_size, "(%s %s %s)",
2038 buf1, is_inv ? ">=" : "<", buf2);
2042 snprintf(buf, buf_size, "(%s %s %s)",
2043 buf1, is_inv ? ">" : "<=", buf2);
2051 static void out_cmp_test(char *buf, size_t buf_size,
2052 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
2054 char buf1[256], buf2[256], buf3[256];
2056 if (po->op == OP_TEST) {
2057 if (IS(opr_name(po, 0), opr_name(po, 1))) {
2058 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
2061 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
2062 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
2063 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
2065 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
2066 po->operand[0].lmod, buf3);
2068 else if (po->op == OP_CMP) {
2069 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
2072 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
2075 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
2076 struct parsed_opr *popr2)
2078 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
2079 ferr(po, "missing lmod for both operands\n");
2081 if (popr1->lmod == OPLM_UNSPEC)
2082 popr1->lmod = popr2->lmod;
2083 else if (popr2->lmod == OPLM_UNSPEC)
2084 popr2->lmod = popr1->lmod;
2085 else if (popr1->lmod != popr2->lmod) {
2086 if (popr1->type_from_var) {
2087 popr1->size_mismatch = 1;
2088 if (popr1->lmod < popr2->lmod)
2090 popr1->lmod = popr2->lmod;
2092 else if (popr2->type_from_var) {
2093 popr2->size_mismatch = 1;
2094 if (popr2->lmod < popr1->lmod)
2096 popr2->lmod = popr1->lmod;
2099 ferr(po, "conflicting lmods: %d vs %d\n",
2100 popr1->lmod, popr2->lmod);
2104 static const char *op_to_c(struct parsed_op *po)
2128 ferr(po, "op_to_c was supplied with %d\n", po->op);
2132 static void op_set_clear_flag(struct parsed_op *po,
2133 enum op_flags flag_set, enum op_flags flag_clear)
2135 po->flags |= flag_set;
2136 po->flags &= ~flag_clear;
2139 // last op in stream - unconditional branch or ret
2140 #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
2141 || ((ops[_i].flags & (OPF_JMP|OPF_CJMP|OPF_RMD)) == OPF_JMP \
2142 && ops[_i].op != OP_CALL))
2144 #define check_i(po, i) \
2146 ferr(po, "bad " #i ": %d\n", i)
2148 static int scan_for_pop(int i, int opcnt, const char *reg,
2149 int magic, int depth, int *maxdepth, int do_flags)
2151 const struct parsed_proto *pp;
2152 struct parsed_op *po;
2156 for (; i < opcnt; i++) {
2158 if (po->cc_scratch == magic)
2159 break; // already checked
2160 po->cc_scratch = magic;
2162 if (po->flags & OPF_TAIL) {
2163 if (po->op == OP_CALL) {
2164 pp = proto_parse(g_fhdr, po->operand[0].name, g_quiet_pp);
2165 if (pp != NULL && pp->is_noreturn)
2166 // no stack cleanup for noreturn
2169 return -1; // deadend
2172 if ((po->flags & (OPF_RMD|OPF_DONE))
2173 || (po->op == OP_PUSH && po->p_argnum != 0)) // arg push
2176 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2177 if (po->btj != NULL) {
2179 for (j = 0; j < po->btj->count; j++) {
2180 check_i(po, po->btj->d[j].bt_i);
2181 ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic,
2182 depth, maxdepth, do_flags);
2184 return ret; // dead end
2189 check_i(po, po->bt_i);
2190 if (po->flags & OPF_CJMP) {
2191 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic,
2192 depth, maxdepth, do_flags);
2194 return ret; // dead end
2202 if ((po->op == OP_POP || po->op == OP_PUSH)
2203 && po->operand[0].type == OPT_REG
2204 && IS(po->operand[0].name, reg))
2206 if (po->op == OP_PUSH && !(po->flags & OPF_FARGNR)) {
2208 if (depth > *maxdepth)
2211 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2213 else if (po->op == OP_POP) {
2216 op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
2221 if (depth < 0) // should not happen
2222 ferr(po, "fail with depth\n");
2224 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2233 // scan for pop starting from 'ret' op (all paths)
2234 static int scan_for_pop_ret(int i, int opcnt, const char *reg,
2240 for (; i < opcnt; i++) {
2241 if (!(ops[i].flags & OPF_TAIL))
2244 for (j = i - 1; j >= 0; j--) {
2245 if (ops[j].flags & (OPF_RMD|OPF_DONE))
2247 if (ops[j].flags & OPF_JMP)
2250 if (ops[j].op == OP_POP && ops[j].datap == NULL
2251 && ops[j].operand[0].type == OPT_REG
2252 && IS(ops[j].operand[0].name, reg))
2255 ops[j].flags |= flag_set;
2259 if (g_labels[j] != NULL)
2264 return found ? 0 : -1;
2267 static void scan_for_pop_const(int i, int opcnt, int *regmask_pp)
2269 struct parsed_op *po;
2270 int is_multipath = 0;
2273 for (j = i + 1; j < opcnt; j++) {
2276 if (po->op == OP_JMP && po->btj == NULL) {
2277 ferr_assert(po, po->bt_i >= 0);
2282 if ((po->flags & (OPF_JMP|OPF_TAIL|OPF_RSAVE))
2283 || po->op == OP_PUSH)
2288 if (g_labels[j] != NULL)
2291 if (po->op == OP_POP && !(po->flags & OPF_RMD))
2293 is_multipath |= !!(po->flags & OPF_PPUSH);
2295 ops[i].flags |= OPF_PPUSH | OPF_DONE;
2297 po->flags |= OPF_PPUSH | OPF_DONE;
2298 *regmask_pp |= 1 << po->operand[0].reg;
2301 ops[i].flags |= OPF_RMD | OPF_DONE;
2302 po->flags |= OPF_DONE;
2303 po->datap = &ops[i];
2310 static void scan_propagate_df(int i, int opcnt)
2312 struct parsed_op *po = &ops[i];
2315 for (; i < opcnt; i++) {
2317 if (po->flags & OPF_DF)
2318 return; // already resolved
2319 po->flags |= OPF_DF;
2321 if (po->op == OP_CALL)
2322 ferr(po, "call with DF set?\n");
2324 if (po->flags & OPF_JMP) {
2325 if (po->btj != NULL) {
2327 for (j = 0; j < po->btj->count; j++) {
2328 check_i(po, po->btj->d[j].bt_i);
2329 scan_propagate_df(po->btj->d[j].bt_i, opcnt);
2334 check_i(po, po->bt_i);
2335 if (po->flags & OPF_CJMP)
2336 scan_propagate_df(po->bt_i, opcnt);
2342 if (po->flags & OPF_TAIL)
2345 if (po->op == OP_CLD) {
2346 po->flags |= OPF_RMD | OPF_DONE;
2351 ferr(po, "missing DF clear?\n");
2354 // is operand 'opr' referenced by parsed_op 'po'?
2355 static int is_opr_referenced(const struct parsed_opr *opr,
2356 const struct parsed_op *po)
2360 if (opr->type == OPT_REG) {
2361 mask = po->regmask_dst | po->regmask_src;
2362 if (po->op == OP_CALL)
2363 mask |= (1 << xAX) | (1 << xCX) | (1 << xDX);
2364 if ((1 << opr->reg) & mask)
2370 for (i = 0; i < po->operand_cnt; i++)
2371 if (IS(po->operand[0].name, opr->name))
2377 // is operand 'opr' read by parsed_op 'po'?
2378 static int is_opr_read(const struct parsed_opr *opr,
2379 const struct parsed_op *po)
2383 if (opr->type == OPT_REG) {
2384 mask = po->regmask_src;
2385 if (po->op == OP_CALL)
2386 // assume worst case
2387 mask |= (1 << xAX) | (1 << xCX) | (1 << xDX);
2388 if ((1 << opr->reg) & mask)
2398 // is operand 'opr' modified by parsed_op 'po'?
2399 static int is_opr_modified(const struct parsed_opr *opr,
2400 const struct parsed_op *po)
2404 if (!(po->flags & OPF_DATA))
2407 if (opr->type == OPT_REG) {
2408 if (po->op == OP_CALL) {
2409 mask = (1 << xAX) | (1 << xCX) | (1 << xDX);
2410 if ((1 << opr->reg) & mask)
2416 if (po->operand[0].type == OPT_REG) {
2417 if (po->regmask_dst & (1 << opr->reg))
2424 return IS(po->operand[0].name, opr->name);
2427 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
2428 static int is_any_opr_modified(const struct parsed_op *po_test,
2429 const struct parsed_op *po, int c_mode)
2434 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2437 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2440 if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
2443 // in reality, it can wreck any register, but in decompiled C
2444 // version it can only overwrite eax or edx:eax
2445 mask = (1 << xAX) | (1 << xDX);
2449 if (po->op == OP_CALL
2450 && ((po_test->regmask_src | po_test->regmask_dst) & mask))
2453 for (i = 0; i < po_test->operand_cnt; i++)
2454 if (IS(po_test->operand[i].name, po->operand[0].name))
2460 // scan for any po_test operand modification in range given
2461 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
2464 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2467 for (; i < opcnt; i++) {
2468 if (is_any_opr_modified(po_test, &ops[i], c_mode))
2475 // scan for po_test operand[0] modification in range given
2476 static int scan_for_mod_opr0(struct parsed_op *po_test,
2479 for (; i < opcnt; i++) {
2480 if (is_opr_modified(&po_test->operand[0], &ops[i]))
2487 static int scan_for_flag_set(int i, int magic, int *branched,
2488 int *setters, int *setter_cnt)
2490 struct label_ref *lr;
2494 if (ops[i].cc_scratch == magic) {
2495 ferr(&ops[i], "%s looped\n", __func__);
2498 ops[i].cc_scratch = magic;
2500 if (g_labels[i] != NULL) {
2503 lr = &g_label_refs[i];
2504 for (; lr->next; lr = lr->next) {
2505 check_i(&ops[i], lr->i);
2506 ret = scan_for_flag_set(lr->i, magic,
2507 branched, setters, setter_cnt);
2512 check_i(&ops[i], lr->i);
2513 if (i > 0 && LAST_OP(i - 1)) {
2517 ret = scan_for_flag_set(lr->i, magic,
2518 branched, setters, setter_cnt);
2524 if (ops[i].flags & OPF_FLAGS) {
2525 setters[*setter_cnt] = i;
2530 if ((ops[i].flags & (OPF_JMP|OPF_CJMP)) == OPF_JMP)
2537 // scan back for cdq, if anything modifies edx, fail
2538 static int scan_for_cdq_edx(int i)
2541 if (g_labels[i] != NULL) {
2542 if (g_label_refs[i].next != NULL)
2544 if (i > 0 && LAST_OP(i - 1)) {
2545 i = g_label_refs[i].i;
2552 if (ops[i].op == OP_CDQ)
2555 if (ops[i].regmask_dst & (1 << xDX))
2562 static int scan_for_reg_clear(int i, int reg)
2565 if (g_labels[i] != NULL) {
2566 if (g_label_refs[i].next != NULL)
2568 if (i > 0 && LAST_OP(i - 1)) {
2569 i = g_label_refs[i].i;
2576 if (ops[i].op == OP_XOR
2577 && ops[i].operand[0].lmod == OPLM_DWORD
2578 && ops[i].operand[0].reg == ops[i].operand[1].reg
2579 && ops[i].operand[0].reg == reg)
2582 if (ops[i].regmask_dst & (1 << reg))
2589 static void patch_esp_adjust(struct parsed_op *po, int adj)
2591 ferr_assert(po, po->op == OP_ADD);
2592 ferr_assert(po, IS(opr_name(po, 0), "esp"));
2593 ferr_assert(po, po->operand[1].type == OPT_CONST);
2595 // this is a bit of a hack, but deals with use of
2596 // single adj for multiple calls
2597 po->operand[1].val -= adj;
2598 po->flags |= OPF_RMD;
2599 if (po->operand[1].val == 0)
2600 po->flags |= OPF_DONE;
2601 ferr_assert(po, (int)po->operand[1].val >= 0);
2604 // scan for positive, constant esp adjust
2605 // multipath case is preliminary
2606 static int scan_for_esp_adjust(int i, int opcnt,
2607 int adj_expect, int *adj, int *is_multipath, int do_update)
2609 struct parsed_op *po;
2612 *adj = *is_multipath = 0;
2614 for (; i < opcnt && *adj < adj_expect; i++) {
2615 if (g_labels[i] != NULL)
2619 if (po->flags & OPF_DONE)
2622 if (po->op == OP_ADD && po->operand[0].reg == xSP) {
2623 if (po->operand[1].type != OPT_CONST)
2624 ferr(&ops[i], "non-const esp adjust?\n");
2625 *adj += po->operand[1].val;
2627 ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
2630 patch_esp_adjust(po, adj_expect);
2632 po->flags |= OPF_RMD;
2636 else if (po->op == OP_PUSH) {
2637 //if (first_pop == -1)
2638 // first_pop = -2; // none
2639 *adj -= lmod_bytes(po, po->operand[0].lmod);
2641 else if (po->op == OP_POP) {
2642 if (!(po->flags & OPF_DONE)) {
2643 // seems like msvc only uses 'pop ecx' for stack realignment..
2644 if (po->operand[0].type != OPT_REG || po->operand[0].reg != xCX)
2646 if (first_pop == -1 && *adj >= 0)
2649 if (do_update && *adj >= 0) {
2650 po->flags |= OPF_RMD;
2652 po->flags |= OPF_DONE;
2655 *adj += lmod_bytes(po, po->operand[0].lmod);
2657 else if (po->flags & (OPF_JMP|OPF_TAIL)) {
2658 if (po->op == OP_JMP && po->btj == NULL) {
2664 if (po->op != OP_CALL)
2666 if (po->operand[0].type != OPT_LABEL)
2668 if (po->pp != NULL && po->pp->is_stdcall)
2670 // assume it's another cdecl call
2674 if (first_pop >= 0) {
2675 // probably 'pop ecx' was used..
2682 static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
2684 struct parsed_op *po;
2688 ferr(ops, "%s: followed bad branch?\n", __func__);
2690 for (; i < opcnt; i++) {
2692 if (po->cc_scratch == magic)
2694 po->cc_scratch = magic;
2697 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2698 if (po->btj != NULL) {
2700 for (j = 0; j < po->btj->count; j++)
2701 scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
2705 scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
2706 if (!(po->flags & OPF_CJMP))
2709 if (po->flags & OPF_TAIL)
2714 static const struct parsed_proto *try_recover_pp(
2715 struct parsed_op *po, const struct parsed_opr *opr, int *search_instead)
2717 const struct parsed_proto *pp = NULL;
2721 // maybe an arg of g_func?
2722 if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
2724 char ofs_reg[16] = { 0, };
2725 int arg, arg_s, arg_i;
2732 parse_stack_access(po, opr->name, ofs_reg,
2733 &offset, &stack_ra, NULL, 0);
2734 if (ofs_reg[0] != 0)
2735 ferr(po, "offset reg on arg access?\n");
2736 if (offset <= stack_ra) {
2737 // search who set the stack var instead
2738 if (search_instead != NULL)
2739 *search_instead = 1;
2743 arg_i = (offset - stack_ra - 4) / 4;
2744 for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
2745 if (g_func_pp->arg[arg].reg != NULL)
2751 if (arg == g_func_pp->argc)
2752 ferr(po, "stack arg %d not in prototype?\n", arg_i);
2754 pp = g_func_pp->arg[arg].fptr;
2756 ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1);
2757 check_func_pp(po, pp, "icall arg");
2759 else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
2761 p = strchr(opr->name + 1, '[');
2762 memcpy(buf, opr->name, p - opr->name);
2763 buf[p - opr->name] = 0;
2764 pp = proto_parse(g_fhdr, buf, g_quiet_pp);
2766 else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
2767 pp = proto_parse(g_fhdr, opr->name, g_quiet_pp);
2770 ferr(po, "proto_parse failed for icall to '%s'\n", opr->name);
2773 check_func_pp(po, pp, "reg-fptr ref");
2779 static void scan_for_call_type(int i, const struct parsed_opr *opr,
2780 int magic, const struct parsed_proto **pp_found, int *pp_i,
2783 const struct parsed_proto *pp = NULL;
2784 struct parsed_op *po;
2785 struct label_ref *lr;
2787 ops[i].cc_scratch = magic;
2790 if (g_labels[i] != NULL) {
2791 lr = &g_label_refs[i];
2792 for (; lr != NULL; lr = lr->next) {
2793 check_i(&ops[i], lr->i);
2794 scan_for_call_type(lr->i, opr, magic, pp_found, pp_i, multi);
2796 if (i > 0 && LAST_OP(i - 1))
2804 if (ops[i].cc_scratch == magic)
2806 ops[i].cc_scratch = magic;
2808 if (!(ops[i].flags & OPF_DATA))
2810 if (!is_opr_modified(opr, &ops[i]))
2812 if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
2813 // most probably trashed by some processing
2818 opr = &ops[i].operand[1];
2819 if (opr->type != OPT_REG)
2823 po = (i >= 0) ? &ops[i] : ops;
2826 // reached the top - can only be an arg-reg
2827 if (opr->type != OPT_REG)
2830 for (i = 0; i < g_func_pp->argc; i++) {
2831 if (g_func_pp->arg[i].reg == NULL)
2833 if (IS(opr->name, g_func_pp->arg[i].reg))
2836 if (i == g_func_pp->argc)
2838 pp = g_func_pp->arg[i].fptr;
2840 ferr(po, "icall: arg%d (%s) is not a fptr?\n",
2841 i + 1, g_func_pp->arg[i].reg);
2842 check_func_pp(po, pp, "icall reg-arg");
2845 pp = try_recover_pp(po, opr, NULL);
2847 if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
2848 if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
2849 || (*pp_found)->is_stdcall != pp->is_stdcall
2850 || (*pp_found)->is_fptr != pp->is_fptr
2851 || (*pp_found)->argc != pp->argc
2852 || (*pp_found)->argc_reg != pp->argc_reg
2853 || (*pp_found)->argc_stack != pp->argc_stack)
2855 ferr(po, "icall: parsed_proto mismatch\n");
2865 // early check for tail call or branch back
2866 static int is_like_tailjmp(int j)
2868 if (!(ops[j].flags & OPF_JMP))
2871 if (ops[j].op == OP_JMP && !ops[j].operand[0].had_ds)
2872 // probably local branch back..
2874 if (ops[j].op == OP_CALL)
2875 // probably noreturn call..
2881 static void scan_prologue_epilogue(int opcnt)
2883 int ecx_push = 0, esp_sub = 0;
2887 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
2888 && ops[1].op == OP_MOV
2889 && IS(opr_name(&ops[1], 0), "ebp")
2890 && IS(opr_name(&ops[1], 1), "esp"))
2893 ops[0].flags |= OPF_RMD | OPF_DONE;
2894 ops[1].flags |= OPF_RMD | OPF_DONE;
2897 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
2898 g_stack_fsz = opr_const(&ops[2], 1);
2899 ops[2].flags |= OPF_RMD | OPF_DONE;
2903 // another way msvc builds stack frame..
2905 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
2907 ops[i].flags |= OPF_RMD | OPF_DONE;
2911 // and another way..
2912 if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
2913 && ops[i].operand[1].type == OPT_CONST
2914 && ops[i + 1].op == OP_CALL
2915 && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
2917 g_stack_fsz += ops[i].operand[1].val;
2918 ops[i].flags |= OPF_RMD | OPF_DONE;
2920 ops[i].flags |= OPF_RMD | OPF_DONE;
2927 for (; i < opcnt; i++)
2928 if (ops[i].op == OP_RET)
2931 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
2932 if (found && is_like_tailjmp(j))
2937 if ((ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp"))
2938 || ops[j].op == OP_LEAVE)
2940 ops[j].flags |= OPF_RMD | OPF_DONE;
2942 else if (!(g_ida_func_attr & IDAFA_NORETURN))
2943 ferr(&ops[j], "'pop ebp' expected\n");
2945 if (g_stack_fsz != 0) {
2946 if (ops[j - 1].op == OP_MOV
2947 && IS(opr_name(&ops[j - 1], 0), "esp")
2948 && IS(opr_name(&ops[j - 1], 1), "ebp"))
2950 ops[j - 1].flags |= OPF_RMD | OPF_DONE;
2952 else if (ops[j].op != OP_LEAVE
2953 && !(g_ida_func_attr & IDAFA_NORETURN))
2955 ferr(&ops[j - 1], "esp restore expected\n");
2958 if (ecx_push && ops[j - 2].op == OP_POP
2959 && IS(opr_name(&ops[j - 2], 0), "ecx"))
2961 ferr(&ops[j - 2], "unexpected ecx pop\n");
2967 } while (i < opcnt);
2974 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
2975 ops[i].flags |= OPF_RMD | OPF_DONE;
2981 for (; i < opcnt; i++) {
2982 if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
2984 if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
2985 && ops[i].operand[1].type == OPT_CONST)
2987 g_stack_fsz = ops[i].operand[1].val;
2988 ops[i].flags |= OPF_RMD | OPF_DONE;
2994 if (ecx_push && !esp_sub) {
2995 // could actually be args for a call..
2996 for (; i < opcnt; i++)
2997 if (ops[i].op != OP_PUSH)
3000 if (ops[i].op == OP_CALL && ops[i].operand[0].type == OPT_LABEL) {
3001 const struct parsed_proto *pp;
3002 pp = proto_parse(g_fhdr, opr_name(&ops[i], 0), 1);
3003 j = pp ? pp->argc_stack : 0;
3004 while (i > 0 && j > 0) {
3006 if (ops[i].op == OP_PUSH) {
3007 ops[i].flags &= ~(OPF_RMD | OPF_DONE);
3012 ferr(&ops[i], "unhandled prologue\n");
3015 i = g_stack_fsz = ecx_push = 0;
3016 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
3017 if (!(ops[i].flags & OPF_RMD))
3027 if (ecx_push || esp_sub)
3033 for (; i < opcnt; i++)
3034 if (ops[i].op == OP_RET)
3037 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
3038 if (found && is_like_tailjmp(j))
3044 for (l = 0; l < ecx_push; l++) {
3045 if (ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ecx"))
3047 else if (ops[j].op == OP_ADD
3048 && IS(opr_name(&ops[j], 0), "esp")
3049 && ops[j].operand[1].type == OPT_CONST)
3052 l += ops[j].operand[1].val / 4 - 1;
3055 ferr(&ops[j], "'pop ecx' expected\n");
3057 ops[j].flags |= OPF_RMD | OPF_DONE;
3061 ferr(&ops[j], "epilogue scan failed\n");
3067 if (ops[j].op != OP_ADD
3068 || !IS(opr_name(&ops[j], 0), "esp")
3069 || ops[j].operand[1].type != OPT_CONST
3070 || ops[j].operand[1].val != g_stack_fsz)
3071 ferr(&ops[j], "'add esp' expected\n");
3073 ops[j].flags |= OPF_RMD | OPF_DONE;
3074 ops[j].operand[1].val = 0; // hack for stack arg scanner
3079 } while (i < opcnt);
3083 static const struct parsed_proto *resolve_icall(int i, int opcnt,
3084 int *pp_i, int *multi_src)
3086 const struct parsed_proto *pp = NULL;
3087 int search_advice = 0;
3092 switch (ops[i].operand[0].type) {
3096 pp = try_recover_pp(&ops[i], &ops[i].operand[0], &search_advice);
3101 scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
3109 // find an instruction that changed opr before i op
3110 // *op_i must be set to -1 by the caller
3111 // *entry is set to 1 if one source is determined to be the caller
3112 // returns 1 if found, *op_i is then set to origin
3113 static int resolve_origin(int i, const struct parsed_opr *opr,
3114 int magic, int *op_i, int *is_caller)
3116 struct label_ref *lr;
3119 if (ops[i].cc_scratch == magic)
3121 ops[i].cc_scratch = magic;
3124 if (g_labels[i] != NULL) {
3125 lr = &g_label_refs[i];
3126 for (; lr != NULL; lr = lr->next) {
3127 check_i(&ops[i], lr->i);
3128 ret |= resolve_origin(lr->i, opr, magic, op_i, is_caller);
3130 if (i > 0 && LAST_OP(i - 1))
3136 if (is_caller != NULL)
3141 if (ops[i].cc_scratch == magic)
3143 ops[i].cc_scratch = magic;
3145 if (!(ops[i].flags & OPF_DATA))
3147 if (!is_opr_modified(opr, &ops[i]))
3153 // XXX: could check if the other op does the same
3162 // find an instruction that previously referenced opr
3163 // if multiple results are found - fail
3164 // *op_i must be set to -1 by the caller
3165 // returns 1 if found, *op_i is then set to referencer insn
3166 static int resolve_last_ref(int i, const struct parsed_opr *opr,
3167 int magic, int *op_i)
3169 struct label_ref *lr;
3172 if (ops[i].cc_scratch == magic)
3174 ops[i].cc_scratch = magic;
3177 if (g_labels[i] != NULL) {
3178 lr = &g_label_refs[i];
3179 for (; lr != NULL; lr = lr->next) {
3180 check_i(&ops[i], lr->i);
3181 ret |= resolve_last_ref(lr->i, opr, magic, op_i);
3183 if (i > 0 && LAST_OP(i - 1))
3191 if (ops[i].cc_scratch == magic)
3193 ops[i].cc_scratch = magic;
3195 if (!is_opr_referenced(opr, &ops[i]))
3206 // find next instruction that reads opr
3207 // if multiple results are found - fail
3208 // *op_i must be set to -1 by the caller
3209 // returns 1 if found, *op_i is then set to referencer insn
3210 static int find_next_read(int i, int opcnt,
3211 const struct parsed_opr *opr, int magic, int *op_i)
3213 struct parsed_op *po;
3216 for (; i < opcnt; i++)
3218 if (ops[i].cc_scratch == magic)
3220 ops[i].cc_scratch = magic;
3223 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
3224 if (po->btj != NULL) {
3226 for (j = 0; j < po->btj->count; j++) {
3227 check_i(po, po->btj->d[j].bt_i);
3228 ret |= find_next_read(po->btj->d[j].bt_i, opcnt, opr,
3234 if (po->flags & OPF_RMD)
3236 check_i(po, po->bt_i);
3237 if (po->flags & OPF_CJMP) {
3238 ret = find_next_read(po->bt_i, opcnt, opr, magic, op_i);
3247 if (!is_opr_read(opr, po)) {
3248 if (is_opr_modified(opr, po))
3251 if (po->flags & OPF_TAIL)
3266 static int try_resolve_const(int i, const struct parsed_opr *opr,
3267 int magic, unsigned int *val)
3272 ret = resolve_origin(i, opr, magic, &s_i, NULL);
3275 if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST)
3278 *val = ops[i].operand[1].val;
3285 static struct parsed_proto *process_call_early(int i, int opcnt,
3288 struct parsed_op *po = &ops[i];
3289 struct parsed_proto *pp;
3295 if (pp == NULL || pp->is_vararg || pp->argc_reg != 0)
3299 // look for and make use of esp adjust
3301 if (!pp->is_stdcall && pp->argc_stack > 0)
3302 ret = scan_for_esp_adjust(i + 1, opcnt,
3303 pp->argc_stack * 4, &adj, &multipath, 0);
3305 if (pp->argc_stack > adj / 4)
3309 if (ops[ret].op == OP_POP && adj != 4)
3317 static struct parsed_proto *process_call(int i, int opcnt)
3319 struct parsed_op *po = &ops[i];
3320 const struct parsed_proto *pp_c;
3321 struct parsed_proto *pp;
3322 const char *tmpname;
3323 int call_i = -1, ref_i = -1;
3324 int adj = 0, multipath = 0;
3327 tmpname = opr_name(po, 0);
3332 pp_c = resolve_icall(i, opcnt, &call_i, &multipath);
3334 if (!pp_c->is_func && !pp_c->is_fptr)
3335 ferr(po, "call to non-func: %s\n", pp_c->name);
3336 pp = proto_clone(pp_c);
3337 my_assert_not(pp, NULL);
3339 // not resolved just to single func
3342 switch (po->operand[0].type) {
3344 // we resolved this call and no longer need the register
3345 po->regmask_src &= ~(1 << po->operand[0].reg);
3347 if (!multipath && i != call_i && ops[call_i].op == OP_MOV
3348 && ops[call_i].operand[1].type == OPT_LABEL)
3350 // no other source users?
3351 ret = resolve_last_ref(i, &po->operand[0], opcnt * 10,
3353 if (ret == 1 && call_i == ref_i) {
3354 // and nothing uses it after us?
3356 ret = find_next_read(i + 1, opcnt, &po->operand[0],
3357 opcnt * 11, &ref_i);
3359 // then also don't need the source mov
3360 ops[call_i].flags |= OPF_RMD;
3372 pp = calloc(1, sizeof(*pp));
3373 my_assert_not(pp, NULL);
3376 ret = scan_for_esp_adjust(i + 1, opcnt,
3377 32*4, &adj, &multipath, 0);
3378 if (ret < 0 || adj < 0) {
3379 if (!g_allow_regfunc)
3380 ferr(po, "non-__cdecl indirect call unhandled yet\n");
3381 pp->is_unresolved = 1;
3385 if (adj > ARRAY_SIZE(pp->arg))
3386 ferr(po, "esp adjust too large: %d\n", adj);
3387 pp->ret_type.name = strdup("int");
3388 pp->argc = pp->argc_stack = adj;
3389 for (arg = 0; arg < pp->argc; arg++)
3390 pp->arg[arg].type.name = strdup("int");
3395 // look for and make use of esp adjust
3398 if (!pp->is_stdcall && pp->argc_stack > 0)
3399 ret = scan_for_esp_adjust(i + 1, opcnt,
3400 pp->argc_stack * 4, &adj, &multipath, 0);
3402 if (pp->is_vararg) {
3403 if (adj / 4 < pp->argc_stack) {
3404 fnote(po, "(this call)\n");
3405 ferr(&ops[ret], "esp adjust is too small: %x < %x\n",
3406 adj, pp->argc_stack * 4);
3408 // modify pp to make it have varargs as normal args
3410 pp->argc += adj / 4 - pp->argc_stack;
3411 for (; arg < pp->argc; arg++) {
3412 pp->arg[arg].type.name = strdup("int");
3415 if (pp->argc > ARRAY_SIZE(pp->arg))
3416 ferr(po, "too many args for '%s'\n", tmpname);
3418 if (pp->argc_stack > adj / 4) {
3419 fnote(po, "(this call)\n");
3420 ferr(&ops[ret], "stack tracking failed for '%s': %x %x\n",
3421 tmpname, pp->argc_stack * 4, adj);
3424 scan_for_esp_adjust(i + 1, opcnt,
3425 pp->argc_stack * 4, &adj, &multipath, 1);
3427 else if (pp->is_vararg)
3428 ferr(po, "missing esp_adjust for vararg func '%s'\n",
3434 static int collect_call_args_early(struct parsed_op *po, int i,
3435 struct parsed_proto *pp, int *regmask)
3440 for (arg = 0; arg < pp->argc; arg++)
3441 if (pp->arg[arg].reg == NULL)
3444 // first see if it can be easily done
3445 for (j = i; j > 0 && arg < pp->argc; )
3447 if (g_labels[j] != NULL)
3451 if (ops[j].op == OP_CALL)
3453 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP)
3455 else if (ops[j].op == OP_POP)
3457 else if (ops[j].flags & OPF_CJMP)
3459 else if (ops[j].op == OP_PUSH) {
3460 if (ops[j].flags & (OPF_FARG|OPF_FARGNR))
3462 ret = scan_for_mod(&ops[j], j + 1, i, 1);
3466 if (pp->arg[arg].type.is_va_list)
3470 for (arg++; arg < pp->argc; arg++)
3471 if (pp->arg[arg].reg == NULL)
3480 for (arg = 0; arg < pp->argc; arg++)
3481 if (pp->arg[arg].reg == NULL)
3484 for (j = i; j > 0 && arg < pp->argc; )
3488 if (ops[j].op == OP_PUSH)
3490 ops[j].p_argnext = -1;
3491 ferr_assert(&ops[j], pp->arg[arg].datap == NULL);
3492 pp->arg[arg].datap = &ops[j];
3494 if (ops[j].operand[0].type == OPT_REG)
3495 *regmask |= 1 << ops[j].operand[0].reg;
3497 ops[j].flags |= OPF_RMD | OPF_DONE | OPF_FARGNR | OPF_FARG;
3498 ops[j].flags &= ~OPF_RSAVE;
3501 for (arg++; arg < pp->argc; arg++)
3502 if (pp->arg[arg].reg == NULL)
3510 static int collect_call_args_r(struct parsed_op *po, int i,
3511 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
3512 int *arg_grp, int arg, int magic, int need_op_saving, int may_reuse)
3514 struct parsed_proto *pp_tmp;
3515 struct parsed_op *po_tmp;
3516 struct label_ref *lr;
3517 int need_to_save_current;
3518 int arg_grp_current = 0;
3519 int save_args_seen = 0;
3527 ferr(po, "dead label encountered\n");
3531 for (; arg < pp->argc; arg++)
3532 if (pp->arg[arg].reg == NULL)
3534 magic = (magic & 0xffffff) | (arg << 24);
3536 for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
3538 if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
3539 if (ops[j].cc_scratch != magic) {
3540 ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
3544 // ok: have already been here
3547 ops[j].cc_scratch = magic;
3549 if (g_labels[j] != NULL && g_label_refs[j].i != -1) {
3550 lr = &g_label_refs[j];
3551 if (lr->next != NULL)
3553 for (; lr->next; lr = lr->next) {
3554 check_i(&ops[j], lr->i);
3555 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
3557 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
3558 arg_grp, arg, magic, need_op_saving, may_reuse);
3563 check_i(&ops[j], lr->i);
3564 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
3566 if (j > 0 && LAST_OP(j - 1)) {
3567 // follow last branch in reverse
3572 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
3573 arg_grp, arg, magic, need_op_saving, may_reuse);
3579 if (ops[j].op == OP_CALL)
3581 if (pp->is_unresolved)
3586 ferr(po, "arg collect hit unparsed call '%s'\n",
3587 ops[j].operand[0].name);
3588 if (may_reuse && pp_tmp->argc_stack > 0)
3589 ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
3590 arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
3592 // esp adjust of 0 means we collected it before
3593 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP
3594 && (ops[j].operand[1].type != OPT_CONST
3595 || ops[j].operand[1].val != 0))
3597 if (pp->is_unresolved)
3600 ferr(po, "arg collect %d/%d hit esp adjust of %d\n",
3601 arg, pp->argc, ops[j].operand[1].val);
3603 else if (ops[j].op == OP_POP && !(ops[j].flags & OPF_DONE))
3605 if (pp->is_unresolved)
3608 ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc);
3610 else if (ops[j].flags & OPF_CJMP)
3612 if (pp->is_unresolved)
3617 else if (ops[j].op == OP_PUSH
3618 && !(ops[j].flags & (OPF_FARGNR|OPF_DONE)))
3620 if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
3623 ops[j].p_argnext = -1;
3624 po_tmp = pp->arg[arg].datap;
3626 ops[j].p_argnext = po_tmp - ops;
3627 pp->arg[arg].datap = &ops[j];
3629 need_to_save_current = 0;
3632 if (ops[j].operand[0].type == OPT_REG)
3633 reg = ops[j].operand[0].reg;
3635 if (!need_op_saving) {
3636 ret = scan_for_mod(&ops[j], j + 1, i, 1);
3637 need_to_save_current = (ret >= 0);
3639 if (need_op_saving || need_to_save_current) {
3640 // mark this push as one that needs operand saving
3641 ops[j].flags &= ~OPF_RMD;
3642 if (ops[j].p_argnum == 0) {
3643 ops[j].p_argnum = arg + 1;
3644 save_args |= 1 << arg;
3646 else if (ops[j].p_argnum < arg + 1) {
3647 // XXX: might kill valid var..
3648 //*save_arg_vars &= ~(1 << (ops[j].p_argnum - 1));
3649 ops[j].p_argnum = arg + 1;
3650 save_args |= 1 << arg;
3653 if (save_args_seen & (1 << (ops[j].p_argnum - 1))) {
3656 if (arg_grp_current >= MAX_ARG_GRP)
3657 ferr(&ops[j], "out of arg groups (arg%d), f %s\n",
3658 ops[j].p_argnum, pp->name);
3661 else if (ops[j].p_argnum == 0)
3662 ops[j].flags |= OPF_RMD;
3664 // some PUSHes are reused by different calls on other branches,
3665 // but that can't happen if we didn't branch, so they
3666 // can be removed from future searches (handles nested calls)
3668 ops[j].flags |= OPF_FARGNR;
3670 ops[j].flags |= OPF_FARG;
3671 ops[j].flags &= ~OPF_RSAVE;
3673 // check for __VALIST
3674 if (!pp->is_unresolved && pp->arg[arg].type.is_va_list) {
3676 ret = resolve_origin(j, &ops[j].operand[0],
3677 magic + 1, &k, NULL);
3678 if (ret == 1 && k >= 0)
3680 if (ops[k].op == OP_LEA) {
3681 snprintf(buf, sizeof(buf), "arg_%X",
3682 g_func_pp->argc_stack * 4);
3683 if (!g_func_pp->is_vararg
3684 || strstr(ops[k].operand[1].name, buf))
3686 ops[k].flags |= OPF_RMD | OPF_DONE;
3687 ops[j].flags |= OPF_RMD | OPF_VAPUSH;
3688 save_args &= ~(1 << arg);
3692 ferr(&ops[j], "lea va_list used, but no vararg?\n");
3694 // check for va_list from g_func_pp arg too
3695 else if (ops[k].op == OP_MOV
3696 && is_stack_access(&ops[k], &ops[k].operand[1]))
3698 ret = stack_frame_access(&ops[k], &ops[k].operand[1],
3699 buf, sizeof(buf), ops[k].operand[1].name, "", 1, 0);
3701 ops[k].flags |= OPF_RMD | OPF_DONE;
3702 ops[j].flags |= OPF_RMD;
3703 ops[j].p_argpass = ret + 1;
3704 save_args &= ~(1 << arg);
3711 *save_arg_vars |= save_args;
3713 // tracking reg usage
3715 *regmask |= 1 << reg;
3718 if (!pp->is_unresolved) {
3720 for (; arg < pp->argc; arg++)
3721 if (pp->arg[arg].reg == NULL)
3724 magic = (magic & 0xffffff) | (arg << 24);
3727 if (ops[j].p_arggrp > arg_grp_current) {
3729 arg_grp_current = ops[j].p_arggrp;
3731 if (ops[j].p_argnum > 0)
3732 save_args_seen |= 1 << (ops[j].p_argnum - 1);
3735 if (arg < pp->argc) {
3736 ferr(po, "arg collect failed for '%s': %d/%d\n",
3737 pp->name, arg, pp->argc);
3741 if (arg_grp_current > *arg_grp)
3742 *arg_grp = arg_grp_current;
3747 static int collect_call_args(struct parsed_op *po, int i,
3748 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
3751 // arg group is for cases when pushes for
3752 // multiple funcs are going on
3753 struct parsed_op *po_tmp;
3754 int save_arg_vars_current = 0;
3759 ret = collect_call_args_r(po, i, pp, regmask,
3760 &save_arg_vars_current, &arg_grp, 0, magic, 0, 0);
3765 // propagate arg_grp
3766 for (a = 0; a < pp->argc; a++) {
3767 if (pp->arg[a].reg != NULL)
3770 po_tmp = pp->arg[a].datap;
3771 while (po_tmp != NULL) {
3772 po_tmp->p_arggrp = arg_grp;
3773 if (po_tmp->p_argnext > 0)
3774 po_tmp = &ops[po_tmp->p_argnext];
3780 save_arg_vars[arg_grp] |= save_arg_vars_current;
3782 if (pp->is_unresolved) {
3784 pp->argc_stack += ret;
3785 for (a = 0; a < pp->argc; a++)
3786 if (pp->arg[a].type.name == NULL)
3787 pp->arg[a].type.name = strdup("int");
3793 static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
3797 for (i = 0; i < pp->argc; i++)
3798 if (pp->arg[i].reg == NULL)
3802 memmove(&pp->arg[i + 1], &pp->arg[i],
3803 sizeof(pp->arg[0]) * pp->argc_stack);
3804 memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
3805 pp->arg[i].reg = strdup(reg);
3806 pp->arg[i].type.name = strdup("int");
3811 static void add_label_ref(struct label_ref *lr, int op_i)
3813 struct label_ref *lr_new;
3820 lr_new = calloc(1, sizeof(*lr_new));
3822 lr_new->next = lr->next;
3826 static struct parsed_data *try_resolve_jumptab(int i, int opcnt)
3828 struct parsed_op *po = &ops[i];
3829 struct parsed_data *pd;
3830 char label[NAMELEN], *p;
3833 p = strchr(po->operand[0].name, '[');
3837 len = p - po->operand[0].name;
3838 strncpy(label, po->operand[0].name, len);
3841 for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
3842 if (IS(g_func_pd[j].label, label)) {
3848 //ferr(po, "label '%s' not parsed?\n", label);
3851 if (pd->type != OPT_OFFSET)
3852 ferr(po, "label '%s' with non-offset data?\n", label);
3854 // find all labels, link
3855 for (j = 0; j < pd->count; j++) {
3856 for (l = 0; l < opcnt; l++) {
3857 if (g_labels[l] != NULL && IS(g_labels[l], pd->d[j].u.label)) {
3858 add_label_ref(&g_label_refs[l], i);
3868 static void clear_labels(int count)
3872 for (i = 0; i < count; i++) {
3873 if (g_labels[i] != NULL) {
3880 static void output_std_flags(FILE *fout, struct parsed_op *po,
3881 int *pfomask, const char *dst_opr_text)
3883 if (*pfomask & (1 << PFO_Z)) {
3884 fprintf(fout, "\n cond_z = (%s%s == 0);",
3885 lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
3886 *pfomask &= ~(1 << PFO_Z);
3888 if (*pfomask & (1 << PFO_S)) {
3889 fprintf(fout, "\n cond_s = (%s%s < 0);",
3890 lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
3891 *pfomask &= ~(1 << PFO_S);
3896 OPP_FORCE_NORETURN = (1 << 0),
3897 OPP_SIMPLE_ARGS = (1 << 1),
3898 OPP_ALIGN = (1 << 2),
3901 static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp,
3904 const char *cconv = "";
3906 if (pp->is_fastcall)
3907 cconv = "__fastcall ";
3908 else if (pp->is_stdcall && pp->argc_reg == 0)
3909 cconv = "__stdcall ";
3911 fprintf(fout, (flags & OPP_ALIGN) ? "%-16s" : "%s", cconv);
3913 if (pp->is_noreturn || (flags & OPP_FORCE_NORETURN))
3914 fprintf(fout, "noreturn ");
3917 static void output_pp(FILE *fout, const struct parsed_proto *pp,
3922 fprintf(fout, (flags & OPP_ALIGN) ? "%-5s" : "%s ",
3926 output_pp_attrs(fout, pp, flags);
3929 fprintf(fout, "%s", pp->name);
3934 for (i = 0; i < pp->argc; i++) {
3936 fprintf(fout, ", ");
3937 if (pp->arg[i].fptr != NULL && !(flags & OPP_SIMPLE_ARGS)) {
3939 output_pp(fout, pp->arg[i].fptr, 0);
3941 else if (pp->arg[i].type.is_retreg) {
3942 fprintf(fout, "u32 *r_%s", pp->arg[i].reg);
3945 fprintf(fout, "%s", pp->arg[i].type.name);
3947 fprintf(fout, " a%d", i + 1);
3950 if (pp->is_vararg) {
3952 fprintf(fout, ", ");
3953 fprintf(fout, "...");
3958 static int get_pp_arg_regmask(const struct parsed_proto *pp)
3963 for (i = 0; i < pp->argc; i++) {
3964 if (pp->arg[i].reg != NULL) {
3965 reg = char_array_i(regs_r32,
3966 ARRAY_SIZE(regs_r32), pp->arg[i].reg);
3968 ferr(ops, "arg '%s' of func '%s' is not a reg?\n",
3969 pp->arg[i].reg, pp->name);
3970 regmask |= 1 << reg;
3977 static char *saved_arg_name(char *buf, size_t buf_size, int grp, int num)
3983 snprintf(buf1, sizeof(buf1), "%d", grp);
3984 snprintf(buf, buf_size, "s%s_a%d", buf1, num);
3989 static void gen_x_cleanup(int opcnt);
3991 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
3993 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
3994 struct parsed_opr *last_arith_dst = NULL;
3995 char buf1[256], buf2[256], buf3[256], cast[64];
3996 const struct parsed_proto *pp_c;
3997 struct parsed_proto *pp, *pp_tmp;
3998 struct parsed_data *pd;
3999 const char *tmpname;
4001 int save_arg_vars[MAX_ARG_GRP] = { 0, };
4003 int need_tmp_var = 0;
4006 int label_pending = 0;
4007 int regmask_save = 0; // regs saved/restored in this func
4008 int regmask_arg = 0; // regs carrying function args (fastcall, etc)
4009 int regmask_now; // temp
4010 int regmask_init = 0; // regs that need zero initialization
4011 int regmask_pp = 0; // regs used in complex push-pop graph
4012 int regmask = 0; // used regs
4022 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
4023 g_stack_frame_used = 0;
4025 g_func_pp = proto_parse(fhdr, funcn, 0);
4026 if (g_func_pp == NULL)
4027 ferr(ops, "proto_parse failed for '%s'\n", funcn);
4029 regmask_arg = get_pp_arg_regmask(g_func_pp);
4032 // - handle ebp/esp frame, remove ops related to it
4033 scan_prologue_epilogue(opcnt);
4036 // - parse calls with labels
4037 // - resolve all branches
4038 for (i = 0; i < opcnt; i++)
4044 if (po->flags & (OPF_RMD|OPF_DONE))
4047 if (po->op == OP_CALL) {
4050 if (po->operand[0].type == OPT_LABEL) {
4051 tmpname = opr_name(po, 0);
4052 if (IS_START(tmpname, "loc_"))
4053 ferr(po, "call to loc_*\n");
4054 pp_c = proto_parse(fhdr, tmpname, 0);
4056 ferr(po, "proto_parse failed for call '%s'\n", tmpname);
4058 pp = proto_clone(pp_c);
4059 my_assert_not(pp, NULL);
4061 else if (po->datap != NULL) {
4062 pp = calloc(1, sizeof(*pp));
4063 my_assert_not(pp, NULL);
4065 ret = parse_protostr(po->datap, pp);
4067 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
4074 check_func_pp(po, pp, "fptr var call");
4075 if (pp->is_noreturn)
4076 po->flags |= OPF_TAIL;
4082 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
4085 if (po->operand[0].type == OPT_REGMEM) {
4086 pd = try_resolve_jumptab(i, opcnt);
4094 for (l = 0; l < opcnt; l++) {
4095 if (g_labels[l] != NULL
4096 && IS(po->operand[0].name, g_labels[l]))
4098 if (l == i + 1 && po->op == OP_JMP) {
4099 // yet another alignment type..
4100 po->flags |= OPF_RMD|OPF_DONE;
4103 add_label_ref(&g_label_refs[l], i);
4109 if (po->bt_i != -1 || (po->flags & OPF_RMD))
4112 if (po->operand[0].type == OPT_LABEL)
4116 ferr(po, "unhandled branch\n");
4120 po->flags |= OPF_TAIL;
4121 if (i > 0 && ops[i - 1].op == OP_POP)
4122 po->flags |= OPF_ATAIL;
4127 // - remove dead labels
4128 // - process trivial calls
4129 for (i = 0; i < opcnt; i++)
4131 if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
4137 if (po->flags & (OPF_RMD|OPF_DONE))
4140 if (po->op == OP_CALL)
4142 pp = process_call_early(i, opcnt, &j);
4144 if (!(po->flags & OPF_ATAIL))
4145 // since we know the args, try to collect them
4146 if (collect_call_args_early(po, i, pp, ®mask) != 0)
4152 // commit esp adjust
4153 ops[j].flags |= OPF_RMD;
4154 if (ops[j].op != OP_POP)
4155 patch_esp_adjust(&ops[j], pp->argc_stack * 4);
4157 ops[j].flags |= OPF_DONE;
4160 if (strstr(pp->ret_type.name, "int64"))
4163 po->flags |= OPF_DONE;
4170 // - handle push <const>/pop pairs
4171 for (i = 0; i < opcnt; i++)
4174 if (po->flags & (OPF_RMD|OPF_DONE))
4177 if (po->op == OP_CALL && !(po->flags & OPF_DONE))
4179 pp = process_call(i, opcnt);
4181 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
4182 // since we know the args, collect them
4183 collect_call_args(po, i, pp, ®mask, save_arg_vars,
4187 if (strstr(pp->ret_type.name, "int64"))
4190 else if (po->op == OP_PUSH && !(po->flags & OPF_FARG)
4191 && !(po->flags & OPF_RSAVE) && po->operand[0].type == OPT_CONST)
4192 scan_for_pop_const(i, opcnt, ®mask_pp);
4196 // - find POPs for PUSHes, rm both
4197 // - scan for STD/CLD, propagate DF
4198 // - scan for all used registers
4199 // - find flag set ops for their users
4200 // - do unreselved calls
4201 // - declare indirect functions
4202 for (i = 0; i < opcnt; i++)
4205 if (po->flags & (OPF_RMD|OPF_DONE))
4208 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
4209 reg = po->operand[0].reg;
4210 if (!(regmask & (1 << reg)))
4211 // not a reg save after all, rerun scan_for_pop
4212 po->flags &= ~OPF_RSAVE;
4214 regmask_save |= 1 << reg;
4217 if (po->op == OP_PUSH && !(po->flags & OPF_FARG)
4218 && !(po->flags & OPF_RSAVE) && !g_func_pp->is_userstack)
4220 if (po->operand[0].type == OPT_REG)
4222 reg = po->operand[0].reg;
4224 ferr(po, "reg not set for push?\n");
4227 ret = scan_for_pop(i + 1, opcnt,
4228 po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
4231 ferr(po, "too much depth: %d\n", depth);
4233 po->flags |= OPF_RMD;
4234 scan_for_pop(i + 1, opcnt, po->operand[0].name,
4235 i + opcnt * 4, 0, &depth, 1);
4238 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
4241 if (regmask & (1 << reg)) {
4242 if (regmask_save & (1 << reg))
4243 ferr(po, "%s already saved?\n", po->operand[0].name);
4247 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg);
4253 if (po->op == OP_STD) {
4254 po->flags |= OPF_DF | OPF_RMD | OPF_DONE;
4255 scan_propagate_df(i + 1, opcnt);
4258 regmask_now = po->regmask_src | po->regmask_dst;
4259 if (regmask_now & (1 << xBP)) {
4260 if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
4261 if (po->regmask_dst & (1 << xBP))
4262 // compiler decided to drop bp frame and use ebp as scratch
4263 scan_fwd_set_flags(i + 1, opcnt, i + opcnt * 5, OPF_EBP_S);
4265 regmask_now &= ~(1 << xBP);
4269 regmask |= regmask_now;
4271 if (po->flags & OPF_CC)
4273 int setters[16], cnt = 0, branched = 0;
4275 ret = scan_for_flag_set(i, i + opcnt * 6,
4276 &branched, setters, &cnt);
4277 if (ret < 0 || cnt <= 0)
4278 ferr(po, "unable to trace flag setter(s)\n");
4279 if (cnt > ARRAY_SIZE(setters))
4280 ferr(po, "too many flag setters\n");
4282 for (j = 0; j < cnt; j++)
4284 tmp_op = &ops[setters[j]]; // flag setter
4287 // to get nicer code, we try to delay test and cmp;
4288 // if we can't because of operand modification, or if we
4289 // have arith op, or branch, make it calculate flags explicitly
4290 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP)
4292 if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
4293 pfomask = 1 << po->pfo;
4295 else if (tmp_op->op == OP_CMPS || tmp_op->op == OP_SCAS) {
4296 pfomask = 1 << po->pfo;
4299 // see if we'll be able to handle based on op result
4300 if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
4301 && po->pfo != PFO_Z && po->pfo != PFO_S
4302 && po->pfo != PFO_P)
4304 || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
4306 pfomask = 1 << po->pfo;
4309 if (tmp_op->op == OP_ADD && po->pfo == PFO_C) {
4310 propagate_lmod(tmp_op, &tmp_op->operand[0],
4311 &tmp_op->operand[1]);
4312 if (tmp_op->operand[0].lmod == OPLM_DWORD)
4317 tmp_op->pfomask |= pfomask;
4318 cond_vars |= pfomask;
4320 // note: may overwrite, currently not a problem
4324 if (po->op == OP_RCL || po->op == OP_RCR
4325 || po->op == OP_ADC || po->op == OP_SBB)
4326 cond_vars |= 1 << PFO_C;
4329 if (po->op == OP_CMPS || po->op == OP_SCAS) {
4330 cond_vars |= 1 << PFO_Z;
4332 else if (po->op == OP_MUL
4333 || (po->op == OP_IMUL && po->operand_cnt == 1))
4335 if (po->operand[0].lmod == OPLM_DWORD)
4338 else if (po->op == OP_CALL) {
4339 // note: resolved non-reg calls are OPF_DONE already
4342 ferr(po, "NULL pp\n");
4344 if (pp->is_unresolved) {
4345 int regmask_stack = 0;
4346 collect_call_args(po, i, pp, ®mask, save_arg_vars,
4349 // this is pretty rough guess:
4350 // see ecx and edx were pushed (and not their saved versions)
4351 for (arg = 0; arg < pp->argc; arg++) {
4352 if (pp->arg[arg].reg != NULL)
4355 tmp_op = pp->arg[arg].datap;
4357 ferr(po, "parsed_op missing for arg%d\n", arg);
4358 if (tmp_op->p_argnum == 0 && tmp_op->operand[0].type == OPT_REG)
4359 regmask_stack |= 1 << tmp_op->operand[0].reg;
4362 if (!((regmask_stack & (1 << xCX))
4363 && (regmask_stack & (1 << xDX))))
4365 if (pp->argc_stack != 0
4366 || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX))))
4368 pp_insert_reg_arg(pp, "ecx");
4369 pp->is_fastcall = 1;
4370 regmask_init |= 1 << xCX;
4371 regmask |= 1 << xCX;
4373 if (pp->argc_stack != 0
4374 || ((regmask | regmask_arg) & (1 << xDX)))
4376 pp_insert_reg_arg(pp, "edx");
4377 regmask_init |= 1 << xDX;
4378 regmask |= 1 << xDX;
4382 // note: __cdecl doesn't fall into is_unresolved category
4383 if (pp->argc_stack > 0)
4387 for (arg = 0; arg < pp->argc; arg++) {
4388 if (pp->arg[arg].reg != NULL) {
4389 reg = char_array_i(regs_r32,
4390 ARRAY_SIZE(regs_r32), pp->arg[arg].reg);
4392 ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg);
4393 if (!(regmask & (1 << reg))) {
4394 regmask_init |= 1 << reg;
4395 regmask |= 1 << reg;
4400 else if (po->op == OP_MOV && po->operand[0].pp != NULL
4401 && po->operand[1].pp != NULL)
4403 // <var> = offset <something>
4404 if ((po->operand[1].pp->is_func || po->operand[1].pp->is_fptr)
4405 && !IS_START(po->operand[1].name, "off_"))
4407 if (!po->operand[0].pp->is_fptr)
4408 ferr(po, "%s not declared as fptr when it should be\n",
4409 po->operand[0].name);
4410 if (pp_cmp_func(po->operand[0].pp, po->operand[1].pp)) {
4411 pp_print(buf1, sizeof(buf1), po->operand[0].pp);
4412 pp_print(buf2, sizeof(buf2), po->operand[1].pp);
4413 fnote(po, "var: %s\n", buf1);
4414 fnote(po, "func: %s\n", buf2);
4415 ferr(po, "^ mismatch\n");
4419 else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void"))
4420 regmask |= 1 << xAX;
4421 else if (po->op == OP_DIV || po->op == OP_IDIV) {
4422 // 32bit division is common, look for it
4423 if (po->op == OP_DIV)
4424 ret = scan_for_reg_clear(i, xDX);
4426 ret = scan_for_cdq_edx(i);
4428 po->flags |= OPF_32BIT;
4432 else if (po->op == OP_CLD)
4433 po->flags |= OPF_RMD | OPF_DONE;
4435 if (po->op == OP_RCL || po->op == OP_RCR || po->op == OP_XCHG) {
4441 // - confirm regmask_save, it might have been reduced
4442 if (regmask_save != 0)
4445 for (i = 0; i < opcnt; i++) {
4447 if (po->flags & OPF_RMD)
4450 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE))
4451 regmask_save |= 1 << po->operand[0].reg;
4455 // output starts here
4457 // define userstack size
4458 if (g_func_pp->is_userstack) {
4459 fprintf(fout, "#ifndef US_SZ_%s\n", g_func_pp->name);
4460 fprintf(fout, "#define US_SZ_%s USERSTACK_SIZE\n", g_func_pp->name);
4461 fprintf(fout, "#endif\n");
4464 // the function itself
4465 ferr_assert(ops, !g_func_pp->is_fptr);
4466 output_pp(fout, g_func_pp,
4467 (g_ida_func_attr & IDAFA_NORETURN) ? OPP_FORCE_NORETURN : 0);
4468 fprintf(fout, "\n{\n");
4470 // declare indirect functions
4471 for (i = 0; i < opcnt; i++) {
4473 if (po->flags & OPF_RMD)
4476 if (po->op == OP_CALL) {
4479 ferr(po, "NULL pp\n");
4481 if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) {
4482 if (pp->name[0] != 0) {
4483 memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
4484 memcpy(pp->name, "i_", 2);
4486 // might be declared already
4488 for (j = 0; j < i; j++) {
4489 if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) {
4490 if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) {
4500 snprintf(pp->name, sizeof(pp->name), "icall%d", i);
4503 output_pp(fout, pp, OPP_SIMPLE_ARGS);
4504 fprintf(fout, ";\n");
4509 // output LUTs/jumptables
4510 for (i = 0; i < g_func_pd_cnt; i++) {
4512 fprintf(fout, " static const ");
4513 if (pd->type == OPT_OFFSET) {
4514 fprintf(fout, "void *jt_%s[] =\n { ", pd->label);
4516 for (j = 0; j < pd->count; j++) {
4518 fprintf(fout, ", ");
4519 fprintf(fout, "&&%s", pd->d[j].u.label);
4523 fprintf(fout, "%s %s[] =\n { ",
4524 lmod_type_u(ops, pd->lmod), pd->label);
4526 for (j = 0; j < pd->count; j++) {
4528 fprintf(fout, ", ");
4529 fprintf(fout, "%u", pd->d[j].u.val);
4532 fprintf(fout, " };\n");
4536 // declare stack frame, va_arg
4538 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
4539 (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz);
4543 if (g_func_pp->is_userstack) {
4544 fprintf(fout, " u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name);
4545 fprintf(fout, " u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n");
4549 if (g_func_pp->is_vararg) {
4550 fprintf(fout, " va_list ap;\n");
4554 // declare arg-registers
4555 for (i = 0; i < g_func_pp->argc; i++) {
4556 if (g_func_pp->arg[i].reg != NULL) {
4557 reg = char_array_i(regs_r32,
4558 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
4559 if (regmask & (1 << reg)) {
4560 if (g_func_pp->arg[i].type.is_retreg)
4561 fprintf(fout, " u32 %s = *r_%s;\n",
4562 g_func_pp->arg[i].reg, g_func_pp->arg[i].reg);
4564 fprintf(fout, " u32 %s = (u32)a%d;\n",
4565 g_func_pp->arg[i].reg, i + 1);
4568 if (g_func_pp->arg[i].type.is_retreg)
4569 ferr(ops, "retreg '%s' is unused?\n",
4570 g_func_pp->arg[i].reg);
4571 fprintf(fout, " // %s = a%d; // unused\n",
4572 g_func_pp->arg[i].reg, i + 1);
4578 // declare normal registers
4579 regmask_now = regmask & ~regmask_arg;
4580 regmask_now &= ~(1 << xSP);
4581 if (regmask_now & 0x00ff) {
4582 for (reg = 0; reg < 8; reg++) {
4583 if (regmask_now & (1 << reg)) {
4584 fprintf(fout, " u32 %s", regs_r32[reg]);
4585 if (regmask_init & (1 << reg))
4586 fprintf(fout, " = 0");
4587 fprintf(fout, ";\n");
4592 if (regmask_now & 0xff00) {
4593 for (reg = 8; reg < 16; reg++) {
4594 if (regmask_now & (1 << reg)) {
4595 fprintf(fout, " mmxr %s", regs_r32[reg]);
4596 if (regmask_init & (1 << reg))
4597 fprintf(fout, " = { 0, }");
4598 fprintf(fout, ";\n");
4605 for (reg = 0; reg < 8; reg++) {
4606 if (regmask_save & (1 << reg)) {
4607 fprintf(fout, " u32 s_%s;\n", regs_r32[reg]);
4613 for (i = 0; i < ARRAY_SIZE(save_arg_vars); i++) {
4614 if (save_arg_vars[i] == 0)
4616 for (reg = 0; reg < 32; reg++) {
4617 if (save_arg_vars[i] & (1 << reg)) {
4618 fprintf(fout, " u32 %s;\n",
4619 saved_arg_name(buf1, sizeof(buf1), i, reg + 1));
4625 // declare push-pop temporaries
4627 for (reg = 0; reg < 8; reg++) {
4628 if (regmask_pp & (1 << reg)) {
4629 fprintf(fout, " u32 pp_%s;\n", regs_r32[reg]);
4636 for (i = 0; i < 8; i++) {
4637 if (cond_vars & (1 << i)) {
4638 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
4645 fprintf(fout, " u32 tmp;\n");
4650 fprintf(fout, " u64 tmp64;\n");
4655 fprintf(fout, "\n");
4657 if (g_func_pp->is_vararg) {
4658 if (g_func_pp->argc_stack == 0)
4659 ferr(ops, "vararg func without stack args?\n");
4660 fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc);
4664 for (i = 0; i < opcnt; i++)
4666 if (g_labels[i] != NULL) {
4667 fprintf(fout, "\n%s:\n", g_labels[i]);
4670 delayed_flag_op = NULL;
4671 last_arith_dst = NULL;
4675 if (po->flags & OPF_RMD)
4680 #define assert_operand_cnt(n_) \
4681 if (po->operand_cnt != n_) \
4682 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
4684 // conditional/flag using op?
4685 if (po->flags & OPF_CC)
4691 // we go through all this trouble to avoid using parsed_flag_op,
4692 // which makes generated code much nicer
4693 if (delayed_flag_op != NULL)
4695 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op,
4696 po->pfo, po->pfo_inv);
4699 else if (last_arith_dst != NULL
4700 && (po->pfo == PFO_Z || po->pfo == PFO_S || po->pfo == PFO_P
4701 || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
4704 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
4705 out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv,
4706 last_arith_dst->lmod, buf3);
4709 else if (tmp_op != NULL) {
4710 // use preprocessed flag calc results
4711 if (!(tmp_op->pfomask & (1 << po->pfo)))
4712 ferr(po, "not prepared for pfo %d\n", po->pfo);
4714 // note: pfo_inv was not yet applied
4715 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
4716 po->pfo_inv ? "!" : "", parsed_flag_op_names[po->pfo]);
4719 ferr(po, "all methods of finding comparison failed\n");
4722 if (po->flags & OPF_JMP) {
4723 fprintf(fout, " if %s", buf1);
4725 else if (po->op == OP_RCL || po->op == OP_RCR
4726 || po->op == OP_ADC || po->op == OP_SBB)
4729 fprintf(fout, " cond_%s = %s;\n",
4730 parsed_flag_op_names[po->pfo], buf1);
4732 else if (po->flags & OPF_DATA) { // SETcc
4733 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
4734 fprintf(fout, " %s = %s;", buf2, buf1);
4737 ferr(po, "unhandled conditional op\n");
4741 pfomask = po->pfomask;
4743 if (po->flags & (OPF_REPZ|OPF_REPNZ)) {
4744 struct parsed_opr opr = {0,};
4747 opr.lmod = OPLM_DWORD;
4748 ret = try_resolve_const(i, &opr, opcnt * 7 + i, &uval);
4750 if (ret != 1 || uval == 0) {
4751 // we need initial flags for ecx=0 case..
4752 if (i > 0 && ops[i - 1].op == OP_XOR
4753 && IS(ops[i - 1].operand[0].name,
4754 ops[i - 1].operand[1].name))
4756 fprintf(fout, " cond_z = ");
4757 if (pfomask & (1 << PFO_C))
4758 fprintf(fout, "cond_c = ");
4759 fprintf(fout, "0;\n");
4761 else if (last_arith_dst != NULL) {
4762 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
4763 out_test_for_cc(buf1, sizeof(buf1), po, PFO_Z, 0,
4764 last_arith_dst->lmod, buf3);
4765 fprintf(fout, " cond_z = %s;\n", buf1);
4768 ferr(po, "missing initial ZF\n");
4775 assert_operand_cnt(2);
4776 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4777 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4778 default_cast_to(buf3, sizeof(buf3), &po->operand[0]);
4779 fprintf(fout, " %s = %s;", buf1,
4780 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4785 assert_operand_cnt(2);
4786 po->operand[1].lmod = OPLM_DWORD; // always
4787 fprintf(fout, " %s = %s;",
4788 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4789 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4794 assert_operand_cnt(2);
4795 fprintf(fout, " %s = %s;",
4796 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4797 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4801 assert_operand_cnt(2);
4802 switch (po->operand[1].lmod) {
4804 strcpy(buf3, "(s8)");
4807 strcpy(buf3, "(s16)");
4810 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
4812 fprintf(fout, " %s = %s;",
4813 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4814 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4819 assert_operand_cnt(2);
4820 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4821 fprintf(fout, " tmp = %s;",
4822 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0));
4823 fprintf(fout, " %s = %s;",
4824 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4825 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4826 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
4827 fprintf(fout, " %s = %stmp;",
4828 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
4829 default_cast_to(buf3, sizeof(buf3), &po->operand[1]));
4830 snprintf(g_comment, sizeof(g_comment), "xchg");
4834 assert_operand_cnt(1);
4835 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4836 fprintf(fout, " %s = ~%s;", buf1, buf1);
4840 assert_operand_cnt(2);
4841 fprintf(fout, " %s = (s32)%s >> 31;",
4842 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4843 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4844 strcpy(g_comment, "cdq");
4848 assert_operand_cnt(3);
4849 if (po->flags & OPF_REP) {
4854 fprintf(fout, " eax = %sesi; esi %c= %d;",
4855 lmod_cast_u_ptr(po, po->operand[0].lmod),
4856 (po->flags & OPF_DF) ? '-' : '+',
4857 lmod_bytes(po, po->operand[0].lmod));
4858 strcpy(g_comment, "lods");
4863 assert_operand_cnt(3);
4864 if (po->flags & OPF_REP) {
4865 fprintf(fout, " for (; ecx != 0; ecx--, edi %c= %d)\n",
4866 (po->flags & OPF_DF) ? '-' : '+',
4867 lmod_bytes(po, po->operand[0].lmod));
4868 fprintf(fout, " %sedi = eax;",
4869 lmod_cast_u_ptr(po, po->operand[0].lmod));
4870 strcpy(g_comment, "rep stos");
4873 fprintf(fout, " %sedi = eax; edi %c= %d;",
4874 lmod_cast_u_ptr(po, po->operand[0].lmod),
4875 (po->flags & OPF_DF) ? '-' : '+',
4876 lmod_bytes(po, po->operand[0].lmod));
4877 strcpy(g_comment, "stos");
4882 assert_operand_cnt(3);
4883 j = lmod_bytes(po, po->operand[0].lmod);
4884 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4885 l = (po->flags & OPF_DF) ? '-' : '+';
4886 if (po->flags & OPF_REP) {
4888 " for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n",
4891 " %sedi = %sesi;", buf1, buf1);
4892 strcpy(g_comment, "rep movs");
4895 fprintf(fout, " %sedi = %sesi; edi %c= %d; esi %c= %d;",
4896 buf1, buf1, l, j, l, j);
4897 strcpy(g_comment, "movs");
4902 // repe ~ repeat while ZF=1
4903 assert_operand_cnt(3);
4904 j = lmod_bytes(po, po->operand[0].lmod);
4905 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4906 l = (po->flags & OPF_DF) ? '-' : '+';
4907 if (po->flags & OPF_REP) {
4909 " for (; ecx != 0; ecx--) {\n");
4910 if (pfomask & (1 << PFO_C)) {
4913 " cond_c = %sesi < %sedi;\n", buf1, buf1);
4914 pfomask &= ~(1 << PFO_C);
4917 " cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n",
4918 buf1, buf1, l, j, l, j);
4920 " if (cond_z %s 0) break;\n",
4921 (po->flags & OPF_REPZ) ? "==" : "!=");
4924 snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
4925 (po->flags & OPF_REPZ) ? "e" : "ne");
4929 " cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;",
4930 buf1, buf1, l, j, l, j);
4931 strcpy(g_comment, "cmps");
4933 pfomask &= ~(1 << PFO_Z);
4934 last_arith_dst = NULL;
4935 delayed_flag_op = NULL;
4939 // only does ZF (for now)
4940 // repe ~ repeat while ZF=1
4941 assert_operand_cnt(3);
4942 j = lmod_bytes(po, po->operand[0].lmod);
4943 l = (po->flags & OPF_DF) ? '-' : '+';
4944 if (po->flags & OPF_REP) {
4946 " for (; ecx != 0; ecx--) {\n");
4948 " cond_z = (%seax == %sedi); edi %c= %d;\n",
4949 lmod_cast_u(po, po->operand[0].lmod),
4950 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4952 " if (cond_z %s 0) break;\n",
4953 (po->flags & OPF_REPZ) ? "==" : "!=");
4956 snprintf(g_comment, sizeof(g_comment), "rep%s scas",
4957 (po->flags & OPF_REPZ) ? "e" : "ne");
4960 fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;",
4961 lmod_cast_u(po, po->operand[0].lmod),
4962 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4963 strcpy(g_comment, "scas");
4965 pfomask &= ~(1 << PFO_Z);
4966 last_arith_dst = NULL;
4967 delayed_flag_op = NULL;
4970 // arithmetic w/flags
4973 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4976 assert_operand_cnt(2);
4977 fprintf(fout, " %s %s= %s;",
4978 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4980 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4981 output_std_flags(fout, po, &pfomask, buf1);
4982 last_arith_dst = &po->operand[0];
4983 delayed_flag_op = NULL;
4988 assert_operand_cnt(2);
4989 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4990 if (pfomask & (1 << PFO_C)) {
4991 if (po->operand[1].type == OPT_CONST) {
4992 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4993 j = po->operand[1].val;
4996 if (po->op == OP_SHL)
5000 fprintf(fout, " cond_c = (%s >> %d) & 1;\n",
5004 ferr(po, "zero shift?\n");
5008 pfomask &= ~(1 << PFO_C);
5010 fprintf(fout, " %s %s= %s;", buf1, op_to_c(po),
5011 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
5012 output_std_flags(fout, po, &pfomask, buf1);
5013 last_arith_dst = &po->operand[0];
5014 delayed_flag_op = NULL;
5018 assert_operand_cnt(2);
5019 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5020 fprintf(fout, " %s = %s%s >> %s;", buf1,
5021 lmod_cast_s(po, po->operand[0].lmod), buf1,
5022 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
5023 output_std_flags(fout, po, &pfomask, buf1);
5024 last_arith_dst = &po->operand[0];
5025 delayed_flag_op = NULL;
5029 assert_operand_cnt(3);
5030 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5031 l = lmod_bytes(po, po->operand[0].lmod) * 8;
5032 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5033 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
5034 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[2]);
5035 fprintf(fout, " %s >>= %s; %s |= %s << (%d - %s);",
5036 buf1, buf3, buf1, buf2, l, buf3);
5037 strcpy(g_comment, "shrd");
5038 output_std_flags(fout, po, &pfomask, buf1);
5039 last_arith_dst = &po->operand[0];
5040 delayed_flag_op = NULL;
5045 assert_operand_cnt(2);
5046 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5047 if (po->operand[1].type == OPT_CONST) {
5048 j = po->operand[1].val;
5049 j %= lmod_bytes(po, po->operand[0].lmod) * 8;
5050 fprintf(fout, po->op == OP_ROL ?
5051 " %s = (%s << %d) | (%s >> %d);" :
5052 " %s = (%s >> %d) | (%s << %d);",
5053 buf1, buf1, j, buf1,
5054 lmod_bytes(po, po->operand[0].lmod) * 8 - j);
5058 output_std_flags(fout, po, &pfomask, buf1);
5059 last_arith_dst = &po->operand[0];
5060 delayed_flag_op = NULL;
5065 assert_operand_cnt(2);
5066 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5067 l = lmod_bytes(po, po->operand[0].lmod) * 8;
5068 if (po->operand[1].type == OPT_CONST) {
5069 j = po->operand[1].val % l;
5071 ferr(po, "zero rotate\n");
5072 fprintf(fout, " tmp = (%s >> %d) & 1;\n",
5073 buf1, (po->op == OP_RCL) ? (l - j) : (j - 1));
5074 if (po->op == OP_RCL) {
5076 " %s = (%s << %d) | (cond_c << %d)",
5077 buf1, buf1, j, j - 1);
5079 fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j);
5083 " %s = (%s >> %d) | (cond_c << %d)",
5084 buf1, buf1, j, l - j);
5086 fprintf(fout, " | (%s << %d)", buf1, l + 1 - j);
5088 fprintf(fout, ";\n");
5089 fprintf(fout, " cond_c = tmp;");
5093 strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr");
5094 output_std_flags(fout, po, &pfomask, buf1);
5095 last_arith_dst = &po->operand[0];
5096 delayed_flag_op = NULL;
5100 assert_operand_cnt(2);
5101 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5102 if (IS(opr_name(po, 0), opr_name(po, 1))) {
5103 // special case for XOR
5104 if (pfomask & (1 << PFO_BE)) { // weird, but it happens..
5105 fprintf(fout, " cond_be = 1;\n");
5106 pfomask &= ~(1 << PFO_BE);
5108 fprintf(fout, " %s = 0;",
5109 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
5110 last_arith_dst = &po->operand[0];
5111 delayed_flag_op = NULL;
5117 assert_operand_cnt(2);
5118 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5119 if (pfomask & (1 << PFO_C)) {
5120 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
5121 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
5122 if (po->operand[0].lmod == OPLM_DWORD) {
5123 fprintf(fout, " tmp64 = (u64)%s + %s;\n", buf1, buf2);
5124 fprintf(fout, " cond_c = tmp64 >> 32;\n");
5125 fprintf(fout, " %s = (u32)tmp64;",
5126 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
5127 strcat(g_comment, "add64");
5130 fprintf(fout, " cond_c = ((u32)%s + %s) >> %d;\n",
5131 buf1, buf2, lmod_bytes(po, po->operand[0].lmod) * 8);
5132 fprintf(fout, " %s += %s;",
5133 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
5136 pfomask &= ~(1 << PFO_C);
5137 output_std_flags(fout, po, &pfomask, buf1);
5138 last_arith_dst = &po->operand[0];
5139 delayed_flag_op = NULL;
5145 assert_operand_cnt(2);
5146 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5147 if (pfomask & ~((1 << PFO_Z) | (1 << PFO_S))) {
5148 for (j = 0; j <= PFO_LE; j++) {
5149 if (!(pfomask & (1 << j)))
5151 if (j == PFO_Z || j == PFO_S)
5154 out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0);
5155 fprintf(fout, " cond_%s = %s;\n",
5156 parsed_flag_op_names[j], buf1);
5157 pfomask &= ~(1 << j);
5164 assert_operand_cnt(2);
5165 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5166 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5167 if (po->op == OP_SBB
5168 && IS(po->operand[0].name, po->operand[1].name))
5170 // avoid use of unitialized var
5171 fprintf(fout, " %s = -cond_c;", buf1);
5172 // carry remains what it was
5173 pfomask &= ~(1 << PFO_C);
5176 fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
5177 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
5179 output_std_flags(fout, po, &pfomask, buf1);
5180 last_arith_dst = &po->operand[0];
5181 delayed_flag_op = NULL;
5185 assert_operand_cnt(2);
5186 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
5187 fprintf(fout, " %s = %s ? __builtin_ffs(%s) - 1 : 0;",
5188 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
5190 output_std_flags(fout, po, &pfomask, buf1);
5191 last_arith_dst = &po->operand[0];
5192 delayed_flag_op = NULL;
5193 strcat(g_comment, "bsf");
5197 if (pfomask & ~(PFOB_S | PFOB_S | PFOB_C)) {
5198 for (j = 0; j <= PFO_LE; j++) {
5199 if (!(pfomask & (1 << j)))
5201 if (j == PFO_Z || j == PFO_S || j == PFO_C)
5204 out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0);
5205 fprintf(fout, " cond_%s = %s;\n",
5206 parsed_flag_op_names[j], buf1);
5207 pfomask &= ~(1 << j);
5213 if (pfomask & (1 << PFO_C))
5214 // carry is unaffected by inc/dec.. wtf?
5215 ferr(po, "carry propagation needed\n");
5217 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5218 if (po->operand[0].type == OPT_REG) {
5219 strcpy(buf2, po->op == OP_INC ? "++" : "--");
5220 fprintf(fout, " %s%s;", buf1, buf2);
5223 strcpy(buf2, po->op == OP_INC ? "+" : "-");
5224 fprintf(fout, " %s %s= 1;", buf1, buf2);
5226 output_std_flags(fout, po, &pfomask, buf1);
5227 last_arith_dst = &po->operand[0];
5228 delayed_flag_op = NULL;
5232 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5233 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
5234 fprintf(fout, " %s = -%s%s;", buf1,
5235 lmod_cast_s(po, po->operand[0].lmod), buf2);
5236 last_arith_dst = &po->operand[0];
5237 delayed_flag_op = NULL;
5238 if (pfomask & (1 << PFO_C)) {
5239 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
5240 pfomask &= ~(1 << PFO_C);
5245 if (po->operand_cnt == 2) {
5246 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5249 if (po->operand_cnt == 3)
5250 ferr(po, "TODO imul3\n");
5253 assert_operand_cnt(1);
5254 switch (po->operand[0].lmod) {
5256 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
5257 fprintf(fout, " tmp64 = %seax * %s%s;\n", buf1, buf1,
5258 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
5259 fprintf(fout, " edx = tmp64 >> 32;\n");
5260 fprintf(fout, " eax = tmp64;");
5263 strcpy(buf1, po->op == OP_IMUL ? "(s16)(s8)" : "(u16)(u8)");
5264 fprintf(fout, " LOWORD(eax) = %seax * %s;", buf1,
5265 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0],
5269 ferr(po, "TODO: unhandled mul type\n");
5272 last_arith_dst = NULL;
5273 delayed_flag_op = NULL;
5278 assert_operand_cnt(1);
5279 if (po->operand[0].lmod != OPLM_DWORD)
5280 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
5282 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
5283 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
5284 po->op == OP_IDIV));
5285 switch (po->operand[0].lmod) {
5287 if (po->flags & OPF_32BIT)
5288 snprintf(buf3, sizeof(buf3), "%seax", buf2);
5290 fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n");
5291 snprintf(buf3, sizeof(buf3), "%stmp64",
5292 (po->op == OP_IDIV) ? "(s64)" : "");
5294 if (po->operand[0].type == OPT_REG
5295 && po->operand[0].reg == xDX)
5297 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
5298 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
5301 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
5302 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
5306 ferr(po, "unhandled division type\n");
5308 last_arith_dst = NULL;
5309 delayed_flag_op = NULL;
5314 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5316 for (j = 0; j < 8; j++) {
5317 if (pfomask & (1 << j)) {
5318 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
5319 fprintf(fout, " cond_%s = %s;",
5320 parsed_flag_op_names[j], buf1);
5327 last_arith_dst = NULL;
5328 delayed_flag_op = po;
5332 // SETcc - should already be handled
5335 // note: we reuse OP_Jcc for SETcc, only flags differ
5337 fprintf(fout, "\n goto %s;", po->operand[0].name);
5341 fprintf(fout, " if (ecx == 0)\n");
5342 fprintf(fout, " goto %s;", po->operand[0].name);
5343 strcat(g_comment, "jecxz");
5347 assert_operand_cnt(1);
5348 last_arith_dst = NULL;
5349 delayed_flag_op = NULL;
5351 if (po->operand[0].type == OPT_REGMEM) {
5352 ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
5355 ferr(po, "parse failure for jmp '%s'\n",
5356 po->operand[0].name);
5357 fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
5360 else if (po->operand[0].type != OPT_LABEL)
5361 ferr(po, "unhandled jmp type\n");
5363 fprintf(fout, " goto %s;", po->operand[0].name);
5367 assert_operand_cnt(1);
5369 my_assert_not(pp, NULL);
5372 if (po->flags & OPF_CC) {
5373 // we treat conditional branch to another func
5374 // (yes such code exists..) as conditional tailcall
5376 fprintf(fout, " {\n");
5379 if (pp->is_fptr && !pp->is_arg) {
5380 fprintf(fout, "%s%s = %s;\n", buf3, pp->name,
5381 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
5383 if (pp->is_unresolved)
5384 fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
5385 buf3, asmfn, po->asmln, pp->name);
5388 fprintf(fout, "%s", buf3);
5389 if (strstr(pp->ret_type.name, "int64")) {
5390 if (po->flags & OPF_TAIL)
5391 ferr(po, "int64 and tail?\n");
5392 fprintf(fout, "tmp64 = ");
5394 else if (!IS(pp->ret_type.name, "void")) {
5395 if (po->flags & OPF_TAIL) {
5396 if (!IS(g_func_pp->ret_type.name, "void")) {
5397 fprintf(fout, "return ");
5398 if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
5399 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
5402 else if (regmask & (1 << xAX)) {
5403 fprintf(fout, "eax = ");
5404 if (pp->ret_type.is_ptr)
5405 fprintf(fout, "(u32)");
5409 if (pp->name[0] == 0)
5410 ferr(po, "missing pp->name\n");
5411 fprintf(fout, "%s%s(", pp->name,
5412 pp->has_structarg ? "_sa" : "");
5414 if (po->flags & OPF_ATAIL) {
5415 if (pp->argc_stack != g_func_pp->argc_stack
5416 || (pp->argc_stack > 0
5417 && pp->is_stdcall != g_func_pp->is_stdcall))
5418 ferr(po, "incompatible tailcall\n");
5419 if (g_func_pp->has_retreg)
5420 ferr(po, "TODO: retreg+tailcall\n");
5422 for (arg = j = 0; arg < pp->argc; arg++) {
5424 fprintf(fout, ", ");
5427 if (pp->arg[arg].type.is_ptr)
5428 snprintf(cast, sizeof(cast), "(%s)",
5429 pp->arg[arg].type.name);
5431 if (pp->arg[arg].reg != NULL) {
5432 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
5436 for (; j < g_func_pp->argc; j++)
5437 if (g_func_pp->arg[j].reg == NULL)
5439 fprintf(fout, "%sa%d", cast, j + 1);
5444 for (arg = 0; arg < pp->argc; arg++) {
5446 fprintf(fout, ", ");
5449 if (pp->arg[arg].type.is_ptr)
5450 snprintf(cast, sizeof(cast), "(%s)",
5451 pp->arg[arg].type.name);
5453 if (pp->arg[arg].reg != NULL) {
5454 if (pp->arg[arg].type.is_retreg)
5455 fprintf(fout, "&%s", pp->arg[arg].reg);
5457 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
5462 tmp_op = pp->arg[arg].datap;
5464 ferr(po, "parsed_op missing for arg%d\n", arg);
5466 if (tmp_op->flags & OPF_VAPUSH) {
5467 fprintf(fout, "ap");
5469 else if (tmp_op->p_argpass != 0) {
5470 fprintf(fout, "a%d", tmp_op->p_argpass);
5472 else if (tmp_op->p_argnum != 0) {
5473 fprintf(fout, "%s%s", cast,
5474 saved_arg_name(buf1, sizeof(buf1),
5475 tmp_op->p_arggrp, tmp_op->p_argnum));
5479 out_src_opr(buf1, sizeof(buf1),
5480 tmp_op, &tmp_op->operand[0], cast, 0));
5484 fprintf(fout, ");");
5486 if (strstr(pp->ret_type.name, "int64")) {
5487 fprintf(fout, "\n");
5488 fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3);
5489 fprintf(fout, "%seax = tmp64;", buf3);
5492 if (pp->is_unresolved) {
5493 snprintf(buf2, sizeof(buf2), " unresolved %dreg",
5495 strcat(g_comment, buf2);
5498 if (po->flags & OPF_TAIL) {
5500 if (i == opcnt - 1 || pp->is_noreturn)
5502 else if (IS(pp->ret_type.name, "void"))
5504 else if (IS(g_func_pp->ret_type.name, "void"))
5506 // else already handled as 'return f()'
5509 if (!IS(g_func_pp->ret_type.name, "void")) {
5510 ferr(po, "int func -> void func tailcall?\n");
5513 fprintf(fout, "\n%sreturn;", buf3);
5514 strcat(g_comment, " ^ tailcall");
5518 strcat(g_comment, " tailcall");
5520 if (pp->is_noreturn)
5521 strcat(g_comment, " noreturn");
5522 if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0)
5523 strcat(g_comment, " argframe");
5524 if (po->flags & OPF_CC)
5525 strcat(g_comment, " cond");
5527 if (po->flags & OPF_CC)
5528 fprintf(fout, "\n }");
5530 delayed_flag_op = NULL;
5531 last_arith_dst = NULL;
5535 if (g_func_pp->is_vararg)
5536 fprintf(fout, " va_end(ap);\n");
5537 if (g_func_pp->has_retreg) {
5538 for (arg = 0; arg < g_func_pp->argc; arg++)
5539 if (g_func_pp->arg[arg].type.is_retreg)
5540 fprintf(fout, " *r_%s = %s;\n",
5541 g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg);
5544 if (IS(g_func_pp->ret_type.name, "void")) {
5545 if (i != opcnt - 1 || label_pending)
5546 fprintf(fout, " return;");
5548 else if (g_func_pp->ret_type.is_ptr) {
5549 fprintf(fout, " return (%s)eax;",
5550 g_func_pp->ret_type.name);
5552 else if (IS(g_func_pp->ret_type.name, "__int64"))
5553 fprintf(fout, " return ((u64)edx << 32) | eax;");
5555 fprintf(fout, " return eax;");
5557 last_arith_dst = NULL;
5558 delayed_flag_op = NULL;
5562 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
5563 if (po->p_argnum != 0) {
5564 // special case - saved func arg
5565 fprintf(fout, " %s = %s;",
5566 saved_arg_name(buf2, sizeof(buf2),
5567 po->p_arggrp, po->p_argnum), buf1);
5570 else if (po->flags & OPF_RSAVE) {
5571 fprintf(fout, " s_%s = %s;", buf1, buf1);
5574 else if (po->flags & OPF_PPUSH) {
5576 ferr_assert(po, tmp_op != NULL);
5577 out_dst_opr(buf2, sizeof(buf2), po, &tmp_op->operand[0]);
5578 fprintf(fout, " pp_%s = %s;", buf2, buf1);
5581 else if (g_func_pp->is_userstack) {
5582 fprintf(fout, " *(--esp) = %s;", buf1);
5585 if (!(g_ida_func_attr & IDAFA_NORETURN))
5586 ferr(po, "stray push encountered\n");
5591 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5592 if (po->flags & OPF_RSAVE) {
5593 fprintf(fout, " %s = s_%s;", buf1, buf1);
5596 else if (po->flags & OPF_PPUSH) {
5598 ferr_assert(po, po->datap == NULL);
5599 fprintf(fout, " %s = pp_%s;", buf1, buf1);
5602 else if (po->datap != NULL) {
5605 fprintf(fout, " %s = %s;", buf1,
5606 out_src_opr(buf2, sizeof(buf2),
5607 tmp_op, &tmp_op->operand[0],
5608 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
5611 else if (g_func_pp->is_userstack) {
5612 fprintf(fout, " %s = *esp++;", buf1);
5616 ferr(po, "stray pop encountered\n");
5625 strcpy(g_comment, "(emms)");
5630 ferr(po, "unhandled op type %d, flags %x\n",
5635 if (g_comment[0] != 0) {
5636 char *p = g_comment;
5637 while (my_isblank(*p))
5639 fprintf(fout, " // %s", p);
5644 fprintf(fout, "\n");
5646 // some sanity checking
5647 if (po->flags & OPF_REP) {
5648 if (po->op != OP_STOS && po->op != OP_MOVS
5649 && po->op != OP_CMPS && po->op != OP_SCAS)
5650 ferr(po, "unexpected rep\n");
5651 if (!(po->flags & (OPF_REPZ|OPF_REPNZ))
5652 && (po->op == OP_CMPS || po->op == OP_SCAS))
5653 ferr(po, "cmps/scas with plain rep\n");
5655 if ((po->flags & (OPF_REPZ|OPF_REPNZ))
5656 && po->op != OP_CMPS && po->op != OP_SCAS)
5657 ferr(po, "unexpected repz/repnz\n");
5660 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
5662 // see is delayed flag stuff is still valid
5663 if (delayed_flag_op != NULL && delayed_flag_op != po) {
5664 if (is_any_opr_modified(delayed_flag_op, po, 0))
5665 delayed_flag_op = NULL;
5668 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
5669 if (is_opr_modified(last_arith_dst, po))
5670 last_arith_dst = NULL;
5676 if (g_stack_fsz && !g_stack_frame_used)
5677 fprintf(fout, " (void)sf;\n");
5679 fprintf(fout, "}\n\n");
5681 gen_x_cleanup(opcnt);
5684 static void gen_x_cleanup(int opcnt)
5688 for (i = 0; i < opcnt; i++) {
5689 struct label_ref *lr, *lr_del;
5691 lr = g_label_refs[i].next;
5692 while (lr != NULL) {
5697 g_label_refs[i].i = -1;
5698 g_label_refs[i].next = NULL;
5700 if (ops[i].op == OP_CALL) {
5702 proto_release(ops[i].pp);
5708 struct func_proto_dep;
5710 struct func_prototype {
5715 int has_ret:3; // -1, 0, 1: unresolved, no, yes
5716 unsigned int dep_resolved:1;
5717 unsigned int is_stdcall:1;
5718 struct func_proto_dep *dep_func;
5720 const struct parsed_proto *pp; // seed pp, if any
5723 struct func_proto_dep {
5725 struct func_prototype *proto;
5726 int regmask_live; // .. at the time of call
5727 unsigned int ret_dep:1; // return from this is caller's return
5730 static struct func_prototype *hg_fp;
5731 static int hg_fp_cnt;
5733 static struct scanned_var {
5735 enum opr_lenmod lmod;
5736 unsigned int is_seeded:1;
5737 unsigned int is_c_str:1;
5738 const struct parsed_proto *pp; // seed pp, if any
5740 static int hg_var_cnt;
5742 static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
5745 static struct func_proto_dep *hg_fp_find_dep(struct func_prototype *fp,
5750 for (i = 0; i < fp->dep_func_cnt; i++)
5751 if (IS(fp->dep_func[i].name, name))
5752 return &fp->dep_func[i];
5757 static void hg_fp_add_dep(struct func_prototype *fp, const char *name)
5760 if (hg_fp_find_dep(fp, name))
5763 if ((fp->dep_func_cnt & 0xff) == 0) {
5764 fp->dep_func = realloc(fp->dep_func,
5765 sizeof(fp->dep_func[0]) * (fp->dep_func_cnt + 0x100));
5766 my_assert_not(fp->dep_func, NULL);
5767 memset(&fp->dep_func[fp->dep_func_cnt], 0,
5768 sizeof(fp->dep_func[0]) * 0x100);
5770 fp->dep_func[fp->dep_func_cnt].name = strdup(name);
5774 static int hg_fp_cmp_name(const void *p1_, const void *p2_)
5776 const struct func_prototype *p1 = p1_, *p2 = p2_;
5777 return strcmp(p1->name, p2->name);
5781 static int hg_fp_cmp_id(const void *p1_, const void *p2_)
5783 const struct func_prototype *p1 = p1_, *p2 = p2_;
5784 return p1->id - p2->id;
5788 // recursive register dep pass
5789 // - track saved regs (part 2)
5790 // - try to figure out arg-regs
5791 // - calculate reg deps
5792 static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits,
5793 struct func_prototype *fp, int regmask_save, int regmask_dst,
5794 int *regmask_dep, int *has_ret)
5796 struct func_proto_dep *dep;
5797 struct parsed_op *po;
5798 int from_caller = 0;
5804 for (; i < opcnt; i++)
5806 if (cbits[i >> 3] & (1 << (i & 7)))
5808 cbits[i >> 3] |= (1 << (i & 7));
5812 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
5813 if (po->btj != NULL) {
5815 for (j = 0; j < po->btj->count; j++) {
5816 check_i(po, po->btj->d[j].bt_i);
5817 gen_hdr_dep_pass(po->btj->d[j].bt_i, opcnt, cbits, fp,
5818 regmask_save, regmask_dst, regmask_dep, has_ret);
5823 check_i(po, po->bt_i);
5824 if (po->flags & OPF_CJMP) {
5825 gen_hdr_dep_pass(po->bt_i, opcnt, cbits, fp,
5826 regmask_save, regmask_dst, regmask_dep, has_ret);
5834 if (po->flags & OPF_FARG)
5835 /* (just calculate register deps) */;
5836 else if (po->op == OP_PUSH && po->operand[0].type == OPT_REG)
5838 reg = po->operand[0].reg;
5840 ferr(po, "reg not set for push?\n");
5842 if (po->flags & OPF_RSAVE) {
5843 regmask_save |= 1 << reg;
5846 if (po->flags & OPF_DONE)
5850 ret = scan_for_pop(i + 1, opcnt,
5851 po->operand[0].name, i + opcnt * 2, 0, &depth, 0);
5853 regmask_save |= 1 << reg;
5854 po->flags |= OPF_RMD;
5855 scan_for_pop(i + 1, opcnt,
5856 po->operand[0].name, i + opcnt * 3, 0, &depth, 1);
5860 else if (po->flags & OPF_RMD)
5862 else if (po->op == OP_CALL) {
5863 po->regmask_dst |= 1 << xAX;
5865 dep = hg_fp_find_dep(fp, po->operand[0].name);
5867 dep->regmask_live = regmask_save | regmask_dst;
5869 else if (po->op == OP_RET) {
5870 if (po->operand_cnt > 0) {
5872 if (fp->argc_stack >= 0
5873 && fp->argc_stack != po->operand[0].val / 4)
5874 ferr(po, "ret mismatch? (%d)\n", fp->argc_stack * 4);
5875 fp->argc_stack = po->operand[0].val / 4;
5879 if (*has_ret != 0 && (po->flags & OPF_TAIL)) {
5880 if (po->op == OP_CALL) {
5885 struct parsed_opr opr = { 0, };
5890 ret = resolve_origin(i, &opr, i + opcnt * 4, &j, &from_caller);
5893 if (ret == -1 && from_caller) {
5894 // unresolved eax - probably void func
5898 if (ops[j].op == OP_CALL) {
5899 dep = hg_fp_find_dep(fp, po->operand[0].name);
5910 l = regmask_save | regmask_dst;
5911 if (g_bp_frame && !(po->flags & OPF_EBP_S))
5914 l = po->regmask_src & ~l;
5917 fnote(po, "dep |= %04x, dst %04x, save %04x (f %x)\n",
5918 l, regmask_dst, regmask_save, po->flags);
5921 regmask_dst |= po->regmask_dst;
5923 if (po->flags & OPF_TAIL)
5928 static void gen_hdr(const char *funcn, int opcnt)
5930 int save_arg_vars[MAX_ARG_GRP] = { 0, };
5931 unsigned char cbits[MAX_OPS / 8];
5932 const struct parsed_proto *pp_c;
5933 struct parsed_proto *pp;
5934 struct func_prototype *fp;
5935 struct parsed_data *pd;
5936 struct parsed_op *po;
5937 const char *tmpname;
5938 int regmask_dummy = 0;
5940 int max_bp_offset = 0;
5944 if ((hg_fp_cnt & 0xff) == 0) {
5945 hg_fp = realloc(hg_fp, sizeof(hg_fp[0]) * (hg_fp_cnt + 0x100));
5946 my_assert_not(hg_fp, NULL);
5947 memset(hg_fp + hg_fp_cnt, 0, sizeof(hg_fp[0]) * 0x100);
5950 fp = &hg_fp[hg_fp_cnt];
5951 snprintf(fp->name, sizeof(fp->name), "%s", funcn);
5953 fp->argc_stack = -1;
5956 // perhaps already in seed header?
5957 fp->pp = proto_parse(g_fhdr, funcn, 1);
5958 if (fp->pp != NULL) {
5959 fp->argc_stack = fp->pp->argc_stack;
5960 fp->is_stdcall = fp->pp->is_stdcall;
5961 fp->regmask_dep = get_pp_arg_regmask(fp->pp);
5962 fp->has_ret = !IS(fp->pp->ret_type.name, "void");
5966 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
5967 g_stack_frame_used = 0;
5970 // - handle ebp/esp frame, remove ops related to it
5971 scan_prologue_epilogue(opcnt);
5975 // - resolve all branches
5976 for (i = 0; i < opcnt; i++)
5982 if (po->flags & (OPF_RMD|OPF_DONE))
5985 if (po->op == OP_CALL) {
5986 tmpname = opr_name(po, 0);
5988 if (po->operand[0].type == OPT_LABEL) {
5989 hg_fp_add_dep(fp, tmpname);
5991 // perhaps a call to already known func?
5992 pp_c = proto_parse(g_fhdr, tmpname, 1);
5994 pp = proto_clone(pp_c);
5996 else if (po->datap != NULL) {
5997 pp = calloc(1, sizeof(*pp));
5998 my_assert_not(pp, NULL);
6000 ret = parse_protostr(po->datap, pp);
6002 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
6006 if (pp != NULL && pp->is_noreturn)
6007 po->flags |= OPF_TAIL;
6013 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
6016 if (po->operand[0].type == OPT_REGMEM) {
6017 pd = try_resolve_jumptab(i, opcnt);
6025 for (l = 0; l < opcnt; l++) {
6026 if (g_labels[l] != NULL
6027 && IS(po->operand[0].name, g_labels[l]))
6029 add_label_ref(&g_label_refs[l], i);
6035 if (po->bt_i != -1 || (po->flags & OPF_RMD))
6038 if (po->operand[0].type == OPT_LABEL)
6042 ferr(po, "unhandled branch\n");
6046 po->flags |= OPF_TAIL;
6047 if (i > 0 && ops[i - 1].op == OP_POP)
6048 po->flags |= OPF_ATAIL;
6053 // - remove dead labels
6054 // - handle push <const>/pop pairs
6055 for (i = 0; i < opcnt; i++)
6057 if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
6063 if (po->flags & (OPF_RMD|OPF_DONE))
6066 if (po->op == OP_PUSH && po->operand[0].type == OPT_CONST)
6067 scan_for_pop_const(i, opcnt, ®mask_dummy);
6071 // - process trivial calls
6072 for (i = 0; i < opcnt; i++)
6075 if (po->flags & (OPF_RMD|OPF_DONE))
6078 if (po->op == OP_CALL)
6080 pp = process_call_early(i, opcnt, &j);
6082 if (!(po->flags & OPF_ATAIL))
6083 // since we know the args, try to collect them
6084 if (collect_call_args_early(po, i, pp, ®mask_dummy) != 0)
6090 // commit esp adjust
6091 ops[j].flags |= OPF_RMD;
6092 if (ops[j].op != OP_POP)
6093 patch_esp_adjust(&ops[j], pp->argc_stack * 4);
6095 ops[j].flags |= OPF_DONE;
6098 po->flags |= OPF_DONE;
6104 // - track saved regs (simple)
6106 for (i = 0; i < opcnt; i++)
6109 if (po->flags & (OPF_RMD|OPF_DONE))
6112 if (po->op == OP_PUSH && po->operand[0].type == OPT_REG)
6114 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
6116 // regmask_save |= 1 << po->operand[0].reg; // do it later
6117 po->flags |= OPF_RSAVE | OPF_RMD | OPF_DONE;
6118 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, OPF_RMD);
6121 else if (po->op == OP_CALL && !(po->flags & OPF_DONE))
6123 pp = process_call(i, opcnt);
6125 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
6126 // since we know the args, collect them
6127 ret = collect_call_args(po, i, pp, ®mask_dummy, save_arg_vars,
6134 memset(cbits, 0, sizeof(cbits));
6138 gen_hdr_dep_pass(0, opcnt, cbits, fp, 0, 0, ®mask_dep, &has_ret);
6140 // find unreachable code - must be fixed in IDA
6141 for (i = 0; i < opcnt; i++)
6143 if (cbits[i >> 3] & (1 << (i & 7)))
6146 if (ops[i].op != OP_NOP)
6147 ferr(&ops[i], "unreachable code\n");
6150 if (has_ret == -1 && (regmask_dep & (1 << xAX)))
6153 for (i = 0; i < g_eqcnt; i++) {
6154 if (g_eqs[i].offset > max_bp_offset && g_eqs[i].offset < 4*32)
6155 max_bp_offset = g_eqs[i].offset;
6158 if (fp->argc_stack < 0) {
6159 max_bp_offset = (max_bp_offset + 3) & ~3;
6160 fp->argc_stack = max_bp_offset / 4;
6161 if ((g_ida_func_attr & IDAFA_BP_FRAME) && fp->argc_stack > 0)
6165 fp->regmask_dep = regmask_dep & ~(1 << xSP);
6166 fp->has_ret = has_ret;
6168 printf("// has_ret %d, regmask_dep %x\n",
6169 fp->has_ret, fp->regmask_dep);
6170 output_hdr_fp(stdout, fp, 1);
6171 if (IS(funcn, "sub_100073FD")) exit(1);
6174 gen_x_cleanup(opcnt);
6177 static void hg_fp_resolve_deps(struct func_prototype *fp)
6179 struct func_prototype fp_s;
6183 // this thing is recursive, so mark first..
6184 fp->dep_resolved = 1;
6186 for (i = 0; i < fp->dep_func_cnt; i++) {
6187 strcpy(fp_s.name, fp->dep_func[i].name);
6188 fp->dep_func[i].proto = bsearch(&fp_s, hg_fp, hg_fp_cnt,
6189 sizeof(hg_fp[0]), hg_fp_cmp_name);
6190 if (fp->dep_func[i].proto != NULL) {
6191 if (!fp->dep_func[i].proto->dep_resolved)
6192 hg_fp_resolve_deps(fp->dep_func[i].proto);
6194 dep = ~fp->dep_func[i].regmask_live
6195 & fp->dep_func[i].proto->regmask_dep;
6196 fp->regmask_dep |= dep;
6197 // printf("dep %s %s |= %x\n", fp->name,
6198 // fp->dep_func[i].name, dep);
6200 if (fp->has_ret == -1)
6201 fp->has_ret = fp->dep_func[i].proto->has_ret;
6206 static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
6209 const struct parsed_proto *pp;
6210 char *p, namebuf[NAMELEN];
6216 for (; count > 0; count--, fp++) {
6217 if (fp->has_ret == -1)
6218 fprintf(fout, "// ret unresolved\n");
6220 fprintf(fout, "// dep:");
6221 for (j = 0; j < fp->dep_func_cnt; j++) {
6222 fprintf(fout, " %s/", fp->dep_func[j].name);
6223 if (fp->dep_func[j].proto != NULL)
6224 fprintf(fout, "%04x/%d", fp->dep_func[j].proto->regmask_dep,
6225 fp->dep_func[j].proto->has_ret);
6227 fprintf(fout, "\n");
6230 p = strchr(fp->name, '@');
6232 memcpy(namebuf, fp->name, p - fp->name);
6233 namebuf[p - fp->name] = 0;
6241 pp = proto_parse(g_fhdr, name, 1);
6242 if (pp != NULL && pp->is_include)
6245 if (fp->pp != NULL) {
6246 // part of seed, output later
6250 regmask_dep = fp->regmask_dep;
6251 argc_stack = fp->argc_stack;
6253 fprintf(fout, "%-5s", fp->pp ? fp->pp->ret_type.name :
6254 (fp->has_ret ? "int" : "void"));
6255 if (regmask_dep && (fp->is_stdcall || argc_stack == 0)
6256 && (regmask_dep & ~((1 << xCX) | (1 << xDX))) == 0)
6258 fprintf(fout, " __fastcall ");
6259 if (!(regmask_dep & (1 << xDX)) && argc_stack == 0)
6265 else if (regmask_dep && !fp->is_stdcall) {
6266 fprintf(fout, "/*__usercall*/ ");
6268 else if (regmask_dep) {
6269 fprintf(fout, "/*__userpurge*/ ");
6271 else if (fp->is_stdcall)
6272 fprintf(fout, " __stdcall ");
6274 fprintf(fout, " __cdecl ");
6276 fprintf(fout, "%s(", name);
6279 for (j = 0; j < xSP; j++) {
6280 if (regmask_dep & (1 << j)) {
6283 fprintf(fout, ", ");
6285 fprintf(fout, "%s", fp->pp->arg[arg - 1].type.name);
6287 fprintf(fout, "int");
6288 fprintf(fout, " a%d/*<%s>*/", arg, regs_r32[j]);
6292 for (j = 0; j < argc_stack; j++) {
6295 fprintf(fout, ", ");
6296 if (fp->pp != NULL) {
6297 fprintf(fout, "%s", fp->pp->arg[arg - 1].type.name);
6298 if (!fp->pp->arg[arg - 1].type.is_ptr)
6302 fprintf(fout, "int ");
6303 fprintf(fout, "a%d", arg);
6306 fprintf(fout, ");\n");
6310 static void output_hdr(FILE *fout)
6312 static const char *lmod_c_names[] = {
6313 [OPLM_UNSPEC] = "???",
6314 [OPLM_BYTE] = "uint8_t",
6315 [OPLM_WORD] = "uint16_t",
6316 [OPLM_DWORD] = "uint32_t",
6317 [OPLM_QWORD] = "uint64_t",
6319 const struct scanned_var *var;
6320 char line[256] = { 0, };
6324 qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_name);
6325 for (i = 0; i < hg_fp_cnt; i++)
6326 hg_fp_resolve_deps(&hg_fp[i]);
6328 // note: messes up .proto ptr, don't use
6329 //qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_id);
6332 for (i = 0; i < hg_var_cnt; i++) {
6335 if (var->pp != NULL)
6338 else if (var->is_c_str)
6339 fprintf(fout, "extern %-8s %s[];", "char", var->name);
6341 fprintf(fout, "extern %-8s %s;",
6342 lmod_c_names[var->lmod], var->name);
6345 fprintf(fout, " // seeded");
6346 fprintf(fout, "\n");
6349 fprintf(fout, "\n");
6351 // output function prototypes
6352 output_hdr_fp(fout, hg_fp, hg_fp_cnt);
6355 fprintf(fout, "\n// - seed -\n");
6358 while (fgets(line, sizeof(line), g_fhdr))
6359 fwrite(line, 1, strlen(line), fout);
6362 // read a line, truncating it if it doesn't fit
6363 static char *my_fgets(char *s, size_t size, FILE *stream)
6373 ret = fgets(s, size, stream);
6374 if (ret != NULL && p >= 0 && s[p] != 0 && s[p] != '\n') {
6375 p = sizeof(buf) - 2;
6378 ret2 = fgets(buf, sizeof(buf), stream);
6380 while (ret2 != NULL && buf[p] != 0 && buf[p] != '\n');
6386 // '=' needs special treatment
6388 static char *next_word_s(char *w, size_t wsize, char *s)
6397 for (i = 1; i < wsize - 1; i++) {
6399 printf("warning: missing closing quote: \"%s\"\n", s);
6408 for (; i < wsize - 1; i++) {
6409 if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
6415 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
6416 printf("warning: '%s' truncated\n", w);
6421 static void scan_variables(FILE *fasm)
6423 struct scanned_var *var;
6424 char line[256] = { 0, };
6432 // skip to next data section
6433 while (my_fgets(line, sizeof(line), fasm))
6438 if (*p == 0 || *p == ';')
6441 p = sskip(next_word_s(words[0], sizeof(words[0]), p));
6442 if (*p == 0 || *p == ';')
6445 if (*p != 's' || !IS_START(p, "segment para public"))
6451 if (p == NULL || !IS_START(p, "segment para public"))
6455 if (!IS_START(p, "'DATA'"))
6459 while (my_fgets(line, sizeof(line), fasm))
6468 if (*p == 0 || *p == ';')
6471 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
6472 words[wordc][0] = 0;
6473 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
6474 if (*p == 0 || *p == ';') {
6480 if (wordc == 2 && IS(words[1], "ends"))
6485 if ((hg_var_cnt & 0xff) == 0) {
6486 hg_vars = realloc(hg_vars, sizeof(hg_vars[0])
6487 * (hg_var_cnt + 0x100));
6488 my_assert_not(hg_vars, NULL);
6489 memset(hg_vars + hg_var_cnt, 0, sizeof(hg_vars[0]) * 0x100);
6492 var = &hg_vars[hg_var_cnt++];
6493 snprintf(var->name, sizeof(var->name), "%s", words[0]);
6495 // maybe already in seed header?
6496 var->pp = proto_parse(g_fhdr, var->name, 1);
6497 if (var->pp != NULL) {
6498 if (var->pp->is_fptr) {
6499 var->lmod = OPLM_DWORD;
6502 else if (var->pp->is_func)
6504 else if (!guess_lmod_from_c_type(&var->lmod, &var->pp->type))
6505 aerr("unhandled C type '%s' for '%s'\n",
6506 var->pp->type.name, var->name);
6512 if (IS(words[1], "dd"))
6513 var->lmod = OPLM_DWORD;
6514 else if (IS(words[1], "dw"))
6515 var->lmod = OPLM_WORD;
6516 else if (IS(words[1], "db")) {
6517 var->lmod = OPLM_BYTE;
6518 if (wordc >= 3 && (l = strlen(words[2])) > 4) {
6519 if (words[2][0] == '\'' && IS(words[2] + l - 2, ",0"))
6523 else if (IS(words[1], "dq"))
6524 var->lmod = OPLM_QWORD;
6525 //else if (IS(words[1], "dt"))
6527 aerr("type '%s' not known\n", words[1]);
6535 static void set_label(int i, const char *name)
6541 p = strchr(name, ':');
6545 if (g_labels[i] != NULL && !IS_START(g_labels[i], "algn_"))
6546 aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
6547 g_labels[i] = realloc(g_labels[i], len + 1);
6548 my_assert_not(g_labels[i], NULL);
6549 memcpy(g_labels[i], name, len);
6550 g_labels[i][len] = 0;
6559 static struct chunk_item *func_chunks;
6560 static int func_chunk_cnt;
6561 static int func_chunk_alloc;
6563 static void add_func_chunk(FILE *fasm, const char *name, int line)
6565 if (func_chunk_cnt >= func_chunk_alloc) {
6566 func_chunk_alloc *= 2;
6567 func_chunks = realloc(func_chunks,
6568 func_chunk_alloc * sizeof(func_chunks[0]));
6569 my_assert_not(func_chunks, NULL);
6571 func_chunks[func_chunk_cnt].fptr = ftell(fasm);
6572 func_chunks[func_chunk_cnt].name = strdup(name);
6573 func_chunks[func_chunk_cnt].asmln = line;
6577 static int cmp_chunks(const void *p1, const void *p2)
6579 const struct chunk_item *c1 = p1, *c2 = p2;
6580 return strcmp(c1->name, c2->name);
6583 static int cmpstringp(const void *p1, const void *p2)
6585 return strcmp(*(char * const *)p1, *(char * const *)p2);
6588 static void scan_ahead(FILE *fasm)
6598 oldpos = ftell(fasm);
6601 while (my_fgets(line, sizeof(line), fasm))
6612 // get rid of random tabs
6613 for (i = 0; line[i] != 0; i++)
6614 if (line[i] == '\t')
6617 if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
6620 next_word(words[0], sizeof(words[0]), p);
6621 if (words[0][0] == 0)
6622 aerr("missing name for func chunk?\n");
6624 add_func_chunk(fasm, words[0], asmln);
6626 else if (IS_START(p, "; sctend"))
6632 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
6633 words[wordc][0] = 0;
6634 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
6635 if (*p == 0 || *p == ';') {
6641 if (wordc == 2 && IS(words[1], "ends"))
6645 fseek(fasm, oldpos, SEEK_SET);
6649 int main(int argc, char *argv[])
6651 FILE *fout, *fasm, *frlist;
6652 struct parsed_data *pd = NULL;
6654 char **rlist = NULL;
6656 int rlist_alloc = 0;
6657 int func_chunks_used = 0;
6658 int func_chunks_sorted = 0;
6659 int func_chunk_i = -1;
6660 long func_chunk_ret = 0;
6661 int func_chunk_ret_ln = 0;
6662 int scanned_ahead = 0;
6664 char words[20][256];
6665 enum opr_lenmod lmod;
6666 char *sctproto = NULL;
6668 int pending_endp = 0;
6670 int skip_warned = 0;
6683 for (arg = 1; arg < argc; arg++) {
6684 if (IS(argv[arg], "-v"))
6686 else if (IS(argv[arg], "-rf"))
6687 g_allow_regfunc = 1;
6688 else if (IS(argv[arg], "-m"))
6690 else if (IS(argv[arg], "-hdr"))
6691 g_header_mode = g_quiet_pp = g_allow_regfunc = 1;
6696 if (argc < arg + 3) {
6697 printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdr.h> [rlist]*\n"
6698 "%s -hdr <out.h> <.asm> <seed.h> [rlist]*\n"
6700 " -hdr - header generation mode\n"
6701 " -rf - allow unannotated indirect calls\n"
6702 " -m - allow multiple .text sections\n"
6703 "[rlist] is a file with function names to skip,"
6711 asmfn = argv[arg++];
6712 fasm = fopen(asmfn, "r");
6713 my_assert_not(fasm, NULL);
6715 hdrfn = argv[arg++];
6716 g_fhdr = fopen(hdrfn, "r");
6717 my_assert_not(g_fhdr, NULL);
6720 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
6721 my_assert_not(rlist, NULL);
6722 // needs special handling..
6723 rlist[rlist_len++] = "__alloca_probe";
6725 func_chunk_alloc = 32;
6726 func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
6727 my_assert_not(func_chunks, NULL);
6729 memset(words, 0, sizeof(words));
6731 for (; arg < argc; arg++) {
6732 frlist = fopen(argv[arg], "r");
6733 my_assert_not(frlist, NULL);
6735 while (my_fgets(line, sizeof(line), frlist)) {
6737 if (*p == 0 || *p == ';')
6740 if (IS_START(p, "#if 0")
6741 || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
6745 else if (IS_START(p, "#endif"))
6752 p = next_word(words[0], sizeof(words[0]), p);
6753 if (words[0][0] == 0)
6756 if (rlist_len >= rlist_alloc) {
6757 rlist_alloc = rlist_alloc * 2 + 64;
6758 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
6759 my_assert_not(rlist, NULL);
6761 rlist[rlist_len++] = strdup(words[0]);
6770 qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
6772 fout = fopen(argv[arg_out], "w");
6773 my_assert_not(fout, NULL);
6776 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
6777 my_assert_not(g_eqs, NULL);
6779 for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
6780 g_label_refs[i].i = -1;
6781 g_label_refs[i].next = NULL;
6785 scan_variables(fasm);
6787 while (my_fgets(line, sizeof(line), fasm))
6796 // get rid of random tabs
6797 for (i = 0; line[i] != 0; i++)
6798 if (line[i] == '\t')
6803 if (p[2] == '=' && IS_START(p, "; =============== S U B"))
6804 goto do_pending_endp; // eww..
6806 if (p[2] == 'A' && IS_START(p, "; Attributes:"))
6808 static const char *attrs[] = {
6817 // parse IDA's attribute-list comment
6818 g_ida_func_attr = 0;
6821 for (; *p != 0; p = sskip(p)) {
6822 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
6823 if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
6824 g_ida_func_attr |= 1 << i;
6825 p += strlen(attrs[i]);
6829 if (i == ARRAY_SIZE(attrs)) {
6830 anote("unparsed IDA attr: %s\n", p);
6833 if (IS(attrs[i], "fpd=")) {
6834 p = next_word(words[0], sizeof(words[0]), p);
6839 else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
6842 next_word(words[0], sizeof(words[0]), p);
6843 if (words[0][0] == 0)
6844 aerr("missing name for func chunk?\n");
6846 if (!scanned_ahead) {
6847 add_func_chunk(fasm, words[0], asmln);
6848 func_chunks_sorted = 0;
6851 else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
6853 if (func_chunk_i >= 0) {
6854 if (func_chunk_i < func_chunk_cnt
6855 && IS(func_chunks[func_chunk_i].name, g_func))
6857 // move on to next chunk
6858 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
6860 aerr("seek failed for '%s' chunk #%d\n",
6861 g_func, func_chunk_i);
6862 asmln = func_chunks[func_chunk_i].asmln;
6866 if (func_chunk_ret == 0)
6867 aerr("no return from chunk?\n");
6868 fseek(fasm, func_chunk_ret, SEEK_SET);
6869 asmln = func_chunk_ret_ln;
6875 else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
6876 func_chunks_used = 1;
6878 if (IS_START(g_func, "sub_")) {
6879 unsigned long addr = strtoul(p, NULL, 16);
6880 unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
6881 if (addr > f_addr && !scanned_ahead) {
6882 anote("scan_ahead caused by '%s', addr %lx\n",
6886 func_chunks_sorted = 0;
6894 for (i = wordc; i < ARRAY_SIZE(words); i++)
6896 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
6897 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
6898 if (*p == 0 || *p == ';') {
6903 if (*p != 0 && *p != ';')
6904 aerr("too many words\n");
6906 // alow asm patches in comments
6908 if (IS_START(p, "; sctpatch:")) {
6910 if (*p == 0 || *p == ';')
6912 goto parse_words; // lame
6914 if (IS_START(p, "; sctproto:")) {
6915 sctproto = strdup(p + 11);
6917 else if (IS_START(p, "; sctend")) {
6926 awarn("wordc == 0?\n");
6930 // don't care about this:
6931 if (words[0][0] == '.'
6932 || IS(words[0], "include")
6933 || IS(words[0], "assume") || IS(words[1], "segment")
6934 || IS(words[0], "align"))
6940 // do delayed endp processing to collect switch jumptables
6942 if (in_func && !g_skip_func && !end && wordc >= 2
6943 && ((words[0][0] == 'd' && words[0][2] == 0)
6944 || (words[1][0] == 'd' && words[1][2] == 0)))
6947 if (words[1][0] == 'd' && words[1][2] == 0) {
6949 if (g_func_pd_cnt >= pd_alloc) {
6950 pd_alloc = pd_alloc * 2 + 16;
6951 g_func_pd = realloc(g_func_pd,
6952 sizeof(g_func_pd[0]) * pd_alloc);
6953 my_assert_not(g_func_pd, NULL);
6955 pd = &g_func_pd[g_func_pd_cnt];
6957 memset(pd, 0, sizeof(*pd));
6958 strcpy(pd->label, words[0]);
6959 pd->type = OPT_CONST;
6960 pd->lmod = lmod_from_directive(words[1]);
6966 anote("skipping alignment byte?\n");
6969 lmod = lmod_from_directive(words[0]);
6970 if (lmod != pd->lmod)
6971 aerr("lmod change? %d->%d\n", pd->lmod, lmod);
6974 if (pd->count_alloc < pd->count + wordc) {
6975 pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
6976 pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
6977 my_assert_not(pd->d, NULL);
6979 for (; i < wordc; i++) {
6980 if (IS(words[i], "offset")) {
6981 pd->type = OPT_OFFSET;
6984 p = strchr(words[i], ',');
6987 if (pd->type == OPT_OFFSET)
6988 pd->d[pd->count].u.label = strdup(words[i]);
6990 pd->d[pd->count].u.val = parse_number(words[i]);
6991 pd->d[pd->count].bt_i = -1;
6997 if (in_func && !g_skip_func) {
6999 gen_hdr(g_func, pi);
7001 gen_func(fout, g_fhdr, g_func, pi);
7006 g_ida_func_attr = 0;
7010 func_chunks_used = 0;
7013 memset(&ops, 0, pi * sizeof(ops[0]));
7018 for (i = 0; i < g_func_pd_cnt; i++) {
7020 if (pd->type == OPT_OFFSET) {
7021 for (j = 0; j < pd->count; j++)
7022 free(pd->d[j].u.label);
7036 if (IS(words[1], "proc")) {
7038 aerr("proc '%s' while in_func '%s'?\n",
7041 if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
7043 strcpy(g_func, words[0]);
7044 set_label(0, words[0]);
7049 if (IS(words[1], "endp"))
7052 aerr("endp '%s' while not in_func?\n", words[0]);
7053 if (!IS(g_func, words[0]))
7054 aerr("endp '%s' while in_func '%s'?\n",
7057 if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1
7058 && ops[0].op == OP_JMP && ops[0].operand[0].had_ds)
7064 if (!g_skip_func && func_chunks_used) {
7065 // start processing chunks
7066 struct chunk_item *ci, key = { g_func, 0 };
7068 func_chunk_ret = ftell(fasm);
7069 func_chunk_ret_ln = asmln;
7070 if (!func_chunks_sorted) {
7071 qsort(func_chunks, func_chunk_cnt,
7072 sizeof(func_chunks[0]), cmp_chunks);
7073 func_chunks_sorted = 1;
7075 ci = bsearch(&key, func_chunks, func_chunk_cnt,
7076 sizeof(func_chunks[0]), cmp_chunks);
7078 aerr("'%s' needs chunks, but none found\n", g_func);
7079 func_chunk_i = ci - func_chunks;
7080 for (; func_chunk_i > 0; func_chunk_i--)
7081 if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
7084 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
7086 aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
7087 asmln = func_chunks[func_chunk_i].asmln;
7095 if (wordc == 2 && IS(words[1], "ends")) {
7099 goto do_pending_endp;
7103 // scan for next text segment
7104 while (my_fgets(line, sizeof(line), fasm)) {
7107 if (*p == 0 || *p == ';')
7110 if (strstr(p, "segment para public 'CODE' use32"))
7117 p = strchr(words[0], ':');
7119 set_label(pi, words[0]);
7123 if (!in_func || g_skip_func) {
7124 if (!skip_warned && !g_skip_func && g_labels[pi] != NULL) {
7126 anote("skipping from '%s'\n", g_labels[pi]);
7130 g_labels[pi] = NULL;
7134 if (wordc > 1 && IS(words[1], "="))
7137 aerr("unhandled equ, wc=%d\n", wordc);
7138 if (g_eqcnt >= eq_alloc) {
7140 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
7141 my_assert_not(g_eqs, NULL);
7144 len = strlen(words[0]);
7145 if (len > sizeof(g_eqs[0].name) - 1)
7146 aerr("equ name too long: %d\n", len);
7147 strcpy(g_eqs[g_eqcnt].name, words[0]);
7149 if (!IS(words[3], "ptr"))
7150 aerr("unhandled equ\n");
7151 if (IS(words[2], "dword"))
7152 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
7153 else if (IS(words[2], "word"))
7154 g_eqs[g_eqcnt].lmod = OPLM_WORD;
7155 else if (IS(words[2], "byte"))
7156 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
7157 else if (IS(words[2], "qword"))
7158 g_eqs[g_eqcnt].lmod = OPLM_QWORD;
7160 aerr("bad lmod: '%s'\n", words[2]);
7162 g_eqs[g_eqcnt].offset = parse_number(words[4]);
7167 if (pi >= ARRAY_SIZE(ops))
7168 aerr("too many ops\n");
7170 parse_op(&ops[pi], words, wordc);
7172 if (sctproto != NULL) {
7173 if (ops[pi].op == OP_CALL || ops[pi].op == OP_JMP)
7174 ops[pi].datap = sctproto;
7190 // vim:ts=2:shiftwidth=2:expandtab