Browse code

Added tail-call optimization

Devine Lu Linvega authored on 27/05/2022 03:26:21
Showing 1 changed files
... ...
@@ -45,6 +45,7 @@ typedef struct {
45 45
 
46 46
 Program p;
47 47
 static int litlast = 0;
48
+static int jsrlast = 0;
48 49
 
49 50
 /* clang-format off */
50 51
 
... ...
@@ -199,9 +200,25 @@ writebyte(Uint8 b)
199 200
 	p.data[p.ptr++] = b;
200 201
 	p.length = p.ptr;
201 202
 	litlast = 0;
203
+	jsrlast = 0;
202 204
 	return 1;
203 205
 }
204 206
 
207
+static int
208
+writeopcode(char *w)
209
+{
210
+	Uint8 res;
211
+	if(jsrlast && scmp(w, "JMP2r", 5)) { /* combine JSR2 JMP2r */
212
+		p.data[p.ptr - 1] = findopcode("JMP2");
213
+		jsrlast = 0;
214
+		return 1;
215
+	}
216
+	res = writebyte(findopcode(w));
217
+	if(scmp(w, "JSR2", 4))
218
+		jsrlast = 1;
219
+	return res;
220
+}
221
+
205 222
 static int
206 223
 writeshort(Uint16 s, int lit)
207 224
 {
... ...
@@ -329,7 +346,7 @@ parse(char *w, FILE *f)
329 346
 	default:
330 347
 		/* opcode */
331 348
 		if(findopcode(w) || scmp(w, "BRK", 4)) {
332
-			if(!writebyte(findopcode(w))) return 0;
349
+			if(!writeopcode(w)) return 0;
333 350
 		}
334 351
 		/* raw byte */
335 352
 		else if(sihx(w) && slen(w) == 2) {