use unsigned types, cc+nested branch fix, var type cast
[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         // scratch reg, must not be eax or edx (for ret edx:eax)
130         fprintf(f, "\tpushl %%ebx\n");
131         sarg_ofs++;
132
133         // construct arg stack
134         for (i = argc_repush - 1; i >= 0; i--) {
135                 if (pp->arg[i].reg == NULL) {
136                         fprintf(f, "\tmovl %d(%%esp), %%ebx\n",
137                                 (sarg_ofs + stack_args - 1) * 4);
138                         fprintf(f, "\tpushl %%ebx\n");
139                         stack_args--;
140                 }
141                 else {
142                         if (IS(pp->arg[i].reg, "ebx"))
143                                 // must reload original ebx
144                                 fprintf(f, "\tmovl %d(%%esp), %%ebx\n",
145                                         (sarg_ofs - 2) * 4);
146
147                         fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
148                 }
149                 sarg_ofs++;
150         }
151
152         // no worries about calling conventions - always __cdecl
153         fprintf(f, "\n\tcall _%s\n\n", sym);
154
155         if (sarg_ofs > 2)
156                 fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - 2) * 4);
157
158         fprintf(f, "\tpopl %%ebx\n");
159
160         if (pp->is_stdcall && pp->argc_stack)
161                 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
162         else
163                 fprintf(f, "\tret\n\n");
164 }
165
166 int main(int argc, char *argv[])
167 {
168         FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
169         struct parsed_proto pp;
170         char line[256];
171         char sym[256];
172         int ret;
173
174         if (argc != 5) {
175                 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
176                         argv[0]);
177                 return 1;
178         }
179
180         hdrfn = argv[4];
181         fhdr = fopen(hdrfn, "r");
182         my_assert_not(fhdr, NULL);
183
184         fsyms_from = fopen(argv[3], "r");
185         my_assert_not(fsyms_from, NULL);
186
187         fsyms_to = fopen(argv[2], "r");
188         my_assert_not(fsyms_to, NULL);
189
190         fout = fopen(argv[1], "w");
191         my_assert_not(fout, NULL);
192
193         fprintf(fout, ".text\n\n");
194         fprintf(fout, "# to asm\n\n");
195
196         while (fgets(line, sizeof(line), fsyms_to))
197         {
198                 next_word(sym, sizeof(sym), line);
199                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
200                         continue;
201
202                 ret = proto_parse(fhdr, sym, &pp);
203                 if (ret)
204                         goto out;
205
206                 out_toasm_x86(fout, sym, &pp);
207                 proto_release(&pp);
208         }
209
210         fprintf(fout, "# from asm\n\n");
211
212         while (fgets(line, sizeof(line), fsyms_from))
213         {
214                 next_word(sym, sizeof(sym), line);
215                 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
216                         continue;
217
218                 ret = proto_parse(fhdr, sym, &pp);
219                 if (ret)
220                         goto out;
221
222                 out_fromasm_x86(fout, sym, &pp);
223                 proto_release(&pp);
224         }
225
226         ret = 0;
227 out:
228         fclose(fout);
229         fclose(fsyms_to);
230         fclose(fsyms_from);
231         fclose(fhdr);
232         if (ret)
233                 remove(argv[1]);
234
235         return ret;
236 }