Update LLVM FCG begin_body

This commit is contained in:
Brandon Fish 2019-05-03 00:14:25 -05:00
parent b016ec6b34
commit c5caf9b6db
4 changed files with 49 additions and 26 deletions

View File

@ -2520,6 +2520,7 @@ pub struct LLVMFunctionCodeGenerator {
// returns: SmallVec<[WpType; 1]>, // returns: SmallVec<[WpType; 1]>,
locals: Vec<PointerValue>, // Contains params and locals locals: Vec<PointerValue>, // Contains params and locals
num_params: usize, num_params: usize,
ctx: Option<CtxType<'static>>,
// num_locals: usize, // num_locals: usize,
// value_stack: Vec<(Location, LocalOrTemp)>, // value_stack: Vec<(Location, LocalOrTemp)>,
// control_stack: Vec<ControlFrame>, // control_stack: Vec<ControlFrame>,
@ -2565,7 +2566,27 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
Ok(()) Ok(())
} }
fn begin_body(&mut self) -> Result<(), CodegenError> { fn begin_body(&mut self, module_info: &ModuleInfo) -> Result<(), CodegenError> {
let start_of_code_block = self
.context
.append_basic_block(&self.function, "start_of_code");
let entry_end_inst = self
.builder
.build_unconditional_branch(&start_of_code_block);
self.builder.position_at_end(&start_of_code_block);
let cache_builder = self.context.create_builder();
cache_builder.position_before(&entry_end_inst);
let module_info =
unsafe { ::std::mem::transmute::<&ModuleInfo, &'static ModuleInfo>(module_info) };
let function = unsafe {
::std::mem::transmute::<&FunctionValue, &'static FunctionValue>(&self.function)
};
let mut ctx = self
.intrinsics
.ctx(module_info, self.builder, function, cache_builder);
self.ctx = Some(ctx);
Ok(()) Ok(())
} }
@ -2637,6 +2658,12 @@ 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 op = match event {
Event::Wasm(x) => x,
Event::Internal(x) => {
return Ok(());
}
};
let mut state = &mut self.state; let mut state = &mut self.state;
let builder = self.builder; let builder = self.builder;
@ -2646,21 +2673,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
let locals = &self.locals; let locals = &self.locals;
let info = module_info; let info = module_info;
let signatures = &self.signatures; let signatures = &self.signatures;
let mut ctx = self.ctx.as_mut().unwrap();
// TODO this should be done only once per function I believe
// just adding here to get compilation
let cache_builder = context.create_builder();
// cache_builder.position_before(&entry_end_inst);
let mut ctx = intrinsics.ctx(info, builder, &function, cache_builder);
// self.ctx;
let op = match event {
Event::Wasm(x) => x,
Event::Internal(x) => {
return Ok(());
//unimplemented!()
}
};
let mut unreachable_depth = 0; let mut unreachable_depth = 0;
if !state.reachable { if !state.reachable {
@ -4724,12 +4737,6 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
); );
let num_params = locals.len(); let num_params = locals.len();
let start_of_code_block = self.context.append_basic_block(&function, "start_of_code");
let entry_end_inst = self
.builder
.build_unconditional_branch(&start_of_code_block);
self.builder.position_at_end(&start_of_code_block);
let code = LLVMFunctionCodeGenerator { let code = LLVMFunctionCodeGenerator {
state, state,
builder: unsafe { ::std::mem::transmute::<&Builder, &'static Builder>(&self.builder) }, builder: unsafe { ::std::mem::transmute::<&Builder, &'static Builder>(&self.builder) },
@ -4758,13 +4765,14 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
intrinsics: unsafe { intrinsics: unsafe {
::std::mem::transmute::<&Intrinsics, &'static Intrinsics>(&self.intrinsics) ::std::mem::transmute::<&Intrinsics, &'static Intrinsics>(&self.intrinsics)
}, },
ctx: None,
}; };
self.functions.push(code); self.functions.push(code);
Ok(self.functions.last_mut().unwrap()) Ok(self.functions.last_mut().unwrap())
} }
fn finalize(self, module_info: &ModuleInfo) -> Result<LLVMBackend, CodegenError> { fn finalize(self, module_info: &ModuleInfo) -> Result<LLVMBackend, CodegenError> {
// self.module.print_to_stderr(); // self.module.print_to_stderr();
generate_trampolines( generate_trampolines(
module_info, module_info,

View File

@ -12,7 +12,9 @@ use smallvec::SmallVec;
use std::fmt::Debug; use std::fmt::Debug;
use std::marker::PhantomData; use std::marker::PhantomData;
use wasmparser::{Operator, Type as WpType}; use wasmparser::{Operator, Type as WpType};
use std::fmt;
#[derive(Debug)]
pub enum Event<'a, 'b> { pub enum Event<'a, 'b> {
Internal(InternalEvent), Internal(InternalEvent),
Wasm(&'b Operator<'a>), Wasm(&'b Operator<'a>),
@ -26,6 +28,19 @@ pub enum InternalEvent {
GetInternal(u32), GetInternal(u32),
} }
impl fmt::Debug for InternalEvent {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
InternalEvent::FunctionBegin(_) => write!(f, "FunctionBegin"),
InternalEvent::FunctionEnd => write!(f, "FunctionEnd"),
InternalEvent::Breakpoint(_) => write!(f, "Breakpoint"),
InternalEvent::SetInternal(_) => write!(f, "SetInternal"),
InternalEvent::GetInternal(_) => write!(f, "GetInternal"),
_ => panic!("unknown event")
}
}
}
pub struct BkptInfo {} pub struct BkptInfo {}
pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule, E: Debug> { pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule, E: Debug> {
@ -246,7 +261,7 @@ pub trait FunctionCodeGenerator<E: Debug> {
fn feed_local(&mut self, ty: WpType, n: usize) -> Result<(), E>; fn feed_local(&mut self, ty: WpType, n: usize) -> Result<(), E>;
/// Called before the first call to `feed_opcode`. /// Called before the first call to `feed_opcode`.
fn begin_body(&mut self) -> Result<(), E>; fn begin_body(&mut self, module_info: &ModuleInfo) -> Result<(), E>;
/// Called for each operator. /// Called for each operator.
fn feed_event(&mut self, op: Event, module_info: &ModuleInfo) -> Result<(), E>; fn feed_event(&mut self, op: Event, module_info: &ModuleInfo) -> Result<(), E>;

View File

@ -244,7 +244,7 @@ pub fn read_module<
ParserState::CodeOperator(ref op) => { ParserState::CodeOperator(ref op) => {
if !body_begun { if !body_begun {
body_begun = true; body_begun = true;
fcg.begin_body() fcg.begin_body(&info)
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
} }
middlewares middlewares

View File

@ -1387,7 +1387,7 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
Ok(()) Ok(())
} }
fn begin_body(&mut self) -> Result<(), CodegenError> { fn begin_body(&mut self, module_info: &ModuleInfo) -> Result<(), CodegenError> {
let a = self.assembler.as_mut().unwrap(); let a = self.assembler.as_mut().unwrap();
a.emit_push(Size::S64, Location::GPR(GPR::RBP)); a.emit_push(Size::S64, Location::GPR(GPR::RBP));
a.emit_mov(Size::S64, Location::GPR(GPR::RSP), Location::GPR(GPR::RBP)); a.emit_mov(Size::S64, Location::GPR(GPR::RSP), Location::GPR(GPR::RBP));