storm hacks
[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 static void out_toasm_x86(FILE *f, const char *sym_in,
27         const char *sym_out, const struct parsed_proto *pp)
28 {
29         int must_save = 0;
30         int sarg_ofs = 1; // stack offset to args, in DWORDs
31         int args_repushed = 0;
32         int argc_repush;
33         int i;
34
35         argc_repush = pp->argc;
36         if (pp->is_vararg)
37                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
38
39         for (i = 0; i < pp->argc; i++) {
40                 if (pp->arg[i].reg != NULL)
41                         must_save |= is_x86_reg_saved(pp->arg[i].reg);
42         }
43
44         fprintf(f, ".global %s%s\n", pp->is_fastcall ? "@" : "_", sym_in);
45         fprintf(f, "%s%s:\n", pp->is_fastcall ? "@" : "_", sym_in);
46
47         if (pp->argc_reg == 0 || pp->is_fastcall) {
48                 fprintf(f, "\t# %s\n",
49                   pp->is_fastcall ? "__fastcall" :
50                   (pp->is_stdcall ? "__stdcall" : "__cdecl"));
51                 fprintf(f, "\tjmp %s\n\n", sym_out);
52                 return;
53         }
54
55         if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
56              && !pp->is_vararg)
57         {
58                 // load arg regs
59                 for (i = 0; i < pp->argc; i++) {
60                         fprintf(f, "\tmovl %d(%%esp), %%%s\n",
61                                 (i + sarg_ofs) * 4, pp->arg[i].reg);
62                 }
63                 fprintf(f, "\tjmp %s\n\n", sym_out);
64                 return;
65         }
66
67         // save the regs
68         for (i = 0; i < pp->argc; i++) {
69                 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) {
70                         fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
71                         sarg_ofs++;
72                 }
73         }
74
75         // reconstruct arg stack
76         for (i = argc_repush - 1; i >= 0; i--) {
77                 if (pp->arg[i].reg == NULL) {
78                         fprintf(f, "\tmovl %d(%%esp), %%eax\n",
79                                 (i + sarg_ofs) * 4);
80                         fprintf(f, "\tpushl %%eax\n");
81                         sarg_ofs++;
82                         args_repushed++;
83                 }
84         }
85         // my_assert(args_repushed, pp->argc_stack);
86
87         // load arg regs
88         for (i = 0; i < pp->argc; i++) {
89                 if (pp->arg[i].reg != NULL) {
90                         fprintf(f, "\tmovl %d(%%esp), %%%s\n",
91                                 (i + sarg_ofs) * 4, pp->arg[i].reg);
92                 }
93         }
94
95         fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
96         fprintf(f, "\tcall %s\n\n", sym_out);
97
98         if (args_repushed && !pp->is_stdcall)
99                 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
100
101         // restore regs
102         for (i = pp->argc - 1; i >= 0; i--) {
103                 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg))
104                         fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
105         }
106
107         fprintf(f, "\tret\n\n");
108 }
109
110 static void out_fromasm_x86(FILE *f, const char *sym,
111         const struct parsed_proto *pp)
112 {
113         int sarg_ofs = 1; // stack offset to args, in DWORDs
114         int saved_regs = 0;
115         int c_is_stdcall;
116         int argc_repush;
117         int stack_args;
118         int ret64;
119         int i;
120
121         argc_repush = pp->argc;
122         stack_args = pp->argc_stack;
123         if (pp->is_vararg) {
124                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
125                 stack_args = argc_repush - pp->argc_reg;
126         }
127
128         ret64 = strstr(pp->ret_type.name, "int64") != NULL;
129
130         fprintf(f, "# %s",
131           pp->is_fastcall ? "__fastcall" :
132           (pp->is_stdcall ? "__stdcall" : "__cdecl"));
133         if (ret64)
134                  fprintf(f, " ret64");
135         fprintf(f, "\n.global %s\n", sym);
136         fprintf(f, "%s:\n", sym);
137
138         if ((pp->argc_reg == 0 || pp->is_fastcall)
139             && !IS(pp->name, "storm_491")) // wants edx save :(
140         {
141                 fprintf(f, "\tjmp %s%s",
142                         pp->is_fastcall ? "@" : "_", sym);
143                 if (pp->is_stdcall)
144                         fprintf(f, "@%d", pp->argc * 4);
145                 fprintf(f, "\n\n");
146                 return;
147         }
148
149         c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
150
151         // at least sc sub_47B150 needs edx to be preserved
152         // int64 returns use edx:eax - no edx save
153         // we use ecx also as scratch
154         fprintf(f, "\tpushl %%ecx\n");
155         saved_regs++;
156         sarg_ofs++;
157         if (!ret64) {
158                 fprintf(f, "\tpushl %%edx\n");
159                 saved_regs++;
160                 sarg_ofs++;
161         }
162
163         // construct arg stack
164         for (i = argc_repush - 1; i >= 0; i--) {
165                 if (pp->arg[i].reg == NULL) {
166                         fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
167                                 (sarg_ofs + stack_args - 1) * 4);
168                         fprintf(f, "\tpushl %%ecx\n");
169                         stack_args--;
170                 }
171                 else {
172                         if (IS(pp->arg[i].reg, "ecx"))
173                                 // must reload original ecx
174                                 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
175                                         (sarg_ofs - 2) * 4);
176
177                         fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
178                 }
179                 sarg_ofs++;
180         }
181
182         fprintf(f, "\n\tcall _%s", sym);
183         if (c_is_stdcall)
184                 fprintf(f, "@%d", pp->argc_stack * 4);
185         fprintf(f, "\n\n");
186
187         if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
188                 fprintf(f, "\tadd $%d,%%esp\n",
189                         (sarg_ofs - (saved_regs + 1)) * 4);
190
191         if (!ret64)
192                 fprintf(f, "\tpopl %%edx\n");
193         fprintf(f, "\tpopl %%ecx\n");
194
195         if (pp->is_stdcall && pp->argc_stack)
196                 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
197         else
198                 fprintf(f, "\tret\n\n");
199 }
200
201 int main(int argc, char *argv[])
202 {
203         FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
204         const struct parsed_proto *pp;
205         char line[256];
206         char sym_noat[256];
207         char sym[256];
208         char *p;
209         int ret = 1;
210
211         if (argc != 5) {
212                 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
213                         argv[0]);
214                 return 1;
215         }
216
217         hdrfn = argv[4];
218         fhdr = fopen(hdrfn, "r");
219         my_assert_not(fhdr, NULL);
220
221         fsyms_from = fopen(argv[3], "r");
222         my_assert_not(fsyms_from, NULL);
223
224         fsyms_to = fopen(argv[2], "r");
225         my_assert_not(fsyms_to, NULL);
226
227         fout = fopen(argv[1], "w");
228         my_assert_not(fout, NULL);
229
230         fprintf(fout, ".text\n\n");
231         fprintf(fout, "# to asm\n\n");
232
233         while (fgets(line, sizeof(line), fsyms_to))
234         {
235                 next_word(sym, sizeof(sym), line);
236                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
237                         continue;
238
239                 // IDA asm doesn't do '@' notation..
240                 strcpy(sym_noat, sym);
241                 p = strchr(sym_noat, '@');
242                 if (p != NULL)
243                         *p = 0;
244
245                 pp = proto_parse(fhdr, sym_noat, 0);
246                 if (pp == NULL)
247                         goto out;
248
249                 out_toasm_x86(fout, sym, sym_noat, pp);
250         }
251
252         fprintf(fout, "# from asm\n\n");
253
254         while (fgets(line, sizeof(line), fsyms_from))
255         {
256                 next_word(sym, sizeof(sym), line);
257                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
258                         continue;
259
260                 pp = proto_parse(fhdr, sym, 0);
261                 if (pp == NULL)
262                         goto out;
263
264                 out_fromasm_x86(fout, sym, pp);
265         }
266
267         ret = 0;
268 out:
269         fclose(fout);
270         fclose(fsyms_to);
271         fclose(fsyms_from);
272         fclose(fhdr);
273         if (ret)
274                 remove(argv[1]);
275
276         return ret;
277 }