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