]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/declare.rs
Unignore u128 test for stage 0,1
[rust.git] / src / librustc_trans / declare.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 //! Declare various LLVM values.
11 //!
12 //! Prefer using functions and methods from this module rather than calling LLVM
13 //! functions directly. These functions do some additional work to ensure we do
14 //! the right thing given the preconceptions of trans.
15 //!
16 //! Some useful guidelines:
17 //!
18 //! * Use declare_* family of methods if you are declaring, but are not
19 //!   interested in defining the ValueRef they return.
20 //! * Use define_* family of methods when you might be defining the ValueRef.
21 //! * When in doubt, define.
22
23 use llvm::{self, ValueRef};
24 use llvm::AttributePlace::Function;
25 use rustc::ty;
26 use abi::{Abi, FnType};
27 use attributes;
28 use context::CrateContext;
29 use common;
30 use type_::Type;
31 use value::Value;
32 use syntax::attr;
33
34 use std::ffi::CString;
35
36
37 /// Declare a global value.
38 ///
39 /// If there’s a value with the same name already declared, the function will
40 /// return its ValueRef instead.
41 pub fn declare_global(ccx: &CrateContext, name: &str, ty: Type) -> llvm::ValueRef {
42     debug!("declare_global(name={:?})", name);
43     let namebuf = CString::new(name).unwrap_or_else(|_|{
44         bug!("name {:?} contains an interior null byte", name)
45     });
46     unsafe {
47         llvm::LLVMRustGetOrInsertGlobal(ccx.llmod(), namebuf.as_ptr(), ty.to_ref())
48     }
49 }
50
51
52 /// Declare a function.
53 ///
54 /// If there’s a value with the same name already declared, the function will
55 /// update the declaration and return existing ValueRef instead.
56 fn declare_raw_fn(ccx: &CrateContext, name: &str, callconv: llvm::CallConv, ty: Type) -> ValueRef {
57     debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
58     let namebuf = CString::new(name).unwrap_or_else(|_|{
59         bug!("name {:?} contains an interior null byte", name)
60     });
61     let llfn = unsafe {
62         llvm::LLVMRustGetOrInsertFunction(ccx.llmod(), namebuf.as_ptr(), ty.to_ref())
63     };
64
65     llvm::SetFunctionCallConv(llfn, callconv);
66     // Function addresses in Rust are never significant, allowing functions to
67     // be merged.
68     llvm::SetUnnamedAddr(llfn, true);
69
70     if ccx.tcx().sess.opts.cg.no_redzone
71         .unwrap_or(ccx.tcx().sess.target.target.options.disable_redzone) {
72         llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
73     }
74
75     // If we're compiling the compiler-builtins crate, e.g. the equivalent of
76     // compiler-rt, then we want to implicitly compile everything with hidden
77     // visibility as we're going to link this object all over the place but
78     // don't want the symbols to get exported.
79     if attr::contains_name(ccx.tcx().hir.krate_attrs(), "compiler_builtins") {
80         unsafe {
81             llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
82         }
83     }
84
85     match ccx.tcx().sess.opts.cg.opt_level.as_ref().map(String::as_ref) {
86         Some("s") => {
87             llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
88         },
89         Some("z") => {
90             llvm::Attribute::MinSize.apply_llfn(Function, llfn);
91             llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
92         },
93         _ => {},
94     }
95
96     llfn
97 }
98
99
100 /// Declare a C ABI function.
101 ///
102 /// Only use this for foreign function ABIs and glue. For Rust functions use
103 /// `declare_fn` instead.
104 ///
105 /// If there’s a value with the same name already declared, the function will
106 /// update the declaration and return existing ValueRef instead.
107 pub fn declare_cfn(ccx: &CrateContext, name: &str, fn_type: Type) -> ValueRef {
108     declare_raw_fn(ccx, name, llvm::CCallConv, fn_type)
109 }
110
111
112 /// Declare a Rust function.
113 ///
114 /// If there’s a value with the same name already declared, the function will
115 /// update the declaration and return existing ValueRef instead.
116 pub fn declare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, name: &str,
117                             fn_type: ty::Ty<'tcx>) -> ValueRef {
118     debug!("declare_rust_fn(name={:?}, fn_type={:?})", name, fn_type);
119     let ty::BareFnTy { abi, ref sig, .. } = *common::ty_fn_ty(ccx, fn_type);
120     let sig = ccx.tcx().erase_late_bound_regions_and_normalize(sig);
121     debug!("declare_rust_fn (after region erasure) sig={:?}", sig);
122
123     let fty = FnType::new(ccx, abi, &sig, &[]);
124     let llfn = declare_raw_fn(ccx, name, fty.cconv, fty.llvm_type(ccx));
125
126     // FIXME(canndrew): This is_never should really be an is_uninhabited
127     if sig.output().is_never() {
128         llvm::Attribute::NoReturn.apply_llfn(Function, llfn);
129     }
130
131     if abi != Abi::Rust && abi != Abi::RustCall {
132         attributes::unwind(llfn, false);
133     }
134
135     fty.apply_attrs_llfn(llfn);
136
137     llfn
138 }
139
140
141 /// Declare a global with an intention to define it.
142 ///
143 /// Use this function when you intend to define a global. This function will
144 /// return None if the name already has a definition associated with it. In that
145 /// case an error should be reported to the user, because it usually happens due
146 /// to user’s fault (e.g. misuse of #[no_mangle] or #[export_name] attributes).
147 pub fn define_global(ccx: &CrateContext, name: &str, ty: Type) -> Option<ValueRef> {
148     if get_defined_value(ccx, name).is_some() {
149         None
150     } else {
151         Some(declare_global(ccx, name, ty))
152     }
153 }
154
155 /// Declare a Rust function with an intention to define it.
156 ///
157 /// Use this function when you intend to define a function. This function will
158 /// return panic if the name already has a definition associated with it. This
159 /// can happen with #[no_mangle] or #[export_name], for example.
160 pub fn define_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
161                            name: &str,
162                            fn_type: ty::Ty<'tcx>) -> ValueRef {
163     if get_defined_value(ccx, name).is_some() {
164         ccx.sess().fatal(&format!("symbol `{}` already defined", name))
165     } else {
166         declare_fn(ccx, name, fn_type)
167     }
168 }
169
170 /// Declare a Rust function with an intention to define it.
171 ///
172 /// Use this function when you intend to define a function. This function will
173 /// return panic if the name already has a definition associated with it. This
174 /// can happen with #[no_mangle] or #[export_name], for example.
175 pub fn define_internal_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
176                                     name: &str,
177                                     fn_type: ty::Ty<'tcx>) -> ValueRef {
178     let llfn = define_fn(ccx, name, fn_type);
179     unsafe { llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::InternalLinkage) };
180     llfn
181 }
182
183
184 /// Get declared value by name.
185 pub fn get_declared_value(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
186     debug!("get_declared_value(name={:?})", name);
187     let namebuf = CString::new(name).unwrap_or_else(|_|{
188         bug!("name {:?} contains an interior null byte", name)
189     });
190     let val = unsafe { llvm::LLVMRustGetNamedValue(ccx.llmod(), namebuf.as_ptr()) };
191     if val.is_null() {
192         debug!("get_declared_value: {:?} value is null", name);
193         None
194     } else {
195         debug!("get_declared_value: {:?} => {:?}", name, Value(val));
196         Some(val)
197     }
198 }
199
200 /// Get defined or externally defined (AvailableExternally linkage) value by
201 /// name.
202 pub fn get_defined_value(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
203     get_declared_value(ccx, name).and_then(|val|{
204         let declaration = unsafe {
205             llvm::LLVMIsDeclaration(val) != 0
206         };
207         if !declaration {
208             Some(val)
209         } else {
210             None
211         }
212     })
213 }