Move all LLVM functions into LLVMWrap
so I can kill the global unsafe
This commit is contained in:
parent
e0eef5e58f
commit
08798fb690
5
main.schala
Normal file
5
main.schala
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
|
||||
fn main()
|
||||
20
|
||||
end
|
@ -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 {
|
||||
|
||||
core::LLVMPositionBuilderAtEnd(builder, bb);
|
||||
LLVMWrap::PositionBuilderAtEnd(builder, bb);
|
||||
|
||||
// Emit a `ret void` into the function
|
||||
core::LLVMBuildRetVoid(builder);
|
||||
LLVMWrap::BuildRetVoid(builder);
|
||||
|
||||
// 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.
|
||||
core::LLVMDisposeBuilder(builder);
|
||||
core::LLVMDisposeModule(module);
|
||||
core::LLVMContextDispose(context);
|
||||
}
|
||||
LLVMWrap::DisposeBuilder(builder);
|
||||
LLVMWrap::DisposeModule(module);
|
||||
LLVMWrap::ContextDispose(context);
|
||||
}
|
||||
|
||||
trait CodeGen {
|
||||
|
@ -4,7 +4,7 @@ x + 20
|
||||
end
|
||||
|
||||
fn x(x)
|
||||
x + a(9000)
|
||||
x + a(9384)
|
||||
end
|
||||
|
||||
a(0)
|
||||
|
Loading…
Reference in New Issue
Block a user