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.
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.
12 use std::ffi::{CStr, CString};
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;
22 use llvm::{self, Attribute, ValueRef};
23 use llvm::AttributePlace::Function;
25 pub use syntax::attr::{self, InlineAttr};
27 use context::CodegenCx;
29 /// Mark LLVM function to use provided inline heuristic.
31 pub fn inline(val: ValueRef, inline: InlineAttr) {
32 use self::InlineAttr::*;
34 Hint => Attribute::InlineHint.apply_llfn(Function, val),
35 Always => Attribute::AlwaysInline.apply_llfn(Function, val),
36 Never => Attribute::NoInline.apply_llfn(Function, val),
38 Attribute::InlineHint.unapply_llfn(Function, val);
39 Attribute::AlwaysInline.unapply_llfn(Function, val);
40 Attribute::NoInline.unapply_llfn(Function, val);
45 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
47 pub fn emit_uwtable(val: ValueRef, emit: bool) {
48 Attribute::UWTable.toggle_llfn(Function, val, emit);
51 /// Tell LLVM whether the function can or cannot unwind.
53 pub fn unwind(val: ValueRef, can_unwind: bool) {
54 Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
57 /// Tell LLVM whether it should optimize function for size.
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);
64 /// Tell LLVM if this function should be 'naked', i.e. skip the epilogue and prologue.
66 pub fn naked(val: ValueRef, is_naked: bool) {
67 Attribute::Naked.toggle_llfn(Function, val, is_naked);
70 pub fn set_frame_pointer_elimination(cx: &CodegenCx, llfn: ValueRef) {
71 // FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
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"));
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 {
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,
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"));
102 /// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
104 pub fn from_fn_attrs(cx: &CodegenCx, llfn: ValueRef, id: DefId) {
106 let attrs = cx.tcx.get_attrs(id);
107 inline(llfn, find_inline_attr(Some(cx.sess().diagnostic()), &attrs));
109 set_frame_pointer_elimination(cx, llfn);
110 set_probestack(cx, llfn);
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") {
117 } else if attr.check_name("allocator") {
118 Attribute::NoAlias.apply_llfn(
119 llvm::AttributePlace::ReturnValue, llfn);
120 } else if attr.check_name("unwind") {
122 } else if attr.check_name("rustc_allocator_nounwind") {
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);
136 fn cstr(s: &'static str) -> &CStr {
137 CStr::from_bytes_with_nul(s.as_bytes()).expect("null-terminated string")
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)
145 .map(|c| c.to_str().unwrap().to_string())
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") {
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());
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);
169 if tcx.fn_sig(id).unsafety() == Unsafety::Normal {
170 let msg = "#[target_feature(..)] can only be applied to \
172 tcx.sess.span_err(attr.span, msg);
174 from_target_feature(tcx, attr, &whitelist, &mut target_features);
176 Rc::new(target_features)
180 fn from_target_feature(
182 attr: &ast::Attribute,
183 whitelist: &FxHashSet<String>,
184 target_features: &mut Vec<String>,
186 let list = match attr.meta_item_list() {
189 let msg = "#[target_feature] attribute must be of the form \
190 #[target_feature(..)]";
191 tcx.sess.span_err(attr.span, &msg);
197 if !item.check_name("enable") {
198 let msg = "#[target_feature(..)] only accepts sub-keys of `enable` \
200 tcx.sess.span_err(item.span, &msg);
203 let value = match item.value_str() {
206 let msg = "#[target_feature] attribute must be of the form \
207 #[target_feature(enable = \"..\")]";
208 tcx.sess.span_err(item.span, &msg);
212 let value = value.as_str();
213 for feature in value.split(',') {
214 if whitelist.contains(feature) {
215 target_features.push(format!("+{}", feature));
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);
223 if feature.starts_with("+") {
224 let valid = whitelist.contains(&feature[1..]);
226 err.help("consider removing the leading `+` in the feature name");