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;
22 use rustc::session::config::Sanitizer;
23 use rustc_data_structures::small_c_str::SmallCStr;
24 use rustc_codegen_ssa::traits::*;
26 /// Declare a function.
28 /// If there’s a value with the same name already declared, the function will
29 /// update the declaration and return existing Value instead.
31 cx: &CodegenCx<'ll, '_>,
33 callconv: llvm::CallConv,
36 debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
37 let namebuf = SmallCStr::new(name);
39 llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty)
42 llvm::SetFunctionCallConv(llfn, callconv);
43 // Function addresses in Rust are never significant, allowing functions to
45 llvm::SetUnnamedAddr(llfn, true);
47 if cx.tcx.sess.opts.cg.no_redzone
48 .unwrap_or(cx.tcx.sess.target.target.options.disable_redzone) {
49 llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
52 if let Some(ref sanitizer) = cx.tcx.sess.opts.debugging_opts.sanitizer {
54 Sanitizer::Address => {
55 llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
57 Sanitizer::Memory => {
58 llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
60 Sanitizer::Thread => {
61 llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
67 attributes::default_optimisation_attrs(cx.tcx.sess, llfn);
68 attributes::non_lazy_bind(cx.sess(), llfn);
72 impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
76 name: &str, ty: &'ll Type
78 debug!("declare_global(name={:?})", name);
80 llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty)
89 declare_raw_fn(self, name, llvm::CCallConv, fn_type)
95 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
97 debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
99 let llfn = declare_raw_fn(self, name, fn_abi.llvm_cconv(), fn_abi.llvm_type(self));
100 fn_abi.apply_attrs_llfn(self, llfn);
108 ) -> Option<&'ll Value> {
109 if self.get_defined_value(name).is_some() {
112 Some(self.declare_global(name, ty))
116 fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
118 llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty)
122 fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
123 debug!("get_declared_value(name={:?})", name);
124 let namebuf = SmallCStr::new(name);
125 unsafe { llvm::LLVMRustGetNamedValue(self.llmod, namebuf.as_ptr()) }
128 fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
129 self.get_declared_value(name).and_then(|val|{
130 let declaration = unsafe {
131 llvm::LLVMIsDeclaration(val) != 0