translate: fabs, another prologue, one va_arg
[ia32rtools.git] / tools / translate.c
index cca300f..e2db06d 100644 (file)
@@ -18,6 +18,7 @@
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <string.h>
 #include <math.h>
 #include <errno.h>
@@ -140,6 +141,7 @@ enum op_op {
   OP_FLDc,
   OP_FST,
   OP_FIST,
+  OP_FABS,
   OP_FADD,
   OP_FDIV,
   OP_FMUL,
@@ -1112,6 +1114,7 @@ static const struct {
   { "fstp",   OP_FST,    1, 1, OPF_FPOP },
   { "fist",   OP_FIST,   1, 1, OPF_FINT },
   { "fistp",  OP_FIST,   1, 1, OPF_FPOP|OPF_FINT },
+  { "fabs",   OP_FABS,   0, 0, 0 },
   { "fadd",   OP_FADD,   0, 2, 0 },
   { "faddp",  OP_FADD,   0, 2, OPF_FPOP },
   { "fdiv",   OP_FDIV,   0, 2, 0 },
@@ -1506,6 +1509,7 @@ static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
   case OP_FISUB:
   case OP_FIDIVR:
   case OP_FISUBR:
+  case OP_FABS:
   case OP_FCHS:
   case OP_FCOS:
   case OP_FSIN:
@@ -1935,6 +1939,9 @@ static int parse_stack_esp_offset(struct parsed_op *po,
   return 0;
 }
 
+// returns g_func_pp arg number if arg is accessed
+// -1 otherwise (stack vars, va_list)
+// note: 'popr' must be from 'po', not some other op
 static int stack_frame_access(struct parsed_op *po,
   struct parsed_opr *popr, char *buf, size_t buf_size,
   const char *name, const char *cast, int is_src, int is_lea)
@@ -1965,13 +1972,15 @@ static int stack_frame_access(struct parsed_op *po,
     arg_i = (offset - stack_ra - 4) / 4;
     if (arg_i < 0 || arg_i >= g_func_pp->argc_stack)
     {
-      if (g_func_pp->is_vararg
-          && arg_i == g_func_pp->argc_stack && is_lea)
-      {
-        // should be va_list
-        if (cast[0] == 0)
-          cast = "(u32)";
-        snprintf(buf, buf_size, "%sap", cast);
+      if (g_func_pp->is_vararg && arg_i == g_func_pp->argc_stack) {
+        if (is_lea) {
+          // should be va_list
+          if (cast[0] == 0)
+            cast = "(u32)";
+          snprintf(buf, buf_size, "%sap", cast);
+        }
+        else
+          snprintf(buf, buf_size, "%sva_arg(ap, u32)", cast);
         return -1;
       }
       ferr(po, "offset 0x%x (%s,%d) doesn't map to any arg\n",
@@ -3630,8 +3639,8 @@ static const struct parsed_proto *try_recover_pp(
 }
 
 static void scan_for_call_type(int i, const struct parsed_opr *opr,
-  int magic, const struct parsed_proto **pp_found, int *pp_i,
-  int *multi)
+  int magic, int is_call_op, const struct parsed_proto **pp_found,
+  int *pp_i, int *multi)
 {
   const struct parsed_proto *pp = NULL;
   struct parsed_op *po;
@@ -3644,7 +3653,8 @@ static void scan_for_call_type(int i, const struct parsed_opr *opr,
       lr = &g_label_refs[i];
       for (; lr != NULL; lr = lr->next) {
         check_i(&ops[i], lr->i);
-        scan_for_call_type(lr->i, opr, magic, pp_found, pp_i, multi);
+        scan_for_call_type(lr->i, opr, magic, is_call_op,
+          pp_found, pp_i, multi);
       }
       if (i > 0 && LAST_OP(i - 1))
         return;
@@ -3689,29 +3699,30 @@ static void scan_for_call_type(int i, const struct parsed_opr *opr,
     if (i == g_func_pp->argc)
       return;
     pp = g_func_pp->arg[i].pp;
-    if (pp == NULL)
-      ferr(po, "icall: arg%d (%s) is not a fptr?\n",
-        i + 1, g_func_pp->arg[i].reg);
+    if (pp == NULL) {
+      if (is_call_op)
+        ferr(po, "icall: arg%d (%s) is not a fptr?\n",
+          i + 1, g_func_pp->arg[i].reg);
+      return;
+    }
     check_func_pp(po, pp, "icall reg-arg");
   }
   else
-    pp = try_recover_pp(po, opr, 1, NULL);
+    pp = try_recover_pp(po, opr, is_call_op, NULL);
 
   if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
-    if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
-      || (*pp_found)->is_stdcall != pp->is_stdcall
-      //|| (*pp_found)->is_fptr != pp->is_fptr
-      || (*pp_found)->argc != pp->argc
-      || (*pp_found)->argc_reg != pp->argc_reg
-      || (*pp_found)->argc_stack != pp->argc_stack)
-    {
+    if (pp_cmp_func(*pp_found, pp)) {
+      if (pp_i != NULL && *pp_i != -1)
+        fnote(&ops[*pp_i], "(other ref)\n");
       ferr(po, "icall: parsed_proto mismatch\n");
     }
-    *multi = 1;
+    if (multi != NULL)
+      *multi = 1;
   }
   if (pp != NULL) {
     *pp_found = pp;
-    *pp_i = po - ops;
+    if (pp_i != NULL)
+      *pp_i = po - ops;
   }
 }
 
@@ -3915,7 +3926,8 @@ static void resolve_branches_parse_calls(int opcnt)
           po->operand_cnt = 0;
           po->regmask_src = pseudo_ops[l].regmask_src;
           po->regmask_dst = pseudo_ops[l].regmask_dst;
-          po->flags = pseudo_ops[l].flags;
+          po->flags &= OPF_TAIL;
+          po->flags |= pseudo_ops[l].flags;
           po->flags |= po->regmask_dst ? OPF_DATA : 0;
           break;
         }
@@ -3976,7 +3988,8 @@ static void resolve_branches_parse_calls(int opcnt)
     if (po->bt_i != -1 || (po->flags & OPF_RMD))
       continue;
 
-    if (po->operand[0].type == OPT_LABEL)
+    if (po->operand[0].type == OPT_LABEL
+        || po->operand[0].type == OPT_REG)
       // assume tail call
       goto tailcall;
 
@@ -4254,20 +4267,72 @@ static void check_simple_sequence(int opcnt, int *fsz)
   *fsz += seq_len * 4;
 }
 
+static int scan_prologue_ecx(int i, int opcnt, int flags_set,
+  int limit, int *ecx_push_out)
+{
+  const struct parsed_proto *pp;
+  int ecx_push = 0, other_push = 0;
+  int ret;
+
+  while (limit > 0 && ops[i].op == OP_PUSH
+         && IS(opr_name(&ops[i], 0), "ecx"))
+  {
+    ops[i].flags |= flags_set;
+    ecx_push++;
+    i++;
+    limit--;
+  }
+
+  ret = i;
+  if (ecx_push == 0 || flags_set != 0)
+    goto out;
+
+  // check if some of the pushes aren't really call args
+  for (; i < opcnt; i++) {
+    if (i > 0 && g_labels[i] != NULL)
+      break;
+    if (ops[i].flags & (OPF_JMP|OPF_TAIL))
+      break;
+    if (ops[i].op == OP_PUSH)
+      other_push++;
+  }
+
+  if (ops[i].op != OP_CALL)
+    goto out;
+
+  pp = ops[i].pp;
+  if (pp == NULL && ops[i].operand[0].type == OPT_LABEL)
+    pp = proto_parse(g_fhdr, opr_name(&ops[i], 0), 1);
+  if (pp == NULL)
+    goto out;
+
+  ferr_assert(&ops[i], ecx_push + other_push >= pp->argc_stack);
+  if (other_push < pp->argc_stack)
+    ecx_push -= pp->argc_stack - other_push;
+
+out:
+  if (ecx_push_out != NULL)
+    *ecx_push_out = ecx_push;
+  return ret;
+}
+
 static int scan_prologue(int i, int opcnt, int *ecx_push, int *esp_sub)
 {
   const char *name;
   int j, len, ret;
+  int ecx_tmp = 0;
 
   for (; i < opcnt; i++)
     if (!(ops[i].flags & OPF_DONE))
       break;
 
-  while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
-    ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
-    g_stack_fsz += 4;
-    (*ecx_push)++;
-    i++;
+  ret = scan_prologue_ecx(i, opcnt, 0, 4, &ecx_tmp);
+  if (ecx_tmp > 0) {
+    scan_prologue_ecx(i, opcnt, OPF_RMD | OPF_DONE | OPF_NOREGS,
+      ecx_tmp, NULL);
+    g_stack_fsz += 4 * ecx_tmp;
+    *ecx_push += ecx_tmp;
+    i = ret;
   }
 
   for (; i < opcnt; i++) {
@@ -4455,39 +4520,6 @@ static void scan_prologue_epilogue(int opcnt, int *stack_align)
   check_simple_sequence(opcnt, &push_fsz);
   i = scan_prologue(0, opcnt, &ecx_push, &esp_sub);
 
-  if (ecx_push && !esp_sub) {
-    // could actually be args for a call..
-    for (; i < opcnt; i++)
-      if (ops[i].op != OP_PUSH)
-        break;
-
-    if (ops[i].op == OP_CALL && ops[i].operand[0].type == OPT_LABEL) {
-      const struct parsed_proto *pp;
-      pp = proto_parse(g_fhdr, opr_name(&ops[i], 0), 1);
-      j = pp ? pp->argc_stack : 0;
-      while (i > 0 && j > 0) {
-        i--;
-        if (ops[i].op == OP_PUSH) {
-          ops[i].flags &= ~(OPF_RMD | OPF_DONE | OPF_NOREGS);
-          j--;
-        }
-      }
-      if (j != 0)
-        ferr(&ops[i], "unhandled prologue\n");
-
-      // recheck
-      i = ecx_push = 0;
-      g_stack_fsz = g_seh_size;
-      while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
-        if (!(ops[i].flags & OPF_RMD))
-          break;
-        g_stack_fsz += 4;
-        ecx_push++;
-        i++;
-      }
-    }
-  }
-
   found = 0;
   if (ecx_push || esp_sub)
   {
@@ -4653,6 +4685,17 @@ static int resolve_origin(int i, const struct parsed_opr *opr,
   }
 }
 
+static int resolve_origin_reg(int i, int reg, int magic, int *op_i,
+  int *is_caller)
+{
+  struct parsed_opr opr = OPR_INIT(OPT_REG, OPLM_DWORD, reg);
+
+  *op_i = -1;
+  if (is_caller != NULL)
+    *is_caller = 0;
+  return resolve_origin(i, &opr, magic, op_i, is_caller);
+}
+
 // find an instruction that previously referenced opr
 // if multiple results are found - fail
 // *op_i must be set to -1 by the caller
@@ -4926,9 +4969,8 @@ static int resolve_used_bits(int i, int opcnt, int reg,
 }
 
 static const struct parsed_proto *resolve_deref(int i, int magic,
-  struct parsed_opr *opr, int level)
+  const struct parsed_opr *opr, int level)
 {
-  struct parsed_opr opr_s = OPR_INIT(OPT_REG, OPLM_DWORD, 0);
   const struct parsed_proto *pp = NULL;
   int from_caller = 0;
   char s_reg[4];
@@ -4950,8 +4992,7 @@ static const struct parsed_proto *resolve_deref(int i, int magic,
   if (reg < 0)
     return NULL;
 
-  opr_s.reg = reg;
-  ret = resolve_origin(i, &opr_s, i + magic, &j, NULL);
+  ret = resolve_origin_reg(i, reg, i + magic, &j, NULL);
   if (ret != 1)
     return NULL;
 
@@ -4966,8 +5007,7 @@ static const struct parsed_proto *resolve_deref(int i, int magic,
             ops[j].operand[1].name);
     if (reg < 0)
       return NULL;
-    opr_s.reg = reg;
-    ret = resolve_origin(j, &opr_s, j + magic, &k, NULL);
+    ret = resolve_origin_reg(j, reg, j + magic, &k, NULL);
     if (ret != 1)
       return NULL;
     j = k;
@@ -5017,32 +5057,34 @@ static const struct parsed_proto *resolve_deref(int i, int magic,
   return proto_lookup_struct(g_fhdr, pp->type.name, offset);
 }
 
-static const struct parsed_proto *resolve_icall(int i, int opcnt,
+static const struct parsed_proto *resolve_func_ptr(int i, int opcnt,
+  int is_call_op, const struct parsed_opr *opr,
   int *pp_i, int *multi_src)
 {
   const struct parsed_proto *pp = NULL;
   int search_advice = 0;
 
-  *multi_src = 0;
-  *pp_i = -1;
+  if (multi_src != NULL)
+    *multi_src = 0;
+  if (pp_i != NULL)
+    *pp_i = -1;
 
-  switch (ops[i].operand[0].type) {
+  switch (opr->type) {
   case OPT_REGMEM:
     // try to resolve struct member calls
-    pp = resolve_deref(i, i + opcnt * 19, &ops[i].operand[0], 0);
+    pp = resolve_deref(i, i + opcnt * 19, opr, 0);
     if (pp != NULL)
       break;
     // fallthrough
   case OPT_LABEL:
   case OPT_OFFSET:
-    pp = try_recover_pp(&ops[i], &ops[i].operand[0],
-           1, &search_advice);
+    pp = try_recover_pp(&ops[i], opr, is_call_op, &search_advice);
     if (!search_advice)
       break;
     // fallthrough
   default:
-    scan_for_call_type(i, &ops[i].operand[0], i + opcnt * 9, &pp,
-      pp_i, multi_src);
+    scan_for_call_type(i, opr, i + opcnt * 9, is_call_op,
+      &pp, pp_i, multi_src);
     break;
   }
 
@@ -5103,7 +5145,8 @@ static struct parsed_proto *process_call(int i, int opcnt)
   if (pp == NULL)
   {
     // indirect call
-    pp_c = resolve_icall(i, opcnt, &call_i, &multipath);
+    pp_c = resolve_func_ptr(i, opcnt, 1, &ops[i].operand[0],
+             &call_i, &multipath);
     if (pp_c != NULL) {
       if (!pp_c->is_func && !pp_c->is_fptr)
         ferr(po, "call to non-func: %s\n", pp_c->name);
@@ -5211,6 +5254,48 @@ out:
   return pp;
 }
 
+static void check_fptr_args(int i, int opcnt, struct parsed_proto *pp)
+{
+  struct parsed_opr s_opr = OPR_INIT(OPT_REG, OPLM_DWORD, 0);
+  const struct parsed_proto *pp_arg, *pp_cmp;
+  const struct parsed_op *po_a;
+  const char *s_reg;
+  int pp_cmp_i;
+  int arg, reg;
+
+  for (arg = 0; arg < pp->argc; arg++) {
+    pp_cmp = NULL;
+    pp_cmp_i = -1;
+
+    pp_arg = pp->arg[arg].pp;
+    if (pp_arg == NULL || !pp_arg->is_func)
+      continue;
+
+    s_reg = pp->arg[arg].reg;
+    if (s_reg != NULL) {
+      reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s_reg);
+      ferr_assert(&ops[i], reg >= 0);
+      s_opr.reg = reg;
+      scan_for_call_type(i, &s_opr, i + arg + opcnt * 28, 0,
+        &pp_cmp, &pp_cmp_i, NULL);
+    }
+    else {
+      po_a = pp->arg[arg].datap;
+      if (po_a != NULL && po_a->op == OP_PUSH)
+        pp_cmp = resolve_func_ptr(po_a - ops, opcnt, 0,
+                   &po_a->operand[0], &pp_cmp_i, NULL);
+      if (pp_cmp_i < 0)
+        pp_cmp_i = po_a - ops;
+    }
+
+    if (pp_cmp != NULL && !pp_compatible_func(pp_arg, pp_cmp)) {
+      if (pp_cmp_i >= 0)
+        fnote(&ops[pp_cmp_i], "(referenced here)\n");
+      ferr(&ops[i], "incompatible fptr arg %d\n", arg + 1);
+    }
+  }
+}
+
 static void mark_float_arg(struct parsed_op *po,
   struct parsed_proto *pp, int arg, int *regmask_ffca)
 {
@@ -5297,8 +5382,8 @@ static int collect_call_args_no_push(int i, struct parsed_proto *pp,
   return 0;
 }
 
-static int collect_call_args_early(int i, struct parsed_proto *pp,
-  int *regmask, int *regmask_ffca)
+static int collect_call_args_early(int i, int opcnt,
+  struct parsed_proto *pp, int *regmask, int *regmask_ffca)
 {
   struct parsed_op *po;
   int arg, ret;
@@ -5400,6 +5485,9 @@ static int collect_call_args_early(int i, struct parsed_proto *pp,
     }
   }
 
+  if (!g_header_mode)
+    check_fptr_args(i, opcnt, pp);
+
   return 0;
 }
 
@@ -5664,7 +5752,7 @@ static int collect_call_args_r(struct parsed_op *po, int i,
   return arg;
 }
 
-static int collect_call_args(struct parsed_op *po, int i,
+static int collect_call_args(struct parsed_op *po, int i, int opcnt,
   struct parsed_proto *pp, int *regmask, int magic)
 {
   // arg group is for cases when pushes for
@@ -5701,6 +5789,9 @@ static int collect_call_args(struct parsed_op *po, int i,
     }
   }
 
+  if (!g_header_mode)
+    check_fptr_args(i, opcnt, pp);
+
   return ret;
 }
 
@@ -6137,7 +6228,8 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
       if (pp != NULL) {
         if (!(po->flags & OPF_ATAIL)) {
           // since we know the args, try to collect them
-          ret = collect_call_args_early(i, pp, &regmask, &regmask_ffca);
+          ret = collect_call_args_early(i, opcnt, pp,
+                  &regmask, &regmask_ffca);
           if (ret != 0)
             pp = NULL;
         }
@@ -6182,7 +6274,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
 
         if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
           // since we know the args, collect them
-          collect_call_args(po, i, pp, &regmask, i + opcnt * 2);
+          collect_call_args(po, i, opcnt, pp, &regmask, i + opcnt * 2);
         }
         // for unresolved, collect after other passes
       }
@@ -6342,7 +6434,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
 
       if (pp->is_unresolved) {
         int regmask_stack = 0;
-        collect_call_args(po, i, pp, &regmask, i + opcnt * 2);
+        collect_call_args(po, i, opcnt, pp, &regmask, i + opcnt * 2);
 
         // this is pretty rough guess:
         // see ecx and edx were pushed (and not their saved versions)
@@ -6861,7 +6953,10 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
            || (tmp_op && (tmp_op->op == OP_AND || tmp_op->op == OP_OR))
            ))
       {
-        out_src_opr_u32(buf3, sizeof(buf3), po, last_arith_dst);
+        struct parsed_op *po_arith = (void *)((char *)last_arith_dst
+          - offsetof(struct parsed_op, operand[0]));
+        ferr_assert(po, &ops[po_arith - ops] == po_arith);
+        out_src_opr_u32(buf3, sizeof(buf3), po_arith, last_arith_dst);
         out_test_for_cc(buf1, sizeof(buf1), po, po->pfo, po->pfo_inv,
           last_arith_dst->lmod, buf3);
         is_delayed = 1;
@@ -7834,6 +7929,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         break;
 
       case OP_RET:
+      do_tail:
         if (g_func_pp->is_vararg)
           fprintf(fout, "  va_end(ap);\n");
         if (g_func_pp->has_retreg) {
@@ -8038,6 +8134,11 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         strcat(g_comment, " fist");
         break;
 
+      case OP_FABS:
+        fprintf(fout, "  %s = fabs%s(%s);", float_st0,
+          need_double ? "" : "f", float_st0);
+        break;
+
       case OP_FADD:
       case OP_FDIV:
       case OP_FMUL:
@@ -8249,7 +8350,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
             fprintf(fout, " f_st0 = f_st1;");
         }
         strcat(g_comment, " ftol");
-        break;
+        goto tail_check;
 
       case OPP_CIPOW:
         if (need_float_stack) {
@@ -8262,7 +8363,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
             need_double ? "" : "f");
         }
         strcat(g_comment, " CIpow");
-        break;
+        goto tail_check;
 
       case OPP_ABORT:
         fprintf(fout, "  do_skip_code_abort();");
@@ -8273,6 +8374,14 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         fprintf(fout, "  do_emms();");
         break;
 
+      tail_check:
+        if (po->flags & OPF_TAIL) {
+          fprintf(fout, "\n");
+          strcat(g_comment, " tail");
+          goto do_tail;
+        }
+        break;
+
       default:
         no_output = 1;
         ferr(po, "unhandled op type %d, flags %x\n",
@@ -8569,6 +8678,9 @@ static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits,
         if (g_bp_frame && !(po->flags & OPF_EBP_S))
           dep->regmask_live |= 1 << xBP;
       }
+      if ((po->flags & OPF_TAIL) && po->pp != NULL
+          && po->pp->is_stdcall)
+        fp->is_stdcall = 1;
     }
     else if (po->op == OP_RET) {
       if (po->operand_cnt > 0) {
@@ -8586,10 +8698,9 @@ static void gen_hdr_dep_pass(int i, int opcnt, unsigned char *cbits,
         ret = 1;
       }
       else {
-        struct parsed_opr opr = OPR_INIT(OPT_REG, OPLM_DWORD, xAX);
         j = -1;
         from_caller = 0;
-        ret = resolve_origin(i, &opr, i + opcnt * 4, &j, &from_caller);
+        ret = resolve_origin_reg(i, xAX, i + opcnt * 4, &j, &from_caller);
       }
 
       if (ret != 1 && from_caller) {
@@ -8740,7 +8851,7 @@ static void gen_hdr(const char *funcn, int opcnt)
       if (pp != NULL) {
         if (!(po->flags & OPF_ATAIL))
           // since we know the args, try to collect them
-          if (collect_call_args_early(i, pp, NULL, NULL) != 0)
+          if (collect_call_args_early(i, opcnt, pp, NULL, NULL) != 0)
             pp = NULL;
       }
 
@@ -8785,7 +8896,7 @@ static void gen_hdr(const char *funcn, int opcnt)
 
       if (!pp->is_unresolved && !(po->flags & OPF_ATAIL)) {
         // since we know the args, collect them
-        ret = collect_call_args(po, i, pp, &regmask_dummy,
+        ret = collect_call_args(po, i, opcnt, pp, &regmask_dummy,
                 i + opcnt * 1);
       }
       if (!(po->flags & OPF_TAIL)
@@ -8972,9 +9083,7 @@ static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
       fp->pp ? fp->pp->ret_type.name :
       fp->has_ret64 ? "__int64" :
       fp->has_ret ? "int" : "void");
-    if (regmask_dep && (fp->is_stdcall || fp->argc_stack > 0)
-      && (regmask_dep & ~mxCX) == 0)
-    {
+    if (regmask_dep == mxCX && fp->is_stdcall && fp->argc_stack > 0) {
       fprintf(fout, "/*__thiscall*/  ");
       argc_normal++;
       regmask_dep = 0;