From 5a0e0c56aab08388833892129eae25be8e9dd208 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 26 May 2022 20:26:21 -0700 Subject: [PATCH] Added tail-call optimization --- src/uxnasm.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 1e7894e..93cefad 100644 --- 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,9 +200,25 @@ 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) { @@ -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) {