X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tools%2Fasmproc.c;h=956ffa249245e7c8148a82a3e3d6add8cb037360;hb=3084511901606db82741bffafc8582ab9b7591cc;hp=678f58feaa01535e6ea6cdd6f31750a87acdee1f;hpb=87ccd363f88828840d1bc7c4614ee3b3eb0dba54;p=ia32rtools.git diff --git a/tools/asmproc.c b/tools/asmproc.c index 678f58f..956ffa2 100644 --- a/tools/asmproc.c +++ b/tools/asmproc.c @@ -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 #include #include #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,20 +202,28 @@ 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; } } - 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 || 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; + } + } + + 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; } } @@ -213,25 +235,39 @@ int main(int argc, char *argv[]) p = next_word(word3, sizeof(word3), p); // push offset + 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 - 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; } } // dd offset - if (IS(word, "dd") && IS(word2, "offset")) { + if (IS(word, "dd") + && (IS(word2, "offset") || strstr(p, "offset"))) + { fprintf(fout, "\t\tdd"); - strcpy(word, word3); + p = next_word(word, sizeof(word), line); goto offset_loop; } @@ -242,9 +278,12 @@ int main(int argc, char *argv[]) p = next_word(word4, sizeof(word4), p); // dd offset - if (IS(word2, "dd") && IS(word3, "offset")) { + if (IS(word2, "dd") + && (IS(word3, "offset") || strstr(p, "offset"))) + { fprintf(fout, "%s\tdd", word); - strcpy(word, word4); + p = next_word(word, sizeof(word), line); + p = next_word(word, sizeof(word), p); goto offset_loop; } @@ -255,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; } } @@ -281,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; } } @@ -294,6 +333,19 @@ pass: 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; @@ -302,18 +354,8 @@ 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 ? "," : ""); - - p2 = next_word(word, sizeof(word), p); - if (word[0] == 0 || word[0] == ';') { - break; - } - if (!IS(word, "offset")) { - printf("could not handle offset array\n"); - break; - } - p = next_word(word, sizeof(word), p2); } fprintf(fout, "%s", p); continue;