Skip to content

Commit 69f2dc1

Browse files
committed
Disable debug info comments.
1 parent ff171e1 commit 69f2dc1

File tree

4 files changed

+63
-30
lines changed

4 files changed

+63
-30
lines changed

src/pass.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,26 @@ class NameManager : public WalkerPass<PostWalker<NameManager, Visitor<NameManage
167167
size_t counter = 0;
168168
};
169169

170+
struct PrinterArgs {
171+
std::ostream& o;
172+
bool minify;
173+
bool fullAST;
174+
bool debugInfo;
175+
176+
PrinterArgs(): o(std::cout), minify(false), fullAST(false), debugInfo(false) {}
177+
PrinterArgs(std::ostream& o): o(o), minify(false), fullAST(false), debugInfo(false) {}
178+
PrinterArgs(std::ostream& o, bool minify, bool fullAST, bool debugInfo): o(o), minify(minify), fullAST(fullAST), debugInfo(debugInfo) {}
179+
};
180+
170181
// Prints out a module
171182
class Printer : public Pass {
172183
protected:
173-
std::ostream& o;
184+
PrinterArgs args;
174185

175186
public:
176-
Printer() : o(std::cout) {}
177-
Printer(std::ostream& o) : o(o) {}
187+
Printer() : args(PrinterArgs()) {}
188+
Printer(std::ostream& o) : args(PrinterArgs(o)) {}
189+
Printer(const PrinterArgs& args) : args(args) {}
178190

179191
void run(PassRunner* runner, Module* module) override;
180192
};

src/passes/Print.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
2828
std::ostream& o;
2929
unsigned indent = 0;
3030

31+
bool debugInfoComments = false;
3132
size_t lastDebugLocationId = 0;
3233
std::vector<DebugLocation>* functionDebugLocations = nullptr;
3334
size_t lastLabelId = 0;
@@ -54,6 +55,8 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
5455

5556
void setFullAST(bool fullAST_) { fullAST = fullAST_; }
5657

58+
void setDebugInfoComments(bool debugInfoComments_) { debugInfoComments = debugInfoComments_; }
59+
5760
void incIndent() {
5861
if (minify) return;
5962
o << '\n';
@@ -84,7 +87,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
8487
}
8588

