diff options
author | Alon Zakai <alonzakai@gmail.com> | 2019-04-12 15:45:10 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-12 15:45:10 -0700 |
commit | 883d14de7157950063f74b81658d00df0d53be8d (patch) | |
tree | ce0eb6bd6f8ba344e41861f3280f5248427072e7 /src/passes/RemoveNonJSOps.cpp | |
parent | 53badfbea40e78eadf652735d247649948e0b9a9 (diff) | |
download | binaryen-883d14de7157950063f74b81658d00df0d53be8d.tar.gz binaryen-883d14de7157950063f74b81658d00df0d53be8d.tar.bz2 binaryen-883d14de7157950063f74b81658d00df0d53be8d.zip |
Wasm2js memory fixes (#2003)
* I64ToI32Lowering - don't assume address 0 is a hardcoded location for scratch memory. Import __tempMemory__ for that.
* RemoveNonJSOps - also use __tempMemory__. Oddly here the address was a hardcoded 1024 (perhaps where the rust program put a static global?).
* Support imported ints in wasm2js, coercing them as needed.
* Add "env" import support in the tests, since now we emit imports from there.
* Make wasm2js tests split out multi-module tests using split_wast which is more robust and avoids emitting multiple outputs in one file (which makes no sense for ES6 modules)
Diffstat (limited to 'src/passes/RemoveNonJSOps.cpp')
-rw-r--r-- | src/passes/RemoveNonJSOps.cpp | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/src/passes/RemoveNonJSOps.cpp b/src/passes/RemoveNonJSOps.cpp index 342f59eba..a98189584 100644 --- a/src/passes/RemoveNonJSOps.cpp +++ b/src/passes/RemoveNonJSOps.cpp @@ -43,6 +43,7 @@ namespace wasm { struct RemoveNonJSOpsPass : public WalkerPass<PostWalker<RemoveNonJSOpsPass>> { std::unique_ptr<Builder> builder; std::unordered_set<Name> neededIntrinsics; + std::set<std::pair<Name, Type>> neededImportedGlobals; bool isFunctionParallel() override { return false; } @@ -101,6 +102,21 @@ struct RemoveNonJSOpsPass : public WalkerPass<PostWalker<RemoveNonJSOpsPass>> { // Intrinsics may use memory, so ensure the module has one. MemoryUtils::ensureExists(module->memory); + + // Add missing globals + for (auto& pair : neededImportedGlobals) { + auto name = pair.first; + auto type = pair.second; + if (!getModule()->getGlobalOrNull(name)) { + auto global = make_unique<Global>(); + global->name = name; + global->type = type; + global->mutable_ = false; + global->module = ENV; + global->base = name; + module->addGlobal(global.release()); + } + } } void addNeededFunctions(Module &m, Name name, std::set<Name> &needed) { @@ -121,7 +137,7 @@ struct RemoveNonJSOpsPass : public WalkerPass<PostWalker<RemoveNonJSOpsPass>> { PostWalker<RemoveNonJSOpsPass>::doWalkFunction(func); } - void visitLoad(Load *curr) { + void visitLoad(Load* curr) { if (curr->align == 0 || curr->align >= curr->bytes) { return; } @@ -143,7 +159,7 @@ struct RemoveNonJSOpsPass : public WalkerPass<PostWalker<RemoveNonJSOpsPass>> { } } - void visitStore(Store *curr) { + void visitStore(Store* curr) { if (curr->align == 0 || curr->align >= curr->bytes) { return; } @@ -165,7 +181,7 @@ struct RemoveNonJSOpsPass : public WalkerPass<PostWalker<RemoveNonJSOpsPass>> { } } - void visitBinary(Binary *curr) { + void visitBinary(Binary* curr) { Name name; switch (curr->op) { case CopySignFloat32: @@ -207,7 +223,7 @@ struct RemoveNonJSOpsPass : public WalkerPass<PostWalker<RemoveNonJSOpsPass>> { replaceCurrent(builder->makeCall(name, {curr->left, curr->right}, curr->type)); } - void rewriteCopysign(Binary *curr) { + void rewriteCopysign(Binary* curr) { Literal signBit, otherBits; UnaryOp int2float, float2int; BinaryOp bitAnd, bitOr; @@ -259,7 +275,7 @@ struct RemoveNonJSOpsPass : public WalkerPass<PostWalker<RemoveNonJSOpsPass>> { ); } - void visitUnary(Unary *curr) { + void visitUnary(Unary* curr) { Name functionCall; switch (curr->op) { case NearestFloat32: @@ -295,9 +311,13 @@ struct RemoveNonJSOpsPass : public WalkerPass<PostWalker<RemoveNonJSOpsPass>> { neededIntrinsics.insert(functionCall); replaceCurrent(builder->makeCall(functionCall, {curr->value}, curr->type)); } + + void visitGetGlobal(GetGlobal* curr) { + neededImportedGlobals.insert(std::make_pair(curr->name, curr->type)); + } }; -Pass *createRemoveNonJSOpsPass() { +Pass* createRemoveNonJSOpsPass() { return new RemoveNonJSOpsPass(); } |