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"
18 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
19 #define IS(w, y) !strcmp(w, y)
20 #define IS_START(w, y) !strncmp(w, y, strlen(y))
22 #include "protoparse.h"
24 static const char *asmfn;
28 #define anote(fmt, ...) \
29 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
30 #define awarn(fmt, ...) \
31 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
32 #define aerr(fmt, ...) do { \
33 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
38 #include "masm_tools.h"
41 OPF_RMD = (1 << 0), /* removed from code generation */
42 OPF_DATA = (1 << 1), /* data processing - writes to dst opr */
43 OPF_FLAGS = (1 << 2), /* sets flags */
44 OPF_JMP = (1 << 3), /* branch, call */
45 OPF_CJMP = (1 << 4), /* cond. branch (cc or jecxz/loop) */
46 OPF_CC = (1 << 5), /* uses flags */
47 OPF_TAIL = (1 << 6), /* ret or tail call */
48 OPF_RSAVE = (1 << 7), /* push/pop is local reg save/load */
49 OPF_REP = (1 << 8), /* prefixed by rep */
50 OPF_REPZ = (1 << 9), /* rep is repe/repz */
51 OPF_REPNZ = (1 << 10), /* rep is repne/repnz */
52 OPF_FARG = (1 << 11), /* push collected as func arg */
53 OPF_FARGNR = (1 << 12), /* push collected as func arg (no reuse) */
54 OPF_EBP_S = (1 << 13), /* ebp used as scratch here, not BP */
55 OPF_DF = (1 << 14), /* DF flag set */
56 OPF_ATAIL = (1 << 15), /* tail call with reused arg frame */
57 OPF_32BIT = (1 << 16), /* 32bit division */
58 OPF_LOCK = (1 << 17), /* op has lock prefix */
59 OPF_VAPUSH = (1 << 18), /* vararg ptr push (as call arg) */
60 OPF_DONE = (1 << 19), /* already fully handled by analysis */
61 OPF_PPUSH = (1 << 20), /* part of complex push-pop graph */
134 // must be sorted (larger len must be further in enum)
143 #define MAX_OPERANDS 3
148 enum opr_lenmod lmod;
149 unsigned int is_ptr:1; // pointer in C
150 unsigned int is_array:1; // array in C
151 unsigned int type_from_var:1; // .. in header, sometimes wrong
152 unsigned int size_mismatch:1; // type override differs from C
153 unsigned int size_lt:1; // type override is larger than C
154 unsigned int had_ds:1; // had ds: prefix
155 const struct parsed_proto *pp; // for OPT_LABEL
163 struct parsed_opr operand[MAX_OPERANDS];
166 unsigned char pfo_inv;
167 unsigned char operand_cnt;
168 unsigned char p_argnum; // arg push: altered before call arg #
169 unsigned char p_arggrp; // arg push: arg group # for above
170 unsigned char p_argpass;// arg push: arg of host func
171 short p_argnext;// arg push: same arg pushed elsewhere or -1
172 int regmask_src; // all referensed regs
174 int pfomask; // flagop: parsed_flag_op that can't be delayed
175 int cc_scratch; // scratch storage during analysis
176 int bt_i; // branch target for branches
177 struct parsed_data *btj;// branch targets for jumptables
178 struct parsed_proto *pp;// parsed_proto for OP_CALL
184 // OP_CALL - parser proto hint (str)
185 // (OPF_CC) - points to one of (OPF_FLAGS) that affects cc op
186 // OP_PUSH - points to OP_POP in complex push/pop graph
187 // OP_POP - points to OP_PUSH in simple push/pop pair
191 enum opr_lenmod lmod;
198 enum opr_lenmod lmod;
212 struct label_ref *next;
216 IDAFA_BP_FRAME = (1 << 0),
217 IDAFA_LIB_FUNC = (1 << 1),
218 IDAFA_STATIC = (1 << 2),
219 IDAFA_NORETURN = (1 << 3),
220 IDAFA_THUNK = (1 << 4),
221 IDAFA_FPD = (1 << 5),
224 // note: limited to 32k due to p_argnext
226 #define MAX_ARG_GRP 2
228 static struct parsed_op ops[MAX_OPS];
229 static struct parsed_equ *g_eqs;
231 static char *g_labels[MAX_OPS];
232 static struct label_ref g_label_refs[MAX_OPS];
233 static const struct parsed_proto *g_func_pp;
234 static struct parsed_data *g_func_pd;
235 static int g_func_pd_cnt;
236 static char g_func[256];
237 static char g_comment[256];
238 static int g_bp_frame;
239 static int g_sp_frame;
240 static int g_stack_frame_used;
241 static int g_stack_fsz;
242 static int g_ida_func_attr;
243 static int g_skip_func;
244 static int g_allow_regfunc;
245 static int g_quiet_pp;
246 static int g_header_mode;
248 #define ferr(op_, fmt, ...) do { \
249 printf("%s:%d: error %u: [%s] '%s': " fmt, asmfn, (op_)->asmln, \
250 __LINE__, g_func, dump_op(op_), ##__VA_ARGS__); \
254 #define fnote(op_, fmt, ...) \
255 printf("%s:%d: note: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \
256 dump_op(op_), ##__VA_ARGS__)
258 #define ferr_assert(op_, cond) do { \
259 if (!(cond)) ferr(op_, "assertion '%s' failed\n", #cond); \
262 const char *regs_r32[] = {
263 "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
264 // not r32, but list here for easy parsing and printing
265 "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
267 const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
268 const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
269 const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
271 enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
273 // possible basic comparison types (without inversion)
274 enum parsed_flag_op {
278 PFO_BE, // 6 CF=1||ZF=1
282 PFO_LE, // e ZF=1||SF!=OF
285 #define PFOB_O (1 << PFO_O)
286 #define PFOB_C (1 << PFO_C)
287 #define PFOB_Z (1 << PFO_Z)
288 #define PFOB_S (1 << PFO_S)
290 static const char *parsed_flag_op_names[] = {
291 "o", "c", "z", "be", "s", "p", "l", "le"
294 static int char_array_i(const char *array[], size_t len, const char *s)
298 for (i = 0; i < len; i++)
305 static void printf_number(char *buf, size_t buf_size,
306 unsigned long number)
308 // output in C-friendly form
309 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
312 static int check_segment_prefix(const char *s)
314 if (s[0] == 0 || s[1] != 's' || s[2] != ':')
328 static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
332 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
334 *reg_lmod = OPLM_QWORD;
338 *reg_lmod = OPLM_DWORD;
341 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
343 *reg_lmod = OPLM_WORD;
346 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
348 *reg_lmod = OPLM_BYTE;
351 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
353 *reg_lmod = OPLM_BYTE;
360 static int parse_indmode(char *name, int *regmask, int need_c_cvt)
362 enum opr_lenmod lmod;
375 while (my_isblank(*s))
377 for (; my_issep(*s); d++, s++)
379 while (my_isblank(*s))
383 // skip '?s:' prefixes
384 if (check_segment_prefix(s))
387 s = next_idt(w, sizeof(w), s);
392 reg = parse_reg(&lmod, w);
394 *regmask |= 1 << reg;
398 if ('0' <= w[0] && w[0] <= '9') {
399 number = parse_number(w);
400 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
404 // probably some label/identifier - pass
407 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
411 strcpy(name, cvtbuf);
416 static int is_reg_in_str(const char *s)
420 if (strlen(s) < 3 || (s[3] && !my_issep(s[3]) && !my_isblank(s[3])))
423 for (i = 0; i < ARRAY_SIZE(regs_r32); i++)
424 if (!strncmp(s, regs_r32[i], 3))
430 static const char *parse_stack_el(const char *name, char *extra_reg,
433 const char *p, *p2, *s;
439 if (g_bp_frame || early_try)
442 if (IS_START(p + 3, "+ebp+") && is_reg_in_str(p)) {
444 if (extra_reg != NULL) {
445 strncpy(extra_reg, name, 3);
450 if (IS_START(p, "ebp+")) {
454 if (p2 != NULL && is_reg_in_str(p)) {
455 if (extra_reg != NULL) {
456 strncpy(extra_reg, p, p2 - p);
457 extra_reg[p2 - p] = 0;
462 if (!('0' <= *p && *p <= '9'))
469 if (!IS_START(name, "esp+"))
475 if (is_reg_in_str(s)) {
476 if (extra_reg != NULL) {
477 strncpy(extra_reg, s, p - s);
478 extra_reg[p - s] = 0;
483 aerr("%s IDA stackvar not set?\n", __func__);
485 if (!('0' <= *s && *s <= '9')) {
486 aerr("%s IDA stackvar offset not set?\n", __func__);
489 if (s[0] == '0' && s[1] == 'x')
492 if (len < sizeof(buf) - 1) {
493 strncpy(buf, s, len);
495 val = strtol(buf, &endp, 16);
496 if (val == 0 || *endp != 0) {
497 aerr("%s num parse fail for '%s'\n", __func__, buf);
506 if ('0' <= *p && *p <= '9')
512 static int guess_lmod_from_name(struct parsed_opr *opr)
514 if (IS_START(opr->name, "dword_") || IS_START(opr->name, "off_")) {
515 opr->lmod = OPLM_DWORD;
518 if (IS_START(opr->name, "word_")) {
519 opr->lmod = OPLM_WORD;
522 if (IS_START(opr->name, "byte_")) {
523 opr->lmod = OPLM_BYTE;
526 if (IS_START(opr->name, "qword_")) {
527 opr->lmod = OPLM_QWORD;
533 static int guess_lmod_from_c_type(enum opr_lenmod *lmod,
534 const struct parsed_type *c_type)
536 static const char *dword_types[] = {
537 "uint32_t", "int", "_DWORD", "UINT_PTR", "DWORD",
538 "WPARAM", "LPARAM", "UINT", "__int32",
539 "LONG", "HIMC", "BOOL", "size_t",
542 static const char *word_types[] = {
543 "uint16_t", "int16_t", "_WORD", "WORD",
544 "unsigned __int16", "__int16",
546 static const char *byte_types[] = {
547 "uint8_t", "int8_t", "char",
548 "unsigned __int8", "__int8", "BYTE", "_BYTE",
550 // structures.. deal the same as with _UNKNOWN for now
556 if (c_type->is_ptr) {
561 n = skip_type_mod(c_type->name);
563 for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
564 if (IS(n, dword_types[i])) {
570 for (i = 0; i < ARRAY_SIZE(word_types); i++) {
571 if (IS(n, word_types[i])) {
577 for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
578 if (IS(n, byte_types[i])) {
587 static char *default_cast_to(char *buf, size_t buf_size,
588 struct parsed_opr *opr)
594 if (opr->pp == NULL || opr->pp->type.name == NULL
597 snprintf(buf, buf_size, "%s", "(void *)");
601 snprintf(buf, buf_size, "(%s)", opr->pp->type.name);
605 static enum opr_type lmod_from_directive(const char *d)
609 else if (IS(d, "dw"))
611 else if (IS(d, "db"))
614 aerr("unhandled directive: '%s'\n", d);
618 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
624 *regmask |= 1 << reg;
627 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
630 static int parse_operand(struct parsed_opr *opr,
631 int *regmask, int *regmask_indirect,
632 char words[16][256], int wordc, int w, unsigned int op_flags)
634 const struct parsed_proto *pp = NULL;
635 enum opr_lenmod tmplmod;
636 unsigned long number;
644 aerr("parse_operand w %d, wordc %d\n", w, wordc);
648 for (i = w; i < wordc; i++) {
649 len = strlen(words[i]);
650 if (words[i][len - 1] == ',') {
651 words[i][len - 1] = 0;
657 wordc_in = wordc - w;
659 if ((op_flags & OPF_JMP) && wordc_in > 0
660 && !('0' <= words[w][0] && words[w][0] <= '9'))
662 const char *label = NULL;
664 if (wordc_in == 3 && !strncmp(words[w], "near", 4)
665 && IS(words[w + 1], "ptr"))
666 label = words[w + 2];
667 else if (wordc_in == 2 && IS(words[w], "short"))
668 label = words[w + 1];
669 else if (wordc_in == 1
670 && strchr(words[w], '[') == NULL
671 && parse_reg(&tmplmod, words[w]) < 0)
675 opr->type = OPT_LABEL;
676 ret = check_segment_prefix(label);
679 aerr("fs/gs used\n");
683 strcpy(opr->name, label);
689 if (IS(words[w + 1], "ptr")) {
690 if (IS(words[w], "dword"))
691 opr->lmod = OPLM_DWORD;
692 else if (IS(words[w], "word"))
693 opr->lmod = OPLM_WORD;
694 else if (IS(words[w], "byte"))
695 opr->lmod = OPLM_BYTE;
696 else if (IS(words[w], "qword"))
697 opr->lmod = OPLM_QWORD;
699 aerr("type parsing failed\n");
701 wordc_in = wordc - w;
706 if (IS(words[w], "offset")) {
707 opr->type = OPT_OFFSET;
708 opr->lmod = OPLM_DWORD;
709 strcpy(opr->name, words[w + 1]);
710 pp = proto_parse(g_fhdr, opr->name, 1);
713 if (IS(words[w], "(offset")) {
714 p = strchr(words[w + 1], ')');
716 aerr("parse of bracketed offset failed\n");
718 opr->type = OPT_OFFSET;
719 strcpy(opr->name, words[w + 1]);
725 aerr("parse_operand 1 word expected\n");
727 ret = check_segment_prefix(words[w]);
730 aerr("fs/gs used\n");
732 memmove(words[w], words[w] + 3, strlen(words[w]) - 2);
734 strcpy(opr->name, words[w]);
736 if (words[w][0] == '[') {
737 opr->type = OPT_REGMEM;
738 ret = sscanf(words[w], "[%[^]]]", opr->name);
740 aerr("[] parse failure\n");
742 parse_indmode(opr->name, regmask_indirect, 1);
743 if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name, NULL, 1))
746 struct parsed_equ *eq =
747 equ_find(NULL, parse_stack_el(opr->name, NULL, 1), &i);
749 opr->lmod = eq->lmod;
753 else if (strchr(words[w], '[')) {
755 p = strchr(words[w], '[');
756 opr->type = OPT_REGMEM;
757 parse_indmode(p, regmask_indirect, 0);
758 strncpy(buf, words[w], p - words[w]);
759 buf[p - words[w]] = 0;
760 pp = proto_parse(g_fhdr, buf, 1);
763 else if (('0' <= words[w][0] && words[w][0] <= '9')
764 || words[w][0] == '-')
766 number = parse_number(words[w]);
767 opr->type = OPT_CONST;
769 printf_number(opr->name, sizeof(opr->name), number);
773 ret = parse_reg(&tmplmod, opr->name);
775 setup_reg_opr(opr, ret, tmplmod, regmask);
779 // most likely var in data segment
780 opr->type = OPT_LABEL;
781 pp = proto_parse(g_fhdr, opr->name, g_quiet_pp);
785 if (pp->is_fptr || pp->is_func) {
786 opr->lmod = OPLM_DWORD;
790 tmplmod = OPLM_UNSPEC;
791 if (!guess_lmod_from_c_type(&tmplmod, &pp->type))
792 anote("unhandled C type '%s' for '%s'\n",
793 pp->type.name, opr->name);
795 if (opr->lmod == OPLM_UNSPEC) {
797 opr->type_from_var = 1;
799 else if (opr->lmod != tmplmod) {
800 opr->size_mismatch = 1;
801 if (tmplmod < opr->lmod)
804 opr->is_ptr = pp->type.is_ptr;
806 opr->is_array = pp->type.is_array;
810 if (opr->lmod == OPLM_UNSPEC)
811 guess_lmod_from_name(opr);
815 static const struct {
820 { "repe", OPF_REP|OPF_REPZ },
821 { "repz", OPF_REP|OPF_REPZ },
822 { "repne", OPF_REP|OPF_REPNZ },
823 { "repnz", OPF_REP|OPF_REPNZ },
824 { "lock", OPF_LOCK }, // ignored for now..
827 #define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC)
829 static const struct {
832 unsigned short minopr;
833 unsigned short maxopr;
836 unsigned char pfo_inv;
838 { "nop", OP_NOP, 0, 0, 0 },
839 { "push", OP_PUSH, 1, 1, 0 },
840 { "pop", OP_POP, 1, 1, OPF_DATA },
841 { "leave",OP_LEAVE, 0, 0, OPF_DATA },
842 { "mov" , OP_MOV, 2, 2, OPF_DATA },
843 { "lea", OP_LEA, 2, 2, OPF_DATA },
844 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
845 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
846 { "xchg", OP_XCHG, 2, 2, OPF_DATA },
847 { "not", OP_NOT, 1, 1, OPF_DATA },
848 { "xlat", OP_XLAT, 0, 0, OPF_DATA },
849 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
850 { "lodsb",OP_LODS, 0, 0, OPF_DATA },
851 { "lodsw",OP_LODS, 0, 0, OPF_DATA },
852 { "lodsd",OP_LODS, 0, 0, OPF_DATA },
853 { "stosb",OP_STOS, 0, 0, OPF_DATA },
854 { "stosw",OP_STOS, 0, 0, OPF_DATA },
855 { "stosd",OP_STOS, 0, 0, OPF_DATA },
856 { "movsb",OP_MOVS, 0, 0, OPF_DATA },
857 { "movsw",OP_MOVS, 0, 0, OPF_DATA },
858 { "movsd",OP_MOVS, 0, 0, OPF_DATA },
859 { "cmpsb",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
860 { "cmpsw",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
861 { "cmpsd",OP_CMPS, 0, 0, OPF_DATA|OPF_FLAGS },
862 { "scasb",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
863 { "scasw",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
864 { "scasd",OP_SCAS, 0, 0, OPF_DATA|OPF_FLAGS },
865 { "std", OP_STD, 0, 0, OPF_DATA }, // special flag
866 { "cld", OP_CLD, 0, 0, OPF_DATA },
867 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
868 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
869 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
870 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
871 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
872 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
873 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
874 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
875 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
876 { "shld", OP_SHLD, 3, 3, OPF_DATA|OPF_FLAGS },
877 { "shrd", OP_SHRD, 3, 3, OPF_DATA|OPF_FLAGS },
878 { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS },
879 { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS },
880 { "rcl", OP_RCL, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
881 { "rcr", OP_RCR, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
882 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
883 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
884 { "bsf", OP_BSF, 2, 2, OPF_DATA|OPF_FLAGS },
885 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
886 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
887 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
888 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
889 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
890 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
891 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
892 { "test", OP_TEST, 2, 2, OPF_FLAGS },
893 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
894 { "retn", OP_RET, 0, 1, OPF_TAIL },
895 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS },
896 { "jmp", OP_JMP, 1, 1, OPF_JMP },
897 { "jecxz",OP_JECXZ, 1, 1, OPF_JMP|OPF_CJMP },
898 { "loop", OP_LOOP, 1, 1, OPF_JMP|OPF_CJMP|OPF_DATA },
899 { "jo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 0 }, // 70 OF=1
900 { "jno", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 1 }, // 71 OF=0
901 { "jc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 CF=1
902 { "jb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72
903 { "jnc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73 CF=0
904 { "jnb", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
905 { "jae", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 1 }, // 73
906 { "jz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74 ZF=1
907 { "je", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 0 }, // 74
908 { "jnz", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75 ZF=0
909 { "jne", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_Z, 1 }, // 75
910 { "jbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76 CF=1||ZF=1
911 { "jna", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 0 }, // 76
912 { "ja", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77 CF=0&&ZF=0
913 { "jnbe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_BE, 1 }, // 77
914 { "js", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 0 }, // 78 SF=1
915 { "jns", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_S, 1 }, // 79 SF=0
916 { "jp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a PF=1
917 { "jpe", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 0 }, // 7a
918 { "jnp", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b PF=0
919 { "jpo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_P, 1 }, // 7b
920 { "jl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c SF!=OF
921 { "jnge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 0 }, // 7c
922 { "jge", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d SF=OF
923 { "jnl", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_L, 1 }, // 7d
924 { "jle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e ZF=1||SF!=OF
925 { "jng", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 0 }, // 7e
926 { "jg", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f ZF=0&&SF=OF
927 { "jnle", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_LE, 1 }, // 7f
928 { "seto", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 0 },
929 { "setno", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_O, 1 },
930 { "setc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
931 { "setb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 0 },
932 { "setnc", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
933 { "setae", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
934 { "setnb", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_C, 1 },
935 { "setz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
936 { "sete", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 0 },
937 { "setnz", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
938 { "setne", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_Z, 1 },
939 { "setbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
940 { "setna", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 0 },
941 { "seta", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
942 { "setnbe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_BE, 1 },
943 { "sets", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 0 },
944 { "setns", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_S, 1 },
945 { "setp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
946 { "setpe", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 0 },
947 { "setnp", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
948 { "setpo", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_P, 1 },
949 { "setl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
950 { "setnge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 0 },
951 { "setge", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
952 { "setnl", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_L, 1 },
953 { "setle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
954 { "setng", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 0 },
955 { "setg", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
956 { "setnle", OP_SCC, 1, 1, OPF_DATA|OPF_CC, PFO_LE, 1 },
959 { "emms", OP_EMMS, 0, 0, OPF_DATA },
960 { "movq", OP_MOV, 2, 2, OPF_DATA },
965 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
967 enum opr_lenmod lmod = OPLM_UNSPEC;
968 int prefix_flags = 0;
976 for (i = 0; i < ARRAY_SIZE(pref_table); i++) {
977 if (IS(words[w], pref_table[i].name)) {
978 prefix_flags = pref_table[i].flags;
985 aerr("lone prefix: '%s'\n", words[0]);
990 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
991 if (IS(words[w], op_table[i].name))
995 if (i == ARRAY_SIZE(op_table)) {
997 aerr("unhandled op: '%s'\n", words[0]);
1002 op->op = op_table[i].op;
1003 op->flags = op_table[i].flags | prefix_flags;
1004 op->pfo = op_table[i].pfo;
1005 op->pfo_inv = op_table[i].pfo_inv;
1006 op->regmask_src = op->regmask_dst = 0;
1009 if (op->op == OP_UD2)
1012 for (opr = 0; opr < op_table[i].maxopr; opr++) {
1013 if (opr >= op_table[i].minopr && w >= wordc)
1016 regmask = regmask_ind = 0;
1017 w = parse_operand(&op->operand[opr], ®mask, ®mask_ind,
1018 words, wordc, w, op->flags);
1020 if (opr == 0 && (op->flags & OPF_DATA))
1021 op->regmask_dst = regmask;
1023 op->regmask_src |= regmask;
1024 op->regmask_src |= regmask_ind;
1028 aerr("parse_op %s incomplete: %d/%d\n",
1029 words[0], w, wordc);
1032 op->operand_cnt = opr;
1033 if (!strncmp(op_table[i].name, "set", 3))
1034 op->operand[0].lmod = OPLM_BYTE;
1037 // first operand is not dst
1040 op->regmask_src |= op->regmask_dst;
1041 op->regmask_dst = 0;
1044 // first operand is src too
1056 op->regmask_src |= op->regmask_dst;
1061 op->regmask_src |= op->regmask_dst;
1062 op->regmask_dst |= op->regmask_src;
1068 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1069 && op->operand[0].lmod == op->operand[1].lmod
1070 && op->operand[0].reg == op->operand[1].reg
1071 && IS(op->operand[0].name, op->operand[1].name)) // ! ah, al..
1073 op->regmask_src = 0;
1076 op->regmask_src |= op->regmask_dst;
1079 // ops with implicit argumets
1081 op->operand_cnt = 2;
1082 setup_reg_opr(&op->operand[0], xAX, OPLM_BYTE, &op->regmask_src);
1083 op->regmask_dst = op->regmask_src;
1084 setup_reg_opr(&op->operand[1], xDX, OPLM_DWORD, &op->regmask_src);
1088 op->operand_cnt = 2;
1089 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
1090 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
1096 if (op->operand_cnt != 0)
1098 if (words[op_w][4] == 'b')
1100 else if (words[op_w][4] == 'w')
1102 else if (words[op_w][4] == 'd')
1104 op->operand_cnt = 3;
1105 setup_reg_opr(&op->operand[0], op->op == OP_LODS ? xSI : xDI,
1106 lmod, &op->regmask_src);
1107 setup_reg_opr(&op->operand[1], xCX, OPLM_DWORD, &op->regmask_src);
1108 op->regmask_dst = op->regmask_src;
1109 setup_reg_opr(&op->operand[2], xAX, OPLM_DWORD,
1110 op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src);
1115 if (op->operand_cnt != 0)
1117 if (words[op_w][4] == 'b')
1119 else if (words[op_w][4] == 'w')
1121 else if (words[op_w][4] == 'd')
1123 op->operand_cnt = 3;
1124 setup_reg_opr(&op->operand[0], xDI, lmod, &op->regmask_src);
1125 setup_reg_opr(&op->operand[1], xSI, OPLM_DWORD, &op->regmask_src);
1126 setup_reg_opr(&op->operand[2], xCX, OPLM_DWORD, &op->regmask_src);
1127 op->regmask_dst = op->regmask_src;
1131 op->regmask_dst = 1 << xCX;
1134 op->operand_cnt = 2;
1135 op->regmask_src = 1 << xCX;
1136 op->operand[1].type = OPT_REG;
1137 op->operand[1].reg = xCX;
1138 op->operand[1].lmod = OPLM_DWORD;
1142 if (op->operand_cnt != 1)
1147 op->regmask_src |= op->regmask_dst;
1148 op->regmask_dst = (1 << xDX) | (1 << xAX);
1149 if (op->operand[0].lmod == OPLM_UNSPEC)
1150 op->operand[0].lmod = OPLM_DWORD;
1155 // we could set up operands for edx:eax, but there is no real need to
1156 // (see is_opr_modified())
1157 op->regmask_src |= op->regmask_dst;
1158 op->regmask_dst = (1 << xDX) | (1 << xAX);
1159 if (op->operand[0].lmod == OPLM_UNSPEC)
1160 op->operand[0].lmod = OPLM_DWORD;
1168 op->regmask_src |= op->regmask_dst;
1169 if (op->operand[1].lmod == OPLM_UNSPEC)
1170 op->operand[1].lmod = OPLM_BYTE;
1174 op->regmask_src |= op->regmask_dst;
1175 if (op->operand[2].lmod == OPLM_UNSPEC)
1176 op->operand[2].lmod = OPLM_BYTE;
1180 op->regmask_src |= op->regmask_dst;
1181 op->regmask_dst = 0;
1182 if (op->operand[0].lmod == OPLM_UNSPEC
1183 && (op->operand[0].type == OPT_CONST
1184 || op->operand[0].type == OPT_OFFSET
1185 || op->operand[0].type == OPT_LABEL))
1186 op->operand[0].lmod = OPLM_DWORD;
1192 if (op->operand[0].type == OPT_REG && op->operand[1].type == OPT_REG
1193 && op->operand[0].lmod == op->operand[1].lmod
1194 && op->operand[0].reg == op->operand[1].reg
1195 && IS(op->operand[0].name, op->operand[1].name)) // ! ah, al..
1197 op->flags |= OPF_RMD | OPF_DONE;
1198 op->regmask_src = op->regmask_dst = 0;
1203 if (op->operand[0].type == OPT_REG
1204 && op->operand[1].type == OPT_REGMEM)
1207 snprintf(buf, sizeof(buf), "%s+0", op->operand[0].name);
1208 if (IS(buf, op->operand[1].name))
1209 op->flags |= OPF_RMD | OPF_DONE;
1214 // trashed regs must be explicitly detected later
1215 op->regmask_dst = 0;
1219 op->regmask_dst = (1 << xBP) | (1 << xSP);
1220 op->regmask_src = 1 << xBP;
1227 if (op->operand[0].type == OPT_REG
1228 && op->operand[0].lmod == OPLM_DWORD
1229 && op->operand[1].type == OPT_CONST)
1231 if ((op->op == OP_AND && op->operand[1].val == 0)
1232 || (op->op == OP_OR && op->operand[1].val == ~0))
1234 op->regmask_src = 0;
1239 static const char *op_name(struct parsed_op *po)
1241 static char buf[16];
1245 if (po->op == OP_JCC || po->op == OP_SCC) {
1247 *p++ = (po->op == OP_JCC) ? 'j' : 's';
1250 strcpy(p, parsed_flag_op_names[po->pfo]);
1254 for (i = 0; i < ARRAY_SIZE(op_table); i++)
1255 if (op_table[i].op == po->op)
1256 return op_table[i].name;
1262 static const char *dump_op(struct parsed_op *po)
1264 static char out[128];
1271 snprintf(out, sizeof(out), "%s", op_name(po));
1272 for (i = 0; i < po->operand_cnt; i++) {
1276 snprintf(p, sizeof(out) - (p - out),
1277 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
1278 po->operand[i].name);
1284 static const char *lmod_type_u(struct parsed_op *po,
1285 enum opr_lenmod lmod)
1297 ferr(po, "invalid lmod: %d\n", lmod);
1298 return "(_invalid_)";
1302 static const char *lmod_cast_u(struct parsed_op *po,
1303 enum opr_lenmod lmod)
1315 ferr(po, "invalid lmod: %d\n", lmod);
1316 return "(_invalid_)";
1320 static const char *lmod_cast_u_ptr(struct parsed_op *po,
1321 enum opr_lenmod lmod)
1333 ferr(po, "invalid lmod: %d\n", lmod);
1334 return "(_invalid_)";
1338 static const char *lmod_cast_s(struct parsed_op *po,
1339 enum opr_lenmod lmod)
1351 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1352 return "(_invalid_)";
1356 static const char *lmod_cast(struct parsed_op *po,
1357 enum opr_lenmod lmod, int is_signed)
1360 lmod_cast_s(po, lmod) :
1361 lmod_cast_u(po, lmod);
1364 static int lmod_bytes(struct parsed_op *po, enum opr_lenmod lmod)
1376 ferr(po, "%s: invalid lmod: %d\n", __func__, lmod);
1381 static const char *opr_name(struct parsed_op *po, int opr_num)
1383 if (opr_num >= po->operand_cnt)
1384 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1385 return po->operand[opr_num].name;
1388 static unsigned int opr_const(struct parsed_op *po, int opr_num)
1390 if (opr_num >= po->operand_cnt)
1391 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
1392 if (po->operand[opr_num].type != OPT_CONST)
1393 ferr(po, "opr %d: const expected\n", opr_num);
1394 return po->operand[opr_num].val;
1397 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
1399 if ((unsigned int)popr->reg >= ARRAY_SIZE(regs_r32))
1400 ferr(po, "invalid reg: %d\n", popr->reg);
1401 return regs_r32[popr->reg];
1404 // cast1 is the "final" cast
1405 static const char *simplify_cast(const char *cast1, const char *cast2)
1407 static char buf[256];
1413 if (IS(cast1, cast2))
1415 if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
1417 if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
1419 if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
1421 if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
1423 if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
1426 snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
1430 static const char *simplify_cast_num(const char *cast, unsigned int val)
1432 if (IS(cast, "(u8)") && val < 0x100)
1434 if (IS(cast, "(s8)") && val < 0x80)
1436 if (IS(cast, "(u16)") && val < 0x10000)
1438 if (IS(cast, "(s16)") && val < 0x8000)
1440 if (IS(cast, "(s32)") && val < 0x80000000)
1446 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
1455 namelen = strlen(name);
1457 p = strchr(name, '+');
1461 ferr(po, "equ parse failed for '%s'\n", name);
1463 if (IS_START(p, "0x"))
1465 *extra_offs = strtol(p, &endp, 16);
1467 ferr(po, "equ parse failed for '%s'\n", name);
1470 for (i = 0; i < g_eqcnt; i++)
1471 if (strncmp(g_eqs[i].name, name, namelen) == 0
1472 && g_eqs[i].name[namelen] == 0)
1476 ferr(po, "unresolved equ name: '%s'\n", name);
1483 static int is_stack_access(struct parsed_op *po,
1484 const struct parsed_opr *popr)
1486 return (parse_stack_el(popr->name, NULL, 0)
1487 || (g_bp_frame && !(po->flags & OPF_EBP_S)
1488 && IS_START(popr->name, "ebp")));
1491 static void parse_stack_access(struct parsed_op *po,
1492 const char *name, char *ofs_reg, int *offset_out,
1493 int *stack_ra_out, const char **bp_arg_out, int is_lea)
1495 const char *bp_arg = "";
1496 const char *p = NULL;
1497 struct parsed_equ *eq;
1504 if (IS_START(name, "ebp-")
1505 || (IS_START(name, "ebp+") && '0' <= name[4] && name[4] <= '9'))
1508 if (IS_START(p, "0x"))
1510 offset = strtoul(p, &endp, 16);
1514 ferr(po, "ebp- parse of '%s' failed\n", name);
1517 bp_arg = parse_stack_el(name, ofs_reg, 0);
1518 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
1519 eq = equ_find(po, bp_arg, &offset);
1521 ferr(po, "detected but missing eq\n");
1522 offset += eq->offset;
1525 if (!strncmp(name, "ebp", 3))
1528 // yes it sometimes LEAs ra for compares..
1529 if (!is_lea && ofs_reg[0] == 0
1530 && stack_ra <= offset && offset < stack_ra + 4)
1532 ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
1535 *offset_out = offset;
1536 *stack_ra_out = stack_ra;
1538 *bp_arg_out = bp_arg;
1541 static int stack_frame_access(struct parsed_op *po,
1542 struct parsed_opr *popr, char *buf, size_t buf_size,
1543 const char *name, const char *cast, int is_src, int is_lea)
1545 enum opr_lenmod tmp_lmod = OPLM_UNSPEC;
1546 const char *prefix = "";
1547 const char *bp_arg = NULL;
1548 char ofs_reg[16] = { 0, };
1549 int i, arg_i, arg_s;
1557 if (po->flags & OPF_EBP_S)
1558 ferr(po, "stack_frame_access while ebp is scratch\n");
1560 parse_stack_access(po, name, ofs_reg, &offset,
1561 &stack_ra, &bp_arg, is_lea);
1563 if (offset > stack_ra)
1565 arg_i = (offset - stack_ra - 4) / 4;
1566 if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
1568 if (g_func_pp->is_vararg
1569 && arg_i == g_func_pp->argc_stack && is_lea)
1571 // should be va_list
1574 snprintf(buf, buf_size, "%sap", cast);
1577 ferr(po, "offset %d (%s,%d) doesn't map to any arg\n",
1578 offset, bp_arg, arg_i);
1580 if (ofs_reg[0] != 0)
1581 ferr(po, "offset reg on arg access?\n");
1583 for (i = arg_s = 0; i < g_func_pp->argc; i++) {
1584 if (g_func_pp->arg[i].reg != NULL)
1590 if (i == g_func_pp->argc)
1591 ferr(po, "arg %d not in prototype?\n", arg_i);
1593 popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
1600 ferr(po, "lea/byte to arg?\n");
1601 if (is_src && (offset & 3) == 0)
1602 snprintf(buf, buf_size, "%sa%d",
1603 simplify_cast(cast, "(u8)"), i + 1);
1605 snprintf(buf, buf_size, "%sBYTE%d(a%d)",
1606 cast, offset & 3, i + 1);
1611 ferr(po, "lea/word to arg?\n");
1616 ferr(po, "problematic arg store\n");
1617 snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
1618 simplify_cast(cast, "*(u16 *)"), i + 1);
1621 ferr(po, "unaligned arg word load\n");
1623 else if (is_src && (offset & 2) == 0)
1624 snprintf(buf, buf_size, "%sa%d",
1625 simplify_cast(cast, "(u16)"), i + 1);
1627 snprintf(buf, buf_size, "%s%sWORD(a%d)",
1628 cast, (offset & 2) ? "HI" : "LO", i + 1);
1640 snprintf(buf, buf_size, "(u32)&a%d + %d",
1643 ferr(po, "unaligned arg store\n");
1645 // mov edx, [ebp+arg_4+2]; movsx ecx, dx
1646 snprintf(buf, buf_size, "%s(a%d >> %d)",
1647 prefix, i + 1, (offset & 3) * 8);
1651 snprintf(buf, buf_size, "%s%sa%d",
1652 prefix, is_lea ? "&" : "", i + 1);
1657 ferr(po, "bp_arg bad lmod: %d\n", popr->lmod);
1661 snprintf(g_comment, sizeof(g_comment), "%s unaligned", bp_arg);
1664 guess_lmod_from_c_type(&tmp_lmod, &g_func_pp->arg[i].type);
1665 if (tmp_lmod != OPLM_DWORD
1666 && (unaligned || (!is_src && lmod_bytes(po, tmp_lmod)
1667 < lmod_bytes(po, popr->lmod) + (offset & 3))))
1669 ferr(po, "bp_arg arg%d/w offset %d and type '%s' is too small\n",
1670 i + 1, offset, g_func_pp->arg[i].type.name);
1672 // can't check this because msvc likes to reuse
1673 // arg space for scratch..
1674 //if (popr->is_ptr && popr->lmod != OPLM_DWORD)
1675 // ferr(po, "bp_arg arg%d: non-dword ptr access\n", i + 1);
1679 if (g_stack_fsz == 0)
1680 ferr(po, "stack var access without stackframe\n");
1681 g_stack_frame_used = 1;
1683 sf_ofs = g_stack_fsz + offset;
1684 lim = (ofs_reg[0] != 0) ? -4 : 0;
1685 if (offset > 0 || sf_ofs < lim)
1686 ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
1696 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1697 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1701 if ((sf_ofs & 1) || ofs_reg[0] != 0) {
1702 // known unaligned or possibly unaligned
1703 strcat(g_comment, " unaligned");
1705 prefix = "*(u16 *)&";
1706 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1707 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1710 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
1714 if ((sf_ofs & 3) || ofs_reg[0] != 0) {
1715 // known unaligned or possibly unaligned
1716 strcat(g_comment, " unaligned");
1718 prefix = "*(u32 *)&";
1719 snprintf(buf, buf_size, "%ssf.b[%d%s%s]",
1720 prefix, sf_ofs, ofs_reg[0] ? "+" : "", ofs_reg);
1723 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
1727 ferr(po, "bp_stack bad lmod: %d\n", popr->lmod);
1734 static void check_func_pp(struct parsed_op *po,
1735 const struct parsed_proto *pp, const char *pfx)
1737 enum opr_lenmod tmp_lmod;
1741 if (pp->argc_reg != 0) {
1742 if (/*!g_allow_regfunc &&*/ !pp->is_fastcall) {
1743 pp_print(buf, sizeof(buf), pp);
1744 ferr(po, "%s: unexpected reg arg in icall: %s\n", pfx, buf);
1746 if (pp->argc_stack > 0 && pp->argc_reg != 2)
1747 ferr(po, "%s: %d reg arg(s) with %d stack arg(s)\n",
1748 pfx, pp->argc_reg, pp->argc_stack);
1751 // fptrs must use 32bit args, callsite might have no information and
1752 // lack a cast to smaller types, which results in incorrectly masked
1753 // args passed (callee may assume masked args, it does on ARM)
1754 if (!pp->is_osinc) {
1755 for (i = 0; i < pp->argc; i++) {
1756 ret = guess_lmod_from_c_type(&tmp_lmod, &pp->arg[i].type);
1757 if (ret && tmp_lmod != OPLM_DWORD)
1758 ferr(po, "reference to %s with arg%d '%s'\n", pp->name,
1759 i + 1, pp->arg[i].type.name);
1764 static const char *check_label_read_ref(struct parsed_op *po,
1767 const struct parsed_proto *pp;
1769 pp = proto_parse(g_fhdr, name, 0);
1771 ferr(po, "proto_parse failed for ref '%s'\n", name);
1774 check_func_pp(po, pp, "ref");
1779 static char *out_src_opr(char *buf, size_t buf_size,
1780 struct parsed_op *po, struct parsed_opr *popr, const char *cast,
1783 char tmp1[256], tmp2[256];
1792 switch (popr->type) {
1795 ferr(po, "lea from reg?\n");
1797 switch (popr->lmod) {
1799 snprintf(buf, buf_size, "%s%s.q", cast, opr_reg_p(po, popr));
1802 snprintf(buf, buf_size, "%s%s", cast, opr_reg_p(po, popr));
1805 snprintf(buf, buf_size, "%s%s",
1806 simplify_cast(cast, "(u16)"), opr_reg_p(po, popr));
1809 if (popr->name[1] == 'h') // XXX..
1810 snprintf(buf, buf_size, "%s(%s >> 8)",
1811 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1813 snprintf(buf, buf_size, "%s%s",
1814 simplify_cast(cast, "(u8)"), opr_reg_p(po, popr));
1817 ferr(po, "invalid src lmod: %d\n", popr->lmod);
1822 if (is_stack_access(po, popr)) {
1823 stack_frame_access(po, popr, buf, buf_size,
1824 popr->name, cast, 1, is_lea);
1828 strcpy(expr, popr->name);
1829 if (strchr(expr, '[')) {
1830 // special case: '[' can only be left for label[reg] form
1831 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
1833 ferr(po, "parse failure for '%s'\n", expr);
1834 if (tmp1[0] == '(') {
1835 // (off_4FFF50+3)[eax]
1836 p = strchr(tmp1 + 1, ')');
1837 if (p == NULL || p[1] != 0)
1838 ferr(po, "parse failure (2) for '%s'\n", expr);
1840 memmove(tmp1, tmp1 + 1, strlen(tmp1));
1842 snprintf(expr, sizeof(expr), "(u32)&%s + %s", tmp1, tmp2);
1845 // XXX: do we need more parsing?
1847 snprintf(buf, buf_size, "%s", expr);
1851 snprintf(buf, buf_size, "%s(%s)",
1852 simplify_cast(cast, lmod_cast_u_ptr(po, popr->lmod)), expr);
1856 name = check_label_read_ref(po, popr->name);
1857 if (cast[0] == 0 && popr->is_ptr)
1861 snprintf(buf, buf_size, "(u32)&%s", name);
1862 else if (popr->size_lt)
1863 snprintf(buf, buf_size, "%s%s%s%s", cast,
1864 lmod_cast_u_ptr(po, popr->lmod),
1865 popr->is_array ? "" : "&", name);
1867 snprintf(buf, buf_size, "%s%s%s", cast, name,
1868 popr->is_array ? "[0]" : "");
1872 name = check_label_read_ref(po, popr->name);
1876 ferr(po, "lea an offset?\n");
1877 snprintf(buf, buf_size, "%s&%s", cast, name);
1882 ferr(po, "lea from const?\n");
1884 printf_number(tmp1, sizeof(tmp1), popr->val);
1885 if (popr->val == 0 && strchr(cast, '*'))
1886 snprintf(buf, buf_size, "NULL");
1888 snprintf(buf, buf_size, "%s%s",
1889 simplify_cast_num(cast, popr->val), tmp1);
1893 ferr(po, "invalid src type: %d\n", popr->type);
1899 // note: may set is_ptr (we find that out late for ebp frame..)
1900 static char *out_dst_opr(char *buf, size_t buf_size,
1901 struct parsed_op *po, struct parsed_opr *popr)
1903 switch (popr->type) {
1905 switch (popr->lmod) {
1907 snprintf(buf, buf_size, "%s.q", opr_reg_p(po, popr));
1910 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
1914 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
1918 if (popr->name[1] == 'h') // XXX..
1919 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
1921 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
1924 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
1929 if (is_stack_access(po, popr)) {
1930 stack_frame_access(po, popr, buf, buf_size,
1931 popr->name, "", 0, 0);
1935 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1938 if (popr->size_mismatch)
1939 snprintf(buf, buf_size, "%s%s%s",
1940 lmod_cast_u_ptr(po, popr->lmod),
1941 popr->is_array ? "" : "&", popr->name);
1943 snprintf(buf, buf_size, "%s%s", popr->name,
1944 popr->is_array ? "[0]" : "");
1948 ferr(po, "invalid dst type: %d\n", popr->type);
1954 static char *out_src_opr_u32(char *buf, size_t buf_size,
1955 struct parsed_op *po, struct parsed_opr *popr)
1957 return out_src_opr(buf, buf_size, po, popr, NULL, 0);
1960 static void out_test_for_cc(char *buf, size_t buf_size,
1961 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1962 enum opr_lenmod lmod, const char *expr)
1964 const char *cast, *scast;
1966 cast = lmod_cast_u(po, lmod);
1967 scast = lmod_cast_s(po, lmod);
1971 case PFO_BE: // CF=1||ZF=1; CF=0
1972 snprintf(buf, buf_size, "(%s%s %s 0)",
1973 cast, expr, is_inv ? "!=" : "==");
1977 case PFO_L: // SF!=OF; OF=0
1978 snprintf(buf, buf_size, "(%s%s %s 0)",
1979 scast, expr, is_inv ? ">=" : "<");
1982 case PFO_LE: // ZF=1||SF!=OF; OF=0
1983 snprintf(buf, buf_size, "(%s%s %s 0)",
1984 scast, expr, is_inv ? ">" : "<=");
1988 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1992 static void out_cmp_for_cc(char *buf, size_t buf_size,
1993 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1995 const char *cast, *scast, *cast_use;
1996 char buf1[256], buf2[256];
1997 enum opr_lenmod lmod;
1999 if (po->op != OP_DEC && po->operand[0].lmod != po->operand[1].lmod)
2000 ferr(po, "%s: lmod mismatch: %d %d\n", __func__,
2001 po->operand[0].lmod, po->operand[1].lmod);
2002 lmod = po->operand[0].lmod;
2004 cast = lmod_cast_u(po, lmod);
2005 scast = lmod_cast_s(po, lmod);
2021 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
2024 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], cast_use, 0);
2025 if (po->op == OP_DEC)
2026 snprintf(buf2, sizeof(buf2), "1");
2028 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], cast_use, 0);
2032 // note: must be unsigned compare
2033 snprintf(buf, buf_size, "(%s %s %s)",
2034 buf1, is_inv ? ">=" : "<", buf2);
2038 snprintf(buf, buf_size, "(%s %s %s)",
2039 buf1, is_inv ? "!=" : "==", buf2);
2043 // note: must be unsigned compare
2044 snprintf(buf, buf_size, "(%s %s %s)",
2045 buf1, is_inv ? ">" : "<=", buf2);
2048 if (is_inv && lmod == OPLM_BYTE
2049 && po->operand[1].type == OPT_CONST
2050 && po->operand[1].val == 0xff)
2052 snprintf(g_comment, sizeof(g_comment), "if %s", buf);
2053 snprintf(buf, buf_size, "(0)");
2057 // note: must be signed compare
2059 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
2060 scast, buf1, buf2, is_inv ? ">=" : "<");
2064 snprintf(buf, buf_size, "(%s %s %s)",
2065 buf1, is_inv ? ">=" : "<", buf2);
2069 snprintf(buf, buf_size, "(%s %s %s)",
2070 buf1, is_inv ? ">" : "<=", buf2);
2078 static void out_cmp_test(char *buf, size_t buf_size,
2079 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
2081 char buf1[256], buf2[256], buf3[256];
2083 if (po->op == OP_TEST) {
2084 if (IS(opr_name(po, 0), opr_name(po, 1))) {
2085 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[0]);
2088 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
2089 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
2090 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
2092 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
2093 po->operand[0].lmod, buf3);
2095 else if (po->op == OP_CMP) {
2096 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv);
2099 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
2102 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
2103 struct parsed_opr *popr2)
2105 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
2106 ferr(po, "missing lmod for both operands\n");
2108 if (popr1->lmod == OPLM_UNSPEC)
2109 popr1->lmod = popr2->lmod;
2110 else if (popr2->lmod == OPLM_UNSPEC)
2111 popr2->lmod = popr1->lmod;
2112 else if (popr1->lmod != popr2->lmod) {
2113 if (popr1->type_from_var) {
2114 popr1->size_mismatch = 1;
2115 if (popr1->lmod < popr2->lmod)
2117 popr1->lmod = popr2->lmod;
2119 else if (popr2->type_from_var) {
2120 popr2->size_mismatch = 1;
2121 if (popr2->lmod < popr1->lmod)
2123 popr2->lmod = popr1->lmod;
2126 ferr(po, "conflicting lmods: %d vs %d\n",
2127 popr1->lmod, popr2->lmod);
2131 static const char *op_to_c(struct parsed_op *po)
2155 ferr(po, "op_to_c was supplied with %d\n", po->op);
2159 static void op_set_clear_flag(struct parsed_op *po,
2160 enum op_flags flag_set, enum op_flags flag_clear)
2162 po->flags |= flag_set;
2163 po->flags &= ~flag_clear;
2166 // last op in stream - unconditional branch or ret
2167 #define LAST_OP(_i) ((ops[_i].flags & OPF_TAIL) \
2168 || ((ops[_i].flags & (OPF_JMP|OPF_CJMP|OPF_RMD)) == OPF_JMP \
2169 && ops[_i].op != OP_CALL))
2171 #define check_i(po, i) \
2173 ferr(po, "bad " #i ": %d\n", i)
2175 static int scan_for_pop(int i, int opcnt, const char *reg,
2176 int magic, int depth, int *maxdepth, int do_flags)
2178 const struct parsed_proto *pp;
2179 struct parsed_op *po;
2183 for (; i < opcnt; i++) {
2185 if (po->cc_scratch == magic)
2186 break; // already checked
2187 po->cc_scratch = magic;
2189 if (po->flags & OPF_TAIL) {
2190 if (po->op == OP_CALL) {
2191 pp = proto_parse(g_fhdr, po->operand[0].name, g_quiet_pp);
2192 if (pp != NULL && pp->is_noreturn)
2193 // no stack cleanup for noreturn
2196 return -1; // deadend
2199 if ((po->flags & (OPF_RMD|OPF_DONE))
2200 || (po->op == OP_PUSH && po->p_argnum != 0)) // arg push
2203 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2204 if (po->btj != NULL) {
2206 for (j = 0; j < po->btj->count; j++) {
2207 check_i(po, po->btj->d[j].bt_i);
2208 ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, reg, magic,
2209 depth, maxdepth, do_flags);
2211 return ret; // dead end
2216 check_i(po, po->bt_i);
2217 if (po->flags & OPF_CJMP) {
2218 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic,
2219 depth, maxdepth, do_flags);
2221 return ret; // dead end
2229 if ((po->op == OP_POP || po->op == OP_PUSH)
2230 && po->operand[0].type == OPT_REG
2231 && IS(po->operand[0].name, reg))
2233 if (po->op == OP_PUSH && !(po->flags & OPF_FARGNR)) {
2235 if (depth > *maxdepth)
2238 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2240 else if (po->op == OP_POP) {
2243 op_set_clear_flag(po, OPF_RMD, OPF_RSAVE);
2248 if (depth < 0) // should not happen
2249 ferr(po, "fail with depth\n");
2251 op_set_clear_flag(po, OPF_RSAVE, OPF_RMD);
2260 // scan for pop starting from 'ret' op (all paths)
2261 static int scan_for_pop_ret(int i, int opcnt, const char *reg,
2267 for (; i < opcnt; i++) {
2268 if (!(ops[i].flags & OPF_TAIL))
2271 for (j = i - 1; j >= 0; j--) {
2272 if (ops[j].flags & (OPF_RMD|OPF_DONE))
2274 if (ops[j].flags & OPF_JMP)
2277 if (ops[j].op == OP_POP && ops[j].datap == NULL
2278 && ops[j].operand[0].type == OPT_REG
2279 && IS(ops[j].operand[0].name, reg))
2282 ops[j].flags |= flag_set;
2286 if (g_labels[j] != NULL)
2291 return found ? 0 : -1;
2294 static void scan_for_pop_const(int i, int opcnt, int *regmask_pp)
2296 struct parsed_op *po;
2297 int is_multipath = 0;
2300 for (j = i + 1; j < opcnt; j++) {
2303 if (po->op == OP_JMP && po->btj == NULL) {
2304 ferr_assert(po, po->bt_i >= 0);
2309 if ((po->flags & (OPF_JMP|OPF_TAIL|OPF_RSAVE))
2310 || po->op == OP_PUSH)
2315 if (g_labels[j] != NULL)
2318 if (po->op == OP_POP && !(po->flags & OPF_RMD))
2320 is_multipath |= !!(po->flags & OPF_PPUSH);
2322 ops[i].flags |= OPF_PPUSH | OPF_DONE;
2324 po->flags |= OPF_PPUSH | OPF_DONE;
2325 *regmask_pp |= 1 << po->operand[0].reg;
2328 ops[i].flags |= OPF_RMD | OPF_DONE;
2329 po->flags |= OPF_DONE;
2330 po->datap = &ops[i];
2337 static void scan_propagate_df(int i, int opcnt)
2339 struct parsed_op *po = &ops[i];
2342 for (; i < opcnt; i++) {
2344 if (po->flags & OPF_DF)
2345 return; // already resolved
2346 po->flags |= OPF_DF;
2348 if (po->op == OP_CALL)
2349 ferr(po, "call with DF set?\n");
2351 if (po->flags & OPF_JMP) {
2352 if (po->btj != NULL) {
2354 for (j = 0; j < po->btj->count; j++) {
2355 check_i(po, po->btj->d[j].bt_i);
2356 scan_propagate_df(po->btj->d[j].bt_i, opcnt);
2361 check_i(po, po->bt_i);
2362 if (po->flags & OPF_CJMP)
2363 scan_propagate_df(po->bt_i, opcnt);
2369 if (po->flags & OPF_TAIL)
2372 if (po->op == OP_CLD) {
2373 po->flags |= OPF_RMD | OPF_DONE;
2378 ferr(po, "missing DF clear?\n");
2381 // is operand 'opr' referenced by parsed_op 'po'?
2382 static int is_opr_referenced(const struct parsed_opr *opr,
2383 const struct parsed_op *po)
2387 if (opr->type == OPT_REG) {
2388 mask = po->regmask_dst | po->regmask_src;
2389 if (po->op == OP_CALL)
2390 mask |= (1 << xAX) | (1 << xCX) | (1 << xDX);
2391 if ((1 << opr->reg) & mask)
2397 for (i = 0; i < po->operand_cnt; i++)
2398 if (IS(po->operand[0].name, opr->name))
2404 // is operand 'opr' read by parsed_op 'po'?
2405 static int is_opr_read(const struct parsed_opr *opr,
2406 const struct parsed_op *po)
2410 if (opr->type == OPT_REG) {
2411 mask = po->regmask_src;
2412 if (po->op == OP_CALL)
2413 // assume worst case
2414 mask |= (1 << xAX) | (1 << xCX) | (1 << xDX);
2415 if ((1 << opr->reg) & mask)
2425 // is operand 'opr' modified by parsed_op 'po'?
2426 static int is_opr_modified(const struct parsed_opr *opr,
2427 const struct parsed_op *po)
2431 if (!(po->flags & OPF_DATA))
2434 if (opr->type == OPT_REG) {
2435 if (po->op == OP_CALL) {
2436 mask = (1 << xAX) | (1 << xCX) | (1 << xDX);
2437 if ((1 << opr->reg) & mask)
2443 if (po->operand[0].type == OPT_REG) {
2444 if (po->regmask_dst & (1 << opr->reg))
2451 return IS(po->operand[0].name, opr->name);
2454 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
2455 static int is_any_opr_modified(const struct parsed_op *po_test,
2456 const struct parsed_op *po, int c_mode)
2461 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
2464 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2467 if ((po_test->regmask_src | po_test->regmask_dst) & po->regmask_dst)
2470 // in reality, it can wreck any register, but in decompiled C
2471 // version it can only overwrite eax or edx:eax
2472 mask = (1 << xAX) | (1 << xDX);
2476 if (po->op == OP_CALL
2477 && ((po_test->regmask_src | po_test->regmask_dst) & mask))
2480 for (i = 0; i < po_test->operand_cnt; i++)
2481 if (IS(po_test->operand[i].name, po->operand[0].name))
2487 // scan for any po_test operand modification in range given
2488 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt,
2491 if (po_test->operand_cnt == 1 && po_test->operand[0].type == OPT_CONST)
2494 for (; i < opcnt; i++) {
2495 if (is_any_opr_modified(po_test, &ops[i], c_mode))
2502 // scan for po_test operand[0] modification in range given
2503 static int scan_for_mod_opr0(struct parsed_op *po_test,
2506 for (; i < opcnt; i++) {
2507 if (is_opr_modified(&po_test->operand[0], &ops[i]))
2514 static int scan_for_flag_set(int i, int magic, int *branched,
2515 int *setters, int *setter_cnt)
2517 struct label_ref *lr;
2521 if (ops[i].cc_scratch == magic) {
2522 // is this a problem?
2523 //ferr(&ops[i], "%s looped\n", __func__);
2526 ops[i].cc_scratch = magic;
2528 if (g_labels[i] != NULL) {
2531 lr = &g_label_refs[i];
2532 for (; lr->next; lr = lr->next) {
2533 check_i(&ops[i], lr->i);
2534 ret = scan_for_flag_set(lr->i, magic,
2535 branched, setters, setter_cnt);
2540 check_i(&ops[i], lr->i);
2541 if (i > 0 && LAST_OP(i - 1)) {
2545 ret = scan_for_flag_set(lr->i, magic,
2546 branched, setters, setter_cnt);
2552 if (ops[i].flags & OPF_FLAGS) {
2553 setters[*setter_cnt] = i;
2558 if ((ops[i].flags & (OPF_JMP|OPF_CJMP)) == OPF_JMP)
2565 // scan back for cdq, if anything modifies edx, fail
2566 static int scan_for_cdq_edx(int i)
2569 if (g_labels[i] != NULL) {
2570 if (g_label_refs[i].next != NULL)
2572 if (i > 0 && LAST_OP(i - 1)) {
2573 i = g_label_refs[i].i;
2580 if (ops[i].op == OP_CDQ)
2583 if (ops[i].regmask_dst & (1 << xDX))
2590 static int scan_for_reg_clear(int i, int reg)
2593 if (g_labels[i] != NULL) {
2594 if (g_label_refs[i].next != NULL)
2596 if (i > 0 && LAST_OP(i - 1)) {
2597 i = g_label_refs[i].i;
2604 if (ops[i].op == OP_XOR
2605 && ops[i].operand[0].lmod == OPLM_DWORD
2606 && ops[i].operand[0].reg == ops[i].operand[1].reg
2607 && ops[i].operand[0].reg == reg)
2610 if (ops[i].regmask_dst & (1 << reg))
2617 static void patch_esp_adjust(struct parsed_op *po, int adj)
2619 ferr_assert(po, po->op == OP_ADD);
2620 ferr_assert(po, IS(opr_name(po, 0), "esp"));
2621 ferr_assert(po, po->operand[1].type == OPT_CONST);
2623 // this is a bit of a hack, but deals with use of
2624 // single adj for multiple calls
2625 po->operand[1].val -= adj;
2626 po->flags |= OPF_RMD;
2627 if (po->operand[1].val == 0)
2628 po->flags |= OPF_DONE;
2629 ferr_assert(po, (int)po->operand[1].val >= 0);
2632 // scan for positive, constant esp adjust
2633 // multipath case is preliminary
2634 static int scan_for_esp_adjust(int i, int opcnt,
2635 int adj_expect, int *adj, int *is_multipath, int do_update)
2637 int adj_expect_unknown = 0;
2638 struct parsed_op *po;
2642 *adj = *is_multipath = 0;
2643 if (adj_expect < 0) {
2644 adj_expect_unknown = 1;
2645 adj_expect = 32 * 4; // enough?
2648 for (; i < opcnt && *adj < adj_expect; i++) {
2649 if (g_labels[i] != NULL)
2653 if (po->flags & OPF_DONE)
2656 if (po->op == OP_ADD && po->operand[0].reg == xSP) {
2657 if (po->operand[1].type != OPT_CONST)
2658 ferr(&ops[i], "non-const esp adjust?\n");
2659 *adj += po->operand[1].val;
2661 ferr(&ops[i], "unaligned esp adjust: %x\n", *adj);
2664 patch_esp_adjust(po, adj_expect);
2666 po->flags |= OPF_RMD;
2670 else if (po->op == OP_PUSH) {
2671 //if (first_pop == -1)
2672 // first_pop = -2; // none
2673 *adj -= lmod_bytes(po, po->operand[0].lmod);
2675 else if (po->op == OP_POP) {
2676 if (!(po->flags & OPF_DONE)) {
2677 // seems like msvc only uses 'pop ecx' for stack realignment..
2678 if (po->operand[0].type != OPT_REG || po->operand[0].reg != xCX)
2680 if (first_pop == -1 && *adj >= 0)
2683 if (do_update && *adj >= 0) {
2684 po->flags |= OPF_RMD;
2686 po->flags |= OPF_DONE;
2689 *adj += lmod_bytes(po, po->operand[0].lmod);
2690 if (*adj > adj_best)
2693 else if (po->flags & (OPF_JMP|OPF_TAIL)) {
2694 if (po->op == OP_JMP && po->btj == NULL) {
2700 if (po->op != OP_CALL)
2702 if (po->operand[0].type != OPT_LABEL)
2704 if (po->pp != NULL && po->pp->is_stdcall)
2706 if (adj_expect_unknown && first_pop >= 0)
2708 // assume it's another cdecl call
2712 if (first_pop >= 0) {
2713 // probably only 'pop ecx' was used
2721 static void scan_fwd_set_flags(int i, int opcnt, int magic, int flags)
2723 struct parsed_op *po;
2727 ferr(ops, "%s: followed bad branch?\n", __func__);
2729 for (; i < opcnt; i++) {
2731 if (po->cc_scratch == magic)
2733 po->cc_scratch = magic;
2736 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
2737 if (po->btj != NULL) {
2739 for (j = 0; j < po->btj->count; j++)
2740 scan_fwd_set_flags(po->btj->d[j].bt_i, opcnt, magic, flags);
2744 scan_fwd_set_flags(po->bt_i, opcnt, magic, flags);
2745 if (!(po->flags & OPF_CJMP))
2748 if (po->flags & OPF_TAIL)
2753 static const struct parsed_proto *try_recover_pp(
2754 struct parsed_op *po, const struct parsed_opr *opr, int *search_instead)
2756 const struct parsed_proto *pp = NULL;
2760 // maybe an arg of g_func?
2761 if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
2763 char ofs_reg[16] = { 0, };
2764 int arg, arg_s, arg_i;
2771 parse_stack_access(po, opr->name, ofs_reg,
2772 &offset, &stack_ra, NULL, 0);
2773 if (ofs_reg[0] != 0)
2774 ferr(po, "offset reg on arg access?\n");
2775 if (offset <= stack_ra) {
2776 // search who set the stack var instead
2777 if (search_instead != NULL)
2778 *search_instead = 1;
2782 arg_i = (offset - stack_ra - 4) / 4;
2783 for (arg = arg_s = 0; arg < g_func_pp->argc; arg++) {
2784 if (g_func_pp->arg[arg].reg != NULL)
2790 if (arg == g_func_pp->argc)
2791 ferr(po, "stack arg %d not in prototype?\n", arg_i);
2793 pp = g_func_pp->arg[arg].fptr;
2795 ferr(po, "icall sa: arg%d is not a fptr?\n", arg + 1);
2796 check_func_pp(po, pp, "icall arg");
2798 else if (opr->type == OPT_REGMEM && strchr(opr->name + 1, '[')) {
2800 p = strchr(opr->name + 1, '[');
2801 memcpy(buf, opr->name, p - opr->name);
2802 buf[p - opr->name] = 0;
2803 pp = proto_parse(g_fhdr, buf, g_quiet_pp);
2805 else if (opr->type == OPT_OFFSET || opr->type == OPT_LABEL) {
2806 pp = proto_parse(g_fhdr, opr->name, g_quiet_pp);
2809 ferr(po, "proto_parse failed for icall to '%s'\n", opr->name);
2812 check_func_pp(po, pp, "reg-fptr ref");
2818 static void scan_for_call_type(int i, const struct parsed_opr *opr,
2819 int magic, const struct parsed_proto **pp_found, int *pp_i,
2822 const struct parsed_proto *pp = NULL;
2823 struct parsed_op *po;
2824 struct label_ref *lr;
2826 ops[i].cc_scratch = magic;
2829 if (g_labels[i] != NULL) {
2830 lr = &g_label_refs[i];
2831 for (; lr != NULL; lr = lr->next) {
2832 check_i(&ops[i], lr->i);
2833 scan_for_call_type(lr->i, opr, magic, pp_found, pp_i, multi);
2835 if (i > 0 && LAST_OP(i - 1))
2843 if (ops[i].cc_scratch == magic)
2845 ops[i].cc_scratch = magic;
2847 if (!(ops[i].flags & OPF_DATA))
2849 if (!is_opr_modified(opr, &ops[i]))
2851 if (ops[i].op != OP_MOV && ops[i].op != OP_LEA) {
2852 // most probably trashed by some processing
2857 opr = &ops[i].operand[1];
2858 if (opr->type != OPT_REG)
2862 po = (i >= 0) ? &ops[i] : ops;
2865 // reached the top - can only be an arg-reg
2866 if (opr->type != OPT_REG || g_func_pp == NULL)
2869 for (i = 0; i < g_func_pp->argc; i++) {
2870 if (g_func_pp->arg[i].reg == NULL)
2872 if (IS(opr->name, g_func_pp->arg[i].reg))
2875 if (i == g_func_pp->argc)
2877 pp = g_func_pp->arg[i].fptr;
2879 ferr(po, "icall: arg%d (%s) is not a fptr?\n",
2880 i + 1, g_func_pp->arg[i].reg);
2881 check_func_pp(po, pp, "icall reg-arg");
2884 pp = try_recover_pp(po, opr, NULL);
2886 if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
2887 if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
2888 || (*pp_found)->is_stdcall != pp->is_stdcall
2889 || (*pp_found)->is_fptr != pp->is_fptr
2890 || (*pp_found)->argc != pp->argc
2891 || (*pp_found)->argc_reg != pp->argc_reg
2892 || (*pp_found)->argc_stack != pp->argc_stack)
2894 ferr(po, "icall: parsed_proto mismatch\n");
2904 static void add_label_ref(struct label_ref *lr, int op_i)
2906 struct label_ref *lr_new;
2913 lr_new = calloc(1, sizeof(*lr_new));
2915 lr_new->next = lr->next;
2919 static struct parsed_data *try_resolve_jumptab(int i, int opcnt)
2921 struct parsed_op *po = &ops[i];
2922 struct parsed_data *pd;
2923 char label[NAMELEN], *p;
2926 p = strchr(po->operand[0].name, '[');
2930 len = p - po->operand[0].name;
2931 strncpy(label, po->operand[0].name, len);
2934 for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
2935 if (IS(g_func_pd[j].label, label)) {
2941 //ferr(po, "label '%s' not parsed?\n", label);
2944 if (pd->type != OPT_OFFSET)
2945 ferr(po, "label '%s' with non-offset data?\n", label);
2947 // find all labels, link
2948 for (j = 0; j < pd->count; j++) {
2949 for (l = 0; l < opcnt; l++) {
2950 if (g_labels[l] != NULL && IS(g_labels[l], pd->d[j].u.label)) {
2951 add_label_ref(&g_label_refs[l], i);
2961 static void clear_labels(int count)
2965 for (i = 0; i < count; i++) {
2966 if (g_labels[i] != NULL) {
2973 static void resolve_branches_parse_calls(int opcnt)
2975 const struct parsed_proto *pp_c;
2976 struct parsed_proto *pp;
2977 struct parsed_data *pd;
2978 struct parsed_op *po;
2979 const char *tmpname;
2982 for (i = 0; i < opcnt; i++)
2988 if (po->op == OP_CALL) {
2991 if (po->operand[0].type == OPT_LABEL) {
2992 tmpname = opr_name(po, 0);
2993 if (IS_START(tmpname, "loc_"))
2994 ferr(po, "call to loc_*\n");
2995 pp_c = proto_parse(g_fhdr, tmpname, g_header_mode);
2996 if (!g_header_mode && pp_c == NULL)
2997 ferr(po, "proto_parse failed for call '%s'\n", tmpname);
3000 pp = proto_clone(pp_c);
3001 my_assert_not(pp, NULL);
3004 else if (po->datap != NULL) {
3005 pp = calloc(1, sizeof(*pp));
3006 my_assert_not(pp, NULL);
3008 ret = parse_protostr(po->datap, pp);
3010 ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
3017 check_func_pp(po, pp, "fptr var call");
3018 if (pp->is_noreturn)
3019 po->flags |= OPF_TAIL;
3025 if (!(po->flags & OPF_JMP) || po->op == OP_RET)
3028 if (po->operand[0].type == OPT_REGMEM) {
3029 pd = try_resolve_jumptab(i, opcnt);
3037 for (l = 0; l < opcnt; l++) {
3038 if (g_labels[l] != NULL
3039 && IS(po->operand[0].name, g_labels[l]))
3041 if (l == i + 1 && po->op == OP_JMP) {
3042 // yet another alignment type..
3043 po->flags |= OPF_RMD|OPF_DONE;
3046 add_label_ref(&g_label_refs[l], i);
3052 if (po->bt_i != -1 || (po->flags & OPF_RMD))
3055 if (po->operand[0].type == OPT_LABEL)
3059 ferr(po, "unhandled branch\n");
3063 po->flags |= OPF_TAIL;
3064 if (i > 0 && ops[i - 1].op == OP_POP)
3065 po->flags |= OPF_ATAIL;
3070 static void scan_prologue_epilogue(int opcnt)
3072 int ecx_push = 0, esp_sub = 0;
3076 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
3077 && ops[1].op == OP_MOV
3078 && IS(opr_name(&ops[1], 0), "ebp")
3079 && IS(opr_name(&ops[1], 1), "esp"))
3082 ops[0].flags |= OPF_RMD | OPF_DONE;
3083 ops[1].flags |= OPF_RMD | OPF_DONE;
3086 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
3087 g_stack_fsz = opr_const(&ops[2], 1);
3088 ops[2].flags |= OPF_RMD | OPF_DONE;
3092 // another way msvc builds stack frame..
3094 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
3096 ops[i].flags |= OPF_RMD | OPF_DONE;
3100 // and another way..
3101 if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
3102 && ops[i].operand[1].type == OPT_CONST
3103 && ops[i + 1].op == OP_CALL
3104 && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
3106 g_stack_fsz += ops[i].operand[1].val;
3107 ops[i].flags |= OPF_RMD | OPF_DONE;
3109 ops[i].flags |= OPF_RMD | OPF_DONE;
3116 for (; i < opcnt; i++)
3117 if (ops[i].flags & OPF_TAIL)
3120 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
3121 if (ops[j].bt_i != -1 || ops[j].btj != NULL)
3127 if ((ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp"))
3128 || ops[j].op == OP_LEAVE)
3130 ops[j].flags |= OPF_RMD | OPF_DONE;
3132 else if (ops[i].op == OP_CALL && ops[i].pp != NULL
3133 && ops[i].pp->is_noreturn)
3135 // on noreturn, msvc sometimes cleans stack, sometimes not
3140 else if (!(g_ida_func_attr & IDAFA_NORETURN))
3141 ferr(&ops[j], "'pop ebp' expected\n");
3143 if (g_stack_fsz != 0) {
3144 if (ops[j].op == OP_LEAVE)
3146 else if (ops[j].op == OP_POP
3147 && ops[j - 1].op == OP_MOV
3148 && IS(opr_name(&ops[j - 1], 0), "esp")
3149 && IS(opr_name(&ops[j - 1], 1), "ebp"))
3151 ops[j - 1].flags |= OPF_RMD | OPF_DONE;
3154 else if (!(g_ida_func_attr & IDAFA_NORETURN))
3156 ferr(&ops[j], "esp restore expected\n");
3159 if (ecx_push && j >= 0 && ops[j].op == OP_POP
3160 && IS(opr_name(&ops[j], 0), "ecx"))
3162 ferr(&ops[j], "unexpected ecx pop\n");
3168 } while (i < opcnt);
3171 ferr(ops, "missing ebp epilogue\n");
3177 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
3178 ops[i].flags |= OPF_RMD | OPF_DONE;
3184 for (; i < opcnt; i++) {
3185 if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
3187 if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
3188 && ops[i].operand[1].type == OPT_CONST)
3190 g_stack_fsz = ops[i].operand[1].val;
3191 ops[i].flags |= OPF_RMD | OPF_DONE;
3197 if (ecx_push && !esp_sub) {
3198 // could actually be args for a call..
3199 for (; i < opcnt; i++)
3200 if (ops[i].op != OP_PUSH)
3203 if (ops[i].op == OP_CALL && ops[i].operand[0].type == OPT_LABEL) {
3204 const struct parsed_proto *pp;
3205 pp = proto_parse(g_fhdr, opr_name(&ops[i], 0), 1);
3206 j = pp ? pp->argc_stack : 0;
3207 while (i > 0 && j > 0) {
3209 if (ops[i].op == OP_PUSH) {
3210 ops[i].flags &= ~(OPF_RMD | OPF_DONE);
3215 ferr(&ops[i], "unhandled prologue\n");
3218 i = g_stack_fsz = ecx_push = 0;
3219 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
3220 if (!(ops[i].flags & OPF_RMD))
3230 if (ecx_push || esp_sub)
3236 for (; i < opcnt; i++)
3237 if (ops[i].flags & OPF_TAIL)
3240 if (i == opcnt && (ops[j].flags & OPF_JMP)) {
3241 if (ops[j].bt_i != -1 || ops[j].btj != NULL)
3248 for (l = 0; l < ecx_push; l++) {
3249 if (ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ecx"))
3251 else if (ops[j].op == OP_ADD
3252 && IS(opr_name(&ops[j], 0), "esp")
3253 && ops[j].operand[1].type == OPT_CONST)
3256 l += ops[j].operand[1].val / 4 - 1;
3259 ferr(&ops[j], "'pop ecx' expected\n");
3261 ops[j].flags |= OPF_RMD | OPF_DONE;
3265 ferr(&ops[j], "epilogue scan failed\n");
3271 if (ops[j].op != OP_ADD
3272 || !IS(opr_name(&ops[j], 0), "esp")
3273 || ops[j].operand[1].type != OPT_CONST
3274 || ops[j].operand[1].val != g_stack_fsz)
3275 ferr(&ops[j], "'add esp' expected\n");
3277 ops[j].flags |= OPF_RMD | OPF_DONE;
3278 ops[j].operand[1].val = 0; // hack for stack arg scanner
3283 } while (i < opcnt);
3286 ferr(ops, "missing esp epilogue\n");
3290 static const struct parsed_proto *resolve_icall(int i, int opcnt,
3291 int *pp_i, int *multi_src)
3293 const struct parsed_proto *pp = NULL;
3294 int search_advice = 0;
3299 switch (ops[i].operand[0].type) {
3303 pp = try_recover_pp(&ops[i], &ops[i].operand[0], &search_advice);
3308 scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
3316 // find an instruction that changed opr before i op
3317 // *op_i must be set to -1 by the caller
3318 // *entry is set to 1 if one source is determined to be the caller
3319 // returns 1 if found, *op_i is then set to origin
3320 static int resolve_origin(int i, const struct parsed_opr *opr,
3321 int magic, int *op_i, int *is_caller)
3323 struct label_ref *lr;
3326 if (ops[i].cc_scratch == magic)
3328 ops[i].cc_scratch = magic;
3331 if (g_labels[i] != NULL) {
3332 lr = &g_label_refs[i];
3333 for (; lr != NULL; lr = lr->next) {
3334 check_i(&ops[i], lr->i);
3335 ret |= resolve_origin(lr->i, opr, magic, op_i, is_caller);
3337 if (i > 0 && LAST_OP(i - 1))
3343 if (is_caller != NULL)
3348 if (ops[i].cc_scratch == magic)
3350 ops[i].cc_scratch = magic;
3352 if (!(ops[i].flags & OPF_DATA))
3354 if (!is_opr_modified(opr, &ops[i]))
3361 // XXX: could check if the other op does the same
3370 // find an instruction that previously referenced opr
3371 // if multiple results are found - fail
3372 // *op_i must be set to -1 by the caller
3373 // returns 1 if found, *op_i is then set to referencer insn
3374 static int resolve_last_ref(int i, const struct parsed_opr *opr,
3375 int magic, int *op_i)
3377 struct label_ref *lr;
3380 if (ops[i].cc_scratch == magic)
3382 ops[i].cc_scratch = magic;
3385 if (g_labels[i] != NULL) {
3386 lr = &g_label_refs[i];
3387 for (; lr != NULL; lr = lr->next) {
3388 check_i(&ops[i], lr->i);
3389 ret |= resolve_last_ref(lr->i, opr, magic, op_i);
3391 if (i > 0 && LAST_OP(i - 1))
3399 if (ops[i].cc_scratch == magic)
3401 ops[i].cc_scratch = magic;
3403 if (!is_opr_referenced(opr, &ops[i]))
3414 // find next instruction that reads opr
3415 // if multiple results are found - fail
3416 // *op_i must be set to -1 by the caller
3417 // returns 1 if found, *op_i is then set to referencer insn
3418 static int find_next_read(int i, int opcnt,
3419 const struct parsed_opr *opr, int magic, int *op_i)
3421 struct parsed_op *po;
3424 for (; i < opcnt; i++)
3426 if (ops[i].cc_scratch == magic)
3428 ops[i].cc_scratch = magic;
3431 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
3432 if (po->btj != NULL) {
3434 for (j = 0; j < po->btj->count; j++) {
3435 check_i(po, po->btj->d[j].bt_i);
3436 ret |= find_next_read(po->btj->d[j].bt_i, opcnt, opr,
3442 if (po->flags & OPF_RMD)
3444 check_i(po, po->bt_i);
3445 if (po->flags & OPF_CJMP) {
3446 ret = find_next_read(po->bt_i, opcnt, opr, magic, op_i);
3455 if (!is_opr_read(opr, po)) {
3456 if (is_opr_modified(opr, po))
3459 if (po->flags & OPF_TAIL)
3474 static int try_resolve_const(int i, const struct parsed_opr *opr,
3475 int magic, unsigned int *val)
3480 ret = resolve_origin(i, opr, magic, &s_i, NULL);
3483 if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST)
3486 *val = ops[i].operand[1].val;
3493 static struct parsed_proto *process_call_early(int i, int opcnt,
3496 struct parsed_op *po = &ops[i];
3497 struct parsed_proto *pp;
3503 if (pp == NULL || pp->is_vararg || pp->argc_reg != 0)
3507 // look for and make use of esp adjust
3509 if (!pp->is_stdcall && pp->argc_stack > 0)
3510 ret = scan_for_esp_adjust(i + 1, opcnt,
3511 pp->argc_stack * 4, &adj, &multipath, 0);
3513 if (pp->argc_stack > adj / 4)
3517 if (ops[ret].op == OP_POP) {
3518 for (j = 1; j < adj / 4; j++) {
3519 if (ops[ret + j].op != OP_POP
3520 || ops[ret + j].operand[0].reg != xCX)
3532 static struct parsed_proto *process_call(int i, int opcnt)
3534 struct parsed_op *po = &ops[i];
3535 const struct parsed_proto *pp_c;
3536 struct parsed_proto *pp;
3537 const char *tmpname;
3538 int call_i = -1, ref_i = -1;
3539 int adj = 0, multipath = 0;
3542 tmpname = opr_name(po, 0);
3547 pp_c = resolve_icall(i, opcnt, &call_i, &multipath);
3549 if (!pp_c->is_func && !pp_c->is_fptr)
3550 ferr(po, "call to non-func: %s\n", pp_c->name);
3551 pp = proto_clone(pp_c);
3552 my_assert_not(pp, NULL);
3554 // not resolved just to single func
3557 switch (po->operand[0].type) {
3559 // we resolved this call and no longer need the register
3560 po->regmask_src &= ~(1 << po->operand[0].reg);
3562 if (!multipath && i != call_i && ops[call_i].op == OP_MOV
3563 && ops[call_i].operand[1].type == OPT_LABEL)
3565 // no other source users?
3566 ret = resolve_last_ref(i, &po->operand[0], opcnt * 10,
3568 if (ret == 1 && call_i == ref_i) {
3569 // and nothing uses it after us?
3571 ret = find_next_read(i + 1, opcnt, &po->operand[0],
3572 opcnt * 11, &ref_i);
3574 // then also don't need the source mov
3575 ops[call_i].flags |= OPF_RMD;
3587 pp = calloc(1, sizeof(*pp));
3588 my_assert_not(pp, NULL);
3591 ret = scan_for_esp_adjust(i + 1, opcnt,
3592 -1, &adj, &multipath, 0);
3593 if (ret < 0 || adj < 0) {
3594 if (!g_allow_regfunc)
3595 ferr(po, "non-__cdecl indirect call unhandled yet\n");
3596 pp->is_unresolved = 1;
3600 if (adj > ARRAY_SIZE(pp->arg))
3601 ferr(po, "esp adjust too large: %d\n", adj);
3602 pp->ret_type.name = strdup("int");
3603 pp->argc = pp->argc_stack = adj;
3604 for (arg = 0; arg < pp->argc; arg++)
3605 pp->arg[arg].type.name = strdup("int");
3610 // look for and make use of esp adjust
3613 if (!pp->is_stdcall && pp->argc_stack > 0) {
3614 int adj_expect = pp->is_vararg ? -1 : pp->argc_stack * 4;
3615 ret = scan_for_esp_adjust(i + 1, opcnt,
3616 adj_expect, &adj, &multipath, 0);
3619 if (pp->is_vararg) {
3620 if (adj / 4 < pp->argc_stack) {
3621 fnote(po, "(this call)\n");
3622 ferr(&ops[ret], "esp adjust is too small: %x < %x\n",
3623 adj, pp->argc_stack * 4);
3625 // modify pp to make it have varargs as normal args
3627 pp->argc += adj / 4 - pp->argc_stack;
3628 for (; arg < pp->argc; arg++) {
3629 pp->arg[arg].type.name = strdup("int");
3632 if (pp->argc > ARRAY_SIZE(pp->arg))
3633 ferr(po, "too many args for '%s'\n", tmpname);
3635 if (pp->argc_stack > adj / 4) {
3636 fnote(po, "(this call)\n");
3637 ferr(&ops[ret], "stack tracking failed for '%s': %x %x\n",
3638 tmpname, pp->argc_stack * 4, adj);
3641 scan_for_esp_adjust(i + 1, opcnt,
3642 pp->argc_stack * 4, &adj, &multipath, 1);
3644 else if (pp->is_vararg)
3645 ferr(po, "missing esp_adjust for vararg func '%s'\n",
3651 static int collect_call_args_early(struct parsed_op *po, int i,
3652 struct parsed_proto *pp, int *regmask)
3657 for (arg = 0; arg < pp->argc; arg++)
3658 if (pp->arg[arg].reg == NULL)
3661 // first see if it can be easily done
3662 for (j = i; j > 0 && arg < pp->argc; )
3664 if (g_labels[j] != NULL)
3668 if (ops[j].op == OP_CALL)
3670 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP)
3672 else if (ops[j].op == OP_POP)
3674 else if (ops[j].flags & OPF_CJMP)
3676 else if (ops[j].op == OP_PUSH) {
3677 if (ops[j].flags & (OPF_FARG|OPF_FARGNR))
3679 ret = scan_for_mod(&ops[j], j + 1, i, 1);
3683 if (pp->arg[arg].type.is_va_list)
3687 for (arg++; arg < pp->argc; arg++)
3688 if (pp->arg[arg].reg == NULL)
3697 for (arg = 0; arg < pp->argc; arg++)
3698 if (pp->arg[arg].reg == NULL)
3701 for (j = i; j > 0 && arg < pp->argc; )
3705 if (ops[j].op == OP_PUSH)
3707 ops[j].p_argnext = -1;
3708 ferr_assert(&ops[j], pp->arg[arg].datap == NULL);
3709 pp->arg[arg].datap = &ops[j];
3711 if (ops[j].operand[0].type == OPT_REG)
3712 *regmask |= 1 << ops[j].operand[0].reg;
3714 ops[j].flags |= OPF_RMD | OPF_DONE | OPF_FARGNR | OPF_FARG;
3715 ops[j].flags &= ~OPF_RSAVE;
3718 for (arg++; arg < pp->argc; arg++)
3719 if (pp->arg[arg].reg == NULL)
3727 static int collect_call_args_r(struct parsed_op *po, int i,
3728 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
3729 int *arg_grp, int arg, int magic, int need_op_saving, int may_reuse)
3731 struct parsed_proto *pp_tmp;
3732 struct parsed_op *po_tmp;
3733 struct label_ref *lr;
3734 int need_to_save_current;
3735 int arg_grp_current = 0;
3736 int save_args_seen = 0;
3744 ferr(po, "dead label encountered\n");
3748 for (; arg < pp->argc; arg++)
3749 if (pp->arg[arg].reg == NULL)
3751 magic = (magic & 0xffffff) | (arg << 24);
3753 for (j = i; j >= 0 && (arg < pp->argc || pp->is_unresolved); )
3755 if (((ops[j].cc_scratch ^ magic) & 0xffffff) == 0) {
3756 if (ops[j].cc_scratch != magic) {
3757 ferr(&ops[j], "arg collect hit same path with diff args for %s\n",
3761 // ok: have already been here
3764 ops[j].cc_scratch = magic;
3766 if (g_labels[j] != NULL && g_label_refs[j].i != -1) {
3767 lr = &g_label_refs[j];
3768 if (lr->next != NULL)
3770 for (; lr->next; lr = lr->next) {
3771 check_i(&ops[j], lr->i);
3772 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
3774 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
3775 arg_grp, arg, magic, need_op_saving, may_reuse);
3780 check_i(&ops[j], lr->i);
3781 if ((ops[lr->i].flags & (OPF_JMP|OPF_CJMP)) != OPF_JMP)
3783 if (j > 0 && LAST_OP(j - 1)) {
3784 // follow last branch in reverse
3789 ret = collect_call_args_r(po, lr->i, pp, regmask, save_arg_vars,
3790 arg_grp, arg, magic, need_op_saving, may_reuse);
3796 if (ops[j].op == OP_CALL)
3798 if (pp->is_unresolved)
3803 ferr(po, "arg collect hit unparsed call '%s'\n",
3804 ops[j].operand[0].name);
3805 if (may_reuse && pp_tmp->argc_stack > 0)
3806 ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
3807 arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
3809 // esp adjust of 0 means we collected it before
3810 else if (ops[j].op == OP_ADD && ops[j].operand[0].reg == xSP
3811 && (ops[j].operand[1].type != OPT_CONST
3812 || ops[j].operand[1].val != 0))
3814 if (pp->is_unresolved)
3817 fnote(po, "(this call)\n");
3818 ferr(&ops[j], "arg collect %d/%d hit esp adjust of %d\n",
3819 arg, pp->argc, ops[j].operand[1].val);
3821 else if (ops[j].op == OP_POP && !(ops[j].flags & OPF_DONE))
3823 if (pp->is_unresolved)
3826 fnote(po, "(this call)\n");
3827 ferr(&ops[j], "arg collect %d/%d hit pop\n", arg, pp->argc);
3829 else if (ops[j].flags & OPF_CJMP)
3831 if (pp->is_unresolved)
3836 else if (ops[j].op == OP_PUSH
3837 && !(ops[j].flags & (OPF_FARGNR|OPF_DONE)))
3839 if (pp->is_unresolved && (ops[j].flags & OPF_RMD))
3842 ops[j].p_argnext = -1;
3843 po_tmp = pp->arg[arg].datap;
3845 ops[j].p_argnext = po_tmp - ops;
3846 pp->arg[arg].datap = &ops[j];
3848 need_to_save_current = 0;
3851 if (ops[j].operand[0].type == OPT_REG)
3852 reg = ops[j].operand[0].reg;
3854 if (!need_op_saving) {
3855 ret = scan_for_mod(&ops[j], j + 1, i, 1);
3856 need_to_save_current = (ret >= 0);
3858 if (need_op_saving || need_to_save_current) {
3859 // mark this push as one that needs operand saving
3860 ops[j].flags &= ~OPF_RMD;
3861 if (ops[j].p_argnum == 0) {
3862 ops[j].p_argnum = arg + 1;
3863 save_args |= 1 << arg;
3865 else if (ops[j].p_argnum < arg + 1) {
3866 // XXX: might kill valid var..
3867 //*save_arg_vars &= ~(1 << (ops[j].p_argnum - 1));
3868 ops[j].p_argnum = arg + 1;
3869 save_args |= 1 << arg;
3872 if (save_args_seen & (1 << (ops[j].p_argnum - 1))) {
3875 if (arg_grp_current >= MAX_ARG_GRP)
3876 ferr(&ops[j], "out of arg groups (arg%d), f %s\n",
3877 ops[j].p_argnum, pp->name);
3880 else if (ops[j].p_argnum == 0)
3881 ops[j].flags |= OPF_RMD;
3883 // some PUSHes are reused by different calls on other branches,
3884 // but that can't happen if we didn't branch, so they
3885 // can be removed from future searches (handles nested calls)
3887 ops[j].flags |= OPF_FARGNR;
3889 ops[j].flags |= OPF_FARG;
3890 ops[j].flags &= ~OPF_RSAVE;
3892 // check for __VALIST
3893 if (!pp->is_unresolved && g_func_pp != NULL
3894 && pp->arg[arg].type.is_va_list)
3897 ret = resolve_origin(j, &ops[j].operand[0],
3898 magic + 1, &k, NULL);
3899 if (ret == 1 && k >= 0)
3901 if (ops[k].op == OP_LEA) {
3902 snprintf(buf, sizeof(buf), "arg_%X",
3903 g_func_pp->argc_stack * 4);
3904 if (!g_func_pp->is_vararg
3905 || strstr(ops[k].operand[1].name, buf))
3907 ops[k].flags |= OPF_RMD | OPF_DONE;
3908 ops[j].flags |= OPF_RMD | OPF_VAPUSH;
3909 save_args &= ~(1 << arg);
3913 ferr(&ops[j], "lea va_list used, but no vararg?\n");
3915 // check for va_list from g_func_pp arg too
3916 else if (ops[k].op == OP_MOV
3917 && is_stack_access(&ops[k], &ops[k].operand[1]))
3919 ret = stack_frame_access(&ops[k], &ops[k].operand[1],
3920 buf, sizeof(buf), ops[k].operand[1].name, "", 1, 0);
3922 ops[k].flags |= OPF_RMD | OPF_DONE;
3923 ops[j].flags |= OPF_RMD;
3924 ops[j].p_argpass = ret + 1;
3925 save_args &= ~(1 << arg);
3932 *save_arg_vars |= save_args;
3934 // tracking reg usage
3936 *regmask |= 1 << reg;
3939 if (!pp->is_unresolved) {
3941 for (; arg < pp->argc; arg++)
3942 if (pp->arg[arg].reg == NULL)
3945 magic = (magic & 0xffffff) | (arg << 24);
3948 if (ops[j].p_arggrp > arg_grp_current) {
3950 arg_grp_current = ops[j].p_arggrp;
3952 if (ops[j].p_argnum > 0)
3953 save_args_seen |= 1 << (ops[j].p_argnum - 1);
3956 if (arg < pp->argc) {
3957 ferr(po, "arg collect failed for '%s': %d/%d\n",
3958 pp->name, arg, pp->argc);
3962 if (arg_grp_current > *arg_grp)
3963 *arg_grp = arg_grp_current;
3968 static int collect_call_args(struct parsed_op *po, int i,
3969 struct parsed_proto *pp, int *regmask, int *save_arg_vars,
3972 // arg group is for cases when pushes for
3973 // multiple funcs are going on
3974 struct parsed_op *po_tmp;
3975 int save_arg_vars_current = 0;
3980 ret = collect_call_args_r(po, i, pp, regmask,
3981 &save_arg_vars_current, &arg_grp, 0, magic, 0, 0);
3986 // propagate arg_grp
3987 for (a = 0; a < pp->argc; a++) {
3988 if (pp->arg[a].reg != NULL)
3991 po_tmp = pp->arg[a].datap;
3992 while (po_tmp != NULL) {
3993 po_tmp->p_arggrp = arg_grp;
3994 if (po_tmp->p_argnext > 0)
3995 po_tmp = &ops[po_tmp->p_argnext];
4001 save_arg_vars[arg_grp] |= save_arg_vars_current;
4003 if (pp->is_unresolved) {
4005 pp->argc_stack += ret;
4006 for (a = 0; a < pp->argc; a++)
4007 if (pp->arg[a].type.name == NULL)
4008 pp->arg[a].type.name = strdup("int");
4014 static void pp_insert_reg_arg(struct parsed_proto *pp, const char *reg)
4018 for (i = 0; i < pp->argc; i++)
4019 if (pp->arg[i].reg == NULL)
4023 memmove(&pp->arg[i + 1], &pp->arg[i],
4024 sizeof(pp->arg[0]) * pp->argc_stack);
4025 memset(&pp->arg[i], 0, sizeof(pp->arg[i]));
4026 pp->arg[i].reg = strdup(reg);
4027 pp->arg[i].type.name = strdup("int");
4032 static void output_std_flags(FILE *fout, struct parsed_op *po,
4033 int *pfomask, const char *dst_opr_text)
4035 if (*pfomask & (1 << PFO_Z)) {
4036 fprintf(fout, "\n cond_z = (%s%s == 0);",
4037 lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
4038 *pfomask &= ~(1 << PFO_Z);
4040 if (*pfomask & (1 << PFO_S)) {
4041 fprintf(fout, "\n cond_s = (%s%s < 0);",
4042 lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
4043 *pfomask &= ~(1 << PFO_S);
4048 OPP_FORCE_NORETURN = (1 << 0),
4049 OPP_SIMPLE_ARGS = (1 << 1),
4050 OPP_ALIGN = (1 << 2),
4053 static void output_pp_attrs(FILE *fout, const struct parsed_proto *pp,
4056 const char *cconv = "";
4058 if (pp->is_fastcall)
4059 cconv = "__fastcall ";
4060 else if (pp->is_stdcall && pp->argc_reg == 0)
4061 cconv = "__stdcall ";
4063 fprintf(fout, (flags & OPP_ALIGN) ? "%-16s" : "%s", cconv);
4065 if (pp->is_noreturn || (flags & OPP_FORCE_NORETURN))
4066 fprintf(fout, "noreturn ");
4069 static void output_pp(FILE *fout, const struct parsed_proto *pp,
4074 fprintf(fout, (flags & OPP_ALIGN) ? "%-5s" : "%s ",
4078 output_pp_attrs(fout, pp, flags);
4081 fprintf(fout, "%s", pp->name);
4086 for (i = 0; i < pp->argc; i++) {
4088 fprintf(fout, ", ");
4089 if (pp->arg[i].fptr != NULL && !(flags & OPP_SIMPLE_ARGS)) {
4091 output_pp(fout, pp->arg[i].fptr, 0);
4093 else if (pp->arg[i].type.is_retreg) {
4094 fprintf(fout, "u32 *r_%s", pp->arg[i].reg);
4097 fprintf(fout, "%s", pp->arg[i].type.name);
4099 fprintf(fout, " a%d", i + 1);
4102 if (pp->is_vararg) {
4104 fprintf(fout, ", ");
4105 fprintf(fout, "...");
4110 static int get_pp_arg_regmask(const struct parsed_proto *pp)
4115 for (i = 0; i < pp->argc; i++) {
4116 if (pp->arg[i].reg != NULL) {
4117 reg = char_array_i(regs_r32,
4118 ARRAY_SIZE(regs_r32), pp->arg[i].reg);
4120 ferr(ops, "arg '%s' of func '%s' is not a reg?\n",
4121 pp->arg[i].reg, pp->name);
4122 regmask |= 1 << reg;
4129 static char *saved_arg_name(char *buf, size_t buf_size, int grp, int num)
4135 snprintf(buf1, sizeof(buf1), "%d", grp);
4136 snprintf(buf, buf_size, "s%s_a%d", buf1, num);
4141 static void gen_x_cleanup(int opcnt);
4143 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
4145 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
4146 struct parsed_opr *last_arith_dst = NULL;
4147 char buf1[256], buf2[256], buf3[256], cast[64];
4148 struct parsed_proto *pp, *pp_tmp;
4149 struct parsed_data *pd;
4151 int save_arg_vars[MAX_ARG_GRP] = { 0, };
4153 int need_tmp_var = 0;
4156 int label_pending = 0;
4157 int regmask_save = 0; // regs saved/restored in this func
4158 int regmask_arg = 0; // regs carrying function args (fastcall, etc)
4159 int regmask_now; // temp
4160 int regmask_init = 0; // regs that need zero initialization
4161 int regmask_pp = 0; // regs used in complex push-pop graph
4162 int regmask = 0; // used regs
4172 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
4173 g_stack_frame_used = 0;
4175 g_func_pp = proto_parse(fhdr, funcn, 0);
4176 if (g_func_pp == NULL)
4177 ferr(ops, "proto_parse failed for '%s'\n", funcn);
4179 regmask_arg = get_pp_arg_regmask(g_func_pp);
4182 // - resolve all branches
4183 // - parse calls with labels
4184 resolve_branches_parse_calls(opcnt);
4187 // - handle ebp/esp frame, remove ops related to it
4188 scan_prologue_epilogue(opcnt);
4191 // - remove dead labels
4192 for (i = 0; i < opcnt; i++)
4194 if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
4201 // - process trivial calls
4202 for (i = 0; i < opcnt; i++)
4205 if (po->flags & (OPF_RMD|OPF_DONE))
4208 if (po->op == OP_CALL)
4210 pp = process_call_early(i, opcnt, &j);
4212 if (!(po->flags & OPF_ATAIL))
4213 // since we know the args, try to collect them
4214 if (collect_call_args_early(po, i, pp, ®mask) != 0)
4220 // commit esp adjust
4221 if (ops[j].op != OP_POP)
4222 patch_esp_adjust(&ops[j], pp->argc_stack * 4);
4224 for (l = 0; l < pp->argc_stack; l++)
4225 ops[j + l].flags |= OPF_DONE | OPF_RMD;
4229 if (strstr(pp->ret_type.name, "int64"))
4232 po->flags |= OPF_DONE;
4239 // - handle push <const>/pop pairs
4240 for (i = 0; i < opcnt; i++)
4243 if (po->flags & (OPF_RMD|OPF_DONE))
4246 if (po->op == OP_CALL && !(po->flags & OPF_DONE))
4248 pp = process_call(i, opcnt);
4250 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
4251 // since we know the args, collect them
4252 collect_call_args(po, i, pp, ®mask, save_arg_vars,
4256 if (strstr(pp->ret_type.name, "int64"))
4259 else if (po->op == OP_PUSH && !(po->flags & OPF_FARG)
4260 && !(po->flags & OPF_RSAVE) && po->operand[0].type == OPT_CONST)
4261 scan_for_pop_const(i, opcnt, ®mask_pp);
4265 // - find POPs for PUSHes, rm both
4266 // - scan for STD/CLD, propagate DF
4267 // - scan for all used registers
4268 // - find flag set ops for their users
4269 // - do unreselved calls
4270 // - declare indirect functions
4271 for (i = 0; i < opcnt; i++)
4274 if (po->flags & (OPF_RMD|OPF_DONE))
4277 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE)) {
4278 reg = po->operand[0].reg;
4279 if (!(regmask & (1 << reg)))
4280 // not a reg save after all, rerun scan_for_pop
4281 po->flags &= ~OPF_RSAVE;
4283 regmask_save |= 1 << reg;
4286 if (po->op == OP_PUSH && !(po->flags & OPF_FARG)
4287 && !(po->flags & OPF_RSAVE) && !g_func_pp->is_userstack)
4289 if (po->operand[0].type == OPT_REG)
4291 reg = po->operand[0].reg;
4293 ferr(po, "reg not set for push?\n");
4296 ret = scan_for_pop(i + 1, opcnt,
4297 po->operand[0].name, i + opcnt * 3, 0, &depth, 0);
4300 ferr(po, "too much depth: %d\n", depth);
4302 po->flags |= OPF_RMD;
4303 scan_for_pop(i + 1, opcnt, po->operand[0].name,
4304 i + opcnt * 4, 0, &depth, 1);
4307 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
4310 if (regmask & (1 << reg)) {
4311 if (regmask_save & (1 << reg))
4312 ferr(po, "%s already saved?\n", po->operand[0].name);
4316 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, arg);
4322 if (po->op == OP_STD) {
4323 po->flags |= OPF_DF | OPF_RMD | OPF_DONE;
4324 scan_propagate_df(i + 1, opcnt);
4327 regmask_now = po->regmask_src | po->regmask_dst;
4328 if (regmask_now & (1 << xBP)) {
4329 if (g_bp_frame && !(po->flags & OPF_EBP_S)) {
4330 if (po->regmask_dst & (1 << xBP))
4331 // compiler decided to drop bp frame and use ebp as scratch
4332 scan_fwd_set_flags(i + 1, opcnt, i + opcnt * 5, OPF_EBP_S);
4334 regmask_now &= ~(1 << xBP);
4338 regmask |= regmask_now;
4340 if (po->flags & OPF_CC)
4342 int setters[16], cnt = 0, branched = 0;
4344 ret = scan_for_flag_set(i, i + opcnt * 6,
4345 &branched, setters, &cnt);
4346 if (ret < 0 || cnt <= 0)
4347 ferr(po, "unable to trace flag setter(s)\n");
4348 if (cnt > ARRAY_SIZE(setters))
4349 ferr(po, "too many flag setters\n");
4351 for (j = 0; j < cnt; j++)
4353 tmp_op = &ops[setters[j]]; // flag setter
4356 // to get nicer code, we try to delay test and cmp;
4357 // if we can't because of operand modification, or if we
4358 // have arith op, or branch, make it calculate flags explicitly
4359 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP)
4361 if (branched || scan_for_mod(tmp_op, setters[j] + 1, i, 0) >= 0)
4362 pfomask = 1 << po->pfo;
4364 else if (tmp_op->op == OP_CMPS || tmp_op->op == OP_SCAS) {
4365 pfomask = 1 << po->pfo;
4368 // see if we'll be able to handle based on op result
4369 if ((tmp_op->op != OP_AND && tmp_op->op != OP_OR
4370 && po->pfo != PFO_Z && po->pfo != PFO_S
4371 && po->pfo != PFO_P)
4373 || scan_for_mod_opr0(tmp_op, setters[j] + 1, i) >= 0)
4375 pfomask = 1 << po->pfo;
4378 if (tmp_op->op == OP_ADD && po->pfo == PFO_C) {
4379 propagate_lmod(tmp_op, &tmp_op->operand[0],
4380 &tmp_op->operand[1]);
4381 if (tmp_op->operand[0].lmod == OPLM_DWORD)
4386 tmp_op->pfomask |= pfomask;
4387 cond_vars |= pfomask;
4389 // note: may overwrite, currently not a problem
4393 if (po->op == OP_RCL || po->op == OP_RCR
4394 || po->op == OP_ADC || po->op == OP_SBB)
4395 cond_vars |= 1 << PFO_C;
4398 if (po->op == OP_CMPS || po->op == OP_SCAS) {
4399 cond_vars |= 1 << PFO_Z;
4401 else if (po->op == OP_MUL
4402 || (po->op == OP_IMUL && po->operand_cnt == 1))
4404 if (po->operand[0].lmod == OPLM_DWORD)
4407 else if (po->op == OP_CALL) {
4408 // note: resolved non-reg calls are OPF_DONE already
4411 ferr(po, "NULL pp\n");
4413 if (pp->is_unresolved) {
4414 int regmask_stack = 0;
4415 collect_call_args(po, i, pp, ®mask, save_arg_vars,
4418 // this is pretty rough guess:
4419 // see ecx and edx were pushed (and not their saved versions)
4420 for (arg = 0; arg < pp->argc; arg++) {
4421 if (pp->arg[arg].reg != NULL)
4424 tmp_op = pp->arg[arg].datap;
4426 ferr(po, "parsed_op missing for arg%d\n", arg);
4427 if (tmp_op->p_argnum == 0 && tmp_op->operand[0].type == OPT_REG)
4428 regmask_stack |= 1 << tmp_op->operand[0].reg;
4431 if (!((regmask_stack & (1 << xCX))
4432 && (regmask_stack & (1 << xDX))))
4434 if (pp->argc_stack != 0
4435 || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX))))
4437 pp_insert_reg_arg(pp, "ecx");
4438 pp->is_fastcall = 1;
4439 regmask_init |= 1 << xCX;
4440 regmask |= 1 << xCX;
4442 if (pp->argc_stack != 0
4443 || ((regmask | regmask_arg) & (1 << xDX)))
4445 pp_insert_reg_arg(pp, "edx");
4446 regmask_init |= 1 << xDX;
4447 regmask |= 1 << xDX;
4451 // note: __cdecl doesn't fall into is_unresolved category
4452 if (pp->argc_stack > 0)
4456 for (arg = 0; arg < pp->argc; arg++) {
4457 if (pp->arg[arg].reg != NULL) {
4458 reg = char_array_i(regs_r32,
4459 ARRAY_SIZE(regs_r32), pp->arg[arg].reg);
4461 ferr(ops, "arg '%s' is not a reg?\n", pp->arg[arg].reg);
4462 if (!(regmask & (1 << reg))) {
4463 regmask_init |= 1 << reg;
4464 regmask |= 1 << reg;
4469 else if (po->op == OP_MOV && po->operand[0].pp != NULL
4470 && po->operand[1].pp != NULL)
4472 // <var> = offset <something>
4473 if ((po->operand[1].pp->is_func || po->operand[1].pp->is_fptr)
4474 && !IS_START(po->operand[1].name, "off_"))
4476 if (!po->operand[0].pp->is_fptr)
4477 ferr(po, "%s not declared as fptr when it should be\n",
4478 po->operand[0].name);
4479 if (pp_cmp_func(po->operand[0].pp, po->operand[1].pp)) {
4480 pp_print(buf1, sizeof(buf1), po->operand[0].pp);
4481 pp_print(buf2, sizeof(buf2), po->operand[1].pp);
4482 fnote(po, "var: %s\n", buf1);
4483 fnote(po, "func: %s\n", buf2);
4484 ferr(po, "^ mismatch\n");
4488 else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void"))
4489 regmask |= 1 << xAX;
4490 else if (po->op == OP_DIV || po->op == OP_IDIV) {
4491 // 32bit division is common, look for it
4492 if (po->op == OP_DIV)
4493 ret = scan_for_reg_clear(i, xDX);
4495 ret = scan_for_cdq_edx(i);
4497 po->flags |= OPF_32BIT;
4501 else if (po->op == OP_CLD)
4502 po->flags |= OPF_RMD | OPF_DONE;
4504 if (po->op == OP_RCL || po->op == OP_RCR || po->op == OP_XCHG) {
4510 // - confirm regmask_save, it might have been reduced
4511 if (regmask_save != 0)
4514 for (i = 0; i < opcnt; i++) {
4516 if (po->flags & OPF_RMD)
4519 if (po->op == OP_PUSH && (po->flags & OPF_RSAVE))
4520 regmask_save |= 1 << po->operand[0].reg;
4524 // output starts here
4526 // define userstack size
4527 if (g_func_pp->is_userstack) {
4528 fprintf(fout, "#ifndef US_SZ_%s\n", g_func_pp->name);
4529 fprintf(fout, "#define US_SZ_%s USERSTACK_SIZE\n", g_func_pp->name);
4530 fprintf(fout, "#endif\n");
4533 // the function itself
4534 ferr_assert(ops, !g_func_pp->is_fptr);
4535 output_pp(fout, g_func_pp,
4536 (g_ida_func_attr & IDAFA_NORETURN) ? OPP_FORCE_NORETURN : 0);
4537 fprintf(fout, "\n{\n");
4539 // declare indirect functions
4540 for (i = 0; i < opcnt; i++) {
4542 if (po->flags & OPF_RMD)
4545 if (po->op == OP_CALL) {
4548 ferr(po, "NULL pp\n");
4550 if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) {
4551 if (pp->name[0] != 0) {
4552 memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
4553 memcpy(pp->name, "i_", 2);
4555 // might be declared already
4557 for (j = 0; j < i; j++) {
4558 if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) {
4559 if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) {
4569 snprintf(pp->name, sizeof(pp->name), "icall%d", i);
4572 output_pp(fout, pp, OPP_SIMPLE_ARGS);
4573 fprintf(fout, ";\n");
4578 // output LUTs/jumptables
4579 for (i = 0; i < g_func_pd_cnt; i++) {
4581 fprintf(fout, " static const ");
4582 if (pd->type == OPT_OFFSET) {
4583 fprintf(fout, "void *jt_%s[] =\n { ", pd->label);
4585 for (j = 0; j < pd->count; j++) {
4587 fprintf(fout, ", ");
4588 fprintf(fout, "&&%s", pd->d[j].u.label);
4592 fprintf(fout, "%s %s[] =\n { ",
4593 lmod_type_u(ops, pd->lmod), pd->label);
4595 for (j = 0; j < pd->count; j++) {
4597 fprintf(fout, ", ");
4598 fprintf(fout, "%u", pd->d[j].u.val);
4601 fprintf(fout, " };\n");
4605 // declare stack frame, va_arg
4607 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
4608 (g_stack_fsz + 3) / 4, (g_stack_fsz + 1) / 2, g_stack_fsz);
4612 if (g_func_pp->is_userstack) {
4613 fprintf(fout, " u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name);
4614 fprintf(fout, " u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n");
4618 if (g_func_pp->is_vararg) {
4619 fprintf(fout, " va_list ap;\n");
4623 // declare arg-registers
4624 for (i = 0; i < g_func_pp->argc; i++) {
4625 if (g_func_pp->arg[i].reg != NULL) {
4626 reg = char_array_i(regs_r32,
4627 ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg);
4628 if (regmask & (1 << reg)) {
4629 if (g_func_pp->arg[i].type.is_retreg)
4630 fprintf(fout, " u32 %s = *r_%s;\n",
4631 g_func_pp->arg[i].reg, g_func_pp->arg[i].reg);
4633 fprintf(fout, " u32 %s = (u32)a%d;\n",
4634 g_func_pp->arg[i].reg, i + 1);
4637 if (g_func_pp->arg[i].type.is_retreg)
4638 ferr(ops, "retreg '%s' is unused?\n",
4639 g_func_pp->arg[i].reg);
4640 fprintf(fout, " // %s = a%d; // unused\n",
4641 g_func_pp->arg[i].reg, i + 1);
4647 // declare normal registers
4648 regmask_now = regmask & ~regmask_arg;
4649 regmask_now &= ~(1 << xSP);
4650 if (regmask_now & 0x00ff) {
4651 for (reg = 0; reg < 8; reg++) {
4652 if (regmask_now & (1 << reg)) {
4653 fprintf(fout, " u32 %s", regs_r32[reg]);
4654 if (regmask_init & (1 << reg))
4655 fprintf(fout, " = 0");
4656 fprintf(fout, ";\n");
4661 if (regmask_now & 0xff00) {
4662 for (reg = 8; reg < 16; reg++) {
4663 if (regmask_now & (1 << reg)) {
4664 fprintf(fout, " mmxr %s", regs_r32[reg]);
4665 if (regmask_init & (1 << reg))
4666 fprintf(fout, " = { 0, }");
4667 fprintf(fout, ";\n");
4674 for (reg = 0; reg < 8; reg++) {
4675 if (regmask_save & (1 << reg)) {
4676 fprintf(fout, " u32 s_%s;\n", regs_r32[reg]);
4682 for (i = 0; i < ARRAY_SIZE(save_arg_vars); i++) {
4683 if (save_arg_vars[i] == 0)
4685 for (reg = 0; reg < 32; reg++) {
4686 if (save_arg_vars[i] & (1 << reg)) {
4687 fprintf(fout, " u32 %s;\n",
4688 saved_arg_name(buf1, sizeof(buf1), i, reg + 1));
4694 // declare push-pop temporaries
4696 for (reg = 0; reg < 8; reg++) {
4697 if (regmask_pp & (1 << reg)) {
4698 fprintf(fout, " u32 pp_%s;\n", regs_r32[reg]);
4705 for (i = 0; i < 8; i++) {
4706 if (cond_vars & (1 << i)) {
4707 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
4714 fprintf(fout, " u32 tmp;\n");
4719 fprintf(fout, " u64 tmp64;\n");
4724 fprintf(fout, "\n");
4726 if (g_func_pp->is_vararg) {
4727 if (g_func_pp->argc_stack == 0)
4728 ferr(ops, "vararg func without stack args?\n");
4729 fprintf(fout, " va_start(ap, a%d);\n", g_func_pp->argc);
4733 for (i = 0; i < opcnt; i++)
4735 if (g_labels[i] != NULL) {
4736 fprintf(fout, "\n%s:\n", g_labels[i]);
4739 delayed_flag_op = NULL;
4740 last_arith_dst = NULL;
4744 if (po->flags & OPF_RMD)
4749 #define assert_operand_cnt(n_) \
4750 if (po->operand_cnt != n_) \
4751 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
4753 // conditional/flag using op?
4754 if (po->flags & OPF_CC)
4760 // we go through all this trouble to avoid using parsed_flag_op,
4761 // which makes generated code much nicer
4762 if (delayed_flag_op != NULL)
4764 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op,
4765 po->pfo, po->pfo_inv);
4768 else if (last_arith_dst != NULL
4769 && (po->pfo == PFO_Z || po->pfo == PFO_S || po->pfo == PFO_P
4770 || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
4773 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
4774 out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv,
4775 last_arith_dst->lmod, buf3);
4778 else if (tmp_op != NULL) {
4779 // use preprocessed flag calc results
4780 if (!(tmp_op->pfomask & (1 << po->pfo)))
4781 ferr(po, "not prepared for pfo %d\n", po->pfo);
4783 // note: pfo_inv was not yet applied
4784 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
4785 po->pfo_inv ? "!" : "", parsed_flag_op_names[po->pfo]);
4788 ferr(po, "all methods of finding comparison failed\n");
4791 if (po->flags & OPF_JMP) {
4792 fprintf(fout, " if %s", buf1);
4794 else if (po->op == OP_RCL || po->op == OP_RCR
4795 || po->op == OP_ADC || po->op == OP_SBB)
4798 fprintf(fout, " cond_%s = %s;\n",
4799 parsed_flag_op_names[po->pfo], buf1);
4801 else if (po->flags & OPF_DATA) { // SETcc
4802 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
4803 fprintf(fout, " %s = %s;", buf2, buf1);
4806 ferr(po, "unhandled conditional op\n");
4810 pfomask = po->pfomask;
4812 if (po->flags & (OPF_REPZ|OPF_REPNZ)) {
4813 struct parsed_opr opr = {0,};
4816 opr.lmod = OPLM_DWORD;
4817 ret = try_resolve_const(i, &opr, opcnt * 7 + i, &uval);
4819 if (ret != 1 || uval == 0) {
4820 // we need initial flags for ecx=0 case..
4821 if (i > 0 && ops[i - 1].op == OP_XOR
4822 && IS(ops[i - 1].operand[0].name,
4823 ops[i - 1].operand[1].name))
4825 fprintf(fout, " cond_z = ");
4826 if (pfomask & (1 << PFO_C))
4827 fprintf(fout, "cond_c = ");
4828 fprintf(fout, "0;\n");
4830 else if (last_arith_dst != NULL) {
4831 out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
4832 out_test_for_cc(buf1, sizeof(buf1), po, PFO_Z, 0,
4833 last_arith_dst->lmod, buf3);
4834 fprintf(fout, " cond_z = %s;\n", buf1);
4837 ferr(po, "missing initial ZF\n");
4844 assert_operand_cnt(2);
4845 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4846 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4847 default_cast_to(buf3, sizeof(buf3), &po->operand[0]);
4848 fprintf(fout, " %s = %s;", buf1,
4849 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4854 assert_operand_cnt(2);
4855 po->operand[1].lmod = OPLM_DWORD; // always
4856 fprintf(fout, " %s = %s;",
4857 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4858 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4863 assert_operand_cnt(2);
4864 fprintf(fout, " %s = %s;",
4865 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4866 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4870 assert_operand_cnt(2);
4871 switch (po->operand[1].lmod) {
4873 strcpy(buf3, "(s8)");
4876 strcpy(buf3, "(s16)");
4879 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
4881 fprintf(fout, " %s = %s;",
4882 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4883 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4888 assert_operand_cnt(2);
4889 propagate_lmod(po, &po->operand[0], &po->operand[1]);
4890 fprintf(fout, " tmp = %s;",
4891 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0));
4892 fprintf(fout, " %s = %s;",
4893 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4894 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
4895 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
4896 fprintf(fout, " %s = %stmp;",
4897 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
4898 default_cast_to(buf3, sizeof(buf3), &po->operand[1]));
4899 snprintf(g_comment, sizeof(g_comment), "xchg");
4903 assert_operand_cnt(1);
4904 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4905 fprintf(fout, " %s = ~%s;", buf1, buf1);
4909 assert_operand_cnt(2);
4910 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
4911 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
4912 fprintf(fout, " %s = *(u8 *)(%s + %s);", buf1, buf2, buf1);
4913 strcpy(g_comment, "xlat");
4917 assert_operand_cnt(2);
4918 fprintf(fout, " %s = (s32)%s >> 31;",
4919 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
4920 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
4921 strcpy(g_comment, "cdq");
4925 assert_operand_cnt(3);
4926 if (po->flags & OPF_REP) {
4931 fprintf(fout, " eax = %sesi; esi %c= %d;",
4932 lmod_cast_u_ptr(po, po->operand[0].lmod),
4933 (po->flags & OPF_DF) ? '-' : '+',
4934 lmod_bytes(po, po->operand[0].lmod));
4935 strcpy(g_comment, "lods");
4940 assert_operand_cnt(3);
4941 if (po->flags & OPF_REP) {
4942 fprintf(fout, " for (; ecx != 0; ecx--, edi %c= %d)\n",
4943 (po->flags & OPF_DF) ? '-' : '+',
4944 lmod_bytes(po, po->operand[0].lmod));
4945 fprintf(fout, " %sedi = eax;",
4946 lmod_cast_u_ptr(po, po->operand[0].lmod));
4947 strcpy(g_comment, "rep stos");
4950 fprintf(fout, " %sedi = eax; edi %c= %d;",
4951 lmod_cast_u_ptr(po, po->operand[0].lmod),
4952 (po->flags & OPF_DF) ? '-' : '+',
4953 lmod_bytes(po, po->operand[0].lmod));
4954 strcpy(g_comment, "stos");
4959 assert_operand_cnt(3);
4960 j = lmod_bytes(po, po->operand[0].lmod);
4961 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4962 l = (po->flags & OPF_DF) ? '-' : '+';
4963 if (po->flags & OPF_REP) {
4965 " for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n",
4968 " %sedi = %sesi;", buf1, buf1);
4969 strcpy(g_comment, "rep movs");
4972 fprintf(fout, " %sedi = %sesi; edi %c= %d; esi %c= %d;",
4973 buf1, buf1, l, j, l, j);
4974 strcpy(g_comment, "movs");
4979 // repe ~ repeat while ZF=1
4980 assert_operand_cnt(3);
4981 j = lmod_bytes(po, po->operand[0].lmod);
4982 strcpy(buf1, lmod_cast_u_ptr(po, po->operand[0].lmod));
4983 l = (po->flags & OPF_DF) ? '-' : '+';
4984 if (po->flags & OPF_REP) {
4986 " for (; ecx != 0; ecx--) {\n");
4987 if (pfomask & (1 << PFO_C)) {
4990 " cond_c = %sesi < %sedi;\n", buf1, buf1);
4991 pfomask &= ~(1 << PFO_C);
4994 " cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n",
4995 buf1, buf1, l, j, l, j);
4997 " if (cond_z %s 0) break;\n",
4998 (po->flags & OPF_REPZ) ? "==" : "!=");
5001 snprintf(g_comment, sizeof(g_comment), "rep%s cmps",
5002 (po->flags & OPF_REPZ) ? "e" : "ne");
5006 " cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;",
5007 buf1, buf1, l, j, l, j);
5008 strcpy(g_comment, "cmps");
5010 pfomask &= ~(1 << PFO_Z);
5011 last_arith_dst = NULL;
5012 delayed_flag_op = NULL;
5016 // only does ZF (for now)
5017 // repe ~ repeat while ZF=1
5018 assert_operand_cnt(3);
5019 j = lmod_bytes(po, po->operand[0].lmod);
5020 l = (po->flags & OPF_DF) ? '-' : '+';
5021 if (po->flags & OPF_REP) {
5023 " for (; ecx != 0; ecx--) {\n");
5025 " cond_z = (%seax == %sedi); edi %c= %d;\n",
5026 lmod_cast_u(po, po->operand[0].lmod),
5027 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
5029 " if (cond_z %s 0) break;\n",
5030 (po->flags & OPF_REPZ) ? "==" : "!=");
5033 snprintf(g_comment, sizeof(g_comment), "rep%s scas",
5034 (po->flags & OPF_REPZ) ? "e" : "ne");
5037 fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;",
5038 lmod_cast_u(po, po->operand[0].lmod),
5039 lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
5040 strcpy(g_comment, "scas");
5042 pfomask &= ~(1 << PFO_Z);
5043 last_arith_dst = NULL;
5044 delayed_flag_op = NULL;
5047 // arithmetic w/flags
5049 if (po->operand[1].type == OPT_CONST && !po->operand[1].val)
5050 goto dualop_arith_const;
5051 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5055 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5056 if (po->operand[1].type == OPT_CONST) {
5057 j = lmod_bytes(po, po->operand[0].lmod);
5058 if (((1ull << j * 8) - 1) == po->operand[1].val)
5059 goto dualop_arith_const;
5064 assert_operand_cnt(2);
5065 fprintf(fout, " %s %s= %s;",
5066 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
5068 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
5069 output_std_flags(fout, po, &pfomask, buf1);
5070 last_arith_dst = &po->operand[0];
5071 delayed_flag_op = NULL;
5075 // and 0, or ~0 used instead mov
5076 assert_operand_cnt(2);
5077 fprintf(fout, " %s = %s;",
5078 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
5079 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
5080 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
5081 output_std_flags(fout, po, &pfomask, buf1);
5082 last_arith_dst = &po->operand[0];
5083 delayed_flag_op = NULL;
5088 assert_operand_cnt(2);
5089 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5090 if (pfomask & (1 << PFO_C)) {
5091 if (po->operand[1].type == OPT_CONST) {
5092 l = lmod_bytes(po, po->operand[0].lmod) * 8;
5093 j = po->operand[1].val;
5096 if (po->op == OP_SHL)
5100 fprintf(fout, " cond_c = (%s >> %d) & 1;\n",
5104 ferr(po, "zero shift?\n");
5108 pfomask &= ~(1 << PFO_C);
5110 fprintf(fout, " %s %s= %s", buf1, op_to_c(po),
5111 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
5112 if (po->operand[1].type != OPT_CONST)
5113 fprintf(fout, " & 0x1f");
5115 output_std_flags(fout, po, &pfomask, buf1);
5116 last_arith_dst = &po->operand[0];
5117 delayed_flag_op = NULL;
5121 assert_operand_cnt(2);
5122 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5123 fprintf(fout, " %s = %s%s >> %s;", buf1,
5124 lmod_cast_s(po, po->operand[0].lmod), buf1,
5125 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
5126 output_std_flags(fout, po, &pfomask, buf1);
5127 last_arith_dst = &po->operand[0];
5128 delayed_flag_op = NULL;
5133 assert_operand_cnt(3);
5134 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5135 l = lmod_bytes(po, po->operand[0].lmod) * 8;
5136 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5137 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
5138 out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[2]);
5139 if (po->operand[2].type != OPT_CONST)
5140 ferr(po, "TODO: masking\n");
5141 if (po->op == OP_SHLD) {
5142 fprintf(fout, " %s <<= %s; %s |= %s >> (%d - %s);",
5143 buf1, buf3, buf1, buf2, l, buf3);
5144 strcpy(g_comment, "shld");
5147 fprintf(fout, " %s >>= %s; %s |= %s << (%d - %s);",
5148 buf1, buf3, buf1, buf2, l, buf3);
5149 strcpy(g_comment, "shrd");
5151 output_std_flags(fout, po, &pfomask, buf1);
5152 last_arith_dst = &po->operand[0];
5153 delayed_flag_op = NULL;
5158 assert_operand_cnt(2);
5159 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5160 if (po->operand[1].type == OPT_CONST) {
5161 j = po->operand[1].val;
5162 j %= lmod_bytes(po, po->operand[0].lmod) * 8;
5163 fprintf(fout, po->op == OP_ROL ?
5164 " %s = (%s << %d) | (%s >> %d);" :
5165 " %s = (%s >> %d) | (%s << %d);",
5166 buf1, buf1, j, buf1,
5167 lmod_bytes(po, po->operand[0].lmod) * 8 - j);
5171 output_std_flags(fout, po, &pfomask, buf1);
5172 last_arith_dst = &po->operand[0];
5173 delayed_flag_op = NULL;
5178 assert_operand_cnt(2);
5179 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5180 l = lmod_bytes(po, po->operand[0].lmod) * 8;
5181 if (po->operand[1].type == OPT_CONST) {
5182 j = po->operand[1].val % l;
5184 ferr(po, "zero rotate\n");
5185 fprintf(fout, " tmp = (%s >> %d) & 1;\n",
5186 buf1, (po->op == OP_RCL) ? (l - j) : (j - 1));
5187 if (po->op == OP_RCL) {
5189 " %s = (%s << %d) | (cond_c << %d)",
5190 buf1, buf1, j, j - 1);
5192 fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j);
5196 " %s = (%s >> %d) | (cond_c << %d)",
5197 buf1, buf1, j, l - j);
5199 fprintf(fout, " | (%s << %d)", buf1, l + 1 - j);
5201 fprintf(fout, ";\n");
5202 fprintf(fout, " cond_c = tmp;");
5206 strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr");
5207 output_std_flags(fout, po, &pfomask, buf1);
5208 last_arith_dst = &po->operand[0];
5209 delayed_flag_op = NULL;
5213 assert_operand_cnt(2);
5214 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5215 if (IS(opr_name(po, 0), opr_name(po, 1))) {
5216 // special case for XOR
5217 if (pfomask & (1 << PFO_BE)) { // weird, but it happens..
5218 fprintf(fout, " cond_be = 1;\n");
5219 pfomask &= ~(1 << PFO_BE);
5221 fprintf(fout, " %s = 0;",
5222 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
5223 last_arith_dst = &po->operand[0];
5224 delayed_flag_op = NULL;
5230 assert_operand_cnt(2);
5231 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5232 if (pfomask & (1 << PFO_C)) {
5233 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
5234 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
5235 if (po->operand[0].lmod == OPLM_DWORD) {
5236 fprintf(fout, " tmp64 = (u64)%s + %s;\n", buf1, buf2);
5237 fprintf(fout, " cond_c = tmp64 >> 32;\n");
5238 fprintf(fout, " %s = (u32)tmp64;",
5239 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
5240 strcat(g_comment, "add64");
5243 fprintf(fout, " cond_c = ((u32)%s + %s) >> %d;\n",
5244 buf1, buf2, lmod_bytes(po, po->operand[0].lmod) * 8);
5245 fprintf(fout, " %s += %s;",
5246 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
5249 pfomask &= ~(1 << PFO_C);
5250 output_std_flags(fout, po, &pfomask, buf1);
5251 last_arith_dst = &po->operand[0];
5252 delayed_flag_op = NULL;
5258 assert_operand_cnt(2);
5259 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5260 if (pfomask & ~((1 << PFO_Z) | (1 << PFO_S))) {
5261 for (j = 0; j <= PFO_LE; j++) {
5262 if (!(pfomask & (1 << j)))
5264 if (j == PFO_Z || j == PFO_S)
5267 out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0);
5268 fprintf(fout, " cond_%s = %s;\n",
5269 parsed_flag_op_names[j], buf1);
5270 pfomask &= ~(1 << j);
5277 assert_operand_cnt(2);
5278 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5279 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5280 if (po->op == OP_SBB
5281 && IS(po->operand[0].name, po->operand[1].name))
5283 // avoid use of unitialized var
5284 fprintf(fout, " %s = -cond_c;", buf1);
5285 // carry remains what it was
5286 pfomask &= ~(1 << PFO_C);
5289 fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
5290 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
5292 output_std_flags(fout, po, &pfomask, buf1);
5293 last_arith_dst = &po->operand[0];
5294 delayed_flag_op = NULL;
5298 assert_operand_cnt(2);
5299 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
5300 fprintf(fout, " %s = %s ? __builtin_ffs(%s) - 1 : 0;",
5301 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
5303 output_std_flags(fout, po, &pfomask, buf1);
5304 last_arith_dst = &po->operand[0];
5305 delayed_flag_op = NULL;
5306 strcat(g_comment, "bsf");
5310 if (pfomask & ~(PFOB_S | PFOB_S | PFOB_C)) {
5311 for (j = 0; j <= PFO_LE; j++) {
5312 if (!(pfomask & (1 << j)))
5314 if (j == PFO_Z || j == PFO_S || j == PFO_C)
5317 out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0);
5318 fprintf(fout, " cond_%s = %s;\n",
5319 parsed_flag_op_names[j], buf1);
5320 pfomask &= ~(1 << j);
5326 if (pfomask & (1 << PFO_C))
5327 // carry is unaffected by inc/dec.. wtf?
5328 ferr(po, "carry propagation needed\n");
5330 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5331 if (po->operand[0].type == OPT_REG) {
5332 strcpy(buf2, po->op == OP_INC ? "++" : "--");
5333 fprintf(fout, " %s%s;", buf1, buf2);
5336 strcpy(buf2, po->op == OP_INC ? "+" : "-");
5337 fprintf(fout, " %s %s= 1;", buf1, buf2);
5339 output_std_flags(fout, po, &pfomask, buf1);
5340 last_arith_dst = &po->operand[0];
5341 delayed_flag_op = NULL;
5345 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5346 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
5347 fprintf(fout, " %s = -%s%s;", buf1,
5348 lmod_cast_s(po, po->operand[0].lmod), buf2);
5349 last_arith_dst = &po->operand[0];
5350 delayed_flag_op = NULL;
5351 if (pfomask & (1 << PFO_C)) {
5352 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
5353 pfomask &= ~(1 << PFO_C);
5358 if (po->operand_cnt == 2) {
5359 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5362 if (po->operand_cnt == 3)
5363 ferr(po, "TODO imul3\n");
5366 assert_operand_cnt(1);
5367 switch (po->operand[0].lmod) {
5369 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
5370 fprintf(fout, " tmp64 = %seax * %s%s;\n", buf1, buf1,
5371 out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
5372 fprintf(fout, " edx = tmp64 >> 32;\n");
5373 fprintf(fout, " eax = tmp64;");
5376 strcpy(buf1, po->op == OP_IMUL ? "(s16)(s8)" : "(u16)(u8)");
5377 fprintf(fout, " LOWORD(eax) = %seax * %s;", buf1,
5378 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0],
5382 ferr(po, "TODO: unhandled mul type\n");
5385 last_arith_dst = NULL;
5386 delayed_flag_op = NULL;
5391 assert_operand_cnt(1);
5392 if (po->operand[0].lmod != OPLM_DWORD)
5393 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
5395 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
5396 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
5397 po->op == OP_IDIV));
5398 switch (po->operand[0].lmod) {
5400 if (po->flags & OPF_32BIT)
5401 snprintf(buf3, sizeof(buf3), "%seax", buf2);
5403 fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n");
5404 snprintf(buf3, sizeof(buf3), "%stmp64",
5405 (po->op == OP_IDIV) ? "(s64)" : "");
5407 if (po->operand[0].type == OPT_REG
5408 && po->operand[0].reg == xDX)
5410 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
5411 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
5414 fprintf(fout, " edx = %s %% %s%s;\n", buf3, buf2, buf1);
5415 fprintf(fout, " eax = %s / %s%s;", buf3, buf2, buf1);
5419 ferr(po, "unhandled division type\n");
5421 last_arith_dst = NULL;
5422 delayed_flag_op = NULL;
5427 propagate_lmod(po, &po->operand[0], &po->operand[1]);
5429 for (j = 0; j < 8; j++) {
5430 if (pfomask & (1 << j)) {
5431 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
5432 fprintf(fout, " cond_%s = %s;",
5433 parsed_flag_op_names[j], buf1);
5440 last_arith_dst = NULL;
5441 delayed_flag_op = po;
5445 // SETcc - should already be handled
5448 // note: we reuse OP_Jcc for SETcc, only flags differ
5450 fprintf(fout, "\n goto %s;", po->operand[0].name);
5454 fprintf(fout, " if (ecx == 0)\n");
5455 fprintf(fout, " goto %s;", po->operand[0].name);
5456 strcat(g_comment, "jecxz");
5460 fprintf(fout, " if (--ecx == 0)\n");
5461 fprintf(fout, " goto %s;", po->operand[0].name);
5462 strcat(g_comment, "loop");
5466 assert_operand_cnt(1);
5467 last_arith_dst = NULL;
5468 delayed_flag_op = NULL;
5470 if (po->operand[0].type == OPT_REGMEM) {
5471 ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
5474 ferr(po, "parse failure for jmp '%s'\n",
5475 po->operand[0].name);
5476 fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
5479 else if (po->operand[0].type != OPT_LABEL)
5480 ferr(po, "unhandled jmp type\n");
5482 fprintf(fout, " goto %s;", po->operand[0].name);
5486 assert_operand_cnt(1);
5488 my_assert_not(pp, NULL);
5491 if (po->flags & OPF_CC) {
5492 // we treat conditional branch to another func
5493 // (yes such code exists..) as conditional tailcall
5495 fprintf(fout, " {\n");
5498 if (pp->is_fptr && !pp->is_arg) {
5499 fprintf(fout, "%s%s = %s;\n", buf3, pp->name,
5500 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
5502 if (pp->is_unresolved)
5503 fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
5504 buf3, asmfn, po->asmln, pp->name);
5507 fprintf(fout, "%s", buf3);
5508 if (strstr(pp->ret_type.name, "int64")) {
5509 if (po->flags & OPF_TAIL)
5510 ferr(po, "int64 and tail?\n");
5511 fprintf(fout, "tmp64 = ");
5513 else if (!IS(pp->ret_type.name, "void")) {
5514 if (po->flags & OPF_TAIL) {
5515 if (!IS(g_func_pp->ret_type.name, "void")) {
5516 fprintf(fout, "return ");
5517 if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
5518 fprintf(fout, "(%s)", g_func_pp->ret_type.name);
5521 else if (regmask & (1 << xAX)) {
5522 fprintf(fout, "eax = ");
5523 if (pp->ret_type.is_ptr)
5524 fprintf(fout, "(u32)");
5528 if (pp->name[0] == 0)
5529 ferr(po, "missing pp->name\n");
5530 fprintf(fout, "%s%s(", pp->name,
5531 pp->has_structarg ? "_sa" : "");
5533 if (po->flags & OPF_ATAIL) {
5534 if (pp->argc_stack != g_func_pp->argc_stack
5535 || (pp->argc_stack > 0
5536 && pp->is_stdcall != g_func_pp->is_stdcall))
5537 ferr(po, "incompatible tailcall\n");
5538 if (g_func_pp->has_retreg)
5539 ferr(po, "TODO: retreg+tailcall\n");
5541 for (arg = j = 0; arg < pp->argc; arg++) {
5543 fprintf(fout, ", ");
5546 if (pp->arg[arg].type.is_ptr)
5547 snprintf(cast, sizeof(cast), "(%s)",
5548 pp->arg[arg].type.name);
5550 if (pp->arg[arg].reg != NULL) {
5551 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
5555 for (; j < g_func_pp->argc; j++)
5556 if (g_func_pp->arg[j].reg == NULL)
5558 fprintf(fout, "%sa%d", cast, j + 1);
5563 for (arg = 0; arg < pp->argc; arg++) {
5565 fprintf(fout, ", ");
5568 if (pp->arg[arg].type.is_ptr)
5569 snprintf(cast, sizeof(cast), "(%s)",
5570 pp->arg[arg].type.name);
5572 if (pp->arg[arg].reg != NULL) {
5573 if (pp->arg[arg].type.is_retreg)
5574 fprintf(fout, "&%s", pp->arg[arg].reg);
5576 fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
5581 tmp_op = pp->arg[arg].datap;
5583 ferr(po, "parsed_op missing for arg%d\n", arg);
5585 if (tmp_op->flags & OPF_VAPUSH) {
5586 fprintf(fout, "ap");
5588 else if (tmp_op->p_argpass != 0) {
5589 fprintf(fout, "a%d", tmp_op->p_argpass);
5591 else if (tmp_op->p_argnum != 0) {
5592 fprintf(fout, "%s%s", cast,
5593 saved_arg_name(buf1, sizeof(buf1),
5594 tmp_op->p_arggrp, tmp_op->p_argnum));
5598 out_src_opr(buf1, sizeof(buf1),
5599 tmp_op, &tmp_op->operand[0], cast, 0));
5603 fprintf(fout, ");");
5605 if (strstr(pp->ret_type.name, "int64")) {
5606 fprintf(fout, "\n");
5607 fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3);
5608 fprintf(fout, "%seax = tmp64;", buf3);
5611 if (pp->is_unresolved) {
5612 snprintf(buf2, sizeof(buf2), " unresolved %dreg",
5614 strcat(g_comment, buf2);
5617 if (po->flags & OPF_TAIL) {
5619 if (i == opcnt - 1 || pp->is_noreturn)
5621 else if (IS(pp->ret_type.name, "void"))
5623 else if (IS(g_func_pp->ret_type.name, "void"))
5625 // else already handled as 'return f()'
5628 if (!IS(g_func_pp->ret_type.name, "void")) {
5629 ferr(po, "int func -> void func tailcall?\n");
5632 fprintf(fout, "\n%sreturn;", buf3);
5633 strcat(g_comment, " ^ tailcall");
5637 strcat(g_comment, " tailcall");
5639 if (pp->is_noreturn)
5640 strcat(g_comment, " noreturn");
5641 if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0)
5642 strcat(g_comment, " argframe");
5643 if (po->flags & OPF_CC)
5644 strcat(g_comment, " cond");
5646 if (po->flags & OPF_CC)
5647 fprintf(fout, "\n }");
5649 delayed_flag_op = NULL;
5650 last_arith_dst = NULL;
5654 if (g_func_pp->is_vararg)
5655 fprintf(fout, " va_end(ap);\n");
5656 if (g_func_pp->has_retreg) {
5657 for (arg = 0; arg < g_func_pp->argc; arg++)
5658 if (g_func_pp->arg[arg].type.is_retreg)
5659 fprintf(fout, " *r_%s = %s;\n",
5660 g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg);
5663 if (IS(g_func_pp->ret_type.name, "void")) {
5664 if (i != opcnt - 1 || label_pending)
5665 fprintf(fout, " return;");
5667 else if (g_func_pp->ret_type.is_ptr) {
5668 fprintf(fout, " return (%s)eax;",
5669 g_func_pp->ret_type.name);
5671 else if (IS(g_func_pp->ret_type.name, "__int64"))
5672 fprintf(fout, " return ((u64)edx << 32) | eax;");
5674 fprintf(fout, " return eax;");
5676 last_arith_dst = NULL;
5677 delayed_flag_op = NULL;
5681 out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
5682 if (po->p_argnum != 0) {
5683 // special case - saved func arg
5684 fprintf(fout, " %s = %s;",
5685 saved_arg_name(buf2, sizeof(buf2),
5686 po->p_arggrp, po->p_argnum), buf1);
5689 else if (po->flags & OPF_RSAVE) {
5690 fprintf(fout, " s_%s = %s;", buf1, buf1);
5693 else if (po->flags & OPF_PPUSH) {
5695 ferr_assert(po, tmp_op != NULL);
5696 out_dst_opr(buf2, sizeof(buf2), po, &tmp_op->operand[0]);
5697 fprintf(fout, " pp_%s = %s;", buf2, buf1);
5700 else if (g_func_pp->is_userstack) {
5701 fprintf(fout, " *(--esp) = %s;", buf1);
5704 if (!(g_ida_func_attr & IDAFA_NORETURN))
5705 ferr(po, "stray push encountered\n");
5710 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
5711 if (po->flags & OPF_RSAVE) {
5712 fprintf(fout, " %s = s_%s;", buf1, buf1);
5715 else if (po->flags & OPF_PPUSH) {
5717 ferr_assert(po, po->datap == NULL);
5718 fprintf(fout, " %s = pp_%s;", buf1, buf1);
5721 else if (po->datap != NULL) {
5724 fprintf(fout, " %s = %s;", buf1,
5725 out_src_opr(buf2, sizeof(buf2),
5726 tmp_op, &tmp_op->operand[0],
5727 default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
5730 else if (g_func_pp->is_userstack) {
5731 fprintf(fout, " %s = *esp++;", buf1);
5735 ferr(po, "stray pop encountered\n");
5744 strcpy(g_comment, "(emms)");
5749 ferr(po, "unhandled op type %d, flags %x\n",
5754 if (g_comment[0] != 0) {
5755 char *p = g_comment;
5756 while (my_isblank(*p))
5758 fprintf(fout, " // %s", p);
5763 fprintf(fout, "\n");
5765 // some sanity checking
5766 if (po->flags & OPF_REP) {
5767 if (po->op != OP_STOS && po->op != OP_MOVS
5768 && po->op != OP_CMPS && po->op != OP_SCAS)
5769 ferr(po, "unexpected rep\n");
5770 if (!(po->flags & (OPF_REPZ|OPF_REPNZ))
5771 && (po->op == OP_CMPS || po->op == OP_SCAS))
5772 ferr(po, "cmps/scas with plain rep\n");
5774 if ((po->flags & (OPF_REPZ|OPF_REPNZ))
5775 && po->op != OP_CMPS && po->op != OP_SCAS)
5776 ferr(po, "unexpected repz/repnz\n");
5779 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
5781 // see is delayed flag stuff is still valid
5782 if (delayed_flag_op != NULL && delayed_flag_op != po) {
5783 if (is_any_opr_modified(delayed_flag_op, po, 0))
5784 delayed_flag_op = NULL;
5787 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
5788 if (is_opr_modified(last_arith_dst, po))
5789 last_arith_dst = NULL;
5795 if (g_stack_fsz && !g_stack_frame_used)
5796 fprintf(fout, " (void)sf;\n");
5798 fprintf(fout, "}\n\n");
5800 gen_x_cleanup(opcnt);
5803 static void gen_x_cleanup(int opcnt)
5807 for (i = 0; i < opcnt; i++) {
5808 struct label_ref *lr, *lr_del;
5810 lr = g_label_refs[i].next;
5811 while (lr != NULL) {
5816 g_label_refs[i].i = -1;
5817 g_label_refs[i].next = NULL;
5819 if (ops[i].op == OP_CALL) {
5821 proto_release(ops[i].pp);
5827 struct func_proto_dep;
5829 struct func_prototype {
5834 int has_ret:3; // -1, 0, 1: unresolved, no, yes
5835 unsigned int dep_resolved:1;
5836 unsigned int is_stdcall:1;
5837 struct func_proto_dep *dep_func;
5839 const struct parsed_proto *pp; // seed pp, if any
5842 struct func_proto_dep {
5844 struct func_prototype *proto;
5845 int regmask_live; // .. at the time of call
5846 unsigned int ret_dep:1; // return from this is caller's return
5849 static struct func_prototype *hg_fp;
5850 static int hg_fp_cnt;
5852 static struct scanned_var {
5854 enum opr_lenmod lmod;
5855 unsigned int is_seeded:1;
5856 unsigned int is_c_str:1;
5857 const struct parsed_proto *pp; // seed pp, if any
5859 static int hg_var_cnt;
5861 static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
5864 struct func_prototype *hg_fp_add(const char *funcn)
5866 struct func_prototype *fp;
5868 if ((hg_fp_cnt & 0xff) == 0) {
5869 hg_fp = realloc(hg_fp, sizeof(hg_fp[0]) * (hg_fp_cnt + 0x100));
5870 my_assert_not(hg_fp, NULL);
5871 memset(hg_fp + hg_fp_cnt, 0, sizeof(hg_fp[0]) * 0x100);
5874 fp = &hg_fp[hg_fp_cnt];
5875 snprintf(fp->name, sizeof(fp->name), "%s", funcn);
5877 fp->argc_stack = -1;
5883 static struct func_proto_dep *hg_fp_find_dep(struct func_prototype *fp,
5888 for (i = 0; i < fp->dep_func_cnt; i++)
5889 if (IS(fp->dep_func[i].name, name))
5890 return &fp->dep_func[i];
5895 static void hg_fp_add_dep(struct func_prototype *fp, const char *name)
5898 if (hg_fp_find_dep(fp, name))
5901 if ((fp->dep_func_cnt & 0xff) == 0) {
5902 fp->dep_func = realloc(fp->dep_func,
5903 sizeof(fp->dep_func[0]) * (fp->dep_func_cnt + 0x100));
5904 my_assert_not(fp->dep_func, NULL);
5905 memset(&fp->dep_func[fp->dep_func_cnt], 0,
5906 sizeof(fp->dep_func[0]) * 0x100);
5908 fp->dep_func[fp->dep_func_cnt].name = strdup(name);
5912 static int hg_fp_cmp_name(const void *p1_, const void *p2_)
5914 const struct func_prototype *p1 = p1_, *p2 = p2_;
5915 return strcmp(p1->name, p2->name);
5919 static int hg_fp_cmp_id(const void *p1_, const void *p2_)
5921 const struct func_prototype *p1 = p1_, *p2 = p2_;
5922 return p1->id - p2->id;
5926 // recursive register dep pass
5927 // - track saved regs (part 2)
5928 // - try to figure out arg-regs
5929 // - calculate reg deps
5930 static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits,
5931 struct func_prototype *fp, int regmask_save, int regmask_dst,
5932 int *regmask_dep, int *has_ret)
5934 struct func_proto_dep *dep;
5935 struct parsed_op *po;
5936 int from_caller = 0;
5942 for (; i < opcnt; i++)
5944 if (cbits[i >> 3] & (1 << (i & 7)))
5946 cbits[i >> 3] |= (1 << (i & 7));
5950 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
5951 if (po->flags & OPF_RMD)
5954 if (po->btj != NULL) {
5956 for (j = 0; j < po->btj->count; j++) {
5957 check_i(po, po->btj->d[j].bt_i);
5958 gen_hdr_dep_pass(po->btj->d[j].bt_i, opcnt, cbits, fp,
5959 regmask_save, regmask_dst, regmask_dep, has_ret);
5964 check_i(po, po->bt_i);
5965 if (po->flags & OPF_CJMP) {
5966 gen_hdr_dep_pass(po->bt_i, opcnt, cbits, fp,
5967 regmask_save, regmask_dst, regmask_dep, has_ret);
5975 if (po->flags & OPF_FARG)
5976 /* (just calculate register deps) */;
5977 else if (po->op == OP_PUSH && po->operand[0].type == OPT_REG)
5979 reg = po->operand[0].reg;
5981 ferr(po, "reg not set for push?\n");
5983 if (po->flags & OPF_RSAVE) {
5984 regmask_save |= 1 << reg;
5987 if (po->flags & OPF_DONE)
5991 ret = scan_for_pop(i + 1, opcnt,
5992 po->operand[0].name, i + opcnt * 2, 0, &depth, 0);
5994 regmask_save |= 1 << reg;
5995 po->flags |= OPF_RMD;
5996 scan_for_pop(i + 1, opcnt,
5997 po->operand[0].name, i + opcnt * 3, 0, &depth, 1);
6001 else if (po->flags & OPF_RMD)
6003 else if (po->op == OP_CALL) {
6004 po->regmask_dst |= 1 << xAX;
6006 dep = hg_fp_find_dep(fp, po->operand[0].name);
6008 dep->regmask_live = regmask_save | regmask_dst;
6010 else if (po->op == OP_RET) {
6011 if (po->operand_cnt > 0) {
6013 if (fp->argc_stack >= 0
6014 && fp->argc_stack != po->operand[0].val / 4)
6015 ferr(po, "ret mismatch? (%d)\n", fp->argc_stack * 4);
6016 fp->argc_stack = po->operand[0].val / 4;
6020 // if has_ret is 0, there is uninitialized eax path,
6021 // which means it's most likely void func
6022 if (*has_ret != 0 && (po->flags & OPF_TAIL)) {
6023 if (po->op == OP_CALL) {
6028 struct parsed_opr opr = { 0, };
6033 ret = resolve_origin(i, &opr, i + opcnt * 4, &j, &from_caller);
6036 if (ret != 1 && from_caller) {
6037 // unresolved eax - probably void func
6041 if (j >= 0 && ops[j].op == OP_CALL) {
6042 dep = hg_fp_find_dep(fp, ops[j].operand[0].name);
6053 l = regmask_save | regmask_dst;
6054 if (g_bp_frame && !(po->flags & OPF_EBP_S))
6057 l = po->regmask_src & ~l;
6060 fnote(po, "dep |= %04x, dst %04x, save %04x (f %x)\n",
6061 l, regmask_dst, regmask_save, po->flags);
6064 regmask_dst |= po->regmask_dst;
6066 if (po->flags & OPF_TAIL)
6071 static void gen_hdr(const char *funcn, int opcnt)
6073 int save_arg_vars[MAX_ARG_GRP] = { 0, };
6074 unsigned char cbits[MAX_OPS / 8];
6075 const struct parsed_proto *pp_c;
6076 struct parsed_proto *pp;
6077 struct func_prototype *fp;
6078 struct parsed_op *po;
6079 int regmask_dummy = 0;
6081 int max_bp_offset = 0;
6086 pp_c = proto_parse(g_fhdr, funcn, 1);
6088 // already in seed, will add to hg_fp later
6091 fp = hg_fp_add(funcn);
6093 g_bp_frame = g_sp_frame = g_stack_fsz = 0;
6094 g_stack_frame_used = 0;
6097 // - resolve all branches
6098 // - parse calls with labels
6099 resolve_branches_parse_calls(opcnt);
6102 // - handle ebp/esp frame, remove ops related to it
6103 scan_prologue_epilogue(opcnt);
6106 // - remove dead labels
6108 for (i = 0; i < opcnt; i++)
6110 if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
6116 if (po->flags & (OPF_RMD|OPF_DONE))
6119 if (po->op == OP_CALL) {
6120 if (po->operand[0].type == OPT_LABEL)
6121 hg_fp_add_dep(fp, opr_name(po, 0));
6122 else if (po->pp != NULL)
6123 hg_fp_add_dep(fp, po->pp->name);
6128 // - remove dead labels
6129 // - handle push <const>/pop pairs
6130 for (i = 0; i < opcnt; i++)
6132 if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
6138 if (po->flags & (OPF_RMD|OPF_DONE))
6141 if (po->op == OP_PUSH && po->operand[0].type == OPT_CONST)
6142 scan_for_pop_const(i, opcnt, ®mask_dummy);
6146 // - process trivial calls
6147 for (i = 0; i < opcnt; i++)
6150 if (po->flags & (OPF_RMD|OPF_DONE))
6153 if (po->op == OP_CALL)
6155 pp = process_call_early(i, opcnt, &j);
6157 if (!(po->flags & OPF_ATAIL))
6158 // since we know the args, try to collect them
6159 if (collect_call_args_early(po, i, pp, ®mask_dummy) != 0)
6165 // commit esp adjust
6166 if (ops[j].op != OP_POP)
6167 patch_esp_adjust(&ops[j], pp->argc_stack * 4);
6169 for (l = 0; l < pp->argc_stack; l++)
6170 ops[j + l].flags |= OPF_DONE | OPF_RMD;
6174 po->flags |= OPF_DONE;
6180 // - track saved regs (simple)
6182 for (i = 0; i < opcnt; i++)
6185 if (po->flags & (OPF_RMD|OPF_DONE))
6188 if (po->op == OP_PUSH && po->operand[0].type == OPT_REG)
6190 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
6192 // regmask_save |= 1 << po->operand[0].reg; // do it later
6193 po->flags |= OPF_RSAVE | OPF_RMD | OPF_DONE;
6194 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, OPF_RMD);
6197 else if (po->op == OP_CALL && !(po->flags & OPF_DONE))
6199 pp = process_call(i, opcnt);
6201 if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
6202 // since we know the args, collect them
6203 ret = collect_call_args(po, i, pp, ®mask_dummy, save_arg_vars,
6210 memset(cbits, 0, sizeof(cbits));
6214 gen_hdr_dep_pass(0, opcnt, cbits, fp, 0, 0, ®mask_dep, &has_ret);
6216 // find unreachable code - must be fixed in IDA
6217 for (i = 0; i < opcnt; i++)
6219 if (cbits[i >> 3] & (1 << (i & 7)))
6222 if (g_labels[i] == NULL && i > 0 && ops[i - 1].op == OP_CALL
6223 && ops[i - 1].pp != NULL && ops[i - 1].pp->is_osinc)
6225 // the compiler sometimes still generates code after
6226 // noreturn OS functions
6229 if (ops[i].op != OP_NOP)
6230 ferr(&ops[i], "unreachable code\n");
6233 for (i = 0; i < g_eqcnt; i++) {
6234 if (g_eqs[i].offset > max_bp_offset && g_eqs[i].offset < 4*32)
6235 max_bp_offset = g_eqs[i].offset;
6238 if (fp->argc_stack < 0) {
6239 max_bp_offset = (max_bp_offset + 3) & ~3;
6240 fp->argc_stack = max_bp_offset / 4;
6241 if ((g_ida_func_attr & IDAFA_BP_FRAME) && fp->argc_stack > 0)
6245 fp->regmask_dep = regmask_dep & ~(1 << xSP);
6246 fp->has_ret = has_ret;
6248 printf("// has_ret %d, regmask_dep %x\n",
6249 fp->has_ret, fp->regmask_dep);
6250 output_hdr_fp(stdout, fp, 1);
6251 if (IS(funcn, "sub_10007F72")) exit(1);
6254 gen_x_cleanup(opcnt);
6257 static void hg_fp_resolve_deps(struct func_prototype *fp)
6259 struct func_prototype fp_s;
6263 // this thing is recursive, so mark first..
6264 fp->dep_resolved = 1;
6266 for (i = 0; i < fp->dep_func_cnt; i++) {
6267 strcpy(fp_s.name, fp->dep_func[i].name);
6268 fp->dep_func[i].proto = bsearch(&fp_s, hg_fp, hg_fp_cnt,
6269 sizeof(hg_fp[0]), hg_fp_cmp_name);
6270 if (fp->dep_func[i].proto != NULL) {
6271 if (!fp->dep_func[i].proto->dep_resolved)
6272 hg_fp_resolve_deps(fp->dep_func[i].proto);
6274 dep = ~fp->dep_func[i].regmask_live
6275 & fp->dep_func[i].proto->regmask_dep;
6276 fp->regmask_dep |= dep;
6277 // printf("dep %s %s |= %x\n", fp->name,
6278 // fp->dep_func[i].name, dep);
6280 if (fp->has_ret == -1 && fp->dep_func[i].ret_dep)
6281 fp->has_ret = fp->dep_func[i].proto->has_ret;
6286 static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
6289 const struct parsed_proto *pp;
6290 char *p, namebuf[NAMELEN];
6296 for (; count > 0; count--, fp++) {
6297 if (fp->has_ret == -1)
6298 fprintf(fout, "// ret unresolved\n");
6300 fprintf(fout, "// dep:");
6301 for (j = 0; j < fp->dep_func_cnt; j++) {
6302 fprintf(fout, " %s/", fp->dep_func[j].name);
6303 if (fp->dep_func[j].proto != NULL)
6304 fprintf(fout, "%04x/%d", fp->dep_func[j].proto->regmask_dep,
6305 fp->dep_func[j].proto->has_ret);
6307 fprintf(fout, "\n");
6310 p = strchr(fp->name, '@');
6312 memcpy(namebuf, fp->name, p - fp->name);
6313 namebuf[p - fp->name] = 0;
6321 pp = proto_parse(g_fhdr, name, 1);
6322 if (pp != NULL && pp->is_include)
6325 if (fp->pp != NULL) {
6326 // part of seed, output later
6330 regmask_dep = fp->regmask_dep;
6331 argc_stack = fp->argc_stack;
6333 fprintf(fout, "%-5s", fp->pp ? fp->pp->ret_type.name :
6334 (fp->has_ret ? "int" : "void"));
6335 if (regmask_dep && (fp->is_stdcall || argc_stack == 0)
6336 && (regmask_dep & ~((1 << xCX) | (1 << xDX))) == 0)
6338 fprintf(fout, " __fastcall ");
6339 if (!(regmask_dep & (1 << xDX)) && argc_stack == 0)
6345 else if (regmask_dep && !fp->is_stdcall) {
6346 fprintf(fout, "/*__usercall*/ ");
6348 else if (regmask_dep) {
6349 fprintf(fout, "/*__userpurge*/ ");
6351 else if (fp->is_stdcall)
6352 fprintf(fout, " __stdcall ");
6354 fprintf(fout, " __cdecl ");
6356 fprintf(fout, "%s(", name);
6359 for (j = 0; j < xSP; j++) {
6360 if (regmask_dep & (1 << j)) {
6363 fprintf(fout, ", ");
6365 fprintf(fout, "%s", fp->pp->arg[arg - 1].type.name);
6367 fprintf(fout, "int");
6368 fprintf(fout, " a%d/*<%s>*/", arg, regs_r32[j]);
6372 for (j = 0; j < argc_stack; j++) {
6375 fprintf(fout, ", ");
6376 if (fp->pp != NULL) {
6377 fprintf(fout, "%s", fp->pp->arg[arg - 1].type.name);
6378 if (!fp->pp->arg[arg - 1].type.is_ptr)
6382 fprintf(fout, "int ");
6383 fprintf(fout, "a%d", arg);
6386 fprintf(fout, ");\n");
6390 static void output_hdr(FILE *fout)
6392 static const char *lmod_c_names[] = {
6393 [OPLM_UNSPEC] = "???",
6394 [OPLM_BYTE] = "uint8_t",
6395 [OPLM_WORD] = "uint16_t",
6396 [OPLM_DWORD] = "uint32_t",
6397 [OPLM_QWORD] = "uint64_t",
6399 const struct scanned_var *var;
6400 struct func_prototype *fp;
6401 char line[256] = { 0, };
6405 // add stuff from headers
6406 for (i = 0; i < pp_cache_size; i++) {
6407 if (pp_cache[i].is_cinc && !pp_cache[i].is_stdcall)
6408 snprintf(name, sizeof(name), "_%s", pp_cache[i].name);
6410 snprintf(name, sizeof(name), "%s", pp_cache[i].name);
6411 fp = hg_fp_add(name);
6412 fp->pp = &pp_cache[i];
6413 fp->argc_stack = fp->pp->argc_stack;
6414 fp->is_stdcall = fp->pp->is_stdcall;
6415 fp->regmask_dep = get_pp_arg_regmask(fp->pp);
6416 fp->has_ret = !IS(fp->pp->ret_type.name, "void");
6420 qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_name);
6421 for (i = 0; i < hg_fp_cnt; i++)
6422 hg_fp_resolve_deps(&hg_fp[i]);
6424 // note: messes up .proto ptr, don't use
6425 //qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_id);
6428 for (i = 0; i < hg_var_cnt; i++) {
6431 if (var->pp != NULL)
6434 else if (var->is_c_str)
6435 fprintf(fout, "extern %-8s %s[];", "char", var->name);
6437 fprintf(fout, "extern %-8s %s;",
6438 lmod_c_names[var->lmod], var->name);
6441 fprintf(fout, " // seeded");
6442 fprintf(fout, "\n");
6445 fprintf(fout, "\n");
6447 // output function prototypes
6448 output_hdr_fp(fout, hg_fp, hg_fp_cnt);
6451 fprintf(fout, "\n// - seed -\n");
6454 while (fgets(line, sizeof(line), g_fhdr))
6455 fwrite(line, 1, strlen(line), fout);
6458 // '=' needs special treatment
6460 static char *next_word_s(char *w, size_t wsize, char *s)
6469 for (i = 1; i < wsize - 1; i++) {
6471 printf("warning: missing closing quote: \"%s\"\n", s);
6480 for (; i < wsize - 1; i++) {
6481 if (s[i] == 0 || my_isblank(s[i]) || (s[i] == '=' && i > 0))
6487 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != '=')
6488 printf("warning: '%s' truncated\n", w);
6493 static void scan_variables(FILE *fasm)
6495 struct scanned_var *var;
6496 char line[256] = { 0, };
6504 // skip to next data section
6505 while (my_fgets(line, sizeof(line), fasm))
6510 if (*p == 0 || *p == ';')
6513 p = sskip(next_word_s(words[0], sizeof(words[0]), p));
6514 if (*p == 0 || *p == ';')
6517 if (*p != 's' || !IS_START(p, "segment para public"))
6523 if (p == NULL || !IS_START(p, "segment para public"))
6527 if (!IS_START(p, "'DATA'"))
6531 while (my_fgets(line, sizeof(line), fasm))
6540 if (*p == 0 || *p == ';')
6543 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
6544 words[wordc][0] = 0;
6545 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
6546 if (*p == 0 || *p == ';') {
6552 if (wordc == 2 && IS(words[1], "ends"))
6557 if (IS_START(words[0], "__IMPORT_DESCRIPTOR_")) {
6558 // when this starts, we don't need anything from this section
6562 if ((hg_var_cnt & 0xff) == 0) {
6563 hg_vars = realloc(hg_vars, sizeof(hg_vars[0])
6564 * (hg_var_cnt + 0x100));
6565 my_assert_not(hg_vars, NULL);
6566 memset(hg_vars + hg_var_cnt, 0, sizeof(hg_vars[0]) * 0x100);
6569 var = &hg_vars[hg_var_cnt++];
6570 snprintf(var->name, sizeof(var->name), "%s", words[0]);
6572 // maybe already in seed header?
6573 var->pp = proto_parse(g_fhdr, var->name, 1);
6574 if (var->pp != NULL) {
6575 if (var->pp->is_fptr) {
6576 var->lmod = OPLM_DWORD;
6579 else if (var->pp->is_func)
6581 else if (!guess_lmod_from_c_type(&var->lmod, &var->pp->type))
6582 aerr("unhandled C type '%s' for '%s'\n",
6583 var->pp->type.name, var->name);
6589 if (IS(words[1], "dd"))
6590 var->lmod = OPLM_DWORD;
6591 else if (IS(words[1], "dw"))
6592 var->lmod = OPLM_WORD;
6593 else if (IS(words[1], "db")) {
6594 var->lmod = OPLM_BYTE;
6595 if (wordc >= 3 && (l = strlen(words[2])) > 4) {
6596 if (words[2][0] == '\'' && IS(words[2] + l - 2, ",0"))
6600 else if (IS(words[1], "dq"))
6601 var->lmod = OPLM_QWORD;
6602 //else if (IS(words[1], "dt"))
6604 aerr("type '%s' not known\n", words[1]);
6612 static void set_label(int i, const char *name)
6618 p = strchr(name, ':');
6622 if (g_labels[i] != NULL && !IS_START(g_labels[i], "algn_"))
6623 aerr("dupe label '%s' vs '%s'?\n", name, g_labels[i]);
6624 g_labels[i] = realloc(g_labels[i], len + 1);
6625 my_assert_not(g_labels[i], NULL);
6626 memcpy(g_labels[i], name, len);
6627 g_labels[i][len] = 0;
6636 static struct chunk_item *func_chunks;
6637 static int func_chunk_cnt;
6638 static int func_chunk_alloc;
6640 static void add_func_chunk(FILE *fasm, const char *name, int line)
6642 if (func_chunk_cnt >= func_chunk_alloc) {
6643 func_chunk_alloc *= 2;
6644 func_chunks = realloc(func_chunks,
6645 func_chunk_alloc * sizeof(func_chunks[0]));
6646 my_assert_not(func_chunks, NULL);
6648 func_chunks[func_chunk_cnt].fptr = ftell(fasm);
6649 func_chunks[func_chunk_cnt].name = strdup(name);
6650 func_chunks[func_chunk_cnt].asmln = line;
6654 static int cmp_chunks(const void *p1, const void *p2)
6656 const struct chunk_item *c1 = p1, *c2 = p2;
6657 return strcmp(c1->name, c2->name);
6660 static int cmpstringp(const void *p1, const void *p2)
6662 return strcmp(*(char * const *)p1, *(char * const *)p2);
6665 static void scan_ahead(FILE *fasm)
6675 oldpos = ftell(fasm);
6678 while (my_fgets(line, sizeof(line), fasm))
6689 // get rid of random tabs
6690 for (i = 0; line[i] != 0; i++)
6691 if (line[i] == '\t')
6694 if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
6697 next_word(words[0], sizeof(words[0]), p);
6698 if (words[0][0] == 0)
6699 aerr("missing name for func chunk?\n");
6701 add_func_chunk(fasm, words[0], asmln);
6703 else if (IS_START(p, "; sctend"))
6709 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
6710 words[wordc][0] = 0;
6711 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
6712 if (*p == 0 || *p == ';') {
6718 if (wordc == 2 && IS(words[1], "ends"))
6722 fseek(fasm, oldpos, SEEK_SET);
6726 int main(int argc, char *argv[])
6728 FILE *fout, *fasm, *frlist;
6729 struct parsed_data *pd = NULL;
6731 char **rlist = NULL;
6733 int rlist_alloc = 0;
6734 int func_chunks_used = 0;
6735 int func_chunks_sorted = 0;
6736 int func_chunk_i = -1;
6737 long func_chunk_ret = 0;
6738 int func_chunk_ret_ln = 0;
6739 int scanned_ahead = 0;
6741 char words[20][256];
6742 enum opr_lenmod lmod;
6743 char *sctproto = NULL;
6745 int pending_endp = 0;
6747 int skip_warned = 0;
6760 for (arg = 1; arg < argc; arg++) {
6761 if (IS(argv[arg], "-v"))
6763 else if (IS(argv[arg], "-rf"))
6764 g_allow_regfunc = 1;
6765 else if (IS(argv[arg], "-m"))
6767 else if (IS(argv[arg], "-hdr"))
6768 g_header_mode = g_quiet_pp = g_allow_regfunc = 1;
6773 if (argc < arg + 3) {
6774 printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdr.h> [rlist]*\n"
6775 "%s -hdr <out.h> <.asm> <seed.h> [rlist]*\n"
6777 " -hdr - header generation mode\n"
6778 " -rf - allow unannotated indirect calls\n"
6779 " -m - allow multiple .text sections\n"
6780 "[rlist] is a file with function names to skip,"
6788 asmfn = argv[arg++];
6789 fasm = fopen(asmfn, "r");
6790 my_assert_not(fasm, NULL);
6792 hdrfn = argv[arg++];
6793 g_fhdr = fopen(hdrfn, "r");
6794 my_assert_not(g_fhdr, NULL);
6797 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
6798 my_assert_not(rlist, NULL);
6799 // needs special handling..
6800 rlist[rlist_len++] = "__alloca_probe";
6802 func_chunk_alloc = 32;
6803 func_chunks = malloc(func_chunk_alloc * sizeof(func_chunks[0]));
6804 my_assert_not(func_chunks, NULL);
6806 memset(words, 0, sizeof(words));
6808 for (; arg < argc; arg++) {
6809 frlist = fopen(argv[arg], "r");
6810 my_assert_not(frlist, NULL);
6812 while (my_fgets(line, sizeof(line), frlist)) {
6814 if (*p == 0 || *p == ';')
6817 if (IS_START(p, "#if 0")
6818 || (g_allow_regfunc && IS_START(p, "#if NO_REGFUNC")))
6822 else if (IS_START(p, "#endif"))
6829 p = next_word(words[0], sizeof(words[0]), p);
6830 if (words[0][0] == 0)
6833 if (rlist_len >= rlist_alloc) {
6834 rlist_alloc = rlist_alloc * 2 + 64;
6835 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
6836 my_assert_not(rlist, NULL);
6838 rlist[rlist_len++] = strdup(words[0]);
6847 qsort(rlist, rlist_len, sizeof(rlist[0]), cmpstringp);
6849 fout = fopen(argv[arg_out], "w");
6850 my_assert_not(fout, NULL);
6853 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
6854 my_assert_not(g_eqs, NULL);
6856 for (i = 0; i < ARRAY_SIZE(g_label_refs); i++) {
6857 g_label_refs[i].i = -1;
6858 g_label_refs[i].next = NULL;
6862 scan_variables(fasm);
6864 while (my_fgets(line, sizeof(line), fasm))
6873 // get rid of random tabs
6874 for (i = 0; line[i] != 0; i++)
6875 if (line[i] == '\t')
6880 if (p[2] == '=' && IS_START(p, "; =============== S U B"))
6881 goto do_pending_endp; // eww..
6883 if (p[2] == 'A' && IS_START(p, "; Attributes:"))
6885 static const char *attrs[] = {
6894 // parse IDA's attribute-list comment
6895 g_ida_func_attr = 0;
6898 for (; *p != 0; p = sskip(p)) {
6899 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
6900 if (!strncmp(p, attrs[i], strlen(attrs[i]))) {
6901 g_ida_func_attr |= 1 << i;
6902 p += strlen(attrs[i]);
6906 if (i == ARRAY_SIZE(attrs)) {
6907 anote("unparsed IDA attr: %s\n", p);
6910 if (IS(attrs[i], "fpd=")) {
6911 p = next_word(words[0], sizeof(words[0]), p);
6916 else if (p[2] == 'S' && IS_START(p, "; START OF FUNCTION CHUNK FOR "))
6919 next_word(words[0], sizeof(words[0]), p);
6920 if (words[0][0] == 0)
6921 aerr("missing name for func chunk?\n");
6923 if (!scanned_ahead) {
6924 add_func_chunk(fasm, words[0], asmln);
6925 func_chunks_sorted = 0;
6928 else if (p[2] == 'E' && IS_START(p, "; END OF FUNCTION CHUNK"))
6930 if (func_chunk_i >= 0) {
6931 if (func_chunk_i < func_chunk_cnt
6932 && IS(func_chunks[func_chunk_i].name, g_func))
6934 // move on to next chunk
6935 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
6937 aerr("seek failed for '%s' chunk #%d\n",
6938 g_func, func_chunk_i);
6939 asmln = func_chunks[func_chunk_i].asmln;
6943 if (func_chunk_ret == 0)
6944 aerr("no return from chunk?\n");
6945 fseek(fasm, func_chunk_ret, SEEK_SET);
6946 asmln = func_chunk_ret_ln;
6952 else if (p[2] == 'F' && IS_START(p, "; FUNCTION CHUNK AT ")) {
6953 func_chunks_used = 1;
6955 if (IS_START(g_func, "sub_")) {
6956 unsigned long addr = strtoul(p, NULL, 16);
6957 unsigned long f_addr = strtoul(g_func + 4, NULL, 16);
6958 if (addr > f_addr && !scanned_ahead) {
6959 anote("scan_ahead caused by '%s', addr %lx\n",
6963 func_chunks_sorted = 0;
6971 for (i = wordc; i < ARRAY_SIZE(words); i++)
6973 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
6974 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
6975 if (*p == 0 || *p == ';') {
6980 if (*p != 0 && *p != ';')
6981 aerr("too many words\n");
6983 // alow asm patches in comments
6985 if (IS_START(p, "; sctpatch:")) {
6987 if (*p == 0 || *p == ';')
6989 goto parse_words; // lame
6991 if (IS_START(p, "; sctproto:")) {
6992 sctproto = strdup(p + 11);
6994 else if (IS_START(p, "; sctend")) {
7003 awarn("wordc == 0?\n");
7007 // don't care about this:
7008 if (words[0][0] == '.'
7009 || IS(words[0], "include")
7010 || IS(words[0], "assume") || IS(words[1], "segment")
7011 || IS(words[0], "align"))
7017 // do delayed endp processing to collect switch jumptables
7019 if (in_func && !g_skip_func && !end && wordc >= 2
7020 && ((words[0][0] == 'd' && words[0][2] == 0)
7021 || (words[1][0] == 'd' && words[1][2] == 0)))
7024 if (words[1][0] == 'd' && words[1][2] == 0) {
7026 if (g_func_pd_cnt >= pd_alloc) {
7027 pd_alloc = pd_alloc * 2 + 16;
7028 g_func_pd = realloc(g_func_pd,
7029 sizeof(g_func_pd[0]) * pd_alloc);
7030 my_assert_not(g_func_pd, NULL);
7032 pd = &g_func_pd[g_func_pd_cnt];
7034 memset(pd, 0, sizeof(*pd));
7035 strcpy(pd->label, words[0]);
7036 pd->type = OPT_CONST;
7037 pd->lmod = lmod_from_directive(words[1]);
7043 anote("skipping alignment byte?\n");
7046 lmod = lmod_from_directive(words[0]);
7047 if (lmod != pd->lmod)
7048 aerr("lmod change? %d->%d\n", pd->lmod, lmod);
7051 if (pd->count_alloc < pd->count + wordc) {
7052 pd->count_alloc = pd->count_alloc * 2 + 14 + wordc;
7053 pd->d = realloc(pd->d, sizeof(pd->d[0]) * pd->count_alloc);
7054 my_assert_not(pd->d, NULL);
7056 for (; i < wordc; i++) {
7057 if (IS(words[i], "offset")) {
7058 pd->type = OPT_OFFSET;
7061 p = strchr(words[i], ',');
7064 if (pd->type == OPT_OFFSET)
7065 pd->d[pd->count].u.label = strdup(words[i]);
7067 pd->d[pd->count].u.val = parse_number(words[i]);
7068 pd->d[pd->count].bt_i = -1;
7074 if (in_func && !g_skip_func) {
7076 gen_hdr(g_func, pi);
7078 gen_func(fout, g_fhdr, g_func, pi);
7083 g_ida_func_attr = 0;
7087 func_chunks_used = 0;
7090 memset(&ops, 0, pi * sizeof(ops[0]));
7095 for (i = 0; i < g_func_pd_cnt; i++) {
7097 if (pd->type == OPT_OFFSET) {
7098 for (j = 0; j < pd->count; j++)
7099 free(pd->d[j].u.label);
7113 if (IS(words[1], "proc")) {
7115 aerr("proc '%s' while in_func '%s'?\n",
7118 if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp))
7120 strcpy(g_func, words[0]);
7121 set_label(0, words[0]);
7126 if (IS(words[1], "endp"))
7129 aerr("endp '%s' while not in_func?\n", words[0]);
7130 if (!IS(g_func, words[0]))
7131 aerr("endp '%s' while in_func '%s'?\n",
7134 if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1
7135 && ops[0].op == OP_JMP && ops[0].operand[0].had_ds)
7141 if (!g_skip_func && func_chunks_used) {
7142 // start processing chunks
7143 struct chunk_item *ci, key = { g_func, 0 };
7145 func_chunk_ret = ftell(fasm);
7146 func_chunk_ret_ln = asmln;
7147 if (!func_chunks_sorted) {
7148 qsort(func_chunks, func_chunk_cnt,
7149 sizeof(func_chunks[0]), cmp_chunks);
7150 func_chunks_sorted = 1;
7152 ci = bsearch(&key, func_chunks, func_chunk_cnt,
7153 sizeof(func_chunks[0]), cmp_chunks);
7155 aerr("'%s' needs chunks, but none found\n", g_func);
7156 func_chunk_i = ci - func_chunks;
7157 for (; func_chunk_i > 0; func_chunk_i--)
7158 if (!IS(func_chunks[func_chunk_i - 1].name, g_func))
7161 ret = fseek(fasm, func_chunks[func_chunk_i].fptr, SEEK_SET);
7163 aerr("seek failed for '%s' chunk #%d\n", g_func, func_chunk_i);
7164 asmln = func_chunks[func_chunk_i].asmln;
7172 if (wordc == 2 && IS(words[1], "ends")) {
7176 goto do_pending_endp;
7180 // scan for next text segment
7181 while (my_fgets(line, sizeof(line), fasm)) {
7184 if (*p == 0 || *p == ';')
7187 if (strstr(p, "segment para public 'CODE' use32"))
7194 p = strchr(words[0], ':');
7196 set_label(pi, words[0]);
7200 if (!in_func || g_skip_func) {
7201 if (!skip_warned && !g_skip_func && g_labels[pi] != NULL) {
7203 anote("skipping from '%s'\n", g_labels[pi]);
7207 g_labels[pi] = NULL;
7211 if (wordc > 1 && IS(words[1], "="))
7214 aerr("unhandled equ, wc=%d\n", wordc);
7215 if (g_eqcnt >= eq_alloc) {
7217 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
7218 my_assert_not(g_eqs, NULL);
7221 len = strlen(words[0]);
7222 if (len > sizeof(g_eqs[0].name) - 1)
7223 aerr("equ name too long: %d\n", len);
7224 strcpy(g_eqs[g_eqcnt].name, words[0]);
7226 if (!IS(words[3], "ptr"))
7227 aerr("unhandled equ\n");
7228 if (IS(words[2], "dword"))
7229 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
7230 else if (IS(words[2], "word"))
7231 g_eqs[g_eqcnt].lmod = OPLM_WORD;
7232 else if (IS(words[2], "byte"))
7233 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
7234 else if (IS(words[2], "qword"))
7235 g_eqs[g_eqcnt].lmod = OPLM_QWORD;
7237 aerr("bad lmod: '%s'\n", words[2]);
7239 g_eqs[g_eqcnt].offset = parse_number(words[4]);
7244 if (pi >= ARRAY_SIZE(ops))
7245 aerr("too many ops\n");
7247 parse_op(&ops[pi], words, wordc);
7249 if (sctproto != NULL) {
7250 if (ops[pi].op == OP_CALL || ops[pi].op == OP_JMP)
7251 ops[pi].datap = sctproto;
7267 // vim:ts=2:shiftwidth=2:expandtab