8 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
9 #define IS(w, y) !strcmp(w, y)
11 #include "protoparse.h"
13 static int is_x86_reg_saved(const char *reg)
15 static const char *nosave_regs[] = { "eax", "edx", "ecx" };
19 for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
20 if (strcmp(reg, nosave_regs[r]) == 0)
26 static void out_toasm_x86(FILE *f, const char *sym_in,
27 const char *sym_out, const struct parsed_proto *pp)
30 int sarg_ofs = 1; // stack offset to args, in DWORDs
31 int args_repushed = 0;
35 argc_repush = pp->argc;
37 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
39 for (i = 0; i < pp->argc; i++) {
40 if (pp->arg[i].reg != NULL)
41 must_save |= is_x86_reg_saved(pp->arg[i].reg);
44 fprintf(f, ".global _%s\n", sym_in);
45 fprintf(f, "_%s:\n", sym_in);
47 if (pp->argc_reg == 0) {
48 fprintf(f, "\tjmp %s\n\n", sym_out);
52 if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
56 for (i = 0; i < pp->argc; i++) {
57 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
58 (i + sarg_ofs) * 4, pp->arg[i].reg);
60 fprintf(f, "\tjmp %s\n\n", sym_out);
65 for (i = 0; i < pp->argc; i++) {
66 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) {
67 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
72 // reconstruct arg stack
73 for (i = argc_repush - 1; i >= 0; i--) {
74 if (pp->arg[i].reg == NULL) {
75 fprintf(f, "\tmovl %d(%%esp), %%eax\n",
77 fprintf(f, "\tpushl %%eax\n");
82 // my_assert(args_repushed, pp->argc_stack);
85 for (i = 0; i < pp->argc; i++) {
86 if (pp->arg[i].reg != NULL) {
87 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
88 (i + sarg_ofs) * 4, pp->arg[i].reg);
92 fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
93 fprintf(f, "\tcall %s\n\n", sym_out);
95 if (args_repushed && !pp->is_stdcall)
96 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
99 for (i = pp->argc - 1; i >= 0; i--) {
100 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg))
101 fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
104 fprintf(f, "\tret\n\n");
107 static void out_fromasm_x86(FILE *f, const char *sym,
108 const struct parsed_proto *pp)
110 int sarg_ofs = 1; // stack offset to args, in DWORDs
117 argc_repush = pp->argc;
118 stack_args = pp->argc_stack;
120 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
121 stack_args = argc_repush - pp->argc_reg;
124 ret64 = strstr(pp->ret_type.name, "int64") != NULL;
126 fprintf(f, "# %s", pp->is_stdcall ? "__stdcall" : "__cdecl");
128 fprintf(f, " ret64");
129 fprintf(f, "\n.global %s\n", sym);
130 fprintf(f, "%s:\n", sym);
132 if (pp->argc_reg == 0) {
133 //fprintf(f, "\tjmp _%s\n\n", sym);
134 fprintf(f, "\tjmp _%s", sym);
135 if (pp->is_stdcall && pp->argc > 0)
136 fprintf(f, "@%d", pp->argc * 4);
141 // at least sc sub_47B150 needs edx to be preserved
142 // int64 returns use edx:eax - no edx save
143 // we use ecx also as scratch
144 fprintf(f, "\tpushl %%ecx\n");
148 fprintf(f, "\tpushl %%edx\n");
153 // construct arg stack
154 for (i = argc_repush - 1; i >= 0; i--) {
155 if (pp->arg[i].reg == NULL) {
156 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
157 (sarg_ofs + stack_args - 1) * 4);
158 fprintf(f, "\tpushl %%ecx\n");
162 if (IS(pp->arg[i].reg, "ecx"))
163 // must reload original ecx
164 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
167 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
172 // no worries about calling conventions - always __cdecl
173 fprintf(f, "\n\tcall _%s\n\n", sym);
175 if (sarg_ofs > saved_regs + 1)
176 fprintf(f, "\tadd $%d,%%esp\n",
177 (sarg_ofs - (saved_regs + 1)) * 4);
180 fprintf(f, "\tpopl %%edx\n");
181 fprintf(f, "\tpopl %%ecx\n");
183 if (pp->is_stdcall && pp->argc_stack)
184 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
186 fprintf(f, "\tret\n\n");
189 int main(int argc, char *argv[])
191 FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
192 const struct parsed_proto *pp;
200 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
206 fhdr = fopen(hdrfn, "r");
207 my_assert_not(fhdr, NULL);
209 fsyms_from = fopen(argv[3], "r");
210 my_assert_not(fsyms_from, NULL);
212 fsyms_to = fopen(argv[2], "r");
213 my_assert_not(fsyms_to, NULL);
215 fout = fopen(argv[1], "w");
216 my_assert_not(fout, NULL);
218 fprintf(fout, ".text\n\n");
219 fprintf(fout, "# to asm\n\n");
221 while (fgets(line, sizeof(line), fsyms_to))
223 next_word(sym, sizeof(sym), line);
224 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
227 // IDA asm doesn't do '@' notation..
228 strcpy(sym_noat, sym);
229 p = strchr(sym_noat, '@');
233 pp = proto_parse(fhdr, sym_noat);
237 out_toasm_x86(fout, sym, sym_noat, pp);
240 fprintf(fout, "# from asm\n\n");
242 while (fgets(line, sizeof(line), fsyms_from))
244 next_word(sym, sizeof(sym), line);
245 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
248 pp = proto_parse(fhdr, sym);
252 out_fromasm_x86(fout, sym, pp);