Trying to debug this segfault

This commit is contained in:
greg 2017-01-15 23:43:24 -08:00
parent dd4816624c
commit 06a5de6e32
3 changed files with 33 additions and 2 deletions

View File

@ -3,5 +3,9 @@ fn hella(a, b) {
a + b
}
fn paha(x, y, z) {
x * y * z
}
2 + 8

View File

@ -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;

View File

@ -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<LLVMBasicBlockRef> {
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()) }