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