diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/asm2wasm.h | 5 | ||||
-rw-r--r-- | src/binaryen-c.cpp | 1 | ||||
-rw-r--r-- | src/binaryen-c.h | 2 | ||||
-rw-r--r-- | src/passes/Print.cpp | 7 | ||||
-rw-r--r-- | src/wasm-binary.h | 27 | ||||
-rw-r--r-- | src/wasm-linker.cpp | 2 | ||||
-rw-r--r-- | src/wasm-s-parser.h | 58 | ||||
-rw-r--r-- | src/wasm.h | 10 |
8 files changed, 84 insertions, 28 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index c88563109..10ad769ac 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -524,6 +524,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) { allocateGlobal(name, type, true, import->module, import->base); delete import; } else { + import->kind = Import::Function; wasm.addImport(import); } }; @@ -1087,6 +1088,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { import->module = ASM2WASM; import->base = F64_REM; import->type = ensureFunctionType("ddd", &wasm); + import->kind = Import::Function; wasm.addImport(import); } return call; @@ -1112,6 +1114,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { import->module = ASM2WASM; import->base = call->target; import->type = ensureFunctionType("iii", &wasm); + import->kind = Import::Function; wasm.addImport(import); } return call; @@ -1150,6 +1153,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { import->module = ASM2WASM; import->base = DEBUGGER; import->type = ensureFunctionType("v", &wasm); + import->kind = Import::Function; wasm.addImport(import); } return call; @@ -1262,6 +1266,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { import->module = ASM2WASM; import->base = F64_TO_INT; import->type = ensureFunctionType("id", &wasm); + import->kind = Import::Function; wasm.addImport(import); } return ret; diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index e333f64d8..ac64b188d 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -718,6 +718,7 @@ BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, const char* intern ret->module = externalModuleName; ret->base = externalBaseName; ret->type = (FunctionType*)type; + ret->kind = Import::Function; wasm->addImport(ret); return ret; } diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 5d7520a77..9ecb3f409 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -373,7 +373,7 @@ void BinaryenModuleOptimize(BinaryenModuleRef module); void BinaryenModuleAutoDrop(BinaryenModuleRef module); // Serialize a module into binary form. -// @return how many bytes were written. This will be less than or equal to bufferSize +// @return how many bytes were written. This will be less than or equal to outputSize size_t BinaryenModuleWrite(BinaryenModuleRef module, char* output, size_t outputSize); // Deserialize a module from binary form. diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 0152f917c..e9e70c1b0 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -534,6 +534,13 @@ struct PrintSExpression : public Visitor<PrintSExpression> { } void visitImport(Import *curr) { printOpening(o, "import "); + switch (curr->kind) { + case Export::Function: break; + case Export::Table: o << "table "; break; + case Export::Memory: o << "memory "; break; + case Export::Global: o << "global "; break; + default: WASM_UNREACHABLE(); + } printName(curr->name) << ' '; printText(o, curr->module.str) << ' '; printText(o, curr->base.str); diff --git a/src/wasm-binary.h b/src/wasm-binary.h index e250e26ff..0949e324b 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -572,7 +572,14 @@ public: o << U32LEB(wasm->imports.size()); for (auto& import : wasm->imports) { if (debug) std::cerr << "write one" << std::endl; - o << U32LEB(getFunctionTypeIndex(import->type->name)); + o << U32LEB(import->kind); + switch (import->kind) { + case Export::Function: o << U32LEB(getFunctionTypeIndex(import->type->name)); + case Export::Table: break; + case Export::Memory: break; + case Export::Global: break; + default: WASM_UNREACHABLE(); + } writeInlineString(import->module.str); writeInlineString(import->base.str); } @@ -1492,10 +1499,20 @@ public: if (debug) std::cerr << "read one" << std::endl; auto curr = new Import; curr->name = Name(std::string("import$") + std::to_string(i)); - auto index = getU32LEB(); - assert(index < wasm.functionTypes.size()); - curr->type = wasm.getFunctionType(index); - assert(curr->type->name.is()); + curr->kind = (Import::Kind)getU32LEB(); + switch (curr->kind) { + case Export::Function: { + auto index = getU32LEB(); + assert(index < wasm.functionTypes.size()); + curr->type = wasm.getFunctionType(index); + assert(curr->type->name.is()); + break; + } + case Export::Table: break; + case Export::Memory: break; + case Export::Global: break; + default: WASM_UNREACHABLE(); + } curr->module = getInlineString(); curr->base = getInlineString(); wasm.addImport(curr); diff --git a/src/wasm-linker.cpp b/src/wasm-linker.cpp index 9e968598a..1631e17a9 100644 --- a/src/wasm-linker.cpp +++ b/src/wasm-linker.cpp @@ -50,6 +50,7 @@ void Linker::ensureImport(Name target, std::string signature) { import->name = import->base = target; import->module = ENV; import->type = ensureFunctionType(signature, &out.wasm); + import->kind = Import::Function; out.wasm.addImport(import); } } @@ -339,6 +340,7 @@ void Linker::emscriptenGlue(std::ostream& o) { import->name = import->base = curr->target; import->module = ENV; import->type = ensureFunctionType(getSig(curr), &parent->out.wasm); + import->kind = Import::Function; parent->out.wasm.addImport(import); } } diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index b6270217b..17185a925 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -1440,33 +1440,49 @@ private: im->name = Name::fromInt(importCounter); } importCounter++; + if (!s[i]->quoted()) { + if (s[i]->str() == MEMORY) { + im->kind = Import::Memory; + } else if (s[2]->str() == TABLE) { + im->kind = Import::Table; + } else if (s[2]->str() == GLOBAL) { + im->kind = Import::Table; + } else { + WASM_UNREACHABLE(); + } + i++; + } else { + im->kind = Import::Function; + } im->module = s[i++]->str(); if (!s[i]->isStr()) throw ParseException("no name for import"); im->base = s[i++]->str(); - std::unique_ptr<FunctionType> type = make_unique<FunctionType>(); - if (s.size() > i) { - Element& params = *s[i]; - IString id = params[0]->str(); - if (id == PARAM) { - for (size_t i = 1; i < params.size(); i++) { - type->params.push_back(stringToWasmType(params[i]->str())); + if (im->kind == Import::Function) { + std::unique_ptr<FunctionType> type = make_unique<FunctionType>(); + if (s.size() > i) { + Element& params = *s[i]; + IString id = params[0]->str(); + if (id == PARAM) { + for (size_t i = 1; i < params.size(); i++) { + type->params.push_back(stringToWasmType(params[i]->str())); + } + } else if (id == RESULT) { + type->result = stringToWasmType(params[1]->str()); + } else if (id == TYPE) { + IString name = params[1]->str(); + if (!wasm.checkFunctionType(name)) throw ParseException("bad function type for import"); + *type = *wasm.getFunctionType(name); + } else { + throw ParseException("bad import element"); + } + if (s.size() > i+1) { + Element& result = *s[i+1]; + assert(result[0]->str() == RESULT); + type->result = stringToWasmType(result[1]->str()); } - } else if (id == RESULT) { - type->result = stringToWasmType(params[1]->str()); - } else if (id == TYPE) { - IString name = params[1]->str(); - if (!wasm.checkFunctionType(name)) throw ParseException("bad function type for import"); - *type = *wasm.getFunctionType(name); - } else { - throw ParseException("bad import element"); - } - if (s.size() > i+1) { - Element& result = *s[i+1]; - assert(result[0]->str() == RESULT); - type->result = stringToWasmType(result[1]->str()); } + im->type = ensureFunctionType(getSig(type.get()), &wasm); } - im->type = ensureFunctionType(getSig(type.get()), &wasm); wasm.addImport(im.release()); } diff --git a/src/wasm.h b/src/wasm.h index f93fb3a80..f26cf05a9 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -1430,10 +1430,18 @@ public: class Import { public: + enum Kind { + Function = 0, + Table = 1, + Memory = 2, + Global = 3, + }; + Import() : type(nullptr) {} Name name, module, base; // name = module.base - FunctionType* type; + Kind kind; + FunctionType* type; // for Function imports }; class Export { |