hlist: handle moar APIs
[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
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
bd2eb956 26// output decorated name
27static const char *pp_to_name(const struct parsed_proto *pp)
28{
29 static char buf[256];
30 char atval[16];
31
32 if (!pp->is_fastcall && pp->argc_reg != 0) {
33 // can only be handled by __cdecl C func
34 snprintf(buf, sizeof(buf), "_%s", pp->name);
35 return buf;
36 }
37
38 atval[0] = 0;
39 if (pp->is_stdcall) {
40 snprintf(atval, sizeof(atval), "@%d",
41 pp->argc * 4);
42 }
43 snprintf(buf, sizeof(buf), "%s%s%s",
44 pp->is_fastcall ? "@" : "_",
45 pp->name, atval);
46
47 return buf;
48}
49
50static void out_toasm_x86(FILE *f, const char *sym_out,
51 const struct parsed_proto *pp)
232aca37 52{
232aca37 53 int must_save = 0;
54 int sarg_ofs = 1; // stack offset to args, in DWORDs
55 int args_repushed = 0;
4f12f671 56 int argc_repush;
bd2eb956 57 const char *name;
232aca37 58 int i;
59
4f12f671 60 argc_repush = pp->argc;
61 if (pp->is_vararg)
62 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
63
c36e914d 64 for (i = 0; i < pp->argc; i++) {
65 if (pp->arg[i].reg != NULL)
66 must_save |= is_x86_reg_saved(pp->arg[i].reg);
232aca37 67 }
68
bd2eb956 69 name = pp_to_name(pp);
70 fprintf(f, ".global %s\n", name);
71 fprintf(f, "%s:\n", name);
232aca37 72
c0050df6 73 if (pp->argc_reg == 0 || pp->is_fastcall) {
74 fprintf(f, "\t# %s\n",
75 pp->is_fastcall ? "__fastcall" :
76 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
54e763a1 77 fprintf(f, "\tjmp %s\n\n", sym_out);
232aca37 78 return;
79 }
80
4f12f671 81 if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
82 && !pp->is_vararg)
83 {
232aca37 84 // load arg regs
c36e914d 85 for (i = 0; i < pp->argc; i++) {
232aca37 86 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
c36e914d 87 (i + sarg_ofs) * 4, pp->arg[i].reg);
232aca37 88 }
54e763a1 89 fprintf(f, "\tjmp %s\n\n", sym_out);
232aca37 90 return;
91 }
92
93 // save the regs
c36e914d 94 for (i = 0; i < pp->argc; i++) {
95 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) {
96 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
232aca37 97 sarg_ofs++;
98 }
99 }
100
101 // reconstruct arg stack
4f12f671 102 for (i = argc_repush - 1; i >= 0; i--) {
c36e914d 103 if (pp->arg[i].reg == NULL) {
232aca37 104 fprintf(f, "\tmovl %d(%%esp), %%eax\n",
105 (i + sarg_ofs) * 4);
106 fprintf(f, "\tpushl %%eax\n");
107 sarg_ofs++;
108 args_repushed++;
109 }
110 }
4f12f671 111 // my_assert(args_repushed, pp->argc_stack);
232aca37 112
113 // load arg regs
c36e914d 114 for (i = 0; i < pp->argc; i++) {
115 if (pp->arg[i].reg != NULL) {
232aca37 116 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
c36e914d 117 (i + sarg_ofs) * 4, pp->arg[i].reg);
232aca37 118 }
119 }
120
c36e914d 121 fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
54e763a1 122 fprintf(f, "\tcall %s\n\n", sym_out);
232aca37 123
c36e914d 124 if (args_repushed && !pp->is_stdcall)
a51421fa 125 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
232aca37 126
127 // restore regs
c36e914d 128 for (i = pp->argc - 1; i >= 0; i--) {
129 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg))
130 fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
232aca37 131 }
132
133 fprintf(f, "\tret\n\n");
134}
135
54e763a1 136static void out_fromasm_x86(FILE *f, const char *sym,
137 const struct parsed_proto *pp)
232aca37 138{
232aca37 139 int sarg_ofs = 1; // stack offset to args, in DWORDs
7ae48d73 140 int saved_regs = 0;
f2de0a97 141 int c_is_stdcall;
4f12f671 142 int argc_repush;
232aca37 143 int stack_args;
7ae48d73 144 int ret64;
232aca37 145 int i;
146
4f12f671 147 argc_repush = pp->argc;
148 stack_args = pp->argc_stack;
149 if (pp->is_vararg) {
150 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
151 stack_args = argc_repush - pp->argc_reg;
152 }
153
7ae48d73 154 ret64 = strstr(pp->ret_type.name, "int64") != NULL;
155
c0050df6 156 fprintf(f, "# %s",
157 pp->is_fastcall ? "__fastcall" :
158 (pp->is_stdcall ? "__stdcall" : "__cdecl"));
7ae48d73 159 if (ret64)
160 fprintf(f, " ret64");
161 fprintf(f, "\n.global %s\n", sym);
232aca37 162 fprintf(f, "%s:\n", sym);
163
f2de0a97 164 if ((pp->argc_reg == 0 || pp->is_fastcall)
165 && !IS(pp->name, "storm_491")) // wants edx save :(
166 {
bd2eb956 167 fprintf(f, "\tjmp %s\n\n", pp_to_name(pp));
232aca37 168 return;
169 }
170
f2de0a97 171 c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall);
172
7ae48d73 173 // at least sc sub_47B150 needs edx to be preserved
174 // int64 returns use edx:eax - no edx save
175 // we use ecx also as scratch
176 fprintf(f, "\tpushl %%ecx\n");
177 saved_regs++;
232aca37 178 sarg_ofs++;
7ae48d73 179 if (!ret64) {
180 fprintf(f, "\tpushl %%edx\n");
181 saved_regs++;
182 sarg_ofs++;
183 }
232aca37 184
185 // construct arg stack
4f12f671 186 for (i = argc_repush - 1; i >= 0; i--) {
c36e914d 187 if (pp->arg[i].reg == NULL) {
7ae48d73 188 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
232aca37 189 (sarg_ofs + stack_args - 1) * 4);
7ae48d73 190 fprintf(f, "\tpushl %%ecx\n");
232aca37 191 stack_args--;
192 }
193 else {
7ae48d73 194 if (IS(pp->arg[i].reg, "ecx"))
195 // must reload original ecx
196 fprintf(f, "\tmovl %d(%%esp), %%ecx\n",
f3d05b09 197 (sarg_ofs - 2) * 4);
198
c36e914d 199 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
232aca37 200 }
201 sarg_ofs++;
202 }
203
bd2eb956 204 fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp));
232aca37 205
f2de0a97 206 if (!c_is_stdcall && sarg_ofs > saved_regs + 1)
7ae48d73 207 fprintf(f, "\tadd $%d,%%esp\n",
208 (sarg_ofs - (saved_regs + 1)) * 4);
232aca37 209
7ae48d73 210 if (!ret64)
211 fprintf(f, "\tpopl %%edx\n");
212 fprintf(f, "\tpopl %%ecx\n");
232aca37 213
c36e914d 214 if (pp->is_stdcall && pp->argc_stack)
215 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
232aca37 216 else
217 fprintf(f, "\tret\n\n");
218}
219
57e4efe9 220int main(int argc, char *argv[])
221{
232aca37 222 FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
bd96f656 223 const struct parsed_proto *pp;
57e4efe9 224 char line[256];
54e763a1 225 char sym_noat[256];
57e4efe9 226 char sym[256];
54e763a1 227 char *p;
228 int ret = 1;
57e4efe9 229
232aca37 230 if (argc != 5) {
231 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
57e4efe9 232 argv[0]);
233 return 1;
234 }
235
232aca37 236 hdrfn = argv[4];
57e4efe9 237 fhdr = fopen(hdrfn, "r");
238 my_assert_not(fhdr, NULL);
239
232aca37 240 fsyms_from = fopen(argv[3], "r");
241 my_assert_not(fsyms_from, NULL);
242
243 fsyms_to = fopen(argv[2], "r");
244 my_assert_not(fsyms_to, NULL);
57e4efe9 245
246 fout = fopen(argv[1], "w");
247 my_assert_not(fout, NULL);
248
249 fprintf(fout, ".text\n\n");
232aca37 250 fprintf(fout, "# to asm\n\n");
57e4efe9 251
232aca37 252 while (fgets(line, sizeof(line), fsyms_to))
57e4efe9 253 {
254 next_word(sym, sizeof(sym), line);
255 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
256 continue;
257
54e763a1 258 // IDA asm doesn't do '@' notation..
259 strcpy(sym_noat, sym);
260 p = strchr(sym_noat, '@');
261 if (p != NULL)
262 *p = 0;
263
36595fd2 264 pp = proto_parse(fhdr, sym_noat, 0);
bd96f656 265 if (pp == NULL)
232aca37 266 goto out;
57e4efe9 267
bd2eb956 268 out_toasm_x86(fout, sym_noat, pp);
232aca37 269 }
57e4efe9 270
232aca37 271 fprintf(fout, "# from asm\n\n");
57e4efe9 272
232aca37 273 while (fgets(line, sizeof(line), fsyms_from))
274 {
275 next_word(sym, sizeof(sym), line);
276 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
277 continue;
57e4efe9 278
36595fd2 279 pp = proto_parse(fhdr, sym, 0);
bd96f656 280 if (pp == NULL)
232aca37 281 goto out;
57e4efe9 282
bd96f656 283 out_fromasm_x86(fout, sym, pp);
57e4efe9 284 }
285
232aca37 286 ret = 0;
287out:
57e4efe9 288 fclose(fout);
232aca37 289 fclose(fsyms_to);
290 fclose(fsyms_from);
291 fclose(fhdr);
292 if (ret)
293 remove(argv[1]);
294
295 return ret;
57e4efe9 296}