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 //! Declare various LLVM values.
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 codegen.
16 //! Some useful guidelines:
18 //! * Use declare_* family of methods if you are declaring, but are not
19 //! interested in defining the Value they return.
20 //! * Use define_* family of methods when you might be defining the Value.
21 //! * When in doubt, define.
24 use llvm::AttributePlace::Function;
25 use rustc::ty::{self, PolyFnSig};
26 use rustc::ty::layout::LayoutOf;
27 use rustc::session::config::Sanitizer;
28 use rustc_data_structures::small_c_str::SmallCStr;
29 use rustc_target::spec::PanicStrategy;
30 use abi::{Abi, FnType, FnTypeExt};
32 use context::CodegenCx;
34 use rustc_codegen_ssa::traits::*;
37 /// Declare a function.
39 /// If there’s a value with the same name already declared, the function will
40 /// update the declaration and return existing Value instead.
42 cx: &CodegenCx<'ll, '_>,
44 callconv: llvm::CallConv,
47 debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
48 let namebuf = SmallCStr::new(name);
50 llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty)
53 llvm::SetFunctionCallConv(llfn, callconv);
54 // Function addresses in Rust are never significant, allowing functions to
56 llvm::SetUnnamedAddr(llfn, true);
58 if cx.tcx.sess.opts.cg.no_redzone
59 .unwrap_or(cx.tcx.sess.target.target.options.disable_redzone) {
60 llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
63 if let Some(ref sanitizer) = cx.tcx.sess.opts.debugging_opts.sanitizer {
65 Sanitizer::Address => {
66 llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
68 Sanitizer::Memory => {
69 llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
71 Sanitizer::Thread => {
72 llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
78 match cx.tcx.sess.opts.cg.opt_level.as_ref().map(String::as_ref) {
80 llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
83 llvm::Attribute::MinSize.apply_llfn(Function, llfn);
84 llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
89 if cx.tcx.sess.panic_strategy() != PanicStrategy::Unwind {
90 attributes::unwind(llfn, false);
93 attributes::non_lazy_bind(cx.sess(), llfn);
98 impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
102 name: &str, ty: &'ll Type
104 debug!("declare_global(name={:?})", name);
105 let namebuf = SmallCStr::new(name);
107 llvm::LLVMRustGetOrInsertGlobal(self.llmod, namebuf.as_ptr(), ty)
116 declare_raw_fn(self, name, llvm::CCallConv, fn_type)
122 sig: PolyFnSig<'tcx>,
124 debug!("declare_rust_fn(name={:?}, sig={:?})", name, sig);
125 let sig = self.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
126 debug!("declare_rust_fn (after region erasure) sig={:?}", sig);
128 let fty = FnType::new(self, sig, &[]);
129 let llfn = declare_raw_fn(self, name, fty.llvm_cconv(), fty.llvm_type(self));
131 if self.layout_of(sig.output()).abi.is_uninhabited() {
132 llvm::Attribute::NoReturn.apply_llfn(Function, llfn);
135 if sig.abi != Abi::Rust && sig.abi != Abi::RustCall {
136 attributes::unwind(llfn, false);
139 fty.apply_attrs_llfn(llfn);
148 ) -> Option<&'ll Value> {
149 if self.get_defined_value(name).is_some() {
152 Some(self.declare_global(name, ty))
156 fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
158 llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty)
165 fn_sig: PolyFnSig<'tcx>,
167 if self.get_defined_value(name).is_some() {
168 self.sess().fatal(&format!("symbol `{}` already defined", name))
170 self.declare_fn(name, fn_sig)
174 fn define_internal_fn(
177 fn_sig: PolyFnSig<'tcx>,
179 let llfn = self.define_fn(name, fn_sig);
180 unsafe { llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::InternalLinkage) };
184 fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
185 debug!("get_declared_value(name={:?})", name);
186 let namebuf = SmallCStr::new(name);
187 unsafe { llvm::LLVMRustGetNamedValue(self.llmod, namebuf.as_ptr()) }
190 fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
191 self.get_declared_value(name).and_then(|val|{
192 let declaration = unsafe {
193 llvm::LLVMIsDeclaration(val) != 0