]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/trans/context.rs
41ef566f2fd7f8e916c7af0039a62e28d91dc9c4
[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::ppaux::Repr;
32 use util::sha2::Sha256;
33 use util::nodemap::{NodeMap, NodeSet, DefIdMap, FnvHashMap, FnvHashSet};
34
35 use std::ffi::CString;
36 use std::cell::{Cell, RefCell};
37 use std::ptr;
38 use std::rc::Rc;
39 use syntax::ast;
40 use syntax::parse::token::InternedString;
41
42 pub struct Stats {
43 pub n_glues_created: Cell<usize>,
44 pub n_null_glues: Cell<usize>,
45 pub n_real_glues: Cell<usize>,
46 pub n_fns: Cell<usize>,
47 pub n_monos: Cell<usize>,
48 pub n_inlines: Cell<usize>,
49 pub n_closures: Cell<usize>,
50 pub n_llvm_insns: Cell<usize>,
51 pub llvm_insns: RefCell<FnvHashMap<String, usize>>,
52 // (ident, llvm-instructions)
53 pub fn_stats: RefCell<Vec<(String, usize)> >,
54 }
55
56 /// The shared portion of a `CrateContext`. There is one `SharedCrateContext`
57 /// per crate. The data here is shared between all compilation units of the
58 /// crate, so it must not contain references to any LLVM data structures
59 /// (aside from metadata-related ones).
60 pub struct SharedCrateContext<'tcx> {
61 local_ccxs: Vec<LocalCrateContext<'tcx>>,
62
63 metadata_llmod: ModuleRef,
64 metadata_llcx: ContextRef,
65
66 export_map: ExportMap,
67 reachable: NodeSet,
68 item_symbols: RefCell<NodeMap<String>>,
69 link_meta: LinkMeta,
70 symbol_hasher: RefCell<Sha256>,
71 tcx: ty::ctxt<'tcx>,
72 stats: Stats,
73 check_overflow: bool,
74 check_drop_flag_for_sanity: bool,
75
76 available_monomorphizations: RefCell<FnvHashSet<String>>,
77 available_drop_glues: RefCell<FnvHashMap<DropGlueKind<'tcx>, String>>,
78 }
79
80 /// The local portion of a `CrateContext`. There is one `LocalCrateContext`
81 /// per compilation unit. Each one has its own LLVM `ContextRef` so that
82 /// several compilation units may be optimized in parallel. All other LLVM
83 /// data structures in the `LocalCrateContext` are tied to that `ContextRef`.
84 pub struct LocalCrateContext<'tcx> {
85 llmod: ModuleRef,
86 llcx: ContextRef,
87 td: TargetData,
88 tn: TypeNames,
89 externs: RefCell<ExternMap>,
90 item_vals: RefCell<NodeMap<ValueRef>>,
91 needs_unwind_cleanup_cache: RefCell<FnvHashMap<Ty<'tcx>, bool>>,
92 fn_pointer_shims: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
93 drop_glues: RefCell<FnvHashMap<DropGlueKind<'tcx>, ValueRef>>,
94 /// Track mapping of external ids to local items imported for inlining
95 external: RefCell<DefIdMap<Option<ast::NodeId>>>,
96 /// Backwards version of the `external` map (inlined items to where they
97 /// came from)
98 external_srcs: RefCell<NodeMap<ast::DefId>>,
99 /// Cache instances of monomorphized functions
100 monomorphized: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
101 monomorphizing: RefCell<DefIdMap<usize>>,
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<'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<'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<'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<'tcx> SharedCrateContext<'tcx> {
240 pub fn new(crate_name: &str,
241 local_count: usize,
242 tcx: 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<'tcx> {
250 let (metadata_llcx, metadata_llmod) = unsafe {
251 create_context_and_module(&tcx.sess, "metadata")
252 };
253
254 let mut shared_ccx = SharedCrateContext {
255 local_ccxs: Vec::with_capacity(local_count),
256 metadata_llmod: metadata_llmod,
257 metadata_llcx: metadata_llcx,
258 export_map: export_map,
259 reachable: reachable,
260 item_symbols: RefCell::new(NodeMap()),
261 link_meta: link_meta,
262 symbol_hasher: RefCell::new(symbol_hasher),
263 tcx: tcx,
264 stats: Stats {
265 n_glues_created: Cell::new(0),
266 n_null_glues: Cell::new(0),
267 n_real_glues: Cell::new(0),
268 n_fns: Cell::new(0),
269 n_monos: Cell::new(0),
270 n_inlines: Cell::new(0),
271 n_closures: Cell::new(0),
272 n_llvm_insns: Cell::new(0),
273 llvm_insns: RefCell::new(FnvHashMap()),
274 fn_stats: RefCell::new(Vec::new()),
275 },
276 check_overflow: check_overflow,
277 check_drop_flag_for_sanity: check_drop_flag_for_sanity,
278 available_monomorphizations: RefCell::new(FnvHashSet()),
279 available_drop_glues: RefCell::new(FnvHashMap()),
280 };
281
282 for i in 0..local_count {
283 // Append ".rs" to crate name as LLVM module identifier.
284 //
285 // LLVM code generator emits a ".file filename" directive
286 // for ELF backends. Value of the "filename" is set as the
287 // LLVM module identifier. Due to a LLVM MC bug[1], LLVM
288 // crashes if the module identifier is same as other symbols
289 // such as a function name in the module.
290 // 1. http://llvm.org/bugs/show_bug.cgi?id=11479
291 let llmod_id = format!("{}.{}.rs", crate_name, i);
292 let local_ccx = LocalCrateContext::new(&shared_ccx, &llmod_id[..]);
293 shared_ccx.local_ccxs.push(local_ccx);
294 }
295
296 shared_ccx
297 }
298
299 pub fn iter<'a>(&'a self) -> CrateContextIterator<'a, 'tcx> {
300 CrateContextIterator {
301 shared: self,
302 index: 0,
303 }
304 }
305
306 pub fn get_ccx<'a>(&'a self, index: usize) -> CrateContext<'a, 'tcx> {
307 CrateContext {
308 shared: self,
309 local: &self.local_ccxs[index],
310 index: index,
311 }
312 }
313
314 fn get_smallest_ccx<'a>(&'a self) -> CrateContext<'a, 'tcx> {
315 let (local_ccx, index) =
316 self.local_ccxs
317 .iter()
318 .zip(0..self.local_ccxs.len())
319 .min_by(|&(local_ccx, _idx)| local_ccx.n_llvm_insns.get())
320 .unwrap();
321 CrateContext {
322 shared: self,
323 local: local_ccx,
324 index: index,
325 }
326 }
327
328
329 pub fn metadata_llmod(&self) -> ModuleRef {
330 self.metadata_llmod
331 }
332
333 pub fn metadata_llcx(&self) -> ContextRef {
334 self.metadata_llcx
335 }
336
337 pub fn export_map<'a>(&'a self) -> &'a ExportMap {
338 &self.export_map
339 }
340
341 pub fn reachable<'a>(&'a self) -> &'a NodeSet {
342 &self.reachable
343 }
344
345 pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
346 &self.item_symbols
347 }
348
349 pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
350 &self.link_meta
351 }
352
353 pub fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> {
354 &self.tcx
355 }
356
357 pub fn take_tcx(self) -> ty::ctxt<'tcx> {
358 self.tcx
359 }
360
361 pub fn sess<'a>(&'a self) -> &'a Session {
362 &self.tcx.sess
363 }
364
365 pub fn stats<'a>(&'a self) -> &'a Stats {
366 &self.stats
367 }
368 }
369
370 impl<'tcx> LocalCrateContext<'tcx> {
371 fn new(shared: &SharedCrateContext<'tcx>,
372 name: &str)
373 -> LocalCrateContext<'tcx> {
374 unsafe {
375 let (llcx, llmod) = create_context_and_module(&shared.tcx.sess, name);
376
377 let td = mk_target_data(&shared.tcx
378 .sess
379 .target
380 .target
381 .data_layout
382 );
383
384 let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo {
385 Some(debuginfo::CrateDebugContext::new(llmod))
386 } else {
387 None
388 };
389
390 let mut local_ccx = LocalCrateContext {
391 llmod: llmod,
392 llcx: llcx,
393 td: td,
394 tn: TypeNames::new(),
395 externs: RefCell::new(FnvHashMap()),
396 item_vals: RefCell::new(NodeMap()),
397 needs_unwind_cleanup_cache: RefCell::new(FnvHashMap()),
398 fn_pointer_shims: RefCell::new(FnvHashMap()),
399 drop_glues: RefCell::new(FnvHashMap()),
400 external: RefCell::new(DefIdMap()),
401 external_srcs: RefCell::new(NodeMap()),
402 monomorphized: RefCell::new(FnvHashMap()),
403 monomorphizing: RefCell::new(DefIdMap()),
404 vtables: RefCell::new(FnvHashMap()),
405 const_cstr_cache: RefCell::new(FnvHashMap()),
406 const_unsized: RefCell::new(FnvHashMap()),
407 const_globals: RefCell::new(FnvHashMap()),
408 const_values: RefCell::new(FnvHashMap()),
409 static_values: RefCell::new(NodeMap()),
410 extern_const_values: RefCell::new(DefIdMap()),
411 impl_method_cache: RefCell::new(FnvHashMap()),
412 closure_bare_wrapper_cache: RefCell::new(FnvHashMap()),
413 lltypes: RefCell::new(FnvHashMap()),
414 llsizingtypes: RefCell::new(FnvHashMap()),
415 adt_reprs: RefCell::new(FnvHashMap()),
416 type_hashcodes: RefCell::new(FnvHashMap()),
417 int_type: Type::from_ref(ptr::null_mut()),
418 opaque_vec_type: Type::from_ref(ptr::null_mut()),
419 builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)),
420 closure_vals: RefCell::new(FnvHashMap()),
421 dbg_cx: dbg_cx,
422 eh_personality: RefCell::new(None),
423 intrinsics: RefCell::new(FnvHashMap()),
424 n_llvm_insns: Cell::new(0),
425 trait_cache: RefCell::new(FnvHashMap()),
426 };
427
428 local_ccx.int_type = Type::int(&local_ccx.dummy_ccx(shared));
429 local_ccx.opaque_vec_type = Type::opaque_vec(&local_ccx.dummy_ccx(shared));
430
431 // Done mutating local_ccx directly. (The rest of the
432 // initialization goes through RefCell.)
433 {
434 let ccx = local_ccx.dummy_ccx(shared);
435
436 let mut str_slice_ty = Type::named_struct(&ccx, "str_slice");
437 str_slice_ty.set_struct_body(&[Type::i8p(&ccx), ccx.int_type()], false);
438 ccx.tn().associate_type("str_slice", &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: !0 as usize,
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 get_intrinsic(&self, key: & &'static str) -> ValueRef {
516 if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
517 return v;
518 }
519 match declare_intrinsic(self, key) {
520 Some(v) => return v,
521 None => panic!()
522 }
523 }
524
525 pub fn is_split_stack_supported(&self) -> bool {
526 self.sess().target.target.options.morestack
527 }
528
529
530 pub fn llmod(&self) -> ModuleRef {
531 self.local.llmod
532 }
533
534 pub fn llcx(&self) -> ContextRef {
535 self.local.llcx
536 }
537
538 pub fn td<'a>(&'a self) -> &'a TargetData {
539 &self.local.td
540 }
541
542 pub fn tn<'a>(&'a self) -> &'a TypeNames {
543 &self.local.tn
544 }
545
546 pub fn externs<'a>(&'a self) -> &'a RefCell<ExternMap> {
547 &self.local.externs
548 }
549
550 pub fn item_vals<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
551 &self.local.item_vals
552 }
553
554 pub fn export_map<'a>(&'a self) -> &'a ExportMap {
555 &self.shared.export_map
556 }
557
558 pub fn reachable<'a>(&'a self) -> &'a NodeSet {
559 &self.shared.reachable
560 }
561
562 pub fn item_symbols<'a>(&'a self) -> &'a RefCell<NodeMap<String>> {
563 &self.shared.item_symbols
564 }
565
566 pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
567 &self.shared.link_meta
568 }
569
570 pub fn needs_unwind_cleanup_cache(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, bool>> {
571 &self.local.needs_unwind_cleanup_cache
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<DropGlueKind<'tcx>, ValueRef>> {
579 &self.local.drop_glues
580 }
581
582 pub fn external<'a>(&'a self) -> &'a RefCell<DefIdMap<Option<ast::NodeId>>> {
583 &self.local.external
584 }
585
586 pub fn external_srcs<'a>(&'a self) -> &'a RefCell<NodeMap<ast::DefId>> {
587 &self.local.external_srcs
588 }
589
590 pub fn monomorphized<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>> {
591 &self.local.monomorphized
592 }
593
594 pub fn monomorphizing<'a>(&'a self) -> &'a RefCell<DefIdMap<usize>> {
595 &self.local.monomorphizing
596 }
597
598 pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>> {
599 &self.local.vtables
600 }
601
602 pub fn const_cstr_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<InternedString, ValueRef>> {
603 &self.local.const_cstr_cache
604 }
605
606 pub fn const_unsized<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
607 &self.local.const_unsized
608 }
609
610 pub fn const_globals<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
611 &self.local.const_globals
612 }
613
614 pub fn const_values<'a>(&'a self) -> &'a RefCell<FnvHashMap<(ast::NodeId, &'tcx Substs<'tcx>),
615 ValueRef>> {
616 &self.local.const_values
617 }
618
619 pub fn static_values<'a>(&'a self) -> &'a RefCell<NodeMap<ValueRef>> {
620 &self.local.static_values
621 }
622
623 pub fn extern_const_values<'a>(&'a self) -> &'a RefCell<DefIdMap<ValueRef>> {
624 &self.local.extern_const_values
625 }
626
627 pub fn impl_method_cache<'a>(&'a self)
628 -> &'a RefCell<FnvHashMap<(ast::DefId, ast::Name), ast::DefId>> {
629 &self.local.impl_method_cache
630 }
631
632 pub fn closure_bare_wrapper_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
633 &self.local.closure_bare_wrapper_cache
634 }
635
636 pub fn lltypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
637 &self.local.lltypes
638 }
639
640 pub fn llsizingtypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
641 &self.local.llsizingtypes
642 }
643
644 pub fn adt_reprs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>> {
645 &self.local.adt_reprs
646 }
647
648 pub fn symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
649 &self.shared.symbol_hasher
650 }
651
652 pub fn type_hashcodes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
653 &self.local.type_hashcodes
654 }
655
656 pub fn stats<'a>(&'a self) -> &'a Stats {
657 &self.shared.stats
658 }
659
660 pub fn available_monomorphizations<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
661 &self.shared.available_monomorphizations
662 }
663
664 pub fn available_drop_glues(&self) -> &RefCell<FnvHashMap<DropGlueKind<'tcx>, String>> {
665 &self.shared.available_drop_glues
666 }
667
668 pub fn int_type(&self) -> Type {
669 self.local.int_type
670 }
671
672 pub fn opaque_vec_type(&self) -> Type {
673 self.local.opaque_vec_type
674 }
675
676 pub fn closure_vals<'a>(&'a self) -> &'a RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>> {
677 &self.local.closure_vals
678 }
679
680 pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext<'tcx>> {
681 &self.local.dbg_cx
682 }
683
684 pub fn eh_personality<'a>(&'a self) -> &'a RefCell<Option<ValueRef>> {
685 &self.local.eh_personality
686 }
687
688 fn intrinsics<'a>(&'a self) -> &'a RefCell<FnvHashMap<&'static str, ValueRef>> {
689 &self.local.intrinsics
690 }
691
692 pub fn count_llvm_insn(&self) {
693 self.local.n_llvm_insns.set(self.local.n_llvm_insns.get() + 1);
694 }
695
696 pub fn trait_cache(&self) -> &RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>,
697 traits::Vtable<'tcx, ()>>> {
698 &self.local.trait_cache
699 }
700
701 /// Return exclusive upper bound on object size.
702 ///
703 /// The theoretical maximum object size is defined as the maximum positive `int` value. This
704 /// ensures that the `offset` semantics remain well-defined by allowing it to correctly index
705 /// every address within an object along with one byte past the end, along with allowing `int`
706 /// to store the difference between any two pointers into an object.
707 ///
708 /// The upper bound on 64-bit currently needs to be lower because LLVM uses a 64-bit integer to
709 /// represent object size in bits. It would need to be 1 << 61 to account for this, but is
710 /// currently conservatively bounded to 1 << 47 as that is enough to cover the current usable
711 /// address space on 64-bit ARMv8 and x86_64.
712 pub fn obj_size_bound(&self) -> u64 {
713 match &self.sess().target.target.target_pointer_width[..] {
714 "32" => 1 << 31,
715 "64" => 1 << 47,
716 _ => unreachable!() // error handled by config::build_target_config
717 }
718 }
719
720 pub fn report_overbig_object(&self, obj: Ty<'tcx>) -> ! {
721 self.sess().fatal(
722 &format!("the type `{}` is too big for the current architecture",
723 obj.repr(self.tcx())))
724 }
725
726 pub fn check_overflow(&self) -> bool {
727 self.shared.check_overflow
728 }
729
730 pub fn check_drop_flag_for_sanity(&self) -> bool {
731 // This controls whether we emit a conditional llvm.debugtrap
732 // guarded on whether the dropflag is one of its (two) valid
733 // values.
734 self.shared.check_drop_flag_for_sanity
735 }
736 }
737
738 /// Declare any llvm intrinsics that you might need
739 fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
740 macro_rules! ifn {
741 ($name:expr, fn() -> $ret:expr) => (
742 if *key == $name {
743 let f = declare::declare_cfn(ccx, $name, Type::func(&[], &$ret),
744 ty::mk_nil(ccx.tcx()));
745 ccx.intrinsics().borrow_mut().insert($name, f.clone());
746 return Some(f);
747 }
748 );
749 ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
750 if *key == $name {
751 let f = declare::declare_cfn(ccx, $name, Type::func(&[$($arg),*], &$ret),
752 ty::mk_nil(ccx.tcx()));
753 ccx.intrinsics().borrow_mut().insert($name, f.clone());
754 return Some(f);
755 }
756 )
757 }
758 macro_rules! mk_struct {
759 ($($field_ty:expr),*) => (Type::struct_(ccx, &[$($field_ty),*], false))
760 }
761
762 let i8p = Type::i8p(ccx);
763 let void = Type::void(ccx);
764 let i1 = Type::i1(ccx);
765 let t_i8 = Type::i8(ccx);
766 let t_i16 = Type::i16(ccx);
767 let t_i32 = Type::i32(ccx);
768 let t_i64 = Type::i64(ccx);
769 let t_f32 = Type::f32(ccx);
770 let t_f64 = Type::f64(ccx);
771
772 ifn!("llvm.memcpy.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
773 ifn!("llvm.memcpy.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
774 ifn!("llvm.memmove.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
775 ifn!("llvm.memmove.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
776 ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
777 ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
778
779 ifn!("llvm.trap", fn() -> void);
780 ifn!("llvm.debugtrap", fn() -> void);
781 ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
782
783 ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
784 ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
785 ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
786 ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
787
788 ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
789 ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
790 ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
791 ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
792 ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
793 ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
794 ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
795 ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
796 ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
797 ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
798 ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
799 ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
800 ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
801 ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
802 ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
803 ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
804
805 ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
806 ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
807
808 ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
809 ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
810
811 ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
812 ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
813 ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
814 ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
815 ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
816 ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
817
818 ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
819 ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
820 ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
821 ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
822
823 ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
824 ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
825 ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
826 ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
827
828 ifn!("llvm.ctlz.i8", fn(t_i8 , i1) -> t_i8);
829 ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
830 ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
831 ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
832
833 ifn!("llvm.cttz.i8", fn(t_i8 , i1) -> t_i8);
834 ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
835 ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
836 ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
837
838 ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
839 ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
840 ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
841
842 ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
843 ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
844 ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
845 ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
846
847 ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
848 ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
849 ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
850 ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
851
852 ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
853 ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
854 ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
855 ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
856
857 ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
858 ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
859 ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
860 ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
861
862 ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
863 ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
864 ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
865 ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
866
867 ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
868 ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
869 ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
870 ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
871
872 ifn!("llvm.lifetime.start", fn(t_i64,i8p) -> void);
873 ifn!("llvm.lifetime.end", fn(t_i64, i8p) -> void);
874
875 ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
876 ifn!("llvm.assume", fn(i1) -> void);
877
878 // Some intrinsics were introduced in later versions of LLVM, but they have
879 // fallbacks in libc or libm and such. Currently, all of these intrinsics
880 // were introduced in LLVM 3.4, so we case on that.
881 macro_rules! compatible_ifn {
882 ($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr) => (
883 ifn!($name, fn($($arg),*) -> $ret);
884 )
885 }
886
887 compatible_ifn!("llvm.copysign.f32", copysignf(t_f32, t_f32) -> t_f32);
888 compatible_ifn!("llvm.copysign.f64", copysign(t_f64, t_f64) -> t_f64);
889 compatible_ifn!("llvm.round.f32", roundf(t_f32) -> t_f32);
890 compatible_ifn!("llvm.round.f64", round(t_f64) -> t_f64);
891
892
893 if ccx.sess().opts.debuginfo != NoDebugInfo {
894 ifn!("llvm.dbg.declare", fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
895 ifn!("llvm.dbg.value", fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);
896 }
897 return None;
898 }