diff options
author | Alon Zakai <azakai@google.com> | 2020-12-01 17:36:13 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-01 17:36:13 -0800 |
commit | a5403d47483836d5e2d53c3f3721bd376551624a (patch) | |
tree | 888d259d6296eda35d0d8e12b080b2a602ece012 /src/passes/Print.cpp | |
parent | 16d44ff96be46e03690fc4853b2b1312e3a543ce (diff) | |
download | binaryen-a5403d47483836d5e2d53c3f3721bd376551624a.tar.gz binaryen-a5403d47483836d5e2d53c3f3721bd376551624a.tar.bz2 binaryen-a5403d47483836d5e2d53c3f3721bd376551624a.zip |
[Printing] Print type names where possible. (#3410)
For a nested type, we used to print e.g.
(param $x (ref (func (param i32))))
Instead of expanding the full type inline, which can get long for
a deeply nested type, print a name when running the Print pass.
In this example that would be something like
(param $x (ref $i32_=>_none))
Diffstat (limited to 'src/passes/Print.cpp')
-rw-r--r-- | src/passes/Print.cpp | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 33b34904e..c597028be 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -133,6 +133,33 @@ std::ostream& operator<<(std::ostream& os, SigName sigName) { return os; } +// Wrapper for printing a type when we try to print the type name as much as +// possible. For example, for a signature we will print the signature's name, +// not its contents. +struct TypeName { + Type type; + TypeName(Type type) : type(type) {} +}; + +std::ostream& operator<<(std::ostream& os, TypeName typeName) { + auto type = typeName.type; + if (type.isRef() && !type.isBasic()) { + os << "(ref "; + if (type.isNullable()) { + os << "null "; + } + auto heapType = type.getHeapType(); + if (heapType.isSignature()) { + os << SigName(heapType.getSignature()); + } else { + os << heapType; + } + os << ')'; + return os; + } + return os << SExprType(typeName.type); +} + } // anonymous namespace // Printing "unreachable" as a instruction prefix type is not valid in wasm text @@ -2328,11 +2355,23 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { } if (curr.params.size() > 0) { o << maybeSpace; - o << ParamType(curr.params); + o << "(param "; + auto sep = ""; + for (auto type : curr.params) { + o << sep << TypeName(type); + sep = " "; + } + o << ')'; } if (curr.results.size() > 0) { o << maybeSpace; - o << ResultType(curr.results); + o << "(result "; + auto sep = ""; + for (auto type : curr.results) { + o << sep << TypeName(type); + sep = " "; + } + o << ')'; } o << ")"; } @@ -2438,7 +2477,7 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { o << '('; printMinor(o, "param "); printLocal(i, currFunction, o); - o << ' ' << param << ')'; + o << ' ' << TypeName(param) << ')'; ++i; } } @@ -2452,7 +2491,7 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { o << '('; printMinor(o, "local "); printLocal(i, currFunction, o) - << ' ' << SExprType(curr->getLocalType(i)) << ')'; + << ' ' << TypeName(curr->getLocalType(i)) << ')'; o << maybeNewLine; } // Print the body. |