8 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
9 #define IS(w, y) !strcmp(w, y)
11 static int find_protostr(char *dst, size_t dlen, FILE *fhdr,
12 const char *sym, int *pline)
19 while (fgets(dst, dlen, fhdr))
22 if (strstr(dst, sym) != NULL)
30 p = dst + strlen(dst);
31 for (p--; p > dst && my_isblank(*p); --p)
37 static int get_regparm(char *dst, size_t dlen, char *p)
44 for (o = 0, i = 1; o < dlen; i++) {
56 static const char *known_types[] = {
81 static int check_type(const char *name)
85 for (i = 0; i < ARRAY_SIZE(known_types); i++) {
86 l = strlen(known_types[i]);
87 if (strncmp(known_types[i], name, l) == 0)
94 /* args are always expanded to 32bit */
95 static const char *map_reg(const char *reg)
97 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
98 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
99 const char *regs_b[] = { "al", "bl", "cl", "dl" };
102 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
103 if (IS(reg, regs_w[i]))
106 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
107 if (IS(reg, regs_b[i]))
113 static const char *hdrfn;
114 static int pline = 0;
116 static int parse_protostr(char *protostr, char **reglist, int *cnt_out,
127 if (p[0] == '/' && p[1] == '/') {
128 //printf("warning: decl for sym '%s' is commented out\n", sym);
134 printf("%s:%d:%ld: unhandled return in '%s'\n",
135 hdrfn, pline, (p - protostr) + 1, protostr);
141 p = next_word(cconv, sizeof(cconv), p);
144 printf("%s:%d:%ld: cconv missing\n",
145 hdrfn, pline, (p - protostr) + 1);
148 if (IS(cconv, "__cdecl"))
150 else if (IS(cconv, "__stdcall"))
152 else if (IS(cconv, "__fastcall"))
154 else if (IS(cconv, "__thiscall"))
156 else if (IS(cconv, "__userpurge"))
157 *is_stdcall = 1; // in all cases seen..
158 else if (IS(cconv, "__usercall"))
159 *is_stdcall = 0; // ..or is it?
161 printf("%s:%d:%ld: unhandled cconv: '%s'\n",
162 hdrfn, pline, (p - protostr) + 1, cconv);
166 p = next_idt(buf, sizeof(buf), p);
169 printf("%s:%d:%ld: func name missing\n",
170 hdrfn, pline, (p - protostr) + 1);
174 ret = get_regparm(regparm, sizeof(regparm), p);
176 if (!IS(regparm, "eax") && !IS(regparm, "ax")
177 && !IS(regparm, "al"))
179 printf("%s:%d:%ld: bad regparm: %s\n",
180 hdrfn, pline, (p - protostr) + 1, regparm);
188 printf("%s:%d:%ld: '(' expected, got '%c'\n",
189 hdrfn, pline, (p - protostr) + 1, *p);
205 printf("%s:%d:%ld: unhandled type for arg%d\n",
206 hdrfn, pline, (p - protostr) + 1, xarg);
212 p = next_idt(buf, sizeof(buf), p);
216 printf("%s:%d:%ld: idt missing for arg%d\n",
217 hdrfn, pline, (p - protostr) + 1, xarg);
221 reglist[xarg - 1] = NULL;
223 ret = get_regparm(regparm, sizeof(regparm), p);
228 reglist[xarg - 1] = strdup(map_reg(regparm));
232 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
233 if (reglist[0] != NULL) {
234 printf("%s:%d: %s with arg1 spec %s?\n",
235 hdrfn, pline, cconv, reglist[0]);
237 reglist[0] = strdup("ecx");
240 if (xarg > 1 && IS(cconv, "__fastcall")) {
241 if (reglist[1] != NULL) {
242 printf("%s:%d: %s with arg2 spec %s?\n",
243 hdrfn, pline, cconv, reglist[1]);
245 reglist[1] = strdup("edx");
253 static int is_x86_reg_saved(const char *reg)
255 static const char *nosave_regs[] = { "eax", "edx", "ecx" };
259 for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
260 if (strcmp(reg, nosave_regs[r]) == 0)
266 static void out_toasm_x86(FILE *f, char *sym, char *reg_list[], int reg_cnt,
269 int have_normal = 0; // normal args
272 int sarg_ofs = 1; // stack offset to args, in DWORDs
273 int args_repushed = 0;
276 for (i = 0; i < reg_cnt; i++) {
277 if (reg_list[i] == NULL) {
283 must_save |= is_x86_reg_saved(reg_list[i]);
286 fprintf(f, ".global _%s\n", sym);
287 fprintf(f, "_%s:\n", sym);
289 if (!have_regs && !is_stdcall) {
290 fprintf(f, "\tjmp %s\n\n", sym);
294 if (!have_normal && !must_save && !is_stdcall) {
296 for (i = 0; i < reg_cnt; i++) {
297 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
298 (i + sarg_ofs) * 4, reg_list[i]);
300 fprintf(f, "\tjmp %s\n\n", sym);
305 for (i = 0; i < reg_cnt; i++) {
306 if (reg_list[i] != NULL && is_x86_reg_saved(reg_list[i])) {
307 fprintf(f, "\tpushl %%%s\n", reg_list[i]);
312 // reconstruct arg stack
313 for (i = reg_cnt - 1; i >= 0; i--) {
314 if (reg_list[i] == NULL) {
315 fprintf(f, "\tmovl %d(%%esp), %%eax\n",
317 fprintf(f, "\tpushl %%eax\n");
322 my_assert(args_repushed, have_normal);
325 for (i = 0; i < reg_cnt; i++) {
326 if (reg_list[i] != NULL) {
327 fprintf(f, "\tmovl %d(%%esp), %%%s\n",
328 (i + sarg_ofs) * 4, reg_list[i]);
332 fprintf(f, "\n\t# %s\n", is_stdcall ? "__stdcall" : "__cdecl");
333 fprintf(f, "\tcall %s\n\n", sym);
335 if (args_repushed && !is_stdcall)
336 fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
339 for (i = reg_cnt - 1; i >= 0; i--) {
340 if (reg_list[i] != NULL && is_x86_reg_saved(reg_list[i]))
341 fprintf(f, "\tpopl %%%s\n", reg_list[i]);
344 fprintf(f, "\tret\n\n");
347 static void out_fromasm_x86(FILE *f, char *sym, char *reg_list[], int reg_cnt,
350 int have_normal = 0; // normal args
352 int sarg_ofs = 1; // stack offset to args, in DWORDs
356 for (i = 0; i < reg_cnt; i++) {
357 if (reg_list[i] == NULL) {
365 fprintf(f, "# %s\n", is_stdcall ? "__stdcall" : "__cdecl");
366 fprintf(f, ".global %s\n", sym);
367 fprintf(f, "%s:\n", sym);
369 if (!have_regs && !is_stdcall) {
370 fprintf(f, "\tjmp _%s\n\n", sym);
374 fprintf(f, "\tpushl %%edx\n"); // just in case..
377 // construct arg stack
378 stack_args = have_normal;
379 for (i = reg_cnt - 1; i >= 0; i--) {
380 if (reg_list[i] == NULL) {
381 fprintf(f, "\tmovl %d(%%esp), %%edx\n",
382 (sarg_ofs + stack_args - 1) * 4);
383 fprintf(f, "\tpushl %%edx\n");
387 fprintf(f, "\tpushl %%%s\n", reg_list[i]);
392 // no worries about calling conventions - always __cdecl
393 fprintf(f, "\n\tcall _%s\n\n", sym);
396 fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - 2) * 4);
398 fprintf(f, "\tpopl %%edx\n");
400 if (is_stdcall && have_normal)
401 fprintf(f, "\tret $%d\n\n", have_normal * 4);
403 fprintf(f, "\tret\n\n");
406 static void free_reglist(char *reg_list[], int reg_cnt)
410 for (i = 0; i < reg_cnt; i++) {
411 if (reg_list[i] == NULL) {
418 int main(int argc, char *argv[])
420 FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
430 printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
436 fhdr = fopen(hdrfn, "r");
437 my_assert_not(fhdr, NULL);
439 fsyms_from = fopen(argv[3], "r");
440 my_assert_not(fsyms_from, NULL);
442 fsyms_to = fopen(argv[2], "r");
443 my_assert_not(fsyms_to, NULL);
445 fout = fopen(argv[1], "w");
446 my_assert_not(fout, NULL);
448 fprintf(fout, ".text\n\n");
449 fprintf(fout, "# to asm\n\n");
451 while (fgets(line, sizeof(line), fsyms_to))
453 next_word(sym, sizeof(sym), line);
454 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
457 ret = find_protostr(protostr, sizeof(protostr), fhdr,
460 printf("%s: sym '%s' is missing\n",
465 ret = parse_protostr(protostr, reg_list, ®_cnt, &is_stdcall);
469 out_toasm_x86(fout, sym, reg_list, reg_cnt, is_stdcall);
470 free_reglist(reg_list, reg_cnt);
473 fprintf(fout, "# from asm\n\n");
475 while (fgets(line, sizeof(line), fsyms_from))
477 next_word(sym, sizeof(sym), line);
478 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
481 ret = find_protostr(protostr, sizeof(protostr), fhdr,
484 printf("%s: sym '%s' is missing\n",
489 ret = parse_protostr(protostr, reg_list, ®_cnt, &is_stdcall);
493 out_fromasm_x86(fout, sym, reg_list, reg_cnt, is_stdcall);
494 free_reglist(reg_list, reg_cnt);