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