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 //NOTE this is incomplete
pub fn FunctionType(return_type: LLVMTypeRef, param_types: &[LLVMTypeRef], is_var_rag: bool) -> LLVMTypeRef { pub fn FunctionType(return_type: LLVMTypeRef, param_types: &[LLVMTypeRef], is_var_rag: bool) -> LLVMTypeRef {
let len = param_types.len();
unsafe { 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) 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) { pub fn compile_ast(ast: AST) {
println!("Compiling!"); println!("Compiling!");
let name = "nop";
let context = LLVMWrap::create_context(); 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 builder = LLVMWrap::CreateBuilderInContext(context);
let void = LLVMWrap::VoidTypeInContext(context); let void = LLVMWrap::VoidTypeInContext(context);
let function_type = LLVMWrap::FunctionType(void, &Vec::new(), false); 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"); let bb = LLVMWrap::AppendBasicBlockInContext(context, function, "entry");
unsafe { LLVMWrap::PositionBuilderAtEnd(builder, bb);
core::LLVMPositionBuilderAtEnd(builder, bb);
// Emit a `ret void` into the function // Emit a `ret void` into the function
core::LLVMBuildRetVoid(builder); LLVMWrap::BuildRetVoid(builder);
// Dump the module as IR to stdout. // Dump the module as IR to stdout.
core::LLVMDumpModule(module); LLVMWrap::DumpModule(module);
// Clean up. Values created in the context mostly get cleaned up there. // Clean up. Values created in the context mostly get cleaned up there.
core::LLVMDisposeBuilder(builder); LLVMWrap::DisposeBuilder(builder);
core::LLVMDisposeModule(module); LLVMWrap::DisposeModule(module);
core::LLVMContextDispose(context); LLVMWrap::ContextDispose(context);
}
} }
trait CodeGen { trait CodeGen {

View File

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