]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/common.rs
refactor `ParamEnv::empty(Reveal)` into two distinct methods
[rust.git] / src / librustc_trans / common.rs
1 // Copyright 2012-2014 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 #![allow(non_camel_case_types, non_snake_case)]
12
13 //! Code that is useful in various trans modules.
14
15 use llvm;
16 use llvm::{ValueRef, ContextRef, TypeKind};
17 use llvm::{True, False, Bool, OperandBundleDef};
18 use rustc::hir::def_id::DefId;
19 use rustc::middle::lang_items::LangItem;
20 use abi;
21 use base;
22 use builder::Builder;
23 use consts;
24 use declare;
25 use type_::Type;
26 use type_of::LayoutLlvmExt;
27 use value::Value;
28 use rustc::ty::{self, Ty, TyCtxt};
29 use rustc::ty::layout::{HasDataLayout, LayoutOf};
30 use rustc::hir;
31
32 use libc::{c_uint, c_char};
33 use std::iter;
34
35 use syntax::abi::Abi;
36 use syntax::symbol::InternedString;
37 use syntax_pos::{Span, DUMMY_SP};
38
39 pub use context::CodegenCx;
40
41 pub fn type_needs_drop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
42     ty.needs_drop(tcx, ty::ParamEnv::reveal_all())
43 }
44
45 pub fn type_is_sized<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
46     ty.is_sized(tcx.at(DUMMY_SP), ty::ParamEnv::reveal_all())
47 }
48
49 pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
50     ty.is_freeze(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP)
51 }
52
53 /*
54 * A note on nomenclature of linking: "extern", "foreign", and "upcall".
55 *
56 * An "extern" is an LLVM symbol we wind up emitting an undefined external
57 * reference to. This means "we don't have the thing in this compilation unit,
58 * please make sure you link it in at runtime". This could be a reference to
59 * C code found in a C library, or rust code found in a rust crate.
60 *
61 * Most "externs" are implicitly declared (automatically) as a result of a
62 * user declaring an extern _module_ dependency; this causes the rust driver
63 * to locate an extern crate, scan its compilation metadata, and emit extern
64 * declarations for any symbols used by the declaring crate.
65 *
66 * A "foreign" is an extern that references C (or other non-rust ABI) code.
67 * There is no metadata to scan for extern references so in these cases either
68 * a header-digester like bindgen, or manual function prototypes, have to
69 * serve as declarators. So these are usually given explicitly as prototype
70 * declarations, in rust code, with ABI attributes on them noting which ABI to
71 * link via.
72 *
73 * An "upcall" is a foreign call generated by the compiler (not corresponding
74 * to any user-written call in the code) into the runtime library, to perform
75 * some helper task such as bringing a task to life, allocating memory, etc.
76 *
77 */
78
79 /// A structure representing an active landing pad for the duration of a basic
80 /// block.
81 ///
82 /// Each `Block` may contain an instance of this, indicating whether the block
83 /// is part of a landing pad or not. This is used to make decision about whether
84 /// to emit `invoke` instructions (e.g. in a landing pad we don't continue to
85 /// use `invoke`) and also about various function call metadata.
86 ///
87 /// For GNU exceptions (`landingpad` + `resume` instructions) this structure is
88 /// just a bunch of `None` instances (not too interesting), but for MSVC
89 /// exceptions (`cleanuppad` + `cleanupret` instructions) this contains data.
90 /// When inside of a landing pad, each function call in LLVM IR needs to be
91 /// annotated with which landing pad it's a part of. This is accomplished via
92 /// the `OperandBundleDef` value created for MSVC landing pads.
93 pub struct Funclet {
94     cleanuppad: ValueRef,
95     operand: OperandBundleDef,
96 }
97
98 impl Funclet {
99     pub fn new(cleanuppad: ValueRef) -> Funclet {
100         Funclet {
101             cleanuppad,
102             operand: OperandBundleDef::new("funclet", &[cleanuppad]),
103         }
104     }
105
106     pub fn cleanuppad(&self) -> ValueRef {
107         self.cleanuppad
108     }
109
110     pub fn bundle(&self) -> &OperandBundleDef {
111         &self.operand
112     }
113 }
114
115 pub fn val_ty(v: ValueRef) -> Type {
116     unsafe {
117         Type::from_ref(llvm::LLVMTypeOf(v))
118     }
119 }
120
121 // LLVM constant constructors.
122 pub fn C_null(t: Type) -> ValueRef {
123     unsafe {
124         llvm::LLVMConstNull(t.to_ref())
125     }
126 }
127
128 pub fn C_undef(t: Type) -> ValueRef {
129     unsafe {
130         llvm::LLVMGetUndef(t.to_ref())
131     }
132 }
133
134 pub fn C_int(t: Type, i: i64) -> ValueRef {
135     unsafe {
136         llvm::LLVMConstInt(t.to_ref(), i as u64, True)
137     }
138 }
139
140 pub fn C_uint(t: Type, i: u64) -> ValueRef {
141     unsafe {
142         llvm::LLVMConstInt(t.to_ref(), i, False)
143     }
144 }
145
146 pub fn C_uint_big(t: Type, u: u128) -> ValueRef {
147     unsafe {
148         let words = [u as u64, (u >> 64) as u64];
149         llvm::LLVMConstIntOfArbitraryPrecision(t.to_ref(), 2, words.as_ptr())
150     }
151 }
152
153 pub fn C_bool(cx: &CodegenCx, val: bool) -> ValueRef {
154     C_uint(Type::i1(cx), val as u64)
155 }
156
157 pub fn C_i32(cx: &CodegenCx, i: i32) -> ValueRef {
158     C_int(Type::i32(cx), i as i64)
159 }
160
161 pub fn C_u32(cx: &CodegenCx, i: u32) -> ValueRef {
162     C_uint(Type::i32(cx), i as u64)
163 }
164
165 pub fn C_u64(cx: &CodegenCx, i: u64) -> ValueRef {
166     C_uint(Type::i64(cx), i)
167 }
168
169 pub fn C_usize(cx: &CodegenCx, i: u64) -> ValueRef {
170     let bit_size = cx.data_layout().pointer_size.bits();
171     if bit_size < 64 {
172         // make sure it doesn't overflow
173         assert!(i < (1<<bit_size));
174     }
175
176     C_uint(cx.isize_ty, i)
177 }
178
179 pub fn C_u8(cx: &CodegenCx, i: u8) -> ValueRef {
180     C_uint(Type::i8(cx), i as u64)
181 }
182
183
184 // This is a 'c-like' raw string, which differs from
185 // our boxed-and-length-annotated strings.
186 pub fn C_cstr(cx: &CodegenCx, s: InternedString, null_terminated: bool) -> ValueRef {
187     unsafe {
188         if let Some(&llval) = cx.const_cstr_cache.borrow().get(&s) {
189             return llval;
190         }
191
192         let sc = llvm::LLVMConstStringInContext(cx.llcx,
193                                                 s.as_ptr() as *const c_char,
194                                                 s.len() as c_uint,
195                                                 !null_terminated as Bool);
196         let sym = cx.generate_local_symbol_name("str");
197         let g = declare::define_global(cx, &sym[..], val_ty(sc)).unwrap_or_else(||{
198             bug!("symbol `{}` is already defined", sym);
199         });
200         llvm::LLVMSetInitializer(g, sc);
201         llvm::LLVMSetGlobalConstant(g, True);
202         llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
203
204         cx.const_cstr_cache.borrow_mut().insert(s, g);
205         g
206     }
207 }
208
209 // NB: Do not use `do_spill_noroot` to make this into a constant string, or
210 // you will be kicked off fast isel. See issue #4352 for an example of this.
211 pub fn C_str_slice(cx: &CodegenCx, s: InternedString) -> ValueRef {
212     let len = s.len();
213     let cs = consts::ptrcast(C_cstr(cx, s, false),
214         cx.layout_of(cx.tcx.mk_str()).llvm_type(cx).ptr_to());
215     C_fat_ptr(cx, cs, C_usize(cx, len as u64))
216 }
217
218 pub fn C_fat_ptr(cx: &CodegenCx, ptr: ValueRef, meta: ValueRef) -> ValueRef {
219     assert_eq!(abi::FAT_PTR_ADDR, 0);
220     assert_eq!(abi::FAT_PTR_EXTRA, 1);
221     C_struct(cx, &[ptr, meta], false)
222 }
223
224 pub fn C_struct(cx: &CodegenCx, elts: &[ValueRef], packed: bool) -> ValueRef {
225     C_struct_in_context(cx.llcx, elts, packed)
226 }
227
228 pub fn C_struct_in_context(llcx: ContextRef, elts: &[ValueRef], packed: bool) -> ValueRef {
229     unsafe {
230         llvm::LLVMConstStructInContext(llcx,
231                                        elts.as_ptr(), elts.len() as c_uint,
232                                        packed as Bool)
233     }
234 }
235
236 pub fn C_array(ty: Type, elts: &[ValueRef]) -> ValueRef {
237     unsafe {
238         return llvm::LLVMConstArray(ty.to_ref(), elts.as_ptr(), elts.len() as c_uint);
239     }
240 }
241
242 pub fn C_vector(elts: &[ValueRef]) -> ValueRef {
243     unsafe {
244         return llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint);
245     }
246 }
247
248 pub fn C_bytes(cx: &CodegenCx, bytes: &[u8]) -> ValueRef {
249     C_bytes_in_context(cx.llcx, bytes)
250 }
251
252 pub fn C_bytes_in_context(llcx: ContextRef, bytes: &[u8]) -> ValueRef {
253     unsafe {
254         let ptr = bytes.as_ptr() as *const c_char;
255         return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
256     }
257 }
258
259 pub fn const_get_elt(v: ValueRef, idx: u64) -> ValueRef {
260     unsafe {
261         assert_eq!(idx as c_uint as u64, idx);
262         let us = &[idx as c_uint];
263         let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
264
265         debug!("const_get_elt(v={:?}, idx={}, r={:?})",
266                Value(v), idx, Value(r));
267
268         r
269     }
270 }
271
272 pub fn const_to_uint(v: ValueRef) -> u64 {
273     unsafe {
274         llvm::LLVMConstIntGetZExtValue(v)
275     }
276 }
277
278 pub fn is_const_integral(v: ValueRef) -> bool {
279     unsafe {
280         !llvm::LLVMIsAConstantInt(v).is_null()
281     }
282 }
283
284 #[inline]
285 fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
286     ((hi as u128) << 64) | (lo as u128)
287 }
288
289 pub fn const_to_opt_u128(v: ValueRef, sign_ext: bool) -> Option<u128> {
290     unsafe {
291         if is_const_integral(v) {
292             let (mut lo, mut hi) = (0u64, 0u64);
293             let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
294                                                        &mut hi as *mut u64, &mut lo as *mut u64);
295             if success {
296                 Some(hi_lo_to_u128(lo, hi))
297             } else {
298                 None
299             }
300         } else {
301             None
302         }
303     }
304 }
305
306 pub fn langcall(tcx: TyCtxt,
307                 span: Option<Span>,
308                 msg: &str,
309                 li: LangItem)
310                 -> DefId {
311     match tcx.lang_items().require(li) {
312         Ok(id) => id,
313         Err(s) => {
314             let msg = format!("{} {}", msg, s);
315             match span {
316                 Some(span) => tcx.sess.span_fatal(span, &msg[..]),
317                 None => tcx.sess.fatal(&msg[..]),
318             }
319         }
320     }
321 }
322
323 // To avoid UB from LLVM, these two functions mask RHS with an
324 // appropriate mask unconditionally (i.e. the fallback behavior for
325 // all shifts). For 32- and 64-bit types, this matches the semantics
326 // of Java. (See related discussion on #1877 and #10183.)
327
328 pub fn build_unchecked_lshift<'a, 'tcx>(
329     bx: &Builder<'a, 'tcx>,
330     lhs: ValueRef,
331     rhs: ValueRef
332 ) -> ValueRef {
333     let rhs = base::cast_shift_expr_rhs(bx, hir::BinOp_::BiShl, lhs, rhs);
334     // #1877, #10183: Ensure that input is always valid
335     let rhs = shift_mask_rhs(bx, rhs);
336     bx.shl(lhs, rhs)
337 }
338
339 pub fn build_unchecked_rshift<'a, 'tcx>(
340     bx: &Builder<'a, 'tcx>, lhs_t: Ty<'tcx>, lhs: ValueRef, rhs: ValueRef
341 ) -> ValueRef {
342     let rhs = base::cast_shift_expr_rhs(bx, hir::BinOp_::BiShr, lhs, rhs);
343     // #1877, #10183: Ensure that input is always valid
344     let rhs = shift_mask_rhs(bx, rhs);
345     let is_signed = lhs_t.is_signed();
346     if is_signed {
347         bx.ashr(lhs, rhs)
348     } else {
349         bx.lshr(lhs, rhs)
350     }
351 }
352
353 fn shift_mask_rhs<'a, 'tcx>(bx: &Builder<'a, 'tcx>, rhs: ValueRef) -> ValueRef {
354     let rhs_llty = val_ty(rhs);
355     bx.and(rhs, shift_mask_val(bx, rhs_llty, rhs_llty, false))
356 }
357
358 pub fn shift_mask_val<'a, 'tcx>(
359     bx: &Builder<'a, 'tcx>,
360     llty: Type,
361     mask_llty: Type,
362     invert: bool
363 ) -> ValueRef {
364     let kind = llty.kind();
365     match kind {
366         TypeKind::Integer => {
367             // i8/u8 can shift by at most 7, i16/u16 by at most 15, etc.
368             let val = llty.int_width() - 1;
369             if invert {
370                 C_int(mask_llty, !val as i64)
371             } else {
372                 C_uint(mask_llty, val)
373             }
374         },
375         TypeKind::Vector => {
376             let mask = shift_mask_val(bx, llty.element_type(), mask_llty.element_type(), invert);
377             bx.vector_splat(mask_llty.vector_length(), mask)
378         },
379         _ => bug!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
380     }
381 }
382
383 pub fn ty_fn_sig<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
384                            ty: Ty<'tcx>)
385                            -> ty::PolyFnSig<'tcx>
386 {
387     match ty.sty {
388         ty::TyFnDef(..) |
389         // Shims currently have type TyFnPtr. Not sure this should remain.
390         ty::TyFnPtr(_) => ty.fn_sig(cx.tcx),
391         ty::TyClosure(def_id, substs) => {
392             let tcx = cx.tcx;
393             let sig = substs.closure_sig(def_id, tcx);
394
395             let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
396             sig.map_bound(|sig| tcx.mk_fn_sig(
397                 iter::once(*env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
398                 sig.output(),
399                 sig.variadic,
400                 sig.unsafety,
401                 sig.abi
402             ))
403         }
404         ty::TyGenerator(def_id, substs, _) => {
405             let tcx = cx.tcx;
406             let sig = substs.generator_poly_sig(def_id, cx.tcx);
407
408             let env_region = ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrEnv);
409             let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
410
411             sig.map_bound(|sig| {
412                 let state_did = tcx.lang_items().gen_state().unwrap();
413                 let state_adt_ref = tcx.adt_def(state_did);
414                 let state_substs = tcx.mk_substs([sig.yield_ty.into(),
415                     sig.return_ty.into()].iter());
416                 let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
417
418                 tcx.mk_fn_sig(iter::once(env_ty),
419                     ret_ty,
420                     false,
421                     hir::Unsafety::Normal,
422                     Abi::Rust
423                 )
424             })
425         }
426         _ => bug!("unexpected type {:?} to ty_fn_sig", ty)
427     }
428 }
429