]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/attributes.rs
Add an unstable FileTypeExt extension trait for Windows
[rust.git] / src / librustc_trans / attributes.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 //! Set and unset common attributes on LLVM values.
11
12 use std::ffi::{CStr, CString};
13 use std::rc::Rc;
14
15 use rustc::hir::Unsafety;
16 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
17 use rustc::session::config::Sanitizer;
18 use rustc::ty::TyCtxt;
19 use rustc::ty::maps::Providers;
20 use rustc_data_structures::fx::FxHashSet;
21
22 use llvm::{self, Attribute, ValueRef};
23 use llvm::AttributePlace::Function;
24 use llvm_util;
25 pub use syntax::attr::{self, InlineAttr};
26 use syntax::ast;
27 use context::CodegenCx;
28
29 /// Mark LLVM function to use provided inline heuristic.
30 #[inline]
31 pub fn inline(val: ValueRef, inline: InlineAttr) {
32     use self::InlineAttr::*;
33     match inline {
34         Hint   => Attribute::InlineHint.apply_llfn(Function, val),
35         Always => Attribute::AlwaysInline.apply_llfn(Function, val),
36         Never  => Attribute::NoInline.apply_llfn(Function, val),
37         None   => {
38             Attribute::InlineHint.unapply_llfn(Function, val);
39             Attribute::AlwaysInline.unapply_llfn(Function, val);
40             Attribute::NoInline.unapply_llfn(Function, val);
41         },
42     };
43 }
44
45 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
46 #[inline]
47 pub fn emit_uwtable(val: ValueRef, emit: bool) {
48     Attribute::UWTable.toggle_llfn(Function, val, emit);
49 }
50
51 /// Tell LLVM whether the function can or cannot unwind.
52 #[inline]
53 pub fn unwind(val: ValueRef, can_unwind: bool) {
54     Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
55 }
56
57 /// Tell LLVM whether it should optimize function for size.
58 #[inline]
59 #[allow(dead_code)] // possibly useful function
60 pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
61     Attribute::OptimizeForSize.toggle_llfn(Function, val, optimize);
62 }
63
64 /// Tell LLVM if this function should be 'naked', i.e. skip the epilogue and prologue.
65 #[inline]
66 pub fn naked(val: ValueRef, is_naked: bool) {
67     Attribute::Naked.toggle_llfn(Function, val, is_naked);
68 }
69
70 pub fn set_frame_pointer_elimination(cx: &CodegenCx, llfn: ValueRef) {
71     // FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
72     // parameter.
73     if cx.sess().must_not_eliminate_frame_pointers() {
74         llvm::AddFunctionAttrStringValue(
75             llfn, llvm::AttributePlace::Function,
76             cstr("no-frame-pointer-elim\0"), cstr("true\0"));
77     }
78 }
79
80 pub fn set_probestack(cx: &CodegenCx, llfn: ValueRef) {
81     // Only use stack probes if the target specification indicates that we
82     // should be using stack probes
83     if !cx.sess().target.target.options.stack_probes {
84         return
85     }
86
87     // Currently stack probes seem somewhat incompatible with the address
88     // sanitizer. With asan we're already protected from stack overflow anyway
89     // so we don't really need stack probes regardless.
90     match cx.sess().opts.debugging_opts.sanitizer {
91         Some(Sanitizer::Address) => return,
92         _ => {}
93     }
94
95     // Flag our internal `__rust_probestack` function as the stack probe symbol.
96     // This is defined in the `compiler-builtins` crate for each architecture.
97     llvm::AddFunctionAttrStringValue(
98         llfn, llvm::AttributePlace::Function,
99         cstr("probe-stack\0"), cstr("__rust_probestack\0"));
100 }
101
102 /// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
103 /// attributes.
104 pub fn from_fn_attrs(cx: &CodegenCx, llfn: ValueRef, id: DefId) {
105     use syntax::attr::*;
106     let attrs = cx.tcx.get_attrs(id);
107     inline(llfn, find_inline_attr(Some(cx.sess().diagnostic()), &attrs));
108
109     set_frame_pointer_elimination(cx, llfn);
110     set_probestack(cx, llfn);
111
112     for attr in attrs.iter() {
113         if attr.check_name("cold") {
114             Attribute::Cold.apply_llfn(Function, llfn);
115         } else if attr.check_name("naked") {
116             naked(llfn, true);
117         } else if attr.check_name("allocator") {
118             Attribute::NoAlias.apply_llfn(
119                 llvm::AttributePlace::ReturnValue, llfn);
120         } else if attr.check_name("unwind") {
121             unwind(llfn, true);
122         } else if attr.check_name("rustc_allocator_nounwind") {
123             unwind(llfn, false);
124         }
125     }
126
127     let target_features = cx.tcx.target_features_enabled(id);
128     if !target_features.is_empty() {
129         let val = CString::new(target_features.join(",")).unwrap();
130         llvm::AddFunctionAttrStringValue(
131             llfn, llvm::AttributePlace::Function,
132             cstr("target-features\0"), &val);
133     }
134 }
135
136 fn cstr(s: &'static str) -> &CStr {
137     CStr::from_bytes_with_nul(s.as_bytes()).expect("null-terminated string")
138 }
139
140 pub fn provide(providers: &mut Providers) {
141     providers.target_features_whitelist = |tcx, cnum| {
142         assert_eq!(cnum, LOCAL_CRATE);
143         Rc::new(llvm_util::target_feature_whitelist(tcx.sess)
144             .iter()
145             .map(|c| c.to_str().unwrap().to_string())
146             .collect())
147     };
148
149     providers.target_features_enabled = |tcx, id| {
150         let whitelist = tcx.target_features_whitelist(LOCAL_CRATE);
151         let mut target_features = Vec::new();
152         for attr in tcx.get_attrs(id).iter() {
153             if !attr.check_name("target_feature") {
154                 continue
155             }
156             if let Some(val) = attr.value_str() {
157                 for feat in val.as_str().split(",").map(|f| f.trim()) {
158                     if !feat.is_empty() && !feat.contains('\0') {
159                         target_features.push(feat.to_string());
160                     }
161                 }
162                 let msg = "#[target_feature = \"..\"] is deprecated and will \
163                            eventually be removed, use \
164                            #[target_feature(enable = \"..\")] instead";
165                 tcx.sess.span_warn(attr.span, &msg);
166                 continue
167             }
168
169             if tcx.fn_sig(id).unsafety() == Unsafety::Normal {
170                 let msg = "#[target_feature(..)] can only be applied to \
171                            `unsafe` function";
172                 tcx.sess.span_err(attr.span, msg);
173             }
174             from_target_feature(tcx, attr, &whitelist, &mut target_features);
175         }
176         Rc::new(target_features)
177     };
178 }
179
180 fn from_target_feature(
181     tcx: TyCtxt,
182     attr: &ast::Attribute,
183     whitelist: &FxHashSet<String>,
184     target_features: &mut Vec<String>,
185 ) {
186     let list = match attr.meta_item_list() {
187         Some(list) => list,
188         None => {
189             let msg = "#[target_feature] attribute must be of the form \
190                        #[target_feature(..)]";
191             tcx.sess.span_err(attr.span, &msg);
192             return
193         }
194     };
195
196     for item in list {
197         if !item.check_name("enable") {
198             let msg = "#[target_feature(..)] only accepts sub-keys of `enable` \
199                        currently";
200             tcx.sess.span_err(item.span, &msg);
201             continue
202         }
203         let value = match item.value_str() {
204             Some(list) => list,
205             None => {
206                 let msg = "#[target_feature] attribute must be of the form \
207                            #[target_feature(enable = \"..\")]";
208                 tcx.sess.span_err(item.span, &msg);
209                 continue
210             }
211         };
212         let value = value.as_str();
213         for feature in value.split(',') {
214             if whitelist.contains(feature) {
215                 target_features.push(format!("+{}", feature));
216                 continue
217             }
218
219             let msg = format!("the feature named `{}` is not valid for \
220                                this target", feature);
221             let mut err = tcx.sess.struct_span_err(item.span, &msg);
222
223             if feature.starts_with("+") {
224                 let valid = whitelist.contains(&feature[1..]);
225                 if valid {
226                     err.help("consider removing the leading `+` in the feature name");
227                 }
228             }
229             err.emit();
230         }
231     }
232 }