translate: fixes for pop scan
[ia32rtools.git] / tools / translate.c
index 1ae5c27..cca300f 100644 (file)
@@ -338,6 +338,7 @@ static int g_regmask_rm;
 static int g_skip_func;
 static int g_allow_regfunc;
 static int g_allow_user_icall;
+static int g_nowarn_reguse;
 static int g_quiet_pp;
 static int g_header_mode;
 
@@ -2707,8 +2708,9 @@ static const char *op_to_c(struct parsed_op *po)
 
 // note: this skips over calls and rm'd stuff assuming they're handled
 // so it's intended to use at one of final passes
+// exception: doesn't skip OPF_RSAVE stuff
 static int scan_for_pop(int i, int opcnt, int magic, int reg,
-  int depth, int seen_noreturn, int flags_set)
+  int depth, int seen_noreturn, int save_level, int flags_set)
 {
   struct parsed_op *po;
   int relevant;
@@ -2722,18 +2724,28 @@ static int scan_for_pop(int i, int opcnt, int magic, int reg,
     po->cc_scratch = magic;
 
     if (po->flags & OPF_TAIL) {
-      if (po->op == OP_CALL) {
-        if (po->pp != NULL && po->pp->is_noreturn)
-          seen_noreturn = 1;
-        else
+      if (po->op == OP_CALL && po->pp != NULL && po->pp->is_noreturn) {
+        // msvc sometimes generates stack cleanup code after
+        // noreturn, set a flag and continue
+        seen_noreturn = 1;
+
+        // ... but stop if there is another path to next insn -
+        // if msvc skipped something stack tracking may mess up
+        if (i + 1 < opcnt && g_labels[i + 1] != NULL)
           goto out;
       }
       else
         goto out;
     }
 
-    if (po->flags & (OPF_RMD|OPF_DONE|OPF_FARG))
+    if (po->flags & OPF_FARG)
       continue;
+    if (po->flags & (OPF_RMD|OPF_DONE)) {
+      if (!(po->flags & OPF_RSAVE))
+        continue;
+      // reprocess, there might be another push in some "parallel"
+      // path that took a pop what we should also take
+    }
 
     if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
       if (po->btj != NULL) {
@@ -2741,7 +2753,7 @@ static int scan_for_pop(int i, int opcnt, int magic, int reg,
         for (j = 0; j < po->btj->count; j++) {
           check_i(po, po->btj->d[j].bt_i);
           ret |= scan_for_pop(po->btj->d[j].bt_i, opcnt, magic, reg,
-                   depth, seen_noreturn, flags_set);
+                   depth, seen_noreturn, save_level, flags_set);
           if (ret < 0)
             return ret; // dead end
         }
@@ -2751,7 +2763,7 @@ static int scan_for_pop(int i, int opcnt, int magic, int reg,
       check_i(po, po->bt_i);
       if (po->flags & OPF_CJMP) {
         ret |= scan_for_pop(po->bt_i, opcnt, magic, reg,
-                 depth, seen_noreturn, flags_set);
+                 depth, seen_noreturn, save_level, flags_set);
         if (ret < 0)
           return ret; // dead end
       }
@@ -2773,6 +2785,13 @@ static int scan_for_pop(int i, int opcnt, int magic, int reg,
     }
     else if (po->op == OP_POP) {
       if (relevant && depth == 0) {
+        if (flags_set == 0 && save_level > 0) {
+          ret = scan_for_pop(i + 1, opcnt, magic, reg,
+                  depth, seen_noreturn, save_level - 1, flags_set);
+          if (ret != 1)
+            // no pop for other levels, current one must be false
+            return -1;
+        }
         po->flags |= flags_set;
         return 1;
       }
@@ -4231,7 +4250,7 @@ static void check_simple_sequence(int opcnt, int *fsz)
   }
 
   // unlike pushes after sub esp,
-  // IDA treats pushed like this as part of var area
+  // IDA treats pushes like this as part of var area
   *fsz += seq_len * 4;
 }
 
@@ -4254,7 +4273,11 @@ static int scan_prologue(int i, int opcnt, int *ecx_push, int *esp_sub)
   for (; i < opcnt; i++) {
     if (i > 0 && g_labels[i] != NULL)
       break;
-    if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
+    if (ops[i].flags & (OPF_JMP|OPF_TAIL))
+      break;
+    if (ops[i].flags & OPF_DONE)
+      continue;
+    if (ops[i].op == OP_PUSH)
       break;
     if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
       && ops[i].operand[1].type == OPT_CONST)
@@ -4292,8 +4315,8 @@ static int scan_prologue(int i, int opcnt, int *ecx_push, int *esp_sub)
         ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
         i = j + 1;
         *esp_sub = 1;
+        break;
       }
-      break;
     }
   }
 
