From 7955baa2e92382bab8dba066ba8b0420cc5447f5 Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Wed, 9 Oct 2019 13:22:58 -0700 Subject: [PATCH 1/2] Add support for reftypes in InstrumentLocals pass This adds support for anyref and exnref types in InstrumentLocals pass. --- scripts/wasm2js.js | 16 +++ src/passes/InstrumentLocals.cpp | 20 +++- ...txt => instrument-locals_all-features.txt} | 100 ++++++++++++++++-- ...st => instrument-locals_all-features.wast} | 10 ++ 4 files changed, 132 insertions(+), 14 deletions(-) rename test/passes/{instrument-locals.txt => instrument-locals_all-features.txt} (58%) rename test/passes/{instrument-locals.wast => instrument-locals_all-features.wast} (67%) diff --git a/scripts/wasm2js.js b/scripts/wasm2js.js index edd7eeeb966..1d09c193ab0 100644 --- a/scripts/wasm2js.js +++ b/scripts/wasm2js.js @@ -130,6 +130,14 @@ var asmLibraryArg = { console.log('get_f64 ' + [loc, index, value]); return value; }, + get_anyref: function(loc, index, value) { + console.log('get_anyref ' + [loc, index, value]); + return value; + }, + get_exnref: function(loc, index, value) { + console.log('get_exnref ' + [loc, index, value]); + return value; + }, set_i32: function(loc, index, value) { console.log('set_i32 ' + [loc, index, value]); return value; @@ -147,6 +155,14 @@ var asmLibraryArg = { console.log('set_f64 ' + [loc, index, value]); return value; }, + set_anyref: function(loc, index, value) { + console.log('set_anyref ' + [loc, index, value]); + return value; + }, + set_exnref: function(loc, index, value) { + console.log('set_exnref ' + [loc, index, value]); + return value; + }, load_ptr: function(loc, bytes, offset, ptr) { console.log('load_ptr ' + [loc, bytes, offset, ptr]); return ptr; diff --git a/src/passes/InstrumentLocals.cpp b/src/passes/InstrumentLocals.cpp index ae1c3083605..8fc68922207 100644 --- a/src/passes/InstrumentLocals.cpp +++ b/src/passes/InstrumentLocals.cpp @@ -57,11 +57,15 @@ Name get_i32("get_i32"); Name get_i64("get_i64"); Name get_f32("get_f32"); Name get_f64("get_f64"); +Name get_anyref("get_anyref"); +Name get_exnref("get_exnref"); Name set_i32("set_i32"); Name set_i64("set_i64"); Name set_f32("set_f32"); Name set_f64("set_f64"); +Name set_anyref("set_anyref"); +Name set_exnref("set_exnref"); struct InstrumentLocals : public WalkerPass> { void visitLocalGet(LocalGet* curr) { @@ -82,9 +86,11 @@ struct InstrumentLocals : public WalkerPass> { case v128: assert(false && "v128 not implemented yet"); case anyref: - assert(false && "anyref not implemented yet"); + import = get_anyref; + break; case exnref: - assert(false && "exnref not implemented yet"); + import = get_exnref; + break; case none: WASM_UNREACHABLE(); case unreachable: @@ -116,9 +122,11 @@ struct InstrumentLocals : public WalkerPass> { case v128: assert(false && "v128 not implemented yet"); case anyref: - assert(false && "anyref not implemented yet"); + import = set_anyref; + break; case exnref: - assert(false && "exnref not implemented yet"); + import = set_exnref; + break; case unreachable: return; // nothing to do here case none: @@ -137,10 +145,14 @@ struct InstrumentLocals : public WalkerPass> { addImport(curr, get_i64, "jiij"); addImport(curr, get_f32, "fiif"); addImport(curr, get_f64, "diid"); + addImport(curr, get_anyref, "aiia"); + addImport(curr, get_exnref, "eiie"); addImport(curr, set_i32, "iiii"); addImport(curr, set_i64, "jiij"); addImport(curr, set_f32, "fiif"); addImport(curr, set_f64, "diid"); + addImport(curr, set_anyref, "aiia"); + addImport(curr, set_exnref, "eiie"); } private: diff --git a/test/passes/instrument-locals.txt b/test/passes/instrument-locals_all-features.txt similarity index 58% rename from test/passes/instrument-locals.txt rename to test/passes/instrument-locals_all-features.txt index b57fbb9a191..7885f39d012 100644 --- a/test/passes/instrument-locals.txt +++ b/test/passes/instrument-locals_all-features.txt @@ -4,19 +4,27 @@ (type $FUNCSIG$jiij (func (param i32 i32 i64) (result i64))) (type $FUNCSIG$fiif (func (param i32 i32 f32) (result f32))) (type $FUNCSIG$diid (func (param i32 i32 f64) (result f64))) + (type $FUNCSIG$aiia (func (param i32 i32 anyref) (result anyref))) + (type $FUNCSIG$eiie (func (param i32 i32 exnref) (result exnref))) (import "env" "get_i32" (func $get_i32 (param i32 i32 i32) (result i32))) (import "env" "get_i64" (func $get_i64 (param i32 i32 i64) (result i64))) (import "env" "get_f32" (func $get_f32 (param i32 i32 f32) (result f32))) (import "env" "get_f64" (func $get_f64 (param i32 i32 f64) (result f64))) + (import "env" "get_anyref" (func $get_anyref (param i32 i32 anyref) (result anyref))) + (import "env" "get_exnref" (func $get_exnref (param i32 i32 exnref) (result exnref))) (import "env" "set_i32" (func $set_i32 (param i32 i32 i32) (result i32))) (import "env" "set_i64" (func $set_i64 (param i32 i32 i64) (result i64))) (import "env" "set_f32" (func $set_f32 (param i32 i32 f32) (result f32))) (import "env" "set_f64" (func $set_f64 (param i32 i32 f64) (result f64))) - (func $A (; 8 ;) (type $FUNCSIG$v) + (import "env" "set_anyref" (func $set_anyref (param i32 i32 anyref) (result anyref))) + (import "env" "set_exnref" (func $set_exnref (param i32 i32 exnref) (result exnref))) + (func $A (; 12 ;) (type $FUNCSIG$v) (local $x i32) (local $y i64) (local $z f32) (local $w f64) + (local $a anyref) + (local $e exnref) (drop (call $get_i32 (i32.const 0) @@ -42,8 +50,22 @@ ) ) (drop - (call $get_i32 + (call $get_anyref (i32.const 3) + (i32.const 4) + (local.get $a) + ) + ) + (drop + (call $get_exnref + (i32.const 4) + (i32.const 5) + (local.get $e) + ) + ) + (drop + (call $get_i32 + (i32.const 5) (i32.const 0) (local.get $x) ) @@ -53,21 +75,35 @@ ) (drop (call $get_f32 - (i32.const 4) + (i32.const 6) (i32.const 2) (local.get $z) ) ) (drop (call $get_f64 - (i32.const 5) + (i32.const 7) (i32.const 3) (local.get $w) ) ) + (drop + (call $get_anyref + (i32.const 8) + (i32.const 4) + (local.get $a) + ) + ) + (drop + (call $get_exnref + (i32.const 9) + (i32.const 5) + (local.get $e) + ) + ) (local.set $x (call $set_i32 - (i32.const 6) + (i32.const 10) (i32.const 0) (i32.const 1) ) @@ -77,21 +113,43 @@ ) (local.set $z (call $set_f32 - (i32.const 7) + (i32.const 11) (i32.const 2) (f32.const 3.2100000381469727) ) ) (local.set $w (call $set_f64 - (i32.const 8) + (i32.const 12) (i32.const 3) (f64.const 4.321) ) ) + (local.set $a + (call $set_anyref + (i32.const 14) + (i32.const 4) + (call $get_anyref + (i32.const 13) + (i32.const 4) + (local.get $a) + ) + ) + ) + (local.set $e + (call $set_exnref + (i32.const 16) + (i32.const 5) + (call $get_exnref + (i32.const 15) + (i32.const 5) + (local.get $e) + ) + ) + ) (local.set $x (call $set_i32 - (i32.const 9) + (i32.const 17) (i32.const 0) (i32.const 11) ) @@ -101,17 +159,39 @@ ) (local.set $z (call $set_f32 - (i32.const 10) + (i32.const 18) (i32.const 2) (f32.const 33.209999084472656) ) ) (local.set $w (call $set_f64 - (i32.const 11) + (i32.const 19) (i32.const 3) (f64.const 44.321) ) ) + (local.set $a + (call $set_anyref + (i32.const 21) + (i32.const 4) + (call $get_anyref + (i32.const 20) + (i32.const 4) + (local.get $a) + ) + ) + ) + (local.set $e + (call $set_exnref + (i32.const 23) + (i32.const 5) + (call $get_exnref + (i32.const 22) + (i32.const 5) + (local.get $e) + ) + ) + ) ) ) diff --git a/test/passes/instrument-locals.wast b/test/passes/instrument-locals_all-features.wast similarity index 67% rename from test/passes/instrument-locals.wast rename to test/passes/instrument-locals_all-features.wast index f96399d0207..eb4d7a2550c 100644 --- a/test/passes/instrument-locals.wast +++ b/test/passes/instrument-locals_all-features.wast @@ -4,26 +4,36 @@ (local $y i64) (local $z f32) (local $w f64) + (local $a anyref) + (local $e exnref) (drop (local.get $x)) (drop (local.get $y)) (drop (local.get $z)) (drop (local.get $w)) + (drop (local.get $a)) + (drop (local.get $e)) (drop (local.get $x)) (drop (local.get $y)) (drop (local.get $z)) (drop (local.get $w)) + (drop (local.get $a)) + (drop (local.get $e)) (local.set $x (i32.const 1)) (local.set $y (i64.const 2)) (local.set $z (f32.const 3.21)) (local.set $w (f64.const 4.321)) + (local.set $a (local.get $a)) + (local.set $e (local.get $e)) (local.set $x (i32.const 11)) (local.set $y (i64.const 22)) (local.set $z (f32.const 33.21)) (local.set $w (f64.const 44.321)) + (local.set $a (local.get $a)) + (local.set $e (local.get $e)) ) ) From 16ab70a21c7b5a4c56d69b387bffe14b36cb8120 Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Thu, 10 Oct 2019 09:35:00 -0700 Subject: [PATCH 2/2] Use `exnref.pop` and `anyref.pop` --- .../passes/instrument-locals_all-features.txt | 38 ++++++------------- .../instrument-locals_all-features.wast | 8 ++-- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/test/passes/instrument-locals_all-features.txt b/test/passes/instrument-locals_all-features.txt index 7885f39d012..1faef3f14b7 100644 --- a/test/passes/instrument-locals_all-features.txt +++ b/test/passes/instrument-locals_all-features.txt @@ -127,29 +127,21 @@ ) (local.set $a (call $set_anyref - (i32.const 14) + (i32.const 13) (i32.const 4) - (call $get_anyref - (i32.const 13) - (i32.const 4) - (local.get $a) - ) + (anyref.pop) ) ) (local.set $e (call $set_exnref - (i32.const 16) + (i32.const 14) (i32.const 5) - (call $get_exnref - (i32.const 15) - (i32.const 5) - (local.get $e) - ) + (exnref.pop) ) ) (local.set $x (call $set_i32 - (i32.const 17) + (i32.const 15) (i32.const 0) (i32.const 11) ) @@ -159,38 +151,30 @@ ) (local.set $z (call $set_f32 - (i32.const 18) + (i32.const 16) (i32.const 2) (f32.const 33.209999084472656) ) ) (local.set $w (call $set_f64 - (i32.const 19) + (i32.const 17) (i32.const 3) (f64.const 44.321) ) ) (local.set $a (call $set_anyref - (i32.const 21) + (i32.const 18) (i32.const 4) - (call $get_anyref - (i32.const 20) - (i32.const 4) - (local.get $a) - ) + (anyref.pop) ) ) (local.set $e (call $set_exnref - (i32.const 23) + (i32.const 19) (i32.const 5) - (call $get_exnref - (i32.const 22) - (i32.const 5) - (local.get $e) - ) + (exnref.pop) ) ) ) diff --git a/test/passes/instrument-locals_all-features.wast b/test/passes/instrument-locals_all-features.wast index eb4d7a2550c..ddcec39ea6d 100644 --- a/test/passes/instrument-locals_all-features.wast +++ b/test/passes/instrument-locals_all-features.wast @@ -25,15 +25,15 @@ (local.set $y (i64.const 2)) (local.set $z (f32.const 3.21)) (local.set $w (f64.const 4.321)) - (local.set $a (local.get $a)) - (local.set $e (local.get $e)) + (local.set $a (anyref.pop)) + (local.set $e (exnref.pop)) (local.set $x (i32.const 11)) (local.set $y (i64.const 22)) (local.set $z (f32.const 33.21)) (local.set $w (f64.const 44.321)) - (local.set $a (local.get $a)) - (local.set $e (local.get $e)) + (local.set $a (anyref.pop)) + (local.set $e (exnref.pop)) ) )