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 // note: must be in ascending order
28 #define anote(fmt, ...) \
29 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
30 #define aerr(fmt, ...) do { \
31 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
36 #include "masm_tools.h"
38 static char *next_word_s(char *w, size_t wsize, char *s)
45 for (i = 0; i < wsize - 1; i++) {
48 if (s[i] == 0 || (!quote && (my_isblank(s[i]) || s[i] == ',')))
54 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != ',')
55 printf("warning: '%s' truncated\n", w);
60 static void next_section(FILE *fasm, char *name)
69 while (fgets(line, sizeof(line), fasm))
79 while (strlen(line) == sizeof(line) - 1) {
80 // one of those long comment lines..
81 if (!fgets(line, sizeof(line), fasm))
87 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
88 p = sskip(next_word(words[wordc], sizeof(words[0]), p));
89 if (*p == 0 || *p == ';') {
98 if (!IS(words[1], "segment"))
101 strcpy(name, words[0]);
106 static enum dx_type parse_dx_directive(const char *name)
122 static const char *type_name(enum dx_type type)
141 static const char *type_name_float(enum dx_type type)
153 return "<bad_float>";
156 static int type_size(enum dx_type type)
175 static char *escape_string(char *s)
180 for (; *s != 0; s++) {
194 return strcpy(s, buf);
197 static void check_sym(FILE *fhdr, const char *name)
199 const struct parsed_proto *pp;
203 pp = proto_parse(fhdr, name, 1);
205 if (IS_START(name, "sub_"))
206 aerr("sub_ sym missing proto: '%s'\n", name);
210 if (!pp->is_func && !pp->is_fptr)
212 if (pp->argc_reg == 0)
214 if (pp->argc_reg == 1 && pp->argc_stack == 0
215 && IS(pp->arg[0].reg, "ecx"))
219 if (pp->argc_reg == 2
220 && IS(pp->arg[0].reg, "ecx")
221 && IS(pp->arg[1].reg, "edx"))
225 snprintf(buf, sizeof(buf), "%s %s(",
226 pp->ret_type.name, name);
229 for (i = 0; i < pp->argc_reg; i++) {
230 snprintf(buf + l, sizeof(buf) - l, "%s%s",
231 i == 0 ? "" : ", ", pp->arg[i].reg);
234 if (pp->argc_stack > 0) {
235 snprintf(buf + l, sizeof(buf) - l, ", {%d stack}", pp->argc_stack);
238 snprintf(buf + l, sizeof(buf) - l, ")");
240 aerr("unhandled reg call: %s\n", buf);
243 static int cmpstringp(const void *p1, const void *p2)
245 return strcmp(*(char * const *)p1, *(char * const *)p2);
248 int main(int argc, char *argv[])
250 FILE *fout, *fasm, *fhdr, *frlist;
277 printf("usage:\n%s <.s> <.asm> <hdrf> [rlist]*\n",
285 fasm = fopen(asmfn, "r");
286 my_assert_not(fasm, NULL);
289 fhdr = fopen(hdrfn, "r");
290 my_assert_not(fhdr, NULL);
292 fout = fopen(argv[arg_out], "w");
293 my_assert_not(fout, NULL);
298 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
299 my_assert_not(pub_syms, NULL);
302 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
303 my_assert_not(rlist, NULL);
305 for (; arg < argc; arg++) {
306 frlist = fopen(argv[arg], "r");
307 my_assert_not(frlist, NULL);
309 while (fgets(line, sizeof(line), frlist)) {
311 if (*p == 0 || *p == ';')
314 p = next_word(words[0], sizeof(words[0]), p);
315 if (words[0][0] == 0)
318 if (rlist_cnt >= rlist_alloc) {
319 rlist_alloc = rlist_alloc * 2 + 64;
320 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
321 my_assert_not(rlist, NULL);
323 rlist[rlist_cnt++] = strdup(words[0]);
331 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
334 next_section(fasm, line);
337 if (IS(line + 1, "text"))
340 if (IS(line + 1, "rdata"))
341 fprintf(fout, "\n.section .rodata\n");
342 else if (IS(line + 1, "data"))
343 fprintf(fout, "\n.data\n");
345 aerr("unhandled section: '%s'\n", line);
347 fprintf(fout, ".align 4\n");
349 while (fgets(line, sizeof(line), fasm))
355 if (*p == 0 || *p == ';')
358 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
359 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
360 if (*p == 0 || *p == ';') {
369 if (wordc == 2 && IS(words[1], "ends"))
371 if (wordc <= 2 && IS(words[0], "end"))
374 aerr("unhandled: '%s'\n", words[0]);
377 if (IS(words[0], "assume"))
380 if (IS(words[0], "align")) {
381 val = parse_number(words[1]);
382 fprintf(fout, "\t\t .align %ld", val);
387 type = parse_dx_directive(words[0]);
388 if (type == DXT_UNSPEC) {
389 type = parse_dx_directive(words[1]);
393 if (type == DXT_UNSPEC)
394 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
397 // public/global name
398 if (pub_sym_cnt >= pub_sym_alloc) {
400 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
401 my_assert_not(pub_syms, NULL);
403 pub_syms[pub_sym_cnt++] = strdup(sym);
406 fprintf(fout, "_%s:", sym);
419 fprintf(fout, "\t\t ");
422 if (type == DXT_BYTE && words[w][0] == '\'') {
423 // string; use asciz for most common case
424 if (w == wordc - 2 && IS(words[w + 1], "0")) {
425 fprintf(fout, ".asciz \"");
429 fprintf(fout, ".ascii \"");
431 for (; w < wordc; w++) {
432 if (words[w][0] == '\'') {
434 p2 = strchr(p, '\'');
436 aerr("unterminated string? '%s'\n", p);
437 memcpy(word, p, p2 - p);
439 fprintf(fout, "%s", escape_string(word));
442 val = parse_number(words[w]);
444 aerr("bad string trailing byte?\n");
445 fprintf(fout, "\\x%02lx", val);
452 if (w == wordc - 2) {
453 if (IS_START(words[w + 1], "dup(")) {
454 cnt = parse_number(words[w]);
455 p = words[w + 1] + 4;
459 memmove(word, p, p2 - p);
464 val = parse_number(word);
466 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
467 cnt, type_size(type), val);
472 if (type == DXT_DWORD && words[w][0] == '\''
473 && words[w][5] == '\'' && strlen(words[w]) == 6)
479 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
480 fprintf(fout, ".long 0x%lx", val);
481 snprintf(comment, sizeof(comment), "%s", words[w]);
485 if (type >= DXT_DWORD && strchr(words[w], '.'))
490 fprintf(fout, "%s %s", type_name_float(type), words[w]);
495 fprintf(fout, "%s ", type_name(type));
496 for (; w < wordc; w++)
501 is_label = is_bss = 0;
502 if (w <= wordc - 2 && IS(words[w], "offset")) {
506 else if (IS(words[w], "?")) {
509 else if (type == DXT_DWORD
510 && !('0' <= words[w][0] && words[w][0] <= '9'))
521 if (IS_START(p, "loc_") || strchr(p, '?') || strchr(p, '@')
522 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
526 snprintf(comment, sizeof(comment), "%s", p);
530 fprintf(fout, "_%s", p);
534 val = parse_number(words[w]);
536 fprintf(fout, "%ld", val);
538 fprintf(fout, "0x%lx", val);
545 if (comment[0] != 0) {
546 fprintf(fout, "\t\t# %s", comment);
557 for (i = 0; i < pub_sym_cnt; i++)
558 fprintf(fout, ".global _%s\n", pub_syms[i]);
567 // vim:ts=2:shiftwidth=2:expandtab