commit a469b2a48f9c5a6b2c6f4e4e61b9cec9011fee06
parent 81418dfc8eca7f85e745b31e2d2d15b73058a7fb
Author: aabacchus <ben@bvnf.space>
Date: Sun, 5 Jun 2022 23:27:18 +0100
write debug info to buffer and save at the end
This allows the optimizations to be re-enabled.
dbg must be initialised to contain all zeroes as any empty bytes remain
to be 0, which causes the debug file to contain an empty line.
Diffstat:
M | uxndebug.c | | | 54 | +++++++++++++++++++++++++++++++----------------------- |
1 file changed, 31 insertions(+), 23 deletions(-)
diff --git a/uxndebug.c b/uxndebug.c
@@ -43,14 +43,16 @@ typedef struct {
char scope[0x40];
} Program;
+typedef struct {
+ unsigned int tokens[LENGTH];
+} Debug_info;
+
Program p;
static int litlast = 0;
static int jsrlast = 0;
+Debug_info dbg = {0};
static int token_number = 0;
-static int debug_lineno = 0;
-static char *debug_filename = "debug.tal.debug";
-FILE *debug_file = NULL;
/* clang-format off */
@@ -196,13 +198,7 @@ makereference(char *scope, char *label, Uint16 addr)
static void
debug_next_byte(void) {
- while (p.ptr - 0x0100 != debug_lineno) {
- fprintf(debug_file, "\n");
- debug_lineno++;
- /* TODO: better way to handle this is to write to buffer with byte,token pairs, then write to file at end */
- }
- fprintf(debug_file, "%d\n", token_number);
- debug_lineno++;
+ dbg.tokens[p.ptr - TRIM] = token_number;
}
static int
@@ -226,14 +222,11 @@ static int
writeopcode(char *w)
{
Uint8 res;
- /* disable optimizations which go back
if(jsrlast && scmp(w, "JMP2r", 5)) { /* tail-call optimization */
- /*
p.data[p.ptr - 1] = jsrlast == 2 ? findopcode("JMP2") : findopcode("JMP");
jsrlast = 0;
return 1;
}
- */
res = writebyte(findopcode(w));
if(scmp(w, "JSR2", 4))
jsrlast = 2;
@@ -253,15 +246,12 @@ writeshort(Uint16 s, int lit)
static int
writelitbyte(Uint8 b)
{
- /* disable optimizations which go back
if(litlast) { /* literals optimization */
- /*
Uint8 hb = p.data[p.ptr - 1];
p.ptr -= 2;
p.length = p.ptr;
return writeshort((hb << 8) + b, 1);
}
- */
if(!writebyte(findopcode("LIT"))) return 0;
if(!writebyte(b)) return 0;
litlast = 1;
@@ -471,16 +461,33 @@ review(char *filename)
p.mlen);
}
+static int
+save_debug_info(char *srcname) {
+ unsigned int i;
+ char debug_filename[0x100];
+ FILE *debug_file;
+
+ if (snprintf(debug_filename, 0x100, "%s.debug", srcname) < 0)
+ return 0;
+ debug_file = fopen(debug_filename, "w");
+ if (debug_file == NULL)
+ return error(debug_filename, "Failed to open.");
+
+ for (i = 0; i < p.length - TRIM; i++) {
+ if (dbg.tokens[i] == 0)
+ fprintf(debug_file, "\n");
+ else
+ fprintf(debug_file, "%d\n", dbg.tokens[i]);
+ }
+
+ fclose(debug_file);
+ return 1;
+}
+
int
main(int argc, char *argv[])
{
FILE *src, *dst;
- debug_file = fopen(debug_filename, "w");
- if (debug_file == NULL) {
- perror(debug_filename);
- return 1;
- }
-
if(argc < 3)
return !error("usage", "input.tal output.rom");
if(!(src = fopen(argv[1], "r")))
@@ -493,6 +500,7 @@ main(int argc, char *argv[])
return !error("Assembly", "Output rom is empty.");
fwrite(p.data + TRIM, p.length - TRIM, 1, dst);
review(argv[2]);
- fclose(debug_file);
+ if (!save_debug_info(argv[1]))
+ return !error("Debug", "Failed to save info.");
return 0;
}