8 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
9 #define IS(w, y) !strcmp(w, y)
11 #include "protoparse.h"
13 static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" };
15 static int is_x86_reg_saved(const char *reg)
17 static const char *nosave_regs[] = { "eax", "edx", "ecx" };
21 for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
22 if (strcmp(reg, nosave_regs[r]) == 0)
28 // output decorated name
29 static const char *pp_to_name(const struct parsed_proto *pp)
34 if (!pp->is_fastcall && pp->argc_reg != 0) {
35 // can only be handled by __cdecl C func
36 snprintf(buf, sizeof(buf), "_%s", pp->name);
42 snprintf(atval, sizeof(atval), "@%d",
45 snprintf(buf, sizeof(buf), "%s%s%s",
46 pp->is_fastcall ? "@" : "_",
52 static void out_toasm_x86(FILE *f, const char *sym_out,
53 const struct parsed_proto *pp)
56 int sarg_ofs = 1; // stack offset to args, in DWORDs
57 int args_repushed = 0;
62 argc_repush = pp->argc;
64 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
66 for (i = 0; i < pp->argc; i++) {
67 if (pp->arg[i].reg != NULL)
68 must_save |= is_x86_reg_saved(pp->arg[i].reg);
71 name = pp_to_name(pp);
72 fprintf(f, ".global %s\n", name);
73 fprintf(f, "%s:\n", name);
75 if (pp->argc_reg == 0 || pp->is_fastcall) {
76 fprintf(f, "\t# %s\n",
77 pp->is_fastcall ? "__fastcall" :
78 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
79 fprintf(f, "\tjmp %s\n\n", sym_out);
83 if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
84 && !pp->is_vararg && !pp->has_retreg)
87 for (i = 0; i < pp->argc; i++) {
88 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
89 (i + sarg_ofs) * 4, pp->arg[i].reg);
91 fprintf(f, "\tjmp %s\n\n", sym_out);
95 // asm_stack_args | saved_regs | ra | args_from_c
98 // because we don't always know what we are calling,
99 // be safe and save everything that has to be saved in __cdecl
100 for (i = 0; i < ARRAY_SIZE(c_save_regs); i++) {
101 fprintf(f, "\tpushl %%%s\n", c_save_regs[i]);
105 // reconstruct arg stack for asm
106 for (i = argc_repush - 1; i >= 0; i--) {
107 if (pp->arg[i].reg == NULL) {
108 fprintf(f, "\tmovl %d(%%esp), %%eax\n",
110 fprintf(f, "\tpushl %%eax\n");
117 for (i = 0; i < pp->argc; i++) {
118 if (pp->arg[i].reg != NULL) {
119 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
120 (i + sarg_ofs) * 4, pp->arg[i].reg);
121 if (pp->arg[i].type.is_retreg)
122 fprintf(f, "\tmovl (%%%s), %%%s\n",
123 pp->arg[i].reg, pp->arg[i].reg);
127 fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
128 fprintf(f, "\tcall %s\n\n", sym_out);
130 if (args_repushed && !pp->is_stdcall) {
131 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
132 sarg_ofs -= args_repushed;
135 // update the retreg regs
136 if (pp->has_retreg) {
137 for (i = 0; i < pp->argc; i++) {
138 if (pp->arg[i].type.is_retreg) {
139 fprintf(f, "\tmovl %d(%%esp), %%ecx\n"
140 "\tmovl %%%s, (%%ecx)\n",
141 (i + sarg_ofs) * 4, pp->arg[i].reg);
147 for (i = ARRAY_SIZE(c_save_regs) - 1; i >= 0; i--)
148 fprintf(f, "\tpopl %%%s\n", c_save_regs[i]);
150 fprintf(f, "\tret\n\n");
153 static void out_fromasm_x86(FILE *f, const char *sym,
154 const struct parsed_proto *pp)
156 int reg_ofs[ARRAY_SIZE(pp->arg)];
157 int sarg_ofs = 1; // stack offset to args, in DWORDs
167 argc_repush = pp->argc;
168 stack_args = pp->argc_stack;
170 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
171 stack_args = argc_repush - pp->argc_reg;
174 ret64 = strstr(pp->ret_type.name, "int64") != NULL;
177 pp->is_fastcall ? "__fastcall" :
178 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
180 fprintf(f, " ret64");
181 fprintf(f, "\n.global %s\n", sym);
182 fprintf(f, "%s:\n", sym);
184 if ((pp->argc_reg == 0 || pp->is_fastcall)
185 && !IS(pp->name, "storm_491")) // wants edx save :(
187 fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
191 c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
193 // at least sc sub_47B150 needs edx to be preserved
194 // int64 returns use edx:eax - no edx save
195 // we use ecx also as scratch
196 fprintf(f, "\tpushl %%ecx\n");
201 fprintf(f, "\tpushl %%edx\n");
207 // need space for retreg args
208 if (pp->has_retreg) {
209 for (i = 0; i < pp->argc; i++) {
210 if (!pp->arg[i].type.is_retreg)
212 if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
213 reg_ofs[i] = ecx_ofs;
216 if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
217 reg_ofs[i] = edx_ofs;
220 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
223 reg_ofs[i] = sarg_ofs;
227 // construct arg stack
228 for (i = argc_repush - 1; i >= 0; i--) {
229 if (pp->arg[i].reg == NULL) {
230 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
231 (sarg_ofs + stack_args - 1) * 4);
232 fprintf(f, "\tpushl %%ecx\n");
236 const char *reg = pp->arg[i].reg;
237 if (pp->arg[i].type.is_retreg) {
239 fprintf(f, "\tlea %d(%%esp), %%ecx\n",
240 (sarg_ofs - reg_ofs[i]) * 4);
242 else if (IS(reg, "ecx"))
243 // must reload original ecx
244 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
247 fprintf(f, "\tpushl %%%s\n", reg);
252 fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
254 if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
255 fprintf(f, "\tadd $%d,%%esp\n",
256 (sarg_ofs - (saved_regs + 1)) * 4);
259 if (pp->has_retreg) {
260 for (i = pp->argc - 1; i >= 0; i--) {
261 if (!pp->arg[i].type.is_retreg)
263 if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
266 if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
269 fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
274 fprintf(f, "\tpopl %%edx\n");
275 fprintf(f, "\tpopl %%ecx\n");
277 if (pp->is_stdcall && pp->argc_stack)
278 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
280 fprintf(f, "\tret\n\n");
283 int main(int argc, char *argv[])
285 FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
286 const struct parsed_proto *pp;
294 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
300 fhdr = fopen(hdrfn, "r");
301 my_assert_not(fhdr, NULL);
303 fsyms_from = fopen(argv[3], "r");
304 my_assert_not(fsyms_from, NULL);
306 fsyms_to = fopen(argv[2], "r");
307 my_assert_not(fsyms_to, NULL);
309 fout = fopen(argv[1], "w");
310 my_assert_not(fout, NULL);
312 fprintf(fout, ".text\n\n");
313 fprintf(fout, "# to asm\n\n");
315 while (fgets(line, sizeof(line), fsyms_to))
317 next_word(sym, sizeof(sym), line);
318 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
321 // IDA asm doesn't do '@' notation..
322 strcpy(sym_noat, sym);
323 p = strchr(sym_noat, '@');
327 pp = proto_parse(fhdr, sym_noat, 0);
331 out_toasm_x86(fout, sym_noat, pp);
334 fprintf(fout, "# from asm\n\n");
336 while (fgets(line, sizeof(line), fsyms_from))
338 next_word(sym, sizeof(sym), line);
339 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
342 pp = proto_parse(fhdr, sym, 0);
346 out_fromasm_x86(fout, sym, pp);