From e103ba221c6e402ee3b8c4af905bd820ba6af014 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 17 Jan 2017 19:48:25 -0800 Subject: [PATCH] Conditionals work! --- compile.schala | 11 +++++++++-- src/compilation.rs | 9 +++++---- src/llvm_wrap.rs | 27 ++++++++------------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/compile.schala b/compile.schala index bd317ab..4923424 100644 --- a/compile.schala +++ b/compile.schala @@ -7,7 +7,14 @@ fn paha(x, y, z) { x * y * z } +a = 0 + +c = if a { + 10 +} else { + 20 +} + q = 4 - -q + 1 +q + 1 + c diff --git a/src/compilation.rs b/src/compilation.rs index 64b586d..944cc8e 100644 --- a/src/compilation.rs +++ b/src/compilation.rs @@ -271,10 +271,11 @@ impl CodeGen for Expression { LLVMWrap::PositionBuilderAtEnd(data.builder, merge_block); - let phi = LLVMWrap::BuildPhi(data.builder, int_type, "phinnode"); - - - zero + let phi = LLVMWrap::BuildPhi(data.builder, int_type, "phinode"); + let values = vec![then_return, else_return]; + let blocks = vec![then_block, else_block]; + LLVMWrap::AddIncoming(phi, values, blocks); + phi } Block(ref exprs) => { let mut ret = zero; diff --git a/src/llvm_wrap.rs b/src/llvm_wrap.rs index 401b7d0..6347cce 100644 --- a/src/llvm_wrap.rs +++ b/src/llvm_wrap.rs @@ -170,12 +170,6 @@ pub fn BuildPhi(builder: LLVMBuilderRef, ty: LLVMTypeRef, name: &str) -> LLVMVal 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) { let name = CString::new(name).unwrap(); unsafe { @@ -264,23 +258,18 @@ pub fn PrintModuleToString(module: LLVMModuleRef) -> String { } } -pub fn BuildPhi(builder: LLVMBuilderRef, ty: LLVMTypeRef, name: &str) -> LLVMValueRef { - unsafe { - let name = CString::new(name).unwrap(); - unsafe { core::LLVMBuildPhi(builder, ty, name.as_ptr()) } - } -} +pub fn AddIncoming(phi_node: LLVMValueRef, mut incoming_values: Vec, + mut incoming_blocks: Vec) { -pub fn AddIncoming(phi_node: LLVMValueRef, incoming_values: Vec, - incoming_blocks: Vec) { - - let count = incoming_blocks.len(); - if incoming_values.len() != count { + let count = incoming_blocks.len() as u32; + if incoming_values.len() as u32 != count { 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) } }