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 int cmpstringp(const void *p1, const void *p2)
316 return strcmp(*(char * const *)p1, *(char * const *)p2);
319 int main(int argc, char *argv[])
321 FILE *fout, *fasm, *fhdr, *frlist;
322 const struct parsed_proto *pp;
348 printf("usage:\n%s <.s> <.asm> <hdrf> [rlist]*\n",
356 fasm = fopen(asmfn, "r");
357 my_assert_not(fasm, NULL);
360 fhdr = fopen(hdrfn, "r");
361 my_assert_not(fhdr, NULL);
363 fout = fopen(argv[arg_out], "w");
364 my_assert_not(fout, NULL);
367 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
368 my_assert_not(pub_syms, NULL);
371 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
372 my_assert_not(rlist, NULL);
374 for (; arg < argc; arg++) {
375 frlist = fopen(argv[arg], "r");
376 my_assert_not(frlist, NULL);
378 while (fgets(line, sizeof(line), frlist)) {
380 if (*p == 0 || *p == ';')
383 p = next_word(words[0], sizeof(words[0]), p);
384 if (words[0][0] == 0)
387 if (rlist_cnt >= rlist_alloc) {
388 rlist_alloc = rlist_alloc * 2 + 64;
389 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
390 my_assert_not(rlist, NULL);
392 rlist[rlist_cnt++] = strdup(words[0]);
400 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
403 next_section(fasm, line);
406 if (IS(line + 1, "text"))
409 if (IS(line + 1, "rdata"))
410 fprintf(fout, "\n.section .rodata\n");
411 else if (IS(line + 1, "data"))
412 fprintf(fout, "\n.data\n");
414 aerr("unhandled section: '%s'\n", line);
416 fprintf(fout, ".align 4\n");
418 while (fgets(line, sizeof(line), fasm))
424 if (*p == 0 || *p == ';')
427 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
428 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
429 if (*p == 0 || *p == ';') {
440 if (IS_START(p, "sctclrtype"))
441 g_func_sym_pp = NULL;
444 if (wordc == 2 && IS(words[1], "ends"))
446 if (wordc <= 2 && IS(words[0], "end"))
449 aerr("unhandled: '%s'\n", words[0]);
452 if (IS(words[0], "assume"))
455 if (IS(words[0], "align")) {
456 val = parse_number(words[1]);
457 fprintf(fout, "\t\t .align %ld", val);
462 type = parse_dx_directive(words[0]);
463 if (type == DXT_UNSPEC) {
464 type = parse_dx_directive(words[1]);
468 if (type == DXT_UNSPEC)
469 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
472 // public/global name
473 if (pub_sym_cnt >= pub_sym_alloc) {
475 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
476 my_assert_not(pub_syms, NULL);
478 pub_syms[pub_sym_cnt++] = strdup(sym);
480 pp = proto_parse(fhdr, sym, 1);
482 g_func_sym_pp = NULL;
485 fprintf(fout, "_%s:", sym);
498 fprintf(fout, "\t\t ");
501 if (type == DXT_BYTE && words[w][0] == '\'') {
502 // string; use asciz for most common case
503 if (w == wordc - 2 && IS(words[w + 1], "0")) {
504 fprintf(fout, ".asciz \"");
508 fprintf(fout, ".ascii \"");
510 for (; w < wordc; w++) {
511 if (words[w][0] == '\'') {
513 p2 = strchr(p, '\'');
515 aerr("unterminated string? '%s'\n", p);
516 memcpy(word, p, p2 - p);
518 fprintf(fout, "%s", escape_string(word));
521 val = parse_number(words[w]);
523 aerr("bad string trailing byte?\n");
524 fprintf(fout, "\\x%02lx", val);
531 if (w == wordc - 2) {
532 if (IS_START(words[w + 1], "dup(")) {
533 cnt = parse_number(words[w]);
534 p = words[w + 1] + 4;
538 memmove(word, p, p2 - p);
543 val = parse_number(word);
545 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
546 cnt, type_size(type), val);
551 if (type == DXT_DWORD && words[w][0] == '\''
552 && words[w][5] == '\'' && strlen(words[w]) == 6)
558 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
559 fprintf(fout, ".long 0x%lx", val);
560 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
564 if (type >= DXT_DWORD && strchr(words[w], '.'))
569 fprintf(fout, "%s %s", type_name_float(type), words[w]);
574 fprintf(fout, "%s ", type_name(type));
575 for (; w < wordc; w++)
580 is_label = is_bss = 0;
581 if (w <= wordc - 2 && IS(words[w], "offset")) {
585 else if (IS(words[w], "?")) {
588 else if (type == DXT_DWORD
589 && !('0' <= words[w][0] && words[w][0] <= '9'))
600 if (IS_START(p, "loc_") || IS_START(p, "__imp")
601 || strchr(p, '?') || strchr(p, '@')
602 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
606 snprintf(g_comment, sizeof(g_comment), "%s", p);
609 pp = check_var(fhdr, sym, p);
611 fprintf(fout, (pp && pp->is_fastcall) ? "@" : "_");
612 fprintf(fout, "%s", p);
613 if (pp && pp->is_stdcall && pp->argc > 0)
614 fprintf(fout, "@%d", pp->argc * 4);
618 val = parse_number(words[w]);
620 fprintf(fout, "%ld", val);
622 fprintf(fout, "0x%lx", val);
629 if (g_comment[0] != 0) {
630 fprintf(fout, "\t\t# %s", g_comment);
640 for (i = 0; i < pub_sym_cnt; i++)
641 fprintf(fout, ".global _%s\n", pub_syms[i]);
650 // vim:ts=2:shiftwidth=2:expandtab