summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-11-29 09:20:45 -0800
committerGitHub <noreply@github.com>2022-11-29 09:20:45 -0800
commitece1da725bd798c205b85518e6bc5fd0d0140240 (patch)
tree57f50d9294ac6c559ebda8813107cad9d3328625
parenteeb838155dbff2fb6731f1ae5550cbb9494532fe (diff)
downloadbinaryen-ece1da725bd798c205b85518e6bc5fd0d0140240.tar.gz
binaryen-ece1da725bd798c205b85518e6bc5fd0d0140240.tar.bz2
binaryen-ece1da725bd798c205b85518e6bc5fd0d0140240.zip
[NFC] Print type names in BINARYEN_PRINT_FULL mode (#5296)
-rw-r--r--src/passes/Print.cpp31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index d9e54b054..16b036693 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -425,6 +425,25 @@ std::ostream& printEscapedString(std::ostream& os, std::string_view str) {
return os << '"';
}
+// Print a name from the type section, if available. Otherwise print the type
+// normally.
+void printTypeOrName(Type type, std::ostream& o, Module* wasm) {
+ if (type.isRef() && wasm) {
+ auto heapType = type.getHeapType();
+ auto iter = wasm->typeNames.find(heapType);
+ if (iter != wasm->typeNames.end()) {
+ o << iter->second.name;
+ if (type.isNullable()) {
+ o << " null";
+ }
+ return;
+ }
+ }
+
+ // No luck with a name, just print the test as best we can.
+ o << type;
+}
+
} // anonymous namespace
// Printing "unreachable" as a instruction prefix type is not valid in wasm text
@@ -2551,7 +2570,9 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
doIndent(o, indent);
}
if (full) {
- o << "[" << expression->type << "] ";
+ o << "[";
+ printTypeOrName(expression->type, o, currModule);
+ o << "] ";
}
visit(expression);
o << maybeNewLine;
@@ -2598,7 +2619,9 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
}
stack.push_back(curr);
if (full) {
- o << "[" << curr->type << "] ";
+ o << "[";
+ printTypeOrName(curr->type, o, currModule);
+ o << "]";
}
o << '(';
printExpressionContents(curr);
@@ -3487,7 +3510,9 @@ static std::ostream& printExpression(Expression* expression,
print.currModule = wasm;
if (full || isFullForced()) {
print.setFull(true);
- o << "[" << expression->type << "] ";
+ o << "[";
+ printTypeOrName(expression->type, o, wasm);
+ o << "] ";
}
print.visit(expression);
return o;