(uxnasm)Optimize tail-call for subroutines too

This commit is contained in:
Devine Lu Linvega 2022-05-31 13:55:00 -07:00
parent 3c06af41c3
commit 18b73f4861
1 changed files with 9 additions and 3 deletions

View File

@ -208,12 +208,18 @@ static int
writeopcode(char *w) writeopcode(char *w)
{ {
Uint8 res; Uint8 res;
if(jsrlast && scmp(w, "JMP2r", 5)) { /* combine JSR2 JMP2r */ /* tail-call optimization */
p.data[p.ptr - 1] = findopcode("JMP2"); if(scmp(w, "JMP2r", 5)) {
return jsrlast--; if(jsrlast) {
p.data[p.ptr - 1] = jsrlast == 2 ? findopcode("JMP2") : findopcode("JMP");
jsrlast = 0;
return 1;
}
} }
res = writebyte(findopcode(w)); res = writebyte(findopcode(w));
if(scmp(w, "JSR2", 4)) if(scmp(w, "JSR2", 4))
jsrlast = 2;
else if(scmp(w, "JSR", 3))
jsrlast = 1; jsrlast = 1;
return res; return res;
} }