]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/attributes.rs
Auto merge of #43651 - petrochenkov:foreign-life, r=eddyb
[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::session::config::Sanitizer;
15
16 use llvm::{self, Attribute, ValueRef};
17 use llvm::AttributePlace::Function;
18 pub use syntax::attr::{self, InlineAttr};
19 use syntax::ast;
20 use context::CrateContext;
21
22 /// Mark LLVM function to use provided inline heuristic.
23 #[inline]
24 pub fn inline(val: ValueRef, inline: InlineAttr) {
25     use self::InlineAttr::*;
26     match inline {
27         Hint   => Attribute::InlineHint.apply_llfn(Function, val),
28         Always => Attribute::AlwaysInline.apply_llfn(Function, val),
29         Never  => Attribute::NoInline.apply_llfn(Function, val),
30         None   => {
31             Attribute::InlineHint.unapply_llfn(Function, val);
32             Attribute::AlwaysInline.unapply_llfn(Function, val);
33             Attribute::NoInline.unapply_llfn(Function, val);
34         },
35     };
36 }
37
38 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
39 #[inline]
40 pub fn emit_uwtable(val: ValueRef, emit: bool) {
41     Attribute::UWTable.toggle_llfn(Function, val, emit);
42 }
43
44 /// Tell LLVM whether the function can or cannot unwind.
45 #[inline]
46 pub fn unwind(val: ValueRef, can_unwind: bool) {
47     Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
48 }
49
50 /// Tell LLVM whether it should optimise function for size.
51 #[inline]
52 #[allow(dead_code)] // possibly useful function
53 pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
54     Attribute::OptimizeForSize.toggle_llfn(Function, val, optimize);
55 }
56
57 /// Tell LLVM if this function should be 'naked', i.e. skip the epilogue and prologue.
58 #[inline]
59 pub fn naked(val: ValueRef, is_naked: bool) {
60     Attribute::Naked.toggle_llfn(Function, val, is_naked);
61 }
62
63 pub fn set_frame_pointer_elimination(ccx: &CrateContext, llfn: ValueRef) {
64     // FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
65     // parameter.
66     if ccx.sess().must_not_eliminate_frame_pointers() {
67         llvm::AddFunctionAttrStringValue(
68             llfn, llvm::AttributePlace::Function,
69             cstr("no-frame-pointer-elim\0"), cstr("true\0"));
70     }
71 }
72
73 pub fn set_probestack(ccx: &CrateContext, llfn: ValueRef) {
74     // Only use stack probes if the target specification indicates that we
75     // should be using stack probes
76     if !ccx.sess().target.target.options.stack_probes {
77         return
78     }
79
80     // Currently stack probes seem somewhat incompatible with the address
81     // sanitizer. With asan we're already protected from stack overflow anyway
82     // so we don't really need stack probes regardless.
83     match ccx.sess().opts.debugging_opts.sanitizer {
84         Some(Sanitizer::Address) => return,
85         _ => {}
86     }
87
88     // Flag our internal `__rust_probestack` function as the stack probe symbol.
89     // This is defined in the `compiler-builtins` crate for each architecture.
90     llvm::AddFunctionAttrStringValue(
91         llfn, llvm::AttributePlace::Function,
92         cstr("probe-stack\0"), cstr("__rust_probestack\0"));
93 }
94
95 /// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
96 /// attributes.
97 pub fn from_fn_attrs(ccx: &CrateContext, attrs: &[ast::Attribute], llfn: ValueRef) {
98     use syntax::attr::*;
99     inline(llfn, find_inline_attr(Some(ccx.sess().diagnostic()), attrs));
100
101     set_frame_pointer_elimination(ccx, llfn);
102     set_probestack(ccx, llfn);
103     let mut target_features = vec![];
104     for attr in attrs {
105         if attr.check_name("target_feature") {
106             if let Some(val) = attr.value_str() {
107                 for feat in val.as_str().split(",").map(|f| f.trim()) {
108                     if !feat.is_empty() && !feat.contains('\0') {
109                         target_features.push(feat.to_string());
110                     }
111                 }
112             }
113         } else 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         }
123     }
124     if !target_features.is_empty() {
125         let val = CString::new(target_features.join(",")).unwrap();
126         llvm::AddFunctionAttrStringValue(
127             llfn, llvm::AttributePlace::Function,
128             cstr("target-features\0"), &val);
129     }
130 }
131
132 fn cstr(s: &'static str) -> &CStr {
133     CStr::from_bytes_with_nul(s.as_bytes()).expect("null-terminated string")
134 }