summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2023-01-03 13:37:19 -0600
committerGitHub <noreply@github.com>2023-01-03 13:37:19 -0600
commitf36229b509ddb59e7ab20cd6fd3ccd962e9990a5 (patch)
tree474cf6f53423aec6e61b42652bb7f8fc6262a9d6
parentfe01812696a8108bcd7103a7a09573c99743fa04 (diff)
downloadbinaryen-f36229b509ddb59e7ab20cd6fd3ccd962e9990a5.tar.gz
binaryen-f36229b509ddb59e7ab20cd6fd3ccd962e9990a5.tar.bz2
binaryen-f36229b509ddb59e7ab20cd6fd3ccd962e9990a5.zip
[NFC][Parser] Track definition indices (#5372)
* [NFC][Parser] Track definition indices For each definition in a module, record that definition's index in the relevant index space. Previously the index was inferred from its position in a list of module definitions, but that scheme does not scale to data segments defined inline inside memory definitions because these data segments occupy a slot in the data segment index space but do not have their own independent definitions. * clarify comment
-rw-r--r--src/wasm/wat-parser.cpp30
1 files changed, 17 insertions, 13 deletions
diff --git a/src/wasm/wat-parser.cpp b/src/wasm/wat-parser.cpp
index c3fd2bc4b..7914c327e 100644
--- a/src/wasm/wat-parser.cpp
+++ b/src/wasm/wat-parser.cpp
@@ -345,10 +345,12 @@ struct ParseInput {
// Utilities
// =========
-// The location and possible name of a module-level definition in the input.
+// The location, possible name, and index in the respective module index space
+// of a module-level definition in the input.
struct DefPos {
Name name;
Index pos;
+ Index index;
};
struct GlobalType {
@@ -427,10 +429,10 @@ Result<> addExports(ParseInput& in,
Result<IndexMap> createIndexMap(ParseInput& in,
const std::vector<DefPos>& defs) {
IndexMap indices;
- for (Index i = 0; i < defs.size(); ++i) {
- if (defs[i].name.is()) {
- if (!indices.insert({defs[i].name, i}).second) {
- return in.err(defs[i].pos, "duplicate element name");
+ for (auto& def : defs) {
+ if (def.name.is()) {
+ if (!indices.insert({def.name, def.index}).second) {
+ return in.err(def.pos, "duplicate element name");
}
}
}
@@ -450,9 +452,9 @@ template<typename Ctx>
Result<> parseDefs(Ctx& ctx,
const std::vector<DefPos>& defs,
MaybeResult<> (*parser)(Ctx&)) {
- for (Index i = 0; i < defs.size(); ++i) {
- ctx.index = i;
- WithPosition with(ctx, defs[i].pos);
+ for (auto& def : defs) {
+ ctx.index = def.index;
+ WithPosition with(ctx, def.pos);
auto parsed = parser(ctx);
CHECK_ERR(parsed);
assert(parsed);
@@ -801,11 +803,13 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
void addArrayType(ArrayT) {}
Result<> addSubtype(Index) { return Ok{}; }
void finishSubtype(Name name, Index pos) {
- subtypeDefs.push_back({name, pos});
+ subtypeDefs.push_back({name, pos, Index(subtypeDefs.size())});
}
size_t getRecGroupStartIndex() { return 0; }
void addRecGroup(Index, size_t) {}
- void finishDeftype(Index pos) { typeDefs.push_back({{}, pos}); }
+ void finishDeftype(Index pos) {
+ typeDefs.push_back({{}, pos, Index(typeDefs.size())});
+ }
Result<TypeUseT>
makeTypeUse(Index pos, std::optional<HeapTypeT> type, ParamsT*, ResultsT*) {
@@ -847,7 +851,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
auto f = addFuncDecl(pos, name, import);
CHECK_ERR(f);
CHECK_ERR(addExports(in, wasm, *f, exports, ExternalKind::Function));
- funcDefs.push_back({name, pos});
+ funcDefs.push_back({name, pos, Index(funcDefs.size())});
return Ok{};
}
@@ -881,7 +885,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
auto m = addMemoryDecl(pos, name, import);
CHECK_ERR(m);
CHECK_ERR(addExports(in, wasm, *m, exports, ExternalKind::Memory));
- memoryDefs.push_back({name, pos});
+ memoryDefs.push_back({name, pos, Index(memoryDefs.size())});
return Ok{};
}
@@ -916,7 +920,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
auto g = addGlobalDecl(pos, name, import);
CHECK_ERR(g);
CHECK_ERR(addExports(in, wasm, *g, exports, ExternalKind::Global));
- globalDefs.push_back({name, pos});
+ globalDefs.push_back({name, pos, Index(globalDefs.size())});
return Ok{};
}
};