diff options
author | Thomas Lively <tlively@google.com> | 2024-01-04 14:25:18 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-04 14:25:18 -0800 |
commit | a58281ca114359cd6e65f5daaf086636aa18b0b0 (patch) | |
tree | ff98bd31d1c87b598027c2303b17855a44346515 /src/passes/Print.cpp | |
parent | 0ed42cf976ce9a3dfbe9cbb0885122e8fb6a377b (diff) | |
download | binaryen-a58281ca114359cd6e65f5daaf086636aa18b0b0.tar.gz binaryen-a58281ca114359cd6e65f5daaf086636aa18b0b0.tar.bz2 binaryen-a58281ca114359cd6e65f5daaf086636aa18b0b0.zip |
Require `then` and `else` with `if` (#6201)
We previously supported (and primarily used) a non-standard text format for
conditionals in which the condition, if-true expression, and if-false expression
were all simply s-expression children of the `if` expression. The standard text
format, however, requires the use of `then` and `else` forms to introduce the
if-true and if-false arms of the conditional. Update the legacy text parser to
require the standard format and update all tests to match. Update the printer to
print the standard format as well.
The .wast and .wat test inputs were mechanically updated with this script:
https://gist.github.com/tlively/85ae7f01f92f772241ec994c840ccbb1
Diffstat (limited to 'src/passes/Print.cpp')
-rw-r--r-- | src/passes/Print.cpp | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index dcd43c323..b6060a853 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -306,7 +306,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> { // loop, if, and try can contain implicit blocks. But they are not needed to // be printed in some cases. - void maybePrintImplicitBlock(Expression* curr, bool allowMultipleInsts); + void maybePrintImplicitBlock(Expression* curr); // Generic visitor, overridden only when necessary. void visitExpression(Expression* curr); @@ -2563,11 +2563,9 @@ void PrintSExpression::printFullLine(Expression* expression) { o << maybeNewLine; } -void PrintSExpression::maybePrintImplicitBlock(Expression* curr, - bool allowMultipleInsts) { +void PrintSExpression::maybePrintImplicitBlock(Expression* curr) { auto block = curr->dynCast<Block>(); - if (!full && block && block->name.isNull() && - (allowMultipleInsts || block->list.size() == 1)) { + if (!full && block && block->name.isNull()) { for (auto expression : block->list) { printFullLine(expression); } @@ -2657,13 +2655,23 @@ void PrintSExpression::visitIf(If* curr) { printExpressionContents(curr); incIndent(); printFullLine(curr->condition); - maybePrintImplicitBlock(curr->ifTrue, false); + doIndent(o, indent); + o << "(then"; + incIndent(); + maybePrintImplicitBlock(curr->ifTrue); + decIndent(); + o << maybeNewLine; if (curr->ifFalse) { + doIndent(o, indent); + o << "(else"; + incIndent(); // Note: debug info here is not used as LLVM does not emit ifs, and since // LLVM is the main source of DWARF, effectively we never encounter ifs // with DWARF. printDebugDelimiterLocation(curr, BinaryLocations::Else); - maybePrintImplicitBlock(curr->ifFalse, false); + maybePrintImplicitBlock(curr->ifFalse); + decIndent(); + o << maybeNewLine; } decIndent(); if (full) { @@ -2677,7 +2685,7 @@ void PrintSExpression::visitLoop(Loop* curr) { o << '('; printExpressionContents(curr); incIndent(); - maybePrintImplicitBlock(curr->body, true); + maybePrintImplicitBlock(curr->body); decIndent(); if (full) { o << " ;; end loop"; @@ -2722,7 +2730,7 @@ void PrintSExpression::visitTry(Try* curr) { o << '('; printMedium(o, "do"); incIndent(); - maybePrintImplicitBlock(curr->body, true); + maybePrintImplicitBlock(curr->body); decIndent(); o << "\n"; for (size_t i = 0; i < curr->catchTags.size(); i++) { @@ -2732,7 +2740,7 @@ void PrintSExpression::visitTry(Try* curr) { printMedium(o, "catch "); printName(curr->catchTags[i], o); incIndent(); - maybePrintImplicitBlock(curr->catchBodies[i], true); + maybePrintImplicitBlock(curr->catchBodies[i]); decIndent(); o << "\n"; } @@ -2742,7 +2750,7 @@ void PrintSExpression::visitTry(Try* curr) { o << '('; printMedium(o, "catch_all"); incIndent(); - maybePrintImplicitBlock(curr->catchBodies.back(), true); + maybePrintImplicitBlock(curr->catchBodies.back()); decIndent(); o << "\n"; } @@ -2770,7 +2778,7 @@ void PrintSExpression::visitTryTable(TryTable* curr) { o << '('; printExpressionContents(curr); incIndent(); - maybePrintImplicitBlock(curr->body, true); + maybePrintImplicitBlock(curr->body); decIndent(); if (full) { o << " ;; end if"; |