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 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/shell-interface.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) { -- cgit v1.2.3 From 4a2c27df27f3415ecd9950110ceddde84008dae4 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 20 Sep 2016 11:46:38 -0700 Subject: table parsing and executing fixes --- src/shell-interface.h | 3 ++- src/wasm-s-parser.h | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'src/shell-interface.h') diff --git a/src/shell-interface.h b/src/shell-interface.h index 4d125ea09..8cb7c2672 100644 --- a/src/shell-interface.h +++ b/src/shell-interface.h @@ -143,7 +143,8 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface { Literal callTable(Index index, LiteralList& arguments, WasmType result, ModuleInstance& instance) override { if (index >= table.size()) trap("callTable overflow"); - auto* func = instance.wasm.getFunction(table[index]); + auto* func = instance.wasm.checkFunction(table[index]); + if (!func) trap("uninitialized table element"); if (func->params.size() != arguments.size()) trap("callIndirect: bad # of arguments"); for (size_t i = 0; i < func->params.size(); i++) { if (func->params[i] != arguments[i].type) { diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 0ab2ed0dc..fde62d7ce 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -62,6 +62,8 @@ class Element { bool dollared_; bool quoted_; + #define element_assert(condition) assert((condition) ? true : (std::cerr << "on: " << *this << '\n' && 0)); + public: Element(MixedArena& allocator) : isList_(true), list_(allocator), line(-1), col(-1) {} @@ -80,7 +82,7 @@ public: } Element* operator[](unsigned i) { - if (i >= list().size()) assert(0 && "expected more elements in list"); + if (i >= list().size()) element_assert(0 && "expected more elements in list"); return list()[i]; } @@ -91,12 +93,12 @@ public: // string methods IString str() { - assert(!isList_); + element_assert(!isList_); return str_; } const char* c_str() { - assert(!isList_); + element_assert(!isList_); return str_.str; } @@ -1640,6 +1642,13 @@ private: im->globalType = stringToWasmType(inner2[1]->str()); throw ParseException("cannot import a mutable global", s.line, s.col); } + } else if (im->kind == Import::Table) { + if (j < inner.size() - 1) { + wasm.table.initial = atoi(inner[j++]->c_str()); + } + if (j < inner.size() - 1) { + wasm.table.max = atoi(inner[j++]->c_str()); + } } wasm.addImport(im.release()); } @@ -1760,13 +1769,11 @@ private: void parseElem(Element& s, Index i = 1) { if (!seenTable) throw ParseException("elem without table", s.line, s.col); - Expression* offset; - if (s[i]->isList()) { - // there is an init expression - offset = parseExpression(s[i++]); - } else { - offset = allocator.alloc()->set(Literal(int32_t(0))); + if (!s[i]->isList()) { + // the table is named + i++; } + auto* offset = parseExpression(s[i++]); Table::Segment segment(offset); for (; i < s.size(); i++) { segment.data.push_back(getFunctionName(*s[i])); -- cgit v1.2.3 From c79bbb19b59161e0768674816b477f74355912b1 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 21 Sep 2016 15:30:12 -0700 Subject: error on putting spectest.print in a table --- src/shell-interface.h | 4 ++++ src/tools/wasm-shell.cpp | 11 +++++++++++ 2 files changed, 15 insertions(+) (limited to 'src/shell-interface.h') diff --git a/src/shell-interface.h b/src/shell-interface.h index 8cb7c2672..b9a90131b 100644 --- a/src/shell-interface.h +++ b/src/shell-interface.h @@ -122,6 +122,10 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface { case f64: globals[import->name] = Literal(double(666.6)); break; default: WASM_UNREACHABLE(); } + } else if (import->kind == Import::Memory && import->module == SPECTEST && import->base == MEMORY) { + // imported memory has initial 1 and max 2 + wasm.memory.initial = 1; + wasm.memory.max = 2; } } } diff --git a/src/tools/wasm-shell.cpp b/src/tools/wasm-shell.cpp index 83d39c9d4..670f5783d 100644 --- a/src/tools/wasm-shell.cpp +++ b/src/tools/wasm-shell.cpp @@ -171,6 +171,17 @@ static void run_asserts(Name moduleName, size_t* i, bool* checked, Module* wasm, break; } } + for (auto& segment : wasm.table.segments) { + for (auto name : segment.data) { + // spec tests consider it illegal to use spectest.print in a table + if (auto* import = wasm.checkImport(name)) { + if (import->module == SPECTEST && import->base == PRINT) { + std::cerr << "cannot put spectest.print in table\n"; + invalid = true; + } + } + } + } } if (!invalid) { Colors::red(std::cerr); -- cgit v1.2.3