From 49763aa9a7fb0f07588a9d19db6896356e52c5f8 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 1 Jun 2022 07:01:15 -0700 Subject: [NFC] Refactor EHUtils::findPops() method (#4704) This moves it out of the validator so it can be used elsewhere. It will be used in #4685 --- src/ir/eh-utils.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/ir/eh-utils.cpp') diff --git a/src/ir/eh-utils.cpp b/src/ir/eh-utils.cpp index 3d369573d..fe33b09c2 100644 --- a/src/ir/eh-utils.cpp +++ b/src/ir/eh-utils.cpp @@ -162,6 +162,37 @@ void handleBlockNestedPops(Function* func, Module& wasm) { TypeUpdating::handleNonDefaultableLocals(func, wasm); } +Pop* findPop(Expression* expr) { + auto pops = findPops(expr); + if (pops.size() == 0) { + return nullptr; + } + assert(pops.size() == 1); + return pops[0]; +} + +SmallVector findPops(Expression* expr) { + SmallVector pops; + SmallVector work; + work.push_back(expr); + while (!work.empty()) { + auto* curr = work.back(); + work.pop_back(); + if (auto* pop = curr->dynCast()) { + pops.push_back(pop); + } else if (auto* try_ = curr->dynCast()) { + // We don't go into inner catch bodies; pops in inner catch bodies + // belong to the inner catches + work.push_back(try_->body); + } else { + for (auto* child : ChildIterator(curr)) { + work.push_back(child); + } + } + } + return pops; +}; + } // namespace EHUtils } // namespace wasm -- cgit v1.2.3