diff options
author | Congcong Cai <congcongcai0907@163.com> | 2022-08-26 03:23:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-25 19:23:42 +0000 |
commit | 37b457a00a46e5604b2a8a388efaf0813b3d1170 (patch) | |
tree | 12e5816490069d3a64d8d442a86659636590ac01 /src/wasm/wasm-binary.cpp | |
parent | 5c035932d7b98b69113a37bbdd90932c818f660f (diff) | |
download | binaryen-37b457a00a46e5604b2a8a388efaf0813b3d1170.tar.gz binaryen-37b457a00a46e5604b2a8a388efaf0813b3d1170.tar.bz2 binaryen-37b457a00a46e5604b2a8a388efaf0813b3d1170.zip |
Fix order of local names in Names section (#4964)
The wasm spec requires the order of local names in that section to be in
increasing order:
https://webassembly.github.io/spec/core/appendix/custom.html#binary-namemap
Diffstat (limited to 'src/wasm/wasm-binary.cpp')
-rw-r--r-- | src/wasm/wasm-binary.cpp | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 87fb0e293..0a5933baf 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -857,28 +857,30 @@ void WasmBinaryWriter::writeNames() { // Pairs of (local index in IR, name). std::vector<std::pair<Index, Name>> localsWithNames; auto numLocals = func->getNumLocals(); - for (Index i = 0; i < numLocals; ++i) { - if (func->hasLocalName(i)) { - localsWithNames.push_back({i, func->getLocalName(i)}); + for (Index indexInFunc = 0; indexInFunc < numLocals; ++indexInFunc) { + if (func->hasLocalName(indexInFunc)) { + Index indexInBinary; + auto iter = funcMappedLocals.find(func->name); + if (iter != funcMappedLocals.end()) { + // TODO: handle multivalue + indexInBinary = iter->second[{indexInFunc, 0}]; + } else { + // No data on funcMappedLocals. That is only possible if we are an + // imported function, where there are no locals to map, and in + // that case the index is unchanged anyhow: parameters always have + // the same index, they are not mapped in any way. + assert(func->imported()); + indexInBinary = indexInFunc; + } + localsWithNames.push_back( + {indexInBinary, func->getLocalName(indexInFunc)}); } } assert(localsWithNames.size()); + std::sort(localsWithNames.begin(), localsWithNames.end()); o << U32LEB(index); o << U32LEB(localsWithNames.size()); - for (auto& [indexInFunc, name] : localsWithNames) { - // TODO: handle multivalue - Index indexInBinary; - auto iter = funcMappedLocals.find(func->name); - if (iter != funcMappedLocals.end()) { - indexInBinary = iter->second[{indexInFunc, 0}]; - } else { - // No data on funcMappedLocals. That is only possible if we are an - // imported function, where there are no locals to map, and in that - // case the index is unchanged anyhow: parameters always have the - // same index, they are not mapped in any way. - assert(func->imported()); - indexInBinary = indexInFunc; - } + for (auto& [indexInBinary, name] : localsWithNames) { o << U32LEB(indexInBinary); writeEscapedName(name.str); } |