X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tools%2Ftranslate.c;h=fe6408786a3a59793caae9bc7a6d95ea4a4f668e;hb=71d50aa7a431645296bc8624618e02e7f3bf73ac;hp=e7fd5c73e6de091d9faf19be848e2c990aa1a64c;hpb=8c83cc48a2090b018cb39a629454b5e9608ba4d8;p=ia32rtools.git diff --git a/tools/translate.c b/tools/translate.c index e7fd5c7..fe64087 100644 --- a/tools/translate.c +++ b/tools/translate.c @@ -4,12 +4,22 @@ * * This work is licensed under the terms of 3-clause BSD license. * See COPYING file in the top-level directory. + * + * recognized asm hint comments: + * sctattr - function attributes (see code) + * sctend - force end of function/chunk + * sctpatch:

- replace current asm line with

+ * sctproto:

- prototype of ref'd function or struct + * sctref - variable is referenced, make global + * sctskip_start - start of skipped code chunk (inclusive) + * sctskip_end - end of skipped code chunk (inclusive) */ #define _GNU_SOURCE #include #include #include +#include #include "my_assert.h" #include "my_str.h" @@ -157,6 +167,7 @@ enum op_op { OPP_ALLSHR, OPP_FTOL, OPP_CIPOW, + OPP_ABORT, // undefined OP_UD2, }; @@ -481,7 +492,7 @@ static int parse_indmode(char *name, int *regmask, int need_c_cvt) } if ('0' <= w[0] && w[0] <= '9') { - number = parse_number(w); + number = parse_number(w, 0); printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number); continue; } @@ -577,8 +588,9 @@ static const char *parse_stack_el(const char *name, char *extra_reg, if (len < sizeof(buf) - 1) { strncpy(buf, s, len); buf[len] = 0; + errno = 0; val = strtol(buf, &endp, 16); - if (val == 0 || *endp != 0) { + if (val == 0 || *endp != 0 || errno != 0) { aerr("%s num parse fail for '%s'\n", __func__, buf); return NULL; } @@ -620,6 +632,9 @@ static int guess_lmod_from_name(struct parsed_opr *opr) static int guess_lmod_from_c_type(enum opr_lenmod *lmod, const struct parsed_type *c_type) { + static const char *qword_types[] = { + "uint64_t", "int64_t", "__int64", + }; static const char *dword_types[] = { "uint32_t", "int", "_DWORD", "UINT_PTR", "DWORD", "WPARAM", "LPARAM", "UINT", "__int32", @@ -668,6 +683,13 @@ static int guess_lmod_from_c_type(enum opr_lenmod *lmod, } } + for (i = 0; i < ARRAY_SIZE(qword_types); i++) { + if (IS(n, qword_types[i])) { + *lmod = OPLM_QWORD; + return 1; + } + } + return 0; } @@ -854,7 +876,7 @@ static int parse_operand(struct parsed_opr *opr, else if (('0' <= words[w][0] && words[w][0] <= '9') || words[w][0] == '-') { - number = parse_number(words[w]); + number = parse_number(words[w], 0); opr->type = OPT_CONST; opr->val = number; printf_number(opr->name, sizeof(opr->name), number); @@ -1724,16 +1746,15 @@ static struct parsed_equ *equ_find(struct parsed_op *po, const char *name, *extra_offs = 0; namelen = strlen(name); - p = strchr(name, '+'); + p = strpbrk(name, "+-"); if (p != NULL) { namelen = p - name; if (namelen <= 0) ferr(po, "equ parse failed for '%s'\n", name); - if (IS_START(p, "0x")) - p += 2; + errno = 0; *extra_offs = strtol(p, &endp, 16); - if (*endp != 0) + if (*endp != 0 || errno != 0) ferr(po, "equ parse failed for '%s'\n", name); } @@ -1777,10 +1798,11 @@ static void parse_stack_access(struct parsed_op *po, p = name + 4; if (IS_START(p, "0x")) p += 2; + errno = 0; offset = strtoul(p, &endp, 16); if (name[3] == '-') offset = -offset; - if (*endp != 0) + if (*endp != 0 || errno != 0) ferr(po, "ebp- parse of '%s' failed\n", name); } else { @@ -1825,8 +1847,9 @@ static int parse_stack_esp_offset(struct parsed_op *po, // just plain offset? if (!IS_START(name, "esp+")) return -1; + errno = 0; offset = strtol(name + 4, &endp, 0); - if (endp == NULL || *endp != 0) + if (endp == NULL || *endp != 0 || errno != 0) return -1; *offset_out = offset; return 0; @@ -1856,7 +1879,6 @@ static int stack_frame_access(struct parsed_op *po, int offset = 0; int retval = -1; int sf_ofs; - int lim; if (po->flags & OPF_EBP_S) ferr(po, "stack_frame_access while ebp is scratch\n"); @@ -1987,8 +2009,7 @@ static int stack_frame_access(struct parsed_op *po, g_stack_frame_used = 1; sf_ofs = g_stack_fsz + offset; - lim = (ofs_reg[0] != 0) ? -4 : 0; - if (offset > 0 || sf_ofs < lim) + if (ofs_reg[0] == 0 && (offset > 0 || sf_ofs < 0)) ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz); if (is_lea) @@ -2310,7 +2331,7 @@ static char *out_src_opr_float(char *buf, size_t buf_size, break; } out_src_opr(tmp, sizeof(tmp), po, popr, "", 1); - snprintf(buf, buf_size, "*((%s *)%s)", cast, tmp); + snprintf(buf, buf_size, "*(%s *)(%s)", cast, tmp); break; default: @@ -2567,10 +2588,10 @@ static int scan_for_pop(int i, int opcnt, int magic, int reg, if (po->pp != NULL && po->pp->is_noreturn) seen_noreturn = 1; else - return -1; + goto out; } else - return -1; // deadend + goto out; } if (po->flags & (OPF_RMD|OPF_DONE|OPF_FARG)) @@ -2621,6 +2642,7 @@ static int scan_for_pop(int i, int opcnt, int magic, int reg, } } +out: // for noreturn, assume msvc skipped stack cleanup return seen_noreturn ? 1 : -1; } @@ -2672,8 +2694,9 @@ static int scan_for_rsave_pop_reg(int i, int magic, int reg, int set_flags) return -1; } - // nothing interesting on this path - return 0; + // nothing interesting on this path, + // still return ret for something recursive calls could find + return ret; } static void find_reachable_exits(int i, int opcnt, int magic, @@ -2724,7 +2747,8 @@ static int scan_for_pop_ret(int i, int opcnt, int reg, int set_flags) { static int exits[MAX_EXITS]; static int exit_count; - int j, ret; + int found = 0; + int e, j, ret; if (!set_flags) { exit_count = 0; @@ -2734,13 +2758,23 @@ static int scan_for_pop_ret(int i, int opcnt, int reg, int set_flags) } for (j = 0; j < exit_count; j++) { - ret = scan_for_rsave_pop_reg(exits[j], i + opcnt * 16 + set_flags, + e = exits[j]; + ret = scan_for_rsave_pop_reg(e, i + opcnt * 16 + set_flags, reg, set_flags); - if (ret == -1) - return -1; + if (ret != -1) { + found |= ret; + continue; + } + if (ops[e].op == OP_CALL && ops[e].pp != NULL + && ops[e].pp->is_noreturn) + { + // assume stack cleanup was skipped + continue; + } + return -1; } - return 1; + return found; } // scan for one or more pop of push @@ -3662,6 +3696,7 @@ static void resolve_branches_parse_calls(int opcnt) struct parsed_data *pd; struct parsed_op *po; const char *tmpname; + enum op_op prev_op; int i, l; int ret; @@ -3725,8 +3760,10 @@ static void resolve_branches_parse_calls(int opcnt) if (pp != NULL) { if (pp->is_fptr) check_func_pp(po, pp, "fptr var call"); - if (pp->is_noreturn) + if (pp->is_noreturn) { po->flags |= OPF_TAIL; + po->flags &= ~OPF_ATAIL; // most likely... + } } po->pp = pp; continue; @@ -3771,8 +3808,14 @@ static void resolve_branches_parse_calls(int opcnt) tailcall: po->op = OP_CALL; po->flags |= OPF_TAIL; - if (i > 0 && ops[i - 1].op == OP_POP) + prev_op = i > 0 ? ops[i - 1].op : OP_UD2; + if (prev_op == OP_POP) po->flags |= OPF_ATAIL; + if (g_stack_fsz + g_bp_frame == 0 && prev_op != OP_PUSH + && (g_func_pp == NULL || g_func_pp->argc_stack > 0)) + { + po->flags |= OPF_ATAIL; + } i--; // reprocess } } @@ -4000,13 +4043,22 @@ static void scan_prologue_epilogue(int opcnt) l += ops[j].operand[1].val / 4 - 1; } else - ferr(&ops[j], "'pop ecx' expected\n"); + break; ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS; j--; } - if (l != ecx_push) + if (l != ecx_push) { + if (i < opcnt && ops[i].op == OP_CALL + && ops[i].pp != NULL && ops[i].pp->is_noreturn) + { + // noreturn tailcall with no epilogue + i++; + found = 1; + continue; + } ferr(&ops[j], "epilogue scan failed\n"); + } found = 1; } @@ -4014,21 +4066,25 @@ static void scan_prologue_epilogue(int opcnt) if (esp_sub) { if (ops[j].op != OP_ADD || !IS(opr_name(&ops[j], 0), "esp") - || ops[j].operand[1].type != OPT_CONST - || ops[j].operand[1].val != g_stack_fsz) + || ops[j].operand[1].type != OPT_CONST) { - if (ops[i].op == OP_CALL && ops[i].pp != NULL - && ops[i].pp->is_noreturn) + if (i < opcnt && ops[i].op == OP_CALL + && ops[i].pp != NULL && ops[i].pp->is_noreturn) { // noreturn tailcall with no epilogue i++; + found = 1; continue; } ferr(&ops[j], "'add esp' expected\n"); } - ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS; - ops[j].operand[1].val = 0; // hack for stack arg scanner + if (ops[j].operand[1].val < g_stack_fsz) + ferr(&ops[j], "esp adj is too low (need %d)\n", g_stack_fsz); + + ops[j].operand[1].val -= g_stack_fsz; // for stack arg scanner + if (ops[j].operand[1].val == 0) + ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS; found = 1; } @@ -4620,6 +4676,9 @@ static struct parsed_proto *process_call(int i, int opcnt) ferr(po, "too many args for '%s'\n", tmpname); } if (pp->argc_stack > adj / 4) { + if (pp->is_noreturn) + // assume no stack adjust was emited + goto out; fnote(po, "(this call)\n"); ferr(&ops[ret], "stack tracking failed for '%s': %x %x\n", tmpname, pp->argc_stack * 4, adj); @@ -4632,6 +4691,7 @@ static struct parsed_proto *process_call(int i, int opcnt) ferr(po, "missing esp_adjust for vararg func '%s'\n", pp->name); +out: return pp; } @@ -4662,8 +4722,10 @@ static int collect_call_args_no_push(int i, struct parsed_proto *pp, ret = parse_stack_esp_offset(po, po->operand[0].name, &offset); if (ret != 0) continue; - if (offset < 0 || offset >= pp->argc_stack * 4 || (offset & 3)) - ferr(po, "bad offset %d (%d args)\n", offset, pp->argc_stack); + if (offset < 0 || offset >= pp->argc_stack * 4 || (offset & 3)) { + //ferr(po, "offset %d, %d args\n", offset, pp->argc_stack); + continue; + } arg = base_arg + offset / 4; po->p_argnext = -1; @@ -4882,8 +4944,8 @@ static int collect_call_args_r(struct parsed_op *po, int i, pp_tmp = ops[j].pp; if (pp_tmp == NULL) - ferr(po, "arg collect hit unparsed call '%s'\n", - ops[j].operand[0].name); + ferr(po, "arg collect %d/%d hit unparsed call '%s'\n", + arg, pp->argc, ops[j].operand[0].name); if (may_reuse && pp_tmp->argc_stack > 0) ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n", arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack); @@ -5586,6 +5648,8 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) reg_use_pass(0, opcnt, cbits, regmask_init, ®mask, 0, ®mask_save, ®mask_init, regmask_arg); + need_float_stack = !!(regmask & mxST7_2); + // pass7: // - find flag set ops for their users // - do unresolved calls @@ -5793,10 +5857,13 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) // this might need it's own pass... if (po->op != OP_FST && po->p_argnum > 0) save_arg_vars[po->p_arggrp] |= 1 << (po->p_argnum - 1); + + // correct for "full stack" mode late enable + if ((po->flags & (OPF_PPUSH|OPF_FPOP)) && need_float_stack) + po->flags |= OPF_FSHIFT; } float_type = need_double ? "double" : "float"; - need_float_stack = !!(regmask & mxST7_2); float_st0 = need_float_stack ? "f_st[f_stp & 7]" : "f_st0"; float_st1 = need_float_stack ? "f_st[(f_stp + 1) & 7]" : "f_st1"; @@ -6683,10 +6750,11 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) lmod_cast_s(po, po->operand[0].lmod), buf2); last_arith_dst = &po->operand[0]; delayed_flag_op = NULL; - if (pfomask & (1 << PFO_C)) { + if (pfomask & PFOB_C) { fprintf(fout, "\n cond_c = (%s != 0);", buf1); - pfomask &= ~(1 << PFO_C); + pfomask &= ~PFOB_C; } + output_std_flags(fout, po, &pfomask, buf1); break; case OP_IMUL: @@ -6892,10 +6960,13 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) pp->has_structarg ? "_sa" : ""); if (po->flags & OPF_ATAIL) { - if (pp->argc_stack != g_func_pp->argc_stack - || (pp->argc_stack > 0 - && pp->is_stdcall != g_func_pp->is_stdcall)) - ferr(po, "incompatible tailcall\n"); + int check_compat = + g_func_pp->is_stdcall && g_func_pp->argc_stack > 0; + check_compat |= pp->argc_stack > 0; + if (check_compat + && (pp->argc_stack != g_func_pp->argc_stack + || pp->is_stdcall != g_func_pp->is_stdcall)) + ferr(po, "incompatible arg-reuse tailcall\n"); if (g_func_pp->has_retreg) ferr(po, "TODO: retreg+tailcall\n"); @@ -6934,7 +7005,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) if (pp->arg[arg].type.is_retreg) fprintf(fout, "&%s", pp->arg[arg].reg); else if (IS(pp->arg[arg].reg, "ebp") - && !(po->flags & OPF_EBP_S)) + && g_bp_frame && !(po->flags & OPF_EBP_S)) { // rare special case fprintf(fout, "%s(u32)&sf.b[sizeof(sf)]", cast); @@ -7446,9 +7517,13 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) strcat(g_comment, " CIpow"); break; + case OPP_ABORT: + fprintf(fout, " do_skip_code_abort();"); + break; + // mmx case OP_EMMS: - strcpy(g_comment, " (emms)"); + fprintf(fout, " do_emms();"); break; default: @@ -7722,8 +7797,11 @@ static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits, po->regmask_dst |= 1 << xAX; dep = hg_fp_find_dep(fp, po->operand[0].name); - if (dep != NULL) + if (dep != NULL) { dep->regmask_live = regmask_save | regmask_dst; + if (g_bp_frame && !(po->flags & OPF_EBP_S)) + dep->regmask_live |= 1 << xBP; + } } else if (po->op == OP_RET) { if (po->operand_cnt > 0) { @@ -7942,7 +8020,7 @@ static void gen_hdr(const char *funcn, int opcnt) // noreturn OS functions break; } - if (ops[i].op != OP_NOP) + if (ops[i].op != OP_NOP && ops[i].op != OPP_ABORT) ferr(&ops[i], "unreachable code\n"); } @@ -8270,10 +8348,14 @@ static int ida_xrefs_show_need(FILE *fasm, char *p, long pos; p = strrchr(p, ';'); - if (p != NULL && *p == ';' && IS_START(p + 2, "DATA XREF: ")) { - p += 13; - if (is_xref_needed(p, rlist, rlist_len)) + if (p != NULL && *p == ';') { + if (IS_START(p + 2, "sctref")) return 1; + if (IS_START(p + 2, "DATA XREF: ")) { + p += 13; + if (is_xref_needed(p, rlist, rlist_len)) + return 1; + } } pos = ftell(fasm); @@ -8292,6 +8374,12 @@ static int ida_xrefs_show_need(FILE *fasm, char *p, p = strrchr(p, ';'); p += 2; + + if (IS_START(p, "sctref")) { + found_need = 1; + break; + } + // it's printed once, but no harm to check again if (IS_START(p, "DATA XREF: ")) p += 11; @@ -8485,7 +8573,7 @@ static int cmp_chunks(const void *p1, const void *p2) return strcmp(c1->name, c2->name); } -static void scan_ahead(FILE *fasm) +static void scan_ahead_for_chunks(FILE *fasm) { char words[2][256]; char line[256]; @@ -8566,7 +8654,8 @@ int main(int argc, char *argv[]) char *sctproto = NULL; int in_func = 0; int pending_endp = 0; - int skip_func = 0; + int skip_code = 0; + int skip_code_end = 0; int skip_warned = 0; int eq_alloc; int verbose = 0; @@ -8632,6 +8721,8 @@ int main(int argc, char *argv[]) memset(words, 0, sizeof(words)); for (; arg < argc; arg++) { + int skip_func = 0; + frlist = fopen(argv[arg], "r"); my_assert_not(frlist, NULL); @@ -8663,7 +8754,6 @@ int main(int argc, char *argv[]) } rlist[rlist_len++] = strdup(words[0]); } - skip_func = 0; fclose(frlist); frlist = NULL; @@ -8824,7 +8914,7 @@ int main(int argc, char *argv[]) if (addr > f_addr && !scanned_ahead) { //anote("scan_ahead caused by '%s', addr %lx\n", // g_func, addr); - scan_ahead(fasm); + scan_ahead_for_chunks(fasm); scanned_ahead = 1; func_chunks_sorted = 0; } @@ -8846,7 +8936,12 @@ parse_words: if (*p != 0 && *p != ';') aerr("too many words\n"); - // alow asm patches in comments + if (skip_code_end) { + skip_code_end = 0; + skip_code = 0; + } + + // allow asm patches in comments if (*p == ';') { if (IS_START(p, "; sctpatch:")) { p = sskip(p + 11); @@ -8862,6 +8957,20 @@ parse_words: if (!pending_endp) break; } + else if (IS_START(p, "; sctskip_start")) { + if (in_func && !g_skip_func) { + if (!skip_code) { + ops[pi].op = OPP_ABORT; + ops[pi].asmln = asmln; + pi++; + } + skip_code = 1; + } + } + else if (IS_START(p, "; sctskip_end")) { + if (skip_code) + skip_code_end = 1; + } } if (wordc == 0) { @@ -8930,7 +9039,7 @@ do_pending_endp: if (pd->type == OPT_OFFSET) pd->d[pd->count].u.label = strdup(words[i]); else - pd->d[pd->count].u.val = parse_number(words[i]); + pd->d[pd->count].u.val = parse_number(words[i], 0); pd->d[pd->count].bt_i = -1; pd->count++; } @@ -9001,6 +9110,8 @@ do_pending_endp: if (!IS(g_func, words[0])) aerr("endp '%s' while in_func '%s'?\n", words[0], g_func); + if (skip_code) + aerr("endp '%s' while skipping code\n", words[0]); if ((g_ida_func_attr & IDAFA_THUNK) && pi == 1 && ops[0].op == OP_JMP && ops[0].operand[0].had_ds) @@ -9068,7 +9179,7 @@ do_pending_endp: continue; } - if (!in_func || g_skip_func) { + if (!in_func || g_skip_func || skip_code) { if (!skip_warned && !g_skip_func && g_labels[pi] != NULL) { if (verbose) anote("skipping from '%s'\n", g_labels[pi]); @@ -9107,7 +9218,7 @@ do_pending_endp: else aerr("bad lmod: '%s'\n", words[2]); - g_eqs[g_eqcnt].offset = parse_number(words[4]); + g_eqs[g_eqcnt].offset = parse_number(words[4], 0); g_eqcnt++; continue; }