@@ -4487,10 +4510,12 @@ static void scan_prologue_epilogue(int opcnt, int *stack_align)
         for (; j >= 0; j--) {
           if (ops[j].op != OP_MOV)
             break;
-          if (ops[j].operand[0].type != OPT_REGMEM)
-            break;
-          if (strstr(ops[j].operand[0].name, "arg_") == NULL)
-            break;
+          if (ops[j].operand[0].type == OPT_REGMEM
+              && strstr(ops[j].operand[0].name, "arg_") != NULL)
+            continue;
+          if (ops[j].operand[0].type == OPT_REG)
+            continue; // assume arg-reg mov
+          break;
         }
       }
 
@@ -5654,6 +5679,14 @@ static int collect_call_args(struct parsed_op *po, int i,
   if (ret < 0)
     return ret;
 
+  if (pp->is_unresolved) {
+    pp->argc += ret;
+    pp->argc_stack += ret;
+    for (a = 0; a < pp->argc; a++)
+      if (pp->arg[a].type.name == NULL)
+        pp->arg[a].type.name = strdup("int");
+  }
+
   if (arg_grp != 0) {
     // propagate arg_grp
     for (a = 0; a < pp->argc; a++) {
@@ -5668,14 +5701,6 @@ static int collect_call_args(struct parsed_op *po, int i,
     }
   }
 
-  if (pp->is_unresolved) {
-    pp->argc += ret;
-    pp->argc_stack += ret;
-    for (a = 0; a < pp->argc; a++)
-      if (pp->arg[a].type.name == NULL)
-        pp->arg[a].type.name = strdup("int");
-  }
-
   return ret;
 }
 
@@ -5726,6 +5751,8 @@ static void reg_use_pass(int i, int opcnt, unsigned char *cbits,
       && !g_func_pp->is_userstack
       && po->operand[0].type == OPT_REG)
     {
+      int save_level = 0;
+
       reg = po->operand[0].reg;
       ferr_assert(po, reg >= 0);
 
@@ -5734,12 +5761,14 @@ static void reg_use_pass(int i, int opcnt, unsigned char *cbits,
       if (regmask_now & (1 << reg)) {
         already_saved = regmask_save_now & (1 << reg);
         flags_set = OPF_RSAVE | OPF_DONE;
+        save_level++;
       }
 
-      ret = scan_for_pop(i + 1, opcnt, i + opcnt * 3, reg, 0, 0, 0);
+      ret = scan_for_pop(i + 1, opcnt, i + opcnt * 3,
+              reg, 0, 0, save_level, 0);
       if (ret == 1) {
         scan_for_pop(i + 1, opcnt, i + opcnt * 4,
-          reg, 0, 0, flags_set);
+          reg, 0, 0, save_level, flags_set);
       }
       else {
         ret = scan_for_pop_ret(i + 1, opcnt, po->operand[0].reg, 0);
@@ -6328,11 +6357,16 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
             regmask_stack |= 1 << tmp_op->operand[0].reg;
         }
 
-        if (!((regmask_stack & (1 << xCX))
-          && (regmask_stack & (1 << xDX))))
+        // quick dumb check for potential reg-args
+        for (j = i - 1; j >= 0 && ops[j].op == OP_MOV; j--)
+          if (ops[j].operand[0].type == OPT_REG)
+            regmask_stack &= ~(1 << ops[j].operand[0].reg);
+
+        if ((regmask_stack & (mxCX|mxDX)) != (mxCX|mxDX)
+            && ((regmask | regmask_arg) & (mxCX|mxDX)))
         {
           if (pp->argc_stack != 0
-           || ((regmask | regmask_arg) & ((1 << xCX)|(1 << xDX))))
+              || ((regmask | regmask_arg) & (mxCX|mxDX)))
           {
             pp_insert_reg_arg(pp, "ecx");
             pp->is_fastcall = 1;
@@ -6340,7 +6374,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
             regmask |= 1 << xCX;
           }
           if (pp->argc_stack != 0
-           || ((regmask | regmask_arg) & (1 << xDX)))
+              || ((regmask | regmask_arg) & mxDX))
           {
             pp_insert_reg_arg(pp, "edx");
             regmask_init |= 1 << xDX;
@@ -6353,7 +6387,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
           pp->is_stdcall = 1;
       }
       if (!(po->flags & OPF_TAIL)
-          && !(g_sct_func_attr & SCTFA_NOWARN))
+          && !(g_sct_func_attr & SCTFA_NOWARN) && !g_nowarn_reguse)
       {
         // treat al write as overwrite to avoid many false positives
         if (IS(pp->ret_type.name, "void") || pp->ret_type.is_float) {
@@ -6461,8 +6495,15 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
     default:
       break;
     }
+  }
+
+  // pass8: final adjustments
+  for (i = 0; i < opcnt; i++)
+  {
+    po = &ops[i];
+    if (po->flags & (OPF_RMD|OPF_DONE))
+      continue;
 
-    // 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);
 
@@ -8329,6 +8370,7 @@ struct func_prototype {
   unsigned int dep_resolved:1;
   unsigned int is_stdcall:1;
   unsigned int eax_pass:1;       // returns without touching eax
+  unsigned int ptr_taken:1;      // pointer taken of this func
   struct func_proto_dep *dep_func;
   int dep_func_cnt;
   const struct parsed_proto *pp; // seed pp, if any
@@ -8341,6 +8383,7 @@ struct func_proto_dep {
   unsigned int ret_dep:1;       // return from this is caller's return
   unsigned int has_ret:1;       // found from eax use after return
   unsigned int has_ret64:1;
+  unsigned int ptr_taken:1;     // pointer taken, not a call
 };
 
 static struct func_prototype *hg_fp;
@@ -8392,10 +8435,14 @@ static struct func_proto_dep *hg_fp_find_dep(struct func_prototype *fp,
   return NULL;
 }
 
-static void hg_fp_add_dep(struct func_prototype *fp, const char *name)
+static void hg_fp_add_dep(struct func_prototype *fp, const char *name,
+  unsigned int ptr_taken)
 {
+  struct func_proto_dep * dep;
+
   // is it a dupe?
-  if (hg_fp_find_dep(fp, name))
+  dep = hg_fp_find_dep(fp, name);
+  if (dep != NULL && dep->ptr_taken == ptr_taken)
     return;
 
   if ((fp->dep_func_cnt & 0xff) == 0) {
@@ -8406,6 +8453,7 @@ static void hg_fp_add_dep(struct func_prototype *fp, const char *name)
       sizeof(fp->dep_func[0]) * 0x100);
   }
   fp->dep_func[fp->dep_func_cnt].name = strdup(name);
+  fp->dep_func[fp->dep_func_cnt].ptr_taken = ptr_taken;
   fp->dep_func_cnt++;
 }
 
@@ -8500,11 +8548,13 @@ static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits,
       if (po->flags & OPF_DONE)
         continue;
 
-      ret = scan_for_pop(i + 1, opcnt, i + opcnt * 2, reg, 0, 0, 0);
+      ret = scan_for_pop(i + 1, opcnt, i + opcnt * 2,
+              reg, 0, 0, 0, 0);
       if (ret == 1) {
         regmask_save |= 1 << reg;
         po->flags |= OPF_RMD;
-        scan_for_pop(i + 1, opcnt, i + opcnt * 3, reg, 0, 0, OPF_RMD);
+        scan_for_pop(i + 1, opcnt, i + opcnt * 3,
+          reg, 0, 0, 0, OPF_RMD);
         continue;
       }
     }
