Skip to content

Commit 7d3ddd0

Browse files
dcodeIOkripken
authored andcommitted
Add Module#emitStackIR to the JS-API (#1717)
Related to #1716 (comment)
1 parent 9c7b910 commit 7d3ddd0

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/js/binaryen.js-post.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,16 @@ Module['Module'] = function(module) {
11621162
out = old;
11631163
return ret;
11641164
};
1165+
this['emitStackIR'] = function(optimize) {
1166+
this['runPasses'](['generate-stack-ir']);
1167+
if (optimize) this['runPasses'](['optimize-stack-ir']);
1168+
var old = out;
1169+
var ret = '';
1170+
out = function(x) { ret += x + '\n' };
1171+
this['runPasses'](['print-stack-ir']);
1172+
out = old;
1173+
return ret;
1174+
};
11651175
this['emitAsmjs'] = function() {
11661176
var old = out;
11671177
var ret = '';

test/binaryen.js/stackir.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var wast = `
2+
(module
3+
(type $i (func (param i32) (result i32)))
4+
(memory $0 0)
5+
(export "test" (func $test))
6+
(func $test (; 0 ;) (type $i) (param $0 i32) (result i32)
7+
(block (result i32)
8+
(block (result i32)
9+
(if (result i32)
10+
(get_local $0)
11+
(get_local $0)
12+
(i32.const 0)
13+
)
14+
)
15+
)
16+
)
17+
)
18+
`;
19+
console.log("=== input wast ===" + wast);
20+
21+
var module = Binaryen.parseText(wast);
22+
23+
console.log("=== default ===");
24+
console.log(module.emitStackIR());
25+
26+
console.log("=== optimize ==="); // should omit the second block
27+
console.log(module.emitStackIR(true));

test/binaryen.js/stackir.js.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
=== input wast ===
2+
(module
3+
(type $i (func (param i32) (result i32)))
4+
(memory $0 0)
5+
(export "test" (func $test))
6+
(func $test (; 0 ;) (type $i) (param $0 i32) (result i32)
7+
(block (result i32)
8+
(block (result i32)
9+
(if (result i32)
10+
(get_local $0)
11+
(get_local $0)
12+
(i32.const 0)
13+
)
14+
)
15+
)
16+
)
17+
)
18+
19+
=== default ===
20+
(module
21+
(type $i (func (param i32) (result i32)))
22+
(memory $0 0)
23+
(export "test" (func $test))
24+
(func $test (; 0 ;) (; has Stack IR ;) (type $i) (param $0 i32) (result i32)
25+
block $block0 (result i32)
26+
get_local $0
27+
if (result i32)
28+
get_local $0
29+
else
30+
i32.const 0
31+
end
32+
end
33+
)
34+
)
35+
36+
=== optimize ===
37+
(module
38+
(type $i (func (param i32) (result i32)))
39+
(memory $0 0)
40+
(export "test" (func $test))
41+
(func $test (; 0 ;) (; has Stack IR ;) (type $i) (param $0 i32) (result i32)
42+
get_local $0
43+
if (result i32)
44+
get_local $0
45+
else
46+
i32.const 0
47+
end
48+
)
49+
)
50+

0 commit comments

Comments
 (0)