]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/attributes.rs
Remove the in-tree `flate` crate
[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 llvm::{self, Attribute, ValueRef};
15 use llvm::AttributePlace::Function;
16 pub use syntax::attr::InlineAttr;
17 use syntax::ast;
18 use context::CrateContext;
19
20
21 /// Mark LLVM function to use provided inline heuristic.
22 #[inline]
23 pub fn inline(val: ValueRef, inline: InlineAttr) {
24     use self::InlineAttr::*;
25     match inline {
26         Hint   => Attribute::InlineHint.apply_llfn(Function, val),
27         Always => Attribute::AlwaysInline.apply_llfn(Function, val),
28         Never  => Attribute::NoInline.apply_llfn(Function, val),
29         None   => {
30             Attribute::InlineHint.unapply_llfn(Function, val);
31             Attribute::AlwaysInline.unapply_llfn(Function, val);
32             Attribute::NoInline.unapply_llfn(Function, val);
33         },
34     };
35 }
36
37 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
38 #[inline]
39 pub fn emit_uwtable(val: ValueRef, emit: bool) {
40     Attribute::UWTable.toggle_llfn(Function, val, emit);
41 }
42
43 /// Tell LLVM whether the function can or cannot unwind.
44 #[inline]
45 pub fn unwind(val: ValueRef, can_unwind: bool) {
46     Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
47 }
48
49 /// Tell LLVM whether it should optimise function for size.
50 #[inline]
51 #[allow(dead_code)] // possibly useful function
52 pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
53     Attribute::OptimizeForSize.toggle_llfn(Function, val, optimize);
54 }
55
56 /// Tell LLVM if this function should be 'naked', i.e. skip the epilogue and prologue.
57 #[inline]
58 pub fn naked(val: ValueRef, is_naked: bool) {
59     Attribute::Naked.toggle_llfn(Function, val, is_naked);
60 }
61
62 pub fn set_frame_pointer_elimination(ccx: &CrateContext, llfn: ValueRef) {
63     // FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
64     // parameter.
65     if ccx.sess().must_not_eliminate_frame_pointers() {
66         llvm::AddFunctionAttrStringValue(
67             llfn, llvm::AttributePlace::Function,
68             cstr("no-frame-pointer-elim\0"), cstr("true\0"));
69     }
70 }
71
72 /// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
73 /// attributes.
74 pub fn from_fn_attrs(ccx: &CrateContext, attrs: &[ast::Attribute], llfn: ValueRef) {
75     use syntax::attr::*;
76     inline(llfn, find_inline_attr(Some(ccx.sess().diagnostic()), attrs));
77
78     set_frame_pointer_elimination(ccx, llfn);
79     let mut target_features = vec![];
80     for attr in attrs {
81         if attr.check_name("target_feature") {
82             if let Some(val) = attr.value_str() {
83                 for feat in val.as_str().split(",").map(|f| f.trim()) {
84                     if !feat.is_empty() && !feat.contains('\0') {
85                         target_features.push(feat.to_string());
86                     }
87                 }
88             }
89         } else if attr.check_name("cold") {
90             Attribute::Cold.apply_llfn(Function, llfn);
91         } else if attr.check_name("naked") {
92             naked(llfn, true);
93         } else if attr.check_name("allocator") {
94             Attribute::NoAlias.apply_llfn(
95                 llvm::AttributePlace::ReturnValue(), llfn);
96         } else if attr.check_name("unwind") {
97             unwind(llfn, true);
98         }
99     }
100     if !target_features.is_empty() {
101         let val = CString::new(target_features.join(",")).unwrap();
102         llvm::AddFunctionAttrStringValue(
103             llfn, llvm::AttributePlace::Function,
104             cstr("target-features\0"), &val);
105     }
106 }
107
108 fn cstr(s: &'static str) -> &CStr {
109     CStr::from_bytes_with_nul(s.as_bytes()).expect("null-terminated string")
110 }