Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,12 @@ class WasmBinaryBuilder {
MixedArena& allocator;
const std::vector<char>& input;
std::istream* sourceMap;
std::pair<uint32_t, Function::DebugLocation> nextDebugLocation;
struct NextDebugLocation {
uint32_t availablePos;
uint32_t previousPos;
Function::DebugLocation next;
};
NextDebugLocation nextDebugLocation;
bool debugInfo = true;
bool DWARF = false;
bool skipFunctionBodies = false;
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ void BinaryenIRWriter<SubType>::visitPossibleBlockContents(Expression* curr) {

template<typename SubType>
void BinaryenIRWriter<SubType>::visit(Expression* curr) {
emitDebugLocation(curr);
// We emit unreachable instructions that create unreachability, but not
// unreachable instructions that just inherit unreachability from their
// children, since the latter won't be reached. This (together with logic in
Expand All @@ -257,6 +256,7 @@ void BinaryenIRWriter<SubType>::visit(Expression* curr) {
if (Properties::isControlFlowStructure(curr)) {
Visitor<BinaryenIRWriter>::visit(curr);
} else {
emitDebugLocation(curr);
emit(curr);
}
}
Expand Down
36 changes: 24 additions & 12 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1572,8 +1572,8 @@ void WasmBinaryWriter::writeField(const Field& field) {
WasmBinaryBuilder::WasmBinaryBuilder(Module& wasm,
FeatureSet features,
const std::vector<char>& input)
: wasm(wasm), allocator(wasm.allocator), input(input), sourceMap(nullptr),
nextDebugLocation(0, {0, 0, 0}), debugLocation() {
: wasm(wasm), allocator(wasm.allocator), input(input),
sourceMap(nullptr), nextDebugLocation{0, 0, {0, 0, 0}}, debugLocation() {
wasm.features = features;
}

Expand Down Expand Up @@ -2741,7 +2741,7 @@ void WasmBinaryBuilder::readSourceMapHeader() {

mustReadChar('\"');
if (maybeReadChar('\"')) { // empty mappings
nextDebugLocation.first = 0;
nextDebugLocation.availablePos = 0;
return;
}
// read first debug location
Expand All @@ -2750,42 +2750,54 @@ void WasmBinaryBuilder::readSourceMapHeader() {
uint32_t lineNumber =
readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number
uint32_t columnNumber = readBase64VLQ(*sourceMap);
nextDebugLocation = {position, {fileIndex, lineNumber, columnNumber}};
nextDebugLocation = {
position, position, {fileIndex, lineNumber, columnNumber}};
}

void WasmBinaryBuilder::readNextDebugLocation() {
if (!sourceMap) {
return;
}

while (nextDebugLocation.first && nextDebugLocation.first <= pos) {
if (nextDebugLocation.availablePos == 0 &&
nextDebugLocation.previousPos <= pos) {
// if source map file had already reached the end and cache position also
// cannot cover the pos clear the debug location
debugLocation.clear();
return;
}

while (nextDebugLocation.availablePos &&
nextDebugLocation.availablePos <= pos) {
debugLocation.clear();
// use debugLocation only for function expressions
if (currFunction) {
debugLocation.insert(nextDebugLocation.second);
debugLocation.insert(nextDebugLocation.next);
}

char ch;
*sourceMap >> ch;
if (ch == '\"') { // end of records
nextDebugLocation.first = 0;
nextDebugLocation.availablePos = 0;
break;
}
if (ch != ',') {
throw MapParseException("Unexpected delimiter");
}

int32_t positionDelta = readBase64VLQ(*sourceMap);
uint32_t position = nextDebugLocation.first + positionDelta;
uint32_t position = nextDebugLocation.availablePos + positionDelta;
int32_t fileIndexDelta = readBase64VLQ(*sourceMap);
uint32_t fileIndex = nextDebugLocation.second.fileIndex + fileIndexDelta;
uint32_t fileIndex = nextDebugLocation.next.fileIndex + fileIndexDelta;
int32_t lineNumberDelta = readBase64VLQ(*sourceMap);
uint32_t lineNumber = nextDebugLocation.second.lineNumber + lineNumberDelta;
uint32_t lineNumber = nextDebugLocation.next.lineNumber + lineNumberDelta;
int32_t columnNumberDelta = readBase64VLQ(*sourceMap);
uint32_t columnNumber =
nextDebugLocation.second.columnNumber + columnNumberDelta;
nextDebugLocation.next.columnNumber + columnNumberDelta;

nextDebugLocation = {position, {fileIndex, lineNumber, columnNumber}};
nextDebugLocation = {position,
nextDebugLocation.availablePos,
{fileIndex, lineNumber, columnNumber}};
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/fib-dbg.wasm.fromBinary
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@
(br $label$4)
)
)
;;@ fib.c:8:0
(return
;;@ fib.c:8:0
(local.get $4)
)
)
Expand Down
46 changes: 46 additions & 0 deletions test/lit/source-map.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
;; RUN: wasm-opt %s -o %t.wasm -osm %t.map -g -q
;; RUN: wasm-opt %t.wasm -ism %t.map -q -o - -S | filecheck %s

(module
(func $foo (param $x i32) (param $y i32)
;;@ src.cpp:10:1
(if
;;@ src.cpp:20:1
(i32.add
;;@ src.cpp:30:1
(local.get $x)
;;@ src.cpp:40:1
(local.get $y)
)
;;@ src.cpp:50:1
(return)
)
;;@ src.cpp:60:1
(call $foo
;;@ src.cpp:70:1
(local.get $x)
;;@ src.cpp:80:1
(local.get $y)
)
)
)

;; CHECK: (func $foo (param $x i32) (param $y i32)
;; CHECK-NEXT: ;;@ src.cpp:20:1
;; CHECK-NEXT: (if
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: ;;@ src.cpp:30:1
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: ;;@ src.cpp:40:1
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: )
;; CHECK-NEXT: ;;@ src.cpp:50:1
;; CHECK-NEXT: (return)
;; CHECK-NEXT: )
;; CHECK-NEXT: ;;@ src.cpp:60:1
;; CHECK-NEXT: (call $foo
;; CHECK-NEXT: ;;@ src.cpp:70:1
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: ;;@ src.cpp:80:1
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: )