]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/declare.rs
Rollup merge of #67090 - kraai:either-to-any, r=jonas-schievink
[rust.git] / src / librustc_codegen_llvm / 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::llvm;
15 use crate::llvm::AttributePlace::Function;
16 use crate::abi::{FnAbi, FnAbiLlvmExt};
17 use crate::attributes;
18 use crate::context::CodegenCx;
19 use crate::type_::Type;
20 use crate::value::Value;
21 use rustc::ty::Ty;
22 use rustc::session::config::Sanitizer;
23 use rustc_data_structures::small_c_str::SmallCStr;
24 use rustc_codegen_ssa::traits::*;
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(
31     cx: &CodegenCx<'ll, '_>,
32     name: &str,
33     callconv: llvm::CallConv,
34     ty: &'ll Type,
35 ) -> &'ll Value {
36     debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
37     let namebuf = SmallCStr::new(name);
38     let llfn = unsafe {
39         llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty)
40     };
41
42     llvm::SetFunctionCallConv(llfn, callconv);
43     // Function addresses in Rust are never significant, allowing functions to
44     // be merged.
45     llvm::SetUnnamedAddr(llfn, true);
46
47     if cx.tcx.sess.opts.cg.no_redzone
48         .unwrap_or(cx.tcx.sess.target.target.options.disable_redzone) {
49         llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
50     }
51
52     if let Some(ref sanitizer) = cx.tcx.sess.opts.debugging_opts.sanitizer {
53         match *sanitizer {
54             Sanitizer::Address => {
55                 llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
56             },
57             Sanitizer::Memory => {
58                 llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
59             },
60             Sanitizer::Thread => {
61                 llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
62             },
63             _ => {}
64         }
65     }
66
67     attributes::default_optimisation_attrs(cx.tcx.sess, llfn);
68     attributes::non_lazy_bind(cx.sess(), llfn);
69     llfn
70 }
71
72 impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
73
74     fn declare_global(
75         &self,
76         name: &str, ty: &'ll Type
77     ) -> &'ll Value {
78         debug!("declare_global(name={:?})", name);
79         unsafe {
80             llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty)
81         }
82     }
83
84     fn declare_cfn(
85         &self,
86         name: &str,
87         fn_type: &'ll Type
88     ) -> &'ll Value {
89         declare_raw_fn(self, name, llvm::CCallConv, fn_type)
90     }
91
92     fn declare_fn(
93         &self,
94         name: &str,
95         fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
96     ) -> &'ll Value {
97         debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
98
99         let llfn = declare_raw_fn(self, name, fn_abi.llvm_cconv(), fn_abi.llvm_type(self));
100         fn_abi.apply_attrs_llfn(self, llfn);
101         llfn
102     }
103
104     fn define_global(
105         &self,
106         name: &str,
107         ty: &'ll Type
108     ) -> Option<&'ll Value> {
109         if self.get_defined_value(name).is_some() {
110             None
111         } else {
112             Some(self.declare_global(name, ty))
113         }
114     }
115
116     fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
117         unsafe {
118             llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty)
119         }
120     }
121
122     fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
123         debug!("get_declared_value(name={:?})", name);
124         let namebuf = SmallCStr::new(name);
125         unsafe { llvm::LLVMRustGetNamedValue(self.llmod, namebuf.as_ptr()) }
126     }
127
128     fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
129         self.get_declared_value(name).and_then(|val|{
130             let declaration = unsafe {
131                 llvm::LLVMIsDeclaration(val) != 0
132             };
133             if !declaration {
134                 Some(val)
135             } else {
136                 None
137             }
138         })
139     }
140 }