summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-11-17 14:20:43 -0800
committerAlon Zakai <alonzakai@gmail.com>2016-12-07 16:50:04 -1000
commit7bd25106baf71882badec1e03f45f6cae5d31560 (patch)
tree2b0c72dd8ff670d26ab502c4cf89b205aa228552
parent98e9e604c7e2e4f928abe8f05691df90cddf09e4 (diff)
downloadbinaryen-7bd25106baf71882badec1e03f45f6cae5d31560.tar.gz
binaryen-7bd25106baf71882badec1e03f45f6cae5d31560.tar.bz2
binaryen-7bd25106baf71882badec1e03f45f6cae5d31560.zip
support asm.js numeric exports by creating a global and exporting that
-rw-r--r--src/asm2wasm.h51
-rw-r--r--test/unit.asm.js2
-rw-r--r--test/unit.fromasm2
-rw-r--r--test/unit.fromasm.imprecise2
-rw-r--r--test/unit.fromasm.imprecise.no-opts2
-rw-r--r--test/unit.fromasm.no-opts2
6 files changed, 44 insertions, 17 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h
index 951a0e019..586adaa99 100644
--- a/src/asm2wasm.h
+++ b/src/asm2wasm.h
@@ -831,25 +831,44 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
for (unsigned k = 0; k < contents->size(); k++) {
Ref pair = contents[k];
IString key = pair[0]->getIString();
- assert(pair[1][0] == NAME);
- IString value = pair[1][1]->getIString();
- if (key == Name("_emscripten_replace_memory")) {
- // asm.js memory growth provides this special non-asm function, which we don't need (we use grow_memory)
- assert(!wasm.checkFunction(value));
- continue;
- } else if (key == UDIVMODDI4) {
- udivmoddi4 = value;
- } else if (key == GET_TEMP_RET0) {
- getTempRet0 = value;
- }
- if (exported.count(key) > 0) {
- // asm.js allows duplicate exports, but not wasm. use the last, like asm.js
- exported[key]->value = value;
+ if (pair[1][0] == NAME) {
+ // exporting a function
+ IString value = pair[1][1]->getIString();
+ if (key == Name("_emscripten_replace_memory")) {
+ // asm.js memory growth provides this special non-asm function, which we don't need (we use grow_memory)
+ assert(!wasm.checkFunction(value));
+ continue;
+ } else if (key == UDIVMODDI4) {
+ udivmoddi4 = value;
+ } else if (key == GET_TEMP_RET0) {
+ getTempRet0 = value;
+ }
+ if (exported.count(key) > 0) {
+ // asm.js allows duplicate exports, but not wasm. use the last, like asm.js
+ exported[key]->value = value;
+ } else {
+ auto* export_ = new Export;
+ export_->name = key;
+ export_->value = value;
+ export_->kind = ExternalKind::Function;
+ wasm.addExport(export_);
+ exported[key] = export_;
+ }
} else {
+ // export a number. create a global and export it
+ assert(pair[1][0] == NUM);
+ assert(exported.count(key) == 0);
+ auto value = pair[1][1]->getInteger();
+ auto global = new Global();
+ global->name = key;
+ global->type = i32;
+ global->init = builder.makeConst(Literal(int32_t(value)));
+ global->mutable_ = false;
+ wasm.addGlobal(global);
auto* export_ = new Export;
export_->name = key;
- export_->value = value;
- export_->kind = ExternalKind::Function;
+ export_->value = global->name;
+ export_->kind = ExternalKind::Global;
wasm.addExport(export_);
exported[key] = export_;
}
diff --git a/test/unit.asm.js b/test/unit.asm.js
index 84fb346cc..51c2fc22a 100644
--- a/test/unit.asm.js
+++ b/test/unit.asm.js
@@ -663,6 +663,6 @@ function asm(global, env, buffer) {
var FUNCTION_TABLE_c = [ z, cneg, z, z, z, z, z, z ];
var FUNCTION_TABLE_vi = [ vi, vi, vi, vi, vi, vi, vi, vi ];
- return { big_negative: big_negative, pick: forgetMe, pick: exportMe, doubleCompares: doubleCompares, intOps: intOps, conversions: conversions, switcher: switcher, frem: frem, big_uint_div_u: big_uint_div_u, fr: fr, negZero: negZero, neg: neg, smallCompare: smallCompare, cneg_nosemicolon: cneg_nosemicolon, forLoop: forLoop, ceiling_32_64: ceiling_32_64, aborts: aborts, continues: continues, bitcasts: bitcasts, recursiveBlockMerging: recursiveBlockMerging, lb: lb, zeroInit: zeroInit, phi: phi, smallIf: smallIf, dropCall: dropCall, useSetGlobal: useSetGlobal, usesSetGlobal2: usesSetGlobal2, breakThroughMany: breakThroughMany, ifChainEmpty: ifChainEmpty, heap8NoShift: heap8NoShift, conditionalTypeFun: conditionalTypeFun, loadSigned: loadSigned, globalOpts: globalOpts, dropCallImport: dropCallImport, loophi: loophi, loophi2: loophi2, relooperJumpThreading: relooperJumpThreading, relooperJumpThreading__ZN4game14preloadweaponsEv: relooperJumpThreading__ZN4game14preloadweaponsEv, __Z12multi_varargiz: __Z12multi_varargiz, jumpThreadDrop: jumpThreadDrop, dropIgnoredImportInIf: dropIgnoredImportInIf, dropIgnoredImportsInIf: dropIgnoredImportsInIf, relooperJumpThreading_irreducible: relooperJumpThreading_irreducible, store_fround: store_fround };
+ return { big_negative: big_negative, pick: forgetMe, pick: exportMe, doubleCompares: doubleCompares, intOps: intOps, conversions: conversions, switcher: switcher, frem: frem, big_uint_div_u: big_uint_div_u, fr: fr, negZero: negZero, neg: neg, smallCompare: smallCompare, cneg_nosemicolon: cneg_nosemicolon, forLoop: forLoop, ceiling_32_64: ceiling_32_64, aborts: aborts, continues: continues, bitcasts: bitcasts, recursiveBlockMerging: recursiveBlockMerging, lb: lb, zeroInit: zeroInit, phi: phi, smallIf: smallIf, dropCall: dropCall, useSetGlobal: useSetGlobal, usesSetGlobal2: usesSetGlobal2, breakThroughMany: breakThroughMany, ifChainEmpty: ifChainEmpty, heap8NoShift: heap8NoShift, conditionalTypeFun: conditionalTypeFun, loadSigned: loadSigned, globalOpts: globalOpts, dropCallImport: dropCallImport, loophi: loophi, loophi2: loophi2, relooperJumpThreading: relooperJumpThreading, relooperJumpThreading__ZN4game14preloadweaponsEv: relooperJumpThreading__ZN4game14preloadweaponsEv, __Z12multi_varargiz: __Z12multi_varargiz, jumpThreadDrop: jumpThreadDrop, dropIgnoredImportInIf: dropIgnoredImportInIf, dropIgnoredImportsInIf: dropIgnoredImportsInIf, relooperJumpThreading_irreducible: relooperJumpThreading_irreducible, store_fround: store_fround, exportedNumber: 42 };
}
diff --git a/test/unit.fromasm b/test/unit.fromasm
index fcddb0b4b..15ff3b517 100644
--- a/test/unit.fromasm
+++ b/test/unit.fromasm
@@ -25,6 +25,7 @@
(global $Int (mut i32) (i32.const 0))
(global $Double (mut f64) (f64.const 0))
(global $n (mut i32) (get_global $n$asm2wasm$import))
+ (global $exportedNumber i32 (i32.const 42))
(export "big_negative" (func $big_negative))
(export "pick" (func $big_negative))
(export "doubleCompares" (func $doubleCompares))
@@ -68,6 +69,7 @@
(export "dropIgnoredImportsInIf" (func $dropIgnoredImportsInIf))
(export "relooperJumpThreading_irreducible" (func $relooperJumpThreading_irreducible))
(export "store_fround" (func $store_fround))
+ (export "exportedNumber" (global $exportedNumber))
(func $big_negative
(nop)
)
diff --git a/test/unit.fromasm.imprecise b/test/unit.fromasm.imprecise
index 94f34c03d..d466f8c27 100644
--- a/test/unit.fromasm.imprecise
+++ b/test/unit.fromasm.imprecise
@@ -20,6 +20,7 @@
(global $Int (mut i32) (i32.const 0))
(global $Double (mut f64) (f64.const 0))
(global $n (mut i32) (get_global $n$asm2wasm$import))
+ (global $exportedNumber i32 (i32.const 42))
(export "big_negative" (func $big_negative))
(export "pick" (func $big_negative))
(export "doubleCompares" (func $doubleCompares))
@@ -63,6 +64,7 @@
(export "dropIgnoredImportsInIf" (func $dropIgnoredImportsInIf))
(export "relooperJumpThreading_irreducible" (func $relooperJumpThreading_irreducible))
(export "store_fround" (func $store_fround))
+ (export "exportedNumber" (global $exportedNumber))
(func $big_negative
(nop)
)
diff --git a/test/unit.fromasm.imprecise.no-opts b/test/unit.fromasm.imprecise.no-opts
index c306d144b..e16dd92f8 100644
--- a/test/unit.fromasm.imprecise.no-opts
+++ b/test/unit.fromasm.imprecise.no-opts
@@ -28,6 +28,7 @@
(global $tempDoublePtr (mut i32) (get_global $tempDoublePtr$asm2wasm$import))
(global $n (mut i32) (get_global $n$asm2wasm$import))
(global $STACKTOP (mut i32) (get_global $STACKTOP$asm2wasm$import))
+ (global $exportedNumber i32 (i32.const 42))
(export "big_negative" (func $big_negative))
(export "pick" (func $exportMe))
(export "doubleCompares" (func $doubleCompares))
@@ -71,6 +72,7 @@
(export "dropIgnoredImportsInIf" (func $dropIgnoredImportsInIf))
(export "relooperJumpThreading_irreducible" (func $relooperJumpThreading_irreducible))
(export "store_fround" (func $store_fround))
+ (export "exportedNumber" (global $exportedNumber))
(func $big_negative
(local $temp f64)
(set_local $temp
diff --git a/test/unit.fromasm.no-opts b/test/unit.fromasm.no-opts
index 1a29eb710..2fef4cae6 100644
--- a/test/unit.fromasm.no-opts
+++ b/test/unit.fromasm.no-opts
@@ -32,6 +32,7 @@
(global $tempDoublePtr (mut i32) (get_global $tempDoublePtr$asm2wasm$import))
(global $n (mut i32) (get_global $n$asm2wasm$import))
(global $STACKTOP (mut i32) (get_global $STACKTOP$asm2wasm$import))
+ (global $exportedNumber i32 (i32.const 42))
(export "big_negative" (func $big_negative))
(export "pick" (func $exportMe))
(export "doubleCompares" (func $doubleCompares))
@@ -75,6 +76,7 @@
(export "dropIgnoredImportsInIf" (func $dropIgnoredImportsInIf))
(export "relooperJumpThreading_irreducible" (func $relooperJumpThreading_irreducible))
(export "store_fround" (func $store_fround))
+ (export "exportedNumber" (global $exportedNumber))
(func $big_negative
(local $temp f64)
(set_local $temp