Added print to file and set inline asm module methods
use llvm_sys::analysis::{LLVMVerifyModule, LLVMVerifierFailureAction};
use llvm_sys::bit_writer::{LLVMWriteBitcodeToFile, LLVMWriteBitcodeToMemoryBuffer};
-use llvm_sys::core::{LLVMAddFunction, LLVMAddGlobal, LLVMDisposeMessage, LLVMDumpModule, LLVMGetNamedFunction, LLVMGetTypeByName, LLVMSetDataLayout, LLVMSetInitializer, LLVMSetTarget, LLVMCloneModule, LLVMDisposeModule, LLVMGetTarget, LLVMGetDataLayout, LLVMModuleCreateWithName, LLVMGetModuleContext, LLVMGetFirstFunction, LLVMGetLastFunction, LLVMSetLinkage, LLVMAddGlobalInAddressSpace, LLVMPrintModuleToString, LLVMGetNamedMetadataNumOperands, LLVMAddNamedMetadataOperand, LLVMGetNamedMetadataOperands, LLVMGetFirstGlobal, LLVMGetLastGlobal, LLVMGetNamedGlobal};
+use llvm_sys::core::{LLVMAddFunction, LLVMAddGlobal, LLVMDisposeMessage, LLVMDumpModule, LLVMGetNamedFunction, LLVMGetTypeByName, LLVMSetDataLayout, LLVMSetInitializer, LLVMSetTarget, LLVMCloneModule, LLVMDisposeModule, LLVMGetTarget, LLVMGetDataLayout, LLVMModuleCreateWithName, LLVMGetModuleContext, LLVMGetFirstFunction, LLVMGetLastFunction, LLVMSetLinkage, LLVMAddGlobalInAddressSpace, LLVMPrintModuleToString, LLVMGetNamedMetadataNumOperands, LLVMAddNamedMetadataOperand, LLVMGetNamedMetadataOperands, LLVMGetFirstGlobal, LLVMGetLastGlobal, LLVMGetNamedGlobal, LLVMPrintModuleToFile, LLVMSetModuleInlineAsm};
use llvm_sys::execution_engine::{LLVMCreateJITCompilerForModule, LLVMCreateMCJITCompilerForModule};
use llvm_sys::prelude::{LLVMValueRef, LLVMModuleRef};
use llvm_sys::LLVMLinkage;
@@ -432,6 +432,38 @@ impl Module {
}
}
+ /// Prints the content of the `Module` to a file.
+ pub fn print_to_file(&self, path: &Path) -> Result<(), String> {
+ let path = path.to_str().expect("Did not find a valid Unicode path string");
+ let mut err_str = unsafe { zeroed() };
+ let return_code = unsafe {
+ LLVMPrintModuleToFile(self.module, path.as_ptr() as *const i8, &mut err_str)
+ };
+
+ // TODO: Verify 1 is error code (LLVM can be inconsistent)
+ if return_code == 1 {
+ let rust_str = unsafe {
+ let rust_str = CStr::from_ptr(err_str).to_string_lossy().into_owned();
+
+ LLVMDisposeMessage(err_str);
+
+ rust_str
+ };
+
+ return Err(rust_str);
+ }
+
+ Ok(())
+ }
+
+ pub fn set_inline_assembly(&self, asm: &str) {
+ let c_string = CString::new(asm).expect("Conversion to CString failed unexpectedly");
+
+ unsafe {
+ LLVMSetModuleInlineAsm(self.module, c_string.as_ptr())
+ }
+ }
+
// REVIEW: Should module take ownership of metadata?
// REVIEW: Should we return a MetadataValue for the global since it's its own value?
// it would be the last item in get_global_metadata I believe
