diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-01-11 20:40:55 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-01-11 20:58:53 -0800 |
commit | 686a8334090f57e1ba218e552819b3c6374059b5 (patch) | |
tree | f5dd8b43127fda4671eb4c7ce93440995525744e /src | |
parent | efe369358fd6c8ede5145f12bfa511b515d2a32a (diff) | |
download | binaryen-686a8334090f57e1ba218e552819b3c6374059b5.tar.gz binaryen-686a8334090f57e1ba218e552819b3c6374059b5.tar.bz2 binaryen-686a8334090f57e1ba218e552819b3c6374059b5.zip |
refactor FunctionType to always be accessed from the Module's central store, which is necessary for simple binary writing
Diffstat (limited to 'src')
-rw-r--r-- | src/asm2wasm.h | 67 | ||||
-rw-r--r-- | src/asm_v_wasm.h | 26 | ||||
-rw-r--r-- | src/passes/RemoveImports.cpp | 2 | ||||
-rw-r--r-- | src/s2wasm.h | 4 | ||||
-rw-r--r-- | src/wasm-binary.h | 2 | ||||
-rw-r--r-- | src/wasm-js.cpp | 2 | ||||
-rw-r--r-- | src/wasm-s-parser.h | 13 | ||||
-rw-r--r-- | src/wasm.h | 8 |
8 files changed, 55 insertions, 69 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index 280624d9b..75862abc8 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -243,26 +243,10 @@ private: } } - FunctionType *getFunctionType(Ref parent, ExpressionList& operands) { + FunctionType* getFunctionType(Ref parent, ExpressionList& operands) { // generate signature WasmType result = !!parent ? detectWasmType(parent, nullptr) : none; - std::string str = "FUNCSIG$"; - str += getSig(result); - for (auto operand : operands) { - str += getSig(operand->type); - } - IString sig(str.c_str(), false); - if (wasm.functionTypesMap.find(sig) == wasm.functionTypesMap.end()) { - // add new type - auto type = allocator.alloc<FunctionType>(); - type->name = sig; - type->result = result; - for (auto operand : operands) { - type->params.push_back(operand->type); - } - wasm.addFunctionType(type); - } - return wasm.functionTypesMap[sig]; + return ensureFunctionType(getSig(result, operands), &wasm, allocator); } public: @@ -421,34 +405,9 @@ private: if (base == ABS) { assert(operands && operands->size() == 1); WasmType type = (*operands)[0]->type; - if (type == i32) { - static FunctionType* builtin = nullptr; - if (!builtin) { - builtin = new FunctionType(); - builtin->params.push_back(i32); - builtin->result = i32; - } - return builtin; - } - if (type == f32) { - static FunctionType* builtin = nullptr; - if (!builtin) { - builtin = new FunctionType(); - builtin->params.push_back(f32); - builtin->result = f32; - } - return builtin; - } - if (type == f64) { - static FunctionType* builtin = nullptr; - if (!builtin) { - builtin = new FunctionType(); - builtin->params.push_back(f64); - builtin->result = f64; - } - return builtin; - } - + if (type == i32) return ensureFunctionType("ii", &wasm, allocator); + if (type == f32) return ensureFunctionType("ff", &wasm, allocator); + if (type == f64) return ensureFunctionType("dd", &wasm, allocator); } } return nullptr; @@ -692,10 +651,10 @@ void Asm2WasmBuilder::processAsm(Ref ast) { // special math builtins FunctionType* builtin = getBuiltinFunctionType(import.module, import.base); if (builtin) { - import.type = *builtin; + import.type = builtin; continue; } - import.type = importedFunctionTypes[name]; + import.type = ensureFunctionType(getSig(&importedFunctionTypes[name]), &wasm, allocator); } else if (import.module != ASM2WASM) { // special-case the special module // never actually used toErase.push_back(name); @@ -906,10 +865,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { import->name = F64_REM; import->module = ASM2WASM; import->base = F64_REM; - import->type.name = F64_REM; - import->type.result = f64; - import->type.params.push_back(f64); - import->type.params.push_back(f64); + import->type = ensureFunctionType("ddd", &wasm, allocator); wasm.addImport(import); } return call; @@ -950,8 +906,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { import->name = DEBUGGER; import->module = ASM2WASM; import->base = DEBUGGER; - import->type.name = DEBUGGER; - import->type.result = none; + import->type = ensureFunctionType("v", &wasm, allocator); wasm.addImport(import); } return call; @@ -1057,9 +1012,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { import->name = F64_TO_INT; import->module = ASM2WASM; import->base = F64_TO_INT; - import->type.name = F64_TO_INT; - import->type.result = i32; - import->type.params.push_back(f64); + import->type = ensureFunctionType("id", &wasm, allocator); wasm.addImport(import); } return ret; diff --git a/src/asm_v_wasm.h b/src/asm_v_wasm.h index f0065bbf9..394c7a321 100644 --- a/src/asm_v_wasm.h +++ b/src/asm_v_wasm.h @@ -17,6 +17,7 @@ #ifndef wasm_asm_v_wasm_h #define wasm_asm_v_wasm_h +#include "mixed_arena.h" #include "emscripten-optimizer/optimizer.h" namespace wasm { @@ -81,6 +82,15 @@ std::string getSig(CallBase *call) { return ret; } +std::string getSig(WasmType result, ExpressionList& operands) { + std::string ret; + ret += getSig(result); + for (auto operand : operands) { + ret += getSig(operand->type); + } + return ret; +} + WasmType sigToWasmType(char sig) { switch (sig) { case 'i': return i32; @@ -101,6 +111,22 @@ FunctionType sigToFunctionType(std::string sig) { return ret; } +FunctionType* ensureFunctionType(std::string sig, Module* wasm, MixedArena& allocator) { + cashew::IString name(("FUNCSIG$" + sig).c_str(), false); + if (wasm->functionTypesMap.find(name) != wasm->functionTypesMap.end()) { + return wasm->functionTypesMap[name]; + } + // add new type + auto type = allocator.alloc<FunctionType>(); + type->name = name; + type->result = sigToWasmType(sig[0]); + for (size_t i = 1; i < sig.size(); i++) { + type->params.push_back(sigToWasmType(sig[i])); + } + wasm->addFunctionType(type); + return type; +} + } // namespace wasm #endif // wasm_asm_v_wasm_h diff --git a/src/passes/RemoveImports.cpp b/src/passes/RemoveImports.cpp index 44b7fc1ed..0c4924a45 100644 --- a/src/passes/RemoveImports.cpp +++ b/src/passes/RemoveImports.cpp @@ -37,7 +37,7 @@ struct RemoveImports : public Pass { } void visitCallImport(CallImport *curr) override { - WasmType type = importsMap[curr->target]->type.result; + WasmType type = importsMap[curr->target]->type->result; if (type == none) { replaceCurrent(allocator->alloc<Nop>()); } else { diff --git a/src/s2wasm.h b/src/s2wasm.h index b99a71159..9cb41b8d6 100644 --- a/src/s2wasm.h +++ b/src/s2wasm.h @@ -627,7 +627,7 @@ private: auto import = allocator.alloc<Import>(); import->name = import->base = target; import->module = ENV; - import->type = sigToFunctionType(getSig(curr)); + import->type = ensureFunctionType(getSig(curr), &wasm, allocator); wasm.addImport(import); } } @@ -1120,7 +1120,7 @@ public: auto import = parent->allocator.alloc<Import>(); import->name = import->base = curr->target; import->module = ENV; - import->type = sigToFunctionType(getSig(curr)); + import->type = ensureFunctionType(getSig(curr), &parent->wasm, parent->allocator); parent->wasm.addImport(import); } } diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 63a272d78..c67670c67 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -412,7 +412,7 @@ public: Name name, type; if (import) { name = import->name; - type = import->type.name; + type = import->type->name; } else { name = function->name; type = function->type; diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp index fdb157319..b767b8a25 100644 --- a/src/wasm-js.cpp +++ b/src/wasm-js.cpp @@ -190,7 +190,7 @@ extern "C" void EMSCRIPTEN_KEEPALIVE instantiate() { if (wasmJSDebug) std::cout << "calling import returning " << ret << '\n'; - switch (import->type.result) { + switch (import->type->result) { case none: return Literal(0); case i32: return Literal((int32_t)ret); case f32: return Literal((float)ret); diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 719a6598b..1fb126ad0 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -28,6 +28,7 @@ #include "mixed_arena.h" #include "shared-constants.h" #include "parsing.h" +#include "asm_v_wasm.h" namespace wasm { @@ -848,7 +849,7 @@ private: auto ret = allocator.alloc<CallImport>(); ret->target = s[1]->str(); Import* import = wasm.importsMap[ret->target]; - ret->type = import->type.result; + ret->type = import->type->result; parseCallOperands(s, 2, ret); return ret; } @@ -996,28 +997,30 @@ private: im->module = s[2]->str(); if (!s[3]->isStr()) onError(); im->base = s[3]->str(); + FunctionType type; if (s.size() > 4) { Element& params = *s[4]; IString id = params[0]->str(); if (id == PARAM) { for (size_t i = 1; i < params.size(); i++) { - im->type.params.push_back(stringToWasmType(params[i]->str())); + type.params.push_back(stringToWasmType(params[i]->str())); } } else if (id == RESULT) { - im->type.result = stringToWasmType(params[1]->str()); + type.result = stringToWasmType(params[1]->str()); } else if (id == TYPE) { IString name = params[1]->str(); assert(wasm.functionTypesMap.find(name) != wasm.functionTypesMap.end()); - im->type = *wasm.functionTypesMap[name]; + type = *wasm.functionTypesMap[name]; } else { onError(); } if (s.size() > 5) { Element& result = *s[5]; assert(result[0]->str() == RESULT); - im->type.result = stringToWasmType(result[1]->str()); + type.result = stringToWasmType(result[1]->str()); } } + im->type = ensureFunctionType(getSig(&type), &wasm, allocator); wasm.addImport(im); } diff --git a/src/wasm.h b/src/wasm.h index c4654580a..306804c66 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -540,6 +540,8 @@ public: WasmType result; std::vector<WasmType> params; + FunctionType() : result(none) {} + std::ostream& print(std::ostream &o, unsigned indent, bool full=false) { if (full) { printOpening(o, "type") << ' ' << name << " (func"; @@ -938,13 +940,15 @@ public: class Import { public: Name name, module, base; // name = module.base - FunctionType type; + FunctionType* type; + + Import() : type(nullptr) {} std::ostream& print(std::ostream &o, unsigned indent) { printOpening(o, "import ") << name << ' '; printText(o, module.str) << ' '; printText(o, base.str); - type.print(o, indent); + if (type) type->print(o, indent); return o << ')'; } }; |