]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/trans/context.rs
0ae69682f914ac591276604014a3ec698dea5ebf
[rustc.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,BuilderRef_res};
22 use trans::debuginfo;
23 use trans::declare;
24 use trans::glue::DropGlueKind;
25 use trans::monomorphize::MonoId;
26 use trans::type_::{Type, TypeNames};
27 use middle::subst::Substs;
28 use middle::ty::{self, Ty};
29 use session::config::NoDebugInfo;
30 use session::Session;
31 use util::sha2::Sha256;
32 use util::nodemap::{NodeMap, NodeSet, DefIdMap, FnvHashMap, FnvHashSet};
33
34 use std::ffi::CString;
35 use std::cell::{Cell, RefCell};
36 use std::ptr;
37 use std::rc::Rc;
38 use syntax::ast;
39 use syntax::parse::token::InternedString;
40
41 pub struct Stats {
42 pub n_glues_created: Cell<usize>,
43 pub n_null_glues: Cell<usize>,
44 pub n_real_glues: Cell<usize>,
45 pub n_fns: Cell<usize>,
46 pub n_monos: Cell<usize>,
47 pub n_inlines: Cell<usize>,
48 pub n_closures: Cell<usize>,
49 pub n_llvm_insns: Cell<usize>,
50 pub llvm_insns: RefCell<FnvHashMap<String, usize>>,
51 // (ident, llvm-instructions)
52 pub fn_stats: RefCell<Vec<(String, usize)> >,
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<'a, 'tcx: 'a> {
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: &'a ty::ctxt<'tcx>,
71 stats: Stats,
72 check_overflow: bool,
73 check_drop_flag_for_sanity: bool,
74
75 available_drop_glues: RefCell<FnvHashMap<DropGlueKind<'tcx>, String>>,
76 use_dll_storage_attrs: bool,
77 }
78
79 /// The local portion of a `CrateContext`. There is one `LocalCrateContext`
80 /// per compilation unit. Each one has its own LLVM `ContextRef` so that
81 /// several compilation units may be optimized in parallel. All other LLVM
82 /// data structures in the `LocalCrateContext` are tied to that `ContextRef`.
83 pub struct LocalCrateContext<'tcx> {
84 llmod: ModuleRef,
85 llcx: ContextRef,
86 td: TargetData,
87 tn: TypeNames,
88 externs: RefCell<ExternMap>,
89 item_vals: RefCell<NodeMap<ValueRef>>,
90 needs_unwind_cleanup_cache: RefCell<FnvHashMap<Ty<'tcx>, bool>>,
91 fn_pointer_shims: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
92 drop_glues: RefCell<FnvHashMap<DropGlueKind<'tcx>, ValueRef>>,
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<usize>>,
101 available_monomorphizations: RefCell<FnvHashSet<String>>,
102 /// Cache generated vtables
103 vtables: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>>,
104 /// Cache of constant strings,
105 const_cstr_cache: RefCell<FnvHashMap<InternedString, ValueRef>>,
106
107 /// Reverse-direction for const ptrs cast from globals.
108 /// Key is a ValueRef holding a *T,
109 /// Val is a ValueRef holding a *[T].
110 ///
111 /// Needed because LLVM loses pointer->pointee association
112 /// when we ptrcast, and we have to ptrcast during translation
113 /// of a [T] const because we form a slice, a (*T,usize) pair, not
114 /// a pointer to an LLVM array type. Similar for trait objects.
115 const_unsized: RefCell<FnvHashMap<ValueRef, ValueRef>>,
116
117 /// Cache of emitted const globals (value -> global)
118 const_globals: RefCell<FnvHashMap<ValueRef, ValueRef>>,
119
120 /// Cache of emitted const values
121 const_values: RefCell<FnvHashMap<(ast::NodeId, &'tcx Substs<'tcx>), ValueRef>>,
122
123 /// Cache of emitted static values
124 static_values: RefCell<NodeMap<ValueRef>>,
125
126 /// Cache of external const values
127 extern_const_values: RefCell<DefIdMap<ValueRef>>,
128
129 impl_method_cache: RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>>,
130
131 /// Cache of closure wrappers for bare fn's.
132 closure_bare_wrapper_cache: RefCell<FnvHashMap<ValueRef, ValueRef>>,
133
134 lltypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
135 llsizingtypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
136 adt_reprs: RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>>,
137 type_hashcodes: RefCell<FnvHashMap<Ty<'tcx>, String>>,
138 int_type: Type,
139 opaque_vec_type: Type,
140 builder: BuilderRef_res,
141
142 /// Holds the LLVM values for closure IDs.
143 closure_vals: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
144
145 dbg_cx: Option<debuginfo::CrateDebugContext<'tcx>>,
146
147 eh_personality: RefCell<Option<ValueRef>>,
148
149 intrinsics: RefCell<FnvHashMap<&'static str, ValueRef>>,
150
151 /// Number of LLVM instructions translated into this `LocalCrateContext`.
152 /// This is used to perform some basic load-balancing to keep all LLVM
153 /// contexts around the same size.
154 n_llvm_insns: Cell<usize>,
155
156 trait_cache: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>,
157 traits::Vtable<'tcx, ()>>>,
158 }
159
160 pub struct CrateContext<'a, 'tcx: 'a> {
161 shared: &'a SharedCrateContext<'a, 'tcx>,
162 local: &'a LocalCrateContext<'tcx>,
163 /// The index of `local` in `shared.local_ccxs`. This is used in
164 /// `maybe_iter(true)` to identify the original `LocalCrateContext`.
165 index: usize,
166 }
167
168 pub struct CrateContextIterator<'a, 'tcx: 'a> {
169 shared: &'a SharedCrateContext<'a, 'tcx>,
170 index: usize,
171 }
172
173 impl<'a, 'tcx> Iterator for CrateContextIterator<'a,'tcx> {
174 type Item = CrateContext<'a, 'tcx>;
175
176 fn next(&mut self) -> Option<CrateContext<'a, 'tcx>> {
177 if self.index >= self.shared.local_ccxs.len() {
178 return None;
179 }
180
181 let index = self.index;
182 self.index += 1;
183
184 Some(CrateContext {
185 shared: self.shared,
186 local: &self.shared.local_ccxs[index],
187 index: index,
188 })
189 }
190 }
191
192 /// The iterator produced by `CrateContext::maybe_iter`.
193 pub struct CrateContextMaybeIterator<'a, 'tcx: 'a> {
194 shared: &'a SharedCrateContext<'a, 'tcx>,
195 index: usize,
196 single: bool,
197 origin: usize,
198 }
199
200 impl<'a, 'tcx> Iterator for CrateContextMaybeIterator<'a, 'tcx> {
201 type Item = (CrateContext<'a, 'tcx>, bool);
202
203 fn next(&mut self) -> Option<(CrateContext<'a, 'tcx>, bool)> {
204 if self.index >= self.shared.local_ccxs.len() {
205 return None;
206 }
207
208 let index = self.index;
209 self.index += 1;
210 if self.single {
211 self.index = self.shared.local_ccxs.len();
212 }
213
214 let ccx = CrateContext {
215 shared: self.shared,
216 local: &self.shared.local_ccxs[index],
217 index: index,
218 };
219 Some((ccx, index == self.origin))
220 }
221 }
222
223
224 unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
225 let llcx = llvm::LLVMContextCreate();
226 let mod_name = CString::new(mod_name).unwrap();
227 let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
228
229 let data_layout = sess.target.target.data_layout.as_bytes();
230 let data_layout = CString::new(data_layout).unwrap();
231 llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
232
233 let llvm_target = sess.target.target.llvm_target.as_bytes();
234 let llvm_target = CString::new(llvm_target).unwrap();
235 llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
236 (llcx, llmod)
237 }
238
239 impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
240 pub fn new(crate_name: &str,
241 local_count: usize,
242 tcx: &'b ty::ctxt<'tcx>,
243 export_map: ExportMap,
244 symbol_hasher: Sha256,
245 link_meta: LinkMeta,
246 reachable: NodeSet,
247 check_overflow: bool,
248 check_drop_flag_for_sanity: bool)
249 -> SharedCrateContext<'b, 'tcx> {
250 let (metadata_llcx, metadata_llmod) = unsafe {
251 create_context_and_module(&tcx.sess, "metadata")
252 };
253
254 // An interesting part of Windows which MSVC forces our hand on (and
255 // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
256 // attributes in LLVM IR as well as native dependencies (in C these
257 // correspond to `__declspec(dllimport)`).
258 //
259 // Whenever a dynamic library is built by MSVC it must have its public
260 // interface specified by functions tagged with `dllexport` or otherwise
261 // they're not available to be linked against. This poses a few problems
262 // for the compiler, some of which are somewhat fundamental, but we use
263 // the `use_dll_storage_attrs` variable below to attach the `dllexport`
264 // attribute to all LLVM functions that are reachable (e.g. they're
265 // already tagged with external linkage). This is suboptimal for a few
266 // reasons:
267 //
268 // * If an object file will never be included in a dynamic library,
269 // there's no need to attach the dllexport attribute. Most object
270 // files in Rust are not destined to become part of a dll as binaries
271 // are statically linked by default.
272 // * If the compiler is emitting both an rlib and a dylib, the same
273 // source object file is currently used but with MSVC this may be less
274 // feasible. The compiler may be able to get around this, but it may
275 // involve some invasive changes to deal with this.
276 //
277 // The flipside of this situation is that whenever you link to a dll and
278 // you import a function from it, the import should be tagged with
279 // `dllimport`. At this time, however, the compiler does not emit
280 // `dllimport` for any declarations other than constants (where it is
281 // required), which is again suboptimal for even more reasons!
282 //
283 // * Calling a function imported from another dll without using
284 // `dllimport` causes the linker/compiler to have extra overhead (one
285 // `jmp` instruction on x86) when calling the function.
286 // * The same object file may be used in different circumstances, so a
287 // function may be imported from a dll if the object is linked into a
288 // dll, but it may be just linked against if linked into an rlib.
289 // * The compiler has no knowledge about whether native functions should
290 // be tagged dllimport or not.
291 //
292 // For now the compiler takes the perf hit (I do not have any numbers to
293 // this effect) by marking very little as `dllimport` and praying the
294 // linker will take care of everything. Fixing this problem will likely
295 // require adding a few attributes to Rust itself (feature gated at the
296 // start) and then strongly recommending static linkage on MSVC!
297 let use_dll_storage_attrs = tcx.sess.target.target.options.is_like_msvc;
298
299 let mut shared_ccx = SharedCrateContext {
300 local_ccxs: Vec::with_capacity(local_count),
301 metadata_llmod: metadata_llmod,
302 metadata_llcx: metadata_llcx,
303 export_map: export_map,
304 reachable: reachable,
305 item_symbols: RefCell::new(NodeMap()),
306 link_meta: link_meta,
307 symbol_hasher: RefCell::new(symbol_hasher),
308 tcx: tcx,
309 stats: Stats {
310 n_glues_created: Cell::new(0),
311 n_null_glues: Cell::new(0),
312 n_real_glues: Cell::new(0),
313 n_fns: Cell::new(0),
314 n_monos: Cell::new(0),
315 n_inlines: Cell::new(0),
316 n_closures: Cell::new(0),
317 n_llvm_insns: Cell::new(0),
318 llvm_insns: RefCell::new(FnvHashMap()),
319 fn_stats: RefCell::new(Vec::new()),
320 },
321 check_overflow: check_overflow,
322 check_drop_flag_for_sanity: check_drop_flag_for_sanity,
323 available_drop_glues: RefCell::new(FnvHashMap()),
324 use_dll_storage_attrs: use_dll_storage_attrs,
325 };
326
327 for i in 0..local_count {
328 // Append ".rs" to crate name as LLVM module identifier.
329 //
330 // LLVM code generator emits a ".file filename" directive
331 // for ELF backends. Value of the "filename" is set as the
332 // LLVM module identifier. Due to a LLVM MC bug[1], LLVM
333 // crashes if the module identifier is same as other symbols
334 // such as a function name in the module.
335 // 1. http://llvm.org/bugs/show_bug.cgi?id=11479
336 let llmod_id = format!("{}.{}.rs", crate_name, i);
337 let local_ccx = LocalCrateContext::new(&shared_ccx, &llmod_id[..]);
338 shared_ccx.local_ccxs.push(local_ccx);
339 }
340
341 shared_ccx
342 }
343
344 pub fn iter<'a>(&'a self) -> CrateContextIterator<'a, 'tcx> {
345 CrateContextIterator {
346 shared: self,
347 index: 0,
348 }
349 }
350
351 pub fn get_ccx<'a>(&'a self, index: usize) -> CrateContext<'a, 'tcx> {
352 CrateContext {
353 shared: self,
354 local: &self.local_ccxs[index],
355 index: index,
356 }
357 }
358
359 fn get_smallest_ccx<'a>(&'a self) -> CrateContext<'a, 'tcx> {
360 let (local_ccx, index) =
361 self.local_ccxs
362 .iter()
363 .zip(0..self.local_ccxs.len())
364 .min_by(|&(local_ccx, _idx)| local_ccx.n_llvm_insns.get())
365 .unwrap();
366 CrateContext {
367 shared: self,
368 local: local_ccx,
369 index: index,
370 }
371 }
372
373
374 pub fn metadata_llmod(&self) -> ModuleRef {
375 self.metadata_llmod
376 }
377
378 pub fn metadata_llcx(&self) -> ContextRef {
379 self.metadata_llcx
380 }
381
382 pub fn export_map<'a>(&'a self) -> &'a ExportMap {
383 &self.export_map
384 }
385
386 pub fn reachable<'a>(&'a self) -> &'a NodeSet {
387 &self.reachable
388 }
389
390 pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
391 &self.item_symbols
392 }
393
394 pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
395 &self.link_meta
396 }
397
398 pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
399 self.tcx
400 }
401
402 pub fn sess<'a>(&'a self) -> &'a Session {
403 &self.tcx.sess
404 }
405
406 pub fn stats<'a>(&'a self) -> &'a Stats {
407 &self.stats
408 }
409
410 pub fn use_dll_storage_attrs(&self) -> bool {
411 self.use_dll_storage_attrs
412 }
413 }
414
415 impl<'tcx> LocalCrateContext<'tcx> {
416 fn new<'a>(shared: &SharedCrateContext<'a, 'tcx>,
417 name: &str)
418 -> LocalCrateContext<'tcx> {
419 unsafe {
420 let (llcx, llmod) = create_context_and_module(&shared.tcx.sess, name);
421
422 let td = mk_target_data(&shared.tcx
423 .sess
424 .target
425 .target
426 .data_layout
427 );
428
429 let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo {
430 Some(debuginfo::CrateDebugContext::new(llmod))
431 } else {
432 None
433 };
434
435 let mut local_ccx = LocalCrateContext {
436 llmod: llmod,
437 llcx: llcx,
438 td: td,
439 tn: TypeNames::new(),
440 externs: RefCell::new(FnvHashMap()),
441 item_vals: RefCell::new(NodeMap()),
442 needs_unwind_cleanup_cache: RefCell::new(FnvHashMap()),
443 fn_pointer_shims: RefCell::new(FnvHashMap()),
444 drop_glues: RefCell::new(FnvHashMap()),
445 external: RefCell::new(DefIdMap()),
446 external_srcs: RefCell::new(NodeMap()),
447 monomorphized: RefCell::new(FnvHashMap()),
448 monomorphizing: RefCell::new(DefIdMap()),
449 available_monomorphizations: RefCell::new(FnvHashSet()),
450 vtables: RefCell::new(FnvHashMap()),
451 const_cstr_cache: RefCell::new(FnvHashMap()),
452 const_unsized: RefCell::new(FnvHashMap()),
453 const_globals: RefCell::new(FnvHashMap()),
454 const_values: RefCell::new(FnvHashMap()),
455 static_values: RefCell::new(NodeMap()),
456 extern_const_values: RefCell::new(DefIdMap()),
457 impl_method_cache: RefCell::new(FnvHashMap()),
458 closure_bare_wrapper_cache: RefCell::new(FnvHashMap()),
459 lltypes: RefCell::new(FnvHashMap()),
460 llsizingtypes: RefCell::new(FnvHashMap()),
461 adt_reprs: RefCell::new(FnvHashMap()),
462 type_hashcodes: RefCell::new(FnvHashMap()),
463 int_type: Type::from_ref(ptr::null_mut()),
464 opaque_vec_type: Type::from_ref(ptr::null_mut()),
465 builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)),
466 closure_vals: RefCell::new(FnvHashMap()),
467 dbg_cx: dbg_cx,
468 eh_personality: RefCell::new(None),
469 intrinsics: RefCell::new(FnvHashMap()),
470 n_llvm_insns: Cell::new(0),
471 trait_cache: RefCell::new(FnvHashMap()),
472 };
473
474 local_ccx.int_type = Type::int(&local_ccx.dummy_ccx(shared));
475 local_ccx.opaque_vec_type = Type::opaque_vec(&local_ccx.dummy_ccx(shared));
476
477 // Done mutating local_ccx directly. (The rest of the
478 // initialization goes through RefCell.)
479 {
480 let ccx = local_ccx.dummy_ccx(shared);
481
482 let mut str_slice_ty = Type::named_struct(&ccx, "str_slice");
483 str_slice_ty.set_struct_body(&[Type::i8p(&ccx), ccx.int_type()], false);
484 ccx.tn().associate_type("str_slice", &str_slice_ty);
485
486 if ccx.sess().count_llvm_insns() {
487 base::init_insn_ctxt()
488 }
489 }
490
491 local_ccx
492 }
493 }
494
495 /// Create a dummy `CrateContext` from `self` and the provided
496 /// `SharedCrateContext`. This is somewhat dangerous because `self` may
497 /// not actually be an element of `shared.local_ccxs`, which can cause some
498 /// operations to panic unexpectedly.
499 ///
500 /// This is used in the `LocalCrateContext` constructor to allow calling
501 /// functions that expect a complete `CrateContext`, even before the local
502 /// portion is fully initialized and attached to the `SharedCrateContext`.
503 fn dummy_ccx<'a>(&'a self, shared: &'a SharedCrateContext<'a, 'tcx>)
504 -> CrateContext<'a, 'tcx> {
505 CrateContext {
506 shared: shared,
507 local: self,
508 index: !0 as usize,
509 }
510 }
511 }
512
513 impl<'b, 'tcx> CrateContext<'b, 'tcx> {
514 pub fn shared(&self) -> &'b SharedCrateContext<'b, 'tcx> {
515 self.shared
516 }
517
518 pub fn local(&self) -> &'b LocalCrateContext<'tcx> {
519 self.local
520 }
521
522
523 /// Get a (possibly) different `CrateContext` from the same
524 /// `SharedCrateContext`.
525 pub fn rotate(&self) -> CrateContext<'b, 'tcx> {
526 self.shared.get_smallest_ccx()
527 }
528
529 /// Either iterate over only `self`, or iterate over all `CrateContext`s in
530 /// the `SharedCrateContext`. The iterator produces `(ccx, is_origin)`
531 /// pairs, where `is_origin` is `true` if `ccx` is `self` and `false`
532 /// otherwise. This method is useful for avoiding code duplication in
533 /// cases where it may or may not be necessary to translate code into every
534 /// context.
535 pub fn maybe_iter(&self, iter_all: bool) -> CrateContextMaybeIterator<'b, 'tcx> {
536 CrateContextMaybeIterator {
537 shared: self.shared,
538 index: if iter_all { 0 } else { self.index },
539 single: !iter_all,
540 origin: self.index,
541 }
542 }
543
544
545 pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
546 self.shared.tcx
547 }
548
549 pub fn sess<'a>(&'a self) -> &'a Session {
550 &self.shared.tcx.sess
551 }
552
553 pub fn builder<'a>(&'a self) -> Builder<'a, 'tcx> {
554 Builder::new(self)
555 }
556
557 pub fn raw_builder<'a>(&'a self) -> BuilderRef {
558 self.local.builder.b
559 }
560
561 pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
562 if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
563 return v;
564 }
565 match declare_intrinsic(self, key) {
566 Some(v) => return v,
567 None => panic!()
568 }
569 }
570
571 pub fn is_split_stack_supported(&self) -> bool {
572 self.sess().target.target.options.morestack
573 }
574
575
576 pub fn llmod(&self) -> ModuleRef {
577 self.local.llmod
578 }
579
580 pub fn llcx(&self) -> ContextRef {
581 self.local.llcx
582 }
583
584 pub fn td<'a>(&'a self) -> &'a TargetData {
585 &self.local.td
586 }
587
588 pub fn tn<'a>(&'a self) -> &'a TypeNames {
589 &self.local.tn
590 }
591
592 pub fn externs<'a>(&'a self) -> &'a RefCell<ExternMap> {
593 &self.local.externs
594 }
595
596 pub fn item_vals<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
597 &self.local.item_vals
598 }
599
600 pub fn export_map<'a>(&'a self) -> &'a ExportMap {
601 &self.shared.export_map
602 }
603
604 pub fn reachable<'a>(&'a self) -> &'a NodeSet {
605 &self.shared.reachable
606 }
607
608 pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
609 &self.shared.item_symbols
610 }
611
612 pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
613 &self.shared.link_meta
614 }
615
616 pub fn needs_unwind_cleanup_cache(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, bool>> {
617 &self.local.needs_unwind_cleanup_cache
618 }
619
620 pub fn fn_pointer_shims(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, ValueRef>> {
621 &self.local.fn_pointer_shims
622 }
623
624 pub fn drop_glues<'a>(&'a self) -> &'a RefCell<FnvHashMap<DropGlueKind<'tcx>, ValueRef>> {
625 &self.local.drop_glues
626 }
627
628 pub fn external<'a>(&'a self) -> &'a RefCell<DefIdMap<Option<ast::NodeId>>> {
629 &self.local.external
630 }
631
632 pub fn external_srcs<'a>(&'a self) -> &'a RefCell<NodeMap<ast::DefId>> {
633 &self.local.external_srcs
634 }
635
636 pub fn monomorphized<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>> {
637 &self.local.monomorphized
638 }
639
640 pub fn monomorphizing<'a>(&'a self) -> &'a RefCell<DefIdMap<usize>> {
641 &self.local.monomorphizing
642 }
643
644 pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>> {
645 &self.local.vtables
646 }
647
648 pub fn const_cstr_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<InternedString, ValueRef>> {
649 &self.local.const_cstr_cache
650 }
651
652 pub fn const_unsized<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
653 &self.local.const_unsized
654 }
655
656 pub fn const_globals<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
657 &self.local.const_globals
658 }
659
660 pub fn const_values<'a>(&'a self) -> &'a RefCell<FnvHashMap<(ast::NodeId, &'tcx Substs<'tcx>),
661 ValueRef>> {
662 &self.local.const_values
663 }
664
665 pub fn static_values<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
666 &self.local.static_values
667 }
668
669 pub fn extern_const_values<'a>(&'a self) -> &'a RefCell<DefIdMap<ValueRef>> {
670 &self.local.extern_const_values
671 }
672
673 pub fn impl_method_cache<'a>(&'a self)
674 -> &'a RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>> {
675 &self.local.impl_method_cache
676 }
677
678 pub fn closure_bare_wrapper_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
679 &self.local.closure_bare_wrapper_cache
680 }
681
682 pub fn lltypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
683 &self.local.lltypes
684 }
685
686 pub fn llsizingtypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
687 &self.local.llsizingtypes
688 }
689
690 pub fn adt_reprs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>> {
691 &self.local.adt_reprs
692 }
693
694 pub fn symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
695 &self.shared.symbol_hasher
696 }
697
698 pub fn type_hashcodes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
699 &self.local.type_hashcodes
700 }
701
702 pub fn stats<'a>(&'a self) -> &'a Stats {
703 &self.shared.stats
704 }
705
706 pub fn available_monomorphizations<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
707 &self.local.available_monomorphizations
708 }
709
710 pub fn available_drop_glues(&self) -> &RefCell<FnvHashMap<DropGlueKind<'tcx>, String>> {
711 &self.shared.available_drop_glues
712 }
713
714 pub fn int_type(&self) -> Type {
715 self.local.int_type
716 }
717
718 pub fn opaque_vec_type(&self) -> Type {
719 self.local.opaque_vec_type
720 }
721
722 pub fn closure_vals<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>> {
723 &self.local.closure_vals
724 }
725
726 pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext<'tcx>> {
727 &self.local.dbg_cx
728 }
729
730 pub fn eh_personality<'a>(&'a self) -> &'a RefCell<Option<ValueRef>> {
731 &self.local.eh_personality
732 }
733
734 fn intrinsics<'a>(&'a self) -> &'a RefCell<FnvHashMap<&'static str, ValueRef>> {
735 &self.local.intrinsics
736 }
737
738 pub fn count_llvm_insn(&self) {
739 self.local.n_llvm_insns.set(self.local.n_llvm_insns.get() + 1);
740 }
741
742 pub fn trait_cache(&self) -> &RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>,
743 traits::Vtable<'tcx, ()>>> {
744 &self.local.trait_cache
745 }
746
747 /// Return exclusive upper bound on object size.
748 ///
749 /// The theoretical maximum object size is defined as the maximum positive `int` value. This
750 /// ensures that the `offset` semantics remain well-defined by allowing it to correctly index
751 /// every address within an object along with one byte past the end, along with allowing `int`
752 /// to store the difference between any two pointers into an object.
753 ///
754 /// The upper bound on 64-bit currently needs to be lower because LLVM uses a 64-bit integer to
755 /// represent object size in bits. It would need to be 1 << 61 to account for this, but is
756 /// currently conservatively bounded to 1 << 47 as that is enough to cover the current usable
757 /// address space on 64-bit ARMv8 and x86_64.
758 pub fn obj_size_bound(&self) -> u64 {
759 match &self.sess().target.target.target_pointer_width[..] {
760 "32" => 1 << 31,
761 "64" => 1 << 47,
762 _ => unreachable!() // error handled by config::build_target_config
763 }
764 }
765
766 pub fn report_overbig_object(&self, obj: Ty<'tcx>) -> ! {
767 self.sess().fatal(
768 &format!("the type `{:?}` is too big for the current architecture",
769 obj))
770 }
771
772 pub fn check_overflow(&self) -> bool {
773 self.shared.check_overflow
774 }
775
776 pub fn check_drop_flag_for_sanity(&self) -> bool {
777 // This controls whether we emit a conditional llvm.debugtrap
778 // guarded on whether the dropflag is one of its (two) valid
779 // values.
780 self.shared.check_drop_flag_for_sanity
781 }
782
783 pub fn use_dll_storage_attrs(&self) -> bool {
784 self.shared.use_dll_storage_attrs()
785 }
786 }
787
788 /// Declare any llvm intrinsics that you might need
789 fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
790 macro_rules! ifn {
791 ($name:expr, fn() -> $ret:expr) => (
792 if *key == $name {
793 let f = declare::declare_cfn(ccx, $name, Type::func(&[], &$ret),
794 ty::mk_nil(ccx.tcx()));
795 ccx.intrinsics().borrow_mut().insert($name, f.clone());
796 return Some(f);
797 }
798 );
799 ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
800 if *key == $name {
801 let f = declare::declare_cfn(ccx, $name, Type::func(&[$($arg),*], &$ret),
802 ty::mk_nil(ccx.tcx()));
803 ccx.intrinsics().borrow_mut().insert($name, f.clone());
804 return Some(f);
805 }
806 )
807 }
808 macro_rules! mk_struct {
809 ($($field_ty:expr),*) => (Type::struct_(ccx, &[$($field_ty),*], false))
810 }
811
812 let i8p = Type::i8p(ccx);
813 let void = Type::void(ccx);
814 let i1 = Type::i1(ccx);
815 let t_i8 = Type::i8(ccx);
816 let t_i16 = Type::i16(ccx);
817 let t_i32 = Type::i32(ccx);
818 let t_i64 = Type::i64(ccx);
819 let t_f32 = Type::f32(ccx);
820 let t_f64 = Type::f64(ccx);
821
822 ifn!("llvm.memcpy.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
823 ifn!("llvm.memcpy.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
824 ifn!("llvm.memmove.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
825 ifn!("llvm.memmove.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
826 ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
827 ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
828
829 ifn!("llvm.trap", fn() -> void);
830 ifn!("llvm.debugtrap", fn() -> void);
831 ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
832
833 ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
834 ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
835 ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
836 ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
837
838 ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
839 ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
840 ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
841 ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
842 ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
843 ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
844 ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
845 ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
846 ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
847 ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
848 ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
849 ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
850 ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
851 ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
852 ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
853 ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
854
855 ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
856 ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
857
858 ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
859 ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
860
861 ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
862 ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
863 ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
864 ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
865 ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
866 ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
867
868 ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
869 ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
870 ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
871 ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
872
873 ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
874 ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
875 ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
876 ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
877
878 ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
879 ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
880 ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
881 ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
882
883 ifn!("llvm.ctlz.i8", fn(t_i8 , i1) -> t_i8);
884 ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
885 ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
886 ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
887
888 ifn!("llvm.cttz.i8", fn(t_i8 , i1) -> t_i8);
889 ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
890 ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
891 ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
892
893 ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
894 ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
895 ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
896
897 ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
898 ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
899 ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
900 ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
901
902 ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
903 ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
904 ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
905 ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
906
907 ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
908 ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
909 ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
910 ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
911
912 ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
913 ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
914 ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
915 ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
916
917 ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
918 ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
919 ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
920 ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
921
922 ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
923 ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
924 ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
925 ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
926
927 ifn!("llvm.lifetime.start", fn(t_i64,i8p) -> void);
928 ifn!("llvm.lifetime.end", fn(t_i64, i8p) -> void);
929
930 ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
931
932 // Some intrinsics were introduced in later versions of LLVM, but they have
933 // fallbacks in libc or libm and such.
934 macro_rules! compatible_ifn {
935 ($name:expr, noop($cname:ident ($($arg:expr),*) -> void), $llvm_version:expr) => (
936 if unsafe { llvm::LLVMVersionMinor() >= $llvm_version } {
937 // The `if key == $name` is already in ifn!
938 ifn!($name, fn($($arg),*) -> void);
939 } else if *key == $name {
940 let f = declare::declare_cfn(ccx, stringify!($cname),
941 Type::func(&[$($arg),*], &void),
942 ty::mk_nil(ccx.tcx()));
943 llvm::SetLinkage(f, llvm::InternalLinkage);
944
945 let bld = ccx.builder();
946 let llbb = unsafe {
947 llvm::LLVMAppendBasicBlockInContext(ccx.llcx(), f,
948 "entry-block\0".as_ptr() as *const _)
949 };
950
951 bld.position_at_end(llbb);
952 bld.ret_void();
953
954 ccx.intrinsics().borrow_mut().insert($name, f.clone());
955 return Some(f);
956 }
957 );
958 ($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr, $llvm_version:expr) => (
959 if unsafe { llvm::LLVMVersionMinor() >= $llvm_version } {
960 // The `if key == $name` is already in ifn!
961 ifn!($name, fn($($arg),*) -> $ret);
962 } else if *key == $name {
963 let f = declare::declare_cfn(ccx, stringify!($cname),
964 Type::func(&[$($arg),*], &$ret),
965 ty::mk_nil(ccx.tcx()));
966 ccx.intrinsics().borrow_mut().insert($name, f.clone());
967 return Some(f);
968 }
969 )
970 }
971
972 compatible_ifn!("llvm.assume", noop(llvmcompat_assume(i1) -> void), 6);
973
974 if ccx.sess().opts.debuginfo != NoDebugInfo {
975 ifn!("llvm.dbg.declare", fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
976 ifn!("llvm.dbg.value", fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);
977 }
978 return None;
979 }