vararg funcs/calls, ebp- in ebp_frame, fixes
[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, char *sym, struct parsed_proto *pp)
27 {
28         int must_save = 0;
29         int sarg_ofs = 1; // stack offset to args, in DWORDs
30         int args_repushed = 0;
31         int argc_repush;
32         int i;
33
34         argc_repush = pp->argc;
35         if (pp->is_vararg)
36                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
37
38         for (i = 0; i < pp->argc; i++) {
39                 if (pp->arg[i].reg != NULL)
40                         must_save |= is_x86_reg_saved(pp->arg[i].reg);
41         }
42
43         fprintf(f, ".global _%s\n", sym);
44         fprintf(f, "_%s:\n", sym);
45
46         if (pp->argc_reg == 0 && !pp->is_stdcall) {
47                 fprintf(f, "\tjmp %s\n\n", sym);
48                 return;
49         }
50
51         if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
52              && !pp->is_vararg)
53         {
54                 // load arg regs
55                 for (i = 0; i < pp->argc; i++) {
56                         fprintf(f, "\tmovl %d(%%esp), %%%s\n",
57                                 (i + sarg_ofs) * 4, pp->arg[i].reg);
58                 }
59                 fprintf(f, "\tjmp %s\n\n", sym);
60                 return;
61         }
62
63         // save the regs
64         for (i = 0; i < pp->argc; i++) {
65                 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) {
66                         fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
67                         sarg_ofs++;
68                 }
69         }
70
71         // reconstruct arg stack
72         for (i = argc_repush - 1; i >= 0; i--) {
73                 if (pp->arg[i].reg == NULL) {
74                         fprintf(f, "\tmovl %d(%%esp), %%eax\n",
75                                 (i + sarg_ofs) * 4);
76                         fprintf(f, "\tpushl %%eax\n");
77                         sarg_ofs++;
78                         args_repushed++;
79                 }
80         }
81         // my_assert(args_repushed, pp->argc_stack);
82
83         // load arg regs
84         for (i = 0; i < pp->argc; i++) {
85                 if (pp->arg[i].reg != NULL) {
86                         fprintf(f, "\tmovl %d(%%esp), %%%s\n",
87                                 (i + sarg_ofs) * 4, pp->arg[i].reg);
88                 }
89         }
90
91         fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
92         fprintf(f, "\tcall %s\n\n", sym);
93
94         if (args_repushed && !pp->is_stdcall)
95                 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
96
97         // restore regs
98         for (i = pp->argc - 1; i >= 0; i--) {
99                 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg))
100                         fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
101         }
102
103         fprintf(f, "\tret\n\n");
104 }
105
106 static void out_fromasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
107 {
108         int sarg_ofs = 1; // stack offset to args, in DWORDs
109         int argc_repush;
110         int stack_args;
111         int i;
112
113         argc_repush = pp->argc;
114         stack_args = pp->argc_stack;
115         if (pp->is_vararg) {
116                 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
117                 stack_args = argc_repush - pp->argc_reg;
118         }
119
120         fprintf(f, "# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
121         fprintf(f, ".global %s\n", sym);
122         fprintf(f, "%s:\n", sym);
123
124         if (pp->argc_reg == 0 && !pp->is_stdcall) {
125                 fprintf(f, "\tjmp _%s\n\n", sym);
126                 return;
127         }
128
129         fprintf(f, "\tpushl %%edx\n"); // just in case..
130         sarg_ofs++;
131
132         // construct arg stack
133         for (i = argc_repush - 1; i >= 0; i--) {
134                 if (pp->arg[i].reg == NULL) {
135                         fprintf(f, "\tmovl %d(%%esp), %%edx\n",
136                                 (sarg_ofs + stack_args - 1) * 4);
137                         fprintf(f, "\tpushl %%edx\n");
138                         stack_args--;
139                 }
140                 else {
141                         if (IS(pp->arg[i].reg, "edx"))
142                                 // must reload original edx
143                                 fprintf(f, "\tmovl %d(%%esp), %%edx\n",
144                                         (sarg_ofs - 2) * 4);
145
146                         fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
147                 }
148                 sarg_ofs++;
149         }
150
151         // no worries about calling conventions - always __cdecl
152         fprintf(f, "\n\tcall _%s\n\n", sym);
153
154         if (sarg_ofs > 2)
155                 fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - 2) * 4);
156
157         fprintf(f, "\tpopl %%edx\n");
158
159         if (pp->is_stdcall && pp->argc_stack)
160                 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
161         else
162                 fprintf(f, "\tret\n\n");
163 }
164
165 int main(int argc, char *argv[])
166 {
167         FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
168         struct parsed_proto pp;
169         char line[256];
170         char sym[256];
171         int ret;
172
173         if (argc != 5) {
174                 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
175                         argv[0]);
176                 return 1;
177         }
178
179         hdrfn = argv[4];
180         fhdr = fopen(hdrfn, "r");
181         my_assert_not(fhdr, NULL);
182
183         fsyms_from = fopen(argv[3], "r");
184         my_assert_not(fsyms_from, NULL);
185
186         fsyms_to = fopen(argv[2], "r");
187         my_assert_not(fsyms_to, NULL);
188
189         fout = fopen(argv[1], "w");
190         my_assert_not(fout, NULL);
191
192         fprintf(fout, ".text\n\n");
193         fprintf(fout, "# to asm\n\n");
194
195         while (fgets(line, sizeof(line), fsyms_to))
196         {
197                 next_word(sym, sizeof(sym), line);
198                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
199                         continue;
200
201                 ret = proto_parse(fhdr, sym, &pp);
202                 if (ret)
203                         goto out;
204
205                 out_toasm_x86(fout, sym, &pp);
206                 proto_release(&pp);
207         }
208
209         fprintf(fout, "# from asm\n\n");
210
211         while (fgets(line, sizeof(line), fsyms_from))
212         {
213                 next_word(sym, sizeof(sym), line);
214                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
215                         continue;
216
217                 ret = proto_parse(fhdr, sym, &pp);
218                 if (ret)
219                         goto out;
220
221                 out_fromasm_x86(fout, sym, &pp);
222                 proto_release(&pp);
223         }
224
225         ret = 0;
226 out:
227         fclose(fout);
228         fclose(fsyms_to);
229         fclose(fsyms_from);
230         fclose(fhdr);
231         if (ret)
232                 remove(argv[1]);
233
234         return ret;
235 }