|
| 1 | +use std::{ |
| 2 | + ffi::{c_char, CStr}, |
| 3 | + marker::PhantomData, |
| 4 | + ops::Deref, |
| 5 | + ptr::NonNull, |
| 6 | +}; |
| 7 | + |
| 8 | +use rustc_data_structures::small_c_str::SmallCStr; |
| 9 | + |
| 10 | +use crate::{errors::LlvmError, llvm}; |
| 11 | + |
| 12 | +#[repr(transparent)] |
| 13 | +pub struct TargetMachineWrapper { |
| 14 | + tm: NonNull<llvm::TargetMachine>, |
| 15 | + phantom: PhantomData<&'static mut llvm::TargetMachine>, |
| 16 | +} |
| 17 | + |
| 18 | +impl TargetMachineWrapper { |
| 19 | + pub fn new( |
| 20 | + triple: &CStr, |
| 21 | + cpu: &CStr, |
| 22 | + features: &CStr, |
| 23 | + abi: &CStr, |
| 24 | + model: llvm::CodeModel, |
| 25 | + reloc: llvm::RelocModel, |
| 26 | + level: llvm::CodeGenOptLevel, |
| 27 | + use_soft_fp: bool, |
| 28 | + function_sections: bool, |
| 29 | + data_sections: bool, |
| 30 | + unique_section_names: bool, |
| 31 | + trap_unreachable: bool, |
| 32 | + singletree: bool, |
| 33 | + asm_comments: bool, |
| 34 | + emit_stack_size_section: bool, |
| 35 | + relax_elf_relocations: bool, |
| 36 | + use_init_array: bool, |
| 37 | + split_dwarf_file: &CStr, |
| 38 | + debug_info_compression: &CStr, |
| 39 | + force_emulated_tls: bool, |
| 40 | + args_cstr_buff: &[u8], |
| 41 | + ) -> Result<Self, LlvmError<'static>> { |
| 42 | + assert!(args_cstr_buff.len() > 0); |
| 43 | + assert!( |
| 44 | + *args_cstr_buff.last().unwrap() == 0, |
| 45 | + "The last character must be a null terminator." |
| 46 | + ); |
| 47 | + |
| 48 | + let tm_ptr = unsafe { |
| 49 | + llvm::LLVMRustCreateTargetMachine( |
| 50 | + triple.as_ptr(), |
| 51 | + cpu.as_ptr(), |
| 52 | + features.as_ptr(), |
| 53 | + abi.as_ptr(), |
| 54 | + model, |
| 55 | + reloc, |
| 56 | + level, |
| 57 | + use_soft_fp, |
| 58 | + function_sections, |
| 59 | + data_sections, |
| 60 | + unique_section_names, |
| 61 | + trap_unreachable, |
| 62 | + singletree, |
| 63 | + asm_comments, |
| 64 | + emit_stack_size_section, |
| 65 | + relax_elf_relocations, |
| 66 | + use_init_array, |
| 67 | + split_dwarf_file.as_ptr(), |
| 68 | + debug_info_compression.as_ptr(), |
| 69 | + force_emulated_tls, |
| 70 | + args_cstr_buff.as_ptr() as *const c_char, |
| 71 | + args_cstr_buff.len(), |
| 72 | + ) |
| 73 | + }; |
| 74 | + |
| 75 | + NonNull::new(tm_ptr) |
| 76 | + .map(|tm| Self { tm, phantom: PhantomData }) |
| 77 | + .ok_or_else(|| LlvmError::CreateTargetMachine { triple: SmallCStr::from(triple) }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl Deref for TargetMachineWrapper { |
| 82 | + type Target = llvm::TargetMachine; |
| 83 | + |
| 84 | + fn deref(&self) -> &Self::Target { |
| 85 | + // SAFETY: constructing ensures we have a valid pointer created by llvm::LLVMRustCreateTargetMachine |
| 86 | + unsafe { self.tm.as_ref() } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl Drop for TargetMachineWrapper { |
| 91 | + fn drop(&mut self) { |
| 92 | + // SAFETY: constructing ensures we have a valid pointer created by llvm::LLVMRustCreateTargetMachine |
| 93 | + // TargetMachineWrapper is not copyable so there is no double free or use after free |
| 94 | + unsafe { |
| 95 | + llvm::LLVMRustDisposeTargetMachine(self.tm.as_mut()); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments