5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
16 #include "my_assert.h"
20 #include "protoparse.h"
22 static const char *asmfn;
25 static const struct parsed_proto *g_func_sym_pp;
26 static char g_comment[256];
27 static int g_warn_cnt;
28 static int g_cconv_novalidate;
29 static int g_arm_mode;
31 // note: must be in ascending order
41 #define anote(fmt, ...) \
42 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
43 #define awarn(fmt, ...) do { \
44 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__); \
45 if (++g_warn_cnt == 10) { \
50 #define aerr(fmt, ...) do { \
51 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
56 #include "masm_tools.h"
58 static char *next_word_s(char *w, size_t wsize, char *s)
65 for (i = 0; i < wsize - 1; i++) {
68 if (s[i] == 0 || (!quote && (my_isblank(s[i]) || s[i] == ',')))
74 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != ',')
75 printf("warning: '%s' truncated\n", w);
80 static void next_section(FILE *fasm, char *name)
89 while (my_fgets(line, sizeof(line), fasm))
101 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
102 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
103 if (*p == 0 || *p == ';') {
112 if (!IS(words[1], "segment"))
115 strcpy(name, words[0]);
120 static enum dx_type parse_dx_directive(const char *name)
136 static const char *type_name(enum dx_type type)
155 static const char *type_name_float(enum dx_type type)
167 return "<bad_float>";
170 static int type_size(enum dx_type type)
189 static char *escape_string(char *s)
194 for (; *s != 0; s++) {
208 if (t - buf > sizeof(buf))
209 aerr("string is too long\n");
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 = '#';
412 // -nd: no symbol decorations
413 printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> <hdrf> [rlist]*\n"
414 "%s -hdr <.h> <.asm>\n",
419 for (arg = 1; arg < argc; arg++) {
420 if (IS(argv[arg], "-nd"))
422 else if (IS(argv[arg], "-i"))
423 g_cconv_novalidate = 1;
424 else if (IS(argv[arg], "-a")) {
428 else if (IS(argv[arg], "-hdr"))
437 fasm = fopen(asmfn, "r");
438 my_assert_not(fasm, NULL);
442 fhdr = fopen(hdrfn, "r");
443 my_assert_not(fhdr, NULL);
446 fout = fopen(argv[arg_out], "w");
447 my_assert_not(fout, NULL);
450 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
451 my_assert_not(pub_syms, NULL);
454 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
455 my_assert_not(rlist, NULL);
457 for (; arg < argc; arg++) {
458 frlist = fopen(argv[arg], "r");
459 my_assert_not(frlist, NULL);
461 while (my_fgets(line, sizeof(line), frlist)) {
463 if (*p == 0 || *p == ';')
466 p = next_word(words[0], sizeof(words[0]), p);
467 if (words[0][0] == 0)
470 if (rlist_cnt >= rlist_alloc) {
471 rlist_alloc = rlist_alloc * 2 + 64;
472 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
473 my_assert_not(rlist, NULL);
475 rlist[rlist_cnt++] = strdup(words[0]);
483 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
485 qsort(unwanted_syms, ARRAY_SIZE(unwanted_syms),
486 sizeof(unwanted_syms[0]), cmpstringp);
491 next_section(fasm, line);
494 if (IS(line + 1, "text"))
497 if (IS(line + 1, "rdata")) {
500 fprintf(fout, "\n.section .rodata\n");
502 else if (IS(line + 1, "data")) {
505 fprintf(fout, "\n.data\n");
508 aerr("unhandled section: '%s'\n", line);
511 fprintf(fout, ".align %d\n", align_value(4));
513 while (my_fgets(line, sizeof(line), fasm))
523 if (IS_START(p, ";org") && sscanf(p + 5, "%Xh", &i) == 1) {
524 // ;org is only seen at section start, so assume . addr 0
526 if (i != 0 && !header_mode)
527 fprintf(fout, "\t\t .skip 0x%x\n", i);
532 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
533 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
534 if (*p == 0 || *p == ';') {
545 if (IS_START(p, "sctclrtype"))
546 g_func_sym_pp = NULL;
549 if (wordc == 2 && IS(words[1], "ends"))
551 if (wordc <= 2 && IS(words[0], "end"))
554 aerr("unhandled: '%s'\n", words[0]);
557 if (IS(words[0], "assume"))
560 if (IS(words[0], "align")) {
564 val = parse_number(words[1], 0);
565 fprintf(fout, "\t\t .align %d", align_value(val));
569 if (IS(words[0], "public")) {
570 // skip, sym should appear in header anyway
575 type = parse_dx_directive(words[0]);
576 if (type == DXT_UNSPEC) {
577 type = parse_dx_directive(words[1]);
581 if (type == DXT_UNSPEC)
582 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
589 fprintf(fout, "extern ");
591 fprintf(fout, "const ");
595 for (i = w; i < wordc; i++)
596 if (words[i][0] == '\'')
599 fprintf(fout, "char %s[];\n", sym);
601 fprintf(fout, "uint8_t %s;\n", sym);
605 fprintf(fout, "uint16_t %s;\n", sym);
609 fprintf(fout, "uint32_t %s;\n", sym);
613 fprintf(fout, "_UNKNOWN %s;\n", sym);
620 snprintf(last_sym, sizeof(last_sym), "%s", sym);
622 pp = proto_parse(fhdr, sym, 1);
624 g_func_sym_pp = NULL;
626 // public/global name
627 if (pub_sym_cnt >= pub_sym_alloc) {
629 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
630 my_assert_not(pub_syms, NULL);
632 pub_syms[pub_sym_cnt++] = strdup(sym);
636 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
652 fprintf(fout, "\t\t ");
655 // fill out some unwanted strings with zeroes..
656 if (type == DXT_BYTE && words[w][0] == '\''
657 && is_unwanted_sym(last_sym))
660 for (; w < wordc; w++) {
661 if (words[w][0] == '\'') {
663 for (; *p && *p != '\''; p++)
667 // assume encoded byte
671 fprintf(fout, ".skip %d", len);
674 else if (type == DXT_BYTE
675 && (words[w][0] == '\''
676 || (w + 1 < wordc && words[w + 1][0] == '\'')))
678 // string; use asciz for most common case
679 if (w == wordc - 2 && IS(words[w + 1], "0")) {
680 fprintf(fout, ".asciz \"");
684 fprintf(fout, ".ascii \"");
686 for (; w < wordc; w++) {
687 if (words[w][0] == '\'') {
689 p2 = strchr(p, '\'');
691 aerr("unterminated string? '%s'\n", p);
692 memcpy(word, p, p2 - p);
694 fprintf(fout, "%s", escape_string(word));
697 val = parse_number(words[w], 0);
699 aerr("bad string trailing byte?\n");
700 // unfortunately \xHH is unusable - gas interprets
701 // things like \x27b as 0x7b, so have to use octal here
702 fprintf(fout, "\\%03lo", val);
709 if (w == wordc - 2) {
710 if (IS_START(words[w + 1], "dup(")) {
711 cnt = parse_number(words[w], 0);
712 p = words[w + 1] + 4;
716 memmove(word, p, p2 - p);
721 val = parse_number(word, 0);
723 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
724 cnt, type_size(type), val);
729 if (type == DXT_DWORD && words[w][0] == '\''
730 && words[w][5] == '\'' && strlen(words[w]) == 6)
736 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
737 fprintf(fout, ".long 0x%lx", val);
738 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
742 if (type >= DXT_DWORD && strchr(words[w], '.'))
747 if (g_arm_mode && type == DXT_TEN) {
748 fprintf(fout, ".fill 10");
749 snprintf(g_comment, sizeof(g_comment), "%s %s",
750 type_name_float(type), words[w]);
753 fprintf(fout, "%s %s", type_name_float(type), words[w]);
758 fprintf(fout, "%s ", type_name(type));
759 for (; w < wordc; w++)
764 is_label = is_bss = 0;
765 if (w <= wordc - 2 && IS(words[w], "offset")) {
769 else if (IS(words[w], "?")) {
772 else if (type == DXT_DWORD
773 && !('0' <= words[w][0] && words[w][0] <= '9'))
784 if (IS_START(p, "loc_") || IS_START(p, "__imp")
785 || strchr(p, '?') || strchr(p, '@')
786 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
790 snprintf(g_comment, sizeof(g_comment), "%s", p);
793 pp = check_var(fhdr, sym, p);
795 fprintf(fout, "%s%s",
796 (no_decorations || p[0] == '_') ? "" : "_", p);
800 fprintf(fout, "%s", pp->name);
802 output_decorated_pp(fout, pp);
807 val64 = parse_number(words[w], 1);
809 fprintf(fout, "%d", (int)val64);
811 fprintf(fout, "0x%" PRIx64, val64);
818 if (g_comment[0] != 0) {
819 fprintf(fout, "\t\t%c %s", comment_char, g_comment);
829 for (i = 0; i < pub_sym_cnt; i++)
830 fprintf(fout, ".global %s%s\n",
831 no_decorations ? "" : "_", pub_syms[i]);
841 // vim:ts=2:shiftwidth=2:expandtab