fixes, standalone version builds
[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
258static void check_var(FILE *fhdr, const char *sym, const char *varname)
259{
260 const struct parsed_proto *pp, *pp_sym;
261 char fp_sym[256], fp_var[256];
262 int i, bad = 0;
263
264 pp = proto_parse(fhdr, varname, 1);
36595fd2 265 if (pp == NULL) {
b545ba7c 266 if (IS_START(varname, "sub_"))
267 awarn("sub_ sym missing proto: '%s'\n", varname);
36595fd2 268 return;
269 }
270
271 if (!pp->is_func && !pp->is_fptr)
272 return;
b545ba7c 273
274 sprint_pp(pp, fp_var, sizeof(fp_var));
275
36595fd2 276 if (pp->argc_reg == 0)
b545ba7c 277 goto check_sym;
36595fd2 278 if (pp->argc_reg == 1 && pp->argc_stack == 0
279 && IS(pp->arg[0].reg, "ecx"))
280 {
b545ba7c 281 goto check_sym;
36595fd2 282 }
b545ba7c 283 if (pp->argc_reg != 2
284 || !IS(pp->arg[0].reg, "ecx")
285 || !IS(pp->arg[1].reg, "edx"))
36595fd2 286 {
b545ba7c 287 awarn("unhandled reg call: %s\n", fp_var);
36595fd2 288 }
36595fd2 289
b545ba7c 290check_sym:
291 sprint_pp_short(pp, g_comment, sizeof(g_comment));
292
293 if (sym != NULL) {
294 g_func_sym_pp = NULL;
295 pp_sym = proto_parse(fhdr, sym, 1);
296 if (pp_sym == NULL)
297 return;
298 if (!pp_sym->is_fptr)
299 aerr("func ptr data, but label '%s' !is_fptr\n", pp_sym->name);
300 g_func_sym_pp = pp_sym;
36595fd2 301 }
b545ba7c 302 else {
303 pp_sym = g_func_sym_pp;
304 if (pp_sym == NULL)
305 return;
306 }
307
308 if (pp->argc != pp_sym->argc || pp->argc_reg != pp_sym->argc_reg)
309 bad = 1;
310 else {
311 for (i = 0; i < pp->argc; i++) {
312 if ((pp->arg[i].reg != NULL) != (pp_sym->arg[i].reg != NULL)) {
313 bad = 1;
314 break;
315 }
316 if ((pp->arg[i].reg != NULL)
317 && !IS(pp->arg[i].reg, pp_sym->arg[i].reg))
318 {
319 bad = 1;
320 break;
321 }
322 }
323 }
324
325 if (bad) {
326 sprint_pp(pp_sym, fp_sym, sizeof(fp_sym));
327 anote("var: %s\n", fp_var);
328 anote("sym: %s\n", fp_sym);
329 awarn("^ mismatch\n");
36595fd2 330 }
36595fd2 331}
332
333static int cmpstringp(const void *p1, const void *p2)
334{
335 return strcmp(*(char * const *)p1, *(char * const *)p2);
336}
337
054f95b2 338int main(int argc, char *argv[])
339{
36595fd2 340 FILE *fout, *fasm, *fhdr, *frlist;
b545ba7c 341 const struct parsed_proto *pp;
054f95b2 342 char words[20][256];
054f95b2 343 char word[256];
344 char line[256];
054f95b2 345 unsigned long val;
346 unsigned long cnt;
347 const char *sym;
348 enum dx_type type;
36595fd2 349 char **pub_syms;
350 int pub_sym_cnt = 0;
351 int pub_sym_alloc;
352 char **rlist;
353 int rlist_cnt = 0;
354 int rlist_alloc;
054f95b2 355 int is_label;
36595fd2 356 int is_bss;
054f95b2 357 int wordc;
358 int first;
359 int arg_out;
360 int arg = 1;
361 int len;
36595fd2 362 int w, i;
054f95b2 363 char *p;
364 char *p2;
365
36595fd2 366 if (argc < 4) {
367 printf("usage:\n%s <.s> <.asm> <hdrf> [rlist]*\n",
054f95b2 368 argv[0]);
369 return 1;
370 }
371
372 arg_out = arg++;
373
374 asmfn = argv[arg++];
375 fasm = fopen(asmfn, "r");
376 my_assert_not(fasm, NULL);
377
378 hdrfn = argv[arg++];
36595fd2 379 fhdr = fopen(hdrfn, "r");
380 my_assert_not(fhdr, NULL);
054f95b2 381
382 fout = fopen(argv[arg_out], "w");
383 my_assert_not(fout, NULL);
384
36595fd2 385 pub_sym_alloc = 64;
386 pub_syms = malloc(pub_sym_alloc * sizeof(pub_syms[0]));
387 my_assert_not(pub_syms, NULL);
388
389 rlist_alloc = 64;
390 rlist = malloc(rlist_alloc * sizeof(rlist[0]));
391 my_assert_not(rlist, NULL);
392
393 for (; arg < argc; arg++) {
394 frlist = fopen(argv[arg], "r");
395 my_assert_not(frlist, NULL);
396
397 while (fgets(line, sizeof(line), frlist)) {
398 p = sskip(line);
399 if (*p == 0 || *p == ';')
400 continue;
401
402 p = next_word(words[0], sizeof(words[0]), p);
403 if (words[0][0] == 0)
404 continue;
405
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);
410 }
411 rlist[rlist_cnt++] = strdup(words[0]);
412 }
413
414 fclose(frlist);
415 frlist = NULL;
416 }
417
418 if (rlist_cnt > 0)
419 qsort(rlist, rlist_cnt, sizeof(rlist[0]), cmpstringp);
420
421 while (1) {
054f95b2 422 next_section(fasm, line);
36595fd2 423 if (feof(fasm))
424 break;
054f95b2 425 if (IS(line + 1, "text"))
426 continue;
427
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");
432 else
433 aerr("unhandled section: '%s'\n", line);
434
435 fprintf(fout, ".align 4\n");
436
437 while (fgets(line, sizeof(line), fasm))
438 {
439 sym = NULL;
440 asmln++;
441
442 p = sskip(line);
443 if (*p == 0 || *p == ';')
444 continue;
445
446 for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
054f95b2 447 p = sskip(next_word_s(words[wordc], sizeof(words[0]), p));
448 if (*p == 0 || *p == ';') {
449 wordc++;
450 break;
451 }
452 if (*p == ',') {
054f95b2 453 p = sskip(p + 1);
454 }
455 }
456
b545ba7c 457 if (*p == ';') {
458 p = sskip(p + 1);
459 if (IS_START(p, "sctclrtype"))
460 g_func_sym_pp = NULL;
461 }
462
054f95b2 463 if (wordc == 2 && IS(words[1], "ends"))
464 break;
36595fd2 465 if (wordc <= 2 && IS(words[0], "end"))
466 break;
054f95b2 467 if (wordc < 2)
468 aerr("unhandled: '%s'\n", words[0]);
469
470 // don't cares
471 if (IS(words[0], "assume"))
472 continue;
473
474 if (IS(words[0], "align")) {
475 val = parse_number(words[1]);
476 fprintf(fout, "\t\t .align %ld", val);
477 goto fin;
478 }
479
480 w = 1;
481 type = parse_dx_directive(words[0]);
482 if (type == DXT_UNSPEC) {
483 type = parse_dx_directive(words[1]);
484 sym = words[0];
485 w = 2;
486 }
487 if (type == DXT_UNSPEC)
488 aerr("unhandled decl: '%s %s'\n", words[0], words[1]);
489
490 if (sym != NULL) {
36595fd2 491 // public/global name
492 if (pub_sym_cnt >= pub_sym_alloc) {
493 pub_sym_alloc *= 2;
494 pub_syms = realloc(pub_syms, pub_sym_alloc * sizeof(pub_syms[0]));
495 my_assert_not(pub_syms, NULL);
496 }
497 pub_syms[pub_sym_cnt++] = strdup(sym);
498
b545ba7c 499 pp = proto_parse(fhdr, sym, 1);
500 if (pp != NULL)
501 g_func_sym_pp = NULL;
502
054f95b2 503 len = strlen(sym);
504 fprintf(fout, "_%s:", sym);
505
506 len += 2;
507 if (len < 8)
508 fprintf(fout, "\t");
509 if (len < 16)
510 fprintf(fout, "\t");
511 if (len <= 16)
512 fprintf(fout, " ");
513 else
514 fprintf(fout, " ");
515 }
516 else {
517 fprintf(fout, "\t\t ");
518 }
519
520 if (type == DXT_BYTE && words[w][0] == '\'') {
521 // string; use asciz for most common case
522 if (w == wordc - 2 && IS(words[w + 1], "0")) {
523 fprintf(fout, ".asciz \"");
524 wordc--;
525 }
526 else
527 fprintf(fout, ".ascii \"");
528
529 for (; w < wordc; w++) {
530 if (words[w][0] == '\'') {
531 p = words[w] + 1;
532 p2 = strchr(p, '\'');
533 if (p2 == NULL)
534 aerr("unterminated string? '%s'\n", p);
535 memcpy(word, p, p2 - p);
536 word[p2 - p] = 0;
537 fprintf(fout, "%s", escape_string(word));
538 }
539 else {
540 val = parse_number(words[w]);
541 if (val & ~0xff)
542 aerr("bad string trailing byte?\n");
543 fprintf(fout, "\\x%02lx", val);
544 }
545 }
546 fprintf(fout, "\"");
547 goto fin;
548 }
549
550 if (w == wordc - 2) {
551 if (IS_START(words[w + 1], "dup(")) {
552 cnt = parse_number(words[w]);
553 p = words[w + 1] + 4;
554 p2 = strchr(p, ')');
555 if (p2 == NULL)
556 aerr("bad dup?\n");
557 memmove(word, p, p2 - p);
558 word[p2 - p] = 0;
36595fd2 559
560 val = 0;
561 if (!IS(word, "?"))
562 val = parse_number(word);
054f95b2 563
564 fprintf(fout, ".fill 0x%02lx,%d,0x%02lx",
565 cnt, type_size(type), val);
566 goto fin;
567 }
568 }
569
570 if (type == DXT_DWORD && words[w][0] == '\''
571 && words[w][5] == '\'' && strlen(words[w]) == 6)
572 {
573 if (w != wordc - 1)
574 aerr("TODO\n");
575
576 p = words[w];
577 val = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
578 fprintf(fout, ".long 0x%lx", val);
b545ba7c 579 snprintf(g_comment, sizeof(g_comment), "%s", words[w]);
054f95b2 580 goto fin;
581 }
582
36595fd2 583 if (type >= DXT_DWORD && strchr(words[w], '.'))
054f95b2 584 {
585 if (w != wordc - 1)
586 aerr("TODO\n");
587
36595fd2 588 fprintf(fout, "%s %s", type_name_float(type), words[w]);
054f95b2 589 goto fin;
590 }
591
592 first = 1;
593 fprintf(fout, "%s ", type_name(type));
594 for (; w < wordc; w++)
595 {
596 if (!first)
597 fprintf(fout, ", ");
598
36595fd2 599 is_label = is_bss = 0;
600 if (w <= wordc - 2 && IS(words[w], "offset")) {
054f95b2 601 is_label = 1;
602 w++;
603 }
36595fd2 604 else if (IS(words[w], "?")) {
605 is_bss = 1;
606 }
054f95b2 607 else if (type == DXT_DWORD
608 && !('0' <= words[w][0] && words[w][0] <= '9'))
609 {
610 // assume label
611 is_label = 1;
612 }
613
36595fd2 614 if (is_bss) {
615 fprintf(fout, "0");
616 }
617 else if (is_label) {
054f95b2 618 p = words[w];
ddaf8bd7 619 if (IS_START(p, "loc_") || IS_START(p, "__imp")
620 || strchr(p, '?') || strchr(p, '@')
36595fd2 621 || bsearch(&p, rlist, rlist_cnt, sizeof(rlist[0]),
622 cmpstringp))
054f95b2 623 {
624 fprintf(fout, "0");
b545ba7c 625 snprintf(g_comment, sizeof(g_comment), "%s", p);
36595fd2 626 }
627 else {
b545ba7c 628 check_var(fhdr, sym, p);
ddaf8bd7 629 if (p[0] != '_')
630 fprintf(fout, "_");
631 fprintf(fout, "%s", p);
054f95b2 632 }
054f95b2 633 }
634 else {
635 val = parse_number(words[w]);
636 if (val < 10)
637 fprintf(fout, "%ld", val);
638 else
639 fprintf(fout, "0x%lx", val);
640 }
641
642 first = 0;
643 }
644
645fin:
b545ba7c 646 if (g_comment[0] != 0) {
647 fprintf(fout, "\t\t# %s", g_comment);
648 g_comment[0] = 0;
054f95b2 649 }
650 fprintf(fout, "\n");
054f95b2 651 }
652 }
653
36595fd2 654 fprintf(fout, "\n");
655
656 // dump public syms
657 for (i = 0; i < pub_sym_cnt; i++)
658 fprintf(fout, ".global _%s\n", pub_syms[i]);
659
054f95b2 660 fclose(fout);
661 fclose(fasm);
36595fd2 662 fclose(fhdr);
054f95b2 663
664 return 0;
665}
666
667// vim:ts=2:shiftwidth=2:expandtab