]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/context.rs
librustc: Stop generating visit glue and remove from TyDesc.
[rust.git] / src / librustc / middle / 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 driver::config::NoDebugInfo;
12 use driver::session::Session;
13 use llvm;
14 use llvm::{ContextRef, ModuleRef, ValueRef, BuilderRef};
15 use llvm::{TargetData};
16 use llvm::mk_target_data;
17 use metadata::common::LinkMeta;
18 use middle::resolve;
19 use middle::traits;
20 use middle::trans::adt;
21 use middle::trans::base;
22 use middle::trans::builder::Builder;
23 use middle::trans::common::{ExternMap,tydesc_info,BuilderRef_res};
24 use middle::trans::debuginfo;
25 use middle::trans::monomorphize::MonoId;
26 use middle::trans::type_::{Type, TypeNames};
27 use middle::ty;
28 use util::sha2::Sha256;
29 use util::nodemap::{NodeMap, NodeSet, DefIdMap};
30
31 use std::cell::{Cell, RefCell};
32 use std::c_str::ToCStr;
33 use std::ptr;
34 use std::rc::Rc;
35 use std::collections::{HashMap, HashSet};
36 use syntax::abi;
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<HashMap<String, uint>>,
51     // (ident, time-in-ms, llvm-instructions)
52     pub fn_stats: RefCell<Vec<(String, uint, 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>,
61
62     metadata_llmod: ModuleRef,
63     metadata_llcx: ContextRef,
64
65     exp_map2: resolve::ExportMap2,
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<HashSet<String>>,
74     available_drop_glues: RefCell<HashMap<ty::t, 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 {
82     llmod: ModuleRef,
83     llcx: ContextRef,
84     td: TargetData,
85     tn: TypeNames,
86     externs: RefCell<ExternMap>,
87     item_vals: RefCell<NodeMap<ValueRef>>,
88     drop_glues: RefCell<HashMap<ty::t, ValueRef>>,
89     tydescs: RefCell<HashMap<ty::t, Rc<tydesc_info>>>,
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<HashMap<MonoId, ValueRef>>,
100     monomorphizing: RefCell<DefIdMap<uint>>,
101     /// Cache generated vtables
102     vtables: RefCell<HashMap<(ty::t,Rc<ty::TraitRef>), ValueRef>>,
103     /// Cache of constant strings,
104     const_cstr_cache: RefCell<HashMap<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<HashMap<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<HashMap<(ast::DefId, ast::Name), ast::DefId>>,
126
127     /// Cache of closure wrappers for bare fn's.
128     closure_bare_wrapper_cache: RefCell<HashMap<ValueRef, ValueRef>>,
129
130     lltypes: RefCell<HashMap<ty::t, Type>>,
131     llsizingtypes: RefCell<HashMap<ty::t, Type>>,
132     adt_reprs: RefCell<HashMap<ty::t, Rc<adt::Repr>>>,
133     type_hashcodes: RefCell<HashMap<ty::t, String>>,
134     all_llvm_symbols: RefCell<HashSet<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<DefIdMap<ValueRef>>,
141
142     dbg_cx: Option<debuginfo::CrateDebugContext>,
143
144     eh_personality: RefCell<Option<ValueRef>>,
145
146     intrinsics: RefCell<HashMap<&'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<HashMap<Rc<ty::TraitRef>,
154                                  traits::Vtable<()>>>,
155 }
156
157 pub struct CrateContext<'a, 'tcx: 'a> {
158     shared: &'a SharedCrateContext<'tcx>,
159     local: &'a LocalCrateContext,
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.targ_cfg
223         .target_strs
224         .data_layout
225         .as_slice()
226         .with_c_str(|buf| {
227         llvm::LLVMSetDataLayout(llmod, buf);
228     });
229     sess.targ_cfg
230         .target_strs
231         .target_triple
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(HashMap::new()),
273                 fn_stats: RefCell::new(Vec::new()),
274             },
275             available_monomorphizations: RefCell::new(HashSet::new()),
276             available_drop_glues: RefCell::new(HashMap::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 symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
351         &self.symbol_hasher
352     }
353
354     pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
355         &self.tcx
356     }
357
358     pub fn take_tcx(self) -> ty::ctxt<'tcx> {
359         self.tcx
360     }
361
362     pub fn sess<'a>(&'a self) -> &'a Session {
363         &self.tcx.sess
364     }
365
366     pub fn stats<'a>(&'a self) -> &'a Stats {
367         &self.stats
368     }
369 }
370
371 impl LocalCrateContext {
372     fn new(shared: &SharedCrateContext,
373            name: &str)
374            -> LocalCrateContext {
375         unsafe {
376             let (llcx, llmod) = create_context_and_module(&shared.tcx.sess, name);
377
378             let td = mk_target_data(shared.tcx
379                                           .sess
380                                           .targ_cfg
381                                           .target_strs
382                                           .data_layout
383                                           .as_slice());
384
385             let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo {
386                 Some(debuginfo::CrateDebugContext::new(llmod))
387             } else {
388                 None
389             };
390
391             let mut local_ccx = LocalCrateContext {
392                 llmod: llmod,
393                 llcx: llcx,
394                 td: td,
395                 tn: TypeNames::new(),
396                 externs: RefCell::new(HashMap::new()),
397                 item_vals: RefCell::new(NodeMap::new()),
398                 drop_glues: RefCell::new(HashMap::new()),
399                 tydescs: RefCell::new(HashMap::new()),
400                 finished_tydescs: Cell::new(false),
401                 external: RefCell::new(DefIdMap::new()),
402                 external_srcs: RefCell::new(NodeMap::new()),
403                 monomorphized: RefCell::new(HashMap::new()),
404                 monomorphizing: RefCell::new(DefIdMap::new()),
405                 vtables: RefCell::new(HashMap::new()),
406                 const_cstr_cache: RefCell::new(HashMap::new()),
407                 const_globals: RefCell::new(HashMap::new()),
408                 const_values: RefCell::new(NodeMap::new()),
409                 static_values: RefCell::new(NodeMap::new()),
410                 extern_const_values: RefCell::new(DefIdMap::new()),
411                 impl_method_cache: RefCell::new(HashMap::new()),
412                 closure_bare_wrapper_cache: RefCell::new(HashMap::new()),
413                 lltypes: RefCell::new(HashMap::new()),
414                 llsizingtypes: RefCell::new(HashMap::new()),
415                 adt_reprs: RefCell::new(HashMap::new()),
416                 type_hashcodes: RefCell::new(HashMap::new()),
417                 all_llvm_symbols: RefCell::new(HashSet::new()),
418                 int_type: Type::from_ref(ptr::null_mut()),
419                 opaque_vec_type: Type::from_ref(ptr::null_mut()),
420                 builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)),
421                 unboxed_closure_vals: RefCell::new(DefIdMap::new()),
422                 dbg_cx: dbg_cx,
423                 eh_personality: RefCell::new(None),
424                 intrinsics: RefCell::new(HashMap::new()),
425                 n_llvm_insns: Cell::new(0u),
426                 trait_cache: RefCell::new(HashMap::new()),
427             };
428
429             local_ccx.int_type = Type::int(&local_ccx.dummy_ccx(shared));
430             local_ccx.opaque_vec_type = Type::opaque_vec(&local_ccx.dummy_ccx(shared));
431
432             // Done mutating local_ccx directly.  (The rest of the
433             // initialization goes through RefCell.)
434             {
435                 let ccx = local_ccx.dummy_ccx(shared);
436
437                 let mut str_slice_ty = Type::named_struct(&ccx, "str_slice");
438                 str_slice_ty.set_struct_body([Type::i8p(&ccx), ccx.int_type()], false);
439                 ccx.tn().associate_type("str_slice", &str_slice_ty);
440
441                 ccx.tn().associate_type("tydesc", &Type::tydesc(&ccx, str_slice_ty));
442
443                 if ccx.sess().count_llvm_insns() {
444                     base::init_insn_ctxt()
445                 }
446             }
447
448             local_ccx
449         }
450     }
451
452     /// Create a dummy `CrateContext` from `self` and  the provided
453     /// `SharedCrateContext`.  This is somewhat dangerous because `self` may
454     /// not actually be an element of `shared.local_ccxs`, which can cause some
455     /// operations to `fail` unexpectedly.
456     ///
457     /// This is used in the `LocalCrateContext` constructor to allow calling
458     /// functions that expect a complete `CrateContext`, even before the local
459     /// portion is fully initialized and attached to the `SharedCrateContext`.
460     fn dummy_ccx<'a, 'tcx>(&'a self, shared: &'a SharedCrateContext<'tcx>)
461                            -> CrateContext<'a, 'tcx> {
462         CrateContext {
463             shared: shared,
464             local: self,
465             index: -1 as uint,
466         }
467     }
468 }
469
470 impl<'b, 'tcx> CrateContext<'b, 'tcx> {
471     pub fn shared(&self) -> &'b SharedCrateContext<'tcx> {
472         self.shared
473     }
474
475     pub fn local(&self) -> &'b LocalCrateContext {
476         self.local
477     }
478
479
480     /// Get a (possibly) different `CrateContext` from the same
481     /// `SharedCrateContext`.
482     pub fn rotate(&self) -> CrateContext<'b, 'tcx> {
483         self.shared.get_smallest_ccx()
484     }
485
486     /// Either iterate over only `self`, or iterate over all `CrateContext`s in
487     /// the `SharedCrateContext`.  The iterator produces `(ccx, is_origin)`
488     /// pairs, where `is_origin` is `true` if `ccx` is `self` and `false`
489     /// otherwise.  This method is useful for avoiding code duplication in
490     /// cases where it may or may not be necessary to translate code into every
491     /// context.
492     pub fn maybe_iter(&self, iter_all: bool) -> CrateContextMaybeIterator<'b, 'tcx> {
493         CrateContextMaybeIterator {
494             shared: self.shared,
495             index: if iter_all { 0 } else { self.index },
496             single: !iter_all,
497             origin: self.index,
498         }
499     }
500
501
502     pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
503         &self.shared.tcx
504     }
505
506     pub fn sess<'a>(&'a self) -> &'a Session {
507         &self.shared.tcx.sess
508     }
509
510     pub fn builder<'a>(&'a self) -> Builder<'a, 'tcx> {
511         Builder::new(self)
512     }
513
514     pub fn raw_builder<'a>(&'a self) -> BuilderRef {
515         self.local.builder.b
516     }
517
518     pub fn tydesc_type(&self) -> Type {
519         self.local.tn.find_type("tydesc").unwrap()
520     }
521
522     pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
523         match self.intrinsics().borrow().find_copy(key) {
524             Some(v) => return v,
525             _ => {}
526         }
527         match declare_intrinsic(self, key) {
528             Some(v) => return v,
529             None => fail!()
530         }
531     }
532
533     // Although there is an experimental implementation of LLVM which
534     // supports SS on armv7 it wasn't approved by Apple, see:
535     // http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140505/216350.html
536     // It looks like it might be never accepted to upstream LLVM.
537     //
538     // So far the decision was to disable them in default builds
539     // but it could be enabled (with patched LLVM)
540     pub fn is_split_stack_supported(&self) -> bool {
541         let ref cfg = self.sess().targ_cfg;
542         (cfg.os != abi::OsiOS || cfg.arch != abi::Arm) && cfg.os != abi::OsWindows
543     }
544
545
546     pub fn llmod(&self) -> ModuleRef {
547         self.local.llmod
548     }
549
550     pub fn llcx(&self) -> ContextRef {
551         self.local.llcx
552     }
553
554     pub fn td<'a>(&'a self) -> &'a TargetData {
555         &self.local.td
556     }
557
558     pub fn tn<'a>(&'a self) -> &'a TypeNames {
559         &self.local.tn
560     }
561
562     pub fn externs<'a>(&'a self) -> &'a RefCell<ExternMap> {
563         &self.local.externs
564     }
565
566     pub fn item_vals<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
567         &self.local.item_vals
568     }
569
570     pub fn exp_map2<'a>(&'a self) -> &'a resolve::ExportMap2 {
571         &self.shared.exp_map2
572     }
573
574     pub fn reachable<'a>(&'a self) -> &'a NodeSet {
575         &self.shared.reachable
576     }
577
578     pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
579         &self.shared.item_symbols
580     }
581
582     pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
583         &self.shared.link_meta
584     }
585
586     pub fn drop_glues<'a>(&'a self) -> &'a RefCell<HashMap<ty::t, ValueRef>> {
587         &self.local.drop_glues
588     }
589
590     pub fn tydescs<'a>(&'a self) -> &'a RefCell<HashMap<ty::t, Rc<tydesc_info>>> {
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<HashMap<MonoId, 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<HashMap<(ty::t,Rc<ty::TraitRef>), ValueRef>> {
615         &self.local.vtables
616     }
617
618     pub fn const_cstr_cache<'a>(&'a self) -> &'a RefCell<HashMap<InternedString, ValueRef>> {
619         &self.local.const_cstr_cache
620     }
621
622     pub fn const_globals<'a>(&'a self) -> &'a RefCell<HashMap<int, ValueRef>> {
623         &self.local.const_globals
624     }
625
626     pub fn const_values<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
627         &self.local.const_values
628     }
629
630     pub fn static_values<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
631         &self.local.static_values
632     }
633
634     pub fn extern_const_values<'a>(&'a self) -> &'a RefCell<DefIdMap<ValueRef>> {
635         &self.local.extern_const_values
636     }
637
638     pub fn impl_method_cache<'a>(&'a self)
639             -> &'a RefCell<HashMap<(ast::DefId, ast::Name), ast::DefId>> {
640         &self.local.impl_method_cache
641     }
642
643     pub fn closure_bare_wrapper_cache<'a>(&'a self) -> &'a RefCell<HashMap<ValueRef, ValueRef>> {
644         &self.local.closure_bare_wrapper_cache
645     }
646
647     pub fn lltypes<'a>(&'a self) -> &'a RefCell<HashMap<ty::t, Type>> {
648         &self.local.lltypes
649     }
650
651     pub fn llsizingtypes<'a>(&'a self) -> &'a RefCell<HashMap<ty::t, Type>> {
652         &self.local.llsizingtypes
653     }
654
655     pub fn adt_reprs<'a>(&'a self) -> &'a RefCell<HashMap<ty::t, Rc<adt::Repr>>> {
656         &self.local.adt_reprs
657     }
658
659     pub fn symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
660         &self.shared.symbol_hasher
661     }
662
663     pub fn type_hashcodes<'a>(&'a self) -> &'a RefCell<HashMap<ty::t, String>> {
664         &self.local.type_hashcodes
665     }
666
667     pub fn all_llvm_symbols<'a>(&'a self) -> &'a RefCell<HashSet<String>> {
668         &self.local.all_llvm_symbols
669     }
670
671     pub fn stats<'a>(&'a self) -> &'a Stats {
672         &self.shared.stats
673     }
674
675     pub fn available_monomorphizations<'a>(&'a self) -> &'a RefCell<HashSet<String>> {
676         &self.shared.available_monomorphizations
677     }
678
679     pub fn available_drop_glues<'a>(&'a self) -> &'a RefCell<HashMap<ty::t, String>> {
680         &self.shared.available_drop_glues
681     }
682
683     pub fn int_type(&self) -> Type {
684         self.local.int_type
685     }
686
687     pub fn opaque_vec_type(&self) -> Type {
688         self.local.opaque_vec_type
689     }
690
691     pub fn unboxed_closure_vals<'a>(&'a self) -> &'a RefCell<DefIdMap<ValueRef>> {
692         &self.local.unboxed_closure_vals
693     }
694
695     pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext> {
696         &self.local.dbg_cx
697     }
698
699     pub fn eh_personality<'a>(&'a self) -> &'a RefCell<Option<ValueRef>> {
700         &self.local.eh_personality
701     }
702
703     fn intrinsics<'a>(&'a self) -> &'a RefCell<HashMap<&'static str, ValueRef>> {
704         &self.local.intrinsics
705     }
706
707     pub fn count_llvm_insn(&self) {
708         self.local.n_llvm_insns.set(self.local.n_llvm_insns.get() + 1);
709     }
710
711     pub fn trait_cache(&self) -> &RefCell<HashMap<Rc<ty::TraitRef>, traits::Vtable<()>>> {
712         &self.local.trait_cache
713     }
714 }
715
716 fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
717     macro_rules! ifn (
718         ($name:expr fn() -> $ret:expr) => (
719             if *key == $name {
720                 let f = base::decl_cdecl_fn(ccx, $name, Type::func([], &$ret), ty::mk_nil());
721                 ccx.intrinsics().borrow_mut().insert($name, f.clone());
722                 return Some(f);
723             }
724         );
725         ($name:expr fn($($arg:expr),*) -> $ret:expr) => (
726             if *key == $name {
727                 let f = base::decl_cdecl_fn(ccx, $name,
728                                   Type::func([$($arg),*], &$ret), ty::mk_nil());
729                 ccx.intrinsics().borrow_mut().insert($name, f.clone());
730                 return Some(f);
731             }
732         )
733     )
734     macro_rules! mk_struct (
735         ($($field_ty:expr),*) => (Type::struct_(ccx, [$($field_ty),*], false))
736     )
737
738     let i8p = Type::i8p(ccx);
739     let void = Type::void(ccx);
740     let i1 = Type::i1(ccx);
741     let t_i8 = Type::i8(ccx);
742     let t_i16 = Type::i16(ccx);
743     let t_i32 = Type::i32(ccx);
744     let t_i64 = Type::i64(ccx);
745     let t_f32 = Type::f32(ccx);
746     let t_f64 = Type::f64(ccx);
747
748     ifn!("llvm.memcpy.p0i8.p0i8.i32" fn(i8p, i8p, t_i32, t_i32, i1) -> void);
749     ifn!("llvm.memcpy.p0i8.p0i8.i64" fn(i8p, i8p, t_i64, t_i32, i1) -> void);
750     ifn!("llvm.memmove.p0i8.p0i8.i32" fn(i8p, i8p, t_i32, t_i32, i1) -> void);
751     ifn!("llvm.memmove.p0i8.p0i8.i64" fn(i8p, i8p, t_i64, t_i32, i1) -> void);
752     ifn!("llvm.memset.p0i8.i32" fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
753     ifn!("llvm.memset.p0i8.i64" fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
754
755     ifn!("llvm.trap" fn() -> void);
756     ifn!("llvm.debugtrap" fn() -> void);
757     ifn!("llvm.frameaddress" fn(t_i32) -> i8p);
758
759     ifn!("llvm.powi.f32" fn(t_f32, t_i32) -> t_f32);
760     ifn!("llvm.powi.f64" fn(t_f64, t_i32) -> t_f64);
761     ifn!("llvm.pow.f32" fn(t_f32, t_f32) -> t_f32);
762     ifn!("llvm.pow.f64" fn(t_f64, t_f64) -> t_f64);
763
764     ifn!("llvm.sqrt.f32" fn(t_f32) -> t_f32);
765     ifn!("llvm.sqrt.f64" fn(t_f64) -> t_f64);
766     ifn!("llvm.sin.f32" fn(t_f32) -> t_f32);
767     ifn!("llvm.sin.f64" fn(t_f64) -> t_f64);
768     ifn!("llvm.cos.f32" fn(t_f32) -> t_f32);
769     ifn!("llvm.cos.f64" fn(t_f64) -> t_f64);
770     ifn!("llvm.exp.f32" fn(t_f32) -> t_f32);
771     ifn!("llvm.exp.f64" fn(t_f64) -> t_f64);
772     ifn!("llvm.exp2.f32" fn(t_f32) -> t_f32);
773     ifn!("llvm.exp2.f64" fn(t_f64) -> t_f64);
774     ifn!("llvm.log.f32" fn(t_f32) -> t_f32);
775     ifn!("llvm.log.f64" fn(t_f64) -> t_f64);
776     ifn!("llvm.log10.f32" fn(t_f32) -> t_f32);
777     ifn!("llvm.log10.f64" fn(t_f64) -> t_f64);
778     ifn!("llvm.log2.f32" fn(t_f32) -> t_f32);
779     ifn!("llvm.log2.f64" fn(t_f64) -> t_f64);
780
781     ifn!("llvm.fma.f32" fn(t_f32, t_f32, t_f32) -> t_f32);
782     ifn!("llvm.fma.f64" fn(t_f64, t_f64, t_f64) -> t_f64);
783
784     ifn!("llvm.fabs.f32" fn(t_f32) -> t_f32);
785     ifn!("llvm.fabs.f64" fn(t_f64) -> t_f64);
786
787     ifn!("llvm.floor.f32" fn(t_f32) -> t_f32);
788     ifn!("llvm.floor.f64" fn(t_f64) -> t_f64);
789     ifn!("llvm.ceil.f32" fn(t_f32) -> t_f32);
790     ifn!("llvm.ceil.f64" fn(t_f64) -> t_f64);
791     ifn!("llvm.trunc.f32" fn(t_f32) -> t_f32);
792     ifn!("llvm.trunc.f64" fn(t_f64) -> t_f64);
793
794     ifn!("llvm.rint.f32" fn(t_f32) -> t_f32);
795     ifn!("llvm.rint.f64" fn(t_f64) -> t_f64);
796     ifn!("llvm.nearbyint.f32" fn(t_f32) -> t_f32);
797     ifn!("llvm.nearbyint.f64" fn(t_f64) -> t_f64);
798
799     ifn!("llvm.ctpop.i8" fn(t_i8) -> t_i8);
800     ifn!("llvm.ctpop.i16" fn(t_i16) -> t_i16);
801     ifn!("llvm.ctpop.i32" fn(t_i32) -> t_i32);
802     ifn!("llvm.ctpop.i64" fn(t_i64) -> t_i64);
803
804     ifn!("llvm.ctlz.i8" fn(t_i8 , i1) -> t_i8);
805     ifn!("llvm.ctlz.i16" fn(t_i16, i1) -> t_i16);
806     ifn!("llvm.ctlz.i32" fn(t_i32, i1) -> t_i32);
807     ifn!("llvm.ctlz.i64" fn(t_i64, i1) -> t_i64);
808
809     ifn!("llvm.cttz.i8" fn(t_i8 , i1) -> t_i8);
810     ifn!("llvm.cttz.i16" fn(t_i16, i1) -> t_i16);
811     ifn!("llvm.cttz.i32" fn(t_i32, i1) -> t_i32);
812     ifn!("llvm.cttz.i64" fn(t_i64, i1) -> t_i64);
813
814     ifn!("llvm.bswap.i16" fn(t_i16) -> t_i16);
815     ifn!("llvm.bswap.i32" fn(t_i32) -> t_i32);
816     ifn!("llvm.bswap.i64" fn(t_i64) -> t_i64);
817
818     ifn!("llvm.sadd.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
819     ifn!("llvm.sadd.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
820     ifn!("llvm.sadd.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
821     ifn!("llvm.sadd.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
822
823     ifn!("llvm.uadd.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
824     ifn!("llvm.uadd.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
825     ifn!("llvm.uadd.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
826     ifn!("llvm.uadd.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
827
828     ifn!("llvm.ssub.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
829     ifn!("llvm.ssub.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
830     ifn!("llvm.ssub.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
831     ifn!("llvm.ssub.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
832
833     ifn!("llvm.usub.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
834     ifn!("llvm.usub.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
835     ifn!("llvm.usub.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
836     ifn!("llvm.usub.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
837
838     ifn!("llvm.smul.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
839     ifn!("llvm.smul.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
840     ifn!("llvm.smul.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
841     ifn!("llvm.smul.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
842
843     ifn!("llvm.umul.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
844     ifn!("llvm.umul.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
845     ifn!("llvm.umul.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
846     ifn!("llvm.umul.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
847
848     ifn!("llvm.lifetime.start" fn(t_i64,i8p) -> void);
849     ifn!("llvm.lifetime.end" fn(t_i64, i8p) -> void);
850
851     ifn!("llvm.expect.i1" fn(i1, i1) -> i1);
852
853     // Some intrinsics were introduced in later versions of LLVM, but they have
854     // fallbacks in libc or libm and such. Currently, all of these intrinsics
855     // were introduced in LLVM 3.4, so we case on that.
856     macro_rules! compatible_ifn (
857         ($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr) => (
858             if unsafe { llvm::LLVMVersionMinor() >= 4 } {
859                 // The `if key == $name` is already in ifn!
860                 ifn!($name fn($($arg),*) -> $ret);
861             } else if *key == $name {
862                 let f = base::decl_cdecl_fn(ccx, stringify!($cname),
863                                       Type::func([$($arg),*], &$ret),
864                                       ty::mk_nil());
865                 ccx.intrinsics().borrow_mut().insert($name, f.clone());
866                 return Some(f);
867             }
868         )
869     )
870
871     compatible_ifn!("llvm.copysign.f32", copysignf(t_f32, t_f32) -> t_f32);
872     compatible_ifn!("llvm.copysign.f64", copysign(t_f64, t_f64) -> t_f64);
873     compatible_ifn!("llvm.round.f32", roundf(t_f32) -> t_f32);
874     compatible_ifn!("llvm.round.f64", round(t_f64) -> t_f64);
875
876
877     if ccx.sess().opts.debuginfo != NoDebugInfo {
878         ifn!("llvm.dbg.declare" fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
879         ifn!("llvm.dbg.value" fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);
880     }
881     return None;
882 }