2016-02-09 23:52:57 -08:00
|
|
|
extern crate llvm_sys;
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
use self::llvm_sys::prelude::*;
|
|
|
|
use self::llvm_sys::core;
|
2016-12-21 03:03:18 -08:00
|
|
|
use std::ptr;
|
2016-12-25 19:24:39 -08:00
|
|
|
use std::ffi::CString;
|
2016-02-10 03:25:37 -08:00
|
|
|
|
2016-02-15 23:41:00 -08:00
|
|
|
use parser::{ParseResult, AST, ASTNode, Prototype, Function, Expression};
|
2016-02-10 03:25:37 -08:00
|
|
|
|
2016-12-21 03:18:30 -08:00
|
|
|
mod LLVMWrap {
|
|
|
|
extern crate llvm_sys;
|
|
|
|
use self::llvm_sys::prelude::*;
|
|
|
|
use self::llvm_sys::core;
|
|
|
|
use std::ptr;
|
2016-12-25 11:19:17 -08:00
|
|
|
use std::ffi::CString;
|
|
|
|
|
2016-12-22 18:40:01 -08:00
|
|
|
pub fn create_context() -> LLVMContextRef {
|
2016-12-21 03:18:30 -08:00
|
|
|
unsafe {
|
|
|
|
core::LLVMContextCreate()
|
|
|
|
}
|
|
|
|
}
|
2016-12-22 18:40:01 -08:00
|
|
|
pub fn module_create_with_name(name: &str) -> LLVMModuleRef {
|
2016-12-21 03:18:30 -08:00
|
|
|
unsafe {
|
|
|
|
let n = name.as_ptr() as *const _;
|
|
|
|
core::LLVMModuleCreateWithName(n)
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 17:50:13 -08:00
|
|
|
pub fn CreateBuilderInContext(context: LLVMContextRef) -> LLVMBuilderRef {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMCreateBuilderInContext(context)
|
|
|
|
}
|
|
|
|
}
|
2016-12-25 11:19:17 -08:00
|
|
|
|
|
|
|
pub fn AppendBasicBlockInContext(context: LLVMContextRef, function: LLVMValueRef, name: &str) -> LLVMBasicBlockRef {
|
|
|
|
let c_name = CString::new(name).unwrap();
|
|
|
|
unsafe {
|
|
|
|
core::LLVMAppendBasicBlockInContext(context, function, c_name.as_ptr())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn AddFunction(module: LLVMModuleRef, name: &str, function_type: LLVMTypeRef) -> LLVMValueRef {
|
|
|
|
let c_name = CString::new(name).unwrap();
|
|
|
|
unsafe {
|
|
|
|
core::LLVMAddFunction(module, c_name.as_ptr(), function_type)
|
|
|
|
}
|
|
|
|
}
|
2016-12-25 12:05:24 -08:00
|
|
|
|
|
|
|
//NOTE this is incomplete
|
|
|
|
pub fn FunctionType(return_type: LLVMTypeRef, param_types: &[LLVMTypeRef], is_var_rag: bool) -> LLVMTypeRef {
|
2016-12-25 15:50:41 -08:00
|
|
|
let len = param_types.len();
|
2016-12-25 12:05:24 -08:00
|
|
|
unsafe {
|
2016-12-25 15:50:41 -08:00
|
|
|
core::LLVMFunctionType(return_type, ptr::null_mut(), len as u32, if is_var_rag { 1 } else { 0 })
|
2016-12-25 12:05:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn VoidTypeInContext(context: LLVMContextRef) -> LLVMTypeRef {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMVoidTypeInContext(context)
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 03:18:30 -08:00
|
|
|
|
2016-12-25 15:50:41 -08:00
|
|
|
pub fn DisposeBuilder(builder: LLVMBuilderRef) {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMDisposeBuilder(builder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn DisposeModule(module: LLVMModuleRef) {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMDisposeModule(module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ContextDispose(context: LLVMContextRef) {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMContextDispose(context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn PositionBuilderAtEnd(builder: LLVMBuilderRef, basic_block: LLVMBasicBlockRef) {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMPositionBuilderAtEnd(builder, basic_block)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 19:24:39 -08:00
|
|
|
pub fn BuildRet(builder: LLVMBuilderRef, val: LLVMValueRef) -> LLVMValueRef {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMBuildRet(builder, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 15:50:41 -08:00
|
|
|
pub fn BuildRetVoid(builder: LLVMBuilderRef) -> LLVMValueRef {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMBuildRetVoid(builder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn DumpModule(module: LLVMModuleRef) {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMDumpModule(module)
|
|
|
|
}
|
|
|
|
}
|
2016-12-25 19:24:39 -08:00
|
|
|
|
|
|
|
pub fn Int64TypeInContext(context: LLVMContextRef) -> LLVMTypeRef {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMInt64TypeInContext(context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ConstInt(int_type: LLVMTypeRef, n: u64, sign_extend: bool) -> LLVMValueRef {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMConstInt(int_type, n, if sign_extend { 1 } else { 0 })
|
|
|
|
}
|
|
|
|
}
|
2016-12-25 15:50:41 -08:00
|
|
|
}
|
2016-12-25 12:05:24 -08:00
|
|
|
|
2016-12-25 22:37:46 -08:00
|
|
|
pub fn compilation_sequence(ast: AST, sourcefile: &str) {
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
let ll_filename = "out.ll";
|
|
|
|
let obj_filename = "out.o";
|
|
|
|
let q: Vec<&str> = sourcefile.split('.').collect();
|
|
|
|
let bin_filename = match &q[..] {
|
|
|
|
&[name, "schala"] => name,
|
|
|
|
_ => panic!("Bad filename {}", sourcefile),
|
|
|
|
};
|
|
|
|
|
|
|
|
compile_ast(ast, ll_filename);
|
|
|
|
Command::new("llc")
|
|
|
|
.arg("-filetype=obj")
|
|
|
|
.arg(ll_filename)
|
|
|
|
.output()
|
|
|
|
.expect("Failed to run llc");
|
|
|
|
|
|
|
|
Command::new("gcc")
|
|
|
|
.arg(format!("-o{}", bin_filename))
|
|
|
|
.arg(obj_filename)
|
|
|
|
.output()
|
|
|
|
.expect("failed to run gcc");
|
|
|
|
|
2016-12-26 12:05:29 -08:00
|
|
|
for filename in [obj_filename].iter() {
|
2016-12-25 22:37:46 -08:00
|
|
|
Command::new("rm")
|
2016-12-26 12:05:29 -08:00
|
|
|
.arg(filename)
|
2016-12-25 22:37:46 -08:00
|
|
|
.output()
|
|
|
|
.expect(&format!("failed to run rm {}", filename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile_ast(ast: AST, filename: &str) {
|
2016-03-04 14:32:22 -08:00
|
|
|
println!("Compiling!");
|
2016-12-25 19:33:47 -08:00
|
|
|
println!("AST is {:?}", ast);
|
2016-12-25 15:50:41 -08:00
|
|
|
|
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
|
|
|
|
2016-12-25 19:24:39 -08:00
|
|
|
//let void = LLVMWrap::VoidTypeInContext(context);
|
2016-12-25 12:05:24 -08:00
|
|
|
|
2016-12-25 19:24:39 -08:00
|
|
|
let int_type = LLVMWrap::Int64TypeInContext(context);
|
|
|
|
let function_type = LLVMWrap::FunctionType(int_type, &Vec::new(), false);
|
|
|
|
let function = LLVMWrap::AddFunction(module, "main", function_type);
|
2016-12-25 11:19:17 -08:00
|
|
|
|
2016-12-25 19:24:39 -08:00
|
|
|
let bb = LLVMWrap::AppendBasicBlockInContext(context, function, "entry");
|
2016-12-25 15:50:41 -08:00
|
|
|
LLVMWrap::PositionBuilderAtEnd(builder, bb);
|
2016-12-21 03:03:18 -08:00
|
|
|
|
2016-12-25 19:33:47 -08:00
|
|
|
/*
|
2016-12-25 19:24:39 -08:00
|
|
|
let int_value: u64 = 84;
|
|
|
|
let int_value = LLVMWrap::ConstInt(int_type, int_value, false);
|
2016-12-25 19:33:47 -08:00
|
|
|
*/
|
2016-12-21 03:03:18 -08:00
|
|
|
|
2016-12-25 22:37:46 -08:00
|
|
|
let value = ast.codegen(context, builder);
|
2016-12-25 19:33:47 -08:00
|
|
|
|
|
|
|
LLVMWrap::BuildRet(builder, value);
|
2016-12-25 19:24:39 -08:00
|
|
|
|
|
|
|
unsafe {
|
2016-12-25 22:37:46 -08:00
|
|
|
let out_file = CString::new(filename).unwrap();
|
2016-12-25 19:24:39 -08:00
|
|
|
core::LLVMPrintModuleToFile(module, out_file.as_ptr(), ptr::null_mut());
|
|
|
|
}
|
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);
|
2016-03-04 14:32:22 -08:00
|
|
|
}
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
trait CodeGen {
|
2016-12-25 22:37:46 -08:00
|
|
|
fn codegen(&self, LLVMContextRef, LLVMBuilderRef) -> LLVMValueRef;
|
2016-02-10 03:25:37 -08:00
|
|
|
}
|
|
|
|
|
2016-12-25 19:33:47 -08:00
|
|
|
impl CodeGen for AST {
|
2016-12-25 22:37:46 -08:00
|
|
|
fn codegen(&self, context: LLVMContextRef, builder: LLVMBuilderRef) -> LLVMValueRef {
|
2016-12-25 19:33:47 -08:00
|
|
|
let first = self.get(0).unwrap();
|
2016-12-25 22:37:46 -08:00
|
|
|
first.codegen(context, builder)
|
2016-12-25 19:33:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
impl CodeGen for ASTNode {
|
2016-12-25 22:37:46 -08:00
|
|
|
fn codegen(&self, context: LLVMContextRef, builder: LLVMBuilderRef) -> LLVMValueRef {
|
2016-12-21 01:42:37 -08:00
|
|
|
use self::ASTNode::*;
|
2016-02-11 10:49:45 -08:00
|
|
|
match self {
|
2016-12-25 22:37:46 -08:00
|
|
|
&ExprNode(ref expr) => expr.codegen(context, builder),
|
|
|
|
&FuncNode(ref func) => func.codegen(context, builder),
|
2016-02-11 10:49:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 19:33:47 -08:00
|
|
|
impl CodeGen for Function {
|
2016-12-25 22:37:46 -08:00
|
|
|
fn codegen(&self, context: LLVMContextRef, builder: LLVMBuilderRef) -> LLVMValueRef {
|
2016-12-25 19:33:47 -08:00
|
|
|
let ref body = self.body;
|
|
|
|
let first = body.get(0).unwrap();
|
2016-12-25 22:37:46 -08:00
|
|
|
first.codegen(context, builder)
|
2016-12-25 19:33:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
impl CodeGen for Expression {
|
2016-12-25 22:37:46 -08:00
|
|
|
fn codegen(&self, context: LLVMContextRef, builder: LLVMBuilderRef) -> LLVMValueRef {
|
2016-12-21 01:42:37 -08:00
|
|
|
use self::Expression::*;
|
2016-12-25 19:33:47 -08:00
|
|
|
|
|
|
|
let int_type = LLVMWrap::Int64TypeInContext(context);
|
|
|
|
|
2016-02-12 23:14:09 -08:00
|
|
|
match self {
|
2016-12-22 18:40:01 -08:00
|
|
|
&BinExp(ref op, ref left, ref right) => {
|
2016-12-26 00:48:57 -08:00
|
|
|
let lhs = left.codegen(context, builder);
|
|
|
|
let rhs = right.codegen(context, builder);
|
|
|
|
unsafe {
|
|
|
|
let generator = match op.as_ref() {
|
|
|
|
"+" => core::LLVMBuildAdd,
|
|
|
|
"-" => core::LLVMBuildSub,
|
|
|
|
"*" => core::LLVMBuildMul,
|
|
|
|
"/" => core::LLVMBuildUDiv,
|
|
|
|
"%" => core::LLVMBuildSRem,
|
|
|
|
_ => panic!("Bad operator {}", op),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let reg_name = CString::new("temporary").unwrap();
|
|
|
|
generator(builder, lhs, rhs, reg_name.as_ptr())
|
|
|
|
}
|
2016-12-22 18:40:01 -08:00
|
|
|
},
|
2016-12-21 03:03:18 -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-21 03:03:18 -08:00
|
|
|
},
|
2016-12-21 01:42:37 -08:00
|
|
|
_ => unimplemented!(),
|
2016-02-12 23:14:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|