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_compatible_func(pp_sym, pp)) {
310 pp_print(fp_sym, sizeof(fp_sym), pp_sym);
311 anote("entry: %s\n", fp_var);
312 anote("label: %s\n", fp_sym);
313 awarn("^ mismatch\n");
319 static void output_decorated_pp(FILE *fout,
320 const struct parsed_proto *pp)
322 if (pp->name[0] != '_')
323 fprintf(fout, pp->is_fastcall ? "@" : "_");
324 fprintf(fout, "%s", pp->name);
325 if (pp->is_stdcall && pp->argc > 0)
326 fprintf(fout, "@%d", pp->argc * 4);
329 static int align_value(int src_val)
332 awarn("bad align: %d\n", src_val);
338 return __builtin_ffs(src_val) - 1;
341 static int cmpstringp(const void *p1, const void *p2)
343 return strcmp(*(char * const *)p1, *(char * const *)p2);
346 /* XXX: maybe move to external file? */
347 static const char *unwanted_syms[] = {
373 static int is_unwanted_sym(const char *sym)
375 return bsearch(&sym, unwanted_syms, ARRAY_SIZE(unwanted_syms),
376 sizeof(unwanted_syms[0]), cmpstringp) != NULL;
379 int main(int argc, char *argv[])
381 FILE *fout, *fasm, *fhdr = NULL, *frlist;
382 const struct parsed_proto *pp;
383 int no_decorations = 0;
385 int maybe_func_table;
389 char comment_char = '#';
418 // -nd: no symbol decorations
419 printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> <hdrf> [rlist]*\n"
420 "%s -hdr <.h> <.asm>\n",
425 for (arg = 1; arg < argc; arg++) {
426 if (IS(argv[arg], "-nd"))
428 else if (IS(argv[arg], "-i"))
429 g_cconv_novalidate = 1;
430 else if (IS(argv[arg], "-a")) {
434 else if (IS(argv[arg], "-hdr"))
443 fasm = fopen(asmfn, "r");
444 my_assert_not(fasm, NULL);
448 fhdr = fopen(hdrfn, "r");
449 my_assert_not(fhdr, NULL);
452 fout = fopen(argv[arg_out], "w");
453 my_assert_not(fout, NULL);
456 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
457 my_assert_not(pub_syms, NULL);
460 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
461 my_assert_not(rlist, NULL);
463 for (; arg < argc; arg++) {
464 frlist = fopen(argv[arg], "r");
465 my_assert_not(frlist, NULL);
467 while (my_fgets(line, sizeof(line), frlist)) {
469 if (*p == 0 || *p == ';' || *p == '#')
472 p = next_word(words[0], sizeof(words[0]), p);
473 if (words[0][0] == 0)
476 if (rlist_cnt >= rlist_alloc) {
477 rlist_alloc = rlist_alloc * 2 + 64;
478 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
479 my_assert_not(rlist, NULL);
481 rlist[rlist_cnt++] = strdup(words[0]);
489 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
491 qsort(unwanted_syms, ARRAY_SIZE(unwanted_syms),
492 sizeof(unwanted_syms[0]), cmpstringp);
496 g_func_sym_pp = NULL;
497 maybe_func_table = 0;
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))
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 maybe_func_table = 0;
562 g_func_sym_pp = NULL;
566 if (wordc == 2 && IS(words[1], "ends"))
568 if (wordc <= 2 && IS(words[0], "end"))
571 aerr("unhandled: '%s'\n", words[0]);
574 if (IS(words[0], "assume"))
577 if (IS(words[0], "align")) {
581 val = parse_number(words[1], 0);
582 fprintf(fout, "\t\t .align %d", align_value(val));
586 if (IS(words[0], "public")) {
587 // skip, sym should appear in header anyway
592 type = parse_dx_directive(words[0]);
593 if (type == DXT_UNSPEC) {
594 type = parse_dx_directive(words[1]);
598 if (type == DXT_UNSPEC)
599 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
606 fprintf(fout, "extern ");
608 fprintf(fout, "const ");
612 for (i = w; i < wordc; i++)
613 if (words[i][0] == '\'')
616 fprintf(fout, "char %s[];\n", sym);
618 fprintf(fout, "uint8_t %s;\n", sym);
622 fprintf(fout, "uint16_t %s;\n", sym);
626 fprintf(fout, "uint32_t %s;\n", sym);
630 fprintf(fout, "_UNKNOWN %s;\n", sym);
637 snprintf(last_sym, sizeof(last_sym), "%s", sym);
638 maybe_func_table = type == DXT_DWORD;
640 if (IS_START(sym, "__IMPORT_DESCRIPTOR_")) {
642 maybe_func_table = 0;
645 pp = proto_parse(fhdr, sym, 1);
647 g_func_sym_pp = NULL;
649 // public/global name
650 if (pub_sym_cnt >= pub_sym_alloc) {
652 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
653 my_assert_not(pub_syms, NULL);
655 pub_syms[pub_sym_cnt++] = strdup(sym);
659 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
675 fprintf(fout, "\t\t ");
678 // fill out some unwanted strings with zeroes..
679 if (type == DXT_BYTE && words[w][0] == '\''
680 && is_unwanted_sym(last_sym))
683 for (; w < wordc; w++) {
684 if (words[w][0] == '\'') {
686 for (; *p && *p != '\''; p++)
690 // assume encoded byte
694 fprintf(fout, ".skip %d", len);
697 else if (type == DXT_BYTE
698 && (words[w][0] == '\''
699 || (w + 1 < wordc && words[w + 1][0] == '\'')))
701 // string; use asciz for most common case
702 if (w == wordc - 2 && IS(words[w + 1], "0")) {
703 fprintf(fout, ".asciz \"");
707 fprintf(fout, ".ascii \"");
709 for (; w < wordc; w++) {
710 if (words[w][0] == '\'') {
712 p2 = strchr(p, '\'');
714 aerr("unterminated string? '%s'\n", p);
715 memcpy(word, p, p2 - p);
717 fprintf(fout, "%s", escape_string(word));
720 val = parse_number(words[w], 0);
722 aerr("bad string trailing byte?\n");
723 // unfortunately \xHH is unusable - gas interprets
724 // things like \x27b as 0x7b, so have to use octal here
725 fprintf(fout, "\\%03lo", val);
732 if (w == wordc - 2) {
733 if (IS_START(words[w + 1], "dup(")) {
734 cnt = parse_number(words[w], 0);
735 p = words[w + 1] + 4;
739 memmove(word, p, p2 - p);
744 val = parse_number(word, 0);
746 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
747 cnt, type_size(type), val);
752 if (type == DXT_DWORD && words[w][0] == '\''
753 && words[w][5] == '\'' && strlen(words[w]) == 6)
759 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
760 fprintf(fout, ".long 0x%lx", val);
761 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
765 if (type >= DXT_DWORD && strchr(words[w], '.'))
770 if (g_arm_mode && type == DXT_TEN) {
771 fprintf(fout, ".fill 10");
772 snprintf(g_comment, sizeof(g_comment), "%s %s",
773 type_name_float(type), words[w]);
776 fprintf(fout, "%s %s", type_name_float(type), words[w]);
781 fprintf(fout, "%s ", type_name(type));
782 for (; w < wordc; w++)
787 is_label = is_bss = 0;
788 if (w <= wordc - 2 && IS(words[w], "offset")) {
792 else if (IS(words[w], "?")) {
795 else if (type == DXT_DWORD
796 && !('0' <= words[w][0] && words[w][0] <= '9'))
807 if (IS_START(p, "loc_") || IS_START(p, "__imp")
808 || strchr(p, '?') || strchr(p, '@')
809 || rm_labels_lines > 0
810 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
814 snprintf(g_comment, sizeof(g_comment), "%s", p);
817 const char *f_sym = maybe_func_table ? last_sym : NULL;
819 pp = check_var(fhdr, f_sym, p, in_export_table);
821 fprintf(fout, "%s%s",
822 (no_decorations || p[0] == '_') ? "" : "_", p);
826 fprintf(fout, "%s", pp->name);
828 output_decorated_pp(fout, pp);
833 val64 = parse_number(words[w], 1);
835 fprintf(fout, "%d", (int)val64);
837 fprintf(fout, "0x%" PRIx64, val64);
839 is_zero_val = val64 == 0;
847 maybe_func_table = 0;
849 if (rm_labels_lines > 0)
852 if (g_comment[0] != 0) {
853 fprintf(fout, "\t\t%c %s", comment_char, g_comment);
863 for (i = 0; i < pub_sym_cnt; i++)
864 fprintf(fout, ".global %s%s\n",
865 no_decorations ? "" : "_", pub_syms[i]);
875 // vim:ts=2:shiftwidth=2:expandtab