From c5caf9b6dbff43947a0c3e8219627be210e48c9f Mon Sep 17 00:00:00 2001 From: Brandon Fish Date: Fri, 3 May 2019 00:14:25 -0500 Subject: [PATCH] Update LLVM FCG begin_body --- lib/llvm-backend/src/code.rs | 54 +++++++++++++---------- lib/runtime-core/src/codegen.rs | 17 ++++++- lib/runtime-core/src/parse.rs | 2 +- lib/singlepass-backend/src/codegen_x64.rs | 2 +- 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/lib/llvm-backend/src/code.rs b/lib/llvm-backend/src/code.rs index f9bfba26b..8745f7e38 100644 --- a/lib/llvm-backend/src/code.rs +++ b/lib/llvm-backend/src/code.rs @@ -2520,6 +2520,7 @@ pub struct LLVMFunctionCodeGenerator { // returns: SmallVec<[WpType; 1]>, locals: Vec, // Contains params and locals num_params: usize, + ctx: Option>, // num_locals: usize, // value_stack: Vec<(Location, LocalOrTemp)>, // control_stack: Vec, @@ -2565,7 +2566,27 @@ impl FunctionCodeGenerator for LLVMFunctionCodeGenerator { 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(()) } @@ -2637,6 +2658,12 @@ impl FunctionCodeGenerator for LLVMFunctionCodeGenerator { // // let cache_builder = context.create_builder(); // 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 builder = self.builder; @@ -2646,21 +2673,7 @@ impl FunctionCodeGenerator for LLVMFunctionCodeGenerator { let locals = &self.locals; let info = module_info; let signatures = &self.signatures; - - // 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 ctx = self.ctx.as_mut().unwrap(); let mut unreachable_depth = 0; if !state.reachable { @@ -4724,12 +4737,6 @@ impl ModuleCodeGenerator ); 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 { state, builder: unsafe { ::std::mem::transmute::<&Builder, &'static Builder>(&self.builder) }, @@ -4758,13 +4765,14 @@ impl ModuleCodeGenerator intrinsics: unsafe { ::std::mem::transmute::<&Intrinsics, &'static Intrinsics>(&self.intrinsics) }, + ctx: None, }; self.functions.push(code); Ok(self.functions.last_mut().unwrap()) } fn finalize(self, module_info: &ModuleInfo) -> Result { -// self.module.print_to_stderr(); + // self.module.print_to_stderr(); generate_trampolines( module_info, diff --git a/lib/runtime-core/src/codegen.rs b/lib/runtime-core/src/codegen.rs index 210a105be..6694fbb86 100644 --- a/lib/runtime-core/src/codegen.rs +++ b/lib/runtime-core/src/codegen.rs @@ -12,7 +12,9 @@ use smallvec::SmallVec; use std::fmt::Debug; use std::marker::PhantomData; use wasmparser::{Operator, Type as WpType}; +use std::fmt; +#[derive(Debug)] pub enum Event<'a, 'b> { Internal(InternalEvent), Wasm(&'b Operator<'a>), @@ -26,6 +28,19 @@ pub enum InternalEvent { 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 trait ModuleCodeGenerator, RM: RunnableModule, E: Debug> { @@ -246,7 +261,7 @@ pub trait FunctionCodeGenerator { fn feed_local(&mut self, ty: WpType, n: usize) -> Result<(), E>; /// 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. fn feed_event(&mut self, op: Event, module_info: &ModuleInfo) -> Result<(), E>; diff --git a/lib/runtime-core/src/parse.rs b/lib/runtime-core/src/parse.rs index e8fabf194..e7e80fc29 100644 --- a/lib/runtime-core/src/parse.rs +++ b/lib/runtime-core/src/parse.rs @@ -244,7 +244,7 @@ pub fn read_module< ParserState::CodeOperator(ref op) => { if !body_begun { body_begun = true; - fcg.begin_body() + fcg.begin_body(&info) .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; } middlewares diff --git a/lib/singlepass-backend/src/codegen_x64.rs b/lib/singlepass-backend/src/codegen_x64.rs index 783ca13af..86bcde07e 100644 --- a/lib/singlepass-backend/src/codegen_x64.rs +++ b/lib/singlepass-backend/src/codegen_x64.rs @@ -1387,7 +1387,7 @@ impl FunctionCodeGenerator for X64FunctionCode { 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(); a.emit_push(Size::S64, Location::GPR(GPR::RBP)); a.emit_mov(Size::S64, Location::GPR(GPR::RSP), Location::GPR(GPR::RBP));