rust/src/librustc_trans/consts.rs

323 lines
12 KiB
Rust
Raw Normal View History

// Copyright 2012 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.
use llvm;
2016-08-02 16:25:19 -05:00
use llvm::{SetUnnamedAddr};
use llvm::{ValueRef, True};
use rustc::hir::def_id::DefId;
2016-03-29 00:50:44 -05:00
use rustc::hir::map as hir_map;
use debuginfo;
2016-12-15 11:11:49 -06:00
use base;
use monomorphize::MonoItem;
use common::{CodegenCx, val_ty};
use declare;
use monomorphize::Instance;
use type_::Type;
use type_of::LayoutLlvmExt;
use rustc::ty;
use rustc::ty::layout::{Align, LayoutOf};
2016-03-29 00:50:44 -05:00
use rustc::hir;
2015-07-31 02:04:06 -05:00
use std::ffi::{CStr, CString};
use syntax::ast;
2016-08-22 22:54:53 -05:00
use syntax::attr;
pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
unsafe {
llvm::LLVMConstPointerCast(val, ty.to_ref())
}
}
pub fn bitcast(val: ValueRef, ty: Type) -> ValueRef {
unsafe {
llvm::LLVMConstBitCast(val, ty.to_ref())
}
}
2018-01-04 23:04:08 -06:00
fn set_global_alignment(cx: &CodegenCx,
gv: ValueRef,
mut align: Align) {
// The target may require greater alignment for globals than the type does.
// Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
// which can force it to be smaller. Rust doesn't support this yet.
2018-01-04 23:04:08 -06:00
if let Some(min) = cx.sess().target.target.options.min_global_align {
match ty::layout::Align::from_bits(min, min) {
Ok(min) => align = align.max(min),
Err(err) => {
2018-01-04 23:04:08 -06:00
cx.sess().err(&format!("invalid minimum global alignment: {}", err));
}
}
}
unsafe {
llvm::LLVMSetAlignment(gv, align.abi() as u32);
}
}
2018-01-04 23:04:08 -06:00
pub fn addr_of_mut(cx: &CodegenCx,
cv: ValueRef,
align: Align,
kind: &str)
-> ValueRef {
2013-03-07 14:34:39 -06:00
unsafe {
2018-01-04 23:04:08 -06:00
let name = cx.generate_local_symbol_name(kind);
let gv = declare::define_global(cx, &name[..], val_ty(cv)).unwrap_or_else(||{
bug!("symbol `{}` is already defined", name);
});
2013-03-07 14:34:39 -06:00
llvm::LLVMSetInitializer(gv, cv);
2018-01-04 23:04:08 -06:00
set_global_alignment(cx, gv, align);
llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
SetUnnamedAddr(gv, true);
2013-03-07 14:34:39 -06:00
gv
}
}
2018-01-04 23:04:08 -06:00
pub fn addr_of(cx: &CodegenCx,
cv: ValueRef,
align: Align,
kind: &str)
-> ValueRef {
2018-01-04 23:04:08 -06:00
if let Some(&gv) = cx.const_globals.borrow().get(&cv) {
unsafe {
// Upgrade the alignment in cases where the same constant is used with different
// alignment requirements
let llalign = align.abi() as u32;
if llalign > llvm::LLVMGetAlignment(gv) {
llvm::LLVMSetAlignment(gv, llalign);
}
}
return gv;
}
2018-01-04 23:04:08 -06:00
let gv = addr_of_mut(cx, cv, align, kind);
unsafe {
llvm::LLVMSetGlobalConstant(gv, True);
}
2018-01-04 23:04:08 -06:00
cx.const_globals.borrow_mut().insert(cv, gv);
gv
}
2018-01-04 23:04:08 -06:00
pub fn get_static(cx: &CodegenCx, def_id: DefId) -> ValueRef {
let instance = Instance::mono(cx.tcx, def_id);
if let Some(&g) = cx.instances.borrow().get(&instance) {
return g;
}
let defined_in_current_codegen_unit = cx.codegen_unit
.items()
.contains_key(&MonoItem::Static(def_id));
assert!(!defined_in_current_codegen_unit,
"consts::get_static() should always hit the cache for \
statics defined in the same CGU, but did not for `{:?}`",
def_id);
2018-01-04 23:04:08 -06:00
let ty = instance.ty(cx.tcx);
let sym = cx.tcx.symbol_name(instance);
2018-01-04 23:04:08 -06:00
let g = if let Some(id) = cx.tcx.hir.as_local_node_id(def_id) {
2018-01-04 23:04:08 -06:00
let llty = cx.layout_of(ty).llvm_type(cx);
let (g, attrs) = match cx.tcx.hir.get(id) {
hir_map::NodeItem(&hir::Item {
ref attrs, span, node: hir::ItemStatic(..), ..
}) => {
2018-01-04 23:04:08 -06:00
if declare::get_declared_value(cx, &sym[..]).is_some() {
span_bug!(span, "trans: Conflicting symbol names for static?");
}
2018-01-04 23:04:08 -06:00
let g = declare::define_global(cx, &sym[..], llty).unwrap();
if !cx.tcx.is_reachable_non_generic(def_id) {
unsafe {
llvm::LLVMRustSetVisibility(g, llvm::Visibility::Hidden);
}
}
(g, attrs)
}
2016-03-06 06:17:53 -06:00
hir_map::NodeForeignItem(&hir::ForeignItem {
ref attrs, span, node: hir::ForeignItemStatic(..), ..
2016-03-06 06:17:53 -06:00
}) => {
let g = if let Some(linkage) = cx.tcx.trans_fn_attrs(def_id).linkage {
2016-03-06 06:17:53 -06:00
// If this is a static with a linkage specified, then we need to handle
// it a little specially. The typesystem prevents things like &T and
// extern "C" fn() from being non-null, so we can't just declare a
// static and call it a day. Some linkages (like weak) will make it such
// that the static actually has a null value.
let llty2 = match ty.sty {
2018-01-04 23:04:08 -06:00
ty::TyRawPtr(ref mt) => cx.layout_of(mt.ty).llvm_type(cx),
2016-03-06 06:17:53 -06:00
_ => {
2018-01-04 23:04:08 -06:00
cx.sess().span_fatal(span, "must have type `*const T` or `*mut T`");
2016-03-06 06:17:53 -06:00
}
};
unsafe {
// Declare a symbol `foo` with the desired linkage.
2018-01-04 23:04:08 -06:00
let g1 = declare::declare_global(cx, &sym, llty2);
llvm::LLVMRustSetLinkage(g1, base::linkage_to_llvm(linkage));
2016-03-06 06:17:53 -06:00
// Declare an internal global `extern_with_linkage_foo` which
// is initialized with the address of `foo`. If `foo` is
// discarded during linking (for example, if `foo` has weak
// linkage and there are no definitions), then
// `extern_with_linkage_foo` will instead be initialized to
// zero.
let mut real_name = "_rust_extern_with_linkage_".to_string();
real_name.push_str(&sym);
2018-01-04 23:04:08 -06:00
let g2 = declare::define_global(cx, &real_name, llty).unwrap_or_else(||{
cx.sess().span_fatal(span,
&format!("symbol `{}` is already defined", &sym))
2016-03-06 06:17:53 -06:00
});
llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage);
2016-03-06 06:17:53 -06:00
llvm::LLVMSetInitializer(g2, g1);
g2
}
} else {
// Generate an external declaration.
2018-01-04 23:04:08 -06:00
declare::declare_global(cx, &sym, llty)
2016-03-06 06:17:53 -06:00
};
(g, attrs)
2016-03-06 06:17:53 -06:00
}
item => bug!("get_static: expected static, found {:?}", item)
};
for attr in attrs {
if attr.check_name("thread_local") {
2018-01-04 23:04:08 -06:00
llvm::set_thread_local_mode(g, cx.tls_model);
}
}
g
} else {
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
// FIXME(nagisa): investigate whether it can be changed into define_global
2018-01-04 23:04:08 -06:00
let g = declare::declare_global(cx, &sym, cx.layout_of(ty).llvm_type(cx));
// Thread-local statics in some other crate need to *always* be linked
// against in a thread-local fashion, so we need to be sure to apply the
// thread-local attribute locally if it was present remotely. If we
// don't do this then linker errors can be generated where the linker
// complains that one object files has a thread local version of the
// symbol and another one doesn't.
2018-01-04 23:04:08 -06:00
for attr in cx.tcx.get_attrs(def_id).iter() {
if attr.check_name("thread_local") {
2018-01-04 23:04:08 -06:00
llvm::set_thread_local_mode(g, cx.tls_model);
}
}
2018-01-04 23:04:08 -06:00
if cx.use_dll_storage_attrs && !cx.tcx.is_foreign_item(def_id) {
// This item is external but not foreign, i.e. it originates from an external Rust
// crate. Since we don't know whether this crate will be linked dynamically or
// statically in the final application, we always mark such symbols as 'dllimport'.
// If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs to
// make things work.
//
// However, in some scenarios we defer emission of statics to downstream
// crates, so there are cases where a static with an upstream DefId
// is actually present in the current crate. We can find out via the
// is_translated_item query.
if !cx.tcx.is_translated_item(def_id) {
unsafe {
llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
}
2016-08-02 16:25:19 -05:00
}
}
g
};
2018-01-04 23:04:08 -06:00
if cx.use_dll_storage_attrs && cx.tcx.is_dllimport_foreign_item(def_id) {
// For foreign (native) libs we know the exact storage type to use.
unsafe {
llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
}
}
2018-01-04 23:04:08 -06:00
cx.instances.borrow_mut().insert(instance, g);
cx.statics.borrow_mut().insert(g, def_id);
g
}
2018-01-04 23:04:08 -06:00
pub fn trans_static<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
def_id: DefId,
is_mutable: bool,
2018-01-16 02:31:48 -06:00
attrs: &[ast::Attribute]) {
unsafe {
2018-01-04 23:04:08 -06:00
let g = get_static(cx, def_id);
2018-01-16 02:31:48 -06:00
let v = match ::mir::trans_static_initializer(cx, def_id) {
Ok(v) => v,
// Error has already been reported
Err(_) => return,
};
// boolean SSA values are i1, but they have to be stored in i8 slots,
// otherwise some LLVM optimization passes don't work as expected
let mut val_llty = val_ty(v);
2018-01-04 23:04:08 -06:00
let v = if val_llty == Type::i1(cx) {
val_llty = Type::i8(cx);
llvm::LLVMConstZExt(v, val_llty.to_ref())
} else {
v
};
2018-01-04 23:04:08 -06:00
let instance = Instance::mono(cx.tcx, def_id);
let ty = instance.ty(cx.tcx);
let llty = cx.layout_of(ty).llvm_type(cx);
let g = if val_llty == llty {
g
} else {
// If we created the global with the wrong type,
// correct the type.
let empty_string = CString::new("").unwrap();
let name_str_ref = CStr::from_ptr(llvm::LLVMGetValueName(g));
let name_string = CString::new(name_str_ref.to_bytes()).unwrap();
llvm::LLVMSetValueName(g, empty_string.as_ptr());
let linkage = llvm::LLVMRustGetLinkage(g);
let visibility = llvm::LLVMRustGetVisibility(g);
let new_g = llvm::LLVMRustGetOrInsertGlobal(
2018-01-04 23:04:08 -06:00
cx.llmod, name_string.as_ptr(), val_llty.to_ref());
llvm::LLVMRustSetLinkage(new_g, linkage);
llvm::LLVMRustSetVisibility(new_g, visibility);
// To avoid breaking any invariants, we leave around the old
// global for the moment; we'll replace all references to it
// with the new global later. (See base::trans_crate.)
2018-01-04 23:04:08 -06:00
cx.statics_to_rauw.borrow_mut().push((g, new_g));
new_g
};
2018-01-04 23:04:08 -06:00
set_global_alignment(cx, g, cx.align_of(ty));
llvm::LLVMSetInitializer(g, v);
rustc: Add `const` globals to the language This change is an implementation of [RFC 69][rfc] which adds a third kind of global to the language, `const`. This global is most similar to what the old `static` was, and if you're unsure about what to use then you should use a `const`. The semantics of these three kinds of globals are: * A `const` does not represent a memory location, but only a value. Constants are translated as rvalues, which means that their values are directly inlined at usage location (similar to a #define in C/C++). Constant values are, well, constant, and can not be modified. Any "modification" is actually a modification to a local value on the stack rather than the actual constant itself. Almost all values are allowed inside constants, whether they have interior mutability or not. There are a few minor restrictions listed in the RFC, but they should in general not come up too often. * A `static` now always represents a memory location (unconditionally). Any references to the same `static` are actually a reference to the same memory location. Only values whose types ascribe to `Sync` are allowed in a `static`. This restriction is in place because many threads may access a `static` concurrently. Lifting this restriction (and allowing unsafe access) is a future extension not implemented at this time. * A `static mut` continues to always represent a memory location. All references to a `static mut` continue to be `unsafe`. This is a large breaking change, and many programs will need to be updated accordingly. A summary of the breaking changes is: * Statics may no longer be used in patterns. Statics now always represent a memory location, which can sometimes be modified. To fix code, repurpose the matched-on-`static` to a `const`. static FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } change this code to: const FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } * Statics may no longer refer to other statics by value. Due to statics being able to change at runtime, allowing them to reference one another could possibly lead to confusing semantics. If you are in this situation, use a constant initializer instead. Note, however, that statics may reference other statics by address, however. * Statics may no longer be used in constant expressions, such as array lengths. This is due to the same restrictions as listed above. Use a `const` instead. [breaking-change] [rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 10:17:01 -05:00
// As an optimization, all shared statics which do not have interior
// mutability are placed into read-only memory.
if !is_mutable {
2018-01-04 23:04:08 -06:00
if cx.type_is_freeze(ty) {
llvm::LLVMSetGlobalConstant(g, llvm::True);
rustc: Add `const` globals to the language This change is an implementation of [RFC 69][rfc] which adds a third kind of global to the language, `const`. This global is most similar to what the old `static` was, and if you're unsure about what to use then you should use a `const`. The semantics of these three kinds of globals are: * A `const` does not represent a memory location, but only a value. Constants are translated as rvalues, which means that their values are directly inlined at usage location (similar to a #define in C/C++). Constant values are, well, constant, and can not be modified. Any "modification" is actually a modification to a local value on the stack rather than the actual constant itself. Almost all values are allowed inside constants, whether they have interior mutability or not. There are a few minor restrictions listed in the RFC, but they should in general not come up too often. * A `static` now always represents a memory location (unconditionally). Any references to the same `static` are actually a reference to the same memory location. Only values whose types ascribe to `Sync` are allowed in a `static`. This restriction is in place because many threads may access a `static` concurrently. Lifting this restriction (and allowing unsafe access) is a future extension not implemented at this time. * A `static mut` continues to always represent a memory location. All references to a `static mut` continue to be `unsafe`. This is a large breaking change, and many programs will need to be updated accordingly. A summary of the breaking changes is: * Statics may no longer be used in patterns. Statics now always represent a memory location, which can sometimes be modified. To fix code, repurpose the matched-on-`static` to a `const`. static FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } change this code to: const FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } * Statics may no longer refer to other statics by value. Due to statics being able to change at runtime, allowing them to reference one another could possibly lead to confusing semantics. If you are in this situation, use a constant initializer instead. Note, however, that statics may reference other statics by address, however. * Statics may no longer be used in constant expressions, such as array lengths. This is due to the same restrictions as listed above. Use a `const` instead. [breaking-change] [rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 10:17:01 -05:00
}
2013-06-21 20:46:34 -05:00
}
debuginfo::create_global_var_metadata(cx, def_id, g);
if attr::contains_name(attrs, "thread_local") {
2018-01-04 23:04:08 -06:00
llvm::set_thread_local_mode(g, cx.tls_model);
}
2018-01-04 23:04:08 -06:00
base::set_link_section(cx, g, attrs);
add an #[used] attribute similar to GCC's __attribute((used))__. This attribute prevents LLVM from optimizing away a non-exported symbol, within a compilation unit (object file), when there are no references to it. This is better explained with an example: ``` #[used] static LIVE: i32 = 0; static REFERENCED: i32 = 0; static DEAD: i32 = 0; fn internal() {} pub fn exported() -> &'static i32 { &REFERENCED } ``` Without optimizations, LLVM pretty much preserves all the static variables and functions within the compilation unit. ``` $ rustc --crate-type=lib --emit=obj symbols.rs && nm -C symbols.o 0000000000000000 t drop::h1be0f8f27a2ba94a 0000000000000000 r symbols::REFERENCED::hb3bdfd46050bc84c 0000000000000000 r symbols::DEAD::hc2ea8f9bd06f380b 0000000000000000 r symbols::LIVE::h0970cf9889edb56e 0000000000000000 T symbols::exported::h6f096c2b1fc292b2 0000000000000000 t symbols::internal::h0ac1aadbc1e3a494 ``` With optimizations, LLVM will drop dead code. Here `internal` is dropped because it's not a exported function/symbol (i.e. not `pub`lic). `DEAD` is dropped for the same reason. `REFERENCED` is preserved, even though it's not exported, because it's referenced by the `exported` function. Finally, `LIVE` survives because of the `#[used]` attribute even though it's not exported or referenced. ``` $ rustc --crate-type=lib -C opt-level=3 --emit=obj symbols.rs && nm -C symbols.o 0000000000000000 r symbols::REFERENCED::hb3bdfd46050bc84c 0000000000000000 r symbols::LIVE::h0970cf9889edb56e 0000000000000000 T symbols::exported::h6f096c2b1fc292b2 ``` Note that the linker knows nothing about `#[used]` and will drop `LIVE` because no other object references to it. ``` $ echo 'fn main() {}' >> symbols.rs $ rustc symbols.rs && nm -C symbols | grep LIVE ``` At this time, `#[used]` only works on `static` variables.
2017-02-20 13:42:47 -06:00
if attr::contains_name(attrs, "used") {
2017-04-05 21:11:22 -05:00
// This static will be stored in the llvm.used variable which is an array of i8*
2018-01-04 23:04:08 -06:00
let cast = llvm::LLVMConstPointerCast(g, Type::i8p(cx).to_ref());
cx.used_statics.borrow_mut().push(cast);
add an #[used] attribute similar to GCC's __attribute((used))__. This attribute prevents LLVM from optimizing away a non-exported symbol, within a compilation unit (object file), when there are no references to it. This is better explained with an example: ``` #[used] static LIVE: i32 = 0; static REFERENCED: i32 = 0; static DEAD: i32 = 0; fn internal() {} pub fn exported() -> &'static i32 { &REFERENCED } ``` Without optimizations, LLVM pretty much preserves all the static variables and functions within the compilation unit. ``` $ rustc --crate-type=lib --emit=obj symbols.rs && nm -C symbols.o 0000000000000000 t drop::h1be0f8f27a2ba94a 0000000000000000 r symbols::REFERENCED::hb3bdfd46050bc84c 0000000000000000 r symbols::DEAD::hc2ea8f9bd06f380b 0000000000000000 r symbols::LIVE::h0970cf9889edb56e 0000000000000000 T symbols::exported::h6f096c2b1fc292b2 0000000000000000 t symbols::internal::h0ac1aadbc1e3a494 ``` With optimizations, LLVM will drop dead code. Here `internal` is dropped because it's not a exported function/symbol (i.e. not `pub`lic). `DEAD` is dropped for the same reason. `REFERENCED` is preserved, even though it's not exported, because it's referenced by the `exported` function. Finally, `LIVE` survives because of the `#[used]` attribute even though it's not exported or referenced. ``` $ rustc --crate-type=lib -C opt-level=3 --emit=obj symbols.rs && nm -C symbols.o 0000000000000000 r symbols::REFERENCED::hb3bdfd46050bc84c 0000000000000000 r symbols::LIVE::h0970cf9889edb56e 0000000000000000 T symbols::exported::h6f096c2b1fc292b2 ``` Note that the linker knows nothing about `#[used]` and will drop `LIVE` because no other object references to it. ``` $ echo 'fn main() {}' >> symbols.rs $ rustc symbols.rs && nm -C symbols | grep LIVE ``` At this time, `#[used]` only works on `static` variables.
2017-02-20 13:42:47 -06:00
}
}
}