Fixed bug that caused incorrect label addresses during the
authorLukas Krickl <lukas@krickl.dev>
Sun, 10 Dec 2023 12:39:59 +0000 (13:39 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 10 Dec 2023 12:39:59 +0000 (13:39 +0100)
first pass

include/ulas.h
src/ulas.c
tests/t0.s

index c598e56e94a8b74122f5cf6e3953b9279fb6d091..34c68388a337dea7d855b7df3207f7415b10135d 100644 (file)
     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
  */
index c4de70799de49ff69602ab825fb19e0729948967..2815dc6622ac6851e43a34bc7686621075a602d0 100644 (file)
@@ -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);
index 2dbd3f0b6eba59b64a268aaf7aac205c0de3768e..60f35048560ee2227c30b43e7526c4436b55a43c 100644 (file)
@@ -97,3 +97,6 @@ l2:
 .incbin "tests/inc.bin"
 #include "tests/t1.s"
   nop
+  jp j1
+j1:
+  jp j1