diff --git a/auto_update_tests.py b/auto_update_tests.py index d76d087506e..c6d376fa043 100755 --- a/auto_update_tests.py +++ b/auto_update_tests.py @@ -41,6 +41,10 @@ print ' '.join(cmd) actual = run_command(cmd) with open(os.path.join('test', wasm), 'w') as o: o.write(actual) + if 'debugInfo' in asm: + cmd += ['--source-map', os.path.join('test', wasm + '.map'), '-o', 'a.wasm'] + run_command(cmd) + for dot_s_dir in ['dot_s', 'llvm_autogenerated']: for s in sorted(os.listdir(os.path.join('test', dot_s_dir))): diff --git a/check.py b/check.py index 05c2d92a6e2..9d5b64f1824 100755 --- a/check.py +++ b/check.py @@ -189,6 +189,31 @@ def do_asm2wasm_test(): fail_with_error('wasm interpreter error: ' + err) # failed to pretty-print fail_with_error('wasm interpreter error') + # verify debug info + if 'debugInfo' in asm: + jsmap = 'a.wasm.map' + cmd += ['--source-map', jsmap, + '--source-map-url', 'http://example.org/' + jsmap, + '-o', 'a.wasm'] + run_command(cmd) + if not os.path.isfile(jsmap): + fail_with_error('Debug info map not created: %s' % jsmap) + with open(wasm + '.map', 'rb') as expected: + with open(jsmap, 'rb') as actual: + fail_if_not_identical(actual.read(), expected.read()) + with open('a.wasm', 'rb') as binary: + url_section_name = bytearray([16]) + bytearray('sourceMappingURL') + payload = 'http://example.org/' + jsmap + assert len(payload) < 256, 'name too long' + url_section_contents = bytearray([len(payload)]) + bytearray(payload) + print url_section_name + binary_contents = bytearray(binary.read()) + if url_section_name not in binary_contents: + fail_with_error('source map url section not found in binary') + if url_section_contents not in binary_contents[binary_contents.index(url_section_name):]: + fail_with_error('source map url not found in url section') + + print '\n[ checking asm2wasm binary reading/writing... ]\n' asmjs = os.path.join(options.binaryen_test, 'hello_world.asm.js') @@ -241,6 +266,8 @@ def do_asm2wasm_test(): print '..', t t = os.path.join(options.binaryen_test, t) cmd = WASM_DIS + [t] + if os.path.isfile(t + '.map'): cmd += ['--source-map', t + '.map'] + actual = run_command(cmd) with open(t + '.fromBinary') as f: diff --git a/src/asm2wasm.h b/src/asm2wasm.h index 16e7c95e1e9..b8439bdbf8d 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -1382,13 +1382,13 @@ void Asm2WasmBuilder::processAsm(Ref ast) { while (i < expressionStack.size()) { exp = expressionStack[i]; if (debugLocations.count(exp) == 0) { - debugLocations[exp] = { fileIndex, lineNumber }; + debugLocations[exp] = { fileIndex, lineNumber, 0 }; break; } i++; } } else { - debugLocations[exp] = { fileIndex, lineNumber }; + debugLocations[exp] = { fileIndex, lineNumber, 0 }; } break; } diff --git a/src/parsing.h b/src/parsing.h index d4df3c1c25b..be3c112f3f0 100644 --- a/src/parsing.h +++ b/src/parsing.h @@ -193,6 +193,25 @@ struct ParseException { } }; +struct MapParseException { + std::string text; + + MapParseException() : text("unknown parse error") {} + MapParseException(std::string text) : text(text) {} + + void dump(std::ostream& o) { + Colors::magenta(o); + o << "["; + Colors::red(o); + o << "map parse exception: "; + Colors::green(o); + o << text; + Colors::magenta(o); + o << "]"; + Colors::normal(o); + } +}; + // Helper for parsers that may not have unique label names. This transforms // the names into unique ones, as required by Binaryen IR. struct UniqueNameMapper { diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 6aebd612bc9..95f60e5e2c3 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -45,6 +45,7 @@ struct PrintSExpression : public Visitor { Module* currModule = nullptr; Function* currFunction = nullptr; + Function::DebugLocation lastPrintedLocation; PrintSExpression(std::ostream& o) : o(o) { setMinify(false); @@ -58,8 +59,11 @@ struct PrintSExpression : public Visitor { auto iter = debugLocations.find(curr); if (iter != debugLocations.end()) { auto fileName = currModule->debugInfoFileNames[iter->second.fileIndex]; - o << ";; " << fileName << ":" << iter->second.lineNumber << '\n'; - doIndent(o, indent); + if (lastPrintedLocation != iter->second) { + lastPrintedLocation = iter->second; + o << ";;@ " << fileName << ":" << iter->second.lineNumber << ":" << iter->second.columnNumber << '\n'; + doIndent(o, indent); + } } } Visitor::visit(curr); @@ -599,6 +603,7 @@ struct PrintSExpression : public Visitor { } void visitFunction(Function *curr) { currFunction = curr; + lastPrintedLocation = { 0, 0, 0 }; printOpening(o, "func ", true); printName(curr->name); if (curr->type.is()) { diff --git a/src/s2wasm.h b/src/s2wasm.h index 6ba4750ec49..efa4ad6013b 100644 --- a/src/s2wasm.h +++ b/src/s2wasm.h @@ -44,6 +44,7 @@ class S2WasmBuilder { MixedArena* allocator; LinkerObject* linkerObj; std::unique_ptr symbolInfo; + std::unordered_map fileIndexMap; public: S2WasmBuilder(const char* input, bool debug) @@ -601,7 +602,9 @@ class S2WasmBuilder { size_t fileId = getInt(); skipWhitespace(); auto quoted = getQuoted(); - WASM_UNUSED(fileId); WASM_UNUSED(quoted); // TODO: use the fileId and quoted + uint32_t index = wasm->debugInfoFileNames.size(); + fileIndexMap[fileId] = index; + wasm->debugInfoFileNames.push_back(std::string(quoted.begin(), quoted.end())); s = strchr(s, '\n'); return; } @@ -665,22 +668,31 @@ class S2WasmBuilder { mustMatch(":"); + Function::DebugLocation debugLocation = { 0, 0, 0 }; + bool useDebugLocation = false; auto recordFile = [&]() { if (debug) dump("file"); size_t fileId = getInt(); skipWhitespace(); auto quoted = getQuoted(); - WASM_UNUSED(fileId); WASM_UNUSED(quoted); // TODO: use the fileId and quoted + uint32_t index = wasm->debugInfoFileNames.size(); + fileIndexMap[fileId] = index; + wasm->debugInfoFileNames.push_back(std::string(quoted.begin(), quoted.end())); s = strchr(s, '\n'); }; auto recordLoc = [&]() { if (debug) dump("loc"); size_t fileId = getInt(); skipWhitespace(); - size_t row = getInt(); + uint32_t row = getInt(); skipWhitespace(); - size_t column = getInt(); - WASM_UNUSED(fileId); WASM_UNUSED(row); WASM_UNUSED(column); // TODO: use the fileId, row and column + uint32_t column = getInt(); + auto iter = fileIndexMap.find(fileId); + if (iter == fileIndexMap.end()) { + abort_on("idx"); + } + useDebugLocation = true; + debugLocation = { iter->second, row, column }; s = strchr(s, '\n'); }; auto recordLabel = [&]() { @@ -746,7 +758,10 @@ class S2WasmBuilder { // parse body func->body = allocator->alloc(); std::vector bstack; - auto addToBlock = [&bstack](Expression* curr) { + auto addToBlock = [&](Expression* curr) { + if (useDebugLocation) { + func->debugLocations[curr] = debugLocation; + } Expression* last = bstack.back(); if (last->is()) { last = last->cast()->body; diff --git a/src/tools/asm2wasm.cpp b/src/tools/asm2wasm.cpp index cf0715198ad..3fa97d9815b 100644 --- a/src/tools/asm2wasm.cpp +++ b/src/tools/asm2wasm.cpp @@ -36,6 +36,8 @@ int main(int argc, const char *argv[]) { bool legalizeJavaScriptFFI = true; Asm2WasmBuilder::TrapMode trapMode = Asm2WasmBuilder::TrapMode::JS; bool wasmOnly = false; + std::string sourceMapFilename; + std::string sourceMapUrl; std::string symbolMap; bool emitBinary = true; @@ -99,9 +101,15 @@ int main(int argc, const char *argv[]) { [&legalizeJavaScriptFFI](Options *o, const std::string &) { legalizeJavaScriptFFI = false; }) - .add("--debuginfo", "-g", "Emit names section and debug info (for debug info you must emit text, -S, for this to work)", + .add("--debuginfo", "-g", "Emit names section in wasm binary (or full debuginfo in wast)", Options::Arguments::Zero, [&](Options *o, const std::string &arguments) { options.passOptions.debugInfo = true; }) + .add("--source-map", "-sm", "Emit source map (if using binary output) to the specified file", + Options::Arguments::One, + [&sourceMapFilename](Options *o, const std::string &argument) { sourceMapFilename = argument; }) + .add("--source-map-url", "-su", "Use specified string as source map URL", + Options::Arguments::One, + [&sourceMapUrl](Options *o, const std::string &argument) { sourceMapUrl = argument; }) .add("--symbolmap", "-s", "Emit a symbol map (indexes => names)", Options::Arguments::One, [&](Options *o, const std::string &argument) { symbolMap = argument; }) @@ -136,8 +144,9 @@ int main(int argc, const char *argv[]) { } Asm2WasmPreProcessor pre; - // wasm binaries can contain a names section, but not full debug info - pre.debugInfo = options.passOptions.debugInfo && !emitBinary; + // wasm binaries can contain a names section, but not full debug info -- + // debug info is disabled if a map file is not specified with wasm binary + pre.debugInfo = options.passOptions.debugInfo && (!emitBinary || sourceMapFilename.size()); auto input( read_file>(options.extra["infile"], Flags::Text, options.debug ? Flags::Debug : Flags::Release)); char *start = pre.process(input.data()); @@ -204,6 +213,10 @@ int main(int argc, const char *argv[]) { writer.setDebugInfo(options.passOptions.debugInfo); writer.setSymbolMap(symbolMap); writer.setBinary(emitBinary); + if (emitBinary) { + writer.setSourceMapFilename(sourceMapFilename); + writer.setSourceMapUrl(sourceMapUrl); + } writer.write(wasm, options.extra["output"]); if (options.debug) std::cerr << "done." << std::endl; diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp index 067c0f28df6..e8003a5ca16 100644 --- a/src/tools/wasm-as.cpp +++ b/src/tools/wasm-as.cpp @@ -30,6 +30,8 @@ using namespace wasm; int main(int argc, const char *argv[]) { bool debugInfo = false; std::string symbolMap; + std::string sourceMapFilename; + std::string sourceMapUrl; Options options("wasm-as", "Assemble a .wast (WebAssembly text format) into a .wasm (WebAssembly binary format)"); options.extra["validate"] = "wasm"; options @@ -51,6 +53,12 @@ int main(int argc, const char *argv[]) { .add("--debuginfo", "-g", "Emit names section and debug info", Options::Arguments::Zero, [&](Options *o, const std::string &arguments) { debugInfo = true; }) + .add("--source-map", "-sm", "Emit source map to the specified file", + Options::Arguments::One, + [&sourceMapFilename](Options *o, const std::string &argument) { sourceMapFilename = argument; }) + .add("--source-map-url", "-su", "Use specified string as source map URL", + Options::Arguments::One, + [&sourceMapUrl](Options *o, const std::string &argument) { sourceMapUrl = argument; }) .add("--symbolmap", "-s", "Emit a symbol map (indexes => names)", Options::Arguments::One, [&](Options *o, const std::string &argument) { symbolMap = argument; }) @@ -86,13 +94,23 @@ int main(int argc, const char *argv[]) { if (options.debug) std::cerr << "binarification..." << std::endl; BufferWithRandomAccess buffer(options.debug); WasmBinaryWriter writer(&wasm, buffer, options.debug); - writer.setDebugInfo(debugInfo); + // if debug info is used, then we want to emit the names section + writer.setNamesSection(debugInfo); + std::unique_ptr sourceMapStream = nullptr; + if (sourceMapFilename.size()) { + sourceMapStream = make_unique(); + sourceMapStream->open(sourceMapFilename); + writer.setSourceMap(sourceMapStream.get(), sourceMapUrl); + } if (symbolMap.size() > 0) writer.setSymbolMap(symbolMap); writer.write(); if (options.debug) std::cerr << "writing to output..." << std::endl; Output output(options.extra["output"], Flags::Binary, options.debug ? Flags::Debug : Flags::Release); buffer.writeTo(output); + if (sourceMapStream) { + sourceMapStream->close(); + } if (options.debug) std::cerr << "Done." << std::endl; } diff --git a/src/tools/wasm-dis.cpp b/src/tools/wasm-dis.cpp index 93c28691362..e6fd7baddf9 100644 --- a/src/tools/wasm-dis.cpp +++ b/src/tools/wasm-dis.cpp @@ -28,6 +28,7 @@ using namespace cashew; using namespace wasm; int main(int argc, const char *argv[]) { + std::string sourceMapFilename; Options options("wasm-dis", "Un-assemble a .wasm (WebAssembly binary format) into a .wast (WebAssembly text format)"); options.add("--output", "-o", "Output file (stdout if not specified)", Options::Arguments::One, @@ -35,6 +36,9 @@ int main(int argc, const char *argv[]) { o->extra["output"] = argument; Colors::disable(); }) + .add("--source-map", "-sm", "Consume source map from the specified file to add location information", + Options::Arguments::One, + [&sourceMapFilename](Options *o, const std::string &argument) { sourceMapFilename = argument; }) .add_positional("INFILE", Options::Arguments::One, [](Options *o, const std::string &argument) { o->extra["infile"] = argument; @@ -46,11 +50,23 @@ int main(int argc, const char *argv[]) { if (options.debug) std::cerr << "parsing binary..." << std::endl; Module wasm; try { + std::unique_ptr sourceMapStream; WasmBinaryBuilder parser(wasm, input, options.debug); + if (sourceMapFilename.size()) { + sourceMapStream = make_unique(); + sourceMapStream->open(sourceMapFilename); + parser.setDebugLocations(sourceMapStream.get()); + } parser.read(); + if (sourceMapStream) { + sourceMapStream->close(); + } } catch (ParseException& p) { p.dump(std::cerr); Fatal() << "error in parsing wasm binary"; + } catch (MapParseException& p) { + p.dump(std::cerr); + Fatal() << "error in parsing wasm source mapping"; } if (options.debug) std::cerr << "Printing..." << std::endl; diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 921f90ac8b5..889a3aa6c90 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -308,6 +308,7 @@ enum EncodedType { namespace UserSections { extern const char* Name; +extern const char* SourceMapUrl; enum Subsection { NameFunction = 1, @@ -534,8 +535,11 @@ inline S32LEB binaryWasmType(WasmType type) { class WasmBinaryWriter : public Visitor { Module* wasm; BufferWithRandomAccess& o; + Function* currFunction = nullptr; bool debug; bool debugInfo = true; + std::ostream* sourceMap = nullptr; + std::string sourceMapUrl; std::string symbolMap; MixedArena allocator; @@ -546,7 +550,11 @@ class WasmBinaryWriter : public Visitor { prepare(); } - void setDebugInfo(bool set) { debugInfo = set; } + void setNamesSection(bool set) { debugInfo = set; } + void setSourceMap(std::ostream* set, std::string url) { + sourceMap = set; + sourceMapUrl = url; + } void setSymbolMap(std::string set) { symbolMap = set; } void write(); @@ -582,8 +590,13 @@ class WasmBinaryWriter : public Visitor { void writeFunctionTableDeclaration(); void writeTableElements(); void writeNames(); + void writeSourceMapUrl(); void writeSymbolMap(); + void writeSourceMapProlog(); + void writeSourceMapEpilog(); + void writeDebugLocation(size_t offset, const Function::DebugLocation& loc); + // helpers void writeInlineString(const char* name); void writeInlineBuffer(const char* data, size_t size); @@ -606,6 +619,20 @@ class WasmBinaryWriter : public Visitor { void recurse(Expression*& curr); std::vector breakStack; + Function::DebugLocation lastDebugLocation; + size_t lastBytecodeOffset; + + void visit(Expression* curr) { + if (sourceMap && currFunction) { + // Dump the sourceMap debug info + auto& debugLocations = currFunction->debugLocations; + auto iter = debugLocations.find(curr); + if (iter != debugLocations.end() && iter->second != lastDebugLocation) { + writeDebugLocation(o.size(), iter->second); + } + } + Visitor::visit(curr); + } void visitBlock(Block *curr); // emits a node, but if it is a block with no name, emit a list of its contents @@ -641,14 +668,17 @@ class WasmBinaryBuilder { MixedArena& allocator; std::vector& input; bool debug; + std::istream* sourceMap; + std::pair nextDebugLocation; size_t pos = 0; Index startIndex = -1; + bool useDebugLocation; std::set seenSections; public: - WasmBinaryBuilder(Module& wasm, std::vector& input, bool debug) : wasm(wasm), allocator(wasm.allocator), input(input), debug(debug) {} + WasmBinaryBuilder(Module& wasm, std::vector& input, bool debug) : wasm(wasm), allocator(wasm.allocator), input(input), debug(debug), sourceMap(nullptr), nextDebugLocation(0, { 0, 0, 0 }), useDebugLocation(false) {} void read(); void readUserSection(size_t payloadLen); @@ -737,6 +767,15 @@ class WasmBinaryBuilder { void readTableElements(); void readNames(size_t); + // Debug information reading helpers + void setDebugLocations(std::istream* sourceMap_) { + sourceMap = sourceMap_; + } + Function::DebugLocation debugLocation; + std::unordered_map debugInfoFileIndices; + void readNextDebugLocation(); + void readSourceMapHeader(); + // AST reading int depth = 0; // only for debugging diff --git a/src/wasm-io.h b/src/wasm-io.h index 803cbaf90c3..afdc4503ce2 100644 --- a/src/wasm-io.h +++ b/src/wasm-io.h @@ -48,11 +48,15 @@ class ModuleWriter : public ModuleIO { bool binary = true; bool debugInfo = false; std::string symbolMap; + std::string sourceMapFilename; + std::string sourceMapUrl; public: void setBinary(bool binary_) { binary = binary_; } void setDebugInfo(bool debugInfo_) { debugInfo = debugInfo_; } void setSymbolMap(std::string symbolMap_) { symbolMap = symbolMap_; } + void setSourceMapFilename(std::string sourceMapFilename_) { sourceMapFilename = sourceMapFilename_; } + void setSourceMapUrl(std::string sourceMapUrl_) { sourceMapUrl = sourceMapUrl_; } // write text void writeText(Module& wasm, std::string filename); diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index be1f6b6999f..b02b6523bcd 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -28,6 +28,16 @@ namespace wasm { +class SourceLocation +{ +public: + cashew::IString filename; + uint32_t line; + uint32_t column; + SourceLocation(cashew::IString filename_, uint32_t line_, uint32_t column_ = 0) + : filename(filename_), line(line_), column(column_) {} +}; + // // An element in an S-Expression: a list or a string // @@ -41,7 +51,7 @@ class Element { bool quoted_; public: - Element(MixedArena& allocator) : isList_(true), list_(allocator), line(-1), col(-1) {} + Element(MixedArena& allocator) : isList_(true), list_(allocator), line(-1), col(-1), loc(nullptr) {} bool isList() { return isList_; } bool isStr() { return !isList_; } @@ -49,6 +59,7 @@ class Element { bool quoted() { return isStr() && quoted_; } size_t line, col; + SourceLocation* loc; // list methods List& list(); @@ -61,14 +72,13 @@ class Element { cashew::IString str(); const char* c_str(); Element* setString(cashew::IString str__, bool dollared__, bool quoted__); - Element* setMetadata(size_t line_, size_t col_); + Element* setMetadata(size_t line_, size_t col_, SourceLocation* loc_); // printing friend std::ostream& operator<<(std::ostream& o, Element& e); void dump(); }; - // // Generic S-Expression parsing into lists // @@ -76,6 +86,7 @@ class SExpressionParser { char* input; size_t line; char* lineStart; + SourceLocation* loc; MixedArena allocator; @@ -87,6 +98,7 @@ class SExpressionParser { private: Element* parse(); void skipWhitespace(); + void parseDebugLocation(); Element* parseString(); }; @@ -102,6 +114,7 @@ class SExpressionWasmBuilder { int functionCounter; int globalCounter; std::map functionTypes; // we need to know function return types before we parse their contents + std::unordered_map debugInfoFileIndices; public: // Assumes control of and modifies the input. @@ -147,6 +160,7 @@ class SExpressionWasmBuilder { Expression* parseExpression(Element& s); private: + Expression* makeExpression(Element& s); Expression* makeBinary(Element& s, BinaryOp op, WasmType type); Expression* makeUnary(Element& s, UnaryOp op, WasmType type); Expression* makeSelect(Element& s); diff --git a/src/wasm.h b/src/wasm.h index 3caab9dbc52..dba08d92ead 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -527,7 +527,9 @@ class Function { std::map localIndices; struct DebugLocation { - uint32_t fileIndex, lineNumber; + uint32_t fileIndex, lineNumber, columnNumber; + bool operator==(const DebugLocation& other) const { return fileIndex == other.fileIndex && lineNumber == other.lineNumber && columnNumber == other.columnNumber; } + bool operator!=(const DebugLocation& other) const { return !(*this == other); } }; std::unordered_map debugLocations; diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 65c6f2a2157..c4bc66f7681 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -33,6 +33,9 @@ void WasmBinaryWriter::prepare() { void WasmBinaryWriter::write() { writeHeader(); + if (sourceMap) { + writeSourceMapProlog(); + } writeTypes(); writeImports(); @@ -46,8 +49,12 @@ void WasmBinaryWriter::write() { writeFunctions(); writeDataSegments(); if (debugInfo) writeNames(); + if (sourceMap) writeSourceMapUrl(); if (symbolMap.size() > 0) writeSymbolMap(); + if (sourceMap) { + writeSourceMapEpilog(); + } finishUp(); } @@ -236,6 +243,7 @@ void WasmBinaryWriter::writeFunctions() { size_t sizePos = writeU32LEBPlaceholder(); size_t start = o.size(); Function* function = wasm->functions[i].get(); + currFunction = function; mappedLocals.clear(); numLocalsByType.clear(); if (debug) std::cerr << "writing" << function->name << std::endl; @@ -258,6 +266,7 @@ void WasmBinaryWriter::writeFunctions() { if (debug) std::cerr << "body size: " << size << ", writing at " << sizePos << ", next starts at " << o.size() << std::endl; o.writeAt(sizePos, U32LEB(size)); } + currFunction = nullptr; finishSection(start); } @@ -420,6 +429,14 @@ void WasmBinaryWriter::writeNames() { finishSection(start); } +void WasmBinaryWriter::writeSourceMapUrl() { + if (debug) std::cerr << "== writeSourceMapUrl" << std::endl; + auto start = startSection(BinaryConsts::Section::User); + writeInlineString(BinaryConsts::UserSections::SourceMapUrl); + writeInlineString(sourceMapUrl.c_str()); + finishSection(start); +} + void WasmBinaryWriter::writeSymbolMap() { std::ofstream file(symbolMap); for (auto& import : wasm->imports) { @@ -433,6 +450,50 @@ void WasmBinaryWriter::writeSymbolMap() { file.close(); } +void WasmBinaryWriter::writeSourceMapProlog() { + lastDebugLocation = { 0, /* lineNumber = */ 1, 0 }; + lastBytecodeOffset = 0; + *sourceMap << "{\"version\":3,\"sources\":["; + for (size_t i = 0; i < wasm->debugInfoFileNames.size(); i++) { + if (i > 0) *sourceMap << ","; + // TODO respect JSON string encoding, e.g. quotes and control chars. + *sourceMap << "\"" << wasm->debugInfoFileNames[i] << "\""; + } + *sourceMap << "],\"names\":[],\"mappings\":\""; +} + +void WasmBinaryWriter::writeSourceMapEpilog() { + *sourceMap << "\"}"; +} + +static void writeBase64VLQ(std::ostream& out, int32_t n) { + uint32_t value = n >= 0 ? n << 1 : ((-n) << 1) | 1; + while (1) { + uint32_t digit = value & 0x1F; + value >>= 5; + if (!value) { + // last VLQ digit -- base64 codes 'A'..'Z', 'a'..'f' + out << char(digit < 26 ? 'A' + digit : 'a' + digit - 26); + break; + } + // more VLG digit will follow -- add continuation bit (0x20), + // base64 codes 'g'..'z', '0'..'9', '+', '/' + out << char(digit < 20 ? 'g' + digit : digit < 30 ? '0' + digit - 20 : digit == 30 ? '+' : '/'); + } +} + +void WasmBinaryWriter::writeDebugLocation(size_t offset, const Function::DebugLocation& loc) { + if (lastBytecodeOffset > 0) { + *sourceMap << ","; + } + writeBase64VLQ(*sourceMap, int32_t(offset - lastBytecodeOffset)); + writeBase64VLQ(*sourceMap, int32_t(loc.fileIndex - lastDebugLocation.fileIndex)); + writeBase64VLQ(*sourceMap, int32_t(loc.lineNumber - lastDebugLocation.lineNumber)); + writeBase64VLQ(*sourceMap, int32_t(loc.columnNumber - lastDebugLocation.columnNumber)); + lastDebugLocation = loc; + lastBytecodeOffset = offset; +} + void WasmBinaryWriter::writeInlineString(const char* name) { int32_t size = strlen(name); o << U32LEB(size); @@ -937,6 +998,7 @@ static Name RETURN_BREAK("binaryen|break-to-return"); void WasmBinaryBuilder::read() { readHeader(); + readSourceMapHeader(); // read sections until the end while (more()) { @@ -1342,6 +1404,7 @@ void WasmBinaryBuilder::readFunctions() { // process the function body if (debug) std::cerr << "processing function: " << i << std::endl; nextLabel = 0; + useDebugLocation = false; breaksToReturn = false; // process body assert(breakStack.empty()); @@ -1389,6 +1452,136 @@ void WasmBinaryBuilder::readExports() { } } +static int32_t readBase64VLQ(std::istream& in) { + uint32_t value = 0; + uint32_t shift = 0; + while (1) { + char ch = in.get(); + if (ch == EOF) + throw MapParseException("unexpected EOF in the middle of VLQ"); + if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch < 'g')) { + // last number digit + uint32_t digit = ch < 'a' ? ch - 'A' : ch - 'a' + 26; + value |= digit << shift; + break; + } + if (!(ch >= 'g' && ch <= 'z') && !(ch >= '0' && ch <= '9') && + ch != '+' && ch != '/') { + throw MapParseException("invalid VLQ digit"); + } + uint32_t digit = ch > '9' ? ch - 'g' : (ch >= '0' ? ch - '0' + 20 : (ch == '+' ? 30 : 31)); + value |= digit << shift; + shift += 5; + } + return value & 1 ? -int32_t(value >> 1) : int32_t(value >> 1); +} + +void WasmBinaryBuilder::readSourceMapHeader() { + if (!sourceMap) return; + + auto maybeReadChar = [&](char expected) { + if (sourceMap->peek() != expected) return false; + sourceMap->get(); + return true; + }; + auto mustReadChar = [&](char expected) { + if (sourceMap->get() != expected) { + throw MapParseException("Unexpected char"); + } + }; + auto findField = [&](const char* name, size_t len) { + bool matching = false; + size_t pos; + while (1) { + int ch = sourceMap->get(); + if (ch == EOF) return false; + if (ch == '\"') { + matching = true; + pos = 0; + } else if (matching && name[pos] == ch) { + ++pos; + if (pos == len) { + if (maybeReadChar('\"')) break; // found field + } + } else { + matching = false; + } + } + mustReadChar(':'); + return true; + }; + auto readString = [&](std::string& str) { + std::vector vec; + mustReadChar('\"'); + if (!maybeReadChar('\"')) { + while (1) { + int ch = sourceMap->get(); + if (ch == EOF) { + throw MapParseException("unexpected EOF in the middle of string"); + } + if (ch == '\"') break; + vec.push_back(ch); + } + } + str = std::string(vec.begin(), vec.end()); + }; + + if (!findField("sources", strlen("sources"))) { + throw MapParseException("cannot find the sources field in map"); + } + mustReadChar('['); + if (!maybeReadChar(']')) { + do { + std::string file; + readString(file); + Index index = wasm.debugInfoFileNames.size(); + wasm.debugInfoFileNames.push_back(file); + debugInfoFileIndices[file] = index; + } while (maybeReadChar(',')); + mustReadChar(']'); + } + + if (!findField("mappings", strlen("mappings"))) { + throw MapParseException("cannot find the mappings field in map"); + } + mustReadChar('\"'); + if (maybeReadChar('\"')) { // empty mappings + nextDebugLocation.first = 0; + return; + } + // read first debug location + uint32_t position = readBase64VLQ(*sourceMap); + uint32_t fileIndex = readBase64VLQ(*sourceMap); + uint32_t lineNumber = readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number + uint32_t columnNumber = readBase64VLQ(*sourceMap); + nextDebugLocation = { position, { fileIndex, lineNumber, columnNumber } }; +} + +void WasmBinaryBuilder::readNextDebugLocation() { + if (!sourceMap) return; + + char ch; + *sourceMap >> ch; + if (ch == '\"') { // end of records + nextDebugLocation.first = 0; + return; + } + if (ch != ',') { + throw MapParseException("Unexpected delimiter"); + } + + int32_t positionDelta = readBase64VLQ(*sourceMap); + uint32_t position = nextDebugLocation.first + positionDelta; + int32_t fileIndexDelta = readBase64VLQ(*sourceMap); + uint32_t fileIndex = nextDebugLocation.second.fileIndex + fileIndexDelta; + int32_t lineNumberDelta = readBase64VLQ(*sourceMap); + uint32_t lineNumber = nextDebugLocation.second.lineNumber + lineNumberDelta; + int32_t columnNumberDelta = readBase64VLQ(*sourceMap); + uint32_t columnNumber = nextDebugLocation.second.columnNumber + columnNumberDelta; + + nextDebugLocation = { position, { fileIndex, lineNumber, columnNumber } }; +} + Expression* WasmBinaryBuilder::readExpression() { assert(depth == 0); processExpressions(); @@ -1627,6 +1820,16 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) { throw ParseException("Reached function end without seeing End opcode"); } if (debug) std::cerr << "zz recurse into " << ++depth << " at " << pos << std::endl; + if (nextDebugLocation.first) { + while (nextDebugLocation.first && nextDebugLocation.first <= pos) { + if (nextDebugLocation.first < pos) { + std::cerr << "skipping debug location info for " << nextDebugLocation.first << std::endl; + } + debugLocation = nextDebugLocation.second; + useDebugLocation = currFunction; // using only for function expressions + readNextDebugLocation(); + } + } uint8_t code = getInt8(); if (debug) std::cerr << "readExpression seeing " << (int)code << std::endl; switch (code) { @@ -1661,6 +1864,9 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) { throw ParseException("bad node code " + std::to_string(code)); } } + if (useDebugLocation && curr) { + currFunction->debugLocations[curr] = debugLocation; + } if (debug) std::cerr << "zz recurse from " << depth-- << " at " << pos << std::endl; return BinaryConsts::ASTNodes(code); } diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp index c3192efbf91..ebc9af8deba 100644 --- a/src/wasm/wasm-io.cpp +++ b/src/wasm/wasm-io.cpp @@ -72,11 +72,21 @@ void ModuleWriter::writeBinary(Module& wasm, std::string filename) { if (debug) std::cerr << "writing binary to " << filename << "\n"; BufferWithRandomAccess buffer(debug); WasmBinaryWriter writer(&wasm, buffer, debug); - writer.setDebugInfo(debugInfo); + // if debug info is used, then we want to emit the names section + writer.setNamesSection(debugInfo); + std::unique_ptr sourceMapStream; + if (sourceMapFilename.size()) { + sourceMapStream = make_unique(); + sourceMapStream->open(sourceMapFilename); + writer.setSourceMap(sourceMapStream.get(), sourceMapUrl); + } if (symbolMap.size() > 0) writer.setSymbolMap(symbolMap); writer.write(); Output output(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release); buffer.writeTo(output); + if (sourceMapStream) { + sourceMapStream->close(); + } } void ModuleWriter::write(Module& wasm, std::string filename) { @@ -88,4 +98,3 @@ void ModuleWriter::write(Module& wasm, std::string filename) { } } - diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 9b053d5b8ce..1842237a579 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -71,9 +71,10 @@ Element* Element::setString(IString str__, bool dollared__, bool quoted__) { return this; } -Element* Element::setMetadata(size_t line_, size_t col_) { +Element* Element::setMetadata(size_t line_, size_t col_, SourceLocation* loc_) { line = line_; col = col_; + loc = loc_; return this; } @@ -93,7 +94,7 @@ void Element::dump() { } -SExpressionParser::SExpressionParser(char* input) : input(input) { +SExpressionParser::SExpressionParser(char* input) : input(input), loc(nullptr) { root = nullptr; line = 1; lineStart = input; @@ -104,6 +105,7 @@ SExpressionParser::SExpressionParser(char* input) : input(input) { Element* SExpressionParser::parse() { std::vector stack; + std::vector stackLocs; Element *curr = allocator.alloc(); while (1) { skipWhitespace(); @@ -111,7 +113,9 @@ Element* SExpressionParser::parse() { if (input[0] == '(') { input++; stack.push_back(curr); - curr = allocator.alloc()->setMetadata(line, input - lineStart - 1); + curr = allocator.alloc()->setMetadata(line, input - lineStart - 1, loc); + stackLocs.push_back(loc); + assert(stack.size() == stackLocs.size()); } else if (input[0] == ')') { input++; auto last = curr; @@ -119,7 +123,10 @@ Element* SExpressionParser::parse() { 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()); @@ -129,6 +136,29 @@ Element* SExpressionParser::parse() { 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)); + 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])) { @@ -139,6 +169,9 @@ void SExpressionParser::skipWhitespace() { input++; } if (input[0] == ';' && input[1] == ';') { + if (input[2] == '@') { + parseDebugLocation(); + } while (input[0] && input[0] != '\n') input++; line++; lineStart = ++input; @@ -198,13 +231,13 @@ Element* SExpressionParser::parseString() { input++; } input++; - return allocator.alloc()->setString(IString(str.c_str(), false), dollared, true)->setMetadata(line, start - lineStart); + 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); + auto ret = allocator.alloc()->setString(IString(start, false), dollared, false)->setMetadata(line, start - lineStart, loc); input[0] = temp; return ret; } @@ -583,6 +616,23 @@ WasmType SExpressionWasmBuilder::stringToWasmType(const char* str, bool allowErr } Expression* SExpressionWasmBuilder::parseExpression(Element& s) { + Expression* result = makeExpression(s); + if (s.loc) { + IString file = s.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]; + currFunction->debugLocations[result] = {fileIndex, s.loc->line, s.loc->column}; + } + return result; +} + +Expression* SExpressionWasmBuilder::makeExpression(Element& s) { IString id = s[0]->str(); const char *str = id.str; const char *dot = strchr(str, '.'); diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 834d9e28f54..d2bf4e75cad 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -28,6 +28,7 @@ Name WASM("wasm"), namespace BinaryConsts { namespace UserSections { const char* Name = "name"; +const char* SourceMapUrl = "sourceMappingURL"; } } diff --git a/test/debugInfo.fromasm b/test/debugInfo.fromasm index 84fb4aece99..7aa584c4371 100644 --- a/test/debugInfo.fromasm +++ b/test/debugInfo.fromasm @@ -11,21 +11,21 @@ (export "fib" (func $fib)) (export "switch_reach" (func $switch_reach)) (func $add (param $0 i32) (param $1 i32) (result i32) - ;; tests/other_file.cpp:314159 + ;;@ tests/other_file.cpp:314159:0 (i32.add (get_local $1) (get_local $1) ) ) (func $ret (param $0 i32) (result i32) - ;; return.cpp:50 + ;;@ return.cpp:50:0 (set_local $0 (i32.shl (get_local $0) (i32.const 1) ) ) - ;; return.cpp:100 + ;;@ return.cpp:100:0 (i32.add (get_local $0) (i32.const 1) @@ -42,21 +42,21 @@ ) ) (func $opts (param $0 i32) (param $1 i32) (result i32) - ;; even-opted.cpp:1 + ;;@ even-opted.cpp:1:0 (set_local $0 (i32.add (get_local $0) (get_local $1) ) ) - ;; even-opted.cpp:2 + ;;@ even-opted.cpp:2:0 (set_local $1 (i32.shr_s (get_local $1) (get_local $0) ) ) - ;; even-opted.cpp:3 + ;;@ even-opted.cpp:3:0 (i32.add (call $i32s-rem (get_local $0) @@ -71,7 +71,7 @@ (local $3 i32) (local $4 i32) (if - ;; fib.c:3 + ;;@ fib.c:3:0 (i32.gt_s (get_local $0) (i32.const 0) @@ -91,21 +91,21 @@ (set_local $1 (i32.const 1) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (return (get_local $1) ) ) ) (loop $while-in - ;; fib.c:4 + ;;@ fib.c:4:0 (set_local $1 (i32.add (get_local $3) (get_local $4) ) ) - ;; fib.c:3 + ;;@ fib.c:3:0 (set_local $2 (i32.add (get_local $2) @@ -113,7 +113,6 @@ ) ) (if - ;; fib.c:3 (i32.ne (get_local $2) (get_local $0) @@ -129,7 +128,7 @@ ) ) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (get_local $1) ) (func $switch_reach (param $0 i32) (result i32) @@ -189,7 +188,7 @@ (get_local $0) ) ) - ;; /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950 + ;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0 (get_local $1) ) ) diff --git a/test/debugInfo.fromasm.clamp b/test/debugInfo.fromasm.clamp index 84fb4aece99..7aa584c4371 100644 --- a/test/debugInfo.fromasm.clamp +++ b/test/debugInfo.fromasm.clamp @@ -11,21 +11,21 @@ (export "fib" (func $fib)) (export "switch_reach" (func $switch_reach)) (func $add (param $0 i32) (param $1 i32) (result i32) - ;; tests/other_file.cpp:314159 + ;;@ tests/other_file.cpp:314159:0 (i32.add (get_local $1) (get_local $1) ) ) (func $ret (param $0 i32) (result i32) - ;; return.cpp:50 + ;;@ return.cpp:50:0 (set_local $0 (i32.shl (get_local $0) (i32.const 1) ) ) - ;; return.cpp:100 + ;;@ return.cpp:100:0 (i32.add (get_local $0) (i32.const 1) @@ -42,21 +42,21 @@ ) ) (func $opts (param $0 i32) (param $1 i32) (result i32) - ;; even-opted.cpp:1 + ;;@ even-opted.cpp:1:0 (set_local $0 (i32.add (get_local $0) (get_local $1) ) ) - ;; even-opted.cpp:2 + ;;@ even-opted.cpp:2:0 (set_local $1 (i32.shr_s (get_local $1) (get_local $0) ) ) - ;; even-opted.cpp:3 + ;;@ even-opted.cpp:3:0 (i32.add (call $i32s-rem (get_local $0) @@ -71,7 +71,7 @@ (local $3 i32) (local $4 i32) (if - ;; fib.c:3 + ;;@ fib.c:3:0 (i32.gt_s (get_local $0) (i32.const 0) @@ -91,21 +91,21 @@ (set_local $1 (i32.const 1) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (return (get_local $1) ) ) ) (loop $while-in - ;; fib.c:4 + ;;@ fib.c:4:0 (set_local $1 (i32.add (get_local $3) (get_local $4) ) ) - ;; fib.c:3 + ;;@ fib.c:3:0 (set_local $2 (i32.add (get_local $2) @@ -113,7 +113,6 @@ ) ) (if - ;; fib.c:3 (i32.ne (get_local $2) (get_local $0) @@ -129,7 +128,7 @@ ) ) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (get_local $1) ) (func $switch_reach (param $0 i32) (result i32) @@ -189,7 +188,7 @@ (get_local $0) ) ) - ;; /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950 + ;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0 (get_local $1) ) ) diff --git a/test/debugInfo.fromasm.clamp.map b/test/debugInfo.fromasm.clamp.map new file mode 100644 index 00000000000..5680294cda5 --- /dev/null +++ b/test/debugInfo.fromasm.clamp.map @@ -0,0 +1 @@ +{"version":3,"sources":["tests/hello_world.c","tests/other_file.cpp","return.cpp","even-opted.cpp","fib.c","/tmp/emscripten_test_binaryen2_28hnAe/src.c"],"names":[],"mappings":"yLC8ylTA,cC7vlTA,OAkDA,mCCnGA,OACA,OACA,qBCAA,wBAKA,MAJA,OADA,0BAKA,iGCsi1DA"} \ No newline at end of file diff --git a/test/debugInfo.fromasm.clamp.no-opts b/test/debugInfo.fromasm.clamp.no-opts index 9e3050695ed..054226a3a3c 100644 --- a/test/debugInfo.fromasm.clamp.no-opts +++ b/test/debugInfo.fromasm.clamp.no-opts @@ -11,15 +11,15 @@ (export "fib" (func $fib)) (export "switch_reach" (func $switch_reach)) (func $add (param $x i32) (param $y i32) (result i32) - ;; tests/hello_world.c:5 + ;;@ tests/hello_world.c:5:0 (set_local $x (get_local $x) ) - ;; tests/hello_world.c:6 + ;;@ tests/hello_world.c:6:0 (set_local $y (get_local $y) ) - ;; tests/other_file.cpp:314159 + ;;@ tests/other_file.cpp:314159:0 (set_local $x (get_local $y) ) @@ -31,14 +31,14 @@ ) ) (func $ret (param $x i32) (result i32) - ;; return.cpp:50 + ;;@ return.cpp:50:0 (set_local $x (i32.shl (get_local $x) (i32.const 1) ) ) - ;; return.cpp:100 + ;;@ return.cpp:100:0 (return (i32.add (get_local $x) @@ -59,21 +59,21 @@ ) ) (func $opts (param $x i32) (param $y i32) (result i32) - ;; even-opted.cpp:1 + ;;@ even-opted.cpp:1:0 (set_local $x (i32.add (get_local $x) (get_local $y) ) ) - ;; even-opted.cpp:2 + ;;@ even-opted.cpp:2:0 (set_local $y (i32.shr_s (get_local $y) (get_local $x) ) ) - ;; even-opted.cpp:3 + ;;@ even-opted.cpp:3:0 (set_local $x (call $i32s-rem (get_local $x) @@ -102,7 +102,7 @@ (set_local $sp (get_global $STACKTOP) ) - ;; fib.c:3 + ;;@ fib.c:3:0 (set_local $$1 (i32.gt_s (get_local $$0) @@ -126,7 +126,7 @@ (set_local $$$0$lcssa (i32.const 1) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (return (get_local $$$0$lcssa) ) @@ -134,21 +134,20 @@ ) (loop $while-in (block $while-out - ;; fib.c:4 + ;;@ fib.c:4:0 (set_local $$2 (i32.add (get_local $$$019) (get_local $$$01518) ) ) - ;; fib.c:3 + ;;@ fib.c:3:0 (set_local $$3 (i32.add (get_local $$$01617) (i32.const 1) ) ) - ;; fib.c:3 (set_local $$exitcond (i32.eq (get_local $$3) @@ -181,7 +180,7 @@ (br $while-in) ) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (return (get_local $$$0$lcssa) ) @@ -278,7 +277,7 @@ (get_local $$p) ) ) - ;; /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950 + ;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0 (return (get_local $$rc$0) ) diff --git a/test/debugInfo.fromasm.clamp.no-opts.map b/test/debugInfo.fromasm.clamp.no-opts.map new file mode 100644 index 00000000000..82a92c68b2e --- /dev/null +++ b/test/debugInfo.fromasm.clamp.no-opts.map @@ -0,0 +1 @@ +{"version":3,"sources":["tests/hello_world.c","tests/other_file.cpp","return.cpp","even-opted.cpp","fib.c","/tmp/emscripten_test_binaryen2_28hnAe/src.c"],"names":[],"mappings":"uMAIA,IACA,ICyylTA,sBC7vlTA,OAkDA,uCCnGA,OACA,OACA,gCCAA,4BAKA,QAJA,OADA,8CAKA,kJCsi1DA"} \ No newline at end of file diff --git a/test/debugInfo.fromasm.imprecise b/test/debugInfo.fromasm.imprecise index 4919d624dab..638911cfc1c 100644 --- a/test/debugInfo.fromasm.imprecise +++ b/test/debugInfo.fromasm.imprecise @@ -10,42 +10,42 @@ (export "fib" (func $fib)) (export "switch_reach" (func $switch_reach)) (func $add (param $0 i32) (param $1 i32) (result i32) - ;; tests/other_file.cpp:314159 + ;;@ tests/other_file.cpp:314159:0 (i32.add (get_local $1) (get_local $1) ) ) (func $ret (param $0 i32) (result i32) - ;; return.cpp:50 + ;;@ return.cpp:50:0 (set_local $0 (i32.shl (get_local $0) (i32.const 1) ) ) - ;; return.cpp:100 + ;;@ return.cpp:100:0 (i32.add (get_local $0) (i32.const 1) ) ) (func $opts (param $0 i32) (param $1 i32) (result i32) - ;; even-opted.cpp:1 + ;;@ even-opted.cpp:1:0 (set_local $0 (i32.add (get_local $0) (get_local $1) ) ) - ;; even-opted.cpp:2 + ;;@ even-opted.cpp:2:0 (set_local $1 (i32.shr_s (get_local $1) (get_local $0) ) ) - ;; even-opted.cpp:3 + ;;@ even-opted.cpp:3:0 (i32.add (i32.rem_s (get_local $0) @@ -60,7 +60,7 @@ (local $3 i32) (local $4 i32) (if - ;; fib.c:3 + ;;@ fib.c:3:0 (i32.gt_s (get_local $0) (i32.const 0) @@ -80,21 +80,21 @@ (set_local $1 (i32.const 1) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (return (get_local $1) ) ) ) (loop $while-in - ;; fib.c:4 + ;;@ fib.c:4:0 (set_local $1 (i32.add (get_local $3) (get_local $4) ) ) - ;; fib.c:3 + ;;@ fib.c:3:0 (set_local $2 (i32.add (get_local $2) @@ -102,7 +102,6 @@ ) ) (if - ;; fib.c:3 (i32.ne (get_local $2) (get_local $0) @@ -118,7 +117,7 @@ ) ) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (get_local $1) ) (func $switch_reach (param $0 i32) (result i32) @@ -178,7 +177,7 @@ (get_local $0) ) ) - ;; /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950 + ;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0 (get_local $1) ) ) diff --git a/test/debugInfo.fromasm.imprecise.map b/test/debugInfo.fromasm.imprecise.map new file mode 100644 index 00000000000..60f44708269 --- /dev/null +++ b/test/debugInfo.fromasm.imprecise.map @@ -0,0 +1 @@ +{"version":3,"sources":["tests/hello_world.c","tests/other_file.cpp","return.cpp","even-opted.cpp","fib.c","/tmp/emscripten_test_binaryen2_28hnAe/src.c"],"names":[],"mappings":"wLC8ylTA,cC7vlTA,OAkDA,eCnGA,OACA,OACA,oBCAA,wBAKA,MAJA,OADA,0BAKA,iGCsi1DA"} \ No newline at end of file diff --git a/test/debugInfo.fromasm.imprecise.no-opts b/test/debugInfo.fromasm.imprecise.no-opts index 4bea2c35520..1ec751ef4d0 100644 --- a/test/debugInfo.fromasm.imprecise.no-opts +++ b/test/debugInfo.fromasm.imprecise.no-opts @@ -11,15 +11,15 @@ (export "fib" (func $fib)) (export "switch_reach" (func $switch_reach)) (func $add (param $x i32) (param $y i32) (result i32) - ;; tests/hello_world.c:5 + ;;@ tests/hello_world.c:5:0 (set_local $x (get_local $x) ) - ;; tests/hello_world.c:6 + ;;@ tests/hello_world.c:6:0 (set_local $y (get_local $y) ) - ;; tests/other_file.cpp:314159 + ;;@ tests/other_file.cpp:314159:0 (set_local $x (get_local $y) ) @@ -31,14 +31,14 @@ ) ) (func $ret (param $x i32) (result i32) - ;; return.cpp:50 + ;;@ return.cpp:50:0 (set_local $x (i32.shl (get_local $x) (i32.const 1) ) ) - ;; return.cpp:100 + ;;@ return.cpp:100:0 (return (i32.add (get_local $x) @@ -47,21 +47,21 @@ ) ) (func $opts (param $x i32) (param $y i32) (result i32) - ;; even-opted.cpp:1 + ;;@ even-opted.cpp:1:0 (set_local $x (i32.add (get_local $x) (get_local $y) ) ) - ;; even-opted.cpp:2 + ;;@ even-opted.cpp:2:0 (set_local $y (i32.shr_s (get_local $y) (get_local $x) ) ) - ;; even-opted.cpp:3 + ;;@ even-opted.cpp:3:0 (set_local $x (i32.rem_s (get_local $x) @@ -90,7 +90,7 @@ (set_local $sp (get_global $STACKTOP) ) - ;; fib.c:3 + ;;@ fib.c:3:0 (set_local $$1 (i32.gt_s (get_local $$0) @@ -114,7 +114,7 @@ (set_local $$$0$lcssa (i32.const 1) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (return (get_local $$$0$lcssa) ) @@ -122,21 +122,20 @@ ) (loop $while-in (block $while-out - ;; fib.c:4 + ;;@ fib.c:4:0 (set_local $$2 (i32.add (get_local $$$019) (get_local $$$01518) ) ) - ;; fib.c:3 + ;;@ fib.c:3:0 (set_local $$3 (i32.add (get_local $$$01617) (i32.const 1) ) ) - ;; fib.c:3 (set_local $$exitcond (i32.eq (get_local $$3) @@ -169,7 +168,7 @@ (br $while-in) ) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (return (get_local $$$0$lcssa) ) @@ -266,7 +265,7 @@ (get_local $$p) ) ) - ;; /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950 + ;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0 (return (get_local $$rc$0) ) diff --git a/test/debugInfo.fromasm.imprecise.no-opts.map b/test/debugInfo.fromasm.imprecise.no-opts.map new file mode 100644 index 00000000000..2fcda4e2331 --- /dev/null +++ b/test/debugInfo.fromasm.imprecise.no-opts.map @@ -0,0 +1 @@ +{"version":3,"sources":["tests/hello_world.c","tests/other_file.cpp","return.cpp","even-opted.cpp","fib.c","/tmp/emscripten_test_binaryen2_28hnAe/src.c"],"names":[],"mappings":"sMAIA,IACA,ICyylTA,sBC7vlTA,OAkDA,kBCnGA,OACA,OACA,+BCAA,4BAKA,QAJA,OADA,8CAKA,kJCsi1DA"} \ No newline at end of file diff --git a/test/debugInfo.fromasm.map b/test/debugInfo.fromasm.map new file mode 100644 index 00000000000..5680294cda5 --- /dev/null +++ b/test/debugInfo.fromasm.map @@ -0,0 +1 @@ +{"version":3,"sources":["tests/hello_world.c","tests/other_file.cpp","return.cpp","even-opted.cpp","fib.c","/tmp/emscripten_test_binaryen2_28hnAe/src.c"],"names":[],"mappings":"yLC8ylTA,cC7vlTA,OAkDA,mCCnGA,OACA,OACA,qBCAA,wBAKA,MAJA,OADA,0BAKA,iGCsi1DA"} \ No newline at end of file diff --git a/test/debugInfo.fromasm.no-opts b/test/debugInfo.fromasm.no-opts index 9e3050695ed..054226a3a3c 100644 --- a/test/debugInfo.fromasm.no-opts +++ b/test/debugInfo.fromasm.no-opts @@ -11,15 +11,15 @@ (export "fib" (func $fib)) (export "switch_reach" (func $switch_reach)) (func $add (param $x i32) (param $y i32) (result i32) - ;; tests/hello_world.c:5 + ;;@ tests/hello_world.c:5:0 (set_local $x (get_local $x) ) - ;; tests/hello_world.c:6 + ;;@ tests/hello_world.c:6:0 (set_local $y (get_local $y) ) - ;; tests/other_file.cpp:314159 + ;;@ tests/other_file.cpp:314159:0 (set_local $x (get_local $y) ) @@ -31,14 +31,14 @@ ) ) (func $ret (param $x i32) (result i32) - ;; return.cpp:50 + ;;@ return.cpp:50:0 (set_local $x (i32.shl (get_local $x) (i32.const 1) ) ) - ;; return.cpp:100 + ;;@ return.cpp:100:0 (return (i32.add (get_local $x) @@ -59,21 +59,21 @@ ) ) (func $opts (param $x i32) (param $y i32) (result i32) - ;; even-opted.cpp:1 + ;;@ even-opted.cpp:1:0 (set_local $x (i32.add (get_local $x) (get_local $y) ) ) - ;; even-opted.cpp:2 + ;;@ even-opted.cpp:2:0 (set_local $y (i32.shr_s (get_local $y) (get_local $x) ) ) - ;; even-opted.cpp:3 + ;;@ even-opted.cpp:3:0 (set_local $x (call $i32s-rem (get_local $x) @@ -102,7 +102,7 @@ (set_local $sp (get_global $STACKTOP) ) - ;; fib.c:3 + ;;@ fib.c:3:0 (set_local $$1 (i32.gt_s (get_local $$0) @@ -126,7 +126,7 @@ (set_local $$$0$lcssa (i32.const 1) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (return (get_local $$$0$lcssa) ) @@ -134,21 +134,20 @@ ) (loop $while-in (block $while-out - ;; fib.c:4 + ;;@ fib.c:4:0 (set_local $$2 (i32.add (get_local $$$019) (get_local $$$01518) ) ) - ;; fib.c:3 + ;;@ fib.c:3:0 (set_local $$3 (i32.add (get_local $$$01617) (i32.const 1) ) ) - ;; fib.c:3 (set_local $$exitcond (i32.eq (get_local $$3) @@ -181,7 +180,7 @@ (br $while-in) ) ) - ;; fib.c:8 + ;;@ fib.c:8:0 (return (get_local $$$0$lcssa) ) @@ -278,7 +277,7 @@ (get_local $$p) ) ) - ;; /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950 + ;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0 (return (get_local $$rc$0) ) diff --git a/test/debugInfo.fromasm.no-opts.map b/test/debugInfo.fromasm.no-opts.map new file mode 100644 index 00000000000..82a92c68b2e --- /dev/null +++ b/test/debugInfo.fromasm.no-opts.map @@ -0,0 +1 @@ +{"version":3,"sources":["tests/hello_world.c","tests/other_file.cpp","return.cpp","even-opted.cpp","fib.c","/tmp/emscripten_test_binaryen2_28hnAe/src.c"],"names":[],"mappings":"uMAIA,IACA,ICyylTA,sBC7vlTA,OAkDA,uCCnGA,OACA,OACA,gCCAA,4BAKA,QAJA,OADA,8CAKA,kJCsi1DA"} \ No newline at end of file diff --git a/test/dot_s/debug.wast b/test/dot_s/debug.wast index aa4f34d9e71..0cc0ae20c9e 100644 --- a/test/dot_s/debug.wast +++ b/test/dot_s/debug.wast @@ -18,18 +18,21 @@ ) (block $label$0 (loop $label$1 + ;;@ fib.c:3:17 (set_local $2 (i32.add (get_local $2) (i32.const 1) ) ) + ;;@ fib.c:3:3 (br_if $label$0 (i32.ge_s (get_local $2) (get_local $0) ) ) + ;;@ fib.c:4:11 (set_local $1 (i32.add (get_local $4) @@ -45,6 +48,7 @@ (br $label$1) ) ) + ;;@ fib.c:6:3 (return (get_local $4) ) diff --git a/test/fib-dbg.wasm b/test/fib-dbg.wasm new file mode 100644 index 00000000000..8e56773f2c2 Binary files /dev/null and b/test/fib-dbg.wasm differ diff --git a/test/fib-dbg.wasm.fromBinary b/test/fib-dbg.wasm.fromBinary new file mode 100644 index 00000000000..3f940fa8ad6 --- /dev/null +++ b/test/fib-dbg.wasm.fromBinary @@ -0,0 +1,228 @@ +(module + (type $0 (func (param i32 i32))) + (type $1 (func (param i32) (result i32))) + (type $2 (func (result i32))) + (type $3 (func (param i32))) + (type $4 (func)) + (import "env" "DYNAMICTOP_PTR" (global $import$0 i32)) + (import "env" "tempDoublePtr" (global $import$1 i32)) + (import "env" "ABORT" (global $import$2 i32)) + (import "env" "STACKTOP" (global $import$3 i32)) + (import "env" "STACK_MAX" (global $import$4 i32)) + (import "env" "gb" (global $import$5 i32)) + (import "env" "fb" (global $import$6 i32)) + (import "global" "NaN" (global $import$7 f64)) + (import "global" "Infinity" (global $import$8 f64)) + (import "env" "memory" (memory $0 256 256)) + (import "env" "table" (table 0 0 anyfunc)) + (import "env" "memoryBase" (global $import$11 i32)) + (import "env" "tableBase" (global $import$12 i32)) + (global $global$0 (mut i32) (get_global $import$0)) + (global $global$1 (mut i32) (get_global $import$1)) + (global $global$2 (mut i32) (get_global $import$2)) + (global $global$3 (mut i32) (get_global $import$3)) + (global $global$4 (mut i32) (get_global $import$4)) + (global $global$5 (mut i32) (get_global $import$5)) + (global $global$6 (mut i32) (get_global $import$6)) + (global $global$7 (mut i32) (i32.const 0)) + (global $global$8 (mut i32) (i32.const 0)) + (global $global$9 (mut i32) (i32.const 0)) + (global $global$10 (mut i32) (i32.const 0)) + (global $global$11 (mut f64) (get_global $import$7)) + (global $global$12 (mut f64) (get_global $import$8)) + (global $global$13 (mut i32) (i32.const 0)) + (global $global$14 (mut i32) (i32.const 0)) + (global $global$15 (mut i32) (i32.const 0)) + (global $global$16 (mut i32) (i32.const 0)) + (global $global$17 (mut f64) (f64.const 0)) + (global $global$18 (mut i32) (i32.const 0)) + (global $global$19 (mut i32) (i32.const 0)) + (global $global$20 (mut i32) (i32.const 0)) + (global $global$21 (mut f64) (f64.const 0)) + (global $global$22 (mut i32) (i32.const 0)) + (global $global$23 (mut f64) (f64.const 0)) + (export "setThrew" (func $setThrew)) + (export "runPostSets" (func $runPostSets)) + (export "establishStackSpace" (func $establishStackSpace)) + (export "stackSave" (func $stackSave)) + (export "stackRestore" (func $stackRestore)) + (export "_fib" (func $_fib)) + (export "stackAlloc" (func $stackAlloc)) + (func $stackAlloc (type $1) (param $var$0 i32) (result i32) + (local $var$1 i32) + (block $label$0 + (set_local $var$1 + (get_global $global$3) + ) + (set_global $global$3 + (i32.add + (get_global $global$3) + (get_local $var$0) + ) + ) + (set_global $global$3 + (i32.and + (i32.add + (get_global $global$3) + (i32.const 15) + ) + (i32.const -16) + ) + ) + (return + (get_local $var$1) + ) + (unreachable) + ) + (unreachable) + ) + (func $stackSave (type $2) (result i32) + (return + (get_global $global$3) + ) + ) + (func $stackRestore (type $3) (param $var$0 i32) + (set_global $global$3 + (get_local $var$0) + ) + ) + (func $establishStackSpace (type $0) (param $var$0 i32) (param $var$1 i32) + (block $label$0 + (set_global $global$3 + (get_local $var$0) + ) + (set_global $global$4 + (get_local $var$1) + ) + ) + ) + (func $setThrew (type $0) (param $var$0 i32) (param $var$1 i32) + (if + (i32.eq + (get_global $global$7) + (i32.const 0) + ) + (block $label$0 + (set_global $global$7 + (get_local $var$0) + ) + (set_global $global$8 + (get_local $var$1) + ) + ) + ) + ) + (func $_fib (type $1) (param $var$0 i32) (result i32) + (local $var$1 i32) + (local $var$2 i32) + (local $var$3 i32) + (local $var$4 i32) + (local $var$5 i32) + (local $var$6 i32) + (local $var$7 i32) + (local $var$8 i32) + (local $var$9 i32) + (local $var$10 i32) + (local $var$11 i32) + ;;@ fib.c:8:0 + (block $label$0 + (set_local $var$11 + (get_global $global$3) + ) + ;;@ fib.c:3:0 + (set_local $var$6 + (i32.gt_s + (get_local $var$0) + (i32.const 0) + ) + ) + ;;@ fib.c:8:0 + (if + ;;@ fib.c:3:0 + (get_local $var$6) + (block $label$1 + (set_local $var$1 + (i32.const 0) + ) + (set_local $var$5 + (i32.const 1) + ) + (set_local $var$8 + (i32.const 0) + ) + ) + (block $label$2 + (set_local $var$4 + (i32.const 1) + ) + ;;@ fib.c:8:0 + (return + (get_local $var$4) + ) + ) + ) + ;;@ fib.c:3:0 + (loop $label$3 + (block $label$4 + ;;@ fib.c:4:0 + (set_local $var$3 + (i32.add + (get_local $var$5) + (get_local $var$1) + ) + ) + ;;@ fib.c:3:0 + (set_local $var$9 + (i32.add + (get_local $var$8) + (i32.const 1) + ) + ) + (set_local $var$7 + (i32.eq + (get_local $var$9) + (get_local $var$0) + ) + ) + (if + (get_local $var$7) + (block $label$5 + (set_local $var$4 + (get_local $var$3) + ) + (br $label$4) + ) + (block $label$6 + (set_local $var$2 + (get_local $var$5) + ) + (set_local $var$5 + (get_local $var$3) + ) + (set_local $var$8 + (get_local $var$9) + ) + (set_local $var$1 + (get_local $var$2) + ) + ) + ) + (br $label$3) + ) + ) + ;;@ fib.c:8:0 + (return + (get_local $var$4) + ) + (unreachable) + (unreachable) + ) + (unreachable) + ) + (func $runPostSets (type $4) + (local $var$0 i32) + (nop) + ) + ;; custom section "sourceMappingURL", size 35 +) + diff --git a/test/fib-dbg.wasm.map b/test/fib-dbg.wasm.map new file mode 100644 index 00000000000..831f55fbd27 --- /dev/null +++ b/test/fib-dbg.wasm.map @@ -0,0 +1 @@ +{"version":3,"sources":["fib.c"],"names":[],"mappings":"moBAEA,4BAKA,QAJA,OADA,OAAA,uCAKA"}