Browse code

(uxnasm)Optimize tail-call for subroutines too

Devine Lu Linvega authored on 31/05/2022 20:55:00
Showing 1 changed files
... ...
@@ -208,12 +208,18 @@ static int
208 208
 writeopcode(char *w)
209 209
 {
210 210
 	Uint8 res;
211
-	if(jsrlast && scmp(w, "JMP2r", 5)) { /* combine JSR2 JMP2r */
212
-		p.data[p.ptr - 1] = findopcode("JMP2");
213
-		return jsrlast--;
211
+	/* tail-call optimization */
212
+	if(scmp(w, "JMP2r", 5)) {
213
+		if(jsrlast) {
214
+			p.data[p.ptr - 1] = jsrlast == 2 ? findopcode("JMP2") : findopcode("JMP");
215
+			jsrlast = 0;
216
+			return 1;
217
+		}
214 218
 	}
215 219
 	res = writebyte(findopcode(w));
216 220
 	if(scmp(w, "JSR2", 4))
221
+		jsrlast = 2;
222
+	else if(scmp(w, "JSR", 3))
217 223
 		jsrlast = 1;
218 224
 	return res;
219 225
 }