From 90087f0972aa34ee37860b70072cb8fc33c89ce3 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Thu, 8 Feb 2024 16:19:40 -0800 Subject: [Parser] Support references to struct fields by name (#6293) Construct a mapping from heap type and field name to field index, then use it while parsing instructions. --- src/parser/contexts.h | 28 +++++++++++++++++++--------- src/parser/wat-parser.cpp | 11 +++++++++-- 2 files changed, 28 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/parser/contexts.h b/src/parser/contexts.h index 5dd48bc59..544851859 100644 --- a/src/parser/contexts.h +++ b/src/parser/contexts.h @@ -1096,6 +1096,8 @@ struct ParseDefsCtx : TypeParserCtx { const std::vector& types; const std::unordered_map& implicitTypes; + const std::unordered_map>& + typeNames; const std::unordered_map& implicitElemIndices; // The index of the current module element. @@ -1113,14 +1115,17 @@ struct ParseDefsCtx : TypeParserCtx { return Ok{}; } - ParseDefsCtx(std::string_view in, - Module& wasm, - const std::vector& types, - const std::unordered_map& implicitTypes, - const std::unordered_map& implicitElemIndices, - const IndexMap& typeIndices) + ParseDefsCtx( + std::string_view in, + Module& wasm, + const std::vector& types, + const std::unordered_map& implicitTypes, + const std::unordered_map>& + typeNames, + const std::unordered_map& implicitElemIndices, + const IndexMap& typeIndices) : TypeParserCtx(typeIndices), in(in), wasm(wasm), builder(wasm), - types(types), implicitTypes(implicitTypes), + types(types), implicitTypes(implicitTypes), typeNames(typeNames), implicitElemIndices(implicitElemIndices), irBuilder(wasm) {} template Result withLoc(Index pos, Result res) { @@ -1192,8 +1197,13 @@ struct ParseDefsCtx : TypeParserCtx { } Result getFieldFromName(HeapType type, Name name) { - // TODO: Field names - return in.err("symbolic field names note yet supported"); + if (auto typeIt = typeNames.find(type); typeIt != typeNames.end()) { + const auto& fieldIdxs = typeIt->second; + if (auto fieldIt = fieldIdxs.find(name); fieldIt != fieldIdxs.end()) { + return fieldIt->second; + } + } + return in.err("unrecognized field name"); } Result getLocalFromIdx(uint32_t idx) { diff --git a/src/parser/wat-parser.cpp b/src/parser/wat-parser.cpp index 50a47d7b6..33066ea71 100644 --- a/src/parser/wat-parser.cpp +++ b/src/parser/wat-parser.cpp @@ -110,6 +110,7 @@ Result<> parseModule(Module& wasm, std::string_view input) { // Parse type definitions. std::vector types; + std::unordered_map> typeNames; { TypeBuilder builder(decls.subtypeDefs.size()); ParseTypeDefsCtx ctx(input, builder, *typeIndices); @@ -124,11 +125,16 @@ Result<> parseModule(Module& wasm, std::string_view input) { return ctx.in.err(decls.typeDefs[err->index].pos, msg.str()); } types = *built; - // Record type names on the module. + // Record type names on the module and in typeNames. for (size_t i = 0; i < types.size(); ++i) { auto& names = ctx.names[i]; - if (names.name.is() || names.fieldNames.size()) { + auto& fieldNames = names.fieldNames; + if (names.name.is() || fieldNames.size()) { wasm.typeNames.insert({types[i], names}); + auto& fieldIdxMap = typeNames[types[i]]; + for (auto [idx, name] : fieldNames) { + fieldIdxMap.insert({name, idx}); + } } } } @@ -167,6 +173,7 @@ Result<> parseModule(Module& wasm, std::string_view input) { wasm, types, implicitTypes, + typeNames, decls.implicitElemIndices, *typeIndices); CHECK_ERR(parseDefs(ctx, decls.tableDefs, table)); -- cgit v1.2.3