2015-03-04 01:08:06 +02:00
|
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
//! 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
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
|
use llvm;
|
2016-08-03 00:25:19 +03:00
|
|
|
|
use llvm::AttributePlace::Function;
|
2018-10-06 22:58:36 +09:00
|
|
|
|
use rustc::ty::{self, PolyFnSig};
|
2018-08-23 16:34:38 +02:00
|
|
|
|
use rustc::ty::layout::LayoutOf;
|
2016-12-29 23:28:11 -05:00
|
|
|
|
use rustc::session::config::Sanitizer;
|
2018-08-07 16:04:34 +02:00
|
|
|
|
use rustc_data_structures::small_c_str::SmallCStr;
|
2017-12-08 21:18:21 +02:00
|
|
|
|
use rustc_target::spec::PanicStrategy;
|
2018-04-25 16:45:29 +03:00
|
|
|
|
use abi::{Abi, FnType, FnTypeExt};
|
2016-03-22 19:23:36 +02:00
|
|
|
|
use attributes;
|
2018-01-05 07:01:54 +02:00
|
|
|
|
use context::CodegenCx;
|
2016-03-22 19:23:36 +02:00
|
|
|
|
use type_::Type;
|
2018-08-07 17:14:40 +02:00
|
|
|
|
use value::{Value, ValueTrait};
|
2015-03-04 01:08:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Declare a global value.
|
|
|
|
|
///
|
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
|
|
|
|
/// return its Value instead.
|
|
|
|
|
pub fn declare_global(cx: &CodegenCx<'ll, '_>, name: &str, ty: &'ll Type) -> &'ll Value {
|
2015-03-04 01:08:06 +02:00
|
|
|
|
debug!("declare_global(name={:?})", name);
|
2018-08-07 16:04:34 +02:00
|
|
|
|
let namebuf = SmallCStr::new(name);
|
2015-03-04 01:08:06 +02:00
|
|
|
|
unsafe {
|
2018-07-02 17:52:53 +03:00
|
|
|
|
llvm::LLVMRustGetOrInsertGlobal(cx.llmod, namebuf.as_ptr(), ty)
|
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-07 17:14:40 +02:00
|
|
|
|
fn declare_raw_fn<Value : ?Sized>(
|
|
|
|
|
cx: &CodegenCx<'ll, '_, &'ll Value>,
|
2018-07-17 18:26:58 +03:00
|
|
|
|
name: &str,
|
|
|
|
|
callconv: llvm::CallConv,
|
|
|
|
|
ty: &'ll Type,
|
2018-08-07 17:14:40 +02:00
|
|
|
|
) -> &'ll Value where Value : ValueTrait {
|
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);
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
|
match cx.tcx.sess.opts.cg.opt_level.as_ref().map(String::as_ref) {
|
2016-03-27 12:42:47 -07:00
|
|
|
|
Some("s") => {
|
2016-08-03 00:25:19 +03:00
|
|
|
|
llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
|
2016-03-27 12:42:47 -07:00
|
|
|
|
},
|
|
|
|
|
Some("z") => {
|
2016-08-03 00:25:19 +03:00
|
|
|
|
llvm::Attribute::MinSize.apply_llfn(Function, llfn);
|
|
|
|
|
llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
|
2016-03-27 12:42:47 -07:00
|
|
|
|
},
|
|
|
|
|
_ => {},
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
|
if cx.tcx.sess.panic_strategy() != PanicStrategy::Unwind {
|
2017-10-04 14:54:28 -07:00
|
|
|
|
attributes::unwind(llfn, false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 19:19:55 +03:00
|
|
|
|
attributes::non_lazy_bind(cx.sess(), llfn);
|
|
|
|
|
|
2018-08-07 17:14:40 +02:00
|
|
|
|
Value::of_llvm(llfn)
|
2015-03-04 01:08:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Declare a C ABI function.
|
|
|
|
|
///
|
2015-05-11 14:58:58 -07:00
|
|
|
|
/// Only use this for foreign function ABIs and glue. For Rust functions use
|
2016-02-23 21:57:22 +02:00
|
|
|
|
/// `declare_fn` instead.
|
2015-03-04 01:08:06 +02:00
|
|
|
|
///
|
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-07 17:14:40 +02:00
|
|
|
|
pub fn declare_cfn<Value : ?Sized>(
|
|
|
|
|
cx: &CodegenCx<'ll, '_, &'ll Value>,
|
|
|
|
|
name: &str,
|
|
|
|
|
fn_type: &'ll Type
|
|
|
|
|
) -> &'ll Value where Value : ValueTrait {
|
2018-01-05 07:04:08 +02:00
|
|
|
|
declare_raw_fn(cx, name, llvm::CCallConv, fn_type)
|
2015-03-04 01:08:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Declare a Rust 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.
|
|
|
|
|
pub fn declare_fn(
|
|
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
|
name: &str,
|
2018-10-06 22:58:36 +09:00
|
|
|
|
sig: PolyFnSig<'tcx>,
|
2018-07-10 13:28:39 +03:00
|
|
|
|
) -> &'ll Value {
|
2018-10-06 22:58:36 +09:00
|
|
|
|
debug!("declare_rust_fn(name={:?}, sig={:?})", name, sig);
|
2018-03-03 08:23:28 -05:00
|
|
|
|
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
|
2015-06-18 20:25:05 +03:00
|
|
|
|
debug!("declare_rust_fn (after region erasure) sig={:?}", sig);
|
2016-02-23 21:57:22 +02:00
|
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
|
let fty = FnType::new(cx, sig, &[]);
|
2018-04-25 16:45:29 +03:00
|
|
|
|
let llfn = declare_raw_fn(cx, name, fty.llvm_cconv(), fty.llvm_type(cx));
|
2015-03-04 01:08:06 +02:00
|
|
|
|
|
2018-08-23 16:34:38 +02:00
|
|
|
|
if cx.layout_of(sig.output()).abi.is_uninhabited() {
|
2016-08-03 00:25:19 +03:00
|
|
|
|
llvm::Attribute::NoReturn.apply_llfn(Function, llfn);
|
2016-02-23 21:57:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 10:51:06 +02:00
|
|
|
|
if sig.abi != Abi::Rust && sig.abi != Abi::RustCall {
|
2016-02-25 12:11:02 +02:00
|
|
|
|
attributes::unwind(llfn, false);
|
2016-02-26 01:10:40 +02:00
|
|
|
|
}
|
2016-02-25 12:11:02 +02:00
|
|
|
|
|
2016-02-26 01:10:40 +02:00
|
|
|
|
fty.apply_attrs_llfn(llfn);
|
2015-03-04 01:08:06 +02:00
|
|
|
|
|
|
|
|
|
llfn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Declare a global with an intention to define it.
|
|
|
|
|
///
|
2015-05-11 14:58:58 -07:00
|
|
|
|
/// 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).
|
2018-07-10 13:28:39 +03:00
|
|
|
|
pub fn define_global(cx: &CodegenCx<'ll, '_>, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
|
2018-01-05 07:04:08 +02:00
|
|
|
|
if get_defined_value(cx, name).is_some() {
|
2015-03-04 01:08:06 +02:00
|
|
|
|
None
|
|
|
|
|
} else {
|
2018-01-05 07:04:08 +02:00
|
|
|
|
Some(declare_global(cx, name, ty))
|
2015-03-04 01:08:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 15:19:07 -04:00
|
|
|
|
/// Declare a private global
|
|
|
|
|
///
|
|
|
|
|
/// Use this function when you intend to define a global without a name.
|
2018-08-07 17:14:40 +02:00
|
|
|
|
pub fn define_private_global(cx: &CodegenCx<'ll, '_, &'ll Value>, ty: &'ll Type) -> &'ll Value {
|
2018-05-23 15:19:07 -04:00
|
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMRustInsertPrivateGlobal(cx.llmod, ty)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-04 01:08:06 +02:00
|
|
|
|
/// Declare a Rust function with an intention to define it.
|
|
|
|
|
///
|
2015-05-11 14:58:58 -07:00
|
|
|
|
/// Use this function when you intend to define a function. This function will
|
2015-07-20 13:27:38 -07:00
|
|
|
|
/// return panic if the name already has a definition associated with it. This
|
|
|
|
|
/// can happen with #[no_mangle] or #[export_name], for example.
|
2018-07-10 13:28:39 +03:00
|
|
|
|
pub fn define_fn(
|
|
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
|
name: &str,
|
2018-10-06 22:58:36 +09:00
|
|
|
|
fn_sig: PolyFnSig<'tcx>,
|
2018-07-10 13:28:39 +03:00
|
|
|
|
) -> &'ll Value {
|
2018-01-05 07:04:08 +02:00
|
|
|
|
if get_defined_value(cx, name).is_some() {
|
|
|
|
|
cx.sess().fatal(&format!("symbol `{}` already defined", name))
|
2015-03-04 01:08:06 +02:00
|
|
|
|
} else {
|
2018-10-06 22:58:36 +09:00
|
|
|
|
declare_fn(cx, name, fn_sig)
|
2015-03-04 01:08:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 23:56:49 -04:00
|
|
|
|
/// Declare a Rust function with an intention to define it.
|
|
|
|
|
///
|
|
|
|
|
/// Use this function when you intend to define a function. This function will
|
|
|
|
|
/// return panic if the name already has a definition associated with it. This
|
|
|
|
|
/// can happen with #[no_mangle] or #[export_name], for example.
|
2018-07-10 13:28:39 +03:00
|
|
|
|
pub fn define_internal_fn(
|
|
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
|
name: &str,
|
2018-10-06 22:58:36 +09:00
|
|
|
|
fn_sig: PolyFnSig<'tcx>,
|
2018-07-10 13:28:39 +03:00
|
|
|
|
) -> &'ll Value {
|
2018-10-06 22:58:36 +09:00
|
|
|
|
let llfn = define_fn(cx, name, fn_sig);
|
2016-09-01 13:52:33 -05:00
|
|
|
|
unsafe { llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::InternalLinkage) };
|
2016-05-09 23:56:49 -04:00
|
|
|
|
llfn
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-04 01:08:06 +02:00
|
|
|
|
|
2016-04-05 13:01:00 +03:00
|
|
|
|
/// Get declared value by name.
|
2018-07-10 13:28:39 +03:00
|
|
|
|
pub fn get_declared_value(cx: &CodegenCx<'ll, '_>, name: &str) -> Option<&'ll Value> {
|
2016-04-05 13:01:00 +03:00
|
|
|
|
debug!("get_declared_value(name={:?})", name);
|
2018-08-07 16:04:34 +02:00
|
|
|
|
let namebuf = SmallCStr::new(name);
|
2018-07-10 13:28:39 +03:00
|
|
|
|
unsafe { llvm::LLVMRustGetNamedValue(cx.llmod, namebuf.as_ptr()) }
|
2016-04-05 13:01:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get defined or externally defined (AvailableExternally linkage) value by
|
|
|
|
|
/// name.
|
2018-07-10 13:28:39 +03:00
|
|
|
|
pub fn get_defined_value(cx: &CodegenCx<'ll, '_>, name: &str) -> Option<&'ll Value> {
|
2018-01-05 07:04:08 +02:00
|
|
|
|
get_declared_value(cx, name).and_then(|val|{
|
2016-04-01 15:24:56 +02:00
|
|
|
|
let declaration = unsafe {
|
|
|
|
|
llvm::LLVMIsDeclaration(val) != 0
|
2015-03-04 01:08:06 +02:00
|
|
|
|
};
|
2016-04-01 15:24:56 +02:00
|
|
|
|
if !declaration {
|
2015-03-04 01:08:06 +02:00
|
|
|
|
Some(val)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
2016-04-05 13:01:00 +03:00
|
|
|
|
})
|
2015-03-04 01:08:06 +02:00
|
|
|
|
}
|