diff options
author | Alon Zakai <azakai@google.com> | 2020-12-08 18:55:20 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-08 18:55:20 -0800 |
commit | 63a042e3a94df7ba3a5c9dde03990a9813fdc366 (patch) | |
tree | 50c05727ce20615f4d0c0206e62dcd56c246766f /src/passes/Print.cpp | |
parent | 2a0059dec2fe01dcf1358e0120c32aadd2d765b6 (diff) | |
download | binaryen-63a042e3a94df7ba3a5c9dde03990a9813fdc366.tar.gz binaryen-63a042e3a94df7ba3a5c9dde03990a9813fdc366.tar.bz2 binaryen-63a042e3a94df7ba3a5c9dde03990a9813fdc366.zip |
[GC] Add basic RTT support (#3432)
This adds rtt.canon and rtt.sub together with RTT type support
that is necessary for them. Together this lets us test roundtripping the
instructions and types.
Also fixes a missing traversal over globals in collectHeapTypes,
which the example from the GC docs requires, as the RTTs are in
globals there.
This does not yet add full interpreter support and other things. It
disables initial contents on GC in the fuzzer, to avoid the fuzzer
breaking.
Renames the binary ID for exnref, which is being removed from
the spec, and which overlaps with the binary ID for rtt.
Diffstat (limited to 'src/passes/Print.cpp')
-rw-r--r-- | src/passes/Print.cpp | 97 |
1 files changed, 60 insertions, 37 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index d7691fe07..2edd069d9 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -57,29 +57,6 @@ static std::ostream& printLocal(Index index, Function* func, std::ostream& o) { return printName(name, o); } -// Unlike the default format, tuple types in s-expressions should not have -// commas. -struct SExprType { - Type type; - SExprType(Type type) : type(type){}; -}; - -static std::ostream& operator<<(std::ostream& o, const SExprType& localType) { - Type type = localType.type; - if (type.isTuple()) { - o << '('; - auto sep = ""; - for (const auto& t : type) { - o << sep << t; - sep = " "; - } - o << ')'; - } else { - o << type; - } - return o; -} - // 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. @@ -88,11 +65,6 @@ struct TypeName { TypeName(Type type) : type(type) {} }; -struct ResultTypeName { - Type type; - ResultTypeName(Type type) : type(type) {} -}; - static void printHeapTypeName(std::ostream& os, HeapType type, bool first = true); @@ -101,6 +73,15 @@ static void printTypeName(std::ostream& os, Type type) { os << type; return; } + if (type.isRtt()) { + auto rtt = type.getRtt(); + os << "rtt_"; + if (rtt.hasDepth()) { + os << rtt.depth << '_'; + } + printHeapTypeName(os, rtt.heapType); + return; + } if (type.isTuple()) { auto sep = ""; for (auto t : type) { @@ -108,7 +89,9 @@ static void printTypeName(std::ostream& os, Type type) { sep = "_"; printTypeName(os, t); } - } else if (type.isRef()) { + return; + } + if (type.isRef()) { os << "ref"; if (type.isNullable()) { os << "?"; @@ -116,9 +99,9 @@ static void printTypeName(std::ostream& os, Type type) { os << "|"; printHeapTypeName(os, type.getHeapType(), false); os << "|"; - } else { - WASM_UNREACHABLE("unsupported print type"); + return; } + WASM_UNREACHABLE("unsupported print type"); } static void printHeapTypeName(std::ostream& os, HeapType type, bool first) { @@ -164,6 +147,37 @@ static void printHeapTypeName(std::ostream& os, HeapType type, bool first) { } } +// Unlike the default format, tuple types in s-expressions should not have +// commas. +struct SExprType { + Type type; + SExprType(Type type) : type(type){}; +}; + +static std::ostream& operator<<(std::ostream& o, const SExprType& localType) { + Type type = localType.type; + if (type.isTuple()) { + o << '('; + auto sep = ""; + for (const auto& t : type) { + o << sep << t; + sep = " "; + } + o << ')'; + } else if (type.isRtt()) { + auto rtt = type.getRtt(); + o << "(rtt "; + if (rtt.hasDepth()) { + o << rtt.depth << ' '; + } + printHeapTypeName(o, rtt.heapType); + o << ')'; + } else { + printTypeName(o, localType.type); + } + return o; +} + std::ostream& operator<<(std::ostream& os, TypeName typeName) { auto type = typeName.type; if (type.isRef() && !type.isBasic()) { @@ -178,6 +192,13 @@ std::ostream& operator<<(std::ostream& os, TypeName typeName) { return os << SExprType(typeName.type); } +// TODO: try to simplify or even remove this, as we may be able to do the same +// things with SExprType +struct ResultTypeName { + Type type; + ResultTypeName(Type type) : type(type) {} +}; + std::ostream& operator<<(std::ostream& os, ResultTypeName typeName) { auto type = typeName.type; os << "(result "; @@ -1673,12 +1694,12 @@ struct PrintExpressionContents WASM_UNREACHABLE("TODO (gc): br_on_cast"); } void visitRttCanon(RttCanon* curr) { - printMedium(o, "rtt.canon"); - WASM_UNREACHABLE("TODO (gc): rtt.canon"); + printMedium(o, "rtt.canon "); + printHeapTypeName(o, curr->type.getRtt().heapType); } void visitRttSub(RttSub* curr) { - printMedium(o, "rtt.sub"); - WASM_UNREACHABLE("TODO (gc): rtt.sub"); + printMedium(o, "rtt.sub "); + printHeapTypeName(o, curr->type.getRtt().heapType); } void visitStructNew(StructNew* curr) { WASM_UNREACHABLE("TODO (gc): struct.new"); @@ -2358,12 +2379,14 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { void visitRttCanon(RttCanon* curr) { o << '('; PrintExpressionContents(currFunction, o).visit(curr); - WASM_UNREACHABLE("TODO (gc): rtt.canon"); + o << ')'; } void visitRttSub(RttSub* curr) { o << '('; PrintExpressionContents(currFunction, o).visit(curr); - WASM_UNREACHABLE("TODO (gc): rtt.sub"); + incIndent(); + printFullLine(curr->parent); + decIndent(); } void visitStructNew(StructNew* curr) { o << '('; |