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-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 {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMFunctionType(return_type, ptr::null_mut(), 0, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn VoidTypeInContext(context: LLVMContextRef) -> LLVMTypeRef {
|
|
|
|
unsafe {
|
|
|
|
core::LLVMVoidTypeInContext(context)
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 03:18:30 -08:00
|
|
|
}
|
|
|
|
|
2016-12-25 12:05:24 -08:00
|
|
|
|
2016-03-04 14:32:22 -08:00
|
|
|
pub fn compile_ast(ast: AST) {
|
|
|
|
println!("Compiling!");
|
2016-12-22 18:40:01 -08:00
|
|
|
let context = LLVMWrap::create_context();
|
|
|
|
let module = LLVMWrap::module_create_with_name("schala");
|
2016-12-21 17:50:13 -08:00
|
|
|
let builder = LLVMWrap::CreateBuilderInContext(context);
|
2016-03-04 14:32:22 -08:00
|
|
|
|
2016-12-25 12:05:24 -08:00
|
|
|
let void = LLVMWrap::VoidTypeInContext(context);
|
|
|
|
let function_type = LLVMWrap::FunctionType(void, &Vec::new(), false);
|
|
|
|
let function = LLVMWrap::AddFunction(module, "nop", function_type);
|
|
|
|
|
|
|
|
let bb = LLVMWrap::AppendBasicBlockInContext(context, function, "entry");
|
2016-12-25 11:19:17 -08:00
|
|
|
|
2016-12-25 12:05:24 -08:00
|
|
|
unsafe {
|
2016-12-21 03:03:18 -08:00
|
|
|
|
|
|
|
core::LLVMPositionBuilderAtEnd(builder, bb);
|
|
|
|
|
|
|
|
// Emit a `ret void` into the function
|
|
|
|
core::LLVMBuildRetVoid(builder);
|
|
|
|
|
|
|
|
// Dump the module as IR to stdout.
|
|
|
|
core::LLVMDumpModule(module);
|
|
|
|
|
|
|
|
// Clean up. Values created in the context mostly get cleaned up there.
|
|
|
|
core::LLVMDisposeBuilder(builder);
|
|
|
|
core::LLVMDisposeModule(module);
|
|
|
|
core::LLVMContextDispose(context);
|
|
|
|
}
|
2016-03-04 14:32:22 -08:00
|
|
|
}
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
trait CodeGen {
|
|
|
|
fn codegen(&self, LLVMContextRef) -> LLVMValueRef;
|
2016-02-10 03:25:37 -08:00
|
|
|
}
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
impl CodeGen for ASTNode {
|
|
|
|
fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef {
|
|
|
|
use self::ASTNode::*;
|
2016-02-11 10:49:45 -08:00
|
|
|
match self {
|
2016-12-21 01:42:37 -08:00
|
|
|
&ExprNode(ref expr) => expr.codegen(context),
|
|
|
|
&FuncNode(ref func) => unimplemented!(),
|
2016-02-11 10:49:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
impl CodeGen for Expression {
|
|
|
|
fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef {
|
|
|
|
use self::Expression::*;
|
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) => {
|
|
|
|
unimplemented!()
|
|
|
|
},
|
2016-12-21 03:03:18 -08:00
|
|
|
&Number(ref n) => {
|
|
|
|
unimplemented!()
|
|
|
|
},
|
2016-12-21 01:42:37 -08:00
|
|
|
_ => unimplemented!(),
|
2016-02-12 23:14:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|