Skip to main content

rustc_codegen_llvm/back/
owned_target_machine.rs

1use std::ffi::CStr;
2use std::marker::PhantomData;
3use std::ptr::NonNull;
4
5use rustc_data_structures::small_c_str::SmallCStr;
6
7use crate::errors::LlvmError;
8use crate::llvm;
9
10/// Responsible for safely creating and disposing llvm::TargetMachine via ffi functions.
11/// Not cloneable as there is no clone function for llvm::TargetMachine.
12#[repr(transparent)]
13pub struct OwnedTargetMachine {
14    tm_unique: NonNull<llvm::TargetMachine>,
15    phantom: PhantomData<llvm::TargetMachine>,
16}
17
18impl OwnedTargetMachine {
19    pub(crate) 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        float_abi: llvm::FloatAbi,
28        function_sections: bool,
29        data_sections: bool,
30        unique_section_names: bool,
31        trap_unreachable: bool,
32        singlethread: bool,
33        verbose_asm: bool,
34        emit_stack_size_section: bool,
35        relax_elf_relocations: bool,
36        use_init_array: bool,
37        split_dwarf_file: &CStr,
38        output_obj_file: &CStr,
39        debug_info_compression: llvm::CompressionKind,
40        use_emulated_tls: bool,
41        use_wasm_eh: bool,
42        large_data_threshold: u64,
43    ) -> Result<Self, LlvmError<'static>> {
44        // SAFETY: llvm::LLVMRustCreateTargetMachine copies pointed to data
45        let tm_ptr = unsafe {
46            llvm::LLVMRustCreateTargetMachine(
47                triple.as_ptr(),
48                cpu.as_ptr(),
49                features.as_ptr(),
50                abi.as_ptr(),
51                model,
52                reloc,
53                level,
54                float_abi,
55                function_sections,
56                data_sections,
57                unique_section_names,
58                trap_unreachable,
59                singlethread,
60                verbose_asm,
61                emit_stack_size_section,
62                relax_elf_relocations,
63                use_init_array,
64                split_dwarf_file.as_ptr(),
65                output_obj_file.as_ptr(),
66                debug_info_compression,
67                use_emulated_tls,
68                use_wasm_eh,
69                large_data_threshold,
70            )
71        };
72
73        NonNull::new(tm_ptr)
74            .map(|tm_unique| Self { tm_unique, phantom: PhantomData })
75            .ok_or_else(|| LlvmError::CreateTargetMachine { triple: SmallCStr::from(triple) })
76    }
77
78    /// Returns inner `llvm::TargetMachine` type.
79    ///
80    /// This could be a `Deref` implementation, but `llvm::TargetMachine` is an extern type and
81    /// `Deref::Target: ?Sized`.
82    pub fn raw(&self) -> &llvm::TargetMachine {
83        // SAFETY: constructing ensures we have a valid pointer created by
84        // llvm::LLVMRustCreateTargetMachine.
85        unsafe { self.tm_unique.as_ref() }
86    }
87}
88
89impl Drop for OwnedTargetMachine {
90    fn drop(&mut self) {
91        // SAFETY: constructing ensures we have a valid pointer created by
92        // llvm::LLVMRustCreateTargetMachine OwnedTargetMachine is not copyable so there is no
93        // double free or use after free.
94        unsafe { llvm::LLVMDisposeTargetMachine(self.tm_unique) };
95    }
96}