Skip to content

Commit e4ae884

Browse files
authored
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
1 parent 6cc61b8 commit e4ae884

1 file changed

Lines changed: 6 additions & 27 deletions

File tree

src/wasm/wasm-emscripten.cpp

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -741,35 +741,14 @@ struct EmJsWalker : public PostWalker<EmJsWalker> {
741741
return;
742742
}
743743
auto funcName = std::string(curr->name.stripPrefix(EM_JS_PREFIX.str));
744-
auto addrConst = curr->body->dynCast<Const>();
745-
if (addrConst == nullptr) {
746-
auto block = curr->body->dynCast<Block>();
747-
Expression* value = nullptr;
748-
if (block && block->list.size() > 0) {
749-
value = block->list[0];
750-
// first item may be a set of a local that we get later
751-
auto* set = value->dynCast<LocalSet>();
752-
if (set) {
753-
value = block->list[1];
754-
}
755-
// look into a return value
756-
if (auto* ret = value->dynCast<Return>()) {
757-
value = ret->value;
758-
}
759-
// if it's a get of that set, use that value
760-
if (auto* get = value->dynCast<LocalGet>()) {
761-
if (set && get->index == set->index) {
762-
value = set->value;
763-
}
764-
}
765-
}
766-
if (value) {
767-
addrConst = value->dynCast<Const>();
768-
}
769-
}
770-
if (addrConst == nullptr) {
744+
// An EM_JS has a single const in the body. Typically it is just returned,
745+
// but in unoptimized code it might be stored to a local and loaded from
746+
// there, and in relocatable code it might get added to __memory_base etc.
747+
FindAll<Const> consts(curr->body);
748+
if (consts.list.size() != 1) {
771749
Fatal() << "Unexpected generated __em_js__ function body: " << curr->name;
772750
}
751+
auto* addrConst = consts.list[0];
773752
auto code = codeForConstAddr(wasm, segmentOffsets, addrConst);
774753
codeByName[funcName] = code;
775754
}

0 commit comments

Comments
 (0)