mkbridge: use names from pp, decorate them
[ia32rtools.git] / tools / mkbridge.c
index 1e2f458..9b87445 100644 (file)
@@ -23,12 +23,38 @@ static int is_x86_reg_saved(const char *reg)
        return !nosave;
 }
 
-static void out_toasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
+// output decorated name
+static const char *pp_to_name(const struct parsed_proto *pp)
+{
+       static char buf[256];
+       char atval[16];
+
+       if (!pp->is_fastcall && pp->argc_reg != 0) {
+               // can only be handled by __cdecl C func
+               snprintf(buf, sizeof(buf), "_%s", pp->name);
+               return buf;
+       }
+
+       atval[0] = 0;
+       if (pp->is_stdcall) {
+               snprintf(atval, sizeof(atval), "@%d",
+                       pp->argc * 4);
+       }
+       snprintf(buf, sizeof(buf), "%s%s%s",
+               pp->is_fastcall ? "@" : "_",
+               pp->name, atval);
+
+       return buf;
+}
+
+static void out_toasm_x86(FILE *f, const char *sym_out,
+       const struct parsed_proto *pp)
 {
        int must_save = 0;
        int sarg_ofs = 1; // stack offset to args, in DWORDs
        int args_repushed = 0;
        int argc_repush;
+       const char *name;
        int i;
 
        argc_repush = pp->argc;
@@ -40,11 +66,15 @@ 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);
+       name = pp_to_name(pp);
+       fprintf(f, ".global %s\n", name);
+       fprintf(f, "%s:\n", name);
 
-       if (pp->argc_reg == 0 && !pp->is_stdcall) {
-               fprintf(f, "\tjmp %s\n\n", sym);
+       if (pp->argc_reg == 0 || pp->is_fastcall) {
+               fprintf(f, "\t# %s\n",
+                 pp->is_fastcall ? "__fastcall" :
+                 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
+               fprintf(f, "\tjmp %s\n\n", sym_out);
                return;
        }
 
@@ -56,7 +86,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 +119,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,11 +133,15 @@ 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 saved_regs = 0;
+       int c_is_stdcall;
        int argc_repush;
        int stack_args;
+       int ret64;
        int i;
 
        argc_repush = pp->argc;
@@ -117,30 +151,49 @@ static void out_fromasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
                stack_args = argc_repush - pp->argc_reg;
        }
 
-       fprintf(f, "# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
-       fprintf(f, ".global %s\n", sym);
+       ret64 = strstr(pp->ret_type.name, "int64") != NULL;
+
+       fprintf(f, "# %s",
+         pp->is_fastcall ? "__fastcall" :
+         (pp->is_stdcall ? "__stdcall" : "__cdecl"));
+       if (ret64)
+                fprintf(f, " ret64");
+       fprintf(f, "\n.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 || pp->is_fastcall)
+           && !IS(pp->name, "storm_491")) // wants edx save :(
+       {
+               fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
                return;
        }
 
-       fprintf(f, "\tpushl %%edx\n"); // just in case..
+       c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
+
+       // at least sc sub_47B150 needs edx to be preserved
+       // int64 returns use edx:eax - no edx save
+       // we use ecx also as scratch
+       fprintf(f, "\tpushl %%ecx\n");
+       saved_regs++;
        sarg_ofs++;
+       if (!ret64) {
+               fprintf(f, "\tpushl %%edx\n");
+               saved_regs++;
+               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), %%ecx\n",
                                (sarg_ofs + stack_args - 1) * 4);
-                       fprintf(f, "\tpushl %%edx\n");
+                       fprintf(f, "\tpushl %%ecx\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, "ecx"))
+                               // must reload original ecx
+                               fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
                                        (sarg_ofs - 2) * 4);
 
                        fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
@@ -148,13 +201,15 @@ static void out_fromasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
                sarg_ofs++;
        }
 
-       // no worries about calling conventions - always __cdecl
-       fprintf(f, "\n\tcall _%s\n\n", sym);
+       fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
 
-       if (sarg_ofs > 2)
-               fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - 2) * 4);
+       if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
+               fprintf(f, "\tadd $%d,%%esp\n",
+                       (sarg_ofs - (saved_regs + 1)) * 4);
 
-       fprintf(f, "\tpopl %%edx\n");
+       if (!ret64)
+               fprintf(f, "\tpopl %%edx\n");
+       fprintf(f, "\tpopl %%ecx\n");
 
        if (pp->is_stdcall && pp->argc_stack)
                fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
@@ -165,10 +220,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 +255,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, 0);
+               if (pp == NULL)
                        goto out;
 
-               out_toasm_x86(fout, sym, &pp);
-               proto_release(&pp);
+               out_toasm_x86(fout, sym_noat, pp);
        }
 
        fprintf(fout, "# from asm\n\n");
@@ -214,12 +276,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, 0);
+               if (pp == NULL)
                        goto out;
 
-               out_fromasm_x86(fout, sym, &pp);
-               proto_release(&pp);
+               out_fromasm_x86(fout, sym, pp);
        }
 
        ret = 0;