use llvm;
use llvm::{ContextRef, ModuleRef, ValueRef, BuilderRef};
-use llvm::{TargetData};
+use llvm::TargetData;
use llvm::mk_target_data;
use metadata::common::LinkMeta;
use middle::def::ExportMap;
use trans::adt;
use trans::base;
use trans::builder::Builder;
-use trans::common::{ExternMap,tydesc_info,BuilderRef_res};
+use trans::common::{ExternMap,BuilderRef_res};
use trans::debuginfo;
+use trans::declare;
+use trans::glue::DropGlueKind;
use trans::monomorphize::MonoId;
use trans::type_::{Type, TypeNames};
use middle::subst::Substs;
use middle::ty::{self, 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 syntax::parse::token::InternedString;
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>>,
+ pub n_glues_created: Cell<usize>,
+ pub n_null_glues: Cell<usize>,
+ pub n_real_glues: Cell<usize>,
+ pub n_fns: Cell<usize>,
+ pub n_monos: Cell<usize>,
+ pub n_inlines: Cell<usize>,
+ pub n_closures: Cell<usize>,
+ pub n_llvm_insns: Cell<usize>,
+ pub llvm_insns: RefCell<FnvHashMap<String, usize>>,
// (ident, llvm-instructions)
- pub fn_stats: RefCell<Vec<(String, uint)> >,
+ pub fn_stats: RefCell<Vec<(String, usize)> >,
}
/// 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> {
+pub struct SharedCrateContext<'a, 'tcx: 'a> {
local_ccxs: Vec<LocalCrateContext<'tcx>>,
metadata_llmod: ModuleRef,
item_symbols: RefCell<NodeMap<String>>,
link_meta: LinkMeta,
symbol_hasher: RefCell<Sha256>,
- tcx: ty::ctxt<'tcx>,
+ tcx: &'a ty::ctxt<'tcx>,
stats: Stats,
+ check_overflow: bool,
+ check_drop_flag_for_sanity: bool,
- available_monomorphizations: RefCell<FnvHashSet<String>>,
- available_drop_glues: RefCell<FnvHashMap<Ty<'tcx>, String>>,
+ available_drop_glues: RefCell<FnvHashMap<DropGlueKind<'tcx>, String>>,
+ use_dll_storage_attrs: bool,
}
/// The local portion of a `CrateContext`. There is one `LocalCrateContext`
item_vals: RefCell<NodeMap<ValueRef>>,
needs_unwind_cleanup_cache: RefCell<FnvHashMap<Ty<'tcx>, bool>>,
fn_pointer_shims: RefCell<FnvHashMap<Ty<'tcx>, 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.
- finished_tydescs: Cell<bool>,
+ drop_glues: RefCell<FnvHashMap<DropGlueKind<'tcx>, ValueRef>>,
/// Track mapping of external ids to local items imported for inlining
external: RefCell<DefIdMap<Option<ast::NodeId>>>,
/// Backwards version of the `external` map (inlined items to where they
external_srcs: RefCell<NodeMap<ast::DefId>>,
/// Cache instances of monomorphized functions
monomorphized: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
- monomorphizing: RefCell<DefIdMap<uint>>,
+ monomorphizing: RefCell<DefIdMap<usize>>,
+ available_monomorphizations: RefCell<FnvHashSet<String>>,
/// Cache generated vtables
- vtables: RefCell<FnvHashMap<(Ty<'tcx>, ty::PolyTraitRef<'tcx>), ValueRef>>,
+ vtables: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>>,
/// Cache of constant strings,
const_cstr_cache: RefCell<FnvHashMap<InternedString, ValueRef>>,
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>>,
int_type: Type,
opaque_vec_type: Type,
builder: BuilderRef_res,
/// 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>,
+ n_llvm_insns: Cell<usize>,
trait_cache: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>,
traits::Vtable<'tcx, ()>>>,
}
pub struct CrateContext<'a, 'tcx: 'a> {
- shared: &'a SharedCrateContext<'tcx>,
+ shared: &'a SharedCrateContext<'a, '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,
+ index: usize,
}
pub struct CrateContextIterator<'a, 'tcx: 'a> {
- shared: &'a SharedCrateContext<'tcx>,
- index: uint,
+ shared: &'a SharedCrateContext<'a, 'tcx>,
+ index: usize,
}
impl<'a, 'tcx> Iterator for CrateContextIterator<'a,'tcx> {
/// The iterator produced by `CrateContext::maybe_iter`.
pub struct CrateContextMaybeIterator<'a, 'tcx: 'a> {
- shared: &'a SharedCrateContext<'tcx>,
- index: uint,
+ shared: &'a SharedCrateContext<'a, 'tcx>,
+ index: usize,
single: bool,
- origin: uint,
+ origin: usize,
}
impl<'a, 'tcx> Iterator for CrateContextMaybeIterator<'a, 'tcx> {
(llcx, llmod)
}
-impl<'tcx> SharedCrateContext<'tcx> {
+impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
pub fn new(crate_name: &str,
- local_count: uint,
- tcx: ty::ctxt<'tcx>,
+ local_count: usize,
+ tcx: &'b ty::ctxt<'tcx>,
export_map: ExportMap,
symbol_hasher: Sha256,
link_meta: LinkMeta,
- reachable: NodeSet)
- -> SharedCrateContext<'tcx> {
+ reachable: NodeSet,
+ check_overflow: bool,
+ check_drop_flag_for_sanity: bool)
+ -> SharedCrateContext<'b, 'tcx> {
let (metadata_llcx, metadata_llmod) = unsafe {
create_context_and_module(&tcx.sess, "metadata")
};
+ // An interesting part of Windows which MSVC forces our hand on (and
+ // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
+ // attributes in LLVM IR as well as native dependencies (in C these
+ // correspond to `__declspec(dllimport)`).
+ //
+ // Whenever a dynamic library is built by MSVC it must have its public
+ // interface specified by functions tagged with `dllexport` or otherwise
+ // they're not available to be linked against. This poses a few problems
+ // for the compiler, some of which are somewhat fundamental, but we use
+ // the `use_dll_storage_attrs` variable below to attach the `dllexport`
+ // attribute to all LLVM functions that are reachable (e.g. they're
+ // already tagged with external linkage). This is suboptimal for a few
+ // reasons:
+ //
+ // * If an object file will never be included in a dynamic library,
+ // there's no need to attach the dllexport attribute. Most object
+ // files in Rust are not destined to become part of a dll as binaries
+ // are statically linked by default.
+ // * If the compiler is emitting both an rlib and a dylib, the same
+ // source object file is currently used but with MSVC this may be less
+ // feasible. The compiler may be able to get around this, but it may
+ // involve some invasive changes to deal with this.
+ //
+ // The flipside of this situation is that whenever you link to a dll and
+ // you import a function from it, the import should be tagged with
+ // `dllimport`. At this time, however, the compiler does not emit
+ // `dllimport` for any declarations other than constants (where it is
+ // required), which is again suboptimal for even more reasons!
+ //
+ // * Calling a function imported from another dll without using
+ // `dllimport` causes the linker/compiler to have extra overhead (one
+ // `jmp` instruction on x86) when calling the function.
+ // * The same object file may be used in different circumstances, so a
+ // function may be imported from a dll if the object is linked into a
+ // dll, but it may be just linked against if linked into an rlib.
+ // * The compiler has no knowledge about whether native functions should
+ // be tagged dllimport or not.
+ //
+ // For now the compiler takes the perf hit (I do not have any numbers to
+ // this effect) by marking very little as `dllimport` and praying the
+ // linker will take care of everything. Fixing this problem will likely
+ // require adding a few attributes to Rust itself (feature gated at the
+ // start) and then strongly recommending static linkage on MSVC!
+ let use_dll_storage_attrs = tcx.sess.target.target.options.is_like_msvc;
+
let mut shared_ccx = SharedCrateContext {
local_ccxs: Vec::with_capacity(local_count),
metadata_llmod: metadata_llmod,
symbol_hasher: RefCell::new(symbol_hasher),
tcx: tcx,
stats: Stats {
- n_static_tydescs: Cell::new(0),
n_glues_created: Cell::new(0),
n_null_glues: Cell::new(0),
n_real_glues: Cell::new(0),
llvm_insns: RefCell::new(FnvHashMap()),
fn_stats: RefCell::new(Vec::new()),
},
- available_monomorphizations: RefCell::new(FnvHashSet()),
+ check_overflow: check_overflow,
+ check_drop_flag_for_sanity: check_drop_flag_for_sanity,
available_drop_glues: RefCell::new(FnvHashMap()),
+ use_dll_storage_attrs: use_dll_storage_attrs,
};
for i in 0..local_count {
}
}
- pub fn get_ccx<'a>(&'a self, index: uint) -> CrateContext<'a, 'tcx> {
+ pub fn get_ccx<'a>(&'a self, index: usize) -> CrateContext<'a, 'tcx> {
CrateContext {
shared: self,
local: &self.local_ccxs[index],
}
pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
- &self.tcx
- }
-
- pub fn take_tcx(self) -> ty::ctxt<'tcx> {
self.tcx
}
pub fn stats<'a>(&'a self) -> &'a Stats {
&self.stats
}
+
+ pub fn use_dll_storage_attrs(&self) -> bool {
+ self.use_dll_storage_attrs
+ }
}
impl<'tcx> LocalCrateContext<'tcx> {
- fn new(shared: &SharedCrateContext<'tcx>,
+ fn new<'a>(shared: &SharedCrateContext<'a, 'tcx>,
name: &str)
-> LocalCrateContext<'tcx> {
unsafe {
.target
.target
.data_layout
- []);
+ );
let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo {
Some(debuginfo::CrateDebugContext::new(llmod))
needs_unwind_cleanup_cache: RefCell::new(FnvHashMap()),
fn_pointer_shims: RefCell::new(FnvHashMap()),
drop_glues: RefCell::new(FnvHashMap()),
- tydescs: RefCell::new(FnvHashMap()),
- finished_tydescs: Cell::new(false),
external: RefCell::new(DefIdMap()),
external_srcs: RefCell::new(NodeMap()),
monomorphized: RefCell::new(FnvHashMap()),
monomorphizing: RefCell::new(DefIdMap()),
+ available_monomorphizations: RefCell::new(FnvHashSet()),
vtables: RefCell::new(FnvHashMap()),
const_cstr_cache: RefCell::new(FnvHashMap()),
const_unsized: RefCell::new(FnvHashMap()),
llsizingtypes: RefCell::new(FnvHashMap()),
adt_reprs: RefCell::new(FnvHashMap()),
type_hashcodes: RefCell::new(FnvHashMap()),
- all_llvm_symbols: RefCell::new(FnvHashSet()),
int_type: Type::from_ref(ptr::null_mut()),
opaque_vec_type: Type::from_ref(ptr::null_mut()),
builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)),
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()
}
/// 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>)
+ fn dummy_ccx<'a>(&'a self, shared: &'a SharedCrateContext<'a, 'tcx>)
-> CrateContext<'a, 'tcx> {
CrateContext {
shared: shared,
local: self,
- index: -1 as uint,
+ index: !0 as usize,
}
}
}
impl<'b, 'tcx> CrateContext<'b, 'tcx> {
- pub fn shared(&self) -> &'b SharedCrateContext<'tcx> {
+ pub fn shared(&self) -> &'b SharedCrateContext<'b, 'tcx> {
self.shared
}
pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
- &self.shared.tcx
+ self.shared.tcx
}
pub fn sess<'a>(&'a self) -> &'a Session {
self.local.builder.b
}
- pub fn tydesc_type(&self) -> Type {
- self.local.tn.find_type("tydesc").unwrap()
- }
-
pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
return v;
&self.local.fn_pointer_shims
}
- pub fn drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, ValueRef>> {
+ pub fn drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<DropGlueKind<'tcx>, ValueRef>> {
&self.local.drop_glues
}
- pub fn tydescs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<tydesc_info<'tcx>>>> {
- &self.local.tydescs
- }
-
- pub fn finished_tydescs<'a>(&'a self) -> &'a Cell<bool> {
- &self.local.finished_tydescs
- }
-
pub fn external<'a>(&'a self) -> &'a RefCell<DefIdMap<Option<ast::NodeId>>> {
&self.local.external
}
&self.local.monomorphized
}
- pub fn monomorphizing<'a>(&'a self) -> &'a RefCell<DefIdMap<uint>> {
+ pub fn monomorphizing<'a>(&'a self) -> &'a RefCell<DefIdMap<usize>> {
&self.local.monomorphizing
}
- pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<(Ty<'tcx>, ty::PolyTraitRef<'tcx>),
- ValueRef>> {
+ pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>> {
&self.local.vtables
}
&self.local.type_hashcodes
}
- pub fn all_llvm_symbols<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
- &self.local.all_llvm_symbols
- }
-
pub fn stats<'a>(&'a self) -> &'a Stats {
&self.shared.stats
}
pub fn available_monomorphizations<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
- &self.shared.available_monomorphizations
+ &self.local.available_monomorphizations
}
- pub fn available_drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
+ pub fn available_drop_glues(&self) -> &RefCell<FnvHashMap<DropGlueKind<'tcx>, String>> {
&self.shared.available_drop_glues
}
/// 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_pointer_width[] {
+ match &self.sess().target.target.target_pointer_width[..] {
"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(
- &format!("the type `{}` is too big for the current architecture",
- obj.repr(self.tcx()))[])
+ &format!("the type `{:?}` is too big for the current architecture",
+ obj))
+ }
+
+ pub fn check_overflow(&self) -> bool {
+ self.shared.check_overflow
+ }
+
+ pub fn check_drop_flag_for_sanity(&self) -> bool {
+ // This controls whether we emit a conditional llvm.debugtrap
+ // guarded on whether the dropflag is one of its (two) valid
+ // values.
+ self.shared.check_drop_flag_for_sanity
+ }
+
+ pub fn use_dll_storage_attrs(&self) -> bool {
+ self.shared.use_dll_storage_attrs()
}
}
+/// Declare any llvm intrinsics that you might need
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(
- ccx, $name, Type::func(&[], &$ret),
- ty::mk_nil(ccx.tcx()));
+ let f = declare::declare_cfn(ccx, $name, Type::func(&[], &$ret),
+ ty::mk_nil(ccx.tcx()));
ccx.intrinsics().borrow_mut().insert($name, f.clone());
return Some(f);
}
);
($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
if *key == $name {
- let f = base::decl_cdecl_fn(ccx, $name,
- Type::func(&[$($arg),*], &$ret), ty::mk_nil(ccx.tcx()));
+ let f = declare::declare_cfn(ccx, $name, Type::func(&[$($arg),*], &$ret),
+ ty::mk_nil(ccx.tcx()));
ccx.intrinsics().borrow_mut().insert($name, f.clone());
return Some(f);
}
ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
+ ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
+ ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
+ ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
+ ifn!("llvm.round.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.lifetime.end", fn(t_i64, i8p) -> void);
ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
- ifn!("llvm.assume", fn(i1) -> void);
// 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.
+ // fallbacks in libc or libm and such.
macro_rules! compatible_ifn {
- ($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr) => (
- if unsafe { llvm::LLVMVersionMinor() >= 4 } {
+ ($name:expr, noop($cname:ident ($($arg:expr),*) -> void), $llvm_version:expr) => (
+ if unsafe { llvm::LLVMVersionMinor() >= $llvm_version } {
+ // The `if key == $name` is already in ifn!
+ ifn!($name, fn($($arg),*) -> void);
+ } else if *key == $name {
+ let f = declare::declare_cfn(ccx, stringify!($cname),
+ Type::func(&[$($arg),*], &void),
+ ty::mk_nil(ccx.tcx()));
+ llvm::SetLinkage(f, llvm::InternalLinkage);
+
+ let bld = ccx.builder();
+ let llbb = unsafe {
+ llvm::LLVMAppendBasicBlockInContext(ccx.llcx(), f,
+ "entry-block\0".as_ptr() as *const _)
+ };
+
+ bld.position_at_end(llbb);
+ bld.ret_void();
+
+ ccx.intrinsics().borrow_mut().insert($name, f.clone());
+ return Some(f);
+ }
+ );
+ ($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr, $llvm_version:expr) => (
+ if unsafe { llvm::LLVMVersionMinor() >= $llvm_version } {
// 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),
- Type::func(&[$($arg),*], &$ret),
- ty::mk_nil(ccx.tcx()));
+ let f = declare::declare_cfn(ccx, stringify!($cname),
+ Type::func(&[$($arg),*], &$ret),
+ ty::mk_nil(ccx.tcx()));
ccx.intrinsics().borrow_mut().insert($name, f.clone());
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);
-
+ compatible_ifn!("llvm.assume", noop(llvmcompat_assume(i1) -> void), 6);
if ccx.sess().opts.debuginfo != NoDebugInfo {
ifn!("llvm.dbg.declare", fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);