diff options
author | Alon Zakai <azakai@google.com> | 2023-11-30 15:16:52 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-30 15:16:52 -0800 |
commit | 42cddbf88ebe74d17490e2dd13826e6da9104b15 (patch) | |
tree | c0712b1baf3d03f62bd1d6f5372e2ccb6ebdcf69 /src/wasm/wasm.cpp | |
parent | a191d66fce93829e7365f7f2f4be2799c62a1ee4 (diff) | |
download | binaryen-42cddbf88ebe74d17490e2dd13826e6da9104b15.tar.gz binaryen-42cddbf88ebe74d17490e2dd13826e6da9104b15.tar.bz2 binaryen-42cddbf88ebe74d17490e2dd13826e6da9104b15.zip |
wasm-metadce all the things (#6142)
Remove hardcoded paths for globals/functions/etc. in favor of general code
paths that support all the module elements uniformly. As a result of that, we
now support all parts of wasm, such as tables and element segments, that
we didn't before.
This refactoring is NFC aside from adding functionality. Note that this reduces
the size of wasm-metadce by 10% while increasing its functionality - the
benefits of writing generic code.
To support this, add some trivial generic helpers to get or iterate over module
elements using their kind in a dynamic manner. Using them might make
wasm-metadce slightly slower, but I can't measure any difference.
Diffstat (limited to 'src/wasm/wasm.cpp')
-rw-r--r-- | src/wasm/wasm.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 24479c1cf..8ff2dc978 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -1482,6 +1482,53 @@ Tag* Module::getTagOrNull(Name name) { return getModuleElementOrNull(tagsMap, name); } +Importable* Module::getImport(ModuleItemKind kind, Name name) { + switch (kind) { + case ModuleItemKind::Function: + return getFunction(name); + case ModuleItemKind::Table: + return getTable(name); + case ModuleItemKind::Memory: + return getMemory(name); + case ModuleItemKind::Global: + return getGlobal(name); + case ModuleItemKind::Tag: + return getTag(name); + case ModuleItemKind::DataSegment: + case ModuleItemKind::ElementSegment: + case ModuleItemKind::Invalid: + WASM_UNREACHABLE("invalid kind"); + } + + WASM_UNREACHABLE("unexpected kind"); +} + +Importable* Module::getImportOrNull(ModuleItemKind kind, Name name) { + auto doReturn = [](Importable* importable) { + return importable->imported() ? importable : nullptr; + }; + + switch (kind) { + case ModuleItemKind::Function: + return doReturn(getFunctionOrNull(name)); + case ModuleItemKind::Table: + return doReturn(getTableOrNull(name)); + case ModuleItemKind::Memory: + return doReturn(getMemoryOrNull(name)); + case ModuleItemKind::Global: + return doReturn(getGlobalOrNull(name)); + case ModuleItemKind::Tag: + return doReturn(getTagOrNull(name)); + case ModuleItemKind::DataSegment: + case ModuleItemKind::ElementSegment: + return nullptr; + case ModuleItemKind::Invalid: + WASM_UNREACHABLE("invalid kind"); + } + + WASM_UNREACHABLE("unexpected kind"); +} + // TODO(@warchant): refactor all usages to use variant with unique_ptr template<typename Vector, typename Map, typename Elem> Elem* addModuleElement(Vector& v, Map& m, Elem* curr, std::string funcName) { |