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