X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tools%2Fasmproc.c;h=e15266948dd3a29268ea86098708c1b0bd2da002;hb=aa1aa2c24c3b224443702c556a5e1c50f6dc9d05;hp=3ff983b24b65608a451cab6b6c5d27c2849fb8b9;hpb=368deb2385c2ee4971964c3c08688c4ad36cb56e;p=ia32rtools.git diff --git a/tools/asmproc.c b/tools/asmproc.c index 3ff983b..e152669 100644 --- a/tools/asmproc.c +++ b/tools/asmproc.c @@ -3,38 +3,7 @@ #include #include "my_assert.h" - -static int my_isblank(char c) -{ - return c == '\t' || c == ' ' || c == '\r' || c == '\n'; -} - -static char *sskip(char *s) -{ - while (my_isblank(*s)) - s++; - - return s; -} - -static char *next_word(char *w, size_t wsize, char *s) -{ - size_t i; - - s = sskip(s); - - for (i = 0; i < wsize - 1; i++) { - if (*s == 0 || my_isblank(s[i])) - break; - w[i] = s[i]; - } - w[i] = 0; - - if (*s != 0 && !my_isblank(s[i])) - printf("warning: '%s' truncated\n", w); - - return s + i; -} +#include "my_str.h" struct sl_item { char *name; @@ -49,15 +18,15 @@ static int cmp_sym(const void *p1_, const void *p2_) int i; for (i = 0; ; i++) { + if ((s1[i] | s2[i]) == 0) + break; + if (s1[i] == s2[i]) continue; - if (s1[i] == 0) { - if (s2[i] == 0 || s2[i] == '@') - break; - } - - if (s2[i] == 0 || s1[i] == '@') + if (s1[i] == 0 && s2[i] == '@') + break; + if (s1[i] == '@' && s2[i] == 0) break; return s1[i] - s2[i]; @@ -87,11 +56,10 @@ void read_list(struct sl_item **sl_in, int *cnt, int *alloc, int c = *cnt; char line[256]; char word[256]; - char *r; while (fgets(line, sizeof(line), f) != NULL) { - r = next_word(word, sizeof(word), line); - if (r == word) + next_word(word, sizeof(word), line); + if (word[0] == 0 || word[0] == ';' || word[0] == '#') continue; sl[c].name = strdup(word); @@ -116,8 +84,7 @@ const char *sym_use(const struct sl_item *sym) static char buf[256+3]; int ret; - ret = snprintf(buf, sizeof(buf), "%s%s", - sym->callsites ? "" : "rm_", sym->name); + ret = snprintf(buf, sizeof(buf), "rm_%s", sym->name); if (ret >= sizeof(buf)) { printf("truncation detected: '%s'\n", buf); exit(1); @@ -126,6 +93,10 @@ 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)) + int main(int argc, char *argv[]) { struct sl_item *symlist, *sym, ssym = { NULL, }; @@ -138,7 +109,10 @@ int main(int argc, char *argv[]) char word2[256]; char word3[256]; char word4[256]; - char *p; + char word5[256]; + char word6[256]; + char func[256]; + char *p, *p2; int i; if (argc < 4) { @@ -154,7 +128,7 @@ int main(int argc, char *argv[]) my_assert_not(symlist, NULL); for (i = 3; i < argc; i++) { - if (!strcmp(argv[i], "-c")) { + if (strcmp(argv[i], "-c") == 0) { patch_callsites = 1; continue; } @@ -194,7 +168,12 @@ int main(int argc, char *argv[]) p = next_word(word2, sizeof(word2), p); - if (!strcasecmp(word2, "proc") || !strcasecmp(word2, "endp")) { + 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); @@ -205,11 +184,13 @@ int main(int argc, char *argv[]) } } - if (!strcasecmp(word, "call") || !strcasecmp(word, "jmp")) { + if (IS_OR3(word, "call", "jmp", "public")) { ssym.name = word2; sym = bsearch(&ssym, symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym); - if (sym != NULL && sym->callsites) { + if (sym != NULL + && (sym->callsites || IS(word2, func))) + { fprintf(fout, "\t\t%s\t%s%s", word, sym_use(sym), p); continue; @@ -222,36 +203,111 @@ int main(int argc, char *argv[]) p = next_word(word3, sizeof(word3), p); - if (!strcasecmp(word, "dd") && !strcasecmp(word2, "offset")) { + // push offset + // jcc short + if ( (IS(word, "push") && IS(word2, "offset")) + || (word[0] == 'j' && IS(word2, "short")) ) { ssym.name = word3; sym = bsearch(&ssym, symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym); - if (sym != NULL && sym->callsites) { - fprintf(fout, "\t\tdd offset %s%s", - sym_use(sym), p); + if (sym != NULL + && (sym->callsites || IS(word3, func))) + { + fprintf(fout, "\t\t%s %s %s%s", + word, word2, sym_use(sym), p); continue; } } + // dd offset + if (IS(word, "dd") && IS(word2, "offset")) { + fprintf(fout, "\t\tdd"); + strcpy(word, word3); + goto offset_loop; + } + p = sskip(p); if (*p == 0 || *p == ';') goto pass; // need at least 4 words p = next_word(word4, sizeof(word4), p); - if (!strcasecmp(word2, "dd") && !strcasecmp(word3, "offset")) { + // dd offset + if (IS(word2, "dd") && IS(word3, "offset")) { + fprintf(fout, "%s\tdd", word); + strcpy(word, word4); + goto offset_loop; + } + + // mov , offset + // jcc ptr + if ( (IS(word, "mov") && IS(word3, "offset")) + || (word[0] == 'j' && IS(word3, "ptr")) ) { 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, + fprintf(fout, "\t\t%s\t%s %s %s%s", + word, word2, word3, sym_use(sym), p); continue; } } + p = sskip(p); + if (*p == 0 || *p == ';') + goto pass; // need at least 5 words + + p = next_word(word5, sizeof(word5), p); + + p = sskip(p); + if (*p == 0 || *p == ';') + goto pass; // need at least 6 words + + p = next_word(word6, sizeof(word6), p); + + // dword ptr , offset + if ( IS(word2, "dword") && IS(word3, "ptr") + && IS(word5, "offset") ) { + ssym.name = word6; + sym = bsearch(&ssym, symlist, symlist_cnt, + sizeof(symlist[0]), cmp_sym); + if (sym != NULL && sym->callsites) { + fprintf(fout, "\t\t%s\tdword ptr %s offset %s%s", + word, word4, sym_use(sym), p); + continue; + } + } + pass: fwrite(line, 1, strlen(line), fout); + continue; + +offset_loop: + while (1) { + 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->callsites) ? sym_use(sym) : 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; } for (i = 0; i < symlist_cnt; i++) { @@ -264,3 +320,5 @@ pass: return 0; } + +// vim:ts=2:shiftwidth=2