2016-02-09 23:52:57 -08:00
|
|
|
extern crate llvm_sys;
|
|
|
|
|
2016-12-27 10:01:36 -08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
use self::llvm_sys::prelude::*;
|
2017-01-14 01:23:14 -08:00
|
|
|
use self::llvm_sys::{LLVMIntPredicate, LLVMRealPredicate};
|
|
|
|
|
2017-01-23 03:03:12 -08:00
|
|
|
use schala_lang::parser::{AST, Statement, Function, Prototype, Expression, BinOp};
|
2017-01-23 19:11:50 -08:00
|
|
|
use language::LLVMCodeString;
|
2016-02-10 03:25:37 -08:00
|
|
|
|
2016-12-26 23:29:24 -08:00
|
|
|
use llvm_wrap as LLVMWrap;
|
2016-12-25 12:05:24 -08:00
|
|
|
|
2016-12-27 10:01:36 -08:00
|
|
|
type VariableMap = HashMap<String, LLVMValueRef>;
|
|
|
|
|
|
|
|
struct CompilationData {
|
|
|
|
context: LLVMContextRef,
|
|
|
|
module: LLVMModuleRef,
|
|
|
|
builder: LLVMBuilderRef,
|
|
|
|
variables: VariableMap,
|
2017-01-14 15:02:34 -08:00
|
|
|
main_function: LLVMValueRef,
|
2017-01-17 10:41:50 -08:00
|
|
|
current_function: Option<LLVMValueRef>,
|
2016-12-27 10:01:36 -08:00
|
|
|
}
|
|
|
|
|
2017-01-23 19:11:50 -08:00
|
|
|
pub fn compile_ast(ast: AST) -> LLVMCodeString {
|
2016-03-04 14:32:22 -08:00
|
|
|
println!("Compiling!");
|
2016-12-27 10:01:36 -08:00
|
|
|
let names: VariableMap = HashMap::new();
|
|
|
|
|
2016-12-22 18:40:01 -08:00
|
|
|
let context = LLVMWrap::create_context();
|
2016-12-25 19:24:39 -08:00
|
|
|
let module = LLVMWrap::module_create_with_name("example module");
|
2016-12-21 17:50:13 -08:00
|
|
|
let builder = LLVMWrap::CreateBuilderInContext(context);
|
2016-03-04 14:32:22 -08:00
|
|
|
|
2017-01-14 15:02:34 -08:00
|
|
|
let program_return_type = LLVMWrap::Int64TypeInContext(context);
|
2017-01-14 17:19:11 -08:00
|
|
|
let main_function_type = LLVMWrap::FunctionType(program_return_type, Vec::new(), false);
|
2017-01-14 15:02:34 -08:00
|
|
|
let main_function: LLVMValueRef = LLVMWrap::AddFunction(module, "main", main_function_type);
|
|
|
|
|
2016-12-27 10:01:36 -08:00
|
|
|
let mut data = CompilationData {
|
|
|
|
context: context,
|
|
|
|
builder: builder,
|
2017-01-13 22:29:17 -08:00
|
|
|
module: module,
|
2016-12-27 10:01:36 -08:00
|
|
|
variables: names,
|
2017-01-14 15:02:34 -08:00
|
|
|
main_function: main_function,
|
2017-01-17 10:41:50 -08:00
|
|
|
current_function: None,
|
2016-12-27 10:01:36 -08:00
|
|
|
};
|
2016-12-25 12:05:24 -08:00
|
|
|
|
2017-01-14 15:02:34 -08:00
|
|
|
let bb = LLVMWrap::AppendBasicBlockInContext(data.context, data.main_function, "entry");
|
2016-12-25 15:50:41 -08:00
|
|
|
LLVMWrap::PositionBuilderAtEnd(builder, bb);
|
2016-12-21 03:03:18 -08:00
|
|
|
|
2016-12-27 10:01:36 -08:00
|
|
|
let value = ast.codegen(&mut data);
|
2016-12-25 19:33:47 -08:00
|
|
|
|
|
|
|
LLVMWrap::BuildRet(builder, value);
|
2016-12-25 19:24:39 -08:00
|
|
|
|
2017-01-18 01:56:42 -08:00
|
|
|
let ret = LLVMWrap::PrintModuleToString(module);
|
2016-12-21 03:03:18 -08:00
|
|
|
|
2016-12-25 15:50:41 -08:00
|
|
|
// Clean up. Values created in the context mostly get cleaned up there.
|
|
|
|
LLVMWrap::DisposeBuilder(builder);
|
|
|
|
LLVMWrap::DisposeModule(module);
|
|
|
|
LLVMWrap::ContextDispose(context);
|
2017-01-23 19:11:50 -08:00
|
|
|
LLVMCodeString(ret)
|
2016-03-04 14:32:22 -08:00
|
|
|
}
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
trait CodeGen {
|
2016-12-29 02:01:48 -08:00
|
|
|
fn codegen(&self, &mut CompilationData) -> LLVMValueRef;
|
2016-02-10 03:25:37 -08:00
|
|
|
}
|
|
|
|
|
2016-12-25 19:33:47 -08:00
|
|
|
impl CodeGen for AST {
|
2016-12-27 10:01:36 -08:00
|
|
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
2017-01-10 01:34:08 -08:00
|
|
|
|
|
|
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
|
|
|
let mut ret = LLVMWrap::ConstInt(int_type, 0, false);
|
|
|
|
|
|
|
|
for statement in self {
|
|
|
|
ret = statement.codegen(data);
|
|
|
|
}
|
|
|
|
ret
|
2016-12-25 19:33:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 02:45:36 -08:00
|
|
|
impl CodeGen for Statement {
|
2016-12-27 10:01:36 -08:00
|
|
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
2017-01-03 02:45:36 -08:00
|
|
|
use self::Statement::*;
|
2016-02-11 10:49:45 -08:00
|
|
|
match self {
|
2016-12-27 10:01:36 -08:00
|
|
|
&ExprNode(ref expr) => expr.codegen(data),
|
2017-01-02 18:41:46 -08:00
|
|
|
&FuncDefNode(ref func) => func.codegen(data),
|
2016-02-11 10:49:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 19:33:47 -08:00
|
|
|
impl CodeGen for Function {
|
2016-12-27 10:01:36 -08:00
|
|
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
2017-01-14 17:19:11 -08:00
|
|
|
|
2017-01-15 07:23:53 -08:00
|
|
|
/* should have a check here for function already being defined */
|
2017-01-14 17:19:11 -08:00
|
|
|
let function = self.prototype.codegen(data);
|
2016-12-25 19:33:47 -08:00
|
|
|
let ref body = self.body;
|
2017-01-14 17:19:11 -08:00
|
|
|
|
2017-01-17 10:41:50 -08:00
|
|
|
data.current_function = Some(function);
|
|
|
|
|
2017-01-14 17:19:11 -08:00
|
|
|
let return_type = LLVMWrap::Int64TypeInContext(data.context);
|
|
|
|
let mut ret = LLVMWrap::ConstInt(return_type, 0, false);
|
|
|
|
|
2017-01-15 07:23:53 -08:00
|
|
|
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);
|
|
|
|
}
|
2017-01-14 17:19:11 -08:00
|
|
|
|
2016-12-27 17:33:24 -08:00
|
|
|
for expr in body {
|
|
|
|
ret = expr.codegen(data);
|
|
|
|
}
|
2017-01-15 23:43:24 -08:00
|
|
|
|
2017-01-16 01:06:59 -08:00
|
|
|
LLVMWrap::BuildRet(data.builder, ret);
|
|
|
|
|
2017-01-15 23:43:24 -08:00
|
|
|
// 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);
|
2017-01-17 10:41:50 -08:00
|
|
|
|
|
|
|
data.current_function = None;
|
|
|
|
|
2016-12-27 17:33:24 -08:00
|
|
|
ret
|
2016-12-25 19:33:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:19:11 -08:00
|
|
|
impl CodeGen for Prototype {
|
|
|
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
|
|
|
let num_args = self.parameters.len();
|
|
|
|
let return_type = LLVMWrap::Int64TypeInContext(data.context);
|
|
|
|
let mut arguments: Vec<LLVMTypeRef> = vec![];
|
|
|
|
|
|
|
|
for _ in 0..num_args {
|
|
|
|
arguments.push(LLVMWrap::Int64TypeInContext(data.context));
|
|
|
|
}
|
2017-01-17 10:41:50 -08:00
|
|
|
|
2017-01-14 17:19:11 -08:00
|
|
|
let function_type =
|
|
|
|
LLVMWrap::FunctionType(return_type,
|
|
|
|
arguments,
|
|
|
|
false);
|
|
|
|
|
|
|
|
let function = LLVMWrap::AddFunction(data.module,
|
|
|
|
&*self.name,
|
|
|
|
function_type);
|
|
|
|
|
2017-01-15 23:43:24 -08:00
|
|
|
let function_params = LLVMWrap::GetParams(function);
|
|
|
|
for (index, param) in function_params.iter().enumerate() {
|
2017-01-15 07:23:53 -08:00
|
|
|
let name = self.parameters.get(index).expect(&format!("Failed this check at index {}", index));
|
2017-01-15 23:43:24 -08:00
|
|
|
let new = *param;
|
|
|
|
|
|
|
|
LLVMWrap::SetValueName(new, name);
|
2017-01-15 07:23:53 -08:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:19:11 -08:00
|
|
|
function
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
impl CodeGen for Expression {
|
2016-12-27 10:01:36 -08:00
|
|
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
2017-01-13 20:05:17 -08:00
|
|
|
use self::BinOp::*;
|
2016-12-21 01:42:37 -08:00
|
|
|
use self::Expression::*;
|
2016-12-25 19:33:47 -08:00
|
|
|
|
2016-12-27 10:01:36 -08:00
|
|
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
2017-01-10 18:07:16 -08:00
|
|
|
let zero = LLVMWrap::ConstInt(int_type, 0, false);
|
2016-12-25 19:33:47 -08:00
|
|
|
|
2017-01-10 03:33:02 -08:00
|
|
|
match *self {
|
2017-01-15 07:23:53 -08:00
|
|
|
Variable(ref name) => *data.variables.get(&**name).expect(&format!("Can't find variable {}", name)),
|
2017-01-13 20:05:17 -08:00
|
|
|
BinExp(Assign, ref left, ref right) => {
|
2016-12-27 17:33:24 -08:00
|
|
|
if let Variable(ref name) = **left {
|
|
|
|
let new_value = right.codegen(data);
|
2017-01-04 04:18:55 -08:00
|
|
|
data.variables.insert((**name).clone(), new_value);
|
2016-12-27 17:33:24 -08:00
|
|
|
new_value
|
|
|
|
} else {
|
|
|
|
panic!("Bad variable assignment")
|
|
|
|
}
|
2016-12-29 02:01:48 -08:00
|
|
|
}
|
2017-01-10 03:33:02 -08:00
|
|
|
BinExp(ref op, ref left, ref right) => {
|
2016-12-27 10:01:36 -08:00
|
|
|
let lhs = left.codegen(data);
|
|
|
|
let rhs = right.codegen(data);
|
2017-01-14 15:13:03 -08:00
|
|
|
op.codegen_with_ops(data, lhs, rhs)
|
2016-12-29 02:01:48 -08:00
|
|
|
}
|
2017-01-10 03:33:02 -08:00
|
|
|
Number(ref n) => {
|
2016-12-25 19:33:47 -08:00
|
|
|
let native_val = *n as u64;
|
|
|
|
let int_value: LLVMValueRef = LLVMWrap::ConstInt(int_type, native_val, false);
|
|
|
|
int_value
|
2016-12-29 02:01:48 -08:00
|
|
|
}
|
2017-01-10 10:30:08 -08:00
|
|
|
Conditional(ref test, ref then_expr, ref else_expr) => {
|
2017-01-10 03:33:02 -08:00
|
|
|
let condition_value = test.codegen(data);
|
2017-01-10 10:30:08 -08:00
|
|
|
let is_nonzero =
|
|
|
|
LLVMWrap::BuildICmp(data.builder,
|
2017-01-14 01:23:14 -08:00
|
|
|
LLVMIntPredicate::LLVMIntNE,
|
2017-01-10 10:30:08 -08:00
|
|
|
condition_value,
|
|
|
|
zero,
|
2017-01-17 10:41:50 -08:00
|
|
|
"ifcond");
|
|
|
|
|
|
|
|
let func = LLVMWrap::GetBasicBlockParent(LLVMWrap::GetInsertBlock(data.builder));
|
2017-01-10 03:33:02 -08:00
|
|
|
|
2017-01-17 10:41:50 -08:00
|
|
|
let mut then_block =
|
|
|
|
LLVMWrap::AppendBasicBlockInContext(data.context, func, "then_block");
|
|
|
|
let mut else_block =
|
|
|
|
LLVMWrap::AppendBasicBlockInContext(data.context, func, "else_block");
|
2017-01-10 10:30:08 -08:00
|
|
|
let merge_block =
|
2017-01-17 10:41:50 -08:00
|
|
|
LLVMWrap::AppendBasicBlockInContext(data.context, func, "ifcont");
|
2017-01-11 00:36:16 -08:00
|
|
|
|
2017-01-17 10:41:50 -08:00
|
|
|
// add conditional branch to ifcond block
|
2017-01-10 10:30:08 -08:00
|
|
|
LLVMWrap::BuildCondBr(data.builder, is_nonzero, then_block, else_block);
|
2017-01-17 10:41:50 -08:00
|
|
|
|
|
|
|
// start inserting into then block
|
2017-01-10 10:30:08 -08:00
|
|
|
LLVMWrap::PositionBuilderAtEnd(data.builder, then_block);
|
2017-01-11 00:36:16 -08:00
|
|
|
|
2017-01-17 10:41:50 -08:00
|
|
|
// then-block codegen
|
2017-01-10 10:30:08 -08:00
|
|
|
let then_return = then_expr.codegen(data);
|
|
|
|
LLVMWrap::BuildBr(data.builder, merge_block);
|
2017-01-11 00:36:16 -08:00
|
|
|
|
2017-01-17 10:41:50 -08:00
|
|
|
// update then block b/c recursive codegen() call may have changed the notion of
|
|
|
|
// the current block
|
|
|
|
then_block = LLVMWrap::GetInsertBlock(data.builder);
|
|
|
|
|
|
|
|
// then do the same stuff again for the else branch
|
|
|
|
//
|
2017-01-11 00:36:16 -08:00
|
|
|
LLVMWrap::PositionBuilderAtEnd(data.builder, else_block);
|
2017-01-10 18:07:16 -08:00
|
|
|
let else_return = match *else_expr {
|
|
|
|
Some(ref e) => e.codegen(data),
|
|
|
|
None => zero,
|
2017-01-10 10:32:56 -08:00
|
|
|
};
|
2017-01-10 10:30:08 -08:00
|
|
|
LLVMWrap::BuildBr(data.builder, merge_block);
|
2017-01-17 10:41:50 -08:00
|
|
|
else_block = LLVMWrap::GetInsertBlock(data.builder);
|
|
|
|
|
2017-01-11 00:36:16 -08:00
|
|
|
LLVMWrap::PositionBuilderAtEnd(data.builder, merge_block);
|
2017-01-17 10:41:50 -08:00
|
|
|
|
2017-01-17 19:48:25 -08:00
|
|
|
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
|
2017-01-10 03:33:02 -08:00
|
|
|
}
|
2017-01-10 18:07:16 -08:00
|
|
|
Block(ref exprs) => {
|
|
|
|
let mut ret = zero;
|
|
|
|
for e in exprs.iter() {
|
|
|
|
ret = e.codegen(data);
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
ref e => {
|
|
|
|
println!("Unimplemented {:?}", e);
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2016-02-12 23:14:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 15:13:03 -08:00
|
|
|
|
|
|
|
impl BinOp {
|
|
|
|
fn codegen_with_ops(&self, data: &CompilationData, lhs: LLVMValueRef, rhs: LLVMValueRef) -> LLVMValueRef {
|
|
|
|
use self::BinOp::*;
|
|
|
|
macro_rules! simple_binop {
|
|
|
|
($fnname: expr, $name: expr) => {
|
|
|
|
$fnname(data.builder, lhs, rhs, $name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
|
|
|
match *self {
|
|
|
|
Add => simple_binop!(LLVMWrap::BuildAdd, "addtemp"),
|
|
|
|
Sub => simple_binop!(LLVMWrap::BuildSub, "subtemp"),
|
|
|
|
Mul => simple_binop!(LLVMWrap::BuildMul, "multemp"),
|
|
|
|
Div => simple_binop!(LLVMWrap::BuildUDiv, "divtemp"),
|
|
|
|
Mod => simple_binop!(LLVMWrap::BuildSRem, "remtemp"),
|
|
|
|
Less => {
|
|
|
|
let pred: LLVMValueRef =
|
|
|
|
LLVMWrap::BuildICmp(data.builder, LLVMIntPredicate::LLVMIntULT, lhs, rhs, "tmp");
|
|
|
|
LLVMWrap::BuildZExt(data.builder, pred, int_type, "temp")
|
|
|
|
}
|
|
|
|
Greater => {
|
|
|
|
let pred: LLVMValueRef =
|
|
|
|
LLVMWrap::BuildICmp(data.builder, LLVMIntPredicate::LLVMIntUGT, lhs, rhs, "tmp");
|
|
|
|
LLVMWrap::BuildZExt(data.builder, pred, int_type, "temp")
|
|
|
|
}
|
|
|
|
ref unknown => panic!("Bad operator {:?}", unknown),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|