]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/declare.rs
Rollup merge of #104672 - Voultapher:unify-sort-modules, r=thomcc
[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::{kcfi_typeid_for_fnabi, typeid_for_fnabi};
24 use smallvec::SmallVec;
25
26 /// Declare a function.
27 ///
28 /// If there’s a value with the same name already declared, the function will
29 /// update the declaration and return existing Value instead.
30 fn declare_raw_fn<'ll>(
31     cx: &CodegenCx<'ll, '_>,
32     name: &str,
33     callconv: llvm::CallConv,
34     unnamed: llvm::UnnamedAddr,
35     visibility: llvm::Visibility,
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     llvm::set_visibility(llfn, visibility);
46
47     let mut attrs = SmallVec::<[_; 4]>::new();
48
49     if cx.tcx.sess.opts.cg.no_redzone.unwrap_or(cx.tcx.sess.target.disable_redzone) {
50         attrs.push(llvm::AttributeKind::NoRedZone.create_attr(cx.llcx));
51     }
52
53     attrs.extend(attributes::non_lazy_bind_attr(cx));
54
55     attributes::apply_to_llfn(llfn, Function, &attrs);
56
57     llfn
58 }
59
60 impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
61     /// Declare a global value.
62     ///
63     /// If there’s a value with the same name already declared, the function will
64     /// return its Value instead.
65     pub fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
66         debug!("declare_global(name={:?})", name);
67         unsafe { llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty) }
68     }
69
70     /// Declare a C ABI function.
71     ///
72     /// Only use this for foreign function ABIs and glue. For Rust functions use
73     /// `declare_fn` instead.
74     ///
75     /// If there’s a value with the same name already declared, the function will
76     /// update the declaration and return existing Value instead.
77     pub fn declare_cfn(
78         &self,
79         name: &str,
80         unnamed: llvm::UnnamedAddr,
81         fn_type: &'ll Type,
82     ) -> &'ll Value {
83         // Declare C ABI functions with the visibility used by C by default.
84         let visibility = if self.tcx.sess.target.default_hidden_visibility {
85             llvm::Visibility::Hidden
86         } else {
87             llvm::Visibility::Default
88         };
89
90         declare_raw_fn(self, name, llvm::CCallConv, unnamed, visibility, fn_type)
91     }
92
93     /// Declare an entry Function
94     ///
95     /// The ABI of this function can change depending on the target (although for now the same as
96     /// `declare_cfn`)
97     ///
98     /// If there’s a value with the same name already declared, the function will
99     /// update the declaration and return existing Value instead.
100     pub fn declare_entry_fn(
101         &self,
102         name: &str,
103         callconv: llvm::CallConv,
104         unnamed: llvm::UnnamedAddr,
105         fn_type: &'ll Type,
106     ) -> &'ll Value {
107         let visibility = if self.tcx.sess.target.default_hidden_visibility {
108             llvm::Visibility::Hidden
109         } else {
110             llvm::Visibility::Default
111         };
112         declare_raw_fn(self, name, callconv, unnamed, visibility, fn_type)
113     }
114
115     /// Declare a Rust function.
116     ///
117     /// If there’s a value with the same name already declared, the function will
118     /// update the declaration and return existing Value instead.
119     pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Value {
120         debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
121
122         // Function addresses in Rust are never significant, allowing functions to
123         // be merged.
124         let llfn = declare_raw_fn(
125             self,
126             name,
127             fn_abi.llvm_cconv(),
128             llvm::UnnamedAddr::Global,
129             llvm::Visibility::Default,
130             fn_abi.llvm_type(self),
131         );
132         fn_abi.apply_attrs_llfn(self, llfn);
133
134         if self.tcx.sess.is_sanitizer_cfi_enabled() {
135             let typeid = typeid_for_fnabi(self.tcx, fn_abi);
136             self.set_type_metadata(llfn, typeid);
137         }
138
139         if self.tcx.sess.is_sanitizer_kcfi_enabled() {
140             let kcfi_typeid = kcfi_typeid_for_fnabi(self.tcx, fn_abi);
141             self.set_kcfi_type_metadata(llfn, kcfi_typeid);
142         }
143
144         llfn
145     }
146
147     /// Declare a global with an intention to define it.
148     ///
149     /// Use this function when you intend to define a global. This function will
150     /// return `None` if the name already has a definition associated with it. In that
151     /// case an error should be reported to the user, because it usually happens due
152     /// to user’s fault (e.g., misuse of `#[no_mangle]` or `#[export_name]` attributes).
153     pub fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
154         if self.get_defined_value(name).is_some() {
155             None
156         } else {
157             Some(self.declare_global(name, ty))
158         }
159     }
160
161     /// Declare a private global
162     ///
163     /// Use this function when you intend to define a global without a name.
164     pub fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
165         unsafe { llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty) }
166     }
167
168     /// Gets declared value by name.
169     pub fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
170         debug!("get_declared_value(name={:?})", name);
171         unsafe { llvm::LLVMRustGetNamedValue(self.llmod, name.as_ptr().cast(), name.len()) }
172     }
173
174     /// Gets defined or externally defined (AvailableExternally linkage) value by
175     /// name.
176     pub fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
177         self.get_declared_value(name).and_then(|val| {
178             let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
179             if !declaration { Some(val) } else { None }
180         })
181     }
182 }