rust/src/librustc_trans/trans/context.rs

903 lines
32 KiB
Rust
Raw Normal View History

// Copyright 2013 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;
2014-09-05 11:18:53 -05:00
use llvm::{ContextRef, ModuleRef, ValueRef, BuilderRef};
use llvm::{TargetData};
use llvm::mk_target_data;
2013-06-12 21:02:33 -05:00
use metadata::common::LinkMeta;
use middle::resolve;
use middle::traits;
use trans::adt;
use trans::base;
use trans::builder::Builder;
use trans::common::{ExternMap,tydesc_info,BuilderRef_res};
use trans::debuginfo;
use trans::monomorphize::MonoId;
use trans::type_::{Type, TypeNames};
use middle::ty::{mod, Ty};
use session::config::NoDebugInfo;
use session::Session;
use util::ppaux::Repr;
use util::sha2::Sha256;
use util::nodemap::{NodeMap, NodeSet, DefIdMap, FnvHashMap, FnvHashSet};
use std::cell::{Cell, RefCell};
use std::c_str::ToCStr;
use std::ptr;
2014-04-21 19:03:02 -05:00
use std::rc::Rc;
use syntax::ast;
use syntax::parse::token::InternedString;
2014-04-21 19:25:56 -05:00
pub struct Stats {
pub n_static_tydescs: Cell<uint>,
pub n_glues_created: Cell<uint>,
pub n_null_glues: Cell<uint>,
pub n_real_glues: Cell<uint>,
pub n_fns: Cell<uint>,
pub n_monos: Cell<uint>,
pub n_inlines: Cell<uint>,
pub n_closures: Cell<uint>,
pub n_llvm_insns: Cell<uint>,
pub llvm_insns: RefCell<FnvHashMap<String, uint>>,
// (ident, llvm-instructions)
pub fn_stats: RefCell<Vec<(String, uint)> >,
2014-04-21 19:25:56 -05:00
}
/// The shared portion of a `CrateContext`. There is one `SharedCrateContext`
/// per crate. The data here is shared between all compilation units of the
/// crate, so it must not contain references to any LLVM data structures
/// (aside from metadata-related ones).
pub struct SharedCrateContext<'tcx> {
local_ccxs: Vec<LocalCrateContext<'tcx>>,
metadata_llmod: ModuleRef,
metadata_llcx: ContextRef,
exp_map2: resolve::ExportMap2,
reachable: NodeSet,
item_symbols: RefCell<NodeMap<String>>,
link_meta: LinkMeta,
symbol_hasher: RefCell<Sha256>,
tcx: ty::ctxt<'tcx>,
stats: Stats,
available_monomorphizations: RefCell<FnvHashSet<String>>,
available_drop_glues: RefCell<FnvHashMap<Ty<'tcx>, String>>,
}
/// The local portion of a `CrateContext`. There is one `LocalCrateContext`
/// per compilation unit. Each one has its own LLVM `ContextRef` so that
/// several compilation units may be optimized in parallel. All other LLVM
/// data structures in the `LocalCrateContext` are tied to that `ContextRef`.
pub struct LocalCrateContext<'tcx> {
2014-09-05 11:18:53 -05:00
llmod: ModuleRef,
llcx: ContextRef,
td: TargetData,
tn: TypeNames,
externs: RefCell<ExternMap>,
item_vals: RefCell<NodeMap<ValueRef>>,
drop_glues: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
tydescs: RefCell<FnvHashMap<Ty<'tcx>, Rc<tydesc_info<'tcx>>>>,
/// Set when running emit_tydescs to enforce that no more tydescs are
/// created.
2014-09-05 11:18:53 -05:00
finished_tydescs: Cell<bool>,
/// Track mapping of external ids to local items imported for inlining
2014-09-05 11:18:53 -05:00
external: RefCell<DefIdMap<Option<ast::NodeId>>>,
/// Backwards version of the `external` map (inlined items to where they
/// came from)
2014-09-05 11:18:53 -05:00
external_srcs: RefCell<NodeMap<ast::DefId>>,
/// Cache instances of monomorphized functions
monomorphized: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
2014-09-05 11:18:53 -05:00
monomorphizing: RefCell<DefIdMap<uint>>,
/// Cache generated vtables
vtables: RefCell<FnvHashMap<(Ty<'tcx>, Rc<ty::TraitRef<'tcx>>), ValueRef>>,
/// Cache of constant strings,
const_cstr_cache: RefCell<FnvHashMap<InternedString, ValueRef>>,
2013-06-12 21:02:33 -05:00
/// Reverse-direction for const ptrs cast from globals.
/// Key is an int, cast from a ValueRef holding a *T,
/// Val is a ValueRef holding a *[T].
///
/// Needed because LLVM loses pointer->pointee association
/// when we ptrcast, and we have to ptrcast during translation
/// of a [T] const because we form a slice, a [*T,int] pair, not
/// a pointer to an LLVM array type.
const_globals: RefCell<FnvHashMap<int, ValueRef>>,
2013-06-12 21:02:33 -05:00
/// Cache of emitted const values
2014-09-05 11:18:53 -05:00
const_values: RefCell<NodeMap<ValueRef>>,
2013-06-12 21:02:33 -05:00
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
/// Cache of emitted static values
static_values: RefCell<NodeMap<ValueRef>>,
/// Cache of external const values
2014-09-05 11:18:53 -05:00
extern_const_values: RefCell<DefIdMap<ValueRef>>,
2013-06-12 21:02:33 -05:00
impl_method_cache: RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>>,
2013-06-14 00:38:17 -05:00
/// Cache of closure wrappers for bare fn's.
closure_bare_wrapper_cache: RefCell<FnvHashMap<ValueRef, ValueRef>>,
2014-09-05 11:18:53 -05:00
lltypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
llsizingtypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
adt_reprs: RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>>,
type_hashcodes: RefCell<FnvHashMap<Ty<'tcx>, String>>,
all_llvm_symbols: RefCell<FnvHashSet<String>>,
2014-09-05 11:18:53 -05:00
int_type: Type,
opaque_vec_type: Type,
builder: BuilderRef_res,
/// Holds the LLVM values for closure IDs.
unboxed_closure_vals: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
dbg_cx: Option<debuginfo::CrateDebugContext<'tcx>>,
2014-04-09 18:56:31 -05:00
2014-09-05 11:18:53 -05:00
eh_personality: RefCell<Option<ValueRef>>,
rustc: Add official support for weak failure This commit is part of the ongoing libstd facade efforts (cc #13851). The compiler now recognizes some language items as "extern { fn foo(...); }" and will automatically perform the following actions: 1. The foreign function has a pre-defined name. 2. The crate and downstream crates can only be built as rlibs until a crate defines the lang item itself. 3. The actual lang item has a pre-defined name. This is essentially nicer compiler support for the hokey core-depends-on-std-failure scheme today, but it is implemented the same way. The details are a little more hidden under the covers. In addition to failure, this commit promotes the eh_personality and rust_stack_exhausted functions to official lang items. The compiler can generate calls to these functions, causing linkage errors if they are left undefined. The checking for these items is not as precise as it could be. Crates compiling with `-Z no-landing-pads` will not need the eh_personality lang item, and crates compiling with no split stacks won't need the stack exhausted lang item. For ease, however, these items are checked for presence in all final outputs of the compiler. It is quite easy to define dummy versions of the functions necessary: #[lang = "stack_exhausted"] extern fn stack_exhausted() { /* ... */ } #[lang = "eh_personality"] extern fn eh_personality() { /* ... */ } cc #11922, rust_stack_exhausted is now a lang item cc #13851, libcollections is blocked on eh_personality becoming weak
2014-05-19 11:30:09 -05:00
intrinsics: RefCell<FnvHashMap<&'static str, ValueRef>>,
/// Number of LLVM instructions translated into this `LocalCrateContext`.
/// This is used to perform some basic load-balancing to keep all LLVM
/// contexts around the same size.
n_llvm_insns: Cell<uint>,
trait_cache: RefCell<FnvHashMap<Rc<ty::TraitRef<'tcx>>,
traits::Vtable<'tcx, ()>>>,
2013-06-12 21:02:33 -05:00
}
pub struct CrateContext<'a, 'tcx: 'a> {
shared: &'a SharedCrateContext<'tcx>,
local: &'a LocalCrateContext<'tcx>,
/// The index of `local` in `shared.local_ccxs`. This is used in
/// `maybe_iter(true)` to identify the original `LocalCrateContext`.
index: uint,
}
pub struct CrateContextIterator<'a, 'tcx: 'a> {
shared: &'a SharedCrateContext<'tcx>,
index: uint,
}
impl<'a, 'tcx> Iterator<CrateContext<'a, 'tcx>> for CrateContextIterator<'a,'tcx> {
fn next(&mut self) -> Option<CrateContext<'a, 'tcx>> {
if self.index >= self.shared.local_ccxs.len() {
return None;
}
let index = self.index;
self.index += 1;
Some(CrateContext {
shared: self.shared,
local: &self.shared.local_ccxs[index],
index: index,
})
}
}
/// The iterator produced by `CrateContext::maybe_iter`.
pub struct CrateContextMaybeIterator<'a, 'tcx: 'a> {
shared: &'a SharedCrateContext<'tcx>,
index: uint,
single: bool,
origin: uint,
}
impl<'a, 'tcx> Iterator<(CrateContext<'a, 'tcx>, bool)> for CrateContextMaybeIterator<'a, 'tcx> {
fn next(&mut self) -> Option<(CrateContext<'a, 'tcx>, bool)> {
if self.index >= self.shared.local_ccxs.len() {
return None;
}
let index = self.index;
self.index += 1;
if self.single {
self.index = self.shared.local_ccxs.len();
}
let ccx = CrateContext {
shared: self.shared,
local: &self.shared.local_ccxs[index],
index: index,
};
Some((ccx, index == self.origin))
}
}
unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
let llcx = llvm::LLVMContextCreate();
let llmod = mod_name.with_c_str(|buf| {
llvm::LLVMModuleCreateWithNameInContext(buf, llcx)
});
sess.target
.target
.data_layout
.as_slice()
.with_c_str(|buf| {
llvm::LLVMSetDataLayout(llmod, buf);
});
sess.target
.target
.llvm_target
.as_slice()
.with_c_str(|buf| {
llvm::LLVMRustSetNormalizedTarget(llmod, buf);
});
(llcx, llmod)
}
impl<'tcx> SharedCrateContext<'tcx> {
pub fn new(crate_name: &str,
local_count: uint,
tcx: ty::ctxt<'tcx>,
emap2: resolve::ExportMap2,
symbol_hasher: Sha256,
link_meta: LinkMeta,
reachable: NodeSet)
-> SharedCrateContext<'tcx> {
let (metadata_llcx, metadata_llmod) = unsafe {
create_context_and_module(&tcx.sess, "metadata")
};
let mut shared_ccx = SharedCrateContext {
local_ccxs: Vec::with_capacity(local_count),
metadata_llmod: metadata_llmod,
metadata_llcx: metadata_llcx,
exp_map2: emap2,
reachable: reachable,
item_symbols: RefCell::new(NodeMap::new()),
link_meta: link_meta,
symbol_hasher: RefCell::new(symbol_hasher),
tcx: tcx,
stats: Stats {
n_static_tydescs: Cell::new(0u),
n_glues_created: Cell::new(0u),
n_null_glues: Cell::new(0u),
n_real_glues: Cell::new(0u),
n_fns: Cell::new(0u),
n_monos: Cell::new(0u),
n_inlines: Cell::new(0u),
n_closures: Cell::new(0u),
n_llvm_insns: Cell::new(0u),
llvm_insns: RefCell::new(FnvHashMap::new()),
fn_stats: RefCell::new(Vec::new()),
},
available_monomorphizations: RefCell::new(FnvHashSet::new()),
available_drop_glues: RefCell::new(FnvHashMap::new()),
};
for i in range(0, local_count) {
// Append ".rs" to crate name as LLVM module identifier.
//
// LLVM code generator emits a ".file filename" directive
// for ELF backends. Value of the "filename" is set as the
// LLVM module identifier. Due to a LLVM MC bug[1], LLVM
// crashes if the module identifier is same as other symbols
// such as a function name in the module.
// 1. http://llvm.org/bugs/show_bug.cgi?id=11479
let llmod_id = format!("{}.{}.rs", crate_name, i);
let local_ccx = LocalCrateContext::new(&shared_ccx, llmod_id.as_slice());
shared_ccx.local_ccxs.push(local_ccx);
}
shared_ccx
}
pub fn iter<'a>(&'a self) -> CrateContextIterator<'a, 'tcx> {
CrateContextIterator {
shared: self,
index: 0,
}
}
pub fn get_ccx<'a>(&'a self, index: uint) -> CrateContext<'a, 'tcx> {
CrateContext {
shared: self,
local: &self.local_ccxs[index],
index: index,
}
}
fn get_smallest_ccx<'a>(&'a self) -> CrateContext<'a, 'tcx> {
let (local_ccx, index) =
self.local_ccxs
.iter()
.zip(range(0, self.local_ccxs.len()))
.min_by(|&(local_ccx, _idx)| local_ccx.n_llvm_insns.get())
.unwrap();
CrateContext {
shared: self,
local: local_ccx,
index: index,
}
}
pub fn metadata_llmod(&self) -> ModuleRef {
self.metadata_llmod
}
pub fn metadata_llcx(&self) -> ContextRef {
self.metadata_llcx
}
pub fn exp_map2<'a>(&'a self) -> &'a resolve::ExportMap2 {
&self.exp_map2
}
pub fn reachable<'a>(&'a self) -> &'a NodeSet {
&self.reachable
}
pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
&self.item_symbols
}
pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
&self.link_meta
}
pub fn symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
&self.symbol_hasher
}
pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
&self.tcx
}
pub fn take_tcx(self) -> ty::ctxt<'tcx> {
self.tcx
}
pub fn sess<'a>(&'a self) -> &'a Session {
&self.tcx.sess
}
pub fn stats<'a>(&'a self) -> &'a Stats {
&self.stats
}
}
impl<'tcx> LocalCrateContext<'tcx> {
fn new(shared: &SharedCrateContext<'tcx>,
name: &str)
-> LocalCrateContext<'tcx> {
unsafe {
let (llcx, llmod) = create_context_and_module(&shared.tcx.sess, name);
let td = mk_target_data(shared.tcx
.sess
.target
.target
.data_layout
.as_slice());
let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo {
Some(debuginfo::CrateDebugContext::new(llmod))
} else {
None
};
let mut local_ccx = LocalCrateContext {
llmod: llmod,
llcx: llcx,
td: td,
tn: TypeNames::new(),
externs: RefCell::new(FnvHashMap::new()),
item_vals: RefCell::new(NodeMap::new()),
drop_glues: RefCell::new(FnvHashMap::new()),
tydescs: RefCell::new(FnvHashMap::new()),
finished_tydescs: Cell::new(false),
external: RefCell::new(DefIdMap::new()),
external_srcs: RefCell::new(NodeMap::new()),
monomorphized: RefCell::new(FnvHashMap::new()),
monomorphizing: RefCell::new(DefIdMap::new()),
vtables: RefCell::new(FnvHashMap::new()),
const_cstr_cache: RefCell::new(FnvHashMap::new()),
const_globals: RefCell::new(FnvHashMap::new()),
const_values: RefCell::new(NodeMap::new()),
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
static_values: RefCell::new(NodeMap::new()),
extern_const_values: RefCell::new(DefIdMap::new()),
impl_method_cache: RefCell::new(FnvHashMap::new()),
closure_bare_wrapper_cache: RefCell::new(FnvHashMap::new()),
lltypes: RefCell::new(FnvHashMap::new()),
llsizingtypes: RefCell::new(FnvHashMap::new()),
adt_reprs: RefCell::new(FnvHashMap::new()),
type_hashcodes: RefCell::new(FnvHashMap::new()),
all_llvm_symbols: RefCell::new(FnvHashSet::new()),
2014-09-14 22:27:36 -05:00
int_type: Type::from_ref(ptr::null_mut()),
opaque_vec_type: Type::from_ref(ptr::null_mut()),
builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)),
unboxed_closure_vals: RefCell::new(FnvHashMap::new()),
dbg_cx: dbg_cx,
rustc: Add official support for weak failure This commit is part of the ongoing libstd facade efforts (cc #13851). The compiler now recognizes some language items as "extern { fn foo(...); }" and will automatically perform the following actions: 1. The foreign function has a pre-defined name. 2. The crate and downstream crates can only be built as rlibs until a crate defines the lang item itself. 3. The actual lang item has a pre-defined name. This is essentially nicer compiler support for the hokey core-depends-on-std-failure scheme today, but it is implemented the same way. The details are a little more hidden under the covers. In addition to failure, this commit promotes the eh_personality and rust_stack_exhausted functions to official lang items. The compiler can generate calls to these functions, causing linkage errors if they are left undefined. The checking for these items is not as precise as it could be. Crates compiling with `-Z no-landing-pads` will not need the eh_personality lang item, and crates compiling with no split stacks won't need the stack exhausted lang item. For ease, however, these items are checked for presence in all final outputs of the compiler. It is quite easy to define dummy versions of the functions necessary: #[lang = "stack_exhausted"] extern fn stack_exhausted() { /* ... */ } #[lang = "eh_personality"] extern fn eh_personality() { /* ... */ } cc #11922, rust_stack_exhausted is now a lang item cc #13851, libcollections is blocked on eh_personality becoming weak
2014-05-19 11:30:09 -05:00
eh_personality: RefCell::new(None),
intrinsics: RefCell::new(FnvHashMap::new()),
n_llvm_insns: Cell::new(0u),
trait_cache: RefCell::new(FnvHashMap::new()),
};
local_ccx.int_type = Type::int(&local_ccx.dummy_ccx(shared));
local_ccx.opaque_vec_type = Type::opaque_vec(&local_ccx.dummy_ccx(shared));
// Done mutating local_ccx directly. (The rest of the
// initialization goes through RefCell.)
{
let ccx = local_ccx.dummy_ccx(shared);
let mut str_slice_ty = Type::named_struct(&ccx, "str_slice");
2014-11-17 02:39:01 -06:00
str_slice_ty.set_struct_body(&[Type::i8p(&ccx), ccx.int_type()], false);
ccx.tn().associate_type("str_slice", &str_slice_ty);
ccx.tn().associate_type("tydesc", &Type::tydesc(&ccx, str_slice_ty));
if ccx.sess().count_llvm_insns() {
base::init_insn_ctxt()
}
}
local_ccx
}
}
/// Create a dummy `CrateContext` from `self` and the provided
/// `SharedCrateContext`. This is somewhat dangerous because `self` may
/// not actually be an element of `shared.local_ccxs`, which can cause some
/// operations to panic unexpectedly.
///
/// This is used in the `LocalCrateContext` constructor to allow calling
/// functions that expect a complete `CrateContext`, even before the local
/// portion is fully initialized and attached to the `SharedCrateContext`.
fn dummy_ccx<'a>(&'a self, shared: &'a SharedCrateContext<'tcx>)
-> CrateContext<'a, 'tcx> {
CrateContext {
shared: shared,
local: self,
index: -1 as uint,
}
}
}
impl<'b, 'tcx> CrateContext<'b, 'tcx> {
pub fn shared(&self) -> &'b SharedCrateContext<'tcx> {
self.shared
}
pub fn local(&self) -> &'b LocalCrateContext<'tcx> {
self.local
}
/// Get a (possibly) different `CrateContext` from the same
/// `SharedCrateContext`.
pub fn rotate(&self) -> CrateContext<'b, 'tcx> {
self.shared.get_smallest_ccx()
}
/// Either iterate over only `self`, or iterate over all `CrateContext`s in
/// the `SharedCrateContext`. The iterator produces `(ccx, is_origin)`
/// pairs, where `is_origin` is `true` if `ccx` is `self` and `false`
/// otherwise. This method is useful for avoiding code duplication in
/// cases where it may or may not be necessary to translate code into every
/// context.
pub fn maybe_iter(&self, iter_all: bool) -> CrateContextMaybeIterator<'b, 'tcx> {
CrateContextMaybeIterator {
shared: self.shared,
index: if iter_all { 0 } else { self.index },
single: !iter_all,
origin: self.index,
}
}
pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
&self.shared.tcx
2014-09-05 11:18:53 -05:00
}
2014-03-05 08:36:01 -06:00
pub fn sess<'a>(&'a self) -> &'a Session {
&self.shared.tcx.sess
2014-03-05 08:36:01 -06:00
}
pub fn builder<'a>(&'a self) -> Builder<'a, 'tcx> {
Builder::new(self)
}
2014-09-05 11:18:53 -05:00
pub fn raw_builder<'a>(&'a self) -> BuilderRef {
self.local.builder.b
2014-09-05 11:18:53 -05:00
}
pub fn tydesc_type(&self) -> Type {
self.local.tn.find_type("tydesc").unwrap()
}
2014-04-09 18:56:31 -05:00
pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
return v;
2014-04-09 18:56:31 -05:00
}
match declare_intrinsic(self, key) {
Some(v) => return v,
None => panic!()
2014-04-09 18:56:31 -05:00
}
}
pub fn is_split_stack_supported(&self) -> bool {
self.sess().target.target.options.morestack
}
2014-09-05 11:18:53 -05:00
pub fn llmod(&self) -> ModuleRef {
self.local.llmod
2014-09-05 11:18:53 -05:00
}
pub fn llcx(&self) -> ContextRef {
self.local.llcx
2014-09-05 11:18:53 -05:00
}
pub fn td<'a>(&'a self) -> &'a TargetData {
&self.local.td
2014-09-05 11:18:53 -05:00
}
pub fn tn<'a>(&'a self) -> &'a TypeNames {
&self.local.tn
2014-09-05 11:18:53 -05:00
}
pub fn externs<'a>(&'a self) -> &'a RefCell<ExternMap> {
&self.local.externs
2014-09-05 11:18:53 -05:00
}
pub fn item_vals<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
&self.local.item_vals
2014-09-05 11:18:53 -05:00
}
pub fn exp_map2<'a>(&'a self) -> &'a resolve::ExportMap2 {
&self.shared.exp_map2
2014-09-05 11:18:53 -05:00
}
pub fn reachable<'a>(&'a self) -> &'a NodeSet {
&self.shared.reachable
2014-09-05 11:18:53 -05:00
}
pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
&self.shared.item_symbols
2014-09-05 11:18:53 -05:00
}
pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
&self.shared.link_meta
2014-09-05 11:18:53 -05:00
}
pub fn drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, ValueRef>> {
&self.local.drop_glues
2014-09-05 11:18:53 -05:00
}
pub fn tydescs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<tydesc_info<'tcx>>>> {
&self.local.tydescs
2014-09-05 11:18:53 -05:00
}
pub fn finished_tydescs<'a>(&'a self) -> &'a Cell<bool> {
&self.local.finished_tydescs
2014-09-05 11:18:53 -05:00
}
pub fn external<'a>(&'a self) -> &'a RefCell<DefIdMap<Option<ast::NodeId>>> {
&self.local.external
2014-09-05 11:18:53 -05:00
}
pub fn external_srcs<'a>(&'a self) -> &'a RefCell<NodeMap<ast::DefId>> {
&self.local.external_srcs
2014-09-05 11:18:53 -05:00
}
pub fn monomorphized<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>> {
&self.local.monomorphized
2014-09-05 11:18:53 -05:00
}
pub fn monomorphizing<'a>(&'a self) -> &'a RefCell<DefIdMap<uint>> {
&self.local.monomorphizing
2014-09-05 11:18:53 -05:00
}
pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<(Ty<'tcx>, Rc<ty::TraitRef<'tcx>>),
ValueRef>> {
&self.local.vtables
2014-09-05 11:18:53 -05:00
}
pub fn const_cstr_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<InternedString, ValueRef>> {
&self.local.const_cstr_cache
2014-09-05 11:18:53 -05:00
}
pub fn const_globals<'a>(&'a self) -> &'a RefCell<FnvHashMap<int, ValueRef>> {
&self.local.const_globals
2014-09-05 11:18:53 -05:00
}
pub fn const_values<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
&self.local.const_values
2014-09-05 11:18:53 -05:00
}
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
pub fn static_values<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
&self.local.static_values
}
2014-09-05 11:18:53 -05:00
pub fn extern_const_values<'a>(&'a self) -> &'a RefCell<DefIdMap<ValueRef>> {
&self.local.extern_const_values
2014-09-05 11:18:53 -05:00
}
pub fn impl_method_cache<'a>(&'a self)
-> &'a RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>> {
&self.local.impl_method_cache
2014-09-05 11:18:53 -05:00
}
pub fn closure_bare_wrapper_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
&self.local.closure_bare_wrapper_cache
2014-09-05 11:18:53 -05:00
}
pub fn lltypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
&self.local.lltypes
2014-09-05 11:18:53 -05:00
}
pub fn llsizingtypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
&self.local.llsizingtypes
2014-09-05 11:18:53 -05:00
}
pub fn adt_reprs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>> {
&self.local.adt_reprs
2014-09-05 11:18:53 -05:00
}
pub fn symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
&self.shared.symbol_hasher
2014-09-05 11:18:53 -05:00
}
pub fn type_hashcodes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
&self.local.type_hashcodes
2014-09-05 11:18:53 -05:00
}
pub fn all_llvm_symbols<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
&self.local.all_llvm_symbols
2014-09-05 11:18:53 -05:00
}
pub fn stats<'a>(&'a self) -> &'a Stats {
&self.shared.stats
2014-09-05 11:18:53 -05:00
}
pub fn available_monomorphizations<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
&self.shared.available_monomorphizations
}
pub fn available_drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
&self.shared.available_drop_glues
}
2014-09-05 11:18:53 -05:00
pub fn int_type(&self) -> Type {
self.local.int_type
2014-09-05 11:18:53 -05:00
}
pub fn opaque_vec_type(&self) -> Type {
self.local.opaque_vec_type
2014-09-05 11:18:53 -05:00
}
pub fn unboxed_closure_vals<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>,ValueRef>> {
&self.local.unboxed_closure_vals
2014-09-05 11:18:53 -05:00
}
pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext<'tcx>> {
&self.local.dbg_cx
2014-09-05 11:18:53 -05:00
}
pub fn eh_personality<'a>(&'a self) -> &'a RefCell<Option<ValueRef>> {
&self.local.eh_personality
2014-09-05 11:18:53 -05:00
}
fn intrinsics<'a>(&'a self) -> &'a RefCell<FnvHashMap<&'static str, ValueRef>> {
&self.local.intrinsics
2014-09-05 11:18:53 -05:00
}
pub fn count_llvm_insn(&self) {
self.local.n_llvm_insns.set(self.local.n_llvm_insns.get() + 1);
}
pub fn trait_cache(&self) -> &RefCell<FnvHashMap<Rc<ty::TraitRef<'tcx>>,
traits::Vtable<'tcx, ()>>> {
&self.local.trait_cache
}
/// Return exclusive upper bound on object size.
///
/// The theoretical maximum object size is defined as the maximum positive `int` value. This
/// ensures that the `offset` semantics remain well-defined by allowing it to correctly index
/// every address within an object along with one byte past the end, along with allowing `int`
/// to store the difference between any two pointers into an object.
///
/// The upper bound on 64-bit currently needs to be lower because LLVM uses a 64-bit integer to
/// represent object size in bits. It would need to be 1 << 61 to account for this, but is
/// currently conservatively bounded to 1 << 47 as that is enough to cover the current usable
/// address space on 64-bit ARMv8 and x86_64.
pub fn obj_size_bound(&self) -> u64 {
match self.sess().target.target.target_word_size[] {
"32" => 1 << 31,
"64" => 1 << 47,
_ => unreachable!() // error handled by config::build_target_config
}
}
pub fn report_overbig_object(&self, obj: Ty<'tcx>) -> ! {
self.sess().fatal(
2014-10-15 12:03:44 -05:00
format!("the type `{}` is too big for the current architecture",
obj.repr(self.tcx())).as_slice())
}
2014-04-09 18:56:31 -05:00
}
fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
macro_rules! ifn (
($name:expr fn() -> $ret:expr) => (
if *key == $name {
let f = base::decl_cdecl_fn(
2014-11-17 02:39:01 -06:00
ccx, $name, Type::func(&[], &$ret),
ty::mk_nil(ccx.tcx()));
2014-09-05 11:18:53 -05:00
ccx.intrinsics().borrow_mut().insert($name, f.clone());
2014-04-09 18:56:31 -05:00
return Some(f);
}
);
($name:expr fn($($arg:expr),*) -> $ret:expr) => (
if *key == $name {
let f = base::decl_cdecl_fn(ccx, $name,
2014-11-17 02:39:01 -06:00
Type::func(&[$($arg),*], &$ret), ty::mk_nil(ccx.tcx()));
2014-09-05 11:18:53 -05:00
ccx.intrinsics().borrow_mut().insert($name, f.clone());
2014-04-09 18:56:31 -05:00
return Some(f);
}
)
)
macro_rules! mk_struct (
2014-11-17 02:39:01 -06:00
($($field_ty:expr),*) => (Type::struct_(ccx, &[$($field_ty),*], false))
2014-04-09 18:56:31 -05:00
)
let i8p = Type::i8p(ccx);
let void = Type::void(ccx);
let i1 = Type::i1(ccx);
let t_i8 = Type::i8(ccx);
let t_i16 = Type::i16(ccx);
let t_i32 = Type::i32(ccx);
let t_i64 = Type::i64(ccx);
let t_f32 = Type::f32(ccx);
let t_f64 = Type::f64(ccx);
ifn!("llvm.memcpy.p0i8.p0i8.i32" fn(i8p, i8p, t_i32, t_i32, i1) -> void);
ifn!("llvm.memcpy.p0i8.p0i8.i64" fn(i8p, i8p, t_i64, t_i32, i1) -> void);
ifn!("llvm.memmove.p0i8.p0i8.i32" fn(i8p, i8p, t_i32, t_i32, i1) -> void);
ifn!("llvm.memmove.p0i8.p0i8.i64" fn(i8p, i8p, t_i64, t_i32, i1) -> void);
ifn!("llvm.memset.p0i8.i32" fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
ifn!("llvm.memset.p0i8.i64" fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
ifn!("llvm.trap" fn() -> void);
ifn!("llvm.debugtrap" fn() -> void);
ifn!("llvm.frameaddress" fn(t_i32) -> i8p);
ifn!("llvm.powi.f32" fn(t_f32, t_i32) -> t_f32);
ifn!("llvm.powi.f64" fn(t_f64, t_i32) -> t_f64);
ifn!("llvm.pow.f32" fn(t_f32, t_f32) -> t_f32);
ifn!("llvm.pow.f64" fn(t_f64, t_f64) -> t_f64);
ifn!("llvm.sqrt.f32" fn(t_f32) -> t_f32);
ifn!("llvm.sqrt.f64" fn(t_f64) -> t_f64);
ifn!("llvm.sin.f32" fn(t_f32) -> t_f32);
ifn!("llvm.sin.f64" fn(t_f64) -> t_f64);
ifn!("llvm.cos.f32" fn(t_f32) -> t_f32);
ifn!("llvm.cos.f64" fn(t_f64) -> t_f64);
ifn!("llvm.exp.f32" fn(t_f32) -> t_f32);
ifn!("llvm.exp.f64" fn(t_f64) -> t_f64);
ifn!("llvm.exp2.f32" fn(t_f32) -> t_f32);
ifn!("llvm.exp2.f64" fn(t_f64) -> t_f64);
ifn!("llvm.log.f32" fn(t_f32) -> t_f32);
ifn!("llvm.log.f64" fn(t_f64) -> t_f64);
ifn!("llvm.log10.f32" fn(t_f32) -> t_f32);
ifn!("llvm.log10.f64" fn(t_f64) -> t_f64);
ifn!("llvm.log2.f32" fn(t_f32) -> t_f32);
ifn!("llvm.log2.f64" fn(t_f64) -> t_f64);
ifn!("llvm.fma.f32" fn(t_f32, t_f32, t_f32) -> t_f32);
ifn!("llvm.fma.f64" fn(t_f64, t_f64, t_f64) -> t_f64);
ifn!("llvm.fabs.f32" fn(t_f32) -> t_f32);
ifn!("llvm.fabs.f64" fn(t_f64) -> t_f64);
ifn!("llvm.floor.f32" fn(t_f32) -> t_f32);
ifn!("llvm.floor.f64" fn(t_f64) -> t_f64);
ifn!("llvm.ceil.f32" fn(t_f32) -> t_f32);
ifn!("llvm.ceil.f64" fn(t_f64) -> t_f64);
ifn!("llvm.trunc.f32" fn(t_f32) -> t_f32);
ifn!("llvm.trunc.f64" fn(t_f64) -> t_f64);
ifn!("llvm.rint.f32" fn(t_f32) -> t_f32);
ifn!("llvm.rint.f64" fn(t_f64) -> t_f64);
ifn!("llvm.nearbyint.f32" fn(t_f32) -> t_f32);
ifn!("llvm.nearbyint.f64" fn(t_f64) -> t_f64);
ifn!("llvm.ctpop.i8" fn(t_i8) -> t_i8);
ifn!("llvm.ctpop.i16" fn(t_i16) -> t_i16);
ifn!("llvm.ctpop.i32" fn(t_i32) -> t_i32);
ifn!("llvm.ctpop.i64" fn(t_i64) -> t_i64);
ifn!("llvm.ctlz.i8" fn(t_i8 , i1) -> t_i8);
ifn!("llvm.ctlz.i16" fn(t_i16, i1) -> t_i16);
ifn!("llvm.ctlz.i32" fn(t_i32, i1) -> t_i32);
ifn!("llvm.ctlz.i64" fn(t_i64, i1) -> t_i64);
ifn!("llvm.cttz.i8" fn(t_i8 , i1) -> t_i8);
ifn!("llvm.cttz.i16" fn(t_i16, i1) -> t_i16);
ifn!("llvm.cttz.i32" fn(t_i32, i1) -> t_i32);
ifn!("llvm.cttz.i64" fn(t_i64, i1) -> t_i64);
ifn!("llvm.bswap.i16" fn(t_i16) -> t_i16);
ifn!("llvm.bswap.i32" fn(t_i32) -> t_i32);
ifn!("llvm.bswap.i64" fn(t_i64) -> t_i64);
ifn!("llvm.sadd.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
ifn!("llvm.sadd.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
ifn!("llvm.sadd.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
ifn!("llvm.sadd.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
ifn!("llvm.uadd.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
ifn!("llvm.uadd.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
ifn!("llvm.uadd.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
ifn!("llvm.uadd.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
ifn!("llvm.ssub.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
ifn!("llvm.ssub.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
ifn!("llvm.ssub.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
ifn!("llvm.ssub.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
ifn!("llvm.usub.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
ifn!("llvm.usub.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
ifn!("llvm.usub.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
ifn!("llvm.usub.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
ifn!("llvm.smul.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
ifn!("llvm.smul.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
ifn!("llvm.smul.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
ifn!("llvm.smul.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
ifn!("llvm.umul.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
ifn!("llvm.umul.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
ifn!("llvm.umul.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
ifn!("llvm.umul.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
Emit LLVM lifetime intrinsics to improve stack usage and codegen in general Lifetime intrinsics help to reduce stack usage, because LLVM can apply stack coloring to reuse the stack slots of dead allocas for new ones. For example these functions now both use the same amount of stack, while previous `bar()` used five times as much as `foo()`: ````rust fn foo() { println("{}", 5); } fn bar() { println("{}", 5); println("{}", 5); println("{}", 5); println("{}", 5); println("{}", 5); } ```` On top of that, LLVM can also optimize out certain operations when it knows that memory is dead after a certain point. For example, it can sometimes remove the zeroing used to cancel the drop glue. This is possible when the glue drop itself was already removed because the zeroing dominated the drop glue call. For example in: ````rust pub fn bar(x: (Box<int>, int)) -> (Box<int>, int) { x } ```` With optimizations, this currently results in: ````llvm define void @_ZN3bar20h330fa42547df8179niaE({ i64*, i64 }* noalias nocapture nonnull sret, { i64*, i64 }* noalias nocapture nonnull) unnamed_addr #0 { "_ZN29_$LP$Box$LT$int$GT$$C$int$RP$39glue_drop.$x22glue_drop$x22$LP$1347$RP$17h88cf42702e5a322aE.exit": %2 = bitcast { i64*, i64 }* %1 to i8* %3 = bitcast { i64*, i64 }* %0 to i8* tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 16, i32 8, i1 false) tail call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 16, i32 8, i1 false) ret void } ```` But with lifetime intrinsics we get: ````llvm define void @_ZN3bar20h330fa42547df8179niaE({ i64*, i64 }* noalias nocapture nonnull sret, { i64*, i64 }* noalias nocapture nonnull) unnamed_addr #0 { "_ZN29_$LP$Box$LT$int$GT$$C$int$RP$39glue_drop.$x22glue_drop$x22$LP$1347$RP$17h88cf42702e5a322aE.exit": %2 = bitcast { i64*, i64 }* %1 to i8* %3 = bitcast { i64*, i64 }* %0 to i8* tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 16, i32 8, i1 false) tail call void @llvm.lifetime.end(i64 16, i8* %2) ret void } ```` Fixes #15665
2014-05-01 12:32:07 -05:00
ifn!("llvm.lifetime.start" fn(t_i64,i8p) -> void);
ifn!("llvm.lifetime.end" fn(t_i64, i8p) -> void);
2014-04-09 18:56:31 -05:00
ifn!("llvm.expect.i1" fn(i1, i1) -> i1);
ifn!("llvm.assume" fn(i1) -> void);
2014-04-09 18:56:31 -05:00
// Some intrinsics were introduced in later versions of LLVM, but they have
// fallbacks in libc or libm and such. Currently, all of these intrinsics
// were introduced in LLVM 3.4, so we case on that.
macro_rules! compatible_ifn (
($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr) => (
if unsafe { llvm::LLVMVersionMinor() >= 4 } {
// The `if key == $name` is already in ifn!
ifn!($name fn($($arg),*) -> $ret);
} else if *key == $name {
let f = base::decl_cdecl_fn(ccx, stringify!($cname),
2014-11-17 02:39:01 -06:00
Type::func(&[$($arg),*], &$ret),
ty::mk_nil(ccx.tcx()));
2014-09-05 11:18:53 -05:00
ccx.intrinsics().borrow_mut().insert($name, f.clone());
2014-04-09 18:56:31 -05:00
return Some(f);
}
)
)
compatible_ifn!("llvm.copysign.f32", copysignf(t_f32, t_f32) -> t_f32);
compatible_ifn!("llvm.copysign.f64", copysign(t_f64, t_f64) -> t_f64);
compatible_ifn!("llvm.round.f32", roundf(t_f32) -> t_f32);
compatible_ifn!("llvm.round.f64", round(t_f64) -> t_f64);
if ccx.sess().opts.debuginfo != NoDebugInfo {
ifn!("llvm.dbg.declare" fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
ifn!("llvm.dbg.value" fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);
}
return None;
}