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"
17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
18 #define IS(w, y) !strcmp(w, y)
19 #define IS_START(w, y) !strncmp(w, y, strlen(y))
21 #include "protoparse.h"
23 static const char *asmfn;
26 static const struct parsed_proto *g_func_sym_pp;
27 static char g_comment[256];
28 static int g_warn_cnt;
29 static int g_cconv_novalidate;
30 static int g_arm_mode;
32 // note: must be in ascending order
42 #define anote(fmt, ...) \
43 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
44 #define awarn(fmt, ...) do { \
45 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__); \
46 if (++g_warn_cnt == 10) { \
51 #define aerr(fmt, ...) do { \
52 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
57 #include "masm_tools.h"
59 static char *next_word_s(char *w, size_t wsize, char *s)
66 for (i = 0; i < wsize - 1; i++) {
69 if (s[i] == 0 || (!quote && (my_isblank(s[i]) || s[i] == ',')))
75 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != ',')
76 printf("warning: '%s' truncated\n", w);
81 static void next_section(FILE *fasm, char *name)
90 while (fgets(line, sizeof(line), fasm))
100 while (strlen(line) == sizeof(line) - 1) {
101 // one of those long comment lines..
102 if (!fgets(line, sizeof(line), fasm))
108 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
109 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
110 if (*p == 0 || *p == ';') {
119 if (!IS(words[1], "segment"))
122 strcpy(name, words[0]);
127 static enum dx_type parse_dx_directive(const char *name)
143 static const char *type_name(enum dx_type type)
162 static const char *type_name_float(enum dx_type type)
174 return "<bad_float>";
177 static int type_size(enum dx_type type)
196 static char *escape_string(char *s)
201 for (; *s != 0; s++) {
215 return strcpy(s, buf);
218 static void sprint_pp_short(const struct parsed_proto *pp, char *buf,
225 if (pp->ret_type.is_ptr)
227 else if (IS(pp->ret_type.name, "void"))
234 for (i = 0; i < pp->argc; i++) {
235 if (pp->arg[i].reg != NULL)
236 snprintf(buf + l, buf_size - l, "%s%s",
237 i == 0 ? "" : ",", pp->arg[i].reg);
239 snprintf(buf + l, buf_size - l, "%sa%d",
240 i == 0 ? "" : ",", i + 1);
243 snprintf(buf + l, buf_size - l, ")");
246 static const struct parsed_proto *check_var(FILE *fhdr,
247 const char *sym, const char *varname)
249 const struct parsed_proto *pp, *pp_sym;
250 char fp_sym[256], fp_var[256], *p;
253 pp = proto_parse(fhdr, varname, 1);
255 if (IS_START(varname, "sub_"))
256 awarn("sub_ sym missing proto: '%s'\n", varname);
260 if (!pp->is_func && !pp->is_fptr)
263 pp_print(fp_var, sizeof(fp_var), pp);
265 if (pp->argc_reg == 0)
267 if (pp->argc_reg == 1 && pp->argc_stack == 0
268 && IS(pp->arg[0].reg, "ecx"))
272 if (!g_cconv_novalidate
273 && (pp->argc_reg != 2
274 || !IS(pp->arg[0].reg, "ecx")
275 || !IS(pp->arg[1].reg, "edx")))
277 awarn("unhandled reg call: %s\n", fp_var);
281 // fptrs must use 32bit args, callsite might have no information and
282 // lack a cast to smaller types, which results in incorrectly masked
283 // args passed (callee may assume masked args, it does on ARM)
284 for (i = 0; i < pp->argc; i++) {
285 if (pp->arg[i].type.is_ptr)
287 p = pp->arg[i].type.name;
288 if (strstr(p, "int8") || strstr(p, "int16")
289 || strstr(p, "char") || strstr(p, "short"))
291 awarn("reference to %s with arg%d '%s'\n", pp->name, i + 1, p);
295 sprint_pp_short(pp, g_comment, sizeof(g_comment));
298 g_func_sym_pp = NULL;
299 pp_sym = proto_parse(fhdr, sym, 1);
302 if (!pp_sym->is_fptr)
303 aerr("func ptr data, but label '%s' !is_fptr\n", pp_sym->name);
304 g_func_sym_pp = pp_sym;
307 pp_sym = g_func_sym_pp;
312 if (pp_cmp_func(pp, pp_sym)) {
313 pp_print(fp_sym, sizeof(fp_sym), pp_sym);
314 anote("var: %s\n", fp_var);
315 anote("sym: %s\n", fp_sym);
316 awarn("^ mismatch\n");
322 static void output_decorated_pp(FILE *fout,
323 const struct parsed_proto *pp)
325 if (pp->name[0] != '_')
326 fprintf(fout, pp->is_fastcall ? "@" : "_");
327 fprintf(fout, "%s", pp->name);
328 if (pp->is_stdcall && pp->argc > 0)
329 fprintf(fout, "@%d", pp->argc * 4);
332 static int align_value(int src_val)
335 awarn("bad align: %d\n", src_val);
341 return __builtin_ffs(src_val) - 1;
344 static int cmpstringp(const void *p1, const void *p2)
346 return strcmp(*(char * const *)p1, *(char * const *)p2);
349 /* XXX: maybe move to external file? */
350 static const char *unwanted_syms[] = {
376 static int is_unwanted_sym(const char *sym)
378 return bsearch(&sym, unwanted_syms, ARRAY_SIZE(unwanted_syms),
379 sizeof(unwanted_syms[0]), cmpstringp) != NULL;
382 int main(int argc, char *argv[])
384 FILE *fout, *fasm, *fhdr, *frlist;
385 const struct parsed_proto *pp;
386 int no_decorations = 0;
387 char comment_char = '#';
414 // -nd: no symbol decorations
415 printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> <hdrf> [rlist]*\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")) {
436 fasm = fopen(asmfn, "r");
437 my_assert_not(fasm, NULL);
440 fhdr = fopen(hdrfn, "r");
441 my_assert_not(fhdr, NULL);
443 fout = fopen(argv[arg_out], "w");
444 my_assert_not(fout, NULL);
447 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
448 my_assert_not(pub_syms, NULL);
451 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
452 my_assert_not(rlist, NULL);
454 for (; arg < argc; arg++) {
455 frlist = fopen(argv[arg], "r");
456 my_assert_not(frlist, NULL);
458 while (fgets(line, sizeof(line), frlist)) {
460 if (*p == 0 || *p == ';')
463 p = next_word(words[0], sizeof(words[0]), p);
464 if (words[0][0] == 0)
467 if (rlist_cnt >= rlist_alloc) {
468 rlist_alloc = rlist_alloc * 2 + 64;
469 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
470 my_assert_not(rlist, NULL);
472 rlist[rlist_cnt++] = strdup(words[0]);
480 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
482 qsort(unwanted_syms, ARRAY_SIZE(unwanted_syms),
483 sizeof(unwanted_syms[0]), cmpstringp);
488 next_section(fasm, line);
491 if (IS(line + 1, "text"))
494 if (IS(line + 1, "rdata"))
495 fprintf(fout, "\n.section .rodata\n");
496 else if (IS(line + 1, "data"))
497 fprintf(fout, "\n.data\n");
499 aerr("unhandled section: '%s'\n", line);
501 fprintf(fout, ".align %d\n", align_value(4));
503 while (fgets(line, sizeof(line), fasm))
513 if (IS_START(p, ";org") && sscanf(p + 5, "%Xh", &i) == 1) {
514 // ;org is only seen at section start, so assume . addr 0
517 fprintf(fout, "\t\t .skip 0x%x\n", i);
522 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
523 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
524 if (*p == 0 || *p == ';') {
535 if (IS_START(p, "sctclrtype"))
536 g_func_sym_pp = NULL;
539 if (wordc == 2 && IS(words[1], "ends"))
541 if (wordc <= 2 && IS(words[0], "end"))
544 aerr("unhandled: '%s'\n", words[0]);
547 if (IS(words[0], "assume"))
550 if (IS(words[0], "align")) {
551 val = parse_number(words[1]);
552 fprintf(fout, "\t\t .align %d", align_value(val));
557 type = parse_dx_directive(words[0]);
558 if (type == DXT_UNSPEC) {
559 type = parse_dx_directive(words[1]);
563 if (type == DXT_UNSPEC)
564 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
567 snprintf(last_sym, sizeof(last_sym), "%s", sym);
569 pp = proto_parse(fhdr, sym, 1);
571 g_func_sym_pp = NULL;
573 // public/global name
574 if (pub_sym_cnt >= pub_sym_alloc) {
576 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
577 my_assert_not(pub_syms, NULL);
579 pub_syms[pub_sym_cnt++] = strdup(sym);
583 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
596 fprintf(fout, "\t\t ");
599 // fill out some unwanted strings with zeroes..
600 if (type == DXT_BYTE && words[w][0] == '\''
601 && is_unwanted_sym(last_sym))
604 for (; w < wordc; w++) {
605 if (words[w][0] == '\'') {
607 for (; *p && *p != '\''; p++)
611 // assume encoded byte
615 fprintf(fout, ".skip %d", len);
618 else if (type == DXT_BYTE
619 && (words[w][0] == '\''
620 || (w + 1 < wordc && words[w + 1][0] == '\'')))
622 // string; use asciz for most common case
623 if (w == wordc - 2 && IS(words[w + 1], "0")) {
624 fprintf(fout, ".asciz \"");
628 fprintf(fout, ".ascii \"");
630 for (; w < wordc; w++) {
631 if (words[w][0] == '\'') {
633 p2 = strchr(p, '\'');
635 aerr("unterminated string? '%s'\n", p);
636 memcpy(word, p, p2 - p);
638 fprintf(fout, "%s", escape_string(word));
641 val = parse_number(words[w]);
643 aerr("bad string trailing byte?\n");
644 fprintf(fout, "\\x%02lx", val);
651 if (w == wordc - 2) {
652 if (IS_START(words[w + 1], "dup(")) {
653 cnt = parse_number(words[w]);
654 p = words[w + 1] + 4;
658 memmove(word, p, p2 - p);
663 val = parse_number(word);
665 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
666 cnt, type_size(type), val);
671 if (type == DXT_DWORD && words[w][0] == '\''
672 && words[w][5] == '\'' && strlen(words[w]) == 6)
678 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
679 fprintf(fout, ".long 0x%lx", val);
680 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
684 if (type >= DXT_DWORD && strchr(words[w], '.'))
689 if (g_arm_mode && type == DXT_TEN) {
690 fprintf(fout, ".fill 10");
691 snprintf(g_comment, sizeof(g_comment), "%s %s",
692 type_name_float(type), words[w]);
695 fprintf(fout, "%s %s", type_name_float(type), words[w]);
700 fprintf(fout, "%s ", type_name(type));
701 for (; w < wordc; w++)
706 is_label = is_bss = 0;
707 if (w <= wordc - 2 && IS(words[w], "offset")) {
711 else if (IS(words[w], "?")) {
714 else if (type == DXT_DWORD
715 && !('0' <= words[w][0] && words[w][0] <= '9'))
726 if (IS_START(p, "loc_") || IS_START(p, "__imp")
727 || strchr(p, '?') || strchr(p, '@')
728 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
732 snprintf(g_comment, sizeof(g_comment), "%s", p);
735 pp = check_var(fhdr, sym, p);
737 fprintf(fout, "%s%s",
738 (no_decorations || p[0] == '_') ? "" : "_", p);
742 fprintf(fout, "%s", pp->name);
744 output_decorated_pp(fout, pp);
749 val = parse_number(words[w]);
751 fprintf(fout, "%ld", val);
753 fprintf(fout, "0x%lx", val);
760 if (g_comment[0] != 0) {
761 fprintf(fout, "\t\t%c %s", comment_char, g_comment);
771 for (i = 0; i < pub_sym_cnt; i++)
772 fprintf(fout, ".global %s%s\n",
773 no_decorations ? "" : "_", pub_syms[i]);
782 // vim:ts=2:shiftwidth=2:expandtab