5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
14 #include "my_assert.h"
18 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
19 #define IS(w, y) !strcmp(w, y)
20 #define IS_START(w, y) !strncmp(w, y, strlen(y))
22 #include "protoparse.h"
24 static const char *asmfn;
27 static const struct parsed_proto *g_func_sym_pp;
28 static char g_comment[256];
29 static int g_warn_cnt;
30 static int g_cconv_novalidate;
31 static int g_arm_mode;
33 // note: must be in ascending order
43 #define anote(fmt, ...) \
44 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
45 #define awarn(fmt, ...) do { \
46 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__); \
47 if (++g_warn_cnt == 10) { \
52 #define aerr(fmt, ...) do { \
53 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
58 #include "masm_tools.h"
60 static char *next_word_s(char *w, size_t wsize, char *s)
67 for (i = 0; i < wsize - 1; i++) {
70 if (s[i] == 0 || (!quote && (my_isblank(s[i]) || s[i] == ',')))
76 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != ',')
77 printf("warning: '%s' truncated\n", w);
82 static void next_section(FILE *fasm, char *name)
91 while (my_fgets(line, sizeof(line), fasm))
103 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
104 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
105 if (*p == 0 || *p == ';') {
114 if (!IS(words[1], "segment"))
117 strcpy(name, words[0]);
122 static enum dx_type parse_dx_directive(const char *name)
138 static const char *type_name(enum dx_type type)
157 static const char *type_name_float(enum dx_type type)
169 return "<bad_float>";
172 static int type_size(enum dx_type type)
191 static char *escape_string(char *s)
196 for (; *s != 0; s++) {
210 return strcpy(s, buf);
213 static void sprint_pp_short(const struct parsed_proto *pp, char *buf,
220 if (pp->ret_type.is_ptr)
222 else if (IS(pp->ret_type.name, "void"))
229 for (i = 0; i < pp->argc; i++) {
230 if (pp->arg[i].reg != NULL)
231 snprintf(buf + l, buf_size - l, "%s%s",
232 i == 0 ? "" : ",", pp->arg[i].reg);
234 snprintf(buf + l, buf_size - l, "%sa%d",
235 i == 0 ? "" : ",", i + 1);
238 snprintf(buf + l, buf_size - l, ")");
241 static const struct parsed_proto *check_var(FILE *fhdr,
242 const char *sym, const char *varname)
244 const struct parsed_proto *pp, *pp_sym;
245 char fp_sym[256], fp_var[256], *p;
248 pp = proto_parse(fhdr, varname, 1);
250 if (IS_START(varname, "sub_"))
251 awarn("sub_ sym missing proto: '%s'\n", varname);
255 if (!pp->is_func && !pp->is_fptr)
258 pp_print(fp_var, sizeof(fp_var), pp);
260 if (pp->argc_reg == 0)
262 if (pp->argc_reg == 1 && pp->argc_stack == 0
263 && IS(pp->arg[0].reg, "ecx"))
267 if (!g_cconv_novalidate
268 && (pp->argc_reg != 2
269 || !IS(pp->arg[0].reg, "ecx")
270 || !IS(pp->arg[1].reg, "edx")))
272 awarn("unhandled reg call: %s\n", fp_var);
276 // fptrs must use 32bit args, callsite might have no information and
277 // lack a cast to smaller types, which results in incorrectly masked
278 // args passed (callee may assume masked args, it does on ARM)
279 for (i = 0; i < pp->argc; i++) {
280 if (pp->arg[i].type.is_ptr)
282 p = pp->arg[i].type.name;
283 if (strstr(p, "int8") || strstr(p, "int16")
284 || strstr(p, "char") || strstr(p, "short"))
286 awarn("reference to %s with arg%d '%s'\n", pp->name, i + 1, p);
290 sprint_pp_short(pp, g_comment, sizeof(g_comment));
293 g_func_sym_pp = NULL;
294 pp_sym = proto_parse(fhdr, sym, 1);
297 if (!pp_sym->is_fptr)
298 aerr("func ptr data, but label '%s' !is_fptr\n", pp_sym->name);
299 g_func_sym_pp = pp_sym;
302 pp_sym = g_func_sym_pp;
307 if (pp_cmp_func(pp, pp_sym)) {
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 = NULL, *frlist;
380 const struct parsed_proto *pp;
381 int no_decorations = 0;
382 char comment_char = '#';
411 // -nd: no symbol decorations
412 printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> <hdrf> [rlist]*\n"
413 "%s -hdr <.h> <.asm>\n",
418 for (arg = 1; arg < argc; arg++) {
419 if (IS(argv[arg], "-nd"))
421 else if (IS(argv[arg], "-i"))
422 g_cconv_novalidate = 1;
423 else if (IS(argv[arg], "-a")) {
427 else if (IS(argv[arg], "-hdr"))
436 fasm = fopen(asmfn, "r");
437 my_assert_not(fasm, NULL);
441 fhdr = fopen(hdrfn, "r");
442 my_assert_not(fhdr, NULL);
445 fout = fopen(argv[arg_out], "w");
446 my_assert_not(fout, NULL);
449 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
450 my_assert_not(pub_syms, NULL);
453 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
454 my_assert_not(rlist, NULL);
456 for (; arg < argc; arg++) {
457 frlist = fopen(argv[arg], "r");
458 my_assert_not(frlist, NULL);
460 while (my_fgets(line, sizeof(line), frlist)) {
462 if (*p == 0 || *p == ';')
465 p = next_word(words[0], sizeof(words[0]), p);
466 if (words[0][0] == 0)
469 if (rlist_cnt >= rlist_alloc) {
470 rlist_alloc = rlist_alloc * 2 + 64;
471 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
472 my_assert_not(rlist, NULL);
474 rlist[rlist_cnt++] = strdup(words[0]);
482 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
484 qsort(unwanted_syms, ARRAY_SIZE(unwanted_syms),
485 sizeof(unwanted_syms[0]), cmpstringp);
490 next_section(fasm, line);
493 if (IS(line + 1, "text"))
496 if (IS(line + 1, "rdata")) {
499 fprintf(fout, "\n.section .rodata\n");
501 else if (IS(line + 1, "data")) {
504 fprintf(fout, "\n.data\n");
507 aerr("unhandled section: '%s'\n", line);
510 fprintf(fout, ".align %d\n", align_value(4));
512 while (my_fgets(line, sizeof(line), fasm))
522 if (IS_START(p, ";org") && sscanf(p + 5, "%Xh", &i) == 1) {
523 // ;org is only seen at section start, so assume . addr 0
525 if (i != 0 && !header_mode)
526 fprintf(fout, "\t\t .skip 0x%x\n", i);
531 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
532 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
533 if (*p == 0 || *p == ';') {
544 if (IS_START(p, "sctclrtype"))
545 g_func_sym_pp = NULL;
548 if (wordc == 2 && IS(words[1], "ends"))
550 if (wordc <= 2 && IS(words[0], "end"))
553 aerr("unhandled: '%s'\n", words[0]);
556 if (IS(words[0], "assume"))
559 if (IS(words[0], "align")) {
563 val = parse_number(words[1]);
564 fprintf(fout, "\t\t .align %d", align_value(val));
569 type = parse_dx_directive(words[0]);
570 if (type == DXT_UNSPEC) {
571 type = parse_dx_directive(words[1]);
575 if (type == DXT_UNSPEC)
576 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
583 fprintf(fout, "extern ");
585 fprintf(fout, "const ");
589 for (i = w; i < wordc; i++)
590 if (words[i][0] == '\'')
593 fprintf(fout, "char %s[];\n", sym);
595 fprintf(fout, "uint8_t %s;\n", sym);
599 fprintf(fout, "uint16_t %s;\n", sym);
603 fprintf(fout, "uint32_t %s;\n", sym);
607 fprintf(fout, "_UNKNOWN %s;\n", sym);
614 snprintf(last_sym, sizeof(last_sym), "%s", sym);
616 pp = proto_parse(fhdr, sym, 1);
618 g_func_sym_pp = NULL;
620 // public/global name
621 if (pub_sym_cnt >= pub_sym_alloc) {
623 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
624 my_assert_not(pub_syms, NULL);
626 pub_syms[pub_sym_cnt++] = strdup(sym);
630 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
646 fprintf(fout, "\t\t ");
649 // fill out some unwanted strings with zeroes..
650 if (type == DXT_BYTE && words[w][0] == '\''
651 && is_unwanted_sym(last_sym))
654 for (; w < wordc; w++) {
655 if (words[w][0] == '\'') {
657 for (; *p && *p != '\''; p++)
661 // assume encoded byte
665 fprintf(fout, ".skip %d", len);
668 else if (type == DXT_BYTE
669 && (words[w][0] == '\''
670 || (w + 1 < wordc && words[w + 1][0] == '\'')))
672 // string; use asciz for most common case
673 if (w == wordc - 2 && IS(words[w + 1], "0")) {
674 fprintf(fout, ".asciz \"");
678 fprintf(fout, ".ascii \"");
680 for (; w < wordc; w++) {
681 if (words[w][0] == '\'') {
683 p2 = strchr(p, '\'');
685 aerr("unterminated string? '%s'\n", p);
686 memcpy(word, p, p2 - p);
688 fprintf(fout, "%s", escape_string(word));
691 val = parse_number(words[w]);
693 aerr("bad string trailing byte?\n");
694 fprintf(fout, "\\x%02lx", val);
701 if (w == wordc - 2) {
702 if (IS_START(words[w + 1], "dup(")) {
703 cnt = parse_number(words[w]);
704 p = words[w + 1] + 4;
708 memmove(word, p, p2 - p);
713 val = parse_number(word);
715 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
716 cnt, type_size(type), val);
721 if (type == DXT_DWORD && words[w][0] == '\''
722 && words[w][5] == '\'' && strlen(words[w]) == 6)
728 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
729 fprintf(fout, ".long 0x%lx", val);
730 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
734 if (type >= DXT_DWORD && strchr(words[w], '.'))
739 if (g_arm_mode && type == DXT_TEN) {
740 fprintf(fout, ".fill 10");
741 snprintf(g_comment, sizeof(g_comment), "%s %s",
742 type_name_float(type), words[w]);
745 fprintf(fout, "%s %s", type_name_float(type), words[w]);
750 fprintf(fout, "%s ", type_name(type));
751 for (; w < wordc; w++)
756 is_label = is_bss = 0;
757 if (w <= wordc - 2 && IS(words[w], "offset")) {
761 else if (IS(words[w], "?")) {
764 else if (type == DXT_DWORD
765 && !('0' <= words[w][0] && words[w][0] <= '9'))
776 if (IS_START(p, "loc_") || IS_START(p, "__imp")
777 || strchr(p, '?') || strchr(p, '@')
778 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
782 snprintf(g_comment, sizeof(g_comment), "%s", p);
785 pp = check_var(fhdr, sym, p);
787 fprintf(fout, "%s%s",
788 (no_decorations || p[0] == '_') ? "" : "_", p);
792 fprintf(fout, "%s", pp->name);
794 output_decorated_pp(fout, pp);
799 val = parse_number(words[w]);
801 fprintf(fout, "%ld", val);
803 fprintf(fout, "0x%lx", val);
810 if (g_comment[0] != 0) {
811 fprintf(fout, "\t\t%c %s", comment_char, g_comment);
821 for (i = 0; i < pub_sym_cnt; i++)
822 fprintf(fout, ".global %s%s\n",
823 no_decorations ? "" : "_", pub_syms[i]);
833 // vim:ts=2:shiftwidth=2:expandtab