1 //! Declare various LLVM values.
3 //! Prefer using functions and methods from this module rather than calling LLVM
4 //! functions directly. These functions do some additional work to ensure we do
5 //! the right thing given the preconceptions of codegen.
7 //! Some useful guidelines:
9 //! * Use declare_* family of methods if you are declaring, but are not
10 //! interested in defining the Value they return.
11 //! * Use define_* family of methods when you might be defining the Value.
12 //! * When in doubt, define.
15 use crate::llvm::AttributePlace::Function;
16 use crate::abi::{FnAbi, FnAbiLlvmExt};
17 use crate::attributes;
18 use crate::context::CodegenCx;
19 use crate::type_::Type;
20 use crate::value::Value;
21 use rustc::ty::{self, PolyFnSig};
22 use rustc::ty::layout::{FnAbiExt, LayoutOf};
23 use rustc::session::config::Sanitizer;
24 use rustc_data_structures::small_c_str::SmallCStr;
25 use rustc_codegen_ssa::traits::*;
27 /// Declare a function.
29 /// If there’s a value with the same name already declared, the function will
30 /// update the declaration and return existing Value instead.
32 cx: &CodegenCx<'ll, '_>,
34 callconv: llvm::CallConv,
37 debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
38 let namebuf = SmallCStr::new(name);
40 llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty)
43 llvm::SetFunctionCallConv(llfn, callconv);
44 // Function addresses in Rust are never significant, allowing functions to
46 llvm::SetUnnamedAddr(llfn, true);
48 if cx.tcx.sess.opts.cg.no_redzone
49 .unwrap_or(cx.tcx.sess.target.target.options.disable_redzone) {
50 llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
53 if let Some(ref sanitizer) = cx.tcx.sess.opts.debugging_opts.sanitizer {
55 Sanitizer::Address => {
56 llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
58 Sanitizer::Memory => {
59 llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
61 Sanitizer::Thread => {
62 llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
68 attributes::default_optimisation_attrs(cx.tcx.sess, llfn);
69 attributes::non_lazy_bind(cx.sess(), llfn);
73 impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
77 name: &str, ty: &'ll Type
79 debug!("declare_global(name={:?})", name);
80 let namebuf = SmallCStr::new(name);
82 llvm::LLVMRustGetOrInsertGlobal(self.llmod, namebuf.as_ptr(), ty)
91 declare_raw_fn(self, name, llvm::CCallConv, fn_type)
99 debug!("declare_rust_fn(name={:?}, sig={:?})", name, sig);
100 let sig = self.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
101 debug!("declare_rust_fn (after region erasure) sig={:?}", sig);
103 let fn_abi = FnAbi::new(self, sig, &[]);
104 let llfn = declare_raw_fn(self, name, fn_abi.llvm_cconv(), fn_abi.llvm_type(self));
106 if self.layout_of(sig.output()).abi.is_uninhabited() {
107 llvm::Attribute::NoReturn.apply_llfn(Function, llfn);
110 fn_abi.apply_attrs_llfn(self, llfn);
119 ) -> Option<&'ll Value> {
120 if self.get_defined_value(name).is_some() {
123 Some(self.declare_global(name, ty))
127 fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
129 llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty)
136 fn_sig: PolyFnSig<'tcx>,
138 if self.get_defined_value(name).is_some() {
139 self.sess().fatal(&format!("symbol `{}` already defined", name))
141 self.declare_fn(name, fn_sig)
145 fn define_internal_fn(
148 fn_sig: PolyFnSig<'tcx>,
150 let llfn = self.define_fn(name, fn_sig);
151 unsafe { llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::InternalLinkage) };
155 fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
156 debug!("get_declared_value(name={:?})", name);
157 let namebuf = SmallCStr::new(name);
158 unsafe { llvm::LLVMRustGetNamedValue(self.llmod, namebuf.as_ptr()) }
161 fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
162 self.get_declared_value(name).and_then(|val|{
163 let declaration = unsafe {
164 llvm::LLVMIsDeclaration(val) != 0