use unsigned types, cc+nested branch fix, var type cast
[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
c36e914d 26static void out_toasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
232aca37 27{
232aca37 28 int must_save = 0;
29 int sarg_ofs = 1; // stack offset to args, in DWORDs
30 int args_repushed = 0;
4f12f671 31 int argc_repush;
232aca37 32 int i;
33
4f12f671 34 argc_repush = pp->argc;
35 if (pp->is_vararg)
36 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
37
c36e914d 38 for (i = 0; i < pp->argc; i++) {
39 if (pp->arg[i].reg != NULL)
40 must_save |= is_x86_reg_saved(pp->arg[i].reg);
232aca37 41 }
42
a51421fa 43 fprintf(f, ".global _%s\n", sym);
44 fprintf(f, "_%s:\n", sym);
232aca37 45
c36e914d 46 if (pp->argc_reg == 0 && !pp->is_stdcall) {
232aca37 47 fprintf(f, "\tjmp %s\n\n", sym);
48 return;
49 }
50
4f12f671 51 if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
52 && !pp->is_vararg)
53 {
232aca37 54 // load arg regs
c36e914d 55 for (i = 0; i < pp->argc; i++) {
232aca37 56 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
c36e914d 57 (i + sarg_ofs) * 4, pp->arg[i].reg);
232aca37 58 }
59 fprintf(f, "\tjmp %s\n\n", sym);
60 return;
61 }
62
63 // save the regs
c36e914d 64 for (i = 0; i < pp->argc; i++) {
65 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) {
66 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
232aca37 67 sarg_ofs++;
68 }
69 }
70
71 // reconstruct arg stack
4f12f671 72 for (i = argc_repush - 1; i >= 0; i--) {
c36e914d 73 if (pp->arg[i].reg == NULL) {
232aca37 74 fprintf(f, "\tmovl %d(%%esp), %%eax\n",
75 (i + sarg_ofs) * 4);
76 fprintf(f, "\tpushl %%eax\n");
77 sarg_ofs++;
78 args_repushed++;
79 }
80 }
4f12f671 81 // my_assert(args_repushed, pp->argc_stack);
232aca37 82
83 // load arg regs
c36e914d 84 for (i = 0; i < pp->argc; i++) {
85 if (pp->arg[i].reg != NULL) {
232aca37 86 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
c36e914d 87 (i + sarg_ofs) * 4, pp->arg[i].reg);
232aca37 88 }
89 }
90
c36e914d 91 fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
232aca37 92 fprintf(f, "\tcall %s\n\n", sym);
93
c36e914d 94 if (args_repushed && !pp->is_stdcall)
a51421fa 95 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
232aca37 96
97 // restore regs
c36e914d 98 for (i = pp->argc - 1; i >= 0; i--) {
99 if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg))
100 fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg);
232aca37 101 }
102
103 fprintf(f, "\tret\n\n");
104}
105
c36e914d 106static void out_fromasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
232aca37 107{
232aca37 108 int sarg_ofs = 1; // stack offset to args, in DWORDs
4f12f671 109 int argc_repush;
232aca37 110 int stack_args;
111 int i;
112
4f12f671 113 argc_repush = pp->argc;
114 stack_args = pp->argc_stack;
115 if (pp->is_vararg) {
116 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
117 stack_args = argc_repush - pp->argc_reg;
118 }
119
c36e914d 120 fprintf(f, "# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
232aca37 121 fprintf(f, ".global %s\n", sym);
122 fprintf(f, "%s:\n", sym);
123
c36e914d 124 if (pp->argc_reg == 0 && !pp->is_stdcall) {
232aca37 125 fprintf(f, "\tjmp _%s\n\n", sym);
126 return;
127 }
128
2b43685d 129 // scratch reg, must not be eax or edx (for ret edx:eax)
130 fprintf(f, "\tpushl %%ebx\n");
232aca37 131 sarg_ofs++;
132
133 // construct arg stack
4f12f671 134 for (i = argc_repush - 1; i >= 0; i--) {
c36e914d 135 if (pp->arg[i].reg == NULL) {
2b43685d 136 fprintf(f, "\tmovl %d(%%esp), %%ebx\n",
232aca37 137 (sarg_ofs + stack_args - 1) * 4);
2b43685d 138 fprintf(f, "\tpushl %%ebx\n");
232aca37 139 stack_args--;
140 }
141 else {
2b43685d 142 if (IS(pp->arg[i].reg, "ebx"))
143 // must reload original ebx
144 fprintf(f, "\tmovl %d(%%esp), %%ebx\n",
f3d05b09 145 (sarg_ofs - 2) * 4);
146
c36e914d 147 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
232aca37 148 }
149 sarg_ofs++;
150 }
151
152 // no worries about calling conventions - always __cdecl
153 fprintf(f, "\n\tcall _%s\n\n", sym);
154
155 if (sarg_ofs > 2)
a51421fa 156 fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - 2) * 4);
232aca37 157
2b43685d 158 fprintf(f, "\tpopl %%ebx\n");
232aca37 159
c36e914d 160 if (pp->is_stdcall && pp->argc_stack)
161 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
232aca37 162 else
163 fprintf(f, "\tret\n\n");
164}
165
57e4efe9 166int main(int argc, char *argv[])
167{
232aca37 168 FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
c36e914d 169 struct parsed_proto pp;
57e4efe9 170 char line[256];
171 char sym[256];
57e4efe9 172 int ret;
173
232aca37 174 if (argc != 5) {
175 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
57e4efe9 176 argv[0]);
177 return 1;
178 }
179
232aca37 180 hdrfn = argv[4];
57e4efe9 181 fhdr = fopen(hdrfn, "r");
182 my_assert_not(fhdr, NULL);
183
232aca37 184 fsyms_from = fopen(argv[3], "r");
185 my_assert_not(fsyms_from, NULL);
186
187 fsyms_to = fopen(argv[2], "r");
188 my_assert_not(fsyms_to, NULL);
57e4efe9 189
190 fout = fopen(argv[1], "w");
191 my_assert_not(fout, NULL);
192
193 fprintf(fout, ".text\n\n");
232aca37 194 fprintf(fout, "# to asm\n\n");
57e4efe9 195
232aca37 196 while (fgets(line, sizeof(line), fsyms_to))
57e4efe9 197 {
198 next_word(sym, sizeof(sym), line);
199 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
200 continue;
201
c36e914d 202 ret = proto_parse(fhdr, sym, &pp);
232aca37 203 if (ret)
204 goto out;
57e4efe9 205
c36e914d 206 out_toasm_x86(fout, sym, &pp);
207 proto_release(&pp);
232aca37 208 }
57e4efe9 209
232aca37 210 fprintf(fout, "# from asm\n\n");
57e4efe9 211
232aca37 212 while (fgets(line, sizeof(line), fsyms_from))
213 {
214 next_word(sym, sizeof(sym), line);
215 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
216 continue;
57e4efe9 217
c36e914d 218 ret = proto_parse(fhdr, sym, &pp);
232aca37 219 if (ret)
220 goto out;
57e4efe9 221
c36e914d 222 out_fromasm_x86(fout, sym, &pp);
223 proto_release(&pp);
57e4efe9 224 }
225
232aca37 226 ret = 0;
227out:
57e4efe9 228 fclose(fout);
232aca37 229 fclose(fsyms_to);
230 fclose(fsyms_from);
231 fclose(fhdr);
232 if (ret)
233 remove(argv[1]);
234
235 return ret;
57e4efe9 236}