1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use llvm::{ContextRef, ModuleRef, ValueRef, BuilderRef};
13 use llvm::{TargetData};
14 use llvm::mk_target_data;
15 use metadata::common::LinkMeta;
16 use middle::def::ExportMap;
20 use trans::builder::Builder;
21 use trans::common::{ExternMap,tydesc_info,BuilderRef_res};
23 use trans::monomorphize::MonoId;
24 use trans::type_::{Type, TypeNames};
25 use middle::ty::{mod, Ty};
26 use session::config::NoDebugInfo;
28 use util::ppaux::Repr;
29 use util::sha2::Sha256;
30 use util::nodemap::{NodeMap, NodeSet, DefIdMap, FnvHashMap, FnvHashSet};
32 use std::cell::{Cell, RefCell};
33 use std::c_str::ToCStr;
37 use syntax::parse::token::InternedString;
40 pub n_static_tydescs: Cell<uint>,
41 pub n_glues_created: Cell<uint>,
42 pub n_null_glues: Cell<uint>,
43 pub n_real_glues: Cell<uint>,
44 pub n_fns: Cell<uint>,
45 pub n_monos: Cell<uint>,
46 pub n_inlines: Cell<uint>,
47 pub n_closures: Cell<uint>,
48 pub n_llvm_insns: Cell<uint>,
49 pub llvm_insns: RefCell<FnvHashMap<String, uint>>,
50 // (ident, llvm-instructions)
51 pub fn_stats: RefCell<Vec<(String, uint)> >,
54 /// The shared portion of a `CrateContext`. There is one `SharedCrateContext`
55 /// per crate. The data here is shared between all compilation units of the
56 /// crate, so it must not contain references to any LLVM data structures
57 /// (aside from metadata-related ones).
58 pub struct SharedCrateContext<'tcx> {
59 local_ccxs: Vec<LocalCrateContext<'tcx>>,
61 metadata_llmod: ModuleRef,
62 metadata_llcx: ContextRef,
64 export_map: ExportMap,
66 item_symbols: RefCell<NodeMap<String>>,
68 symbol_hasher: RefCell<Sha256>,
72 available_monomorphizations: RefCell<FnvHashSet<String>>,
73 available_drop_glues: RefCell<FnvHashMap<Ty<'tcx>, String>>,
76 /// The local portion of a `CrateContext`. There is one `LocalCrateContext`
77 /// per compilation unit. Each one has its own LLVM `ContextRef` so that
78 /// several compilation units may be optimized in parallel. All other LLVM
79 /// data structures in the `LocalCrateContext` are tied to that `ContextRef`.
80 pub struct LocalCrateContext<'tcx> {
85 externs: RefCell<ExternMap>,
86 item_vals: RefCell<NodeMap<ValueRef>>,
87 needs_unwind_cleanup_cache: RefCell<FnvHashMap<Ty<'tcx>, bool>>,
88 fn_pointer_shims: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
89 drop_glues: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
90 tydescs: RefCell<FnvHashMap<Ty<'tcx>, Rc<tydesc_info<'tcx>>>>,
91 /// Set when running emit_tydescs to enforce that no more tydescs are
93 finished_tydescs: Cell<bool>,
94 /// Track mapping of external ids to local items imported for inlining
95 external: RefCell<DefIdMap<Option<ast::NodeId>>>,
96 /// Backwards version of the `external` map (inlined items to where they
98 external_srcs: RefCell<NodeMap<ast::DefId>>,
99 /// Cache instances of monomorphized functions
100 monomorphized: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
101 monomorphizing: RefCell<DefIdMap<uint>>,
102 /// Cache generated vtables
103 vtables: RefCell<FnvHashMap<(Ty<'tcx>, ty::PolyTraitRef<'tcx>), ValueRef>>,
104 /// Cache of constant strings,
105 const_cstr_cache: RefCell<FnvHashMap<InternedString, ValueRef>>,
107 /// Reverse-direction for const ptrs cast from globals.
108 /// Key is an int, cast from a ValueRef holding a *T,
109 /// Val is a ValueRef holding a *[T].
111 /// Needed because LLVM loses pointer->pointee association
112 /// when we ptrcast, and we have to ptrcast during translation
113 /// of a [T] const because we form a slice, a [*T,int] pair, not
114 /// a pointer to an LLVM array type.
115 const_globals: RefCell<FnvHashMap<int, ValueRef>>,
117 /// Cache of emitted const values
118 const_values: RefCell<NodeMap<ValueRef>>,
120 /// Cache of emitted static values
121 static_values: RefCell<NodeMap<ValueRef>>,
123 /// Cache of external const values
124 extern_const_values: RefCell<DefIdMap<ValueRef>>,
126 impl_method_cache: RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>>,
128 /// Cache of closure wrappers for bare fn's.
129 closure_bare_wrapper_cache: RefCell<FnvHashMap<ValueRef, ValueRef>>,
131 lltypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
132 llsizingtypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
133 adt_reprs: RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>>,
134 type_hashcodes: RefCell<FnvHashMap<Ty<'tcx>, String>>,
135 all_llvm_symbols: RefCell<FnvHashSet<String>>,
137 opaque_vec_type: Type,
138 builder: BuilderRef_res,
140 /// Holds the LLVM values for closure IDs.
141 unboxed_closure_vals: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
143 dbg_cx: Option<debuginfo::CrateDebugContext<'tcx>>,
145 eh_personality: RefCell<Option<ValueRef>>,
147 intrinsics: RefCell<FnvHashMap<&'static str, ValueRef>>,
149 /// Number of LLVM instructions translated into this `LocalCrateContext`.
150 /// This is used to perform some basic load-balancing to keep all LLVM
151 /// contexts around the same size.
152 n_llvm_insns: Cell<uint>,
154 trait_cache: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>,
155 traits::Vtable<'tcx, ()>>>,
158 pub struct CrateContext<'a, 'tcx: 'a> {
159 shared: &'a SharedCrateContext<'tcx>,
160 local: &'a LocalCrateContext<'tcx>,
161 /// The index of `local` in `shared.local_ccxs`. This is used in
162 /// `maybe_iter(true)` to identify the original `LocalCrateContext`.
166 pub struct CrateContextIterator<'a, 'tcx: 'a> {
167 shared: &'a SharedCrateContext<'tcx>,
171 impl<'a, 'tcx> Iterator for CrateContextIterator<'a,'tcx> {
172 type Item = CrateContext<'a, 'tcx>;
174 fn next(&mut self) -> Option<CrateContext<'a, 'tcx>> {
175 if self.index >= self.shared.local_ccxs.len() {
179 let index = self.index;
184 local: &self.shared.local_ccxs[index],
190 /// The iterator produced by `CrateContext::maybe_iter`.
191 pub struct CrateContextMaybeIterator<'a, 'tcx: 'a> {
192 shared: &'a SharedCrateContext<'tcx>,
198 impl<'a, 'tcx> Iterator for CrateContextMaybeIterator<'a, 'tcx> {
199 type Item = (CrateContext<'a, 'tcx>, bool);
201 fn next(&mut self) -> Option<(CrateContext<'a, 'tcx>, bool)> {
202 if self.index >= self.shared.local_ccxs.len() {
206 let index = self.index;
209 self.index = self.shared.local_ccxs.len();
212 let ccx = CrateContext {
214 local: &self.shared.local_ccxs[index],
217 Some((ccx, index == self.origin))
222 unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
223 let llcx = llvm::LLVMContextCreate();
224 let llmod = mod_name.with_c_str(|buf| {
225 llvm::LLVMModuleCreateWithNameInContext(buf, llcx)
231 llvm::LLVMSetDataLayout(llmod, buf);
237 llvm::LLVMRustSetNormalizedTarget(llmod, buf);
242 impl<'tcx> SharedCrateContext<'tcx> {
243 pub fn new(crate_name: &str,
246 export_map: ExportMap,
247 symbol_hasher: Sha256,
250 -> SharedCrateContext<'tcx> {
251 let (metadata_llcx, metadata_llmod) = unsafe {
252 create_context_and_module(&tcx.sess, "metadata")
255 let mut shared_ccx = SharedCrateContext {
256 local_ccxs: Vec::with_capacity(local_count),
257 metadata_llmod: metadata_llmod,
258 metadata_llcx: metadata_llcx,
259 export_map: export_map,
260 reachable: reachable,
261 item_symbols: RefCell::new(NodeMap::new()),
262 link_meta: link_meta,
263 symbol_hasher: RefCell::new(symbol_hasher),
266 n_static_tydescs: Cell::new(0u),
267 n_glues_created: Cell::new(0u),
268 n_null_glues: Cell::new(0u),
269 n_real_glues: Cell::new(0u),
270 n_fns: Cell::new(0u),
271 n_monos: Cell::new(0u),
272 n_inlines: Cell::new(0u),
273 n_closures: Cell::new(0u),
274 n_llvm_insns: Cell::new(0u),
275 llvm_insns: RefCell::new(FnvHashMap::new()),
276 fn_stats: RefCell::new(Vec::new()),
278 available_monomorphizations: RefCell::new(FnvHashSet::new()),
279 available_drop_glues: RefCell::new(FnvHashMap::new()),
282 for i in range(0, local_count) {
283 // Append ".rs" to crate name as LLVM module identifier.
285 // LLVM code generator emits a ".file filename" directive
286 // for ELF backends. Value of the "filename" is set as the
287 // LLVM module identifier. Due to a LLVM MC bug[1], LLVM
288 // crashes if the module identifier is same as other symbols
289 // such as a function name in the module.
290 // 1. http://llvm.org/bugs/show_bug.cgi?id=11479
291 let llmod_id = format!("{}.{}.rs", crate_name, i);
292 let local_ccx = LocalCrateContext::new(&shared_ccx, llmod_id[]);
293 shared_ccx.local_ccxs.push(local_ccx);
299 pub fn iter<'a>(&'a self) -> CrateContextIterator<'a, 'tcx> {
300 CrateContextIterator {
306 pub fn get_ccx<'a>(&'a self, index: uint) -> CrateContext<'a, 'tcx> {
309 local: &self.local_ccxs[index],
314 fn get_smallest_ccx<'a>(&'a self) -> CrateContext<'a, 'tcx> {
315 let (local_ccx, index) =
318 .zip(range(0, self.local_ccxs.len()))
319 .min_by(|&(local_ccx, _idx)| local_ccx.n_llvm_insns.get())
329 pub fn metadata_llmod(&self) -> ModuleRef {
333 pub fn metadata_llcx(&self) -> ContextRef {
337 pub fn export_map<'a>(&'a self) -> &'a ExportMap {
341 pub fn reachable<'a>(&'a self) -> &'a NodeSet {
345 pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
349 pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
353 pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
357 pub fn take_tcx(self) -> ty::ctxt<'tcx> {
361 pub fn sess<'a>(&'a self) -> &'a Session {
365 pub fn stats<'a>(&'a self) -> &'a Stats {
370 impl<'tcx> LocalCrateContext<'tcx> {
371 fn new(shared: &SharedCrateContext<'tcx>,
373 -> LocalCrateContext<'tcx> {
375 let (llcx, llmod) = create_context_and_module(&shared.tcx.sess, name);
377 let td = mk_target_data(shared.tcx
384 let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo {
385 Some(debuginfo::CrateDebugContext::new(llmod))
390 let mut local_ccx = LocalCrateContext {
394 tn: TypeNames::new(),
395 externs: RefCell::new(FnvHashMap::new()),
396 item_vals: RefCell::new(NodeMap::new()),
397 needs_unwind_cleanup_cache: RefCell::new(FnvHashMap::new()),
398 fn_pointer_shims: RefCell::new(FnvHashMap::new()),
399 drop_glues: RefCell::new(FnvHashMap::new()),
400 tydescs: RefCell::new(FnvHashMap::new()),
401 finished_tydescs: Cell::new(false),
402 external: RefCell::new(DefIdMap::new()),
403 external_srcs: RefCell::new(NodeMap::new()),
404 monomorphized: RefCell::new(FnvHashMap::new()),
405 monomorphizing: RefCell::new(DefIdMap::new()),
406 vtables: RefCell::new(FnvHashMap::new()),
407 const_cstr_cache: RefCell::new(FnvHashMap::new()),
408 const_globals: RefCell::new(FnvHashMap::new()),
409 const_values: RefCell::new(NodeMap::new()),
410 static_values: RefCell::new(NodeMap::new()),
411 extern_const_values: RefCell::new(DefIdMap::new()),
412 impl_method_cache: RefCell::new(FnvHashMap::new()),
413 closure_bare_wrapper_cache: RefCell::new(FnvHashMap::new()),
414 lltypes: RefCell::new(FnvHashMap::new()),
415 llsizingtypes: RefCell::new(FnvHashMap::new()),
416 adt_reprs: RefCell::new(FnvHashMap::new()),
417 type_hashcodes: RefCell::new(FnvHashMap::new()),
418 all_llvm_symbols: RefCell::new(FnvHashSet::new()),
419 int_type: Type::from_ref(ptr::null_mut()),
420 opaque_vec_type: Type::from_ref(ptr::null_mut()),
421 builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)),
422 unboxed_closure_vals: RefCell::new(FnvHashMap::new()),
424 eh_personality: RefCell::new(None),
425 intrinsics: RefCell::new(FnvHashMap::new()),
426 n_llvm_insns: Cell::new(0u),
427 trait_cache: RefCell::new(FnvHashMap::new()),
430 local_ccx.int_type = Type::int(&local_ccx.dummy_ccx(shared));
431 local_ccx.opaque_vec_type = Type::opaque_vec(&local_ccx.dummy_ccx(shared));
433 // Done mutating local_ccx directly. (The rest of the
434 // initialization goes through RefCell.)
436 let ccx = local_ccx.dummy_ccx(shared);
438 let mut str_slice_ty = Type::named_struct(&ccx, "str_slice");
439 str_slice_ty.set_struct_body(&[Type::i8p(&ccx), ccx.int_type()], false);
440 ccx.tn().associate_type("str_slice", &str_slice_ty);
442 ccx.tn().associate_type("tydesc", &Type::tydesc(&ccx, str_slice_ty));
444 if ccx.sess().count_llvm_insns() {
445 base::init_insn_ctxt()
453 /// Create a dummy `CrateContext` from `self` and the provided
454 /// `SharedCrateContext`. This is somewhat dangerous because `self` may
455 /// not actually be an element of `shared.local_ccxs`, which can cause some
456 /// operations to panic unexpectedly.
458 /// This is used in the `LocalCrateContext` constructor to allow calling
459 /// functions that expect a complete `CrateContext`, even before the local
460 /// portion is fully initialized and attached to the `SharedCrateContext`.
461 fn dummy_ccx<'a>(&'a self, shared: &'a SharedCrateContext<'tcx>)
462 -> CrateContext<'a, 'tcx> {
471 impl<'b, 'tcx> CrateContext<'b, 'tcx> {
472 pub fn shared(&self) -> &'b SharedCrateContext<'tcx> {
476 pub fn local(&self) -> &'b LocalCrateContext<'tcx> {
481 /// Get a (possibly) different `CrateContext` from the same
482 /// `SharedCrateContext`.
483 pub fn rotate(&self) -> CrateContext<'b, 'tcx> {
484 self.shared.get_smallest_ccx()
487 /// Either iterate over only `self`, or iterate over all `CrateContext`s in
488 /// the `SharedCrateContext`. The iterator produces `(ccx, is_origin)`
489 /// pairs, where `is_origin` is `true` if `ccx` is `self` and `false`
490 /// otherwise. This method is useful for avoiding code duplication in
491 /// cases where it may or may not be necessary to translate code into every
493 pub fn maybe_iter(&self, iter_all: bool) -> CrateContextMaybeIterator<'b, 'tcx> {
494 CrateContextMaybeIterator {
496 index: if iter_all { 0 } else { self.index },
503 pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
507 pub fn sess<'a>(&'a self) -> &'a Session {
508 &self.shared.tcx.sess
511 pub fn builder<'a>(&'a self) -> Builder<'a, 'tcx> {
515 pub fn raw_builder<'a>(&'a self) -> BuilderRef {
519 pub fn tydesc_type(&self) -> Type {
520 self.local.tn.find_type("tydesc").unwrap()
523 pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
524 if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
527 match declare_intrinsic(self, key) {
533 pub fn is_split_stack_supported(&self) -> bool {
534 self.sess().target.target.options.morestack
538 pub fn llmod(&self) -> ModuleRef {
542 pub fn llcx(&self) -> ContextRef {
546 pub fn td<'a>(&'a self) -> &'a TargetData {
550 pub fn tn<'a>(&'a self) -> &'a TypeNames {
554 pub fn externs<'a>(&'a self) -> &'a RefCell<ExternMap> {
558 pub fn item_vals<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
559 &self.local.item_vals
562 pub fn export_map<'a>(&'a self) -> &'a ExportMap {
563 &self.shared.export_map
566 pub fn reachable<'a>(&'a self) -> &'a NodeSet {
567 &self.shared.reachable
570 pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
571 &self.shared.item_symbols
574 pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
575 &self.shared.link_meta
578 pub fn needs_unwind_cleanup_cache(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, bool>> {
579 &self.local.needs_unwind_cleanup_cache
582 pub fn fn_pointer_shims(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, ValueRef>> {
583 &self.local.fn_pointer_shims
586 pub fn drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, ValueRef>> {
587 &self.local.drop_glues
590 pub fn tydescs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<tydesc_info<'tcx>>>> {
594 pub fn finished_tydescs<'a>(&'a self) -> &'a Cell<bool> {
595 &self.local.finished_tydescs
598 pub fn external<'a>(&'a self) -> &'a RefCell<DefIdMap<Option<ast::NodeId>>> {
602 pub fn external_srcs<'a>(&'a self) -> &'a RefCell<NodeMap<ast::DefId>> {
603 &self.local.external_srcs
606 pub fn monomorphized<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>> {
607 &self.local.monomorphized
610 pub fn monomorphizing<'a>(&'a self) -> &'a RefCell<DefIdMap<uint>> {
611 &self.local.monomorphizing
614 pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<(Ty<'tcx>, ty::PolyTraitRef<'tcx>),
619 pub fn const_cstr_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<InternedString, ValueRef>> {
620 &self.local.const_cstr_cache
623 pub fn const_globals<'a>(&'a self) -> &'a RefCell<FnvHashMap<int, ValueRef>> {
624 &self.local.const_globals
627 pub fn const_values<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
628 &self.local.const_values
631 pub fn static_values<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
632 &self.local.static_values
635 pub fn extern_const_values<'a>(&'a self) -> &'a RefCell<DefIdMap<ValueRef>> {
636 &self.local.extern_const_values
639 pub fn impl_method_cache<'a>(&'a self)
640 -> &'a RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>> {
641 &self.local.impl_method_cache
644 pub fn closure_bare_wrapper_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
645 &self.local.closure_bare_wrapper_cache
648 pub fn lltypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
652 pub fn llsizingtypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
653 &self.local.llsizingtypes
656 pub fn adt_reprs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>> {
657 &self.local.adt_reprs
660 pub fn symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
661 &self.shared.symbol_hasher
664 pub fn type_hashcodes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
665 &self.local.type_hashcodes
668 pub fn all_llvm_symbols<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
669 &self.local.all_llvm_symbols
672 pub fn stats<'a>(&'a self) -> &'a Stats {
676 pub fn available_monomorphizations<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
677 &self.shared.available_monomorphizations
680 pub fn available_drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
681 &self.shared.available_drop_glues
684 pub fn int_type(&self) -> Type {
688 pub fn opaque_vec_type(&self) -> Type {
689 self.local.opaque_vec_type
692 pub fn unboxed_closure_vals<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>,ValueRef>> {
693 &self.local.unboxed_closure_vals
696 pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext<'tcx>> {
700 pub fn eh_personality<'a>(&'a self) -> &'a RefCell<Option<ValueRef>> {
701 &self.local.eh_personality
704 fn intrinsics<'a>(&'a self) -> &'a RefCell<FnvHashMap<&'static str, ValueRef>> {
705 &self.local.intrinsics
708 pub fn count_llvm_insn(&self) {
709 self.local.n_llvm_insns.set(self.local.n_llvm_insns.get() + 1);
712 pub fn trait_cache(&self) -> &RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>,
713 traits::Vtable<'tcx, ()>>> {
714 &self.local.trait_cache
717 /// Return exclusive upper bound on object size.
719 /// The theoretical maximum object size is defined as the maximum positive `int` value. This
720 /// ensures that the `offset` semantics remain well-defined by allowing it to correctly index
721 /// every address within an object along with one byte past the end, along with allowing `int`
722 /// to store the difference between any two pointers into an object.
724 /// The upper bound on 64-bit currently needs to be lower because LLVM uses a 64-bit integer to
725 /// represent object size in bits. It would need to be 1 << 61 to account for this, but is
726 /// currently conservatively bounded to 1 << 47 as that is enough to cover the current usable
727 /// address space on 64-bit ARMv8 and x86_64.
728 pub fn obj_size_bound(&self) -> u64 {
729 match self.sess().target.target.target_word_size[] {
732 _ => unreachable!() // error handled by config::build_target_config
736 pub fn report_overbig_object(&self, obj: Ty<'tcx>) -> ! {
738 format!("the type `{}` is too big for the current architecture",
739 obj.repr(self.tcx()))[])
743 fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
745 ($name:expr fn() -> $ret:expr) => (
747 let f = base::decl_cdecl_fn(
748 ccx, $name, Type::func(&[], &$ret),
749 ty::mk_nil(ccx.tcx()));
750 ccx.intrinsics().borrow_mut().insert($name, f.clone());
754 ($name:expr fn($($arg:expr),*) -> $ret:expr) => (
756 let f = base::decl_cdecl_fn(ccx, $name,
757 Type::func(&[$($arg),*], &$ret), ty::mk_nil(ccx.tcx()));
758 ccx.intrinsics().borrow_mut().insert($name, f.clone());
763 macro_rules! mk_struct (
764 ($($field_ty:expr),*) => (Type::struct_(ccx, &[$($field_ty),*], false))
767 let i8p = Type::i8p(ccx);
768 let void = Type::void(ccx);
769 let i1 = Type::i1(ccx);
770 let t_i8 = Type::i8(ccx);
771 let t_i16 = Type::i16(ccx);
772 let t_i32 = Type::i32(ccx);
773 let t_i64 = Type::i64(ccx);
774 let t_f32 = Type::f32(ccx);
775 let t_f64 = Type::f64(ccx);
777 ifn!("llvm.memcpy.p0i8.p0i8.i32" fn(i8p, i8p, t_i32, t_i32, i1) -> void);
778 ifn!("llvm.memcpy.p0i8.p0i8.i64" fn(i8p, i8p, t_i64, t_i32, i1) -> void);
779 ifn!("llvm.memmove.p0i8.p0i8.i32" fn(i8p, i8p, t_i32, t_i32, i1) -> void);
780 ifn!("llvm.memmove.p0i8.p0i8.i64" fn(i8p, i8p, t_i64, t_i32, i1) -> void);
781 ifn!("llvm.memset.p0i8.i32" fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
782 ifn!("llvm.memset.p0i8.i64" fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
784 ifn!("llvm.trap" fn() -> void);
785 ifn!("llvm.debugtrap" fn() -> void);
786 ifn!("llvm.frameaddress" fn(t_i32) -> i8p);
788 ifn!("llvm.powi.f32" fn(t_f32, t_i32) -> t_f32);
789 ifn!("llvm.powi.f64" fn(t_f64, t_i32) -> t_f64);
790 ifn!("llvm.pow.f32" fn(t_f32, t_f32) -> t_f32);
791 ifn!("llvm.pow.f64" fn(t_f64, t_f64) -> t_f64);
793 ifn!("llvm.sqrt.f32" fn(t_f32) -> t_f32);
794 ifn!("llvm.sqrt.f64" fn(t_f64) -> t_f64);
795 ifn!("llvm.sin.f32" fn(t_f32) -> t_f32);
796 ifn!("llvm.sin.f64" fn(t_f64) -> t_f64);
797 ifn!("llvm.cos.f32" fn(t_f32) -> t_f32);
798 ifn!("llvm.cos.f64" fn(t_f64) -> t_f64);
799 ifn!("llvm.exp.f32" fn(t_f32) -> t_f32);
800 ifn!("llvm.exp.f64" fn(t_f64) -> t_f64);
801 ifn!("llvm.exp2.f32" fn(t_f32) -> t_f32);
802 ifn!("llvm.exp2.f64" fn(t_f64) -> t_f64);
803 ifn!("llvm.log.f32" fn(t_f32) -> t_f32);
804 ifn!("llvm.log.f64" fn(t_f64) -> t_f64);
805 ifn!("llvm.log10.f32" fn(t_f32) -> t_f32);
806 ifn!("llvm.log10.f64" fn(t_f64) -> t_f64);
807 ifn!("llvm.log2.f32" fn(t_f32) -> t_f32);
808 ifn!("llvm.log2.f64" fn(t_f64) -> t_f64);
810 ifn!("llvm.fma.f32" fn(t_f32, t_f32, t_f32) -> t_f32);
811 ifn!("llvm.fma.f64" fn(t_f64, t_f64, t_f64) -> t_f64);
813 ifn!("llvm.fabs.f32" fn(t_f32) -> t_f32);
814 ifn!("llvm.fabs.f64" fn(t_f64) -> t_f64);
816 ifn!("llvm.floor.f32" fn(t_f32) -> t_f32);
817 ifn!("llvm.floor.f64" fn(t_f64) -> t_f64);
818 ifn!("llvm.ceil.f32" fn(t_f32) -> t_f32);
819 ifn!("llvm.ceil.f64" fn(t_f64) -> t_f64);
820 ifn!("llvm.trunc.f32" fn(t_f32) -> t_f32);
821 ifn!("llvm.trunc.f64" fn(t_f64) -> t_f64);
823 ifn!("llvm.rint.f32" fn(t_f32) -> t_f32);
824 ifn!("llvm.rint.f64" fn(t_f64) -> t_f64);
825 ifn!("llvm.nearbyint.f32" fn(t_f32) -> t_f32);
826 ifn!("llvm.nearbyint.f64" fn(t_f64) -> t_f64);
828 ifn!("llvm.ctpop.i8" fn(t_i8) -> t_i8);
829 ifn!("llvm.ctpop.i16" fn(t_i16) -> t_i16);
830 ifn!("llvm.ctpop.i32" fn(t_i32) -> t_i32);
831 ifn!("llvm.ctpop.i64" fn(t_i64) -> t_i64);
833 ifn!("llvm.ctlz.i8" fn(t_i8 , i1) -> t_i8);
834 ifn!("llvm.ctlz.i16" fn(t_i16, i1) -> t_i16);
835 ifn!("llvm.ctlz.i32" fn(t_i32, i1) -> t_i32);
836 ifn!("llvm.ctlz.i64" fn(t_i64, i1) -> t_i64);
838 ifn!("llvm.cttz.i8" fn(t_i8 , i1) -> t_i8);
839 ifn!("llvm.cttz.i16" fn(t_i16, i1) -> t_i16);
840 ifn!("llvm.cttz.i32" fn(t_i32, i1) -> t_i32);
841 ifn!("llvm.cttz.i64" fn(t_i64, i1) -> t_i64);
843 ifn!("llvm.bswap.i16" fn(t_i16) -> t_i16);
844 ifn!("llvm.bswap.i32" fn(t_i32) -> t_i32);
845 ifn!("llvm.bswap.i64" fn(t_i64) -> t_i64);
847 ifn!("llvm.sadd.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
848 ifn!("llvm.sadd.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
849 ifn!("llvm.sadd.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
850 ifn!("llvm.sadd.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
852 ifn!("llvm.uadd.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
853 ifn!("llvm.uadd.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
854 ifn!("llvm.uadd.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
855 ifn!("llvm.uadd.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
857 ifn!("llvm.ssub.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
858 ifn!("llvm.ssub.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
859 ifn!("llvm.ssub.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
860 ifn!("llvm.ssub.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
862 ifn!("llvm.usub.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
863 ifn!("llvm.usub.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
864 ifn!("llvm.usub.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
865 ifn!("llvm.usub.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
867 ifn!("llvm.smul.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
868 ifn!("llvm.smul.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
869 ifn!("llvm.smul.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
870 ifn!("llvm.smul.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
872 ifn!("llvm.umul.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
873 ifn!("llvm.umul.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
874 ifn!("llvm.umul.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
875 ifn!("llvm.umul.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
877 ifn!("llvm.lifetime.start" fn(t_i64,i8p) -> void);
878 ifn!("llvm.lifetime.end" fn(t_i64, i8p) -> void);
880 ifn!("llvm.expect.i1" fn(i1, i1) -> i1);
881 ifn!("llvm.assume" fn(i1) -> void);
883 // Some intrinsics were introduced in later versions of LLVM, but they have
884 // fallbacks in libc or libm and such. Currently, all of these intrinsics
885 // were introduced in LLVM 3.4, so we case on that.
886 macro_rules! compatible_ifn (
887 ($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr) => (
888 if unsafe { llvm::LLVMVersionMinor() >= 4 } {
889 // The `if key == $name` is already in ifn!
890 ifn!($name fn($($arg),*) -> $ret);
891 } else if *key == $name {
892 let f = base::decl_cdecl_fn(ccx, stringify!($cname),
893 Type::func(&[$($arg),*], &$ret),
894 ty::mk_nil(ccx.tcx()));
895 ccx.intrinsics().borrow_mut().insert($name, f.clone());
901 compatible_ifn!("llvm.copysign.f32", copysignf(t_f32, t_f32) -> t_f32);
902 compatible_ifn!("llvm.copysign.f64", copysign(t_f64, t_f64) -> t_f64);
903 compatible_ifn!("llvm.round.f32", roundf(t_f32) -> t_f32);
904 compatible_ifn!("llvm.round.f64", round(t_f64) -> t_f64);
907 if ccx.sess().opts.debuginfo != NoDebugInfo {
908 ifn!("llvm.dbg.declare" fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
909 ifn!("llvm.dbg.value" fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);