Skip to content

Commit 411bc36

Browse files
committed
added additional optimizations
1 parent c974534 commit 411bc36

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

src/optimizer.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,51 @@ const optimizers = {
5050
return e;
5151
},
5252

53+
BitwiseExpr(e) {
54+
e.left = optimize(e.left);
55+
e.right = optimize(e.right);
56+
if (e.left.kind === 'num' && e.right.kind === 'num') {
57+
let result;
58+
switch (e.op) {
59+
case '&': result = e.left.value & e.right.value; break;
60+
case '|': result = e.left.value | e.right.value; break;
61+
case '^': result = e.left.value ^ e.right.value; break;
62+
}
63+
return core.num(result);
64+
}
65+
if (e.op === '&') {
66+
if ((e.left.kind === 'num' && e.left.value === 0) ||
67+
(e.right.kind === 'num' && e.right.value === 0)) {
68+
return core.num(0);
69+
}
70+
}
71+
if (e.op === '|') {
72+
if (e.left.kind === 'num' && e.left.value === 0) return e.right;
73+
if (e.right.kind === 'num' && e.right.value === 0) return e.left;
74+
}
75+
if (e.op === '^' && e.right.kind === 'num' && e.right.value === 0) {
76+
return e.left;
77+
}
78+
return e;
79+
},
80+
81+
ShiftExpr(e) {
82+
e.left = optimize(e.left);
83+
e.right = optimize(e.right);
84+
if (e.left.kind === 'num' && e.right.kind === 'num') {
85+
let result;
86+
switch (e.op) {
87+
case '<<': result = e.left.value << e.right.value; break;
88+
case '>>': result = e.left.value >> e.right.value; break;
89+
}
90+
return core.num(result);
91+
}
92+
if (e.right.kind === 'num' && e.right.value === 0) {
93+
return e.left;
94+
}
95+
return e;
96+
},
97+
5398
AddExpr(e) {
5499
e.left = optimize(e.left);
55100
e.right = optimize(e.right);
@@ -114,7 +159,7 @@ const optimizers = {
114159
},
115160

116161
TimeCall(t) {
117-
t.funcCall = optimize(t.funcCall);
162+
t.id = optimize(t.id);
118163
t.timeValue = optimize(t.timeValue);
119164
return t;
120165
},

test/optimizer.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,81 @@ const tests = [
140140
core.factor(null, "~", id("x")),
141141
core.factor(null, "~", id("x")),
142142
],
143+
[
144+
"folds bitwise AND with constants",
145+
core.bitwiseExpr(num(5), "&", num(3)),
146+
num(1),
147+
],
148+
[
149+
"folds bitwise OR with constants",
150+
core.bitwiseExpr(num(2), "|", num(8)),
151+
num(10),
152+
],
153+
[
154+
"folds bitwise XOR with constants",
155+
core.bitwiseExpr(num(6), "^", num(3)),
156+
num(5),
157+
],
158+
[
159+
"optimizes x & 0 to 0",
160+
core.bitwiseExpr(id("x"), "&", num(0)),
161+
num(0),
162+
],
163+
[
164+
"optimizes 0 & x to 0",
165+
core.bitwiseExpr(num(0), "&", id("x")),
166+
num(0),
167+
],
168+
[
169+
"optimizes x | 0 to x",
170+
core.bitwiseExpr(id("x"), "|", num(0)),
171+
id("x"),
172+
],
173+
[
174+
"optimizes 0 | x to x",
175+
core.bitwiseExpr(num(0), "|", id("x")),
176+
id("x"),
177+
],
178+
[
179+
"optimizes x ^ 0 to x",
180+
core.bitwiseExpr(id("x"), "^", num(0)),
181+
id("x"),
182+
],
183+
[
184+
"leaves bitwise expression with variables unoptimized",
185+
core.bitwiseExpr(id("a"), "&", id("b")),
186+
core.bitwiseExpr(id("a"), "&", id("b")),
187+
],
188+
[
189+
"folds left shift with constants",
190+
core.shiftExpr(num(4), "<<", num(2)),
191+
num(16),
192+
],
193+
[
194+
"folds right shift with constants",
195+
core.shiftExpr(num(16), ">>", num(2)),
196+
num(4),
197+
],
198+
[
199+
"optimizes x << 0 to x",
200+
core.shiftExpr(id("x"), "<<", num(0)),
201+
id("x"),
202+
],
203+
[
204+
"optimizes x >> 0 to x",
205+
core.shiftExpr(id("x"), ">>", num(0)),
206+
id("x"),
207+
],
208+
[
209+
"leaves shift expression with variables unoptimized",
210+
core.shiftExpr(id("a"), "<<", id("b")),
211+
core.shiftExpr(id("a"), "<<", id("b")),
212+
],
213+
[
214+
"optimizes shift with negative numbers (JS semantics)",
215+
core.shiftExpr(num(-4), ">>", num(1)),
216+
num(-2), // In JS, -4 >> 1 is -2
217+
],
143218
[
144219
"optimizes print statement expression",
145220
core.printStmt(add(num(2), "+", num(3))),

0 commit comments

Comments
 (0)