8 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
9 #define IS(w, y) !strcmp(w, y)
11 #include "protoparse.h"
16 #define anote(fmt, ...) \
17 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
18 #define awarn(fmt, ...) \
19 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__)
20 #define aerr(fmt, ...) do { \
21 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
26 OPF_RMD = (1 << 0), /* removed or optimized out */
27 OPF_DATA = (1 << 1), /* data processing - writes to dst opr */
28 OPF_FLAGS = (1 << 2), /* sets flags */
29 OPF_JMP = (1 << 3), /* branches, ret and call */
30 OPF_CC = (1 << 4), /* uses flags */
31 OPF_TAIL = (1 << 5), /* ret or tail call */
100 #define MAX_OPERANDS 3
104 enum opr_lenmod lmod;
112 struct parsed_opr operand[MAX_OPERANDS];
115 int regmask_src; // all referensed regs
117 int pfomask; // flagop: parsed_flag_op that can't be delayed
118 int argmask; // push: args that are altered before call
119 int cc_scratch; // scratch storage during analysis
120 int bt_i; // branch target (for branches)
121 struct parsed_op *lrl; // label reference list entry
126 // OP_CALL - ptr to parsed_proto
127 // (OPF_CC) - point to corresponding (OPF_FLAGS)
131 enum opr_lenmod lmod;
137 static struct parsed_op ops[MAX_OPS];
138 static struct parsed_equ *g_eqs;
140 static char g_labels[MAX_OPS][32];
141 static struct parsed_op *g_label_refs[MAX_OPS];
142 static struct parsed_proto g_func_pp;
143 static char g_func[256];
144 static char g_comment[256];
145 static int g_bp_frame;
146 static int g_bp_stack;
147 #define ferr(op_, fmt, ...) do { \
148 printf("error:%s:#%ld: '%s': " fmt, g_func, (op_) - ops, \
149 dump_op(op_), ##__VA_ARGS__); \
155 const char *regs_r32[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp" };
156 const char *regs_r16[] = { "ax", "bx", "cx", "dx", "si", "di", "bp", "sp" };
157 const char *regs_r8l[] = { "al", "bl", "cl", "dl" };
158 const char *regs_r8h[] = { "ah", "bh", "ch", "dh" };
160 enum x86_regs { xUNSPEC = -1, xAX, xBX, xCX, xDX, xSI, xDI, xBP, xSP };
162 // possible basic comparison types (without inversion)
163 enum parsed_flag_op {
167 PFO_BE, // 6 CF=1||ZF=1
171 PFO_LE, // e ZF=1||SF!=OF
174 static const char *parsed_flag_op_names[] = {
175 "o", "c", "z", "be", "s", "p", "l", "le"
178 static int char_array_i(const char *array[], size_t len, const char *s)
182 for (i = 0; i < len; i++)
189 static void printf_number(char *buf, size_t buf_size, long number)
191 // output in C-friendly form
192 snprintf(buf, buf_size, number < 10 ? "%lu" : "0x%02lx", number);
195 static int parse_reg(enum opr_lenmod *reg_lmod, const char *s)
199 reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s);
201 *reg_lmod = OPLM_DWORD;
204 reg = char_array_i(regs_r16, ARRAY_SIZE(regs_r16), s);
206 *reg_lmod = OPLM_WORD;
209 reg = char_array_i(regs_r8h, ARRAY_SIZE(regs_r8h), s);
211 *reg_lmod = OPLM_BYTE;
214 reg = char_array_i(regs_r8l, ARRAY_SIZE(regs_r8l), s);
216 *reg_lmod = OPLM_BYTE;
223 static long parse_number(const char *number)
225 int len = strlen(number);
226 const char *p = number;
236 if (len > 1 && *p == '0')
238 if (number[len - 1] == 'h') {
239 ret = strtol(p, &endp, 16);
240 bad = (*endp != 'h');
243 ret = strtol(p, &endp, 10);
247 aerr("number parsing failed\n");
253 static int parse_indmode(char *name, int *regmask, int need_c_cvt)
255 enum opr_lenmod lmod;
268 while (my_isblank(*s))
270 for (; my_issep(*s); d++, s++)
272 while (my_isblank(*s))
277 if (!strncmp(s, "ds:", 3))
280 s = next_idt(w, sizeof(w), s);
285 reg = parse_reg(&lmod, w);
287 *regmask |= 1 << reg;
291 if ('0' <= w[0] && w[0] <= '9') {
292 number = parse_number(w);
293 printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
297 // probably some label/identifier - pass
300 snprintf(d, sizeof(cvtbuf) - (d - cvtbuf), "%s", w);
304 strcpy(name, cvtbuf);
309 static int guess_lmod_from_name(struct parsed_opr *opr)
311 if (!strncmp(opr->name, "dword_", 6)) {
312 opr->lmod = OPLM_DWORD;
315 if (!strncmp(opr->name, "word_", 5)) {
316 opr->lmod = OPLM_WORD;
319 if (!strncmp(opr->name, "byte_", 5)) {
320 opr->lmod = OPLM_BYTE;
326 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
332 *regmask |= 1 << reg;
335 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name);
337 static int parse_operand(struct parsed_opr *opr,
338 int *regmask, int *regmask_indirect,
339 char words[16][256], int wordc, int w, unsigned int op_flags)
341 enum opr_lenmod tmplmod;
347 aerr("parse_operand w %d, wordc %d\n", w, wordc);
351 for (i = w; i < wordc; i++) {
352 len = strlen(words[i]);
353 if (words[i][len - 1] == ',') {
354 words[i][len - 1] = 0;
360 if (op_flags & OPF_JMP) {
363 if (wordc - w == 3 && IS(words[w + 1], "ptr"))
364 label = words[w + 2];
365 else if (wordc - w == 2 && IS(words[w], "short"))
366 label = words[w + 1];
367 else if (wordc - w == 1)
370 aerr("jump parse error");
372 opr->type = OPT_LABEL;
373 strcpy(opr->name, label);
377 if (wordc - w >= 3) {
378 if (IS(words[w + 1], "ptr")) {
379 if (IS(words[w], "dword"))
380 opr->lmod = OPLM_DWORD;
381 else if (IS(words[w], "word"))
382 opr->lmod = OPLM_WORD;
383 else if (IS(words[w], "byte"))
384 opr->lmod = OPLM_BYTE;
386 aerr("type parsing failed\n");
391 if (wordc - w == 2 && IS(words[w], "offset")) {
392 opr->type = OPT_OFFSET;
393 strcpy(opr->name, words[w + 1]);
398 aerr("parse_operand 1 word expected\n");
400 strcpy(opr->name, words[w]);
402 if (words[w][0] == '[') {
403 opr->type = OPT_REGMEM;
404 ret = sscanf(words[w], "[%[^]]]", opr->name);
406 aerr("[] parse failure\n");
408 parse_indmode(opr->name, regmask_indirect, 1);
409 if (opr->lmod == OPLM_UNSPEC && !strncmp(opr->name, "ebp+", 4)) {
411 struct parsed_equ *eq = equ_find(NULL, opr->name + 4);
413 opr->lmod = eq->lmod;
417 else if (strchr(words[w], '[')) {
419 opr->type = OPT_REGMEM;
420 if (opr->lmod == OPLM_UNSPEC)
421 guess_lmod_from_name(opr);
422 parse_indmode(strchr(words[w], '['), regmask_indirect, 0);
425 else if (('0' <= words[w][0] && words[w][0] <= '9')
426 || words[w][0] == '-')
428 number = parse_number(words[w]);
429 opr->type = OPT_CONST;
431 printf_number(opr->name, sizeof(opr->name), number);
435 ret = parse_reg(&tmplmod, opr->name);
437 setup_reg_opr(opr, ret, tmplmod, regmask);
441 // most likely var in data segment
442 opr->type = OPT_LABEL;
443 if (opr->lmod == OPLM_UNSPEC)
444 guess_lmod_from_name(opr);
445 if (opr->lmod != OPLM_UNSPEC)
448 // TODO: scan data seg to determine type?
452 static const struct {
459 { "push", OP_PUSH, 1, 1, 0 },
460 { "pop", OP_POP, 1, 1, OPF_DATA },
461 { "mov" , OP_MOV, 2, 2, OPF_DATA },
462 { "lea", OP_LEA, 2, 2, OPF_DATA },
463 { "movzx",OP_MOVZX, 2, 2, OPF_DATA },
464 { "movsx",OP_MOVSX, 2, 2, OPF_DATA },
465 { "not", OP_NOT, 1, 1, OPF_DATA },
466 { "cdq", OP_CDQ, 0, 0, OPF_DATA },
467 { "add", OP_ADD, 2, 2, OPF_DATA|OPF_FLAGS },
468 { "sub", OP_SUB, 2, 2, OPF_DATA|OPF_FLAGS },
469 { "and", OP_AND, 2, 2, OPF_DATA|OPF_FLAGS },
470 { "or", OP_OR, 2, 2, OPF_DATA|OPF_FLAGS },
471 { "xor", OP_XOR, 2, 2, OPF_DATA|OPF_FLAGS },
472 { "shl", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
473 { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS },
474 { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS },
475 { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS },
476 { "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC },
477 { "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC },
478 { "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
479 { "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
480 { "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
481 { "mul", OP_MUL, 1, 1, OPF_DATA|OPF_FLAGS },
482 { "imul", OP_IMUL, 1, 3, OPF_DATA|OPF_FLAGS },
483 { "div", OP_DIV, 1, 1, OPF_DATA|OPF_FLAGS },
484 { "idiv", OP_IDIV, 1, 1, OPF_DATA|OPF_FLAGS },
485 { "test", OP_TEST, 2, 2, OPF_FLAGS },
486 { "cmp", OP_CMP, 2, 2, OPF_FLAGS },
487 { "retn", OP_RET, 0, 1, OPF_JMP|OPF_TAIL },
488 { "call", OP_CALL, 1, 1, OPF_JMP|OPF_FLAGS },
489 { "jmp", OP_JMP, 1, 1, OPF_JMP },
490 { "jo", OP_JO, 1, 1, OPF_JMP|OPF_CC }, // 70 OF=1
491 { "jno", OP_JNO, 1, 1, OPF_JMP|OPF_CC }, // 71 OF=0
492 { "jc", OP_JC, 1, 1, OPF_JMP|OPF_CC }, // 72 CF=1
493 { "jb", OP_JC, 1, 1, OPF_JMP|OPF_CC }, // 72
494 { "jnc", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73 CF=0
495 { "jae", OP_JNC, 1, 1, OPF_JMP|OPF_CC }, // 73
496 { "jz", OP_JZ, 1, 1, OPF_JMP|OPF_CC }, // 74 ZF=1
497 { "je", OP_JZ, 1, 1, OPF_JMP|OPF_CC }, // 74
498 { "jnz", OP_JNZ, 1, 1, OPF_JMP|OPF_CC }, // 75 ZF=0
499 { "jne", OP_JNZ, 1, 1, OPF_JMP|OPF_CC }, // 75
500 { "jbe", OP_JBE, 1, 1, OPF_JMP|OPF_CC }, // 76 CF=1 || ZF=1
501 { "jna", OP_JBE, 1, 1, OPF_JMP|OPF_CC }, // 76
502 { "ja", OP_JA, 1, 1, OPF_JMP|OPF_CC }, // 77 CF=0 && ZF=0
503 { "jnbe", OP_JA, 1, 1, OPF_JMP|OPF_CC }, // 77
504 { "js", OP_JS, 1, 1, OPF_JMP|OPF_CC }, // 78 SF=1
505 { "jns", OP_JNS, 1, 1, OPF_JMP|OPF_CC }, // 79 SF=0
506 { "jp", OP_JP, 1, 1, OPF_JMP|OPF_CC }, // 7a PF=1
507 { "jpe", OP_JP, 1, 1, OPF_JMP|OPF_CC }, // 7a
508 { "jnp", OP_JNP, 1, 1, OPF_JMP|OPF_CC }, // 7b PF=0
509 { "jpo", OP_JNP, 1, 1, OPF_JMP|OPF_CC }, // 7b
510 { "jl", OP_JL, 1, 1, OPF_JMP|OPF_CC }, // 7c SF!=OF
511 { "jnge", OP_JL, 1, 1, OPF_JMP|OPF_CC }, // 7c
512 { "jge", OP_JGE, 1, 1, OPF_JMP|OPF_CC }, // 7d SF=OF
513 { "jnl", OP_JGE, 1, 1, OPF_JMP|OPF_CC }, // 7d
514 { "jle", OP_JLE, 1, 1, OPF_JMP|OPF_CC }, // 7e ZF=1 || SF!=OF
515 { "jng", OP_JLE, 1, 1, OPF_JMP|OPF_CC }, // 7e
516 { "jg", OP_JG, 1, 1, OPF_JMP|OPF_CC }, // 7f ZF=0 && SF=OF
517 { "jnle", OP_JG, 1, 1, OPF_JMP|OPF_CC }, // 7f
518 { "seto", OP_JO, 1, 1, OPF_DATA|OPF_CC },
519 { "setno", OP_JNO, 1, 1, OPF_DATA|OPF_CC },
520 { "setc", OP_JC, 1, 1, OPF_DATA|OPF_CC },
521 { "setb", OP_JC, 1, 1, OPF_DATA|OPF_CC },
522 { "setnc", OP_JNC, 1, 1, OPF_DATA|OPF_CC },
523 { "setae", OP_JNC, 1, 1, OPF_DATA|OPF_CC },
524 { "setz", OP_JZ, 1, 1, OPF_DATA|OPF_CC },
525 { "sete", OP_JZ, 1, 1, OPF_DATA|OPF_CC },
526 { "setnz", OP_JNZ, 1, 1, OPF_DATA|OPF_CC },
527 { "setne", OP_JNZ, 1, 1, OPF_DATA|OPF_CC },
528 { "setbe", OP_JBE, 1, 1, OPF_DATA|OPF_CC },
529 { "setna", OP_JBE, 1, 1, OPF_DATA|OPF_CC },
530 { "seta", OP_JA, 1, 1, OPF_DATA|OPF_CC },
531 { "setnbe", OP_JA, 1, 1, OPF_DATA|OPF_CC },
532 { "sets", OP_JS, 1, 1, OPF_DATA|OPF_CC },
533 { "setns", OP_JNS, 1, 1, OPF_DATA|OPF_CC },
534 { "setp", OP_JP, 1, 1, OPF_DATA|OPF_CC },
535 { "setpe", OP_JP, 1, 1, OPF_DATA|OPF_CC },
536 { "setnp", OP_JNP, 1, 1, OPF_DATA|OPF_CC },
537 { "setpo", OP_JNP, 1, 1, OPF_DATA|OPF_CC },
538 { "setl", OP_JL, 1, 1, OPF_DATA|OPF_CC },
539 { "setnge", OP_JL, 1, 1, OPF_DATA|OPF_CC },
540 { "setge", OP_JGE, 1, 1, OPF_DATA|OPF_CC },
541 { "setnl", OP_JGE, 1, 1, OPF_DATA|OPF_CC },
542 { "setle", OP_JLE, 1, 1, OPF_DATA|OPF_CC },
543 { "setng", OP_JLE, 1, 1, OPF_DATA|OPF_CC },
544 { "setg", OP_JG, 1, 1, OPF_DATA|OPF_CC },
545 { "setnle", OP_JG, 1, 1, OPF_DATA|OPF_CC },
548 static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
556 for (i = 0; i < ARRAY_SIZE(op_table); i++) {
557 if (IS(words[0], op_table[i].name))
561 if (i == ARRAY_SIZE(op_table))
562 aerr("unhandled op: '%s'\n", words[0]);
564 op->op = op_table[i].op;
565 op->flags = op_table[i].flags;
566 op->regmask_src = op->regmask_dst = 0;
568 for (opr = 0; opr < op_table[i].minopr; opr++) {
569 regmask = regmask_ind = 0;
570 w = parse_operand(&op->operand[opr], ®mask, ®mask_ind,
571 words, wordc, w, op->flags);
573 if (opr == 0 && (op->flags & OPF_DATA))
574 op->regmask_dst = regmask;
575 // for now, mark dst as src too
576 op->regmask_src |= regmask | regmask_ind;
579 for (; w < wordc && opr < op_table[i].maxopr; opr++) {
580 w = parse_operand(&op->operand[opr],
581 &op->regmask_src, &op->regmask_src,
582 words, wordc, w, op->flags);
586 aerr("parse_op %s incomplete: %d/%d\n",
590 op->operand_cnt = opr;
591 if (!strncmp(op_table[i].name, "set", 3))
592 op->operand[0].lmod = OPLM_BYTE;
594 // ops with implicit argumets
598 setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst);
599 setup_reg_opr(&op->operand[1], xAX, OPLM_DWORD, &op->regmask_src);
603 if (op->operand_cnt != 1)
608 op->regmask_dst = (1 << xDX) | (1 << xAX);
609 op->regmask_src |= (1 << xAX);
610 if (op->operand[0].lmod == OPLM_UNSPEC)
611 op->operand[0].lmod = OPLM_DWORD;
616 // we could set up operands for edx:eax, but there is no real need to
617 // (see is_opr_modified())
618 regmask = (1 << xDX) | (1 << xAX);
619 op->regmask_dst = regmask;
620 op->regmask_src |= regmask;
621 if (op->operand[0].lmod == OPLM_UNSPEC)
622 op->operand[0].lmod = OPLM_DWORD;
628 if (op->operand[1].lmod == OPLM_UNSPEC)
629 op->operand[1].lmod = OPLM_BYTE;
637 static const char *op_name(enum op_op op)
641 for (i = 0; i < ARRAY_SIZE(op_table); i++)
642 if (op_table[i].op == op)
643 return op_table[i].name;
649 static const char *dump_op(struct parsed_op *po)
651 static char out[128];
655 snprintf(out, sizeof(out), "%s", op_name(po->op));
656 for (i = 0; i < po->operand_cnt; i++) {
660 snprintf(p, sizeof(out) - (p - out),
661 po->operand[i].type == OPT_REGMEM ? " [%s]" : " %s",
662 po->operand[i].name);
668 static const char *opr_name(struct parsed_op *po, int opr_num)
670 if (opr_num >= po->operand_cnt)
671 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
672 return po->operand[opr_num].name;
675 static unsigned int opr_const(struct parsed_op *po, int opr_num)
677 if (opr_num >= po->operand_cnt)
678 ferr(po, "opr OOR: %d/%d\n", opr_num, po->operand_cnt);
679 if (po->operand[opr_num].type != OPT_CONST)
680 ferr(po, "opr %d: const expected\n", opr_num);
681 return po->operand[opr_num].val;
684 static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
686 if ((unsigned int)popr->reg >= MAX_REGS)
687 ferr(po, "invalid reg: %d\n", popr->reg);
688 return regs_r32[popr->reg];
691 static struct parsed_equ *equ_find(struct parsed_op *po, const char *name)
695 for (i = 0; i < g_eqcnt; i++)
696 if (IS(g_eqs[i].name, name))
700 ferr(po, "unresolved equ name: '%s'\n", name);
707 static void bg_frame_access(struct parsed_op *po, enum opr_lenmod lmod,
708 char *buf, size_t buf_size, const char *bp_arg,
709 int is_src, int is_lea)
711 const char *prefix = "";
712 struct parsed_equ *eq;
716 snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
718 eq = equ_find(po, bp_arg);
720 if (eq->offset >= 0) {
721 arg_i = eq->offset / 4 - 2;
722 if (arg_i < 0 || arg_i >= g_func_pp.argc_stack)
723 ferr(po, "offset %d doesn't map to any arg\n", eq->offset);
725 for (i = arg_s = 0; i < g_func_pp.argc; i++) {
726 if (g_func_pp.arg[i].reg != NULL)
732 if (i == g_func_pp.argc)
733 ferr(po, "arg %d not in prototype?\n", arg_i);
735 ferr(po, "lea to arg?\n");
737 snprintf(buf, buf_size, "%sa%d", is_src ? "(u32)" : "", i + 1);
741 ferr(po, "bp_stack access after it was not detected\n");
743 sf_ofs = g_bp_stack + eq->offset;
745 ferr(po, "bp_stack offset %d/%d\n", eq->offset, g_bp_stack);
753 snprintf(buf, buf_size, "%ssf.b[%d]", prefix, sf_ofs);
756 snprintf(buf, buf_size, "%ssf.w[%d]", prefix, sf_ofs / 2);
759 snprintf(buf, buf_size, "%ssf.d[%d]", prefix, sf_ofs / 4);
762 ferr(po, "bp_stack bad lmod: %d\n", lmod);
767 static char *out_src_opr(char *buf, size_t buf_size,
768 struct parsed_op *po, struct parsed_opr *popr, int is_lea)
770 const char *cast = "";
771 char tmp1[256], tmp2[256];
775 switch (popr->type) {
778 ferr(po, "lea from reg?\n");
780 switch (popr->lmod) {
782 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
785 snprintf(buf, buf_size, "(u16)%s", opr_reg_p(po, popr));
788 if (popr->name[1] == 'h') // XXX..
789 snprintf(buf, buf_size, "(u8)(%s >> 8)", opr_reg_p(po, popr));
791 snprintf(buf, buf_size, "(u8)%s", opr_reg_p(po, popr));
794 ferr(po, "invalid src lmod: %d\n", popr->lmod);
799 if (g_bp_frame && !strncmp(popr->name, "ebp+", 4)) {
800 bg_frame_access(po, popr->lmod, buf, buf_size,
801 popr->name + 4, 1, is_lea);
805 strcpy(expr, popr->name);
806 if (strchr(expr, '[')) {
807 // special case: '[' can only be left for label[reg] form
808 ret = sscanf(expr, "%[^[][%[^]]]", tmp1, tmp2);
810 ferr(po, "parse failure for '%s'\n", expr);
811 snprintf(expr, sizeof(expr), "(u32)%s + %s", tmp1, tmp2);
814 // XXX: do we need more parsing?
816 snprintf(buf, buf_size, "%s", expr);
820 switch (popr->lmod) {
831 ferr(po, "invalid lmod: %d\n", popr->lmod);
833 snprintf(buf, buf_size, "%s(%s)", cast, expr);
838 snprintf(buf, buf_size, "(u32)&%s", popr->name);
840 snprintf(buf, buf_size, "(u32)%s", popr->name);
845 ferr(po, "lea an offset?\n");
846 snprintf(buf, buf_size, "(u32)&%s", popr->name);
851 ferr(po, "lea from const?\n");
853 printf_number(buf, buf_size, popr->val);
857 ferr(po, "invalid src type: %d\n", popr->type);
863 static char *out_dst_opr(char *buf, size_t buf_size,
864 struct parsed_op *po, struct parsed_opr *popr)
866 switch (popr->type) {
868 switch (popr->lmod) {
870 snprintf(buf, buf_size, "%s", opr_reg_p(po, popr));
874 snprintf(buf, buf_size, "LOWORD(%s)", opr_reg_p(po, popr));
878 if (popr->name[1] == 'h') // XXX..
879 snprintf(buf, buf_size, "BYTE1(%s)", opr_reg_p(po, popr));
881 snprintf(buf, buf_size, "LOBYTE(%s)", opr_reg_p(po, popr));
884 ferr(po, "invalid dst lmod: %d\n", popr->lmod);
889 if (g_bp_frame && !strncmp(popr->name, "ebp+", 4)) {
890 bg_frame_access(po, popr->lmod, buf, buf_size,
891 popr->name + 4, 0, 0);
895 return out_src_opr(buf, buf_size, po, popr, 0);
898 ferr(po, "invalid dst type: %d\n", popr->type);
904 static const char *lmod_cast_u(struct parsed_op *po,
905 enum opr_lenmod lmod)
915 ferr(po, "invalid lmod: %d\n", lmod);
916 return "(_invalid_)";
920 static const char *lmod_cast_s(struct parsed_op *po,
921 enum opr_lenmod lmod)
931 ferr(po, "invalid lmod: %d\n", lmod);
932 return "(_invalid_)";
936 static const char *lmod_cast(struct parsed_op *po,
937 enum opr_lenmod lmod, int is_signed)
940 lmod_cast_s(po, lmod) :
941 lmod_cast_u(po, lmod);
944 static enum parsed_flag_op split_cond(struct parsed_op *po,
945 enum op_op op, int *is_inv)
997 ferr(po, "split_cond: bad op %d\n", op);
1002 static void out_test_for_cc(char *buf, size_t buf_size,
1003 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1004 enum opr_lenmod lmod, const char *expr)
1006 const char *cast, *scast;
1008 cast = lmod_cast_u(po, lmod);
1009 scast = lmod_cast_s(po, lmod);
1013 case PFO_BE: // CF=1||ZF=1; CF=0
1014 snprintf(buf, buf_size, "(%s%s %s 0)",
1015 cast, expr, is_inv ? "!=" : "==");
1019 case PFO_L: // SF!=OF; OF=0
1020 snprintf(buf, buf_size, "(%s%s %s 0)",
1021 scast, expr, is_inv ? ">=" : "<");
1024 case PFO_LE: // ZF=1||SF!=OF; OF=0
1025 snprintf(buf, buf_size, "(%s%s %s 0)",
1026 scast, expr, is_inv ? ">" : "<=");
1030 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1034 static void out_cmp_for_cc(char *buf, size_t buf_size,
1035 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv,
1036 enum opr_lenmod lmod, const char *expr1, const char *expr2)
1038 const char *cast, *scast;
1040 cast = lmod_cast_u(po, lmod);
1041 scast = lmod_cast_s(po, lmod);
1045 // note: must be unsigned compare
1046 snprintf(buf, buf_size, "(%s%s %s %s%s)",
1047 cast, expr1, is_inv ? ">=" : "<", cast, expr2);
1051 snprintf(buf, buf_size, "(%s%s %s %s%s)",
1052 cast, expr1, is_inv ? "!=" : "==", cast, expr2);
1056 // note: must be unsigned compare
1057 snprintf(buf, buf_size, "(%s%s %s %s%s)",
1058 cast, expr1, is_inv ? ">" : "<=", cast, expr2);
1061 // note: must be signed compare
1063 snprintf(buf, buf_size, "(%s(%s - %s) %s 0)",
1064 scast, expr1, expr2, is_inv ? ">=" : "<");
1068 snprintf(buf, buf_size, "(%s%s %s %s%s)",
1069 scast, expr1, is_inv ? ">=" : "<", scast, expr2);
1073 snprintf(buf, buf_size, "(%s%s %s %s%s)",
1074 scast, expr1, is_inv ? ">" : "<=", scast, expr2);
1078 ferr(po, "%s: unhandled parsed_flag_op: %d\n", __func__, pfo);
1082 static void out_cmp_test(char *buf, size_t buf_size,
1083 struct parsed_op *po, enum parsed_flag_op pfo, int is_inv)
1085 char buf1[256], buf2[256], buf3[256];
1087 if (po->op == OP_TEST) {
1088 if (IS(opr_name(po, 0), opr_name(po, 1))) {
1089 out_src_opr(buf3, sizeof(buf3), po, &po->operand[0], 0);
1092 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], 0);
1093 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0);
1094 snprintf(buf3, sizeof(buf3), "(%s & %s)", buf1, buf2);
1096 out_test_for_cc(buf, buf_size, po, pfo, is_inv,
1097 po->operand[0].lmod, buf3);
1099 else if (po->op == OP_CMP) {
1100 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0], 0);
1101 out_src_opr(buf3, sizeof(buf3), po, &po->operand[1], 0);
1102 out_cmp_for_cc(buf, buf_size, po, pfo, is_inv,
1103 po->operand[0].lmod, buf2, buf3);
1106 ferr(po, "%s: unhandled op: %d\n", __func__, po->op);
1109 static void propagate_lmod(struct parsed_op *po, struct parsed_opr *popr1,
1110 struct parsed_opr *popr2)
1112 if (popr1->lmod == OPLM_UNSPEC && popr2->lmod == OPLM_UNSPEC)
1113 ferr(po, "missing lmod for both operands\n");
1115 if (popr1->lmod == OPLM_UNSPEC)
1116 popr1->lmod = popr2->lmod;
1117 else if (popr2->lmod == OPLM_UNSPEC)
1118 popr2->lmod = popr1->lmod;
1119 else if (popr1->lmod != popr2->lmod)
1120 ferr(po, "conflicting lmods: %d vs %d\n", popr1->lmod, popr2->lmod);
1123 static const char *op_to_c(struct parsed_op *po)
1147 ferr(po, "op_to_c was supplied with %d\n", po->op);
1151 static int scan_for_pop(int i, int opcnt, const char *reg,
1152 int magic, int do_patch)
1154 struct parsed_op *po;
1157 for (; i < opcnt; i++) {
1159 if (po->cc_scratch == magic)
1160 break; // already checked
1161 po->cc_scratch = magic;
1163 if (po->flags & OPF_TAIL)
1164 return -1; // deadend
1166 if (po->flags & OPF_RMD)
1169 if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
1171 ferr(po, "dead branch\n");
1175 if (po->flags & OPF_CC) {
1176 ret |= scan_for_pop(po->bt_i, opcnt, reg, magic, do_patch);
1178 return ret; // dead end
1186 if (po->op == OP_POP && po->operand[0].type == OPT_REG
1187 && IS(po->operand[0].name, reg))
1190 po->flags |= OPF_RMD;
1198 // scan for pop starting from 'ret' op (all paths)
1199 static int scan_for_pop_ret(int i, int opcnt, const char *reg, int do_patch)
1204 for (; i < opcnt; i++) {
1205 if (!(ops[i].flags & OPF_TAIL))
1208 for (j = i - 1; j >= 0; j--) {
1209 if (ops[j].flags & OPF_RMD)
1211 if (ops[j].flags & OPF_JMP)
1214 if (ops[j].op == OP_POP && ops[j].operand[0].type == OPT_REG
1215 && IS(ops[j].operand[0].name, reg))
1219 ops[j].flags |= OPF_RMD;
1223 if (g_labels[j][0] != 0)
1228 return found ? 0 : -1;
1231 // is operand 'opr modified' by parsed_op 'po'?
1232 static int is_opr_modified(const struct parsed_opr *opr,
1233 const struct parsed_op *po)
1235 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
1238 if (opr->type == OPT_REG && po->operand[0].type == OPT_REG) {
1239 if (po->regmask_dst & (1 << opr->reg))
1245 return IS(po->operand[0].name, opr->name);
1248 // is any operand of parsed_op 'po_test' modified by parsed_op 'po'?
1249 static int is_any_opr_modified(const struct parsed_op *po_test,
1250 const struct parsed_op *po)
1254 if ((po->flags & OPF_RMD) || !(po->flags & OPF_DATA))
1257 if (po_test->regmask_src & po->regmask_dst)
1260 for (i = 0; i < po_test->operand_cnt; i++)
1261 if (IS(po_test->operand[i].name, po->operand[0].name))
1267 // scan for any po_test operand modification in range given
1268 static int scan_for_mod(struct parsed_op *po_test, int i, int opcnt)
1270 for (; i < opcnt; i++) {
1271 if (is_any_opr_modified(po_test, &ops[i]))
1278 // scan for po_test operand[0] modification in range given
1279 static int scan_for_mod_opr0(struct parsed_op *po_test,
1282 for (; i < opcnt; i++) {
1283 if (is_opr_modified(&po_test->operand[0], &ops[i]))
1290 static int scan_for_flag_set(int i)
1292 for (; i >= 0; i--) {
1293 if (ops[i].flags & OPF_FLAGS)
1296 if ((ops[i].flags & OPF_JMP) && !(ops[i].flags & OPF_CC))
1298 if (g_labels[i][0] != 0)
1305 // scan back for cdq, if anything modifies edx, fail
1306 static int scan_for_cdq_edx(int i)
1308 for (; i >= 0; i--) {
1309 if (ops[i].op == OP_CDQ)
1312 if (ops[i].regmask_dst & (1 << xDX))
1314 if (g_labels[i][0] != 0)
1321 static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
1323 struct parsed_op *po, *delayed_flag_op = NULL, *tmp_op;
1324 struct parsed_opr *last_arith_dst = NULL;
1325 char buf1[256], buf2[256], buf3[256];
1326 struct parsed_proto *pp, *pp_tmp;
1327 const char *tmpname;
1328 enum parsed_flag_op pfo;
1329 int save_arg_vars = 0;
1330 int cmp_result_vars = 0;
1331 int need_mul_var = 0;
1333 int regmask_arg = 0;
1343 g_bp_frame = g_bp_stack = 0;
1345 ret = proto_parse(fhdr, funcn, &g_func_pp);
1347 ferr(ops, "proto_parse failed for '%s'\n", funcn);
1349 fprintf(fout, "%s %s(", g_func_pp.ret_type, funcn);
1350 for (i = 0; i < g_func_pp.argc; i++) {
1352 fprintf(fout, ", ");
1353 fprintf(fout, "%s a%d", g_func_pp.arg[i].type, i + 1);
1355 fprintf(fout, ")\n{\n");
1358 // - handle ebp frame, remove ops related to it
1359 if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
1360 && ops[1].op == OP_MOV
1361 && IS(opr_name(&ops[1], 0), "ebp")
1362 && IS(opr_name(&ops[1], 1), "esp"))
1367 ops[0].flags |= OPF_RMD;
1368 ops[1].flags |= OPF_RMD;
1370 if (ops[2].op == OP_SUB && IS(opr_name(&ops[2], 0), "esp")) {
1371 g_bp_stack = opr_const(&ops[2], 1);
1372 ops[2].flags |= OPF_RMD;
1375 // another way msvc builds stack frame..
1377 while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
1379 ops[i].flags |= OPF_RMD;
1387 for (; i < opcnt; i++)
1388 if (ops[i].op == OP_RET)
1390 if (ops[i - 1].op != OP_POP || !IS(opr_name(&ops[i - 1], 0), "ebp"))
1391 ferr(&ops[i - 1], "'pop ebp' expected\n");
1392 ops[i - 1].flags |= OPF_RMD;
1394 if (g_bp_stack != 0) {
1395 if (ops[i - 2].op != OP_MOV
1396 || !IS(opr_name(&ops[i - 2], 0), "esp")
1397 || !IS(opr_name(&ops[i - 2], 1), "ebp"))
1399 ferr(&ops[i - 2], "esp restore expected\n");
1401 ops[i - 2].flags |= OPF_RMD;
1403 if (ecx_push && ops[i - 3].op == OP_POP
1404 && IS(opr_name(&ops[i - 3], 0), "ecx"))
1406 ferr(&ops[i - 3], "unexpected ecx pop\n");
1411 } while (i < opcnt);
1415 // - resolve all branches
1416 for (i = 0; i < opcnt; i++) {
1420 if ((po->flags & OPF_RMD) || !(po->flags & OPF_JMP)
1421 || po->op == OP_CALL || po->op == OP_RET)
1424 for (j = 0; j < opcnt; j++) {
1425 if (g_labels[j][0] && IS(po->operand[0].name, g_labels[j])) {
1427 po->lrl = g_label_refs[j];
1428 g_label_refs[j] = po;
1433 if (po->bt_i == -1) {
1436 po->flags |= OPF_TAIL;
1441 // - find POPs for PUSHes, rm both
1442 // - scan for all used registers
1443 // - find flag set ops for their users
1445 for (i = 0; i < opcnt; i++) {
1447 if (po->flags & OPF_RMD)
1450 if (po->op == OP_PUSH && po->operand[0].type == OPT_REG) {
1451 if (po->operand[0].reg < 0)
1452 ferr(po, "reg not set for push?\n");
1453 if (!(regmask & (1 << po->operand[0].reg))) { // reg save
1454 ret = scan_for_pop(i + 1, opcnt, po->operand[0].name, i + 1, 0);
1456 po->flags |= OPF_RMD;
1457 scan_for_pop(i + 1, opcnt, po->operand[0].name, i + 2, 1);
1460 ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 0);
1462 po->flags |= OPF_RMD;
1463 scan_for_pop_ret(i + 1, opcnt, po->operand[0].name, 1);
1469 regmask |= po->regmask_src | po->regmask_dst;
1471 if (po->flags & OPF_CC)
1473 ret = scan_for_flag_set(i - 1);
1475 ferr(po, "unable to trace flag setter\n");
1477 tmp_op = &ops[ret]; // flag setter
1478 pfo = split_cond(po, po->op, &dummy);
1481 // to get nicer code, we try to delay test and cmp;
1482 // if we can't because of operand modification, or if we
1483 // have math op, make it calculate flags explicitly
1484 if (tmp_op->op == OP_TEST || tmp_op->op == OP_CMP) {
1485 if (scan_for_mod(tmp_op, ret + 1, i) >= 0)
1489 if ((pfo != PFO_Z && pfo != PFO_S && pfo != PFO_P)
1490 || scan_for_mod_opr0(tmp_op, ret + 1, i) >= 0)
1494 tmp_op->pfomask |= pfomask;
1495 cmp_result_vars |= pfomask;
1499 if (po->op == OP_ADC || po->op == OP_SBB)
1500 cmp_result_vars |= 1 << PFO_C;
1502 else if (po->op == OP_CALL)
1504 pp = malloc(sizeof(*pp));
1505 my_assert_not(pp, NULL);
1506 tmpname = opr_name(&ops[i], 0);
1507 ret = proto_parse(fhdr, tmpname, pp);
1509 ferr(po, "proto_parse failed for '%s'\n", tmpname);
1511 for (arg = 0; arg < pp->argc; arg++)
1512 if (pp->arg[arg].reg == NULL)
1515 for (j = i - 1; j >= 0 && arg < pp->argc; j--) {
1516 if (ops[j].op == OP_CALL) {
1517 pp_tmp = ops[j].datap;
1519 ferr(po, "arg collect hit unparsed call\n");
1520 if (pp_tmp->argc_stack > 0)
1521 ferr(po, "arg collect hit '%s' with %d stack args\n",
1522 opr_name(&ops[j], 0), pp_tmp->argc_stack);
1524 else if ((ops[j].flags & OPF_TAIL)
1525 || (ops[j].flags & (OPF_JMP|OPF_CC)) == OPF_JMP)
1530 if (ops[j].op == OP_PUSH) {
1531 pp->arg[arg].datap = &ops[j];
1532 ret = scan_for_mod(&ops[j], j + 1, i);
1534 // mark this push as one that needs operand saving
1535 ops[j].argmask |= 1 << arg;
1536 save_arg_vars |= 1 << arg;
1539 ops[j].flags |= OPF_RMD;
1542 for (arg++; arg < pp->argc; arg++)
1543 if (pp->arg[arg].reg == NULL)
1547 if (g_labels[j][0] != 0) {
1548 if (j > 0 && ((ops[j - 1].flags & OPF_TAIL)
1549 || (ops[j - 1].flags & (OPF_JMP|OPF_CC)) == OPF_JMP))
1551 // follow the branch in reverse
1552 if (g_label_refs[j] == NULL)
1553 ferr(po, "no refs for '%s'?\n", g_labels[j]);
1554 if (g_label_refs[j]->lrl != NULL)
1555 ferr(po, "unhandled multiple fefs to '%s'\n", g_labels[j]);
1556 j = (g_label_refs[j] - ops) + 1;
1563 ferr(po, "arg collect failed for '%s'\n", tmpname);
1566 else if (po->op == OP_MUL
1567 || (po->op == OP_IMUL && po->operand_cnt == 1))
1573 // declare stack frame
1575 fprintf(fout, " union { u32 d[%d]; u16 w[%d]; u8 b[%d]; } sf;\n",
1576 (g_bp_stack + 3) / 4, (g_bp_stack + 1) / 2, g_bp_stack);
1578 // declare arg-registers
1579 for (i = 0; i < g_func_pp.argc; i++) {
1580 if (g_func_pp.arg[i].reg != NULL) {
1581 reg = char_array_i(regs_r32,
1582 ARRAY_SIZE(regs_r32), g_func_pp.arg[i].reg);
1584 ferr(ops, "arg '%s' is not a reg?\n", g_func_pp.arg[i].reg);
1586 regmask_arg |= 1 << reg;
1587 fprintf(fout, " u32 %s = (u32)a%d;\n",
1588 g_func_pp.arg[i].reg, i + 1);
1593 // declare other regs - special case for eax
1594 if (!((regmask | regmask_arg) & 1) && !IS(g_func_pp.ret_type, "void")) {
1595 fprintf(fout, " u32 eax = 0;\n");
1599 regmask &= ~regmask_arg;
1601 regmask &= ~(1 << xBP);
1603 for (reg = 0; reg < 8; reg++) {
1604 if (regmask & (1 << reg)) {
1605 fprintf(fout, " u32 %s;\n", regs_r32[reg]);
1611 if (save_arg_vars) {
1612 for (reg = 0; reg < 32; reg++) {
1613 if (save_arg_vars & (1 << reg)) {
1614 fprintf(fout, " u32 s_a%d;\n", reg + 1);
1620 if (cmp_result_vars) {
1621 for (i = 0; i < 8; i++) {
1622 if (cmp_result_vars & (1 << i)) {
1623 fprintf(fout, " u32 cond_%s;\n", parsed_flag_op_names[i]);
1630 fprintf(fout, " u64 mul_tmp;\n");
1635 fprintf(fout, "\n");
1638 for (i = 0; i < opcnt; i++)
1640 if (g_labels[i][0] != 0)
1641 fprintf(fout, "\n%s:\n", g_labels[i]);
1644 if (po->flags & OPF_RMD)
1649 #define assert_operand_cnt(n_) \
1650 if (po->operand_cnt != n_) \
1651 ferr(po, "operand_cnt is %d/%d\n", po->operand_cnt, n_)
1653 // conditional/flag using op?
1654 if (po->flags & OPF_CC)
1659 pfo = split_cond(po, po->op, &is_inv);
1661 // we go through all this trouble to avoid using parsed_flag_op,
1662 // which makes generated code much nicer
1663 if (delayed_flag_op != NULL)
1665 out_cmp_test(buf1, sizeof(buf1), delayed_flag_op, pfo, is_inv);
1668 else if (last_arith_dst != NULL
1669 && (pfo == PFO_Z || pfo == PFO_S || pfo == PFO_P))
1671 out_src_opr(buf3, sizeof(buf3), po, last_arith_dst, 0);
1672 out_test_for_cc(buf1, sizeof(buf1), po, pfo, is_inv,
1673 last_arith_dst->lmod, buf3);
1676 else if (po->datap != NULL) {
1677 // use preprocessed results
1679 if (!tmp_op || !(tmp_op->pfomask & (1 << pfo)))
1680 ferr(po, "not prepared for pfo %d\n", pfo);
1682 // note: is_inv was not yet applied
1683 snprintf(buf1, sizeof(buf1), "(%scond_%s)",
1684 is_inv ? "!" : "", parsed_flag_op_names[pfo]);
1687 ferr(po, "all methods of finding comparison failed\n");
1690 if (po->flags & OPF_JMP) {
1691 fprintf(fout, " if %s\n", buf1);
1693 else if (po->op == OP_ADC || po->op == OP_SBB) {
1695 fprintf(fout, " cond_%s = %s;\n",
1696 parsed_flag_op_names[pfo], buf1);
1698 else if (po->flags & OPF_DATA) { // SETcc
1699 out_dst_opr(buf2, sizeof(buf2), po, &po->operand[0]);
1700 fprintf(fout, " %s = %s;", buf2, buf1);
1703 ferr(po, "unhandled conditional op\n");
1707 pfomask = po->pfomask;
1712 assert_operand_cnt(2);
1713 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1714 fprintf(fout, " %s = %s;",
1715 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1716 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1720 assert_operand_cnt(2);
1721 po->operand[1].lmod = OPLM_DWORD; // always
1722 fprintf(fout, " %s = %s;",
1723 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1724 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 1));
1728 assert_operand_cnt(2);
1729 fprintf(fout, " %s = %s;",
1730 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1731 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1735 assert_operand_cnt(2);
1736 switch (po->operand[1].lmod) {
1738 strcpy(buf3, "(s8)");
1741 strcpy(buf3, "(s16)");
1744 ferr(po, "invalid src lmod: %d\n", po->operand[1].lmod);
1746 fprintf(fout, " %s = %s%s;",
1747 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1749 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1753 assert_operand_cnt(1);
1754 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1755 fprintf(fout, " %s = ~%s;", buf1, buf1);
1759 assert_operand_cnt(2);
1760 fprintf(fout, " %s = (s32)%s >> 31;",
1761 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1762 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1763 strcpy(g_comment, "cdq");
1766 // arithmetic w/flags
1771 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1776 assert_operand_cnt(2);
1777 fprintf(fout, " %s %s= %s;",
1778 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1780 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1781 last_arith_dst = &po->operand[0];
1782 delayed_flag_op = NULL;
1786 assert_operand_cnt(2);
1787 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1788 if (IS(opr_name(po, 0), opr_name(po, 1))) {
1789 // special case for XOR
1790 fprintf(fout, " %s = 0;",
1791 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
1792 last_arith_dst = &po->operand[0];
1793 delayed_flag_op = NULL;
1799 assert_operand_cnt(2);
1800 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1801 fprintf(fout, " %s = %s%s >> %s;", buf1,
1802 lmod_cast_s(po, po->operand[0].lmod), buf1,
1803 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1804 last_arith_dst = &po->operand[0];
1805 delayed_flag_op = NULL;
1810 assert_operand_cnt(2);
1811 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1812 fprintf(fout, " %s %s= %s + cond_c;",
1813 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
1815 out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
1816 last_arith_dst = &po->operand[0];
1817 delayed_flag_op = NULL;
1822 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1823 strcpy(buf2, po->op == OP_INC ? "++" : "--");
1824 fprintf(fout, " %s%s;", buf1, buf2);
1825 last_arith_dst = &po->operand[0];
1826 delayed_flag_op = NULL;
1830 out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
1831 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0], 0);
1832 fprintf(fout, " %s = -%s%s;", buf1,
1833 lmod_cast_s(po, po->operand[0].lmod), buf2);
1834 last_arith_dst = &po->operand[0];
1835 delayed_flag_op = NULL;
1836 if (pfomask & (1 << PFO_C)) {
1837 fprintf(fout, "\n cond_c = (%s != 0);", buf1);
1838 pfomask &= ~(1 << PFO_C);
1843 if (po->operand_cnt == 2)
1845 if (po->operand_cnt == 3)
1846 ferr(po, "TODO imul3\n");
1849 assert_operand_cnt(1);
1850 strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
1851 fprintf(fout, " mul_tmp = %seax * %s%s;\n", buf1, buf1,
1852 out_src_opr(buf2, sizeof(buf2), po, &po->operand[0], 0));
1853 fprintf(fout, " edx = mul_tmp >> 32;\n");
1854 fprintf(fout, " eax = mul_tmp;");
1855 last_arith_dst = NULL;
1856 delayed_flag_op = NULL;
1861 assert_operand_cnt(1);
1862 if (po->operand[0].lmod != OPLM_DWORD)
1863 ferr(po, "unhandled lmod %d\n", po->operand[0].lmod);
1865 // 32bit division is common, look for it
1866 if (scan_for_cdq_edx(i - 1) >= 0) {
1867 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], 0);
1868 strcpy(buf2, lmod_cast(po, po->operand[0].lmod,
1869 po->op == OP_IDIV));
1870 fprintf(fout, " edx = %seax %% %s%s;\n", buf2, buf2, buf1);
1871 fprintf(fout, " eax = %seax / %s%s;", buf2, buf2, buf1);
1874 ferr(po, "TODO 64bit divident\n");
1875 last_arith_dst = NULL;
1876 delayed_flag_op = NULL;
1881 propagate_lmod(po, &po->operand[0], &po->operand[1]);
1883 for (j = 0; j < 8; j++) {
1884 if (pfomask & (1 << j)) {
1885 out_cmp_test(buf1, sizeof(buf1), po, j, 0);
1886 fprintf(fout, " cond_%s = %s;",
1887 parsed_flag_op_names[j], buf1);
1894 delayed_flag_op = po;
1897 // note: we reuse OP_Jcc for SETcc, only flags differ
1898 case OP_JO ... OP_JG:
1899 if (po->flags & OPF_JMP)
1900 fprintf(fout, " goto %s;", po->operand[0].name);
1901 // else SETcc - should already be handled
1905 assert_operand_cnt(1);
1906 if (po->operand[0].type != OPT_LABEL)
1907 ferr(po, "unhandled call type\n");
1909 fprintf(fout, " goto %s;", po->operand[0].name);
1913 assert_operand_cnt(1);
1914 if (po->operand[0].type != OPT_LABEL)
1915 ferr(po, "unhandled call type\n");
1919 ferr(po, "NULL pp\n");
1922 if (!IS(pp->ret_type, "void")) {
1923 if (po->flags & OPF_TAIL)
1924 fprintf(fout, "return ");
1926 fprintf(fout, "eax = ");
1927 if (strchr(pp->ret_type, '*'))
1928 fprintf(fout, "(u32)");
1931 fprintf(fout, "%s(", opr_name(po, 0));
1932 for (arg = 0; arg < pp->argc; arg++) {
1934 fprintf(fout, ", ");
1936 if (strchr(pp->arg[arg].type, '*'))
1937 fprintf(fout, "(%s)", pp->arg[arg].type);
1939 if (pp->arg[arg].reg != NULL) {
1940 fprintf(fout, "%s", pp->arg[arg].reg);
1945 tmp_op = pp->arg[arg].datap;
1947 ferr(po, "parsed_op missing for arg%d\n", arg);
1948 if (tmp_op->argmask) {
1949 fprintf(fout, "s_a%d", arg + 1);
1953 out_src_opr(buf1, sizeof(buf1),
1954 tmp_op, &tmp_op->operand[0], 0));
1957 fprintf(fout, ");");
1959 if (po->flags & OPF_TAIL) {
1960 strcpy(g_comment, "tailcall");
1961 if (IS(pp->ret_type, "void"))
1962 fprintf(fout, "\n return;");
1964 delayed_flag_op = NULL;
1965 last_arith_dst = NULL;
1969 if (IS(g_func_pp.ret_type, "void"))
1970 fprintf(fout, " return;");
1972 fprintf(fout, " return eax;");
1977 // special case - saved func arg
1978 for (j = 0; j < 32; j++) {
1979 if (po->argmask & (1 << j)) {
1980 fprintf(fout, " s_a%d = %s;", j + 1,
1981 out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], 0));
1986 ferr(po, "push encountered\n");
1990 ferr(po, "pop encountered\n");
1995 ferr(po, "unhandled op type %d, flags %x\n",
2000 if (g_comment[0] != 0) {
2001 fprintf(fout, " // %s", g_comment);
2006 fprintf(fout, "\n");
2009 ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
2011 // see is delayed flag stuff is still valid
2012 if (delayed_flag_op != NULL && delayed_flag_op != po) {
2013 if (is_any_opr_modified(delayed_flag_op, po))
2014 delayed_flag_op = NULL;
2017 if (last_arith_dst != NULL && last_arith_dst != &po->operand[0]) {
2018 if (is_opr_modified(last_arith_dst, po))
2019 last_arith_dst = NULL;
2023 fprintf(fout, "}\n\n");
2026 for (i = 0; i < opcnt; i++) {
2027 if (ops[i].op == OP_CALL) {
2035 proto_release(&g_func_pp);
2038 int main(int argc, char *argv[])
2040 FILE *fout, *fasm, *fhdr;
2042 char words[16][256];
2044 int skip_warned = 0;
2052 printf("usage:\n%s <.c> <.asm> <hdrf>\n",
2058 fhdr = fopen(hdrfn, "r");
2059 my_assert_not(fhdr, NULL);
2062 fasm = fopen(asmfn, "r");
2063 my_assert_not(fasm, NULL);
2065 fout = fopen(argv[1], "w");
2066 my_assert_not(fout, NULL);
2069 g_eqs = malloc(eq_alloc * sizeof(g_eqs[0]));
2070 my_assert_not(g_eqs, NULL);
2072 while (fgets(line, sizeof(line), fasm))
2077 if (*p == 0 || *p == ';')
2080 memset(words, 0, sizeof(words));
2081 for (wordc = 0; wordc < 16; wordc++) {
2082 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
2083 if (*p == 0 || *p == ';') {
2091 awarn("wordc == 0?\n");
2095 // don't care about this:
2096 if (words[0][0] == '.'
2097 || IS(words[0], "include")
2098 || IS(words[0], "assume") || IS(words[1], "segment")
2099 || IS(words[0], "align"))
2104 if (IS(words[1], "proc")) {
2106 aerr("proc '%s' while in_func '%s'?\n",
2108 strcpy(g_func, words[0]);
2113 if (IS(words[1], "endp")) {
2115 aerr("endp '%s' while not in_func?\n", words[0]);
2116 if (!IS(g_func, words[0]))
2117 aerr("endp '%s' while in_func '%s'?\n",
2119 gen_func(fout, fhdr, g_func, pi);
2124 memset(&ops, 0, pi * sizeof(ops[0]));
2125 memset(g_labels, 0, pi * sizeof(g_labels[0]));
2126 memset(g_label_refs, 0, pi * sizeof(g_label_refs[0]));
2133 if (IS(words[1], "=")) {
2135 aerr("unhandled equ, wc=%d\n", wordc);
2136 if (g_eqcnt >= eq_alloc) {
2138 g_eqs = realloc(g_eqs, eq_alloc * sizeof(g_eqs[0]));
2139 my_assert_not(g_eqs, NULL);
2142 len = strlen(words[0]);
2143 if (len > sizeof(g_eqs[0].name) - 1)
2144 aerr("equ name too long: %d\n", len);
2145 strcpy(g_eqs[g_eqcnt].name, words[0]);
2147 if (!IS(words[3], "ptr"))
2148 aerr("unhandled equ\n");
2149 if (IS(words[2], "dword"))
2150 g_eqs[g_eqcnt].lmod = OPLM_DWORD;
2151 else if (IS(words[2], "word"))
2152 g_eqs[g_eqcnt].lmod = OPLM_WORD;
2153 else if (IS(words[2], "byte"))
2154 g_eqs[g_eqcnt].lmod = OPLM_BYTE;
2156 aerr("bad lmod: '%s'\n", words[2]);
2158 g_eqs[g_eqcnt].offset = parse_number(words[4]);
2163 if (pi >= ARRAY_SIZE(ops))
2164 aerr("too many ops\n");
2166 p = strchr(words[0], ':');
2169 if (len > sizeof(g_labels[0]) - 1)
2170 aerr("label too long: %d\n", len);
2171 if (g_labels[pi][0] != 0)
2172 aerr("dupe label?\n");
2173 memcpy(g_labels[pi], words[0], len);
2174 g_labels[pi][len] = 0;
2179 if (!skip_warned && g_labels[pi][0] != 0) {
2180 anote("skipping from '%s'\n", g_labels[pi]);
2183 g_labels[pi][0] = 0;
2187 parse_op(&ops[pi], words, wordc);
2198 // vim:ts=2:shiftwidth=2:expandtab