diff options
Diffstat (limited to 'src/llvm/mod.rs')
-rw-r--r-- | src/llvm/mod.rs | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/llvm/mod.rs b/src/llvm/mod.rs index 01ed3f2..16e6bfd 100644 --- a/src/llvm/mod.rs +++ b/src/llvm/mod.rs @@ -8,17 +8,28 @@ //! For the scope of this tutorial we mainly use assertions to validate the results from the LLVM //! API calls. -use llvm_sys::{core::LLVMShutdown, prelude::LLVMBasicBlockRef}; +use llvm_sys::{ + core::LLVMShutdown, + error::{LLVMDisposeErrorMessage, LLVMErrorRef, LLVMGetErrorMessage}, + prelude::LLVMBasicBlockRef, + target::{ + LLVM_InitializeNativeAsmParser, LLVM_InitializeNativeAsmPrinter, + LLVM_InitializeNativeTarget, + }, +}; +use std::ffi::CStr; use std::marker::PhantomData; mod builder; +mod lljit; mod module; mod pass_manager; mod type_; mod value; pub use builder::IRBuilder; +pub use lljit::{LLJit, ResourceTracker}; pub use module::Module; pub use pass_manager::FunctionPassManager; pub use type_::Type; @@ -28,6 +39,37 @@ pub use value::{FnValue, Value}; #[derive(Copy, Clone)] pub struct BasicBlock<'llvm>(LLVMBasicBlockRef, PhantomData<&'llvm ()>); +struct Error<'llvm>(&'llvm mut libc::c_char); + +impl<'llvm> Error<'llvm> { + fn from(err: LLVMErrorRef) -> Option<Error<'llvm>> { + (!err.is_null()).then(|| Error(unsafe { &mut *LLVMGetErrorMessage(err) })) + } + + fn as_str(&self) -> &str { + unsafe { CStr::from_ptr(self.0) } + .to_str() + .expect("Expected valid UTF8 string from LLVM API") + } +} + +impl Drop for Error<'_> { + fn drop(&mut self) { + unsafe { + LLVMDisposeErrorMessage(self.0 as *mut libc::c_char); + } + } +} + +/// Initialize native target for corresponding to host (useful for jitting). +pub fn initialize_native_taget() { + unsafe { + assert_eq!(LLVM_InitializeNativeTarget(), 0); + assert_eq!(LLVM_InitializeNativeAsmParser(), 0); + assert_eq!(LLVM_InitializeNativeAsmPrinter(), 0); + } +} + /// Deallocate and destroy all "ManagedStatic" variables. pub fn shutdown() { unsafe { |