@@ -8602,6 +8652,7 @@ static void gen_hdr(const char *funcn, int opcnt)
   struct func_prototype *fp;
   struct func_proto_dep *dep;
   struct parsed_op *po;
+  const char *tmpname;
   int regmask_dummy = 0;
   int regmask_dep;
   int regmask_use;
@@ -8633,6 +8684,7 @@ static void gen_hdr(const char *funcn, int opcnt)
   // pass3:
   // - remove dead labels
   // - collect calls
+  // - collect function ptr refs
   for (i = 0; i < opcnt; i++)
   {
     if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
@@ -8646,9 +8698,19 @@ static void gen_hdr(const char *funcn, int opcnt)
 
     if (po->op == OP_CALL) {
       if (po->operand[0].type == OPT_LABEL)
-        hg_fp_add_dep(fp, opr_name(po, 0));
+        hg_fp_add_dep(fp, opr_name(po, 0), 0);
       else if (po->pp != NULL)
-        hg_fp_add_dep(fp, po->pp->name);
+        hg_fp_add_dep(fp, po->pp->name, 0);
+    }
+    else if (po->op == OP_MOV && po->operand[1].type == OPT_OFFSET) {
+      tmpname = opr_name(po, 1);
+      if (IS_START(tmpname, "p_") || IS_START(tmpname, "sub_"))
+        hg_fp_add_dep(fp, tmpname, 1);
+    }
+    else if (po->op == OP_PUSH && po->operand[0].type == OPT_OFFSET) {
+      tmpname = opr_name(po, 0);
+      if (IS_START(tmpname, "p_") || IS_START(tmpname, "sub_"))
+        hg_fp_add_dep(fp, tmpname, 1);
     }
   }
 
