Move all LLVM functions into LLVMWrap

so I can kill the global unsafe
This commit is contained in:
greg 2016-12-25 15:50:41 -08:00
parent e0eef5e58f
commit 08798fb690
3 changed files with 57 additions and 17 deletions

5
main.schala Normal file
View File

@ -0,0 +1,5 @@
fn main()
20
end

View File

@ -46,8 +46,9 @@ mod LLVMWrap {
//NOTE this is incomplete
pub fn FunctionType(return_type: LLVMTypeRef, param_types: &[LLVMTypeRef], is_var_rag: bool) -> LLVMTypeRef {
let len = param_types.len();
unsafe {
core::LLVMFunctionType(return_type, ptr::null_mut(), 0, 0)
core::LLVMFunctionType(return_type, ptr::null_mut(), len as u32, if is_var_rag { 1 } else { 0 })
}
}
@ -56,36 +57,70 @@ mod LLVMWrap {
core::LLVMVoidTypeInContext(context)
}
}
}
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)
}
}
pub fn BuildRetVoid(builder: LLVMBuilderRef) -> LLVMValueRef {
unsafe {
core::LLVMBuildRetVoid(builder)
}
}
pub fn DumpModule(module: LLVMModuleRef) {
unsafe {
core::LLVMDumpModule(module)
}
}
}
pub fn compile_ast(ast: AST) {
println!("Compiling!");
let name = "nop";
let context = LLVMWrap::create_context();
let module = LLVMWrap::module_create_with_name("schala");
let module = LLVMWrap::module_create_with_name(name);
let builder = LLVMWrap::CreateBuilderInContext(context);
let void = LLVMWrap::VoidTypeInContext(context);
let function_type = LLVMWrap::FunctionType(void, &Vec::new(), false);
let function = LLVMWrap::AddFunction(module, "nop", function_type);
let function = LLVMWrap::AddFunction(module, name, function_type);
let bb = LLVMWrap::AppendBasicBlockInContext(context, function, "entry");
unsafe {
LLVMWrap::PositionBuilderAtEnd(builder, bb);
core::LLVMPositionBuilderAtEnd(builder, bb);
// Emit a `ret void` into the function
LLVMWrap::BuildRetVoid(builder);
// Emit a `ret void` into the function
core::LLVMBuildRetVoid(builder);
// Dump the module as IR to stdout.
LLVMWrap::DumpModule(module);
// 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);
}
// Clean up. Values created in the context mostly get cleaned up there.
LLVMWrap::DisposeBuilder(builder);
LLVMWrap::DisposeModule(module);
LLVMWrap::ContextDispose(context);
}
trait CodeGen {

View File

@ -4,7 +4,7 @@ x + 20
end
fn x(x)
x + a(9000)
x + a(9384)
end
a(0)