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.
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.
11 #![allow(non_camel_case_types, non_snake_case)]
13 //! Code that is useful in various codegen modules.
15 use llvm::{self, TypeKind};
16 use llvm::{True, False, Bool, BasicBlock};
17 use rustc::hir::def_id::DefId;
18 use rustc::middle::lang_items::LangItem;
25 use type_of::LayoutLlvmExt;
27 use interfaces::{Backend, CommonMethods, CommonWriteMethods};
29 use rustc::ty::{self, Ty, TyCtxt};
30 use rustc::ty::layout::{HasDataLayout, LayoutOf};
32 use interfaces::BuilderMethods;
34 use libc::{c_uint, c_char};
36 use syntax::symbol::LocalInternedString;
37 use syntax_pos::{Span, DUMMY_SP};
39 pub use context::CodegenCx;
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())
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())
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)
53 pub struct OperandBundleDef<'a, Value : 'a> {
58 impl OperandBundleDef<'ll, &'ll Value> {
59 pub fn new(name: &'ll str, val: &'ll Value) -> Self {
67 pub enum IntPredicate {
81 pub enum RealPredicate {
100 pub enum AtomicRmwBinOp {
114 pub enum AtomicOrdering {
119 // Consume, // Not specified yet.
123 SequentiallyConsistent,
126 pub enum SynchronizationScope {
127 // FIXME: figure out if this variant is needed at all.
135 * A note on nomenclature of linking: "extern", "foreign", and "upcall".
137 * An "extern" is an LLVM symbol we wind up emitting an undefined external
138 * reference to. This means "we don't have the thing in this compilation unit,
139 * please make sure you link it in at runtime". This could be a reference to
140 * C code found in a C library, or rust code found in a rust crate.
142 * Most "externs" are implicitly declared (automatically) as a result of a
143 * user declaring an extern _module_ dependency; this causes the rust driver
144 * to locate an extern crate, scan its compilation metadata, and emit extern
145 * declarations for any symbols used by the declaring crate.
147 * A "foreign" is an extern that references C (or other non-rust ABI) code.
148 * There is no metadata to scan for extern references so in these cases either
149 * a header-digester like bindgen, or manual function prototypes, have to
150 * serve as declarators. So these are usually given explicitly as prototype
151 * declarations, in rust code, with ABI attributes on them noting which ABI to
154 * An "upcall" is a foreign call generated by the compiler (not corresponding
155 * to any user-written call in the code) into the runtime library, to perform
156 * some helper task such as bringing a task to life, allocating memory, etc.
160 /// A structure representing an active landing pad for the duration of a basic
163 /// Each `Block` may contain an instance of this, indicating whether the block
164 /// is part of a landing pad or not. This is used to make decision about whether
165 /// to emit `invoke` instructions (e.g. in a landing pad we don't continue to
166 /// use `invoke`) and also about various function call metadata.
168 /// For GNU exceptions (`landingpad` + `resume` instructions) this structure is
169 /// just a bunch of `None` instances (not too interesting), but for MSVC
170 /// exceptions (`cleanuppad` + `cleanupret` instructions) this contains data.
171 /// When inside of a landing pad, each function call in LLVM IR needs to be
172 /// annotated with which landing pad it's a part of. This is accomplished via
173 /// the `OperandBundleDef` value created for MSVC landing pads.
174 pub struct Funclet<'ll> {
175 cleanuppad: &'ll Value,
176 operand: OperandBundleDef<'ll, &'ll Value>,
180 pub fn new(cleanuppad: &'ll Value) -> Self {
183 operand: OperandBundleDef::new("funclet", cleanuppad),
187 pub fn cleanuppad(&self) -> &'ll Value {
191 pub fn bundle(&self) -> &OperandBundleDef<'ll, &'ll Value> {
196 impl Backend for CodegenCx<'ll, 'tcx> {
197 type Value = &'ll Value;
198 type BasicBlock = &'ll BasicBlock;
199 type Type = &'ll Type;
200 type Context = &'ll llvm::Context;
203 impl<'ll, 'tcx : 'll> CommonMethods for CodegenCx<'ll, 'tcx> {
205 // LLVM constant constructors.
206 fn c_null(&self, t: &'ll Type) -> &'ll Value {
208 llvm::LLVMConstNull(t)
212 fn c_undef(&self, t: &'ll Type) -> &'ll Value {
214 llvm::LLVMGetUndef(t)
218 fn c_int(&self, t: &'ll Type, i: i64) -> &'ll Value {
220 llvm::LLVMConstInt(t, i as u64, True)
224 fn c_uint(&self, t: &'ll Type, i: u64) -> &'ll Value {
226 llvm::LLVMConstInt(t, i, False)
230 fn c_uint_big(&self, t: &'ll Type, u: u128) -> &'ll Value {
232 let words = [u as u64, (u >> 64) as u64];
233 llvm::LLVMConstIntOfArbitraryPrecision(t, 2, words.as_ptr())
237 fn c_bool(&self, val: bool) -> &'ll Value {
238 &self.c_uint(Type::i1(&self), val as u64)
241 fn c_i32(&self, i: i32) -> &'ll Value {
242 &self.c_int(Type::i32(&self), i as i64)
245 fn c_u32(&self, i: u32) -> &'ll Value {
246 &self.c_uint(Type::i32(&self), i as u64)
249 fn c_u64(&self, i: u64) -> &'ll Value {
250 &self.c_uint(Type::i64(&self), i)
253 fn c_usize(&self, i: u64) -> &'ll Value {
254 let bit_size = self.data_layout().pointer_size.bits();
256 // make sure it doesn't overflow
257 assert!(i < (1<<bit_size));
260 &self.c_uint(&self.isize_ty, i)
263 fn c_u8(&self, i: u8) -> &'ll Value {
264 &self.c_uint(Type::i8(&self), i as u64)
268 // This is a 'c-like' raw string, which differs from
269 // our boxed-and-length-annotated strings.
272 s: LocalInternedString,
273 null_terminated: bool,
276 if let Some(&llval) = &self.const_cstr_cache.borrow().get(&s) {
280 let sc = llvm::LLVMConstStringInContext(&self.llcx,
281 s.as_ptr() as *const c_char,
283 !null_terminated as Bool);
284 let sym = &self.generate_local_symbol_name("str");
285 let g = declare::define_global(&self, &sym[..], &self.val_ty(sc)).unwrap_or_else(||{
286 bug!("symbol `{}` is already defined", sym);
288 llvm::LLVMSetInitializer(g, sc);
289 llvm::LLVMSetGlobalConstant(g, True);
290 llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
292 &self.const_cstr_cache.borrow_mut().insert(s, g);
297 // NB: Do not use `do_spill_noroot` to make this into a constant string, or
298 // you will be kicked off fast isel. See issue #4352 for an example of this.
299 fn c_str_slice(&self, s: LocalInternedString) -> &'ll Value {
301 let cs = consts::ptrcast(&self.c_cstr(s, false),
302 &self.layout_of(&self.tcx.mk_str()).llvm_type(&self).ptr_to());
303 &self.c_fat_ptr(cs, &self.c_usize(len as u64))
311 assert_eq!(abi::FAT_PTR_ADDR, 0);
312 assert_eq!(abi::FAT_PTR_EXTRA, 1);
313 &self.c_struct(&[ptr, meta], false)
321 &self.c_struct_in_context(&self.llcx, elts, packed)
324 fn c_array(ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
326 return llvm::LLVMConstArray(ty, elts.as_ptr(), elts.len() as c_uint);
330 fn c_vector(elts: &[&'ll Value]) -> &'ll Value {
332 return llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint);
336 fn c_bytes(&self, bytes: &[u8]) -> &'ll Value {
337 &self.c_bytes_in_context(&self.llcx, bytes)
340 fn const_get_elt(v: &'ll Value, idx: u64) -> &'ll Value {
342 assert_eq!(idx as c_uint as u64, idx);
343 let us = &[idx as c_uint];
344 let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
346 debug!("const_get_elt(v={:?}, idx={}, r={:?})",
353 fn const_get_real(v: &'ll Value) -> Option<(f64, bool)> {
355 if Self::is_const_real(v) {
356 let mut loses_info: llvm::Bool = ::std::mem::uninitialized();
357 let r = llvm::LLVMConstRealGetDouble(v, &mut loses_info);
358 let loses_info = if loses_info == 1 { true } else { false };
359 Some((r, loses_info))
366 fn const_to_uint(v: &'ll Value) -> u64 {
368 llvm::LLVMConstIntGetZExtValue(v)
372 fn is_const_integral(v: &'ll Value) -> bool {
374 llvm::LLVMIsAConstantInt(v).is_some()
378 fn is_const_real(v: &'ll Value) -> bool {
380 llvm::LLVMIsAConstantFP(v).is_some()
384 fn const_to_opt_u128(v: &'ll Value, sign_ext: bool) -> Option<u128> {
386 if Self::is_const_integral(v) {
387 let (mut lo, mut hi) = (0u64, 0u64);
388 let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
391 Some(hi_lo_to_u128(lo, hi))
402 pub fn val_ty(v: &'ll Value) -> &'ll Type {
408 pub fn c_bytes_in_context(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
410 let ptr = bytes.as_ptr() as *const c_char;
411 return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
415 pub fn c_struct_in_context(
416 llcx: &'a llvm::Context,
421 llvm::LLVMConstStructInContext(llcx,
422 elts.as_ptr(), elts.len() as c_uint,
427 impl<'ll, 'tcx : 'll> CommonWriteMethods for CodegenCx<'ll, 'tcx> {
428 fn val_ty(&self, v: &'ll Value) -> &'ll Type {
432 fn c_bytes_in_context(&self, llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
433 c_bytes_in_context(llcx, bytes)
436 fn c_struct_in_context(
438 llcx: &'a llvm::Context,
442 c_struct_in_context(llcx, elts, packed)
448 fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
449 ((hi as u128) << 64) | (lo as u128)
452 pub fn langcall(tcx: TyCtxt,
457 tcx.lang_items().require(li).unwrap_or_else(|s| {
458 let msg = format!("{} {}", msg, s);
460 Some(span) => tcx.sess.span_fatal(span, &msg[..]),
461 None => tcx.sess.fatal(&msg[..]),
466 // To avoid UB from LLVM, these two functions mask RHS with an
467 // appropriate mask unconditionally (i.e. the fallback behavior for
468 // all shifts). For 32- and 64-bit types, this matches the semantics
469 // of Java. (See related discussion on #1877 and #10183.)
471 pub fn build_unchecked_lshift(
472 bx: &Builder<'a, 'll, 'tcx>,
476 let rhs = base::cast_shift_expr_rhs(bx, hir::BinOpKind::Shl, lhs, rhs);
477 // #1877, #10183: Ensure that input is always valid
478 let rhs = shift_mask_rhs(bx, rhs);
482 pub fn build_unchecked_rshift(
483 bx: &Builder<'a, 'll, 'tcx>, lhs_t: Ty<'tcx>, lhs: &'ll Value, rhs: &'ll Value
485 let rhs = base::cast_shift_expr_rhs(bx, hir::BinOpKind::Shr, lhs, rhs);
486 // #1877, #10183: Ensure that input is always valid
487 let rhs = shift_mask_rhs(bx, rhs);
488 let is_signed = lhs_t.is_signed();
496 fn shift_mask_rhs(bx: &Builder<'a, 'll, 'tcx>, rhs: &'ll Value) -> &'ll Value {
497 let rhs_llty = bx.cx().val_ty(rhs);
498 bx.and(rhs, shift_mask_val(bx, rhs_llty, rhs_llty, false))
501 pub fn shift_mask_val(
502 bx: &Builder<'a, 'll, 'tcx>,
504 mask_llty: &'ll Type,
507 let kind = llty.kind();
509 TypeKind::Integer => {
510 // i8/u8 can shift by at most 7, i16/u16 by at most 15, etc.
511 let val = llty.int_width() - 1;
513 bx.cx.c_int(mask_llty, !val as i64)
515 bx.cx.c_uint(mask_llty, val)
518 TypeKind::Vector => {
519 let mask = shift_mask_val(bx, llty.element_type(), mask_llty.element_type(), invert);
520 bx.vector_splat(mask_llty.vector_length(), mask)
522 _ => bug!("shift_mask_val: expected Integer or Vector, found {:?}", kind),