summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-02-08 16:19:40 -0800
committerGitHub <noreply@github.com>2024-02-08 16:19:40 -0800
commit90087f0972aa34ee37860b70072cb8fc33c89ce3 (patch)
tree7a56e60f9798299135563e4c8e725ceb1dd3a773 /src
parentf5d8d30171e53c225fc640e6db8aa42973c8804c (diff)
downloadbinaryen-90087f0972aa34ee37860b70072cb8fc33c89ce3.tar.gz
binaryen-90087f0972aa34ee37860b70072cb8fc33c89ce3.tar.bz2
binaryen-90087f0972aa34ee37860b70072cb8fc33c89ce3.zip
[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.
Diffstat (limited to 'src')
-rw-r--r--src/parser/contexts.h28
-rw-r--r--src/parser/wat-parser.cpp11
2 files changed, 28 insertions, 11 deletions
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<ParseDefsCtx> {
const std::vector<HeapType>& types;
const std::unordered_map<Index, HeapType>& implicitTypes;
+ const std::unordered_map<HeapType, std::unordered_map<Name, Index>>&
+ typeNames;
const std::unordered_map<Index, Index>& implicitElemIndices;
// The index of the current module element.
@@ -1113,14 +1115,17 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
return Ok{};
}
- ParseDefsCtx(std::string_view in,
- Module& wasm,
- const std::vector<HeapType>& types,
- const std::unordered_map<Index, HeapType>& implicitTypes,
- const std::unordered_map<Index, Index>& implicitElemIndices,
- const IndexMap& typeIndices)
+ ParseDefsCtx(
+ std::string_view in,
+ Module& wasm,
+ const std::vector<HeapType>& types,
+ const std::unordered_map<Index, HeapType>& implicitTypes,
+ const std::unordered_map<HeapType, std::unordered_map<Name, Index>>&
+ typeNames,
+ const std::unordered_map<Index, Index>& 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<typename T> Result<T> withLoc(Index pos, Result<T> res) {
@@ -1192,8 +1197,13 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
}
Result<Index> 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<Index> 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<HeapType> types;
+ std::unordered_map<HeapType, std::unordered_map<Name, Index>> 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));