diff options
author | Alon Zakai <azakai@google.com> | 2020-12-03 16:40:56 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-03 16:40:56 -0800 |
commit | bd9872ddf850bf177298a5274a15807e6227cd3d (patch) | |
tree | 7336966d47bf4b54610e62a835e06f4ec29ceddc /src/passes/Print.cpp | |
parent | d6444b5032a64f3abe35bf60eb96e151669d2fa5 (diff) | |
download | binaryen-bd9872ddf850bf177298a5274a15807e6227cd3d.tar.gz binaryen-bd9872ddf850bf177298a5274a15807e6227cd3d.tar.bz2 binaryen-bd9872ddf850bf177298a5274a15807e6227cd3d.zip |
[Types] Refactor signature collection to heap type collection. NFC. (#3420)
This will allow writing GC types in the future, which are non-signature
heap types.
To allow this PR to work, it adds operator< for HeapType so that it
can be used in the data structures that collect uses.
Drive-by fix of a weird hack with sending a Name* in Print.
Diffstat (limited to 'src/passes/Print.cpp')
-rw-r--r-- | src/passes/Print.cpp | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index c597028be..ba3981e21 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -141,6 +141,11 @@ struct TypeName { TypeName(Type type) : type(type) {} }; +struct HeapTypeName { + HeapType type; + HeapTypeName(HeapType type) : type(type) {} +}; + std::ostream& operator<<(std::ostream& os, TypeName typeName) { auto type = typeName.type; if (type.isRef() && !type.isBasic()) { @@ -160,6 +165,16 @@ std::ostream& operator<<(std::ostream& os, TypeName typeName) { return os << SExprType(typeName.type); } +std::ostream& operator<<(std::ostream& os, HeapTypeName typeName) { + auto type = typeName.type; + if (type.isSignature()) { + os << SigName(type.getSignature()); + } else { + os << type; + } + return os; +} + } // anonymous namespace // Printing "unreachable" as a instruction prefix type is not valid in wasm text @@ -2348,10 +2363,10 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { WASM_UNREACHABLE("TODO (gc): array.len"); } // Module-level visitors - void handleSignature(Signature curr, Name* funcName = nullptr) { + void handleSignature(Signature curr, Name name = Name()) { o << "(func"; - if (funcName) { - o << " $" << *funcName; + if (name.is()) { + o << " $" << name; } if (curr.params.size() > 0) { o << maybeSpace; @@ -2375,6 +2390,13 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { } o << ")"; } + void handleHeapType(HeapType type) { + if (type.isSignature()) { + handleSignature(type.getSignature()); + } else { + WASM_UNREACHABLE("unsupported heap type"); + } + } void visitExport(Export* curr) { o << '('; printMedium(o, "export "); @@ -2453,7 +2475,7 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { lastPrintedLocation = {0, 0, 0}; o << '('; emitImportHeader(curr); - handleSignature(curr->sig, &curr->name); + handleSignature(curr->sig, curr->name); o << ')'; o << maybeNewLine; } @@ -2702,15 +2724,15 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { printName(curr->name, o); } incIndent(); - std::vector<Signature> signatures; - std::unordered_map<Signature, Index> indices; - ModuleUtils::collectSignatures(*curr, signatures, indices); - for (auto sig : signatures) { + std::vector<HeapType> types; + std::unordered_map<HeapType, Index> indices; + ModuleUtils::collectHeapTypes(*curr, types, indices); + for (auto type : types) { doIndent(o, indent); o << '('; printMedium(o, "type") << ' '; - o << SigName(sig) << ' '; - handleSignature(sig); + o << HeapTypeName(type) << ' '; + handleHeapType(type); o << ")" << maybeNewLine; } ModuleUtils::iterImportedMemories( |