some func ptr arg type checking
[ia32rtools.git] / tools / cvt_data.c
index 044857b..237abef 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * ia32rtools
+ * (C) notaz, 2013,2014
+ *
+ * This work is licensed under the terms of 3-clause BSD license.
+ * See COPYING file in the top-level directory.
+ */
+
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
@@ -138,7 +146,7 @@ static const char *type_name(enum dx_type type)
   case DXT_BYTE:
     return ".byte";
   case DXT_WORD:
-    return ".word";
+    return ".hword";
   case DXT_DWORD:
     return ".long";
   case DXT_QUAD:
@@ -239,8 +247,8 @@ static const struct parsed_proto *check_var(FILE *fhdr,
   const char *sym, const char *varname)
 {
   const struct parsed_proto *pp, *pp_sym;
-  char fp_sym[256], fp_var[256];
-  int i, bad = 0;
+  char fp_sym[256], fp_var[256], *p;
+  int i;
 
   pp = proto_parse(fhdr, varname, 1);
   if (pp == NULL) {
@@ -270,6 +278,20 @@ static const struct parsed_proto *check_var(FILE *fhdr,
   }
 
 check_sym:
+  // fptrs must use 32bit args, callsite might have no information and
+  // lack a cast to smaller types, which results in incorrectly masked
+  // args passed (callee may assume masked args, it does on ARM)
+  for (i = 0; i < pp->argc; i++) {
+    if (pp->arg[i].type.is_ptr)
+      continue;
+    p = pp->arg[i].type.name;
+    if (strstr(p, "int8") || strstr(p, "int16")
+      || strstr(p, "char") || strstr(p, "short"))
+    {
+      awarn("reference to %s with arg%d '%s'\n", pp->name, i + 1, p);
+    }
+  }
+
   sprint_pp_short(pp, g_comment, sizeof(g_comment));
 
   if (sym != NULL) {
@@ -287,24 +309,7 @@ check_sym:
       return pp;
   }
 
-  if (pp->argc != pp_sym->argc || pp->argc_reg != pp_sym->argc_reg)
-    bad = 1;
-  else {
-    for (i = 0; i < pp->argc; i++) {
-      if ((pp->arg[i].reg != NULL) != (pp_sym->arg[i].reg != NULL)) {
-        bad = 1;
-        break;
-      }
-      if ((pp->arg[i].reg != NULL)
-        && !IS(pp->arg[i].reg, pp_sym->arg[i].reg))
-      {
-        bad = 1;
-        break;
-      }
-    }
-  }
-
-  if (bad) {
+  if (pp_cmp_func(pp, pp_sym)) {
     pp_print(fp_sym, sizeof(fp_sym), pp_sym);
     anote("var: %s\n", fp_var);
     anote("sym: %s\n", fp_sym);
@@ -501,9 +506,19 @@ int main(int argc, char *argv[])
       asmln++;
 
       p = sskip(line);
-      if (*p == 0 || *p == ';')
+      if (*p == 0)
         continue;
 
+      if (*p == ';') {
+        if (IS_START(p, ";org") && sscanf(p + 5, "%Xh", &i) == 1) {
+          // ;org is only seen at section start, so assume . addr 0
+          i &= 0xfff;
+          if (i != 0)
+            fprintf(fout, "\t\t  .skip 0x%x\n", i);
+        }
+        continue;
+      }
+
       for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
         p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
         if (*p == 0 || *p == ';') {