fix tests
[ia32rtools.git] / tools / asmproc.c
index 02d06a3..6e6b62b 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.
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -9,6 +17,7 @@ struct sl_item {
        char *name;
        unsigned int callsites:1;
        unsigned int found:1;
+       unsigned int ignore_missing:1;
 };
 
 static int cmp_sym(const void *p1_, const void *p2_)
@@ -17,6 +26,11 @@ static int cmp_sym(const void *p1_, const void *p2_)
        const char *s1 = p1->name, *s2 = p2->name;
        int i;
 
+       if (*s1 == '_')
+               s1++;
+       if (*s2 == '_')
+               s2++;
+
        for (i = 0; ; i++) {
                if ((s1[i] | s2[i]) == 0)
                        break;
@@ -50,7 +64,7 @@ static int cmp_sym_sort(const void *p1_, const void *p2_)
 }
 
 void read_list(struct sl_item **sl_in, int *cnt, int *alloc,
-       FILE *f, int callsites)
+       FILE *f, int callsites, int ignore_missing)
 {
        struct sl_item *sl = *sl_in;
        int c = *cnt;
@@ -64,6 +78,7 @@ void read_list(struct sl_item **sl_in, int *cnt, int *alloc,
 
                sl[c].name = strdup(word);
                sl[c].callsites = callsites;
+               sl[c].ignore_missing = ignore_missing;
                sl[c].found = 0;
                c++;
 
@@ -79,12 +94,13 @@ void read_list(struct sl_item **sl_in, int *cnt, int *alloc,
        *cnt = c;
 }
 
-const char *sym_use(const struct sl_item *sym)
+const char *sym_use(const struct sl_item *sym, int is_rm)
 {
        static char buf[256+3];
        int ret;
 
-       ret = snprintf(buf, sizeof(buf), "rm_%s", sym->name);
+       ret = snprintf(buf, sizeof(buf), "%s%s",
+         is_rm ? "rm_" : "", sym->name);
        if (ret >= sizeof(buf)) {
                printf("truncation detected: '%s'\n", buf);
                exit(1);
@@ -101,6 +117,7 @@ int main(int argc, char *argv[])
 {
        struct sl_item *symlist, *sym, ssym = { NULL, };
        int patch_callsites = 0;
+       int ignore_missing = 0;
        FILE *fout, *fin, *f;
        int symlist_alloc;
        int symlist_cnt;
@@ -111,13 +128,15 @@ int main(int argc, char *argv[])
        char word4[256];
        char word5[256];
        char word6[256];
-       char *p;
+       char func[256];
+       char *p, *p2;
        int i;
 
        if (argc < 4) {
-               // -c - patch callsites
-               printf("usage:\n%s <asmf_out> <asmf_in> [[-c] <listf>]*>\n",
+               printf("usage:\n%s <asmf_out> <asmf_in> [[-c][-i] <listf>]*>\n",
                        argv[0]);
+               printf("  -c - patch callsites\n"
+                                        "  -i - ignore missing syms\n");
                return 1;
        }
 
@@ -131,14 +150,19 @@ int main(int argc, char *argv[])
                        patch_callsites = 1;
                        continue;
                }
+               if (strcmp(argv[i], "-i") == 0) {
+                       ignore_missing = 1;
+                       continue;
+               }
 
                f = fopen(argv[i], "r");
                my_assert_not(f, NULL);
                read_list(&symlist, &symlist_cnt, &symlist_alloc,
-                       f, patch_callsites);
+                       f, patch_callsites, ignore_missing);
                fclose(f);
 
                patch_callsites = 0;
+               ignore_missing = 0;
        }
 
        qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort);
@@ -168,23 +192,38 @@ int main(int argc, char *argv[])
                p = next_word(word2, sizeof(word2), p);
 
                if (IS_OR2(word2, "proc", "endp")) {
+                       if (IS(word2, "proc"))
+                               strcpy(func, word);
+                       else
+                               func[0] = 0;
+
                        ssym.name = word;
                        sym = bsearch(&ssym, symlist, symlist_cnt,
                                sizeof(symlist[0]), cmp_sym);
                        if (sym != NULL) {
                                sym->found = 1;
-                               fprintf(fout, "rm_%s\t%s%s", word, word2, p);
+                               fprintf(fout, "%s\t%s%s", sym_use(sym, 1), word2, p);
                                continue;
                        }
                }
 
-               if (IS_OR3(word, "call", "jmp", "public")) {
+               if (IS_OR2(word, "call", "jmp")) {
                        ssym.name = word2;
                        sym = bsearch(&ssym, symlist, symlist_cnt,
                                sizeof(symlist[0]), cmp_sym);
-                       if (sym != NULL && sym->callsites) {
+                       if (sym != NULL) {
                                fprintf(fout, "\t\t%s\t%s%s", word,
-                                       sym_use(sym), p);
+                                       sym_use(sym, sym->callsites || IS(word2, func)), p);
+                               continue;
+                       }
+               }
+
+               if (IS(word, "public")) {
+                       ssym.name = word2;
+                       sym = bsearch(&ssym, symlist, symlist_cnt,
+                               sizeof(symlist[0]), cmp_sym);
+                       if (sym != NULL) {
+                               fprintf(fout, "\t\tpublic %s%s", sym_use(sym, 1), p);
                                continue;
                        }
                }
@@ -195,21 +234,43 @@ int main(int argc, char *argv[])
 
                p = next_word(word3, sizeof(word3), p);
 
-               // dd offset <sym>
                // push offset <sym>
+               if (IS(word, "push") && IS(word2, "offset")) {
+                       ssym.name = word3;
+                       sym = bsearch(&ssym, symlist, symlist_cnt,
+                               sizeof(symlist[0]), cmp_sym);
+                       if (sym != NULL) {
+                               fprintf(fout, "\t\t%s %s %s%s", word, word2,
+                                 sym_use(sym, sym->callsites || IS(word3, func)), p);
+                               continue;
+                       }
+               }
+
                // jcc short <sym>
-               if ( (IS_OR2(word, "dd", "push") && IS(word2, "offset"))
-                 || (word[0] == 'j' && IS(word2, "short")) ) {
+               if (word[0] == 'j' && IS(word2, "short") && !IS(word3, "exit")) {
                        ssym.name = word3;
                        sym = bsearch(&ssym, symlist, symlist_cnt,
                                sizeof(symlist[0]), cmp_sym);
-                       if (sym != NULL && sym->callsites) {
-                               fprintf(fout, "\t\t%s %s %s%s",
-                                       word, word2, sym_use(sym), p);
+                       if (sym != NULL) {
+                               fprintf(fout, "\t\t%s ", word);
+                               // for conditional "call", don't print 'short'
+                               if (IS(word3, func))
+                                       fprintf(fout, "short ");
+                               fprintf(fout, "%s%s",
+                                 sym_use(sym, sym->callsites || IS(word3, func)), p);
                                continue;
                        }
                }
 
+               // dd offset <sym>
+               if (IS(word, "dd")
+                       && (IS(word2, "offset") || strstr(p, "offset")))
+               {
+                       fprintf(fout, "\t\tdd");
+                       p = next_word(word, sizeof(word), line);
+                       goto offset_loop;
+               }
+
                p = sskip(p);
                if (*p == 0 || *p == ';')
                        goto pass; // need at least 4 words
@@ -217,15 +278,13 @@ int main(int argc, char *argv[])
                p = next_word(word4, sizeof(word4), p);
 
                // <name> dd offset <sym>
-               if (IS(word2, "dd") && IS(word3, "offset")) {
-                       ssym.name = word4;
-                       sym = bsearch(&ssym, symlist, symlist_cnt,
-                               sizeof(symlist[0]), cmp_sym);
-                       if (sym != NULL && sym->callsites) {
-                               fprintf(fout, "%s\tdd offset %s%s", word,
-                                       sym_use(sym), p);
-                               continue;
-                       }
+               if (IS(word2, "dd")
+                       && (IS(word3, "offset") || strstr(p, "offset")))
+               {
+                       fprintf(fout, "%s\tdd", word);
+                       p = next_word(word, sizeof(word), line);
+                       p = next_word(word, sizeof(word), p);
+                       goto offset_loop;
                }
 
                // mov <something>, offset <sym>
@@ -235,10 +294,10 @@ int main(int argc, char *argv[])
                        ssym.name = word4;
                        sym = bsearch(&ssym, symlist, symlist_cnt,
                                sizeof(symlist[0]), cmp_sym);
-                       if (sym != NULL && sym->callsites) {
+                       if (sym != NULL) {
                                fprintf(fout, "\t\t%s\t%s %s %s%s",
                                        word, word2, word3,
-                                       sym_use(sym), p);
+                                       sym_use(sym, sym->callsites), p);
                                continue;
                        }
                }
@@ -261,19 +320,49 @@ int main(int argc, char *argv[])
                        ssym.name = word6;
                        sym = bsearch(&ssym, symlist, symlist_cnt,
                                sizeof(symlist[0]), cmp_sym);
-                       if (sym != NULL && sym->callsites) {
+                       if (sym != NULL) {
                                fprintf(fout, "\t\t%s\tdword ptr %s offset %s%s",
-                                       word, word4, sym_use(sym), p);
+                                       word, word4, sym_use(sym, sym->callsites), p);
                                continue;
                        }
                }
 
 pass:
                fwrite(line, 1, strlen(line), fout);
+               continue;
+
+offset_loop:
+               while (1) {
+                       p2 = next_word(word, sizeof(word), p);
+                       if (word[0] == 0 || word[0] == ';') {
+                               break;
+                       }
+                       if (!IS(word, "offset")) {
+                               // pass through
+                               p2 = strstr(p, "offset");
+                               if (p2 == NULL)
+                                       break;
+                               fwrite(p, 1, p2 - p, fout);
+                               p2 = next_word(word, sizeof(word), p2);
+                       }
+                       p = next_word(word, sizeof(word), p2);
+                       p2 = strchr(word, ',');
+                       if (p2)
+                               *p2 = 0;
+
+                       ssym.name = word;
+                       sym = bsearch(&ssym, symlist, symlist_cnt,
+                               sizeof(symlist[0]), cmp_sym);
+                       fprintf(fout, " offset %s%s",
+                               (sym != NULL) ? sym_use(sym, sym->callsites) : word,
+                               p2 ? "," : "");
+               }
+               fprintf(fout, "%s", p);
+               continue;
        }
 
        for (i = 0; i < symlist_cnt; i++) {
-               if (!symlist[i].found)
+               if (!symlist[i].found && !symlist[i].ignore_missing)
                        printf("warning: sym '%s' not found\n", symlist[i].name);
        }
 
@@ -282,3 +371,5 @@ pass:
 
        return 0;
 }
+
+// vim:ts=2:shiftwidth=2