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
12 use driver::session::NoDebugInfo;
13 use driver::session::Session;
14 use lib::llvm::{ContextRef, ModuleRef, ValueRef};
15 use lib::llvm::{llvm, TargetData, TypeNames};
16 use lib::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;
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 collections::{HashMap, HashSet};
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<~str, uint>>,
49 // (ident, time-in-ms, llvm-instructions)
50 pub fn_stats: RefCell<Vec<(~str, 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<~str>>,
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 pevent 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, ~str>>,
113 pub all_llvm_symbols: RefCell<HashSet<~str>>,
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 intrinsics: RefCell<HashMap<&'static str, ValueRef>>,
126 }
127
128 impl CrateContext {
129 pub fn new(name: &str,
130 tcx: ty::ctxt,
131 emap2: resolve::ExportMap2,
132 symbol_hasher: Sha256,
133 link_meta: LinkMeta,
134 reachable: NodeSet)
135 -> CrateContext {
136 unsafe {
137 let llcx = llvm::LLVMContextCreate();
138 let llmod = name.with_c_str(|buf| {
139 llvm::LLVMModuleCreateWithNameInContext(buf, llcx)
140 });
141 let metadata_llmod = format!("{}_metadata", name).with_c_str(|buf| {
142 llvm::LLVMModuleCreateWithNameInContext(buf, llcx)
143 });
144 tcx.sess.targ_cfg.target_strs.data_layout.with_c_str(|buf| {
145 llvm::LLVMSetDataLayout(llmod, buf);
146 llvm::LLVMSetDataLayout(metadata_llmod, buf);
147 });
148 tcx.sess.targ_cfg.target_strs.target_triple.with_c_str(|buf| {
149 llvm::LLVMRustSetNormalizedTarget(llmod, buf);
150 llvm::LLVMRustSetNormalizedTarget(metadata_llmod, buf);
151 });
152
153 let td = mk_target_data(tcx.sess.targ_cfg.target_strs.data_layout);
154
155 let dbg_cx = if tcx.sess.opts.debuginfo != NoDebugInfo {
156 Some(debuginfo::CrateDebugContext::new(llmod))
157 } else {
158 None
159 };
160
161 let mut ccx = CrateContext {
162 llmod: llmod,
163 llcx: llcx,
164 metadata_llmod: metadata_llmod,
165 td: td,
166 tn: TypeNames::new(),
167 externs: RefCell::new(HashMap::new()),
168 item_vals: RefCell::new(NodeMap::new()),
169 exp_map2: emap2,
170 reachable: reachable,
171 item_symbols: RefCell::new(NodeMap::new()),
172 link_meta: link_meta,
173 drop_glues: RefCell::new(HashMap::new()),
174 tydescs: RefCell::new(HashMap::new()),
175 finished_tydescs: Cell::new(false),
176 external: RefCell::new(DefIdMap::new()),
177 external_srcs: RefCell::new(NodeMap::new()),
178 non_inlineable_statics: RefCell::new(NodeSet::new()),
179 monomorphized: RefCell::new(HashMap::new()),
180 monomorphizing: RefCell::new(DefIdMap::new()),
181 vtables: RefCell::new(HashMap::new()),
182 const_cstr_cache: RefCell::new(HashMap::new()),
183 const_globals: RefCell::new(HashMap::new()),
184 const_values: RefCell::new(NodeMap::new()),
185 extern_const_values: RefCell::new(DefIdMap::new()),
186 impl_method_cache: RefCell::new(HashMap::new()),
187 closure_bare_wrapper_cache: RefCell::new(HashMap::new()),
188 lltypes: RefCell::new(HashMap::new()),
189 llsizingtypes: RefCell::new(HashMap::new()),
190 adt_reprs: RefCell::new(HashMap::new()),
191 symbol_hasher: RefCell::new(symbol_hasher),
192 type_hashcodes: RefCell::new(HashMap::new()),
193 all_llvm_symbols: RefCell::new(HashSet::new()),
194 tcx: tcx,
195 stats: Stats {
196 n_static_tydescs: Cell::new(0u),
197 n_glues_created: Cell::new(0u),
198 n_null_glues: Cell::new(0u),
199 n_real_glues: Cell::new(0u),
200 n_fns: Cell::new(0u),
201 n_monos: Cell::new(0u),
202 n_inlines: Cell::new(0u),
203 n_closures: Cell::new(0u),
204 n_llvm_insns: Cell::new(0u),
205 llvm_insns: RefCell::new(HashMap::new()),
206 fn_stats: RefCell::new(Vec::new()),
207 },
208 int_type: Type::from_ref(ptr::null()),
209 opaque_vec_type: Type::from_ref(ptr::null()),
210 builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)),
211 uses_gc: false,
212 dbg_cx: dbg_cx,
213 intrinsics: RefCell::new(HashMap::new()),
214 };
215
216 ccx.int_type = Type::int(&ccx);
217 ccx.opaque_vec_type = Type::opaque_vec(&ccx);
218
219 ccx.tn.associate_type("tydesc", &Type::tydesc(&ccx));
220
221 let mut str_slice_ty = Type::named_struct(&ccx, "str_slice");
222 str_slice_ty.set_struct_body([Type::i8p(&ccx), ccx.int_type], false);
223 ccx.tn.associate_type("str_slice", &str_slice_ty);
224
225 if ccx.sess().count_llvm_insns() {
226 base::init_insn_ctxt()
227 }
228
229 ccx
230 }
231 }
232
233 pub fn tcx<'a>(&'a self) -> &'a ty::ctxt {
234 &self.tcx
235 }
236
237 pub fn sess<'a>(&'a self) -> &'a Session {
238 &self.tcx.sess
239 }
240
241 pub fn builder<'a>(&'a self) -> Builder<'a> {
242 Builder::new(self)
243 }
244
245 pub fn tydesc_type(&self) -> Type {
246 self.tn.find_type("tydesc").unwrap()
247 }
248
249 pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
250 match self.intrinsics.borrow().find_copy(key) {
251 Some(v) => return v,
252 _ => {}
253 }
254 match declare_intrinsic(self, key) {
255 Some(v) => return v,
256 None => fail!()
257 }
258 }
259 }
260
261 fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
262 macro_rules! ifn (
263 ($name:expr fn() -> $ret:expr) => (
264 if *key == $name {
265 let f = base::decl_cdecl_fn(ccx.llmod, $name, Type::func([], &$ret), ty::mk_nil());
266 ccx.intrinsics.borrow_mut().insert($name, f.clone());
267 return Some(f);
268 }
269 );
270 ($name:expr fn($($arg:expr),*) -> $ret:expr) => (
271 if *key == $name {
272 let f = base::decl_cdecl_fn(ccx.llmod, $name,
273 Type::func([$($arg),*], &$ret), ty::mk_nil());
274 ccx.intrinsics.borrow_mut().insert($name, f.clone());
275 return Some(f);
276 }
277 )
278 )
279 macro_rules! mk_struct (
280 ($($field_ty:expr),*) => (Type::struct_(ccx, [$($field_ty),*], false))
281 )
282
283 let i8p = Type::i8p(ccx);
284 let void = Type::void(ccx);
285 let i1 = Type::i1(ccx);
286 let t_i8 = Type::i8(ccx);
287 let t_i16 = Type::i16(ccx);
288 let t_i32 = Type::i32(ccx);
289 let t_i64 = Type::i64(ccx);
290 let t_f32 = Type::f32(ccx);
291 let t_f64 = Type::f64(ccx);
292
293 ifn!("llvm.memcpy.p0i8.p0i8.i32" fn(i8p, i8p, t_i32, t_i32, i1) -> void);
294 ifn!("llvm.memcpy.p0i8.p0i8.i64" fn(i8p, i8p, t_i64, t_i32, i1) -> void);
295 ifn!("llvm.memmove.p0i8.p0i8.i32" fn(i8p, i8p, t_i32, t_i32, i1) -> void);
296 ifn!("llvm.memmove.p0i8.p0i8.i64" fn(i8p, i8p, t_i64, t_i32, i1) -> void);
297 ifn!("llvm.memset.p0i8.i32" fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
298 ifn!("llvm.memset.p0i8.i64" fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
299
300 ifn!("llvm.trap" fn() -> void);
301 ifn!("llvm.debugtrap" fn() -> void);
302 ifn!("llvm.frameaddress" fn(t_i32) -> i8p);
303
304 ifn!("llvm.powi.f32" fn(t_f32, t_i32) -> t_f32);
305 ifn!("llvm.powi.f64" fn(t_f64, t_i32) -> t_f64);
306 ifn!("llvm.pow.f32" fn(t_f32, t_f32) -> t_f32);
307 ifn!("llvm.pow.f64" fn(t_f64, t_f64) -> t_f64);
308
309 ifn!("llvm.sqrt.f32" fn(t_f32) -> t_f32);
310 ifn!("llvm.sqrt.f64" fn(t_f64) -> t_f64);
311 ifn!("llvm.sin.f32" fn(t_f32) -> t_f32);
312 ifn!("llvm.sin.f64" fn(t_f64) -> t_f64);
313 ifn!("llvm.cos.f32" fn(t_f32) -> t_f32);
314 ifn!("llvm.cos.f64" fn(t_f64) -> t_f64);
315 ifn!("llvm.exp.f32" fn(t_f32) -> t_f32);
316 ifn!("llvm.exp.f64" fn(t_f64) -> t_f64);
317 ifn!("llvm.exp2.f32" fn(t_f32) -> t_f32);
318 ifn!("llvm.exp2.f64" fn(t_f64) -> t_f64);
319 ifn!("llvm.log.f32" fn(t_f32) -> t_f32);
320 ifn!("llvm.log.f64" fn(t_f64) -> t_f64);
321 ifn!("llvm.log10.f32" fn(t_f32) -> t_f32);
322 ifn!("llvm.log10.f64" fn(t_f64) -> t_f64);
323 ifn!("llvm.log2.f32" fn(t_f32) -> t_f32);
324 ifn!("llvm.log2.f64" fn(t_f64) -> t_f64);
325
326 ifn!("llvm.fma.f32" fn(t_f32, t_f32, t_f32) -> t_f32);
327 ifn!("llvm.fma.f64" fn(t_f64, t_f64, t_f64) -> t_f64);
328
329 ifn!("llvm.fabs.f32" fn(t_f32) -> t_f32);
330 ifn!("llvm.fabs.f64" fn(t_f64) -> t_f64);
331
332 ifn!("llvm.floor.f32" fn(t_f32) -> t_f32);
333 ifn!("llvm.floor.f64" fn(t_f64) -> t_f64);
334 ifn!("llvm.ceil.f32" fn(t_f32) -> t_f32);
335 ifn!("llvm.ceil.f64" fn(t_f64) -> t_f64);
336 ifn!("llvm.trunc.f32" fn(t_f32) -> t_f32);
337 ifn!("llvm.trunc.f64" fn(t_f64) -> t_f64);
338
339 ifn!("llvm.rint.f32" fn(t_f32) -> t_f32);
340 ifn!("llvm.rint.f64" fn(t_f64) -> t_f64);
341 ifn!("llvm.nearbyint.f32" fn(t_f32) -> t_f32);
342 ifn!("llvm.nearbyint.f64" fn(t_f64) -> t_f64);
343
344 ifn!("llvm.ctpop.i8" fn(t_i8) -> t_i8);
345 ifn!("llvm.ctpop.i16" fn(t_i16) -> t_i16);
346 ifn!("llvm.ctpop.i32" fn(t_i32) -> t_i32);
347 ifn!("llvm.ctpop.i64" fn(t_i64) -> t_i64);
348
349 ifn!("llvm.ctlz.i8" fn(t_i8 , i1) -> t_i8);
350 ifn!("llvm.ctlz.i16" fn(t_i16, i1) -> t_i16);
351 ifn!("llvm.ctlz.i32" fn(t_i32, i1) -> t_i32);
352 ifn!("llvm.ctlz.i64" fn(t_i64, i1) -> t_i64);
353
354 ifn!("llvm.cttz.i8" fn(t_i8 , i1) -> t_i8);
355 ifn!("llvm.cttz.i16" fn(t_i16, i1) -> t_i16);
356 ifn!("llvm.cttz.i32" fn(t_i32, i1) -> t_i32);
357 ifn!("llvm.cttz.i64" fn(t_i64, i1) -> t_i64);
358
359 ifn!("llvm.bswap.i16" fn(t_i16) -> t_i16);
360 ifn!("llvm.bswap.i32" fn(t_i32) -> t_i32);
361 ifn!("llvm.bswap.i64" fn(t_i64) -> t_i64);
362
363 ifn!("llvm.sadd.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
364 ifn!("llvm.sadd.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
365 ifn!("llvm.sadd.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
366 ifn!("llvm.sadd.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
367
368 ifn!("llvm.uadd.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
369 ifn!("llvm.uadd.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
370 ifn!("llvm.uadd.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
371 ifn!("llvm.uadd.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
372
373 ifn!("llvm.ssub.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
374 ifn!("llvm.ssub.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
375 ifn!("llvm.ssub.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
376 ifn!("llvm.ssub.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
377
378 ifn!("llvm.usub.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
379 ifn!("llvm.usub.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
380 ifn!("llvm.usub.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
381 ifn!("llvm.usub.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
382
383 ifn!("llvm.smul.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
384 ifn!("llvm.smul.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
385 ifn!("llvm.smul.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
386 ifn!("llvm.smul.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
387
388 ifn!("llvm.umul.with.overflow.i8" fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
389 ifn!("llvm.umul.with.overflow.i16" fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
390 ifn!("llvm.umul.with.overflow.i32" fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
391 ifn!("llvm.umul.with.overflow.i64" fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
392
393 ifn!("llvm.expect.i1" fn(i1, i1) -> i1);
394
395 // Some intrinsics were introduced in later versions of LLVM, but they have
396 // fallbacks in libc or libm and such. Currently, all of these intrinsics
397 // were introduced in LLVM 3.4, so we case on that.
398 macro_rules! compatible_ifn (
399 ($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr) => (
400 if unsafe { llvm::LLVMVersionMinor() >= 4 } {
401 // The `if key == $name` is already in ifn!
402 ifn!($name fn($($arg),*) -> $ret);
403 } else if *key == $name {
404 let f = base::decl_cdecl_fn(ccx.llmod, stringify!($cname),
405 Type::func([$($arg),*], &$ret),
406 ty::mk_nil());
407 ccx.intrinsics.borrow_mut().insert($name, f.clone());
408 return Some(f);
409 }
410 )
411 )
412
413 compatible_ifn!("llvm.copysign.f32", copysignf(t_f32, t_f32) -> t_f32);
414 compatible_ifn!("llvm.copysign.f64", copysign(t_f64, t_f64) -> t_f64);
415 compatible_ifn!("llvm.round.f32", roundf(t_f32) -> t_f32);
416 compatible_ifn!("llvm.round.f64", round(t_f64) -> t_f64);
417
418
419 if ccx.sess().opts.debuginfo != NoDebugInfo {
420 ifn!("llvm.dbg.declare" fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
421 ifn!("llvm.dbg.value" fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);
422 }
423 return None;
424 }