]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/declare.rs
Rollup merge of #100753 - LuisCardosoOliveira:translation-migrate-session, r=davidtwco
[rust.git] / compiler / rustc_codegen_llvm / src / declare.rs
1 //! Declare various LLVM values.
2 //!
3 //! Prefer using functions and methods from this module rather than calling LLVM
4 //! functions directly. These functions do some additional work to ensure we do
5 //! the right thing given the preconceptions of codegen.
6 //!
7 //! Some useful guidelines:
8 //!
9 //! * Use declare_* family of methods if you are declaring, but are not
10 //!   interested in defining the Value they return.
11 //! * Use define_* family of methods when you might be defining the Value.
12 //! * When in doubt, define.
13
14 use crate::abi::{FnAbi, FnAbiLlvmExt};
15 use crate::attributes;
16 use crate::context::CodegenCx;
17 use crate::llvm;
18 use crate::llvm::AttributePlace::Function;
19 use crate::type_::Type;
20 use crate::value::Value;
21 use rustc_codegen_ssa::traits::TypeMembershipMethods;
22 use rustc_middle::ty::Ty;
23 use rustc_symbol_mangling::typeid::typeid_for_fnabi;
24 use smallvec::SmallVec;
25 use tracing::debug;
26
27 /// Declare a function.
28 ///
29 /// If there’s a value with the same name already declared, the function will
30 /// update the declaration and return existing Value instead.
31 fn declare_raw_fn<'ll>(
32     cx: &CodegenCx<'ll, '_>,
33     name: &str,
34     callconv: llvm::CallConv,
35     unnamed: llvm::UnnamedAddr,
36     ty: &'ll Type,
37 ) -> &'ll Value {
38     debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
39     let llfn = unsafe {
40         llvm::LLVMRustGetOrInsertFunction(cx.llmod, name.as_ptr().cast(), name.len(), ty)
41     };
42
43     llvm::SetFunctionCallConv(llfn, callconv);
44     llvm::SetUnnamedAddress(llfn, unnamed);
45
46     let mut attrs = SmallVec::<[_; 4]>::new();
47
48     if cx.tcx.sess.opts.cg.no_redzone.unwrap_or(cx.tcx.sess.target.disable_redzone) {
49         attrs.push(llvm::AttributeKind::NoRedZone.create_attr(cx.llcx));
50     }
51
52     attrs.extend(attributes::non_lazy_bind_attr(cx));
53
54     attributes::apply_to_llfn(llfn, Function, &attrs);
55
56     llfn
57 }
58
59 impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
60     /// Declare a global value.
61     ///
62     /// If there’s a value with the same name already declared, the function will
63     /// return its Value instead.
64     pub fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
65         debug!("declare_global(name={:?})", name);
66         unsafe { llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty) }
67     }
68
69     /// Declare a C ABI function.
70     ///
71     /// Only use this for foreign function ABIs and glue. For Rust functions use
72     /// `declare_fn` instead.
73     ///
74     /// If there’s a value with the same name already declared, the function will
75     /// update the declaration and return existing Value instead.
76     pub fn declare_cfn(
77         &self,
78         name: &str,
79         unnamed: llvm::UnnamedAddr,
80         fn_type: &'ll Type,
81     ) -> &'ll Value {
82         declare_raw_fn(self, name, llvm::CCallConv, unnamed, fn_type)
83     }
84
85     /// Declare a Rust function.
86     ///
87     /// If there’s a value with the same name already declared, the function will
88     /// update the declaration and return existing Value instead.
89     pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Value {
90         debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
91
92         // Function addresses in Rust are never significant, allowing functions to
93         // be merged.
94         let llfn = declare_raw_fn(
95             self,
96             name,
97             fn_abi.llvm_cconv(),
98             llvm::UnnamedAddr::Global,
99             fn_abi.llvm_type(self),
100         );
101         fn_abi.apply_attrs_llfn(self, llfn);
102
103         if self.tcx.sess.is_sanitizer_cfi_enabled() {
104             let typeid = typeid_for_fnabi(self.tcx, fn_abi);
105             self.set_type_metadata(llfn, typeid);
106         }
107
108         llfn
109     }
110
111     /// Declare a global with an intention to define it.
112     ///
113     /// Use this function when you intend to define a global. This function will
114     /// return `None` if the name already has a definition associated with it. In that
115     /// case an error should be reported to the user, because it usually happens due
116     /// to user’s fault (e.g., misuse of `#[no_mangle]` or `#[export_name]` attributes).
117     pub fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
118         if self.get_defined_value(name).is_some() {
119             None
120         } else {
121             Some(self.declare_global(name, ty))
122         }
123     }
124
125     /// Declare a private global
126     ///
127     /// Use this function when you intend to define a global without a name.
128     pub fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
129         unsafe { llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty) }
130     }
131
132     /// Gets declared value by name.
133     pub fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
134         debug!("get_declared_value(name={:?})", name);
135         unsafe { llvm::LLVMRustGetNamedValue(self.llmod, name.as_ptr().cast(), name.len()) }
136     }
137
138     /// Gets defined or externally defined (AvailableExternally linkage) value by
139     /// name.
140     pub fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
141         self.get_declared_value(name).and_then(|val| {
142             let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
143             if !declaration { Some(val) } else { None }
144         })
145     }
146 }