Skip to content

call_indirect is now structural in spec:314 #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,6 @@ class ModuleInstance {
if (index >= instance.wasm.table.names.size()) trap("callIndirect: overflow");
Name name = instance.wasm.table.names[index];
Function *func = instance.wasm.getFunction(name);
if (func->type.is() && func->type != curr->fullType) trap("callIndirect: bad type");
if (func->params.size() != arguments.size()) trap("callIndirect: bad # of arguments");
for (size_t i = 0; i < func->params.size(); i++) {
if (func->params[i] != arguments[i].type) {
Expand Down
51 changes: 42 additions & 9 deletions src/wasm-s-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,21 +312,49 @@ class SExpressionWasmBuilder {
}
functionNames.push_back(name);
functionCounter++;
FunctionType* type = nullptr;
functionTypes[name] = none;
std::vector<WasmType> params;
for (;i < s.size(); i++) {
Element& curr = *s[i];
IString id = curr[0]->str();
if (id == RESULT) {
functionTypes[name] = stringToWasmType(curr[1]->str());
return;
} else if (id == TYPE) {
Name typeName = curr[1]->str();
if (!wasm.checkFunctionType(typeName)) throw ParseException("unknown function");
FunctionType* type = wasm.getFunctionType(typeName);
type = wasm.getFunctionType(typeName);
functionTypes[name] = type->result;
return;
} else if (id == PARAM && curr.size() > 1) {
Index j = 1;
if (curr[j]->dollared()) {
// dollared input symbols cannot be types
params.push_back(stringToWasmType(curr[j + 1]->str(), true));
} else {
while (j < curr.size()) {
params.push_back(stringToWasmType(curr[j++]->str(), true));
}
}
}
}
if (!type) {
// if no function type provided, generate one, but reuse a previous one with the
// right structure if there is
// see https://github.com/WebAssembly/spec/pull/301
bool need = true;
std::unique_ptr<FunctionType> functionType = make_unique<FunctionType>();
functionType->result = functionTypes[name];
functionType->params = std::move(params);
for (auto& existing : wasm.functionTypes) {
if (existing->structuralComparison(*functionType)) {
need = false;
break;
}
}
if (need) {
wasm.addFunctionType(functionType.release());
}
}
functionTypes[name] = none;
}

void preParseImports(Element& curr) {
Expand Down Expand Up @@ -500,16 +528,21 @@ class SExpressionWasmBuilder {
body = allocator.alloc<Nop>();
}
if (currFunction->result != result) throw ParseException("bad func declaration", s.line, s.col);
/* TODO: spec in flux, https://github.com/WebAssembly/spec/pull/301
// see https://github.com/WebAssembly/spec/pull/301
if (type.isNull()) {
// if no function type provided, generate a private one for this function
// if no function type name provided, then we generated one
auto* functionType = sigToFunctionType(getSig(currFunction.get()));
wasm.addFunctionType(functionType);
type = functionType->name;
for (auto& existing : wasm.functionTypes) {
if (existing->structuralComparison(*functionType)) {
type = existing->name;
break;
}
}
assert(type.is());
}
*/
currFunction->body = body;
currFunction->type = type;

wasm.addFunction(currFunction.release());
currLocalTypes.clear();
labelStack.clear();
Expand Down
8 changes: 6 additions & 2 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1065,15 +1065,19 @@ class FunctionType {

FunctionType() : result(none) {}

bool operator==(FunctionType& b) {
if (name != b.name) return false; // XXX
bool structuralComparison(FunctionType& b) {
if (result != b.result) return false;
if (params.size() != b.params.size()) return false;
for (size_t i = 0; i < params.size(); i++) {
if (params[i] != b.params[i]) return false;
}
return true;
}

bool operator==(FunctionType& b) {
if (name != b.name) return false;
return structuralComparison(b);
}
bool operator!=(FunctionType& b) {
return !(*this == b);
}
Expand Down