From 37d18513d8c09d6296de6c8e2e93e8621bfb64bc Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sun, 10 Dec 2023 13:39:59 +0100 Subject: [PATCH] Fixed bug that caused incorrect label addresses during the first pass --- include/ulas.h | 13 +++++++++++++ src/ulas.c | 13 ++++++++----- tests/t0.s | 3 +++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/include/ulas.h b/include/ulas.h index c598e56..34c6838 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -74,6 +74,19 @@ fprintf((f), "%s", (fmt)); \ } +// this is a bit of a hack to get the int expression to evaluate anyway +// because expressions only work in the final pass +// Beware that this can cause unforseen writes to the file and should really +// only be uesd to evalulate an expression that needs to be evaled during +// all passes and nothing else! +#define ULAS_EVALEXPRS(...) \ + { \ + int pass = ulas.pass; \ + ulas.pass = ULAS_PASS_FINAL; \ + __VA_ARGS__; \ + ulas.pass = pass; \ + } + /** * Output target files */ diff --git a/src/ulas.c b/src/ulas.c index c4de707..2815dc6 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -1017,11 +1017,11 @@ found: if (rc != -1) { rc = found_dir; } - + free(ulas.filename); ulas.filename = prev_path; ulas.line = prev_lines; - + fclose(f); fclose(tmp); return rc; @@ -2326,8 +2326,9 @@ int ulas_asmdirfill(FILE *dst, const char **line, unsigned long n, int *rc) { ULASERR("Expected ,\n"); return 0; } + int count = 0; - int count = ulas_intexpr(line, n, rc); + ULAS_EVALEXPRS(count = ulas_intexpr(line, n, rc)); if (count < 0) { ULASERR("Count must be positive\n"); return 0; @@ -2434,9 +2435,11 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { } switch (dir) { - case ULAS_ASMDIR_ORG: - ulas.address = ulas_intexpr(&line, strnlen(start, n), &rc); + case ULAS_ASMDIR_ORG: { + ULAS_EVALEXPRS(ulas.address = + ulas_intexpr(&line, strnlen(start, n), &rc)); break; + } case ULAS_ASMDIR_DEF: // only do this in the final pass rc = ulas_asmdirdef(&line, n); diff --git a/tests/t0.s b/tests/t0.s index 2dbd3f0..60f3504 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -97,3 +97,6 @@ l2: .incbin "tests/inc.bin" #include "tests/t1.s" nop + jp j1 +j1: + jp j1 -- 2.30.2