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};
14 use llvm::{self, Attribute, ValueRef};
15 use llvm::AttributePlace::Function;
16 pub use syntax::attr::InlineAttr;
18 use context::CrateContext;
21 /// Mark LLVM function to use provided inline heuristic.
23 pub fn inline(val: ValueRef, inline: InlineAttr) {
24 use self::InlineAttr::*;
26 Hint => Attribute::InlineHint.apply_llfn(Function, val),
27 Always => Attribute::AlwaysInline.apply_llfn(Function, val),
28 Never => Attribute::NoInline.apply_llfn(Function, val),
30 Attribute::InlineHint.unapply_llfn(Function, val);
31 Attribute::AlwaysInline.unapply_llfn(Function, val);
32 Attribute::NoInline.unapply_llfn(Function, val);
37 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
39 pub fn emit_uwtable(val: ValueRef, emit: bool) {
40 Attribute::UWTable.toggle_llfn(Function, val, emit);
43 /// Tell LLVM whether the function can or cannot unwind.
45 pub fn unwind(val: ValueRef, can_unwind: bool) {
46 Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
49 /// Tell LLVM whether it should optimise function for size.
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);
56 /// Tell LLVM if this function should be 'naked', i.e. skip the epilogue and prologue.
58 pub fn naked(val: ValueRef, is_naked: bool) {
59 Attribute::Naked.toggle_llfn(Function, val, is_naked);
62 pub fn set_frame_pointer_elimination(ccx: &CrateContext, llfn: ValueRef) {
63 // FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
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"));
72 /// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
74 pub fn from_fn_attrs(ccx: &CrateContext, attrs: &[ast::Attribute], llfn: ValueRef) {
76 inline(llfn, find_inline_attr(Some(ccx.sess().diagnostic()), attrs));
78 set_frame_pointer_elimination(ccx, llfn);
79 let mut target_features = vec![];
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());
89 } else if attr.check_name("cold") {
90 Attribute::Cold.apply_llfn(Function, llfn);
91 } else if attr.check_name("naked") {
93 } else if attr.check_name("allocator") {
94 Attribute::NoAlias.apply_llfn(
95 llvm::AttributePlace::ReturnValue(), llfn);
96 } else if attr.check_name("unwind") {
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);
108 fn cstr(s: &'static str) -> &CStr {
109 CStr::from_bytes_with_nul(s.as_bytes()).expect("null-terminated string")