@@ -8814,6 +8876,11 @@ static void hg_fp_resolve_deps(struct func_prototype *fp)
     dep->proto = bsearch(&fp_s, hg_fp, hg_fp_cnt,
       sizeof(hg_fp[0]), hg_fp_cmp_name);
     if (dep->proto != NULL) {
+      if (dep->ptr_taken) {
+        dep->proto->ptr_taken = 1;
+        continue;
+      }
+
       if (!dep->proto->dep_resolved)
         hg_fp_resolve_deps(dep->proto);
 
@@ -8843,11 +8910,8 @@ static void do_func_refs_from_data(void)
     strcpy(fp_s.name, hg_refs[i]);
     fp = bsearch(&fp_s, hg_fp, hg_fp_cnt,
       sizeof(hg_fp[0]), hg_fp_cmp_name);
-    if (fp == NULL)
-      continue;
-
-    if (fp->argc_stack != 0 && (fp->regmask_dep & (mxCX | mxDX)))
-      fp->regmask_dep |= mxCX | mxDX;
+    if (fp != NULL)
+      fp->ptr_taken = 1;
   }
 }
 
@@ -8897,6 +8961,12 @@ static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
 
     regmask_dep = fp->regmask_dep;
     argc_normal = fp->argc_stack;
+    if (fp->ptr_taken && regmask_dep
+        && (regmask_dep & ~(mxCX|mxDX)) == 0)
+    {
+      if ((regmask_dep & mxDX) || fp->argc_stack > 0)
+        regmask_dep |= mxCX | mxDX;
+    }
 
     fprintf(fout, "%-5s",
       fp->pp ? fp->pp->ret_type.name :
@@ -9441,6 +9511,8 @@ int main(int argc, char *argv[])
       g_allow_regfunc = 1;
     else if (IS(argv[arg], "-uc"))
       g_allow_user_icall = 1;
+    else if (IS(argv[arg], "-wu"))
+      g_nowarn_reguse = 1;
     else if (IS(argv[arg], "-m"))
       multi_seg = 1;
     else if (IS(argv[arg], "-hdr"))
@@ -9450,13 +9522,14 @@ int main(int argc, char *argv[])
   }
 
   if (argc < arg + 3) {
-    printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdr.h> [rlist]*\n"
+    printf("usage:\n%s [options] <.c> <.asm> <hdr.h> [rlist]*\n"
            "%s -hdr <out.h> <.asm> <seed.h> [rlist]*\n"
            "options:\n"
            "  -hdr - header generation mode\n"
            "  -rf  - allow unannotated indirect calls\n"
            "  -uc  - allow ind. calls/refs to __usercall\n"
            "  -m   - allow multiple .text sections\n"
+           "  -wu  - don't warn about bad reg use\n"
            "[rlist] is a file with function names to skip,"
            " one per line\n",
       argv[0], argv[0]);