From 82bd2c4e5717cbe19a1a6c34cfdd5116b13a68dc Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 18 Jan 2023 13:31:26 -0600 Subject: Add a TypeNameGenerator that uses names from a Module (#5437) If the module does not have a name for a particular type, the new utility falls back to use a different user-configurable type name generator, just like the existing IndexedTypeNameGenerator does. Also change how heap types are printed by this printing machinery (which is currently only used for debugging) so that their names are printed in addition to their contents. This makes the printer much more useful for debugging. --- src/wasm-type-printing.h | 28 ++++++++++++++++++++++++++++ src/wasm/wasm-type.cpp | 6 +++++- 2 files changed, 33 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/wasm-type-printing.h b/src/wasm-type-printing.h index 685d488fd..3a8740350 100644 --- a/src/wasm-type-printing.h +++ b/src/wasm-type-printing.h @@ -24,6 +24,7 @@ #include "support/name.h" #include "support/utilities.h" #include "wasm-type.h" +#include "wasm.h" namespace wasm { @@ -93,6 +94,33 @@ struct IndexedTypeNameGenerator } }; +// Prints heap types stored in a module, falling back to the given +// FallbackGenerator if the module does not have a name for type type. +template +struct ModuleTypeNameGenerator + : TypeNameGeneratorBase> { + const Module& wasm; + DefaultTypeNameGenerator defaultGenerator; + FallbackGenerator& fallback; + + ModuleTypeNameGenerator(const Module& wasm, FallbackGenerator& fallback) + : wasm(wasm), fallback(fallback) {} + + // TODO: Use C++20 `requires` to clean this up. + template + ModuleTypeNameGenerator( + const Module& wasm, + std::enable_if_t>* = nullptr) + : ModuleTypeNameGenerator(wasm, defaultGenerator) {} + + TypeNames getNames(HeapType type) { + if (auto it = wasm.typeNames.find(type); it != wasm.typeNames.end()) { + return it->second; + } + return fallback.getNames(type); + } +}; + } // namespace wasm #endif // wasm_wasm_type_printing_h diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp index 64fd07759..e5493c8d1 100644 --- a/src/wasm/wasm-type.cpp +++ b/src/wasm/wasm-type.cpp @@ -1867,6 +1867,10 @@ std::ostream& TypePrinter::print(HeapType type) { } } + os << "(type "; + printHeapTypeName(type); + os << " "; + if (isTemp(type)) { os << "(; temp ;) "; } @@ -1885,7 +1889,7 @@ std::ostream& TypePrinter::print(HeapType type) { } else { WASM_UNREACHABLE("unexpected type"); } - return os; + return os << ")"; } std::ostream& TypePrinter::print(const Tuple& tuple) { -- cgit v1.2.3