minor fixes
[ia32rtools.git] / tools / asmproc.c
index 6f1e3d5..956ffa2 100644 (file)
@@ -1,9 +1,18 @@
+/*
+ * 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>
 
 #include "my_assert.h"
 #include "my_str.h"
+#include "common.h"
 
 struct sl_item {
        char *name;
@@ -18,6 +27,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;
@@ -81,12 +95,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);
@@ -95,7 +110,6 @@ const char *sym_use(const struct sl_item *sym)
        return buf;
 }
 
-#define IS(w, y) !strcasecmp(w, y)
 #define IS_OR2(w, x, y) (IS(w, x) || IS(w, y))
 #define IS_OR3(w, x, y, z) (IS(w, x) || IS(w, y) || IS(w, z))
 
@@ -188,7 +202,7 @@ int main(int argc, char *argv[])
                                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;
                        }
                }
@@ -197,11 +211,9 @@ int main(int argc, char *argv[])
                        ssym.name = word2;
                        sym = bsearch(&ssym, symlist, symlist_cnt,
                                sizeof(symlist[0]), cmp_sym);
-                       if (sym != NULL
-                           && (sym->callsites || IS(word2, func)))
-                       {
+                       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;
                        }
                }
@@ -211,7 +223,7 @@ int main(int argc, char *argv[])
                        sym = bsearch(&ssym, symlist, symlist_cnt,
                                sizeof(symlist[0]), cmp_sym);
                        if (sym != NULL) {
-                               fprintf(fout, "\t\tpublic %s%s", sym_use(sym), p);
+                               fprintf(fout, "\t\tpublic %s%s", sym_use(sym, 1), p);
                                continue;
                        }
                }
@@ -223,17 +235,29 @@ int main(int argc, char *argv[])
                p = next_word(word3, sizeof(word3), p);
 
                // 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(word, "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 || IS(word3, func)))
-                       {
-                               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;
                        }
                }
@@ -270,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;
                        }
                }
@@ -296,9 +320,9 @@ 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;
                        }
                }
@@ -330,7 +354,7 @@ offset_loop:
                        sym = bsearch(&ssym, symlist, symlist_cnt,
                                sizeof(symlist[0]), cmp_sym);
                        fprintf(fout, " offset %s%s",
-                               (sym != NULL && sym->callsites) ? sym_use(sym) : word,
+                               (sym != NULL) ? sym_use(sym, sym->callsites) : word,
                                p2 ? "," : "");
                }
                fprintf(fout, "%s", p);