]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/context.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / librustc_trans / trans / context.rs
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.
4 //
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.
10
11 use llvm;
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;
17 use middle::traits;
18 use trans::adt;
19 use trans::base;
20 use trans::builder::Builder;
21 use trans::common::{ExternMap,tydesc_info,BuilderRef_res};
22 use trans::debuginfo;
23 use trans::monomorphize::MonoId;
24 use trans::type_::{Type, TypeNames};
25 use middle::subst::Substs;
26 use middle::ty::{self, Ty};
27 use session::config::NoDebugInfo;
28 use session::Session;
29 use util::ppaux::Repr;
30 use util::sha2::Sha256;
31 use util::nodemap::{NodeMap, NodeSet, DefIdMap, FnvHashMap, FnvHashSet};
32
33 use std::ffi::CString;
34 use std::cell::{Cell, RefCell};
35 use std::ptr;
36 use std::rc::Rc;
37 use syntax::ast;
38 use syntax::parse::token::InternedString;
39
40 pub struct Stats {
41     pub n_static_tydescs: Cell<uint>,
42     pub n_glues_created: Cell<uint>,
43     pub n_null_glues: Cell<uint>,
44     pub n_real_glues: Cell<uint>,
45     pub n_fns: Cell<uint>,
46     pub n_monos: Cell<uint>,
47     pub n_inlines: Cell<uint>,
48     pub n_closures: Cell<uint>,
49     pub n_llvm_insns: Cell<uint>,
50     pub llvm_insns: RefCell<FnvHashMap<String, uint>>,
51     // (ident, llvm-instructions)
52     pub fn_stats: RefCell<Vec<(String, uint)> >,
53 }
54
55 /// The shared portion of a `CrateContext`.  There is one `SharedCrateContext`
56 /// per crate.  The data here is shared between all compilation units of the
57 /// crate, so it must not contain references to any LLVM data structures
58 /// (aside from metadata-related ones).
59 pub struct SharedCrateContext<'tcx> {
60     local_ccxs: Vec<LocalCrateContext<'tcx>>,
61
62     metadata_llmod: ModuleRef,
63     metadata_llcx: ContextRef,
64
65     export_map: ExportMap,
66     reachable: NodeSet,
67     item_symbols: RefCell<NodeMap<String>>,
68     link_meta: LinkMeta,
69     symbol_hasher: RefCell<Sha256>,
70     tcx: ty::ctxt<'tcx>,
71     stats: Stats,
72
73     available_monomorphizations: RefCell<FnvHashSet<String>>,
74     available_drop_glues: RefCell<FnvHashMap<Ty<'tcx>, String>>,
75 }
76
77 /// The local portion of a `CrateContext`.  There is one `LocalCrateContext`
78 /// per compilation unit.  Each one has its own LLVM `ContextRef` so that
79 /// several compilation units may be optimized in parallel.  All other LLVM
80 /// data structures in the `LocalCrateContext` are tied to that `ContextRef`.
81 pub struct LocalCrateContext<'tcx> {
82     llmod: ModuleRef,
83     llcx: ContextRef,
84     td: TargetData,
85     tn: TypeNames,
86     externs: RefCell<ExternMap>,
87     item_vals: RefCell<NodeMap<ValueRef>>,
88     needs_unwind_cleanup_cache: RefCell<FnvHashMap<Ty<'tcx>, bool>>,
89     fn_pointer_shims: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
90     drop_glues: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
91     tydescs: RefCell<FnvHashMap<Ty<'tcx>, Rc<tydesc_info<'tcx>>>>,
92     /// Set when running emit_tydescs to enforce that no more tydescs are
93     /// created.
94     finished_tydescs: Cell<bool>,
95     /// Track mapping of external ids to local items imported for inlining
96     external: RefCell<DefIdMap<Option<ast::NodeId>>>,
97     /// Backwards version of the `external` map (inlined items to where they
98     /// came from)
99     external_srcs: RefCell<NodeMap<ast::DefId>>,
100     /// Cache instances of monomorphized functions
101     monomorphized: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
102     monomorphizing: RefCell<DefIdMap<uint>>,
103     /// Cache generated vtables
104     vtables: RefCell<FnvHashMap<(Ty<'tcx>, ty::PolyTraitRef<'tcx>), ValueRef>>,
105     /// Cache of constant strings,
106     const_cstr_cache: RefCell<FnvHashMap<InternedString, ValueRef>>,
107
108     /// Reverse-direction for const ptrs cast from globals.
109     /// Key is a ValueRef holding a *T,
110     /// Val is a ValueRef holding a *[T].
111     ///
112     /// Needed because LLVM loses pointer->pointee association
113     /// when we ptrcast, and we have to ptrcast during translation
114     /// of a [T] const because we form a slice, a (*T,usize) pair, not
115     /// a pointer to an LLVM array type. Similar for trait objects.
116     const_unsized: RefCell<FnvHashMap<ValueRef, ValueRef>>,
117
118     /// Cache of emitted const globals (value -> global)
119     const_globals: RefCell<FnvHashMap<ValueRef, ValueRef>>,
120
121     /// Cache of emitted const values
122     const_values: RefCell<FnvHashMap<(ast::NodeId, &'tcx Substs<'tcx>), ValueRef>>,
123
124     /// Cache of emitted static values
125     static_values: RefCell<NodeMap<ValueRef>>,
126
127     /// Cache of external const values
128     extern_const_values: RefCell<DefIdMap<ValueRef>>,
129
130     impl_method_cache: RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>>,
131
132     /// Cache of closure wrappers for bare fn's.
133     closure_bare_wrapper_cache: RefCell<FnvHashMap<ValueRef, ValueRef>>,
134
135     lltypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
136     llsizingtypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
137     adt_reprs: RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>>,
138     type_hashcodes: RefCell<FnvHashMap<Ty<'tcx>, String>>,
139     all_llvm_symbols: RefCell<FnvHashSet<String>>,
140     int_type: Type,
141     opaque_vec_type: Type,
142     builder: BuilderRef_res,
143
144     /// Holds the LLVM values for closure IDs.
145     closure_vals: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
146
147     dbg_cx: Option<debuginfo::CrateDebugContext<'tcx>>,
148
149     eh_personality: RefCell<Option<ValueRef>>,
150
151     intrinsics: RefCell<FnvHashMap<&'static str, ValueRef>>,
152
153     /// Number of LLVM instructions translated into this `LocalCrateContext`.
154     /// This is used to perform some basic load-balancing to keep all LLVM
155     /// contexts around the same size.
156     n_llvm_insns: Cell<uint>,
157
158     trait_cache: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>,
159                                     traits::Vtable<'tcx, ()>>>,
160 }
161
162 pub struct CrateContext<'a, 'tcx: 'a> {
163     shared: &'a SharedCrateContext<'tcx>,
164     local: &'a LocalCrateContext<'tcx>,
165     /// The index of `local` in `shared.local_ccxs`.  This is used in
166     /// `maybe_iter(true)` to identify the original `LocalCrateContext`.
167     index: uint,
168 }
169
170 pub struct CrateContextIterator<'a, 'tcx: 'a> {
171     shared: &'a SharedCrateContext<'tcx>,
172     index: uint,
173 }
174
175 impl<'a, 'tcx> Iterator for CrateContextIterator<'a,'tcx> {
176     type Item = CrateContext<'a, 'tcx>;
177
178     fn next(&mut self) -> Option<CrateContext<'a, 'tcx>> {
179         if self.index >= self.shared.local_ccxs.len() {
180             return None;
181         }
182
183         let index = self.index;
184         self.index += 1;
185
186         Some(CrateContext {
187             shared: self.shared,
188             local: &self.shared.local_ccxs[index],
189             index: index,
190         })
191     }
192 }
193
194 /// The iterator produced by `CrateContext::maybe_iter`.
195 pub struct CrateContextMaybeIterator<'a, 'tcx: 'a> {
196     shared: &'a SharedCrateContext<'tcx>,
197     index: uint,
198     single: bool,
199     origin: uint,
200 }
201
202 impl<'a, 'tcx> Iterator for CrateContextMaybeIterator<'a, 'tcx> {
203     type Item = (CrateContext<'a, 'tcx>, bool);
204
205     fn next(&mut self) -> Option<(CrateContext<'a, 'tcx>, bool)> {
206         if self.index >= self.shared.local_ccxs.len() {
207             return None;
208         }
209
210         let index = self.index;
211         self.index += 1;
212         if self.single {
213             self.index = self.shared.local_ccxs.len();
214         }
215
216         let ccx = CrateContext {
217             shared: self.shared,
218             local: &self.shared.local_ccxs[index],
219             index: index,
220         };
221         Some((ccx, index == self.origin))
222     }
223 }
224
225
226 unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
227     let llcx = llvm::LLVMContextCreate();
228     let mod_name = CString::from_slice(mod_name.as_bytes());
229     let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
230
231     let data_layout = &*sess.target.target.data_layout;
232     let data_layout = CString::from_slice(data_layout.as_bytes());
233     llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
234
235     let llvm_target = &*sess.target.target.llvm_target;
236     let llvm_target = CString::from_slice(llvm_target.as_bytes());
237     llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
238     (llcx, llmod)
239 }
240
241 impl<'tcx> SharedCrateContext<'tcx> {
242     pub fn new(crate_name: &str,
243                local_count: uint,
244                tcx: ty::ctxt<'tcx>,
245                export_map: ExportMap,
246                symbol_hasher: Sha256,
247                link_meta: LinkMeta,
248                reachable: NodeSet)
249                -> SharedCrateContext<'tcx> {
250         let (metadata_llcx, metadata_llmod) = unsafe {
251             create_context_and_module(&tcx.sess, "metadata")
252         };
253
254         let mut shared_ccx = SharedCrateContext {
255             local_ccxs: Vec::with_capacity(local_count),
256             metadata_llmod: metadata_llmod,
257             metadata_llcx: metadata_llcx,
258             export_map: export_map,
259             reachable: reachable,
260             item_symbols: RefCell::new(NodeMap()),
261             link_meta: link_meta,
262             symbol_hasher: RefCell::new(symbol_hasher),
263             tcx: tcx,
264             stats: Stats {
265                 n_static_tydescs: Cell::new(0),
266                 n_glues_created: Cell::new(0),
267                 n_null_glues: Cell::new(0),
268                 n_real_glues: Cell::new(0),
269                 n_fns: Cell::new(0),
270                 n_monos: Cell::new(0),
271                 n_inlines: Cell::new(0),
272                 n_closures: Cell::new(0),
273                 n_llvm_insns: Cell::new(0),
274                 llvm_insns: RefCell::new(FnvHashMap()),
275                 fn_stats: RefCell::new(Vec::new()),
276             },
277             available_monomorphizations: RefCell::new(FnvHashSet()),
278             available_drop_glues: RefCell::new(FnvHashMap()),
279         };
280
281         for i in 0..local_count {
282             // Append ".rs" to crate name as LLVM module identifier.
283             //
284             // LLVM code generator emits a ".file filename" directive
285             // for ELF backends. Value of the "filename" is set as the
286             // LLVM module identifier.  Due to a LLVM MC bug[1], LLVM
287             // crashes if the module identifier is same as other symbols
288             // such as a function name in the module.
289             // 1. http://llvm.org/bugs/show_bug.cgi?id=11479
290             let llmod_id = format!("{}.{}.rs", crate_name, i);
291             let local_ccx = LocalCrateContext::new(&shared_ccx, &llmod_id[]);
292             shared_ccx.local_ccxs.push(local_ccx);
293         }
294
295         shared_ccx
296     }
297
298     pub fn iter<'a>(&'a self) -> CrateContextIterator<'a, 'tcx> {
299         CrateContextIterator {
300             shared: self,
301             index: 0,
302         }
303     }
304
305     pub fn get_ccx<'a>(&'a self, index: uint) -> CrateContext<'a, 'tcx> {
306         CrateContext {
307             shared: self,
308             local: &self.local_ccxs[index],
309             index: index,
310         }
311     }
312
313     fn get_smallest_ccx<'a>(&'a self) -> CrateContext<'a, 'tcx> {
314         let (local_ccx, index) =
315             self.local_ccxs
316                 .iter()
317                 .zip(0..self.local_ccxs.len())
318                 .min_by(|&(local_ccx, _idx)| local_ccx.n_llvm_insns.get())
319                 .unwrap();
320         CrateContext {
321             shared: self,
322             local: local_ccx,
323             index: index,
324         }
325     }
326
327
328     pub fn metadata_llmod(&self) -> ModuleRef {
329         self.metadata_llmod
330     }
331
332     pub fn metadata_llcx(&self) -> ContextRef {
333         self.metadata_llcx
334     }
335
336     pub fn export_map<'a>(&'a self) -> &'a ExportMap {
337         &self.export_map
338     }
339
340     pub fn reachable<'a>(&'a self) -> &'a NodeSet {
341         &self.reachable
342     }
343
344     pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
345         &self.item_symbols
346     }
347
348     pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
349         &self.link_meta
350     }
351
352     pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
353         &self.tcx
354     }
355
356     pub fn take_tcx(self) -> ty::ctxt<'tcx> {
357         self.tcx
358     }
359
360     pub fn sess<'a>(&'a self) -> &'a Session {
361         &self.tcx.sess
362     }
363
364     pub fn stats<'a>(&'a self) -> &'a Stats {
365         &self.stats
366     }
367 }
368
369 impl<'tcx> LocalCrateContext<'tcx> {
370     fn new(shared: &SharedCrateContext<'tcx>,
371            name: &str)
372            -> LocalCrateContext<'tcx> {
373         unsafe {
374             let (llcx, llmod) = create_context_and_module(&shared.tcx.sess, name);
375
376             let td = mk_target_data(&shared.tcx
377                                           .sess
378                                           .target
379                                           .target
380                                           .data_layout
381                                           []);
382
383             let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo {
384                 Some(debuginfo::CrateDebugContext::new(llmod))
385             } else {
386                 None
387             };
388
389             let mut local_ccx = LocalCrateContext {
390                 llmod: llmod,
391                 llcx: llcx,
392                 td: td,
393                 tn: TypeNames::new(),
394                 externs: RefCell::new(FnvHashMap()),
395                 item_vals: RefCell::new(NodeMap()),
396                 needs_unwind_cleanup_cache: RefCell::new(FnvHashMap()),
397                 fn_pointer_shims: RefCell::new(FnvHashMap()),
398                 drop_glues: RefCell::new(FnvHashMap()),
399                 tydescs: RefCell::new(FnvHashMap()),
400                 finished_tydescs: Cell::new(false),
401                 external: RefCell::new(DefIdMap()),
402                 external_srcs: RefCell::new(NodeMap()),
403                 monomorphized: RefCell::new(FnvHashMap()),
404                 monomorphizing: RefCell::new(DefIdMap()),
405                 vtables: RefCell::new(FnvHashMap()),
406                 const_cstr_cache: RefCell::new(FnvHashMap()),
407                 const_unsized: RefCell::new(FnvHashMap()),
408                 const_globals: RefCell::new(FnvHashMap()),
409                 const_values: RefCell::new(FnvHashMap()),
410                 static_values: RefCell::new(NodeMap()),
411                 extern_const_values: RefCell::new(DefIdMap()),
412                 impl_method_cache: RefCell::new(FnvHashMap()),
413                 closure_bare_wrapper_cache: RefCell::new(FnvHashMap()),
414                 lltypes: RefCell::new(FnvHashMap()),
415                 llsizingtypes: RefCell::new(FnvHashMap()),
416                 adt_reprs: RefCell::new(FnvHashMap()),
417                 type_hashcodes: RefCell::new(FnvHashMap()),
418                 all_llvm_symbols: RefCell::new(FnvHashSet()),
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                 closure_vals: RefCell::new(FnvHashMap()),
423                 dbg_cx: dbg_cx,
424                 eh_personality: RefCell::new(None),
425                 intrinsics: RefCell::new(FnvHashMap()),
426                 n_llvm_insns: Cell::new(0),
427                 trait_cache: RefCell::new(FnvHashMap()),
428             };
429
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));
432
433             // Done mutating local_ccx directly.  (The rest of the
434             // initialization goes through RefCell.)
435             {
436                 let ccx = local_ccx.dummy_ccx(shared);
437
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);
441
442                 ccx.tn().associate_type("tydesc", &Type::tydesc(&ccx, str_slice_ty));
443
444                 if ccx.sess().count_llvm_insns() {
445                     base::init_insn_ctxt()
446                 }
447             }
448
449             local_ccx
450         }
451     }
452
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.
457     ///
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> {
463         CrateContext {
464             shared: shared,
465             local: self,
466             index: -1 as uint,
467         }
468     }
469 }
470
471 impl<'b, 'tcx> CrateContext<'b, 'tcx> {
472     pub fn shared(&self) -> &'b SharedCrateContext<'tcx> {
473         self.shared
474     }
475
476     pub fn local(&self) -> &'b LocalCrateContext<'tcx> {
477         self.local
478     }
479
480
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()
485     }
486
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
492     /// context.
493     pub fn maybe_iter(&self, iter_all: bool) -> CrateContextMaybeIterator<'b, 'tcx> {
494         CrateContextMaybeIterator {
495             shared: self.shared,
496             index: if iter_all { 0 } else { self.index },
497             single: !iter_all,
498             origin: self.index,
499         }
500     }
501
502
503     pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
504         &self.shared.tcx
505     }
506
507     pub fn sess<'a>(&'a self) -> &'a Session {
508         &self.shared.tcx.sess
509     }
510
511     pub fn builder<'a>(&'a self) -> Builder<'a, 'tcx> {
512         Builder::new(self)
513     }
514
515     pub fn raw_builder<'a>(&'a self) -> BuilderRef {
516         self.local.builder.b
517     }
518
519     pub fn tydesc_type(&self) -> Type {
520         self.local.tn.find_type("tydesc").unwrap()
521     }
522
523     pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
524         if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
525             return v;
526         }
527         match declare_intrinsic(self, key) {
528             Some(v) => return v,
529             None => panic!()
530         }
531     }
532
533     pub fn is_split_stack_supported(&self) -> bool {
534         self.sess().target.target.options.morestack
535     }
536
537
538     pub fn llmod(&self) -> ModuleRef {
539         self.local.llmod
540     }
541
542     pub fn llcx(&self) -> ContextRef {
543         self.local.llcx
544     }
545
546     pub fn td<'a>(&'a self) -> &'a TargetData {
547         &self.local.td
548     }
549
550     pub fn tn<'a>(&'a self) -> &'a TypeNames {
551         &self.local.tn
552     }
553
554     pub fn externs<'a>(&'a self) -> &'a RefCell<ExternMap> {
555         &self.local.externs
556     }
557
558     pub fn item_vals<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
559         &self.local.item_vals
560     }
561
562     pub fn export_map<'a>(&'a self) -> &'a ExportMap {
563         &self.shared.export_map
564     }
565
566     pub fn reachable<'a>(&'a self) -> &'a NodeSet {
567         &self.shared.reachable
568     }
569
570     pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
571         &self.shared.item_symbols
572     }
573
574     pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
575         &self.shared.link_meta
576     }
577
578     pub fn needs_unwind_cleanup_cache(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, bool>> {
579         &self.local.needs_unwind_cleanup_cache
580     }
581
582     pub fn fn_pointer_shims(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, ValueRef>> {
583         &self.local.fn_pointer_shims
584     }
585
586     pub fn drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, ValueRef>> {
587         &self.local.drop_glues
588     }
589
590     pub fn tydescs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<tydesc_info<'tcx>>>> {
591         &self.local.tydescs
592     }
593
594     pub fn finished_tydescs<'a>(&'a self) -> &'a Cell<bool> {
595         &self.local.finished_tydescs
596     }
597
598     pub fn external<'a>(&'a self) -> &'a RefCell<DefIdMap<Option<ast::NodeId>>> {
599         &self.local.external
600     }
601
602     pub fn external_srcs<'a>(&'a self) -> &'a RefCell<NodeMap<ast::DefId>> {
603         &self.local.external_srcs
604     }
605
606     pub fn monomorphized<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>> {
607         &self.local.monomorphized
608     }
609
610     pub fn monomorphizing<'a>(&'a self) -> &'a RefCell<DefIdMap<uint>> {
611         &self.local.monomorphizing
612     }
613
614     pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<(Ty<'tcx>, ty::PolyTraitRef<'tcx>),
615                                                             ValueRef>> {
616         &self.local.vtables
617     }
618
619     pub fn const_cstr_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<InternedString, ValueRef>> {
620         &self.local.const_cstr_cache
621     }
622
623     pub fn const_unsized<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
624         &self.local.const_unsized
625     }
626
627     pub fn const_globals<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
628         &self.local.const_globals
629     }
630
631     pub fn const_values<'a>(&'a self) -> &'a RefCell<FnvHashMap<(ast::NodeId, &'tcx Substs<'tcx>),
632                                                                 ValueRef>> {
633         &self.local.const_values
634     }
635
636     pub fn static_values<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
637         &self.local.static_values
638     }
639
640     pub fn extern_const_values<'a>(&'a self) -> &'a RefCell<DefIdMap<ValueRef>> {
641         &self.local.extern_const_values
642     }
643
644     pub fn impl_method_cache<'a>(&'a self)
645             -> &'a RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>> {
646         &self.local.impl_method_cache
647     }
648
649     pub fn closure_bare_wrapper_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
650         &self.local.closure_bare_wrapper_cache
651     }
652
653     pub fn lltypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
654         &self.local.lltypes
655     }
656
657     pub fn llsizingtypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
658         &self.local.llsizingtypes
659     }
660
661     pub fn adt_reprs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>> {
662         &self.local.adt_reprs
663     }
664
665     pub fn symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
666         &self.shared.symbol_hasher
667     }
668
669     pub fn type_hashcodes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
670         &self.local.type_hashcodes
671     }
672
673     pub fn all_llvm_symbols<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
674         &self.local.all_llvm_symbols
675     }
676
677     pub fn stats<'a>(&'a self) -> &'a Stats {
678         &self.shared.stats
679     }
680
681     pub fn available_monomorphizations<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
682         &self.shared.available_monomorphizations
683     }
684
685     pub fn available_drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
686         &self.shared.available_drop_glues
687     }
688
689     pub fn int_type(&self) -> Type {
690         self.local.int_type
691     }
692
693     pub fn opaque_vec_type(&self) -> Type {
694         self.local.opaque_vec_type
695     }
696
697     pub fn closure_vals<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>> {
698         &self.local.closure_vals
699     }
700
701     pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext<'tcx>> {
702         &self.local.dbg_cx
703     }
704
705     pub fn eh_personality<'a>(&'a self) -> &'a RefCell<Option<ValueRef>> {
706         &self.local.eh_personality
707     }
708
709     fn intrinsics<'a>(&'a self) -> &'a RefCell<FnvHashMap<&'static str, ValueRef>> {
710         &self.local.intrinsics
711     }
712
713     pub fn count_llvm_insn(&self) {
714         self.local.n_llvm_insns.set(self.local.n_llvm_insns.get() + 1);
715     }
716
717     pub fn trait_cache(&self) -> &RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>,
718                                                      traits::Vtable<'tcx, ()>>> {
719         &self.local.trait_cache
720     }
721
722     /// Return exclusive upper bound on object size.
723     ///
724     /// The theoretical maximum object size is defined as the maximum positive `int` value. This
725     /// ensures that the `offset` semantics remain well-defined by allowing it to correctly index
726     /// every address within an object along with one byte past the end, along with allowing `int`
727     /// to store the difference between any two pointers into an object.
728     ///
729     /// The upper bound on 64-bit currently needs to be lower because LLVM uses a 64-bit integer to
730     /// represent object size in bits. It would need to be 1 << 61 to account for this, but is
731     /// currently conservatively bounded to 1 << 47 as that is enough to cover the current usable
732     /// address space on 64-bit ARMv8 and x86_64.
733     pub fn obj_size_bound(&self) -> u64 {
734         match &self.sess().target.target.target_pointer_width[] {
735             "32" => 1 << 31,
736             "64" => 1 << 47,
737             _ => unreachable!() // error handled by config::build_target_config
738         }
739     }
740
741     pub fn report_overbig_object(&self, obj: Ty<'tcx>) -> ! {
742         self.sess().fatal(
743             &format!("the type `{}` is too big for the current architecture",
744                     obj.repr(self.tcx()))[])
745     }
746 }
747
748 fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
749     macro_rules! ifn {
750         ($name:expr, fn() -> $ret:expr) => (
751             if *key == $name {
752                 let f = base::decl_cdecl_fn(
753                     ccx, $name, Type::func(&[], &$ret),
754                     ty::mk_nil(ccx.tcx()));
755                 ccx.intrinsics().borrow_mut().insert($name, f.clone());
756                 return Some(f);
757             }
758         );
759         ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
760             if *key == $name {
761                 let f = base::decl_cdecl_fn(ccx, $name,
762                                   Type::func(&[$($arg),*], &$ret), ty::mk_nil(ccx.tcx()));
763                 ccx.intrinsics().borrow_mut().insert($name, f.clone());
764                 return Some(f);
765             }
766         )
767     }
768     macro_rules! mk_struct {
769         ($($field_ty:expr),*) => (Type::struct_(ccx, &[$($field_ty),*], false))
770     }
771
772     let i8p = Type::i8p(ccx);
773     let void = Type::void(ccx);
774     let i1 = Type::i1(ccx);
775     let t_i8 = Type::i8(ccx);
776     let t_i16 = Type::i16(ccx);
777     let t_i32 = Type::i32(ccx);
778     let t_i64 = Type::i64(ccx);
779     let t_f32 = Type::f32(ccx);
780     let t_f64 = Type::f64(ccx);
781
782     ifn!("llvm.memcpy.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
783     ifn!("llvm.memcpy.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
784     ifn!("llvm.memmove.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
785     ifn!("llvm.memmove.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
786     ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
787     ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
788
789     ifn!("llvm.trap", fn() -> void);
790     ifn!("llvm.debugtrap", fn() -> void);
791     ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
792
793     ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
794     ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
795     ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
796     ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
797
798     ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
799     ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
800     ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
801     ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
802     ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
803     ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
804     ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
805     ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
806     ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
807     ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
808     ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
809     ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
810     ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
811     ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
812     ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
813     ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
814
815     ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
816     ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
817
818     ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
819     ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
820
821     ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
822     ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
823     ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
824     ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
825     ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
826     ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
827
828     ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
829     ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
830     ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
831     ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
832
833     ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
834     ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
835     ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
836     ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
837
838     ifn!("llvm.ctlz.i8", fn(t_i8 , i1) -> t_i8);
839     ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
840     ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
841     ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
842
843     ifn!("llvm.cttz.i8", fn(t_i8 , i1) -> t_i8);
844     ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
845     ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
846     ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
847
848     ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
849     ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
850     ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
851
852     ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
853     ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
854     ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
855     ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
856
857     ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
858     ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
859     ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
860     ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
861
862     ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
863     ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
864     ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
865     ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
866
867     ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
868     ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
869     ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
870     ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
871
872     ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
873     ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
874     ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
875     ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
876
877     ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
878     ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
879     ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
880     ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
881
882     ifn!("llvm.lifetime.start", fn(t_i64,i8p) -> void);
883     ifn!("llvm.lifetime.end", fn(t_i64, i8p) -> void);
884
885     ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
886     ifn!("llvm.assume", fn(i1) -> void);
887
888     // Some intrinsics were introduced in later versions of LLVM, but they have
889     // fallbacks in libc or libm and such. Currently, all of these intrinsics
890     // were introduced in LLVM 3.4, so we case on that.
891     macro_rules! compatible_ifn {
892         ($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr) => (
893             if unsafe { llvm::LLVMVersionMinor() >= 4 } {
894                 // The `if key == $name` is already in ifn!
895                 ifn!($name, fn($($arg),*) -> $ret);
896             } else if *key == $name {
897                 let f = base::decl_cdecl_fn(ccx, stringify!($cname),
898                                       Type::func(&[$($arg),*], &$ret),
899                                       ty::mk_nil(ccx.tcx()));
900                 ccx.intrinsics().borrow_mut().insert($name, f.clone());
901                 return Some(f);
902             }
903         )
904     }
905
906     compatible_ifn!("llvm.copysign.f32", copysignf(t_f32, t_f32) -> t_f32);
907     compatible_ifn!("llvm.copysign.f64", copysign(t_f64, t_f64) -> t_f64);
908     compatible_ifn!("llvm.round.f32", roundf(t_f32) -> t_f32);
909     compatible_ifn!("llvm.round.f64", round(t_f64) -> t_f64);
910
911
912     if ccx.sess().opts.debuginfo != NoDebugInfo {
913         ifn!("llvm.dbg.declare", fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
914         ifn!("llvm.dbg.value", fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);
915     }
916     return None;
917 }