Skip to content

Commit 89afc3e

Browse files
committed
Disable debug info comments.
1 parent 310b759 commit 89afc3e

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
@@ -165,14 +165,26 @@ class NameManager : public WalkerPass<PostWalker<NameManager, Visitor<NameManage
165165
size_t counter = 0;
166166
};
167167

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

173184
public:
174-
Printer() : o(std::cout) {}
175-
Printer(std::ostream& o) : o(o) {}
185+
Printer() : args(PrinterArgs()) {}
186+
Printer(std::ostream& o) : args(PrinterArgs(o)) {}
187+
Printer(const PrinterArgs& args) : args(args) {}
176188

177189
void run(PassRunner* runner, Module* module) override;
178190
};

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';
@@ -81,7 +84,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
8184
}
8285

8386
void visit(Expression *curr) {
84-
if (curr->debugInfo.labelIndex != lastLabelId) {
87+
if (debugInfoComments && curr->debugInfo.labelIndex != lastLabelId) {
8588
lastLabelId = curr->debugInfo.labelIndex;
8689
if (functionLabels) { // no labels -- skipping
8790
const Name& labelName = (*functionLabels)[lastLabelId];
@@ -90,7 +93,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
9093
!minify && doIndent(o, indent);
9194
}
9295
}
93-
if (curr->debugInfo.locationIndex != lastDebugLocationId) {
96+
if (debugInfoComments && curr->debugInfo.locationIndex != lastDebugLocationId) {
9497
lastDebugLocationId = curr->debugInfo.locationIndex;
9598
// skipping location id 0 or debug information is absent
9699
if (functionDebugLocations && lastDebugLocationId) {
@@ -491,7 +494,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
491494
printMinorOpening(o, "local ") << printableLocal(i) << ' ' << printWasmType(curr->getLocalType(i)) << ")";
492495
o << maybeNewLine;
493496
}
494-
if (functionLabels) { // no labels -- skipping
497+
if (debugInfoComments && functionLabels) { // no labels -- skipping
495498
const Name& labelName = (*functionLabels)[0]; // first label is normally a function name
496499
doIndent(o, indent);
497500
o << "(;!bookmark " << labelName.str << ";)";
@@ -517,6 +520,9 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
517520
o << ')';
518521
}
519522
void printDebugSections(Module *curr) {
523+
if (!debugInfoComments)
524+
return;
525+
520526
bool hasSequences = false;
521527
for (auto& child : curr->functions) {
522528
if (child->labels.size() > 0) {
@@ -606,12 +612,14 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
606612
visitTable(&curr->table);
607613
o << maybeNewLine;
608614
}
609-
for(auto const &item : curr->debugFileMap) {
610-
doIndent(o, indent);
611-
o << "(;!file " << item.first << " ";
612-
printText(o, item.second.str);
613-
o << ";)";
614-
o << maybeNewLine;
615+
if (debugInfoComments) {
616+
for(auto const &item : curr->debugFileMap) {
617+
doIndent(o, indent);
618+
o << "(;!file " << item.first << " ";
619+
printText(o, item.second.str);
620+
o << ";)";
621+
o << maybeNewLine;
622+
}
615623
}
616624
for (auto& child : curr->functions) {
617625
doIndent(o, indent);
@@ -625,7 +633,10 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
625633
};
626634

627635
void Printer::run(PassRunner* runner, Module* module) {
628-
PrintSExpression print(o);
636+
PrintSExpression print(args.o);
637+
print.setMinify(args.minify);
638+
print.setFullAST(args.fullAST);
639+
print.setDebugInfoComments(args.debugInfo);
629640
print.visitModule(module);
630641
}
631642

@@ -635,14 +646,8 @@ static RegisterPass<Printer> registerPass("print", "print in s-expression format
635646

636647
class MinifiedPrinter : public Printer {
637648
public:
638-
MinifiedPrinter() : Printer() {}
639-
MinifiedPrinter(std::ostream& o) : Printer(o) {}
640-
641-
void run(PassRunner* runner, Module* module) override {
642-
PrintSExpression print(o);
643-
print.setMinify(true);
644-
print.visitModule(module);
645-
}
649+
MinifiedPrinter() : Printer(PrinterArgs(std::cout, true, false, false)) {}
650+
MinifiedPrinter(std::ostream& o) : Printer(PrinterArgs(o, true, false, false)) {}
646651
};
647652

648653
static RegisterPass<MinifiedPrinter> registerMinifyPass("print-minified", "print in minified s-expression format");
@@ -651,18 +656,21 @@ static RegisterPass<MinifiedPrinter> registerMinifyPass("print-minified", "print
651656

652657
class FullPrinter : public Printer {
653658
public:
654-
FullPrinter() : Printer() {}
655-
FullPrinter(std::ostream& o) : Printer(o) {}
656-
657-
void run(PassRunner* runner, Module* module) override {
658-
PrintSExpression print(o);
659-
print.setFullAST(true);
660-
print.visitModule(module);
661-
}
659+
FullPrinter() : Printer(PrinterArgs(std::cout, false, true, false)) {}
660+
FullPrinter(std::ostream& o) : Printer(PrinterArgs(o, false, true, false)) {}
662661
};
663662

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

665+
// Prints out full ast module with additional debug info comments
666+
class DebugInfoCommentsPrinter : public Printer {
667+
public:
668+
DebugInfoCommentsPrinter() : Printer(PrinterArgs(std::cout, false, true, true)) {}
669+
DebugInfoCommentsPrinter(std::ostream& o) : Printer(PrinterArgs(o, false, true, true)) {}
670+
};
671+
672+
static RegisterPass<DebugInfoCommentsPrinter> registerDebugInfoCommentsPass("print-debug-info", "print in full s-expression format with debug info comments");
673+
666674
// Print individual expressions
667675

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

src/s2wasm-main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ using namespace wasm;
3030
int main(int argc, const char *argv[]) {
3131
bool ignoreUnknownSymbols = false;
3232
bool generateEmscriptenGlue = false;
33+
bool addDebugInfoComments = false;
3334
std::string startFunction;
3435
Options options("s2wasm", "Link .s file into .wast");
3536
options
@@ -74,6 +75,11 @@ int main(int argc, const char *argv[]) {
7475
[&generateEmscriptenGlue](Options *, const std::string &) {
7576
generateEmscriptenGlue = true;
7677
})
78+
.add("--debug-info", "", "Add debug info comments to the AST",
79+
Options::Arguments::Zero,
80+
[&addDebugInfoComments](Options *, const std::string &) {
81+
addDebugInfoComments = true;
82+
})
7783
.add_positional("INFILE", Options::Arguments::One,
7884
[](Options *o, const std::string &argument) {
7985
o->extra["infile"] = argument;
@@ -113,7 +119,8 @@ int main(int argc, const char *argv[]) {
113119

114120
if (options.debug) std::cerr << "Printing..." << std::endl;
115121
Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
116-
WasmPrinter::printModule(&wasm, output.getStream());
122+
WasmPrinter::printModule(&wasm, PrinterArgs(output.getStream(), false, false, addDebugInfoComments));
123+
117124
output << meta.str() << std::endl;
118125

119126
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(nullptr);
37+
passRunner.add<Printer>(args);
38+
passRunner.run(module);
39+
}
40+
3541
static std::ostream& printModule(Module* module) {
3642
return printModule(module, std::cout);
3743
}

0 commit comments

Comments
 (0)