mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-26 23:21:35 +00:00
Implement MCG.{finalize, from_cache}
This commit is contained in:
@ -1,32 +1,43 @@
|
|||||||
// Parts of the following code are Copyright 2018 Cranelift Developers
|
// 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
|
// 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, relocation::call_names};
|
use crate::{
|
||||||
|
cache::{BackendCache, CacheGenerator},
|
||||||
|
func_env::FuncEnv,
|
||||||
|
get_isa, module,
|
||||||
|
module::{Converter, Module},
|
||||||
|
relocation::call_names,
|
||||||
|
resolver::FuncResolverBuilder,
|
||||||
|
signal::Caller,
|
||||||
|
trampoline::Trampolines,
|
||||||
|
};
|
||||||
|
|
||||||
|
use cranelift_codegen::entity::EntityRef;
|
||||||
|
use cranelift_codegen::ir::{self, Ebb, InstBuilder, ValueLabel};
|
||||||
|
use cranelift_codegen::timing;
|
||||||
|
use cranelift_codegen::{cursor::FuncCursor, isa};
|
||||||
|
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
|
||||||
|
use cranelift_wasm::{self, translate_module, FuncTranslator, ModuleEnvironment};
|
||||||
|
use cranelift_wasm::{get_vmctx_value_label, translate_operator, TranslationState};
|
||||||
|
use cranelift_wasm::{FuncEnvironment, ReturnMode, WasmError, WasmResult};
|
||||||
|
use std::mem;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use wasmer_runtime_core::error::CompileError;
|
||||||
use wasmer_runtime_core::{
|
use wasmer_runtime_core::{
|
||||||
backend::{Backend, CacheGen, Token},
|
backend::{Backend, CacheGen, Token},
|
||||||
cache::{Artifact, Error as CacheError},
|
cache::{Artifact, Error as CacheError},
|
||||||
codegen::*,
|
codegen::*,
|
||||||
module::{ModuleInfo, ModuleInner},
|
|
||||||
structures::{TypedIndex, Map},
|
|
||||||
memory::MemoryType,
|
memory::MemoryType,
|
||||||
|
module::{ModuleInfo, ModuleInner},
|
||||||
|
structures::{Map, TypedIndex},
|
||||||
types::{
|
types::{
|
||||||
FuncSig, FuncIndex,
|
ElementType, FuncIndex, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit, Initializer,
|
||||||
ElementType, GlobalDescriptor, GlobalIndex, GlobalInit, Initializer, LocalFuncIndex,
|
LocalFuncIndex, LocalOrImport, MemoryDescriptor, MemoryIndex, SigIndex, TableDescriptor,
|
||||||
LocalOrImport, MemoryDescriptor, SigIndex, TableDescriptor, Value, MemoryIndex, TableIndex,
|
TableIndex, Value,
|
||||||
},
|
},
|
||||||
vm,
|
vm,
|
||||||
};
|
};
|
||||||
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 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};
|
|
||||||
|
|
||||||
pub struct CraneliftModuleCodeGenerator {
|
pub struct CraneliftModuleCodeGenerator {
|
||||||
isa: Box<isa::TargetIsa>,
|
isa: Box<isa::TargetIsa>,
|
||||||
@ -37,9 +48,7 @@ pub struct CraneliftModuleCodeGenerator {
|
|||||||
func_bodies: Map<LocalFuncIndex, ir::Function>,
|
func_bodies: Map<LocalFuncIndex, ir::Function>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ClifFuncEnv {
|
pub struct ClifFuncEnv {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
||||||
for CraneliftModuleCodeGenerator
|
for CraneliftModuleCodeGenerator
|
||||||
@ -64,23 +73,28 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_function(&mut self, module_info: &ModuleInfo) -> Result<&mut CraneliftFunctionCodeGenerator, CodegenError> {
|
fn next_function(
|
||||||
|
&mut self,
|
||||||
|
module_info: &ModuleInfo,
|
||||||
|
) -> Result<&mut CraneliftFunctionCodeGenerator, CodegenError> {
|
||||||
// define_function_body(
|
// define_function_body(
|
||||||
|
|
||||||
let mut func_translator = FuncTranslator::new();
|
let mut func_translator = FuncTranslator::new();
|
||||||
|
|
||||||
// let func_body = {
|
// let func_body = {
|
||||||
|
|
||||||
|
|
||||||
// let mut func_env = FuncEnv::new(self);
|
// let mut func_env = FuncEnv::new(self);
|
||||||
|
|
||||||
// TODO should func_index come from self.functions?
|
// TODO should func_index come from self.functions?
|
||||||
let func_index = self.func_bodies.next_index();
|
let func_index = self.func_bodies.next_index();
|
||||||
let name = ir::ExternalName::user(0, func_index.index() as u32);
|
let name = ir::ExternalName::user(0, func_index.index() as u32);
|
||||||
|
|
||||||
let sig = generate_signature(self,
|
let sig = generate_signature(
|
||||||
self.get_func_type(&module_info, Converter(func_index.convert_up(&module_info)).into()),
|
self,
|
||||||
|
self.get_func_type(
|
||||||
|
&module_info,
|
||||||
|
Converter(func_index.convert_up(&module_info)).into(),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut func = ir::Function::with_name_signature(name, sig);
|
let mut func = ir::Function::with_name_signature(name, sig);
|
||||||
@ -93,9 +107,11 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
|||||||
func_body: func,
|
func_body: func,
|
||||||
func_translator,
|
func_translator,
|
||||||
next_local: 0,
|
next_local: 0,
|
||||||
// translator:
|
|
||||||
};
|
};
|
||||||
let builder = FunctionBuilder::new(&mut func_env.func_body, &mut func_env.func_translator.func_ctx);
|
let builder = FunctionBuilder::new(
|
||||||
|
&mut func_env.func_body,
|
||||||
|
&mut func_env.func_translator.func_ctx,
|
||||||
|
);
|
||||||
func_env.builder = Some(builder);
|
func_env.builder = Some(builder);
|
||||||
|
|
||||||
let mut builder = func_env.builder.as_mut().unwrap();
|
let mut builder = func_env.builder.as_mut().unwrap();
|
||||||
@ -117,8 +133,9 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
|||||||
// function and its return values.
|
// function and its return values.
|
||||||
let exit_block = builder.create_ebb();
|
let exit_block = builder.create_ebb();
|
||||||
builder.append_ebb_params_for_function_returns(exit_block);
|
builder.append_ebb_params_for_function_returns(exit_block);
|
||||||
func_translator.state.initialize(&builder.func.signature, exit_block);
|
func_translator
|
||||||
|
.state
|
||||||
|
.initialize(&builder.func.signature, exit_block);
|
||||||
|
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
{
|
{
|
||||||
@ -294,7 +311,26 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
|||||||
self,
|
self,
|
||||||
_module_info: &ModuleInfo,
|
_module_info: &ModuleInfo,
|
||||||
) -> Result<(Caller, Box<dyn CacheGen>), CodegenError> {
|
) -> Result<(Caller, Box<dyn CacheGen>), CodegenError> {
|
||||||
unimplemented!()
|
let (func_resolver_builder, handler_data) =
|
||||||
|
FuncResolverBuilder::new(self.isa, functions, &self.info)?;
|
||||||
|
|
||||||
|
let trampolines = Arc::new(Trampolines::new(self.isa, &self.info));
|
||||||
|
|
||||||
|
let (func_resolver, backend_cache) = func_resolver_builder.finalize(
|
||||||
|
&self.info.signatures,
|
||||||
|
Arc::clone(&trampolines),
|
||||||
|
handler_data.clone(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let cache_gen = Box::new(CacheGenerator::new(
|
||||||
|
backend_cache,
|
||||||
|
Arc::clone(&func_resolver.memory),
|
||||||
|
));
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
Caller::new(handler_data, trampolines, func_resolver),
|
||||||
|
cache_gen,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), CodegenError> {
|
fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), CodegenError> {
|
||||||
@ -314,8 +350,16 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn from_cache(_cache: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
|
unsafe fn from_cache(cache: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
|
||||||
unimplemented!()
|
module::Module::from_cache(cache)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<CompileError> for CodegenError {
|
||||||
|
fn from(other: CompileError) -> CodegenError {
|
||||||
|
CodegenError {
|
||||||
|
message: format!("{:?}", other),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -983,6 +1027,26 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CraneliftFunctionCodeGenerator {
|
||||||
|
/// Creates a signature with VMContext as the last param
|
||||||
|
pub fn generate_signature(
|
||||||
|
&self,
|
||||||
|
clif_sig_index: cranelift_wasm::SignatureIndex,
|
||||||
|
) -> ir::Signature {
|
||||||
|
// Get signature
|
||||||
|
let mut signature = self.env.signatures[Converter(clif_sig_index).into()].clone();
|
||||||
|
|
||||||
|
// Add the vmctx parameter type to it
|
||||||
|
signature.params.insert(
|
||||||
|
0,
|
||||||
|
ir::AbiParam::special(self.pointer_type(), ir::ArgumentPurpose::VMContext),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Return signature
|
||||||
|
signature
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
|
impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
|
||||||
fn feed_return(&mut self, _ty: WpType) -> Result<(), CodegenError> {
|
fn feed_return(&mut self, _ty: WpType) -> Result<(), CodegenError> {
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -994,7 +1058,12 @@ impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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);
|
cranelift_wasm::declare_locals(
|
||||||
|
self.builder.as_mut().unwrap(),
|
||||||
|
n as u32,
|
||||||
|
ty,
|
||||||
|
&mut self.next_local,
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1060,7 +1129,6 @@ impl CraneliftModuleCodeGenerator {
|
|||||||
let sig_index: SigIndex = module_info.func_assoc[Converter(func_index).into()];
|
let sig_index: SigIndex = module_info.func_assoc[Converter(func_index).into()];
|
||||||
Converter(sig_index).into()
|
Converter(sig_index).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CraneliftFunctionCodeGenerator {
|
impl CraneliftFunctionCodeGenerator {
|
||||||
@ -1091,7 +1159,6 @@ fn pointer_type(mcg: &CraneliftModuleCodeGenerator) -> ir::Type {
|
|||||||
ir::Type::int(u16::from(mcg.isa.frontend_config().pointer_bits())).unwrap()
|
ir::Type::int(u16::from(mcg.isa.frontend_config().pointer_bits())).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Declare local variables for the signature parameters that correspond to WebAssembly locals.
|
/// Declare local variables for the signature parameters that correspond to WebAssembly locals.
|
||||||
///
|
///
|
||||||
/// Return the number of local variables declared.
|
/// Return the number of local variables declared.
|
||||||
@ -1119,4 +1186,3 @@ fn declare_wasm_parameters(builder: &mut FunctionBuilder, entry_block: Ebb) -> u
|
|||||||
|
|
||||||
next_local
|
next_local
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use crate::{module::Converter,
|
use crate::{module::Converter, module_env::ModuleEnv, relocation::call_names};
|
||||||
module_env::ModuleEnv, relocation::call_names};
|
|
||||||
use cranelift_codegen::{
|
use cranelift_codegen::{
|
||||||
cursor::FuncCursor,
|
cursor::FuncCursor,
|
||||||
ir::{self, InstBuilder},
|
ir::{self, InstBuilder},
|
||||||
|
Reference in New Issue
Block a user