minor makefile update
[ia32rtools.git] / tools / mkbridge.c
CommitLineData
57e4efe9 1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "my_assert.h"
6#include "my_str.h"
7
232aca37 8#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
3e52f54c 9#define IS(w, y) !strcmp(w, y)
232aca37 10
c36e914d 11#include "protoparse.h"
232aca37 12
1f84f6b3 13static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" };
14
232aca37 15static int is_x86_reg_saved(const char *reg)
16{
17 static const char *nosave_regs[] = { "eax", "edx", "ecx" };
18 int nosave = 0;
19 int r;
20
21 for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
22 if (strcmp(reg, nosave_regs[r]) == 0)
23 nosave = 1;
24
25 return !nosave;
26}
27
bd2eb956 28// output decorated name
29static const char *pp_to_name(const struct parsed_proto *pp)
30{
31 static char buf[256];
32 char atval[16];
33
34 if (!pp->is_fastcall && pp->argc_reg != 0) {
35 // can only be handled by __cdecl C func
36 snprintf(buf, sizeof(buf), "_%s", pp->name);
37 return buf;
38 }
39
40 atval[0] = 0;
41 if (pp->is_stdcall) {
42 snprintf(atval, sizeof(atval), "@%d",
43 pp->argc * 4);
44 }
45 snprintf(buf, sizeof(buf), "%s%s%s",
46 pp->is_fastcall ? "@" : "_",
47 pp->name, atval);
48
49 return buf;
50}
51
52static void out_toasm_x86(FILE *f, const char *sym_out,
53 const struct parsed_proto *pp)
232aca37 54{
232aca37 55 int must_save = 0;
56 int sarg_ofs = 1; // stack offset to args, in DWORDs
57 int args_repushed = 0;
4f12f671 58 int argc_repush;
bd2eb956 59 const char *name;
232aca37 60 int i;
61
4f12f671 62 argc_repush = pp->argc;
63 if (pp->is_vararg)
64 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
65
c36e914d 66 for (i = 0; i < pp->argc; i++) {
67 if (pp->arg[i].reg != NULL)
68 must_save |= is_x86_reg_saved(pp->arg[i].reg);
232aca37 69 }
70
bd2eb956 71 name = pp_to_name(pp);
72 fprintf(f, ".global %s\n", name);
73 fprintf(f, "%s:\n", name);
232aca37 74
c0050df6 75 if (pp->argc_reg == 0 || pp->is_fastcall) {
76 fprintf(f, "\t# %s\n",
77 pp->is_fastcall ? "__fastcall" :
78 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
54e763a1 79 fprintf(f, "\tjmp %s\n\n", sym_out);
232aca37 80 return;
81 }
82
4f12f671 83 if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
1f84f6b3 84 && !pp->is_vararg && !pp->has_retreg)
4f12f671 85 {
232aca37 86 // load arg regs
c36e914d 87 for (i = 0; i < pp->argc; i++) {
232aca37 88 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
c36e914d 89 (i + sarg_ofs) * 4, pp->arg[i].reg);
232aca37 90 }
54e763a1 91 fprintf(f, "\tjmp %s\n\n", sym_out);
232aca37 92 return;
93 }
94
1f84f6b3 95 // asm_stack_args | saved_regs | ra | args_from_c
96
232aca37 97 // save the regs
1f84f6b3 98 // because we don't always know what we are calling,
99 // be safe and save everything that has to be saved in __cdecl
100 for (i = 0; i < ARRAY_SIZE(c_save_regs); i++) {
101 fprintf(f, "\tpushl %%%s\n", c_save_regs[i]);
102 sarg_ofs++;
232aca37 103 }
104
1f84f6b3 105 // reconstruct arg stack for asm
4f12f671 106 for (i = argc_repush - 1; i >= 0; i--) {
c36e914d 107 if (pp->arg[i].reg == NULL) {
232aca37 108 fprintf(f, "\tmovl %d(%%esp), %%eax\n",
109 (i + sarg_ofs) * 4);
110 fprintf(f, "\tpushl %%eax\n");
111 sarg_ofs++;
112 args_repushed++;
113 }
114 }
232aca37 115
116 // load arg regs
c36e914d 117 for (i = 0; i < pp->argc; i++) {
118 if (pp->arg[i].reg != NULL) {
232aca37 119 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
c36e914d 120 (i + sarg_ofs) * 4, pp->arg[i].reg);
1f84f6b3 121 if (pp->arg[i].type.is_retreg)
122 fprintf(f, "\tmovl (%%%s), %%%s\n",
123 pp->arg[i].reg, pp->arg[i].reg);
232aca37 124 }
125 }
126
c36e914d 127 fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
54e763a1 128 fprintf(f, "\tcall %s\n\n", sym_out);
232aca37 129
1f84f6b3 130 if (args_repushed && !pp->is_stdcall) {
a51421fa 131 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
1f84f6b3 132 sarg_ofs -= args_repushed;
133 }
232aca37 134
1f84f6b3 135 // update the retreg regs
136 if (pp->has_retreg) {
137 for (i = 0; i < pp->argc; i++) {
138 if (pp->arg[i].type.is_retreg) {
139 fprintf(f, "\tmovl %d(%%esp), %%ecx\n"
140 "\tmovl %%%s, (%%ecx)\n",
141 (i + sarg_ofs) * 4, pp->arg[i].reg);
142 }
143 }
232aca37 144 }
145
1f84f6b3 146 // restore regs
147 for (i = ARRAY_SIZE(c_save_regs) - 1; i >= 0; i--)
148 fprintf(f, "\tpopl %%%s\n", c_save_regs[i]);
149
232aca37 150 fprintf(f, "\tret\n\n");
151}
152
54e763a1 153static void out_fromasm_x86(FILE *f, const char *sym,
154 const struct parsed_proto *pp)
232aca37 155{
1f84f6b3 156 int reg_ofs[ARRAY_SIZE(pp->arg)];
232aca37 157 int sarg_ofs = 1; // stack offset to args, in DWORDs
7ae48d73 158 int saved_regs = 0;
1f84f6b3 159 int ecx_ofs = -1;
160 int edx_ofs = -1;
f2de0a97 161 int c_is_stdcall;
4f12f671 162 int argc_repush;
232aca37 163 int stack_args;
7ae48d73 164 int ret64;
232aca37 165 int i;
166
4f12f671 167 argc_repush = pp->argc;
168 stack_args = pp->argc_stack;
169 if (pp->is_vararg) {
170 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
171 stack_args = argc_repush - pp->argc_reg;
172 }
173
7ae48d73 174 ret64 = strstr(pp->ret_type.name, "int64") != NULL;
175
c0050df6 176 fprintf(f, "# %s",
177 pp->is_fastcall ? "__fastcall" :
178 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
7ae48d73 179 if (ret64)
180 fprintf(f, " ret64");
181 fprintf(f, "\n.global %s\n", sym);
232aca37 182 fprintf(f, "%s:\n", sym);
183
f2de0a97 184 if ((pp->argc_reg == 0 || pp->is_fastcall)
185 && !IS(pp->name, "storm_491")) // wants edx save :(
186 {
bd2eb956 187 fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
232aca37 188 return;
189 }
190
f2de0a97 191 c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
192
7ae48d73 193 // at least sc sub_47B150 needs edx to be preserved
194 // int64 returns use edx:eax - no edx save
195 // we use ecx also as scratch
196 fprintf(f, "\tpushl %%ecx\n");
197 saved_regs++;
232aca37 198 sarg_ofs++;
1f84f6b3 199 ecx_ofs = sarg_ofs;
7ae48d73 200 if (!ret64) {
201 fprintf(f, "\tpushl %%edx\n");
202 saved_regs++;
203 sarg_ofs++;
1f84f6b3 204 edx_ofs = sarg_ofs;
205 }
206
207 // need space for retreg args
208 if (pp->has_retreg) {
209 for (i = 0; i < pp->argc; i++) {
210 if (!pp->arg[i].type.is_retreg)
211 continue;
212 if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
213 reg_ofs[i] = ecx_ofs;
214 continue;
215 }
216 if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
217 reg_ofs[i] = edx_ofs;
218 continue;
219 }
220 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
221 saved_regs++;
222 sarg_ofs++;
223 reg_ofs[i] = sarg_ofs;
224 }
7ae48d73 225 }
232aca37 226
227 // construct arg stack
4f12f671 228 for (i = argc_repush - 1; i >= 0; i--) {
c36e914d 229 if (pp->arg[i].reg == NULL) {
7ae48d73 230 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
232aca37 231 (sarg_ofs + stack_args - 1) * 4);
7ae48d73 232 fprintf(f, "\tpushl %%ecx\n");
232aca37 233 stack_args--;
234 }
235 else {
1f84f6b3 236 const char *reg = pp->arg[i].reg;
237 if (pp->arg[i].type.is_retreg) {
238 reg = "ecx";
239 fprintf(f, "\tlea %d(%%esp), %%ecx\n",
240 (sarg_ofs - reg_ofs[i]) * 4);
241 }
242 else if (IS(reg, "ecx"))
7ae48d73 243 // must reload original ecx
244 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
f3d05b09 245 (sarg_ofs - 2) * 4);
246
1f84f6b3 247 fprintf(f, "\tpushl %%%s\n", reg);
232aca37 248 }
249 sarg_ofs++;
250 }
251
bd2eb956 252 fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
232aca37 253
f2de0a97 254 if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
7ae48d73 255 fprintf(f, "\tadd $%d,%%esp\n",
256 (sarg_ofs - (saved_regs + 1)) * 4);
232aca37 257
1f84f6b3 258 // pop retregs
259 if (pp->has_retreg) {
260 for (i = pp->argc - 1; i >= 0; i--) {
261 if (!pp->arg[i].type.is_retreg)
262 continue;
263 if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) {
264 continue;
265 }
266 if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) {
267 continue;
268 }
269 fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
270 }
271 }
272
7ae48d73 273 if (!ret64)
274 fprintf(f, "\tpopl %%edx\n");
275 fprintf(f, "\tpopl %%ecx\n");
232aca37 276
c36e914d 277 if (pp->is_stdcall && pp->argc_stack)
278 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
232aca37 279 else
280 fprintf(f, "\tret\n\n");
281}
282
57e4efe9 283int main(int argc, char *argv[])
284{
232aca37 285 FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
bd96f656 286 const struct parsed_proto *pp;
57e4efe9 287 char line[256];
54e763a1 288 char sym_noat[256];
57e4efe9 289 char sym[256];
54e763a1 290 char *p;
291 int ret = 1;
57e4efe9 292
232aca37 293 if (argc != 5) {
294 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
57e4efe9 295 argv[0]);
296 return 1;
297 }
298
232aca37 299 hdrfn = argv[4];
57e4efe9 300 fhdr = fopen(hdrfn, "r");
301 my_assert_not(fhdr, NULL);
302
232aca37 303 fsyms_from = fopen(argv[3], "r");
304 my_assert_not(fsyms_from, NULL);
305
306 fsyms_to = fopen(argv[2], "r");
307 my_assert_not(fsyms_to, NULL);
57e4efe9 308
309 fout = fopen(argv[1], "w");
310 my_assert_not(fout, NULL);
311
312 fprintf(fout, ".text\n\n");
232aca37 313 fprintf(fout, "# to asm\n\n");
57e4efe9 314
232aca37 315 while (fgets(line, sizeof(line), fsyms_to))
57e4efe9 316 {
317 next_word(sym, sizeof(sym), line);
318 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
319 continue;
320
54e763a1 321 // IDA asm doesn't do '@' notation..
322 strcpy(sym_noat, sym);
323 p = strchr(sym_noat, '@');
324 if (p != NULL)
325 *p = 0;
326
36595fd2 327 pp = proto_parse(fhdr, sym_noat, 0);
bd96f656 328 if (pp == NULL)
232aca37 329 goto out;
57e4efe9 330
bd2eb956 331 out_toasm_x86(fout, sym_noat, pp);
232aca37 332 }
57e4efe9 333
232aca37 334 fprintf(fout, "# from asm\n\n");
57e4efe9 335
232aca37 336 while (fgets(line, sizeof(line), fsyms_from))
337 {
338 next_word(sym, sizeof(sym), line);
339 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
340 continue;
57e4efe9 341
36595fd2 342 pp = proto_parse(fhdr, sym, 0);
bd96f656 343 if (pp == NULL)
232aca37 344 goto out;
57e4efe9 345
bd96f656 346 out_fromasm_x86(fout, sym, pp);
57e4efe9 347 }
348
232aca37 349 ret = 0;
350out:
57e4efe9 351 fclose(fout);
232aca37 352 fclose(fsyms_to);
353 fclose(fsyms_from);
354 fclose(fhdr);
355 if (ret)
356 remove(argv[1]);
357
358 return ret;
57e4efe9 359}