commit 5a0e0c56aab08388833892129eae25be8e9dd208
parent e00e74b9d043fef03a3692251fb792eabfcb4cd9
Author: Devine Lu Linvega <aliceffekt@gmail.com>
Date:   Thu, 26 May 2022 20:26:21 -0700
Added tail-call optimization
Diffstat:
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/src/uxnasm.c b/src/uxnasm.c
@@ -45,6 +45,7 @@ typedef struct {
 
 Program p;
 static int litlast = 0;
+static int jsrlast = 0;
 
 /* clang-format off */
 
@@ -199,10 +200,26 @@ writebyte(Uint8 b)
 	p.data[p.ptr++] = b;
 	p.length = p.ptr;
 	litlast = 0;
+	jsrlast = 0;
 	return 1;
 }
 
 static int
+writeopcode(char *w)
+{
+	Uint8 res;
+	if(jsrlast && scmp(w, "JMP2r", 5)) { /* combine JSR2 JMP2r */
+		p.data[p.ptr - 1] = findopcode("JMP2");
+		jsrlast = 0;
+		return 1;
+	}
+	res = writebyte(findopcode(w));
+	if(scmp(w, "JSR2", 4))
+		jsrlast = 1;
+	return res;
+}
+
+static int
 writeshort(Uint16 s, int lit)
 {
 	if(lit)
@@ -329,7 +346,7 @@ parse(char *w, FILE *f)
 	default:
 		/* opcode */
 		if(findopcode(w) || scmp(w, "BRK", 4)) {
-			if(!writebyte(findopcode(w))) return 0;
+			if(!writeopcode(w)) return 0;
 		}
 		/* raw byte */
 		else if(sihx(w) && slen(w) == 2) {