diff options
author | Alon Zakai <alonzakai@gmail.com> | 2018-12-05 14:55:05 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-05 14:55:05 -0800 |
commit | 4c764d52b1e734c8dad295e1c970007512794bf6 (patch) | |
tree | 31fab60a74096a9328bea6a8d912379aa6c95790 /src/ir/module-utils.h | |
parent | 6cd3386410ba53e20afbf523c955f73bd352299b (diff) | |
download | binaryen-4c764d52b1e734c8dad295e1c970007512794bf6.tar.gz binaryen-4c764d52b1e734c8dad295e1c970007512794bf6.tar.bz2 binaryen-4c764d52b1e734c8dad295e1c970007512794bf6.zip |
Add function rename utility (#1805)
And use it in wasm-emscripten
Diffstat (limited to 'src/ir/module-utils.h')
-rw-r--r-- | src/ir/module-utils.h | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/ir/module-utils.h b/src/ir/module-utils.h index d6ee50dd0..851b926c6 100644 --- a/src/ir/module-utils.h +++ b/src/ir/module-utils.h @@ -18,6 +18,7 @@ #define wasm_ir_module_h #include "wasm.h" +#include "ir/find_all.h" #include "ir/manipulation.h" namespace wasm { @@ -128,6 +129,52 @@ inline void copyModule(Module& in, Module& out) { out.debugInfoFileNames = in.debugInfoFileNames; } +// Renaming + +template<typename T> +inline void renameFunctions(Module& wasm, T& map) { + // Update the function itself. + for (auto& pair : map) { + assert(!wasm.getFunctionOrNull(pair.second)); + wasm.getFunction(pair.first)->name = pair.second; + } + wasm.updateMaps(); + // Update other global things. + auto maybeUpdate = [&](Name& name) { + auto iter = map.find(name); + if (iter != map.end()) { + name = iter->second; + } + }; + maybeUpdate(wasm.start); + for (auto& segment : wasm.table.segments) { + for (auto& name : segment.data) { + maybeUpdate(name); + } + } + for (auto& exp : wasm.exports) { + if (exp->kind == ExternalKind::Function) { + maybeUpdate(exp->value); + } + } + // Update call instructions. + for (auto& func : wasm.functions) { + // TODO: parallelize + if (!func->imported()) { + FindAll<Call> calls(func->body); + for (auto* call : calls.list) { + maybeUpdate(call->target); + } + } + } +} + +inline void renameFunction(Module& wasm, Name oldName, Name newName) { + std::map<Name, Name> map; + map[oldName] = newName; + renameFunctions(wasm, map); +} + // Convenient iteration over imported/non-imported module elements template<typename T> |