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