compilation does something usefl
Following along with: http://blog.ulysse.io/2016/07/03/llvm-getting-started.html
This commit is contained in:
parent
d3f6bbabaa
commit
c18c1a639a
@ -2,13 +2,38 @@ extern crate llvm_sys;
|
|||||||
|
|
||||||
use self::llvm_sys::prelude::*;
|
use self::llvm_sys::prelude::*;
|
||||||
use self::llvm_sys::core;
|
use self::llvm_sys::core;
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
|
|
||||||
use parser::{ParseResult, AST, ASTNode, Prototype, Function, Expression};
|
use parser::{ParseResult, AST, ASTNode, Prototype, Function, Expression};
|
||||||
|
|
||||||
pub fn compile_ast(ast: AST) {
|
pub fn compile_ast(ast: AST) {
|
||||||
println!("Compiling!");
|
println!("Compiling!");
|
||||||
|
unsafe {
|
||||||
|
let context = core::LLVMContextCreate();
|
||||||
|
let module = core::LLVMModuleCreateWithName(b"schala\0".as_ptr() as *const _);
|
||||||
|
let builder = core::LLVMCreateBuilderInContext(context);
|
||||||
|
|
||||||
|
let void = core::LLVMVoidTypeInContext(context);
|
||||||
|
let function_type = core::LLVMFunctionType(void, ptr::null_mut(), 0, 0);
|
||||||
|
let function = core::LLVMAddFunction(module, b"nop\0".as_ptr() as *const _,
|
||||||
|
function_type);
|
||||||
|
|
||||||
|
let bb = core::LLVMAppendBasicBlockInContext(context, function,
|
||||||
|
b"entry\0".as_ptr() as *const _);
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait CodeGen {
|
trait CodeGen {
|
||||||
@ -30,7 +55,9 @@ impl CodeGen for Expression {
|
|||||||
fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef {
|
fn codegen(&self, context: LLVMContextRef) -> LLVMValueRef {
|
||||||
use self::Expression::*;
|
use self::Expression::*;
|
||||||
match self {
|
match self {
|
||||||
&Number(ref n) => unimplemented!(),
|
&Number(ref n) => {
|
||||||
|
unimplemented!()
|
||||||
|
},
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user