translate: support for data imports
[ia32rtools.git] / tools / translate.c
index d686d6e..e6ab802 100644 (file)
@@ -2142,7 +2142,7 @@ static void check_func_pp(struct parsed_op *po,
 }
 
 static const char *check_label_read_ref(struct parsed_op *po,
-  const char *name)
+  const char *name, int *is_import)
 {
   const struct parsed_proto *pp;
 
@@ -2153,6 +2153,9 @@ static const char *check_label_read_ref(struct parsed_op *po,
   if (pp->is_func)
     check_func_pp(po, pp, "ref");
 
+  if (is_import != NULL)
+    *is_import = pp->is_import;
+
   return pp->name;
 }
 
@@ -2171,6 +2174,7 @@ static char *out_src_opr(char *buf, size_t buf_size,
   char tmp1[256], tmp2[256];
   char expr[256];
   const char *name;
+  int is_import = 0;
   char *p;
   int ret;
 
@@ -2243,7 +2247,11 @@ static char *out_src_opr(char *buf, size_t buf_size,
     break;
 
   case OPT_LABEL:
-    name = check_label_read_ref(po, popr->name);
+    name = check_label_read_ref(po, popr->name, &is_import);
+    if (is_import)
+      // for imported data, asm is loading the offset
+      goto do_offset;
+
     if (cast[0] == 0 && popr->is_ptr)
       cast = "(u32)";
 
@@ -2259,7 +2267,8 @@ static char *out_src_opr(char *buf, size_t buf_size,
     break;
 
   case OPT_OFFSET:
-    name = check_label_read_ref(po, popr->name);
+  do_offset:
+    name = check_label_read_ref(po, popr->name, NULL);
     if (cast[0] == 0)
       cast = "(u32)";
     if (is_lea)
@@ -3819,8 +3828,12 @@ static void resolve_branches_parse_calls(int opcnt)
       else if (po->operand[0].type == OPT_LABEL)
       {
         tmpname = opr_name(po, 0);
-        if (IS_START(tmpname, "loc_"))
-          ferr(po, "call to loc_*\n");
+        if (IS_START(tmpname, "loc_")) {
+          if (!g_seh_found)
+            ferr(po, "call to loc_*\n");
+          // eliminate_seh() must take care of it
+          continue;
+        }
         if (IS(tmpname, "__alloca_probe"))
           continue;
         if (IS(tmpname, "__SEH_prolog")) {
@@ -3922,6 +3935,7 @@ tailcall:
 
 static int resolve_origin(int i, const struct parsed_opr *opr,
   int magic, int *op_i, int *is_caller);
+static void set_label(int i, const char *name);
 
 static void eliminate_seh_writes(int opcnt)
 {
@@ -3949,6 +3963,87 @@ static void eliminate_seh_writes(int opcnt)
   }
 }
 
+static void eliminate_seh_finally(int opcnt)
+{
+  const char *target_name = NULL;
+  const char *return_name = NULL;
+  int exits[MAX_EXITS];
+  int exit_count = 0;
+  int call_i = -1;
+  int target_i = -1;
+  int return_i = -1;
+  int tgend_i = -1;
+  int i;
+
+  for (i = 0; i < opcnt; i++) {
+    if (ops[i].op != OP_CALL)
+      continue;
+    if (!IS_START(opr_name(&ops[i], 0), "loc_"))
+      continue;
+    if (target_name != NULL)
+      ferr(&ops[i], "multiple finally calls? (last was %s)\n",
+        target_name);
+    target_name = opr_name(&ops[i], 0);
+    call_i = i;
+
+    if (g_labels[i + 1] == NULL)
+      set_label(i + 1, "seh_fin_done");
+    return_name = g_labels[i + 1];
+    return_i = i + 1;
+  }
+
+  if (call_i == -1)
+    // no finally block
+    return;
+
+  // find finally code (bt_i is not set because it's call)
+  for (i = 0; i < opcnt; i++) {
+    if (g_labels[i] == NULL)
+      continue;
+    if (!IS(g_labels[i], target_name))
+      continue;
+
+    ferr_assert(&ops[i], target_i == -1);
+    target_i = i;
+  }
+  ferr_assert(&ops[0], target_i != -1);
+
+  find_reachable_exits(target_i, opcnt, target_i + opcnt * 24,
+    exits, &exit_count);
+  ferr_assert(&ops[target_i], exit_count == 1);
+  ferr_assert(&ops[target_i], ops[exits[0]].op == OP_RET);
+  tgend_i = exits[0];
+
+  // convert to jumps, link
+  ops[call_i].op = OP_JMP;
+  ops[call_i].bt_i = target_i;
+  add_label_ref(&g_label_refs[target_i], call_i);
+
+  ops[tgend_i].op = OP_JMP;
+  ops[tgend_i].flags &= ~OPF_TAIL;
+  ops[tgend_i].flags |= OPF_JMP;
+  ops[tgend_i].bt_i = return_i;
+  ops[tgend_i].operand_cnt = 1;
+  ops[tgend_i].operand[0].type = OPT_LABEL;
+  snprintf(ops[tgend_i].operand[0].name, NAMELEN, "%s", return_name);
+  add_label_ref(&g_label_refs[return_i], tgend_i);
+
+  // rm seh finally entry code
+  for (i = target_i - 1; i >= 0; i--) {
+    if (g_labels[i] != NULL && g_label_refs[i].i != -1)
+      return;
+    if (ops[i].flags & OPF_CJMP)
+      return;
+    if (ops[i].flags & (OPF_JMP | OPF_TAIL))
+      break;
+  }
+  for (i = target_i - 1; i >= 0; i--) {
+    if (ops[i].flags & (OPF_JMP | OPF_TAIL))
+      break;
+    ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+  }
+}
+
 static void eliminate_seh(int opcnt)
 {
   int i, j, k, ret;
@@ -3991,6 +4086,7 @@ static void eliminate_seh(int opcnt)
   }
 
   eliminate_seh_writes(opcnt);
+  eliminate_seh_finally(opcnt);
 }
 
 static void eliminate_seh_calls(int opcnt)
@@ -4029,6 +4125,7 @@ static void eliminate_seh_calls(int opcnt)
   ferr_assert(ops, epilog_found);
 
   eliminate_seh_writes(opcnt);
+  eliminate_seh_finally(opcnt);
 }
 
 static int scan_prologue(int i, int opcnt, int *ecx_push, int *esp_sub)
@@ -6222,6 +6319,9 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
 
       if (pp->is_fptr && !(pp->name[0] != 0 && pp->is_arg)) {
         if (pp->name[0] != 0) {
+          if (IS_START(pp->name, "guess"))
+            pp->is_guessed = 1;
+
           memmove(pp->name + 2, pp->name, strlen(pp->name) + 1);
           memcpy(pp->name, "i_", 2);
 
@@ -7259,9 +7359,10 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
           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 || IS_START(pp->name, "i_guess"))
-            fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
-              buf3, asmfn, po->asmln, pp->name);
+        }
+        if (pp->is_fptr && (pp->is_unresolved || pp->is_guessed)) {
+          fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
+            buf3, asmfn, po->asmln, pp->name);
         }
 
         fprintf(fout, "%s", buf3);
@@ -8394,8 +8495,11 @@ static void gen_hdr(const char *funcn, int opcnt)
       // noreturn OS functions
       break;
     }
-    if (ops[i].op != OP_NOP && ops[i].op != OPP_ABORT)
+    if (!(ops[i].flags & OPF_RMD)
+        && ops[i].op != OP_NOP && ops[i].op != OPP_ABORT)
+    {
       ferr(&ops[i], "unreachable code\n");
+    }
   }
 
   for (i = 0; i < g_eqcnt; i++) {