]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/context.rs
auto merge of #15749 : vhbit/rust/treemap-doc-fixes, r=alexcrichton
[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};
15 use llvm::{TargetData};
16 use llvm::mk_target_data;
17 use metadata::common::LinkMeta;
18 use middle::resolve;
19 use middle::trans::adt;
20 use middle::trans::base;
21 use middle::trans::builder::Builder;
22 use middle::trans::common::{ExternMap,tydesc_info,BuilderRef_res};
23 use middle::trans::debuginfo;
24 use middle::trans::monomorphize::MonoId;
25 use middle::trans::type_::{Type, TypeNames};
26 use middle::ty;
27 use util::sha2::Sha256;
28 use util::nodemap::{NodeMap, NodeSet, DefIdMap};
29
30 use std::cell::{Cell, RefCell};
31 use std::c_str::ToCStr;
32 use std::ptr;
33 use std::rc::Rc;
34 use std::collections::{HashMap, HashSet};
35 use syntax::abi;
36 use syntax::ast;
37 use syntax::parse::token::InternedString;
38
39 pub struct Stats {
40     pub n_static_tydescs: Cell<uint>,
41     pub n_glues_created: Cell<uint>,
42     pub n_null_glues: Cell<uint>,
43     pub n_real_glues: Cell<uint>,
44     pub n_fns: Cell<uint>,
45     pub n_monos: Cell<uint>,
46     pub n_inlines: Cell<uint>,
47     pub n_closures: Cell<uint>,
48     pub n_llvm_insns: Cell<uint>,
49     pub llvm_insns: RefCell<HashMap<String, uint>>,
50     // (ident, time-in-ms, llvm-instructions)
51     pub fn_stats: RefCell<Vec<(String, uint, uint)> >,
52 }
53
54 pub struct CrateContext {
55     pub llmod: ModuleRef,
56     pub llcx: ContextRef,
57     pub metadata_llmod: ModuleRef,
58     pub td: TargetData,
59     pub tn: TypeNames,
60     pub externs: RefCell<ExternMap>,
61     pub item_vals: RefCell<NodeMap<ValueRef>>,
62     pub exp_map2: resolve::ExportMap2,
63     pub reachable: NodeSet,
64     pub item_symbols: RefCell<NodeMap<String>>,
65     pub link_meta: LinkMeta,
66     pub drop_glues: RefCell<HashMap<ty::t, ValueRef>>,
67     pub tydescs: RefCell<HashMap<ty::t, Rc<tydesc_info>>>,
68     /// Set when running emit_tydescs to enforce that no more tydescs are
69     /// created.
70     pub finished_tydescs: Cell<bool>,
71     /// Track mapping of external ids to local items imported for inlining
72     pub external: RefCell<DefIdMap<Option<ast::NodeId>>>,
73     /// Backwards version of the `external` map (inlined items to where they
74     /// came from)
75     pub external_srcs: RefCell<NodeMap<ast::DefId>>,
76     /// A set of static items which cannot be inlined into other crates. This
77     /// will prevent in IIItem() structures from being encoded into the metadata
78     /// that is generated
79     pub non_inlineable_statics: RefCell<NodeSet>,
80     /// Cache instances of monomorphized functions
81     pub monomorphized: RefCell<HashMap<MonoId, ValueRef>>,
82     pub monomorphizing: RefCell<DefIdMap<uint>>,
83     /// Cache generated vtables
84     pub vtables: RefCell<HashMap<(ty::t, MonoId), ValueRef>>,
85     /// Cache of constant strings,
86     pub const_cstr_cache: RefCell<HashMap<InternedString, ValueRef>>,
87
88     /// Reverse-direction for const ptrs cast from globals.
89     /// Key is an int, cast from a ValueRef holding a *T,
90     /// Val is a ValueRef holding a *[T].
91     ///
92     /// Needed because LLVM loses pointer->pointee association
93     /// when we ptrcast, and we have to ptrcast during translation
94     /// of a [T] const because we form a slice, a [*T,int] pair, not
95     /// a pointer to an LLVM array type.
96     pub const_globals: RefCell<HashMap<int, ValueRef>>,
97
98     /// Cache of emitted const values
99     pub const_values: RefCell<NodeMap<ValueRef>>,
100
101     /// Cache of external const values
102     pub extern_const_values: RefCell<DefIdMap<ValueRef>>,
103
104     pub impl_method_cache: RefCell<HashMap<(ast::DefId, ast::Name), ast::DefId>>,
105
106     /// Cache of closure wrappers for bare fn's.
107     pub closure_bare_wrapper_cache: RefCell<HashMap<ValueRef, ValueRef>>,
108
109     pub lltypes: RefCell<HashMap<ty::t, Type>>,
110     pub llsizingtypes: RefCell<HashMap<ty::t, Type>>,
111     pub adt_reprs: RefCell<HashMap<ty::t, Rc<adt::Repr>>>,
112     pub symbol_hasher: RefCell<Sha256>,
113     pub type_hashcodes: RefCell<HashMap<ty::t, String>>,
114     pub all_llvm_symbols: RefCell<HashSet<String>>,
115     pub tcx: ty::ctxt,
116     pub stats: Stats,
117     pub int_type: Type,
118     pub opaque_vec_type: Type,
119     pub builder: BuilderRef_res,
120
121     /// Holds the LLVM values for closure IDs.
122     pub unboxed_closure_vals: RefCell<DefIdMap<ValueRef>>,
123
124     /// Set when at least one function uses GC. Needed so that
125     /// decl_gc_metadata knows whether to link to the module metadata, which
126     /// is not emitted by LLVM's GC pass when no functions use GC.
127     pub uses_gc: bool,
128     pub dbg_cx: Option<debuginfo::CrateDebugContext>,
129
130     pub eh_personality: RefCell<Option<ValueRef>>,
131
132     intrinsics: RefCell<HashMap<&'static str, ValueRef>>,
133 }
134
135 impl CrateContext {
136     pub fn new(name: &str,
137                tcx: ty::ctxt,
138                emap2: resolve::ExportMap2,
139                symbol_hasher: Sha256,
140                link_meta: LinkMeta,
141                reachable: NodeSet)
142                -> CrateContext {
143         unsafe {
144             let llcx = llvm::LLVMContextCreate();
145             let llmod = name.with_c_str(|buf| {
146                 llvm::LLVMModuleCreateWithNameInContext(buf, llcx)
147             });
148             let metadata_llmod = format!("{}_metadata", name).with_c_str(|buf| {
149                 llvm::LLVMModuleCreateWithNameInContext(buf, llcx)
150             });
151             tcx.sess
152                .targ_cfg
153                .target_strs
154                .data_layout
155                .as_slice()
156                .with_c_str(|buf| {
157                 llvm::LLVMSetDataLayout(llmod, buf);
158                 llvm::LLVMSetDataLayout(metadata_llmod, buf);
159             });
160             tcx.sess
161                .targ_cfg
162                .target_strs
163                .target_triple
164                .as_slice()
165                .with_c_str(|buf| {
166                 llvm::LLVMRustSetNormalizedTarget(llmod, buf);
167                 llvm::LLVMRustSetNormalizedTarget(metadata_llmod, buf);
168             });
169
170             let td = mk_target_data(tcx.sess
171                                        .targ_cfg
172                                        .target_strs
173                                        .data_layout
174                                        .as_slice());
175
176             let dbg_cx = if tcx.sess.opts.debuginfo != NoDebugInfo {
177                 Some(debuginfo::CrateDebugContext::new(llmod))
178             } else {
179                 None
180             };
181
182             let mut ccx = CrateContext {
183                 llmod: llmod,
184                 llcx: llcx,
185                 metadata_llmod: metadata_llmod,
186                 td: td,
187                 tn: TypeNames::new(),
188                 externs: RefCell::new(HashMap::new()),
189                 item_vals: RefCell::new(NodeMap::new()),
190                 exp_map2: emap2,
191                 reachable: reachable,
192                 item_symbols: RefCell::new(NodeMap::new()),
193                 link_meta: link_meta,
194                 drop_glues: RefCell::new(HashMap::new()),
195                 tydescs: RefCell::new(HashMap::new()),
196                 finished_tydescs: Cell::new(false),
197                 external: RefCell::new(DefIdMap::new()),
198                 external_srcs: RefCell::new(NodeMap::new()),
199                 non_inlineable_statics: RefCell::new(NodeSet::new()),
200                 monomorphized: RefCell::new(HashMap::new()),
201                 monomorphizing: RefCell::new(DefIdMap::new()),
202                 vtables: RefCell::new(HashMap::new()),
203                 const_cstr_cache: RefCell::new(HashMap::new()),
204                 const_globals: RefCell::new(HashMap::new()),
205                 const_values: RefCell::new(NodeMap::new()),
206                 extern_const_values: RefCell::new(DefIdMap::new()),
207                 impl_method_cache: RefCell::new(HashMap::new()),
208                 closure_bare_wrapper_cache: RefCell::new(HashMap::new()),
209                 lltypes: RefCell::new(HashMap::new()),
210                 llsizingtypes: RefCell::new(HashMap::new()),
211                 adt_reprs: RefCell::new(HashMap::new()),
212                 symbol_hasher: RefCell::new(symbol_hasher),
213                 type_hashcodes: RefCell::new(HashMap::new()),
214                 all_llvm_symbols: RefCell::new(HashSet::new()),
215                 tcx: tcx,
216                 stats: Stats {
217                     n_static_tydescs: Cell::new(0u),
218                     n_glues_created: Cell::new(0u),
219                     n_null_glues: Cell::new(0u),
220                     n_real_glues: Cell::new(0u),
221                     n_fns: Cell::new(0u),
222                     n_monos: Cell::new(0u),
223                     n_inlines: Cell::new(0u),
224                     n_closures: Cell::new(0u),
225                     n_llvm_insns: Cell::new(0u),
226                     llvm_insns: RefCell::new(HashMap::new()),
227                     fn_stats: RefCell::new(Vec::new()),
228                 },
229                 int_type: Type::from_ref(ptr::mut_null()),
230                 opaque_vec_type: Type::from_ref(ptr::mut_null()),
231                 builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)),
232                 unboxed_closure_vals: RefCell::new(DefIdMap::new()),
233                 uses_gc: false,
234                 dbg_cx: dbg_cx,
235                 eh_personality: RefCell::new(None),
236                 intrinsics: RefCell::new(HashMap::new()),
237             };
238
239             ccx.int_type = Type::int(&ccx);
240             ccx.opaque_vec_type = Type::opaque_vec(&ccx);
241
242             let mut str_slice_ty = Type::named_struct(&ccx, "str_slice");
243             str_slice_ty.set_struct_body([Type::i8p(&ccx), ccx.int_type], false);
244             ccx.tn.associate_type("str_slice", &str_slice_ty);
245
246             ccx.tn.associate_type("tydesc", &Type::tydesc(&ccx, str_slice_ty));
247
248             if ccx.sess().count_llvm_insns() {
249                 base::init_insn_ctxt()
250             }
251
252             ccx
253         }
254     }
255
256     pub fn tcx<'a>(&'a self) -> &'a ty::ctxt {
257         &self.tcx
258     }
259
260     pub fn sess<'a>(&'a self) -> &'a Session {
261         &self.tcx.sess
262     }
263
264     pub fn builder<'a>(&'a self) -> Builder<'a> {
265         Builder::new(self)
266     }
267
268     pub fn tydesc_type(&self) -> Type {
269         self.tn.find_type("tydesc").unwrap()
270     }
271
272     pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
273         match self.intrinsics.borrow().find_copy(key) {
274             Some(v) => return v,
275             _ => {}
276         }
277         match declare_intrinsic(self, key) {
278             Some(v) => return v,
279             None => fail!()
280         }
281     }
282
283     // Although there is an experimental implementation of LLVM which
284     // supports SS on armv7 it wasn't approved by Apple, see:
285     // http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140505/216350.html
286     // It looks like it might be never accepted to upstream LLVM.
287     //
288     // So far the decision was to disable them in default builds
289     // but it could be enabled (with patched LLVM)
290     pub fn is_split_stack_supported(&self) -> bool {
291         let ref cfg = self.sess().targ_cfg;
292         cfg.os != abi::OsiOS || cfg.arch != abi::Arm
293     }
294 }
295
296 fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
297     macro_rules! ifn (
298         ($name:expr fn() -> $ret:expr) => (
299             if *key == $name {
300                 let f = base::decl_cdecl_fn(ccx, $name, Type::func([], &$ret), ty::mk_nil());
301                 ccx.intrinsics.borrow_mut().insert($name, f.clone());
302                 return Some(f);
303             }
304         );
305         ($name:expr fn($($arg:expr),*) -> $ret:expr) => (
306             if *key == $name {
307                 let f = base::decl_cdecl_fn(ccx, $name,
308                                   Type::func([$($arg),*], &$ret), ty::mk_nil());
309                 ccx.intrinsics.borrow_mut().insert($name, f.clone());
310                 return Some(f);
311             }
312         )
313     )
314     macro_rules! mk_struct (
315         ($($field_ty:expr),*) => (Type::struct_(ccx, [$($field_ty),*], false))
316     )
317
318     let i8p = Type::i8p(ccx);
319     let void = Type::void(ccx);
320     let i1 = Type::i1(ccx);
321     let t_i8 = Type::i8(ccx);
322     let t_i16 = Type::i16(ccx);
323     let t_i32 = Type::i32(ccx);
324     let t_i64 = Type::i64(ccx);
325     let t_f32 = Type::f32(ccx);
326     let t_f64 = Type::f64(ccx);
327
328     ifn!("llvm.memcpy.p0i8.p0i8.i32" fn(i8p, i8p, t_i32, t_i32, i1) -> void);
329     ifn!("llvm.memcpy.p0i8.p0i8.i64" fn(i8p, i8p, t_i64, t_i32, i1) -> void);
330     ifn!("llvm.memmove.p0i8.p0i8.i32" fn(i8p, i8p, t_i32, t_i32, i1) -> void);
331     ifn!("llvm.memmove.p0i8.p0i8.i64" fn(i8p, i8p, t_i64, t_i32, i1) -> void);
332     ifn!("llvm.memset.p0i8.i32" fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
333     ifn!("llvm.memset.p0i8.i64" fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
334
335     ifn!("llvm.trap" fn() -> void);
336     ifn!("llvm.debugtrap" fn() -> void);
337     ifn!("llvm.frameaddress" fn(t_i32) -> i8p);
338
339     ifn!("llvm.powi.f32" fn(t_f32, t_i32) -> t_f32);
340     ifn!("llvm.powi.f64" fn(t_f64, t_i32) -> t_f64);
341     ifn!("llvm.pow.f32" fn(t_f32, t_f32) -> t_f32);
342     ifn!("llvm.pow.f64" fn(t_f64, t_f64) -> t_f64);
343
344     ifn!("llvm.sqrt.f32" fn(t_f32) -> t_f32);
345     ifn!("llvm.sqrt.f64" fn(t_f64) -> t_f64);
346     ifn!("llvm.sin.f32" fn(t_f32) -> t_f32);
347     ifn!("llvm.sin.f64" fn(t_f64) -> t_f64);
348     ifn!("llvm.cos.f32" fn(t_f32) -> t_f32);
349     ifn!("llvm.cos.f64" fn(t_f64) -> t_f64);
350     ifn!("llvm.exp.f32" fn(t_f32) -> t_f32);
351     ifn!("llvm.exp.f64" fn(t_f64) -> t_f64);
352     ifn!("llvm.exp2.f32" fn(t_f32) -> t_f32);
353     ifn!("llvm.exp2.f64" fn(t_f64) -> t_f64);
354     ifn!("llvm.log.f32" fn(t_f32) -> t_f32);
355     ifn!("llvm.log.f64" fn(t_f64) -> t_f64);
356     ifn!("llvm.log10.f32" fn(t_f32) -> t_f32);
357     ifn!("llvm.log10.f64" fn(t_f64) -> t_f64);
358     ifn!("llvm.log2.f32" fn(t_f32) -> t_f32);
359     ifn!("llvm.log2.f64" fn(t_f64) -> t_f64);
360
361     ifn!("llvm.fma.f32" fn(t_f32, t_f32, t_f32) -> t_f32);
362     ifn!("llvm.fma.f64" fn(t_f64, t_f64, t_f64) -> t_f64);
363
364     ifn!("llvm.fabs.f32" fn(t_f32) -> t_f32);
365     ifn!("llvm.fabs.f64" fn(t_f64) -> t_f64);
366
367     ifn!("llvm.floor.f32" fn(t_f32) -> t_f32);
368     ifn!("llvm.floor.f64" fn(t_f64) -> t_f64);
369     ifn!("llvm.ceil.f32" fn(t_f32) -> t_f32);
370     ifn!("llvm.ceil.f64" fn(t_f64) -> t_f64);
371     ifn!("llvm.trunc.f32" fn(t_f32) -> t_f32);
372     ifn!("llvm.trunc.f64" fn(t_f64) -> t_f64);
373
374     ifn!("llvm.rint.f32" fn(t_f32) -> t_f32);
375     ifn!("llvm.rint.f64" fn(t_f64) -> t_f64);
376     ifn!("llvm.nearbyint.f32" fn(t_f32) -> t_f32);
377     ifn!("llvm.nearbyint.f64" fn(t_f64) -> t_f64);
378
379     ifn!("llvm.ctpop.i8" fn(t_i8) -> t_i8);
380     ifn!("llvm.ctpop.i16" fn(t_i16) -> t_i16);
381     ifn!("llvm.ctpop.i32" fn(t_i32) -> t_i32);
382     ifn!("llvm.ctpop.i64" fn(t_i64) -> t_i64);
383
384     ifn!("llvm.ctlz.i8" fn(t_i8 , i1) -> t_i8);
385     ifn!("llvm.ctlz.i16" fn(t_i16, i1) -> t_i16);
386     ifn!("llvm.ctlz.i32" fn(t_i32, i1) -> t_i32);
387     ifn!("llvm.ctlz.i64" fn(t_i64, i1) -> t_i64);
388
389     ifn!("llvm.cttz.i8" fn(t_i8 , i1) -> t_i8);
390     ifn!("llvm.cttz.i16" fn(t_i16, i1) -> t_i16);
391     ifn!("llvm.cttz.i32" fn(t_i32, i1) -> t_i32);
392     ifn!("llvm.cttz.i64" fn(t_i64, i1) -> t_i64);
393
394     ifn!("llvm.bswap.i16" fn(t_i16) -> t_i16);
395     ifn!("llvm.bswap.i32" fn(t_i32) -> t_i32);
396     ifn!("llvm.bswap.i64" fn(t_i64) -> t_i64);
397
398     ifn!("llvm.sadd.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
399     ifn!("llvm.sadd.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
400     ifn!("llvm.sadd.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
401     ifn!("llvm.sadd.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
402
403     ifn!("llvm.uadd.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
404     ifn!("llvm.uadd.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
405     ifn!("llvm.uadd.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
406     ifn!("llvm.uadd.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
407
408     ifn!("llvm.ssub.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
409     ifn!("llvm.ssub.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
410     ifn!("llvm.ssub.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
411     ifn!("llvm.ssub.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
412
413     ifn!("llvm.usub.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
414     ifn!("llvm.usub.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
415     ifn!("llvm.usub.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
416     ifn!("llvm.usub.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
417
418     ifn!("llvm.smul.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
419     ifn!("llvm.smul.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
420     ifn!("llvm.smul.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
421     ifn!("llvm.smul.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
422
423     ifn!("llvm.umul.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
424     ifn!("llvm.umul.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
425     ifn!("llvm.umul.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
426     ifn!("llvm.umul.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
427
428     ifn!("llvm.lifetime.start" fn(t_i64,i8p) -> void);
429     ifn!("llvm.lifetime.end" fn(t_i64, i8p) -> void);
430
431     ifn!("llvm.expect.i1" fn(i1, i1) -> i1);
432
433     // Some intrinsics were introduced in later versions of LLVM, but they have
434     // fallbacks in libc or libm and such. Currently, all of these intrinsics
435     // were introduced in LLVM 3.4, so we case on that.
436     macro_rules! compatible_ifn (
437         ($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr) => (
438             if unsafe { llvm::LLVMVersionMinor() >= 4 } {
439                 // The `if key == $name` is already in ifn!
440                 ifn!($name fn($($arg),*) -> $ret);
441             } else if *key == $name {
442                 let f = base::decl_cdecl_fn(ccx, stringify!($cname),
443                                       Type::func([$($arg),*], &$ret),
444                                       ty::mk_nil());
445                 ccx.intrinsics.borrow_mut().insert($name, f.clone());
446                 return Some(f);
447             }
448         )
449     )
450
451     compatible_ifn!("llvm.copysign.f32", copysignf(t_f32, t_f32) -> t_f32);
452     compatible_ifn!("llvm.copysign.f64", copysign(t_f64, t_f64) -> t_f64);
453     compatible_ifn!("llvm.round.f32", roundf(t_f32) -> t_f32);
454     compatible_ifn!("llvm.round.f64", round(t_f64) -> t_f64);
455
456
457     if ccx.sess().opts.debuginfo != NoDebugInfo {
458         ifn!("llvm.dbg.declare" fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
459         ifn!("llvm.dbg.value" fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);
460     }
461     return None;
462 }