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::*;
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);
81 llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty)
90 declare_raw_fn(self, name, llvm::CCallConv, fn_type)
96 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
98 debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
100 let llfn = declare_raw_fn(self, name, fn_abi.llvm_cconv(), fn_abi.llvm_type(self));
101 fn_abi.apply_attrs_llfn(self, llfn);
109 ) -> Option<&'ll Value> {
110 if self.get_defined_value(name).is_some() {
113 Some(self.declare_global(name, ty))
117 fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
119 llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty)
123 fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
124 debug!("get_declared_value(name={:?})", name);
125 let namebuf = SmallCStr::new(name);
126 unsafe { llvm::LLVMRustGetNamedValue(self.llmod, namebuf.as_ptr()) }
129 fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
130 self.get_declared_value(name).and_then(|val|{
131 let declaration = unsafe {
132 llvm::LLVMIsDeclaration(val) != 0