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.
14 use crate::abi::{FnAbi, FnAbiLlvmExt};
15 use crate::attributes;
16 use crate::context::CodegenCx;
18 use crate::llvm::AttributePlace::Function;
19 use crate::type_::Type;
20 use crate::value::Value;
21 use rustc_middle::ty::Ty;
22 use smallvec::SmallVec;
25 /// Declare a function.
27 /// If there’s a value with the same name already declared, the function will
28 /// update the declaration and return existing Value instead.
29 fn declare_raw_fn<'ll>(
30 cx: &CodegenCx<'ll, '_>,
32 callconv: llvm::CallConv,
33 unnamed: llvm::UnnamedAddr,
36 debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
38 llvm::LLVMRustGetOrInsertFunction(cx.llmod, name.as_ptr().cast(), name.len(), ty)
41 llvm::SetFunctionCallConv(llfn, callconv);
42 llvm::SetUnnamedAddress(llfn, unnamed);
44 let mut attrs = SmallVec::<[_; 4]>::new();
46 if cx.tcx.sess.opts.cg.no_redzone.unwrap_or(cx.tcx.sess.target.disable_redzone) {
47 attrs.push(llvm::AttributeKind::NoRedZone.create_attr(cx.llcx));
50 attrs.extend(attributes::non_lazy_bind_attr(cx));
52 attributes::apply_to_llfn(llfn, Function, &attrs);
57 impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
58 /// Declare a global value.
60 /// If there’s a value with the same name already declared, the function will
61 /// return its Value instead.
62 pub fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
63 debug!("declare_global(name={:?})", name);
64 unsafe { llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty) }
67 /// Declare a C ABI function.
69 /// Only use this for foreign function ABIs and glue. For Rust functions use
70 /// `declare_fn` instead.
72 /// If there’s a value with the same name already declared, the function will
73 /// update the declaration and return existing Value instead.
77 unnamed: llvm::UnnamedAddr,
80 declare_raw_fn(self, name, llvm::CCallConv, unnamed, fn_type)
83 /// Declare a Rust function.
85 /// If there’s a value with the same name already declared, the function will
86 /// update the declaration and return existing Value instead.
87 pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Value {
88 debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
90 // Function addresses in Rust are never significant, allowing functions to
92 let llfn = declare_raw_fn(
96 llvm::UnnamedAddr::Global,
97 fn_abi.llvm_type(self),
99 fn_abi.apply_attrs_llfn(self, llfn);
103 /// Declare a global with an intention to define it.
105 /// Use this function when you intend to define a global. This function will
106 /// return `None` if the name already has a definition associated with it. In that
107 /// case an error should be reported to the user, because it usually happens due
108 /// to user’s fault (e.g., misuse of `#[no_mangle]` or `#[export_name]` attributes).
109 pub fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
110 if self.get_defined_value(name).is_some() {
113 Some(self.declare_global(name, ty))
117 /// Declare a private global
119 /// Use this function when you intend to define a global without a name.
120 pub fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
121 unsafe { llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty) }
124 /// Gets declared value by name.
125 pub fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
126 debug!("get_declared_value(name={:?})", name);
127 unsafe { llvm::LLVMRustGetNamedValue(self.llmod, name.as_ptr().cast(), name.len()) }
130 /// Gets defined or externally defined (AvailableExternally linkage) value by
132 pub fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
133 self.get_declared_value(name).and_then(|val| {
134 let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
135 if !declaration { Some(val) } else { None }