use main header, parse variable types, asm patch comment
[ia32rtools.git] / tools / translate.c
index fbc8ff6..e7f4bdd 100644 (file)
@@ -8,11 +8,13 @@
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
 #define IS(w, y) !strcmp(w, y)
+#define IS_START(w, y) !strncmp(w, y, strlen(y))
 
 #include "protoparse.h"
 
-const char *asmfn;
+static const char *asmfn;
 static int asmln;
+static FILE *g_fhdr;
 
 #define anote(fmt, ...) \
        printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
@@ -111,6 +113,7 @@ enum opr_lenmod {
 struct parsed_opr {
   enum opr_type type;
   enum opr_lenmod lmod;
+  unsigned int is_ptr:1;  // pointer in C
   int reg;
   unsigned int val;
   char name[256];
@@ -294,7 +297,7 @@ static int parse_indmode(char *name, int *regmask, int need_c_cvt)
     *d = 0;
 
     // skip 'ds:' prefix
-    if (!strncmp(s, "ds:", 3))
+    if (IS_START(s, "ds:"))
       s += 3;
 
     s = next_idt(w, sizeof(w), s);
@@ -334,12 +337,12 @@ static const char *parse_stack_el(const char *name)
   long val;
   int len;
 
-  if (!strncmp(name, "ebp+", 4)
+  if (IS_START(name, "ebp+")
       && !('0' <= name[4] && name[4] <= '9'))
   {
     return name + 4;
   }
-  if (strncmp(name, "esp+", 4) != 0)
+  if (!IS_START(name, "esp+"))
     return NULL;
 
   p = strchr(name + 4, '+');
@@ -390,6 +393,61 @@ static int guess_lmod_from_name(struct parsed_opr *opr)
   return 0;
 }
 
+static int guess_lmod_from_c_type(struct parsed_opr *opr, const char *c_type)
+{
+  static const char *ptr_types[] = {
+    "LPCSTR",
+  };
+  static const char *dword_types[] = {
+    "int", "_DWORD", "DWORD", "HANDLE", "HWND", "HMODULE",
+  };
+  static const char *word_types[] = {
+    "__int16", "unsigned __int16",
+  };
+  static const char *byte_types[] = {
+    "char", "__int8", "unsigned __int8", "BYTE",
+  };
+  int i;
+
+  if (strchr(c_type, '*')) {
+    opr->lmod = OPLM_DWORD;
+    opr->is_ptr = 1;
+    return 1;
+  }
+
+  for (i = 0; i < ARRAY_SIZE(dword_types); i++) {
+    if (IS(c_type, dword_types[i])) {
+      opr->lmod = OPLM_DWORD;
+      return 1;
+    }
+  }
+
+  for (i = 0; i < ARRAY_SIZE(ptr_types); i++) {
+    if (IS(c_type, ptr_types[i])) {
+      opr->lmod = OPLM_DWORD;
+      opr->is_ptr = 1;
+      return 1;
+    }
+  }
+
+  for (i = 0; i < ARRAY_SIZE(word_types); i++) {
+    if (IS(c_type, word_types[i])) {
+      opr->lmod = OPLM_WORD;
+      return 1;
+    }
+  }
+
+  for (i = 0; i < ARRAY_SIZE(byte_types); i++) {
+    if (IS(c_type, byte_types[i])) {
+      opr->lmod = OPLM_BYTE;
+      return 1;
+    }
+  }
+
+  anote("unhandled C type '%s' for '%s'\n", c_type, opr->name);
+  return 0;
+}
+
 static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
   int *regmask)
 {
@@ -399,16 +457,19 @@ static void setup_reg_opr(struct parsed_opr *opr, int reg, enum opr_lenmod lmod,
   *regmask |= 1 << reg;
 }
 
-static struct parsed_equ *equ_find(struct parsed_op *po, const char *name);
+static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
+  int *extra_offs);
 
 static int parse_operand(struct parsed_opr *opr,
   int *regmask, int *regmask_indirect,
   char words[16][256], int wordc, int w, unsigned int op_flags)
 {
+  struct parsed_proto pp;
   enum opr_lenmod tmplmod;
   int ret, len;
   long number;
   int wordc_in;
+  char *tmp;
   int i;
 
   if (w >= wordc)
@@ -444,6 +505,8 @@ static int parse_operand(struct parsed_opr *opr,
 
     if (label != NULL) {
       opr->type = OPT_LABEL;
+      if (IS_START(label, "ds:"))
+        label += 3;
       strcpy(opr->name, label);
       return wordc;
     }
@@ -464,16 +527,30 @@ static int parse_operand(struct parsed_opr *opr,
     }
   }
 
-  if (wordc_in == 2 && IS(words[w], "offset")) {
-    opr->type = OPT_OFFSET;
-    strcpy(opr->name, words[w + 1]);
-    return wordc;
+  if (wordc_in == 2) {
+    if (IS(words[w], "offset")) {
+      opr->type = OPT_OFFSET;
+      strcpy(opr->name, words[w + 1]);
+      return wordc;
+    }
+    if (IS(words[w], "(offset")) {
+      char *p = strchr(words[w + 1], ')');
+      if (p == NULL)
+        aerr("parse of bracketed offset failed\n");
+      *p = 0;
+      opr->type = OPT_OFFSET;
+      strcpy(opr->name, words[w + 1]);
+      return wordc;
+    }
   }
 
   if (wordc_in != 1)
     aerr("parse_operand 1 word expected\n");
 
-  strcpy(opr->name, words[w]);
+  tmp = words[w];
+  if (IS_START(tmp, "ds:"))
+    tmp += 3;
+  strcpy(opr->name, tmp);
 
   if (words[w][0] == '[') {
     opr->type = OPT_REGMEM;
@@ -485,7 +562,7 @@ static int parse_operand(struct parsed_opr *opr,
     if (opr->lmod == OPLM_UNSPEC && parse_stack_el(opr->name)) {
       // might be an equ
       struct parsed_equ *eq =
-        equ_find(NULL, parse_stack_el(opr->name));
+        equ_find(NULL, parse_stack_el(opr->name), &i);
       if (eq)
         opr->lmod = eq->lmod;
     }
@@ -517,12 +594,19 @@ static int parse_operand(struct parsed_opr *opr,
 
   // most likely var in data segment
   opr->type = OPT_LABEL;
+
+  ret = proto_parse(g_fhdr, opr->name, &pp);
+  if (ret == 0) {
+    if (pp.is_fptr) {
+      opr->lmod = OPLM_DWORD;
+      opr->is_ptr = 1;
+    }
+    else if (opr->lmod == OPLM_UNSPEC)
+      guess_lmod_from_c_type(opr, pp.ret_type);
+  }
+
   if (opr->lmod == OPLM_UNSPEC)
     guess_lmod_from_name(opr);
-  if (opr->lmod != OPLM_UNSPEC)
-    return wordc;
-
-  // TODO: scan data seg to determine type?
   return wordc;
 }
 
@@ -905,12 +989,33 @@ static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
   return regs_r32[popr->reg];
 }
 
-static struct parsed_equ *equ_find(struct parsed_op *po, const char *name)
+static struct parsed_equ *equ_find(struct parsed_op *po, const char *name,
+  int *extra_offs)
 {
+  const char *p;
+  char *endp;
+  int namelen;
   int i;
 
+  *extra_offs = 0;
+  namelen = strlen(name);
+
+  p = strchr(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;
+    *extra_offs = strtol(p, &endp, 16);
+    if (*endp != 0)
+      ferr(po, "equ parse failed for '%s'\n", name);
+  }
+
   for (i = 0; i < g_eqcnt; i++)
-    if (IS(g_eqs[i].name, name))
+    if (strncmp(g_eqs[i].name, name, namelen) == 0
+     && g_eqs[i].name[namelen] == 0)
       break;
   if (i >= g_eqcnt) {
     if (po != NULL)
@@ -930,25 +1035,28 @@ static void stack_frame_access(struct parsed_op *po,
   int i, arg_i, arg_s;
   const char *bp_arg;
   int stack_ra = 0;
+  int offset = 0;
   int sf_ofs;
 
   bp_arg = parse_stack_el(name);
   snprintf(g_comment, sizeof(g_comment), "%s", bp_arg);
-  eq = equ_find(po, bp_arg);
+  eq = equ_find(po, bp_arg, &offset);
   if (eq == NULL)
     ferr(po, "detected but missing eq\n");
 
+  offset += eq->offset;
+
   if (!strncmp(name, "ebp", 3))
     stack_ra = 4;
 
-  if (stack_ra <= eq->offset && eq->offset < stack_ra + 4)
-    ferr(po, "reference to ra? %d %d\n", eq->offset, stack_ra);
+  if (stack_ra <= offset && offset < stack_ra + 4)
+    ferr(po, "reference to ra? %d %d\n", offset, stack_ra);
 
-  if (eq->offset > stack_ra) {
-    arg_i = (eq->offset - stack_ra - 4) / 4;
+  if (offset > stack_ra) {
+    arg_i = (offset - stack_ra - 4) / 4;
     if (arg_i < 0 || arg_i >= g_func_pp.argc_stack)
       ferr(po, "offset %d (%s) doesn't map to any arg\n",
-        eq->offset, bp_arg);
+        offset, bp_arg);
 
     for (i = arg_s = 0; i < g_func_pp.argc; i++) {
       if (g_func_pp.arg[i].reg != NULL)
@@ -968,9 +1076,9 @@ static void stack_frame_access(struct parsed_op *po,
     if (g_stack_fsz == 0)
       ferr(po, "stack var access without stackframe\n");
 
-    sf_ofs = g_stack_fsz + eq->offset;
+    sf_ofs = g_stack_fsz + offset;
     if (sf_ofs < 0)
-      ferr(po, "bp_stack offset %d/%d\n", eq->offset, g_stack_fsz);
+      ferr(po, "bp_stack offset %d/%d\n", offset, g_stack_fsz);
 
     if (is_lea)
       prefix = "(u32)&";
@@ -1744,6 +1852,15 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         ops[ret].flags |= OPF_RMD;
       }
 
+      // can't call functions with non-__cdecl callbacks yet
+      for (arg = 0; arg < pp->argc; arg++) {
+        if (pp->arg[arg].fptr != NULL) {
+          pp_tmp = pp->arg[arg].fptr;
+          if (pp_tmp->is_stdcall || pp_tmp->argc != pp_tmp->argc_stack)
+            ferr(po, "'%s' has a non-__cdecl callback\n", tmpname);
+        }
+      }
+
       for (arg = 0; arg < pp->argc; arg++)
         if (pp->arg[arg].reg == NULL)
           break;
@@ -2053,8 +2170,9 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
       case OP_MOV:
         assert_operand_cnt(2);
         propagate_lmod(po, &po->operand[0], &po->operand[1]);
-        fprintf(fout, "  %s = %s;",
+        fprintf(fout, "  %s = %s%s;",
             out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
+            po->operand[0].is_ptr ? "(void *)" : "",
             out_src_opr(buf2, sizeof(buf2), po, &po->operand[1], 0));
         break;
 
@@ -2334,10 +2452,15 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
             fprintf(fout, "(u32)");
         }
 
-        if (po->operand[0].type != OPT_LABEL)
+        if (po->operand[0].type != OPT_LABEL) {
           fprintf(fout, "icall%d(", i);
-        else
-          fprintf(fout, "%s(", opr_name(po, 0));
+        }
+        else {
+          if (pp->name[0] == 0)
+            ferr(po, "missing pp->name\n");
+          fprintf(fout, "%s(", pp->name);
+        }
+
         for (arg = 0; arg < pp->argc; arg++) {
           if (arg > 0)
             fprintf(fout, ", ");
@@ -2504,7 +2627,7 @@ static int cmpstringp(const void *p1, const void *p2)
 
 int main(int argc, char *argv[])
 {
-  FILE *fout, *fasm, *fhdr, *frlist;
+  FILE *fout, *fasm, *frlist;
   char line[256];
   char words[16][256];
   int ida_func_attr = 0;
@@ -2541,8 +2664,8 @@ int main(int argc, char *argv[])
   my_assert_not(fasm, NULL);
 
   hdrfn = argv[arg++];
-  fhdr = fopen(hdrfn, "r");
-  my_assert_not(fhdr, NULL);
+  g_fhdr = fopen(hdrfn, "r");
+  my_assert_not(g_fhdr, NULL);
 
   rlist_alloc = 64;
   rlist = malloc(rlist_alloc * sizeof(rlist[0]));
@@ -2633,6 +2756,7 @@ int main(int argc, char *argv[])
       continue;
     }
 
+parse_words:
     memset(words, 0, sizeof(words));
     for (wordc = 0; wordc < 16; wordc++) {
       p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
@@ -2642,6 +2766,14 @@ int main(int argc, char *argv[])
       }
     }
 
+    // alow asm patches in comments
+    if (*p == ';' && IS_START(p, "; sctpatch: ")) {
+      p = sskip(p + 12);
+      if (*p == 0 || *p == ';')
+        continue;
+      goto parse_words; // lame
+    }
+
     if (wordc == 0) {
       // shouldn't happen
       awarn("wordc == 0?\n");
@@ -2679,7 +2811,7 @@ int main(int argc, char *argv[])
           words[0], g_func);
 
       if (in_func && !skip_func)
-        gen_func(fout, fhdr, g_func, pi);
+        gen_func(fout, g_fhdr, g_func, pi);
 
       in_func = 0;
       skip_warned = 0;
@@ -2751,7 +2883,7 @@ int main(int argc, char *argv[])
 
   fclose(fout);
   fclose(fasm);
-  fclose(fhdr);
+  fclose(g_fhdr);
 
   return 0;
 }