diff --git a/src/pass.h b/src/pass.h index bfbcb26d8d0..abfb836501a 100644 --- a/src/pass.h +++ b/src/pass.h @@ -167,14 +167,26 @@ class NameManager : public WalkerPass { std::ostream& o; unsigned indent = 0; + bool debugInfoComments = false; size_t lastDebugLocationId = 0; std::vector* functionDebugLocations = nullptr; size_t lastLabelId = 0; @@ -54,6 +55,8 @@ struct PrintSExpression : public Visitor { void setFullAST(bool fullAST_) { fullAST = fullAST_; } + void setDebugInfoComments(bool debugInfoComments_) { debugInfoComments = debugInfoComments_; } + void incIndent() { if (minify) return; o << '\n'; @@ -84,7 +87,7 @@ struct PrintSExpression : public Visitor { } void visit(Expression *curr) { - if (curr->debugInfo.labelIndex != lastLabelId) { + if (debugInfoComments && curr->debugInfo.labelIndex != lastLabelId) { lastLabelId = curr->debugInfo.labelIndex; if (functionLabels) { // no labels -- skipping const Name& labelName = (*functionLabels)[lastLabelId]; @@ -93,7 +96,7 @@ struct PrintSExpression : public Visitor { !minify && doIndent(o, indent); } } - if (curr->debugInfo.locationIndex != lastDebugLocationId) { + if (debugInfoComments && curr->debugInfo.locationIndex != lastDebugLocationId) { lastDebugLocationId = curr->debugInfo.locationIndex; // skipping location id 0 or debug information is absent if (functionDebugLocations && lastDebugLocationId) { @@ -561,7 +564,7 @@ struct PrintSExpression : public Visitor { printMinorOpening(o, "local ") << printableLocal(i) << ' ' << printWasmType(curr->getLocalType(i)) << ")"; o << maybeNewLine; } - if (functionLabels) { // no labels -- skipping + if (debugInfoComments && functionLabels) { // no labels -- skipping const Name& labelName = (*functionLabels)[0]; // first label is normally a function name doIndent(o, indent); o << "(;!bookmark " << labelName.str << ";)"; @@ -587,6 +590,9 @@ struct PrintSExpression : public Visitor { o << ')'; } void printDebugSections(Module *curr) { + if (!debugInfoComments) + return; + bool hasSequences = false; for (auto& child : curr->functions) { if (child->labels.size() > 0) { @@ -676,12 +682,14 @@ struct PrintSExpression : public Visitor { visitTable(&curr->table); o << maybeNewLine; } - for(auto const &item : curr->debugFileMap) { - doIndent(o, indent); - o << "(;!file " << item.first << " "; - printText(o, item.second.str); - o << ";)"; - o << maybeNewLine; + if (debugInfoComments) { + for(auto const &item : curr->debugFileMap) { + doIndent(o, indent); + o << "(;!file " << item.first << " "; + printText(o, item.second.str); + o << ";)"; + o << maybeNewLine; + } } for (auto& child : curr->functions) { doIndent(o, indent); @@ -695,7 +703,10 @@ struct PrintSExpression : public Visitor { }; void Printer::run(PassRunner* runner, Module* module) { - PrintSExpression print(o); + PrintSExpression print(args.o); + print.setMinify(args.minify); + print.setFullAST(args.fullAST); + print.setDebugInfoComments(args.debugInfo); print.visitModule(module); } @@ -705,14 +716,8 @@ static RegisterPass registerPass("print", "print in s-expression format class MinifiedPrinter : public Printer { public: - MinifiedPrinter() : Printer() {} - MinifiedPrinter(std::ostream& o) : Printer(o) {} - - void run(PassRunner* runner, Module* module) override { - PrintSExpression print(o); - print.setMinify(true); - print.visitModule(module); - } + MinifiedPrinter() : Printer(PrinterArgs(std::cout, true, false, false)) {} + MinifiedPrinter(std::ostream& o) : Printer(PrinterArgs(o, true, false, false)) {} }; static RegisterPass registerMinifyPass("print-minified", "print in minified s-expression format"); @@ -721,18 +726,21 @@ static RegisterPass registerMinifyPass("print-minified", "print class FullPrinter : public Printer { public: - FullPrinter() : Printer() {} - FullPrinter(std::ostream& o) : Printer(o) {} - - void run(PassRunner* runner, Module* module) override { - PrintSExpression print(o); - print.setFullAST(true); - print.visitModule(module); - } + FullPrinter() : Printer(PrinterArgs(std::cout, false, true, false)) {} + FullPrinter(std::ostream& o) : Printer(PrinterArgs(o, false, true, false)) {} }; static RegisterPass registerFullASTPass("print-full", "print in full s-expression format"); +// Prints out full ast module with additional debug info comments +class DebugInfoCommentsPrinter : public Printer { + public: + DebugInfoCommentsPrinter() : Printer(PrinterArgs(std::cout, false, true, true)) {} + DebugInfoCommentsPrinter(std::ostream& o) : Printer(PrinterArgs(o, false, true, true)) {} +}; + +static RegisterPass registerDebugInfoCommentsPass("print-debug-info", "print in full s-expression format with debug info comments"); + // Print individual expressions std::ostream& WasmPrinter::printExpression(Expression* expression, std::ostream& o, bool minify) { diff --git a/src/tools/s2wasm.cpp b/src/tools/s2wasm.cpp index a6bbdea181b..ae24aa08569 100644 --- a/src/tools/s2wasm.cpp +++ b/src/tools/s2wasm.cpp @@ -31,6 +31,7 @@ using namespace wasm; int main(int argc, const char *argv[]) { bool ignoreUnknownSymbols = false; bool generateEmscriptenGlue = false; + bool addDebugInfoComments = false; std::string startFunction; std::vector archiveLibraries; Options options("s2wasm", "Link .s file into .wast"); @@ -81,6 +82,11 @@ int main(int argc, const char *argv[]) { [&archiveLibraries](Options *o, const std::string &argument) { archiveLibraries.push_back(argument); }) + .add("--debug-info", "", "Add debug info comments to the AST", + Options::Arguments::Zero, + [&addDebugInfoComments](Options *, const std::string &) { + addDebugInfoComments = true; + }) .add_positional("INFILE", Options::Arguments::One, [](Options *o, const std::string &argument) { o->extra["infile"] = argument; @@ -133,7 +139,8 @@ int main(int argc, const char *argv[]) { if (options.debug) std::cerr << "Printing..." << std::endl; Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release); - WasmPrinter::printModule(&linker.getOutput().wasm, output.getStream()); + WasmPrinter::printModule(&linker.getOutput().wasm, PrinterArgs(output.getStream(), false, false, addDebugInfoComments)); + output << meta.str(); if (options.debug) std::cerr << "Done." << std::endl; diff --git a/src/wasm-printing.h b/src/wasm-printing.h index d1f1c42a5c2..805a1394780 100644 --- a/src/wasm-printing.h +++ b/src/wasm-printing.h @@ -32,6 +32,12 @@ struct WasmPrinter { return o; } + static void printModule(Module* module, const PrinterArgs& args) { + PassRunner passRunner(module); + passRunner.add(args); + passRunner.run(); + } + static std::ostream& printModule(Module* module) { return printModule(module, std::cout); }