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));
@ -2476,7 +2476,7 @@ pub struct LLVMModuleCodeGenerator {
builder: Builder, builder: Builder,
functions: Vec<LLVMFunctionCodeGenerator>, functions: Vec<LLVMFunctionCodeGenerator>,
functions_llvm: Map<LocalFuncIndex, FunctionValue>, functions_llvm: Map<LocalFuncIndex, FunctionValue>,
// signatures: Option<Arc<Map<SigIndex, FuncSig>>>, // signatures: Option<Arc<Map<SigIndex, FuncSig>>>,
signatures: Map<SigIndex, FunctionType>, signatures: Map<SigIndex, FunctionType>,
signatures_raw: Map<SigIndex, FuncSig>, signatures_raw: Map<SigIndex, FuncSig>,
function_signatures: Option<Arc<Map<FuncIndex, SigIndex>>>, function_signatures: Option<Arc<Map<FuncIndex, SigIndex>>>,
@ -2486,7 +2486,7 @@ pub struct LLVMModuleCodeGenerator {
intrinsics: Intrinsics, intrinsics: Intrinsics,
personality_func: FunctionValue, personality_func: FunctionValue,
module: Module, module: Module,
// ctx: CtxType, // ctx: CtxType,
} }
// pub struct LLVMExecutionContext { // pub struct LLVMExecutionContext {
@ -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,75 +2570,73 @@ 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]; //
// // let function = self.functions[func_index];
// let function = self.functions[func_index]; // let mut state = State::new();
// let mut state = State::new(); // let entry_block = context.append_basic_block(&function, "entry");
// let entry_block = context.append_basic_block(&function, "entry"); //
// // let return_block = context.append_basic_block(&function, "return");
// let return_block = context.append_basic_block(&function, "return"); // builder.position_at_end(&return_block);
// builder.position_at_end(&return_block); //
// // let phis: SmallVec<[PhiValue; 1]> = func_sig
// let phis: SmallVec<[PhiValue; 1]> = func_sig // .returns()
// .returns() // .iter()
// .iter() // .map(|&wasmer_ty| type_to_llvm(intrinsics, wasmer_ty))
// .map(|&wasmer_ty| type_to_llvm(intrinsics, wasmer_ty)) // .map(|ty| builder.build_phi(ty, &state.var_name()))
// .map(|ty| builder.build_phi(ty, &state.var_name())) // .collect();
// .collect(); //
// // state.push_block(return_block, phis);
// state.push_block(return_block, phis); // builder.position_at_end(&entry_block);
// builder.position_at_end(&entry_block); //
// // let mut locals = Vec::with_capacity(locals_reader.get_count() as usize); // TODO fix capacity
// let mut locals = Vec::with_capacity(locals_reader.get_count() as usize); // TODO fix capacity //
// // locals.extend(
// locals.extend( // function
// function // .get_param_iter()
// .get_param_iter() // .skip(1)
// .skip(1) // .enumerate()
// .enumerate() // .map(|(index, param)| {
// .map(|(index, param)| { // let ty = param.get_type();
// let ty = param.get_type(); //
// // let alloca = builder.build_alloca(ty, &format!("local{}", index));
// let alloca = builder.build_alloca(ty, &format!("local{}", index)); // builder.build_store(alloca, param);
// builder.build_store(alloca, param); // alloca
// alloca // }),
// }), // );
// ); //
// // let param_len = locals.len();
// let param_len = locals.len(); //
// // let mut local_idx = 0;
// let mut local_idx = 0; // for local in locals_reader.into_iter() {
// for local in locals_reader.into_iter() { // let (count, ty) = local?;
// let (count, ty) = local?; // let wasmer_ty = type_to_type(ty)?;
// let wasmer_ty = type_to_type(ty)?; // let ty = type_to_llvm(intrinsics, wasmer_ty);
// let ty = type_to_llvm(intrinsics, wasmer_ty); //
// // let default_value = match wasmer_ty {
// let default_value = match wasmer_ty { // Type::I32 => intrinsics.i32_zero.as_basic_value_enum(),
// Type::I32 => intrinsics.i32_zero.as_basic_value_enum(), // Type::I64 => intrinsics.i64_zero.as_basic_value_enum(),
// Type::I64 => intrinsics.i64_zero.as_basic_value_enum(), // Type::F32 => intrinsics.f32_zero.as_basic_value_enum(),
// Type::F32 => intrinsics.f32_zero.as_basic_value_enum(), // Type::F64 => intrinsics.f64_zero.as_basic_value_enum(),
// Type::F64 => intrinsics.f64_zero.as_basic_value_enum(), // };
// }; //
// // for _ in 0..count {
// for _ in 0..count { // let alloca = builder.build_alloca(ty, &format!("local{}", param_len + local_idx));
// let alloca = builder.build_alloca(ty, &format!("local{}", param_len + local_idx)); //
// // builder.build_store(alloca, default_value);
// builder.build_store(alloca, default_value); //
// // locals.push(alloca);
// locals.push(alloca); // local_idx += 1;
// local_idx += 1; // }
// } // }
// } //
// // let start_of_code_block = context.append_basic_block(&function, "start_of_code");
// let start_of_code_block = context.append_basic_block(&function, "start_of_code"); // let entry_end_inst = builder.build_unconditional_branch(&start_of_code_block);
// let entry_end_inst = builder.build_unconditional_branch(&start_of_code_block); // builder.position_at_end(&start_of_code_block);
// builder.position_at_end(&start_of_code_block); //
// // 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;
@ -2624,14 +2647,12 @@ 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();
// cache_builder.position_before(&entry_end_inst); // cache_builder.position_before(&entry_end_inst);
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,
@ -2646,7 +2667,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
match *op { match *op {
Operator::Block { ty: _ } | Operator::Loop { ty: _ } | Operator::If { ty: _ } => { Operator::Block { ty: _ } | Operator::Loop { ty: _ } | Operator::If { ty: _ } => {
unreachable_depth += 1; unreachable_depth += 1;
// continue; // continue;
return Ok(()); return Ok(());
} }
Operator::Else => { Operator::Else => {
@ -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),
}
} }
} }
@ -4590,43 +4613,42 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
for LLVMModuleCodeGenerator for LLVMModuleCodeGenerator
{ {
fn new() -> LLVMModuleCodeGenerator { fn new() -> LLVMModuleCodeGenerator {
let context = Context::create(); let context = Context::create();
let module = context.create_module("module"); let module = context.create_module("module");
let builder = context.create_builder(); let builder = context.create_builder();
let intrinsics = Intrinsics::declare(&module, &context); let intrinsics = Intrinsics::declare(&module, &context);
let personality_func = module.add_function(
"__gxx_personality_v0",
intrinsics.i32_ty.fn_type(&[], false),
Some(Linkage::External),
);
let personality_func = module.add_function(
"__gxx_personality_v0",
intrinsics.i32_ty.fn_type(&[], false),
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
// .iter() // .iter()
// .map(|(_, sig)| func_sig_to_llvm(&context, &intrinsics, sig)) // .map(|(_, sig)| func_sig_to_llvm(&context, &intrinsics, sig))
// .collect(); // .collect();
let signatures = Map::new(); let signatures = Map::new();
// let functions: Map<LocalFuncIndex, FunctionValue> = info // let functions: Map<LocalFuncIndex, FunctionValue> = info
// .func_assoc // .func_assoc
// .iter() // .iter()
// .skip(info.imported_functions.len()) // .skip(info.imported_functions.len())
// .map(|(func_index, &sig_index)| { // .map(|(func_index, &sig_index)| {
// let func = module.add_function( // let func = module.add_function(
// &format!("fn{}", func_index.index()), // &format!("fn{}", func_index.index()),
// signatures[sig_index], // signatures[sig_index],
// Some(Linkage::External), // Some(Linkage::External),
// ); // );
// func.set_personality_function(personality_func); // func.set_personality_function(personality_func);
// func // func
// }) // })
// .collect(); // .collect();
let functions = Map::new(); let functions = Map::new();
// let mut ctx = intrinsics.ctx(info, builder, &function, cache_builder); // let mut ctx = intrinsics.ctx(info, builder, &function, cache_builder);
LLVMModuleCodeGenerator { LLVMModuleCodeGenerator {
context: context, context: context,
@ -4642,7 +4664,7 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
func_import_count: 0, func_import_count: 0,
intrinsics: intrinsics, intrinsics: intrinsics,
personality_func: personality_func, personality_func: personality_func,
// ctx: ctx, // ctx: ctx,
} }
} }
@ -4659,42 +4681,61 @@ 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
// signatures: self.signatures.as_ref().unwrap().clone(), // signatures: self.signatures.as_ref().unwrap().clone(),
// function_signatures: self.function_signatures.as_ref().unwrap().clone(), // function_signatures: self.function_signatures.as_ref().unwrap().clone(),
// //
// assembler: Some(assembler), // assembler: Some(assembler),
// function_labels: Some(function_labels), // function_labels: Some(function_labels),
// br_table_data: Some(br_table_data), // br_table_data: Some(br_table_data),
// 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(())
} }
@ -4722,4 +4766,4 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
self.func_import_count += 1; self.func_import_count += 1;
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")]