commit 6f0f69b

ricmaps  ·  2026-11-26 01:34:58 +0000 UTC
parent 433c30e
make boolean operators tail-call optimized
1 files changed,  +18, -9
M qbe.c
M qbe.c
+18, -9
 1@@ -217,6 +217,8 @@ funchlt(struct func *f) {
 2 
 3 static struct operand *funcexpr(struct func *, struct expr *);
 4 static void functail(struct func *, struct expr *);
 5+static void funcarm(struct func *, struct expr *, struct operand *, struct block *, int);
 6+static struct operand *funcjoin(struct func *, struct operand *, struct block *, int);
 7 
 8 static usize
 9 ctoridx(struct binding *b) {
10@@ -402,9 +404,8 @@ funccall(struct func *f, struct expr *e) {
11 	return r;
12 }
13 
14-
15 static struct operand *
16-funcshort(struct func *f, struct expr *e) {
17+funcshort(struct func *f, struct expr *e, int tail) {
18 	struct operand *r;
19 	struct block *rhs;
20 	struct block *skip;
21@@ -424,13 +425,15 @@ funcshort(struct func *f, struct expr *e) {
22 	else
23 		funcjnz(f, l, skip, rhs);
24 	funclabel(f, rhs);
25-	funccopy(f, r, funcexpr(f, e->args->next));
26-	funcjmp(f, join);
27+	funcarm(f, e->args->next, r, join, tail);
28 	funclabel(f, skip);
29-	funccopy(f, r, mkconst(conj ? 0 : 1));
30-	funcjmp(f, join);
31-	funclabel(f, join);
32-	return r;
33+	if(tail)
34+		funcret(f, mkconst(conj ? 0 : 1));
35+	else {
36+		funccopy(f, r, mkconst(conj ? 0 : 1));
37+		funcjmp(f, join);
38+	}
39+	return funcjoin(f, r, join, tail);
40 }
41 
42 static struct operand *
43@@ -444,7 +447,7 @@ funcop(struct func *f, struct expr *e) {
44 	int cmp = 0;
45 
46 	if(e->tok.kind == TAND || e->tok.kind == TOR)
47-		return funcshort(f, e);
48+		return funcshort(f, e, 0);
49 	if(e->tok.kind == TBANG)
50 		return funcinst(f, ICEQ, 'l', funcexpr(f, e->args), mkconst(0));
51 	t = e->args->type;
52@@ -703,6 +706,12 @@ functail(struct func *f, struct expr *e) {
53 	case EBLOCK:
54 		funcblock(f, e, 1);
55 		return;
56+	case ECALL:
57+		if(e->tok.kind == TAND || e->tok.kind == TOR) {
58+			funcshort(f, e, 1);
59+			return;
60+		}
61+		break;
62 	default:
63 		break;
64 	}