]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/declare.rs
Rollup merge of #56416 - GuillaumeGomez:css-body, r=QuietMisdreavus
[rust.git] / src / librustc_codegen_llvm / declare.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 //! Declare various LLVM values.
11 //!
12 //! Prefer using functions and methods from this module rather than calling LLVM
13 //! functions directly. These functions do some additional work to ensure we do
14 //! the right thing given the preconceptions of codegen.
15 //!
16 //! Some useful guidelines:
17 //!
18 //! * Use declare_* family of methods if you are declaring, but are not
19 //!   interested in defining the Value they return.
20 //! * Use define_* family of methods when you might be defining the Value.
21 //! * When in doubt, define.
22
23 use llvm;
24 use llvm::AttributePlace::Function;
25 use rustc::ty::{self, PolyFnSig};
26 use rustc::ty::layout::LayoutOf;
27 use rustc::session::config::Sanitizer;
28 use rustc_data_structures::small_c_str::SmallCStr;
29 use rustc_target::spec::PanicStrategy;
30 use abi::{Abi, FnType, FnTypeExt};
31 use attributes;
32 use context::CodegenCx;
33 use type_::Type;
34 use rustc_codegen_ssa::traits::*;
35 use value::Value;
36
37 /// Declare a function.
38 ///
39 /// If there’s a value with the same name already declared, the function will
40 /// update the declaration and return existing Value instead.
41 fn declare_raw_fn(
42     cx: &CodegenCx<'ll, '_>,
43     name: &str,
44     callconv: llvm::CallConv,
45     ty: &'ll Type,
46 ) -> &'ll Value {
47     debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
48     let namebuf = SmallCStr::new(name);
49     let llfn = unsafe {
50         llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty)
51     };
52
53     llvm::SetFunctionCallConv(llfn, callconv);
54     // Function addresses in Rust are never significant, allowing functions to
55     // be merged.
56     llvm::SetUnnamedAddr(llfn, true);
57
58     if cx.tcx.sess.opts.cg.no_redzone
59         .unwrap_or(cx.tcx.sess.target.target.options.disable_redzone) {
60         llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
61     }
62
63     if let Some(ref sanitizer) = cx.tcx.sess.opts.debugging_opts.sanitizer {
64         match *sanitizer {
65             Sanitizer::Address => {
66                 llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
67             },
68             Sanitizer::Memory => {
69                 llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
70             },
71             Sanitizer::Thread => {
72                 llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
73             },
74             _ => {}
75         }
76     }
77
78     match cx.tcx.sess.opts.cg.opt_level.as_ref().map(String::as_ref) {
79         Some("s") => {
80             llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
81         },
82         Some("z") => {
83             llvm::Attribute::MinSize.apply_llfn(Function, llfn);
84             llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
85         },
86         _ => {},
87     }
88
89     if cx.tcx.sess.panic_strategy() != PanicStrategy::Unwind {
90         attributes::unwind(llfn, false);
91     }
92
93     attributes::non_lazy_bind(cx.sess(), llfn);
94
95     llfn
96 }
97
98 impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
99
100     fn declare_global(
101         &self,
102         name: &str, ty: &'ll Type
103     ) -> &'ll Value {
104         debug!("declare_global(name={:?})", name);
105         let namebuf = SmallCStr::new(name);
106         unsafe {
107             llvm::LLVMRustGetOrInsertGlobal(self.llmod, namebuf.as_ptr(), ty)
108         }
109     }
110
111     fn declare_cfn(
112         &self,
113         name: &str,
114         fn_type: &'ll Type
115     ) -> &'ll Value {
116         declare_raw_fn(self, name, llvm::CCallConv, fn_type)
117     }
118
119     fn declare_fn(
120         &self,
121         name: &str,
122         sig: PolyFnSig<'tcx>,
123     ) -> &'ll Value {
124         debug!("declare_rust_fn(name={:?}, sig={:?})", name, sig);
125         let sig = self.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
126         debug!("declare_rust_fn (after region erasure) sig={:?}", sig);
127
128         let fty = FnType::new(self, sig, &[]);
129         let llfn = declare_raw_fn(self, name, fty.llvm_cconv(), fty.llvm_type(self));
130
131         if self.layout_of(sig.output()).abi.is_uninhabited() {
132             llvm::Attribute::NoReturn.apply_llfn(Function, llfn);
133         }
134
135         if sig.abi != Abi::Rust && sig.abi != Abi::RustCall {
136             attributes::unwind(llfn, false);
137         }
138
139         fty.apply_attrs_llfn(llfn);
140
141         llfn
142     }
143
144     fn define_global(
145         &self,
146         name: &str,
147         ty: &'ll Type
148     ) -> Option<&'ll Value> {
149         if self.get_defined_value(name).is_some() {
150             None
151         } else {
152             Some(self.declare_global(name, ty))
153         }
154     }
155
156     fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
157         unsafe {
158             llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty)
159         }
160     }
161
162     fn define_fn(
163         &self,
164         name: &str,
165         fn_sig: PolyFnSig<'tcx>,
166     ) -> &'ll Value {
167         if self.get_defined_value(name).is_some() {
168             self.sess().fatal(&format!("symbol `{}` already defined", name))
169         } else {
170             self.declare_fn(name, fn_sig)
171         }
172     }
173
174     fn define_internal_fn(
175         &self,
176         name: &str,
177         fn_sig: PolyFnSig<'tcx>,
178     ) -> &'ll Value {
179         let llfn = self.define_fn(name, fn_sig);
180         unsafe { llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::InternalLinkage) };
181         llfn
182     }
183
184     fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
185         debug!("get_declared_value(name={:?})", name);
186         let namebuf = SmallCStr::new(name);
187         unsafe { llvm::LLVMRustGetNamedValue(self.llmod, namebuf.as_ptr()) }
188     }
189
190     fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
191         self.get_declared_value(name).and_then(|val|{
192             let declaration = unsafe {
193                 llvm::LLVMIsDeclaration(val) != 0
194             };
195             if !declaration {
196                 Some(val)
197             } else {
198                 None
199             }
200         })
201     }
202 }