From 06a5de6e32f2b18e4f6cc966f53642726e25ad9c Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 15 Jan 2017 23:43:24 -0800 Subject: [PATCH] Trying to debug this segfault --- compile.schala | 4 ++++ src/compilation.rs | 16 ++++++++++++++-- src/llvm_wrap.rs | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/compile.schala b/compile.schala index 721879b..04c7dc2 100644 --- a/compile.schala +++ b/compile.schala @@ -3,5 +3,9 @@ fn hella(a, b) { a + b } +fn paha(x, y, z) { + x * y * z +} + 2 + 8 diff --git a/src/compilation.rs b/src/compilation.rs index 714429e..431b0aa 100644 --- a/src/compilation.rs +++ b/src/compilation.rs @@ -136,6 +136,10 @@ impl CodeGen for Function { for expr in body { ret = expr.codegen(data); } + + // get basic block of main + let main_bb = LLVMWrap::GetBasicBlocks(data.main_function).get(0).expect("Couldn't get first block of main").clone(); + LLVMWrap::PositionBuilderAtEnd(data.builder, main_bb); ret } } @@ -159,9 +163,14 @@ impl CodeGen for Prototype { &*self.name, function_type); - for (index, param) in LLVMWrap::GetParams(function).iter().enumerate() { + let function_params = LLVMWrap::GetParams(function); + println!("Params: {:?}", function_params); + for (index, param) in function_params.iter().enumerate() { let name = self.parameters.get(index).expect(&format!("Failed this check at index {}", index)); - LLVMWrap::SetValueName(*param, name); + println!("Gonna set value name for : {}, value is {:?}", name, param); + let new = *param; + + LLVMWrap::SetValueName(new, name); } function @@ -198,6 +207,7 @@ impl CodeGen for Expression { int_value } Conditional(ref test, ref then_expr, ref else_expr) => { + /* let condition_value = test.codegen(data); let is_nonzero = LLVMWrap::BuildICmp(data.builder, @@ -228,6 +238,8 @@ impl CodeGen for Expression { LLVMWrap::BuildBr(data.builder, merge_block); LLVMWrap::PositionBuilderAtEnd(data.builder, merge_block); zero + */ + unreachable!() } Block(ref exprs) => { let mut ret = zero; diff --git a/src/llvm_wrap.rs b/src/llvm_wrap.rs index a622ef7..148577e 100644 --- a/src/llvm_wrap.rs +++ b/src/llvm_wrap.rs @@ -178,6 +178,7 @@ pub fn AddIncoming(phi: LLVMValueRef, incoming_values: *mut LLVMValueRef, incomi pub fn SetValueName(value: LLVMValueRef, name: &str) { let name = CString::new(name).unwrap(); + println!("Value: {:?}", value); unsafe { core::LLVMSetValueName(value, name.as_ptr()) } @@ -239,6 +240,20 @@ pub fn BuildICmp(builder: LLVMBuilderRef, unsafe { core::LLVMBuildICmp(builder, op, lhs, rhs, name.as_ptr()) } } +pub fn GetBasicBlocks(function: LLVMValueRef) -> Vec { + let size = CountBasicBlocks(function); + unsafe { + let mut container = Vec::with_capacity(size); + let p = container.as_mut_ptr(); + core::LLVMGetBasicBlocks(function, p); + Vec::from_raw_parts(p, size, size) + } +} + +pub fn CountBasicBlocks(function: LLVMValueRef) -> usize { + unsafe { core::LLVMCountBasicBlocks(function) as usize } +} + pub fn PrintModuleToFile(module: LLVMModuleRef, filename: &str) -> LLVMBool { let out_file = CString::new(filename).unwrap(); unsafe { core::LLVMPrintModuleToFile(module, out_file.as_ptr(), ptr::null_mut()) }