8 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
9 #define IS(w, y) !strcmp(w, y)
11 #include "protoparse.h"
13 static int is_x86_reg_saved(const char *reg)
15 static const char *nosave_regs[] = { "eax", "edx", "ecx" };
19 for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
20 if (strcmp(reg, nosave_regs[r]) == 0)
26 static void out_toasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
29 int sarg_ofs = 1; // stack offset to args, in DWORDs
30 int args_repushed = 0;
34 argc_repush = pp->argc;
36 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
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);
43 fprintf(f, ".global _%s\n", sym);
44 fprintf(f, "_%s:\n", sym);
46 if (pp->argc_reg == 0 && !pp->is_stdcall) {
47 fprintf(f, "\tjmp %s\n\n", sym);
51 if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
55 for (i = 0; i < pp->argc; i++) {
56 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
57 (i + sarg_ofs) * 4, pp->arg[i].reg);
59 fprintf(f, "\tjmp %s\n\n", sym);
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);
71 // reconstruct arg stack
72 for (i = argc_repush - 1; i >= 0; i--) {
73 if (pp->arg[i].reg == NULL) {
74 fprintf(f, "\tmovl %d(%%esp), %%eax\n",
76 fprintf(f, "\tpushl %%eax\n");
81 // my_assert(args_repushed, pp->argc_stack);
84 for (i = 0; i < pp->argc; i++) {
85 if (pp->arg[i].reg != NULL) {
86 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
87 (i + sarg_ofs) * 4, pp->arg[i].reg);
91 fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
92 fprintf(f, "\tcall %s\n\n", sym);
94 if (args_repushed && !pp->is_stdcall)
95 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
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);
103 fprintf(f, "\tret\n\n");
106 static void out_fromasm_x86(FILE *f, char *sym, struct parsed_proto *pp)
108 int sarg_ofs = 1; // stack offset to args, in DWORDs
113 argc_repush = pp->argc;
114 stack_args = pp->argc_stack;
116 argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
117 stack_args = argc_repush - pp->argc_reg;
120 fprintf(f, "# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
121 fprintf(f, ".global %s\n", sym);
122 fprintf(f, "%s:\n", sym);
124 if (pp->argc_reg == 0 && !pp->is_stdcall) {
125 fprintf(f, "\tjmp _%s\n\n", sym);
129 fprintf(f, "\tpushl %%edx\n"); // just in case..
132 // construct arg stack
133 for (i = argc_repush - 1; i >= 0; i--) {
134 if (pp->arg[i].reg == NULL) {
135 fprintf(f, "\tmovl %d(%%esp), %%edx\n",
136 (sarg_ofs + stack_args - 1) * 4);
137 fprintf(f, "\tpushl %%edx\n");
141 if (IS(pp->arg[i].reg, "edx"))
142 // must reload original edx
143 fprintf(f, "\tmovl %d(%%esp), %%edx\n",
146 fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg);
151 // no worries about calling conventions - always __cdecl
152 fprintf(f, "\n\tcall _%s\n\n", sym);
155 fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - 2) * 4);
157 fprintf(f, "\tpopl %%edx\n");
159 if (pp->is_stdcall && pp->argc_stack)
160 fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
162 fprintf(f, "\tret\n\n");
165 int main(int argc, char *argv[])
167 FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
168 struct parsed_proto pp;
174 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
180 fhdr = fopen(hdrfn, "r");
181 my_assert_not(fhdr, NULL);
183 fsyms_from = fopen(argv[3], "r");
184 my_assert_not(fsyms_from, NULL);
186 fsyms_to = fopen(argv[2], "r");
187 my_assert_not(fsyms_to, NULL);
189 fout = fopen(argv[1], "w");
190 my_assert_not(fout, NULL);
192 fprintf(fout, ".text\n\n");
193 fprintf(fout, "# to asm\n\n");
195 while (fgets(line, sizeof(line), fsyms_to))
197 next_word(sym, sizeof(sym), line);
198 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
201 ret = proto_parse(fhdr, sym, &pp);
205 out_toasm_x86(fout, sym, &pp);
209 fprintf(fout, "# from asm\n\n");
211 while (fgets(line, sizeof(line), fsyms_from))
213 next_word(sym, sizeof(sym), line);
214 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
217 ret = proto_parse(fhdr, sym, &pp);
221 out_fromasm_x86(fout, sym, &pp);