summaryrefslogtreecommitdiff
path: root/src/passes/LegalizeJSInterface.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes/LegalizeJSInterface.cpp')
-rw-r--r--src/passes/LegalizeJSInterface.cpp120
1 files changed, 55 insertions, 65 deletions
diff --git a/src/passes/LegalizeJSInterface.cpp b/src/passes/LegalizeJSInterface.cpp
index 4dc680972..8c7bc4414 100644
--- a/src/passes/LegalizeJSInterface.cpp
+++ b/src/passes/LegalizeJSInterface.cpp
@@ -32,7 +32,6 @@
#include "asm_v_wasm.h"
#include "asmjs/shared-constants.h"
-#include "ir/function-type-utils.h"
#include "ir/import-utils.h"
#include "ir/literal-utils.h"
#include "ir/utils.h"
@@ -153,12 +152,12 @@ private:
std::map<Name, Name> illegalImportsToLegal;
template<typename T> bool isIllegal(T* t) {
- for (auto param : t->params) {
- if (param == i64) {
+ for (auto param : t->sig.params.expand()) {
+ if (param == Type::i64) {
return true;
}
}
- return t->result == i64;
+ return t->sig.results == Type::i64;
}
bool isDynCall(Name name) { return name.startsWith("dynCall_"); }
@@ -190,25 +189,29 @@ private:
auto* call = module->allocator.alloc<Call>();
call->target = func->name;
- call->type = func->result;
+ call->type = func->sig.results;
- for (auto param : func->params) {
- if (param == i64) {
+ const std::vector<Type>& params = func->sig.params.expand();
+ std::vector<Type> legalParams;
+ for (auto param : params) {
+ if (param == Type::i64) {
call->operands.push_back(I64Utilities::recreateI64(
- builder, legal->params.size(), legal->params.size() + 1));
- legal->params.push_back(i32);
- legal->params.push_back(i32);
+ builder, legalParams.size(), legalParams.size() + 1));
+ legalParams.push_back(Type::i32);
+ legalParams.push_back(Type::i32);
} else {
call->operands.push_back(
- builder.makeLocalGet(legal->params.size(), param));
- legal->params.push_back(param);
+ builder.makeLocalGet(legalParams.size(), param));
+ legalParams.push_back(param);
}
}
+ legal->sig.params = Type(legalParams);
- if (func->result == i64) {
- Function* f = getFunctionOrImport(module, SET_TEMP_RET0, "vi");
- legal->result = i32;
- auto index = Builder::addVar(legal, Name(), i64);
+ if (func->sig.results == Type::i64) {
+ Function* f =
+ getFunctionOrImport(module, SET_TEMP_RET0, Type::i32, Type::none);
+ legal->sig.results = Type::i32;
+ 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(
@@ -217,7 +220,7 @@ private:
block->finalize();
legal->body = block;
} else {
- legal->result = func->result;
+ legal->sig.results = func->sig.results;
legal->body = call;
}
@@ -232,66 +235,55 @@ private:
// JS import
Name makeLegalStubForCalledImport(Function* im, Module* module) {
Builder builder(*module);
- auto type = make_unique<FunctionType>();
- type->name = Name(std::string("legaltype$") + im->name.str);
- auto legal = make_unique<Function>();
- legal->name = Name(std::string("legalimport$") + im->name.str);
- legal->module = im->module;
- legal->base = im->base;
- legal->type = type->name;
- auto func = make_unique<Function>();
- func->name = Name(std::string("legalfunc$") + im->name.str);
+ auto legalIm = make_unique<Function>();
+ legalIm->name = Name(std::string("legalimport$") + im->name.str);
+ legalIm->module = im->module;
+ legalIm->base = im->base;
+ auto stub = make_unique<Function>();
+ stub->name = Name(std::string("legalfunc$") + im->name.str);
+ stub->sig = im->sig;
auto* call = module->allocator.alloc<Call>();
- call->target = legal->name;
-
- auto* imFunctionType = ensureFunctionType(getSig(im), module);
-
- for (auto param : imFunctionType->params) {
- if (param == i64) {
- call->operands.push_back(
- I64Utilities::getI64Low(builder, func->params.size()));
- call->operands.push_back(
- I64Utilities::getI64High(builder, func->params.size()));
- type->params.push_back(i32);
- type->params.push_back(i32);
+ call->target = legalIm->name;
+
+ const std::vector<Type>& imParams = im->sig.params.expand();
+ std::vector<Type> params;
+ for (size_t i = 0; i < imParams.size(); ++i) {
+ if (imParams[i] == Type::i64) {
+ call->operands.push_back(I64Utilities::getI64Low(builder, i));
+ call->operands.push_back(I64Utilities::getI64High(builder, i));
+ params.push_back(i32);
+ params.push_back(i32);
} else {
- call->operands.push_back(
- builder.makeLocalGet(func->params.size(), param));
- type->params.push_back(param);
+ call->operands.push_back(builder.makeLocalGet(i, imParams[i]));
+ params.push_back(imParams[i]);
}
- func->params.push_back(param);
}
- if (imFunctionType->result == i64) {
- Function* f = getFunctionOrImport(module, GET_TEMP_RET0, "i");
- call->type = i32;
+ if (im->sig.results == Type::i64) {
+ Function* f =
+ getFunctionOrImport(module, GET_TEMP_RET0, Type::none, Type::i32);
+ call->type = Type::i32;
Expression* get = builder.makeCall(f->name, {}, call->type);
- func->body = I64Utilities::recreateI64(builder, call, get);
- type->result = i32;
+ stub->body = I64Utilities::recreateI64(builder, call, get);
} else {
- call->type = imFunctionType->result;
- func->body = call;
- type->result = imFunctionType->result;
+ call->type = im->sig.results;
+ stub->body = call;
}
- func->result = imFunctionType->result;
- FunctionTypeUtils::fillFunction(legal.get(), type.get());
+ legalIm->sig = Signature(Type(params), call->type);
- const auto& funcName = func->name;
- if (!module->getFunctionOrNull(funcName)) {
- module->addFunction(std::move(func));
- }
- if (!module->getFunctionTypeOrNull(type->name)) {
- module->addFunctionType(std::move(type));
+ const auto& stubName = stub->name;
+ if (!module->getFunctionOrNull(stubName)) {
+ module->addFunction(std::move(stub));
}
- if (!module->getFunctionOrNull(legal->name)) {
- module->addFunction(std::move(legal));
+ if (!module->getFunctionOrNull(legalIm->name)) {
+ module->addFunction(std::move(legalIm));
}
- return funcName;
+ return stubName;
}
static Function*
- getFunctionOrImport(Module* module, Name name, std::string sig) {
+ getFunctionOrImport(Module* module, Name name, Type params, Type results) {
// First look for the function by name
if (Function* f = module->getFunctionOrNull(name)) {
return f;
@@ -306,9 +298,7 @@ private:
import->name = name;
import->module = ENV;
import->base = name;
- auto* functionType = ensureFunctionType(std::move(sig), module);
- import->type = functionType->name;
- FunctionTypeUtils::fillFunction(import, functionType);
+ import->sig = Signature(params, results);
module->addFunction(import);
return import;
}