support __fastcall
[ia32rtools.git] / tools / cvt_data.c
CommitLineData
054f95b2 1#define _GNU_SOURCE
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#include "my_assert.h"
7#include "my_str.h"
8
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))
12
13#include "protoparse.h"
14
15static const char *asmfn;
16static int asmln;
054f95b2 17
b545ba7c 18static const struct parsed_proto *g_func_sym_pp;
19static char g_comment[256];
20static int g_warn_cnt;
21
36595fd2 22// note: must be in ascending order
054f95b2 23enum dx_type {
24 DXT_UNSPEC,
25 DXT_BYTE,
26 DXT_WORD,
27 DXT_DWORD,
28 DXT_QUAD,
29 DXT_TEN,
30};
31
36595fd2 32#define anote(fmt, ...) \
33 printf("%s:%d: note: " fmt, asmfn, asmln, ##__VA_ARGS__)
b545ba7c 34#define awarn(fmt, ...) do { \
35 printf("%s:%d: warning: " fmt, asmfn, asmln, ##__VA_ARGS__); \
36 if (++g_warn_cnt == 10) { \
37 fcloseall(); \
38 exit(1); \
39 } \
40} while (0)
054f95b2 41#define aerr(fmt, ...) do { \
42 printf("%s:%d: error: " fmt, asmfn, asmln, ##__VA_ARGS__); \
43 fcloseall(); \
44 exit(1); \
45} while (0)
46
47#include "masm_tools.h"
48
49static char *next_word_s(char *w, size_t wsize, char *s)
50{
51 int quote = 0;
52 size_t i;
53
54 s = sskip(s);
55
56 for (i = 0; i < wsize - 1; i++) {
57 if (s[i] == '\'')
58 quote ^= 1;
59 if (s[i] == 0 || (!quote && (my_isblank(s[i]) || s[i] == ',')))
60 break;
61 w[i] = s[i];
62 }
63 w[i] = 0;
64
65 if (s[i] != 0 && !my_isblank(s[i]) && s[i] != ',')
66 printf("warning: '%s' truncated\n", w);
67
68 return s + i;
69}
70
71static void next_section(FILE *fasm, char *name)
72{
73 char words[2][256];
74 char line[256];
75 int wordc;
76 char *p;
77
78 name[0] = 0;
79
80 while (fgets(line, sizeof(line), fasm))
81 {
82 wordc = 0;
83 asmln++;
84
85 p = sskip(line);
86 if (*p == 0)
87 continue;
88
89 if (*p == ';') {
90 while (strlen(line) == sizeof(line) - 1) {
91 // one of those long comment lines..
92 if (!fgets(line, sizeof(line), fasm))
93 break;
94 }
95 continue;
96 }
97
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 == ';') {
101 wordc++;
102 break;
103 }
104 }
105
106 if (wordc < 2)
107 continue;
108
109 if (!IS(words[1], "segment"))
110 continue;
111
112 strcpy(name, words[0]);
113 break;
114 }
115}
116
117static enum dx_type parse_dx_directive(const char *name)
118{
119 if (IS(name, "dd"))
120 return DXT_DWORD;
121 if (IS(name, "dw"))
122 return DXT_WORD;
123 if (IS(name, "db"))
124 return DXT_BYTE;
125 if (IS(name, "dq"))
126 return DXT_QUAD;
127 if (IS(name, "dt"))
128 return DXT_TEN;
129
130 return DXT_UNSPEC;
131}
132
133static const char *type_name(enum dx_type type)
134{
135 switch (type) {
136 case DXT_BYTE:
137 return ".byte";
138 case DXT_WORD:
139 return ".word";
140 case DXT_DWORD:
141 return ".long";
142 case DXT_QUAD:
143 return ".quad";
144 case DXT_TEN:
145 return ".tfloat";
146 case DXT_UNSPEC:
147 break;
148 }
149 return "<bad>";
150}
151
36595fd2 152static const char *type_name_float(enum dx_type type)
153{
154 switch (type) {
155 case DXT_DWORD:
156 return ".float";
157 case DXT_QUAD:
158 return ".double";
159 case DXT_TEN:
160 return ".tfloat";
161 default:
162 break;
163 }
164 return "<bad_float>";
165}
166
054f95b2 167static int type_size(enum dx_type type)
168{
169 switch (type) {
170 case DXT_BYTE:
171 return 1;
172 case DXT_WORD:
173 return 2;
174 case DXT_DWORD:
175 return 4;
176 case DXT_QUAD:
177 return 8;
178 case DXT_TEN:
179 return 10;
180 case DXT_UNSPEC:
181 break;
182 }
183 return -1;
184}
185
186static char *escape_string(char *s)
187{
188 char buf[256];
189 char *t = buf;
190
191 for (; *s != 0; s++) {
192 if (*s == '"') {
193 strcpy(t, "\\22");
194 t += strlen(t);
195 continue;
196 }
197 if (*s == '\\') {
198 strcpy(t, "\\\\");
199 t += strlen(t);
200 continue;
201 }
202 *t++ = *s;
203 }
204 *t = *s;
205 return strcpy(s, buf);
206}
207
b545ba7c 208static void sprint_pp(const struct parsed_proto *pp, char *buf,
209 size_t buf_size)
36595fd2 210{
b545ba7c 211 size_t l;
212 int i;
36595fd2 213
b545ba7c 214 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
215 l = strlen(buf);
216
217 for (i = 0; i < pp->argc_reg; i++) {
218 snprintf(buf + l, buf_size - l, "%s%s",
219 i == 0 ? "" : ", ", pp->arg[i].reg);
220 l = strlen(buf);
221 }
222 if (pp->argc_stack > 0) {
223 snprintf(buf + l, buf_size - l, "%s{%d stack}",
224 i == 0 ? "" : ", ", pp->argc_stack);
225 l = strlen(buf);
226 }
227 snprintf(buf + l, buf_size - l, ")");
228}
229
230static void sprint_pp_short(const struct parsed_proto *pp, char *buf,
231 size_t buf_size)
232{
233 char *p = buf;
234 size_t l;
235 int i;
236
237 if (pp->ret_type.is_ptr)
238 *p++ = 'p';
239 else if (IS(pp->ret_type.name, "void"))
240 *p++ = 'v';
241 else
242 *p++ = 'i';
243 *p++ = '(';
244 l = 2;
245
246 for (i = 0; i < pp->argc; i++) {
247 if (pp->arg[i].reg != NULL)
248 snprintf(buf + l, buf_size - l, "%s%s",
249 i == 0 ? "" : ",", pp->arg[i].reg);
250 else
251 snprintf(buf + l, buf_size - l, "%sa%d",
252 i == 0 ? "" : ",", i + 1);
253 l = strlen(buf);
254 }
255 snprintf(buf + l, buf_size - l, ")");
256}
257
c0050df6 258static const struct parsed_proto *check_var(FILE *fhdr,
259 const char *sym, const char *varname)
b545ba7c 260{
261 const struct parsed_proto *pp, *pp_sym;
262 char fp_sym[256], fp_var[256];
263 int i, bad = 0;
264
265 pp = proto_parse(fhdr, varname, 1);
36595fd2 266 if (pp == NULL) {
b545ba7c 267 if (IS_START(varname, "sub_"))
268 awarn("sub_ sym missing proto: '%s'\n", varname);
c0050df6 269 return NULL;
36595fd2 270 }
271
272 if (!pp->is_func && !pp->is_fptr)
c0050df6 273 return NULL;
b545ba7c 274
275 sprint_pp(pp, fp_var, sizeof(fp_var));
276
36595fd2 277 if (pp->argc_reg == 0)
b545ba7c 278 goto check_sym;
36595fd2 279 if (pp->argc_reg == 1 && pp->argc_stack == 0
280 && IS(pp->arg[0].reg, "ecx"))
281 {
b545ba7c 282 goto check_sym;
36595fd2 283 }
b545ba7c 284 if (pp->argc_reg != 2
285 || !IS(pp->arg[0].reg, "ecx")
286 || !IS(pp->arg[1].reg, "edx"))
36595fd2 287 {
b545ba7c 288 awarn("unhandled reg call: %s\n", fp_var);
36595fd2 289 }
36595fd2 290
b545ba7c 291check_sym:
292 sprint_pp_short(pp, g_comment, sizeof(g_comment));
293
294 if (sym != NULL) {
295 g_func_sym_pp = NULL;
296 pp_sym = proto_parse(fhdr, sym, 1);
297 if (pp_sym == NULL)
c0050df6 298 return pp;
b545ba7c 299 if (!pp_sym->is_fptr)
300 aerr("func ptr data, but label '%s' !is_fptr\n", pp_sym->name);
301 g_func_sym_pp = pp_sym;
36595fd2 302 }
b545ba7c 303 else {
304 pp_sym = g_func_sym_pp;
305 if (pp_sym == NULL)
c0050df6 306 return pp;
b545ba7c 307 }
308
309 if (pp->argc != pp_sym->argc || pp->argc_reg != pp_sym->argc_reg)
310 bad = 1;
311 else {
312 for (i = 0; i < pp->argc; i++) {
313 if ((pp->arg[i].reg != NULL) != (pp_sym->arg[i].reg != NULL)) {
314 bad = 1;
315 break;
316 }
317 if ((pp->arg[i].reg != NULL)
318 && !IS(pp->arg[i].reg, pp_sym->arg[i].reg))
319 {
320 bad = 1;
321 break;
322 }
323 }
324 }
325
326 if (bad) {
327 sprint_pp(pp_sym, fp_sym, sizeof(fp_sym));
328 anote("var: %s\n", fp_var);
329 anote("sym: %s\n", fp_sym);
330 awarn("^ mismatch\n");
36595fd2 331 }
c0050df6 332
333 return pp;
36595fd2 334}
335
336static int cmpstringp(const void *p1, const void *p2)
337{
338 return strcmp(*(char * const *)p1, *(char * const *)p2);
339}
340
054f95b2 341int main(int argc, char *argv[])
342{
36595fd2 343 FILE *fout, *fasm, *fhdr, *frlist;
b545ba7c 344 const struct parsed_proto *pp;
054f95b2 345 char words[20][256];
054f95b2 346 char word[256];
347 char line[256];
054f95b2 348 unsigned long val;
349 unsigned long cnt;
350 const char *sym;
351 enum dx_type type;
36595fd2 352 char **pub_syms;
353 int pub_sym_cnt = 0;
354 int pub_sym_alloc;
355 char **rlist;
356 int rlist_cnt = 0;
357 int rlist_alloc;
054f95b2 358 int is_label;
36595fd2 359 int is_bss;
054f95b2 360 int wordc;
361 int first;
362 int arg_out;
363 int arg = 1;
364 int len;
36595fd2 365 int w, i;
054f95b2 366 char *p;
367 char *p2;
368
36595fd2 369 if (argc < 4) {
370 printf("usage:\n%s <.s> <.asm> <hdrf> [rlist]*\n",
054f95b2 371 argv[0]);
372 return 1;
373 }
374
375 arg_out = arg++;
376
377 asmfn = argv[arg++];
378 fasm = fopen(asmfn, "r");
379 my_assert_not(fasm, NULL);
380
381 hdrfn = argv[arg++];
36595fd2 382 fhdr = fopen(hdrfn, "r");
383 my_assert_not(fhdr, NULL);
054f95b2 384
385 fout = fopen(argv[arg_out], "w");
386 my_assert_not(fout, NULL);
387
36595fd2 388 pub_sym_alloc = 64;
389 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
390 my_assert_not(pub_syms, NULL);
391
392 rlist_alloc = 64;
393 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
394 my_assert_not(rlist, NULL);
395
396 for (; arg < argc; arg++) {
397 frlist = fopen(argv[arg], "r");
398 my_assert_not(frlist, NULL);
399
400 while (fgets(line, sizeof(line), frlist)) {
401 p = sskip(line);
402 if (*p == 0 || *p == ';')
403 continue;
404
405 p = next_word(words[0], sizeof(words[0]), p);
406 if (words[0][0] == 0)
407 continue;
408
409 if (rlist_cnt >= rlist_alloc) {
410 rlist_alloc = rlist_alloc * 2 + 64;
411 rlist = realloc(rlist, rlist_alloc * sizeof(rlist[0]));
412 my_assert_not(rlist, NULL);
413 }
414 rlist[rlist_cnt++] = strdup(words[0]);
415 }
416
417 fclose(frlist);
418 frlist = NULL;
419 }
420
421 if (rlist_cnt > 0)
422 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
423
424 while (1) {
054f95b2 425 next_section(fasm, line);
36595fd2 426 if (feof(fasm))
427 break;
054f95b2 428 if (IS(line + 1, "text"))
429 continue;
430
431 if (IS(line + 1, "rdata"))
432 fprintf(fout, "\n.section .rodata\n");
433 else if (IS(line + 1, "data"))
434 fprintf(fout, "\n.data\n");
435 else
436 aerr("unhandled section: '%s'\n", line);
437
438 fprintf(fout, ".align 4\n");
439
440 while (fgets(line, sizeof(line), fasm))
441 {
442 sym = NULL;
443 asmln++;
444
445 p = sskip(line);
446 if (*p == 0 || *p == ';')
447 continue;
448
449 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
054f95b2 450 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
451 if (*p == 0 || *p == ';') {
452 wordc++;
453 break;
454 }
455 if (*p == ',') {
054f95b2 456 p = sskip(p + 1);
457 }
458 }
459
b545ba7c 460 if (*p == ';') {
461 p = sskip(p + 1);
462 if (IS_START(p, "sctclrtype"))
463 g_func_sym_pp = NULL;
464 }
465
054f95b2 466 if (wordc == 2 && IS(words[1], "ends"))
467 break;
36595fd2 468 if (wordc <= 2 && IS(words[0], "end"))
469 break;
054f95b2 470 if (wordc < 2)
471 aerr("unhandled: '%s'\n", words[0]);
472
473 // don't cares
474 if (IS(words[0], "assume"))
475 continue;
476
477 if (IS(words[0], "align")) {
478 val = parse_number(words[1]);
479 fprintf(fout, "\t\t .align %ld", val);
480 goto fin;
481 }
482
483 w = 1;
484 type = parse_dx_directive(words[0]);
485 if (type == DXT_UNSPEC) {
486 type = parse_dx_directive(words[1]);
487 sym = words[0];
488 w = 2;
489 }
490 if (type == DXT_UNSPEC)
491 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
492
493 if (sym != NULL) {
36595fd2 494 // public/global name
495 if (pub_sym_cnt >= pub_sym_alloc) {
496 pub_sym_alloc *= 2;
497 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
498 my_assert_not(pub_syms, NULL);
499 }
500 pub_syms[pub_sym_cnt++] = strdup(sym);
501
b545ba7c 502 pp = proto_parse(fhdr, sym, 1);
503 if (pp != NULL)
504 g_func_sym_pp = NULL;
505
054f95b2 506 len = strlen(sym);
507 fprintf(fout, "_%s:", sym);
508
509 len += 2;
510 if (len < 8)
511 fprintf(fout, "\t");
512 if (len < 16)
513 fprintf(fout, "\t");
514 if (len <= 16)
515 fprintf(fout, " ");
516 else
517 fprintf(fout, " ");
518 }
519 else {
520 fprintf(fout, "\t\t ");
521 }
522
523 if (type == DXT_BYTE && words[w][0] == '\'') {
524 // string; use asciz for most common case
525 if (w == wordc - 2 && IS(words[w + 1], "0")) {
526 fprintf(fout, ".asciz \"");
527 wordc--;
528 }
529 else
530 fprintf(fout, ".ascii \"");
531
532 for (; w < wordc; w++) {
533 if (words[w][0] == '\'') {
534 p = words[w] + 1;
535 p2 = strchr(p, '\'');
536 if (p2 == NULL)
537 aerr("unterminated string? '%s'\n", p);
538 memcpy(word, p, p2 - p);
539 word[p2 - p] = 0;
540 fprintf(fout, "%s", escape_string(word));
541 }
542 else {
543 val = parse_number(words[w]);
544 if (val & ~0xff)
545 aerr("bad string trailing byte?\n");
546 fprintf(fout, "\\x%02lx", val);
547 }
548 }
549 fprintf(fout, "\"");
550 goto fin;
551 }
552
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;
557 p2 = strchr(p, ')');
558 if (p2 == NULL)
559 aerr("bad dup?\n");
560 memmove(word, p, p2 - p);
561 word[p2 - p] = 0;
36595fd2 562
563 val = 0;
564 if (!IS(word, "?"))
565 val = parse_number(word);
054f95b2 566
567 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
568 cnt, type_size(type), val);
569 goto fin;
570 }
571 }
572
573 if (type == DXT_DWORD && words[w][0] == '\''
574 && words[w][5] == '\'' && strlen(words[w]) == 6)
575 {
576 if (w != wordc - 1)
577 aerr("TODO\n");
578
579 p = words[w];
580 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
581 fprintf(fout, ".long 0x%lx", val);
b545ba7c 582 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
054f95b2 583 goto fin;
584 }
585
36595fd2 586 if (type >= DXT_DWORD && strchr(words[w], '.'))
054f95b2 587 {
588 if (w != wordc - 1)
589 aerr("TODO\n");
590
36595fd2 591 fprintf(fout, "%s %s", type_name_float(type), words[w]);
054f95b2 592 goto fin;
593 }
594
595 first = 1;
596 fprintf(fout, "%s ", type_name(type));
597 for (; w < wordc; w++)
598 {
599 if (!first)
600 fprintf(fout, ", ");
601
36595fd2 602 is_label = is_bss = 0;
603 if (w <= wordc - 2 && IS(words[w], "offset")) {
054f95b2 604 is_label = 1;
605 w++;
606 }
36595fd2 607 else if (IS(words[w], "?")) {
608 is_bss = 1;
609 }
054f95b2 610 else if (type == DXT_DWORD
611 && !('0' <= words[w][0] && words[w][0] <= '9'))
612 {
613 // assume label
614 is_label = 1;
615 }
616
36595fd2 617 if (is_bss) {
618 fprintf(fout, "0");
619 }
620 else if (is_label) {
054f95b2 621 p = words[w];
ddaf8bd7 622 if (IS_START(p, "loc_") || IS_START(p, "__imp")
623 || strchr(p, '?') || strchr(p, '@')
36595fd2 624 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
625 cmpstringp))
054f95b2 626 {
627 fprintf(fout, "0");
b545ba7c 628 snprintf(g_comment, sizeof(g_comment), "%s", p);
36595fd2 629 }
630 else {
c0050df6 631 pp = check_var(fhdr, sym, p);
ddaf8bd7 632 if (p[0] != '_')
c0050df6 633 fprintf(fout, (pp && pp->is_fastcall) ? "@" : "_");
ddaf8bd7 634 fprintf(fout, "%s", p);
c0050df6 635 if (pp && pp->is_stdcall && pp->argc > 0)
636 fprintf(fout, "@%d", pp->argc * 4);
054f95b2 637 }
054f95b2 638 }
639 else {
640 val = parse_number(words[w]);
641 if (val < 10)
642 fprintf(fout, "%ld", val);
643 else
644 fprintf(fout, "0x%lx", val);
645 }
646
647 first = 0;
648 }
649
650fin:
b545ba7c 651 if (g_comment[0] != 0) {
652 fprintf(fout, "\t\t# %s", g_comment);
653 g_comment[0] = 0;
054f95b2 654 }
655 fprintf(fout, "\n");
054f95b2 656 }
657 }
658
36595fd2 659 fprintf(fout, "\n");
660
661 // dump public syms
662 for (i = 0; i < pub_sym_cnt; i++)
663 fprintf(fout, ".global _%s\n", pub_syms[i]);
664
054f95b2 665 fclose(fout);
666 fclose(fasm);
36595fd2 667 fclose(fhdr);
054f95b2 668
669 return 0;
670}
671
672// vim:ts=2:shiftwidth=2:expandtab