Merge branch 'master' into feat-runtime-core-clos-host-function

This commit is contained in:
Ivan Enderlin
2019-11-12 00:55:40 +01:00
93 changed files with 1035 additions and 306 deletions

View File

@ -22,6 +22,7 @@ use inkwell::{
use smallvec::SmallVec;
use std::{
cell::RefCell,
collections::HashMap,
rc::Rc,
sync::{Arc, RwLock},
};
@ -851,6 +852,7 @@ pub struct LLVMModuleCodeGenerator {
signatures: Map<SigIndex, FunctionType>,
signatures_raw: Map<SigIndex, FuncSig>,
function_signatures: Option<Arc<Map<FuncIndex, SigIndex>>>,
llvm_functions: Rc<RefCell<HashMap<FuncIndex, FunctionValue>>>,
func_import_count: usize,
personality_func: FunctionValue,
module: Rc<RefCell<Module>>,
@ -865,6 +867,7 @@ pub struct LLVMFunctionCodeGenerator {
alloca_builder: Option<Builder>,
intrinsics: Option<Intrinsics>,
state: State,
llvm_functions: Rc<RefCell<HashMap<FuncIndex, FunctionValue>>>,
function: FunctionValue,
func_sig: FuncSig,
signatures: Map<SigIndex, FunctionType>,
@ -1692,7 +1695,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
let func_sig = &info.signatures[sigindex];
let (params, func_ptr) = match func_index.local_or_import(info) {
LocalOrImport::Local(local_func_index) => {
LocalOrImport::Local(_) => {
let params: Vec<_> = std::iter::once(ctx.basic())
.chain(
state
@ -1722,15 +1725,9 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
)
.collect();
let func_ptr = ctx.local_func(
local_func_index,
llvm_sig,
intrinsics,
self.module.clone(),
builder,
);
let func_ptr = self.llvm_functions.borrow_mut()[&func_index];
(params, func_ptr)
(params, func_ptr.as_global_value().as_pointer_value())
}
LocalOrImport::Import(import_func_index) => {
let (func_ptr_untyped, ctx_ptr) =
@ -1766,7 +1763,6 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
.collect();
let func_ptr_ty = llvm_sig.ptr_type(AddressSpace::Generic);
let func_ptr = builder.build_pointer_cast(
func_ptr_untyped,
func_ptr_ty,
@ -8019,6 +8015,7 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
signatures: Map::new(),
signatures_raw: Map::new(),
function_signatures: None,
llvm_functions: Rc::new(RefCell::new(HashMap::new())),
func_import_count: 0,
personality_func,
stackmaps: Rc::new(RefCell::new(StackmapRegistry::default())),
@ -8053,15 +8050,11 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
),
};
let sig_id = self.function_signatures.as_ref().unwrap()
[FuncIndex::new(self.func_import_count + self.functions.len())];
let func_index = FuncIndex::new(self.func_import_count + self.functions.len());
let sig_id = self.function_signatures.as_ref().unwrap()[func_index];
let func_sig = self.signatures_raw[sig_id].clone();
let function = self.module.borrow_mut().add_function(
&format!("fn{}", self.func_import_count + self.functions.len()),
self.signatures[sig_id],
Some(Linkage::External),
);
let function = &self.llvm_functions.borrow_mut()[&func_index];
function.set_personality_function(self.personality_func);
let mut state = State::new();
@ -8119,7 +8112,8 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
builder: Some(builder),
alloca_builder: Some(alloca_builder),
intrinsics: Some(intrinsics),
function,
llvm_functions: self.llvm_functions.clone(),
function: *function,
func_sig: func_sig,
locals,
signatures: self.signatures.clone(),
@ -8233,6 +8227,16 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
&mut self,
assoc: Map<FuncIndex, SigIndex>,
) -> Result<(), CodegenError> {
for (index, sig_id) in &assoc {
if index.index() >= self.func_import_count {
let function = self.module.borrow_mut().add_function(
&format!("fn{}", index.index()),
self.signatures[*sig_id],
Some(Linkage::External),
);
self.llvm_functions.borrow_mut().insert(index, function);
}
}
self.function_signatures = Some(Arc::new(assoc));
Ok(())
}

View File

@ -3,9 +3,7 @@ use inkwell::{
builder::Builder,
context::Context,
module::Module,
types::{
BasicType, FloatType, FunctionType, IntType, PointerType, StructType, VectorType, VoidType,
},
types::{BasicType, FloatType, IntType, PointerType, StructType, VectorType, VoidType},
values::{
BasicValue, BasicValueEnum, FloatValue, FunctionValue, InstructionValue, IntValue,
PointerValue, VectorValue,
@ -21,8 +19,7 @@ use wasmer_runtime_core::{
module::ModuleInfo,
structures::TypedIndex,
types::{
GlobalIndex, ImportedFuncIndex, LocalFuncIndex, LocalOrImport, MemoryIndex, SigIndex,
TableIndex, Type,
GlobalIndex, ImportedFuncIndex, LocalOrImport, MemoryIndex, SigIndex, TableIndex, Type,
},
units::Pages,
vm::{Ctx, INTERNALS_SIZE},
@ -900,55 +897,6 @@ impl<'a> CtxType<'a> {
(base_ptr, bounds)
}
pub fn local_func(
&mut self,
index: LocalFuncIndex,
fn_ty: FunctionType,
intrinsics: &Intrinsics,
module: Rc<RefCell<Module>>,
builder: &Builder,
) -> PointerValue {
let local_func_array_ptr_ptr = unsafe {
builder.build_struct_gep(
self.ctx_ptr_value,
offset_to_index(Ctx::offset_local_functions()),
"local_func_array_ptr_ptr",
)
};
let local_func_array_ptr = builder
.build_load(local_func_array_ptr_ptr, "local_func_array_ptr")
.into_pointer_value();
tbaa_label(
module.clone(),
intrinsics,
"context_field_ptr_to_local_funcs",
local_func_array_ptr.as_instruction_value().unwrap(),
None,
);
let local_func_ptr_ptr = unsafe {
builder.build_in_bounds_gep(
local_func_array_ptr,
&[intrinsics.i32_ty.const_int(index.index() as u64, false)],
"local_func_ptr_ptr",
)
};
let local_func_ptr = builder
.build_load(local_func_ptr_ptr, "local_func_ptr")
.into_pointer_value();
tbaa_label(
module.clone(),
intrinsics,
"local_func_ptr",
local_func_ptr.as_instruction_value().unwrap(),
Some(index.index() as u32),
);
builder.build_pointer_cast(
local_func_ptr,
fn_ty.ptr_type(AddressSpace::Generic),
"local_func_ptr",
)
}
pub fn dynamic_sigindex(&mut self, index: SigIndex, intrinsics: &Intrinsics) -> IntValue {
let (cached_sigindices, ctx_ptr_value, cache_builder) = (
&mut self.cached_sigindices,
@ -1042,7 +990,7 @@ impl<'a> CtxType<'a> {
module.clone(),
intrinsics,
field_name,
globals_array_ptr_ptr.as_instruction_value().unwrap(),
global_array_ptr.as_instruction_value().unwrap(),
None,
);
let const_index = intrinsics.i32_ty.const_int(index, false);
@ -1060,7 +1008,7 @@ impl<'a> CtxType<'a> {
module.clone(),
intrinsics,
"global_ptr",
globals_array_ptr_ptr.as_instruction_value().unwrap(),
global_ptr.as_instruction_value().unwrap(),
Some(index as u32),
);