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 if (t - buf > sizeof(buf))
211 aerr("string is too long\n");
212 return strcpy(s, buf);
215 static void sprint_pp_short(const struct parsed_proto *pp, char *buf,
222 if (pp->ret_type.is_ptr)
224 else if (IS(pp->ret_type.name, "void"))
231 for (i = 0; i < pp->argc; i++) {
232 if (pp->arg[i].reg != NULL)
233 snprintf(buf + l, buf_size - l, "%s%s",
234 i == 0 ? "" : ",", pp->arg[i].reg);
236 snprintf(buf + l, buf_size - l, "%sa%d",
237 i == 0 ? "" : ",", i + 1);
240 snprintf(buf + l, buf_size - l, ")");
243 static const struct parsed_proto *check_var(FILE *fhdr,
244 const char *sym, const char *varname)
246 const struct parsed_proto *pp, *pp_sym;
247 char fp_sym[256], fp_var[256], *p;
250 pp = proto_parse(fhdr, varname, 1);
252 if (IS_START(varname, "sub_"))
253 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 pp_print(fp_sym, sizeof(fp_sym), pp_sym);
311 anote("var: %s\n", fp_var);
312 anote("sym: %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;
384 char comment_char = '#';
413 // -nd: no symbol decorations
414 printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> <hdrf> [rlist]*\n"
415 "%s -hdr <.h> <.asm>\n",
420 for (arg = 1; arg < argc; arg++) {
421 if (IS(argv[arg], "-nd"))
423 else if (IS(argv[arg], "-i"))
424 g_cconv_novalidate = 1;
425 else if (IS(argv[arg], "-a")) {
429 else if (IS(argv[arg], "-hdr"))
438 fasm = fopen(asmfn, "r");
439 my_assert_not(fasm, NULL);
443 fhdr = fopen(hdrfn, "r");
444 my_assert_not(fhdr, NULL);
447 fout = fopen(argv[arg_out], "w");
448 my_assert_not(fout, NULL);
451 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
452 my_assert_not(pub_syms, NULL);
455 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
456 my_assert_not(rlist, NULL);
458 for (; arg < argc; arg++) {
459 frlist = fopen(argv[arg], "r");
460 my_assert_not(frlist, NULL);
462 while (my_fgets(line, sizeof(line), frlist)) {
464 if (*p == 0 || *p == ';')
467 p = next_word(words[0], sizeof(words[0]), p);
468 if (words[0][0] == 0)
471 if (rlist_cnt >= rlist_alloc) {
472 rlist_alloc = rlist_alloc * 2 + 64;
473 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
474 my_assert_not(rlist, NULL);
476 rlist[rlist_cnt++] = strdup(words[0]);
484 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
486 qsort(unwanted_syms, ARRAY_SIZE(unwanted_syms),
487 sizeof(unwanted_syms[0]), cmpstringp);
492 next_section(fasm, line);
495 if (IS(line + 1, "text"))
498 if (IS(line + 1, "rdata")) {
501 fprintf(fout, "\n.section .rodata\n");
503 else if (IS(line + 1, "data")) {
506 fprintf(fout, "\n.data\n");
509 aerr("unhandled section: '%s'\n", line);
512 fprintf(fout, ".align %d\n", align_value(4));
514 while (my_fgets(line, sizeof(line), fasm))
524 if (IS_START(p, ";org") && sscanf(p + 5, "%Xh", &i) == 1) {
525 // ;org is only seen at section start, so assume . addr 0
527 if (i != 0 && !header_mode)
528 fprintf(fout, "\t\t .skip 0x%x\n", i);
533 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
534 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
535 if (*p == 0 || *p == ';') {
546 if (IS_START(p, "sctclrtype"))
547 g_func_sym_pp = NULL;
550 if (wordc == 2 && IS(words[1], "ends"))
552 if (wordc <= 2 && IS(words[0], "end"))
555 aerr("unhandled: '%s'\n", words[0]);
558 if (IS(words[0], "assume"))
561 if (IS(words[0], "align")) {
565 val = parse_number(words[1]);
566 fprintf(fout, "\t\t .align %d", align_value(val));
571 type = parse_dx_directive(words[0]);
572 if (type == DXT_UNSPEC) {
573 type = parse_dx_directive(words[1]);
577 if (type == DXT_UNSPEC)
578 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
585 fprintf(fout, "extern ");
587 fprintf(fout, "const ");
591 for (i = w; i < wordc; i++)
592 if (words[i][0] == '\'')
595 fprintf(fout, "char %s[];\n", sym);
597 fprintf(fout, "uint8_t %s;\n", sym);
601 fprintf(fout, "uint16_t %s;\n", sym);
605 fprintf(fout, "uint32_t %s;\n", sym);
609 fprintf(fout, "_UNKNOWN %s;\n", sym);
616 snprintf(last_sym, sizeof(last_sym), "%s", sym);
618 pp = proto_parse(fhdr, sym, 1);
620 g_func_sym_pp = NULL;
622 // public/global name
623 if (pub_sym_cnt >= pub_sym_alloc) {
625 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
626 my_assert_not(pub_syms, NULL);
628 pub_syms[pub_sym_cnt++] = strdup(sym);
632 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
648 fprintf(fout, "\t\t ");
651 // fill out some unwanted strings with zeroes..
652 if (type == DXT_BYTE && words[w][0] == '\''
653 && is_unwanted_sym(last_sym))
656 for (; w < wordc; w++) {
657 if (words[w][0] == '\'') {
659 for (; *p && *p != '\''; p++)
663 // assume encoded byte
667 fprintf(fout, ".skip %d", len);
670 else if (type == DXT_BYTE
671 && (words[w][0] == '\''
672 || (w + 1 < wordc && words[w + 1][0] == '\'')))
674 // string; use asciz for most common case
675 if (w == wordc - 2 && IS(words[w + 1], "0")) {
676 fprintf(fout, ".asciz \"");
680 fprintf(fout, ".ascii \"");
682 for (; w < wordc; w++) {
683 if (words[w][0] == '\'') {
685 p2 = strchr(p, '\'');
687 aerr("unterminated string? '%s'\n", p);
688 memcpy(word, p, p2 - p);
690 fprintf(fout, "%s", escape_string(word));
693 val = parse_number(words[w]);
695 aerr("bad string trailing byte?\n");
696 // unfortunately \xHH is unusable - gas interprets
697 // things like \x27b as 0x7b, so have to use octal here
698 fprintf(fout, "\\%03lo", val);
705 if (w == wordc - 2) {
706 if (IS_START(words[w + 1], "dup(")) {
707 cnt = parse_number(words[w]);
708 p = words[w + 1] + 4;
712 memmove(word, p, p2 - p);
717 val = parse_number(word);
719 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
720 cnt, type_size(type), val);
725 if (type == DXT_DWORD && words[w][0] == '\''
726 && words[w][5] == '\'' && strlen(words[w]) == 6)
732 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
733 fprintf(fout, ".long 0x%lx", val);
734 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
738 if (type >= DXT_DWORD && strchr(words[w], '.'))
743 if (g_arm_mode && type == DXT_TEN) {
744 fprintf(fout, ".fill 10");
745 snprintf(g_comment, sizeof(g_comment), "%s %s",
746 type_name_float(type), words[w]);
749 fprintf(fout, "%s %s", type_name_float(type), words[w]);
754 fprintf(fout, "%s ", type_name(type));
755 for (; w < wordc; w++)
760 is_label = is_bss = 0;
761 if (w <= wordc - 2 && IS(words[w], "offset")) {
765 else if (IS(words[w], "?")) {
768 else if (type == DXT_DWORD
769 && !('0' <= words[w][0] && words[w][0] <= '9'))
780 if (IS_START(p, "loc_") || IS_START(p, "__imp")
781 || strchr(p, '?') || strchr(p, '@')
782 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
786 snprintf(g_comment, sizeof(g_comment), "%s", p);
789 pp = check_var(fhdr, sym, p);
791 fprintf(fout, "%s%s",
792 (no_decorations || p[0] == '_') ? "" : "_", p);
796 fprintf(fout, "%s", pp->name);
798 output_decorated_pp(fout, pp);
803 val = parse_number(words[w]);
805 fprintf(fout, "%ld", val);
807 fprintf(fout, "0x%lx", val);
814 if (g_comment[0] != 0) {
815 fprintf(fout, "\t\t%c %s", comment_char, g_comment);
825 for (i = 0; i < pub_sym_cnt; i++)
826 fprintf(fout, ".global %s%s\n",
827 no_decorations ? "" : "_", pub_syms[i]);
837 // vim:ts=2:shiftwidth=2:expandtab