Skip to content

Commit

Permalink
Disable debug info comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed May 27, 2016
1 parent ff171e1 commit 69f2dc1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
18 changes: 15 additions & 3 deletions src/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,26 @@ class NameManager : public WalkerPass<PostWalker<NameManager, Visitor<NameManage
size_t counter = 0;
};

struct PrinterArgs {
std::ostream& o;
bool minify;
bool fullAST;
bool debugInfo;

PrinterArgs(): o(std::cout), minify(false), fullAST(false), debugInfo(false) {}
PrinterArgs(std::ostream& o): o(o), minify(false), fullAST(false), debugInfo(false) {}
PrinterArgs(std::ostream& o, bool minify, bool fullAST, bool debugInfo): o(o), minify(minify), fullAST(fullAST), debugInfo(debugInfo) {}
};

// Prints out a module
class Printer : public Pass {
protected:
std::ostream& o;
PrinterArgs args;

public:
Printer() : o(std::cout) {}
Printer(std::ostream& o) : o(o) {}
Printer() : args(PrinterArgs()) {}
Printer(std::ostream& o) : args(PrinterArgs(o)) {}
Printer(const PrinterArgs& args) : args(args) {}

void run(PassRunner* runner, Module* module) override;
};
Expand Down
60 changes: 34 additions & 26 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
std::ostream& o;
unsigned indent = 0;

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

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

void setDebugInfoComments(bool debugInfoComments_) { debugInfoComments = debugInfoComments_; }

void incIndent() {
if (minify) return;
o << '\n';
Expand Down Expand Up @@ -84,7 +87,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}

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];
Expand All @@ -93,7 +96,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
!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) {
Expand Down Expand Up @@ -561,7 +564,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
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 << ";)";
Expand All @@ -587,6 +590,9 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
o << ')';
}
void printDebugSections(Module *curr) {
if (!debugInfoComments)
return;

bool hasSequences = false;
for (auto& child : curr->functions) {
if (child->labels.size() > 0) {
Expand Down Expand Up @@ -676,12 +682,14 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
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);
Expand All @@ -695,7 +703,10 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
};

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);
}

Expand All @@ -705,14 +716,8 @@ static RegisterPass<Printer> 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<MinifiedPrinter> registerMinifyPass("print-minified", "print in minified s-expression format");
Expand All @@ -721,18 +726,21 @@ static RegisterPass<MinifiedPrinter> 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<FullPrinter> 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<DebugInfoCommentsPrinter> 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) {
Expand Down
9 changes: 8 additions & 1 deletion src/tools/s2wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> archiveLibraries;
Options options("s2wasm", "Link .s file into .wast");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/wasm-printing.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ struct WasmPrinter {
return o;
}

static void printModule(Module* module, const PrinterArgs& args) {
PassRunner passRunner(module);
passRunner.add<Printer>(args);
passRunner.run();
}

static std::ostream& printModule(Module* module) {
return printModule(module, std::cout);
}
Expand Down

0 comments on commit 69f2dc1

Please sign in to comment.