]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/declare.rs
Rollup merge of #94094 - chrisnc:tcp-nodelay-windows-bool, r=dtolnay
[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_middle::ty::Ty;
22 use smallvec::SmallVec;
23 use tracing::debug;
24
25 /// Declare a function.
26 ///
27 /// If there’s a value with the same name already declared, the function will
28 /// update the declaration and return existing Value instead.
29 fn declare_raw_fn<'ll>(
30     cx: &CodegenCx<'ll, '_>,
31     name: &str,
32     callconv: llvm::CallConv,
33     unnamed: llvm::UnnamedAddr,
34     ty: &'ll Type,
35 ) -> &'ll Value {
36     debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
37     let llfn = unsafe {
38         llvm::LLVMRustGetOrInsertFunction(cx.llmod, name.as_ptr().cast(), name.len(), ty)
39     };
40
41     llvm::SetFunctionCallConv(llfn, callconv);
42     llvm::SetUnnamedAddress(llfn, unnamed);
43
44     let mut attrs_to_remove = SmallVec::<[_; 4]>::new();
45     let mut attrs_to_add = SmallVec::<[_; 4]>::new();
46
47     if cx.tcx.sess.opts.cg.no_redzone.unwrap_or(cx.tcx.sess.target.disable_redzone) {
48         attrs_to_add.push(llvm::AttributeKind::NoRedZone.create_attr(cx.llcx));
49     }
50
51     let (to_remove, to_add) = attributes::default_optimisation_attrs(cx);
52     attrs_to_remove.extend(to_remove);
53     attrs_to_add.extend(to_add);
54
55     attrs_to_add.extend(attributes::non_lazy_bind_attr(cx));
56
57     attributes::remove_from_llfn(llfn, Function, &attrs_to_remove);
58     attributes::apply_to_llfn(llfn, Function, &attrs_to_add);
59
60     llfn
61 }
62
63 impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
64     /// Declare a global value.
65     ///
66     /// If there’s a value with the same name already declared, the function will
67     /// return its Value instead.
68     pub fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
69         debug!("declare_global(name={:?})", name);
70         unsafe { llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty) }
71     }
72
73     /// Declare a C ABI function.
74     ///
75     /// Only use this for foreign function ABIs and glue. For Rust functions use
76     /// `declare_fn` instead.
77     ///
78     /// If there’s a value with the same name already declared, the function will
79     /// update the declaration and return existing Value instead.
80     pub fn declare_cfn(
81         &self,
82         name: &str,
83         unnamed: llvm::UnnamedAddr,
84         fn_type: &'ll Type,
85     ) -> &'ll Value {
86         declare_raw_fn(self, name, llvm::CCallConv, unnamed, fn_type)
87     }
88
89     /// Declare a Rust function.
90     ///
91     /// If there’s a value with the same name already declared, the function will
92     /// update the declaration and return existing Value instead.
93     pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Value {
94         debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
95
96         // Function addresses in Rust are never significant, allowing functions to
97         // be merged.
98         let llfn = declare_raw_fn(
99             self,
100             name,
101             fn_abi.llvm_cconv(),
102             llvm::UnnamedAddr::Global,
103             fn_abi.llvm_type(self),
104         );
105         fn_abi.apply_attrs_llfn(self, llfn);
106         llfn
107     }
108
109     /// Declare a global with an intention to define it.
110     ///
111     /// Use this function when you intend to define a global. This function will
112     /// return `None` if the name already has a definition associated with it. In that
113     /// case an error should be reported to the user, because it usually happens due
114     /// to user’s fault (e.g., misuse of `#[no_mangle]` or `#[export_name]` attributes).
115     pub fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
116         if self.get_defined_value(name).is_some() {
117             None
118         } else {
119             Some(self.declare_global(name, ty))
120         }
121     }
122
123     /// Declare a private global
124     ///
125     /// Use this function when you intend to define a global without a name.
126     pub fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
127         unsafe { llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty) }
128     }
129
130     /// Gets declared value by name.
131     pub fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
132         debug!("get_declared_value(name={:?})", name);
133         unsafe { llvm::LLVMRustGetNamedValue(self.llmod, name.as_ptr().cast(), name.len()) }
134     }
135
136     /// Gets defined or externally defined (AvailableExternally linkage) value by
137     /// name.
138     pub fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
139         self.get_declared_value(name).and_then(|val| {
140             let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
141             if !declaration { Some(val) } else { None }
142         })
143     }
144 }