Compiling functions half-works

This commit is contained in:
greg 2017-01-15 07:23:53 -08:00
parent 8f2d9b900b
commit 748a85db02
3 changed files with 51 additions and 4 deletions

View File

@ -1,2 +1,7 @@
8 < 2 fn hella(a, b) {
a + b
}
2 + 8

View File

@ -117,13 +117,21 @@ impl CodeGen for Statement {
impl CodeGen for Function { impl CodeGen for Function {
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef { fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
/* should have a check here for function already being defined */
let function = self.prototype.codegen(data); let function = self.prototype.codegen(data);
let ref body = self.body; let ref body = self.body;
let return_type = LLVMWrap::Int64TypeInContext(data.context); let return_type = LLVMWrap::Int64TypeInContext(data.context);
let mut ret = LLVMWrap::ConstInt(return_type, 0, false); let mut ret = LLVMWrap::ConstInt(return_type, 0, false);
println!("Getting here"); let block = LLVMWrap::AppendBasicBlockInContext(data.context, function, "entry");
LLVMWrap::PositionBuilderAtEnd(data.builder, block);
//insert function params into variables
for value in LLVMWrap::GetParams(function) {
let name = LLVMWrap::GetValueName(value);
data.variables.insert(name, value);
}
for expr in body { for expr in body {
ret = expr.codegen(data); ret = expr.codegen(data);
@ -151,6 +159,11 @@ impl CodeGen for Prototype {
&*self.name, &*self.name,
function_type); function_type);
for (index, param) in LLVMWrap::GetParams(function).iter().enumerate() {
let name = self.parameters.get(index).expect(&format!("Failed this check at index {}", index));
LLVMWrap::SetValueName(*param, name);
}
function function
} }
} }
@ -164,7 +177,7 @@ impl CodeGen for Expression {
let zero = LLVMWrap::ConstInt(int_type, 0, false); let zero = LLVMWrap::ConstInt(int_type, 0, false);
match *self { match *self {
Variable(ref name) => *data.variables.get(&**name).unwrap(), Variable(ref name) => *data.variables.get(&**name).expect(&format!("Can't find variable {}", name)),
BinExp(Assign, ref left, ref right) => { BinExp(Assign, ref left, ref right) => {
if let Variable(ref name) = **left { if let Variable(ref name) = **left {
let new_value = right.codegen(data); let new_value = right.codegen(data);

View File

@ -6,7 +6,8 @@ use self::llvm_sys::{LLVMIntPredicate, LLVMRealPredicate};
use self::llvm_sys::prelude::*; use self::llvm_sys::prelude::*;
use self::llvm_sys::core; use self::llvm_sys::core;
use std::ptr; use std::ptr;
use std::ffi::CString; use std::ffi::{CString, CStr};
use std::os::raw::c_char;
pub fn create_context() -> LLVMContextRef { pub fn create_context() -> LLVMContextRef {
unsafe { core::LLVMContextCreate() } unsafe { core::LLVMContextCreate() }
@ -175,6 +176,34 @@ pub fn AddIncoming(phi: LLVMValueRef, incoming_values: *mut LLVMValueRef, incomi
unsafe { core::LLVMAddIncoming(phi, incoming_values, incoming_blocks, count) } unsafe { core::LLVMAddIncoming(phi, incoming_values, incoming_blocks, count) }
} }
pub fn SetValueName(value: LLVMValueRef, name: &str) {
let name = CString::new(name).unwrap();
unsafe {
core::LLVMSetValueName(value, name.as_ptr())
}
}
pub fn GetValueName(value: LLVMValueRef) -> String {
unsafe {
let name_ptr: *const c_char = core::LLVMGetValueName(value);
CStr::from_ptr(name_ptr).to_string_lossy().into_owned()
}
}
pub fn GetParams(function: LLVMValueRef) -> Vec<LLVMValueRef> {
let size = CountParams(function);
unsafe {
let mut container = Vec::with_capacity(size);
let p = container.as_mut_ptr();
core::LLVMGetParams(function, p);
Vec::from_raw_parts(p, size, size)
}
}
pub fn CountParams(function: LLVMValueRef) -> usize {
unsafe { core::LLVMCountParams(function) as usize }
}
pub fn BuildFCmp(builder: LLVMBuilderRef, pub fn BuildFCmp(builder: LLVMBuilderRef,
op: LLVMRealPredicate, op: LLVMRealPredicate,
lhs: LLVMValueRef, lhs: LLVMValueRef,