8689
void visit(Expression *curr) {
87-
if (curr->debugInfo.labelIndex != lastLabelId) {
90+
if (debugInfoComments && curr->debugInfo.labelIndex != lastLabelId) {
8891
lastLabelId = curr->debugInfo.labelIndex;
8992
if (functionLabels) { // no labels -- skipping
9093
const Name& labelName = (*functionLabels)[lastLabelId];
@@ -93,7 +96,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
9396
!minify && doIndent(o, indent);
9497
}
9598
}
96-
if (curr->debugInfo.locationIndex != lastDebugLocationId) {
99+
if (debugInfoComments && curr->debugInfo.locationIndex != lastDebugLocationId) {
97100
lastDebugLocationId = curr->debugInfo.locationIndex;
98101
// skipping location id 0 or debug information is absent
99102
if (functionDebugLocations && lastDebugLocationId) {
@@ -561,7 +564,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
561564
printMinorOpening(o, "local ") << printableLocal(i) << ' ' << printWasmType(curr->getLocalType(i)) << ")";
562565
o << maybeNewLine;
563566
}
564-
if (functionLabels) { // no labels -- skipping
567+
if (debugInfoComments && functionLabels) { // no labels -- skipping
565568
const Name& labelName = (*functionLabels)[0]; // first label is normally a function name
566569
doIndent(o, indent);
567570
o << "(;!bookmark " << labelName.str << ";)";
@@ -587,6 +590,9 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
587590
o << ')';
588591
}
589592
void printDebugSections(Module *curr) {
593+
if (!debugInfoComments)
594+
return;
595+
590596
bool hasSequences = false;
591597
for (auto& child : curr->functions) {
592598
if (child->labels.size() > 0) {
@@ -676,12 +682,14 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
676682
visitTable(&curr->table);
677683
o << maybeNewLine;
678684
}
679-
for(auto const &item : curr->debugFileMap) {
680-
doIndent(o, indent);
681-
o << "(;!file " << item.first << " ";
682-
printText(o, item.second.str);
683-
o << ";)";
684-
o << maybeNewLine;
685+
if (debugInfoComments) {
686+
for(auto const &item : curr->debugFileMap) {
687+
doIndent(o, indent);
688+
o << "(;!file " << item.first << " ";
689+
printText(o, item.second.str);
690+
o << ";)";
691+
o << maybeNewLine;
692+
}
685693
}
686694
for (auto& child : curr->functions) {
687695
doIndent(o, indent);
@@ -695,7 +703,10 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
695703
};
696704

697705
void Printer::run(PassRunner* runner, Module* module) {
698-
PrintSExpression print(o);
706+
PrintSExpression print(args.o);
707+
print.setMinify(args.minify);
708+
print.setFullAST(args.fullAST);
709+
print.setDebugInfoComments(args.debugInfo);
699710
print.visitModule(module);
700711
}
701712

@@ -705,14 +716,8 @@ static RegisterPass<Printer> registerPass("print", "print in s-expression format
705716

706717
class MinifiedPrinter : public Printer {
707718
public:
708-
MinifiedPrinter() : Printer() {}
709-
MinifiedPrinter(std::ostream& o) : Printer(o) {}
710-
711-
void run(PassRunner* runner, Module* module) override {
712-
PrintSExpression print(o);
713-
print.setMinify(true);
714-
print.visitModule(module);
715-
}
719+
MinifiedPrinter() : Printer(PrinterArgs(std::cout, true, false, false)) {}
720+
MinifiedPrinter(std::ostream& o) : Printer(PrinterArgs(o, true, false, false)) {}
716721
};
717722

718723
static RegisterPass<MinifiedPrinter> registerMinifyPass("print-minified", "print in minified s-expression format");
@@ -721,18 +726,21 @@ static RegisterPass<MinifiedPrinter> registerMinifyPass("print-minified", "print
721726

722727
class FullPrinter : public Printer {
723728
public:
724-
FullPrinter() : Printer() {}
725-
FullPrinter(std::ostream& o) : Printer(o) {}
726-
727-
void run(PassRunner* runner, Module* module) override {
728-
PrintSExpression print(o);
729-
print.setFullAST(true);
730-
print.visitModule(module);
731-
}
729+
FullPrinter() : Printer(PrinterArgs(std::cout, false, true, false)) {}
730+
FullPrinter(std::ostream& o) : Printer(PrinterArgs(o, false, true, false)) {}
732731
};
733732

734733
static RegisterPass<FullPrinter> registerFullASTPass("print-full", "print in full s-expression format");
735734

735+
// Prints out full ast module with additional debug info comments
736+
class DebugInfoCommentsPrinter : public Printer {
737+
public:
738+
DebugInfoCommentsPrinter() : Printer(PrinterArgs(std::cout, false, true, true)) {}
739+
DebugInfoCommentsPrinter(std::ostream& o) : Printer(PrinterArgs(o, false, true, true)) {}
740+
};
741+
742+
static RegisterPass<DebugInfoCommentsPrinter> registerDebugInfoCommentsPass("print-debug-info", "print in full s-expression format with debug info comments");
743+
736744
// Print individual expressions
737745

738746
std::ostream& WasmPrinter::printExpression(Expression* expression, std::ostream& o, bool minify) {

src/tools/s2wasm.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ using namespace wasm;
3131
int main(int argc, const char *argv[]) {
3232
bool ignoreUnknownSymbols = false;
3333
bool generateEmscriptenGlue = false;
34+
bool addDebugInfoComments = false;
3435
std::string startFunction;
3536
std::vector<std::string> archiveLibraries;
3637
Options options("s2wasm", "Link .s file into .wast");
@@ -81,6 +82,11 @@ int main(int argc, const char *argv[]) {
8182
[&archiveLibraries](Options *o, const std::string &argument) {
8283
archiveLibraries.push_back(argument);
8384
})
85+
.add("--debug-info", "", "Add debug info comments to the AST",
86+
Options::Arguments::Zero,
87+
[&addDebugInfoComments](Options *, const std::string &) {
88+
addDebugInfoComments = true;
89+
})
8490
.add_positional("INFILE", Options::Arguments::One,
8591
[](Options *o, const std::string &argument) {
8692
o->extra["infile"] = argument;
@@ -133,7 +139,8 @@ int main(int argc, const char *argv[]) {
133139

134140
if (options.debug) std::cerr << "Printing..." << std::endl;
135141
Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
136-
WasmPrinter::printModule(&linker.getOutput().wasm, output.getStream());
142+
WasmPrinter::printModule(&linker.getOutput().wasm, PrinterArgs(output.getStream(), false, false, addDebugInfoComments));
143+
137144
output << meta.str();
138145

139146
if (options.debug) std::cerr << "Done." << std::endl;

src/wasm-printing.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ struct WasmPrinter {
3232
return o;
3333
}
3434

35+
static void printModule(Module* module, const PrinterArgs& args) {
36+
PassRunner passRunner(module);
37+
passRunner.add<Printer>(args);
38+
passRunner.run();
39+
}
40+
3541
static std::ostream& printModule(Module* module) {
3642
return printModule(module, std::cout);
3743
}

0 commit comments

Comments
 (0)