-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathreloop.js
71 lines (63 loc) · 1.88 KB
/
reloop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var binaryen = require("binaryen");
binaryen.setOptimizeLevel(3);
binaryen.setShrinkLevel(0);
function usingBranches() {
var module = new binaryen.Module();
var rl = new binaryen.Relooper(module);
var entry = rl.addBlock(module.nop());
for (let i = 0; i < 10; ++i) {
rl.addBranch(entry,
rl.addBlock(
module.return(
module.call("other", [ module.i32.const(i) ], binaryen.none, binaryen.i32)
)
),
module.i32.eq(
module.local.get(0, binaryen.i32),
module.i32.const(i)
)
);
}
rl.addBranch(entry,
rl.addBlock(
module.unreachable()
)
);
module.addFunction("test", binaryen.i32, binaryen.i32, [ binaryen.i32 ], rl.renderAndDispose(entry, 1));
module.addFunctionImport("other", "env", "other", binaryen.i32, binaryen.i32);
module.addFunctionExport("test", "test");
console.log("=== unoptimized ===");
console.log(module.emitText());
module.optimize();
console.log("=== optimized ===");
console.log(module.emitText());
}
usingBranches();
function usingSwitch() {
var module = new binaryen.Module();
var rl = new binaryen.Relooper(module);
var entry = rl.addBlockWithSwitch(module.nop(), module.local.get(0, binaryen.i32));
for (let i = 0; i < 10; ++i) {
rl.addBranchForSwitch(entry,
rl.addBlock(
module.return(
module.call("other", [ module.i32.const(i) ], binaryen.none, binaryen.i32)
)
),
[ i ]
);
}
rl.addBranchForSwitch(entry,
rl.addBlock(
module.unreachable()
),
[]
);
module.addFunction("test", binaryen.i32, binaryen.i32, [ binaryen.i32 ], rl.renderAndDispose(entry, 1));
module.addFunctionImport("other", "env", "other", binaryen.none, binaryen.i32);
module.addFunctionExport("test", "test");
console.log(module.emitText());
module.optimize();
console.log(module.emitText());
}
// usingSwitch();