/* * Copyright 2015 WebAssembly Community Group participants * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "wasm-s-parser.h" #include #include #include #include "asm_v_wasm.h" #include "asmjs/shared-constants.h" #include "ir/branch-utils.h" #include "ir/function-type-utils.h" #include "shared-constants.h" #include "wasm-binary.h" #include "wasm-builder.h" #define abort_on(str) { throw ParseException(std::string("abort_on ") + str); } #define element_assert(condition) assert((condition) ? true : (std::cerr << "on: " << *this << '\n' && 0)); using cashew::IString; namespace { int unhex(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; throw wasm::ParseException("invalid hexadecimal"); } } namespace wasm { static Address getCheckedAddress(const Element* s, const char* errorText) { uint64_t num = atoll(s->c_str()); if (num > std::numeric_limits::max()) { throw ParseException(errorText, s->line, s->col); } return num; } Element::List& Element::list() { if (!isList()) throw ParseException("expected list", line, col); return list_; } Element* Element::operator[](unsigned i) { if (!isList()) throw ParseException("expected list", line, col); if (i >= list().size()) throw ParseException("expected more elements in list", line, col); return list()[i]; } IString Element::str() const { if (!isStr()) throw ParseException("expected string", line, col); return str_; } const char* Element::c_str() const { if (!isStr()) throw ParseException("expected string", line, col); return str_.str; } Element* Element::setString(IString str__, bool dollared__, bool quoted__) { isList_ = false; str_ = str__; dollared_ = dollared__; quoted_ = quoted__; return this; } Element* Element::setMetadata(size_t line_, size_t col_, SourceLocation* startLoc_) { line = line_; col = col_; startLoc = startLoc_; return this; } std::ostream& operator<<(std::ostream& o, Element& e) { if (e.isList_) { o << '('; for (auto item : e.list_) o << ' ' << *item; o << " )"; } else { o << e.str_.str; } return o; } void Element::dump() { std::cout << "dumping " << this << " : " << *this << ".\n"; } SExpressionParser::SExpressionParser(char* input) : input(input), loc(nullptr) { root = nullptr; line = 1; lineStart = input; while (!root) { // keep parsing until we pass an initial comment root = parse(); } } Element* SExpressionParser::parse() { std::vector stack; std::vector stackLocs; Element *curr = allocator.alloc(); while (1) { skipWhitespace(); if (input[0] == 0) break; if (input[0] == '(') { input++; stack.push_back(curr); curr = allocator.alloc()->setMetadata(line, input - lineStart - 1, loc); stackLocs.push_back(loc); assert(stack.size() == stackLocs.size()); } else if (input[0] == ')') { input++; curr->endLoc = loc; auto last = curr; if (stack.empty()) { throw ParseException("s-expr stack empty"); } curr = stack.back(); assert(stack.size() == stackLocs.size()); stack.pop_back(); loc = stackLocs.back(); stackLocs.pop_back(); curr->list().push_back(last); } else { curr->list().push_back(parseString()); } } if (stack.size() != 0) throw ParseException("stack is not empty", curr->line, curr->col); return curr; } void SExpressionParser::parseDebugLocation() { // Extracting debug location (if valid) char* debugLoc = input + 3; // skipping ";;@" while (debugLoc[0] && debugLoc[0] == ' ') debugLoc++; char* debugLocEnd = debugLoc; while (debugLocEnd[0] && debugLocEnd[0] != '\n') debugLocEnd++; char* pos = debugLoc; while (pos < debugLocEnd && pos[0] != ':') pos++; if (pos >= debugLocEnd) { return; // no line number } std::string name(debugLoc, pos); char* lineStart = ++pos; while (pos < debugLocEnd && pos[0] != ':') pos++; std::string lineStr(lineStart, pos); if (pos >= debugLocEnd) { return; // no column number } std::string colStr(++pos, debugLocEnd); void* buf = allocator.allocSpace(sizeof(SourceLocation), alignof(SourceLocation)); loc = new (buf) SourceLocation(IString(name.c_str(), false), atoi(lineStr.c_str()), atoi(colStr.c_str())); } void SExpressionParser::skipWhitespace() { while (1) { while (isspace(input[0])) { if (input[0] == '\n') { line++; lineStart = input + 1; } input++; } if (input[0] == ';' && input[1] == ';') { if (input[2] == '@') { parseDebugLocation(); } while (input[0] && input[0] != '\n') input++; line++; lineStart = ++input; } else if (input[0] == '(' && input[1] == ';') { // Skip nested block comments. input += 2; int depth = 1; while (1) { if (input[0] == 0) { return; } if (input[0] == '(' && input[1] == ';') { input += 2; depth++; } else if (input[0] == ';' && input[1] == ')') { input += 2; --depth; if (depth == 0) { break; } } else if (input[0] == '\n') { line++; lineStart = input; input++; } else { input++; } } } else { return; } } } Element* SExpressionParser::parseString() { bool dollared = false; if (input[0] == '$') { input++; dollared = true; } char *start = input; if (input[0] == '"') { // parse escaping \", but leave code escaped - we'll handle escaping in memory segments specifically input++; std::string str; while (1) { if (input[0] == 0) throw ParseException("unterminated string", line, start - lineStart); if (input[0] == '"') break; if (input[0] == '\\') { str += input[0]; if (input[1] == 0) throw ParseException("unterminated string escape", line, start - lineStart); str += input[1]; input += 2; continue; } str += input[0]; input++; } input++; return allocator.alloc()->setString(IString(str.c_str(), false), dollared, true)->setMetadata(line, start - lineStart, loc); } while (input[0] && !isspace(input[0]) && input[0] != ')' && input[0] != '(' && input[0] != ';') input++; if (start == input) throw ParseException("expected string", line, input - lineStart); char temp = input[0]; input[0] = 0; auto ret = allocator.alloc()->setString(IString(start, false), dollared, false)->setMetadata(line, start - lineStart, loc); input[0] = temp; return ret; } SExpressionWasmBuilder::SExpressionWasmBuilder(Module& wasm, Element& module, Name* moduleName) : wasm(wasm), allocator(wasm.allocator), globalCounter(0) { if (module.size() == 0) throw ParseException("empty toplevel, expected module"); if (module[0]->str() != MODULE) throw ParseException("toplevel does not start with module"); if (module.size() == 1) return; Index i = 1; if (module[i]->dollared()) { if (moduleName) { *moduleName = module[i]->str(); } i++; } if (i < module.size() && module[i]->isStr()) { // these s-expressions contain a binary module, actually std::vector data; while (i < module.size()) { auto str = module[i++]->c_str(); if (auto size = strlen(str)) { stringToBinary(str, size, data); } } WasmBinaryBuilder binaryBuilder(wasm, data, false); binaryBuilder.read(); return; } Index implementedFunctions = 0; functionCounter = 0; for (unsigned j = i; j < module.size(); j++) { auto& s = *module[j]; preParseFunctionType(s); preParseImports(s); if (s[0]->str() == FUNC && !isImport(s)) { implementedFunctions++; } } functionCounter -= implementedFunctions; // we go through the functions again, now parsing them, and the counter begins from where imports ended for (unsigned j = i; j < module.size(); j++) { parseModuleElement(*module[j]); } } bool SExpressionWasmBuilder::isImport(Element& curr) { for (Index i = 0; i < curr.size(); i++) { auto& x = *curr[i]; if (x.isList() && x.size() > 0 && x[0]->isStr() && x[0]->str() == IMPORT) return true; } return false; } void SExpressionWasmBuilder::preParseImports(Element& curr) { IString id = curr[0]->str(); if (id == IMPORT) parseImport(curr); if (isImport(curr)) { if (id == FUNC) parseFunction(curr, true /* preParseImport */); else if (id == GLOBAL) parseGlobal(curr, true /* preParseImport */); else if (id == TABLE) parseTable(curr, true /* preParseImport */); else if (id == MEMORY) parseMemory(curr, true /* preParseImport */); else throw ParseException("fancy import we don't support yet", curr.line, curr.col); } } void SExpressionWasmBuilder::parseModuleElement(Element& curr) { if (isImport(curr)) return; // already done IString id = curr[0]->str(); if (id == START) return parseStart(curr); if (id == FUNC) return parseFunction(curr); if (id == MEMORY) return parseMemory(curr); if (id == DATA) return parseData(curr); if (id == EXPORT) return parseExport(curr); if (id == IMPORT) return; // already done if (id == GLOBAL) return parseGlobal(curr); if (id == TABLE) return parseTable(curr); if (id == ELEM) return parseElem(curr); if (id == TYPE) return; // already done std::cerr << "bad module element " << id.str << '\n'; throw ParseException("unknown module element", curr.line, curr.col); } Name SExpressionWasmBuilder::getFunctionName(Element& s) { if (s.dollared()) { return s.str(); } else { // index size_t offset = atoi(s.str().c_str()); if (offset >= functionNames.size()) throw ParseException("unknown function in getFunctionName"); return functionNames[offset]; } } Name SExpressionWasmBuilder::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]; } } Name SExpressionWasmBuilder::getGlobalName(Element& s) { if (s.dollared()) { return s.str(); } else { // index size_t offset = atoi(s.str().c_str()); if (offset >= globalNames.size()) throw ParseException("unknown global in getGlobalName"); return globalNames[offset]; } } void SExpressionWasmBuilder::preParseFunctionType(Element& s) { IString id = s[0]->str(); if (id == TYPE) return parseType(s); if (id != FUNC) return; size_t i = 1; Name name, exportName; i = parseFunctionNames(s, name, exportName); if (!name.is()) { // unnamed, use an index name = Name::fromInt(functionCounter); } functionNames.push_back(name); functionCounter++; FunctionType* type = nullptr; functionTypes[name] = none; std::vector params; for (;i < s.size(); i++) { Element& curr = *s[i]; IString id = curr[0]->str(); if (id == RESULT) { if (curr.size() > 2) throw ParseException("invalid result arity", curr.line, curr.col); functionTypes[name] = stringToType(curr[1]->str()); } else if (id == TYPE) { Name typeName = getFunctionTypeName(*curr[1]); if (!wasm.getFunctionTypeOrNull(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) { Index j = 1; if (curr[j]->dollared()) { // dollared input symbols cannot be types params.push_back(stringToType(curr[j + 1]->str(), true)); } else { while (j < curr.size()) { params.push_back(stringToType(curr[j++]->str(), true)); } } } } if (!type) { // if no function type provided, generate one, but reuse a previous one with the // right structure if there is one. // see https://github.com/WebAssembly/spec/pull/301 bool need = true; std::unique_ptr functionType = make_unique(); functionType->result = functionTypes[name]; functionType->params = std::move(params); for (auto& existing : wasm.functionTypes) { if (existing->structuralComparison(*functionType)) { need = false; break; } } if (need) { functionType->name = Name::fromInt(wasm.functionTypes.size()); functionTypeNames.push_back(functionType->name); if (wasm.getFunctionTypeOrNull(functionType->name)) throw ParseException("duplicate function type", s.line, s.col); wasm.addFunctionType(functionType.release()); } } } size_t SExpressionWasmBuilder::parseFunctionNames(Element& s, Name& name, Name& exportName) { size_t i = 1; while (i < s.size() && i < 3 && s[i]->isStr()) { if (s[i]->quoted()) { // an export name exportName = s[i]->str(); i++; } else if (s[i]->dollared()) { name = s[i]->str(); i++; } else { break; } } if (i < s.size() && s[i]->isList()) { auto& inner = *s[i]; if (inner.size() > 0 && inner[0]->str() == EXPORT) { exportName = inner[1]->str(); i++; } } #if 0 if (exportName.is() && !name.is()) { name = exportName; // useful for debugging } #endif return i; } void SExpressionWasmBuilder::parseFunction(Element& s, bool preParseImport) { size_t i = 1; Name name, exportName; i = parseFunctionNames(s, name, exportName); 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(); ex->name = exportName; ex->value = name; ex->kind = ExternalKind::Function; if (wasm.getExportOrNull(ex->name)) throw ParseException("duplicate export", s.line, s.col); wasm.addExport(ex.release()); } Expression* body = nullptr; localIndex = 0; otherIndex = 0; brokeToAutoBlock = false; std::vector typeParams; // we may have both params and a type. store the type info here std::vector params; std::vector vars; Type result = none; Name type; Block* autoBlock = nullptr; // we may need to add a block for the very top level Name importModule, importBase; auto makeFunction = [&]() { currFunction = std::unique_ptr(Builder(wasm).makeFunction( name, std::move(params), result, std::move(vars) )); }; auto ensureAutoBlock = [&]() { if (!autoBlock) { autoBlock = allocator.alloc(); autoBlock->list.push_back(body); body = autoBlock; } }; for (;i < s.size(); i++) { Element& curr = *s[i]; IString id = curr[0]->str(); if (id == PARAM || id == LOCAL) { size_t j = 1; while (j < curr.size()) { IString name; Type type = none; if (!curr[j]->dollared()) { // dollared input symbols cannot be types type = stringToType(curr[j]->str(), true); } if (type != none) { // a type, so an unnamed parameter name = Name::fromInt(localIndex); } else { name = curr[j]->str(); type = stringToType(curr[j+1]->str()); j++; } j++; if (id == PARAM) { params.emplace_back(name, type); } else { vars.emplace_back(name, type); } localIndex++; currLocalTypes[name] = type; } } else if (id == RESULT) { if (curr.size() > 2) throw ParseException("invalid result arity", curr.line, curr.col); result = stringToType(curr[1]->str()); } else if (id == TYPE) { Name name = getFunctionTypeName(*curr[1]); type = name; if (!wasm.getFunctionTypeOrNull(name)) throw ParseException("unknown function type"); FunctionType* type = wasm.getFunctionType(name); result = type->result; for (size_t j = 0; j < type->params.size(); j++) { IString name = Name::fromInt(j); Type currType = type->params[j]; typeParams.emplace_back(name, currType); currLocalTypes[name] = currType; } } else if (id == IMPORT) { importModule = curr[1]->str(); importBase = curr[2]->str(); } else { // body if (typeParams.size() > 0 && params.size() == 0) { params = typeParams; } if (!currFunction) makeFunction(); Expression* ex = parseExpression(curr); if (!body) { body = ex; } else { ensureAutoBlock(); autoBlock->list.push_back(ex); } } } // see https://github.com/WebAssembly/spec/pull/301 if (type.isNull()) { // if no function type name provided, then we generated one auto functionType = make_unique(sigToFunctionType(getSigFromStructs(result, params))); for (auto& existing : wasm.functionTypes) { if (existing->structuralComparison(*functionType)) { type = existing->name; break; } } if (!type.is()) throw ParseException("no function type [internal error?]", s.line, s.col); } if (importModule.is()) { // this is an import, actually if (!preParseImport) throw ParseException("!preParseImport in func"); auto im = make_unique(); im->name = name; im->module = importModule; im->base = importBase; im->type = type; FunctionTypeUtils::fillFunction(im.get(), wasm.getFunctionType(type)); functionTypes[name] = im->result; if (wasm.getFunctionOrNull(im->name)) throw ParseException("duplicate import", s.line, s.col); wasm.addFunction(im.release()); if (currFunction) throw ParseException("import module inside function dec"); currLocalTypes.clear(); nameMapper.clear(); return; } if (preParseImport) throw ParseException("preParseImport in func"); if (brokeToAutoBlock) { ensureAutoBlock(); autoBlock->name = FAKE_RETURN; } if (autoBlock) { autoBlock->finalize(result); } if (!currFunction) { makeFunction(); body = allocator.alloc(); } if (currFunction->result != result) throw ParseException("bad func declaration", s.line, s.col); currFunction->body = body; currFunction->type = type; if (s.startLoc) { currFunction->prologLocation.insert(getDebugLocation(*s.startLoc)); } if (s.endLoc) { currFunction->epilogLocation.insert(getDebugLocation(*s.endLoc)); } if (wasm.getFunctionOrNull(currFunction->name)) throw ParseException("duplicate function", s.line, s.col); wasm.addFunction(currFunction.release()); currLocalTypes.clear(); nameMapper.clear(); } Type SExpressionWasmBuilder::stringToType(const char* str, bool allowError, bool prefix) { if (str[0] == 'i') { if (str[1] == '3' && str[2] == '2' && (prefix || str[3] == 0)) return i32; if (str[1] == '6' && str[2] == '4' && (prefix || str[3] == 0)) return i64; } if (str[0] == 'f') { if (str[1] == '3' && str[2] == '2' && (prefix || str[3] == 0)) return f32; if (str[1] == '6' && str[2] == '4' && (prefix || str[3] == 0)) return f64; } if (str[0] == 'v') { if (str[1] == '1' && str[2] == '2' && str[3] == '8' && (prefix || str[4] == 0)) { return v128; } } if (allowError) return none; throw ParseException("invalid wasm type"); } Function::DebugLocation SExpressionWasmBuilder::getDebugLocation(const SourceLocation& loc) { IString file = loc.filename; auto& debugInfoFileNames = wasm.debugInfoFileNames; auto iter = debugInfoFileIndices.find(file); if (iter == debugInfoFileIndices.end()) { Index index = debugInfoFileNames.size(); debugInfoFileNames.push_back(file.c_str()); debugInfoFileIndices[file] = index; } uint32_t fileIndex = debugInfoFileIndices[file]; return {fileIndex, loc.line, loc.column}; } Expression* SExpressionWasmBuilder::parseExpression(Element& s) { Expression* result = makeExpression(s); if (s.startLoc) { currFunction->debugLocations[result] = getDebugLocation(*s.startLoc); } return result; } Expression* SExpressionWasmBuilder::makeExpression(Element& s) { #define INSTRUCTION_PARSER #include "gen-s-parser.inc" } Expression* SExpressionWasmBuilder::makeUnreachable() { return allocator.alloc(); } Expression* SExpressionWasmBuilder::makeNop() { return allocator.alloc(); } Expression* SExpressionWasmBuilder::makeBinary(Element& s, BinaryOp op) { auto ret = allocator.alloc(); ret->op = op; ret->left = parseExpression(s[1]); ret->right = parseExpression(s[2]); ret->finalize(); return ret; } Expression* SExpressionWasmBuilder::makeUnary(Element& s, UnaryOp op) { auto ret = allocator.alloc(); ret->op = op; ret->value = parseExpression(s[1]); ret->finalize(); return ret; } Expression* SExpressionWasmBuilder::makeSelect(Element& s) { auto ret = allocator.alloc