diff options
Diffstat (limited to 'src/wasm')
-rw-r--r-- | src/wasm/wasm-binary.cpp | 51 | ||||
-rw-r--r-- | src/wasm/wasm-type.cpp | 161 |
2 files changed, 116 insertions, 96 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 2abb32837..8565ddaad 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -291,30 +291,37 @@ void WasmBinaryWriter::writeTypes() { if (type.isShared()) { o << S32LEB(BinaryConsts::EncodedType::Shared); } - if (type.isSignature()) { - o << S32LEB(BinaryConsts::EncodedType::Func); - auto sig = type.getSignature(); - for (auto& sigType : {sig.params, sig.results}) { - o << U32LEB(sigType.size()); - for (const auto& type : sigType) { - writeType(type); + switch (type.getKind()) { + case HeapTypeKind::Func: { + o << S32LEB(BinaryConsts::EncodedType::Func); + auto sig = type.getSignature(); + for (auto& sigType : {sig.params, sig.results}) { + o << U32LEB(sigType.size()); + for (const auto& type : sigType) { + writeType(type); + } } + break; } - } else if (type.isContinuation()) { - o << S32LEB(BinaryConsts::EncodedType::Cont); - writeHeapType(type.getContinuation().type); - } else if (type.isStruct()) { - o << S32LEB(BinaryConsts::EncodedType::Struct); - auto fields = type.getStruct().fields; - o << U32LEB(fields.size()); - for (const auto& field : fields) { - writeField(field); - } - } else if (type.isArray()) { - o << S32LEB(BinaryConsts::EncodedType::Array); - writeField(type.getArray().element); - } else { - WASM_UNREACHABLE("TODO GC type writing"); + case HeapTypeKind::Struct: { + o << S32LEB(BinaryConsts::EncodedType::Struct); + auto fields = type.getStruct().fields; + o << U32LEB(fields.size()); + for (const auto& field : fields) { + writeField(field); + } + break; + } + case HeapTypeKind::Array: + o << S32LEB(BinaryConsts::EncodedType::Array); + writeField(type.getArray().element); + break; + case HeapTypeKind::Cont: + o << S32LEB(BinaryConsts::EncodedType::Cont); + writeHeapType(type.getContinuation().type); + break; + case HeapTypeKind::Basic: + WASM_UNREACHABLE("unexpected kind"); } } finishSection(start); diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp index 34299a9a1..84ae51521 100644 --- a/src/wasm/wasm-type.cpp +++ b/src/wasm/wasm-type.cpp @@ -1233,42 +1233,46 @@ size_t HeapType::getDepth() const { // In addition to the explicit supertypes we just traversed over, there is // implicit supertyping wrt basic types. A signature type always has one more // super, HeapType::func, etc. - if (!isBasic()) { - if (isFunction() || isContinuation()) { - depth++; - } else if (isStruct()) { + switch (getKind()) { + case HeapTypeKind::Basic: + // Some basic types have supers. + switch (getBasic(Unshared)) { + case HeapType::ext: + case HeapType::func: + case HeapType::cont: + case HeapType::any: + case HeapType::exn: + break; + case HeapType::eq: + depth++; + break; + case HeapType::i31: + case HeapType::struct_: + case HeapType::array: + case HeapType::string: + depth += 2; + break; + case HeapType::none: + case HeapType::nofunc: + case HeapType::nocont: + case HeapType::noext: + case HeapType::noexn: + // Bottom types are infinitely deep. + depth = size_t(-1l); + } + break; + case HeapTypeKind::Func: + case HeapTypeKind::Cont: + ++depth; + break; + case HeapTypeKind::Struct: // specific struct types <: struct <: eq <: any depth += 3; - } else if (isArray()) { + break; + case HeapTypeKind::Array: // specific array types <: array <: eq <: any depth += 3; - } - } else { - // Some basic types have supers. - switch (getBasic(Unshared)) { - case HeapType::ext: - case HeapType::func: - case HeapType::cont: - case HeapType::any: - case HeapType::exn: - break; - case HeapType::eq: - depth++; - break; - case HeapType::i31: - case HeapType::struct_: - case HeapType::array: - case HeapType::string: - depth += 2; - break; - case HeapType::none: - case HeapType::nofunc: - case HeapType::nocont: - case HeapType::noext: - case HeapType::noexn: - // Bottom types are infinitely deep. - depth = size_t(-1l); - } + break; } return depth; } @@ -1353,31 +1357,30 @@ bool HeapType::isSubType(HeapType left, HeapType right) { } std::vector<Type> HeapType::getTypeChildren() const { - if (isBasic()) { - return {}; - } - if (isStruct()) { - std::vector<Type> children; - for (auto& field : getStruct().fields) { - children.push_back(field.type); + switch (getKind()) { + case HeapTypeKind::Basic: + return {}; + case HeapTypeKind::Func: { + std::vector<Type> children; + auto sig = getSignature(); + for (auto tuple : {sig.params, sig.results}) { + for (auto t : tuple) { + children.push_back(t); + } + } + return children; } - return children; - } - if (isArray()) { - return {getArray().element.type}; - } - if (isSignature()) { - std::vector<Type> children; - auto sig = getSignature(); - for (auto tuple : {sig.params, sig.results}) { - for (auto t : tuple) { - children.push_back(t); + case HeapTypeKind::Struct: { + std::vector<Type> children; + for (auto& field : getStruct().fields) { + children.push_back(field.type); } + return children; } - return children; - } - if (isContinuation()) { - return {}; + case HeapTypeKind::Array: + return {getArray().element.type}; + case HeapTypeKind::Cont: + return {}; } WASM_UNREACHABLE("unexpected kind"); } @@ -1586,16 +1589,21 @@ TypeNames DefaultTypeNameGenerator::getNames(HeapType type) { if (inserted) { // Generate a new name for this type we have not previously seen. std::stringstream stream; - if (type.isSignature()) { - stream << "func." << funcCount++; - } else if (type.isContinuation()) { - stream << "cont." << contCount++; - } else if (type.isStruct()) { - stream << "struct." << structCount++; - } else if (type.isArray()) { - stream << "array." << arrayCount++; - } else { - WASM_UNREACHABLE("unexpected kind"); + switch (type.getKind()) { + case HeapTypeKind::Func: + stream << "func." << funcCount++; + break; + case HeapTypeKind::Struct: + stream << "struct." << structCount++; + break; + case HeapTypeKind::Array: + stream << "array." << arrayCount++; + break; + case HeapTypeKind::Cont: + stream << "cont." << contCount++; + break; + case HeapTypeKind::Basic: + WASM_UNREACHABLE("unexpected kind"); } it->second = {stream.str(), {}}; } @@ -1972,16 +1980,21 @@ std::ostream& TypePrinter::print(HeapType type) { if (type.isShared()) { os << "(shared "; } - if (type.isSignature()) { - print(type.getSignature()); - } else if (type.isContinuation()) { - print(type.getContinuation()); - } else if (type.isStruct()) { - print(type.getStruct(), names.fieldNames); - } else if (type.isArray()) { - print(type.getArray()); - } else { - WASM_UNREACHABLE("unexpected type"); + switch (type.getKind()) { + case HeapTypeKind::Func: + print(type.getSignature()); + break; + case HeapTypeKind::Struct: + print(type.getStruct(), names.fieldNames); + break; + case HeapTypeKind::Array: + print(type.getArray()); + break; + case HeapTypeKind::Cont: + print(type.getContinuation()); + break; + case HeapTypeKind::Basic: + WASM_UNREACHABLE("unexpected kind"); } if (type.isShared()) { os << ')'; |