diff options
-rw-r--r-- | src/passes/Print.cpp | 54 | ||||
-rw-r--r-- | test/heap-types.wast.from-wast | 10 |
2 files changed, 51 insertions, 13 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index db7704c3f..7a160a8eb 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -244,6 +244,27 @@ std::ostream& printResultTypeName(std::ostream& os, return os; } +// Generic processing of a struct's field, given an optional module. Calls func +// with the field name, if it is present, or with a null Name if not. +template<typename T> +void processFieldName(Module* wasm, HeapType type, Index index, T func) { + if (wasm) { + auto it = wasm->typeNames.find(type); + if (it != wasm->typeNames.end()) { + auto& fieldNames = it->second.fieldNames; + auto it = fieldNames.find(index); + if (it != fieldNames.end()) { + auto name = it->second; + if (name.is()) { + func(it->second); + return; + } + } + } + } + func(Name()); +} + } // anonymous namespace // Printing "unreachable" as a instruction prefix type is not valid in wasm text @@ -1894,13 +1915,22 @@ struct PrintExpressionContents // instruction is never reached anyhow. printMedium(o, "block "); } + void printFieldName(HeapType type, Index index) { + processFieldName(wasm, type, index, [&](Name name) { + if (name.is()) { + o << '$' << name; + } else { + o << index; + } + }); + } void visitStructGet(StructGet* curr) { if (curr->ref->type == Type::unreachable) { printUnreachableReplacement(); return; } - const auto& field = - curr->ref->type.getHeapType().getStruct().fields[curr->index]; + auto heapType = curr->ref->type.getHeapType(); + const auto& field = heapType.getStruct().fields[curr->index]; if (field.type == Type::i32 && field.packedType != Field::not_packed) { if (curr->signed_) { printMedium(o, "struct.get_s "); @@ -1910,9 +1940,9 @@ struct PrintExpressionContents } else { printMedium(o, "struct.get "); } - printHeapTypeName(o, curr->ref->type.getHeapType(), wasm); + printHeapTypeName(o, heapType, wasm); o << ' '; - o << curr->index; + printFieldName(heapType, curr->index); } void visitStructSet(StructSet* curr) { if (curr->ref->type == Type::unreachable) { @@ -1920,9 +1950,10 @@ struct PrintExpressionContents return; } printMedium(o, "struct.set "); - printHeapTypeName(o, curr->ref->type.getHeapType(), wasm); + auto heapType = curr->ref->type.getHeapType(); + printHeapTypeName(o, heapType, wasm); o << ' '; - o << curr->index; + printFieldName(heapType, curr->index); } void visitArrayNew(ArrayNew* curr) { printMedium(o, "array.new_"); @@ -2839,11 +2870,18 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { o << ')'; } void handleStruct(const Struct& curr) { + auto type = HeapType(curr); + const auto& fields = curr.fields; o << "(struct "; auto sep = ""; - for (auto field : curr.fields) { + for (Index i = 0; i < fields.size(); i++) { o << sep << "(field "; - handleFieldBody(field); + processFieldName(currModule, type, i, [&](Name name) { + if (name.is()) { + o << '$' << name << ' '; + } + }); + handleFieldBody(fields[i]); o << ')'; sep = " "; } diff --git a/test/heap-types.wast.from-wast b/test/heap-types.wast.from-wast index ca45eb0af..ceef0739a 100644 --- a/test/heap-types.wast.from-wast +++ b/test/heap-types.wast.from-wast @@ -1,11 +1,11 @@ (module - (type $struct.A (struct (field i32) (field f32) (field f64))) + (type $struct.A (struct (field i32) (field f32) (field $named f64))) (type $struct.B (struct (field i8) (field (mut i16)) (field (ref null $struct.A)) (field (mut (ref null $struct.A))))) (type $matrix (array (ref null $vector))) (type $vector (array (mut f64))) (type $anyref_=>_none (func (param anyref))) (type $parent (struct )) - (type $struct.C (struct (field (mut f32)))) + (type $struct.C (struct (field $named-mut (mut f32)))) (type $none_=>_none (func)) (type $rtt_1_$parent_=>_none (func (param (rtt 1 $parent)))) (type $rtt_$parent_=>_none (func (param (rtt $parent)))) @@ -42,12 +42,12 @@ ) ) (drop - (struct.get $struct.A 2 + (struct.get $struct.A $named (local.get $x) ) ) (drop - (struct.get $struct.A 2 + (struct.get $struct.A $named (local.get $x) ) ) @@ -88,7 +88,7 @@ (i32.const 1) ) ) - (struct.set $struct.C 0 + (struct.set $struct.C $named-mut (ref.null $struct.C) (f32.const 100) ) |