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"
16 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
17 #define IS(w, y) !strcmp(w, y)
19 #include "protoparse.h"
21 static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" };
23 static int is_x86_reg_saved(const char *reg)
25 static const char *nosave_regs[] = { "eax", "edx", "ecx" };
29 for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
30 if (strcmp(reg, nosave_regs[r]) == 0)
36 // output decorated name
37 static const char *pp_to_name(const struct parsed_proto *pp)
42 if (!pp->is_fastcall && pp->argc_reg != 0) {
43 // can only be handled by __cdecl C func
44 snprintf(buf, sizeof(buf), "_%s", pp->name);
50 snprintf(atval, sizeof(atval), "@%d",
53 snprintf(buf, sizeof(buf), "%s%s%s",
54 pp->is_fastcall ? "@" : "_",
60 static void out_toasm_x86(FILE *f, const char *sym_out,
61 const struct parsed_proto *pp)
64 int sarg_ofs = 1; // stack offset to args, in DWORDs
65 int args_repushed = 0;
70 argc_repush = pp->argc;
72 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
74 for (i = 0; i < pp->argc; i++) {
75 if (pp->arg[i].reg != NULL)
76 must_save |= is_x86_reg_saved(pp->arg[i].reg);
79 name = pp_to_name(pp);
80 fprintf(f, ".global %s\n", name);
81 fprintf(f, "%s:\n", name);
83 if (pp->argc_reg == 0 || pp->is_fastcall) {
84 fprintf(f, "\t# %s\n",
85 pp->is_fastcall ? "__fastcall" :
86 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
87 fprintf(f, "\tjmp %s\n\n", sym_out);
91 if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
92 && !pp->is_vararg && !pp->has_retreg)
95 for (i = 0; i < pp->argc; i++) {
96 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
97 (i + sarg_ofs) * 4, pp->arg[i].reg);
99 fprintf(f, "\tjmp %s\n\n", sym_out);
103 // asm_stack_args | saved_regs | ra | args_from_c
106 // because we don't always know what we are calling,
107 // be safe and save everything that has to be saved in __cdecl
108 for (i = 0; i < ARRAY_SIZE(c_save_regs); i++) {
109 fprintf(f, "\tpushl %%%s\n", c_save_regs[i]);
113 // reconstruct arg stack for asm
114 for (i = argc_repush - 1; i >= 0; i--) {
115 if (pp->arg[i].reg == NULL) {
116 fprintf(f, "\tmovl %d(%%esp), %%eax\n",
118 fprintf(f, "\tpushl %%eax\n");
125 for (i = 0; i < pp->argc; i++) {
126 if (pp->arg[i].reg != NULL) {
127 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
128 (i + sarg_ofs) * 4, pp->arg[i].reg);
129 if (pp->arg[i].type.is_retreg)
130 fprintf(f, "\tmovl (%%%s), %%%s\n",
131 pp->arg[i].reg, pp->arg[i].reg);
135 fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
136 fprintf(f, "\tcall %s\n\n", sym_out);
138 if (args_repushed && !pp->is_stdcall) {
139 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
140 sarg_ofs -= args_repushed;
143 // update the retreg regs
144 if (pp->has_retreg) {
145 for (i = 0; i < pp->argc; i++) {
146 if (pp->arg[i].type.is_retreg) {
147 fprintf(f, "\tmovl %d(%%esp), %%ecx\n"
148 "\tmovl %%%s, (%%ecx)\n",
149 (i + sarg_ofs) * 4, pp->arg[i].reg);
155 for (i = ARRAY_SIZE(c_save_regs) - 1; i >= 0; i--)
156 fprintf(f, "\tpopl %%%s\n", c_save_regs[i]);
158 fprintf(f, "\tret\n\n");
161 static void out_fromasm_x86(FILE *f, const char *sym,
162 const struct parsed_proto *pp)
164 int reg_ofs[ARRAY_SIZE(pp->arg)];
165 int sarg_ofs = 1; // stack offset to args, in DWORDs
175 argc_repush = pp->argc;
176 stack_args = pp->argc_stack;
178 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
179 stack_args = argc_repush - pp->argc_reg;
182 ret64 = strstr(pp->ret_type.name, "int64") != NULL;
185 pp->is_fastcall ? "__fastcall" :
186 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
188 fprintf(f, " ret64");
189 fprintf(f, "\n.global %s\n", sym);
190 fprintf(f, "%s:\n", sym);
192 if ((pp->argc_reg == 0 || pp->is_fastcall)
193 && !IS(pp->name, "storm_491")) // wants edx save :(
195 fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
199 c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
201 // at least sc sub_47B150 needs edx to be preserved
202 // int64 returns use edx:eax - no edx save
203 // we use ecx also as scratch
204 fprintf(f, "\tpushl %%ecx\n");
209 fprintf(f, "\tpushl %%edx\n");
215 // need space for retreg args
216 if (pp->has_retreg) {
217 for (i = 0; i < pp->argc; i++) {
218 if (!pp->arg[i].type.is_retreg)
220 if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
221 reg_ofs[i] = ecx_ofs;
224 if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
225 reg_ofs[i] = edx_ofs;
228 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
231 reg_ofs[i] = sarg_ofs;
235 // construct arg stack
236 for (i = argc_repush - 1; i >= 0; i--) {
237 if (pp->arg[i].reg == NULL) {
238 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
239 (sarg_ofs + stack_args - 1) * 4);
240 fprintf(f, "\tpushl %%ecx\n");
244 const char *reg = pp->arg[i].reg;
245 if (pp->arg[i].type.is_retreg) {
247 fprintf(f, "\tlea %d(%%esp), %%ecx\n",
248 (sarg_ofs - reg_ofs[i]) * 4);
250 else if (IS(reg, "ecx"))
251 // must reload original ecx
252 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
255 fprintf(f, "\tpushl %%%s\n", reg);
260 fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
262 if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
263 fprintf(f, "\tadd $%d,%%esp\n",
264 (sarg_ofs - (saved_regs + 1)) * 4);
267 if (pp->has_retreg) {
268 for (i = pp->argc - 1; i >= 0; i--) {
269 if (!pp->arg[i].type.is_retreg)
271 if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
274 if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
277 fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
282 fprintf(f, "\tpopl %%edx\n");
283 fprintf(f, "\tpopl %%ecx\n");
285 if (pp->is_stdcall && pp->argc_stack)
286 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
288 fprintf(f, "\tret\n\n");
291 int main(int argc, char *argv[])
293 FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
294 const struct parsed_proto *pp;
302 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
308 fhdr = fopen(hdrfn, "r");
309 my_assert_not(fhdr, NULL);
311 fsyms_from = fopen(argv[3], "r");
312 my_assert_not(fsyms_from, NULL);
314 fsyms_to = fopen(argv[2], "r");
315 my_assert_not(fsyms_to, NULL);
317 fout = fopen(argv[1], "w");
318 my_assert_not(fout, NULL);
320 fprintf(fout, ".text\n\n");
321 fprintf(fout, "# to asm\n\n");
323 while (fgets(line, sizeof(line), fsyms_to))
325 next_word(sym, sizeof(sym), line);
326 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
329 // IDA asm doesn't do '@' notation..
330 strcpy(sym_noat, sym);
331 p = strchr(sym_noat, '@');
335 pp = proto_parse(fhdr, sym_noat, 0);
339 out_toasm_x86(fout, sym_noat, pp);
342 fprintf(fout, "# from asm\n\n");
344 while (fgets(line, sizeof(line), fsyms_from))
346 next_word(sym, sizeof(sym), line);
347 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
350 pp = proto_parse(fhdr, sym, 0);
354 out_fromasm_x86(fout, sym, pp);