reg return, __userstack, repe fix, more..
[ia32rtools.git] / tools / mkbridge.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "my_assert.h"
6 #include "my_str.h"
7
8 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
9 #define IS(w, y) !strcmp(w, y)
10
11 #include "protoparse.h"
12
13 static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" };
14
15 static int is_x86_reg_saved(const char *reg)
16 {
17         static const char *nosave_regs[] = { "eax", "edx", "ecx" };
18         int nosave = 0;
19         int r;
20
21         for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
22                 if (strcmp(reg, nosave_regs[r]) == 0)
23                         nosave = 1;
24
25         return !nosave;
26 }
27
28 // output decorated name
29 static const char *pp_to_name(const struct parsed_proto *pp)
30 {
31         static char buf[256];
32         char atval[16];
33
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);
37                 return buf;
38         }
39
40         atval[0] = 0;
41         if (pp->is_stdcall) {
42                 snprintf(atval, sizeof(atval), "@%d",
43                         pp->argc * 4);
44         }
45         snprintf(buf, sizeof(buf), "%s%s%s",
46                 pp->is_fastcall ? "@" : "_",
47                 pp->name, atval);
48
49         return buf;
50 }
51
52 static void out_toasm_x86(FILE *f, const char *sym_out,
53         const struct parsed_proto *pp)
54 {
55         int must_save = 0;
56         int sarg_ofs = 1; // stack offset to args, in DWORDs
57         int args_repushed = 0;
58         int argc_repush;
59         const char *name;
60         int i;
61
62         argc_repush = pp->argc;
63         if (pp->is_vararg)
64                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
65
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);
69         }
70
71         name = pp_to_name(pp);
72         fprintf(f, ".global %s\n", name);
73         fprintf(f, "%s:\n", name);
74
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);
80                 return;
81         }
82
83         if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
84              && !pp->is_vararg && !pp->has_retreg)
85         {
86                 // load arg regs
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);
90                 }
91                 fprintf(f, "\tjmp %s\n\n", sym_out);
92                 return;
93         }
94
95         // asm_stack_args | saved_regs | ra | args_from_c
96
97         // save the regs
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]);
102                 sarg_ofs++;
103         }
104
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",
109                                 (i + sarg_ofs) * 4);
110                         fprintf(f, "\tpushl %%eax\n");
111                         sarg_ofs++;
112                         args_repushed++;
113                 }
114         }
115
116         // load arg regs
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);
124                 }
125         }
126
127         fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
128         fprintf(f, "\tcall %s\n\n", sym_out);
129
130         if (args_repushed && !pp->is_stdcall) {
131                 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
132                 sarg_ofs -= args_repushed;
133         }
134
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);
142                         }
143                 }
144         }
145
146         // restore regs
147         for (i = ARRAY_SIZE(c_save_regs) - 1; i >= 0; i--)
148                 fprintf(f, "\tpopl %%%s\n", c_save_regs[i]);
149
150         fprintf(f, "\tret\n\n");
151 }
152
153 static void out_fromasm_x86(FILE *f, const char *sym,
154         const struct parsed_proto *pp)
155 {
156         int reg_ofs[ARRAY_SIZE(pp->arg)];
157         int sarg_ofs = 1; // stack offset to args, in DWORDs
158         int saved_regs = 0;
159         int ecx_ofs = -1;
160         int edx_ofs = -1;
161         int c_is_stdcall;
162         int argc_repush;
163         int stack_args;
164         int ret64;
165         int i;
166
167         argc_repush = pp->argc;
168         stack_args = pp->argc_stack;
169         if (pp->is_vararg) {
170                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
171                 stack_args = argc_repush - pp->argc_reg;
172         }
173
174         ret64 = strstr(pp->ret_type.name, "int64") != NULL;
175
176         fprintf(f, "# %s",
177           pp->is_fastcall ? "__fastcall" :
178           (pp->is_stdcall ? "__stdcall" : "__cdecl"));
179         if (ret64)
180                  fprintf(f, " ret64");
181         fprintf(f, "\n.global %s\n", sym);
182         fprintf(f, "%s:\n", sym);
183
184         if ((pp->argc_reg == 0 || pp->is_fastcall)
185             && !IS(pp->name, "storm_491")) // wants edx save :(
186         {
187                 fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
188                 return;
189         }
190
191         c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
192
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");
197         saved_regs++;
198         sarg_ofs++;
199         ecx_ofs = sarg_ofs;
200         if (!ret64) {
201                 fprintf(f, "\tpushl %%edx\n");
202                 saved_regs++;
203                 sarg_ofs++;
204                 edx_ofs = sarg_ofs;
205         }
206
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)
211                                 continue;
212                         if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
213                                 reg_ofs[i] = ecx_ofs;
214                                 continue;
215                         }
216                         if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
217                                 reg_ofs[i] = edx_ofs;
218                                 continue;
219                         }
220                         fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
221                         saved_regs++;
222                         sarg_ofs++;
223                         reg_ofs[i] = sarg_ofs;
224                 }
225         }
226
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");
233                         stack_args--;
234                 }
235                 else {
236                         const char *reg = pp->arg[i].reg;
237                         if (pp->arg[i].type.is_retreg) {
238                                 reg = "ecx";
239                                 fprintf(f, "\tlea %d(%%esp), %%ecx\n",
240                                   (sarg_ofs - reg_ofs[i]) * 4);
241                         }
242                         else if (IS(reg, "ecx"))
243                                 // must reload original ecx
244                                 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
245                                         (sarg_ofs - 2) * 4);
246
247                         fprintf(f, "\tpushl %%%s\n", reg);
248                 }
249                 sarg_ofs++;
250         }
251
252         fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
253
254         if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
255                 fprintf(f, "\tadd $%d,%%esp\n",
256                         (sarg_ofs - (saved_regs + 1)) * 4);
257
258         // pop retregs
259         if (pp->has_retreg) {
260                 for (i = pp->argc - 1; i >= 0; i--) {
261                         if (!pp->arg[i].type.is_retreg)
262                                 continue;
263                         if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
264                                 continue;
265                         }
266                         if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
267                                 continue;
268                         }
269                         fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
270                 }
271         }
272
273         if (!ret64)
274                 fprintf(f, "\tpopl %%edx\n");
275         fprintf(f, "\tpopl %%ecx\n");
276
277         if (pp->is_stdcall && pp->argc_stack)
278                 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
279         else
280                 fprintf(f, "\tret\n\n");
281 }
282
283 int main(int argc, char *argv[])
284 {
285         FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
286         const struct parsed_proto *pp;
287         char line[256];
288         char sym_noat[256];
289         char sym[256];
290         char *p;
291         int ret = 1;
292
293         if (argc != 5) {
294                 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
295                         argv[0]);
296                 return 1;
297         }
298
299         hdrfn = argv[4];
300         fhdr = fopen(hdrfn, "r");
301         my_assert_not(fhdr, NULL);
302
303         fsyms_from = fopen(argv[3], "r");
304         my_assert_not(fsyms_from, NULL);
305
306         fsyms_to = fopen(argv[2], "r");
307         my_assert_not(fsyms_to, NULL);
308
309         fout = fopen(argv[1], "w");
310         my_assert_not(fout, NULL);
311
312         fprintf(fout, ".text\n\n");
313         fprintf(fout, "# to asm\n\n");
314
315         while (fgets(line, sizeof(line), fsyms_to))
316         {
317                 next_word(sym, sizeof(sym), line);
318                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
319                         continue;
320
321                 // IDA asm doesn't do '@' notation..
322                 strcpy(sym_noat, sym);
323                 p = strchr(sym_noat, '@');
324                 if (p != NULL)
325                         *p = 0;
326
327                 pp = proto_parse(fhdr, sym_noat, 0);
328                 if (pp == NULL)
329                         goto out;
330
331                 out_toasm_x86(fout, sym_noat, pp);
332         }
333
334         fprintf(fout, "# from asm\n\n");
335
336         while (fgets(line, sizeof(line), fsyms_from))
337         {
338                 next_word(sym, sizeof(sym), line);
339                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
340                         continue;
341
342                 pp = proto_parse(fhdr, sym, 0);
343                 if (pp == NULL)
344                         goto out;
345
346                 out_fromasm_x86(fout, sym, pp);
347         }
348
349         ret = 0;
350 out:
351         fclose(fout);
352         fclose(fsyms_to);
353         fclose(fsyms_from);
354         fclose(fhdr);
355         if (ret)
356                 remove(argv[1]);
357
358         return ret;
359 }