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