Skip to content

Commit

Permalink
Implement parsing and printing
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Sep 20, 2024
1 parent 6171fe9 commit 0a9d251
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5662,6 +5662,14 @@ void BinaryenModuleInterpret(BinaryenModuleRef module) {
ModuleRunner instance(*(Module*)module, &interface, {});
}

BinaryenIndex BinaryenModuleAddDebugInfoSymbolName(BinaryenModuleRef module,
const char* symbolname) {
auto& debugInfoSymbolNames = ((Module*)module)->debugInfoSymbolNames;
BinaryenIndex index = debugInfoSymbolNames.size();
debugInfoSymbolNames.push_back(symbolname);
return index;
}

BinaryenIndex BinaryenModuleAddDebugInfoFileName(BinaryenModuleRef module,
const char* filename) {
auto& debugInfoFileNames = ((Module*)module)->debugInfoFileNames;
Expand All @@ -5670,6 +5678,14 @@ BinaryenIndex BinaryenModuleAddDebugInfoFileName(BinaryenModuleRef module,
return index;
}

const char* BinaryenModuleGetDebugInfoSymbolName(BinaryenModuleRef module,
BinaryenIndex index) {
const auto& debugInfoSymbolNames = ((Module*)module)->debugInfoSymbolNames;
return index < debugInfoSymbolNames.size()
? debugInfoSymbolNames.at(index).c_str()
: nullptr;
}

const char* BinaryenModuleGetDebugInfoFileName(BinaryenModuleRef module,
BinaryenIndex index) {
const auto& debugInfoFileNames = ((Module*)module)->debugInfoFileNames;
Expand Down
2 changes: 2 additions & 0 deletions src/ir/module-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ void copyModuleItems(const Module& in, Module& out) {
// to map file name indices from this modules to file name indices in
// the target module.
std::optional<std::vector<Index>> fileIndexMap;
// TODO: Update this with symbol names
if (!in.debugInfoFileNames.empty()) {
std::unordered_map<std::string, Index> debugInfoFileIndices;
for (Index i = 0; i < out.debugInfoFileNames.size(); i++) {
Expand Down Expand Up @@ -241,6 +242,7 @@ void copyModule(const Module& in, Module& out) {
out.start = in.start;
out.customSections = in.customSections;
out.debugInfoFileNames = in.debugInfoFileNames;
out.debugInfoSymbolNames = in.debugInfoSymbolNames;
out.features = in.features;
}

Expand Down
13 changes: 13 additions & 0 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
typeNames;
const std::unordered_map<Index, Index>& implicitElemIndices;

std::unordered_map<std::string_view, Index> debugSymbolNameIndices;
std::unordered_map<std::string_view, Index> debugFileIndices;

// The index of the current module element.
Expand Down Expand Up @@ -1783,6 +1784,18 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
return;
}

std::optional<BinaryLocation> nameIndex;
auto namePos = contents.find(':');
if (namePos != contents.npos) {
auto name = contents.substr(namePos + 1);
auto [it, inserted] =
debugSymbolNameIndices.insert({name, debugSymbolNameIndices.size()});
if (inserted) {
assert(wasm.debugInfoSymbolNames.size() == it->second);
wasm.debugInfoSymbolNames.push_back(std::string(file));
}
}

// TODO: If we ever parallelize the parse, access to
// `wasm.debugInfoFileNames` will have to be protected by a lock.
auto [it, inserted] =
Expand Down
9 changes: 8 additions & 1 deletion src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2504,7 +2504,14 @@ void PrintSExpression::printDebugLocation(
} else {
auto fileName = currModule->debugInfoFileNames[location->fileIndex];
o << ";;@ " << fileName << ":" << location->lineNumber << ":"
<< location->columnNumber << '\n';
<< location->columnNumber;

if (location->nameIndex.has_value()) {
auto symbolName = currModule->debugInfoSymbolNames[*(location->nameIndex)];
o << ":" << symbolName;
}

o << '\n';
}
doIndent(o, indent);
}
Expand Down
1 change: 1 addition & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,7 @@ class WasmBinaryReader {
// Debug information reading helpers
void setDebugLocations(std::istream* sourceMap_) { sourceMap = sourceMap_; }
std::unordered_map<std::string, Index> debugInfoFileIndices;
std::unordered_map<std::string, Index> debugInfoSymbolNameIndices;
void readNextDebugLocation();
void readSourceMapHeader();

Expand Down
1 change: 1 addition & 0 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,7 @@ class Module {

// Source maps debug info.
std::vector<std::string> debugInfoFileNames;
std::vector<std::string> debugInfoSymbolNames;

// `features` are the features allowed to be used in this module and should be
// respected regardless of the value of`hasFeaturesSection`.
Expand Down
34 changes: 30 additions & 4 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,17 @@ void WasmBinaryWriter::writeSourceMapProlog() {
// TODO respect JSON string encoding, e.g. quotes and control chars.
*sourceMap << "\"" << wasm->debugInfoFileNames[i] << "\"";
}
*sourceMap << "],\"names\":[],\"mappings\":\"";
*sourceMap << "],\"names\":[";

for (size_t i = 0; i < wasm->debugInfoSymbolNames.size(); i++) {
if (i > 0) {
*sourceMap << ",";
}
// TODO respect JSON string encoding, e.g. quotes and control chars.
*sourceMap << "\"" << wasm->debugInfoSymbolNames[i] << "\"";
}

*sourceMap << "],\"mappings\":\"";
}

static void writeBase64VLQ(std::ostream& out, int32_t n) {
Expand All @@ -1236,9 +1246,10 @@ static void writeBase64VLQ(std::ostream& out, int32_t n) {
}
// 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 ? '+' : '/');
out << char(digit < 20 ? 'g' + digit
: digit < 30 ? '0' + digit - 20
: digit == 30 ? '+'
: '/');
}
}

Expand Down Expand Up @@ -2886,6 +2897,21 @@ void WasmBinaryReader::readSourceMapHeader() {
mustReadChar(']');
}

if (findField("names")) {
skipWhitespace();
mustReadChar('[');
if (!maybeReadChar(']')) {
do {
std::string symbol;
readString(symbol);
Index index = wasm.debugInfoSymbolNames.size();
wasm.debugInfoSymbolNames.push_back(symbol);
debugInfoSymbolNameIndices[symbol] = index;
} while (maybeReadChar(','));
mustReadChar(']');
}
}

if (!findField("mappings")) {
throw MapParseException("cannot find the 'mappings' field in map");
}
Expand Down
5 changes: 4 additions & 1 deletion src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,9 @@ void Module::updateMaps() {
assert(tagsMap.size() == tags.size());
}

void Module::clearDebugInfo() { debugInfoFileNames.clear(); }
void Module::clearDebugInfo() {
debugInfoFileNames.clear();
debugInfoSymbolNames.clear();
}

} // namespace wasm

0 comments on commit 0a9d251

Please sign in to comment.