commit 18b73f48611f565f3b01728da9f1a60c5cf3d435
parent 3c06af41c3788f9b9c4968e763ee7449d13c1543
Author: Devine Lu Linvega <aliceffekt@gmail.com>
Date: Tue, 31 May 2022 13:55:00 -0700
(uxnasm)Optimize tail-call for subroutines too
Diffstat:
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git 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;
}