mkbridge: use names from pp, decorate them
[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 int is_x86_reg_saved(const char *reg)
14 {
15         static const char *nosave_regs[] = { "eax", "edx", "ecx" };
16         int nosave = 0;
17         int r;
18
19         for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
20                 if (strcmp(reg, nosave_regs[r]) == 0)
21                         nosave = 1;
22
23         return !nosave;
24 }
25
26 // output decorated name
27 static const char *pp_to_name(const struct parsed_proto *pp)
28 {
29         static char buf[256];
30         char atval[16];
31
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);
35                 return buf;
36         }
37
38         atval[0] = 0;
39         if (pp->is_stdcall) {
40                 snprintf(atval, sizeof(atval), "@%d",
41                         pp->argc * 4);
42         }
43         snprintf(buf, sizeof(buf), "%s%s%s",
44                 pp->is_fastcall ? "@" : "_",
45                 pp->name, atval);
46
47         return buf;
48 }
49
50 static void out_toasm_x86(FILE *f, const char *sym_out,
51         const struct parsed_proto *pp)
52 {
53         int must_save = 0;
54         int sarg_ofs = 1; // stack offset to args, in DWORDs
55         int args_repushed = 0;
56         int argc_repush;
57         const char *name;
58         int i;
59
60         argc_repush = pp->argc;
61         if (pp->is_vararg)
62                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
63
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);
67         }
68
69         name = pp_to_name(pp);
70         fprintf(f, ".global %s\n", name);
71         fprintf(f, "%s:\n", name);
72
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);
78                 return;
79         }
80
81         if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
82              && !pp->is_vararg)
83         {
84                 // load arg regs
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);
88                 }
89                 fprintf(f, "\tjmp %s\n\n", sym_out);
90                 return;
91         }
92
93         // save the regs
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);
97                         sarg_ofs++;
98                 }
99         }
100
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",
105                                 (i + sarg_ofs) * 4);
106                         fprintf(f, "\tpushl %%eax\n");
107                         sarg_ofs++;
108                         args_repushed++;
109                 }
110         }
111         // my_assert(args_repushed, pp->argc_stack);
112
113         // load arg regs
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);
118                 }
119         }
120
121         fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
122         fprintf(f, "\tcall %s\n\n", sym_out);
123
124         if (args_repushed && !pp->is_stdcall)
125                 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
126
127         // restore regs
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);
131         }
132
133         fprintf(f, "\tret\n\n");
134 }
135
136 static void out_fromasm_x86(FILE *f, const char *sym,
137         const struct parsed_proto *pp)
138 {
139         int sarg_ofs = 1; // stack offset to args, in DWORDs
140         int saved_regs = 0;
141         int c_is_stdcall;
142         int argc_repush;
143         int stack_args;
144         int ret64;
145         int i;
146
147         argc_repush = pp->argc;
148         stack_args = pp->argc_stack;
149         if (pp->is_vararg) {
150                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
151                 stack_args = argc_repush - pp->argc_reg;
152         }
153
154         ret64 = strstr(pp->ret_type.name, "int64") != NULL;
155
156         fprintf(f, "# %s",
157           pp->is_fastcall ? "__fastcall" :
158           (pp->is_stdcall ? "__stdcall" : "__cdecl"));
159         if (ret64)
160                  fprintf(f, " ret64");
161         fprintf(f, "\n.global %s\n", sym);
162         fprintf(f, "%s:\n", sym);
163
164         if ((pp->argc_reg == 0 || pp->is_fastcall)
165             && !IS(pp->name, "storm_491")) // wants edx save :(
166         {
167                 fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
168                 return;
169         }
170
171         c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
172
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");
177         saved_regs++;
178         sarg_ofs++;
179         if (!ret64) {
180                 fprintf(f, "\tpushl %%edx\n");
181                 saved_regs++;
182                 sarg_ofs++;
183         }
184
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");
191                         stack_args--;
192                 }
193                 else {
194                         if (IS(pp->arg[i].reg, "ecx"))
195                                 // must reload original ecx
196                                 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
197                                         (sarg_ofs - 2) * 4);
198
199                         fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
200                 }
201                 sarg_ofs++;
202         }
203
204         fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
205
206         if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
207                 fprintf(f, "\tadd $%d,%%esp\n",
208                         (sarg_ofs - (saved_regs + 1)) * 4);
209
210         if (!ret64)
211                 fprintf(f, "\tpopl %%edx\n");
212         fprintf(f, "\tpopl %%ecx\n");
213
214         if (pp->is_stdcall && pp->argc_stack)
215                 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
216         else
217                 fprintf(f, "\tret\n\n");
218 }
219
220 int main(int argc, char *argv[])
221 {
222         FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
223         const struct parsed_proto *pp;
224         char line[256];
225         char sym_noat[256];
226         char sym[256];
227         char *p;
228         int ret = 1;
229
230         if (argc != 5) {
231                 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
232                         argv[0]);
233                 return 1;
234         }
235
236         hdrfn = argv[4];
237         fhdr = fopen(hdrfn, "r");
238         my_assert_not(fhdr, NULL);
239
240         fsyms_from = fopen(argv[3], "r");
241         my_assert_not(fsyms_from, NULL);
242
243         fsyms_to = fopen(argv[2], "r");
244         my_assert_not(fsyms_to, NULL);
245
246         fout = fopen(argv[1], "w");
247         my_assert_not(fout, NULL);
248
249         fprintf(fout, ".text\n\n");
250         fprintf(fout, "# to asm\n\n");
251
252         while (fgets(line, sizeof(line), fsyms_to))
253         {
254                 next_word(sym, sizeof(sym), line);
255                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
256                         continue;
257
258                 // IDA asm doesn't do '@' notation..
259                 strcpy(sym_noat, sym);
260                 p = strchr(sym_noat, '@');
261                 if (p != NULL)
262                         *p = 0;
263
264                 pp = proto_parse(fhdr, sym_noat, 0);
265                 if (pp == NULL)
266                         goto out;
267
268                 out_toasm_x86(fout, sym_noat, pp);
269         }
270
271         fprintf(fout, "# from asm\n\n");
272
273         while (fgets(line, sizeof(line), fsyms_from))
274         {
275                 next_word(sym, sizeof(sym), line);
276                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
277                         continue;
278
279                 pp = proto_parse(fhdr, sym, 0);
280                 if (pp == NULL)
281                         goto out;
282
283                 out_fromasm_x86(fout, sym, pp);
284         }
285
286         ret = 0;
287 out:
288         fclose(fout);
289         fclose(fsyms_to);
290         fclose(fsyms_from);
291         fclose(fhdr);
292         if (ret)
293                 remove(argv[1]);
294
295         return ret;
296 }