5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
14 #include "my_assert.h"
17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
18 #define IS(w, y) !strcmp(w, y)
19 #define IS_START(w, y) !strncmp(w, y, strlen(y))
21 #include "protoparse.h"
23 static const char *asmfn;
27 #define anote(fmt, ...) \
28 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
29 #define awarn(fmt, ...) \
30 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
31 #define aerr(fmt, ...) do { \
32 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
37 #include "masm_tools.h"
40 OPF_RMD = (1 << 0), /* removed or optimized out */
41 OPF_DATA = (1 << 1), /* data processing - writes to dst opr */
42 OPF_FLAGS = (1 << 2), /* sets flags */
43 OPF_JMP = (1 << 3), /* branch, call */
44 OPF_CJMP = (1 << 4), /* cond. branch (cc or jecxz) */
45 OPF_CC = (1 << 5), /* uses flags */
46 OPF_TAIL = (1 << 6), /* ret or tail call */
47 OPF_RSAVE = (1 << 7), /* push/pop is local reg save/load */
48 OPF_REP = (1 << 8), /* prefixed by rep */
49 OPF_REPZ = (1 << 9), /* rep is repe/repz */
50 OPF_REPNZ = (1 << 10), /* rep is repne/repnz */
51 OPF_FARG = (1 << 11), /* push collected as func arg */
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) */
128 // must be sorted (larger len must be further in enum)
137 #define MAX_OPERANDS 3
142 enum opr_lenmod lmod;
143 unsigned int is_ptr:1; // pointer in C
144 unsigned int is_array:1; // array in C
145 unsigned int type_from_var:1; // .. in header, sometimes wrong
146 unsigned int size_mismatch:1; // type override differs from C
147 unsigned int size_lt:1; // type override is larger than C
148 unsigned int had_ds:1; // had ds: prefix
149 const struct parsed_proto *pp; // for OPT_LABEL
157 struct parsed_opr operand[MAX_OPERANDS];
160 unsigned char pfo_inv;
161 unsigned char operand_cnt;
162 unsigned char p_argnum; // arg push: altered before call arg #
163 unsigned char p_arggrp; // arg push: arg group # for above
164 unsigned char p_argpass;// arg push: arg of host func
165 short p_argnext;// arg push: same arg pushed elsewhere or -1
166 int regmask_src; // all referensed regs
168 int pfomask; // flagop: parsed_flag_op that can't be delayed
169 int cc_scratch; // scratch storage during analysis
170 int bt_i; // branch target for branches
171 struct parsed_data *btj;// branch targets for jumptables
172 struct parsed_proto *pp;// parsed_proto for OP_CALL
178 // OP_CALL - parser proto hint (str)
179 // (OPF_CC) - points to one of (OPF_FLAGS) that affects cc op
180 // OP_POP - points to OP_PUSH in push/pop pair
184 enum opr_lenmod lmod;
191 enum opr_lenmod lmod;
205 struct label_ref *next;
209 IDAFA_BP_FRAME = (1 << 0),
210 IDAFA_LIB_FUNC = (1 << 1),
211 IDAFA_STATIC = (1 << 2),
212 IDAFA_NORETURN = (1 << 3),
213 IDAFA_THUNK = (1 << 4),
214 IDAFA_FPD = (1 << 5),
217 // note: limited to 32k due to p_argnext
219 #define MAX_ARG_GRP 2
221 static struct parsed_op ops[MAX_OPS];
222 static struct parsed_equ *g_eqs;
224 static char *g_labels[MAX_OPS];
225 static struct label_ref g_label_refs[MAX_OPS];
226 static const struct parsed_proto *g_func_pp;
227 static struct parsed_data *g_func_pd;
228 static int g_func_pd_cnt;
229 static char g_func[256];
230 static char g_comment[256];
231 static int g_bp_frame;
232 static int g_sp_frame;
233 static int g_stack_frame_used;
234 static int g_stack_fsz;
235 static int g_ida_func_attr;
236 static int g_allow_regfunc;
237 static int g_quiet_pp;
238 static int g_header_mode;
240 #define ferr(op_, fmt, ...) do { \
241 printf("%s:%d: error: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
242 dump_op(op_), ##__VA_ARGS__); \
246 #define fnote(op_, fmt, ...) \
247 printf("%s:%d: note: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
248 dump_op(op_), ##__VA_ARGS__)
250 const char *regs_r32[] = {
251 "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
252 // not r32, but list here for easy parsing and printing
253 "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
255 const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
256 const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
257 const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
259 enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
261 // possible basic comparison types (without inversion)
262 enum parsed_flag_op {
266 PFO_BE, // 6 CF=1||ZF=1
270 PFO_LE, // e ZF=1||SF!=OF
273 #define PFOB_O (1 << PFO_O)
274 #define PFOB_C (1 << PFO_C)
275 #define PFOB_Z (1 << PFO_Z)
276 #define PFOB_S (1 << PFO_S)
278 static const char *parsed_flag_op_names[] = {
279 "o", "c", "z", "be", "s", "p", "l", "le"
282 static int char_array_i(const char *array[], size_t len, const char *s)
286 for (i = 0; i < len; i++)
293 static void printf_number(char *buf, size_t buf_size,
294 unsigned long number)
296 // output in C-friendly form
297 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
300 static int check_segment_prefix(const char *s)
302 if (s[0] == 0 || s[1] != 's' || s[2] != ':')
316 static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
320 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
322 *reg_lmod = OPLM_QWORD;
326 *reg_lmod = OPLM_DWORD;
329 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
331 *reg_lmod = OPLM_WORD;
334 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
336 *reg_lmod = OPLM_BYTE;
339 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
341 *reg_lmod = OPLM_BYTE;
348 static int parse_indmode(char *name, int *regmask, int need_c_cvt)
350 enum opr_lenmod lmod;
363 while (my_isblank(*s))
365 for (; my_issep(*s); d++, s++)
367 while (my_isblank(*s))
371 // skip '?s:' prefixes
372 if (check_segment_prefix(s))
375 s = next_idt(w, sizeof(w), s);
380 reg = parse_reg(&lmod, w);
382 *regmask |= 1 << reg;
386 if ('0' <= w[0] && w[0] <= '9') {
387 number = parse_number(w);
388 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
392 // probably some label/identifier - pass
395 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
399 strcpy(name, cvtbuf);
404 static int is_reg_in_str(const char *s)
408 if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3])))
411 for (i = 0; i < ARRAY_SIZE(regs_r32); i++)
412 if (!strncmp(s, regs_r32[i], 3))
418 static const char *parse_stack_el(const char *name, char *extra_reg,
421 const char *p, *p2, *s;
427 if (g_bp_frame || early_try)
430 if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) {
432 if (extra_reg != NULL) {
433 strncpy(extra_reg, name, 3);
438 if (IS_START(p, "ebp+")) {
442 if (p2 != NULL && is_reg_in_str(p)) {
443 if (extra_reg != NULL) {
444 strncpy(extra_reg, p, p2 - p);
445 extra_reg[p2 - p] = 0;
450 if (!('0' <= *p && *p <= '9'))
457 if (!IS_START(name, "esp+"))
463 if (is_reg_in_str(s)) {
464 if (extra_reg != NULL) {
465 strncpy(extra_reg, s, p - s);
466 extra_reg[p - s] = 0;
471 aerr("%s IDA stackvar not set?\n", __func__);
473 if (!('0' <= *s && *s <= '9')) {
474 aerr("%s IDA stackvar offset not set?\n", __func__);
477 if (s[0] == '0' && s[1] == 'x')
480 if (len < sizeof(buf) - 1) {
481 strncpy(buf, s, len);
483 val = strtol(buf, &endp, 16);
484 if (val == 0 || *endp != 0) {
485 aerr("%s num parse fail for '%s'\n", __func__, buf);
494 if ('0' <= *p && *p <= '9')
500 static int guess_lmod_from_name(struct parsed_opr *opr)
502 if (!strncmp(opr->name, "dword_", 6)) {
503 opr->lmod = OPLM_DWORD;
506 if (!strncmp(opr->name, "word_", 5)) {
507 opr->lmod = OPLM_WORD;
510 if (!strncmp(opr->name, "byte_", 5)) {
511 opr->lmod = OPLM_BYTE;
514 if (!strncmp(opr->name, "qword_", 6)) {
515 opr->lmod = OPLM_QWORD;
521 static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
522 const struct parsed_type *c_type)
524 static const char *dword_types[] = {
525 "int", "_DWORD", "UINT_PTR", "DWORD",
526 "WPARAM", "LPARAM", "UINT", "__int32",
527 "LONG", "HIMC", "BOOL", "size_t",
530 static const char *word_types[] = {
531 "uint16_t", "int16_t", "_WORD", "WORD",
532 "unsigned __int16", "__int16",
534 static const char *byte_types[] = {
535 "uint8_t", "int8_t", "char",
536 "unsigned __int8", "__int8", "BYTE", "_BYTE",
538 // structures.. deal the same as with _UNKNOWN for now
544 if (c_type->is_ptr) {
549 n = skip_type_mod(c_type->name);
551 for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
552 if (IS(n, dword_types[i])) {
558 for (i = 0; i < ARRAY_SIZE(word_types); i++) {
559 if (IS(n, word_types[i])) {
565 for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
566 if (IS(n, byte_types[i])) {
575 static char *default_cast_to(char *buf, size_t buf_size,
576 struct parsed_opr *opr)
582 if (opr->pp == NULL || opr->pp->type.name == NULL
585 snprintf(buf, buf_size, "%s", "(void *)");
589 snprintf(buf, buf_size, "(%s)", opr->pp->type.name);
593 static enum opr_type lmod_from_directive(const char *d)
597 else if (IS(d, "dw"))
599 else if (IS(d, "db"))
602 aerr("unhandled directive: '%s'\n", d);
606 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
612 *regmask |= 1 << reg;
615 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
618 static int parse_operand(struct parsed_opr *opr,
619 int *regmask, int *regmask_indirect,
620 char words[16][256], int wordc, int w, unsigned int op_flags)
622 const struct parsed_proto *pp = NULL;
623 enum opr_lenmod tmplmod;
624 unsigned long number;
632 aerr("parse_operand w %d, wordc %d\n", w, wordc);
636 for (i = w; i < wordc; i++) {
637 len = strlen(words[i]);
638 if (words[i][len - 1] == ',') {
639 words[i][len - 1] = 0;
645 wordc_in = wordc - w;
647 if ((op_flags & OPF_JMP) && wordc_in > 0
648 && !('0' <= words[w][0] && words[w][0] <= '9'))
650 const char *label = NULL;
652 if (wordc_in == 3 && !strncmp(words[w], "near", 4)
653 && IS(words[w + 1], "ptr"))
654 label = words[w + 2];
655 else if (wordc_in == 2 && IS(words[w], "short"))
656 label = words[w + 1];
657 else if (wordc_in == 1
658 && strchr(words[w], '[') == NULL
659 && parse_reg(&tmplmod, words[w]) < 0)
663 opr->type = OPT_LABEL;
664 ret = check_segment_prefix(label);
667 aerr("fs/gs used\n");
671 strcpy(opr->name, label);
677 if (IS(words[w + 1], "ptr")) {
678 if (IS(words[w], "dword"))
679 opr->lmod = OPLM_DWORD;
680 else if (IS(words[w], "word"))
681 opr->lmod = OPLM_WORD;
682 else if (IS(words[w], "byte"))
683 opr->lmod = OPLM_BYTE;
684 else if (IS(words[w], "qword"))
685 opr->lmod = OPLM_QWORD;
687 aerr("type parsing failed\n");
689 wordc_in = wordc - w;
694 if (IS(words[w], "offset")) {
695 opr->type = OPT_OFFSET;
696 opr->lmod = OPLM_DWORD;
697 strcpy(opr->name, words[w + 1]);
698 pp = proto_parse(g_fhdr, opr->name, 1);
701 if (IS(words[w], "(offset")) {
702 p = strchr(words[w + 1], ')');
704 aerr("parse of bracketed offset failed\n");
706 opr->type = OPT_OFFSET;
707 strcpy(opr->name, words[w + 1]);
713 aerr("parse_operand 1 word expected\n");
715 ret = check_segment_prefix(words[w]);
718 aerr("fs/gs used\n");
720 memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
722 strcpy(opr->name, words[w]);
724 if (words[w][0] == '[') {
725 opr->type = OPT_REGMEM;
726 ret = sscanf(words[w], "[%[^]]]", opr->name);
728 aerr("[] parse failure\n");
730 parse_indmode(opr->name, regmask_indirect, 1);
731 if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL, 1))
734 struct parsed_equ *eq =
735 equ_find(NULL, parse_stack_el(opr->name, NULL, 1), &i);
737 opr->lmod = eq->lmod;
741 else if (strchr(words[w], '[')) {
743 p = strchr(words[w], '[');
744 opr->type = OPT_REGMEM;
745 parse_indmode(p, regmask_indirect, 0);
746 strncpy(buf, words[w], p - words[w]);
747 buf[p - words[w]] = 0;
748 pp = proto_parse(g_fhdr, buf, 1);
751 else if (('0' <= words[w][0] && words[w][0] <= '9')
752 || words[w][0] == '-')
754 number = parse_number(words[w]);
755 opr->type = OPT_CONST;
757 printf_number(opr->name, sizeof(opr->name), number);
761 ret = parse_reg(&tmplmod, opr->name);
763 setup_reg_opr(opr, ret, tmplmod, regmask);
767 // most likely var in data segment
768 opr->type = OPT_LABEL;
769 pp = proto_parse(g_fhdr, opr->name, g_quiet_pp);
773 if (pp->is_fptr || pp->is_func) {
774 opr->lmod = OPLM_DWORD;
778 tmplmod = OPLM_UNSPEC;
779 if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
780 anote("unhandled C type '%s' for '%s'\n",
781 pp->type.name, opr->name);
783 if (opr->lmod == OPLM_UNSPEC) {
785 opr->type_from_var = 1;
787 else if (opr->lmod != tmplmod) {
788 opr->size_mismatch = 1;
789 if (tmplmod < opr->lmod)
792 opr->is_ptr = pp->type.is_ptr;
794 opr->is_array = pp->type.is_array;
798 if (opr->lmod == OPLM_UNSPEC)
799 guess_lmod_from_name(opr);
803 static const struct {
808 { "repe", OPF_REP|OPF_REPZ },
809 { "repz", OPF_REP|OPF_REPZ },
810 { "repne", OPF_REP|OPF_REPNZ },
811 { "repnz", OPF_REP|OPF_REPNZ },
812 { "lock", OPF_LOCK }, // ignored for now..
815 #define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC)
817 static const struct {
820 unsigned short minopr;
821 unsigned short maxopr;
824 unsigned char pfo_inv;
826 { "nop", OP_NOP, 0, 0, 0 },
827 { "push", OP_PUSH, 1, 1, 0 },
828 { "pop", OP_POP, 1, 1, OPF_DATA },
829 { "leave",OP_LEAVE, 0, 0, OPF_DATA },
830 { "mov" , OP_MOV, 2, 2, OPF_DATA },
831 { "lea", OP_LEA, 2, 2, OPF_DATA },
832 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
833 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
834 { "xchg", OP_XCHG, 2, 2, OPF_DATA },
835 { "not", OP_NOT, 1, 1, OPF_DATA },
836 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
837 { "lodsb",OP_LODS, 0, 0, OPF_DATA },
838 { "lodsw",OP_LODS, 0, 0, OPF_DATA },
839 { "lodsd",OP_LODS, 0, 0, OPF_DATA },
840 { "stosb",OP_STOS, 0, 0, OPF_DATA },
841 { "stosw",OP_STOS, 0, 0, OPF_DATA },
842 { "stosd",OP_STOS, 0, 0, OPF_DATA },
843 { "movsb",OP_MOVS, 0, 0, OPF_DATA },
844 { "movsw",OP_MOVS, 0, 0, OPF_DATA },
845 { "movsd",OP_MOVS, 0, 0, OPF_DATA },
846 { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
847 { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
848 { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
849 { "scasb",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
850 { "scasw",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
851 { "scasd",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
852 { "std", OP_STD, 0, 0, OPF_DATA }, // special flag
853 { "cld", OP_CLD, 0, 0, OPF_DATA },
854 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
855 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
856 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
857 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
858 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
859 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
860 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
861 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
862 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
863 { "shrd", OP_SHRD, 3, 3, OPF_DATA|OPF_FLAGS },
864 { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS },
865 { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS },
866 { "rcl", OP_RCL, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
867 { "rcr", OP_RCR, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
868 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
869 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
870 { "bsf", OP_BSF, 2, 2, OPF_DATA|OPF_FLAGS },
871 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
872 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
873 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
874 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
875 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
876 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
877 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
878 { "test", OP_TEST, 2, 2, OPF_FLAGS },
879 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
880 { "retn", OP_RET, 0, 1, OPF_TAIL },
881 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
882 { "jmp", OP_JMP, 1, 1, OPF_JMP },
883 { "jecxz",OP_JECXZ, 1, 1, OPF_JMP|OPF_CJMP },
884 { "jo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 0 }, // 70 OF=1
885 { "jno", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 1 }, // 71 OF=0
886 { "jc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 CF=1
887 { "jb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72
888 { "jnc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 CF=0
889 { "jnb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
890 { "jae", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
891 { "jz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74 ZF=1
892 { "je", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74
893 { "jnz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75 ZF=0
894 { "jne", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75
895 { "jbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 CF=1||ZF=1
896 { "jna", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76
897 { "ja", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 CF=0&&ZF=0
898 { "jnbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77
899 { "js", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 0 }, // 78 SF=1
900 { "jns", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 1 }, // 79 SF=0
901 { "jp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a PF=1
902 { "jpe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a
903 { "jnp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b PF=0
904 { "jpo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b
905 { "jl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c SF!=OF
906 { "jnge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c
907 { "jge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d SF=OF
908 { "jnl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d
909 { "jle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e ZF=1||SF!=OF
910 { "jng", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e
911 { "jg", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f ZF=0&&SF=OF
912 { "jnle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f
913 { "seto", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 0 },
914 { "setno", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 1 },
915 { "setc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
916 { "setb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
917 { "setnc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
918 { "setae", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
919 { "setnb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
920 { "setz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
921 { "sete", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
922 { "setnz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
923 { "setne", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
924 { "setbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
925 { "setna", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
926 { "seta", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
927 { "setnbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
928 { "sets", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 0 },
929 { "setns", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 1 },
930 { "setp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
931 { "setpe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
932 { "setnp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
933 { "setpo", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
934 { "setl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
935 { "setnge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
936 { "setge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
937 { "setnl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
938 { "setle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
939 { "setng", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
940 { "setg", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
941 { "setnle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
944 { "emms", OP_EMMS, 0, 0, OPF_DATA },
945 { "movq", OP_MOV, 2, 2, OPF_DATA },
950 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
952 enum opr_lenmod lmod = OPLM_UNSPEC;
953 int prefix_flags = 0;
961 for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
962 if (IS(words[w], pref_table[i].name)) {
963 prefix_flags = pref_table[i].flags;
970 aerr("lone prefix: '%s'\n", words[0]);
975 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
976 if (IS(words[w], op_table[i].name))
980 if (i == ARRAY_SIZE(op_table)) {
981 anote("unhandled op: '%s'\n", words[0]);
986 op->op = op_table[i].op;
987 op->flags = op_table[i].flags | prefix_flags;
988 op->pfo = op_table[i].pfo;
989 op->pfo_inv = op_table[i].pfo_inv;
990 op->regmask_src = op->regmask_dst = 0;
993 if (op->op == OP_UD2)
996 for (opr = 0; opr < op_table[i].maxopr; opr++) {
997 if (opr >= op_table[i].minopr && w >= wordc)
1000 regmask = regmask_ind = 0;
1001 w = parse_operand(&op->operand[opr], ®mask, ®mask_ind,
1002 words, wordc, w, op->flags);
1004 if (opr == 0 && (op->flags & OPF_DATA))
1005 op->regmask_dst = regmask;
1007 op->regmask_src |= regmask;
1008 op->regmask_src |= regmask_ind;
1012 aerr("parse_op %s incomplete: %d/%d\n",
1013 words[0], w, wordc);
1016 op->operand_cnt = opr;
1017 if (!strncmp(op_table[i].name, "set", 3))
1018 op->operand[0].lmod = OPLM_BYTE;
1021 // first operand is not dst
1024 op->regmask_src |= op->regmask_dst;
1025 op->regmask_dst = 0;
1028 // first operand is src too
1040 op->regmask_src |= op->regmask_dst;
1045 op->regmask_src |= op->regmask_dst;
1046 op->regmask_dst |= op->regmask_src;
1052 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1053 && op->operand[0].lmod == op->operand[1].lmod
1054 && op->operand[0].reg == op->operand[1].reg
1055 && IS(op->operand[0].name, op->operand[1].name)) // ! ah, al..
1057 op->regmask_src = 0;
1060 op->regmask_src |= op->regmask_dst;
1063 // ops with implicit argumets
1065 op->operand_cnt = 2;
1066 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
1067 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
1073 if (op->operand_cnt != 0)
1075 if (words[op_w][4] == 'b')
1077 else if (words[op_w][4] == 'w')
1079 else if (words[op_w][4] == 'd')
1081 op->operand_cnt = 3;
1082 setup_reg_opr(&op->operand[0], op->op == OP_LODS ? xSI : xDI,
1083 lmod, &op->regmask_src);
1084 setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src);
1085 op->regmask_dst = op->regmask_src;
1086 setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD,
1087 op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src);
1092 if (op->operand_cnt != 0)
1094 if (words[op_w][4] == 'b')
1096 else if (words[op_w][4] == 'w')
1098 else if (words[op_w][4] == 'd')
1100 op->operand_cnt = 3;
1101 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
1102 setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src);
1103 setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src);
1104 op->regmask_dst = op->regmask_src;
1108 op->operand_cnt = 1;
1109 op->regmask_src = 1 << xCX;
1110 op->operand[0].type = OPT_REG;
1111 op->operand[0].reg = xCX;
1112 op->operand[0].lmod = OPLM_DWORD;
1116 if (op->operand_cnt != 1)
1121 op->regmask_src |= op->regmask_dst;
1122 op->regmask_dst = (1 << xDX) | (1 << xAX);
1123 if (op->operand[0].lmod == OPLM_UNSPEC)
1124 op->operand[0].lmod = OPLM_DWORD;
1129 // we could set up operands for edx:eax, but there is no real need to
1130 // (see is_opr_modified())
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;
1142 op->regmask_src |= op->regmask_dst;
1143 if (op->operand[1].lmod == OPLM_UNSPEC)
1144 op->operand[1].lmod = OPLM_BYTE;
1148 op->regmask_src |= op->regmask_dst;
1149 if (op->operand[2].lmod == OPLM_UNSPEC)
1150 op->operand[2].lmod = OPLM_BYTE;
1154 op->regmask_src |= op->regmask_dst;
1155 op->regmask_dst = 0;
1156 if (op->operand[0].lmod == OPLM_UNSPEC
1157 && (op->operand[0].type == OPT_CONST
1158 || op->operand[0].type == OPT_OFFSET
1159 || op->operand[0].type == OPT_LABEL))
1160 op->operand[0].lmod = OPLM_DWORD;
1166 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1167 && op->operand[0].lmod == op->operand[1].lmod
1168 && op->operand[0].reg == op->operand[1].reg
1169 && IS(op->operand[0].name, op->operand[1].name)) // ! ah, al..
1171 op->flags |= OPF_RMD;
1172 op->regmask_src = op->regmask_dst = 0;
1177 if (op->operand[0].type == OPT_REG
1178 && op->operand[1].type == OPT_REGMEM)
1181 snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
1182 if (IS(buf, op->operand[1].name))
1183 op->flags |= OPF_RMD;
1188 // trashed regs must be explicitly detected later
1189 op->regmask_dst = 0;
1193 op->regmask_dst = (1 << xBP) | (1 << xSP);
1194 op->regmask_src = 1 << xBP;
1202 static const char *op_name(struct parsed_op *po)
1204 static char buf[16];
1208 if (po->op == OP_JCC || po->op == OP_SCC) {
1210 *p++ = (po->op == OP_JCC) ? 'j' : 's';
1213 strcpy(p, parsed_flag_op_names[po->pfo]);
1217 for (i = 0; i < ARRAY_SIZE(op_table); i++)
1218 if (op_table[i].op == po->op)
1219 return op_table[i].name;
1225 static const char *dump_op(struct parsed_op *po)
1227 static char out[128];
1234 snprintf(out, sizeof(out), "%s", op_name(po));
1235 for (i = 0; i < po->operand_cnt; i++) {
1239 snprintf(p, sizeof(out) - (p - out),
1240 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1241 po->operand[i].name);
1247 static const char *lmod_type_u(struct parsed_op *po,
1248 enum opr_lenmod lmod)
1260 ferr(po, "invalid lmod: %d\n", lmod);
1261 return "(_invalid_)";
1265 static const char *lmod_cast_u(struct parsed_op *po,
1266 enum opr_lenmod lmod)
1278 ferr(po, "invalid lmod: %d\n", lmod);
1279 return "(_invalid_)";
1283 static const char *lmod_cast_u_ptr(struct parsed_op *po,
1284 enum opr_lenmod lmod)
1296 ferr(po, "invalid lmod: %d\n", lmod);
1297 return "(_invalid_)";
1301 static const char *lmod_cast_s(struct parsed_op *po,
1302 enum opr_lenmod lmod)
1314 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1315 return "(_invalid_)";
1319 static const char *lmod_cast(struct parsed_op *po,
1320 enum opr_lenmod lmod, int is_signed)
1323 lmod_cast_s(po, lmod) :
1324 lmod_cast_u(po, lmod);
1327 static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1339 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1344 static const char *opr_name(struct parsed_op *po, int opr_num)
1346 if (opr_num >= po->operand_cnt)
1347 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1348 return po->operand[opr_num].name;
1351 static unsigned int opr_const(struct parsed_op *po, int opr_num)
1353 if (opr_num >= po->operand_cnt)
1354 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1355 if (po->operand[opr_num].type != OPT_CONST)
1356 ferr(po, "opr %d: const expected\n", opr_num);
1357 return po->operand[opr_num].val;
1360 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1362 if ((unsigned int)popr->reg >= ARRAY_SIZE(regs_r32))
1363 ferr(po, "invalid reg: %d\n", popr->reg);
1364 return regs_r32[popr->reg];
1367 // cast1 is the "final" cast
1368 static const char *simplify_cast(const char *cast1, const char *cast2)
1370 static char buf[256];
1376 if (IS(cast1, cast2))
1378 if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
1380 if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
1382 if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
1384 if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
1386 if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1389 snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1393 static const char *simplify_cast_num(const char *cast, unsigned int val)
1395 if (IS(cast, "(u8)") && val < 0x100)
1397 if (IS(cast, "(s8)") && val < 0x80)
1399 if (IS(cast, "(u16)") && val < 0x10000)
1401 if (IS(cast, "(s16)") && val < 0x8000)
1403 if (IS(cast, "(s32)") && val < 0x80000000)
1409 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1418 namelen = strlen(name);
1420 p = strchr(name, '+');
1424 ferr(po, "equ parse failed for '%s'\n", name);
1426 if (IS_START(p, "0x"))
1428 *extra_offs = strtol(p, &endp, 16);
1430 ferr(po, "equ parse failed for '%s'\n", name);
1433 for (i = 0; i < g_eqcnt; i++)
1434 if (strncmp(g_eqs[i].name, name, namelen) == 0
1435 && g_eqs[i].name[namelen] == 0)
1439 ferr(po, "unresolved equ name: '%s'\n", name);
1446 static int is_stack_access(struct parsed_op *po,
1447 const struct parsed_opr *popr)
1449 return (parse_stack_el(popr->name, NULL, 0)
1450 || (g_bp_frame && !(po->flags & OPF_EBP_S)
1451 && IS_START(popr->name, "ebp")));
1454 static void parse_stack_access(struct parsed_op *po,
1455 const char *name, char *ofs_reg, int *offset_out,
1456 int *stack_ra_out, const char **bp_arg_out, int is_lea)
1458 const char *bp_arg = "";
1459 const char *p = NULL;
1460 struct parsed_equ *eq;
1467 if (IS_START(name, "ebp-")
1468 || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1471 if (IS_START(p, "0x"))
1473 offset = strtoul(p, &endp, 16);
1477 ferr(po, "ebp- parse of '%s' failed\n", name);
1480 bp_arg = parse_stack_el(name, ofs_reg, 0);
1481 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1482 eq = equ_find(po, bp_arg, &offset);
1484 ferr(po, "detected but missing eq\n");
1485 offset += eq->offset;
1488 if (!strncmp(name, "ebp", 3))
1491 // yes it sometimes LEAs ra for compares..
1492 if (!is_lea && ofs_reg[0] == 0
1493 && stack_ra <= offset && offset < stack_ra + 4)
1495 ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
1498 *offset_out = offset;
1499 *stack_ra_out = stack_ra;
1501 *bp_arg_out = bp_arg;
1504 static int stack_frame_access(struct parsed_op *po,
1505 struct parsed_opr *popr, char *buf, size_t buf_size,
1506 const char *name, const char *cast, int is_src, int is_lea)
1508 enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1509 const char *prefix = "";
1510 const char *bp_arg = NULL;
1511 char ofs_reg[16] = { 0, };
1512 int i, arg_i, arg_s;
1520 if (po->flags & OPF_EBP_S)
1521 ferr(po, "stack_frame_access while ebp is scratch\n");
1523 parse_stack_access(po, name, ofs_reg, &offset,
1524 &stack_ra, &bp_arg, is_lea);
1526 if (offset > stack_ra)
1528 arg_i = (offset - stack_ra - 4) / 4;
1529 if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
1531 if (g_func_pp->is_vararg
1532 && arg_i == g_func_pp->argc_stack && is_lea)
1534 // should be va_list
1537 snprintf(buf, buf_size, "%sap", cast);
1540 ferr(po, "offset %d (%s,%d) doesn't map to any arg\n",
1541 offset, bp_arg, arg_i);
1543 if (ofs_reg[0] != 0)
1544 ferr(po, "offset reg on arg access?\n");
1546 for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1547 if (g_func_pp->arg[i].reg != NULL)
1553 if (i == g_func_pp->argc)
1554 ferr(po, "arg %d not in prototype?\n", arg_i);
1556 popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
1563 ferr(po, "lea/byte to arg?\n");
1564 if (is_src && (offset & 3) == 0)
1565 snprintf(buf, buf_size, "%sa%d",
1566 simplify_cast(cast, "(u8)"), i + 1);
1568 snprintf(buf, buf_size, "%sBYTE%d(a%d)",
1569 cast, offset & 3, i + 1);
1574 ferr(po, "lea/word to arg?\n");
1579 ferr(po, "problematic arg store\n");
1580 snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
1581 simplify_cast(cast, "*(u16 *)"), i + 1);
1584 ferr(po, "unaligned arg word load\n");
1586 else if (is_src && (offset & 2) == 0)
1587 snprintf(buf, buf_size, "%sa%d",
1588 simplify_cast(cast, "(u16)"), i + 1);
1590 snprintf(buf, buf_size, "%s%sWORD(a%d)",
1591 cast, (offset & 2) ? "HI" : "LO", i + 1);
1603 snprintf(buf, buf_size, "(u32)&a%d + %d",
1606 ferr(po, "unaligned arg store\n");
1608 // mov edx, [ebp+arg_4+2]; movsx ecx, dx
1609 snprintf(buf, buf_size, "%s(a%d >> %d)",
1610 prefix, i + 1, (offset & 3) * 8);
1614 snprintf(buf, buf_size, "%s%sa%d",
1615 prefix, is_lea ? "&" : "", i + 1);
1620 ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
1624 snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
1627 guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
1628 if (tmp_lmod != OPLM_DWORD
1629 && (unaligned || (!is_src && lmod_bytes(po, tmp_lmod)
1630 < lmod_bytes(po, popr->lmod) + (offset & 3))))
1632 ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
1633 i + 1, offset, g_func_pp->arg[i].type.name);
1635 // can't check this because msvc likes to reuse
1636 // arg space for scratch..
1637 //if (popr->is_ptr && popr->lmod != OPLM_DWORD)
1638 // ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
1642 if (g_stack_fsz == 0)
1643 ferr(po, "stack var access without stackframe\n");
1644 g_stack_frame_used = 1;
1646 sf_ofs = g_stack_fsz + offset;
1647 lim = (ofs_reg[0] != 0) ? -4 : 0;
1648 if (offset > 0 || sf_ofs < lim)
1649 ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
1659 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1660 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1664 if ((sf_ofs & 1) || ofs_reg[0] != 0) {
1665 // known unaligned or possibly unaligned
1666 strcat(g_comment, " unaligned");
1668 prefix = "*(u16 *)&";
1669 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1670 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1673 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
1677 if ((sf_ofs & 3) || ofs_reg[0] != 0) {
1678 // known unaligned or possibly unaligned
1679 strcat(g_comment, " unaligned");
1681 prefix = "*(u32 *)&";
1682 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1683 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1686 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
1690 ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
1697 static void check_func_pp(struct parsed_op *po,
1698 const struct parsed_proto *pp, const char *pfx)
1700 enum opr_lenmod tmp_lmod;
1704 if (pp->argc_reg != 0) {
1705 if (/*!g_allow_regfunc &&*/ !pp->is_fastcall) {
1706 pp_print(buf, sizeof(buf), pp);
1707 ferr(po, "%s: unexpected reg arg in icall: %s\n", pfx, buf);
1709 if (pp->argc_stack > 0 && pp->argc_reg != 2)
1710 ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
1711 pfx, pp->argc_reg, pp->argc_stack);
1714 // fptrs must use 32bit args, callsite might have no information and
1715 // lack a cast to smaller types, which results in incorrectly masked
1716 // args passed (callee may assume masked args, it does on ARM)
1717 if (!pp->is_oslib) {
1718 for (i = 0; i < pp->argc; i++) {
1719 ret = guess_lmod_from_c_type(&tmp_lmod, &pp->arg[i].type);
1720 if (ret && tmp_lmod != OPLM_DWORD)
1721 ferr(po, "reference to %s with arg%d '%s'\n", pp->name,
1722 i + 1, pp->arg[i].type.name);
1727 static const char *check_label_read_ref(struct parsed_op *po,
1730 const struct parsed_proto *pp;
1732 pp = proto_parse(g_fhdr, name, 0);
1734 ferr(po, "proto_parse failed for ref '%s'\n", name);
1737 check_func_pp(po, pp, "ref");
1742 static char *out_src_opr(char *buf, size_t buf_size,
1743 struct parsed_op *po, struct parsed_opr *popr, const char *cast,
1746 char tmp1[256], tmp2[256];
1755 switch (popr->type) {
1758 ferr(po, "lea from reg?\n");
1760 switch (popr->lmod) {
1762 snprintf(buf, buf_size, "%s%s.q", cast, opr_reg_p(po, popr));
1765 snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
1768 snprintf(buf, buf_size, "%s%s",
1769 simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
1772 if (popr->name[1] == 'h') // XXX..
1773 snprintf(buf, buf_size, "%s(%s >> 8)",
1774 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1776 snprintf(buf, buf_size, "%s%s",
1777 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1780 ferr(po, "invalid src lmod: %d\n", popr->lmod);
1785 if (is_stack_access(po, popr)) {
1786 stack_frame_access(po, popr, buf, buf_size,
1787 popr->name, cast, 1, is_lea);
1791 strcpy(expr, popr->name);
1792 if (strchr(expr, '[')) {
1793 // special case: '[' can only be left for label[reg] form
1794 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
1796 ferr(po, "parse failure for '%s'\n", expr);
1797 if (tmp1[0] == '(') {
1798 // (off_4FFF50+3)[eax]
1799 p = strchr(tmp1 + 1, ')');
1800 if (p == NULL || p[1] != 0)
1801 ferr(po, "parse failure (2) for '%s'\n", expr);
1803 memmove(tmp1, tmp1 + 1, strlen(tmp1));
1805 snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
1808 // XXX: do we need more parsing?
1810 snprintf(buf, buf_size, "%s", expr);
1814 snprintf(buf, buf_size, "%s(%s)",
1815 simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
1819 name = check_label_read_ref(po, popr->name);
1820 if (cast[0] == 0 && popr->is_ptr)
1824 snprintf(buf, buf_size, "(u32)&%s", name);
1825 else if (popr->size_lt)
1826 snprintf(buf, buf_size, "%s%s%s%s", cast,
1827 lmod_cast_u_ptr(po, popr->lmod),
1828 popr->is_array ? "" : "&", name);
1830 snprintf(buf, buf_size, "%s%s%s", cast, name,
1831 popr->is_array ? "[0]" : "");
1835 name = check_label_read_ref(po, popr->name);
1839 ferr(po, "lea an offset?\n");
1840 snprintf(buf, buf_size, "%s&%s", cast, name);
1845 ferr(po, "lea from const?\n");
1847 printf_number(tmp1, sizeof(tmp1), popr->val);
1848 if (popr->val == 0 && strchr(cast, '*'))
1849 snprintf(buf, buf_size, "NULL");
1851 snprintf(buf, buf_size, "%s%s",
1852 simplify_cast_num(cast, popr->val), tmp1);
1856 ferr(po, "invalid src type: %d\n", popr->type);
1862 // note: may set is_ptr (we find that out late for ebp frame..)
1863 static char *out_dst_opr(char *buf, size_t buf_size,
1864 struct parsed_op *po, struct parsed_opr *popr)
1866 switch (popr->type) {
1868 switch (popr->lmod) {
1870 snprintf(buf, buf_size, "%s.q", opr_reg_p(po, popr));
1873 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
1877 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
1881 if (popr->name[1] == 'h') // XXX..
1882 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
1884 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
1887 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
1892 if (is_stack_access(po, popr)) {
1893 stack_frame_access(po, popr, buf, buf_size,
1894 popr->name, "", 0, 0);
1898 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1901 if (popr->size_mismatch)
1902 snprintf(buf, buf_size, "%s%s%s",
1903 lmod_cast_u_ptr(po, popr->lmod),
1904 popr->is_array ? "" : "&", popr->name);
1906 snprintf(buf, buf_size, "%s%s", popr->name,
1907 popr->is_array ? "[0]" : "");
1911 ferr(po, "invalid dst type: %d\n", popr->type);
1917 static char *out_src_opr_u32(char *buf, size_t buf_size,
1918 struct parsed_op *po, struct parsed_opr *popr)
1920 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1923 static void out_test_for_cc(char *buf, size_t buf_size,
1924 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1925 enum opr_lenmod lmod, const char *expr)
1927 const char *cast, *scast;
1929 cast = lmod_cast_u(po, lmod);
1930 scast = lmod_cast_s(po, lmod);
1934 case PFO_BE: // CF=1||ZF=1; CF=0
1935 snprintf(buf, buf_size, "(%s%s %s 0)",
1936 cast, expr, is_inv ? "!=" : "==");
1940 case PFO_L: // SF!=OF; OF=0
1941 snprintf(buf, buf_size, "(%s%s %s 0)",
1942 scast, expr, is_inv ? ">=" : "<");
1945 case PFO_LE: // ZF=1||SF!=OF; OF=0
1946 snprintf(buf, buf_size, "(%s%s %s 0)",
1947 scast, expr, is_inv ? ">" : "<=");
1951 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1955 static void out_cmp_for_cc(char *buf, size_t buf_size,
1956 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1958 const char *cast, *scast, *cast_use;
1959 char buf1[256], buf2[256];
1960 enum opr_lenmod lmod;
1962 if (po->op != OP_DEC && po->operand[0].lmod != po->operand[1].lmod)
1963 ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
1964 po->operand[0].lmod, po->operand[1].lmod);
1965 lmod = po->operand[0].lmod;
1967 cast = lmod_cast_u(po, lmod);
1968 scast = lmod_cast_s(po, lmod);
1984 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1987 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
1988 if (po->op == OP_DEC)
1989 snprintf(buf2, sizeof(buf2), "1");
1991 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
1995 // note: must be unsigned compare
1996 snprintf(buf, buf_size, "(%s %s %s)",
1997 buf1, is_inv ? ">=" : "<", buf2);
2001 snprintf(buf, buf_size, "(%s %s %s)",
2002 buf1, is_inv ? "!=" : "==", buf2);
2006 // note: must be unsigned compare
2007 snprintf(buf, buf_size, "(%s %s %s)",
2008 buf1, is_inv ? ">" : "<=", buf2);
2011 if (is_inv && lmod == OPLM_BYTE
2012 && po->operand[1].type == OPT_CONST
2013 && po->operand[1].val == 0xff)
2015 snprintf(g_comment, sizeof(g_comment), "if %s", buf);
2016 snprintf(buf, buf_size, "(0)");
2020 // note: must be signed compare
2022 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
2023 scast, buf1, buf2, is_inv ? ">=" : "<");
2027 snprintf(buf, buf_size, "(%s %s %s)",
2028 buf1, is_inv ? ">=" : "<", buf2);
2032 snprintf(buf, buf_size, "(%s %s %s)",
2033 buf1, is_inv ? ">" : "<=", buf2);
2041 static void out_cmp_test(char *buf, size_t buf_size,
2042 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
2044 char buf1[256], buf2[256], buf3[256];
2046 if (po->op == OP_TEST) {
2047 if (IS(opr_name(po, 0), opr_name(po, 1))) {
2048 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
2051 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
2052 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
2053 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
2055 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
2056 po->operand[0].lmod, buf3);
2058 else if (po->op == OP_CMP) {
2059 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
2062 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
2065 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
2066 struct parsed_opr *popr2)
2068 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
2069 ferr(po, "missing lmod for both operands\n");
2071 if (popr1->lmod == OPLM_UNSPEC)
2072 popr1->lmod = popr2->lmod;
2073 else if (popr2->lmod == OPLM_UNSPEC)
2074 popr2->lmod = popr1->lmod;
2075 else if (popr1->lmod != popr2->lmod) {
2076 if (popr1->type_from_var) {
2077 popr1->size_mismatch = 1;
2078 if (popr1->lmod < popr2->lmod)
2080 popr1->lmod = popr2->lmod;
2082 else if (popr2->type_from_var) {
2083 popr2->size_mismatch = 1;
2084 if (popr2->lmod < popr1->lmod)
2086 popr2->lmod = popr1->lmod;
2089 ferr(po, "conflicting lmods: %d vs %d\n",
2090 popr1->lmod, popr2->lmod);
2094 static const char *op_to_c(struct parsed_op *po)
2118 ferr(po, "op_to_c was supplied with %d\n", po->op);
2122 static void op_set_clear_flag(struct parsed_op *po,
2123 enum op_flags flag_set, enum op_flags flag_clear)
2125 po->flags |= flag_set;
2126 po->flags &= ~flag_clear;
2129 // last op in stream - unconditional branch or ret
2130 #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
2131 || ((ops[_i].flags & (OPF_JMP|OPF_CJMP|OPF_RMD)) == OPF_JMP \
2132 && ops[_i].op != OP_CALL))
2134 static int scan_for_pop(int i, int opcnt, const char *reg,
2135 int magic, int depth, int *maxdepth, int do_flags)
2137 const struct parsed_proto *pp;
2138 struct parsed_op *po;
2142 for (; i < opcnt; i++) {
2144 if (po->cc_scratch == magic)
2145 break; // already checked
2146 po->cc_scratch = magic;
2148 if (po->flags & OPF_TAIL) {
2149 if (po->op == OP_CALL) {
2150 pp = proto_parse(g_fhdr, po->operand[0].name, g_quiet_pp);
2151 if (pp != NULL && pp->is_noreturn)
2152 // no stack cleanup for noreturn
2155 return -1; // deadend
2158 if ((po->flags & OPF_RMD)
2159 || (po->op == OP_PUSH && po->p_argnum != 0)) // arg push
2162 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2163 if (po->btj != NULL) {
2165 for (j = 0; j < po->btj->count; j++) {
2166 ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic,
2167 depth, maxdepth, do_flags);
2169 return ret; // dead end
2175 ferr(po, "dead branch\n");
2179 if (po->flags & OPF_CJMP) {
2180 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic,
2181 depth, maxdepth, do_flags);
2183 return ret; // dead end
2191 if ((po->op == OP_POP || po->op == OP_PUSH)
2192 && po->operand[0].type == OPT_REG
2193 && IS(po->operand[0].name, reg))
2195 if (po->op == OP_PUSH && !(po->flags & OPF_FARGNR)) {
2197 if (depth > *maxdepth)
2200 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2202 else if (po->op == OP_POP) {
2205 op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
2210 if (depth < 0) // should not happen
2211 ferr(po, "fail with depth\n");
2213 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2222 // scan for pop starting from 'ret' op (all paths)
2223 static int scan_for_pop_ret(int i, int opcnt, const char *reg,
2229 for (; i < opcnt; i++) {
2230 if (!(ops[i].flags & OPF_TAIL))
2233 for (j = i - 1; j >= 0; j--) {
2234 if (ops[j].flags & OPF_RMD)
2236 if (ops[j].flags & OPF_JMP)
2239 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
2240 && IS(ops[j].operand[0].name, reg))
2243 ops[j].flags |= flag_set;
2247 if (g_labels[j] != NULL)
2252 return found ? 0 : -1;
2255 // XXX: merge with scan_for_pop?
2256 static void scan_for_pop_const(int i, int opcnt)
2260 for (j = i + 1; j < opcnt; j++) {
2261 if ((ops[j].flags & (OPF_JMP|OPF_TAIL|OPF_RSAVE))
2262 || ops[j].op == OP_PUSH || g_labels[i] != NULL)
2267 if (!(ops[j].flags & OPF_RMD) && ops[j].op == OP_POP)
2269 ops[i].flags |= OPF_RMD;
2270 ops[j].datap = &ops[i];
2276 static void scan_propagate_df(int i, int opcnt)
2278 struct parsed_op *po = &ops[i];
2281 for (; i < opcnt; i++) {
2283 if (po->flags & OPF_DF)
2284 return; // already resolved
2285 po->flags |= OPF_DF;
2287 if (po->op == OP_CALL)
2288 ferr(po, "call with DF set?\n");
2290 if (po->flags & OPF_JMP) {
2291 if (po->btj != NULL) {
2293 for (j = 0; j < po->btj->count; j++)
2294 scan_propagate_df(po->btj->d[j].bt_i, opcnt);
2299 ferr(po, "dead branch\n");
2303 if (po->flags & OPF_CJMP)
2304 scan_propagate_df(po->bt_i, opcnt);
2310 if (po->flags & OPF_TAIL)
2313 if (po->op == OP_CLD) {
2314 po->flags |= OPF_RMD;
2319 ferr(po, "missing DF clear?\n");
2322 // is operand 'opr' modified by parsed_op 'po'?
2323 static int is_opr_modified(const struct parsed_opr *opr,
2324 const struct parsed_op *po)
2328 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2331 if (opr->type == OPT_REG) {
2332 if (po->op == OP_CALL) {
2333 mask = (1 << xAX) | (1 << xCX) | (1 << xDX);
2334 if ((1 << opr->reg) & mask)
2340 if (po->operand[0].type == OPT_REG) {
2341 if (po->regmask_dst & (1 << opr->reg))
2348 return IS(po->operand[0].name, opr->name);
2351 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
2352 static int is_any_opr_modified(const struct parsed_op *po_test,
2353 const struct parsed_op *po, int c_mode)
2358 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2361 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2364 if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
2367 // in reality, it can wreck any register, but in decompiled C
2368 // version it can only overwrite eax or edx:eax
2369 mask = (1 << xAX) | (1 << xDX);
2373 if (po->op == OP_CALL
2374 && ((po_test->regmask_src | po_test->regmask_dst) & mask))
2377 for (i = 0; i < po_test->operand_cnt; i++)
2378 if (IS(po_test->operand[i].name, po->operand[0].name))
2384 // scan for any po_test operand modification in range given
2385 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
2388 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2391 for (; i < opcnt; i++) {
2392 if (is_any_opr_modified(po_test, &ops[i], c_mode))
2399 // scan for po_test operand[0] modification in range given
2400 static int scan_for_mod_opr0(struct parsed_op *po_test,
2403 for (; i < opcnt; i++) {
2404 if (is_opr_modified(&po_test->operand[0], &ops[i]))
2411 #define check_i(po, i) \
2413 ferr(po, "bad " #i ": %d\n", i)
2415 static int scan_for_flag_set(int i, int magic, int *branched,
2416 int *setters, int *setter_cnt)
2418 struct label_ref *lr;
2422 if (ops[i].cc_scratch == magic) {
2423 ferr(&ops[i], "%s looped\n", __func__);
2426 ops[i].cc_scratch = magic;
2428 if (g_labels[i] != NULL) {
2431 lr = &g_label_refs[i];
2432 for (; lr->next; lr = lr->next) {
2433 check_i(&ops[i], lr->i);
2434 ret = scan_for_flag_set(lr->i, magic,
2435 branched, setters, setter_cnt);
2440 check_i(&ops[i], lr->i);
2441 if (i > 0 && LAST_OP(i - 1)) {
2445 ret = scan_for_flag_set(lr->i, magic,
2446 branched, setters, setter_cnt);
2452 if (ops[i].flags & OPF_FLAGS) {
2453 setters[*setter_cnt] = i;
2458 if ((ops[i].flags & (OPF_JMP|OPF_CJMP)) == OPF_JMP)
2465 // scan back for cdq, if anything modifies edx, fail
2466 static int scan_for_cdq_edx(int i)
2469 if (g_labels[i] != NULL) {
2470 if (g_label_refs[i].next != NULL)
2472 if (i > 0 && LAST_OP(i - 1)) {
2473 i = g_label_refs[i].i;
2480 if (ops[i].op == OP_CDQ)
2483 if (ops[i].regmask_dst & (1 << xDX))
2490 static int scan_for_reg_clear(int i, int reg)
2493 if (g_labels[i] != NULL) {
2494 if (g_label_refs[i].next != NULL)
2496 if (i > 0 && LAST_OP(i - 1)) {
2497 i = g_label_refs[i].i;
2504 if (ops[i].op == OP_XOR
2505 && ops[i].operand[0].lmod == OPLM_DWORD
2506 && ops[i].operand[0].reg == ops[i].operand[1].reg
2507 && ops[i].operand[0].reg == reg)
2510 if (ops[i].regmask_dst & (1 << reg))
2517 // scan for positive, constant esp adjust
2518 static int scan_for_esp_adjust(int i, int opcnt,
2519 unsigned int adj_expect, int *adj, int *multipath)
2521 struct parsed_op *po;
2524 *adj = *multipath = 0;
2526 for (; i < opcnt && *adj < adj_expect; i++) {
2529 if (g_labels[i] != NULL)
2532 if (po->op == OP_ADD && po->operand[0].reg == xSP) {
2533 if (po->operand[1].type != OPT_CONST)
2534 ferr(&ops[i], "non-const esp adjust?\n");
2535 *adj += po->operand[1].val;
2537 ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
2540 else if (po->op == OP_PUSH && !(po->flags & OPF_RMD)) {
2541 //if (first_pop == -1)
2542 // first_pop = -2; // none
2543 *adj -= lmod_bytes(po, po->operand[0].lmod);
2545 else if (po->op == OP_POP && !(po->flags & OPF_RMD)) {
2546 // seems like msvc only uses 'pop ecx' for stack realignment..
2547 if (po->operand[0].type != OPT_REG || po->operand[0].reg != xCX)
2549 if (first_pop == -1 && *adj >= 0)
2551 *adj += lmod_bytes(po, po->operand[0].lmod);
2553 else if (po->flags & (OPF_JMP|OPF_TAIL)) {
2554 if (po->op == OP_JMP && po->btj == NULL) {
2558 if (po->op != OP_CALL)
2560 if (po->operand[0].type != OPT_LABEL)
2562 if (po->pp != NULL && po->pp->is_stdcall)
2567 if (first_pop >= 0) {
2568 // probably 'pop ecx' was used..
2575 static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
2577 struct parsed_op *po;
2581 ferr(ops, "%s: followed bad branch?\n", __func__);
2583 for (; i < opcnt; i++) {
2585 if (po->cc_scratch == magic)
2587 po->cc_scratch = magic;
2590 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2591 if (po->btj != NULL) {
2593 for (j = 0; j < po->btj->count; j++)
2594 scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
2598 scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
2599 if (!(po->flags & OPF_CJMP))
2602 if (po->flags & OPF_TAIL)
2607 static const struct parsed_proto *try_recover_pp(
2608 struct parsed_op *po, const struct parsed_opr *opr, int *search_instead)
2610 const struct parsed_proto *pp = NULL;
2614 // maybe an arg of g_func?
2615 if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
2617 char ofs_reg[16] = { 0, };
2618 int arg, arg_s, arg_i;
2625 parse_stack_access(po, opr->name, ofs_reg,
2626 &offset, &stack_ra, NULL, 0);
2627 if (ofs_reg[0] != 0)
2628 ferr(po, "offset reg on arg access?\n");
2629 if (offset <= stack_ra) {
2630 // search who set the stack var instead
2631 if (search_instead != NULL)
2632 *search_instead = 1;
2636 arg_i = (offset - stack_ra - 4) / 4;
2637 for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
2638 if (g_func_pp->arg[arg].reg != NULL)
2644 if (arg == g_func_pp->argc)
2645 ferr(po, "stack arg %d not in prototype?\n", arg_i);
2647 pp = g_func_pp->arg[arg].fptr;
2649 ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1);
2650 check_func_pp(po, pp, "icall arg");
2652 else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
2654 p = strchr(opr->name + 1, '[');
2655 memcpy(buf, opr->name, p - opr->name);
2656 buf[p - opr->name] = 0;
2657 pp = proto_parse(g_fhdr, buf, g_quiet_pp);
2659 else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
2660 pp = proto_parse(g_fhdr, opr->name, g_quiet_pp);
2663 ferr(po, "proto_parse failed for icall to '%s'\n", opr->name);
2666 check_func_pp(po, pp, "reg-fptr ref");
2672 static void scan_for_call_type(int i, const struct parsed_opr *opr,
2673 int magic, const struct parsed_proto **pp_found, int *multi)
2675 const struct parsed_proto *pp = NULL;
2676 struct parsed_op *po;
2677 struct label_ref *lr;
2679 ops[i].cc_scratch = magic;
2682 if (g_labels[i] != NULL) {
2683 lr = &g_label_refs[i];
2684 for (; lr != NULL; lr = lr->next) {
2685 check_i(&ops[i], lr->i);
2686 scan_for_call_type(lr->i, opr, magic, pp_found, multi);
2688 if (i > 0 && LAST_OP(i - 1))
2696 if (ops[i].cc_scratch == magic)
2698 ops[i].cc_scratch = magic;
2700 if (!(ops[i].flags & OPF_DATA))
2702 if (!is_opr_modified(opr, &ops[i]))
2704 if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
2705 // most probably trashed by some processing
2710 opr = &ops[i].operand[1];
2711 if (opr->type != OPT_REG)
2715 po = (i >= 0) ? &ops[i] : ops;
2718 // reached the top - can only be an arg-reg
2719 if (opr->type != OPT_REG)
2722 for (i = 0; i < g_func_pp->argc; i++) {
2723 if (g_func_pp->arg[i].reg == NULL)
2725 if (IS(opr->name, g_func_pp->arg[i].reg))
2728 if (i == g_func_pp->argc)
2730 pp = g_func_pp->arg[i].fptr;
2732 ferr(po, "icall: arg%d (%s) is not a fptr?\n",
2733 i + 1, g_func_pp->arg[i].reg);
2734 check_func_pp(po, pp, "icall reg-arg");
2737 pp = try_recover_pp(po, opr, NULL);
2739 if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
2740 if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
2741 || (*pp_found)->is_stdcall != pp->is_stdcall
2742 || (*pp_found)->is_fptr != pp->is_fptr
2743 || (*pp_found)->argc != pp->argc
2744 || (*pp_found)->argc_reg != pp->argc_reg
2745 || (*pp_found)->argc_stack != pp->argc_stack)
2747 ferr(po, "icall: parsed_proto mismatch\n");
2755 // early check for tail call or branch back
2756 static int is_like_tailjmp(int j)
2758 if (!(ops[j].flags & OPF_JMP))
2761 if (ops[j].op == OP_JMP && !ops[j].operand[0].had_ds)
2762 // probably local branch back..
2764 if (ops[j].op == OP_CALL)
2765 // probably noreturn call..
2771 static void scan_prologue_epilogue(int opcnt)
2773 int ecx_push = 0, esp_sub = 0;
2777 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
2778 && ops[1].op == OP_MOV
2779 && IS(opr_name(&ops[1], 0), "ebp")
2780 && IS(opr_name(&ops[1], 1), "esp"))
2783 ops[0].flags |= OPF_RMD;
2784 ops[1].flags |= OPF_RMD;
2787 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
2788 g_stack_fsz = opr_const(&ops[2], 1);
2789 ops[2].flags |= OPF_RMD;
2793 // another way msvc builds stack frame..
2795 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
2797 ops[i].flags |= OPF_RMD;
2801 // and another way..
2802 if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
2803 && ops[i].operand[1].type == OPT_CONST
2804 && ops[i + 1].op == OP_CALL
2805 && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
2807 g_stack_fsz += ops[i].operand[1].val;
2808 ops[i].flags |= OPF_RMD;
2810 ops[i].flags |= OPF_RMD;
2817 for (; i < opcnt; i++)
2818 if (ops[i].op == OP_RET)
2821 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
2822 if (found && is_like_tailjmp(j))
2827 if ((ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp"))
2828 || ops[j].op == OP_LEAVE)
2830 ops[j].flags |= OPF_RMD;
2832 else if (!(g_ida_func_attr & IDAFA_NORETURN))
2833 ferr(&ops[j], "'pop ebp' expected\n");
2835 if (g_stack_fsz != 0) {
2836 if (ops[j - 1].op == OP_MOV
2837 && IS(opr_name(&ops[j - 1], 0), "esp")
2838 && IS(opr_name(&ops[j - 1], 1), "ebp"))
2840 ops[j - 1].flags |= OPF_RMD;
2842 else if (ops[j].op != OP_LEAVE
2843 && !(g_ida_func_attr & IDAFA_NORETURN))
2845 ferr(&ops[j - 1], "esp restore expected\n");
2848 if (ecx_push && ops[j - 2].op == OP_POP
2849 && IS(opr_name(&ops[j - 2], 0), "ecx"))
2851 ferr(&ops[j - 2], "unexpected ecx pop\n");
2857 } while (i < opcnt);
2864 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
2865 ops[i].flags |= OPF_RMD;
2871 for (; i < opcnt; i++) {
2872 if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
2874 if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
2875 && ops[i].operand[1].type == OPT_CONST)
2877 g_stack_fsz = ops[i].operand[1].val;
2878 ops[i].flags |= OPF_RMD;
2884 if (ecx_push && !esp_sub) {
2885 // could actually be args for a call..
2886 for (; i < opcnt; i++)
2887 if (ops[i].op != OP_PUSH)
2890 if (ops[i].op == OP_CALL && ops[i].operand[0].type == OPT_LABEL) {
2891 const struct parsed_proto *pp;
2892 pp = proto_parse(g_fhdr, opr_name(&ops[i], 0), 1);
2893 j = pp ? pp->argc_stack : 0;
2894 while (i > 0 && j > 0) {
2896 if (ops[i].op == OP_PUSH) {
2897 ops[i].flags &= ~OPF_RMD;
2902 ferr(&ops[i], "unhandled prologue\n");
2905 i = g_stack_fsz = ecx_push = 0;
2906 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
2907 if (!(ops[i].flags & OPF_RMD))
2917 if (ecx_push || esp_sub)
2923 for (; i < opcnt; i++)
2924 if (ops[i].op == OP_RET)
2927 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
2928 if (found && is_like_tailjmp(j))
2934 for (l = 0; l < ecx_push; l++) {
2935 if (ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ecx"))
2937 else if (ops[j].op == OP_ADD
2938 && IS(opr_name(&ops[j], 0), "esp")
2939 && ops[j].operand[1].type == OPT_CONST)
2942 ecx_push -= ops[j].operand[1].val / 4 - 1;
2945 ferr(&ops[j], "'pop ecx' expected\n");
2947 ops[j].flags |= OPF_RMD;
2951 ferr(&ops[j], "epilogue scan failed\n");
2957 if (ops[j].op != OP_ADD
2958 || !IS(opr_name(&ops[j], 0), "esp")
2959 || ops[j].operand[1].type != OPT_CONST
2960 || ops[j].operand[1].val != g_stack_fsz)
2961 ferr(&ops[j], "'add esp' expected\n");
2963 ops[j].flags |= OPF_RMD;
2964 ops[j].operand[1].val = 0; // hack for stack arg scanner
2969 } while (i < opcnt);
2973 static const struct parsed_proto *resolve_icall(int i, int opcnt,
2976 const struct parsed_proto *pp = NULL;
2977 int search_advice = 0;
2981 switch (ops[i].operand[0].type) {
2985 pp = try_recover_pp(&ops[i], &ops[i].operand[0], &search_advice);
2990 scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
2998 // find an instruction that changed opr before i op
2999 // *op_i must be set to -1 by caller
3000 // *entry is set to 1 if one source is determined to be the caller
3001 // returns 1 if found, *op_i is then set to origin
3002 static int resolve_origin(int i, const struct parsed_opr *opr,
3003 int magic, int *op_i, int *is_caller)
3005 struct label_ref *lr;
3008 if (ops[i].cc_scratch == magic)
3010 ops[i].cc_scratch = magic;
3013 if (g_labels[i] != NULL) {
3014 lr = &g_label_refs[i];
3015 for (; lr != NULL; lr = lr->next) {
3016 check_i(&ops[i], lr->i);
3017 ret |= resolve_origin(lr->i, opr, magic, op_i, is_caller);
3019 if (i > 0 && LAST_OP(i - 1))
3025 if (is_caller != NULL)
3030 if (ops[i].cc_scratch == magic)
3032 ops[i].cc_scratch = magic;
3034 if (!(ops[i].flags & OPF_DATA))
3036 if (!is_opr_modified(opr, &ops[i]))
3042 // XXX: could check if the other op does the same
3051 static int try_resolve_const(int i, const struct parsed_opr *opr,
3052 int magic, unsigned int *val)
3057 ret = resolve_origin(i, opr, magic, &s_i, NULL);
3060 if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST)
3063 *val = ops[i].operand[1].val;
3070 static struct parsed_proto *process_call(int i, int opcnt)
3072 struct parsed_op *po = &ops[i];
3073 const struct parsed_proto *pp_c;
3074 struct parsed_proto *pp;
3075 const char *tmpname;
3079 tmpname = opr_name(po, 0);
3084 pp_c = resolve_icall(i, opcnt, &l);
3086 if (!pp_c->is_func && !pp_c->is_fptr)
3087 ferr(po, "call to non-func: %s\n", pp_c->name);
3088 pp = proto_clone(pp_c);
3089 my_assert_not(pp, NULL);
3091 // not resolved just to single func
3094 switch (po->operand[0].type) {
3096 // we resolved this call and no longer need the register
3097 po->regmask_src &= ~(1 << po->operand[0].reg);
3107 pp = calloc(1, sizeof(*pp));
3108 my_assert_not(pp, NULL);
3111 ret = scan_for_esp_adjust(i + 1, opcnt, ~0, &j, &l);
3112 if (ret < 0 || j < 0) {
3113 if (!g_allow_regfunc)
3114 ferr(po, "non-__cdecl indirect call unhandled yet\n");
3115 pp->is_unresolved = 1;
3119 if (j > ARRAY_SIZE(pp->arg))
3120 ferr(po, "esp adjust too large: %d\n", j);
3121 pp->ret_type.name = strdup("int");
3122 pp->argc = pp->argc_stack = j;
3123 for (arg = 0; arg < pp->argc; arg++)
3124 pp->arg[arg].type.name = strdup("int");
3129 // look for and make use of esp adjust
3131 if (!pp->is_stdcall && pp->argc_stack > 0)
3132 ret = scan_for_esp_adjust(i + 1, opcnt,
3133 pp->argc_stack * 4, &j, &l);
3135 if (pp->is_vararg) {
3136 if (j / 4 < pp->argc_stack)
3137 ferr(po, "esp adjust is too small: %x < %x\n",
3138 j, pp->argc_stack * 4);
3139 // modify pp to make it have varargs as normal args
3141 pp->argc += j / 4 - pp->argc_stack;
3142 for (; arg < pp->argc; arg++) {
3143 pp->arg[arg].type.name = strdup("int");
3146 if (pp->argc > ARRAY_SIZE(pp->arg))
3147 ferr(po, "too many args for '%s'\n", tmpname);
3149 if (pp->argc_stack > j / 4) {
3150 fnote(po, "(this call)\n");
3151 ferr(&ops[ret], "stack tracking failed for '%s': %x %x\n",
3152 tmpname, pp->argc_stack * 4, j);
3155 ops[ret].flags |= OPF_RMD;
3156 if (ops[ret].op == OP_POP) {
3158 // deal with multi-pop stack adjust
3160 while (ops[ret].op == OP_POP && j > 0 && ret < opcnt) {
3161 ops[ret].flags |= OPF_RMD;
3168 // a bit of a hack, but deals with use of
3169 // single adj for multiple calls
3170 ops[ret].operand[1].val -= j;
3173 else if (pp->is_vararg)
3174 ferr(po, "missing esp_adjust for vararg func '%s'\n",
3180 static int collect_call_args_r(struct parsed_op *po, int i,
3181 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
3182 int *arg_grp, int arg, int magic, int need_op_saving, int may_reuse)
3184 struct parsed_proto *pp_tmp;
3185 struct parsed_op *po_tmp;
3186 struct label_ref *lr;
3187 int need_to_save_current;
3188 int arg_grp_current = 0;
3189 int save_args_seen = 0;
3197 ferr(po, "dead label encountered\n");
3201 for (; arg < pp->argc; arg++)
3202 if (pp->arg[arg].reg == NULL)
3204 magic = (magic & 0xffffff) | (arg << 24);
3206 for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
3208 if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
3209 if (ops[j].cc_scratch != magic) {
3210 ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
3214 // ok: have already been here
3217 ops[j].cc_scratch = magic;
3219 if (g_labels[j] != NULL && g_label_refs[j].i != -1) {
3220 lr = &g_label_refs[j];
3221 if (lr->next != NULL)
3223 for (; lr->next; lr = lr->next) {
3224 check_i(&ops[j], lr->i);
3225 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
3227 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
3228 arg_grp, arg, magic, need_op_saving, may_reuse);
3233 check_i(&ops[j], lr->i);
3234 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
3236 if (j > 0 && LAST_OP(j - 1)) {
3237 // follow last branch in reverse
3242 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
3243 arg_grp, arg, magic, need_op_saving, may_reuse);
3249 if (ops[j].op == OP_CALL)
3251 if (pp->is_unresolved)
3256 ferr(po, "arg collect hit unparsed call '%s'\n",
3257 ops[j].operand[0].name);
3258 if (may_reuse && pp_tmp->argc_stack > 0)
3259 ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
3260 arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
3262 // esp adjust of 0 means we collected it before
3263 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP
3264 && (ops[j].operand[1].type != OPT_CONST
3265 || ops[j].operand[1].val != 0))
3267 if (pp->is_unresolved)
3270 ferr(po, "arg collect %d/%d hit esp adjust of %d\n",
3271 arg, pp->argc, ops[j].operand[1].val);
3273 else if (ops[j].op == OP_POP && !(ops[j].flags & OPF_RMD)
3274 && ops[j].datap == NULL)
3276 if (pp->is_unresolved)
3279 ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc);
3281 else if (ops[j].flags & OPF_CJMP)
3283 if (pp->is_unresolved)
3288 else if (ops[j].op == OP_PUSH && !(ops[j].flags & OPF_FARGNR))
3290 if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
3293 ops[j].p_argnext = -1;
3294 po_tmp = pp->arg[arg].datap;
3296 ops[j].p_argnext = po_tmp - ops;
3297 pp->arg[arg].datap = &ops[j];
3299 need_to_save_current = 0;
3302 if (ops[j].operand[0].type == OPT_REG)
3303 reg = ops[j].operand[0].reg;
3305 if (!need_op_saving) {
3306 ret = scan_for_mod(&ops[j], j + 1, i, 1);
3307 need_to_save_current = (ret >= 0);
3309 if (need_op_saving || need_to_save_current) {
3310 // mark this push as one that needs operand saving
3311 ops[j].flags &= ~OPF_RMD;
3312 if (ops[j].p_argnum == 0) {
3313 ops[j].p_argnum = arg + 1;
3314 save_args |= 1 << arg;
3316 else if (ops[j].p_argnum < arg + 1) {
3317 // XXX: might kill valid var..
3318 //*save_arg_vars &= ~(1 << (ops[j].p_argnum - 1));
3319 ops[j].p_argnum = arg + 1;
3320 save_args |= 1 << arg;
3323 if (save_args_seen & (1 << (ops[j].p_argnum - 1))) {
3326 if (arg_grp_current >= MAX_ARG_GRP)
3327 ferr(&ops[j], "out of arg groups (arg%d), f %s\n",
3328 ops[j].p_argnum, pp->name);
3331 else if (ops[j].p_argnum == 0)
3332 ops[j].flags |= OPF_RMD;
3334 // some PUSHes are reused by different calls on other branches,
3335 // but that can't happen if we didn't branch, so they
3336 // can be removed from future searches (handles nested calls)
3338 ops[j].flags |= OPF_FARGNR;
3340 ops[j].flags |= OPF_FARG;
3341 ops[j].flags &= ~OPF_RSAVE;
3343 // check for __VALIST
3344 if (!pp->is_unresolved && pp->arg[arg].type.is_va_list) {
3346 ret = resolve_origin(j, &ops[j].operand[0],
3347 magic + 1, &k, NULL);
3348 if (ret == 1 && k >= 0)
3350 if (ops[k].op == OP_LEA) {
3351 snprintf(buf, sizeof(buf), "arg_%X",
3352 g_func_pp->argc_stack * 4);
3353 if (!g_func_pp->is_vararg
3354 || strstr(ops[k].operand[1].name, buf))
3356 ops[k].flags |= OPF_RMD;
3357 ops[j].flags |= OPF_RMD | OPF_VAPUSH;
3358 save_args &= ~(1 << arg);
3362 ferr(&ops[j], "lea va_list used, but no vararg?\n");
3364 // check for va_list from g_func_pp arg too
3365 else if (ops[k].op == OP_MOV
3366 && is_stack_access(&ops[k], &ops[k].operand[1]))
3368 ret = stack_frame_access(&ops[k], &ops[k].operand[1],
3369 buf, sizeof(buf), ops[k].operand[1].name, "", 1, 0);
3371 ops[k].flags |= OPF_RMD;
3372 ops[j].flags |= OPF_RMD;
3373 ops[j].p_argpass = ret + 1;
3374 save_args &= ~(1 << arg);
3381 *save_arg_vars |= save_args;
3383 // tracking reg usage
3385 *regmask |= 1 << reg;
3388 if (!pp->is_unresolved) {
3390 for (; arg < pp->argc; arg++)
3391 if (pp->arg[arg].reg == NULL)
3394 magic = (magic & 0xffffff) | (arg << 24);
3397 if (ops[j].p_arggrp > arg_grp_current) {
3399 arg_grp_current = ops[j].p_arggrp;
3401 if (ops[j].p_argnum > 0)
3402 save_args_seen |= 1 << (ops[j].p_argnum - 1);
3405 if (arg < pp->argc) {
3406 ferr(po, "arg collect failed for '%s': %d/%d\n",
3407 pp->name, arg, pp->argc);
3411 if (arg_grp_current > *arg_grp)
3412 *arg_grp = arg_grp_current;
3417 static int collect_call_args(struct parsed_op *po, int i,
3418 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
3421 // arg group is for cases when pushes for
3422 // multiple funcs are going on
3423 struct parsed_op *po_tmp;
3424 int save_arg_vars_current = 0;
3429 ret = collect_call_args_r(po, i, pp, regmask,
3430 &save_arg_vars_current, &arg_grp, 0, magic, 0, 0);
3435 // propagate arg_grp
3436 for (a = 0; a < pp->argc; a++) {
3437 if (pp->arg[a].reg != NULL)
3440 po_tmp = pp->arg[a].datap;
3441 while (po_tmp != NULL) {
3442 po_tmp->p_arggrp = arg_grp;
3443 if (po_tmp->p_argnext > 0)
3444 po_tmp = &ops[po_tmp->p_argnext];
3450 save_arg_vars[arg_grp] |= save_arg_vars_current;
3452 if (pp->is_unresolved) {
3454 pp->argc_stack += ret;
3455 for (a = 0; a < pp->argc; a++)
3456 if (pp->arg[a].type.name == NULL)
3457 pp->arg[a].type.name = strdup("int");
3463 static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
3467 for (i = 0; i < pp->argc; i++)
3468 if (pp->arg[i].reg == NULL)
3472 memmove(&pp->arg[i + 1], &pp->arg[i],
3473 sizeof(pp->arg[0]) * pp->argc_stack);
3474 memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
3475 pp->arg[i].reg = strdup(reg);
3476 pp->arg[i].type.name = strdup("int");
3481 static void add_label_ref(struct label_ref *lr, int op_i)
3483 struct label_ref *lr_new;
3490 lr_new = calloc(1, sizeof(*lr_new));
3492 lr_new->next = lr->next;
3496 static struct parsed_data *try_resolve_jumptab(int i, int opcnt)
3498 struct parsed_op *po = &ops[i];
3499 struct parsed_data *pd;
3500 char label[NAMELEN], *p;
3503 p = strchr(po->operand[0].name, '[');
3507 len = p - po->operand[0].name;
3508 strncpy(label, po->operand[0].name, len);
3511 for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
3512 if (IS(g_func_pd[j].label, label)) {
3518 //ferr(po, "label '%s' not parsed?\n", label);
3521 if (pd->type != OPT_OFFSET)
3522 ferr(po, "label '%s' with non-offset data?\n", label);
3524 // find all labels, link
3525 for (j = 0; j < pd->count; j++) {
3526 for (l = 0; l < opcnt; l++) {
3527 if (g_labels[l] != NULL && IS(g_labels[l], pd->d[j].u.label)) {
3528 add_label_ref(&g_label_refs[l], i);
3538 static void clear_labels(int count)
3542 for (i = 0; i < count; i++) {
3543 if (g_labels[i] != NULL) {
3550 static void output_std_flags(FILE *fout, struct parsed_op *po,
3551 int *pfomask, const char *dst_opr_text)
3553 if (*pfomask & (1 << PFO_Z)) {
3554 fprintf(fout, "\n cond_z = (%s%s == 0);",
3555 lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
3556 *pfomask &= ~(1 << PFO_Z);
3558 if (*pfomask & (1 << PFO_S)) {
3559 fprintf(fout, "\n cond_s = (%s%s < 0);",
3560 lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
3561 *pfomask &= ~(1 << PFO_S);
3565 static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp,
3568 if (pp->is_fastcall)
3569 fprintf(fout, "__fastcall ");
3570 else if (pp->is_stdcall && pp->argc_reg == 0)
3571 fprintf(fout, "__stdcall ");
3572 if (pp->is_noreturn || is_noreturn)
3573 fprintf(fout, "noreturn ");
3576 static int get_pp_arg_regmask(const struct parsed_proto *pp)
3581 for (i = 0; i < pp->argc; i++) {
3582 if (pp->arg[i].reg != NULL) {
3583 reg = char_array_i(regs_r32,
3584 ARRAY_SIZE(regs_r32), pp->arg[i].reg);
3586 ferr(ops, "arg '%s' of func '%s' is not a reg?\n",
3587 pp->arg[i].reg, pp->name);
3588 regmask |= 1 << reg;
3595 static char *saved_arg_name(char *buf, size_t buf_size, int grp, int num)
3601 snprintf(buf1, sizeof(buf1), "%d", grp);
3602 snprintf(buf, buf_size, "s%s_a%d", buf1, num);
3607 static void gen_x_cleanup(int opcnt);
3609 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
3611 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
3612 struct parsed_opr *last_arith_dst = NULL;
3613 char buf1[256], buf2[256], buf3[256], cast[64];
3614 const struct parsed_proto *pp_c;
3615 struct parsed_proto *pp, *pp_tmp;
3616 struct parsed_data *pd;
3617 const char *tmpname;
3619 int save_arg_vars[MAX_ARG_GRP] = { 0, };
3621 int need_tmp_var = 0;
3624 int label_pending = 0;
3625 int regmask_save = 0;
3626 int regmask_arg = 0;
3627 int regmask_now = 0;
3628 int regmask_init = 0;
3639 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
3640 g_stack_frame_used = 0;
3642 g_func_pp = proto_parse(fhdr, funcn, 0);
3643 if (g_func_pp == NULL)
3644 ferr(ops, "proto_parse failed for '%s'\n", funcn);
3646 regmask_arg = get_pp_arg_regmask(g_func_pp);
3649 // - handle ebp/esp frame, remove ops related to it
3650 scan_prologue_epilogue(opcnt);
3653 // - parse calls with labels
3654 // - resolve all branches
3655 for (i = 0; i < opcnt; i++)
3661 if (po->flags & OPF_RMD)
3664 if (po->op == OP_CALL) {
3667 if (po->operand[0].type == OPT_LABEL) {
3668 tmpname = opr_name(po, 0);
3669 if (IS_START(tmpname, "loc_"))
3670 ferr(po, "call to loc_*\n");
3671 pp_c = proto_parse(fhdr, tmpname, 0);
3673 ferr(po, "proto_parse failed for call '%s'\n", tmpname);
3675 pp = proto_clone(pp_c);
3676 my_assert_not(pp, NULL);
3678 else if (po->datap != NULL) {
3679 pp = calloc(1, sizeof(*pp));
3680 my_assert_not(pp, NULL);
3682 ret = parse_protostr(po->datap, pp);
3684 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
3691 check_func_pp(po, pp, "fptr var call");
3692 if (pp->is_noreturn)
3693 po->flags |= OPF_TAIL;
3699 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
3702 if (po->operand[0].type == OPT_REGMEM) {
3703 pd = try_resolve_jumptab(i, opcnt);
3711 for (l = 0; l < opcnt; l++) {
3712 if (g_labels[l] != NULL
3713 && IS(po->operand[0].name, g_labels[l]))
3715 if (l == i + 1 && po->op == OP_JMP) {
3716 // yet another alignment type..
3717 po->flags |= OPF_RMD;
3720 add_label_ref(&g_label_refs[l], i);
3726 if (po->bt_i != -1 || (po->flags & OPF_RMD))
3729 if (po->operand[0].type == OPT_LABEL)
3733 ferr(po, "unhandled branch\n");
3737 po->flags |= OPF_TAIL;
3738 if (i > 0 && ops[i - 1].op == OP_POP)
3739 po->flags |= OPF_ATAIL;
3744 // - remove dead labels
3746 for (i = 0; i < opcnt; i++)
3748 if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
3754 if (po->flags & OPF_RMD)
3757 if (po->op == OP_CALL)
3759 pp = process_call(i, opcnt);
3761 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
3762 // since we know the args, collect them
3763 collect_call_args(po, i, pp, ®mask, save_arg_vars,
3767 if (strstr(pp->ret_type.name, "int64"))
3773 // - find POPs for PUSHes, rm both
3774 // - scan for STD/CLD, propagate DF
3775 // - scan for all used registers
3776 // - find flag set ops for their users
3777 // - do unreselved calls
3778 // - declare indirect functions
3779 for (i = 0; i < opcnt; i++) {
3781 if (po->flags & OPF_RMD)
3784 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
3785 reg = po->operand[0].reg;
3786 if (!(regmask & (1 << reg)))
3787 // not a reg save after all, rerun scan_for_pop
3788 po->flags &= ~OPF_RSAVE;
3790 regmask_save |= 1 << reg;
3793 if (po->op == OP_PUSH && !(po->flags & OPF_FARG)
3794 && !(po->flags & OPF_RSAVE) && !g_func_pp->is_userstack)
3796 if (po->operand[0].type == OPT_REG)
3798 reg = po->operand[0].reg;
3800 ferr(po, "reg not set for push?\n");
3803 ret = scan_for_pop(i + 1, opcnt,
3804 po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
3807 ferr(po, "too much depth: %d\n", depth);
3809 po->flags |= OPF_RMD;
3810 scan_for_pop(i + 1, opcnt, po->operand[0].name,
3811 i + opcnt * 4, 0, &depth, 1);
3814 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
3817 if (regmask & (1 << reg)) {
3818 if (regmask_save & (1 << reg))
3819 ferr(po, "%s already saved?\n", po->operand[0].name);
3823 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg);
3827 else if (po->operand[0].type == OPT_CONST) {
3828 scan_for_pop_const(i, opcnt);
3832 if (po->op == OP_STD) {
3833 po->flags |= OPF_DF | OPF_RMD;
3834 scan_propagate_df(i + 1, opcnt);
3837 regmask_now = po->regmask_src | po->regmask_dst;
3838 if (regmask_now & (1 << xBP)) {
3839 if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
3840 if (po->regmask_dst & (1 << xBP))
3841 // compiler decided to drop bp frame and use ebp as scratch
3842 scan_fwd_set_flags(i + 1, opcnt, i + opcnt * 5, OPF_EBP_S);
3844 regmask_now &= ~(1 << xBP);
3848 regmask |= regmask_now;
3850 if (po->flags & OPF_CC)
3852 int setters[16], cnt = 0, branched = 0;
3854 ret = scan_for_flag_set(i, i + opcnt * 6,
3855 &branched, setters, &cnt);
3856 if (ret < 0 || cnt <= 0)
3857 ferr(po, "unable to trace flag setter(s)\n");
3858 if (cnt > ARRAY_SIZE(setters))
3859 ferr(po, "too many flag setters\n");
3861 for (j = 0; j < cnt; j++)
3863 tmp_op = &ops[setters[j]]; // flag setter
3866 // to get nicer code, we try to delay test and cmp;
3867 // if we can't because of operand modification, or if we
3868 // have arith op, or branch, make it calculate flags explicitly
3869 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP)
3871 if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
3872 pfomask = 1 << po->pfo;
3874 else if (tmp_op->op == OP_CMPS || tmp_op->op == OP_SCAS) {
3875 pfomask = 1 << po->pfo;
3878 // see if we'll be able to handle based on op result
3879 if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
3880 && po->pfo != PFO_Z && po->pfo != PFO_S
3881 && po->pfo != PFO_P)
3883 || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
3885 pfomask = 1 << po->pfo;
3888 if (tmp_op->op == OP_ADD && po->pfo == PFO_C) {
3889 propagate_lmod(tmp_op, &tmp_op->operand[0],
3890 &tmp_op->operand[1]);
3891 if (tmp_op->operand[0].lmod == OPLM_DWORD)
3896 tmp_op->pfomask |= pfomask;
3897 cond_vars |= pfomask;
3899 // note: may overwrite, currently not a problem
3903 if (po->op == OP_RCL || po->op == OP_RCR
3904 || po->op == OP_ADC || po->op == OP_SBB)
3905 cond_vars |= 1 << PFO_C;
3908 if (po->op == OP_CMPS || po->op == OP_SCAS) {
3909 cond_vars |= 1 << PFO_Z;
3911 else if (po->op == OP_MUL
3912 || (po->op == OP_IMUL && po->operand_cnt == 1))
3914 if (po->operand[0].lmod == OPLM_DWORD)
3917 else if (po->op == OP_CALL) {
3920 ferr(po, "NULL pp\n");
3922 if (pp->is_unresolved) {
3923 int regmask_stack = 0;
3924 collect_call_args(po, i, pp, ®mask, save_arg_vars,
3927 // this is pretty rough guess:
3928 // see ecx and edx were pushed (and not their saved versions)
3929 for (arg = 0; arg < pp->argc; arg++) {
3930 if (pp->arg[arg].reg != NULL)
3933 tmp_op = pp->arg[arg].datap;
3935 ferr(po, "parsed_op missing for arg%d\n", arg);
3936 if (tmp_op->p_argnum == 0 && tmp_op->operand[0].type == OPT_REG)
3937 regmask_stack |= 1 << tmp_op->operand[0].reg;
3940 if (!((regmask_stack & (1 << xCX))
3941 && (regmask_stack & (1 << xDX))))
3943 if (pp->argc_stack != 0
3944 || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX))))
3946 pp_insert_reg_arg(pp, "ecx");
3947 pp->is_fastcall = 1;
3948 regmask_init |= 1 << xCX;
3949 regmask |= 1 << xCX;
3951 if (pp->argc_stack != 0
3952 || ((regmask | regmask_arg) & (1 << xDX)))
3954 pp_insert_reg_arg(pp, "edx");
3955 regmask_init |= 1 << xDX;
3956 regmask |= 1 << xDX;
3960 // note: __cdecl doesn't fall into is_unresolved category
3961 if (pp->argc_stack > 0)
3965 for (arg = 0; arg < pp->argc; arg++) {
3966 if (pp->arg[arg].reg != NULL) {
3967 reg = char_array_i(regs_r32,
3968 ARRAY_SIZE(regs_r32), pp->arg[arg].reg);
3970 ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg);
3971 if (!(regmask & (1 << reg))) {
3972 regmask_init |= 1 << reg;
3973 regmask |= 1 << reg;
3978 else if (po->op == OP_MOV && po->operand[0].pp != NULL
3979 && po->operand[1].pp != NULL)
3981 // <var> = offset <something>
3982 if ((po->operand[1].pp->is_func || po->operand[1].pp->is_fptr)
3983 && !IS_START(po->operand[1].name, "off_"))
3985 if (!po->operand[0].pp->is_fptr)
3986 ferr(po, "%s not declared as fptr when it should be\n",
3987 po->operand[0].name);
3988 if (pp_cmp_func(po->operand[0].pp, po->operand[1].pp)) {
3989 pp_print(buf1, sizeof(buf1), po->operand[0].pp);
3990 pp_print(buf2, sizeof(buf2), po->operand[1].pp);
3991 fnote(po, "var: %s\n", buf1);
3992 fnote(po, "func: %s\n", buf2);
3993 ferr(po, "^ mismatch\n");
3997 else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void"))
3998 regmask |= 1 << xAX;
3999 else if (po->op == OP_DIV || po->op == OP_IDIV) {
4000 // 32bit division is common, look for it
4001 if (po->op == OP_DIV)
4002 ret = scan_for_reg_clear(i, xDX);
4004 ret = scan_for_cdq_edx(i);
4006 po->flags |= OPF_32BIT;
4010 else if (po->op == OP_CLD)
4011 po->flags |= OPF_RMD;
4013 if (po->op == OP_RCL || po->op == OP_RCR || po->op == OP_XCHG) {
4019 // - confirm regmask_save, it might have been reduced
4020 if (regmask_save != 0)
4023 for (i = 0; i < opcnt; i++) {
4025 if (po->flags & OPF_RMD)
4028 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE))
4029 regmask_save |= 1 << po->operand[0].reg;
4033 // output starts here
4035 // define userstack size
4036 if (g_func_pp->is_userstack) {
4037 fprintf(fout, "#ifndef US_SZ_%s\n", g_func_pp->name);
4038 fprintf(fout, "#define US_SZ_%s USERSTACK_SIZE\n", g_func_pp->name);
4039 fprintf(fout, "#endif\n");
4042 // the function itself
4043 fprintf(fout, "%s ", g_func_pp->ret_type.name);
4044 output_pp_attrs(fout, g_func_pp, g_ida_func_attr & IDAFA_NORETURN);
4045 fprintf(fout, "%s(", g_func_pp->name);
4047 for (i = 0; i < g_func_pp->argc; i++) {
4049 fprintf(fout, ", ");
4050 if (g_func_pp->arg[i].fptr != NULL) {
4052 pp = g_func_pp->arg[i].fptr;
4053 fprintf(fout, "%s (", pp->ret_type.name);
4054 output_pp_attrs(fout, pp, 0);
4055 fprintf(fout, "*a%d)(", i + 1);
4056 for (j = 0; j < pp->argc; j++) {
4058 fprintf(fout, ", ");
4059 if (pp->arg[j].fptr)
4060 ferr(ops, "nested fptr\n");
4061 fprintf(fout, "%s", pp->arg[j].type.name);
4063 if (pp->is_vararg) {
4065 fprintf(fout, ", ");
4066 fprintf(fout, "...");
4070 else if (g_func_pp->arg[i].type.is_retreg) {
4071 fprintf(fout, "u32 *r_%s", g_func_pp->arg[i].reg);
4074 fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1);
4077 if (g_func_pp->is_vararg) {
4079 fprintf(fout, ", ");
4080 fprintf(fout, "...");
4083 fprintf(fout, ")\n{\n");
4085 // declare indirect functions
4086 for (i = 0; i < opcnt; i++) {
4088 if (po->flags & OPF_RMD)
4091 if (po->op == OP_CALL) {
4094 ferr(po, "NULL pp\n");
4096 if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) {
4097 if (pp->name[0] != 0) {
4098 memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
4099 memcpy(pp->name, "i_", 2);
4101 // might be declared already
4103 for (j = 0; j < i; j++) {
4104 if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) {
4105 if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) {
4115 snprintf(pp->name, sizeof(pp->name), "icall%d", i);
4117 fprintf(fout, " %s (", pp->ret_type.name);
4118 output_pp_attrs(fout, pp, 0);
4119 fprintf(fout, "*%s)(", pp->name);
4120 for (j = 0; j < pp->argc; j++) {
4122 fprintf(fout, ", ");
4123 fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1);
4125 fprintf(fout, ");\n");
4130 // output LUTs/jumptables
4131 for (i = 0; i < g_func_pd_cnt; i++) {
4133 fprintf(fout, " static const ");
4134 if (pd->type == OPT_OFFSET) {
4135 fprintf(fout, "void *jt_%s[] =\n { ", pd->label);
4137 for (j = 0; j < pd->count; j++) {
4139 fprintf(fout, ", ");
4140 fprintf(fout, "&&%s", pd->d[j].u.label);
4144 fprintf(fout, "%s %s[] =\n { ",
4145 lmod_type_u(ops, pd->lmod), pd->label);
4147 for (j = 0; j < pd->count; j++) {
4149 fprintf(fout, ", ");
4150 fprintf(fout, "%u", pd->d[j].u.val);
4153 fprintf(fout, " };\n");
4157 // declare stack frame, va_arg
4159 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
4160 (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz);
4164 if (g_func_pp->is_userstack) {
4165 fprintf(fout, " u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name);
4166 fprintf(fout, " u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n");
4170 if (g_func_pp->is_vararg) {
4171 fprintf(fout, " va_list ap;\n");
4175 // declare arg-registers
4176 for (i = 0; i < g_func_pp->argc; i++) {
4177 if (g_func_pp->arg[i].reg != NULL) {
4178 reg = char_array_i(regs_r32,
4179 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
4180 if (regmask & (1 << reg)) {
4181 if (g_func_pp->arg[i].type.is_retreg)
4182 fprintf(fout, " u32 %s = *r_%s;\n",
4183 g_func_pp->arg[i].reg, g_func_pp->arg[i].reg);
4185 fprintf(fout, " u32 %s = (u32)a%d;\n",
4186 g_func_pp->arg[i].reg, i + 1);
4189 if (g_func_pp->arg[i].type.is_retreg)
4190 ferr(ops, "retreg '%s' is unused?\n",
4191 g_func_pp->arg[i].reg);
4192 fprintf(fout, " // %s = a%d; // unused\n",
4193 g_func_pp->arg[i].reg, i + 1);
4199 regmask_now = regmask & ~regmask_arg;
4200 regmask_now &= ~(1 << xSP);
4201 if (regmask_now & 0x00ff) {
4202 for (reg = 0; reg < 8; reg++) {
4203 if (regmask_now & (1 << reg)) {
4204 fprintf(fout, " u32 %s", regs_r32[reg]);
4205 if (regmask_init & (1 << reg))
4206 fprintf(fout, " = 0");
4207 fprintf(fout, ";\n");
4212 if (regmask_now & 0xff00) {
4213 for (reg = 8; reg < 16; reg++) {
4214 if (regmask_now & (1 << reg)) {
4215 fprintf(fout, " mmxr %s", regs_r32[reg]);
4216 if (regmask_init & (1 << reg))
4217 fprintf(fout, " = { 0, }");
4218 fprintf(fout, ";\n");
4225 for (reg = 0; reg < 8; reg++) {
4226 if (regmask_save & (1 << reg)) {
4227 fprintf(fout, " u32 s_%s;\n", regs_r32[reg]);
4233 for (i = 0; i < ARRAY_SIZE(save_arg_vars); i++) {
4234 if (save_arg_vars[i] == 0)
4236 for (reg = 0; reg < 32; reg++) {
4237 if (save_arg_vars[i] & (1 << reg)) {
4238 fprintf(fout, " u32 %s;\n",
4239 saved_arg_name(buf1, sizeof(buf1), i, reg + 1));
4246 for (i = 0; i < 8; i++) {
4247 if (cond_vars & (1 << i)) {
4248 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
4255 fprintf(fout, " u32 tmp;\n");
4260 fprintf(fout, " u64 tmp64;\n");
4265 fprintf(fout, "\n");
4267 if (g_func_pp->is_vararg) {
4268 if (g_func_pp->argc_stack == 0)
4269 ferr(ops, "vararg func without stack args?\n");
4270 fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc);
4274 for (i = 0; i < opcnt; i++)
4276 if (g_labels[i] != NULL) {
4277 fprintf(fout, "\n%s:\n", g_labels[i]);
4280 delayed_flag_op = NULL;
4281 last_arith_dst = NULL;
4285 if (po->flags & OPF_RMD)
4290 #define assert_operand_cnt(n_) \
4291 if (po->operand_cnt != n_) \
4292 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
4294 // conditional/flag using op?
4295 if (po->flags & OPF_CC)
4301 // we go through all this trouble to avoid using parsed_flag_op,
4302 // which makes generated code much nicer
4303 if (delayed_flag_op != NULL)
4305 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op,
4306 po->pfo, po->pfo_inv);
4309 else if (last_arith_dst != NULL
4310 && (po->pfo == PFO_Z || po->pfo == PFO_S || po->pfo == PFO_P
4311 || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
4314 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
4315 out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv,
4316 last_arith_dst->lmod, buf3);
4319 else if (tmp_op != NULL) {
4320 // use preprocessed flag calc results
4321 if (!(tmp_op->pfomask & (1 << po->pfo)))
4322 ferr(po, "not prepared for pfo %d\n", po->pfo);
4324 // note: pfo_inv was not yet applied
4325 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
4326 po->pfo_inv ? "!" : "", parsed_flag_op_names[po->pfo]);
4329 ferr(po, "all methods of finding comparison failed\n");
4332 if (po->flags & OPF_JMP) {
4333 fprintf(fout, " if %s", buf1);
4335 else if (po->op == OP_RCL || po->op == OP_RCR
4336 || po->op == OP_ADC || po->op == OP_SBB)
4339 fprintf(fout, " cond_%s = %s;\n",
4340 parsed_flag_op_names[po->pfo], buf1);
4342 else if (po->flags & OPF_DATA) { // SETcc
4343 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
4344 fprintf(fout, " %s = %s;", buf2, buf1);
4347 ferr(po, "unhandled conditional op\n");
4351 pfomask = po->pfomask;
4353 if (po->flags & (OPF_REPZ|OPF_REPNZ)) {
4354 struct parsed_opr opr = {0,};
4357 opr.lmod = OPLM_DWORD;
4358 ret = try_resolve_const(i, &opr, opcnt * 7 + i, &uval);
4360 if (ret != 1 || uval == 0) {
4361 // we need initial flags for ecx=0 case..
4362 if (i > 0 && ops[i - 1].op == OP_XOR
4363 && IS(ops[i - 1].operand[0].name,
4364 ops[i - 1].operand[1].name))
4366 fprintf(fout, " cond_z = ");
4367 if (pfomask & (1 << PFO_C))
4368 fprintf(fout, "cond_c = ");
4369 fprintf(fout, "0;\n");
4371 else if (last_arith_dst != NULL) {
4372 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
4373 out_test_for_cc(buf1, sizeof(buf1), po, PFO_Z, 0,
4374 last_arith_dst->lmod, buf3);
4375 fprintf(fout, " cond_z = %s;\n", buf1);
4378 ferr(po, "missing initial ZF\n");
4385 assert_operand_cnt(2);
4386 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4387 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4388 default_cast_to(buf3, sizeof(buf3), &po->operand[0]);
4389 fprintf(fout, " %s = %s;", buf1,
4390 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4395 assert_operand_cnt(2);
4396 po->operand[1].lmod = OPLM_DWORD; // always
4397 fprintf(fout, " %s = %s;",
4398 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4399 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4404 assert_operand_cnt(2);
4405 fprintf(fout, " %s = %s;",
4406 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4407 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4411 assert_operand_cnt(2);
4412 switch (po->operand[1].lmod) {
4414 strcpy(buf3, "(s8)");
4417 strcpy(buf3, "(s16)");
4420 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
4422 fprintf(fout, " %s = %s;",
4423 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4424 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4429 assert_operand_cnt(2);
4430 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4431 fprintf(fout, " tmp = %s;",
4432 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0));
4433 fprintf(fout, " %s = %s;",
4434 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4435 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4436 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
4437 fprintf(fout, " %s = %stmp;",
4438 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
4439 default_cast_to(buf3, sizeof(buf3), &po->operand[1]));
4440 snprintf(g_comment, sizeof(g_comment), "xchg");
4444 assert_operand_cnt(1);
4445 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4446 fprintf(fout, " %s = ~%s;", buf1, buf1);
4450 assert_operand_cnt(2);
4451 fprintf(fout, " %s = (s32)%s >> 31;",
4452 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4453 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4454 strcpy(g_comment, "cdq");
4458 assert_operand_cnt(3);
4459 if (po->flags & OPF_REP) {
4464 fprintf(fout, " eax = %sesi; esi %c= %d;",
4465 lmod_cast_u_ptr(po, po->operand[0].lmod),
4466 (po->flags & OPF_DF) ? '-' : '+',
4467 lmod_bytes(po, po->operand[0].lmod));
4468 strcpy(g_comment, "lods");
4473 assert_operand_cnt(3);
4474 if (po->flags & OPF_REP) {
4475 fprintf(fout, " for (; ecx != 0; ecx--, edi %c= %d)\n",
4476 (po->flags & OPF_DF) ? '-' : '+',
4477 lmod_bytes(po, po->operand[0].lmod));
4478 fprintf(fout, " %sedi = eax;",
4479 lmod_cast_u_ptr(po, po->operand[0].lmod));
4480 strcpy(g_comment, "rep stos");
4483 fprintf(fout, " %sedi = eax; edi %c= %d;",
4484 lmod_cast_u_ptr(po, po->operand[0].lmod),
4485 (po->flags & OPF_DF) ? '-' : '+',
4486 lmod_bytes(po, po->operand[0].lmod));
4487 strcpy(g_comment, "stos");
4492 assert_operand_cnt(3);
4493 j = lmod_bytes(po, po->operand[0].lmod);
4494 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4495 l = (po->flags & OPF_DF) ? '-' : '+';
4496 if (po->flags & OPF_REP) {
4498 " for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n",
4501 " %sedi = %sesi;", buf1, buf1);
4502 strcpy(g_comment, "rep movs");
4505 fprintf(fout, " %sedi = %sesi; edi %c= %d; esi %c= %d;",
4506 buf1, buf1, l, j, l, j);
4507 strcpy(g_comment, "movs");
4512 // repe ~ repeat while ZF=1
4513 assert_operand_cnt(3);
4514 j = lmod_bytes(po, po->operand[0].lmod);
4515 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4516 l = (po->flags & OPF_DF) ? '-' : '+';
4517 if (po->flags & OPF_REP) {
4519 " for (; ecx != 0; ecx--) {\n");
4520 if (pfomask & (1 << PFO_C)) {
4523 " cond_c = %sesi < %sedi;\n", buf1, buf1);
4524 pfomask &= ~(1 << PFO_C);
4527 " cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n",
4528 buf1, buf1, l, j, l, j);
4530 " if (cond_z %s 0) break;\n",
4531 (po->flags & OPF_REPZ) ? "==" : "!=");
4534 snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
4535 (po->flags & OPF_REPZ) ? "e" : "ne");
4539 " cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;",
4540 buf1, buf1, l, j, l, j);
4541 strcpy(g_comment, "cmps");
4543 pfomask &= ~(1 << PFO_Z);
4544 last_arith_dst = NULL;
4545 delayed_flag_op = NULL;
4549 // only does ZF (for now)
4550 // repe ~ repeat while ZF=1
4551 assert_operand_cnt(3);
4552 j = lmod_bytes(po, po->operand[0].lmod);
4553 l = (po->flags & OPF_DF) ? '-' : '+';
4554 if (po->flags & OPF_REP) {
4556 " for (; ecx != 0; ecx--) {\n");
4558 " cond_z = (%seax == %sedi); edi %c= %d;\n",
4559 lmod_cast_u(po, po->operand[0].lmod),
4560 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4562 " if (cond_z %s 0) break;\n",
4563 (po->flags & OPF_REPZ) ? "==" : "!=");
4566 snprintf(g_comment, sizeof(g_comment), "rep%s scas",
4567 (po->flags & OPF_REPZ) ? "e" : "ne");
4570 fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;",
4571 lmod_cast_u(po, po->operand[0].lmod),
4572 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
4573 strcpy(g_comment, "scas");
4575 pfomask &= ~(1 << PFO_Z);
4576 last_arith_dst = NULL;
4577 delayed_flag_op = NULL;
4580 // arithmetic w/flags
4583 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4586 assert_operand_cnt(2);
4587 fprintf(fout, " %s %s= %s;",
4588 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4590 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4591 output_std_flags(fout, po, &pfomask, buf1);
4592 last_arith_dst = &po->operand[0];
4593 delayed_flag_op = NULL;
4598 assert_operand_cnt(2);
4599 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4600 if (pfomask & (1 << PFO_C)) {
4601 if (po->operand[1].type == OPT_CONST) {
4602 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4603 j = po->operand[1].val;
4606 if (po->op == OP_SHL)
4610 fprintf(fout, " cond_c = (%s >> %d) & 1;\n",
4614 ferr(po, "zero shift?\n");
4618 pfomask &= ~(1 << PFO_C);
4620 fprintf(fout, " %s %s= %s;", buf1, op_to_c(po),
4621 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4622 output_std_flags(fout, po, &pfomask, buf1);
4623 last_arith_dst = &po->operand[0];
4624 delayed_flag_op = NULL;
4628 assert_operand_cnt(2);
4629 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4630 fprintf(fout, " %s = %s%s >> %s;", buf1,
4631 lmod_cast_s(po, po->operand[0].lmod), buf1,
4632 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4633 output_std_flags(fout, po, &pfomask, buf1);
4634 last_arith_dst = &po->operand[0];
4635 delayed_flag_op = NULL;
4639 assert_operand_cnt(3);
4640 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4641 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4642 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4643 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4644 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[2]);
4645 fprintf(fout, " %s >>= %s; %s |= %s << (%d - %s);",
4646 buf1, buf3, buf1, buf2, l, buf3);
4647 strcpy(g_comment, "shrd");
4648 output_std_flags(fout, po, &pfomask, buf1);
4649 last_arith_dst = &po->operand[0];
4650 delayed_flag_op = NULL;
4655 assert_operand_cnt(2);
4656 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4657 if (po->operand[1].type == OPT_CONST) {
4658 j = po->operand[1].val;
4659 j %= lmod_bytes(po, po->operand[0].lmod) * 8;
4660 fprintf(fout, po->op == OP_ROL ?
4661 " %s = (%s << %d) | (%s >> %d);" :
4662 " %s = (%s >> %d) | (%s << %d);",
4663 buf1, buf1, j, buf1,
4664 lmod_bytes(po, po->operand[0].lmod) * 8 - j);
4668 output_std_flags(fout, po, &pfomask, buf1);
4669 last_arith_dst = &po->operand[0];
4670 delayed_flag_op = NULL;
4675 assert_operand_cnt(2);
4676 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4677 l = lmod_bytes(po, po->operand[0].lmod) * 8;
4678 if (po->operand[1].type == OPT_CONST) {
4679 j = po->operand[1].val % l;
4681 ferr(po, "zero rotate\n");
4682 fprintf(fout, " tmp = (%s >> %d) & 1;\n",
4683 buf1, (po->op == OP_RCL) ? (l - j) : (j - 1));
4684 if (po->op == OP_RCL) {
4686 " %s = (%s << %d) | (cond_c << %d)",
4687 buf1, buf1, j, j - 1);
4689 fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j);
4693 " %s = (%s >> %d) | (cond_c << %d)",
4694 buf1, buf1, j, l - j);
4696 fprintf(fout, " | (%s << %d)", buf1, l + 1 - j);
4698 fprintf(fout, ";\n");
4699 fprintf(fout, " cond_c = tmp;");
4703 strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr");
4704 output_std_flags(fout, po, &pfomask, buf1);
4705 last_arith_dst = &po->operand[0];
4706 delayed_flag_op = NULL;
4710 assert_operand_cnt(2);
4711 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4712 if (IS(opr_name(po, 0), opr_name(po, 1))) {
4713 // special case for XOR
4714 if (pfomask & (1 << PFO_BE)) { // weird, but it happens..
4715 fprintf(fout, " cond_be = 1;\n");
4716 pfomask &= ~(1 << PFO_BE);
4718 fprintf(fout, " %s = 0;",
4719 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4720 last_arith_dst = &po->operand[0];
4721 delayed_flag_op = NULL;
4727 assert_operand_cnt(2);
4728 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4729 if (pfomask & (1 << PFO_C)) {
4730 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4731 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4732 if (po->operand[0].lmod == OPLM_DWORD) {
4733 fprintf(fout, " tmp64 = (u64)%s + %s;\n", buf1, buf2);
4734 fprintf(fout, " cond_c = tmp64 >> 32;\n");
4735 fprintf(fout, " %s = (u32)tmp64;",
4736 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
4737 strcat(g_comment, "add64");
4740 fprintf(fout, " cond_c = ((u32)%s + %s) >> %d;\n",
4741 buf1, buf2, lmod_bytes(po, po->operand[0].lmod) * 8);
4742 fprintf(fout, " %s += %s;",
4743 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4746 pfomask &= ~(1 << PFO_C);
4747 output_std_flags(fout, po, &pfomask, buf1);
4748 last_arith_dst = &po->operand[0];
4749 delayed_flag_op = NULL;
4755 assert_operand_cnt(2);
4756 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4757 if (pfomask & ~((1 << PFO_Z) | (1 << PFO_S))) {
4758 for (j = 0; j <= PFO_LE; j++) {
4759 if (!(pfomask & (1 << j)))
4761 if (j == PFO_Z || j == PFO_S)
4764 out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0);
4765 fprintf(fout, " cond_%s = %s;\n",
4766 parsed_flag_op_names[j], buf1);
4767 pfomask &= ~(1 << j);
4774 assert_operand_cnt(2);
4775 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4776 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4777 if (po->op == OP_SBB
4778 && IS(po->operand[0].name, po->operand[1].name))
4780 // avoid use of unitialized var
4781 fprintf(fout, " %s = -cond_c;", buf1);
4782 // carry remains what it was
4783 pfomask &= ~(1 << PFO_C);
4786 fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
4787 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4789 output_std_flags(fout, po, &pfomask, buf1);
4790 last_arith_dst = &po->operand[0];
4791 delayed_flag_op = NULL;
4795 assert_operand_cnt(2);
4796 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4797 fprintf(fout, " %s = %s ? __builtin_ffs(%s) - 1 : 0;",
4798 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4800 output_std_flags(fout, po, &pfomask, buf1);
4801 last_arith_dst = &po->operand[0];
4802 delayed_flag_op = NULL;
4803 strcat(g_comment, "bsf");
4807 if (pfomask & ~(PFOB_S | PFOB_S | PFOB_C)) {
4808 for (j = 0; j <= PFO_LE; j++) {
4809 if (!(pfomask & (1 << j)))
4811 if (j == PFO_Z || j == PFO_S || j == PFO_C)
4814 out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0);
4815 fprintf(fout, " cond_%s = %s;\n",
4816 parsed_flag_op_names[j], buf1);
4817 pfomask &= ~(1 << j);
4823 if (pfomask & (1 << PFO_C))
4824 // carry is unaffected by inc/dec.. wtf?
4825 ferr(po, "carry propagation needed\n");
4827 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4828 if (po->operand[0].type == OPT_REG) {
4829 strcpy(buf2, po->op == OP_INC ? "++" : "--");
4830 fprintf(fout, " %s%s;", buf1, buf2);
4833 strcpy(buf2, po->op == OP_INC ? "+" : "-");
4834 fprintf(fout, " %s %s= 1;", buf1, buf2);
4836 output_std_flags(fout, po, &pfomask, buf1);
4837 last_arith_dst = &po->operand[0];
4838 delayed_flag_op = NULL;
4842 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4843 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
4844 fprintf(fout, " %s = -%s%s;", buf1,
4845 lmod_cast_s(po, po->operand[0].lmod), buf2);
4846 last_arith_dst = &po->operand[0];
4847 delayed_flag_op = NULL;
4848 if (pfomask & (1 << PFO_C)) {
4849 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
4850 pfomask &= ~(1 << PFO_C);
4855 if (po->operand_cnt == 2) {
4856 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4859 if (po->operand_cnt == 3)
4860 ferr(po, "TODO imul3\n");
4863 assert_operand_cnt(1);
4864 switch (po->operand[0].lmod) {
4866 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
4867 fprintf(fout, " tmp64 = %seax * %s%s;\n", buf1, buf1,
4868 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
4869 fprintf(fout, " edx = tmp64 >> 32;\n");
4870 fprintf(fout, " eax = tmp64;");
4873 strcpy(buf1, po->op == OP_IMUL ? "(s16)(s8)" : "(u16)(u8)");
4874 fprintf(fout, " LOWORD(eax) = %seax * %s;", buf1,
4875 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0],
4879 ferr(po, "TODO: unhandled mul type\n");
4882 last_arith_dst = NULL;
4883 delayed_flag_op = NULL;
4888 assert_operand_cnt(1);
4889 if (po->operand[0].lmod != OPLM_DWORD)
4890 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
4892 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
4893 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
4894 po->op == OP_IDIV));
4895 switch (po->operand[0].lmod) {
4897 if (po->flags & OPF_32BIT)
4898 snprintf(buf3, sizeof(buf3), "%seax", buf2);
4900 fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n");
4901 snprintf(buf3, sizeof(buf3), "%stmp64",
4902 (po->op == OP_IDIV) ? "(s64)" : "");
4904 if (po->operand[0].type == OPT_REG
4905 && po->operand[0].reg == xDX)
4907 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4908 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4911 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
4912 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
4916 ferr(po, "unhandled division type\n");
4918 last_arith_dst = NULL;
4919 delayed_flag_op = NULL;
4924 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4926 for (j = 0; j < 8; j++) {
4927 if (pfomask & (1 << j)) {
4928 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
4929 fprintf(fout, " cond_%s = %s;",
4930 parsed_flag_op_names[j], buf1);
4937 last_arith_dst = NULL;
4938 delayed_flag_op = po;
4942 // SETcc - should already be handled
4945 // note: we reuse OP_Jcc for SETcc, only flags differ
4947 fprintf(fout, "\n goto %s;", po->operand[0].name);
4951 fprintf(fout, " if (ecx == 0)\n");
4952 fprintf(fout, " goto %s;", po->operand[0].name);
4953 strcat(g_comment, "jecxz");
4957 assert_operand_cnt(1);
4958 last_arith_dst = NULL;
4959 delayed_flag_op = NULL;
4961 if (po->operand[0].type == OPT_REGMEM) {
4962 ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
4965 ferr(po, "parse failure for jmp '%s'\n",
4966 po->operand[0].name);
4967 fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
4970 else if (po->operand[0].type != OPT_LABEL)
4971 ferr(po, "unhandled jmp type\n");
4973 fprintf(fout, " goto %s;", po->operand[0].name);
4977 assert_operand_cnt(1);
4979 my_assert_not(pp, NULL);
4982 if (po->flags & OPF_CC) {
4983 // we treat conditional branch to another func
4984 // (yes such code exists..) as conditional tailcall
4986 fprintf(fout, " {\n");
4989 if (pp->is_fptr && !pp->is_arg) {
4990 fprintf(fout, "%s%s = %s;\n", buf3, pp->name,
4991 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
4993 if (pp->is_unresolved)
4994 fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
4995 buf3, asmfn, po->asmln, pp->name);
4998 fprintf(fout, "%s", buf3);
4999 if (strstr(pp->ret_type.name, "int64")) {
5000 if (po->flags & OPF_TAIL)
5001 ferr(po, "int64 and tail?\n");
5002 fprintf(fout, "tmp64 = ");
5004 else if (!IS(pp->ret_type.name, "void")) {
5005 if (po->flags & OPF_TAIL) {
5006 if (!IS(g_func_pp->ret_type.name, "void")) {
5007 fprintf(fout, "return ");
5008 if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
5009 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
5012 else if (regmask & (1 << xAX)) {
5013 fprintf(fout, "eax = ");
5014 if (pp->ret_type.is_ptr)
5015 fprintf(fout, "(u32)");
5019 if (pp->name[0] == 0)
5020 ferr(po, "missing pp->name\n");
5021 fprintf(fout, "%s%s(", pp->name,
5022 pp->has_structarg ? "_sa" : "");
5024 if (po->flags & OPF_ATAIL) {
5025 if (pp->argc_stack != g_func_pp->argc_stack
5026 || (pp->argc_stack > 0
5027 && pp->is_stdcall != g_func_pp->is_stdcall))
5028 ferr(po, "incompatible tailcall\n");
5029 if (g_func_pp->has_retreg)
5030 ferr(po, "TODO: retreg+tailcall\n");
5032 for (arg = j = 0; arg < pp->argc; arg++) {
5034 fprintf(fout, ", ");
5037 if (pp->arg[arg].type.is_ptr)
5038 snprintf(cast, sizeof(cast), "(%s)",
5039 pp->arg[arg].type.name);
5041 if (pp->arg[arg].reg != NULL) {
5042 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
5046 for (; j < g_func_pp->argc; j++)
5047 if (g_func_pp->arg[j].reg == NULL)
5049 fprintf(fout, "%sa%d", cast, j + 1);
5054 for (arg = 0; arg < pp->argc; arg++) {
5056 fprintf(fout, ", ");
5059 if (pp->arg[arg].type.is_ptr)
5060 snprintf(cast, sizeof(cast), "(%s)",
5061 pp->arg[arg].type.name);
5063 if (pp->arg[arg].reg != NULL) {
5064 if (pp->arg[arg].type.is_retreg)
5065 fprintf(fout, "&%s", pp->arg[arg].reg);
5067 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
5072 tmp_op = pp->arg[arg].datap;
5074 ferr(po, "parsed_op missing for arg%d\n", arg);
5076 if (tmp_op->flags & OPF_VAPUSH) {
5077 fprintf(fout, "ap");
5079 else if (tmp_op->p_argpass != 0) {
5080 fprintf(fout, "a%d", tmp_op->p_argpass);
5082 else if (tmp_op->p_argnum != 0) {
5083 fprintf(fout, "%s%s", cast,
5084 saved_arg_name(buf1, sizeof(buf1),
5085 tmp_op->p_arggrp, tmp_op->p_argnum));
5089 out_src_opr(buf1, sizeof(buf1),
5090 tmp_op, &tmp_op->operand[0], cast, 0));
5094 fprintf(fout, ");");
5096 if (strstr(pp->ret_type.name, "int64")) {
5097 fprintf(fout, "\n");
5098 fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3);
5099 fprintf(fout, "%seax = tmp64;", buf3);
5102 if (pp->is_unresolved) {
5103 snprintf(buf2, sizeof(buf2), " unresolved %dreg",
5105 strcat(g_comment, buf2);
5108 if (po->flags & OPF_TAIL) {
5110 if (i == opcnt - 1 || pp->is_noreturn)
5112 else if (IS(pp->ret_type.name, "void"))
5114 else if (IS(g_func_pp->ret_type.name, "void"))
5116 // else already handled as 'return f()'
5119 if (!IS(g_func_pp->ret_type.name, "void")) {
5120 ferr(po, "int func -> void func tailcall?\n");
5123 fprintf(fout, "\n%sreturn;", buf3);
5124 strcat(g_comment, " ^ tailcall");
5128 strcat(g_comment, " tailcall");
5130 if (pp->is_noreturn)
5131 strcat(g_comment, " noreturn");
5132 if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0)
5133 strcat(g_comment, " argframe");
5134 if (po->flags & OPF_CC)
5135 strcat(g_comment, " cond");
5137 if (po->flags & OPF_CC)
5138 fprintf(fout, "\n }");
5140 delayed_flag_op = NULL;
5141 last_arith_dst = NULL;
5145 if (g_func_pp->is_vararg)
5146 fprintf(fout, " va_end(ap);\n");
5147 if (g_func_pp->has_retreg) {
5148 for (arg = 0; arg < g_func_pp->argc; arg++)
5149 if (g_func_pp->arg[arg].type.is_retreg)
5150 fprintf(fout, " *r_%s = %s;\n",
5151 g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg);
5154 if (IS(g_func_pp->ret_type.name, "void")) {
5155 if (i != opcnt - 1 || label_pending)
5156 fprintf(fout, " return;");
5158 else if (g_func_pp->ret_type.is_ptr) {
5159 fprintf(fout, " return (%s)eax;",
5160 g_func_pp->ret_type.name);
5162 else if (IS(g_func_pp->ret_type.name, "__int64"))
5163 fprintf(fout, " return ((u64)edx << 32) | eax;");
5165 fprintf(fout, " return eax;");
5167 last_arith_dst = NULL;
5168 delayed_flag_op = NULL;
5172 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
5173 if (po->p_argnum != 0) {
5174 // special case - saved func arg
5175 fprintf(fout, " %s = %s;",
5176 saved_arg_name(buf2, sizeof(buf2),
5177 po->p_arggrp, po->p_argnum), buf1);
5180 else if (po->flags & OPF_RSAVE) {
5181 fprintf(fout, " s_%s = %s;", buf1, buf1);
5184 else if (g_func_pp->is_userstack) {
5185 fprintf(fout, " *(--esp) = %s;", buf1);
5188 if (!(g_ida_func_attr & IDAFA_NORETURN))
5189 ferr(po, "stray push encountered\n");
5194 if (po->flags & OPF_RSAVE) {
5195 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5196 fprintf(fout, " %s = s_%s;", buf1, buf1);
5199 else if (po->datap != NULL) {
5202 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5203 fprintf(fout, " %s = %s;", buf1,
5204 out_src_opr(buf2, sizeof(buf2),
5205 tmp_op, &tmp_op->operand[0],
5206 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
5209 else if (g_func_pp->is_userstack) {
5210 fprintf(fout, " %s = *esp++;",
5211 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
5215 ferr(po, "stray pop encountered\n");
5224 strcpy(g_comment, "(emms)");
5229 ferr(po, "unhandled op type %d, flags %x\n",
5234 if (g_comment[0] != 0) {
5235 char *p = g_comment;
5236 while (my_isblank(*p))
5238 fprintf(fout, " // %s", p);
5243 fprintf(fout, "\n");
5245 // some sanity checking
5246 if (po->flags & OPF_REP) {
5247 if (po->op != OP_STOS && po->op != OP_MOVS
5248 && po->op != OP_CMPS && po->op != OP_SCAS)
5249 ferr(po, "unexpected rep\n");
5250 if (!(po->flags & (OPF_REPZ|OPF_REPNZ))
5251 && (po->op == OP_CMPS || po->op == OP_SCAS))
5252 ferr(po, "cmps/scas with plain rep\n");
5254 if ((po->flags & (OPF_REPZ|OPF_REPNZ))
5255 && po->op != OP_CMPS && po->op != OP_SCAS)
5256 ferr(po, "unexpected repz/repnz\n");
5259 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
5261 // see is delayed flag stuff is still valid
5262 if (delayed_flag_op != NULL && delayed_flag_op != po) {
5263 if (is_any_opr_modified(delayed_flag_op, po, 0))
5264 delayed_flag_op = NULL;
5267 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
5268 if (is_opr_modified(last_arith_dst, po))
5269 last_arith_dst = NULL;
5275 if (g_stack_fsz && !g_stack_frame_used)
5276 fprintf(fout, " (void)sf;\n");
5278 fprintf(fout, "}\n\n");
5280 gen_x_cleanup(opcnt);
5283 static void gen_x_cleanup(int opcnt)
5287 for (i = 0; i < opcnt; i++) {
5288 struct label_ref *lr, *lr_del;
5290 lr = g_label_refs[i].next;
5291 while (lr != NULL) {
5296 g_label_refs[i].i = -1;
5297 g_label_refs[i].next = NULL;
5299 if (ops[i].op == OP_CALL) {
5301 proto_release(ops[i].pp);
5307 struct func_proto_dep;
5309 struct func_prototype {
5314 int has_ret:3; // -1, 0, 1: unresolved, no, yes
5315 unsigned int dep_resolved:1;
5316 unsigned int is_stdcall:1;
5317 struct func_proto_dep *dep_func;
5321 struct func_proto_dep {
5323 struct func_prototype *proto;
5324 int regmask_live; // .. at the time of call
5325 unsigned int ret_dep:1; // return from this is caller's return
5328 static struct func_prototype *hg_fp;
5329 static int hg_fp_cnt;
5331 static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
5334 static struct func_proto_dep *hg_fp_find_dep(struct func_prototype *fp,
5339 for (i = 0; i < fp->dep_func_cnt; i++)
5340 if (IS(fp->dep_func[i].name, name))
5341 return &fp->dep_func[i];
5346 static void hg_fp_add_dep(struct func_prototype *fp, const char *name)
5349 if (hg_fp_find_dep(fp, name))
5352 if ((fp->dep_func_cnt & 0xff) == 0) {
5353 fp->dep_func = realloc(fp->dep_func,
5354 sizeof(fp->dep_func[0]) * (fp->dep_func_cnt + 0x100));
5355 my_assert_not(fp->dep_func, NULL);
5356 memset(&fp->dep_func[fp->dep_func_cnt], 0,
5357 sizeof(fp->dep_func[0]) * 0x100);
5359 fp->dep_func[fp->dep_func_cnt].name = strdup(name);
5363 static int hg_fp_cmp_name(const void *p1_, const void *p2_)
5365 const struct func_prototype *p1 = p1_, *p2 = p2_;
5366 return strcmp(p1->name, p2->name);
5370 static int hg_fp_cmp_id(const void *p1_, const void *p2_)
5372 const struct func_prototype *p1 = p1_, *p2 = p2_;
5373 return p1->id - p2->id;
5377 static void gen_hdr(const char *funcn, int opcnt)
5379 const struct parsed_proto *pp_c;
5380 struct parsed_proto *pp;
5381 struct func_prototype *fp;
5382 struct func_proto_dep *dep;
5383 struct parsed_data *pd;
5384 struct parsed_op *po;
5385 const char *tmpname;
5386 int regmask_save = 0;
5387 int regmask_dst = 0;
5388 int regmask_dep = 0;
5389 int max_bp_offset = 0;
5391 int from_caller = 0;
5395 if ((hg_fp_cnt & 0xff) == 0) {
5396 hg_fp = realloc(hg_fp, sizeof(hg_fp[0]) * (hg_fp_cnt + 0x100));
5397 my_assert_not(hg_fp, NULL);
5398 memset(hg_fp + hg_fp_cnt, 0, sizeof(hg_fp[0]) * 0x100);
5401 fp = &hg_fp[hg_fp_cnt];
5402 snprintf(fp->name, sizeof(fp->name), "%s", funcn);
5404 fp->argc_stack = -1;
5407 // perhaps already in seed header?
5408 pp_c = proto_parse(g_fhdr, funcn, 1);
5410 fp->argc_stack = pp_c->argc_stack;
5411 fp->regmask_dep = get_pp_arg_regmask(pp_c);
5412 fp->has_ret = !IS(pp_c->ret_type.name, "void");
5416 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
5417 g_stack_frame_used = 0;
5420 // - handle ebp/esp frame, remove ops related to it
5421 scan_prologue_epilogue(opcnt);
5425 // - resolve all branches
5426 for (i = 0; i < opcnt; i++)
5432 if (po->flags & OPF_RMD)
5435 if (po->op == OP_CALL) {
5436 tmpname = opr_name(po, 0);
5438 if (po->operand[0].type == OPT_LABEL) {
5439 hg_fp_add_dep(fp, tmpname);
5441 // perhaps a call to already known func?
5442 pp_c = proto_parse(g_fhdr, tmpname, 1);
5444 pp = proto_clone(pp_c);
5446 else if (po->datap != NULL) {
5447 pp = calloc(1, sizeof(*pp));
5448 my_assert_not(pp, NULL);
5450 ret = parse_protostr(po->datap, pp);
5452 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
5456 if (pp != NULL && pp->is_noreturn)
5457 po->flags |= OPF_TAIL;
5463 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
5466 if (po->operand[0].type == OPT_REGMEM) {
5467 pd = try_resolve_jumptab(i, opcnt);
5475 for (l = 0; l < opcnt; l++) {
5476 if (g_labels[l] != NULL
5477 && IS(po->operand[0].name, g_labels[l]))
5479 add_label_ref(&g_label_refs[l], i);
5485 if (po->bt_i != -1 || (po->flags & OPF_RMD))
5488 if (po->operand[0].type == OPT_LABEL)
5492 ferr(po, "unhandled branch\n");
5496 po->flags |= OPF_TAIL;
5497 if (i > 0 && ops[i - 1].op == OP_POP)
5498 po->flags |= OPF_ATAIL;
5503 // - remove dead labels
5505 // - handle push <const>/pop pairs
5506 for (i = 0; i < opcnt; i++)
5508 if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
5514 if (po->flags & OPF_RMD)
5517 if (po->op == OP_CALL) {
5518 pp = process_call(i, opcnt);
5520 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
5521 int regmask_dummy = 0, save_arg_vars[MAX_ARG_GRP] = { 0, };
5522 // since we know the args, collect them
5523 collect_call_args(po, i, pp, ®mask_dummy, save_arg_vars,
5527 else if (po->op == OP_PUSH && po->operand[0].type == OPT_CONST) {
5528 scan_for_pop_const(i, opcnt);
5533 // - track saved regs
5534 // - try to figure out arg-regs
5535 for (i = 0; i < opcnt; i++)
5539 if (po->flags & OPF_FARG)
5540 /* (just calculate register deps) */;
5541 else if (po->flags & OPF_RMD)
5543 else if (po->op == OP_PUSH && po->operand[0].type == OPT_REG)
5545 reg = po->operand[0].reg;
5547 ferr(po, "reg not set for push?\n");
5550 ret = scan_for_pop(i + 1, opcnt,
5551 po->operand[0].name, i + opcnt * 1, 0, &depth, 0);
5553 regmask_save |= 1 << reg;
5554 po->flags |= OPF_RMD;
5555 scan_for_pop(i + 1, opcnt,
5556 po->operand[0].name, i + opcnt * 2, 0, &depth, 1);
5559 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
5561 regmask_save |= 1 << reg;
5562 po->flags |= OPF_RMD;
5563 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, OPF_RMD);
5567 else if (po->op == OP_CALL) {
5568 po->regmask_dst |= 1 << xAX;
5570 dep = hg_fp_find_dep(fp, po->operand[0].name);
5572 dep->regmask_live = regmask_save | regmask_dst;
5574 else if (po->op == OP_RET) {
5575 if (po->operand_cnt > 0) {
5577 if (fp->argc_stack >= 0
5578 && fp->argc_stack != po->operand[0].val / 4)
5579 ferr(po, "ret mismatch? (%d)\n", fp->argc_stack * 4);
5580 fp->argc_stack = po->operand[0].val / 4;
5584 if (has_ret != 0 && (po->flags & OPF_TAIL)) {
5585 if (po->op == OP_CALL) {
5590 struct parsed_opr opr = { 0, };
5595 ret = resolve_origin(i, &opr, i + opcnt * 3, &j, &from_caller);
5598 if (ret == -1 && from_caller) {
5599 // unresolved eax - probably void func
5603 if (ops[j].op == OP_CALL) {
5604 dep = hg_fp_find_dep(fp, po->operand[0].name);
5615 l = regmask_save | regmask_dst;
5616 if (g_bp_frame && !(po->flags & OPF_EBP_S))
5619 l = po->regmask_src & ~l;
5622 fnote(po, "dep |= %04x, dst %04x, save %04x\n", l,
5623 regmask_dst, regmask_save);
5626 regmask_dst |= po->regmask_dst;
5629 if (has_ret == -1 && (regmask_dep & (1 << xAX)))
5632 for (i = 0; i < g_eqcnt; i++) {
5633 if (g_eqs[i].offset > max_bp_offset && g_eqs[i].offset < 4*32)
5634 max_bp_offset = g_eqs[i].offset;
5637 if (fp->argc_stack < 0) {
5638 max_bp_offset = (max_bp_offset + 3) & ~3;
5639 fp->argc_stack = max_bp_offset / 4;
5640 if ((g_ida_func_attr & IDAFA_BP_FRAME) && fp->argc_stack > 0)
5644 fp->regmask_dep = regmask_dep & ~(1 << xSP);
5645 fp->has_ret = has_ret;
5647 gen_x_cleanup(opcnt);
5650 static void hg_fp_resolve_deps(struct func_prototype *fp)
5652 struct func_prototype fp_s;
5655 // this thing is recursive, so mark first..
5656 fp->dep_resolved = 1;
5658 for (i = 0; i < fp->dep_func_cnt; i++) {
5659 strcpy(fp_s.name, fp->dep_func[i].name);
5660 fp->dep_func[i].proto = bsearch(&fp_s, hg_fp, hg_fp_cnt,
5661 sizeof(hg_fp[0]), hg_fp_cmp_name);
5662 if (fp->dep_func[i].proto != NULL) {
5663 if (!fp->dep_func[i].proto->dep_resolved)
5664 hg_fp_resolve_deps(fp->dep_func[i].proto);
5666 fp->regmask_dep |= ~fp->dep_func[i].regmask_live
5667 & fp->dep_func[i].proto->regmask_dep;
5669 if (fp->has_ret == -1)
5670 fp->has_ret = fp->dep_func[i].proto->has_ret;
5675 static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
5678 char *p, buf[NAMELEN];
5684 for (; count > 0; count--, fp++) {
5685 if (fp->has_ret == -1)
5686 fprintf(fout, "// ret unresolved\n");
5688 fprintf(fout, "// dep:");
5689 for (j = 0; j < fp->dep_func_cnt; j++) {
5690 fprintf(fout, " %s/", fp->dep_func[j].name);
5691 if (fp->dep_func[j].proto != NULL)
5692 fprintf(fout, "%04x/%d", fp->dep_func[j].proto->regmask_dep,
5693 fp->dep_func[j].proto->has_ret);
5695 fprintf(fout, "\n");
5698 regmask_dep = fp->regmask_dep;
5699 argc_stack = fp->argc_stack;
5701 fprintf(fout, fp->has_ret ? "int " : "void ");
5702 if (regmask_dep && (fp->is_stdcall || argc_stack == 0)
5703 && (regmask_dep & ~((1 << xCX) | (1 << xDX))) == 0)
5705 fprintf(fout, " __fastcall ");
5706 if (!(regmask_dep & (1 << xDX)) && argc_stack == 0)
5712 else if (regmask_dep && !fp->is_stdcall) {
5713 fprintf(fout, "/*__usercall*/ ");
5715 else if (regmask_dep) {
5716 fprintf(fout, "/*__userpurge*/ ");
5718 else if (fp->is_stdcall)
5719 fprintf(fout, " __stdcall ");
5721 fprintf(fout, " __cdecl ");
5723 p = strchr(fp->name, '@');
5725 memcpy(buf, fp->name, p - fp->name);
5726 buf[p - fp->name] = 0;
5733 fprintf(fout, "%s(", cp);
5736 for (j = 0; j < xSP; j++) {
5737 if (regmask_dep & (1 << j)) {
5740 fprintf(fout, ", ");
5741 fprintf(fout, "int a%d/*<%s>*/", arg, regs_r32[j]);
5745 for (j = 0; j < argc_stack; j++) {
5748 fprintf(fout, ", ");
5749 fprintf(fout, "int a%d", arg);
5752 fprintf(fout, ");\n");
5756 static void output_hdr(FILE *fout)
5761 qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_name);
5762 for (i = 0; i < hg_fp_cnt; i++)
5763 hg_fp_resolve_deps(&hg_fp[i]);
5765 // note: messes up .proto ptr, don't use
5766 //qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_id);
5768 output_hdr_fp(fout, hg_fp, hg_fp_cnt);
5771 static void set_label(int i, const char *name)
5777 p = strchr(name, ':');
5781 if (g_labels[i] != NULL && !IS_START(g_labels[i], "algn_"))
5782 aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
5783 g_labels[i] = realloc(g_labels[i], len + 1);
5784 my_assert_not(g_labels[i], NULL);
5785 memcpy(g_labels[i], name, len);
5786 g_labels[i][len] = 0;
5789 // '=' needs special treatment..
5790 static char *next_word_s(char *w, size_t wsize, char *s)
5796 for (i = 0; i < wsize - 1; i++) {
5797 if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
5803 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
5804 printf("warning: '%s' truncated\n", w);
5815 static struct chunk_item *func_chunks;
5816 static int func_chunk_cnt;
5817 static int func_chunk_alloc;
5819 static void add_func_chunk(FILE *fasm, const char *name, int line)
5821 if (func_chunk_cnt >= func_chunk_alloc) {
5822 func_chunk_alloc *= 2;
5823 func_chunks = realloc(func_chunks,
5824 func_chunk_alloc * sizeof(func_chunks[0]));
5825 my_assert_not(func_chunks, NULL);
5827 func_chunks[func_chunk_cnt].fptr = ftell(fasm);
5828 func_chunks[func_chunk_cnt].name = strdup(name);
5829 func_chunks[func_chunk_cnt].asmln = line;
5833 static int cmp_chunks(const void *p1, const void *p2)
5835 const struct chunk_item *c1 = p1, *c2 = p2;
5836 return strcmp(c1->name, c2->name);
5839 static int cmpstringp(const void *p1, const void *p2)
5841 return strcmp(*(char * const *)p1, *(char * const *)p2);
5844 static void scan_ahead(FILE *fasm)
5854 oldpos = ftell(fasm);
5857 while (fgets(line, sizeof(line), fasm))
5868 // get rid of random tabs
5869 for (i = 0; line[i] != 0; i++)
5870 if (line[i] == '\t')
5873 if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
5876 next_word(words[0], sizeof(words[0]), p);
5877 if (words[0][0] == 0)
5878 aerr("missing name for func chunk?\n");
5880 add_func_chunk(fasm, words[0], asmln);
5882 else if (IS_START(p, "; sctend"))
5888 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
5889 words[wordc][0] = 0;
5890 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
5891 if (*p == 0 || *p == ';') {
5897 if (wordc == 2 && IS(words[1], "ends"))
5901 fseek(fasm, oldpos, SEEK_SET);
5905 int main(int argc, char *argv[])
5907 FILE *fout, *fasm, *frlist;
5908 struct parsed_data *pd = NULL;
5910 char **rlist = NULL;
5912 int rlist_alloc = 0;
5913 int func_chunks_used = 0;
5914 int func_chunks_sorted = 0;
5915 int func_chunk_i = -1;
5916 long func_chunk_ret = 0;
5917 int func_chunk_ret_ln = 0;
5918 int scanned_ahead = 0;
5920 char words[20][256];
5921 enum opr_lenmod lmod;
5922 char *sctproto = NULL;
5924 int pending_endp = 0;
5926 int skip_warned = 0;
5939 for (arg = 1; arg < argc; arg++) {
5940 if (IS(argv[arg], "-v"))
5942 else if (IS(argv[arg], "-rf"))
5943 g_allow_regfunc = 1;
5944 else if (IS(argv[arg], "-m"))
5946 else if (IS(argv[arg], "-hdr"))
5947 g_header_mode = g_quiet_pp = g_allow_regfunc = 1;
5952 if (argc < arg + 3) {
5953 printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdrf> [rlist]*\n"
5954 "%s -hdr <out.h> <.asm> <seed.h> [rlist]*\n",
5961 asmfn = argv[arg++];
5962 fasm = fopen(asmfn, "r");
5963 my_assert_not(fasm, NULL);
5965 hdrfn = argv[arg++];
5966 g_fhdr = fopen(hdrfn, "r");
5967 my_assert_not(g_fhdr, NULL);
5970 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
5971 my_assert_not(rlist, NULL);
5972 // needs special handling..
5973 rlist[rlist_len++] = "__alloca_probe";
5975 func_chunk_alloc = 32;
5976 func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
5977 my_assert_not(func_chunks, NULL);
5979 memset(words, 0, sizeof(words));
5981 for (; arg < argc; arg++) {
5982 frlist = fopen(argv[arg], "r");
5983 my_assert_not(frlist, NULL);
5985 while (fgets(line, sizeof(line), frlist)) {
5987 if (*p == 0 || *p == ';')
5990 if (IS_START(p, "#if 0")
5991 || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
5995 else if (IS_START(p, "#endif"))
6002 p = next_word(words[0], sizeof(words[0]), p);
6003 if (words[0][0] == 0)
6006 if (rlist_len >= rlist_alloc) {
6007 rlist_alloc = rlist_alloc * 2 + 64;
6008 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
6009 my_assert_not(rlist, NULL);
6011 rlist[rlist_len++] = strdup(words[0]);
6020 qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
6022 fout = fopen(argv[arg_out], "w");
6023 my_assert_not(fout, NULL);
6026 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
6027 my_assert_not(g_eqs, NULL);
6029 for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
6030 g_label_refs[i].i = -1;
6031 g_label_refs[i].next = NULL;
6034 while (fgets(line, sizeof(line), fasm))
6043 // get rid of random tabs
6044 for (i = 0; line[i] != 0; i++)
6045 if (line[i] == '\t')
6050 if (p[2] == '=' && IS_START(p, "; =============== S U B"))
6051 goto do_pending_endp; // eww..
6053 if (p[2] == 'A' && IS_START(p, "; Attributes:"))
6055 static const char *attrs[] = {
6064 // parse IDA's attribute-list comment
6065 g_ida_func_attr = 0;
6068 for (; *p != 0; p = sskip(p)) {
6069 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
6070 if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
6071 g_ida_func_attr |= 1 << i;
6072 p += strlen(attrs[i]);
6076 if (i == ARRAY_SIZE(attrs)) {
6077 anote("unparsed IDA attr: %s\n", p);
6080 if (IS(attrs[i], "fpd=")) {
6081 p = next_word(words[0], sizeof(words[0]), p);
6086 else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
6089 next_word(words[0], sizeof(words[0]), p);
6090 if (words[0][0] == 0)
6091 aerr("missing name for func chunk?\n");
6093 if (!scanned_ahead) {
6094 add_func_chunk(fasm, words[0], asmln);
6095 func_chunks_sorted = 0;
6098 else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
6100 if (func_chunk_i >= 0) {
6101 if (func_chunk_i < func_chunk_cnt
6102 && IS(func_chunks[func_chunk_i].name, g_func))
6104 // move on to next chunk
6105 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
6107 aerr("seek failed for '%s' chunk #%d\n",
6108 g_func, func_chunk_i);
6109 asmln = func_chunks[func_chunk_i].asmln;
6113 if (func_chunk_ret == 0)
6114 aerr("no return from chunk?\n");
6115 fseek(fasm, func_chunk_ret, SEEK_SET);
6116 asmln = func_chunk_ret_ln;
6122 else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
6123 func_chunks_used = 1;
6125 if (IS_START(g_func, "sub_")) {
6126 unsigned long addr = strtoul(p, NULL, 16);
6127 unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
6128 if (addr > f_addr && !scanned_ahead) {
6129 anote("scan_ahead caused by '%s', addr %lx\n",
6133 func_chunks_sorted = 0;
6141 for (i = wordc; i < ARRAY_SIZE(words); i++)
6143 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
6144 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
6145 if (*p == 0 || *p == ';') {
6150 if (*p != 0 && *p != ';')
6151 aerr("too many words\n");
6153 // alow asm patches in comments
6155 if (IS_START(p, "; sctpatch:")) {
6157 if (*p == 0 || *p == ';')
6159 goto parse_words; // lame
6161 if (IS_START(p, "; sctproto:")) {
6162 sctproto = strdup(p + 11);
6164 else if (IS_START(p, "; sctend")) {
6173 awarn("wordc == 0?\n");
6177 // don't care about this:
6178 if (words[0][0] == '.'
6179 || IS(words[0], "include")
6180 || IS(words[0], "assume") || IS(words[1], "segment")
6181 || IS(words[0], "align"))
6187 // do delayed endp processing to collect switch jumptables
6189 if (in_func && !skip_func && !end && wordc >= 2
6190 && ((words[0][0] == 'd' && words[0][2] == 0)
6191 || (words[1][0] == 'd' && words[1][2] == 0)))
6194 if (words[1][0] == 'd' && words[1][2] == 0) {
6196 if (g_func_pd_cnt >= pd_alloc) {
6197 pd_alloc = pd_alloc * 2 + 16;
6198 g_func_pd = realloc(g_func_pd,
6199 sizeof(g_func_pd[0]) * pd_alloc);
6200 my_assert_not(g_func_pd, NULL);
6202 pd = &g_func_pd[g_func_pd_cnt];
6204 memset(pd, 0, sizeof(*pd));
6205 strcpy(pd->label, words[0]);
6206 pd->type = OPT_CONST;
6207 pd->lmod = lmod_from_directive(words[1]);
6213 anote("skipping alignment byte?\n");
6216 lmod = lmod_from_directive(words[0]);
6217 if (lmod != pd->lmod)
6218 aerr("lmod change? %d->%d\n", pd->lmod, lmod);
6221 if (pd->count_alloc < pd->count + wordc) {
6222 pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
6223 pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
6224 my_assert_not(pd->d, NULL);
6226 for (; i < wordc; i++) {
6227 if (IS(words[i], "offset")) {
6228 pd->type = OPT_OFFSET;
6231 p = strchr(words[i], ',');
6234 if (pd->type == OPT_OFFSET)
6235 pd->d[pd->count].u.label = strdup(words[i]);
6237 pd->d[pd->count].u.val = parse_number(words[i]);
6238 pd->d[pd->count].bt_i = -1;
6244 if (in_func && !skip_func) {
6246 gen_hdr(g_func, pi);
6248 gen_func(fout, g_fhdr, g_func, pi);
6253 g_ida_func_attr = 0;
6257 func_chunks_used = 0;
6260 memset(&ops, 0, pi * sizeof(ops[0]));
6265 for (i = 0; i < g_func_pd_cnt; i++) {
6267 if (pd->type == OPT_OFFSET) {
6268 for (j = 0; j < pd->count; j++)
6269 free(pd->d[j].u.label);
6283 if (IS(words[1], "proc")) {
6285 aerr("proc '%s' while in_func '%s'?\n",
6288 if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
6290 strcpy(g_func, words[0]);
6291 set_label(0, words[0]);
6296 if (IS(words[1], "endp"))
6299 aerr("endp '%s' while not in_func?\n", words[0]);
6300 if (!IS(g_func, words[0]))
6301 aerr("endp '%s' while in_func '%s'?\n",
6304 if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1
6305 && ops[0].op == OP_JMP && ops[0].operand[0].had_ds)
6311 if (!skip_func && func_chunks_used) {
6312 // start processing chunks
6313 struct chunk_item *ci, key = { g_func, 0 };
6315 func_chunk_ret = ftell(fasm);
6316 func_chunk_ret_ln = asmln;
6317 if (!func_chunks_sorted) {
6318 qsort(func_chunks, func_chunk_cnt,
6319 sizeof(func_chunks[0]), cmp_chunks);
6320 func_chunks_sorted = 1;
6322 ci = bsearch(&key, func_chunks, func_chunk_cnt,
6323 sizeof(func_chunks[0]), cmp_chunks);
6325 aerr("'%s' needs chunks, but none found\n", g_func);
6326 func_chunk_i = ci - func_chunks;
6327 for (; func_chunk_i > 0; func_chunk_i--)
6328 if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
6331 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
6333 aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
6334 asmln = func_chunks[func_chunk_i].asmln;
6342 if (wordc == 2 && IS(words[1], "ends")) {
6346 goto do_pending_endp;
6350 // scan for next text segment
6351 while (fgets(line, sizeof(line), fasm)) {
6354 if (*p == 0 || *p == ';')
6357 if (strstr(p, "segment para public 'CODE' use32"))
6364 p = strchr(words[0], ':');
6366 set_label(pi, words[0]);
6370 if (!in_func || skip_func) {
6371 if (!skip_warned && !skip_func && g_labels[pi] != NULL) {
6373 anote("skipping from '%s'\n", g_labels[pi]);
6377 g_labels[pi] = NULL;
6381 if (wordc > 1 && IS(words[1], "="))
6384 aerr("unhandled equ, wc=%d\n", wordc);
6385 if (g_eqcnt >= eq_alloc) {
6387 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
6388 my_assert_not(g_eqs, NULL);
6391 len = strlen(words[0]);
6392 if (len > sizeof(g_eqs[0].name) - 1)
6393 aerr("equ name too long: %d\n", len);
6394 strcpy(g_eqs[g_eqcnt].name, words[0]);
6396 if (!IS(words[3], "ptr"))
6397 aerr("unhandled equ\n");
6398 if (IS(words[2], "dword"))
6399 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
6400 else if (IS(words[2], "word"))
6401 g_eqs[g_eqcnt].lmod = OPLM_WORD;
6402 else if (IS(words[2], "byte"))
6403 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
6404 else if (IS(words[2], "qword"))
6405 g_eqs[g_eqcnt].lmod = OPLM_QWORD;
6407 aerr("bad lmod: '%s'\n", words[2]);
6409 g_eqs[g_eqcnt].offset = parse_number(words[4]);
6414 if (pi >= ARRAY_SIZE(ops))
6415 aerr("too many ops\n");
6417 parse_op(&ops[pi], words, wordc);
6419 if (sctproto != NULL) {
6420 if (ops[pi].op == OP_CALL || ops[pi].op == OP_JMP)
6421 ops[pi].datap = sctproto;
6437 // vim:ts=2:shiftwidth=2:expandtab