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 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
21 #define IS(w, y) !strcmp(w, y)
22 #define IS_START(w, y) !strncmp(w, y, strlen(y))
24 #include "protoparse.h"
26 static const char *asmfn;
29 static const struct parsed_proto *g_func_sym_pp;
30 static char g_comment[256];
31 static int g_warn_cnt;
32 static int g_cconv_novalidate;
33 static int g_arm_mode;
35 // note: must be in ascending order
45 #define anote(fmt, ...) \
46 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
47 #define awarn(fmt, ...) do { \
48 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__); \
49 if (++g_warn_cnt == 10) { \
54 #define aerr(fmt, ...) do { \
55 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
60 #include "masm_tools.h"
62 static char *next_word_s(char *w, size_t wsize, char *s)
69 for (i = 0; i < wsize - 1; i++) {
72 if (s[i] == 0 || (!quote && (my_isblank(s[i]) || s[i] == ',')))
78 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != ',')
79 printf("warning: '%s' truncated\n", w);
84 static void next_section(FILE *fasm, char *name)
93 while (my_fgets(line, sizeof(line), fasm))
105 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
106 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
107 if (*p == 0 || *p == ';') {
116 if (!IS(words[1], "segment"))
119 strcpy(name, words[0]);
124 static enum dx_type parse_dx_directive(const char *name)
140 static const char *type_name(enum dx_type type)
159 static const char *type_name_float(enum dx_type type)
171 return "<bad_float>";
174 static int type_size(enum dx_type type)
193 static char *escape_string(char *s)
198 for (; *s != 0; s++) {
212 if (t - buf > sizeof(buf))
213 aerr("string is too long\n");
214 return strcpy(s, buf);
217 static void sprint_pp_short(const struct parsed_proto *pp, char *buf,
224 if (pp->ret_type.is_ptr)
226 else if (IS(pp->ret_type.name, "void"))
233 for (i = 0; i < pp->argc; i++) {
234 if (pp->arg[i].reg != NULL)
235 snprintf(buf + l, buf_size - l, "%s%s",
236 i == 0 ? "" : ",", pp->arg[i].reg);
238 snprintf(buf + l, buf_size - l, "%sa%d",
239 i == 0 ? "" : ",", i + 1);
242 snprintf(buf + l, buf_size - l, ")");
245 static const struct parsed_proto *check_var(FILE *fhdr,
246 const char *sym, const char *varname)
248 const struct parsed_proto *pp, *pp_sym;
249 char fp_sym[256], fp_var[256], *p;
252 pp = proto_parse(fhdr, varname, 1);
254 if (IS_START(varname, "sub_"))
255 awarn("sub_ sym missing proto: '%s'\n", varname);
259 if (!pp->is_func && !pp->is_fptr)
262 pp_print(fp_var, sizeof(fp_var), pp);
264 if (pp->argc_reg == 0)
266 if (pp->argc_reg == 1 && pp->argc_stack == 0
267 && IS(pp->arg[0].reg, "ecx"))
271 if (!g_cconv_novalidate
272 && (pp->argc_reg != 2
273 || !IS(pp->arg[0].reg, "ecx")
274 || !IS(pp->arg[1].reg, "edx")))
276 awarn("unhandled reg call: %s\n", fp_var);
280 // fptrs must use 32bit args, callsite might have no information and
281 // lack a cast to smaller types, which results in incorrectly masked
282 // args passed (callee may assume masked args, it does on ARM)
283 for (i = 0; i < pp->argc; i++) {
284 if (pp->arg[i].type.is_ptr)
286 p = pp->arg[i].type.name;
287 if (strstr(p, "int8") || strstr(p, "int16")
288 || strstr(p, "char") || strstr(p, "short"))
290 awarn("reference to %s with arg%d '%s'\n", pp->name, i + 1, p);
294 sprint_pp_short(pp, g_comment, sizeof(g_comment));
297 g_func_sym_pp = NULL;
298 pp_sym = proto_parse(fhdr, sym, 1);
301 if (!pp_sym->is_fptr)
302 aerr("func ptr data, but label '%s' !is_fptr\n", pp_sym->name);
303 g_func_sym_pp = pp_sym;
306 pp_sym = g_func_sym_pp;
311 if (pp_cmp_func(pp, pp_sym)) {
312 pp_print(fp_sym, sizeof(fp_sym), pp_sym);
313 anote("var: %s\n", fp_var);
314 anote("sym: %s\n", fp_sym);
315 awarn("^ mismatch\n");
321 static void output_decorated_pp(FILE *fout,
322 const struct parsed_proto *pp)
324 if (pp->name[0] != '_')
325 fprintf(fout, pp->is_fastcall ? "@" : "_");
326 fprintf(fout, "%s", pp->name);
327 if (pp->is_stdcall && pp->argc > 0)
328 fprintf(fout, "@%d", pp->argc * 4);
331 static int align_value(int src_val)
334 awarn("bad align: %d\n", src_val);
340 return __builtin_ffs(src_val) - 1;
343 static int cmpstringp(const void *p1, const void *p2)
345 return strcmp(*(char * const *)p1, *(char * const *)p2);
348 /* XXX: maybe move to external file? */
349 static const char *unwanted_syms[] = {
375 static int is_unwanted_sym(const char *sym)
377 return bsearch(&sym, unwanted_syms, ARRAY_SIZE(unwanted_syms),
378 sizeof(unwanted_syms[0]), cmpstringp) != NULL;
381 int main(int argc, char *argv[])
383 FILE *fout, *fasm, *fhdr = NULL, *frlist;
384 const struct parsed_proto *pp;
385 int no_decorations = 0;
386 char comment_char = '#';
416 // -nd: no symbol decorations
417 printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> <hdrf> [rlist]*\n"
418 "%s -hdr <.h> <.asm>\n",
423 for (arg = 1; arg < argc; arg++) {
424 if (IS(argv[arg], "-nd"))
426 else if (IS(argv[arg], "-i"))
427 g_cconv_novalidate = 1;
428 else if (IS(argv[arg], "-a")) {
432 else if (IS(argv[arg], "-hdr"))
441 fasm = fopen(asmfn, "r");
442 my_assert_not(fasm, NULL);
446 fhdr = fopen(hdrfn, "r");
447 my_assert_not(fhdr, NULL);
450 fout = fopen(argv[arg_out], "w");
451 my_assert_not(fout, NULL);
454 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
455 my_assert_not(pub_syms, NULL);
458 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
459 my_assert_not(rlist, NULL);
461 for (; arg < argc; arg++) {
462 frlist = fopen(argv[arg], "r");
463 my_assert_not(frlist, NULL);
465 while (my_fgets(line, sizeof(line), frlist)) {
467 if (*p == 0 || *p == ';')
470 p = next_word(words[0], sizeof(words[0]), p);
471 if (words[0][0] == 0)
474 if (rlist_cnt >= rlist_alloc) {
475 rlist_alloc = rlist_alloc * 2 + 64;
476 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
477 my_assert_not(rlist, NULL);
479 rlist[rlist_cnt++] = strdup(words[0]);
487 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
489 qsort(unwanted_syms, ARRAY_SIZE(unwanted_syms),
490 sizeof(unwanted_syms[0]), cmpstringp);
495 next_section(fasm, line);
498 if (IS(line + 1, "text"))
501 if (IS(line + 1, "rdata")) {
504 fprintf(fout, "\n.section .rodata\n");
506 else if (IS(line + 1, "data")) {
509 fprintf(fout, "\n.data\n");
512 aerr("unhandled section: '%s'\n", line);
515 fprintf(fout, ".align %d\n", align_value(4));
517 while (my_fgets(line, sizeof(line), fasm))
527 if (IS_START(p, ";org") && sscanf(p + 5, "%Xh", &i) == 1) {
528 // ;org is only seen at section start, so assume . addr 0
530 if (i != 0 && !header_mode)
531 fprintf(fout, "\t\t .skip 0x%x\n", i);
536 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
537 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
538 if (*p == 0 || *p == ';') {
549 if (IS_START(p, "sctclrtype"))
550 g_func_sym_pp = NULL;
553 if (wordc == 2 && IS(words[1], "ends"))
555 if (wordc <= 2 && IS(words[0], "end"))
558 aerr("unhandled: '%s'\n", words[0]);
561 if (IS(words[0], "assume"))
564 if (IS(words[0], "align")) {
568 val = parse_number(words[1], 0);
569 fprintf(fout, "\t\t .align %d", align_value(val));
574 type = parse_dx_directive(words[0]);
575 if (type == DXT_UNSPEC) {
576 type = parse_dx_directive(words[1]);
580 if (type == DXT_UNSPEC)
581 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
588 fprintf(fout, "extern ");
590 fprintf(fout, "const ");
594 for (i = w; i < wordc; i++)
595 if (words[i][0] == '\'')
598 fprintf(fout, "char %s[];\n", sym);
600 fprintf(fout, "uint8_t %s;\n", sym);
604 fprintf(fout, "uint16_t %s;\n", sym);
608 fprintf(fout, "uint32_t %s;\n", sym);
612 fprintf(fout, "_UNKNOWN %s;\n", sym);
619 snprintf(last_sym, sizeof(last_sym), "%s", sym);
621 pp = proto_parse(fhdr, sym, 1);
623 g_func_sym_pp = NULL;
625 // public/global name
626 if (pub_sym_cnt >= pub_sym_alloc) {
628 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
629 my_assert_not(pub_syms, NULL);
631 pub_syms[pub_sym_cnt++] = strdup(sym);
635 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
651 fprintf(fout, "\t\t ");
654 // fill out some unwanted strings with zeroes..
655 if (type == DXT_BYTE && words[w][0] == '\''
656 && is_unwanted_sym(last_sym))
659 for (; w < wordc; w++) {
660 if (words[w][0] == '\'') {
662 for (; *p && *p != '\''; p++)
666 // assume encoded byte
670 fprintf(fout, ".skip %d", len);
673 else if (type == DXT_BYTE
674 && (words[w][0] == '\''
675 || (w + 1 < wordc && words[w + 1][0] == '\'')))
677 // string; use asciz for most common case
678 if (w == wordc - 2 && IS(words[w + 1], "0")) {
679 fprintf(fout, ".asciz \"");
683 fprintf(fout, ".ascii \"");
685 for (; w < wordc; w++) {
686 if (words[w][0] == '\'') {
688 p2 = strchr(p, '\'');
690 aerr("unterminated string? '%s'\n", p);
691 memcpy(word, p, p2 - p);
693 fprintf(fout, "%s", escape_string(word));
696 val = parse_number(words[w], 0);
698 aerr("bad string trailing byte?\n");
699 // unfortunately \xHH is unusable - gas interprets
700 // things like \x27b as 0x7b, so have to use octal here
701 fprintf(fout, "\\%03lo", val);
708 if (w == wordc - 2) {
709 if (IS_START(words[w + 1], "dup(")) {
710 cnt = parse_number(words[w], 0);
711 p = words[w + 1] + 4;
715 memmove(word, p, p2 - p);
720 val = parse_number(word, 0);
722 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
723 cnt, type_size(type), val);
728 if (type == DXT_DWORD && words[w][0] == '\''
729 && words[w][5] == '\'' && strlen(words[w]) == 6)
735 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
736 fprintf(fout, ".long 0x%lx", val);
737 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
741 if (type >= DXT_DWORD && strchr(words[w], '.'))
746 if (g_arm_mode && type == DXT_TEN) {
747 fprintf(fout, ".fill 10");
748 snprintf(g_comment, sizeof(g_comment), "%s %s",
749 type_name_float(type), words[w]);
752 fprintf(fout, "%s %s", type_name_float(type), words[w]);
757 fprintf(fout, "%s ", type_name(type));
758 for (; w < wordc; w++)
763 is_label = is_bss = 0;
764 if (w <= wordc - 2 && IS(words[w], "offset")) {
768 else if (IS(words[w], "?")) {
771 else if (type == DXT_DWORD
772 && !('0' <= words[w][0] && words[w][0] <= '9'))
783 if (IS_START(p, "loc_") || IS_START(p, "__imp")
784 || strchr(p, '?') || strchr(p, '@')
785 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
789 snprintf(g_comment, sizeof(g_comment), "%s", p);
792 pp = check_var(fhdr, sym, p);
794 fprintf(fout, "%s%s",
795 (no_decorations || p[0] == '_') ? "" : "_", p);
799 fprintf(fout, "%s", pp->name);
801 output_decorated_pp(fout, pp);
806 val64 = parse_number(words[w], 1);
808 fprintf(fout, "%d", (int)val64);
810 fprintf(fout, "0x%" PRIx64, val64);
817 if (g_comment[0] != 0) {
818 fprintf(fout, "\t\t%c %s", comment_char, g_comment);
828 for (i = 0; i < pub_sym_cnt; i++)
829 fprintf(fout, ".global %s%s\n",
830 no_decorations ? "" : "_", pub_syms[i]);
840 // vim:ts=2:shiftwidth=2:expandtab