From 3d406f1dd223aaa86273187026d7f69ca32c9b6c Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 26 Dec 2016 23:29:24 -0800 Subject: [PATCH] Move llmv wrapper into separate file --- src/compilation.rs | 142 +-------------------------------------------- src/llvm_wrap.rs | 140 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 3 files changed, 142 insertions(+), 141 deletions(-) create mode 100644 src/llvm_wrap.rs diff --git a/src/compilation.rs b/src/compilation.rs index 6346a53..cc112e9 100644 --- a/src/compilation.rs +++ b/src/compilation.rs @@ -7,147 +7,7 @@ use std::ffi::CString; use parser::{ParseResult, AST, ASTNode, Prototype, Function, Expression}; -mod LLVMWrap { - extern crate llvm_sys; - use self::llvm_sys::prelude::*; - use self::llvm_sys::core; - use std::ptr; - use std::ffi::CString; - - pub fn create_context() -> LLVMContextRef { - unsafe { - core::LLVMContextCreate() - } - } - pub fn module_create_with_name(name: &str) -> LLVMModuleRef { - unsafe { - let n = name.as_ptr() as *const _; - core::LLVMModuleCreateWithName(n) - } - } - pub fn CreateBuilderInContext(context: LLVMContextRef) -> LLVMBuilderRef { - unsafe { - core::LLVMCreateBuilderInContext(context) - } - } - - pub fn AppendBasicBlockInContext(context: LLVMContextRef, function: LLVMValueRef, name: &str) -> LLVMBasicBlockRef { - let c_name = CString::new(name).unwrap(); - unsafe { - core::LLVMAppendBasicBlockInContext(context, function, c_name.as_ptr()) - } - } - - pub fn AddFunction(module: LLVMModuleRef, name: &str, function_type: LLVMTypeRef) -> LLVMValueRef { - let c_name = CString::new(name).unwrap(); - unsafe { - core::LLVMAddFunction(module, c_name.as_ptr(), function_type) - } - } - - //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(), len as u32, if is_var_rag { 1 } else { 0 }) - } - } - - pub fn VoidTypeInContext(context: LLVMContextRef) -> LLVMTypeRef { - unsafe { - 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 BuildRet(builder: LLVMBuilderRef, val: LLVMValueRef) -> LLVMValueRef { - unsafe { - core::LLVMBuildRet(builder, val) - } - } - - pub fn BuildRetVoid(builder: LLVMBuilderRef) -> LLVMValueRef { - unsafe { - core::LLVMBuildRetVoid(builder) - } - } - - pub fn DumpModule(module: LLVMModuleRef) { - unsafe { - core::LLVMDumpModule(module) - } - } - - pub fn Int64TypeInContext(context: LLVMContextRef) -> LLVMTypeRef { - unsafe { - core::LLVMInt64TypeInContext(context) - } - } - - pub fn ConstInt(int_type: LLVMTypeRef, n: u64, sign_extend: bool) -> LLVMValueRef { - unsafe { - core::LLVMConstInt(int_type, n, if sign_extend { 1 } else { 0 }) - } - } - - pub fn BuildAdd(builder: LLVMBuilderRef, lhs: LLVMValueRef, rhs: LLVMValueRef, reg_name: &str) -> LLVMValueRef { - let name = CString::new(reg_name).unwrap(); - unsafe { - core::LLVMBuildAdd(builder, lhs, rhs, name.as_ptr()) - } - } - - pub fn BuildSub(builder: LLVMBuilderRef, lhs: LLVMValueRef, rhs: LLVMValueRef, reg_name: &str) -> LLVMValueRef { - let name = CString::new(reg_name).unwrap(); - unsafe { - core::LLVMBuildSub(builder, lhs, rhs, name.as_ptr()) - } - } - - pub fn BuildMul(builder: LLVMBuilderRef, lhs: LLVMValueRef, rhs: LLVMValueRef, reg_name: &str) -> LLVMValueRef { - let name = CString::new(reg_name).unwrap(); - unsafe { - core::LLVMBuildMul(builder, lhs, rhs, name.as_ptr()) - } - } - - pub fn BuildUDiv(builder: LLVMBuilderRef, lhs: LLVMValueRef, rhs: LLVMValueRef, reg_name: &str) -> LLVMValueRef { - let name = CString::new(reg_name).unwrap(); - unsafe { - core::LLVMBuildUDiv(builder, lhs, rhs, name.as_ptr()) - } - } - - pub fn BuildSRem(builder: LLVMBuilderRef, lhs: LLVMValueRef, rhs: LLVMValueRef, reg_name: &str) -> LLVMValueRef { - let name = CString::new(reg_name).unwrap(); - unsafe { - core::LLVMBuildSRem(builder, lhs, rhs, name.as_ptr()) - } - } -} +use llvm_wrap as LLVMWrap; pub fn compilation_sequence(ast: AST, sourcefile: &str) { use std::process::Command; diff --git a/src/llvm_wrap.rs b/src/llvm_wrap.rs new file mode 100644 index 0000000..96e67d0 --- /dev/null +++ b/src/llvm_wrap.rs @@ -0,0 +1,140 @@ +extern crate llvm_sys; + +use self::llvm_sys::prelude::*; +use self::llvm_sys::core; +use std::ptr; +use std::ffi::CString; + +pub fn create_context() -> LLVMContextRef { + unsafe { + core::LLVMContextCreate() + } +} +pub fn module_create_with_name(name: &str) -> LLVMModuleRef { + unsafe { + let n = name.as_ptr() as *const _; + core::LLVMModuleCreateWithName(n) + } +} +pub fn CreateBuilderInContext(context: LLVMContextRef) -> LLVMBuilderRef { + unsafe { + core::LLVMCreateBuilderInContext(context) + } +} + +pub fn AppendBasicBlockInContext(context: LLVMContextRef, function: LLVMValueRef, name: &str) -> LLVMBasicBlockRef { + let c_name = CString::new(name).unwrap(); + unsafe { + core::LLVMAppendBasicBlockInContext(context, function, c_name.as_ptr()) + } +} + +pub fn AddFunction(module: LLVMModuleRef, name: &str, function_type: LLVMTypeRef) -> LLVMValueRef { + let c_name = CString::new(name).unwrap(); + unsafe { + core::LLVMAddFunction(module, c_name.as_ptr(), function_type) + } +} + +//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(), len as u32, if is_var_rag { 1 } else { 0 }) + } +} + +pub fn VoidTypeInContext(context: LLVMContextRef) -> LLVMTypeRef { + unsafe { + 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 BuildRet(builder: LLVMBuilderRef, val: LLVMValueRef) -> LLVMValueRef { + unsafe { + core::LLVMBuildRet(builder, val) + } +} + +pub fn BuildRetVoid(builder: LLVMBuilderRef) -> LLVMValueRef { + unsafe { + core::LLVMBuildRetVoid(builder) + } +} + +pub fn DumpModule(module: LLVMModuleRef) { + unsafe { + core::LLVMDumpModule(module) + } +} + +pub fn Int64TypeInContext(context: LLVMContextRef) -> LLVMTypeRef { + unsafe { + core::LLVMInt64TypeInContext(context) + } +} + +pub fn ConstInt(int_type: LLVMTypeRef, n: u64, sign_extend: bool) -> LLVMValueRef { + unsafe { + core::LLVMConstInt(int_type, n, if sign_extend { 1 } else { 0 }) + } +} + +pub fn BuildAdd(builder: LLVMBuilderRef, lhs: LLVMValueRef, rhs: LLVMValueRef, reg_name: &str) -> LLVMValueRef { + let name = CString::new(reg_name).unwrap(); + unsafe { + core::LLVMBuildAdd(builder, lhs, rhs, name.as_ptr()) + } +} + +pub fn BuildSub(builder: LLVMBuilderRef, lhs: LLVMValueRef, rhs: LLVMValueRef, reg_name: &str) -> LLVMValueRef { + let name = CString::new(reg_name).unwrap(); + unsafe { + core::LLVMBuildSub(builder, lhs, rhs, name.as_ptr()) + } +} + +pub fn BuildMul(builder: LLVMBuilderRef, lhs: LLVMValueRef, rhs: LLVMValueRef, reg_name: &str) -> LLVMValueRef { + let name = CString::new(reg_name).unwrap(); + unsafe { + core::LLVMBuildMul(builder, lhs, rhs, name.as_ptr()) + } +} + +pub fn BuildUDiv(builder: LLVMBuilderRef, lhs: LLVMValueRef, rhs: LLVMValueRef, reg_name: &str) -> LLVMValueRef { + let name = CString::new(reg_name).unwrap(); + unsafe { + core::LLVMBuildUDiv(builder, lhs, rhs, name.as_ptr()) + } +} + +pub fn BuildSRem(builder: LLVMBuilderRef, lhs: LLVMValueRef, rhs: LLVMValueRef, reg_name: &str) -> LLVMValueRef { + let name = CString::new(reg_name).unwrap(); + unsafe { + core::LLVMBuildSRem(builder, lhs, rhs, name.as_ptr()) + } +} diff --git a/src/main.rs b/src/main.rs index 2dbdb50..e2dd182 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ mod eval; use compilation::{compilation_sequence}; mod compilation; +mod llvm_wrap; fn main() { let args: Vec = std::env::args().collect();