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, int is_export)
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);
257 if (!pp->is_func && !pp->is_fptr)
260 pp_print(fp_var, sizeof(fp_var), pp);
262 if (pp->argc_reg == 0)
264 if (pp->argc_reg == 1 && pp->argc_stack == 0
265 && IS(pp->arg[0].reg, "ecx"))
269 if (!g_cconv_novalidate
270 && (pp->argc_reg != 2
271 || !IS(pp->arg[0].reg, "ecx")
272 || !IS(pp->arg[1].reg, "edx")))
274 awarn("unhandled reg call: %s\n", fp_var);
278 // fptrs must use 32bit args, callsite might have no information and
279 // lack a cast to smaller types, which results in incorrectly masked
280 // args passed (callee may assume masked args, it does on ARM)
281 for (i = 0; i < pp->argc; i++) {
282 if (pp->arg[i].type.is_ptr)
284 p = pp->arg[i].type.name;
285 if (strstr(p, "int8") || strstr(p, "int16")
286 || strstr(p, "char") || strstr(p, "short"))
288 awarn("reference to %s with arg%d '%s'\n", pp->name, i + 1, p);
292 sprint_pp_short(pp, g_comment, sizeof(g_comment));
295 g_func_sym_pp = NULL;
296 pp_sym = proto_parse(fhdr, sym, 1);
299 if (!pp_sym->is_fptr)
300 aerr("func ptr data, but label '%s' !is_fptr\n", pp_sym->name);
301 g_func_sym_pp = pp_sym;
304 pp_sym = g_func_sym_pp;
309 if (pp_cmp_func(pp, pp_sym)) {
310 if (pp_sym->argc_stack == 0 && pp_sym->is_fastcall
311 && pp->argc_stack == 0
312 && (pp->is_fastcall || pp->argc_reg == 0)
313 && pp_sym->argc_reg > pp->argc_reg)
314 ; /* fascall compatible func doesn't use all args -> ok */
316 pp_print(fp_sym, sizeof(fp_sym), pp_sym);
317 anote("var: %s\n", fp_var);
318 anote("sym: %s\n", fp_sym);
319 awarn("^ mismatch\n");
326 static void output_decorated_pp(FILE *fout,
327 const struct parsed_proto *pp)
329 if (pp->name[0] != '_')
330 fprintf(fout, pp->is_fastcall ? "@" : "_");
331 fprintf(fout, "%s", pp->name);
332 if (pp->is_stdcall && pp->argc > 0)
333 fprintf(fout, "@%d", pp->argc * 4);
336 static int align_value(int src_val)
339 awarn("bad align: %d\n", src_val);
345 return __builtin_ffs(src_val) - 1;
348 static int cmpstringp(const void *p1, const void *p2)
350 return strcmp(*(char * const *)p1, *(char * const *)p2);
353 /* XXX: maybe move to external file? */
354 static const char *unwanted_syms[] = {
380 static int is_unwanted_sym(const char *sym)
382 return bsearch(&sym, unwanted_syms, ARRAY_SIZE(unwanted_syms),
383 sizeof(unwanted_syms[0]), cmpstringp) != NULL;
386 int main(int argc, char *argv[])
388 FILE *fout, *fasm, *fhdr = NULL, *frlist;
389 const struct parsed_proto *pp;
390 int no_decorations = 0;
391 int in_export_table = 0;
392 char comment_char = '#';
422 // -nd: no symbol decorations
423 printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> <hdrf> [rlist]*\n"
424 "%s -hdr <.h> <.asm>\n",
429 for (arg = 1; arg < argc; arg++) {
430 if (IS(argv[arg], "-nd"))
432 else if (IS(argv[arg], "-i"))
433 g_cconv_novalidate = 1;
434 else if (IS(argv[arg], "-a")) {
438 else if (IS(argv[arg], "-hdr"))
447 fasm = fopen(asmfn, "r");
448 my_assert_not(fasm, NULL);
452 fhdr = fopen(hdrfn, "r");
453 my_assert_not(fhdr, NULL);
456 fout = fopen(argv[arg_out], "w");
457 my_assert_not(fout, NULL);
460 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
461 my_assert_not(pub_syms, NULL);
464 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
465 my_assert_not(rlist, NULL);
467 for (; arg < argc; arg++) {
468 frlist = fopen(argv[arg], "r");
469 my_assert_not(frlist, NULL);
471 while (my_fgets(line, sizeof(line), frlist)) {
473 if (*p == 0 || *p == ';' || *p == '#')
476 p = next_word(words[0], sizeof(words[0]), p);
477 if (words[0][0] == 0)
480 if (rlist_cnt >= rlist_alloc) {
481 rlist_alloc = rlist_alloc * 2 + 64;
482 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
483 my_assert_not(rlist, NULL);
485 rlist[rlist_cnt++] = strdup(words[0]);
493 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
495 qsort(unwanted_syms, ARRAY_SIZE(unwanted_syms),
496 sizeof(unwanted_syms[0]), cmpstringp);
501 next_section(fasm, line);
504 if (IS(line + 1, "text"))
507 if (IS(line + 1, "rdata")) {
510 fprintf(fout, "\n.section .rodata\n");
512 else if (IS(line + 1, "data")) {
515 fprintf(fout, "\n.data\n");
518 aerr("unhandled section: '%s'\n", line);
521 fprintf(fout, ".align %d\n", align_value(4));
523 while (my_fgets(line, sizeof(line), fasm))
533 if (IS_START(p, ";org") && sscanf(p + 5, "%Xh", &i) == 1) {
534 // ;org is only seen at section start, so assume . addr 0
536 if (i != 0 && !header_mode)
537 fprintf(fout, "\t\t .skip 0x%x\n", i);
539 else if (IS_START(p, "; Export Address"))
541 else if (IS_START(p, "; Export"))
546 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
547 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
548 if (*p == 0 || *p == ';') {
559 if (IS_START(p, "sctclrtype"))
560 g_func_sym_pp = NULL;
563 if (wordc == 2 && IS(words[1], "ends"))
565 if (wordc <= 2 && IS(words[0], "end"))
568 aerr("unhandled: '%s'\n", words[0]);
571 if (IS(words[0], "assume"))
574 if (IS(words[0], "align")) {
578 val = parse_number(words[1], 0);
579 fprintf(fout, "\t\t .align %d", align_value(val));
583 if (IS(words[0], "public")) {
584 // skip, sym should appear in header anyway
589 type = parse_dx_directive(words[0]);
590 if (type == DXT_UNSPEC) {
591 type = parse_dx_directive(words[1]);
595 if (type == DXT_UNSPEC)
596 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
603 fprintf(fout, "extern ");
605 fprintf(fout, "const ");
609 for (i = w; i < wordc; i++)
610 if (words[i][0] == '\'')
613 fprintf(fout, "char %s[];\n", sym);
615 fprintf(fout, "uint8_t %s;\n", sym);
619 fprintf(fout, "uint16_t %s;\n", sym);
623 fprintf(fout, "uint32_t %s;\n", sym);
627 fprintf(fout, "_UNKNOWN %s;\n", sym);
634 snprintf(last_sym, sizeof(last_sym), "%s", sym);
636 pp = proto_parse(fhdr, sym, 1);
638 g_func_sym_pp = NULL;
640 // public/global name
641 if (pub_sym_cnt >= pub_sym_alloc) {
643 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
644 my_assert_not(pub_syms, NULL);
646 pub_syms[pub_sym_cnt++] = strdup(sym);
650 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
666 fprintf(fout, "\t\t ");
669 // fill out some unwanted strings with zeroes..
670 if (type == DXT_BYTE && words[w][0] == '\''
671 && is_unwanted_sym(last_sym))
674 for (; w < wordc; w++) {
675 if (words[w][0] == '\'') {
677 for (; *p && *p != '\''; p++)
681 // assume encoded byte
685 fprintf(fout, ".skip %d", len);
688 else if (type == DXT_BYTE
689 && (words[w][0] == '\''
690 || (w + 1 < wordc && words[w + 1][0] == '\'')))
692 // string; use asciz for most common case
693 if (w == wordc - 2 && IS(words[w + 1], "0")) {
694 fprintf(fout, ".asciz \"");
698 fprintf(fout, ".ascii \"");
700 for (; w < wordc; w++) {
701 if (words[w][0] == '\'') {
703 p2 = strchr(p, '\'');
705 aerr("unterminated string? '%s'\n", p);
706 memcpy(word, p, p2 - p);
708 fprintf(fout, "%s", escape_string(word));
711 val = parse_number(words[w], 0);
713 aerr("bad string trailing byte?\n");
714 // unfortunately \xHH is unusable - gas interprets
715 // things like \x27b as 0x7b, so have to use octal here
716 fprintf(fout, "\\%03lo", val);
723 if (w == wordc - 2) {
724 if (IS_START(words[w + 1], "dup(")) {
725 cnt = parse_number(words[w], 0);
726 p = words[w + 1] + 4;
730 memmove(word, p, p2 - p);
735 val = parse_number(word, 0);
737 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
738 cnt, type_size(type), val);
743 if (type == DXT_DWORD && words[w][0] == '\''
744 && words[w][5] == '\'' && strlen(words[w]) == 6)
750 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
751 fprintf(fout, ".long 0x%lx", val);
752 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
756 if (type >= DXT_DWORD && strchr(words[w], '.'))
761 if (g_arm_mode && type == DXT_TEN) {
762 fprintf(fout, ".fill 10");
763 snprintf(g_comment, sizeof(g_comment), "%s %s",
764 type_name_float(type), words[w]);
767 fprintf(fout, "%s %s", type_name_float(type), words[w]);
772 fprintf(fout, "%s ", type_name(type));
773 for (; w < wordc; w++)
778 is_label = is_bss = 0;
779 if (w <= wordc - 2 && IS(words[w], "offset")) {
783 else if (IS(words[w], "?")) {
786 else if (type == DXT_DWORD
787 && !('0' <= words[w][0] && words[w][0] <= '9'))
798 if (IS_START(p, "loc_") || IS_START(p, "__imp")
799 || strchr(p, '?') || strchr(p, '@')
800 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
804 snprintf(g_comment, sizeof(g_comment), "%s", p);
807 pp = check_var(fhdr, sym, p, in_export_table);
809 fprintf(fout, "%s%s",
810 (no_decorations || p[0] == '_') ? "" : "_", p);
814 fprintf(fout, "%s", pp->name);
816 output_decorated_pp(fout, pp);
821 val64 = parse_number(words[w], 1);
823 fprintf(fout, "%d", (int)val64);
825 fprintf(fout, "0x%" PRIx64, val64);
832 if (g_comment[0] != 0) {
833 fprintf(fout, "\t\t%c %s", comment_char, g_comment);
843 for (i = 0; i < pub_sym_cnt; i++)
844 fprintf(fout, ".global %s%s\n",
845 no_decorations ? "" : "_", pub_syms[i]);
855 // vim:ts=2:shiftwidth=2:expandtab