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 // output decorated name
27 static const char *pp_to_name(const struct parsed_proto *pp)
32 if (!pp->is_fastcall && pp->argc_reg != 0) {
33 // can only be handled by __cdecl C func
34 snprintf(buf, sizeof(buf), "_%s", pp->name);
40 snprintf(atval, sizeof(atval), "@%d",
43 snprintf(buf, sizeof(buf), "%s%s%s",
44 pp->is_fastcall ? "@" : "_",
50 static void out_toasm_x86(FILE *f, const char *sym_out,
51 const struct parsed_proto *pp)
54 int sarg_ofs = 1; // stack offset to args, in DWORDs
55 int args_repushed = 0;
60 argc_repush = pp->argc;
62 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
64 for (i = 0; i < pp->argc; i++) {
65 if (pp->arg[i].reg != NULL)
66 must_save |= is_x86_reg_saved(pp->arg[i].reg);
69 name = pp_to_name(pp);
70 fprintf(f, ".global %s\n", name);
71 fprintf(f, "%s:\n", name);
73 if (pp->argc_reg == 0 || pp->is_fastcall) {
74 fprintf(f, "\t# %s\n",
75 pp->is_fastcall ? "__fastcall" :
76 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
77 fprintf(f, "\tjmp %s\n\n", sym_out);
81 if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
85 for (i = 0; i < pp->argc; i++) {
86 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
87 (i + sarg_ofs) * 4, pp->arg[i].reg);
89 fprintf(f, "\tjmp %s\n\n", sym_out);
94 for (i = 0; i < pp->argc; i++) {
95 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) {
96 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
101 // reconstruct arg stack
102 for (i = argc_repush - 1; i >= 0; i--) {
103 if (pp->arg[i].reg == NULL) {
104 fprintf(f, "\tmovl %d(%%esp), %%eax\n",
106 fprintf(f, "\tpushl %%eax\n");
111 // my_assert(args_repushed, pp->argc_stack);
114 for (i = 0; i < pp->argc; i++) {
115 if (pp->arg[i].reg != NULL) {
116 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
117 (i + sarg_ofs) * 4, pp->arg[i].reg);
121 fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
122 fprintf(f, "\tcall %s\n\n", sym_out);
124 if (args_repushed && !pp->is_stdcall)
125 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
128 for (i = pp->argc - 1; i >= 0; i--) {
129 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg))
130 fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
133 fprintf(f, "\tret\n\n");
136 static void out_fromasm_x86(FILE *f, const char *sym,
137 const struct parsed_proto *pp)
139 int sarg_ofs = 1; // stack offset to args, in DWORDs
147 argc_repush = pp->argc;
148 stack_args = pp->argc_stack;
150 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
151 stack_args = argc_repush - pp->argc_reg;
154 ret64 = strstr(pp->ret_type.name, "int64") != NULL;
157 pp->is_fastcall ? "__fastcall" :
158 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
160 fprintf(f, " ret64");
161 fprintf(f, "\n.global %s\n", sym);
162 fprintf(f, "%s:\n", sym);
164 if ((pp->argc_reg == 0 || pp->is_fastcall)
165 && !IS(pp->name, "storm_491")) // wants edx save :(
167 fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
171 c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
173 // at least sc sub_47B150 needs edx to be preserved
174 // int64 returns use edx:eax - no edx save
175 // we use ecx also as scratch
176 fprintf(f, "\tpushl %%ecx\n");
180 fprintf(f, "\tpushl %%edx\n");
185 // construct arg stack
186 for (i = argc_repush - 1; i >= 0; i--) {
187 if (pp->arg[i].reg == NULL) {
188 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
189 (sarg_ofs + stack_args - 1) * 4);
190 fprintf(f, "\tpushl %%ecx\n");
194 if (IS(pp->arg[i].reg, "ecx"))
195 // must reload original ecx
196 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
199 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
204 fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
206 if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
207 fprintf(f, "\tadd $%d,%%esp\n",
208 (sarg_ofs - (saved_regs + 1)) * 4);
211 fprintf(f, "\tpopl %%edx\n");
212 fprintf(f, "\tpopl %%ecx\n");
214 if (pp->is_stdcall && pp->argc_stack)
215 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
217 fprintf(f, "\tret\n\n");
220 int main(int argc, char *argv[])
222 FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
223 const struct parsed_proto *pp;
231 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
237 fhdr = fopen(hdrfn, "r");
238 my_assert_not(fhdr, NULL);
240 fsyms_from = fopen(argv[3], "r");
241 my_assert_not(fsyms_from, NULL);
243 fsyms_to = fopen(argv[2], "r");
244 my_assert_not(fsyms_to, NULL);
246 fout = fopen(argv[1], "w");
247 my_assert_not(fout, NULL);
249 fprintf(fout, ".text\n\n");
250 fprintf(fout, "# to asm\n\n");
252 while (fgets(line, sizeof(line), fsyms_to))
254 next_word(sym, sizeof(sym), line);
255 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
258 // IDA asm doesn't do '@' notation..
259 strcpy(sym_noat, sym);
260 p = strchr(sym_noat, '@');
264 pp = proto_parse(fhdr, sym_noat, 0);
268 out_toasm_x86(fout, sym_noat, pp);
271 fprintf(fout, "# from asm\n\n");
273 while (fgets(line, sizeof(line), fsyms_from))
275 next_word(sym, sizeof(sym), line);
276 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
279 pp = proto_parse(fhdr, sym, 0);
283 out_fromasm_x86(fout, sym, pp);