1
0
mirror of https://github.com/fluencelabs/wasmer synced 2025-04-01 07:21:03 +00:00

Implement MCG.{finalize, from_cache}

This commit is contained in:
Brandon Fish 2019-05-19 10:53:33 -05:00
parent 6d5dd5ff21
commit 47479b547f
2 changed files with 336 additions and 271 deletions
lib/clif-backend/src

@ -1,32 +1,43 @@
// 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, 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 wasmer_runtime_core::error::CompileError;
use wasmer_runtime_core::{
backend::{Backend, CacheGen, Token},
cache::{Artifact, Error as CacheError},
codegen::*,
module::{ModuleInfo, ModuleInner},
structures::{TypedIndex, Map},
memory::MemoryType,
module::{ModuleInfo, ModuleInner},
structures::{Map, TypedIndex},
types::{
FuncSig, FuncIndex,
ElementType, GlobalDescriptor, GlobalIndex, GlobalInit, Initializer, LocalFuncIndex,
LocalOrImport, MemoryDescriptor, SigIndex, TableDescriptor, Value, MemoryIndex, TableIndex,
ElementType, FuncIndex, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit, Initializer,
LocalFuncIndex, LocalOrImport, MemoryDescriptor, MemoryIndex, SigIndex, TableDescriptor,
TableIndex, Value,
},
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 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 {
isa: Box<isa::TargetIsa>,
@ -37,9 +48,7 @@ pub struct CraneliftModuleCodeGenerator {
func_bodies: Map<LocalFuncIndex, ir::Function>,
}
pub struct ClifFuncEnv {
}
pub struct ClifFuncEnv {}
impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
for CraneliftModuleCodeGenerator
@ -64,23 +73,28 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
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(
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?
let func_index = self.func_bodies.next_index();
let name = ir::ExternalName::user(0, func_index.index() as u32);
let sig = generate_signature(self,
self.get_func_type(&module_info, Converter(func_index.convert_up(&module_info)).into()),
let sig = generate_signature(
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);
@ -93,9 +107,11 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
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);
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_mut().unwrap();
@ -117,8 +133,9 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
// function and its return values.
let exit_block = builder.create_ebb();
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")]
{
@ -280,8 +297,8 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
pos.ins().jump(entry_ebb, new_ebb_params.as_slice());
}
// func
// };
// func
// };
// Add function body to list of function bodies.
//self.func_bodies.push(func);
@ -294,7 +311,26 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
self,
_module_info: &ModuleInfo,
) -> 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> {
@ -314,8 +350,16 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
Ok(())
}
unsafe fn from_cache(_cache: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
unimplemented!()
unsafe fn from_cache(cache: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
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 {
fn feed_return(&mut self, _ty: WpType) -> Result<(), CodegenError> {
Ok(())
@ -994,7 +1058,12 @@ impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
}
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(())
}
@ -1060,7 +1129,6 @@ impl CraneliftModuleCodeGenerator {
let sig_index: SigIndex = module_info.func_assoc[Converter(func_index).into()];
Converter(sig_index).into()
}
}
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()
}
/// Declare local variables for the signature parameters that correspond to WebAssembly locals.
///
/// Return the number of local variables declared.
@ -1119,4 +1186,3 @@ fn declare_wasm_parameters(builder: &mut FunctionBuilder, entry_block: Ebb) -> u
next_local
}

@ -1,5 +1,4 @@
use crate::{module::Converter,
module_env::ModuleEnv, relocation::call_names};
use crate::{module::Converter, module_env::ModuleEnv, relocation::call_names};
use cranelift_codegen::{
cursor::FuncCursor,
ir::{self, InstBuilder},