type, bp frame, arg tracking improvements
[ia32rtools.git] / tools / mkbridge.c
index 1e2f458..4fb10a9 100644 (file)
@@ -23,7 +23,8 @@ static int is_x86_reg_saved(const char *reg)
        return !nosave;
 }
 
-static void out_toasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
+static void out_toasm_x86(FILE *f, const char *sym_in,
+       const char *sym_out, const struct parsed_proto *pp)
 {
        int must_save = 0;
        int sarg_ofs = 1; // stack offset to args, in DWORDs
@@ -40,11 +41,11 @@ static void out_toasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
                        must_save |= is_x86_reg_saved(pp->arg[i].reg);
        }
 
-       fprintf(f, ".global _%s\n", sym);
-       fprintf(f, "_%s:\n", sym);
+       fprintf(f, ".global _%s\n", sym_in);
+       fprintf(f, "_%s:\n", sym_in);
 
-       if (pp->argc_reg == 0 && !pp->is_stdcall) {
-               fprintf(f, "\tjmp %s\n\n", sym);
+       if (pp->argc_reg == 0) {
+               fprintf(f, "\tjmp %s\n\n", sym_out);
                return;
        }
 
@@ -56,7 +57,7 @@ static void out_toasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
                        fprintf(f, "\tmovl %d(%%esp), %%%s\n",
                                (i + sarg_ofs) * 4, pp->arg[i].reg);
                }
-               fprintf(f, "\tjmp %s\n\n", sym);
+               fprintf(f, "\tjmp %s\n\n", sym_out);
                return;
        }
 
@@ -89,7 +90,7 @@ static void out_toasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
        }
 
        fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
-       fprintf(f, "\tcall %s\n\n", sym);
+       fprintf(f, "\tcall %s\n\n", sym_out);
 
        if (args_repushed && !pp->is_stdcall)
                fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
@@ -103,7 +104,8 @@ static void out_toasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
        fprintf(f, "\tret\n\n");
 }
 
-static void out_fromasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
+static void out_fromasm_x86(FILE *f, const char *sym,
+       const struct parsed_proto *pp)
 {
        int sarg_ofs = 1; // stack offset to args, in DWORDs
        int argc_repush;
@@ -121,26 +123,31 @@ static void out_fromasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
        fprintf(f, ".global %s\n", sym);
        fprintf(f, "%s:\n", sym);
 
-       if (pp->argc_reg == 0 && !pp->is_stdcall) {
-               fprintf(f, "\tjmp _%s\n\n", sym);
+       if (pp->argc_reg == 0) {
+               //fprintf(f, "\tjmp _%s\n\n", sym);
+               fprintf(f, "\tjmp _%s", sym);
+               if (pp->is_stdcall && pp->argc > 0)
+                       fprintf(f, "@%d", pp->argc * 4);
+               fprintf(f, "\n\n");
                return;
        }
 
-       fprintf(f, "\tpushl %%edx\n"); // just in case..
+       // scratch reg, must not be eax or edx (for ret edx:eax)
+       fprintf(f, "\tpushl %%ebx\n");
        sarg_ofs++;
 
        // construct arg stack
        for (i = argc_repush - 1; i >= 0; i--) {
                if (pp->arg[i].reg == NULL) {
-                       fprintf(f, "\tmovl %d(%%esp), %%edx\n",
+                       fprintf(f, "\tmovl %d(%%esp), %%ebx\n",
                                (sarg_ofs + stack_args - 1) * 4);
-                       fprintf(f, "\tpushl %%edx\n");
+                       fprintf(f, "\tpushl %%ebx\n");
                        stack_args--;
                }
                else {
-                       if (IS(pp->arg[i].reg, "edx"))
-                               // must reload original edx
-                               fprintf(f, "\tmovl %d(%%esp), %%edx\n",
+                       if (IS(pp->arg[i].reg, "ebx"))
+                               // must reload original ebx
+                               fprintf(f, "\tmovl %d(%%esp), %%ebx\n",
                                        (sarg_ofs - 2) * 4);
 
                        fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
@@ -154,7 +161,7 @@ static void out_fromasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
        if (sarg_ofs > 2)
                fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - 2) * 4);
 
-       fprintf(f, "\tpopl %%edx\n");
+       fprintf(f, "\tpopl %%ebx\n");
 
        if (pp->is_stdcall && pp->argc_stack)
                fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
@@ -165,10 +172,12 @@ static void out_fromasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
 int main(int argc, char *argv[])
 {
        FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
-       struct parsed_proto pp;
+       const struct parsed_proto *pp;
        char line[256];
+       char sym_noat[256];
        char sym[256];
-       int ret;
+       char *p;
+       int ret = 1;
 
        if (argc != 5) {
                printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
@@ -198,12 +207,17 @@ int main(int argc, char *argv[])
                if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
                        continue;
 
-               ret = proto_parse(fhdr, sym, &pp);
-               if (ret)
+               // IDA asm doesn't do '@' notation..
+               strcpy(sym_noat, sym);
+               p = strchr(sym_noat, '@');
+               if (p != NULL)
+                       *p = 0;
+
+               pp = proto_parse(fhdr, sym_noat);
+               if (pp == NULL)
                        goto out;
 
-               out_toasm_x86(fout, sym, &pp);
-               proto_release(&pp);
+               out_toasm_x86(fout, sym, sym_noat, pp);
        }
 
        fprintf(fout, "# from asm\n\n");
@@ -214,12 +228,11 @@ int main(int argc, char *argv[])
                if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
                        continue;
 
-               ret = proto_parse(fhdr, sym, &pp);
-               if (ret)
+               pp = proto_parse(fhdr, sym);
+               if (pp == NULL)
                        goto out;
 
-               out_fromasm_x86(fout, sym, &pp);
-               proto_release(&pp);
+               out_fromasm_x86(fout, sym, pp);
        }
 
        ret = 0;