Starting to add actual LLVM generating code
This commit is contained in:
parent
5d0648b9c0
commit
d3f6bbabaa
@ -8,5 +8,3 @@ authors = ["greg <greg.shuflin@protonmail.com>"]
|
|||||||
simplerepl = { path = "../simplerepl" }
|
simplerepl = { path = "../simplerepl" }
|
||||||
llvm-sys = "*"
|
llvm-sys = "*"
|
||||||
|
|
||||||
[dependencies.iron_llvm]
|
|
||||||
git = "https://github.com/jauhien/iron-llvm.git"
|
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
extern crate llvm_sys;
|
extern crate llvm_sys;
|
||||||
extern crate iron_llvm;
|
|
||||||
|
|
||||||
use self::llvm_sys::prelude::LLVMValueRef;
|
use self::llvm_sys::prelude::*;
|
||||||
|
use self::llvm_sys::core;
|
||||||
|
|
||||||
use self::iron_llvm::core;
|
|
||||||
use self::iron_llvm::core::types::{RealTypeCtor, RealTypeRef};
|
|
||||||
use self::iron_llvm::{LLVMRef, LLVMRefCtor};
|
|
||||||
|
|
||||||
use parser::{ParseResult, AST, ASTNode, Prototype, Function, Expression};
|
use parser::{ParseResult, AST, ASTNode, Prototype, Function, Expression};
|
||||||
|
|
||||||
@ -14,112 +11,27 @@ pub fn compile_ast(ast: AST) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
trait CodeGen {
|
||||||
pub struct Context {
|
fn codegen(&self, LLVMContextRef) -> LLVMValueRef;
|
||||||
context: core::Context,
|
|
||||||
builder: core::Builder,
|
|
||||||
named_values: HashMap<String, LLVMValueRef>,
|
|
||||||
ty: RealTypeRef,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
|
||||||
pub fn new() -> Context {
|
|
||||||
let context = core::Context::get_global();
|
|
||||||
let builder = core::Builder::new();
|
|
||||||
let named_values = HashMap::new();
|
|
||||||
let ty = RealTypeRef::get_double();
|
|
||||||
Context {
|
|
||||||
context: context,
|
|
||||||
builder: builder,
|
|
||||||
named_values: named_values,
|
|
||||||
ty: ty,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait ModuleProvider {
|
impl CodeGen for ASTNode {
|
||||||
fn dump(&self);
|
fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef {
|
||||||
fn get_module(&mut self) -> &mut core::Module;
|
use self::ASTNode::*;
|
||||||
fn get_function(&mut self, name: &str) -> Option<(FunctionRef, bool)>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SimpleModuleProvider {
|
|
||||||
module: core::Module,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SimpleModuleProvider {
|
|
||||||
pub fn new(name: &str) -> SimpleModuleProvider {
|
|
||||||
let module = core::Module::new(name);
|
|
||||||
SimpleModuleProvider {
|
|
||||||
module: module,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ModuleProvider for SimpleModuleProvider {
|
|
||||||
fn dump(&self) {
|
|
||||||
self.module.dump();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_module(&mut self) -> &mut core::Module {
|
|
||||||
&mut self.module
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_function(&mut self, name: &str) -> Option<(FunctionRef, bool)> {
|
|
||||||
match self.module.get_function_by_name(name) {
|
|
||||||
Some(f) => Some((f, f.count_basic_block() > 0)),
|
|
||||||
None => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type IRBuildingResult = Result<(LLVMValueRef, bool), String>;
|
|
||||||
|
|
||||||
fn error(msg: &str) -> IRBuildingResult {
|
|
||||||
Err(msg.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait IRBuilder {
|
|
||||||
fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IRBuilder for ParseResult<AST> {
|
|
||||||
fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult {
|
|
||||||
match self {
|
match self {
|
||||||
&Ok(ast) => ast.codegen(context, module_provider),
|
&ExprNode(ref expr) => expr.codegen(context),
|
||||||
&Err(err) => Err(err.msg.clone())
|
&FuncNode(ref func) => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IRBuilder for AST {
|
impl CodeGen for Expression {
|
||||||
fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult {
|
fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef {
|
||||||
let mut result = error("empty AST");
|
use self::Expression::*;
|
||||||
for node in self.iter() {
|
|
||||||
result = Ok(try!(node.codegen(context, module_provider)));
|
|
||||||
}
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IRBuilder for ASTNode {
|
|
||||||
fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult {
|
|
||||||
match self {
|
match self {
|
||||||
&ASTNode::ExprNode(ref expression) => expression.codegen(context, module_provider),
|
&Number(ref n) => unimplemented!(),
|
||||||
&ASTNode::FuncNode(ref function) => function.codegen(context, module_provider),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IRBuilder for Function {
|
|
||||||
fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IRBuilder for Expression {
|
|
||||||
fn codegen(&self, context: &mut Context, module_provider: &mut ModuleProvider) -> IRBuildingResult {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user