diff options
-rw-r--r-- | src/wasm-binary.h | 5 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 53 | ||||
-rw-r--r-- | test/binaryen.js/sourcemap.js.txt | 2 | ||||
-rw-r--r-- | test/debugInfo.fromasm.clamp.map | 2 | ||||
-rw-r--r-- | test/debugInfo.fromasm.clamp.no-opts.map | 2 | ||||
-rw-r--r-- | test/debugInfo.fromasm.imprecise.map | 2 | ||||
-rw-r--r-- | test/debugInfo.fromasm.imprecise.no-opts.map | 2 | ||||
-rw-r--r-- | test/debugInfo.fromasm.map | 2 | ||||
-rw-r--r-- | test/debugInfo.fromasm.no-opts.map | 2 | ||||
-rw-r--r-- | test/debugInfo.fromasm.read-written | 29 |
10 files changed, 64 insertions, 37 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index ae71251bb..cdac878c1 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -733,8 +733,11 @@ class WasmBinaryWriter { MixedArena allocator; + // storage of source map locations until the section is placed at its final location + // (shrinking LEBs may cause changes there) + std::vector<std::pair<size_t, const Function::DebugLocation*>> sourceMapLocations; + size_t sourceMapLocationsSizeAtSectionStart; Function::DebugLocation lastDebugLocation; - size_t lastBytecodeOffset; void prepare(); public: diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 673738455..6a5775151 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -95,6 +95,7 @@ void WasmBinaryWriter::writeResizableLimits(Address initial, Address maximum, template<typename T> int32_t WasmBinaryWriter::startSection(T code) { o << U32LEB(code); + if (sourceMap) sourceMapLocationsSizeAtSectionStart = sourceMapLocations.size(); return writeU32LEBPlaceholder(); // section size to be filled in later } @@ -105,7 +106,13 @@ void WasmBinaryWriter::finishSection(int32_t start) { // we can save some room, nice assert(sizeFieldSize < MaxLEB32Bytes); std::move(&o[start] + MaxLEB32Bytes, &o[start] + MaxLEB32Bytes + size, &o[start] + sizeFieldSize); - o.resize(o.size() - (MaxLEB32Bytes - sizeFieldSize)); + auto adjustment = MaxLEB32Bytes - sizeFieldSize; + o.resize(o.size() - adjustment); + if (sourceMap) { + for (auto i = sourceMapLocationsSizeAtSectionStart; i < sourceMapLocations.size(); ++i) { + sourceMapLocations[i].first -= adjustment; + } + } } } @@ -220,6 +227,7 @@ void WasmBinaryWriter::writeFunctions() { size_t total = wasm->functions.size(); o << U32LEB(total); for (size_t i = 0; i < total; i++) { + size_t sourceMapLocationsSizeAtFunctionStart = sourceMapLocations.size(); if (debug) std::cerr << "write one at" << o.size() << std::endl; size_t sizePos = writeU32LEBPlaceholder(); size_t start = o.size(); @@ -247,7 +255,13 @@ void WasmBinaryWriter::writeFunctions() { // we can save some room, nice assert(sizeFieldSize < MaxLEB32Bytes); std::move(&o[start], &o[start] + size, &o[sizePos] + sizeFieldSize); - o.resize(o.size() - (MaxLEB32Bytes - sizeFieldSize)); + auto adjustment = MaxLEB32Bytes - sizeFieldSize; + o.resize(o.size() - adjustment); + if (sourceMap) { + for (auto i = sourceMapLocationsSizeAtFunctionStart; i < sourceMapLocations.size(); ++i) { + sourceMapLocations[i].first -= adjustment; + } + } } tableOfContents.functionBodies.emplace_back(function->name, sizePos + sizeFieldSize, size); } @@ -482,7 +496,6 @@ void WasmBinaryWriter::writeSymbolMap() { 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 << ","; @@ -492,10 +505,6 @@ void WasmBinaryWriter::writeSourceMapProlog() { *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) { @@ -512,6 +521,26 @@ static void writeBase64VLQ(std::ostream& out, int32_t n) { } } +void WasmBinaryWriter::writeSourceMapEpilog() { + // write source map entries + size_t lastOffset = 0; + Function::DebugLocation lastLoc = { 0, /* lineNumber = */ 1, 0 }; + for (const auto &offsetAndlocPair : sourceMapLocations) { + if (lastOffset > 0) { + *sourceMap << ","; + } + size_t offset = offsetAndlocPair.first; + const Function::DebugLocation& loc = *offsetAndlocPair.second; + writeBase64VLQ(*sourceMap, int32_t(offset - lastOffset)); + writeBase64VLQ(*sourceMap, int32_t(loc.fileIndex - lastLoc.fileIndex)); + writeBase64VLQ(*sourceMap, int32_t(loc.lineNumber - lastLoc.lineNumber)); + writeBase64VLQ(*sourceMap, int32_t(loc.columnNumber - lastLoc.columnNumber)); + lastLoc = loc; + lastOffset = offset; + } + *sourceMap << "\"}"; +} + void WasmBinaryWriter::writeUserSections() { for (auto& section : wasm->userSections) { auto start = startSection(0); @@ -529,15 +558,8 @@ void WasmBinaryWriter::writeDebugLocation(Expression* curr, Function* func) { if (iter != debugLocations.end() && iter->second != lastDebugLocation) { auto offset = o.size(); auto& loc = iter->second; - 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)); + sourceMapLocations.emplace_back(offset, &loc); lastDebugLocation = loc; - lastBytecodeOffset = offset; } } @@ -1784,6 +1806,7 @@ void WasmBinaryBuilder::readFunctions() { } } currFunction = nullptr; + useDebugLocation = false; functions.push_back(func); } if (debug) std::cerr << " end function bodies" << std::endl; diff --git a/test/binaryen.js/sourcemap.js.txt b/test/binaryen.js/sourcemap.js.txt index 0065a09db..edb2d86a7 100644 --- a/test/binaryen.js/sourcemap.js.txt +++ b/test/binaryen.js/sourcemap.js.txt @@ -5,4 +5,4 @@ module.c 020: u r c e M a p p i n g U R L 0f m 030: o d u l e . w a s m . m a p -{"version":3,"sources":["module.c"],"names":[],"mappings":"gCAAE"} +{"version":3,"sources":["module.c"],"names":[],"mappings":"wBAAE"} diff --git a/test/debugInfo.fromasm.clamp.map b/test/debugInfo.fromasm.clamp.map index c16ed9483..0f4b9aca9 100644 --- a/test/debugInfo.fromasm.clamp.map +++ b/test/debugInfo.fromasm.clamp.map @@ -1 +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","(unknown)"],"names":[],"mappings":"0IC8ylTA,QC7vlTA,OAkDA,wBCnGA,OACA,OACA,cCAA,kBAKA,QAJA,OADA,0BAKA,wECsi1DA,KCrvyDA"}
\ No newline at end of file +{"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","(unknown)"],"names":[],"mappings":"mIC8ylTA,QC7vlTA,OAkDA,wBCnGA,OACA,OACA,cCAA,kBAKA,QAJA,OADA,0BAKA,wECsi1DA,KCrvyDA"}
\ No newline at end of file diff --git a/test/debugInfo.fromasm.clamp.no-opts.map b/test/debugInfo.fromasm.clamp.no-opts.map index dc7e479dd..14e4f4e73 100644 --- a/test/debugInfo.fromasm.clamp.no-opts.map +++ b/test/debugInfo.fromasm.clamp.no-opts.map @@ -1 +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","(unknown)"],"names":[],"mappings":"0LAIA,IACA,ICyylTA,aC7vlTA,OAkDA,0BCnGA,OACA,OACA,uBCAA,4BAKA,QAJA,OADA,8CAKA,yICsi1DA,OCrvyDA"}
\ No newline at end of file +{"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","(unknown)"],"names":[],"mappings":"mLAIA,IACA,ICyylTA,aC7vlTA,OAkDA,0BCnGA,OACA,OACA,uBCAA,4BAKA,QAJA,OADA,8CAKA,0ICsi1DA,MCrvyDA"}
\ No newline at end of file diff --git a/test/debugInfo.fromasm.imprecise.map b/test/debugInfo.fromasm.imprecise.map index f70ea494b..9e6ef8390 100644 --- a/test/debugInfo.fromasm.imprecise.map +++ b/test/debugInfo.fromasm.imprecise.map @@ -1 +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","(unknown)"],"names":[],"mappings":"mGC8ylTA,QC7vlTA,OAkDA,QCnGA,OACA,OACA,aCAA,kBAKA,QAJA,OADA,0BAKA,wECsi1DA,KCrvyDA"}
\ No newline at end of file +{"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","(unknown)"],"names":[],"mappings":"4FC8ylTA,QC7vlTA,OAkDA,QCnGA,OACA,OACA,aCAA,kBAKA,QAJA,OADA,0BAKA,wECsi1DA,KCrvyDA"}
\ No newline at end of file diff --git a/test/debugInfo.fromasm.imprecise.no-opts.map b/test/debugInfo.fromasm.imprecise.no-opts.map index e37a63acc..f28541fa2 100644 --- a/test/debugInfo.fromasm.imprecise.no-opts.map +++ b/test/debugInfo.fromasm.imprecise.no-opts.map @@ -1 +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","(unknown)"],"names":[],"mappings":"yLAIA,IACA,ICyylTA,aC7vlTA,OAkDA,SCnGA,OACA,OACA,sBCAA,4BAKA,QAJA,OADA,8CAKA,yICsi1DA,OCrvyDA"}
\ No newline at end of file +{"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","(unknown)"],"names":[],"mappings":"kLAIA,IACA,ICyylTA,aC7vlTA,OAkDA,SCnGA,OACA,OACA,sBCAA,4BAKA,QAJA,OADA,8CAKA,0ICsi1DA,MCrvyDA"}
\ No newline at end of file diff --git a/test/debugInfo.fromasm.map b/test/debugInfo.fromasm.map index c16ed9483..0f4b9aca9 100644 --- a/test/debugInfo.fromasm.map +++ b/test/debugInfo.fromasm.map @@ -1 +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","(unknown)"],"names":[],"mappings":"0IC8ylTA,QC7vlTA,OAkDA,wBCnGA,OACA,OACA,cCAA,kBAKA,QAJA,OADA,0BAKA,wECsi1DA,KCrvyDA"}
\ No newline at end of file +{"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","(unknown)"],"names":[],"mappings":"mIC8ylTA,QC7vlTA,OAkDA,wBCnGA,OACA,OACA,cCAA,kBAKA,QAJA,OADA,0BAKA,wECsi1DA,KCrvyDA"}
\ No newline at end of file diff --git a/test/debugInfo.fromasm.no-opts.map b/test/debugInfo.fromasm.no-opts.map index dc7e479dd..14e4f4e73 100644 --- a/test/debugInfo.fromasm.no-opts.map +++ b/test/debugInfo.fromasm.no-opts.map @@ -1 +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","(unknown)"],"names":[],"mappings":"0LAIA,IACA,ICyylTA,aC7vlTA,OAkDA,0BCnGA,OACA,OACA,uBCAA,4BAKA,QAJA,OADA,8CAKA,yICsi1DA,OCrvyDA"}
\ No newline at end of file +{"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","(unknown)"],"names":[],"mappings":"mLAIA,IACA,ICyylTA,aC7vlTA,OAkDA,0BCnGA,OACA,OACA,uBCAA,4BAKA,QAJA,OADA,8CAKA,0ICsi1DA,MCrvyDA"}
\ No newline at end of file diff --git a/test/debugInfo.fromasm.read-written b/test/debugInfo.fromasm.read-written index 2ea421bfc..63f356c1b 100644 --- a/test/debugInfo.fromasm.read-written +++ b/test/debugInfo.fromasm.read-written @@ -12,55 +12,53 @@ (export "switch_reach" (func $switch_reach)) (export "nofile" (func $nofile)) (func $add (; 0 ;) (type $0) (param $var$0 i32) (param $var$1 i32) (result i32) + ;;@ tests/other_file.cpp:314159:0 (i32.add (get_local $var$1) (get_local $var$1) ) ) (func $ret (; 1 ;) (type $1) (param $var$0 i32) (result i32) + ;;@ return.cpp:50:0 (set_local $var$0 (i32.shl (get_local $var$0) (i32.const 1) ) ) - ;;@ tests/other_file.cpp:314159:0 + ;;@ return.cpp:100:0 (i32.add (get_local $var$0) (i32.const 1) ) ) (func $i32s-rem (; 2 ;) (type $0) (param $var$0 i32) (param $var$1 i32) (result i32) - ;;@ return.cpp:100:0 (if (result i32) - ;;@ return.cpp:50:0 (get_local $var$1) - ;;@ return.cpp:100:0 (i32.rem_s - ;;@ return.cpp:50:0 (get_local $var$0) (get_local $var$1) ) - ;;@ return.cpp:100:0 (i32.const 0) ) ) (func $opts (; 3 ;) (type $0) (param $var$0 i32) (param $var$1 i32) (result i32) + ;;@ even-opted.cpp:1:0 (set_local $var$0 (i32.add (get_local $var$0) (get_local $var$1) ) ) + ;;@ even-opted.cpp:2:0 (set_local $var$1 (i32.shr_s (get_local $var$1) (get_local $var$0) ) ) - ;;@ even-opted.cpp:2:0 + ;;@ even-opted.cpp:3:0 (i32.add - ;;@ even-opted.cpp:1:0 (call $i32s-rem (get_local $var$0) (get_local $var$1) @@ -73,9 +71,10 @@ (local $var$2 i32) (local $var$3 i32) (local $var$4 i32) - ;;@ fib.c:3:0 + ;;@ fib.c:8:0 (set_local $var$4 (if (result i32) + ;;@ fib.c:3:0 (i32.gt_s (get_local $var$0) (i32.const 0) @@ -90,35 +89,35 @@ (set_local $var$1 (i32.const 1) ) + ;;@ fib.c:8:0 (return (get_local $var$1) ) ) ) ) + ;;@ fib.c:3:0 (loop $label$3 + ;;@ fib.c:4:0 (set_local $var$1 (i32.add (get_local $var$3) (get_local $var$4) ) ) - ;;@ fib.c:8:0 + ;;@ fib.c:3:0 (set_local $var$2 (i32.add (get_local $var$2) (i32.const 1) ) ) - ;;@ fib.c:3:0 (if - ;;@ fib.c:4:0 (i32.ne (get_local $var$2) (get_local $var$0) ) (block - ;;@ fib.c:3:0 (set_local $var$4 (get_local $var$3) ) @@ -129,11 +128,11 @@ ) ) ) + ;;@ fib.c:8:0 (get_local $var$1) ) (func $switch_reach (; 5 ;) (type $1) (param $var$0 i32) (result i32) (local $var$1 i32) - ;;@ fib.c:8:0 (set_local $var$1 (block $label$1 (result i32) (block $label$2 @@ -183,9 +182,11 @@ (get_local $var$0) ) ) + ;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0 (get_local $var$1) ) (func $nofile (; 6 ;) (type $2) + ;;@ (unknown):1337:0 (call $nofile) ) ) |