Commit 68c33b29 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm][cleanup] Extract processing of imported functions

The function InstanceBuilder::ProcessImports got long and hard to
understand. This CL is the first of a series which aims at extracting
parts of ProcessImports into specialized functions.

This CL extracts the processing of imported functions into a separate
function. Aside from copying the code, I made the following changes:
* Use {enabled_} instead of a newly created WasmFeatures object.
  - I think this is more correct anyways.
* Rename {index} to {import_index}.
* Load {native_module} from {instance} for every function, instead of
  loading it once for all imports.

R=clemensh@chromium.org

Bug: v8:8562
Change-Id: I9533f302929eedd395962253c340ba35324df631
Reviewed-on: https://chromium-review.googlesource.com/c/1382467
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58331}
parent 9fa98c13
...@@ -415,6 +415,13 @@ class InstanceBuilder { ...@@ -415,6 +415,13 @@ class InstanceBuilder {
// need to recompile with bounds checks before creating the instance. // need to recompile with bounds checks before creating the instance.
MaybeHandle<JSArrayBuffer> FindImportedMemoryBuffer() const; MaybeHandle<JSArrayBuffer> FindImportedMemoryBuffer() const;
// Processes a single imported function.
bool ProcessImportedFunction(Handle<WasmInstanceObject> instance,
int import_index, int func_index,
Handle<String> module_name,
Handle<String> import_name,
Handle<Object> value);
// Process the imports, including functions, tables, globals, and memory, in // Process the imports, including functions, tables, globals, and memory, in
// order, loading them from the {ffi_} object. Returns the number of imported // order, loading them from the {ffi_} object. Returns the number of imported
// functions. // functions.
...@@ -1579,46 +1586,25 @@ MaybeHandle<JSArrayBuffer> InstanceBuilder::FindImportedMemoryBuffer() const { ...@@ -1579,46 +1586,25 @@ MaybeHandle<JSArrayBuffer> InstanceBuilder::FindImportedMemoryBuffer() const {
return {}; return {};
} }
// Process the imports, including functions, tables, globals, and memory, in bool InstanceBuilder::ProcessImportedFunction(
// order, loading them from the {ffi_} object. Returns the number of imported Handle<WasmInstanceObject> instance, int import_index, int func_index,
// functions. Handle<String> module_name, Handle<String> import_name,
int InstanceBuilder::ProcessImports(Handle<WasmInstanceObject> instance) { Handle<Object> value) {
int num_imported_functions = 0;
int num_imported_tables = 0;
int num_imported_mutable_globals = 0;
WasmFeatures enabled_features = WasmFeaturesFromIsolate(isolate_);
DCHECK_EQ(module_->import_table.size(), sanitized_imports_.size());
int num_imports = static_cast<int>(module_->import_table.size());
NativeModule* native_module = instance->module_object()->native_module();
for (int index = 0; index < num_imports; ++index) {
const WasmImport& import = module_->import_table[index];
Handle<String> module_name = sanitized_imports_[index].module_name;
Handle<String> import_name = sanitized_imports_[index].import_name;
Handle<Object> value = sanitized_imports_[index].value;
switch (import.kind) {
case kExternalFunction: {
// Function imports must be callable. // Function imports must be callable.
if (!value->IsCallable()) { if (!value->IsCallable()) {
ReportLinkError("function import requires a callable", index, ReportLinkError("function import requires a callable", import_index,
module_name, import_name); module_name, import_name);
return -1; return false;
} }
uint32_t func_index = import.index;
DCHECK_EQ(num_imported_functions, func_index);
auto js_receiver = Handle<JSReceiver>::cast(value); auto js_receiver = Handle<JSReceiver>::cast(value);
FunctionSig* expected_sig = module_->functions[func_index].sig; FunctionSig* expected_sig = module_->functions[func_index].sig;
auto kind = compiler::GetWasmImportCallKind(js_receiver, expected_sig, auto kind = compiler::GetWasmImportCallKind(js_receiver, expected_sig,
enabled_features.bigint); enabled_.bigint);
switch (kind) { switch (kind) {
case compiler::WasmImportCallKind::kLinkError: case compiler::WasmImportCallKind::kLinkError:
ReportLinkError( ReportLinkError("imported function does not match the expected type",
"imported function does not match the expected type", index, import_index, module_name, import_name);
module_name, import_name); return false;
return -1;
case compiler::WasmImportCallKind::kWasmToWasm: { case compiler::WasmImportCallKind::kWasmToWasm: {
// The imported function is a WASM function from another instance. // The imported function is a WASM function from another instance.
auto imported_function = Handle<WasmExportedFunction>::cast(value); auto imported_function = Handle<WasmExportedFunction>::cast(value);
...@@ -1628,12 +1614,12 @@ int InstanceBuilder::ProcessImports(Handle<WasmInstanceObject> instance) { ...@@ -1628,12 +1614,12 @@ int InstanceBuilder::ProcessImports(Handle<WasmInstanceObject> instance) {
Address imported_target = imported_function->GetWasmCallTarget(); Address imported_target = imported_function->GetWasmCallTarget();
ImportedFunctionEntry entry(instance, func_index); ImportedFunctionEntry entry(instance, func_index);
entry.SetWasmToWasm(*imported_instance, imported_target); entry.SetWasmToWasm(*imported_instance, imported_target);
break; return true;
} }
default: { default: {
// The imported function is a callable. // The imported function is a callable.
WasmCode* wasm_code = NativeModule* native_module = instance->module_object()->native_module();
native_module->import_wrapper_cache()->GetOrCompile( WasmCode* wasm_code = native_module->import_wrapper_cache()->GetOrCompile(
isolate_, kind, expected_sig); isolate_, kind, expected_sig);
ImportedFunctionEntry entry(instance, func_index); ImportedFunctionEntry entry(instance, func_index);
if (wasm_code->kind() == WasmCode::kWasmToJsWrapper) { if (wasm_code->kind() == WasmCode::kWasmToJsWrapper) {
...@@ -1641,13 +1627,40 @@ int InstanceBuilder::ProcessImports(Handle<WasmInstanceObject> instance) { ...@@ -1641,13 +1627,40 @@ int InstanceBuilder::ProcessImports(Handle<WasmInstanceObject> instance) {
entry.SetWasmToJs(isolate_, js_receiver, wasm_code); entry.SetWasmToJs(isolate_, js_receiver, wasm_code);
} else { } else {
// Wasm math intrinsics are compiled as regular Wasm functions. // Wasm math intrinsics are compiled as regular Wasm functions.
DCHECK(kind >= DCHECK(kind >= compiler::WasmImportCallKind::kFirstMathIntrinsic &&
compiler::WasmImportCallKind::kFirstMathIntrinsic &&
kind <= compiler::WasmImportCallKind::kLastMathIntrinsic); kind <= compiler::WasmImportCallKind::kLastMathIntrinsic);
entry.SetWasmToWasm(*instance, wasm_code->instruction_start()); entry.SetWasmToWasm(*instance, wasm_code->instruction_start());
} }
break; return true;
}
} }
UNREACHABLE();
}
// Process the imports, including functions, tables, globals, and memory, in
// order, loading them from the {ffi_} object. Returns the number of imported
// functions.
int InstanceBuilder::ProcessImports(Handle<WasmInstanceObject> instance) {
int num_imported_functions = 0;
int num_imported_tables = 0;
int num_imported_mutable_globals = 0;
DCHECK_EQ(module_->import_table.size(), sanitized_imports_.size());
int num_imports = static_cast<int>(module_->import_table.size());
for (int index = 0; index < num_imports; ++index) {
const WasmImport& import = module_->import_table[index];
Handle<String> module_name = sanitized_imports_[index].module_name;
Handle<String> import_name = sanitized_imports_[index].import_name;
Handle<Object> value = sanitized_imports_[index].value;
switch (import.kind) {
case kExternalFunction: {
uint32_t func_index = import.index;
DCHECK_EQ(num_imported_functions, func_index);
if (!ProcessImportedFunction(instance, index, func_index, module_name,
import_name, value)) {
return -1;
} }
num_imported_functions++; num_imported_functions++;
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment