diff options
author | Alon Zakai <azakai@google.com> | 2020-12-17 12:39:21 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-17 12:39:21 -0800 |
commit | 2257f857069faa56335d2e24d7d6853c9501fcb7 (patch) | |
tree | ca67dc5b66b7d24759fc1043ed154b7f002a0188 /src/passes/Print.cpp | |
parent | a8ded16f56afd880a9a6459fe5ce55a8667d9b3e (diff) | |
download | binaryen-2257f857069faa56335d2e24d7d6853c9501fcb7.tar.gz binaryen-2257f857069faa56335d2e24d7d6853c9501fcb7.tar.bz2 binaryen-2257f857069faa56335d2e24d7d6853c9501fcb7.zip |
Refactor printing code so that printing Expressions always works (#3450)
This avoids needing to add include wasm-printing if a file doesn't already have it.
To achieve that, add the std::ostream hooks in wasm.h, and also use them
when possible, removing the need for the special WasmPrinter object.
Also stop printing in "full" (print types on each line) in error messages by default. The
user can still get that, as always, using BINARYEN_PRINT_FULL=1 in the env.
Diffstat (limited to 'src/passes/Print.cpp')
-rw-r--r-- | src/passes/Print.cpp | 82 |
1 files changed, 53 insertions, 29 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index b61a72720..c0e940d04 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -21,12 +21,22 @@ #include <ir/module-utils.h> #include <pass.h> #include <pretty_printing.h> -#include <wasm-printing.h> #include <wasm-stack.h> #include <wasm.h> namespace wasm { +static std::ostream& printExpression(Expression* expression, + std::ostream& o, + bool minify = false, + bool full = false); + +static std::ostream& +printStackInst(StackInst* inst, std::ostream& o, Function* func = nullptr); + +static std::ostream& +printStackIR(StackIR* ir, std::ostream& o, Function* func = nullptr); + namespace { bool isFullForced() { @@ -1814,9 +1824,9 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { bool full = false; // whether to not elide nodes in output when possible // (like implicit blocks) and to emit types - bool printStackIR = false; // whether to print stack IR if it is present - // (if false, and Stack IR is there, we just - // note it exists) + bool stackIR = false; // whether to print stack IR if it is present + // (if false, and Stack IR is there, we just + // note it exists) Module* currModule = nullptr; Function* currFunction = nullptr; @@ -1876,7 +1886,7 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { void setFull(bool full_) { full = full_; } - void setPrintStackIR(bool printStackIR_) { printStackIR = printStackIR_; } + void setStackIR(bool stackIR_) { stackIR = stackIR_; } void setDebugInfo(bool debugInfo_) { debugInfo = debugInfo_; } @@ -2680,7 +2690,7 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { o << '('; printMajor(o, "func "); printName(curr->name, o); - if (!printStackIR && curr->stackIR && !minify) { + if (!stackIR && curr->stackIR && !minify) { o << " (; has Stack IR ;)"; } if (curr->sig.params.size() > 0) { @@ -2708,7 +2718,7 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { o << maybeNewLine; } // Print the body. - if (!printStackIR || !curr->stackIR) { + if (!stackIR || !curr->stackIR) { // It is ok to emit a block here, as a function can directly contain a // list, even if our ast avoids that for simplicity. We can just do that // optimization here.. @@ -2723,7 +2733,7 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { } } else { // Print the stack IR. - WasmPrinter::printStackIR(curr->stackIR.get(), o, curr); + printStackIR(curr->stackIR.get(), o, curr); } if (currFunction->epilogLocation.size() && lastPrintedLocation != *currFunction->epilogLocation.begin()) { @@ -3061,29 +3071,17 @@ public: void run(PassRunner* runner, Module* module) override { PrintSExpression print(o); print.setDebugInfo(runner->options.debugInfo); - print.setPrintStackIR(true); + print.setStackIR(true); print.visitModule(module); } }; Pass* createPrintStackIRPass() { return new PrintStackIR(); } -// Print individual expressions - -std::ostream& WasmPrinter::printModule(Module* module, std::ostream& o) { - PassRunner runner(module); - Printer(&o).run(&runner, module); - return o; -} - -std::ostream& WasmPrinter::printModule(Module* module) { - return printModule(module, std::cout); -} - -std::ostream& WasmPrinter::printExpression(Expression* expression, - std::ostream& o, - bool minify, - bool full) { +static std::ostream& printExpression(Expression* expression, + std::ostream& o, + bool minify, + bool full) { if (!expression) { o << "(null expression)"; return o; @@ -3098,8 +3096,8 @@ std::ostream& WasmPrinter::printExpression(Expression* expression, return o; } -std::ostream& -WasmPrinter::printStackInst(StackInst* inst, std::ostream& o, Function* func) { +static std::ostream& +printStackInst(StackInst* inst, std::ostream& o, Function* func) { switch (inst->op) { case StackInst::Basic: { PrintExpressionContents(func, o).visit(inst->origin); @@ -3133,8 +3131,8 @@ WasmPrinter::printStackInst(StackInst* inst, std::ostream& o, Function* func) { return o; } -std::ostream& -WasmPrinter::printStackIR(StackIR* ir, std::ostream& o, Function* func) { +static std::ostream& +printStackIR(StackIR* ir, std::ostream& o, Function* func) { size_t indent = func ? 2 : 0; auto doIndent = [&indent, &o]() { for (size_t j = 0; j < indent; j++) { @@ -3198,3 +3196,29 @@ WasmPrinter::printStackIR(StackIR* ir, std::ostream& o, Function* func) { } } // namespace wasm + +namespace std { + +std::ostream& operator<<(std::ostream& o, wasm::Module& module) { + wasm::PassRunner runner(&module); + wasm::Printer(&o).run(&runner, &module); + return o; +} + +std::ostream& operator<<(std::ostream& o, wasm::Expression& expression) { + return wasm::printExpression(&expression, o); +} + +std::ostream& operator<<(std::ostream& o, wasm::Expression* expression) { + return wasm::printExpression(expression, o); +} + +std::ostream& operator<<(std::ostream& o, wasm::StackInst& inst) { + return wasm::printStackInst(&inst, o); +} + +std::ostream& operator<<(std::ostream& o, wasm::StackIR& ir) { + return wasm::printStackIR(&ir, o); +} + +} // namespace std |