X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tools%2Ftranslate.c;h=cb036c14207179cf48cf34a1e28453e2e08b227a;hb=60fe410ce8c940b42a341f8dfebd365b76a77c14;hp=e3d8f5bb6e23c65d0676de38a1b5fcc24e5cb88c;hpb=092f64e1941e7eb188ce00d301e5734d53e3974c;p=ia32rtools.git diff --git a/tools/translate.c b/tools/translate.c index e3d8f5b..cb036c1 100644 --- a/tools/translate.c +++ b/tools/translate.c @@ -128,9 +128,10 @@ struct parsed_opr { unsigned int size_mismatch:1; // type override differs from C unsigned int size_lt:1; // type override is larger than C unsigned int had_ds:1; // had ds: prefix + const struct parsed_proto *pp; // for OPT_LABEL int reg; unsigned int val; - char name[256]; + char name[112]; }; struct parsed_op { @@ -148,8 +149,9 @@ struct parsed_op { int cc_scratch; // scratch storage during analysis int bt_i; // branch target for branches struct parsed_data *btj;// branch targets for jumptables - struct parsed_proto *pp; + struct parsed_proto *pp;// parsed_proto for OP_CALL void *datap; + int asmln; }; // datap: @@ -211,13 +213,13 @@ static int g_stack_fsz; static int g_ida_func_attr; static int g_allow_regfunc; #define ferr(op_, fmt, ...) do { \ - printf("error:%s:#%zd: '%s': " fmt, g_func, (op_) - ops, \ + printf("%s:%d: error: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \ dump_op(op_), ##__VA_ARGS__); \ fcloseall(); \ exit(1); \ } while (0) #define fnote(op_, fmt, ...) \ - printf("error:%s:#%zd: '%s': " fmt, g_func, (op_) - ops, \ + printf("%s:%d: note: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \ dump_op(op_), ##__VA_ARGS__) #define MAX_REGS 8 @@ -507,6 +509,24 @@ static int guess_lmod_from_c_type(enum opr_lenmod *lmod, return 0; } +static char *default_cast_to(char *buf, size_t buf_size, + struct parsed_opr *opr) +{ + buf[0] = 0; + + if (!opr->is_ptr) + return buf; + if (opr->pp == NULL || opr->pp->type.name == NULL + || opr->pp->is_fptr) + { + snprintf(buf, buf_size, "%s", "(void *)"); + return buf; + } + + snprintf(buf, buf_size, "(%s)", opr->pp->type.name); + return buf; +} + static enum opr_type lmod_from_directive(const char *d) { if (IS(d, "dd")) @@ -699,6 +719,7 @@ do_label: } opr->is_array = pp->type.is_array; } + opr->pp = pp; if (opr->lmod == OPLM_UNSPEC) guess_lmod_from_name(opr); @@ -884,6 +905,7 @@ static void parse_op(struct parsed_op *op, char words[16][256], int wordc) op->pfo = op_table[i].pfo; op->pfo_inv = op_table[i].pfo_inv; op->regmask_src = op->regmask_dst = 0; + op->asmln = asmln; for (opr = 0; opr < op_table[i].minopr; opr++) { regmask = regmask_ind = 0; @@ -2871,39 +2893,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) if (g_func_pp == NULL) ferr(ops, "proto_parse failed for '%s'\n", funcn); - fprintf(fout, "%s ", g_func_pp->ret_type.name); - output_pp_attrs(fout, g_func_pp, g_ida_func_attr & IDAFA_NORETURN); - fprintf(fout, "%s(", g_func_pp->name); - for (i = 0; i < g_func_pp->argc; i++) { - if (i > 0) - fprintf(fout, ", "); - if (g_func_pp->arg[i].fptr != NULL) { - // func pointer.. - pp = g_func_pp->arg[i].fptr; - fprintf(fout, "%s (", pp->ret_type.name); - output_pp_attrs(fout, pp, 0); - fprintf(fout, "*a%d)(", i + 1); - for (j = 0; j < pp->argc; j++) { - if (j > 0) - fprintf(fout, ", "); - if (pp->arg[j].fptr) - ferr(ops, "nested fptr\n"); - fprintf(fout, "%s", pp->arg[j].type.name); - } - if (pp->is_vararg) { - if (j > 0) - fprintf(fout, ", "); - fprintf(fout, "..."); - } - fprintf(fout, ")"); - } - else if (g_func_pp->arg[i].type.is_retreg) { - fprintf(fout, "u32 *r_%s", g_func_pp->arg[i].reg); - } - else { - fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1); - } if (g_func_pp->arg[i].reg != NULL) { reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), g_func_pp->arg[i].reg); @@ -2912,13 +2902,6 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) regmask_arg |= 1 << reg; } } - if (g_func_pp->is_vararg) { - if (i > 0) - fprintf(fout, ", "); - fprintf(fout, "..."); - } - - fprintf(fout, ")\n{\n"); // pass1: // - handle ebp/esp frame, remove ops related to it @@ -3492,39 +3475,6 @@ tailcall: } } } - - // declare indirect funcs - if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) { - if (pp->name[0] != 0) { - memmove(pp->name + 2, pp->name, strlen(pp->name) + 1); - memcpy(pp->name, "i_", 2); - - // might be declared already - found = 0; - for (j = 0; j < i; j++) { - if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) { - if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) { - found = 1; - break; - } - } - } - if (found) - continue; - } - else - snprintf(pp->name, sizeof(pp->name), "icall%d", i); - - fprintf(fout, " %s (", pp->ret_type.name); - output_pp_attrs(fout, pp, 0); - fprintf(fout, "*%s)(", pp->name); - for (j = 0; j < pp->argc; j++) { - if (j > 0) - fprintf(fout, ", "); - fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1); - } - fprintf(fout, ");\n"); - } } else if (po->op == OP_RET && !IS(g_func_pp->ret_type.name, "void")) regmask |= 1 << xAX; @@ -3562,6 +3512,102 @@ tailcall: } } + // output starts here + + // define userstack size + if (g_func_pp->is_userstack) { + fprintf(fout, "#ifndef US_SZ_%s\n", g_func_pp->name); + fprintf(fout, "#define US_SZ_%s USERSTACK_SIZE\n", g_func_pp->name); + fprintf(fout, "#endif\n"); + } + + // the function itself + fprintf(fout, "%s ", g_func_pp->ret_type.name); + output_pp_attrs(fout, g_func_pp, g_ida_func_attr & IDAFA_NORETURN); + fprintf(fout, "%s(", g_func_pp->name); + + for (i = 0; i < g_func_pp->argc; i++) { + if (i > 0) + fprintf(fout, ", "); + if (g_func_pp->arg[i].fptr != NULL) { + // func pointer.. + pp = g_func_pp->arg[i].fptr; + fprintf(fout, "%s (", pp->ret_type.name); + output_pp_attrs(fout, pp, 0); + fprintf(fout, "*a%d)(", i + 1); + for (j = 0; j < pp->argc; j++) { + if (j > 0) + fprintf(fout, ", "); + if (pp->arg[j].fptr) + ferr(ops, "nested fptr\n"); + fprintf(fout, "%s", pp->arg[j].type.name); + } + if (pp->is_vararg) { + if (j > 0) + fprintf(fout, ", "); + fprintf(fout, "..."); + } + fprintf(fout, ")"); + } + else if (g_func_pp->arg[i].type.is_retreg) { + fprintf(fout, "u32 *r_%s", g_func_pp->arg[i].reg); + } + else { + fprintf(fout, "%s a%d", g_func_pp->arg[i].type.name, i + 1); + } + } + if (g_func_pp->is_vararg) { + if (i > 0) + fprintf(fout, ", "); + fprintf(fout, "..."); + } + + fprintf(fout, ")\n{\n"); + + // declare indirect functions + for (i = 0; i < opcnt; i++) { + po = &ops[i]; + if (po->flags & OPF_RMD) + continue; + + if (po->op == OP_CALL) { + pp = po->pp; + if (pp == NULL) + ferr(po, "NULL pp\n"); + + if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) { + if (pp->name[0] != 0) { + memmove(pp->name + 2, pp->name, strlen(pp->name) + 1); + memcpy(pp->name, "i_", 2); + + // might be declared already + found = 0; + for (j = 0; j < i; j++) { + if (ops[j].op == OP_CALL && (pp_tmp = ops[j].pp)) { + if (pp_tmp->is_fptr && IS(pp->name, pp_tmp->name)) { + found = 1; + break; + } + } + } + if (found) + continue; + } + else + snprintf(pp->name, sizeof(pp->name), "icall%d", i); + + fprintf(fout, " %s (", pp->ret_type.name); + output_pp_attrs(fout, pp, 0); + fprintf(fout, "*%s)(", pp->name); + for (j = 0; j < pp->argc; j++) { + if (j > 0) + fprintf(fout, ", "); + fprintf(fout, "%s a%d", pp->arg[j].type.name, j + 1); + } + fprintf(fout, ");\n"); + } + } + } // output LUTs/jumptables for (i = 0; i < g_func_pd_cnt; i++) { @@ -3598,8 +3644,8 @@ tailcall: } if (g_func_pp->is_userstack) { - fprintf(fout, " u32 fake_sf[1024];\n"); - fprintf(fout, " u32 *esp = &fake_sf[1024];\n"); + fprintf(fout, " u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name); + fprintf(fout, " u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n"); had_decl = 1; } @@ -3807,9 +3853,10 @@ tailcall: assert_operand_cnt(2); propagate_lmod(po, &po->operand[0], &po->operand[1]); out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); + default_cast_to(buf3, sizeof(buf3), &po->operand[0]); fprintf(fout, " %s = %s;", buf1, out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], - po->operand[0].is_ptr ? "(void *)" : "", 0)); + buf3, 0)); break; case OP_LEA: @@ -3853,9 +3900,11 @@ tailcall: out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 0)); fprintf(fout, " %s = %s;", out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), - out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], "", 0)); - fprintf(fout, " %s = tmp;", - out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1])); + out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], + default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0)); + fprintf(fout, " %s = %stmp;", + out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]), + default_cast_to(buf3, sizeof(buf3), &po->operand[1])); snprintf(g_comment, sizeof(g_comment), "xchg"); break; @@ -3880,7 +3929,7 @@ tailcall: ferr(po, "TODO\n"); } else { - fprintf(fout, " eax = %sedi; esi %c= %d;", + fprintf(fout, " eax = %sesi; esi %c= %d;", lmod_cast_u_ptr(po, po->operand[0].lmod), (po->flags & OPF_DF) ? '-' : '+', lmod_bytes(po, po->operand[0].lmod)); @@ -3939,11 +3988,11 @@ tailcall: if (pfomask & (1 << PFO_C)) { // ugh.. fprintf(fout, - " cond_c = %sedi < %sesi;\n", buf1, buf1); + " cond_c = %sesi < %sedi;\n", buf1, buf1); pfomask &= ~(1 << PFO_C); } fprintf(fout, - " cond_z = (%sedi == %sesi); edi %c= %d, esi %c= %d;\n", + " cond_z = (%sesi == %sedi); esi %c= %d, edi %c= %d;\n", buf1, buf1, l, j, l, j); fprintf(fout, " if (cond_z %s 0) break;\n", @@ -3955,7 +4004,7 @@ tailcall: } else { fprintf(fout, - " cond_z = (%sedi = %sesi); edi %c= %d; esi %c= %d;", + " cond_z = (%sesi == %sedi); esi %c= %d; edi %c= %d;", buf1, buf1, l, j, l, j); strcpy(g_comment, "cmps"); } @@ -3986,7 +4035,7 @@ tailcall: (po->flags & OPF_REPZ) ? "e" : "ne"); } else { - fprintf(fout, " cond_z = (%seax = %sedi); edi %c= %d;", + fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;", lmod_cast_u(po, po->operand[0].lmod), lmod_cast_u_ptr(po, po->operand[0].lmod), l, j); strcpy(g_comment, "scas"); @@ -4342,10 +4391,14 @@ tailcall: fprintf(fout, " {\n"); } - if (pp->is_fptr && !pp->is_arg) + if (pp->is_fptr && !pp->is_arg) { fprintf(fout, "%s%s = %s;\n", buf3, pp->name, out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "(void *)", 0)); + if (pp->is_unresolved) + fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n", + buf3, asmfn, po->asmln, pp->name); + } fprintf(fout, "%s", buf3); if (strstr(pp->ret_type.name, "int64")) { @@ -4443,7 +4496,7 @@ tailcall: } if (pp->is_unresolved) { - snprintf(buf2, sizeof(buf2), " unresoved %dreg", + snprintf(buf2, sizeof(buf2), " unresolved %dreg", pp->argc_reg); strcat(g_comment, buf2); } @@ -4544,7 +4597,7 @@ tailcall: fprintf(fout, " %s = %s;", buf1, out_src_opr(buf2, sizeof(buf2), tmp_op, &tmp_op->operand[0], - po->operand[0].is_ptr ? "(void *)" : "", 0)); + default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0)); break; } else if (g_func_pp->is_userstack) { @@ -4745,6 +4798,9 @@ static void scan_ahead(FILE *fasm) add_func_chunk(fasm, words[0], asmln); } + else if (IS_START(p, "; sctend")) + break; + continue; } // *p == ';' @@ -4790,6 +4846,7 @@ int main(int argc, char *argv[]) int eq_alloc; int verbose = 0; int multi_seg = 0; + int end = 0; int arg_out; int arg; int pi = 0; @@ -5020,6 +5077,11 @@ parse_words: if (IS_START(p, "; sctproto:")) { sctproto = strdup(p + 11); } + else if (IS_START(p, "; sctend")) { + end = 1; + if (!pending_endp) + break; + } } if (wordc == 0) { @@ -5040,7 +5102,7 @@ parse_words: do_pending_endp: // do delayed endp processing to collect switch jumptables if (pending_endp) { - if (in_func && !skip_func && wordc >= 2 + if (in_func && !skip_func && !end && wordc >= 2 && ((words[0][0] == 'd' && words[0][2] == 0) || (words[1][0] == 'd' && words[1][2] == 0))) { @@ -5123,6 +5185,9 @@ do_pending_endp: } g_func_pd_cnt = 0; pd = NULL; + + if (end) + break; if (wordc == 0) continue; } @@ -5187,8 +5252,12 @@ do_pending_endp: } if (wordc == 2 && IS(words[1], "ends")) { - if (!multi_seg) + if (!multi_seg) { + end = 1; + if (pending_endp) + goto do_pending_endp; break; + } // scan for next text segment while (fgets(line, sizeof(line), fasm)) { @@ -5257,7 +5326,7 @@ do_pending_endp: parse_op(&ops[pi], words, wordc); if (sctproto != NULL) { - if (ops[pi].op == OP_CALL) + if (ops[pi].op == OP_CALL || ops[pi].op == OP_JMP) ops[pi].datap = sctproto; sctproto = NULL; }