From 18b73f48611f565f3b01728da9f1a60c5cf3d435 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Tue, 31 May 2022 13:55:00 -0700 Subject: [PATCH] (uxnasm)Optimize tail-call for subroutines too --- src/uxnasm.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 60aa9c2..7464e12 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -208,12 +208,18 @@ static int writeopcode(char *w) { Uint8 res; - if(jsrlast && scmp(w, "JMP2r", 5)) { /* combine JSR2 JMP2r */ - p.data[p.ptr - 1] = findopcode("JMP2"); - return jsrlast--; + /* tail-call optimization */ + if(scmp(w, "JMP2r", 5)) { + if(jsrlast) { + 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; + else if(scmp(w, "JSR", 3)) jsrlast = 1; return res; }