mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-12 22:05:33 +00:00
Implement more function codegen
This commit is contained in:
parent
2d10306c87
commit
6d5dd5ff21
14
Cargo.lock
generated
14
Cargo.lock
generated
@ -309,7 +309,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "cranelift-bforest"
|
||||
version = "0.30.0"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#f291d01c3a23282e1432c955beb6ef0ab34db7ea"
|
||||
dependencies = [
|
||||
"cranelift-entity 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
|
||||
]
|
||||
@ -317,7 +317,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "cranelift-codegen"
|
||||
version = "0.30.0"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#f291d01c3a23282e1432c955beb6ef0ab34db7ea"
|
||||
dependencies = [
|
||||
"cranelift-bforest 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
|
||||
"cranelift-codegen-meta 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
|
||||
@ -331,7 +331,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "cranelift-codegen-meta"
|
||||
version = "0.30.0"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#f291d01c3a23282e1432c955beb6ef0ab34db7ea"
|
||||
dependencies = [
|
||||
"cranelift-entity 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
|
||||
]
|
||||
@ -339,12 +339,12 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "cranelift-entity"
|
||||
version = "0.30.0"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#f291d01c3a23282e1432c955beb6ef0ab34db7ea"
|
||||
|
||||
[[package]]
|
||||
name = "cranelift-frontend"
|
||||
version = "0.30.0"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#f291d01c3a23282e1432c955beb6ef0ab34db7ea"
|
||||
dependencies = [
|
||||
"cranelift-codegen 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
|
||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -354,7 +354,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "cranelift-native"
|
||||
version = "0.30.0"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#f291d01c3a23282e1432c955beb6ef0ab34db7ea"
|
||||
dependencies = [
|
||||
"cranelift-codegen 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
|
||||
"raw-cpuid 6.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -364,7 +364,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "cranelift-wasm"
|
||||
version = "0.30.0"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#71581b822e51edab335368af1e0102ab0c811464"
|
||||
source = "git+https://github.com/wasmerio/cranelift.git?branch=wasmer#f291d01c3a23282e1432c955beb6ef0ab34db7ea"
|
||||
dependencies = [
|
||||
"cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cranelift-codegen 0.30.0 (git+https://github.com/wasmerio/cranelift.git?branch=wasmer)",
|
||||
|
@ -13,7 +13,7 @@ wasmer-runtime-core = { path = "../runtime-core", version = "0.4.1" }
|
||||
# cranelift-codegen = { path = "../../../cranelift/cranelift-codegen" }
|
||||
# cranelift-entity = { path = "../../../cranelift/cranelift-entity" }
|
||||
# cranelift-frontend = { path = "../../../cranelift/cranelift-frontend" }
|
||||
# cranelift-wasm = { git = "https://github.com/wasmerio/cranelift.git", branch = "wasmer" }
|
||||
# cranelift-wasm = { path = "../../../cranelift/cranelift-wasm" }
|
||||
cranelift-native = { git = "https://github.com/wasmerio/cranelift.git", branch = "wasmer" }
|
||||
cranelift-codegen = { git = "https://github.com/wasmerio/cranelift.git", branch = "wasmer" }
|
||||
cranelift-entity = { git = "https://github.com/wasmerio/cranelift.git", branch = "wasmer" }
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Parts of the following code are Copyright 2018 Cranelift Developers
|
||||
// and subject to the license https://github.com/CraneStation/cranelift/blob/c47ca7bafc8fc48358f1baa72360e61fc1f7a0f2/cranelift-wasm/LICENSE
|
||||
|
||||
use crate::{func_env::FuncEnv, module::{Converter, Module}, signal::Caller, get_isa};
|
||||
use crate::{func_env::FuncEnv, module::{Converter, Module}, signal::Caller, get_isa, relocation::call_names};
|
||||
use std::sync::Arc;
|
||||
use wasmer_runtime_core::{
|
||||
backend::{Backend, CacheGen, Token},
|
||||
@ -9,17 +9,21 @@ use wasmer_runtime_core::{
|
||||
codegen::*,
|
||||
module::{ModuleInfo, ModuleInner},
|
||||
structures::{TypedIndex, Map},
|
||||
memory::MemoryType,
|
||||
types::{
|
||||
FuncSig, FuncIndex,
|
||||
ElementType, GlobalDescriptor, GlobalIndex, GlobalInit, Initializer, LocalFuncIndex,
|
||||
LocalOrImport, MemoryDescriptor, SigIndex, TableDescriptor, Value,
|
||||
LocalOrImport, MemoryDescriptor, SigIndex, TableDescriptor, Value, MemoryIndex, TableIndex,
|
||||
},
|
||||
vm,
|
||||
};
|
||||
use cranelift_codegen::isa;
|
||||
use std::mem;
|
||||
use cranelift_codegen::{isa, cursor::FuncCursor};
|
||||
use cranelift_codegen::entity::EntityRef;
|
||||
use cranelift_codegen::ir::{self, Ebb, InstBuilder, ValueLabel};
|
||||
use cranelift_codegen::timing;
|
||||
use wasmparser::Type as WpType;
|
||||
use cranelift_wasm::{FuncEnvironment, ReturnMode, WasmError, WasmResult};
|
||||
use cranelift_wasm::{translate_operator, TranslationState, get_vmctx_value_label};
|
||||
use cranelift_wasm::{self, translate_module, FuncTranslator, ModuleEnvironment};
|
||||
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
|
||||
@ -64,9 +68,6 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
||||
|
||||
// define_function_body(
|
||||
|
||||
|
||||
|
||||
|
||||
let mut func_translator = FuncTranslator::new();
|
||||
|
||||
// let func_body = {
|
||||
@ -74,6 +75,7 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
||||
|
||||
// let mut func_env = FuncEnv::new(self);
|
||||
|
||||
// TODO should func_index come from self.functions?
|
||||
let func_index = self.func_bodies.next_index();
|
||||
let name = ir::ExternalName::user(0, func_index.index() as u32);
|
||||
|
||||
@ -90,12 +92,13 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
||||
builder: None,
|
||||
func_body: func,
|
||||
func_translator,
|
||||
next_local: 0,
|
||||
// translator:
|
||||
};
|
||||
let builder = FunctionBuilder::new(&mut func_env.func_body, &mut func_env.func_translator.func_ctx);
|
||||
func_env.builder = Some(builder);
|
||||
|
||||
let mut builder = func_env.builder.as_ref().unwrap();
|
||||
let mut builder = func_env.builder.as_mut().unwrap();
|
||||
|
||||
// TODO srcloc
|
||||
//builder.set_srcloc(cur_srcloc(&reader));
|
||||
@ -281,10 +284,7 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
||||
// };
|
||||
|
||||
// Add function body to list of function bodies.
|
||||
// self.func_bodies.push(func);
|
||||
|
||||
|
||||
|
||||
//self.func_bodies.push(func);
|
||||
|
||||
self.functions.push(func_env);
|
||||
Ok(self.functions.last_mut().unwrap())
|
||||
@ -323,6 +323,664 @@ pub struct CraneliftFunctionCodeGenerator {
|
||||
func_body: ir::Function,
|
||||
builder: Option<FunctionBuilder<'static>>,
|
||||
func_translator: FuncTranslator,
|
||||
next_local: usize,
|
||||
}
|
||||
|
||||
impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
/// Gets configuration information needed for compiling functions
|
||||
fn target_config(&self) -> isa::TargetFrontendConfig {
|
||||
self.env.target_config()
|
||||
}
|
||||
|
||||
/// Gets native pointers types.
|
||||
///
|
||||
/// `I64` on 64-bit arch; `I32` on 32-bit arch.
|
||||
fn pointer_type(&self) -> ir::Type {
|
||||
ir::Type::int(u16::from(self.target_config().pointer_bits())).unwrap()
|
||||
}
|
||||
|
||||
/// Gets the size of a native pointer in bytes.
|
||||
fn pointer_bytes(&self) -> u8 {
|
||||
self.target_config().pointer_bytes()
|
||||
}
|
||||
|
||||
/// Sets up the necessary preamble definitions in `func` to access the global identified
|
||||
/// by `index`.
|
||||
///
|
||||
/// The index space covers both imported and locally declared globals.
|
||||
fn make_global(
|
||||
&mut self,
|
||||
func: &mut ir::Function,
|
||||
clif_global_index: cranelift_wasm::GlobalIndex,
|
||||
) -> cranelift_wasm::WasmResult<cranelift_wasm::GlobalVariable> {
|
||||
let global_index: GlobalIndex = Converter(clif_global_index).into();
|
||||
|
||||
// Create VMContext value.
|
||||
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
||||
let ptr_type = self.pointer_type();
|
||||
|
||||
let local_global_addr = match global_index.local_or_import(&self.env.module.info) {
|
||||
LocalOrImport::Local(local_global_index) => {
|
||||
let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: (vm::Ctx::offset_globals() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
let offset = local_global_index.index() * mem::size_of::<*mut vm::LocalGlobal>();
|
||||
|
||||
let local_global_ptr_ptr = func.create_global_value(ir::GlobalValueData::IAddImm {
|
||||
base: globals_base_addr,
|
||||
offset: (offset as i64).into(),
|
||||
global_type: ptr_type,
|
||||
});
|
||||
|
||||
func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: local_global_ptr_ptr,
|
||||
offset: 0.into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
})
|
||||
}
|
||||
LocalOrImport::Import(import_global_index) => {
|
||||
let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: (vm::Ctx::offset_imported_globals() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
let offset = import_global_index.index() * mem::size_of::<*mut vm::LocalGlobal>();
|
||||
|
||||
let local_global_ptr_ptr = func.create_global_value(ir::GlobalValueData::IAddImm {
|
||||
base: globals_base_addr,
|
||||
offset: (offset as i64).into(),
|
||||
global_type: ptr_type,
|
||||
});
|
||||
|
||||
func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: local_global_ptr_ptr,
|
||||
offset: 0.into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
Ok(cranelift_wasm::GlobalVariable::Memory {
|
||||
gv: local_global_addr,
|
||||
offset: (vm::LocalGlobal::offset_data() as i32).into(),
|
||||
ty: self.env.get_global(clif_global_index).ty,
|
||||
})
|
||||
}
|
||||
|
||||
/// Sets up the necessary preamble definitions in `func` to access the linear memory identified
|
||||
/// by `index`.
|
||||
///
|
||||
/// The index space covers both imported and locally declared memories.
|
||||
fn make_heap(
|
||||
&mut self,
|
||||
func: &mut ir::Function,
|
||||
clif_mem_index: cranelift_wasm::MemoryIndex,
|
||||
) -> cranelift_wasm::WasmResult<ir::Heap> {
|
||||
let mem_index: MemoryIndex = Converter(clif_mem_index).into();
|
||||
// Create VMContext value.
|
||||
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
||||
let ptr_type = self.pointer_type();
|
||||
|
||||
let (local_memory_ptr_ptr, description) =
|
||||
match mem_index.local_or_import(&self.env.module.info) {
|
||||
LocalOrImport::Local(local_mem_index) => {
|
||||
let memories_base_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: (vm::Ctx::offset_memories() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
let local_memory_ptr_offset =
|
||||
local_mem_index.index() * mem::size_of::<*mut vm::LocalMemory>();
|
||||
|
||||
(
|
||||
func.create_global_value(ir::GlobalValueData::IAddImm {
|
||||
base: memories_base_addr,
|
||||
offset: (local_memory_ptr_offset as i64).into(),
|
||||
global_type: ptr_type,
|
||||
}),
|
||||
self.env.module.info.memories[local_mem_index],
|
||||
)
|
||||
}
|
||||
LocalOrImport::Import(import_mem_index) => {
|
||||
let memories_base_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: (vm::Ctx::offset_imported_memories() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
let local_memory_ptr_offset =
|
||||
import_mem_index.index() * mem::size_of::<*mut vm::LocalMemory>();
|
||||
|
||||
(
|
||||
func.create_global_value(ir::GlobalValueData::IAddImm {
|
||||
base: memories_base_addr,
|
||||
offset: (local_memory_ptr_offset as i64).into(),
|
||||
global_type: ptr_type,
|
||||
}),
|
||||
self.env.module.info.imported_memories[import_mem_index].1,
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let (local_memory_ptr, local_memory_base) = {
|
||||
let local_memory_ptr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: local_memory_ptr_ptr,
|
||||
offset: 0.into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
(
|
||||
local_memory_ptr,
|
||||
func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: local_memory_ptr,
|
||||
offset: (vm::LocalMemory::offset_base() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: false,
|
||||
}),
|
||||
)
|
||||
};
|
||||
|
||||
match description.memory_type() {
|
||||
mem_type @ MemoryType::Dynamic => {
|
||||
let local_memory_bound = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: local_memory_ptr,
|
||||
offset: (vm::LocalMemory::offset_bound() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: false,
|
||||
});
|
||||
|
||||
Ok(func.create_heap(ir::HeapData {
|
||||
base: local_memory_base,
|
||||
min_size: (description.minimum.bytes().0 as u64).into(),
|
||||
offset_guard_size: mem_type.guard_size().into(),
|
||||
style: ir::HeapStyle::Dynamic {
|
||||
bound_gv: local_memory_bound,
|
||||
},
|
||||
index_type: ir::types::I32,
|
||||
}))
|
||||
}
|
||||
mem_type @ MemoryType::Static | mem_type @ MemoryType::SharedStatic => Ok(func
|
||||
.create_heap(ir::HeapData {
|
||||
base: local_memory_base,
|
||||
min_size: (description.minimum.bytes().0 as u64).into(),
|
||||
offset_guard_size: mem_type.guard_size().into(),
|
||||
style: ir::HeapStyle::Static {
|
||||
bound: mem_type.bounds().unwrap().into(),
|
||||
},
|
||||
index_type: ir::types::I32,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets up the necessary preamble definitions in `func` to access the table identified
|
||||
/// by `index`.
|
||||
///
|
||||
/// The index space covers both imported and locally declared tables.
|
||||
fn make_table(
|
||||
&mut self,
|
||||
func: &mut ir::Function,
|
||||
clif_table_index: cranelift_wasm::TableIndex,
|
||||
) -> cranelift_wasm::WasmResult<ir::Table> {
|
||||
let table_index: TableIndex = Converter(clif_table_index).into();
|
||||
// Create VMContext value.
|
||||
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
||||
let ptr_type = self.pointer_type();
|
||||
|
||||
let (table_struct_ptr_ptr, description) = match table_index
|
||||
.local_or_import(&self.env.module.info)
|
||||
{
|
||||
LocalOrImport::Local(local_table_index) => {
|
||||
let tables_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: (vm::Ctx::offset_tables() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
let table_struct_ptr_offset =
|
||||
local_table_index.index() * vm::LocalTable::size() as usize;
|
||||
|
||||
let table_struct_ptr_ptr = func.create_global_value(ir::GlobalValueData::IAddImm {
|
||||
base: tables_base,
|
||||
offset: (table_struct_ptr_offset as i64).into(),
|
||||
global_type: ptr_type,
|
||||
});
|
||||
|
||||
(
|
||||
table_struct_ptr_ptr,
|
||||
self.env.module.info.tables[local_table_index],
|
||||
)
|
||||
}
|
||||
LocalOrImport::Import(import_table_index) => {
|
||||
let tables_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: (vm::Ctx::offset_imported_tables() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
let table_struct_ptr_offset =
|
||||
import_table_index.index() * vm::LocalTable::size() as usize;
|
||||
|
||||
let table_struct_ptr_ptr = func.create_global_value(ir::GlobalValueData::IAddImm {
|
||||
base: tables_base,
|
||||
offset: (table_struct_ptr_offset as i64).into(),
|
||||
global_type: ptr_type,
|
||||
});
|
||||
|
||||
(
|
||||
table_struct_ptr_ptr,
|
||||
self.env.module.info.imported_tables[import_table_index].1,
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let table_struct_ptr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: table_struct_ptr_ptr,
|
||||
offset: 0.into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
let table_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: table_struct_ptr,
|
||||
offset: (vm::LocalTable::offset_base() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
// The table can reallocate, so the ptr can't be readonly.
|
||||
readonly: false,
|
||||
});
|
||||
|
||||
let table_count = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: table_struct_ptr,
|
||||
offset: (vm::LocalTable::offset_count() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
// The table length can change, so it can't be readonly.
|
||||
readonly: false,
|
||||
});
|
||||
|
||||
Ok(func.create_table(ir::TableData {
|
||||
base_gv: table_base,
|
||||
min_size: (description.minimum as u64).into(),
|
||||
bound_gv: table_count,
|
||||
element_size: (vm::Anyfunc::size() as u64).into(),
|
||||
index_type: ir::types::I32,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Sets up a signature definition in `func`'s preamble.
|
||||
///
|
||||
/// Signature may contain additional argument, but arguments marked as ArgumentPurpose::Normal`
|
||||
/// must correspond to the arguments in the wasm signature
|
||||
fn make_indirect_sig(
|
||||
&mut self,
|
||||
func: &mut ir::Function,
|
||||
clif_sig_index: cranelift_wasm::SignatureIndex,
|
||||
) -> cranelift_wasm::WasmResult<ir::SigRef> {
|
||||
// Create a signature reference out of specified signature (with VMContext param added).
|
||||
Ok(func.import_signature(self.generate_signature(clif_sig_index)))
|
||||
}
|
||||
|
||||
/// Sets up an external function definition in the preamble of `func` that can be used to
|
||||
/// directly call the function `index`.
|
||||
///
|
||||
/// The index space covers both imported functions and functions defined in the current module.
|
||||
fn make_direct_func(
|
||||
&mut self,
|
||||
func: &mut ir::Function,
|
||||
func_index: cranelift_wasm::FuncIndex,
|
||||
) -> cranelift_wasm::WasmResult<ir::FuncRef> {
|
||||
// Get signature of function.
|
||||
let signature_index = self.env.get_func_type(func_index);
|
||||
|
||||
// Create a signature reference from specified signature (with VMContext param added).
|
||||
let signature = func.import_signature(self.generate_signature(signature_index));
|
||||
|
||||
// Get name of function.
|
||||
let name = ir::ExternalName::user(0, func_index.as_u32());
|
||||
|
||||
// Create function reference from fuction data.
|
||||
Ok(func.import_function(ir::ExtFuncData {
|
||||
name,
|
||||
signature,
|
||||
// Make this colocated so all calls between local functions are relative.
|
||||
colocated: true,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Generates an indirect call IR with `callee` and `call_args`.
|
||||
///
|
||||
/// Inserts instructions at `pos` to the function `callee` in the table
|
||||
/// `table_index` with WebAssembly signature `sig_index`
|
||||
#[cfg_attr(feature = "cargo-clippy", allow(clippy::too_many_arguments))]
|
||||
fn translate_call_indirect(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
_table_index: cranelift_wasm::TableIndex,
|
||||
table: ir::Table,
|
||||
clif_sig_index: cranelift_wasm::SignatureIndex,
|
||||
sig_ref: ir::SigRef,
|
||||
callee: ir::Value,
|
||||
call_args: &[ir::Value],
|
||||
) -> cranelift_wasm::WasmResult<ir::Inst> {
|
||||
// Get the pointer type based on machine's pointer size.
|
||||
let ptr_type = self.pointer_type();
|
||||
|
||||
// The `callee` value is an index into a table of Anyfunc structures.
|
||||
let entry_addr = pos.ins().table_addr(ptr_type, table, callee, 0);
|
||||
|
||||
let mflags = ir::MemFlags::trusted();
|
||||
|
||||
let func_ptr = pos.ins().load(
|
||||
ptr_type,
|
||||
mflags,
|
||||
entry_addr,
|
||||
vm::Anyfunc::offset_func() as i32,
|
||||
);
|
||||
|
||||
let vmctx_ptr = {
|
||||
let loaded_vmctx_ptr = pos.ins().load(
|
||||
ptr_type,
|
||||
mflags,
|
||||
entry_addr,
|
||||
vm::Anyfunc::offset_vmctx() as i32,
|
||||
);
|
||||
|
||||
let argument_vmctx_ptr = pos
|
||||
.func
|
||||
.special_param(ir::ArgumentPurpose::VMContext)
|
||||
.expect("missing vmctx parameter");
|
||||
|
||||
// If the loaded vmctx ptr is zero, use the caller vmctx, else use the callee (loaded) vmctx.
|
||||
pos.ins()
|
||||
.select(loaded_vmctx_ptr, loaded_vmctx_ptr, argument_vmctx_ptr)
|
||||
};
|
||||
|
||||
let found_sig = pos.ins().load(
|
||||
ir::types::I32,
|
||||
mflags,
|
||||
entry_addr,
|
||||
vm::Anyfunc::offset_sig_id() as i32,
|
||||
);
|
||||
|
||||
pos.ins().trapz(func_ptr, ir::TrapCode::IndirectCallToNull);
|
||||
|
||||
let expected_sig = {
|
||||
let sig_index_global = pos.func.create_global_value(ir::GlobalValueData::Symbol {
|
||||
// The index of the `ExternalName` is the undeduplicated, signature index.
|
||||
name: ir::ExternalName::user(
|
||||
call_names::SIG_NAMESPACE,
|
||||
clif_sig_index.index() as u32,
|
||||
),
|
||||
offset: 0.into(),
|
||||
colocated: false,
|
||||
});
|
||||
|
||||
pos.ins().symbol_value(ir::types::I64, sig_index_global)
|
||||
|
||||
// let dynamic_sigindices_array_ptr = pos.ins().load(
|
||||
// ptr_type,
|
||||
// mflags,
|
||||
|
||||
// )
|
||||
|
||||
// let expected_sig = pos.ins().iconst(ir::types::I32, sig_index.index() as i64);
|
||||
|
||||
// self.env.deduplicated[clif_sig_index]
|
||||
};
|
||||
|
||||
let not_equal_flags = pos.ins().ifcmp(found_sig, expected_sig);
|
||||
|
||||
pos.ins().trapif(
|
||||
ir::condcodes::IntCC::NotEqual,
|
||||
not_equal_flags,
|
||||
ir::TrapCode::BadSignature,
|
||||
);
|
||||
|
||||
// Build a value list for the indirect call instruction containing the call_args
|
||||
// and the vmctx parameter.
|
||||
let mut args = Vec::with_capacity(call_args.len() + 1);
|
||||
args.push(vmctx_ptr);
|
||||
args.extend(call_args.iter().cloned());
|
||||
|
||||
Ok(pos.ins().call_indirect(sig_ref, func_ptr, &args))
|
||||
}
|
||||
|
||||
/// Generates a call IR with `callee` and `call_args` and inserts it at `pos`
|
||||
/// TODO: add support for imported functions
|
||||
fn translate_call(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
clif_callee_index: cranelift_wasm::FuncIndex,
|
||||
callee: ir::FuncRef,
|
||||
call_args: &[ir::Value],
|
||||
) -> cranelift_wasm::WasmResult<ir::Inst> {
|
||||
let callee_index: FuncIndex = Converter(clif_callee_index).into();
|
||||
let ptr_type = self.pointer_type();
|
||||
|
||||
match callee_index.local_or_import(&self.env.module.info) {
|
||||
LocalOrImport::Local(local_function_index) => {
|
||||
// this is an internal function
|
||||
let vmctx = pos
|
||||
.func
|
||||
.special_param(ir::ArgumentPurpose::VMContext)
|
||||
.expect("missing vmctx parameter");
|
||||
|
||||
let mut args = Vec::with_capacity(call_args.len() + 1);
|
||||
args.push(vmctx);
|
||||
args.extend(call_args.iter().cloned());
|
||||
|
||||
let sig_ref = pos.func.dfg.ext_funcs[callee].signature;
|
||||
let function_ptr = {
|
||||
let mflags = ir::MemFlags::trusted();
|
||||
|
||||
let function_array_ptr = pos.ins().load(
|
||||
ptr_type,
|
||||
mflags,
|
||||
vmctx,
|
||||
vm::Ctx::offset_local_functions() as i32,
|
||||
);
|
||||
|
||||
pos.ins().load(
|
||||
ptr_type,
|
||||
mflags,
|
||||
function_array_ptr,
|
||||
(local_function_index.index() as i32) * 8,
|
||||
)
|
||||
};
|
||||
|
||||
Ok(pos.ins().call_indirect(sig_ref, function_ptr, &args))
|
||||
}
|
||||
LocalOrImport::Import(imported_func_index) => {
|
||||
// this is an imported function
|
||||
let vmctx = pos.func.create_global_value(ir::GlobalValueData::VMContext);
|
||||
|
||||
let imported_funcs = pos.func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: (vm::Ctx::offset_imported_funcs() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
let imported_func_offset =
|
||||
imported_func_index.index() * vm::ImportedFunc::size() as usize;
|
||||
|
||||
let imported_func_struct_addr =
|
||||
pos.func.create_global_value(ir::GlobalValueData::IAddImm {
|
||||
base: imported_funcs,
|
||||
offset: (imported_func_offset as i64).into(),
|
||||
global_type: ptr_type,
|
||||
});
|
||||
|
||||
let imported_func_addr = pos.func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: imported_func_struct_addr,
|
||||
offset: (vm::ImportedFunc::offset_func() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
let imported_vmctx_addr = pos.func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: imported_func_struct_addr,
|
||||
offset: (vm::ImportedFunc::offset_vmctx() as i32).into(),
|
||||
global_type: ptr_type,
|
||||
readonly: true,
|
||||
});
|
||||
|
||||
let imported_func_addr = pos.ins().global_value(ptr_type, imported_func_addr);
|
||||
let imported_vmctx_addr = pos.ins().global_value(ptr_type, imported_vmctx_addr);
|
||||
|
||||
let sig_ref = pos.func.dfg.ext_funcs[callee].signature;
|
||||
|
||||
let mut args = Vec::with_capacity(call_args.len() + 1);
|
||||
args.push(imported_vmctx_addr);
|
||||
args.extend(call_args.iter().cloned());
|
||||
|
||||
Ok(pos
|
||||
.ins()
|
||||
.call_indirect(sig_ref, imported_func_addr, &args[..]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates code corresponding to wasm `memory.grow`.
|
||||
///
|
||||
/// `index` refers to the linear memory to query.
|
||||
///
|
||||
/// `heap` refers to the IR generated by `make_heap`.
|
||||
///
|
||||
/// `val` refers the value to grow the memory by.
|
||||
fn translate_memory_grow(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
clif_mem_index: cranelift_wasm::MemoryIndex,
|
||||
_heap: ir::Heap,
|
||||
by_value: ir::Value,
|
||||
) -> cranelift_wasm::WasmResult<ir::Value> {
|
||||
let signature = pos.func.import_signature(ir::Signature {
|
||||
call_conv: self.target_config().default_call_conv,
|
||||
params: vec![
|
||||
ir::AbiParam::special(self.pointer_type(), ir::ArgumentPurpose::VMContext),
|
||||
ir::AbiParam::new(ir::types::I32),
|
||||
ir::AbiParam::new(ir::types::I32),
|
||||
],
|
||||
returns: vec![ir::AbiParam::new(ir::types::I32)],
|
||||
});
|
||||
|
||||
let mem_index: MemoryIndex = Converter(clif_mem_index).into();
|
||||
|
||||
let (namespace, mem_index, description) =
|
||||
match mem_index.local_or_import(&self.env.module.info) {
|
||||
LocalOrImport::Local(local_mem_index) => (
|
||||
call_names::LOCAL_NAMESPACE,
|
||||
local_mem_index.index(),
|
||||
self.env.module.info.memories[local_mem_index],
|
||||
),
|
||||
LocalOrImport::Import(import_mem_index) => (
|
||||
call_names::IMPORT_NAMESPACE,
|
||||
import_mem_index.index(),
|
||||
self.env.module.info.imported_memories[import_mem_index].1,
|
||||
),
|
||||
};
|
||||
|
||||
let name_index = match description.memory_type() {
|
||||
MemoryType::Dynamic => call_names::DYNAMIC_MEM_GROW,
|
||||
MemoryType::Static => call_names::STATIC_MEM_GROW,
|
||||
MemoryType::SharedStatic => call_names::SHARED_STATIC_MEM_GROW,
|
||||
};
|
||||
|
||||
let name = ir::ExternalName::user(namespace, name_index);
|
||||
|
||||
let mem_grow_func = pos.func.import_function(ir::ExtFuncData {
|
||||
name,
|
||||
signature,
|
||||
colocated: false,
|
||||
});
|
||||
|
||||
let const_mem_index = pos.ins().iconst(ir::types::I32, mem_index as i64);
|
||||
|
||||
let vmctx = pos
|
||||
.func
|
||||
.special_param(ir::ArgumentPurpose::VMContext)
|
||||
.expect("missing vmctx parameter");
|
||||
|
||||
let call_inst = pos
|
||||
.ins()
|
||||
.call(mem_grow_func, &[vmctx, const_mem_index, by_value]);
|
||||
|
||||
Ok(*pos.func.dfg.inst_results(call_inst).first().unwrap())
|
||||
}
|
||||
|
||||
/// Generates code corresponding to wasm `memory.size`.
|
||||
///
|
||||
/// `index` refers to the linear memory to query.
|
||||
///
|
||||
/// `heap` refers to the IR generated by `make_heap`.
|
||||
fn translate_memory_size(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
clif_mem_index: cranelift_wasm::MemoryIndex,
|
||||
_heap: ir::Heap,
|
||||
) -> cranelift_wasm::WasmResult<ir::Value> {
|
||||
let signature = pos.func.import_signature(ir::Signature {
|
||||
call_conv: self.target_config().default_call_conv,
|
||||
params: vec![
|
||||
ir::AbiParam::special(self.pointer_type(), ir::ArgumentPurpose::VMContext),
|
||||
ir::AbiParam::new(ir::types::I32),
|
||||
],
|
||||
returns: vec![ir::AbiParam::new(ir::types::I32)],
|
||||
});
|
||||
|
||||
let mem_index: MemoryIndex = Converter(clif_mem_index).into();
|
||||
|
||||
let (namespace, mem_index, description) =
|
||||
match mem_index.local_or_import(&self.env.module.info) {
|
||||
LocalOrImport::Local(local_mem_index) => (
|
||||
call_names::LOCAL_NAMESPACE,
|
||||
local_mem_index.index(),
|
||||
self.env.module.info.memories[local_mem_index],
|
||||
),
|
||||
LocalOrImport::Import(import_mem_index) => (
|
||||
call_names::IMPORT_NAMESPACE,
|
||||
import_mem_index.index(),
|
||||
self.env.module.info.imported_memories[import_mem_index].1,
|
||||
),
|
||||
};
|
||||
|
||||
let name_index = match description.memory_type() {
|
||||
MemoryType::Dynamic => call_names::DYNAMIC_MEM_SIZE,
|
||||
MemoryType::Static => call_names::STATIC_MEM_SIZE,
|
||||
MemoryType::SharedStatic => call_names::SHARED_STATIC_MEM_SIZE,
|
||||
};
|
||||
|
||||
let name = ir::ExternalName::user(namespace, name_index);
|
||||
|
||||
let mem_grow_func = pos.func.import_function(ir::ExtFuncData {
|
||||
name,
|
||||
signature,
|
||||
colocated: false,
|
||||
});
|
||||
|
||||
let const_mem_index = pos.ins().iconst(ir::types::I32, mem_index as i64);
|
||||
let vmctx = pos
|
||||
.func
|
||||
.special_param(ir::ArgumentPurpose::VMContext)
|
||||
.expect("missing vmctx parameter");
|
||||
|
||||
let call_inst = pos.ins().call(mem_grow_func, &[vmctx, const_mem_index]);
|
||||
|
||||
Ok(*pos.func.dfg.inst_results(call_inst).first().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
|
||||
@ -331,10 +989,12 @@ impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
|
||||
}
|
||||
|
||||
fn feed_param(&mut self, _ty: WpType) -> Result<(), CodegenError> {
|
||||
self.next_local += 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn feed_local(&mut self, _ty: WpType, _n: usize) -> Result<(), CodegenError> {
|
||||
fn feed_local(&mut self, ty: WpType, n: usize) -> Result<(), CodegenError> {
|
||||
cranelift_wasm::declare_locals(self.builder.as_mut().unwrap(), n as u32, ty, &mut self.next_local);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -349,14 +1009,37 @@ impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let builder = &self.builder;
|
||||
let builder = self.builder.as_mut().unwrap();
|
||||
//let func_environment = FuncEnv::new();
|
||||
//let state = TranslationState::new();
|
||||
//translate_operator(*op, builder, state, func_environment);
|
||||
translate_operator(*op, builder, &mut self.func_translator.state, self);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finalize(&mut self) -> Result<(), CodegenError> {
|
||||
let return_mode = self.return_mode();
|
||||
let state = &mut self.func_translator.state;
|
||||
let builder = self.builder.as_mut().unwrap();
|
||||
|
||||
// The final `End` operator left us in the exit block where we need to manually add a return
|
||||
// instruction.
|
||||
//
|
||||
// If the exit block is unreachable, it may not have the correct arguments, so we would
|
||||
// generate a return instruction that doesn't match the signature.
|
||||
if state.reachable {
|
||||
debug_assert!(builder.is_pristine());
|
||||
if !builder.is_unreachable() {
|
||||
match return_mode {
|
||||
ReturnMode::NormalReturns => builder.ins().return_(&state.stack),
|
||||
ReturnMode::FallthroughReturn => builder.ins().fallthrough_return(&state.stack),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Discard any remaining values on the stack. Either we just returned them,
|
||||
// or the end of the function is unreachable.
|
||||
state.stack.clear();
|
||||
|
||||
self.builder.as_mut().unwrap().finalize();
|
||||
Ok(())
|
||||
}
|
||||
@ -378,8 +1061,12 @@ impl CraneliftModuleCodeGenerator {
|
||||
Converter(sig_index).into()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
impl CraneliftFunctionCodeGenerator {
|
||||
pub fn return_mode(&self) -> ReturnMode {
|
||||
ReturnMode::NormalReturns
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a signature with VMContext as the last param
|
||||
|
Loading…
Reference in New Issue
Block a user