9 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
10 #define IS(w, y) !strcmp(w, y)
11 #define IS_START(w, y) !strncmp(w, y, strlen(y))
13 #include "protoparse.h"
15 static const char *asmfn;
18 static const struct parsed_proto *g_func_sym_pp;
19 static char g_comment[256];
20 static int g_warn_cnt;
21 static int g_cconv_novalidate;
22 static int g_arm_mode;
24 // note: must be in ascending order
34 #define anote(fmt, ...) \
35 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
36 #define awarn(fmt, ...) do { \
37 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__); \
38 if (++g_warn_cnt == 10) { \
43 #define aerr(fmt, ...) do { \
44 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
49 #include "masm_tools.h"
51 static char *next_word_s(char *w, size_t wsize, char *s)
58 for (i = 0; i < wsize - 1; i++) {
61 if (s[i] == 0 || (!quote && (my_isblank(s[i]) || s[i] == ',')))
67 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != ',')
68 printf("warning: '%s' truncated\n", w);
73 static void next_section(FILE *fasm, char *name)
82 while (fgets(line, sizeof(line), fasm))
92 while (strlen(line) == sizeof(line) - 1) {
93 // one of those long comment lines..
94 if (!fgets(line, sizeof(line), fasm))
100 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
101 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
102 if (*p == 0 || *p == ';') {
111 if (!IS(words[1], "segment"))
114 strcpy(name, words[0]);
119 static enum dx_type parse_dx_directive(const char *name)
135 static const char *type_name(enum dx_type type)
154 static const char *type_name_float(enum dx_type type)
166 return "<bad_float>";
169 static int type_size(enum dx_type type)
188 static char *escape_string(char *s)
193 for (; *s != 0; s++) {
207 return strcpy(s, buf);
210 static void sprint_pp_short(const struct parsed_proto *pp, char *buf,
217 if (pp->ret_type.is_ptr)
219 else if (IS(pp->ret_type.name, "void"))
226 for (i = 0; i < pp->argc; i++) {
227 if (pp->arg[i].reg != NULL)
228 snprintf(buf + l, buf_size - l, "%s%s",
229 i == 0 ? "" : ",", pp->arg[i].reg);
231 snprintf(buf + l, buf_size - l, "%sa%d",
232 i == 0 ? "" : ",", i + 1);
235 snprintf(buf + l, buf_size - l, ")");
238 static const struct parsed_proto *check_var(FILE *fhdr,
239 const char *sym, const char *varname)
241 const struct parsed_proto *pp, *pp_sym;
242 char fp_sym[256], fp_var[256];
245 pp = proto_parse(fhdr, varname, 1);
247 if (IS_START(varname, "sub_"))
248 awarn("sub_ sym missing proto: '%s'\n", varname);
252 if (!pp->is_func && !pp->is_fptr)
255 pp_print(fp_var, sizeof(fp_var), pp);
257 if (pp->argc_reg == 0)
259 if (pp->argc_reg == 1 && pp->argc_stack == 0
260 && IS(pp->arg[0].reg, "ecx"))
264 if (!g_cconv_novalidate
265 && (pp->argc_reg != 2
266 || !IS(pp->arg[0].reg, "ecx")
267 || !IS(pp->arg[1].reg, "edx")))
269 awarn("unhandled reg call: %s\n", fp_var);
273 sprint_pp_short(pp, g_comment, sizeof(g_comment));
276 g_func_sym_pp = NULL;
277 pp_sym = proto_parse(fhdr, sym, 1);
280 if (!pp_sym->is_fptr)
281 aerr("func ptr data, but label '%s' !is_fptr\n", pp_sym->name);
282 g_func_sym_pp = pp_sym;
285 pp_sym = g_func_sym_pp;
290 if (pp->argc != pp_sym->argc || pp->argc_reg != pp_sym->argc_reg)
293 for (i = 0; i < pp->argc; i++) {
294 if ((pp->arg[i].reg != NULL) != (pp_sym->arg[i].reg != NULL)) {
298 if ((pp->arg[i].reg != NULL)
299 && !IS(pp->arg[i].reg, pp_sym->arg[i].reg))
308 pp_print(fp_sym, sizeof(fp_sym), pp_sym);
309 anote("var: %s\n", fp_var);
310 anote("sym: %s\n", fp_sym);
311 awarn("^ mismatch\n");
317 static void output_decorated_pp(FILE *fout,
318 const struct parsed_proto *pp)
320 if (pp->name[0] != '_')
321 fprintf(fout, pp->is_fastcall ? "@" : "_");
322 fprintf(fout, "%s", pp->name);
323 if (pp->is_stdcall && pp->argc > 0)
324 fprintf(fout, "@%d", pp->argc * 4);
327 static int align_value(int src_val)
330 awarn("bad align: %d\n", src_val);
336 return __builtin_ffs(src_val) - 1;
339 static int cmpstringp(const void *p1, const void *p2)
341 return strcmp(*(char * const *)p1, *(char * const *)p2);
344 /* XXX: maybe move to external file? */
345 static const char *unwanted_syms[] = {
371 static int is_unwanted_sym(const char *sym)
373 return bsearch(&sym, unwanted_syms, ARRAY_SIZE(unwanted_syms),
374 sizeof(unwanted_syms[0]), cmpstringp) != NULL;
377 int main(int argc, char *argv[])
379 FILE *fout, *fasm, *fhdr, *frlist;
380 const struct parsed_proto *pp;
381 int no_decorations = 0;
382 char comment_char = '#';
409 // -nd: no symbol decorations
410 printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> <hdrf> [rlist]*\n",
415 for (arg = 1; arg < argc; arg++) {
416 if (IS(argv[arg], "-nd"))
418 else if (IS(argv[arg], "-i"))
419 g_cconv_novalidate = 1;
420 else if (IS(argv[arg], "-a")) {
431 fasm = fopen(asmfn, "r");
432 my_assert_not(fasm, NULL);
435 fhdr = fopen(hdrfn, "r");
436 my_assert_not(fhdr, NULL);
438 fout = fopen(argv[arg_out], "w");
439 my_assert_not(fout, NULL);
442 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
443 my_assert_not(pub_syms, NULL);
446 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
447 my_assert_not(rlist, NULL);
449 for (; arg < argc; arg++) {
450 frlist = fopen(argv[arg], "r");
451 my_assert_not(frlist, NULL);
453 while (fgets(line, sizeof(line), frlist)) {
455 if (*p == 0 || *p == ';')
458 p = next_word(words[0], sizeof(words[0]), p);
459 if (words[0][0] == 0)
462 if (rlist_cnt >= rlist_alloc) {
463 rlist_alloc = rlist_alloc * 2 + 64;
464 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
465 my_assert_not(rlist, NULL);
467 rlist[rlist_cnt++] = strdup(words[0]);
475 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
477 qsort(unwanted_syms, ARRAY_SIZE(unwanted_syms),
478 sizeof(unwanted_syms[0]), cmpstringp);
483 next_section(fasm, line);
486 if (IS(line + 1, "text"))
489 if (IS(line + 1, "rdata"))
490 fprintf(fout, "\n.section .rodata\n");
491 else if (IS(line + 1, "data"))
492 fprintf(fout, "\n.data\n");
494 aerr("unhandled section: '%s'\n", line);
496 fprintf(fout, ".align %d\n", align_value(4));
498 while (fgets(line, sizeof(line), fasm))
504 if (*p == 0 || *p == ';')
507 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
508 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
509 if (*p == 0 || *p == ';') {
520 if (IS_START(p, "sctclrtype"))
521 g_func_sym_pp = NULL;
524 if (wordc == 2 && IS(words[1], "ends"))
526 if (wordc <= 2 && IS(words[0], "end"))
529 aerr("unhandled: '%s'\n", words[0]);
532 if (IS(words[0], "assume"))
535 if (IS(words[0], "align")) {
536 val = parse_number(words[1]);
537 fprintf(fout, "\t\t .align %d", align_value(val));
542 type = parse_dx_directive(words[0]);
543 if (type == DXT_UNSPEC) {
544 type = parse_dx_directive(words[1]);
548 if (type == DXT_UNSPEC)
549 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
552 snprintf(last_sym, sizeof(last_sym), "%s", sym);
554 pp = proto_parse(fhdr, sym, 1);
556 g_func_sym_pp = NULL;
558 // public/global name
559 if (pub_sym_cnt >= pub_sym_alloc) {
561 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
562 my_assert_not(pub_syms, NULL);
564 pub_syms[pub_sym_cnt++] = strdup(sym);
568 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
581 fprintf(fout, "\t\t ");
584 // fill out some unwanted strings with zeroes..
585 if (type == DXT_BYTE && words[w][0] == '\''
586 && is_unwanted_sym(last_sym))
589 for (; w < wordc; w++) {
590 if (words[w][0] == '\'') {
592 for (; *p && *p != '\''; p++)
596 // assume encoded byte
600 fprintf(fout, ".skip %d", len);
603 else if (type == DXT_BYTE
604 && (words[w][0] == '\''
605 || (w + 1 < wordc && words[w + 1][0] == '\'')))
607 // string; use asciz for most common case
608 if (w == wordc - 2 && IS(words[w + 1], "0")) {
609 fprintf(fout, ".asciz \"");
613 fprintf(fout, ".ascii \"");
615 for (; w < wordc; w++) {
616 if (words[w][0] == '\'') {
618 p2 = strchr(p, '\'');
620 aerr("unterminated string? '%s'\n", p);
621 memcpy(word, p, p2 - p);
623 fprintf(fout, "%s", escape_string(word));
626 val = parse_number(words[w]);
628 aerr("bad string trailing byte?\n");
629 fprintf(fout, "\\x%02lx", val);
636 if (w == wordc - 2) {
637 if (IS_START(words[w + 1], "dup(")) {
638 cnt = parse_number(words[w]);
639 p = words[w + 1] + 4;
643 memmove(word, p, p2 - p);
648 val = parse_number(word);
650 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
651 cnt, type_size(type), val);
656 if (type == DXT_DWORD && words[w][0] == '\''
657 && words[w][5] == '\'' && strlen(words[w]) == 6)
663 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
664 fprintf(fout, ".long 0x%lx", val);
665 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
669 if (type >= DXT_DWORD && strchr(words[w], '.'))
674 if (g_arm_mode && type == DXT_TEN) {
675 fprintf(fout, ".fill 10");
676 snprintf(g_comment, sizeof(g_comment), "%s %s",
677 type_name_float(type), words[w]);
680 fprintf(fout, "%s %s", type_name_float(type), words[w]);
685 fprintf(fout, "%s ", type_name(type));
686 for (; w < wordc; w++)
691 is_label = is_bss = 0;
692 if (w <= wordc - 2 && IS(words[w], "offset")) {
696 else if (IS(words[w], "?")) {
699 else if (type == DXT_DWORD
700 && !('0' <= words[w][0] && words[w][0] <= '9'))
711 if (IS_START(p, "loc_") || IS_START(p, "__imp")
712 || strchr(p, '?') || strchr(p, '@')
713 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
717 snprintf(g_comment, sizeof(g_comment), "%s", p);
720 pp = check_var(fhdr, sym, p);
722 fprintf(fout, "%s%s",
723 (no_decorations || p[0] == '_') ? "" : "_", p);
727 fprintf(fout, "%s", pp->name);
729 output_decorated_pp(fout, pp);
734 val = parse_number(words[w]);
736 fprintf(fout, "%ld", val);
738 fprintf(fout, "0x%lx", val);
745 if (g_comment[0] != 0) {
746 fprintf(fout, "\t\t%c %s", comment_char, g_comment);
756 for (i = 0; i < pub_sym_cnt; i++)
757 fprintf(fout, ".global %s%s\n",
758 no_decorations ? "" : "_", pub_syms[i]);
767 // vim:ts=2:shiftwidth=2:expandtab