2015-03-04 01:08:06 +02:00
|
|
|
|
//! Declare various LLVM values.
|
|
|
|
|
//!
|
2015-05-11 14:58:58 -07: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 16:10:16 +03:00
|
|
|
|
//! the right thing given the preconceptions of codegen.
|
2015-03-04 01:08:06 +02:00
|
|
|
|
//!
|
|
|
|
|
//! Some useful guidelines:
|
|
|
|
|
//!
|
2015-05-11 14:58:58 -07:00
|
|
|
|
//! * Use declare_* family of methods if you are declaring, but are not
|
2018-07-10 13:28:39 +03:00
|
|
|
|
//! interested in defining the Value they return.
|
|
|
|
|
//! * Use define_* family of methods when you might be defining the Value.
|
2015-03-04 01:08:06 +02:00
|
|
|
|
//! * When in doubt, define.
|
2016-11-11 08:21:22 -08:00
|
|
|
|
|
2019-02-18 03:58:58 +09:00
|
|
|
|
use crate::llvm;
|
|
|
|
|
use crate::llvm::AttributePlace::Function;
|
2019-10-29 16:35:26 +02:00
|
|
|
|
use crate::abi::{FnAbi, FnTypeLlvmExt};
|
2019-02-18 03:58:58 +09:00
|
|
|
|
use crate::attributes;
|
|
|
|
|
use crate::context::CodegenCx;
|
|
|
|
|
use crate::type_::Type;
|
|
|
|
|
use crate::value::Value;
|
2019-05-14 15:20:29 +05:30
|
|
|
|
use rustc::ty::{self, PolyFnSig};
|
2019-10-29 19:17:16 +02:00
|
|
|
|
use rustc::ty::layout::{FnAbiExt, LayoutOf};
|
2019-01-19 00:37:52 +02:00
|
|
|
|
use rustc::session::config::Sanitizer;
|
2018-08-07 16:04:34 +02:00
|
|
|
|
use rustc_data_structures::small_c_str::SmallCStr;
|
2018-11-16 13:45:28 +02:00
|
|
|
|
use rustc_codegen_ssa::traits::*;
|
2015-03-04 01:08:06 +02:00
|
|
|
|
|
|
|
|
|
/// Declare a function.
|
|
|
|
|
///
|
2015-05-11 14:58:58 -07:00
|
|
|
|
/// If there’s a value with the same name already declared, the function will
|
2018-07-10 13:28:39 +03:00
|
|
|
|
/// update the declaration and return existing Value instead.
|
2018-08-22 17:48:32 +02:00
|
|
|
|
fn declare_raw_fn(
|
|
|
|
|
cx: &CodegenCx<'ll, '_>,
|
2018-07-17 18:26:58 +03:00
|
|
|
|
name: &str,
|
|
|
|
|
callconv: llvm::CallConv,
|
|
|
|
|
ty: &'ll Type,
|
2018-08-22 17:48:32 +02:00
|
|
|
|
) -> &'ll Value {
|
2016-02-23 21:46:08 +02:00
|
|
|
|
debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
|
2018-08-07 16:04:34 +02:00
|
|
|
|
let namebuf = SmallCStr::new(name);
|
2015-03-04 01:08:06 +02:00
|
|
|
|
let llfn = unsafe {
|
2018-07-02 17:52:53 +03:00
|
|
|
|
llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty)
|
2015-03-04 01:08:06 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
llvm::SetFunctionCallConv(llfn, callconv);
|
2015-05-11 14:58:58 -07:00
|
|
|
|
// Function addresses in Rust are never significant, allowing functions to
|
|
|
|
|
// be merged.
|
2015-03-04 01:08:06 +02:00
|
|
|
|
llvm::SetUnnamedAddr(llfn, true);
|
|
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
|
if cx.tcx.sess.opts.cg.no_redzone
|
|
|
|
|
.unwrap_or(cx.tcx.sess.target.target.options.disable_redzone) {
|
2016-08-03 00:25:19 +03:00
|
|
|
|
llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
|
2015-03-04 01:08:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
|
if let Some(ref sanitizer) = cx.tcx.sess.opts.debugging_opts.sanitizer {
|
2016-12-29 23:28:11 -05:00
|
|
|
|
match *sanitizer {
|
|
|
|
|
Sanitizer::Address => {
|
|
|
|
|
llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
|
|
|
|
|
},
|
|
|
|
|
Sanitizer::Memory => {
|
|
|
|
|
llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
|
|
|
|
|
},
|
|
|
|
|
Sanitizer::Thread => {
|
|
|
|
|
llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-19 00:37:52 +02:00
|
|
|
|
attributes::default_optimisation_attrs(cx.tcx.sess, llfn);
|
2018-09-26 19:19:55 +03:00
|
|
|
|
attributes::non_lazy_bind(cx.sess(), llfn);
|
2018-08-22 17:48:32 +02:00
|
|
|
|
llfn
|
2015-03-04 01:08:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
|
|
|
|
|
|
|
|
|
fn declare_global(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str, ty: &'ll Type
|
|
|
|
|
) -> &'ll Value {
|
|
|
|
|
debug!("declare_global(name={:?})", name);
|
|
|
|
|
let namebuf = SmallCStr::new(name);
|
|
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMRustGetOrInsertGlobal(self.llmod, namebuf.as_ptr(), ty)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-04 01:08:06 +02:00
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
fn declare_cfn(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
fn_type: &'ll Type
|
|
|
|
|
) -> &'ll Value {
|
|
|
|
|
declare_raw_fn(self, name, llvm::CCallConv, fn_type)
|
|
|
|
|
}
|
2015-03-04 01:08:06 +02:00
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
fn declare_fn(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
sig: PolyFnSig<'tcx>,
|
|
|
|
|
) -> &'ll Value {
|
|
|
|
|
debug!("declare_rust_fn(name={:?}, sig={:?})", name, sig);
|
|
|
|
|
let sig = self.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
|
|
|
|
|
debug!("declare_rust_fn (after region erasure) sig={:?}", sig);
|
|
|
|
|
|
2019-10-29 16:35:26 +02:00
|
|
|
|
let fn_abi = FnAbi::new(self, sig, &[]);
|
|
|
|
|
let llfn = declare_raw_fn(self, name, fn_abi.llvm_cconv(), fn_abi.llvm_type(self));
|
2018-09-20 15:47:22 +02:00
|
|
|
|
|
2018-09-24 10:51:13 +02:00
|
|
|
|
if self.layout_of(sig.output()).abi.is_uninhabited() {
|
2018-09-20 15:47:22 +02:00
|
|
|
|
llvm::Attribute::NoReturn.apply_llfn(Function, llfn);
|
|
|
|
|
}
|
2016-02-23 21:57:22 +02:00
|
|
|
|
|
2019-10-29 16:35:26 +02:00
|
|
|
|
fn_abi.apply_attrs_llfn(self, llfn);
|
2016-02-23 21:57:22 +02:00
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
llfn
|
2016-02-26 01:10:40 +02:00
|
|
|
|
}
|
2016-02-25 12:11:02 +02:00
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
fn define_global(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
ty: &'ll Type
|
|
|
|
|
) -> Option<&'ll Value> {
|
|
|
|
|
if self.get_defined_value(name).is_some() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(self.declare_global(name, ty))
|
|
|
|
|
}
|
2015-03-04 01:08:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
|
|
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty)
|
|
|
|
|
}
|
2018-05-23 15:19:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
fn define_fn(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
fn_sig: PolyFnSig<'tcx>,
|
|
|
|
|
) -> &'ll Value {
|
|
|
|
|
if self.get_defined_value(name).is_some() {
|
|
|
|
|
self.sess().fatal(&format!("symbol `{}` already defined", name))
|
|
|
|
|
} else {
|
|
|
|
|
self.declare_fn(name, fn_sig)
|
|
|
|
|
}
|
2015-03-04 01:08:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
fn define_internal_fn(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
fn_sig: PolyFnSig<'tcx>,
|
|
|
|
|
) -> &'ll Value {
|
|
|
|
|
let llfn = self.define_fn(name, fn_sig);
|
|
|
|
|
unsafe { llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::InternalLinkage) };
|
|
|
|
|
llfn
|
|
|
|
|
}
|
2016-05-09 23:56:49 -04:00
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
|
|
|
|
|
debug!("get_declared_value(name={:?})", name);
|
|
|
|
|
let namebuf = SmallCStr::new(name);
|
|
|
|
|
unsafe { llvm::LLVMRustGetNamedValue(self.llmod, namebuf.as_ptr()) }
|
|
|
|
|
}
|
2016-04-05 13:01:00 +03:00
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
|
|
|
|
|
self.get_declared_value(name).and_then(|val|{
|
|
|
|
|
let declaration = unsafe {
|
|
|
|
|
llvm::LLVMIsDeclaration(val) != 0
|
|
|
|
|
};
|
|
|
|
|
if !declaration {
|
|
|
|
|
Some(val)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2015-03-04 01:08:06 +02:00
|
|
|
|
}
|