X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tools%2Fmkbridge.c;h=2066f02197d6f1d58d8597e8d913922b2beeb0bf;hb=5c024ef78bfc9bac8b67870c169ab8732d8f7d89;hp=4c70246786d10dc267372dbb91c9df0500f2ba2a;hpb=a51421fac74994697dcfd507336eac414a334199;p=ia32rtools.git diff --git a/tools/mkbridge.c b/tools/mkbridge.c index 4c70246..2066f02 100644 --- a/tools/mkbridge.c +++ b/tools/mkbridge.c @@ -6,204 +6,9 @@ #include "my_str.h" #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) +#define IS(w, y) !strcmp(w, y) -static int find_protostr(char *dst, size_t dlen, FILE *fhdr, - const char *sym, int *pline) -{ - int line = 0; - char *p; - - rewind(fhdr); - - while (fgets(dst, dlen, fhdr)) - { - line++; - if (strstr(dst, sym) != NULL) - break; - } - *pline = line; - - if (feof(fhdr)) - return -1; - - p = dst + strlen(dst); - for (p--; p > dst && my_isblank(*p); --p) - *p = 0; - - return 0; -} - -static int get_regparm(char *dst, size_t dlen, char *p) -{ - int i, o; - - if (*p != '<') - return 0; - - for (o = 0, i = 1; o < dlen; i++) { - if (p[i] == 0) - return 0; - if (p[i] == '>') - break; - dst[o++] = p[i]; - } - dst[o] = 0; - return i + 1; -} - -// hmh.. -static const char *known_types[] = { - "char", - "unsigned __int8", - "int", - "signed int", - "unsigned int", - "void", - "BYTE", - "WORD", - "DWORD", - "HMODULE", - "HANDLE", - "HWND", - "LPCSTR", - "size_t", - "void *", - "const void *", - "FILE *", -}; - -static int check_type(const char *name) -{ - int i, l; - - for (i = 0; i < ARRAY_SIZE(known_types); i++) { - l = strlen(known_types[i]); - if (strncmp(known_types[i], name, l) == 0) - return l; - } - - return 0; -} - -static const char *hdrfn; -static int pline = 0; - -static int parse_protostr(char *protostr, char **reglist, int *cnt_out, - int *is_stdcall) -{ - char regparm[16]; - char buf[256]; - int xarg = 0; - int ret; - char *p; - - p = protostr; - if (p[0] == '/' && p[1] == '/') { - //printf("warning: decl for sym '%s' is commented out\n", sym); - p = sskip(p + 2); - } - - ret = check_type(p); - if (ret <= 0) { - printf("%s:%d:%ld: unhandled return in '%s'\n", - hdrfn, pline, (p - protostr) + 1, protostr); - return 1; - } - p += ret; - p = sskip(p); - - p = next_word(buf, sizeof(buf), p); - p = sskip(p); - if (buf[0] == 0) { - printf("%s:%d:%ld: cconv missing\n", - hdrfn, pline, (p - protostr) + 1); - return 1; - } - if (strcmp(buf, "__cdecl") == 0) - *is_stdcall = 0; - else if (strcmp(buf, "__stdcall") == 0) - *is_stdcall = 1; - else if (strcmp(buf, "__userpurge") == 0) - *is_stdcall = 1; // in all cases seen.. - else if (strcmp(buf, "__usercall") == 0) - *is_stdcall = 0; // ..or is it? - else { - // TODO: __thiscall needs special handling (arg1~ecx) - printf("%s:%d:%ld: unhandled cconv: '%s'\n", - hdrfn, pline, (p - protostr) + 1, buf); - return 1; - } - - p = next_idt(buf, sizeof(buf), p); - p = sskip(p); - if (buf[0] == 0) { - printf("%s:%d:%ld: func name missing\n", - hdrfn, pline, (p - protostr) + 1); - return 1; - } - - ret = get_regparm(regparm, sizeof(regparm), p); - if (ret > 0) { - if (strcmp(regparm, "eax") && strcmp(regparm, "ax") - && strcmp(regparm, "al")) - { - printf("%s:%d:%ld: bad regparm: %s\n", - hdrfn, pline, (p - protostr) + 1, regparm); - return 1; - } - p += ret; - p = sskip(p); - } - - if (*p != '(') { - printf("%s:%d:%ld: '(' expected, got '%c'\n", - hdrfn, pline, (p - protostr) + 1, *p); - return 1; - } - p++; - - while (1) { - p = sskip(p); - if (*p == ')') - break; - if (*p == ',') - p = sskip(p + 1); - - xarg++; - - ret = check_type(p); - if (ret <= 0) { - printf("%s:%d:%ld: unhandled type for arg%d\n", - hdrfn, pline, (p - protostr) + 1, xarg); - return 1; - } - p += ret; - p = sskip(p); - - p = next_idt(buf, sizeof(buf), p); - p = sskip(p); -#if 0 - if (buf[0] == 0) { - printf("%s:%d:%ld: idt missing for arg%d\n", - hdrfn, pline, (p - protostr) + 1, xarg); - return 1; - } -#endif - reglist[xarg - 1] = NULL; - - ret = get_regparm(regparm, sizeof(regparm), p); - if (ret > 0) { - p += ret; - p = sskip(p); - - reglist[xarg - 1] = strdup(regparm); - } - } - - *cnt_out = xarg; - - return 0; -} +#include "protoparse.h" static int is_x86_reg_saved(const char *reg) { @@ -218,55 +23,58 @@ static int is_x86_reg_saved(const char *reg) return !nosave; } -static void out_toasm_x86(FILE *f, char *sym, char *reg_list[], int reg_cnt, - int is_stdcall) +static void out_toasm_x86(FILE *f, const char *sym_in, + const char *sym_out, const struct parsed_proto *pp) { - int have_normal = 0; // normal args - int have_regs = 0; int must_save = 0; int sarg_ofs = 1; // stack offset to args, in DWORDs int args_repushed = 0; + int argc_repush; int i; - for (i = 0; i < reg_cnt; i++) { - if (reg_list[i] == NULL) { - have_normal++; - continue; - } + argc_repush = pp->argc; + if (pp->is_vararg) + argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough? - have_regs++; - must_save |= is_x86_reg_saved(reg_list[i]); + for (i = 0; i < pp->argc; i++) { + if (pp->arg[i].reg != NULL) + 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%s\n", pp->is_fastcall ? "@" : "_", sym_in); + fprintf(f, "%s%s:\n", pp->is_fastcall ? "@" : "_", sym_in); - if (!have_regs && !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; } - if (!have_normal && !must_save && !is_stdcall) { + if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall + && !pp->is_vararg) + { // load arg regs - for (i = 0; i < reg_cnt; i++) { + for (i = 0; i < pp->argc; i++) { fprintf(f, "\tmovl %d(%%esp), %%%s\n", - (i + sarg_ofs) * 4, reg_list[i]); + (i + sarg_ofs) * 4, pp->arg[i].reg); } - fprintf(f, "\tjmp %s\n\n", sym); + fprintf(f, "\tjmp %s\n\n", sym_out); return; } // save the regs - for (i = 0; i < reg_cnt; i++) { - if (reg_list[i] != NULL && is_x86_reg_saved(reg_list[i])) { - fprintf(f, "\tpushl %%%s\n", reg_list[i]); + 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++; } } // reconstruct arg stack - for (i = reg_cnt - 1; i >= 0; i--) { - if (reg_list[i] == NULL) { + for (i = argc_repush - 1; i >= 0; i--) { + if (pp->arg[i].reg == NULL) { fprintf(f, "\tmovl %d(%%esp), %%eax\n", (i + sarg_ofs) * 4); fprintf(f, "\tpushl %%eax\n"); @@ -274,72 +82,94 @@ static void out_toasm_x86(FILE *f, char *sym, char *reg_list[], int reg_cnt, args_repushed++; } } - my_assert(args_repushed, have_normal); + // my_assert(args_repushed, pp->argc_stack); // load arg regs - for (i = 0; i < reg_cnt; i++) { - if (reg_list[i] != NULL) { + for (i = 0; i < pp->argc; i++) { + if (pp->arg[i].reg != NULL) { fprintf(f, "\tmovl %d(%%esp), %%%s\n", - (i + sarg_ofs) * 4, reg_list[i]); + (i + sarg_ofs) * 4, pp->arg[i].reg); } } - fprintf(f, "\n\t# %s\n", is_stdcall ? "__stdcall" : "__cdecl"); - fprintf(f, "\tcall %s\n\n", sym); + fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl"); + fprintf(f, "\tcall %s\n\n", sym_out); - if (args_repushed && !is_stdcall) + if (args_repushed && !pp->is_stdcall) fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4); // restore regs - for (i = reg_cnt - 1; i >= 0; i--) { - if (reg_list[i] != NULL && is_x86_reg_saved(reg_list[i])) - fprintf(f, "\tpopl %%%s\n", reg_list[i]); + 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); } fprintf(f, "\tret\n\n"); } -static void out_fromasm_x86(FILE *f, char *sym, char *reg_list[], int reg_cnt, - int is_stdcall) +static void out_fromasm_x86(FILE *f, const char *sym, + const struct parsed_proto *pp) { - int have_normal = 0; // normal args - int have_regs = 0; int sarg_ofs = 1; // stack offset to args, in DWORDs + int saved_regs = 0; + int argc_repush; int stack_args; + int ret64; int i; - for (i = 0; i < reg_cnt; i++) { - if (reg_list[i] == NULL) { - have_normal++; - continue; - } - - have_regs++; + argc_repush = pp->argc; + stack_args = pp->argc_stack; + if (pp->is_vararg) { + argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough? + stack_args = argc_repush - pp->argc_reg; } - fprintf(f, "# %s\n", 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 (!have_regs) { - fprintf(f, "\tjmp _%s\n\n", sym); + if (pp->argc_reg == 0 || pp->is_fastcall) { + fprintf(f, "\tjmp %s%s", + pp->is_fastcall ? "@" : "_", 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.. + // 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 - stack_args = have_normal; - for (i = reg_cnt - 1; i >= 0; i--) { - if (reg_list[i] == NULL) { - fprintf(f, "\tmovl %d(%%esp), %%edx\n", + for (i = argc_repush - 1; i >= 0; i--) { + if (pp->arg[i].reg == NULL) { + 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 { - fprintf(f, "\tpushl %%%s\n", reg_list[i]); + 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); } sarg_ofs++; } @@ -347,13 +177,16 @@ static void out_fromasm_x86(FILE *f, char *sym, char *reg_list[], int reg_cnt, // no worries about calling conventions - always __cdecl fprintf(f, "\n\tcall _%s\n\n", sym); - if (sarg_ofs > 2) - fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - 2) * 4); + if (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 (is_stdcall && have_normal) - fprintf(f, "\tret $%d\n\n", have_normal * 4); + if (pp->is_stdcall && pp->argc_stack) + fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4); else fprintf(f, "\tret\n\n"); } @@ -361,13 +194,12 @@ static void out_fromasm_x86(FILE *f, char *sym, char *reg_list[], int reg_cnt, int main(int argc, char *argv[]) { FILE *fout, *fsyms_to, *fsyms_from, *fhdr; - char protostr[256]; + const struct parsed_proto *pp; char line[256]; + char sym_noat[256]; char sym[256]; - char *reg_list[16]; - int is_stdcall = 0; - int reg_cnt = 0; - int ret; + char *p; + int ret = 1; if (argc != 5) { printf("usage:\n%s \n", @@ -397,19 +229,17 @@ int main(int argc, char *argv[]) if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#') continue; - ret = find_protostr(protostr, sizeof(protostr), fhdr, - sym, &pline); - if (ret != 0) { - printf("%s: sym '%s' is missing\n", - hdrfn, sym); - goto out; - } + // IDA asm doesn't do '@' notation.. + strcpy(sym_noat, sym); + p = strchr(sym_noat, '@'); + if (p != NULL) + *p = 0; - ret = parse_protostr(protostr, reg_list, ®_cnt, &is_stdcall); - if (ret) + pp = proto_parse(fhdr, sym_noat, 0); + if (pp == NULL) goto out; - out_toasm_x86(fout, sym, reg_list, reg_cnt, is_stdcall); + out_toasm_x86(fout, sym, sym_noat, pp); } fprintf(fout, "# from asm\n\n"); @@ -420,19 +250,11 @@ int main(int argc, char *argv[]) if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#') continue; - ret = find_protostr(protostr, sizeof(protostr), fhdr, - sym, &pline); - if (ret != 0) { - printf("%s: sym '%s' is missing\n", - hdrfn, sym); - goto out; - } - - ret = parse_protostr(protostr, reg_list, ®_cnt, &is_stdcall); - if (ret) + pp = proto_parse(fhdr, sym, 0); + if (pp == NULL) goto out; - out_fromasm_x86(fout, sym, reg_list, reg_cnt, is_stdcall); + out_fromasm_x86(fout, sym, pp); } ret = 0;