From 2b77f6ae60cfaf3f3cbdcc4121e82a619b9372c3 Mon Sep 17 00:00:00 2001 From: Daniel Wirtz Date: Sat, 8 Feb 2020 00:52:15 +0100 Subject: Add C-/JS-APIs for lowMemoryUnused and pass arguments (#2639) Allows a user to enable/disable the `lowMemoryUnused` option and to get/set/clear arbitrary pass arguments when using the C- or JS-APIs. * binaryen.**getLowMemoryUnused**(): `boolean` * binaryen.**setLowMemoryUnused**(on: `boolean`): `void` * binaryen.**getPassArgument**(key: `string`): `string | null` * binaryen.**setPassArgument**(key: `string`, value: `string | null`): `void` * binaryen.**clearPassArguments**(): `void` --- test/binaryen.js/low-memory-unused.js | 40 +++++++++++++++++ test/binaryen.js/low-memory-unused.js.txt | 75 +++++++++++++++++++++++++++++++ test/binaryen.js/pass-arguments.js | 17 +++++++ test/binaryen.js/pass-arguments.js.txt | 25 +++++++++++ 4 files changed, 157 insertions(+) create mode 100644 test/binaryen.js/low-memory-unused.js create mode 100644 test/binaryen.js/low-memory-unused.js.txt create mode 100644 test/binaryen.js/pass-arguments.js create mode 100644 test/binaryen.js/pass-arguments.js.txt (limited to 'test/binaryen.js') diff --git a/test/binaryen.js/low-memory-unused.js b/test/binaryen.js/low-memory-unused.js new file mode 100644 index 000000000..37bdc2cb0 --- /dev/null +++ b/test/binaryen.js/low-memory-unused.js @@ -0,0 +1,40 @@ +var wast = ` +(module + (memory $0 1) + (export "test" (func $test)) + (func $test (param $0 i32) (result i32) + (i32.load + (i32.add + (local.get $0) + (i32.const 128) + ) + ) + ) +) +`; + +console.log("=== input wast ===" + wast); + +var module = binaryen.parseText(wast); + +console.log("=== unoptimized ==="); +assert(module.validate()); +console.log(module.emitText()); + +console.log("=== optimized, lowMemoryUnused=" + binaryen.getLowMemoryUnused() + " ==="); +module.optimize(); +assert(module.validate()); +console.log(module.emitText()); + +binaryen.setAPITracing(true); +binaryen.setLowMemoryUnused(true); +assert(binaryen.getLowMemoryUnused()); +binaryen.setAPITracing(false); +console.log(); + +console.log("=== optimized, lowMemoryUnused=" + binaryen.getLowMemoryUnused() + " ==="); +module.optimize(); +assert(module.validate()); +console.log(module.emitText()); + +module.dispose(); diff --git a/test/binaryen.js/low-memory-unused.js.txt b/test/binaryen.js/low-memory-unused.js.txt new file mode 100644 index 000000000..13850128c --- /dev/null +++ b/test/binaryen.js/low-memory-unused.js.txt @@ -0,0 +1,75 @@ +=== input wast === +(module + (memory $0 1) + (export "test" (func $test)) + (func $test (param $0 i32) (result i32) + (i32.load + (i32.add + (local.get $0) + (i32.const 128) + ) + ) + ) +) + +=== unoptimized === +(module + (type $i32_=>_i32 (func (param i32) (result i32))) + (memory $0 1) + (export "test" (func $test)) + (func $test (; 0 ;) (param $0 i32) (result i32) + (i32.load + (i32.add + (local.get $0) + (i32.const 128) + ) + ) + ) +) + +=== optimized, lowMemoryUnused=false === +(module + (type $i32_=>_i32 (func (param i32) (result i32))) + (memory $0 1) + (export "test" (func $test)) + (func $test (; 0 ;) (; has Stack IR ;) (param $0 i32) (result i32) + (i32.load + (i32.add + (local.get $0) + (i32.const 128) + ) + ) + ) +) + +// beginning a Binaryen API trace +#include +#include +#include "binaryen-c.h" +int main() { + std::map expressions; + std::map functions; + std::map globals; + std::map events; + std::map exports; + std::map relooperBlocks; + BinaryenModuleRef the_module = NULL; + RelooperRef the_relooper = NULL; + BinaryenSetLowMemoryUnused(1); + BinaryenGetLowMemoryUnused(); + return 0; +} +// ending a Binaryen API trace + +=== optimized, lowMemoryUnused=true === +(module + (type $i32_=>_i32 (func (param i32) (result i32))) + (memory $0 1) + (export "test" (func $test)) + (func $test (; 0 ;) (; has Stack IR ;) (param $0 i32) (result i32) + (i32.load offset=128 + (local.get $0) + ) + ) +) + diff --git a/test/binaryen.js/pass-arguments.js b/test/binaryen.js/pass-arguments.js new file mode 100644 index 000000000..7e1f83a2b --- /dev/null +++ b/test/binaryen.js/pass-arguments.js @@ -0,0 +1,17 @@ +binaryen.setAPITracing(true); + +assert(binaryen.getPassArgument("theKey") === null); + +binaryen.setPassArgument("theKey", "theValue"); +assert(binaryen.getPassArgument("theKey") === "theValue"); + +binaryen.setPassArgument("theKey", null); +assert(binaryen.getPassArgument("theKey") === null); + +binaryen.setPassArgument("theKey", "theValue2"); +assert(binaryen.getPassArgument("theKey") === "theValue2"); + +binaryen.clearPassArguments(); +assert(binaryen.getPassArgument("theKey") === null); + +binaryen.setAPITracing(false); diff --git a/test/binaryen.js/pass-arguments.js.txt b/test/binaryen.js/pass-arguments.js.txt new file mode 100644 index 000000000..f877b2248 --- /dev/null +++ b/test/binaryen.js/pass-arguments.js.txt @@ -0,0 +1,25 @@ +// beginning a Binaryen API trace +#include +#include +#include "binaryen-c.h" +int main() { + std::map expressions; + std::map functions; + std::map globals; + std::map events; + std::map exports; + std::map relooperBlocks; + BinaryenModuleRef the_module = NULL; + RelooperRef the_relooper = NULL; + BinaryenGetPassArgument("theKey"); + BinaryenSetPassArgument("theKey", "theValue"); + BinaryenGetPassArgument("theKey"); + BinaryenSetPassArgument("theKey", NULL); + BinaryenGetPassArgument("theKey"); + BinaryenSetPassArgument("theKey", "theValue2"); + BinaryenGetPassArgument("theKey"); + BinaryenClearPassArguments(); + BinaryenGetPassArgument("theKey"); + return 0; +} +// ending a Binaryen API trace -- cgit v1.2.3