From ec66e273e350c3d48df0ccaaf73c53b14485848f Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Thu, 13 Apr 2017 15:12:48 -0700 Subject: Replace text annotations with explicit file/line for debug info (#967) Rather than storing debug info as text annotations, store explicit file and line information. This will make it easier to experiment with outputting other serializations or representations (e.g. source maps), and will allow outputting debug info for binaries as well. --- src/asm2wasm.h | 10 ++++++---- src/passes/Print.cpp | 9 +++++---- src/wasm.h | 7 +++++-- 3 files changed, 16 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/asm2wasm.h b/src/asm2wasm.h index 5adf82762..ada026176 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -1171,6 +1171,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) { if (runOptimizationPasses) { optimizingBuilder->finish(); } + wasm.debugInfoFileNames = std::move(preprocessor.debugInfoFileNames); // second pass. first, function imports @@ -1306,11 +1307,12 @@ void Asm2WasmBuilder::processAsm(Ref ast) { // this is a debuginfo node. turn it into an annotation on the last stack auto* last = lastExpression; lastExpression = nullptr; - auto& annotations = getFunction()->annotations; + auto& debugLocations = getFunction()->debugLocations; if (last) { - auto fileIndex = call->operands[0]->cast()->value.geti32(); - auto lineNumber = call->operands[1]->cast()->value.geti32(); - annotations[last] = parent->preprocessor.debugInfoFileNames[fileIndex] + ":" + std::to_string(lineNumber); + uint32_t fileIndex = call->operands[0]->cast()->value.geti32(); + assert(getModule()->debugInfoFileNames.size() > fileIndex); + uint32_t lineNumber = call->operands[1]->cast()->value.geti32(); + debugLocations[last] = {fileIndex, lineNumber}; } // eliminate the debug info call ExpressionManipulator::nop(curr); diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 04bf74529..9a2edcb6b 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -49,10 +49,11 @@ struct PrintSExpression : public Visitor { void visit(Expression* curr) { if (currFunction) { // show an annotation, if there is one - auto& annotations = currFunction->annotations; - auto iter = annotations.find(curr); - if (iter != annotations.end()) { - o << ";; " << iter->second << '\n'; + auto& debugLocations = currFunction->debugLocations; + 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); } } diff --git a/src/wasm.h b/src/wasm.h index 9cfcb4ca8..b27fe6acb 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -510,8 +510,10 @@ public: std::vector localNames; std::map localIndices; - // node annotations, printed alongside the node in the text format - std::unordered_map annotations; + struct DebugLocation { + uint32_t fileIndex, lineNumber; + }; + std::unordered_map debugLocations; Function() : result(none) {} @@ -647,6 +649,7 @@ public: Name start; std::vector userSections; + std::vector debugInfoFileNames; MixedArena allocator; -- cgit v1.2.3