diff options
author | Alon Zakai <alonzakai@gmail.com> | 2018-09-19 15:50:30 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-19 15:50:30 -0700 |
commit | fe88b47749115009da0447e340cbdc86edf30984 (patch) | |
tree | 7dfd9aba7086c8aa6dff4877ac1ee3b9d78bc5ce /src/tools/wasm-ctor-eval.cpp | |
parent | a53356ab155a7d8c2f334dc9a3c1432bacbc78fe (diff) | |
download | binaryen-fe88b47749115009da0447e340cbdc86edf30984.tar.gz binaryen-fe88b47749115009da0447e340cbdc86edf30984.tar.bz2 binaryen-fe88b47749115009da0447e340cbdc86edf30984.zip |
Unify imported and non-imported things (#1678)
Fixes #1649
This moves us to a single object for functions, which can be imported or nor, and likewise for globals (as a result, GetGlobals do not need to check if the global is imported or not, etc.). All imported things now inherit from Importable, which has the module and base of the import, and if they are set then it is an import.
For convenient iteration, there are a few helpers like
ModuleUtils::iterDefinedGlobals(wasm, [&](Global* global) {
.. use global ..
});
as often iteration only cares about imported or defined (non-imported) things.
Diffstat (limited to 'src/tools/wasm-ctor-eval.cpp')
-rw-r--r-- | src/tools/wasm-ctor-eval.cpp | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index e11454b04..4272c5dba 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -36,6 +36,7 @@ #include "ir/global-utils.h" #include "ir/import-utils.h" #include "ir/literal-utils.h" +#include "ir/module-utils.h" using namespace wasm; @@ -124,23 +125,23 @@ public: EvallingModuleInstance(Module& wasm, ExternalInterface* externalInterface) : ModuleInstanceBase(wasm, externalInterface) { // if any global in the module has a non-const constructor, it is using a global import, // which we don't have, and is illegal to use - for (auto& global : wasm.globals) { + ModuleUtils::iterDefinedGlobals(wasm, [&](Global* global) { if (!global->init->is<Const>()) { // some constants are ok to use if (auto* get = global->init->dynCast<GetGlobal>()) { auto name = get->name; - auto* import = wasm.getImport(name); + auto* import = wasm.getGlobal(name); if (import->module == Name("env") && ( import->base == Name("STACKTOP") || // stack constants are special, we handle them import->base == Name("STACK_MAX") )) { - continue; // this is fine + return; // this is fine } } // this global is dangerously initialized by an import, so if it is used, we must fail globals.addDangerous(global->name); } - } + }); } std::vector<char> stack; @@ -173,34 +174,33 @@ struct CtorEvalExternalInterface : EvallingModuleInstance::ExternalInterface { void importGlobals(EvallingGlobalManager& globals, Module& wasm_) override { // fill usable values for stack imports, and globals initialized to them - if (auto* stackTop = ImportUtils::getImport(wasm_, "env", "STACKTOP")) { + ImportInfo imports(wasm_); + if (auto* stackTop = imports.getImportedGlobal("env", "STACKTOP")) { globals[stackTop->name] = Literal(int32_t(STACK_START)); if (auto* stackTop = GlobalUtils::getGlobalInitializedToImport(wasm_, "env", "STACKTOP")) { globals[stackTop->name] = Literal(int32_t(STACK_START)); } } - if (auto* stackMax = ImportUtils::getImport(wasm_, "env", "STACK_MAX")) { + if (auto* stackMax = imports.getImportedGlobal("env", "STACK_MAX")) { globals[stackMax->name] = Literal(int32_t(STACK_START)); if (auto* stackMax = GlobalUtils::getGlobalInitializedToImport(wasm_, "env", "STACK_MAX")) { globals[stackMax->name] = Literal(int32_t(STACK_START)); } } // fill in fake values for everything else, which is dangerous to use - for (auto& global : wasm_.globals) { - if (globals.find(global->name) == globals.end()) { - globals[global->name] = LiteralUtils::makeLiteralZero(global->type); + ModuleUtils::iterDefinedGlobals(wasm_, [&](Global* defined) { + if (globals.find(defined->name) == globals.end()) { + globals[defined->name] = LiteralUtils::makeLiteralZero(defined->type); } - } - for (auto& import : wasm_.imports) { - if (import->kind == ExternalKind::Global) { - if (globals.find(import->name) == globals.end()) { - globals[import->name] = LiteralUtils::makeLiteralZero(import->globalType); - } + }); + ModuleUtils::iterImportedGlobals(wasm_, [&](Global* import) { + if (globals.find(import->name) == globals.end()) { + globals[import->name] = LiteralUtils::makeLiteralZero(import->type); } - } + }); } - Literal callImport(Import *import, LiteralList& arguments) override { + Literal callImport(Function* import, LiteralList& arguments) override { std::string extra; if (import->module == "env" && import->base == "___cxa_atexit") { extra = "\nrecommendation: build with -s NO_EXIT_RUNTIME=1 so that calls to atexit are not emitted"; @@ -227,7 +227,8 @@ struct CtorEvalExternalInterface : EvallingModuleInstance::ExternalInterface { if (start <= index && index < end) { auto name = segment.data[index - start]; // if this is one of our functions, we can call it; if it was imported, fail - if (wasm->getFunctionOrNull(name)) { + auto* func = wasm->getFunction(name); + if (!func->imported()) { return instance.callFunctionInternal(name, arguments); } else { throw FailToEvalException(std::string("callTable on imported function: ") + name.str); |