2015-03-03 23:08:06 +00:00
|
|
|
|
//! Declare various LLVM values.
|
|
|
|
|
//!
|
2015-05-11 21:58:58 +00:00
|
|
|
|
//! Prefer using functions and methods from this module rather than calling LLVM
|
|
|
|
|
//! functions directly. These functions do some additional work to ensure we do
|
2018-05-08 13:10:16 +00:00
|
|
|
|
//! the right thing given the preconceptions of codegen.
|
2015-03-03 23:08:06 +00:00
|
|
|
|
//!
|
|
|
|
|
//! Some useful guidelines:
|
|
|
|
|
//!
|
2015-05-11 21:58:58 +00:00
|
|
|
|
//! * Use declare_* family of methods if you are declaring, but are not
|
2018-07-10 10:28:39 +00:00
|
|
|
|
//! interested in defining the Value they return.
|
|
|
|
|
//! * Use define_* family of methods when you might be defining the Value.
|
2015-03-03 23:08:06 +00:00
|
|
|
|
//! * When in doubt, define.
|
2016-11-11 16:21:22 +00:00
|
|
|
|
|
2019-10-29 17:30:31 +00:00
|
|
|
|
use crate::abi::{FnAbi, FnAbiLlvmExt};
|
2019-02-17 18:58:58 +00:00
|
|
|
|
use crate::attributes;
|
|
|
|
|
use crate::context::CodegenCx;
|
|
|
|
|
use crate::llvm;
|
|
|
|
|
use crate::llvm::AttributePlace::Function;
|
|
|
|
|
use crate::type_::Type;
|
|
|
|
|
use crate::value::Value;
|
2022-04-01 05:50:41 +00:00
|
|
|
|
use rustc_codegen_ssa::traits::TypeMembershipMethods;
|
2020-03-29 15:19:48 +00:00
|
|
|
|
use rustc_middle::ty::Ty;
|
2022-04-01 05:50:41 +00:00
|
|
|
|
use rustc_symbol_mangling::typeid::typeid_for_fnabi;
|
2022-02-21 16:19:16 +00:00
|
|
|
|
use smallvec::SmallVec;
|
2015-03-03 23:08:06 +00:00
|
|
|
|
|
|
|
|
|
/// Declare a function.
|
|
|
|
|
///
|
2015-05-11 21:58:58 +00:00
|
|
|
|
/// If there’s a value with the same name already declared, the function will
|
2018-07-10 10:28:39 +00:00
|
|
|
|
/// update the declaration and return existing Value instead.
|
2021-12-14 18:49:49 +00:00
|
|
|
|
fn declare_raw_fn<'ll>(
|
2018-08-22 15:48:32 +00:00
|
|
|
|
cx: &CodegenCx<'ll, '_>,
|
2018-07-17 15:26:58 +00:00
|
|
|
|
name: &str,
|
|
|
|
|
callconv: llvm::CallConv,
|
2021-01-23 22:19:49 +00:00
|
|
|
|
unnamed: llvm::UnnamedAddr,
|
2022-09-28 17:42:30 +00:00
|
|
|
|
visibility: llvm::Visibility,
|
2018-07-17 15:26:58 +00:00
|
|
|
|
ty: &'ll Type,
|
2018-08-22 15:48:32 +00:00
|
|
|
|
) -> &'ll Value {
|
2016-02-23 19:46:08 +00:00
|
|
|
|
debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
|
2020-03-10 00:00:00 +00:00
|
|
|
|
let llfn = unsafe {
|
|
|
|
|
llvm::LLVMRustGetOrInsertFunction(cx.llmod, name.as_ptr().cast(), name.len(), ty)
|
|
|
|
|
};
|
2015-03-03 23:08:06 +00:00
|
|
|
|
|
|
|
|
|
llvm::SetFunctionCallConv(llfn, callconv);
|
2021-01-23 22:19:49 +00:00
|
|
|
|
llvm::SetUnnamedAddress(llfn, unnamed);
|
2022-09-28 21:43:58 +00:00
|
|
|
|
llvm::set_visibility(llfn, visibility);
|
2015-03-03 23:08:06 +00:00
|
|
|
|
|
2022-02-21 19:47:56 +00:00
|
|
|
|
let mut attrs = SmallVec::<[_; 4]>::new();
|
2022-02-21 16:19:16 +00:00
|
|
|
|
|
2020-11-08 11:27:51 +00:00
|
|
|
|
if cx.tcx.sess.opts.cg.no_redzone.unwrap_or(cx.tcx.sess.target.disable_redzone) {
|
2022-02-21 19:47:56 +00:00
|
|
|
|
attrs.push(llvm::AttributeKind::NoRedZone.create_attr(cx.llcx));
|
2015-03-03 23:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-21 19:47:56 +00:00
|
|
|
|
attrs.extend(attributes::non_lazy_bind_attr(cx));
|
2022-02-21 16:19:16 +00:00
|
|
|
|
|
2022-02-21 19:47:56 +00:00
|
|
|
|
attributes::apply_to_llfn(llfn, Function, &attrs);
|
2021-07-13 11:14:26 +00:00
|
|
|
|
|
2018-08-22 15:48:32 +00:00
|
|
|
|
llfn
|
2015-03-03 23:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 18:49:49 +00:00
|
|
|
|
impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
|
2020-09-18 11:06:53 +00:00
|
|
|
|
/// Declare a global value.
|
|
|
|
|
///
|
|
|
|
|
/// If there’s a value with the same name already declared, the function will
|
|
|
|
|
/// return its Value instead.
|
|
|
|
|
pub fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
|
2018-09-20 13:47:22 +00:00
|
|
|
|
debug!("declare_global(name={:?})", name);
|
2019-12-04 20:00:28 +00:00
|
|
|
|
unsafe { llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty) }
|
2018-09-20 13:47:22 +00:00
|
|
|
|
}
|
2015-03-03 23:08:06 +00:00
|
|
|
|
|
2020-09-18 11:06:53 +00:00
|
|
|
|
/// Declare a C ABI function.
|
|
|
|
|
///
|
|
|
|
|
/// Only use this for foreign function ABIs and glue. For Rust functions use
|
|
|
|
|
/// `declare_fn` instead.
|
|
|
|
|
///
|
|
|
|
|
/// If there’s a value with the same name already declared, the function will
|
|
|
|
|
/// update the declaration and return existing Value instead.
|
2021-01-23 22:19:49 +00:00
|
|
|
|
pub fn declare_cfn(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
unnamed: llvm::UnnamedAddr,
|
|
|
|
|
fn_type: &'ll Type,
|
|
|
|
|
) -> &'ll Value {
|
2022-09-28 21:50:58 +00:00
|
|
|
|
// Declare C ABI functions with the visibility used by C by default.
|
|
|
|
|
let visibility = if self.tcx.sess.target.default_hidden_visibility {
|
|
|
|
|
llvm::Visibility::Hidden
|
|
|
|
|
} else {
|
|
|
|
|
llvm::Visibility::Default
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-28 17:42:30 +00:00
|
|
|
|
declare_raw_fn(self, name, llvm::CCallConv, unnamed, visibility, fn_type)
|
2018-09-20 13:47:22 +00:00
|
|
|
|
}
|
2015-03-03 23:08:06 +00:00
|
|
|
|
|
2022-11-05 09:06:38 +00:00
|
|
|
|
/// Declare an entry Function
|
|
|
|
|
///
|
|
|
|
|
/// The ABI of this function can change depending on the target (although for now the same as
|
|
|
|
|
/// `declare_cfn`)
|
|
|
|
|
///
|
|
|
|
|
/// If there’s a value with the same name already declared, the function will
|
|
|
|
|
/// update the declaration and return existing Value instead.
|
|
|
|
|
pub fn declare_entry_fn(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
callconv: llvm::CallConv,
|
|
|
|
|
unnamed: llvm::UnnamedAddr,
|
|
|
|
|
fn_type: &'ll Type,
|
|
|
|
|
) -> &'ll Value {
|
|
|
|
|
let visibility = if self.tcx.sess.target.default_hidden_visibility {
|
|
|
|
|
llvm::Visibility::Hidden
|
|
|
|
|
} else {
|
|
|
|
|
llvm::Visibility::Default
|
|
|
|
|
};
|
|
|
|
|
declare_raw_fn(self, name, callconv, unnamed, visibility, fn_type)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 11:06:53 +00:00
|
|
|
|
/// Declare a Rust function.
|
|
|
|
|
///
|
|
|
|
|
/// If there’s a value with the same name already declared, the function will
|
|
|
|
|
/// update the declaration and return existing Value instead.
|
|
|
|
|
pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Value {
|
2019-10-29 16:34:50 +00:00
|
|
|
|
debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
|
2018-09-20 13:47:22 +00:00
|
|
|
|
|
2021-01-23 22:19:49 +00:00
|
|
|
|
// Function addresses in Rust are never significant, allowing functions to
|
|
|
|
|
// be merged.
|
|
|
|
|
let llfn = declare_raw_fn(
|
|
|
|
|
self,
|
|
|
|
|
name,
|
|
|
|
|
fn_abi.llvm_cconv(),
|
|
|
|
|
llvm::UnnamedAddr::Global,
|
2022-09-28 17:42:30 +00:00
|
|
|
|
llvm::Visibility::Default,
|
2021-01-23 22:19:49 +00:00
|
|
|
|
fn_abi.llvm_type(self),
|
|
|
|
|
);
|
2019-10-29 14:35:26 +00:00
|
|
|
|
fn_abi.apply_attrs_llfn(self, llfn);
|
2022-04-01 05:50:41 +00:00
|
|
|
|
|
|
|
|
|
if self.tcx.sess.is_sanitizer_cfi_enabled() {
|
|
|
|
|
let typeid = typeid_for_fnabi(self.tcx, fn_abi);
|
|
|
|
|
self.set_type_metadata(llfn, typeid);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 13:47:22 +00:00
|
|
|
|
llfn
|
2016-02-25 23:10:40 +00:00
|
|
|
|
}
|
2016-02-25 10:11:02 +00:00
|
|
|
|
|
2020-09-18 11:06:53 +00:00
|
|
|
|
/// Declare a global with an intention to define it.
|
|
|
|
|
///
|
|
|
|
|
/// Use this function when you intend to define a global. This function will
|
|
|
|
|
/// return `None` if the name already has a definition associated with it. In that
|
|
|
|
|
/// case an error should be reported to the user, because it usually happens due
|
|
|
|
|
/// to user’s fault (e.g., misuse of `#[no_mangle]` or `#[export_name]` attributes).
|
|
|
|
|
pub fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
|
2018-09-20 13:47:22 +00:00
|
|
|
|
if self.get_defined_value(name).is_some() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(self.declare_global(name, ty))
|
|
|
|
|
}
|
2015-03-03 23:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 11:06:53 +00:00
|
|
|
|
/// Declare a private global
|
|
|
|
|
///
|
|
|
|
|
/// Use this function when you intend to define a global without a name.
|
|
|
|
|
pub fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
|
2018-09-20 13:47:22 +00:00
|
|
|
|
unsafe { llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty) }
|
2018-05-23 19:19:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 11:06:53 +00:00
|
|
|
|
/// Gets declared value by name.
|
|
|
|
|
pub fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
|
2018-09-20 13:47:22 +00:00
|
|
|
|
debug!("get_declared_value(name={:?})", name);
|
2020-03-10 00:00:00 +00:00
|
|
|
|
unsafe { llvm::LLVMRustGetNamedValue(self.llmod, name.as_ptr().cast(), name.len()) }
|
2018-09-20 13:47:22 +00:00
|
|
|
|
}
|
2016-04-05 10:01:00 +00:00
|
|
|
|
|
2020-09-18 11:06:53 +00:00
|
|
|
|
/// Gets defined or externally defined (AvailableExternally linkage) value by
|
|
|
|
|
/// name.
|
|
|
|
|
pub fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
|
2018-09-20 13:47:22 +00:00
|
|
|
|
self.get_declared_value(name).and_then(|val| {
|
|
|
|
|
let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
|
|
|
|
|
if !declaration { Some(val) } else { None }
|
|
|
|
|
})
|
|
|
|
|
}
|
2015-03-03 23:08:06 +00:00
|
|
|
|
}
|