X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tools%2Ftranslate.c;h=1a12b2125e0266046d6f1f5f254471c94125eabe;hb=2b70f6d33838eccf24330e79cf939f2d5632a1c7;hp=6c432dde9aa699e5bb1f5318dd8e531f8e0f76cb;hpb=66bdb2b07a1f50833ad79ea71a378dafd79030e6;p=ia32rtools.git diff --git a/tools/translate.c b/tools/translate.c index 6c432dd..1a12b21 100644 --- a/tools/translate.c +++ b/tools/translate.c @@ -13,6 +13,7 @@ #include "my_assert.h" #include "my_str.h" +#include "common.h" #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) #define IS(w, y) !strcmp(w, y) @@ -41,7 +42,7 @@ enum op_flags { OPF_DATA = (1 << 1), /* data processing - writes to dst opr */ OPF_FLAGS = (1 << 2), /* sets flags */ OPF_JMP = (1 << 3), /* branch, call */ - OPF_CJMP = (1 << 4), /* cond. branch (cc or jecxz) */ + OPF_CJMP = (1 << 4), /* cond. branch (cc or jecxz/loop) */ OPF_CC = (1 << 5), /* uses flags */ OPF_TAIL = (1 << 6), /* ret or tail call */ OPF_RSAVE = (1 << 7), /* push/pop is local reg save/load */ @@ -72,6 +73,7 @@ enum op_op { OP_MOVSX, OP_XCHG, OP_NOT, + OP_XLAT, OP_CDQ, OP_LODS, OP_STOS, @@ -89,6 +91,7 @@ enum op_op { OP_SHL, OP_SHR, OP_SAR, + OP_SHLD, OP_SHRD, OP_ROL, OP_ROR, @@ -109,6 +112,7 @@ enum op_op { OP_CALL, OP_JMP, OP_JECXZ, + OP_LOOP, OP_JCC, OP_SCC, // x87 @@ -242,8 +246,8 @@ static int g_quiet_pp; static int g_header_mode; #define ferr(op_, fmt, ...) do { \ - printf("%s:%d: error: [%s] '%s': " fmt, asmfn, (op_)->asmln, g_func, \ - dump_op(op_), ##__VA_ARGS__); \ + printf("%s:%d: error %u: [%s] '%s': " fmt, asmfn, (op_)->asmln, \ + __LINE__, g_func, dump_op(op_), ##__VA_ARGS__); \ fcloseall(); \ exit(1); \ } while (0) @@ -252,8 +256,7 @@ static int g_header_mode; dump_op(op_), ##__VA_ARGS__) #define ferr_assert(op_, cond) do { \ - if (!(cond)) ferr(op_, "assertion '%s' failed on ln :%d\n", #cond, \ - __LINE__); \ + if (!(cond)) ferr(op_, "assertion '%s' failed\n", #cond); \ } while (0) const char *regs_r32[] = { @@ -508,19 +511,19 @@ static const char *parse_stack_el(const char *name, char *extra_reg, static int guess_lmod_from_name(struct parsed_opr *opr) { - if (!strncmp(opr->name, "dword_", 6)) { + if (IS_START(opr->name, "dword_") || IS_START(opr->name, "off_")) { opr->lmod = OPLM_DWORD; return 1; } - if (!strncmp(opr->name, "word_", 5)) { + if (IS_START(opr->name, "word_")) { opr->lmod = OPLM_WORD; return 1; } - if (!strncmp(opr->name, "byte_", 5)) { + if (IS_START(opr->name, "byte_")) { opr->lmod = OPLM_BYTE; return 1; } - if (!strncmp(opr->name, "qword_", 6)) { + if (IS_START(opr->name, "qword_")) { opr->lmod = OPLM_QWORD; return 1; } @@ -842,6 +845,7 @@ static const struct { { "movsx",OP_MOVSX, 2, 2, OPF_DATA }, { "xchg", OP_XCHG, 2, 2, OPF_DATA }, { "not", OP_NOT, 1, 1, OPF_DATA }, + { "xlat", OP_XLAT, 0, 0, OPF_DATA }, { "cdq", OP_CDQ, 0, 0, OPF_DATA }, { "lodsb",OP_LODS, 0, 0, OPF_DATA }, { "lodsw",OP_LODS, 0, 0, OPF_DATA }, @@ -869,6 +873,7 @@ static const struct { { "shr", OP_SHR, 2, 2, OPF_DATA|OPF_FLAGS }, { "sal", OP_SHL, 2, 2, OPF_DATA|OPF_FLAGS }, { "sar", OP_SAR, 2, 2, OPF_DATA|OPF_FLAGS }, + { "shld", OP_SHLD, 3, 3, OPF_DATA|OPF_FLAGS }, { "shrd", OP_SHRD, 3, 3, OPF_DATA|OPF_FLAGS }, { "rol", OP_ROL, 2, 2, OPF_DATA|OPF_FLAGS }, { "ror", OP_ROR, 2, 2, OPF_DATA|OPF_FLAGS }, @@ -890,6 +895,7 @@ static const struct { { "call", OP_CALL, 1, 1, OPF_JMP|OPF_DATA|OPF_FLAGS }, { "jmp", OP_JMP, 1, 1, OPF_JMP }, { "jecxz",OP_JECXZ, 1, 1, OPF_JMP|OPF_CJMP }, + { "loop", OP_LOOP, 1, 1, OPF_JMP|OPF_CJMP|OPF_DATA }, { "jo", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 0 }, // 70 OF=1 { "jno", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_O, 1 }, // 71 OF=0 { "jc", OP_JCC, 1, 1, OPF_CJMP_CC, PFO_C, 0 }, // 72 CF=1 @@ -1071,6 +1077,13 @@ static void parse_op(struct parsed_op *op, char words[16][256], int wordc) break; // ops with implicit argumets + case OP_XLAT: + op->operand_cnt = 2; + setup_reg_opr(&op->operand[0], xAX, OPLM_BYTE, &op->regmask_src); + op->regmask_dst = op->regmask_src; + setup_reg_opr(&op->operand[1], xDX, OPLM_DWORD, &op->regmask_src); + break; + case OP_CDQ: op->operand_cnt = 2; setup_reg_opr(&op->operand[0], xDX, OPLM_DWORD, &op->regmask_dst); @@ -1114,12 +1127,15 @@ static void parse_op(struct parsed_op *op, char words[16][256], int wordc) op->regmask_dst = op->regmask_src; break; + case OP_LOOP: + op->regmask_dst = 1 << xCX; + // fallthrough case OP_JECXZ: - op->operand_cnt = 1; + op->operand_cnt = 2; op->regmask_src = 1 << xCX; - op->operand[0].type = OPT_REG; - op->operand[0].reg = xCX; - op->operand[0].lmod = OPLM_DWORD; + op->operand[1].type = OPT_REG; + op->operand[1].reg = xCX; + op->operand[1].lmod = OPLM_DWORD; break; case OP_IMUL: @@ -1207,6 +1223,17 @@ static void parse_op(struct parsed_op *op, char words[16][256], int wordc) default: break; } + + if (op->operand[0].type == OPT_REG + && op->operand[0].lmod == OPLM_DWORD + && op->operand[1].type == OPT_CONST) + { + if ((op->op == OP_AND && op->operand[1].val == 0) + || (op->op == OP_OR && op->operand[1].val == ~0)) + { + op->regmask_src = 0; + } + } } static const char *op_name(struct parsed_op *po) @@ -2492,8 +2519,9 @@ static int scan_for_flag_set(int i, int magic, int *branched, while (i >= 0) { if (ops[i].cc_scratch == magic) { - ferr(&ops[i], "%s looped\n", __func__); - return -1; + // is this a problem? + //ferr(&ops[i], "%s looped\n", __func__); + return 0; } ops[i].cc_scratch = magic; @@ -2606,10 +2634,16 @@ static void patch_esp_adjust(struct parsed_op *po, int adj) static int scan_for_esp_adjust(int i, int opcnt, int adj_expect, int *adj, int *is_multipath, int do_update) { + int adj_expect_unknown = 0; struct parsed_op *po; int first_pop = -1; + int adj_best = 0; *adj = *is_multipath = 0; + if (adj_expect < 0) { + adj_expect_unknown = 1; + adj_expect = 32 * 4; // enough? + } for (; i < opcnt && *adj < adj_expect; i++) { if (g_labels[i] != NULL) @@ -2653,6 +2687,8 @@ static int scan_for_esp_adjust(int i, int opcnt, } *adj += lmod_bytes(po, po->operand[0].lmod); + if (*adj > adj_best) + adj_best = *adj; } else if (po->flags & (OPF_JMP|OPF_TAIL)) { if (po->op == OP_JMP && po->btj == NULL) { @@ -2667,12 +2703,15 @@ static int scan_for_esp_adjust(int i, int opcnt, break; if (po->pp != NULL && po->pp->is_stdcall) break; + if (adj_expect_unknown && first_pop >= 0) + break; // assume it's another cdecl call } } if (first_pop >= 0) { - // probably 'pop ecx' was used.. + // probably only 'pop ecx' was used + *adj = adj_best; return first_pop; } @@ -2824,7 +2863,7 @@ static void scan_for_call_type(int i, const struct parsed_opr *opr, if (i < 0) { // reached the top - can only be an arg-reg - if (opr->type != OPT_REG) + if (opr->type != OPT_REG || g_func_pp == NULL) return; for (i = 0; i < g_func_pp->argc; i++) { @@ -3102,22 +3141,25 @@ static void scan_prologue_epilogue(int opcnt) ferr(&ops[j], "'pop ebp' expected\n"); if (g_stack_fsz != 0) { - if (ops[j - 1].op == OP_MOV + if (ops[j].op == OP_LEAVE) + j--; + else if (ops[j].op == OP_POP + && ops[j - 1].op == OP_MOV && IS(opr_name(&ops[j - 1], 0), "esp") && IS(opr_name(&ops[j - 1], 1), "ebp")) { ops[j - 1].flags |= OPF_RMD | OPF_DONE; + j -= 2; } - else if (ops[j].op != OP_LEAVE - && !(g_ida_func_attr & IDAFA_NORETURN)) + else if (!(g_ida_func_attr & IDAFA_NORETURN)) { - ferr(&ops[j - 1], "esp restore expected\n"); + ferr(&ops[j], "esp restore expected\n"); } - if (ecx_push && ops[j - 2].op == OP_POP - && IS(opr_name(&ops[j - 2], 0), "ecx")) + if (ecx_push && j >= 0 && ops[j].op == OP_POP + && IS(opr_name(&ops[j], 0), "ecx")) { - ferr(&ops[j - 2], "unexpected ecx pop\n"); + ferr(&ops[j], "unexpected ecx pop\n"); } } @@ -3304,7 +3346,7 @@ static int resolve_origin(int i, const struct parsed_opr *opr, } if (ops[i].cc_scratch == magic) - return 0; + return ret; ops[i].cc_scratch = magic; if (!(ops[i].flags & OPF_DATA)) @@ -3314,13 +3356,14 @@ static int resolve_origin(int i, const struct parsed_opr *opr, if (*op_i >= 0) { if (*op_i == i) - return 1; + return ret | 1; + // XXX: could check if the other op does the same return -1; } *op_i = i; - return 1; + return ret | 1; } } @@ -3454,7 +3497,7 @@ static struct parsed_proto *process_call_early(int i, int opcnt, struct parsed_proto *pp; int multipath = 0; int adj = 0; - int ret; + int j, ret; pp = po->pp; if (pp == NULL || pp->is_vararg || pp->argc_reg != 0) @@ -3471,8 +3514,15 @@ static struct parsed_proto *process_call_early(int i, int opcnt, return NULL; if (multipath) return NULL; - if (ops[ret].op == OP_POP && adj != 4) - return NULL; + if (ops[ret].op == OP_POP) { + for (j = 1; j < adj / 4; j++) { + if (ops[ret + j].op != OP_POP + || ops[ret + j].operand[0].reg != xCX) + { + return NULL; + } + } + } } *adj_i = ret; @@ -3539,7 +3589,7 @@ static struct parsed_proto *process_call(int i, int opcnt) pp->is_fptr = 1; ret = scan_for_esp_adjust(i + 1, opcnt, - 32*4, &adj, &multipath, 0); + -1, &adj, &multipath, 0); if (ret < 0 || adj < 0) { if (!g_allow_regfunc) ferr(po, "non-__cdecl indirect call unhandled yet\n"); @@ -3560,9 +3610,11 @@ static struct parsed_proto *process_call(int i, int opcnt) // look for and make use of esp adjust multipath = 0; ret = -1; - if (!pp->is_stdcall && pp->argc_stack > 0) + if (!pp->is_stdcall && pp->argc_stack > 0) { + int adj_expect = pp->is_vararg ? -1 : pp->argc_stack * 4; ret = scan_for_esp_adjust(i + 1, opcnt, - pp->argc_stack * 4, &adj, &multipath, 0); + adj_expect, &adj, &multipath, 0); + } if (ret >= 0) { if (pp->is_vararg) { if (adj / 4 < pp->argc_stack) { @@ -3762,7 +3814,8 @@ static int collect_call_args_r(struct parsed_op *po, int i, if (pp->is_unresolved) break; - ferr(po, "arg collect %d/%d hit esp adjust of %d\n", + fnote(po, "(this call)\n"); + ferr(&ops[j], "arg collect %d/%d hit esp adjust of %d\n", arg, pp->argc, ops[j].operand[1].val); } else if (ops[j].op == OP_POP && !(ops[j].flags & OPF_DONE)) @@ -3770,7 +3823,8 @@ static int collect_call_args_r(struct parsed_op *po, int i, if (pp->is_unresolved) break; - ferr(po, "arg collect %d/%d hit pop\n", arg, pp->argc); + fnote(po, "(this call)\n"); + ferr(&ops[j], "arg collect %d/%d hit pop\n", arg, pp->argc); } else if (ops[j].flags & OPF_CJMP) { @@ -3836,7 +3890,9 @@ static int collect_call_args_r(struct parsed_op *po, int i, ops[j].flags &= ~OPF_RSAVE; // check for __VALIST - if (!pp->is_unresolved && pp->arg[arg].type.is_va_list) { + if (!pp->is_unresolved && g_func_pp != NULL + && pp->arg[arg].type.is_va_list) + { k = -1; ret = resolve_origin(j, &ops[j].operand[0], magic + 1, &k, NULL); @@ -4162,11 +4218,12 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) if (pp != NULL) { if (j >= 0) { // commit esp adjust - ops[j].flags |= OPF_RMD; if (ops[j].op != OP_POP) patch_esp_adjust(&ops[j], pp->argc_stack * 4); - else - ops[j].flags |= OPF_DONE; + else { + for (l = 0; l < pp->argc_stack; l++) + ops[j + l].flags |= OPF_DONE | OPF_RMD; + } } if (strstr(pp->ret_type.name, "int64")) @@ -4848,6 +4905,14 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) fprintf(fout, " %s = ~%s;", buf1, buf1); break; + case OP_XLAT: + assert_operand_cnt(2); + out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); + out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]); + fprintf(fout, " %s = *(u8 *)(%s + %s);", buf1, buf2, buf1); + strcpy(g_comment, "xlat"); + break; + case OP_CDQ: assert_operand_cnt(2); fprintf(fout, " %s = (s32)%s >> 31;", @@ -4981,22 +5046,20 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) // arithmetic w/flags case OP_AND: - if (po->operand[1].type == OPT_CONST && !po->operand[1].val) { - // deal with complex dst clear - assert_operand_cnt(2); - fprintf(fout, " %s = %s;", - out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), - out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], - default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0)); - output_std_flags(fout, po, &pfomask, buf1); - last_arith_dst = &po->operand[0]; - delayed_flag_op = NULL; - break; - } - // fallthrough + if (po->operand[1].type == OPT_CONST && !po->operand[1].val) + goto dualop_arith_const; + propagate_lmod(po, &po->operand[0], &po->operand[1]); + goto dualop_arith; + case OP_OR: propagate_lmod(po, &po->operand[0], &po->operand[1]); - // fallthrough + if (po->operand[1].type == OPT_CONST) { + j = lmod_bytes(po, po->operand[0].lmod); + if (((1ull << j * 8) - 1) == po->operand[1].val) + goto dualop_arith_const; + } + goto dualop_arith; + dualop_arith: assert_operand_cnt(2); fprintf(fout, " %s %s= %s;", @@ -5008,6 +5071,18 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) delayed_flag_op = NULL; break; + dualop_arith_const: + // and 0, or ~0 used instead mov + assert_operand_cnt(2); + fprintf(fout, " %s = %s;", + out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]), + out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], + default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0)); + output_std_flags(fout, po, &pfomask, buf1); + last_arith_dst = &po->operand[0]; + delayed_flag_op = NULL; + break; + case OP_SHL: case OP_SHR: assert_operand_cnt(2); @@ -5032,8 +5107,11 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) ferr(po, "TODO\n"); pfomask &= ~(1 << PFO_C); } - fprintf(fout, " %s %s= %s;", buf1, op_to_c(po), + fprintf(fout, " %s %s= %s", buf1, op_to_c(po), out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1])); + if (po->operand[1].type != OPT_CONST) + fprintf(fout, " & 0x1f"); + fprintf(fout, ";"); output_std_flags(fout, po, &pfomask, buf1); last_arith_dst = &po->operand[0]; delayed_flag_op = NULL; @@ -5050,6 +5128,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) delayed_flag_op = NULL; break; + case OP_SHLD: case OP_SHRD: assert_operand_cnt(3); propagate_lmod(po, &po->operand[0], &po->operand[1]); @@ -5057,9 +5136,18 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]); out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]); out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[2]); - fprintf(fout, " %s >>= %s; %s |= %s << (%d - %s);", - buf1, buf3, buf1, buf2, l, buf3); - strcpy(g_comment, "shrd"); + if (po->operand[2].type != OPT_CONST) + ferr(po, "TODO: masking\n"); + if (po->op == OP_SHLD) { + fprintf(fout, " %s <<= %s; %s |= %s >> (%d - %s);", + buf1, buf3, buf1, buf2, l, buf3); + strcpy(g_comment, "shld"); + } + else { + fprintf(fout, " %s >>= %s; %s |= %s << (%d - %s);", + buf1, buf3, buf1, buf2, l, buf3); + strcpy(g_comment, "shrd"); + } output_std_flags(fout, po, &pfomask, buf1); last_arith_dst = &po->operand[0]; delayed_flag_op = NULL; @@ -5368,6 +5456,12 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt) strcat(g_comment, "jecxz"); break; + case OP_LOOP: + fprintf(fout, " if (--ecx == 0)\n"); + fprintf(fout, " goto %s;", po->operand[0].name); + strcat(g_comment, "loop"); + break; + case OP_JMP: assert_operand_cnt(1); last_arith_dst = NULL; @@ -5767,6 +5861,25 @@ static int hg_var_cnt; static void output_hdr_fp(FILE *fout, const struct func_prototype *fp, int count); +struct func_prototype *hg_fp_add(const char *funcn) +{ + struct func_prototype *fp; + + if ((hg_fp_cnt & 0xff) == 0) { + hg_fp = realloc(hg_fp, sizeof(hg_fp[0]) * (hg_fp_cnt + 0x100)); + my_assert_not(hg_fp, NULL); + memset(hg_fp + hg_fp_cnt, 0, sizeof(hg_fp[0]) * 0x100); + } + + fp = &hg_fp[hg_fp_cnt]; + snprintf(fp->name, sizeof(fp->name), "%s", funcn); + fp->id = hg_fp_cnt; + fp->argc_stack = -1; + hg_fp_cnt++; + + return fp; +} + static struct func_proto_dep *hg_fp_find_dep(struct func_prototype *fp, const char *name) { @@ -5835,6 +5948,9 @@ static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits, po = &ops[i]; if ((po->flags & OPF_JMP) && po->op != OP_CALL) { + if (po->flags & OPF_RMD) + continue; + if (po->btj != NULL) { // jumptable for (j = 0; j < po->btj->count; j++) { @@ -5901,6 +6017,8 @@ static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits, } } + // if has_ret is 0, there is uninitialized eax path, + // which means it's most likely void func if (*has_ret != 0 && (po->flags & OPF_TAIL)) { if (po->op == OP_CALL) { j = i; @@ -5915,13 +6033,13 @@ static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits, ret = resolve_origin(i, &opr, i + opcnt * 4, &j, &from_caller); } - if (ret == -1 && from_caller) { + if (ret != 1 && from_caller) { // unresolved eax - probably void func *has_ret = 0; } else { - if (ops[j].op == OP_CALL) { - dep = hg_fp_find_dep(fp, po->operand[0].name); + if (j >= 0 && ops[j].op == OP_CALL) { + dep = hg_fp_find_dep(fp, ops[j].operand[0].name); if (dep != NULL) dep->ret_dep = 1; else @@ -5954,6 +6072,7 @@ static void gen_hdr(const char *funcn, int opcnt) { int save_arg_vars[MAX_ARG_GRP] = { 0, }; unsigned char cbits[MAX_OPS / 8]; + const struct parsed_proto *pp_c; struct parsed_proto *pp; struct func_prototype *fp; struct parsed_op *po; @@ -5961,29 +6080,15 @@ static void gen_hdr(const char *funcn, int opcnt) int regmask_dep; int max_bp_offset = 0; int has_ret; - int i, j, ret; - - if ((hg_fp_cnt & 0xff) == 0) { - hg_fp = realloc(hg_fp, sizeof(hg_fp[0]) * (hg_fp_cnt + 0x100)); - my_assert_not(hg_fp, NULL); - memset(hg_fp + hg_fp_cnt, 0, sizeof(hg_fp[0]) * 0x100); - } - - fp = &hg_fp[hg_fp_cnt]; - snprintf(fp->name, sizeof(fp->name), "%s", funcn); - fp->id = hg_fp_cnt; - fp->argc_stack = -1; - hg_fp_cnt++; + int i, j, l; + int ret; - // perhaps already in seed header? - fp->pp = proto_parse(g_fhdr, funcn, 1); - if (fp->pp != NULL) { - fp->argc_stack = fp->pp->argc_stack; - fp->is_stdcall = fp->pp->is_stdcall; - fp->regmask_dep = get_pp_arg_regmask(fp->pp); - fp->has_ret = !IS(fp->pp->ret_type.name, "void"); + pp_c = proto_parse(g_fhdr, funcn, 1); + if (pp_c != NULL) + // already in seed, will add to hg_fp later return; - } + + fp = hg_fp_add(funcn); g_bp_frame = g_sp_frame = g_stack_fsz = 0; g_stack_frame_used = 0; @@ -6058,11 +6163,12 @@ static void gen_hdr(const char *funcn, int opcnt) if (pp != NULL) { if (j >= 0) { // commit esp adjust - ops[j].flags |= OPF_RMD; if (ops[j].op != OP_POP) patch_esp_adjust(&ops[j], pp->argc_stack * 4); - else - ops[j].flags |= OPF_DONE; + else { + for (l = 0; l < pp->argc_stack; l++) + ops[j + l].flags |= OPF_DONE | OPF_RMD; + } } po->flags |= OPF_DONE; @@ -6113,13 +6219,17 @@ static void gen_hdr(const char *funcn, int opcnt) if (cbits[i >> 3] & (1 << (i & 7))) continue; + if (g_labels[i] == NULL && i > 0 && ops[i - 1].op == OP_CALL + && ops[i - 1].pp != NULL && ops[i - 1].pp->is_osinc) + { + // the compiler sometimes still generates code after + // noreturn OS functions + break; + } if (ops[i].op != OP_NOP) ferr(&ops[i], "unreachable code\n"); } - if (has_ret == -1 && (regmask_dep & (1 << xAX))) - has_ret = 1; - for (i = 0; i < g_eqcnt; i++) { if (g_eqs[i].offset > max_bp_offset && g_eqs[i].offset < 4*32) max_bp_offset = g_eqs[i].offset; @@ -6138,7 +6248,7 @@ static void gen_hdr(const char *funcn, int opcnt) printf("// has_ret %d, regmask_dep %x\n", fp->has_ret, fp->regmask_dep); output_hdr_fp(stdout, fp, 1); - if (IS(funcn, "sub_100073FD")) exit(1); + if (IS(funcn, "sub_10007F72")) exit(1); #endif gen_x_cleanup(opcnt); @@ -6167,7 +6277,7 @@ static void hg_fp_resolve_deps(struct func_prototype *fp) // printf("dep %s %s |= %x\n", fp->name, // fp->dep_func[i].name, dep); - if (fp->has_ret == -1) + if (fp->has_ret == -1 && fp->dep_func[i].ret_dep) fp->has_ret = fp->dep_func[i].proto->has_ret; } } @@ -6287,9 +6397,25 @@ static void output_hdr(FILE *fout) [OPLM_QWORD] = "uint64_t", }; const struct scanned_var *var; + struct func_prototype *fp; char line[256] = { 0, }; + char name[256]; int i; + // add stuff from headers + for (i = 0; i < pp_cache_size; i++) { + if (pp_cache[i].is_cinc && !pp_cache[i].is_stdcall) + snprintf(name, sizeof(name), "_%s", pp_cache[i].name); + else + snprintf(name, sizeof(name), "%s", pp_cache[i].name); + fp = hg_fp_add(name); + fp->pp = &pp_cache[i]; + fp->argc_stack = fp->pp->argc_stack; + fp->is_stdcall = fp->pp->is_stdcall; + fp->regmask_dep = get_pp_arg_regmask(fp->pp); + fp->has_ret = !IS(fp->pp->ret_type.name, "void"); + } + // resolve deps qsort(hg_fp, hg_fp_cnt, sizeof(hg_fp[0]), hg_fp_cmp_name); for (i = 0; i < hg_fp_cnt; i++) @@ -6329,30 +6455,6 @@ static void output_hdr(FILE *fout) fwrite(line, 1, strlen(line), fout); } -// read a line, truncating it if it doesn't fit -static char *my_fgets(char *s, size_t size, FILE *stream) -{ - char *ret, *ret2; - char buf[64]; - int p; - - p = size - 2; - if (p >= 0) - s[p] = 0; - - ret = fgets(s, size, stream); - if (ret != NULL && p >= 0 && s[p] != 0 && s[p] != '\n') { - p = sizeof(buf) - 2; - do { - buf[p] = 0; - ret2 = fgets(buf, sizeof(buf), stream); - } - while (ret2 != NULL && buf[p] != 0 && buf[p] != '\n'); - } - - return ret; -} - // '=' needs special treatment // also ' quote static char *next_word_s(char *w, size_t wsize, char *s) @@ -6452,6 +6554,11 @@ static void scan_variables(FILE *fasm) if (wordc < 2) continue; + if (IS_START(words[0], "__IMPORT_DESCRIPTOR_")) { + // when this starts, we don't need anything from this section + break; + } + if ((hg_var_cnt & 0xff) == 0) { hg_vars = realloc(hg_vars, sizeof(hg_vars[0]) * (hg_var_cnt + 0x100));