From 103fa3250c46b0c4cf04573c5e075185ca574016 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 17 Jan 2024 11:29:19 -0800 Subject: [WebAssembly] Use ValType instead of integer types to model wasm tables (#78012) LLVM models some features found in the binary format with raw integers and others with nested or enumerated types. This PR switches modeling of tables and segments to use wasm::ValType rather than uint32_t. This NFC change is in preparation for modeling more reference types, but IMO is also cleaner and closer to the spec. --- llvm/lib/Object/WasmObjectFile.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'llvm/lib/Object/WasmObjectFile.cpp') diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index 94cd969..b9a8e97 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -258,7 +258,7 @@ static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) { static wasm::WasmTableType readTableType(WasmObjectFile::ReadContext &Ctx) { wasm::WasmTableType TableType; - TableType.ElemType = readUint8(Ctx); + TableType.ElemType = wasm::ValType(readVaruint32(Ctx)); TableType.Limits = readLimits(Ctx); return TableType; } @@ -1163,8 +1163,8 @@ Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { Im.Table = readTableType(Ctx); NumImportedTables++; auto ElemType = Im.Table.ElemType; - if (ElemType != wasm::WASM_TYPE_FUNCREF && - ElemType != wasm::WASM_TYPE_EXTERNREF) + if (ElemType != wasm::ValType::FUNCREF && + ElemType != wasm::ValType::EXTERNREF) return make_error("invalid table element type", object_error::parse_failed); break; @@ -1220,8 +1220,8 @@ Error WasmObjectFile::parseTableSection(ReadContext &Ctx) { T.Index = NumImportedTables + Tables.size(); Tables.push_back(T); auto ElemType = Tables.back().Type.ElemType; - if (ElemType != wasm::WASM_TYPE_FUNCREF && - ElemType != wasm::WASM_TYPE_EXTERNREF) { + if (ElemType != wasm::ValType::FUNCREF && + ElemType != wasm::ValType::EXTERNREF) { return make_error("invalid table element type", object_error::parse_failed); } @@ -1534,21 +1534,22 @@ Error WasmObjectFile::parseElemSection(ReadContext &Ctx) { } if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND) { - Segment.ElemKind = readUint8(Ctx); + auto ElemKind = readVaruint32(Ctx); if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS) { - if (Segment.ElemKind != uint8_t(wasm::ValType::FUNCREF) && - Segment.ElemKind != uint8_t(wasm::ValType::EXTERNREF)) { + Segment.ElemKind = wasm::ValType(ElemKind); + if (Segment.ElemKind != wasm::ValType::FUNCREF && + Segment.ElemKind != wasm::ValType::EXTERNREF) { return make_error("invalid reference type", object_error::parse_failed); } } else { - if (Segment.ElemKind != 0) + if (ElemKind != 0) return make_error("invalid elemtype", object_error::parse_failed); - Segment.ElemKind = uint8_t(wasm::ValType::FUNCREF); + Segment.ElemKind = wasm::ValType::FUNCREF; } } else { - Segment.ElemKind = uint8_t(wasm::ValType::FUNCREF); + Segment.ElemKind = wasm::ValType::FUNCREF; } if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS) -- cgit v1.1