summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/js/binaryen.js-post.js10
-rw-r--r--test/binaryen.js/stackir.js27
-rw-r--r--test/binaryen.js/stackir.js.txt50
3 files changed, 87 insertions, 0 deletions
diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js
index e34273a2a..8e4d67482 100644
--- a/src/js/binaryen.js-post.js
+++ b/src/js/binaryen.js-post.js
@@ -1162,6 +1162,16 @@ Module['Module'] = function(module) {
out = old;
return ret;
};
+ this['emitStackIR'] = function(optimize) {
+ this['runPasses'](['generate-stack-ir']);
+ if (optimize) this['runPasses'](['optimize-stack-ir']);
+ var old = out;
+ var ret = '';
+ out = function(x) { ret += x + '\n' };
+ this['runPasses'](['print-stack-ir']);
+ out = old;
+ return ret;
+ };
this['emitAsmjs'] = function() {
var old = out;
var ret = '';
diff --git a/test/binaryen.js/stackir.js b/test/binaryen.js/stackir.js
new file mode 100644
index 000000000..5c4b14f9d
--- /dev/null
+++ b/test/binaryen.js/stackir.js
@@ -0,0 +1,27 @@
+var wast = `
+(module
+ (type $i (func (param i32) (result i32)))
+ (memory $0 0)
+ (export "test" (func $test))
+ (func $test (; 0 ;) (type $i) (param $0 i32) (result i32)
+ (block (result i32)
+ (block (result i32)
+ (if (result i32)
+ (get_local $0)
+ (get_local $0)
+ (i32.const 0)
+ )
+ )
+ )
+ )
+)
+`;
+console.log("=== input wast ===" + wast);
+
+var module = Binaryen.parseText(wast);
+
+console.log("=== default ===");
+console.log(module.emitStackIR());
+
+console.log("=== optimize ==="); // should omit the second block
+console.log(module.emitStackIR(true));
diff --git a/test/binaryen.js/stackir.js.txt b/test/binaryen.js/stackir.js.txt
new file mode 100644
index 000000000..f0985ee5c
--- /dev/null
+++ b/test/binaryen.js/stackir.js.txt
@@ -0,0 +1,50 @@
+=== input wast ===
+(module
+ (type $i (func (param i32) (result i32)))
+ (memory $0 0)
+ (export "test" (func $test))
+ (func $test (; 0 ;) (type $i) (param $0 i32) (result i32)
+ (block (result i32)
+ (block (result i32)
+ (if (result i32)
+ (get_local $0)
+ (get_local $0)
+ (i32.const 0)
+ )
+ )
+ )
+ )
+)
+
+=== default ===
+(module
+ (type $i (func (param i32) (result i32)))
+ (memory $0 0)
+ (export "test" (func $test))
+ (func $test (; 0 ;) (; has Stack IR ;) (type $i) (param $0 i32) (result i32)
+ block $block0 (result i32)
+ get_local $0
+ if (result i32)
+ get_local $0
+ else
+ i32.const 0
+ end
+ end
+ )
+)
+
+=== optimize ===
+(module
+ (type $i (func (param i32) (result i32)))
+ (memory $0 0)
+ (export "test" (func $test))
+ (func $test (; 0 ;) (; has Stack IR ;) (type $i) (param $0 i32) (result i32)
+ get_local $0
+ if (result i32)
+ get_local $0
+ else
+ i32.const 0
+ end
+ )
+)
+