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 int rm_labels_lines = 0;
393 char comment_char = '#';
423 // -nd: no symbol decorations
424 printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> <hdrf> [rlist]*\n"
425 "%s -hdr <.h> <.asm>\n",
430 for (arg = 1; arg < argc; arg++) {
431 if (IS(argv[arg], "-nd"))
433 else if (IS(argv[arg], "-i"))
434 g_cconv_novalidate = 1;
435 else if (IS(argv[arg], "-a")) {
439 else if (IS(argv[arg], "-hdr"))
448 fasm = fopen(asmfn, "r");
449 my_assert_not(fasm, NULL);
453 fhdr = fopen(hdrfn, "r");
454 my_assert_not(fhdr, NULL);
457 fout = fopen(argv[arg_out], "w");
458 my_assert_not(fout, NULL);
461 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
462 my_assert_not(pub_syms, NULL);
465 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
466 my_assert_not(rlist, NULL);
468 for (; arg < argc; arg++) {
469 frlist = fopen(argv[arg], "r");
470 my_assert_not(frlist, NULL);
472 while (my_fgets(line, sizeof(line), frlist)) {
474 if (*p == 0 || *p == ';' || *p == '#')
477 p = next_word(words[0], sizeof(words[0]), p);
478 if (words[0][0] == 0)
481 if (rlist_cnt >= rlist_alloc) {
482 rlist_alloc = rlist_alloc * 2 + 64;
483 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
484 my_assert_not(rlist, NULL);
486 rlist[rlist_cnt++] = strdup(words[0]);
494 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
496 qsort(unwanted_syms, ARRAY_SIZE(unwanted_syms),
497 sizeof(unwanted_syms[0]), cmpstringp);
502 next_section(fasm, line);
505 if (IS(line + 1, "text"))
508 if (IS(line + 1, "rdata")) {
511 fprintf(fout, "\n.section .rodata\n");
513 else if (IS(line + 1, "data")) {
516 fprintf(fout, "\n.data\n");
519 aerr("unhandled section: '%s'\n", line);
522 fprintf(fout, ".align %d\n", align_value(4));
524 while (my_fgets(line, sizeof(line), fasm))
534 if (IS_START(p, ";org") && sscanf(p + 5, "%Xh", &i) == 1) {
535 // ;org is only seen at section start, so assume . addr 0
537 if (i != 0 && !header_mode)
538 fprintf(fout, "\t\t .skip 0x%x\n", i);
540 else if (IS_START(p, "; Export Address"))
542 else if (IS_START(p, "; Export"))
547 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
548 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
549 if (*p == 0 || *p == ';') {
560 if (IS_START(p, "sctclrtype"))
561 g_func_sym_pp = NULL;
564 if (wordc == 2 && IS(words[1], "ends"))
566 if (wordc <= 2 && IS(words[0], "end"))
569 aerr("unhandled: '%s'\n", words[0]);
572 if (IS(words[0], "assume"))
575 if (IS(words[0], "align")) {
579 val = parse_number(words[1], 0);
580 fprintf(fout, "\t\t .align %d", align_value(val));
584 if (IS(words[0], "public")) {
585 // skip, sym should appear in header anyway
590 type = parse_dx_directive(words[0]);
591 if (type == DXT_UNSPEC) {
592 type = parse_dx_directive(words[1]);
596 if (type == DXT_UNSPEC)
597 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
604 fprintf(fout, "extern ");
606 fprintf(fout, "const ");
610 for (i = w; i < wordc; i++)
611 if (words[i][0] == '\'')
614 fprintf(fout, "char %s[];\n", sym);
616 fprintf(fout, "uint8_t %s;\n", sym);
620 fprintf(fout, "uint16_t %s;\n", sym);
624 fprintf(fout, "uint32_t %s;\n", sym);
628 fprintf(fout, "_UNKNOWN %s;\n", sym);
635 snprintf(last_sym, sizeof(last_sym), "%s", sym);
636 if (IS_START(sym, "__IMPORT_DESCRIPTOR_"))
639 pp = proto_parse(fhdr, sym, 1);
641 g_func_sym_pp = NULL;
643 // public/global name
644 if (pub_sym_cnt >= pub_sym_alloc) {
646 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
647 my_assert_not(pub_syms, NULL);
649 pub_syms[pub_sym_cnt++] = strdup(sym);
653 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
669 fprintf(fout, "\t\t ");
672 // fill out some unwanted strings with zeroes..
673 if (type == DXT_BYTE && words[w][0] == '\''
674 && is_unwanted_sym(last_sym))
677 for (; w < wordc; w++) {
678 if (words[w][0] == '\'') {
680 for (; *p && *p != '\''; p++)
684 // assume encoded byte
688 fprintf(fout, ".skip %d", len);
691 else if (type == DXT_BYTE
692 && (words[w][0] == '\''
693 || (w + 1 < wordc && words[w + 1][0] == '\'')))
695 // string; use asciz for most common case
696 if (w == wordc - 2 && IS(words[w + 1], "0")) {
697 fprintf(fout, ".asciz \"");
701 fprintf(fout, ".ascii \"");
703 for (; w < wordc; w++) {
704 if (words[w][0] == '\'') {
706 p2 = strchr(p, '\'');
708 aerr("unterminated string? '%s'\n", p);
709 memcpy(word, p, p2 - p);
711 fprintf(fout, "%s", escape_string(word));
714 val = parse_number(words[w], 0);
716 aerr("bad string trailing byte?\n");
717 // unfortunately \xHH is unusable - gas interprets
718 // things like \x27b as 0x7b, so have to use octal here
719 fprintf(fout, "\\%03lo", val);
726 if (w == wordc - 2) {
727 if (IS_START(words[w + 1], "dup(")) {
728 cnt = parse_number(words[w], 0);
729 p = words[w + 1] + 4;
733 memmove(word, p, p2 - p);
738 val = parse_number(word, 0);
740 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
741 cnt, type_size(type), val);
746 if (type == DXT_DWORD && words[w][0] == '\''
747 && words[w][5] == '\'' && strlen(words[w]) == 6)
753 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
754 fprintf(fout, ".long 0x%lx", val);
755 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
759 if (type >= DXT_DWORD && strchr(words[w], '.'))
764 if (g_arm_mode && type == DXT_TEN) {
765 fprintf(fout, ".fill 10");
766 snprintf(g_comment, sizeof(g_comment), "%s %s",
767 type_name_float(type), words[w]);
770 fprintf(fout, "%s %s", type_name_float(type), words[w]);
775 fprintf(fout, "%s ", type_name(type));
776 for (; w < wordc; w++)
781 is_label = is_bss = 0;
782 if (w <= wordc - 2 && IS(words[w], "offset")) {
786 else if (IS(words[w], "?")) {
789 else if (type == DXT_DWORD
790 && !('0' <= words[w][0] && words[w][0] <= '9'))
801 if (IS_START(p, "loc_") || IS_START(p, "__imp")
802 || strchr(p, '?') || strchr(p, '@')
803 || rm_labels_lines > 0
804 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
808 snprintf(g_comment, sizeof(g_comment), "%s", p);
811 pp = check_var(fhdr, sym, p, in_export_table);
813 fprintf(fout, "%s%s",
814 (no_decorations || p[0] == '_') ? "" : "_", p);
818 fprintf(fout, "%s", pp->name);
820 output_decorated_pp(fout, pp);
825 val64 = parse_number(words[w], 1);
827 fprintf(fout, "%d", (int)val64);
829 fprintf(fout, "0x%" PRIx64, val64);
836 if (rm_labels_lines > 0)
839 if (g_comment[0] != 0) {
840 fprintf(fout, "\t\t%c %s", comment_char, g_comment);
850 for (i = 0; i < pub_sym_cnt; i++)
851 fprintf(fout, ".global %s%s\n",
852 no_decorations ? "" : "_", pub_syms[i]);
862 // vim:ts=2:shiftwidth=2:expandtab