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