Conditionals work!

This commit is contained in:
greg 2017-01-17 19:48:25 -08:00
parent d5f01a7b1f
commit e103ba221c
3 changed files with 22 additions and 25 deletions

View File

@ -7,7 +7,14 @@ fn paha(x, y, z) {
x * y * z x * y * z
} }
a = 0
c = if a {
10
} else {
20
}
q = 4 q = 4
q + 1 + c
q + 1

View File

@ -271,10 +271,11 @@ impl CodeGen for Expression {
LLVMWrap::PositionBuilderAtEnd(data.builder, merge_block); LLVMWrap::PositionBuilderAtEnd(data.builder, merge_block);
let phi = LLVMWrap::BuildPhi(data.builder, int_type, "phinnode"); let phi = LLVMWrap::BuildPhi(data.builder, int_type, "phinode");
let values = vec![then_return, else_return];
let blocks = vec![then_block, else_block];
zero LLVMWrap::AddIncoming(phi, values, blocks);
phi
} }
Block(ref exprs) => { Block(ref exprs) => {
let mut ret = zero; let mut ret = zero;

View File

@ -170,12 +170,6 @@ pub fn BuildPhi(builder: LLVMBuilderRef, ty: LLVMTypeRef, name: &str) -> LLVMVal
unsafe { core::LLVMBuildPhi(builder, ty, name.as_ptr()) } unsafe { core::LLVMBuildPhi(builder, ty, name.as_ptr()) }
} }
pub fn AddIncoming(phi: LLVMValueRef, incoming_values: *mut LLVMValueRef, incoming_blocks: *mut LLVMBasicBlockRef,
count: u32) {
unsafe { core::LLVMAddIncoming(phi, incoming_values, incoming_blocks, count) }
}
pub fn SetValueName(value: LLVMValueRef, name: &str) { pub fn SetValueName(value: LLVMValueRef, name: &str) {
let name = CString::new(name).unwrap(); let name = CString::new(name).unwrap();
unsafe { unsafe {
@ -264,23 +258,18 @@ pub fn PrintModuleToString(module: LLVMModuleRef) -> String {
} }
} }
pub fn BuildPhi(builder: LLVMBuilderRef, ty: LLVMTypeRef, name: &str) -> LLVMValueRef { pub fn AddIncoming(phi_node: LLVMValueRef, mut incoming_values: Vec<LLVMValueRef>,
unsafe { mut incoming_blocks: Vec<LLVMBasicBlockRef>) {
let name = CString::new(name).unwrap();
unsafe { core::LLVMBuildPhi(builder, ty, name.as_ptr()) }
}
}
pub fn AddIncoming(phi_node: LLVMValueRef, incoming_values: Vec<LLVMValueRef>, let count = incoming_blocks.len() as u32;
incoming_blocks: Vec<LLVMBasicBlockRef>) { if incoming_values.len() as u32 != count {
let count = incoming_blocks.len();
if incoming_values.len() != count {
panic!("Bad invocation of AddIncoming"); panic!("Bad invocation of AddIncoming");
} }
unsafe {
//core::LLVMAddIncoming(phi_node);
unsafe {
let vals = incoming_values.as_mut_ptr();
let blocks = incoming_blocks.as_mut_ptr();
core::LLVMAddIncoming(phi_node, vals, blocks, count)
} }
} }