From e4ae8848b484110207c9a77d0c089aac3db482bb Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 18 Jul 2019 15:24:17 -0700 Subject: Generalize EM_JS parsing code. (#2233) The key thing is that there is a single constant, which may or may not be saved/loaded from a local, and may or may not get an added global if in relocatable code. Fixes emscripten-core/emscripten#8993 --- src/wasm/wasm-emscripten.cpp | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/wasm/wasm-emscripten.cpp b/src/wasm/wasm-emscripten.cpp index df5da949e..a3433bf9f 100644 --- a/src/wasm/wasm-emscripten.cpp +++ b/src/wasm/wasm-emscripten.cpp @@ -741,35 +741,14 @@ struct EmJsWalker : public PostWalker { return; } auto funcName = std::string(curr->name.stripPrefix(EM_JS_PREFIX.str)); - auto addrConst = curr->body->dynCast(); - if (addrConst == nullptr) { - auto block = curr->body->dynCast(); - Expression* value = nullptr; - if (block && block->list.size() > 0) { - value = block->list[0]; - // first item may be a set of a local that we get later - auto* set = value->dynCast(); - if (set) { - value = block->list[1]; - } - // look into a return value - if (auto* ret = value->dynCast()) { - value = ret->value; - } - // if it's a get of that set, use that value - if (auto* get = value->dynCast()) { - if (set && get->index == set->index) { - value = set->value; - } - } - } - if (value) { - addrConst = value->dynCast(); - } - } - if (addrConst == nullptr) { + // An EM_JS has a single const in the body. Typically it is just returned, + // but in unoptimized code it might be stored to a local and loaded from + // there, and in relocatable code it might get added to __memory_base etc. + FindAll consts(curr->body); + if (consts.list.size() != 1) { Fatal() << "Unexpected generated __em_js__ function body: " << curr->name; } + auto* addrConst = consts.list[0]; auto code = codeForConstAddr(wasm, segmentOffsets, addrConst); codeByName[funcName] = code; } -- cgit v1.2.3