5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
13 #include "my_assert.h"
17 #include "protoparse.h"
19 static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" };
21 static int is_x86_reg_saved(const char *reg)
23 static const char *nosave_regs[] = { "eax", "edx", "ecx" };
27 for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
28 if (strcmp(reg, nosave_regs[r]) == 0)
34 // output decorated name
35 static const char *pp_to_name(const struct parsed_proto *pp)
40 if (!pp->is_fastcall && pp->argc_reg != 0) {
41 // can only be handled by __cdecl C func
42 snprintf(buf, sizeof(buf), "_%s", pp->name);
48 snprintf(atval, sizeof(atval), "@%d",
51 snprintf(buf, sizeof(buf), "%s%s%s",
52 pp->is_fastcall ? "@" : "_",
58 static void out_toasm_x86(FILE *f, const char *sym_out,
59 const struct parsed_proto *pp)
62 int sarg_ofs = 1; // stack offset to args, in DWORDs
63 int args_repushed = 0;
68 argc_repush = pp->argc;
70 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
72 for (i = 0; i < pp->argc; i++) {
73 if (pp->arg[i].reg != NULL)
74 must_save |= is_x86_reg_saved(pp->arg[i].reg);
77 name = pp_to_name(pp);
78 fprintf(f, ".global %s\n", name);
79 fprintf(f, "%s:\n", name);
81 if (pp->argc_reg == 0 || pp->is_fastcall) {
82 fprintf(f, "\t# %s\n",
83 pp->is_fastcall ? "__fastcall" :
84 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
85 fprintf(f, "\tjmp %s\n\n", sym_out);
89 if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
90 && !pp->is_vararg && !pp->has_retreg)
93 for (i = 0; i < pp->argc; i++) {
94 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
95 (i + sarg_ofs) * 4, pp->arg[i].reg);
97 fprintf(f, "\tjmp %s\n\n", sym_out);
101 // asm_stack_args | saved_regs | ra | args_from_c
104 // because we don't always know what we are calling,
105 // be safe and save everything that has to be saved in __cdecl
106 for (i = 0; i < ARRAY_SIZE(c_save_regs); i++) {
107 fprintf(f, "\tpushl %%%s\n", c_save_regs[i]);
111 // reconstruct arg stack for asm
112 for (i = argc_repush - 1; i >= 0; i--) {
113 if (pp->arg[i].reg == NULL) {
114 fprintf(f, "\tmovl %d(%%esp), %%eax\n",
116 fprintf(f, "\tpushl %%eax\n");
123 for (i = 0; i < pp->argc; i++) {
124 if (pp->arg[i].reg != NULL) {
125 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
126 (i + sarg_ofs) * 4, pp->arg[i].reg);
127 if (pp->arg[i].type.is_retreg)
128 fprintf(f, "\tmovl (%%%s), %%%s\n",
129 pp->arg[i].reg, pp->arg[i].reg);
133 fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
134 fprintf(f, "\tcall %s\n\n", sym_out);
136 if (args_repushed && !pp->is_stdcall) {
137 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
138 sarg_ofs -= args_repushed;
141 // update the retreg regs
142 if (pp->has_retreg) {
143 for (i = 0; i < pp->argc; i++) {
144 if (pp->arg[i].type.is_retreg) {
145 fprintf(f, "\tmovl %d(%%esp), %%ecx\n"
146 "\tmovl %%%s, (%%ecx)\n",
147 (i + sarg_ofs) * 4, pp->arg[i].reg);
153 for (i = ARRAY_SIZE(c_save_regs) - 1; i >= 0; i--)
154 fprintf(f, "\tpopl %%%s\n", c_save_regs[i]);
156 fprintf(f, "\tret\n\n");
159 static void out_fromasm_x86(FILE *f, const char *sym,
160 const struct parsed_proto *pp)
162 int reg_ofs[ARRAY_SIZE(pp->arg)];
163 int sarg_ofs = 1; // stack offset to args, in DWORDs
173 argc_repush = pp->argc;
174 stack_args = pp->argc_stack;
176 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
177 stack_args = argc_repush - pp->argc_reg;
180 ret64 = strstr(pp->ret_type.name, "int64") != NULL;
183 pp->is_fastcall ? "__fastcall" :
184 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
186 fprintf(f, " ret64");
187 if (!pp->is_fastcall && pp->argc_reg != 0)
190 if (pp->is_stdcall && !pp->is_fastcall && pp->argc_reg != 0
191 && !IS_START(sym, "sub_") && !IS_START(sym, "f_"))
193 // alias for possible .def export
196 snprintf(sym2, sizeof(sym2), "_%s@%d",
198 fprintf(f, "\n.global %s # for .def\n", sym2);
199 fprintf(f, "%s:", sym2);
201 fprintf(f, "\n.global %s\n", sym);
202 fprintf(f, "%s:\n", sym);
204 if ((pp->argc_reg == 0 || pp->is_fastcall)
205 && !IS(pp->name, "storm_491")) // wants edx save :(
207 fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
211 c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
213 // at least sc sub_47B150 needs edx to be preserved
214 // int64 returns use edx:eax - no edx save
215 // we use ecx also as scratch
216 fprintf(f, "\tpushl %%ecx\n");
221 fprintf(f, "\tpushl %%edx\n");
227 // need space for retreg args
228 if (pp->has_retreg) {
229 for (i = 0; i < pp->argc; i++) {
230 if (!pp->arg[i].type.is_retreg)
232 if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
233 reg_ofs[i] = ecx_ofs;
236 if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
237 reg_ofs[i] = edx_ofs;
240 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
243 reg_ofs[i] = sarg_ofs;
247 // construct arg stack
248 for (i = argc_repush - 1; i >= 0; i--) {
249 if (pp->arg[i].reg == NULL) {
250 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
251 (sarg_ofs + stack_args - 1) * 4);
252 fprintf(f, "\tpushl %%ecx\n");
256 const char *reg = pp->arg[i].reg;
257 if (pp->arg[i].type.is_retreg) {
259 fprintf(f, "\tlea %d(%%esp), %%ecx\n",
260 (sarg_ofs - reg_ofs[i]) * 4);
262 else if (IS(reg, "ecx"))
263 // must reload original ecx
264 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
267 fprintf(f, "\tpushl %%%s\n", reg);
272 fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
274 if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
275 fprintf(f, "\tadd $%d,%%esp\n",
276 (sarg_ofs - (saved_regs + 1)) * 4);
279 if (pp->has_retreg) {
280 for (i = pp->argc - 1; i >= 0; i--) {
281 if (!pp->arg[i].type.is_retreg)
283 if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
286 if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
289 fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
294 fprintf(f, "\tpopl %%edx\n");
295 fprintf(f, "\tpopl %%ecx\n");
297 if (pp->is_stdcall && pp->argc_stack)
298 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
300 fprintf(f, "\tret\n\n");
303 int main(int argc, char *argv[])
305 FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
306 const struct parsed_proto *pp;
314 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
320 fhdr = fopen(hdrfn, "r");
321 my_assert_not(fhdr, NULL);
323 fsyms_from = fopen(argv[3], "r");
324 my_assert_not(fsyms_from, NULL);
326 fsyms_to = fopen(argv[2], "r");
327 my_assert_not(fsyms_to, NULL);
329 fout = fopen(argv[1], "w");
330 my_assert_not(fout, NULL);
332 fprintf(fout, ".text\n\n");
333 fprintf(fout, "# C -> asm\n\n");
335 while (fgets(line, sizeof(line), fsyms_to))
337 next_word(sym, sizeof(sym), line);
338 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
341 // IDA asm doesn't do '@' notation..
342 strcpy(sym_noat, sym);
343 p = strchr(sym_noat, '@');
347 pp = proto_parse(fhdr, sym_noat, 0);
351 out_toasm_x86(fout, sym_noat, pp);
354 fprintf(fout, "# asm -> C\n\n");
356 while (fgets(line, sizeof(line), fsyms_from))
358 next_word(sym, sizeof(sym), line);
359 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
362 pp = proto_parse(fhdr, sym, 0);
366 out_fromasm_x86(fout, sym, pp);