X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tools%2Fmkbridge.c;h=01a8f56629d2bef4591496c781c54d7b4c229293;hb=2b70f6d33838eccf24330e79cf939f2d5632a1c7;hp=705567fa7a94d23d267509e40ffe5777f9c92a1b;hpb=36595fd27d06b03f1abf320bd511aec325845ed5;p=ia32rtools.git diff --git a/tools/mkbridge.c b/tools/mkbridge.c index 705567f..01a8f56 100644 --- a/tools/mkbridge.c +++ b/tools/mkbridge.c @@ -1,3 +1,11 @@ +/* + * 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 @@ -10,6 +18,8 @@ #include "protoparse.h" +static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" }; + static int is_x86_reg_saved(const char *reg) { static const char *nosave_regs[] = { "eax", "edx", "ecx" }; @@ -23,13 +33,38 @@ static int is_x86_reg_saved(const char *reg) return !nosave; } -static void out_toasm_x86(FILE *f, const char *sym_in, - const char *sym_out, const 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; @@ -41,16 +76,20 @@ static void out_toasm_x86(FILE *f, const char *sym_in, must_save |= is_x86_reg_saved(pp->arg[i].reg); } - fprintf(f, ".global _%s\n", sym_in); - fprintf(f, "_%s:\n", sym_in); + name = pp_to_name(pp); + fprintf(f, ".global %s\n", name); + fprintf(f, "%s:\n", name); - if (pp->argc_reg == 0) { + 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; } if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall - && !pp->is_vararg) + && !pp->is_vararg && !pp->has_retreg) { // load arg regs for (i = 0; i < pp->argc; i++) { @@ -61,15 +100,17 @@ static void out_toasm_x86(FILE *f, const char *sym_in, return; } + // asm_stack_args | saved_regs | ra | args_from_c + // save the regs - for (i = 0; i < pp->argc; i++) { - if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) { - fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg); - sarg_ofs++; - } + // because we don't always know what we are calling, + // be safe and save everything that has to be saved in __cdecl + for (i = 0; i < ARRAY_SIZE(c_save_regs); i++) { + fprintf(f, "\tpushl %%%s\n", c_save_regs[i]); + sarg_ofs++; } - // reconstruct arg stack + // reconstruct arg stack for asm for (i = argc_repush - 1; i >= 0; i--) { if (pp->arg[i].reg == NULL) { fprintf(f, "\tmovl %d(%%esp), %%eax\n", @@ -79,36 +120,53 @@ static void out_toasm_x86(FILE *f, const char *sym_in, args_repushed++; } } - // my_assert(args_repushed, pp->argc_stack); // load arg regs for (i = 0; i < pp->argc; i++) { if (pp->arg[i].reg != NULL) { fprintf(f, "\tmovl %d(%%esp), %%%s\n", (i + sarg_ofs) * 4, pp->arg[i].reg); + if (pp->arg[i].type.is_retreg) + fprintf(f, "\tmovl (%%%s), %%%s\n", + pp->arg[i].reg, pp->arg[i].reg); } } fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl"); fprintf(f, "\tcall %s\n\n", sym_out); - if (args_repushed && !pp->is_stdcall) + if (args_repushed && !pp->is_stdcall) { fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4); + sarg_ofs -= args_repushed; + } - // restore regs - for (i = pp->argc - 1; i >= 0; i--) { - if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) - fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg); + // update the retreg regs + if (pp->has_retreg) { + for (i = 0; i < pp->argc; i++) { + if (pp->arg[i].type.is_retreg) { + fprintf(f, "\tmovl %d(%%esp), %%ecx\n" + "\tmovl %%%s, (%%ecx)\n", + (i + sarg_ofs) * 4, pp->arg[i].reg); + } + } } + // restore regs + for (i = ARRAY_SIZE(c_save_regs) - 1; i >= 0; i--) + fprintf(f, "\tpopl %%%s\n", c_save_regs[i]); + fprintf(f, "\tret\n\n"); } static void out_fromasm_x86(FILE *f, const char *sym, const struct parsed_proto *pp) { + int reg_ofs[ARRAY_SIZE(pp->arg)]; int sarg_ofs = 1; // stack offset to args, in DWORDs int saved_regs = 0; + int ecx_ofs = -1; + int edx_ofs = -1; + int c_is_stdcall; int argc_repush; int stack_args; int ret64; @@ -123,31 +181,55 @@ static void out_fromasm_x86(FILE *f, const char *sym, ret64 = strstr(pp->ret_type.name, "int64") != NULL; - fprintf(f, "# %s", pp->is_stdcall ? "__stdcall" : "__cdecl"); + 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) { - //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"); + 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; } + 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++; + ecx_ofs = sarg_ofs; if (!ret64) { fprintf(f, "\tpushl %%edx\n"); saved_regs++; sarg_ofs++; + edx_ofs = sarg_ofs; + } + + // need space for retreg args + if (pp->has_retreg) { + for (i = 0; i < pp->argc; i++) { + if (!pp->arg[i].type.is_retreg) + continue; + if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) { + reg_ofs[i] = ecx_ofs; + continue; + } + if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) { + reg_ofs[i] = edx_ofs; + continue; + } + fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg); + saved_regs++; + sarg_ofs++; + reg_ofs[i] = sarg_ofs; + } } // construct arg stack @@ -159,23 +241,43 @@ static void out_fromasm_x86(FILE *f, const char *sym, stack_args--; } else { - if (IS(pp->arg[i].reg, "ecx")) + const char *reg = pp->arg[i].reg; + if (pp->arg[i].type.is_retreg) { + reg = "ecx"; + fprintf(f, "\tlea %d(%%esp), %%ecx\n", + (sarg_ofs - reg_ofs[i]) * 4); + } + else if (IS(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); + fprintf(f, "\tpushl %%%s\n", reg); } 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 > saved_regs + 1) + if (!c_is_stdcall && sarg_ofs > saved_regs + 1) fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - (saved_regs + 1)) * 4); + // pop retregs + if (pp->has_retreg) { + for (i = pp->argc - 1; i >= 0; i--) { + if (!pp->arg[i].type.is_retreg) + continue; + if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) { + continue; + } + if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) { + continue; + } + fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg); + } + } + if (!ret64) fprintf(f, "\tpopl %%edx\n"); fprintf(f, "\tpopl %%ecx\n"); @@ -234,7 +336,7 @@ int main(int argc, char *argv[]) if (pp == NULL) goto out; - out_toasm_x86(fout, sym, sym_noat, pp); + out_toasm_x86(fout, sym_noat, pp); } fprintf(fout, "# from asm\n\n");