Implement feed_local

This commit is contained in:
Brandon Fish
2019-04-30 20:11:44 -05:00
parent 21dd01c3aa
commit ec253c73ab
2 changed files with 187 additions and 143 deletions

View File

@ -25,11 +25,11 @@ use wasmparser::{
Type as WpType, Type as WpType,
}; };
use crate::backend::LLVMBackend;
use crate::intrinsics::{CtxType, GlobalCache, Intrinsics, MemoryCache}; use crate::intrinsics::{CtxType, GlobalCache, Intrinsics, MemoryCache};
use crate::read_info::type_to_type; use crate::read_info::type_to_type;
use crate::state::{ControlFrame, IfElseState, State}; use crate::state::{ControlFrame, IfElseState, State};
use crate::trampolines::generate_trampolines; use crate::trampolines::generate_trampolines;
use crate::backend::LLVMBackend;
fn func_sig_to_llvm(context: &Context, intrinsics: &Intrinsics, sig: &FuncSig) -> FunctionType { fn func_sig_to_llvm(context: &Context, intrinsics: &Intrinsics, sig: &FuncSig) -> FunctionType {
let user_param_types = sig.params().iter().map(|&ty| type_to_llvm(intrinsics, ty)); let user_param_types = sig.params().iter().map(|&ty| type_to_llvm(intrinsics, ty));
@ -2518,8 +2518,8 @@ pub struct LLVMFunctionCodeGenerator {
// br_table_data: Option<Vec<Vec<usize>>>, // br_table_data: Option<Vec<Vec<usize>>>,
// breakpoints: Option<HashMap<AssemblyOffset, Box<Fn(BkptInfo) + Send + Sync + 'static>>>, // breakpoints: Option<HashMap<AssemblyOffset, Box<Fn(BkptInfo) + Send + Sync + 'static>>>,
// returns: SmallVec<[WpType; 1]>, // returns: SmallVec<[WpType; 1]>,
locals: Vec<PointerValue>, locals: Vec<PointerValue>, // Contains params and locals
// num_params: usize, num_params: usize,
// num_locals: usize, // num_locals: usize,
// value_stack: Vec<(Location, LocalOrTemp)>, // value_stack: Vec<(Location, LocalOrTemp)>,
// control_stack: Vec<ControlFrame>, // control_stack: Vec<ControlFrame>,
@ -2529,15 +2529,40 @@ pub struct LLVMFunctionCodeGenerator {
impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator { impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
fn feed_return(&mut self, ty: WpType) -> Result<(), CodegenError> { fn feed_return(&mut self, ty: WpType) -> Result<(), CodegenError> {
unimplemented!() Ok(())
} }
fn feed_param(&mut self, ty: WpType) -> Result<(), CodegenError> { fn feed_param(&mut self, ty: WpType) -> Result<(), CodegenError> {
unimplemented!() Ok(())
} }
fn feed_local(&mut self, ty: WpType, n: usize) -> Result<(), CodegenError> { fn feed_local(&mut self, ty: WpType, n: usize) -> Result<(), CodegenError> {
unimplemented!() let param_len = self.num_params;
let mut local_idx = 0;
// let (count, ty) = local?;
let count = n;
let wasmer_ty = type_to_type(ty)?;
let ty = type_to_llvm(self.intrinsics, wasmer_ty);
let default_value = match wasmer_ty {
Type::I32 => self.intrinsics.i32_zero.as_basic_value_enum(),
Type::I64 => self.intrinsics.i64_zero.as_basic_value_enum(),
Type::F32 => self.intrinsics.f32_zero.as_basic_value_enum(),
Type::F64 => self.intrinsics.f64_zero.as_basic_value_enum(),
};
for _ in 0..count {
let alloca = self
.builder
.build_alloca(ty, &format!("local{}", param_len + local_idx));
self.builder.build_store(alloca, default_value);
self.locals.push(alloca);
local_idx += 1;
}
Ok(())
} }
fn begin_body(&mut self) -> Result<(), CodegenError> { fn begin_body(&mut self) -> Result<(), CodegenError> {
@ -2545,7 +2570,6 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
} }
fn feed_event(&mut self, event: Event, module_info: &ModuleInfo) -> Result<(), CodegenError> { fn feed_event(&mut self, event: Event, module_info: &ModuleInfo) -> Result<(), CodegenError> {
// let sig_index = info.func_assoc[func_index.convert_up(info)]; // let sig_index = info.func_assoc[func_index.convert_up(info)];
// let func_sig = &info.signatures[sig_index]; // let func_sig = &info.signatures[sig_index];
// //
@ -2614,7 +2638,6 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
// let cache_builder = context.create_builder(); // let cache_builder = context.create_builder();
// cache_builder.position_before(&entry_end_inst); // cache_builder.position_before(&entry_end_inst);
let mut state = &mut self.state; let mut state = &mut self.state;
let builder = self.builder; let builder = self.builder;
let context = self.context; let context = self.context;
@ -2624,7 +2647,6 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
let info = module_info; let info = module_info;
let signatures = &self.signatures; let signatures = &self.signatures;
// TODO this should be done only once per function I believe // TODO this should be done only once per function I believe
// just adding here to get compilation // just adding here to get compilation
let cache_builder = context.create_builder(); let cache_builder = context.create_builder();
@ -2632,7 +2654,6 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
let mut ctx = intrinsics.ctx(info, builder, &function, cache_builder); let mut ctx = intrinsics.ctx(info, builder, &function, cache_builder);
// self.ctx; // self.ctx;
let op = match event { let op = match event {
Event::Wasm(x) => x, Event::Wasm(x) => x,
Event::Internal(x) => { Event::Internal(x) => {
@ -2803,7 +2824,8 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
.iter() .iter()
.enumerate() .enumerate()
.map(|(case_index, &depth)| { .map(|(case_index, &depth)| {
let frame_result: Result<&ControlFrame, BinaryReaderError> = state.frame_at_depth(depth); let frame_result: Result<&ControlFrame, BinaryReaderError> =
state.frame_at_depth(depth);
let frame = match frame_result { let frame = match frame_result {
Ok(v) => v, Ok(v) => v,
Err(e) => return Err(e), Err(e) => return Err(e),
@ -4572,7 +4594,6 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
} }
unimplemented!() unimplemented!()
} }
fn finalize(&mut self) -> Result<(), CodegenError> { fn finalize(&mut self) -> Result<(), CodegenError> {
@ -4582,7 +4603,9 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
impl From<BinaryReaderError> for CodegenError { impl From<BinaryReaderError> for CodegenError {
fn from(other: BinaryReaderError) -> CodegenError { fn from(other: BinaryReaderError) -> CodegenError {
CodegenError { message: format!("{:?}", other) } CodegenError {
message: format!("{:?}", other),
}
} }
} }
@ -4602,7 +4625,6 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
Some(Linkage::External), Some(Linkage::External),
); );
// TODO signatures and functions // TODO signatures and functions
// let signatures: Map<SigIndex, FunctionType> = info // let signatures: Map<SigIndex, FunctionType> = info
// .signatures // .signatures
@ -4659,23 +4681,40 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
use std::mem; use std::mem;
let sig_id = self.function_signatures.as_ref().unwrap()[FuncIndex::new(self.func_import_count + self.functions.len())]; let sig_id = self.function_signatures.as_ref().unwrap()
[FuncIndex::new(self.func_import_count + self.functions.len())];
let func_sig = self.signatures_raw[sig_id].clone(); let func_sig = self.signatures_raw[sig_id].clone();
let func = self.module.add_function( let function = self.module.add_function(
&format!("fn{}", self.func_import_count + self.functions.len()), &format!("fn{}", self.func_import_count + self.functions.len()),
self.signatures[sig_id], self.signatures[sig_id],
Some(Linkage::External), Some(Linkage::External),
); );
func.set_personality_function(self.personality_func); function.set_personality_function(self.personality_func);
let mut locals = Vec::new();
locals.extend(
function
.get_param_iter()
.skip(1)
.enumerate()
.map(|(index, param)| {
let ty = param.get_type();
let alloca = self.builder.build_alloca(ty, &format!("local{}", index));
self.builder.build_store(alloca, param);
alloca
}),
);
let num_params = locals.len();
let code = LLVMFunctionCodeGenerator { let code = LLVMFunctionCodeGenerator {
state: State::new(), state: State::new(),
builder: unsafe { ::std::mem::transmute::<&Builder, &'static Builder>(&self.builder) }, builder: unsafe { ::std::mem::transmute::<&Builder, &'static Builder>(&self.builder) },
context: unsafe { ::std::mem::transmute::<&Context, &'static Context>(&self.context) }, context: unsafe { ::std::mem::transmute::<&Context, &'static Context>(&self.context) },
function: func, function,
func_sig: func_sig, func_sig: func_sig,
locals: vec![], locals,
signatures: self.signatures.clone(), signatures: self.signatures.clone(),
// context: self.fu // context: self.fu
@ -4688,13 +4727,15 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
// breakpoints: Some(breakpoints), // breakpoints: Some(breakpoints),
// returns: smallvec![], // returns: smallvec![],
// locals: vec![], // locals: vec![],
// num_params: 0, num_params,
// num_locals: 0, // num_locals: 0,
// value_stack: vec![], // value_stack: vec![],
// control_stack: vec![], // control_stack: vec![],
// machine: Machine::new(), // machine: Machine::new(),
// unreachable_depth: 0, // unreachable_depth: 0,
intrinsics: unsafe { ::std::mem::transmute::<&Intrinsics, &'static Intrinsics>(&self.intrinsics) }, intrinsics: unsafe {
::std::mem::transmute::<&Intrinsics, &'static Intrinsics>(&self.intrinsics)
},
}; };
self.functions.push(code); self.functions.push(code);
Ok(self.functions.last_mut().unwrap()) Ok(self.functions.last_mut().unwrap())
@ -4713,7 +4754,10 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
Ok(()) Ok(())
} }
fn feed_function_signatures(&mut self, assoc: Map<FuncIndex, SigIndex>) -> Result<(), CodegenError> { fn feed_function_signatures(
&mut self,
assoc: Map<FuncIndex, SigIndex>,
) -> Result<(), CodegenError> {
self.function_signatures = Some(Arc::new(assoc)); self.function_signatures = Some(Arc::new(assoc));
Ok(()) Ok(())
} }

View File

@ -18,8 +18,8 @@ fn get_compiler() -> impl Compiler {
#[cfg(feature = "llvm")] #[cfg(feature = "llvm")]
fn get_compiler() -> impl Compiler { fn get_compiler() -> impl Compiler {
use wasmer_llvm_backend::LLVMStreamingCompiler; use wasmer_llvm_backend::LLVMCompiler;
LLVMStreamingCompiler::new() LLVMCompiler::new()
} }
#[cfg(feature = "singlepass")] #[cfg(feature = "singlepass")]