9 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
10 #define IS(w, y) !strcmp(w, y)
11 #define IS_START(w, y) !strncmp(w, y, strlen(y))
13 #include "protoparse.h"
15 static const char *asmfn;
18 static const struct parsed_proto *g_func_sym_pp;
19 static char g_comment[256];
20 static int g_warn_cnt;
21 static int g_cconv_novalidate;
23 // note: must be in ascending order
33 #define anote(fmt, ...) \
34 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
35 #define awarn(fmt, ...) do { \
36 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__); \
37 if (++g_warn_cnt == 10) { \
42 #define aerr(fmt, ...) do { \
43 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
48 #include "masm_tools.h"
50 static char *next_word_s(char *w, size_t wsize, char *s)
57 for (i = 0; i < wsize - 1; i++) {
60 if (s[i] == 0 || (!quote && (my_isblank(s[i]) || s[i] == ',')))
66 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != ',')
67 printf("warning: '%s' truncated\n", w);
72 static void next_section(FILE *fasm, char *name)
81 while (fgets(line, sizeof(line), fasm))
91 while (strlen(line) == sizeof(line) - 1) {
92 // one of those long comment lines..
93 if (!fgets(line, sizeof(line), fasm))
99 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
100 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
101 if (*p == 0 || *p == ';') {
110 if (!IS(words[1], "segment"))
113 strcpy(name, words[0]);
118 static enum dx_type parse_dx_directive(const char *name)
134 static const char *type_name(enum dx_type type)
153 static const char *type_name_float(enum dx_type type)
165 return "<bad_float>";
168 static int type_size(enum dx_type type)
187 static char *escape_string(char *s)
192 for (; *s != 0; s++) {
206 return strcpy(s, buf);
209 static void sprint_pp_short(const struct parsed_proto *pp, char *buf,
216 if (pp->ret_type.is_ptr)
218 else if (IS(pp->ret_type.name, "void"))
225 for (i = 0; i < pp->argc; i++) {
226 if (pp->arg[i].reg != NULL)
227 snprintf(buf + l, buf_size - l, "%s%s",
228 i == 0 ? "" : ",", pp->arg[i].reg);
230 snprintf(buf + l, buf_size - l, "%sa%d",
231 i == 0 ? "" : ",", i + 1);
234 snprintf(buf + l, buf_size - l, ")");
237 static const struct parsed_proto *check_var(FILE *fhdr,
238 const char *sym, const char *varname)
240 const struct parsed_proto *pp, *pp_sym;
241 char fp_sym[256], fp_var[256];
244 pp = proto_parse(fhdr, varname, 1);
246 if (IS_START(varname, "sub_"))
247 awarn("sub_ sym missing proto: '%s'\n", varname);
251 if (!pp->is_func && !pp->is_fptr)
254 pp_print(fp_var, sizeof(fp_var), pp);
256 if (pp->argc_reg == 0)
258 if (pp->argc_reg == 1 && pp->argc_stack == 0
259 && IS(pp->arg[0].reg, "ecx"))
263 if (!g_cconv_novalidate
264 && (pp->argc_reg != 2
265 || !IS(pp->arg[0].reg, "ecx")
266 || !IS(pp->arg[1].reg, "edx")))
268 awarn("unhandled reg call: %s\n", fp_var);
272 sprint_pp_short(pp, g_comment, sizeof(g_comment));
275 g_func_sym_pp = NULL;
276 pp_sym = proto_parse(fhdr, sym, 1);
279 if (!pp_sym->is_fptr)
280 aerr("func ptr data, but label '%s' !is_fptr\n", pp_sym->name);
281 g_func_sym_pp = pp_sym;
284 pp_sym = g_func_sym_pp;
289 if (pp->argc != pp_sym->argc || pp->argc_reg != pp_sym->argc_reg)
292 for (i = 0; i < pp->argc; i++) {
293 if ((pp->arg[i].reg != NULL) != (pp_sym->arg[i].reg != NULL)) {
297 if ((pp->arg[i].reg != NULL)
298 && !IS(pp->arg[i].reg, pp_sym->arg[i].reg))
307 pp_print(fp_sym, sizeof(fp_sym), pp_sym);
308 anote("var: %s\n", fp_var);
309 anote("sym: %s\n", fp_sym);
310 awarn("^ mismatch\n");
316 static void output_decorated_pp(FILE *fout,
317 const struct parsed_proto *pp)
319 if (pp->name[0] != '_')
320 fprintf(fout, pp->is_fastcall ? "@" : "_");
321 fprintf(fout, "%s", pp->name);
322 if (pp->is_stdcall && pp->argc > 0)
323 fprintf(fout, "@%d", pp->argc * 4);
326 static int cmpstringp(const void *p1, const void *p2)
328 return strcmp(*(char * const *)p1, *(char * const *)p2);
331 int main(int argc, char *argv[])
333 FILE *fout, *fasm, *fhdr, *frlist;
334 const struct parsed_proto *pp;
335 int no_decorations = 0;
361 // -nd: no symbol decorations
362 printf("usage:\n%s [-nd] [-i] <.s> <.asm> <hdrf> [rlist]*\n",
367 for (arg = 1; arg < argc; arg++) {
368 if (IS(argv[arg], "-nd"))
370 else if (IS(argv[arg], "-i"))
371 g_cconv_novalidate = 1;
379 fasm = fopen(asmfn, "r");
380 my_assert_not(fasm, NULL);
383 fhdr = fopen(hdrfn, "r");
384 my_assert_not(fhdr, NULL);
386 fout = fopen(argv[arg_out], "w");
387 my_assert_not(fout, NULL);
390 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
391 my_assert_not(pub_syms, NULL);
394 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
395 my_assert_not(rlist, NULL);
397 for (; arg < argc; arg++) {
398 frlist = fopen(argv[arg], "r");
399 my_assert_not(frlist, NULL);
401 while (fgets(line, sizeof(line), frlist)) {
403 if (*p == 0 || *p == ';')
406 p = next_word(words[0], sizeof(words[0]), p);
407 if (words[0][0] == 0)
410 if (rlist_cnt >= rlist_alloc) {
411 rlist_alloc = rlist_alloc * 2 + 64;
412 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
413 my_assert_not(rlist, NULL);
415 rlist[rlist_cnt++] = strdup(words[0]);
423 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
426 next_section(fasm, line);
429 if (IS(line + 1, "text"))
432 if (IS(line + 1, "rdata"))
433 fprintf(fout, "\n.section .rodata\n");
434 else if (IS(line + 1, "data"))
435 fprintf(fout, "\n.data\n");
437 aerr("unhandled section: '%s'\n", line);
439 fprintf(fout, ".align 4\n");
441 while (fgets(line, sizeof(line), fasm))
447 if (*p == 0 || *p == ';')
450 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
451 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
452 if (*p == 0 || *p == ';') {
463 if (IS_START(p, "sctclrtype"))
464 g_func_sym_pp = NULL;
467 if (wordc == 2 && IS(words[1], "ends"))
469 if (wordc <= 2 && IS(words[0], "end"))
472 aerr("unhandled: '%s'\n", words[0]);
475 if (IS(words[0], "assume"))
478 if (IS(words[0], "align")) {
479 val = parse_number(words[1]);
480 fprintf(fout, "\t\t .align %ld", val);
485 type = parse_dx_directive(words[0]);
486 if (type == DXT_UNSPEC) {
487 type = parse_dx_directive(words[1]);
491 if (type == DXT_UNSPEC)
492 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
495 // public/global name
496 if (pub_sym_cnt >= pub_sym_alloc) {
498 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
499 my_assert_not(pub_syms, NULL);
501 pub_syms[pub_sym_cnt++] = strdup(sym);
503 pp = proto_parse(fhdr, sym, 1);
505 g_func_sym_pp = NULL;
508 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
521 fprintf(fout, "\t\t ");
525 && (words[w][0] == '\''
526 || (w + 1 < wordc && words[w + 1][0] == '\'')))
528 // string; use asciz for most common case
529 if (w == wordc - 2 && IS(words[w + 1], "0")) {
530 fprintf(fout, ".asciz \"");
534 fprintf(fout, ".ascii \"");
536 for (; w < wordc; w++) {
537 if (words[w][0] == '\'') {
539 p2 = strchr(p, '\'');
541 aerr("unterminated string? '%s'\n", p);
542 memcpy(word, p, p2 - p);
544 fprintf(fout, "%s", escape_string(word));
547 val = parse_number(words[w]);
549 aerr("bad string trailing byte?\n");
550 fprintf(fout, "\\x%02lx", val);
557 if (w == wordc - 2) {
558 if (IS_START(words[w + 1], "dup(")) {
559 cnt = parse_number(words[w]);
560 p = words[w + 1] + 4;
564 memmove(word, p, p2 - p);
569 val = parse_number(word);
571 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
572 cnt, type_size(type), val);
577 if (type == DXT_DWORD && words[w][0] == '\''
578 && words[w][5] == '\'' && strlen(words[w]) == 6)
584 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
585 fprintf(fout, ".long 0x%lx", val);
586 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
590 if (type >= DXT_DWORD && strchr(words[w], '.'))
595 fprintf(fout, "%s %s", type_name_float(type), words[w]);
600 fprintf(fout, "%s ", type_name(type));
601 for (; w < wordc; w++)
606 is_label = is_bss = 0;
607 if (w <= wordc - 2 && IS(words[w], "offset")) {
611 else if (IS(words[w], "?")) {
614 else if (type == DXT_DWORD
615 && !('0' <= words[w][0] && words[w][0] <= '9'))
626 if (IS_START(p, "loc_") || IS_START(p, "__imp")
627 || strchr(p, '?') || strchr(p, '@')
628 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
632 snprintf(g_comment, sizeof(g_comment), "%s", p);
635 pp = check_var(fhdr, sym, p);
637 fprintf(fout, "%s%s",
638 (no_decorations || p[0] == '_') ? "" : "_", p);
642 fprintf(fout, "%s", pp->name);
644 output_decorated_pp(fout, pp);
649 val = parse_number(words[w]);
651 fprintf(fout, "%ld", val);
653 fprintf(fout, "0x%lx", val);
660 if (g_comment[0] != 0) {
661 fprintf(fout, "\t\t# %s", g_comment);
671 for (i = 0; i < pub_sym_cnt; i++)
672 fprintf(fout, ".global %s%s\n",
673 no_decorations ? "" : "_", pub_syms[i]);
682 // vim:ts=2:shiftwidth=2:expandtab