#include "asmjs/shared-constants.h"
#include "ir/element-utils.h"
#include "ir/import-utils.h"
#include "ir/literal-utils.h"
#include "ir/utils.h"
#include "pass.h"
#include "shared-constants.h"
#include "wasm-builder.h"
#include "wasm.h"
#include <utility>
namespace wasm {
namespace {
static Name GET_TEMP_RET_EXPORT("__get_temp_ret");
static Name SET_TEMP_RET_EXPORT("__set_temp_ret");
static Name GET_TEMP_RET_IMPORT("getTempRet0");
static Name SET_TEMP_RET_IMPORT("setTempRet0");
struct LegalizeJSInterface : public Pass {
bool addsEffects() override { return true; }
bool full;
LegalizeJSInterface(bool full) : full(full) {}
void run(Module* module) override {
setTempRet0 = nullptr;
getTempRet0 = nullptr;
auto exportOriginals =
getPassOptions().hasArgument("legalize-js-interface-export-originals");
exportedHelpers =
getPassOptions().hasArgument("legalize-js-interface-exported-helpers");
std::vector<std::unique_ptr<Export>> newExports;
for (auto& ex : module->exports) {
if (ex->kind == ExternalKind::Function) {
auto* func = module->getFunction(ex->value);
if (isIllegal(func) && shouldBeLegalized(ex.get(), func)) {
auto legalName = makeLegalStub(func, module);
ex->value = legalName;
if (exportOriginals) {
if (!func->imported() && !isDynCall(ex->name)) {
Builder builder(*module);
Name newName = std::string("orig$") + ex->name.toString();
newExports.push_back(builder.makeExport(
newName, func->name, ExternalKind::Function));
}
}
}
}
}
for (auto& ex : newExports) {
module->addExport(std::move(ex));
}
std::vector<Function*> originalFunctions;
for (auto& func : module->functions) {
originalFunctions.push_back(func.get());
}
for (auto* im : originalFunctions) {
if (im->imported() && isIllegal(im) && shouldBeLegalized(im)) {
auto funcName = makeLegalStubForCalledImport(im, module);
illegalImportsToLegal[im->name] = funcName;
ElementUtils::iterAllElementFunctionNames(module, [&](Name& name) {
if (name == im->name) {
name = funcName;
}
});
}
}
if (!illegalImportsToLegal.empty()) {
struct Fixer : public WalkerPass<PostWalker<Fixer>> {
bool isFunctionParallel() override { return true; }
std::unique_ptr<Pass> create() override {
return std::make_unique<Fixer>(illegalImportsToLegal);
}
std::map<Name, Name>* illegalImportsToLegal;
Fixer(std::map<Name, Name>* illegalImportsToLegal)
: illegalImportsToLegal(illegalImportsToLegal) {}
void visitCall(Call* curr) {
auto iter = illegalImportsToLegal->find(curr->target);
if (iter == illegalImportsToLegal->end()) {
return;
}
replaceCurrent(
Builder(*getModule())
.makeCall(
iter->second, curr->operands, curr->type, curr->isReturn));
}
void visitRefFunc(RefFunc* curr) {
auto iter = illegalImportsToLegal->find(curr->func);
if (iter == illegalImportsToLegal->end()) {
return;
}
curr->func = iter->second;
}
};
Fixer fixer(&illegalImportsToLegal);
fixer.run(getPassRunner(), module);
fixer.runOnModuleCode(getPassRunner(), module);
for (const auto& pair : illegalImportsToLegal) {
module->removeFunction(pair.first);
}
}
module->removeExport(GET_TEMP_RET_EXPORT);
module->removeExport(SET_TEMP_RET_EXPORT);
}
private:
std::map<Name, Name> illegalImportsToLegal;
bool exportedHelpers = false;
Function* getTempRet0 = nullptr;
Function* setTempRet0 = nullptr;
template<typename T> bool isIllegal(T* t) {
for (const auto& param : t->getParams()) {
if (param == Type::i64) {
return true;
}
}
return t->getResults() == Type::i64;
}
bool isDynCall(Name name) { return name.startsWith("dynCall_"); }
bool shouldBeLegalized(Export* ex, Function* func) {
if (full) {
return true;
}
return isDynCall(ex->name);
}
bool shouldBeLegalized(Function* im) {
if (full) {
return true;
}
return im->module == ENV && im->base.startsWith("invoke_");
}
Function* tempSetter(Module* module) {
if (!setTempRet0) {
if (exportedHelpers) {
auto* ex = module->getExport(SET_TEMP_RET_EXPORT);
setTempRet0 = module->getFunction(ex->value);
} else {
setTempRet0 = getFunctionOrImport(
module, SET_TEMP_RET_IMPORT, Type::i32, Type::none);
}
}
return setTempRet0;
}
Function* tempGetter(Module* module) {
if (!getTempRet0) {
if (exportedHelpers) {
auto* ex = module->getExport(GET_TEMP_RET_EXPORT);
getTempRet0 = module->getFunction(ex->value);
} else {
getTempRet0 = getFunctionOrImport(
module, GET_TEMP_RET_IMPORT, Type::none, Type::i32);
}
}
return getTempRet0;
}
Name makeLegalStub(Function* func, Module* module) {
Name legalName(std::string("legalstub$") + func->name.toString());
if (module->getFunctionOrNull(legalName)) {
return legalName;
}
Builder builder(*module);
auto* legal = new Function();
legal->name = legalName;
auto* call = module->allocator.alloc<Call>();
call->target = func->name;
call->type = func->getResults();
std::vector<Type> legalParams;
for (const auto& param : func->getParams()) {
if (param == Type::i64) {
call->operands.push_back(I64Utilities::recreateI64(
builder, legalParams.size(), legalParams.size() + 1));
legalParams.push_back(Type::i32);
legalParams.push_back(Type::i32);
} else {
call->operands.push_back(
builder.makeLocalGet(legalParams.size(), param));
legalParams.push_back(param);
}
}
Type resultsType =
func->getResults() == Type::i64 ? Type::i32 : func->getResults();
legal->type = Signature(Type(legalParams), resultsType);
if (func->getResults() == Type::i64) {
auto index = Builder::addVar(legal, Name(), Type::i64);
auto* block = builder.makeBlock();
block->list.push_back(builder.makeLocalSet(index, call));
block->list.push_back(
builder.makeCall(tempSetter(module)->name,
{I64Utilities::getI64High(builder, index)},
Type::none));
block->list.push_back(I64Utilities::getI64Low(builder, index));
block->finalize();
legal->body = block;
} else {
legal->body = call;
}
return module->addFunction(legal)->name;
}
Name makeLegalStubForCalledImport(Function* im, Module* module) {
Builder builder(*module);
auto legalIm = std::make_unique<Function>();
legalIm->name = Name(std::string("legalimport$") + im->name.toString());
legalIm->module = im->module;
legalIm->base = im->base;
auto stub = std::make_unique<Function>();
stub->name = Name(std::string("legalfunc$") + im->name.toString());
stub->type = im->type;
auto* call = module->allocator.alloc<Call>();
call->target = legalIm->name;
std::vector<Type> params;
Index i = 0;
for (const auto& param : im->getParams()) {
if (param == Type::i64) {
call->operands.push_back(I64Utilities::getI64Low(builder, i));
call->operands.push_back(I64Utilities::getI64High(builder, i));
params.push_back(Type::i32);
params.push_back(Type::i32);
} else {
call->operands.push_back(builder.makeLocalGet(i, param));
params.push_back(param);
}
++i;
}
if (im->getResults() == Type::i64) {
call->type = Type::i32;
Expression* get =
builder.makeCall(tempGetter(module)->name, {}, call->type);
stub->body = I64Utilities::recreateI64(builder, call, get);
} else {
call->type = im->getResults();
stub->body = call;
}
legalIm->type = Signature(Type(params), call->type);
const auto& stubName = stub->name;
if (!module->getFunctionOrNull(stubName)) {
module->addFunction(std::move(stub));
}
if (!module->getFunctionOrNull(legalIm->name)) {
module->addFunction(std::move(legalIm));
}
return stubName;
}
static Function*
getFunctionOrImport(Module* module, Name name, Type params, Type results) {
if (Function* f = module->getFunctionOrNull(name)) {
return f;
}
ImportInfo info(*module);
if (Function* f = info.getImportedFunction(ENV, name)) {
return f;
}
auto import = Builder::makeFunction(name, Signature(params, results), {});
import->module = ENV;
import->base = name;
auto* ret = import.get();
module->addFunction(std::move(import));
return ret;
}
};
struct LegalizeAndPruneJSInterface : public LegalizeJSInterface {
LegalizeAndPruneJSInterface() : LegalizeJSInterface(true) {}
void run(Module* module) override {
LegalizeJSInterface::run(module);
prune(module);
}
void prune(Module* module) {
std::unordered_map<Name, Name> exportedFunctions;
for (auto& exp : module->exports) {
if (exp->kind == ExternalKind::Function) {
exportedFunctions[exp->value] = exp->name;
}
}
for (auto& func : module->functions) {
auto imported = func->imported();
auto exported = exportedFunctions.count(func->name);
if (!imported && !exported) {
continue;
}
auto sig = func->type.getSignature();
auto illegal = isIllegal(sig.results);
illegal =
illegal || std::any_of(sig.params.begin(),
sig.params.end(),
[&](const Type& t) { return isIllegal(t); });
if (!illegal) {
continue;
}
if (imported) {
func->module = func->base = Name();
Builder builder(*module);
if (sig.results == Type::none) {
func->body = builder.makeNop();
} else {
func->body =
builder.makeConstantExpression(Literal::makeZeros(sig.results));
}
}
if (exported) {
module->removeExport(exportedFunctions[func->name]);
}
}
}
bool isIllegal(Type type) {
auto features = type.getFeatures();
return features.hasSIMD() || features.hasMultivalue();
}
};
}
Pass* createLegalizeJSInterfacePass() { return new LegalizeJSInterface(true); }
Pass* createLegalizeJSInterfaceMinimallyPass() {
return new LegalizeJSInterface(false);
}
Pass* createLegalizeAndPruneJSInterfacePass() {
return new LegalizeAndPruneJSInterface();
}
}