]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/attributes.rs
d5ec8d1b5526256573cf69552de26dc754f878fd
[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
14 use rustc::hir::TransFnAttrFlags;
15 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
16 use rustc::session::config::Sanitizer;
17 use rustc::ty::maps::Providers;
18 use rustc_data_structures::sync::Lrc;
19
20 use llvm::{self, Attribute, ValueRef};
21 use llvm::AttributePlace::Function;
22 use llvm_util;
23 pub use syntax::attr::{self, InlineAttr};
24 use context::CodegenCx;
25
26 /// Mark LLVM function to use provided inline heuristic.
27 #[inline]
28 pub fn inline(val: ValueRef, inline: InlineAttr) {
29     use self::InlineAttr::*;
30     match inline {
31         Hint   => Attribute::InlineHint.apply_llfn(Function, val),
32         Always => Attribute::AlwaysInline.apply_llfn(Function, val),
33         Never  => Attribute::NoInline.apply_llfn(Function, val),
34         None   => {
35             Attribute::InlineHint.unapply_llfn(Function, val);
36             Attribute::AlwaysInline.unapply_llfn(Function, val);
37             Attribute::NoInline.unapply_llfn(Function, val);
38         },
39     };
40 }
41
42 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
43 #[inline]
44 pub fn emit_uwtable(val: ValueRef, emit: bool) {
45     Attribute::UWTable.toggle_llfn(Function, val, emit);
46 }
47
48 /// Tell LLVM whether the function can or cannot unwind.
49 #[inline]
50 pub fn unwind(val: ValueRef, can_unwind: bool) {
51     Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
52 }
53
54 /// Tell LLVM whether it should optimize function for size.
55 #[inline]
56 #[allow(dead_code)] // possibly useful function
57 pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
58     Attribute::OptimizeForSize.toggle_llfn(Function, val, optimize);
59 }
60
61 /// Tell LLVM if this function should be 'naked', i.e. skip the epilogue and prologue.
62 #[inline]
63 pub fn naked(val: ValueRef, is_naked: bool) {
64     Attribute::Naked.toggle_llfn(Function, val, is_naked);
65 }
66
67 pub fn set_frame_pointer_elimination(cx: &CodegenCx, llfn: ValueRef) {
68     // FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
69     // parameter.
70     if cx.sess().must_not_eliminate_frame_pointers() {
71         llvm::AddFunctionAttrStringValue(
72             llfn, llvm::AttributePlace::Function,
73             cstr("no-frame-pointer-elim\0"), cstr("true\0"));
74     }
75 }
76
77 pub fn set_probestack(cx: &CodegenCx, llfn: ValueRef) {
78     // Only use stack probes if the target specification indicates that we
79     // should be using stack probes
80     if !cx.sess().target.target.options.stack_probes {
81         return
82     }
83
84     // Currently stack probes seem somewhat incompatible with the address
85     // sanitizer. With asan we're already protected from stack overflow anyway
86     // so we don't really need stack probes regardless.
87     match cx.sess().opts.debugging_opts.sanitizer {
88         Some(Sanitizer::Address) => return,
89         _ => {}
90     }
91
92     // Flag our internal `__rust_probestack` function as the stack probe symbol.
93     // This is defined in the `compiler-builtins` crate for each architecture.
94     llvm::AddFunctionAttrStringValue(
95         llfn, llvm::AttributePlace::Function,
96         cstr("probe-stack\0"), cstr("__rust_probestack\0"));
97 }
98
99 /// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
100 /// attributes.
101 pub fn from_fn_attrs(cx: &CodegenCx, llfn: ValueRef, id: DefId) {
102     let trans_fn_attrs = cx.tcx.trans_fn_attrs(id);
103
104     inline(llfn, trans_fn_attrs.inline);
105
106     set_frame_pointer_elimination(cx, llfn);
107     set_probestack(cx, llfn);
108
109     if trans_fn_attrs.flags.contains(TransFnAttrFlags::COLD) {
110         Attribute::Cold.apply_llfn(Function, llfn);
111     }
112     if trans_fn_attrs.flags.contains(TransFnAttrFlags::NAKED) {
113         naked(llfn, true);
114     }
115     if trans_fn_attrs.flags.contains(TransFnAttrFlags::ALLOCATOR) {
116         Attribute::NoAlias.apply_llfn(
117             llvm::AttributePlace::ReturnValue, llfn);
118     }
119     if trans_fn_attrs.flags.contains(TransFnAttrFlags::UNWIND) {
120         unwind(llfn, true);
121     }
122     if trans_fn_attrs.flags.contains(TransFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND) {
123         unwind(llfn, false);
124     }
125
126     let features =
127         trans_fn_attrs.target_features
128         .iter()
129         .map(|f| {
130             let feature = &*f.as_str();
131             format!("+{}", llvm_util::to_llvm_feature(cx.tcx.sess, feature))
132         })
133         .collect::<Vec<String>>()
134         .join(",");
135
136     if !features.is_empty() {
137         let val = CString::new(features).unwrap();
138         llvm::AddFunctionAttrStringValue(
139             llfn, llvm::AttributePlace::Function,
140             cstr("target-features\0"), &val);
141     }
142 }
143
144 fn cstr(s: &'static str) -> &CStr {
145     CStr::from_bytes_with_nul(s.as_bytes()).expect("null-terminated string")
146 }
147
148 pub fn provide(providers: &mut Providers) {
149     providers.target_features_whitelist = |tcx, cnum| {
150         assert_eq!(cnum, LOCAL_CRATE);
151         Lrc::new(llvm_util::target_feature_whitelist(tcx.sess)
152             .iter()
153             .map(|c| c.to_string())
154             .collect())
155     };
156 }