From 0a9df805f688d5eab8be380ab7c9dde115d88852 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 16 Sep 2016 17:55:58 -0700 Subject: br_if returns its value --- src/wasm-interpreter.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/wasm-interpreter.h') diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index 292d6a521..5d4e7d7b4 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -155,18 +155,19 @@ public: Flow visitBreak(Break *curr) { NOTE_ENTER("Break"); bool condition = true; - Flow flow(curr->name); + Flow flow; if (curr->value) { flow = visit(curr->value); if (flow.breaking()) return flow; - flow.breakTo = curr->name; } if (curr->condition) { Flow conditionFlow = visit(curr->condition); if (conditionFlow.breaking()) return conditionFlow; condition = conditionFlow.value.getInteger() != 0; + if (!condition) return flow; } - return condition ? flow : Flow(); + flow.breakTo = curr->name; + return flow; } Flow visitSwitch(Switch *curr) { NOTE_ENTER("Switch"); -- cgit v1.2.3 From 0201f77b30f875de0d0fc8e407ffc985d47f8535 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 16 Sep 2016 21:08:58 -0700 Subject: support module operations in shell tests --- src/shared-constants.h | 1 - src/tools/wasm-shell.cpp | 83 ++++++++++++++++++++++++++++++++---------------- src/wasm-interpreter.h | 11 +++++++ src/wasm-s-parser.h | 4 +-- src/wasm.cpp | 1 - 5 files changed, 68 insertions(+), 32 deletions(-) (limited to 'src/wasm-interpreter.h') diff --git a/src/shared-constants.h b/src/shared-constants.h index e37879088..2d656f0eb 100644 --- a/src/shared-constants.h +++ b/src/shared-constants.h @@ -49,7 +49,6 @@ extern Name GROW_WASM_MEMORY, FAKE_RETURN, SPECTEST, PRINT, - INVOKE, EXIT; } // namespace wasm diff --git a/src/tools/wasm-shell.cpp b/src/tools/wasm-shell.cpp index ddb2f8a85..e0bc5d7e9 100644 --- a/src/tools/wasm-shell.cpp +++ b/src/tools/wasm-shell.cpp @@ -37,28 +37,51 @@ using namespace wasm; Name ASSERT_RETURN("assert_return"), ASSERT_TRAP("assert_trap"), ASSERT_INVALID("assert_invalid"), - ASSERT_MALFORMED("assert_malformed"); + ASSERT_MALFORMED("assert_malformed"), + INVOKE("invoke"), + GET("get"); + +// Modules named in the file + +std::map> modules; +std::map> builders; +std::map> interfaces; +std::map> instances; // -// An invocation into a module +// An operation on a module // -struct Invocation { +struct Operation { ModuleInstance* instance; - IString name; + Name operation; + Name name; LiteralList arguments; - Invocation(Element& invoke, ModuleInstance* instance, SExpressionWasmBuilder& builder) : instance(instance) { - assert(invoke[0]->str() == INVOKE); - name = invoke[1]->str(); - for (size_t j = 2; j < invoke.size(); j++) { - Expression* argument = builder.parseExpression(*invoke[j]); + Operation(Element& element, ModuleInstance* instanceInit, SExpressionWasmBuilder& builder) : instance(instanceInit) { + operation = element[0]->str(); + Index i = 1; + if (element.size() >= 3 && element[2]->isStr()) { + // module also specified + Name moduleName = element[i++]->str(); + instance = instances[moduleName].get(); + } + name = element[i++]->str(); + for (size_t j = i; j < element.size(); j++) { + Expression* argument = builder.parseExpression(*element[j]); arguments.push_back(argument->dynCast()->value); } } - Literal invoke() { - return instance->callExport(name, arguments); + Literal operate() { + if (operation == INVOKE) { + return instance->callExport(name, arguments); + } else if (operation == GET) { + return instance->getExport(name); + } else { + Fatal() << "unknown operation: " << operation << '\n'; + WASM_UNREACHABLE(); + } } }; @@ -75,15 +98,17 @@ static void verify_result(Literal a, Literal b) { } } -static void run_asserts(size_t* i, bool* checked, Module* wasm, +static void run_asserts(Name moduleName, size_t* i, bool* checked, Module* wasm, Element* root, - std::unique_ptr* builder, + SExpressionWasmBuilder* builder, Name entry) { - std::unique_ptr interface; - std::unique_ptr instance; + ModuleInstance* instance = nullptr; if (wasm) { - interface = wasm::make_unique(); // prefix make_unique to work around visual studio bugs - instance = wasm::make_unique(*wasm, interface.get()); + auto tempInterface = wasm::make_unique(); // prefix make_unique to work around visual studio bugs + auto tempInstance = wasm::make_unique(*wasm, tempInterface.get()); + interfaces[moduleName].swap(tempInterface); + instances[moduleName].swap(tempInstance); + instance = instances[moduleName].get(); if (entry.is()) { Function* function = wasm->getFunction(entry); if (!function) { @@ -139,23 +164,23 @@ static void run_asserts(size_t* i, bool* checked, Module* wasm, } } else if (id == INVOKE) { assert(wasm); - Invocation invocation(curr, instance.get(), *builder->get()); - invocation.invoke(); + Operation operation(curr, instance, *builder); + operation.operate(); } else if (wasm) { // if no wasm, we skipped the module // an invoke test bool trapped = false; WASM_UNUSED(trapped); Literal result; try { - Invocation invocation(*curr[1], instance.get(), *builder->get()); - result = invocation.invoke(); + Operation operation(*curr[1], instance, *builder); + result = operation.operate(); } catch (const TrapException&) { trapped = true; } if (id == ASSERT_RETURN) { assert(!trapped); if (curr.size() >= 3) { - Literal expected = builder->get() + Literal expected = builder ->parseExpression(*curr[2]) ->dynCast() ->value; @@ -234,14 +259,16 @@ int main(int argc, const char* argv[]) { Colors::green(std::cerr); std::cerr << "BUILDING MODULE [line: " << curr.line << "]\n"; Colors::normal(std::cerr); - Module wasm; - std::unique_ptr builder; - builder = wasm::make_unique(wasm, *root[i]); + auto module = wasm::make_unique(); + Name moduleName; + auto builder = wasm::make_unique(*module, *root[i], &moduleName); + builders[moduleName].swap(builder); + modules[moduleName].swap(module); i++; - assert(WasmValidator().validate(wasm)); - run_asserts(&i, &checked, &wasm, &root, &builder, entry); + assert(WasmValidator().validate(*modules[moduleName])); + run_asserts(moduleName, &i, &checked, modules[moduleName].get(), &root, builders[moduleName].get(), entry); } else { - run_asserts(&i, &checked, nullptr, &root, nullptr, entry); + run_asserts(Name(), &i, &checked, nullptr, &root, nullptr, entry); } } } catch (ParseException& p) { diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index 5d4e7d7b4..f699ba6f8 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -554,12 +554,23 @@ public: } } + // call an exported function Literal callExport(Name name, LiteralList& arguments) { Export *export_ = wasm.checkExport(name); if (!export_) externalInterface->trap("callExport not found"); return callFunction(export_->value, arguments); } + // get an exported global + Literal getExport(Name name) { + Export *export_ = wasm.checkExport(name); + if (!export_) externalInterface->trap("getExport external not found"); + Name internalName = export_->value; + auto iter = globals.find(internalName); + if (iter == globals.end()) externalInterface->trap("getExport internal not found"); + return iter->second; + } + std::string printFunctionStack() { std::string ret = "/== (binaryen interpreter stack trace)\n"; for (int i = int(functionStack.size()) - 1; i >= 0; i--) { diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index f244a08f9..0f11bd521 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -1473,7 +1473,7 @@ private: ex->kind = Export::Table; } else if (inner[0]->str() == GLOBAL) { ex->value = inner[1]->str(); - ex->kind = Export::Table; + ex->kind = Export::Global; } else { WASM_UNREACHABLE(); } @@ -1487,7 +1487,7 @@ private: ex->kind = Export::Table; } else if (s[2]->str() == GLOBAL) { ex->value = s[3]->str(); - ex->kind = Export::Table; + ex->kind = Export::Global; } else { WASM_UNREACHABLE(); } diff --git a/src/wasm.cpp b/src/wasm.cpp index 144cabd16..8badaa6be 100644 --- a/src/wasm.cpp +++ b/src/wasm.cpp @@ -74,7 +74,6 @@ Name GROW_WASM_MEMORY("__growWasmMemory"), FAKE_RETURN("fake_return_waka123"), SPECTEST("spectest"), PRINT("print"), - INVOKE("invoke"), EXIT("exit"); // core AST type checking -- cgit v1.2.3 From 1338ad989ba30007bc1ba7f0fd05237a9fbec474 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 20 Sep 2016 11:04:14 -0700 Subject: support spectest.global --- src/shell-interface.h | 15 ++++++++++++++- src/wasm-interpreter.h | 6 +++++- src/wasm-s-parser.h | 8 ++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) (limited to 'src/wasm-interpreter.h') diff --git a/src/shell-interface.h b/src/shell-interface.h index ee9ff166a..4d125ea09 100644 --- a/src/shell-interface.h +++ b/src/shell-interface.h @@ -111,7 +111,20 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface { } } - void importGlobals(std::map& globals, Module& wasm) override {} + void importGlobals(std::map& globals, Module& wasm) override { + // add spectest globals + for (auto& import : wasm.imports) { + if (import->kind == Import::Global && import->module == SPECTEST && import->base == GLOBAL) { + switch (import->globalType) { + case i32: globals[import->name] = Literal(int32_t(666)); break; + case i64: globals[import->name] = Literal(int64_t(666)); break; + case f32: globals[import->name] = Literal(float(666.6)); break; + case f64: globals[import->name] = Literal(double(666.6)); break; + default: WASM_UNREACHABLE(); + } + } + } + } Literal callImport(Import *import, LiteralList& arguments) override { if (import->module == SPECTEST && import->base == PRINT) { diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index f699ba6f8..9bd813b84 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -707,6 +707,7 @@ public: auto name = curr->name; NOTE_EVAL1(name); NOTE_EVAL1(instance.globals[name]); + assert(instance.globals.find(name) != instance.globals.end()); return instance.globals[name]; } Flow visitSetGlobal(SetGlobal *curr) { @@ -787,7 +788,10 @@ public: assert(!flow.breaking() || flow.breakTo == RETURN_FLOW); // cannot still be breaking, it means we missed our stop Literal ret = flow.value; if (function->result == none) ret = Literal(); - assert(function->result == ret.type); + if (function->result != ret.type) { + std::cerr << "calling " << function->name << " resulted in " << ret << " but the function type is " << function->result << '\n'; + abort(); + } callDepth = previousCallDepth; // may decrease more than one, if we jumped up the stack // if we jumped up the stack, we also need to pop higher frames while (functionStack.size() > previousFunctionStackSize) { diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 0d2a44407..525cc29f7 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -1646,7 +1646,7 @@ private: void parseGlobal(Element& s, bool preParseImport = false) { std::unique_ptr global = make_unique(); size_t i = 1; - if (s[i]->dollared()) { + if (s[i]->dollared() && !(s[i]->isStr() && isWasmType(s[i]->str()))) { global->name = s[i++]->str(); } else { global->name = Name::fromInt(globalCounter); @@ -1698,7 +1698,11 @@ private: } assert(!preParseImport); global->type = type; - global->init = parseExpression(s[i++]); + if (i < s.size()) { + global->init = parseExpression(s[i++]); + } else { + throw ParseException("global without init", s.line, s.col); + } global->mutable_ = mutable_; assert(i == s.size()); wasm.addGlobal(global.release()); -- cgit v1.2.3 From e0b2af0827e98fb91f4ff90b57b7579da1608365 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 20 Sep 2016 17:00:38 -0700 Subject: refactor wasm.h to remove numericIndex hacks, and move indexing to the parsers --- src/binaryen-c.cpp | 1 + src/passes/OptimizeInstructions.cpp | 23 ++++++----- src/wasm-binary.h | 10 +++-- src/wasm-interpreter.h | 12 +++--- src/wasm-s-parser.h | 52 ++++++++++++++++++------- src/wasm.h | 47 +++++----------------- test/passes/coalesce-locals-learning.txt | 4 +- test/passes/coalesce-locals-learning.wast | 4 +- test/passes/coalesce-locals.txt | 4 +- test/passes/coalesce-locals.wast | 4 +- test/passes/duplicate-function-elimination.txt | 2 +- test/passes/duplicate-function-elimination.wast | 2 +- test/passes/remove-unused-brs.txt | 2 +- test/passes/remove-unused-brs.wast | 2 +- 14 files changed, 88 insertions(+), 81 deletions(-) (limited to 'src/wasm-interpreter.h') diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 3ce24bdbd..14fc4680d 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -128,6 +128,7 @@ BinaryenFunctionTypeRef BinaryenAddFunctionType(BinaryenModuleRef module, const auto* wasm = (Module*)module; auto* ret = new FunctionType; if (name) ret->name = name; + else ret->name = Name::fromInt(wasm->functionTypes.size()); ret->result = WasmType(result); for (BinaryenIndex i = 0; i < numParams; i++) { ret->params.push_back(WasmType(paramTypes[i])); diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 669a19b89..48639a341 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -55,15 +55,20 @@ struct PatternDatabase { input = strdup( #include "OptimizeInstructions.wast.processed" ); - SExpressionParser parser(input); - Element& root = *parser.root; - SExpressionWasmBuilder builder(wasm, *root[0]); - // parse module form - auto* func = wasm.getFunction("patterns"); - auto* body = func->body->cast(); - for (auto* item : body->list) { - auto* pair = item->cast(); - patternMap[pair->list[0]->_id].emplace_back(pair->list[0], pair->list[1]); + try { + SExpressionParser parser(input); + Element& root = *parser.root; + SExpressionWasmBuilder builder(wasm, *root[0]); + // parse module form + auto* func = wasm.getFunction("patterns"); + auto* body = func->body->cast(); + for (auto* item : body->list) { + auto* pair = item->cast(); + patternMap[pair->list[0]->_id].emplace_back(pair->list[0], pair->list[1]); + } + } catch (ParseException& p) { + p.dump(std::cerr); + Fatal() << "error in parsing wasm binary"; } } diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 1bc18e687..85aaf9279 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -652,7 +652,7 @@ public: if (debug) std::cerr << "write one at" << o.size() << std::endl; size_t sizePos = writeU32LEBPlaceholder(); size_t start = o.size(); - Function* function = wasm->getFunction(i); + Function* function = wasm->functions[i].get(); mappedLocals.clear(); numLocalsByType.clear(); if (debug) std::cerr << "writing" << function->name << std::endl; @@ -1495,6 +1495,7 @@ public: assert(numResults == 1); curr->result = getWasmType(); } + curr->name = Name::fromInt(wasm.functionTypes.size()); wasm.addFunctionType(curr); } } @@ -1526,7 +1527,7 @@ public: case Import::Function: { auto index = getU32LEB(); assert(index < wasm.functionTypes.size()); - curr->functionType = wasm.getFunctionType(index); + curr->functionType = wasm.functionTypes[index].get(); assert(curr->functionType->name.is()); functionImportIndexes.push_back(curr->name); break; @@ -1551,7 +1552,7 @@ public: for (size_t i = 0; i < num; i++) { if (debug) std::cerr << "read one" << std::endl; auto index = getU32LEB(); - functionTypes.push_back(wasm.getFunctionType(index)); + functionTypes.push_back(wasm.functionTypes[index].get()); } } @@ -1659,6 +1660,7 @@ public: curr->type = getWasmType(); curr->init = readExpression(); curr->mutable_ = true; // TODO + curr->name = Name("global$" + std::to_string(wasm.globals.size())); wasm.addGlobal(curr); } } @@ -1998,7 +2000,7 @@ public: if (debug) std::cerr << "zz node: CallIndirect" << std::endl; auto arity = getU32LEB(); WASM_UNUSED(arity); - auto* fullType = wasm.getFunctionType(getU32LEB()); + auto* fullType = wasm.functionTypes.at(getU32LEB()).get(); curr->fullType = fullType->name; auto num = fullType->params.size(); assert(num == arity); diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index 9bd813b84..942ba1ab6 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -51,14 +51,14 @@ class Flow { public: Flow() {} Flow(Literal value) : value(value) {} - Flow(IString breakTo) : breakTo(breakTo) {} + Flow(Name breakTo) : breakTo(breakTo) {} Literal value; - IString breakTo; // if non-null, a break is going on + Name breakTo; // if non-null, a break is going on bool breaking() { return breakTo.is(); } - void clearIf(IString target) { + void clearIf(Name target) { if (breakTo == target) { breakTo.clear(); } @@ -589,7 +589,7 @@ private: std::vector functionStack; // Call a function, starting an invocation. - Literal callFunction(IString name, LiteralList& arguments) { + Literal callFunction(Name name, LiteralList& arguments) { // if the last call ended in a jump up the stack, it might have left stuff for us to clean up here callDepth = 0; functionStack.clear(); @@ -598,7 +598,7 @@ private: public: // Internal function call. Must be public so that callTable implementations can use it (refactor?) - Literal callFunctionInternal(IString name, LiteralList& arguments) { + Literal callFunctionInternal(Name name, LiteralList& arguments) { class FunctionScope { public: @@ -757,7 +757,7 @@ public: return Literal(int32_t(ret)); } case HasFeature: { - IString id = curr->nameOperand; + Name id = curr->nameOperand; if (id == WASM) return Literal(1); return Literal((int32_t)0); } diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index f9a43e69b..a914b6598 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -275,6 +275,8 @@ class SExpressionWasmBuilder { Module& wasm; MixedArena& allocator; std::vector functionNames; + std::vector functionTypeNames; + std::vector globalNames; int functionCounter; int globalCounter; std::map functionTypes; // we need to know function return types before we parse their contents @@ -346,8 +348,8 @@ private: if (curr.size() > 2) throw ParseException("invalid result arity", curr.line, curr.col); functionTypes[name] = stringToWasmType(curr[1]->str()); } else if (id == TYPE) { - Name typeName = curr[1]->str(); - if (!wasm.checkFunctionType(typeName)) throw ParseException("unknown function", curr.line, curr.col); + Name typeName = getFunctionTypeName(*curr[1]); + if (!wasm.checkFunctionType(typeName)) throw ParseException("unknown function type", curr.line, curr.col); type = wasm.getFunctionType(typeName); functionTypes[name] = type->result; } else if (id == PARAM && curr.size() > 1) { @@ -377,6 +379,8 @@ private: } } if (need) { + functionType->name = Name::fromInt(wasm.functionTypes.size()); + functionTypeNames.push_back(functionType->name); wasm.addFunctionType(functionType.release()); } } @@ -441,11 +445,22 @@ private: } else { // index size_t offset = atoi(s.str().c_str()); - if (offset >= functionNames.size()) throw ParseException("unknown function"); + if (offset >= functionNames.size()) throw ParseException("unknown function in getFunctionName"); return functionNames[offset]; } } + Name getFunctionTypeName(Element& s) { + if (s.dollared()) { + return s.str(); + } else { + // index + size_t offset = atoi(s.str().c_str()); + if (offset >= functionTypeNames.size()) throw ParseException("unknown function type in getFunctionTypeName"); + return functionTypeNames[offset]; + } + } + void parseStart(Element& s) { wasm.addStart(getFunctionName(*s[1])); } @@ -484,9 +499,18 @@ private: size_t i = 1; Name name, exportName; i = parseFunctionNames(s, name, exportName); - if (!name.is()) { - // unnamed, use an index - name = Name::fromInt(functionCounter); + if (!preParseImport) { + if (!name.is()) { + // unnamed, use an index + name = Name::fromInt(functionCounter); + } + functionCounter++; + } else { + // just preparsing, functionCounter was incremented by preParseFunctionType + if (!name.is()) { + // unnamed, use an index + name = functionNames[functionCounter - 1]; + } } if (exportName.is()) { auto ex = make_unique(); @@ -496,7 +520,6 @@ private: if (wasm.checkExport(ex->name)) throw ParseException("duplicate export", s.line, s.col); wasm.addExport(ex.release()); } - functionCounter++; Expression* body = nullptr; localIndex = 0; otherIndex = 0; @@ -555,7 +578,7 @@ private: if (curr.size() > 2) throw ParseException("invalid result arity", curr.line, curr.col); result = stringToWasmType(curr[1]->str()); } else if (id == TYPE) { - Name name = curr[1]->str(); + Name name = getFunctionTypeName(*curr[1]); type = name; if (!wasm.checkFunctionType(name)) throw ParseException("unknown function type"); FunctionType* type = wasm.getFunctionType(name); @@ -601,9 +624,6 @@ private: assert(preParseImport); std::unique_ptr im = make_unique(); im->name = name; - if (!im->name.is()) { - im->name = name; - } im->module = importModule; im->base = importBase; im->kind = Import::Function; @@ -629,7 +649,6 @@ private: if (currFunction->result != result) throw ParseException("bad func declaration", s.line, s.col); currFunction->body = body; currFunction->type = type; - wasm.addFunction(currFunction.release()); currLocalTypes.clear(); labelStack.clear(); @@ -1293,7 +1312,7 @@ private: } Expression* makeCall(Element& s) { - auto target = s[1]->str(); + auto target = getFunctionName(*s[1]); auto* import = wasm.checkImport(target); if (import && import->kind == Import::Function) { auto ret = allocator.alloc(); @@ -1604,8 +1623,10 @@ private: if (!im->name.is()) { if (im->kind == Import::Function) { im->name = Name::fromInt(functionCounter++); + functionNames.push_back(im->name); } else if (im->kind == Import::Global) { im->name = Name::fromInt(globalCounter++); + globalNames.push_back(im->name); } else if (im->kind == Import::Memory) { im->name = Name::fromInt(0); } else if (im->kind == Import::Table) { @@ -1700,6 +1721,7 @@ private: global->name = Name::fromInt(globalCounter); } globalCounter++; + globalNames.push_back(global->name); bool mutable_ = false; WasmType type = none; bool exported = false; @@ -1861,6 +1883,10 @@ private: type->result = stringToWasmType(curr[1]->str()); } } + if (!type->name.is()) { + type->name = Name::fromInt(wasm.functionTypes.size()); + } + functionTypeNames.push_back(type->name); wasm.addFunctionType(type.release()); } }; diff --git a/src/wasm.h b/src/wasm.h index 5c20a3a10..53b4e2938 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -1552,12 +1552,6 @@ private: public: Module() {}; - FunctionType* getFunctionType(Index i) { assert(i < functionTypes.size()); return functionTypes[i].get(); } - Import* getImport(Index i) { assert(i < imports.size()); return imports[i].get(); } - Export* getExport(Index i) { assert(i < exports.size()); return exports[i].get(); } - Function* getFunction(Index i) { assert(i < functions.size()); return functions[i].get(); } - Global* getGlobal(Index i) { assert(i < globals.size()); return globals[i].get(); } - FunctionType* getFunctionType(Name name) { assert(functionTypesMap.count(name)); return functionTypesMap[name]; } Import* getImport(Name name) { assert(importsMap.count(name)); return importsMap[name]; } Export* getExport(Name name) { assert(exportsMap.count(name)); return exportsMap[name]; } @@ -1570,56 +1564,35 @@ public: Function* checkFunction(Name name) { if (!functionsMap.count(name)) return nullptr; return functionsMap[name]; } Global* checkGlobal(Name name) { if (!globalsMap.count(name)) return nullptr; return globalsMap[name]; } - FunctionType* checkFunctionType(Index i) { if (i >= functionTypes.size()) return nullptr; return functionTypes[i].get(); } - Import* checkImport(Index i) { if (i >= imports.size()) return nullptr; return imports[i].get(); } - Export* checkExport(Index i) { if (i >= exports.size()) return nullptr; return exports[i].get(); } - Function* checkFunction(Index i) { if (i >= functions.size()) return nullptr; return functions[i].get(); } - Global* checkGlobal(Index i) { if (i >= globals.size()) return nullptr; return globals[i].get(); } - void addFunctionType(FunctionType* curr) { - Name numericName = Name::fromInt(functionTypes.size()); // TODO: remove all these, assert on names already existing, do numeric stuff in wasm-s-parser etc. - if (curr->name.isNull()) { - curr->name = numericName; - } + assert(curr->name.is()); functionTypes.push_back(std::unique_ptr(curr)); + assert(functionTypesMap.find(curr->name) == functionTypesMap.end()); functionTypesMap[curr->name] = curr; - functionTypesMap[numericName] = curr; } void addImport(Import* curr) { - Name numericName = Name::fromInt(imports.size()); - if (curr->name.isNull()) { - curr->name = numericName; - } + assert(curr->name.is()); imports.push_back(std::unique_ptr(curr)); + assert(importsMap.find(curr->name) == importsMap.end()); importsMap[curr->name] = curr; - importsMap[numericName] = curr; } void addExport(Export* curr) { - Name numericName = Name::fromInt(exports.size()); - if (curr->name.isNull()) { - curr->name = numericName; - } + assert(curr->name.is()); exports.push_back(std::unique_ptr(curr)); + assert(exportsMap.find(curr->name) == exportsMap.end()); exportsMap[curr->name] = curr; - exportsMap[numericName] = curr; } void addFunction(Function* curr) { - Name numericName = Name::fromInt(functions.size()); - if (curr->name.isNull()) { - curr->name = numericName; - } + assert(curr->name.is()); functions.push_back(std::unique_ptr(curr)); + assert(functionsMap.find(curr->name) == functionsMap.end()); functionsMap[curr->name] = curr; - functionsMap[numericName] = curr; } void addGlobal(Global* curr) { - Name numericName = Name::fromInt(globals.size()); - if (curr->name.isNull()) { - curr->name = numericName; - } + assert(curr->name.is()); globals.push_back(std::unique_ptr(curr)); + assert(globalsMap.find(curr->name) == globalsMap.end()); globalsMap[curr->name] = curr; - globalsMap[numericName] = curr; } void addStart(const Name &s) { diff --git a/test/passes/coalesce-locals-learning.txt b/test/passes/coalesce-locals-learning.txt index f5622d097..06b96b497 100644 --- a/test/passes/coalesce-locals-learning.txt +++ b/test/passes/coalesce-locals-learning.txt @@ -281,7 +281,7 @@ (get_local $1) ) ) - (func $if-through2 (type $2) + (func $if-through3 (type $2) (local $0 i32) (local $1 i32) (set_local $0 @@ -440,7 +440,7 @@ (get_local $4) ) ) - (func $interfere-in-dead (type $2) + (func $interfere-in-dead4 (type $2) (local $0 i32) (local $1 i32) (block $block diff --git a/test/passes/coalesce-locals-learning.wast b/test/passes/coalesce-locals-learning.wast index 469a034ba..23fc9cba8 100644 --- a/test/passes/coalesce-locals-learning.wast +++ b/test/passes/coalesce-locals-learning.wast @@ -289,7 +289,7 @@ (get_local $y) ) ) - (func $if-through2 (type $2) + (func $if-through3 (type $2) (local $x i32) (local $y i32) (set_local $x @@ -453,7 +453,7 @@ (get_local $w) ) ) - (func $interfere-in-dead (type $2) + (func $interfere-in-dead4 (type $2) (local $x i32) (local $y i32) (block $block diff --git a/test/passes/coalesce-locals.txt b/test/passes/coalesce-locals.txt index db7098ab6..a6af3d5d1 100644 --- a/test/passes/coalesce-locals.txt +++ b/test/passes/coalesce-locals.txt @@ -281,7 +281,7 @@ (get_local $1) ) ) - (func $if-through2 (type $2) + (func $if-through3 (type $2) (local $0 i32) (local $1 i32) (set_local $0 @@ -438,7 +438,7 @@ (get_local $4) ) ) - (func $interfere-in-dead (type $2) + (func $interfere-in-dead4 (type $2) (local $0 i32) (local $1 i32) (block $block diff --git a/test/passes/coalesce-locals.wast b/test/passes/coalesce-locals.wast index cd763af10..da71b7665 100644 --- a/test/passes/coalesce-locals.wast +++ b/test/passes/coalesce-locals.wast @@ -289,7 +289,7 @@ (get_local $y) ) ) - (func $if-through2 (type $2) + (func $if-through3 (type $2) (local $x i32) (local $y i32) (set_local $x @@ -451,7 +451,7 @@ (get_local $w) ) ) - (func $interfere-in-dead (type $2) + (func $interfere-in-dead4 (type $2) (local $x i32) (local $y i32) (block $block diff --git a/test/passes/duplicate-function-elimination.txt b/test/passes/duplicate-function-elimination.txt index 333155c80..e9cabe2ad 100644 --- a/test/passes/duplicate-function-elimination.txt +++ b/test/passes/duplicate-function-elimination.txt @@ -368,7 +368,7 @@ (memory 0) (type $FUNCSIG$v (func)) (import "env" "i" (func $i)) - (import "env" "j" (func $i)) + (import "env" "j" (func $j)) (func $erase (type $FUNCSIG$v) (call $i) ) diff --git a/test/passes/duplicate-function-elimination.wast b/test/passes/duplicate-function-elimination.wast index fcd2378e1..ba429ed30 100644 --- a/test/passes/duplicate-function-elimination.wast +++ b/test/passes/duplicate-function-elimination.wast @@ -438,7 +438,7 @@ (memory 0) (type $FUNCSIG$v (func)) (import $i "env" "i") - (import $i "env" "j") + (import $j "env" "j") (func $erase (type $FUNCSIG$v) (call $i) ) diff --git a/test/passes/remove-unused-brs.txt b/test/passes/remove-unused-brs.txt index da19d7ac8..7d76db7f0 100644 --- a/test/passes/remove-unused-brs.txt +++ b/test/passes/remove-unused-brs.txt @@ -191,7 +191,7 @@ ) ) ) - (func $b15 (type $1) + (func $b15b (type $1) (block $topmost (if (i32.const 18) diff --git a/test/passes/remove-unused-brs.wast b/test/passes/remove-unused-brs.wast index 50d936dc7..24995b235 100644 --- a/test/passes/remove-unused-brs.wast +++ b/test/passes/remove-unused-brs.wast @@ -198,7 +198,7 @@ ) ) ) - (func $b15 (type $1) + (func $b15b (type $1) (block $topmost (if (i32.const 18) -- cgit v1.2.3