diff options
Diffstat (limited to 'src/ir')
-rw-r--r-- | src/ir/global-utils.h | 9 | ||||
-rw-r--r-- | src/ir/module-utils.h | 23 |
2 files changed, 30 insertions, 2 deletions
diff --git a/src/ir/global-utils.h b/src/ir/global-utils.h index e096aec8c..b575d6952 100644 --- a/src/ir/global-utils.h +++ b/src/ir/global-utils.h @@ -54,6 +54,15 @@ getGlobalInitializedToImport(Module& wasm, Name module, Name base) { } inline bool canInitializeGlobal(const Expression* curr) { + if (auto* tuple = curr->dynCast<TupleMake>()) { + for (auto* op : tuple->operands) { + if (!op->is<Const>() && !op->is<RefNull>() && + !op->is<RefFunc>() & !op->is<GlobalGet>()) { + return false; + } + } + return true; + } return curr->is<Const>() || curr->is<RefNull>() || curr->is<RefFunc>() || curr->is<GlobalGet>(); } diff --git a/src/ir/module-utils.h b/src/ir/module-utils.h index be924f742..485bc4833 100644 --- a/src/ir/module-utils.h +++ b/src/ir/module-utils.h @@ -35,8 +35,8 @@ namespace ModuleUtils { // just use them directly). struct BinaryIndexes { std::unordered_map<Name, Index> functionIndexes; - std::unordered_map<Name, Index> globalIndexes; std::unordered_map<Name, Index> eventIndexes; + std::unordered_map<Name, Index> globalIndexes; BinaryIndexes(Module& wasm) { auto addIndexes = [&](auto& source, auto& indexes) { @@ -56,8 +56,27 @@ struct BinaryIndexes { } }; addIndexes(wasm.functions, functionIndexes); - addIndexes(wasm.globals, globalIndexes); addIndexes(wasm.events, eventIndexes); + + // Globals may have tuple types in the IR, in which case they lower to + // multiple globals, one for each tuple element, in the binary. Tuple + // globals therefore occupy multiple binary indices, and we have to take + // that into account when calculating indices. + Index globalCount = 0; + auto addGlobal = [&](auto* curr) { + globalIndexes[curr->name] = globalCount; + globalCount += curr->type.size(); + }; + for (auto& curr : wasm.globals) { + if (curr->imported()) { + addGlobal(curr.get()); + } + } + for (auto& curr : wasm.globals) { + if (!curr->imported()) { + addGlobal(curr.get()); + } + } } }; |