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::*;
|
2016-12-29 02:09:44 -08:00
|
|
|
use parser::{AST, ASTNode, Function, Expression};
|
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-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";
|
2016-12-29 02:01:48 -08:00
|
|
|
let q: Vec<&str> = sourcefile.split('.').collect();
|
2016-12-25 22:37:46 -08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 10:01:36 -08:00
|
|
|
type VariableMap = HashMap<String, LLVMValueRef>;
|
|
|
|
|
|
|
|
struct CompilationData {
|
|
|
|
context: LLVMContextRef,
|
|
|
|
module: LLVMModuleRef,
|
|
|
|
builder: LLVMBuilderRef,
|
|
|
|
variables: VariableMap,
|
|
|
|
}
|
|
|
|
|
2016-12-25 22:37:46 -08:00
|
|
|
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-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
|
|
|
|
2016-12-27 10:01:36 -08:00
|
|
|
let mut data = CompilationData {
|
|
|
|
context: context,
|
|
|
|
module: module,
|
|
|
|
builder: builder,
|
|
|
|
variables: names,
|
|
|
|
};
|
2016-12-25 12:05:24 -08:00
|
|
|
|
2016-12-27 10:01:36 -08:00
|
|
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
2016-12-25 19:24:39 -08:00
|
|
|
let function_type = LLVMWrap::FunctionType(int_type, &Vec::new(), false);
|
2016-12-27 10:01:36 -08:00
|
|
|
let function = LLVMWrap::AddFunction(data.module, "main", function_type);
|
2016-12-25 11:19:17 -08:00
|
|
|
|
2016-12-27 10:01:36 -08:00
|
|
|
let bb = LLVMWrap::AppendBasicBlockInContext(data.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-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
|
|
|
|
2016-12-27 01:14:52 -08:00
|
|
|
LLVMWrap::PrintModuleToFile(module, filename);
|
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-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 {
|
2016-12-25 19:33:47 -08:00
|
|
|
let first = self.get(0).unwrap();
|
2016-12-27 10:01:36 -08:00
|
|
|
first.codegen(data)
|
2016-12-25 19:33:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:42:37 -08:00
|
|
|
impl CodeGen for ASTNode {
|
2016-12-27 10:01:36 -08:00
|
|
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
2016-12-21 01:42:37 -08:00
|
|
|
use self::ASTNode::*;
|
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 {
|
2016-12-25 19:33:47 -08:00
|
|
|
let ref body = self.body;
|
2016-12-27 17:33:24 -08:00
|
|
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
|
|
|
let mut ret = LLVMWrap::ConstInt(int_type, 0, false);
|
|
|
|
for expr in body {
|
|
|
|
ret = expr.codegen(data);
|
|
|
|
}
|
|
|
|
ret
|
2016-12-25 19:33:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-12-30 18:23:28 -08:00
|
|
|
println!("Running codegen on: {:?}", self);
|
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);
|
2016-12-25 19:33:47 -08:00
|
|
|
|
2016-02-12 23:14:09 -08:00
|
|
|
match self {
|
2016-12-29 02:01:48 -08:00
|
|
|
&Variable(ref name) => *data.variables.get(name).unwrap(),
|
2016-12-27 17:33:24 -08:00
|
|
|
&BinExp(ref op, ref left, ref right) if op == "=" => {
|
|
|
|
if let Variable(ref name) = **left {
|
|
|
|
let new_value = right.codegen(data);
|
|
|
|
data.variables.insert(name.clone(), new_value);
|
|
|
|
new_value
|
|
|
|
} else {
|
|
|
|
panic!("Bad variable assignment")
|
|
|
|
}
|
2016-12-29 02:01:48 -08:00
|
|
|
}
|
2016-12-22 18:40:01 -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);
|
2016-12-26 12:36:28 -08:00
|
|
|
let generator = match op.as_ref() {
|
|
|
|
"+" => LLVMWrap::BuildAdd,
|
|
|
|
"-" => LLVMWrap::BuildSub,
|
|
|
|
"*" => LLVMWrap::BuildMul,
|
|
|
|
"/" => LLVMWrap::BuildUDiv,
|
|
|
|
"%" => LLVMWrap::BuildSRem,
|
|
|
|
_ => panic!("Bad operator {}", op),
|
|
|
|
};
|
|
|
|
|
2016-12-27 10:01:36 -08:00
|
|
|
generator(data.builder, lhs, rhs, "temp")
|
2016-12-29 02:01:48 -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-29 02:01:48 -08:00
|
|
|
}
|
2016-12-21 01:42:37 -08:00
|
|
|
_ => unimplemented!(),
|
2016-02-12 23:14:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|