-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrap llvm::TargetMachine and use pointers instead of &'static mut for…
… creating/disposing
- Loading branch information
Showing
7 changed files
with
140 additions
and
40 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
compiler/rustc_codegen_llvm/src/back/target_machine_wrapper.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::{ | ||
ffi::{c_char, CStr}, | ||
marker::PhantomData, | ||
ops::Deref, | ||
ptr::NonNull, | ||
}; | ||
|
||
use rustc_data_structures::small_c_str::SmallCStr; | ||
|
||
use crate::{errors::LlvmError, llvm}; | ||
|
||
#[repr(transparent)] | ||
pub struct TargetMachineWrapper { | ||
tm: NonNull<llvm::TargetMachine>, | ||
phantom: PhantomData<&'static mut llvm::TargetMachine>, | ||
} | ||
|
||
impl TargetMachineWrapper { | ||
pub fn new( | ||
triple: &CStr, | ||
cpu: &CStr, | ||
features: &CStr, | ||
abi: &CStr, | ||
model: llvm::CodeModel, | ||
reloc: llvm::RelocModel, | ||
level: llvm::CodeGenOptLevel, | ||
use_soft_fp: bool, | ||
function_sections: bool, | ||
data_sections: bool, | ||
unique_section_names: bool, | ||
trap_unreachable: bool, | ||
singletree: bool, | ||
asm_comments: bool, | ||
emit_stack_size_section: bool, | ||
relax_elf_relocations: bool, | ||
use_init_array: bool, | ||
split_dwarf_file: &CStr, | ||
debug_info_compression: &CStr, | ||
force_emulated_tls: bool, | ||
args_cstr_buff: &[u8], | ||
) -> Result<Self, LlvmError<'static>> { | ||
assert!(args_cstr_buff.len() > 0); | ||
assert!( | ||
*args_cstr_buff.last().unwrap() == 0, | ||
"The last character must be a null terminator." | ||
); | ||
|
||
let tm_ptr = unsafe { | ||
llvm::LLVMRustCreateTargetMachine( | ||
triple.as_ptr(), | ||
cpu.as_ptr(), | ||
features.as_ptr(), | ||
abi.as_ptr(), | ||
model, | ||
reloc, | ||
level, | ||
use_soft_fp, | ||
function_sections, | ||
data_sections, | ||
unique_section_names, | ||
trap_unreachable, | ||
singletree, | ||
asm_comments, | ||
emit_stack_size_section, | ||
relax_elf_relocations, | ||
use_init_array, | ||
split_dwarf_file.as_ptr(), | ||
debug_info_compression.as_ptr(), | ||
force_emulated_tls, | ||
args_cstr_buff.as_ptr() as *const c_char, | ||
args_cstr_buff.len(), | ||
) | ||
}; | ||
|
||
NonNull::new(tm_ptr) | ||
.map(|tm| Self { tm, phantom: PhantomData }) | ||
.ok_or_else(|| LlvmError::CreateTargetMachine { triple: SmallCStr::from(triple) }) | ||
} | ||
} | ||
|
||
impl Deref for TargetMachineWrapper { | ||
type Target = llvm::TargetMachine; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
// SAFETY: constructing ensures we have a valid pointer created by llvm::LLVMRustCreateTargetMachine | ||
unsafe { self.tm.as_ref() } | ||
} | ||
} | ||
|
||
impl Drop for TargetMachineWrapper { | ||
fn drop(&mut self) { | ||
// SAFETY: constructing ensures we have a valid pointer created by llvm::LLVMRustCreateTargetMachine | ||
// TargetMachineWrapper is not copyable so there is no double free or use after free | ||
unsafe { | ||
llvm::LLVMRustDisposeTargetMachine(self.tm.as_mut()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters