get rid of some duplication
[ia32rtools.git] / tools / mkbridge.c
1 /*
2  * ia32rtools
3  * (C) notaz, 2013,2014
4  *
5  * This work is licensed under the terms of 3-clause BSD license.
6  * See COPYING file in the top-level directory.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "my_assert.h"
14 #include "my_str.h"
15 #include "common.h"
16
17 #include "protoparse.h"
18
19 static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" };
20
21 static int is_x86_reg_saved(const char *reg)
22 {
23         static const char *nosave_regs[] = { "eax", "edx", "ecx" };
24         int nosave = 0;
25         int r;
26
27         for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
28                 if (strcmp(reg, nosave_regs[r]) == 0)
29                         nosave = 1;
30
31         return !nosave;
32 }
33
34 // output decorated name
35 static const char *pp_to_name(const struct parsed_proto *pp)
36 {
37         static char buf[256];
38         char atval[16];
39
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);
43                 return buf;
44         }
45
46         atval[0] = 0;
47         if (pp->is_stdcall) {
48                 snprintf(atval, sizeof(atval), "@%d",
49                         pp->argc * 4);
50         }
51         snprintf(buf, sizeof(buf), "%s%s%s",
52                 pp->is_fastcall ? "@" : "_",
53                 pp->name, atval);
54
55         return buf;
56 }
57
58 static void out_toasm_x86(FILE *f, const char *sym_out,
59         const struct parsed_proto *pp)
60 {
61         int must_save = 0;
62         int sarg_ofs = 1; // stack offset to args, in DWORDs
63         int args_repushed = 0;
64         int argc_repush;
65         const char *name;
66         int i;
67
68         argc_repush = pp->argc;
69         if (pp->is_vararg)
70                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
71
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);
75         }
76
77         name = pp_to_name(pp);
78         fprintf(f, ".global %s\n", name);
79         fprintf(f, "%s:\n", name);
80
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);
86                 return;
87         }
88
89         if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
90              && !pp->is_vararg && !pp->has_retreg)
91         {
92                 // load arg regs
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);
96                 }
97                 fprintf(f, "\tjmp %s\n\n", sym_out);
98                 return;
99         }
100
101         // asm_stack_args | saved_regs | ra | args_from_c
102
103         // save the regs
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]);
108                 sarg_ofs++;
109         }
110
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",
115                                 (i + sarg_ofs) * 4);
116                         fprintf(f, "\tpushl %%eax\n");
117                         sarg_ofs++;
118                         args_repushed++;
119                 }
120         }
121
122         // load arg regs
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);
130                 }
131         }
132
133         fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
134         fprintf(f, "\tcall %s\n\n", sym_out);
135
136         if (args_repushed && !pp->is_stdcall) {
137                 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
138                 sarg_ofs -= args_repushed;
139         }
140
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);
148                         }
149                 }
150         }
151
152         // restore regs
153         for (i = ARRAY_SIZE(c_save_regs) - 1; i >= 0; i--)
154                 fprintf(f, "\tpopl %%%s\n", c_save_regs[i]);
155
156         fprintf(f, "\tret\n\n");
157 }
158
159 static void out_fromasm_x86(FILE *f, const char *sym,
160         const struct parsed_proto *pp)
161 {
162         int reg_ofs[ARRAY_SIZE(pp->arg)];
163         int sarg_ofs = 1; // stack offset to args, in DWORDs
164         int saved_regs = 0;
165         int ecx_ofs = -1;
166         int edx_ofs = -1;
167         int c_is_stdcall;
168         int argc_repush;
169         int stack_args;
170         int ret64;
171         int i;
172
173         argc_repush = pp->argc;
174         stack_args = pp->argc_stack;
175         if (pp->is_vararg) {
176                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
177                 stack_args = argc_repush - pp->argc_reg;
178         }
179
180         ret64 = strstr(pp->ret_type.name, "int64") != NULL;
181
182         fprintf(f, "# %s",
183           pp->is_fastcall ? "__fastcall" :
184           (pp->is_stdcall ? "__stdcall" : "__cdecl"));
185         if (ret64)
186                  fprintf(f, " ret64");
187         fprintf(f, "\n.global %s\n", sym);
188         fprintf(f, "%s:\n", sym);
189
190         if ((pp->argc_reg == 0 || pp->is_fastcall)
191             && !IS(pp->name, "storm_491")) // wants edx save :(
192         {
193                 fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
194                 return;
195         }
196
197         c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
198
199         // at least sc sub_47B150 needs edx to be preserved
200         // int64 returns use edx:eax - no edx save
201         // we use ecx also as scratch
202         fprintf(f, "\tpushl %%ecx\n");
203         saved_regs++;
204         sarg_ofs++;
205         ecx_ofs = sarg_ofs;
206         if (!ret64) {
207                 fprintf(f, "\tpushl %%edx\n");
208                 saved_regs++;
209                 sarg_ofs++;
210                 edx_ofs = sarg_ofs;
211         }
212
213         // need space for retreg args
214         if (pp->has_retreg) {
215                 for (i = 0; i < pp->argc; i++) {
216                         if (!pp->arg[i].type.is_retreg)
217                                 continue;
218                         if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
219                                 reg_ofs[i] = ecx_ofs;
220                                 continue;
221                         }
222                         if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
223                                 reg_ofs[i] = edx_ofs;
224                                 continue;
225                         }
226                         fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
227                         saved_regs++;
228                         sarg_ofs++;
229                         reg_ofs[i] = sarg_ofs;
230                 }
231         }
232
233         // construct arg stack
234         for (i = argc_repush - 1; i >= 0; i--) {
235                 if (pp->arg[i].reg == NULL) {
236                         fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
237                                 (sarg_ofs + stack_args - 1) * 4);
238                         fprintf(f, "\tpushl %%ecx\n");
239                         stack_args--;
240                 }
241                 else {
242                         const char *reg = pp->arg[i].reg;
243                         if (pp->arg[i].type.is_retreg) {
244                                 reg = "ecx";
245                                 fprintf(f, "\tlea %d(%%esp), %%ecx\n",
246                                   (sarg_ofs - reg_ofs[i]) * 4);
247                         }
248                         else if (IS(reg, "ecx"))
249                                 // must reload original ecx
250                                 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
251                                         (sarg_ofs - 2) * 4);
252
253                         fprintf(f, "\tpushl %%%s\n", reg);
254                 }
255                 sarg_ofs++;
256         }
257
258         fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
259
260         if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
261                 fprintf(f, "\tadd $%d,%%esp\n",
262                         (sarg_ofs - (saved_regs + 1)) * 4);
263
264         // pop retregs
265         if (pp->has_retreg) {
266                 for (i = pp->argc - 1; i >= 0; i--) {
267                         if (!pp->arg[i].type.is_retreg)
268                                 continue;
269                         if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
270                                 continue;
271                         }
272                         if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
273                                 continue;
274                         }
275                         fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
276                 }
277         }
278
279         if (!ret64)
280                 fprintf(f, "\tpopl %%edx\n");
281         fprintf(f, "\tpopl %%ecx\n");
282
283         if (pp->is_stdcall && pp->argc_stack)
284                 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
285         else
286                 fprintf(f, "\tret\n\n");
287 }
288
289 int main(int argc, char *argv[])
290 {
291         FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
292         const struct parsed_proto *pp;
293         char line[256];
294         char sym_noat[256];
295         char sym[256];
296         char *p;
297         int ret = 1;
298
299         if (argc != 5) {
300                 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
301                         argv[0]);
302                 return 1;
303         }
304
305         hdrfn = argv[4];
306         fhdr = fopen(hdrfn, "r");
307         my_assert_not(fhdr, NULL);
308
309         fsyms_from = fopen(argv[3], "r");
310         my_assert_not(fsyms_from, NULL);
311
312         fsyms_to = fopen(argv[2], "r");
313         my_assert_not(fsyms_to, NULL);
314
315         fout = fopen(argv[1], "w");
316         my_assert_not(fout, NULL);
317
318         fprintf(fout, ".text\n\n");
319         fprintf(fout, "# to asm\n\n");
320
321         while (fgets(line, sizeof(line), fsyms_to))
322         {
323                 next_word(sym, sizeof(sym), line);
324                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
325                         continue;
326
327                 // IDA asm doesn't do '@' notation..
328                 strcpy(sym_noat, sym);
329                 p = strchr(sym_noat, '@');
330                 if (p != NULL)
331                         *p = 0;
332
333                 pp = proto_parse(fhdr, sym_noat, 0);
334                 if (pp == NULL)
335                         goto out;
336
337                 out_toasm_x86(fout, sym_noat, pp);
338         }
339
340         fprintf(fout, "# from asm\n\n");
341
342         while (fgets(line, sizeof(line), fsyms_from))
343         {
344                 next_word(sym, sizeof(sym), line);
345                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
346                         continue;
347
348                 pp = proto_parse(fhdr, sym, 0);
349                 if (pp == NULL)
350                         goto out;
351
352                 out_fromasm_x86(fout, sym, pp);
353         }
354
355         ret = 0;
356 out:
357         fclose(fout);
358         fclose(fsyms_to);
359         fclose(fsyms_from);
360         fclose(fhdr);
361         if (ret)
362                 remove(argv[1]);
363
364         return ret;
365 }