X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tools%2Fcvt_data.c;h=bc9b2dae2499c086bc976f44cc597d10f3b45277;hb=9ea60b8d585086fa64f7a8cd298ec1cd698ad56d;hp=237abef7c11dc2246f334ee353e6f8dfdd7562c8;hpb=179b79a987ed0f464f47f724032aff6c90bc50c0;p=ia32rtools.git diff --git a/tools/cvt_data.c b/tools/cvt_data.c index 237abef..bc9b2da 100644 --- a/tools/cvt_data.c +++ b/tools/cvt_data.c @@ -13,6 +13,7 @@ #include "my_assert.h" #include "my_str.h" +#include "common.h" #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) #define IS(w, y) !strcmp(w, y) @@ -87,7 +88,7 @@ static void next_section(FILE *fasm, char *name) name[0] = 0; - while (fgets(line, sizeof(line), fasm)) + while (my_fgets(line, sizeof(line), fasm)) { wordc = 0; asmln++; @@ -96,14 +97,8 @@ static void next_section(FILE *fasm, char *name) if (*p == 0) continue; - if (*p == ';') { - while (strlen(line) == sizeof(line) - 1) { - // one of those long comment lines.. - if (!fgets(line, sizeof(line), fasm)) - break; - } + if (*p == ';') continue; - } for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) { p = sskip(next_word(words[wordc], sizeof(words[0]), p)); @@ -381,7 +376,7 @@ static int is_unwanted_sym(const char *sym) int main(int argc, char *argv[]) { - FILE *fout, *fasm, *fhdr, *frlist; + FILE *fout, *fasm, *fhdr = NULL, *frlist; const struct parsed_proto *pp; int no_decorations = 0; char comment_char = '#'; @@ -399,6 +394,8 @@ int main(int argc, char *argv[]) char **rlist; int rlist_cnt = 0; int rlist_alloc; + int header_mode = 0; + int is_ro = 0; int is_label; int is_bss; int wordc; @@ -412,8 +409,9 @@ int main(int argc, char *argv[]) if (argc < 4) { // -nd: no symbol decorations - printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> [rlist]*\n", - argv[0]); + printf("usage:\n%s [-nd] [-i] [-a] <.s> <.asm> [rlist]*\n" + "%s -hdr <.h> <.asm>\n", + argv[0], argv[0]); return 1; } @@ -426,6 +424,8 @@ int main(int argc, char *argv[]) comment_char = '@'; g_arm_mode = 1; } + else if (IS(argv[arg], "-hdr")) + header_mode = 1; else break; } @@ -436,9 +436,11 @@ int main(int argc, char *argv[]) fasm = fopen(asmfn, "r"); my_assert_not(fasm, NULL); - hdrfn = argv[arg++]; - fhdr = fopen(hdrfn, "r"); - my_assert_not(fhdr, NULL); + if (!header_mode) { + hdrfn = argv[arg++]; + fhdr = fopen(hdrfn, "r"); + my_assert_not(fhdr, NULL); + } fout = fopen(argv[arg_out], "w"); my_assert_not(fout, NULL); @@ -455,7 +457,7 @@ int main(int argc, char *argv[]) frlist = fopen(argv[arg], "r"); my_assert_not(frlist, NULL); - while (fgets(line, sizeof(line), frlist)) { + while (my_fgets(line, sizeof(line), frlist)) { p = sskip(line); if (*p == 0 || *p == ';') continue; @@ -491,16 +493,23 @@ int main(int argc, char *argv[]) if (IS(line + 1, "text")) continue; - if (IS(line + 1, "rdata")) - fprintf(fout, "\n.section .rodata\n"); - else if (IS(line + 1, "data")) - fprintf(fout, "\n.data\n"); + if (IS(line + 1, "rdata")) { + is_ro = 1; + if (!header_mode) + fprintf(fout, "\n.section .rodata\n"); + } + else if (IS(line + 1, "data")) { + is_ro = 0; + if (!header_mode) + fprintf(fout, "\n.data\n"); + } else aerr("unhandled section: '%s'\n", line); - fprintf(fout, ".align %d\n", align_value(4)); + if (!header_mode) + fprintf(fout, ".align %d\n", align_value(4)); - while (fgets(line, sizeof(line), fasm)) + while (my_fgets(line, sizeof(line), fasm)) { sym = NULL; asmln++; @@ -513,7 +522,7 @@ int main(int argc, char *argv[]) if (IS_START(p, ";org") && sscanf(p + 5, "%Xh", &i) == 1) { // ;org is only seen at section start, so assume . addr 0 i &= 0xfff; - if (i != 0) + if (i != 0 && !header_mode) fprintf(fout, "\t\t .skip 0x%x\n", i); } continue; @@ -548,6 +557,9 @@ int main(int argc, char *argv[]) continue; if (IS(words[0], "align")) { + if (header_mode) + continue; + val = parse_number(words[1]); fprintf(fout, "\t\t .align %d", align_value(val)); goto fin; @@ -563,7 +575,42 @@ int main(int argc, char *argv[]) if (type == DXT_UNSPEC) aerr("unhandled decl: '%s %s'\n", words[0], words[1]); - if (sym != NULL) { + if (sym != NULL) + { + if (header_mode) { + int is_str = 0; + + fprintf(fout, "extern "); + if (is_ro) + fprintf(fout, "const "); + + switch (type) { + case DXT_BYTE: + for (i = w; i < wordc; i++) + if (words[i][0] == '\'') + is_str = 1; + if (is_str) + fprintf(fout, "char %s[];\n", sym); + else + fprintf(fout, "uint8_t %s;\n", sym); + break; + + case DXT_WORD: + fprintf(fout, "uint16_t %s;\n", sym); + break; + + case DXT_DWORD: + fprintf(fout, "uint32_t %s;\n", sym); + break; + + default: + fprintf(fout, "_UNKNOWN %s;\n", sym); + break; + } + + continue; + } + snprintf(last_sym, sizeof(last_sym), "%s", sym); pp = proto_parse(fhdr, sym, 1); @@ -593,6 +640,9 @@ int main(int argc, char *argv[]) fprintf(fout, " "); } else { + if (header_mode) + continue; + fprintf(fout, "\t\t "); } @@ -774,7 +824,8 @@ fin: fclose(fout); fclose(fasm); - fclose(fhdr); + if (fhdr != NULL) + fclose(fhdr); return 0; }