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;
22 // note: must be in ascending order
32 #define anote(fmt, ...) \
33 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
34 #define awarn(fmt, ...) do { \
35 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__); \
36 if (++g_warn_cnt == 10) { \
41 #define aerr(fmt, ...) do { \
42 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
47 #include "masm_tools.h"
49 static char *next_word_s(char *w, size_t wsize, char *s)
56 for (i = 0; i < wsize - 1; i++) {
59 if (s[i] == 0 || (!quote && (my_isblank(s[i]) || s[i] == ',')))
65 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != ',')
66 printf("warning: '%s' truncated\n", w);
71 static void next_section(FILE *fasm, char *name)
80 while (fgets(line, sizeof(line), fasm))
90 while (strlen(line) == sizeof(line) - 1) {
91 // one of those long comment lines..
92 if (!fgets(line, sizeof(line), fasm))
98 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
99 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
100 if (*p == 0 || *p == ';') {
109 if (!IS(words[1], "segment"))
112 strcpy(name, words[0]);
117 static enum dx_type parse_dx_directive(const char *name)
133 static const char *type_name(enum dx_type type)
152 static const char *type_name_float(enum dx_type type)
164 return "<bad_float>";
167 static int type_size(enum dx_type type)
186 static char *escape_string(char *s)
191 for (; *s != 0; s++) {
205 return strcpy(s, buf);
208 static void sprint_pp_short(const struct parsed_proto *pp, char *buf,
215 if (pp->ret_type.is_ptr)
217 else if (IS(pp->ret_type.name, "void"))
224 for (i = 0; i < pp->argc; i++) {
225 if (pp->arg[i].reg != NULL)
226 snprintf(buf + l, buf_size - l, "%s%s",
227 i == 0 ? "" : ",", pp->arg[i].reg);
229 snprintf(buf + l, buf_size - l, "%sa%d",
230 i == 0 ? "" : ",", i + 1);
233 snprintf(buf + l, buf_size - l, ")");
236 static const struct parsed_proto *check_var(FILE *fhdr,
237 const char *sym, const char *varname)
239 const struct parsed_proto *pp, *pp_sym;
240 char fp_sym[256], fp_var[256];
243 pp = proto_parse(fhdr, varname, 1);
245 if (IS_START(varname, "sub_"))
246 awarn("sub_ sym missing proto: '%s'\n", varname);
250 if (!pp->is_func && !pp->is_fptr)
253 pp_print(fp_var, sizeof(fp_var), pp);
255 if (pp->argc_reg == 0)
257 if (pp->argc_reg == 1 && pp->argc_stack == 0
258 && IS(pp->arg[0].reg, "ecx"))
262 if (pp->argc_reg != 2
263 || !IS(pp->arg[0].reg, "ecx")
264 || !IS(pp->arg[1].reg, "edx"))
266 awarn("unhandled reg call: %s\n", fp_var);
270 sprint_pp_short(pp, g_comment, sizeof(g_comment));
273 g_func_sym_pp = NULL;
274 pp_sym = proto_parse(fhdr, sym, 1);
277 if (!pp_sym->is_fptr)
278 aerr("func ptr data, but label '%s' !is_fptr\n", pp_sym->name);
279 g_func_sym_pp = pp_sym;
282 pp_sym = g_func_sym_pp;
287 if (pp->argc != pp_sym->argc || pp->argc_reg != pp_sym->argc_reg)
290 for (i = 0; i < pp->argc; i++) {
291 if ((pp->arg[i].reg != NULL) != (pp_sym->arg[i].reg != NULL)) {
295 if ((pp->arg[i].reg != NULL)
296 && !IS(pp->arg[i].reg, pp_sym->arg[i].reg))
305 pp_print(fp_sym, sizeof(fp_sym), pp_sym);
306 anote("var: %s\n", fp_var);
307 anote("sym: %s\n", fp_sym);
308 awarn("^ mismatch\n");
314 static void output_decorated_pp(FILE *fout,
315 const struct parsed_proto *pp)
317 if (pp->name[0] != '_')
318 fprintf(fout, pp->is_fastcall ? "@" : "_");
319 fprintf(fout, "%s", pp->name);
320 if (pp->is_stdcall && pp->argc > 0)
321 fprintf(fout, "@%d", pp->argc * 4);
324 static int cmpstringp(const void *p1, const void *p2)
326 return strcmp(*(char * const *)p1, *(char * const *)p2);
329 int main(int argc, char *argv[])
331 FILE *fout, *fasm, *fhdr, *frlist;
332 const struct parsed_proto *pp;
333 int no_decorations = 0;
359 // -nd: no symbol decorations
360 printf("usage:\n%s [-nd] <.s> <.asm> <hdrf> [rlist]*\n",
365 for (arg = 1; arg < argc; arg++) {
366 if (IS(argv[arg], "-nd"))
375 fasm = fopen(asmfn, "r");
376 my_assert_not(fasm, NULL);
379 fhdr = fopen(hdrfn, "r");
380 my_assert_not(fhdr, NULL);
382 fout = fopen(argv[arg_out], "w");
383 my_assert_not(fout, NULL);
386 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
387 my_assert_not(pub_syms, NULL);
390 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
391 my_assert_not(rlist, NULL);
393 for (; arg < argc; arg++) {
394 frlist = fopen(argv[arg], "r");
395 my_assert_not(frlist, NULL);
397 while (fgets(line, sizeof(line), frlist)) {
399 if (*p == 0 || *p == ';')
402 p = next_word(words[0], sizeof(words[0]), p);
403 if (words[0][0] == 0)
406 if (rlist_cnt >= rlist_alloc) {
407 rlist_alloc = rlist_alloc * 2 + 64;
408 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
409 my_assert_not(rlist, NULL);
411 rlist[rlist_cnt++] = strdup(words[0]);
419 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
422 next_section(fasm, line);
425 if (IS(line + 1, "text"))
428 if (IS(line + 1, "rdata"))
429 fprintf(fout, "\n.section .rodata\n");
430 else if (IS(line + 1, "data"))
431 fprintf(fout, "\n.data\n");
433 aerr("unhandled section: '%s'\n", line);
435 fprintf(fout, ".align 4\n");
437 while (fgets(line, sizeof(line), fasm))
443 if (*p == 0 || *p == ';')
446 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
447 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
448 if (*p == 0 || *p == ';') {
459 if (IS_START(p, "sctclrtype"))
460 g_func_sym_pp = NULL;
463 if (wordc == 2 && IS(words[1], "ends"))
465 if (wordc <= 2 && IS(words[0], "end"))
468 aerr("unhandled: '%s'\n", words[0]);
471 if (IS(words[0], "assume"))
474 if (IS(words[0], "align")) {
475 val = parse_number(words[1]);
476 fprintf(fout, "\t\t .align %ld", val);
481 type = parse_dx_directive(words[0]);
482 if (type == DXT_UNSPEC) {
483 type = parse_dx_directive(words[1]);
487 if (type == DXT_UNSPEC)
488 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
491 // public/global name
492 if (pub_sym_cnt >= pub_sym_alloc) {
494 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
495 my_assert_not(pub_syms, NULL);
497 pub_syms[pub_sym_cnt++] = strdup(sym);
499 pp = proto_parse(fhdr, sym, 1);
501 g_func_sym_pp = NULL;
504 fprintf(fout, "%s%s:", no_decorations ? "" : "_", sym);
517 fprintf(fout, "\t\t ");
521 && (words[w][0] == '\''
522 || (w + 1 < wordc && words[w + 1][0] == '\'')))
524 // string; use asciz for most common case
525 if (w == wordc - 2 && IS(words[w + 1], "0")) {
526 fprintf(fout, ".asciz \"");
530 fprintf(fout, ".ascii \"");
532 for (; w < wordc; w++) {
533 if (words[w][0] == '\'') {
535 p2 = strchr(p, '\'');
537 aerr("unterminated string? '%s'\n", p);
538 memcpy(word, p, p2 - p);
540 fprintf(fout, "%s", escape_string(word));
543 val = parse_number(words[w]);
545 aerr("bad string trailing byte?\n");
546 fprintf(fout, "\\x%02lx", val);
553 if (w == wordc - 2) {
554 if (IS_START(words[w + 1], "dup(")) {
555 cnt = parse_number(words[w]);
556 p = words[w + 1] + 4;
560 memmove(word, p, p2 - p);
565 val = parse_number(word);
567 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
568 cnt, type_size(type), val);
573 if (type == DXT_DWORD && words[w][0] == '\''
574 && words[w][5] == '\'' && strlen(words[w]) == 6)
580 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
581 fprintf(fout, ".long 0x%lx", val);
582 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
586 if (type >= DXT_DWORD && strchr(words[w], '.'))
591 fprintf(fout, "%s %s", type_name_float(type), words[w]);
596 fprintf(fout, "%s ", type_name(type));
597 for (; w < wordc; w++)
602 is_label = is_bss = 0;
603 if (w <= wordc - 2 && IS(words[w], "offset")) {
607 else if (IS(words[w], "?")) {
610 else if (type == DXT_DWORD
611 && !('0' <= words[w][0] && words[w][0] <= '9'))
622 if (IS_START(p, "loc_") || IS_START(p, "__imp")
623 || strchr(p, '?') || strchr(p, '@')
624 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
628 snprintf(g_comment, sizeof(g_comment), "%s", p);
631 pp = check_var(fhdr, sym, p);
633 fprintf(fout, "%s%s",
634 (no_decorations || p[0] == '_') ? "" : "_", p);
638 fprintf(fout, "%s", pp->name);
640 output_decorated_pp(fout, pp);
645 val = parse_number(words[w]);
647 fprintf(fout, "%ld", val);
649 fprintf(fout, "0x%lx", val);
656 if (g_comment[0] != 0) {
657 fprintf(fout, "\t\t# %s", g_comment);
667 for (i = 0; i < pub_sym_cnt; i++)
668 fprintf(fout, ".global %s%s\n",
669 no_decorations ? "" : "_", pub_syms[i]);
678 // vim:ts=2:shiftwidth=2:expandtab