2014-02-10 08:36:31 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-07-18 07:45:17 -05:00
|
|
|
#![allow(non_camel_case_types, non_snake_case)]
|
2014-02-10 08:36:31 -06:00
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
//! Code that is useful in various trans modules.
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm;
|
2017-01-01 01:42:09 -06:00
|
|
|
use llvm::{ValueRef, ContextRef, TypeKind};
|
2016-12-18 17:06:41 -06:00
|
|
|
use llvm::{True, False, Bool, OperandBundleDef};
|
2016-03-29 04:54:26 -05:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-11-16 04:27:43 -06:00
|
|
|
use rustc::hir::map::DefPathData;
|
2017-05-27 13:48:09 -05:00
|
|
|
use rustc::middle::lang_items::LangItem;
|
2016-03-22 12:23:36 -05:00
|
|
|
use base;
|
|
|
|
use builder::Builder;
|
|
|
|
use consts;
|
|
|
|
use declare;
|
|
|
|
use type_::Type;
|
|
|
|
use value::Value;
|
2017-09-12 10:28:17 -05:00
|
|
|
use rustc::traits;
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2017-06-01 13:50:53 -05:00
|
|
|
use rustc::ty::layout::{HasDataLayout, Layout, LayoutTyper};
|
2016-12-26 07:34:03 -06:00
|
|
|
use rustc::ty::subst::{Kind, Subst, Substs};
|
2016-03-29 00:50:44 -05:00
|
|
|
use rustc::hir;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
use libc::{c_uint, c_char};
|
2016-11-09 15:09:28 -06:00
|
|
|
use std::iter;
|
2016-02-23 13:57:22 -06:00
|
|
|
|
2016-12-26 07:34:03 -06:00
|
|
|
use syntax::abi::Abi;
|
2017-01-03 21:54:22 -06:00
|
|
|
use syntax::symbol::InternedString;
|
2017-09-12 10:28:17 -05:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2016-05-05 23:47:28 -05:00
|
|
|
pub use context::{CrateContext, SharedCrateContext};
|
2013-06-12 21:02:33 -05:00
|
|
|
|
2016-12-18 12:50:07 -06:00
|
|
|
pub fn type_is_fat_ptr<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
|
2017-01-05 12:35:30 -06:00
|
|
|
if let Layout::FatPointer { .. } = *ccx.layout_of(ty) {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2015-01-06 17:22:24 -06:00
|
|
|
}
|
|
|
|
}
|
2014-12-18 08:26:10 -06:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn type_is_immediate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
|
2017-01-05 12:31:47 -06:00
|
|
|
let layout = ccx.layout_of(ty);
|
|
|
|
match *layout {
|
|
|
|
Layout::CEnum { .. } |
|
|
|
|
Layout::Scalar { .. } |
|
|
|
|
Layout::Vector { .. } => true,
|
2015-01-08 06:14:07 -06:00
|
|
|
|
2017-01-05 12:31:47 -06:00
|
|
|
Layout::FatPointer { .. } => false,
|
|
|
|
|
|
|
|
Layout::Array { .. } |
|
|
|
|
Layout::Univariant { .. } |
|
|
|
|
Layout::General { .. } |
|
|
|
|
Layout::UntaggedUnion { .. } |
|
|
|
|
Layout::RawNullablePointer { .. } |
|
|
|
|
Layout::StructWrappedNullablePointer { .. } => {
|
2017-03-09 22:25:51 -06:00
|
|
|
!layout.is_unsized() && layout.size(ccx).bytes() == 0
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
make small (<= size_of::<int>()) tuples immediate
fn foo() -> (u32, u8, u8, u8, u8) {
(4, 5, 6, 7, 8)
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3foo18hbb616262f874f8daf4v0.0E({ i32, i8, i8, i8, i8 }* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
%2 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 0
store i32 4, i32* %2, align 4
%3 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 1
store i8 5, i8* %3, align 4
%4 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 2
store i8 6, i8* %4, align 1
%5 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 3
store i8 7, i8* %5, align 2
%6 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 4
store i8 8, i8* %6, align 1
ret void
}
After:
; Function Attrs: nounwind readnone uwtable
define { i32, i8, i8, i8, i8 } @_ZN3foo18hbb616262f874f8daf4v0.0E({ i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
ret { i32, i8, i8, i8, i8 } { i32 4, i8 5, i8 6, i8 7, i8 8 }
}
2013-09-30 17:29:42 -05:00
|
|
|
}
|
2013-09-30 16:45:53 -05:00
|
|
|
}
|
|
|
|
|
2016-05-25 03:55:44 -05:00
|
|
|
/// Returns true if the type is represented as a pair of immediates.
|
|
|
|
pub fn type_is_imm_pair<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>)
|
|
|
|
-> bool {
|
2017-06-25 04:41:24 -05:00
|
|
|
let layout = ccx.layout_of(ty);
|
|
|
|
match *layout {
|
2016-05-25 03:55:44 -05:00
|
|
|
Layout::FatPointer { .. } => true,
|
|
|
|
Layout::Univariant { ref variant, .. } => {
|
|
|
|
// There must be only 2 fields.
|
2016-10-01 20:25:40 -05:00
|
|
|
if variant.offsets.len() != 2 {
|
2016-05-25 03:55:44 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-25 04:41:24 -05:00
|
|
|
// The two fields must be both immediates.
|
|
|
|
type_is_immediate(ccx, layout.field_type(ccx, 0)) &&
|
|
|
|
type_is_immediate(ccx, layout.field_type(ccx, 1))
|
2016-05-25 03:55:44 -05:00
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Identify types which have size zero at runtime.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn type_is_zero_size<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
|
2017-03-01 21:35:25 -06:00
|
|
|
let layout = ccx.layout_of(ty);
|
2017-03-09 22:25:51 -06:00
|
|
|
!layout.is_unsized() && layout.size(ccx).bytes() == 0
|
2014-01-16 14:11:22 -06:00
|
|
|
}
|
|
|
|
|
2017-09-12 10:28:17 -05:00
|
|
|
pub fn type_needs_drop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
ty.needs_drop(tcx, ty::ParamEnv::empty(traits::Reveal::All))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_is_sized<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
ty.is_sized(tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
ty.is_freeze(tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
|
|
|
|
}
|
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
/*
|
2015-01-06 17:22:24 -06:00
|
|
|
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
|
|
|
|
*
|
|
|
|
* An "extern" is an LLVM symbol we wind up emitting an undefined external
|
|
|
|
* reference to. This means "we don't have the thing in this compilation unit,
|
|
|
|
* please make sure you link it in at runtime". This could be a reference to
|
|
|
|
* C code found in a C library, or rust code found in a rust crate.
|
|
|
|
*
|
|
|
|
* Most "externs" are implicitly declared (automatically) as a result of a
|
|
|
|
* user declaring an extern _module_ dependency; this causes the rust driver
|
|
|
|
* to locate an extern crate, scan its compilation metadata, and emit extern
|
|
|
|
* declarations for any symbols used by the declaring crate.
|
|
|
|
*
|
|
|
|
* A "foreign" is an extern that references C (or other non-rust ABI) code.
|
|
|
|
* There is no metadata to scan for extern references so in these cases either
|
|
|
|
* a header-digester like bindgen, or manual function prototypes, have to
|
|
|
|
* serve as declarators. So these are usually given explicitly as prototype
|
|
|
|
* declarations, in rust code, with ABI attributes on them noting which ABI to
|
|
|
|
* link via.
|
|
|
|
*
|
|
|
|
* An "upcall" is a foreign call generated by the compiler (not corresponding
|
|
|
|
* to any user-written call in the code) into the runtime library, to perform
|
|
|
|
* some helper task such as bringing a task to life, allocating memory, etc.
|
|
|
|
*
|
|
|
|
*/
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2015-10-23 20:18:44 -05:00
|
|
|
/// A structure representing an active landing pad for the duration of a basic
|
|
|
|
/// block.
|
|
|
|
///
|
|
|
|
/// Each `Block` may contain an instance of this, indicating whether the block
|
|
|
|
/// is part of a landing pad or not. This is used to make decision about whether
|
|
|
|
/// to emit `invoke` instructions (e.g. in a landing pad we don't continue to
|
|
|
|
/// use `invoke`) and also about various function call metadata.
|
|
|
|
///
|
|
|
|
/// For GNU exceptions (`landingpad` + `resume` instructions) this structure is
|
|
|
|
/// just a bunch of `None` instances (not too interesting), but for MSVC
|
|
|
|
/// exceptions (`cleanuppad` + `cleanupret` instructions) this contains data.
|
|
|
|
/// When inside of a landing pad, each function call in LLVM IR needs to be
|
|
|
|
/// annotated with which landing pad it's a part of. This is accomplished via
|
|
|
|
/// the `OperandBundleDef` value created for MSVC landing pads.
|
2016-12-12 07:48:39 -06:00
|
|
|
pub struct Funclet {
|
|
|
|
cleanuppad: ValueRef,
|
|
|
|
operand: OperandBundleDef,
|
2015-10-23 20:18:44 -05:00
|
|
|
}
|
|
|
|
|
2016-12-12 07:48:39 -06:00
|
|
|
impl Funclet {
|
2016-12-19 15:10:48 -06:00
|
|
|
pub fn new(cleanuppad: ValueRef) -> Funclet {
|
|
|
|
Funclet {
|
2017-08-07 00:54:09 -05:00
|
|
|
cleanuppad,
|
2016-12-12 07:48:39 -06:00
|
|
|
operand: OperandBundleDef::new("funclet", &[cleanuppad]),
|
2016-12-19 15:10:48 -06:00
|
|
|
}
|
2015-10-23 20:18:44 -05:00
|
|
|
}
|
|
|
|
|
2016-12-12 07:48:39 -06:00
|
|
|
pub fn cleanuppad(&self) -> ValueRef {
|
|
|
|
self.cleanuppad
|
2015-10-23 20:18:44 -05:00
|
|
|
}
|
2016-05-29 14:01:06 -05:00
|
|
|
|
2016-12-12 07:48:39 -06:00
|
|
|
pub fn bundle(&self) -> &OperandBundleDef {
|
|
|
|
&self.operand
|
2016-05-29 14:01:06 -05:00
|
|
|
}
|
2015-10-23 20:18:44 -05:00
|
|
|
}
|
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
pub fn val_ty(v: ValueRef) -> Type {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
Type::from_ref(llvm::LLVMTypeOf(v))
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2011-07-14 19:08:22 -05:00
|
|
|
// LLVM constant constructors.
|
2013-06-15 22:45:48 -05:00
|
|
|
pub fn C_null(t: Type) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstNull(t.to_ref())
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
pub fn C_undef(t: Type) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMGetUndef(t.to_ref())
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2017-08-05 04:27:28 -05:00
|
|
|
pub fn C_int(t: Type, i: i64) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
2017-08-05 04:27:28 -05:00
|
|
|
llvm::LLVMConstInt(t.to_ref(), i as u64, True)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn C_uint(t: Type, i: u64) -> ValueRef {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstInt(t.to_ref(), i, False)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2017-02-03 20:53:09 -06:00
|
|
|
pub fn C_big_integral(t: Type, u: u128) -> ValueRef {
|
|
|
|
unsafe {
|
|
|
|
let words = [u as u64, u.wrapping_shr(64) as u64];
|
|
|
|
llvm::LLVMConstIntOfArbitraryPrecision(t.to_ref(), 2, words.as_ptr())
|
2016-08-22 19:56:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_bool(ccx: &CrateContext, val: bool) -> ValueRef {
|
2017-08-05 04:27:28 -05:00
|
|
|
C_uint(Type::i1(ccx), val as u64)
|
2013-02-06 16:28:02 -06:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_i32(ccx: &CrateContext, i: i32) -> ValueRef {
|
2017-08-05 04:27:28 -05:00
|
|
|
C_int(Type::i32(ccx), i as i64)
|
2011-10-26 00:23:28 -05:00
|
|
|
}
|
|
|
|
|
2015-02-21 16:26:29 -06:00
|
|
|
pub fn C_u32(ccx: &CrateContext, i: u32) -> ValueRef {
|
2017-08-05 04:27:28 -05:00
|
|
|
C_uint(Type::i32(ccx), i as u64)
|
2015-02-21 16:26:29 -06:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_u64(ccx: &CrateContext, i: u64) -> ValueRef {
|
2017-08-05 04:27:28 -05:00
|
|
|
C_uint(Type::i64(ccx), i)
|
2013-09-29 04:20:11 -05:00
|
|
|
}
|
|
|
|
|
2017-08-05 04:27:28 -05:00
|
|
|
pub fn C_usize(ccx: &CrateContext, i: u64) -> ValueRef {
|
2017-06-01 13:50:53 -05:00
|
|
|
let bit_size = ccx.data_layout().pointer_size.bits();
|
2015-08-15 01:43:39 -05:00
|
|
|
if bit_size < 64 {
|
|
|
|
// make sure it doesn't overflow
|
2017-08-05 04:27:28 -05:00
|
|
|
assert!(i < (1<<bit_size));
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2014-10-15 12:26:43 -05:00
|
|
|
|
2017-08-05 04:27:28 -05:00
|
|
|
C_uint(ccx.isize_ty(), i)
|
2011-10-14 18:45:25 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2015-08-05 02:46:59 -05:00
|
|
|
pub fn C_u8(ccx: &CrateContext, i: u8) -> ValueRef {
|
2017-08-05 04:27:28 -05:00
|
|
|
C_uint(Type::i8(ccx), i as u64)
|
2013-01-30 13:46:19 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
// This is a 'c-like' raw string, which differs from
|
|
|
|
// our boxed-and-length-annotated strings.
|
2014-04-03 15:26:08 -05:00
|
|
|
pub fn C_cstr(cx: &CrateContext, s: InternedString, null_terminated: bool) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
2016-02-23 09:48:07 -06:00
|
|
|
if let Some(&llval) = cx.const_cstr_cache().borrow().get(&s) {
|
|
|
|
return llval;
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2012-04-21 15:23:25 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
let sc = llvm::LLVMConstStringInContext(cx.llcx(),
|
2015-02-03 18:04:50 -06:00
|
|
|
s.as_ptr() as *const c_char,
|
|
|
|
s.len() as c_uint,
|
2015-01-08 06:14:07 -06:00
|
|
|
!null_terminated as Bool);
|
2016-10-21 11:41:20 -05:00
|
|
|
let sym = cx.generate_local_symbol_name("str");
|
2015-03-03 17:08:06 -06:00
|
|
|
let g = declare::define_global(cx, &sym[..], val_ty(sc)).unwrap_or_else(||{
|
2016-03-28 18:46:02 -05:00
|
|
|
bug!("symbol `{}` is already defined", sym);
|
2015-03-03 17:08:06 -06:00
|
|
|
});
|
2015-01-08 06:14:07 -06:00
|
|
|
llvm::LLVMSetInitializer(g, sc);
|
|
|
|
llvm::LLVMSetGlobalConstant(g, True);
|
2016-09-01 13:52:33 -05:00
|
|
|
llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
|
2012-04-21 15:23:25 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
cx.const_cstr_cache().borrow_mut().insert(s, g);
|
|
|
|
g
|
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2013-01-05 01:06:25 -06:00
|
|
|
// NB: Do not use `do_spill_noroot` to make this into a constant string, or
|
|
|
|
// you will be kicked off fast isel. See issue #4352 for an example of this.
|
2014-01-10 16:02:36 -06:00
|
|
|
pub fn C_str_slice(cx: &CrateContext, s: InternedString) -> ValueRef {
|
2015-02-03 18:04:50 -06:00
|
|
|
let len = s.len();
|
2017-06-26 10:33:50 -05:00
|
|
|
let cs = consts::ptrcast(C_cstr(cx, s, false),
|
|
|
|
cx.llvm_type_of(cx.tcx().mk_str()).ptr_to());
|
2017-08-05 04:27:28 -05:00
|
|
|
C_named_struct(cx.str_slice_type(), &[cs, C_usize(cx, len as u64)])
|
2012-06-08 15:26:06 -05:00
|
|
|
}
|
|
|
|
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 12:52:52 -05:00
|
|
|
pub fn C_struct(cx: &CrateContext, elts: &[ValueRef], packed: bool) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
C_struct_in_context(cx.llcx(), elts, packed)
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 12:52:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn C_struct_in_context(llcx: ContextRef, elts: &[ValueRef], packed: bool) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstStructInContext(llcx,
|
|
|
|
elts.as_ptr(), elts.len() as c_uint,
|
|
|
|
packed as Bool)
|
|
|
|
}
|
2013-01-23 18:25:47 -06:00
|
|
|
}
|
|
|
|
|
2014-02-15 15:15:03 -06:00
|
|
|
pub fn C_named_struct(t: Type, elts: &[ValueRef]) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstNamedStruct(t.to_ref(), elts.as_ptr(), elts.len() as c_uint)
|
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
pub fn C_array(ty: Type, elts: &[ValueRef]) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
return llvm::LLVMConstArray(ty.to_ref(), elts.as_ptr(), elts.len() as c_uint);
|
|
|
|
}
|
2011-07-27 17:14:59 -05:00
|
|
|
}
|
2011-08-04 12:46:10 -05:00
|
|
|
|
2015-01-29 06:03:34 -06:00
|
|
|
pub fn C_vector(elts: &[ValueRef]) -> ValueRef {
|
|
|
|
unsafe {
|
|
|
|
return llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 12:52:52 -05:00
|
|
|
pub fn C_bytes(cx: &CrateContext, bytes: &[u8]) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
C_bytes_in_context(cx.llcx(), bytes)
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 12:52:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn C_bytes_in_context(llcx: ContextRef, bytes: &[u8]) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
let ptr = bytes.as_ptr() as *const c_char;
|
|
|
|
return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
|
|
|
|
}
|
2012-09-05 17:27:22 -05:00
|
|
|
}
|
|
|
|
|
2017-06-25 04:41:24 -05:00
|
|
|
pub fn const_get_elt(v: ValueRef, idx: u64) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
2017-06-25 04:41:24 -05:00
|
|
|
assert_eq!(idx as c_uint as u64, idx);
|
|
|
|
let us = &[idx as c_uint];
|
2015-01-08 06:14:07 -06:00
|
|
|
let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2017-06-25 04:41:24 -05:00
|
|
|
debug!("const_get_elt(v={:?}, idx={}, r={:?})",
|
|
|
|
Value(v), idx, Value(r));
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2016-02-18 11:49:45 -06:00
|
|
|
r
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn const_to_uint(v: ValueRef) -> u64 {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstIntGetZExtValue(v)
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:27:25 -05:00
|
|
|
pub fn is_const_integral(v: ValueRef) -> bool {
|
2015-03-26 19:37:10 -05:00
|
|
|
unsafe {
|
|
|
|
!llvm::LLVMIsAConstantInt(v).is_null()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 22:06:53 -06:00
|
|
|
#[inline]
|
|
|
|
fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
|
|
|
|
((hi as u128) << 64) | (lo as u128)
|
|
|
|
}
|
|
|
|
|
2016-08-25 17:32:46 -05:00
|
|
|
pub fn const_to_opt_u128(v: ValueRef, sign_ext: bool) -> Option<u128> {
|
2015-03-26 19:37:10 -05:00
|
|
|
unsafe {
|
|
|
|
if is_const_integral(v) {
|
2016-08-25 17:32:46 -05:00
|
|
|
let (mut lo, mut hi) = (0u64, 0u64);
|
|
|
|
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
|
|
|
|
&mut hi as *mut u64, &mut lo as *mut u64);
|
|
|
|
if success {
|
2016-11-19 22:06:53 -06:00
|
|
|
Some(hi_lo_to_u128(lo, hi))
|
2016-08-25 17:32:46 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-03-26 19:37:10 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 00:39:32 -05:00
|
|
|
pub fn langcall(tcx: TyCtxt,
|
2014-01-07 10:54:58 -06:00
|
|
|
span: Option<Span>,
|
|
|
|
msg: &str,
|
|
|
|
li: LangItem)
|
2015-08-16 05:32:28 -05:00
|
|
|
-> DefId {
|
2017-08-31 10:57:41 -05:00
|
|
|
match tcx.lang_items().require(li) {
|
2013-07-15 22:42:13 -05:00
|
|
|
Ok(id) => id,
|
|
|
|
Err(s) => {
|
2013-09-28 00:38:08 -05:00
|
|
|
let msg = format!("{} {}", msg, s);
|
2013-07-15 22:42:13 -05:00
|
|
|
match span {
|
2016-05-25 00:39:32 -05:00
|
|
|
Some(span) => tcx.sess.span_fatal(span, &msg[..]),
|
|
|
|
None => tcx.sess.fatal(&msg[..]),
|
2013-07-15 22:42:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-02 14:52:50 -05:00
|
|
|
|
2015-10-21 16:35:15 -05:00
|
|
|
// To avoid UB from LLVM, these two functions mask RHS with an
|
|
|
|
// appropriate mask unconditionally (i.e. the fallback behavior for
|
|
|
|
// all shifts). For 32- and 64-bit types, this matches the semantics
|
|
|
|
// of Java. (See related discussion on #1877 and #10183.)
|
|
|
|
|
2016-12-17 20:54:32 -06:00
|
|
|
pub fn build_unchecked_lshift<'a, 'tcx>(
|
2016-12-31 17:00:24 -06:00
|
|
|
bcx: &Builder<'a, 'tcx>,
|
2016-12-17 20:54:32 -06:00
|
|
|
lhs: ValueRef,
|
|
|
|
rhs: ValueRef
|
|
|
|
) -> ValueRef {
|
2015-10-21 16:35:15 -05:00
|
|
|
let rhs = base::cast_shift_expr_rhs(bcx, hir::BinOp_::BiShl, lhs, rhs);
|
|
|
|
// #1877, #10183: Ensure that input is always valid
|
2016-12-11 09:59:20 -06:00
|
|
|
let rhs = shift_mask_rhs(bcx, rhs);
|
|
|
|
bcx.shl(lhs, rhs)
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
|
2016-12-17 20:54:32 -06:00
|
|
|
pub fn build_unchecked_rshift<'a, 'tcx>(
|
2016-12-31 17:00:24 -06:00
|
|
|
bcx: &Builder<'a, 'tcx>, lhs_t: Ty<'tcx>, lhs: ValueRef, rhs: ValueRef
|
2016-12-17 20:54:32 -06:00
|
|
|
) -> ValueRef {
|
2015-10-21 16:35:15 -05:00
|
|
|
let rhs = base::cast_shift_expr_rhs(bcx, hir::BinOp_::BiShr, lhs, rhs);
|
|
|
|
// #1877, #10183: Ensure that input is always valid
|
2016-12-11 09:59:20 -06:00
|
|
|
let rhs = shift_mask_rhs(bcx, rhs);
|
2015-10-21 16:35:15 -05:00
|
|
|
let is_signed = lhs_t.is_signed();
|
|
|
|
if is_signed {
|
2016-12-11 09:59:20 -06:00
|
|
|
bcx.ashr(lhs, rhs)
|
2015-10-21 16:35:15 -05:00
|
|
|
} else {
|
2016-12-11 09:59:20 -06:00
|
|
|
bcx.lshr(lhs, rhs)
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 17:00:24 -06:00
|
|
|
fn shift_mask_rhs<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, rhs: ValueRef) -> ValueRef {
|
2015-10-21 16:35:15 -05:00
|
|
|
let rhs_llty = val_ty(rhs);
|
2016-12-11 09:59:20 -06:00
|
|
|
bcx.and(rhs, shift_mask_val(bcx, rhs_llty, rhs_llty, false))
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
|
2016-12-17 20:54:32 -06:00
|
|
|
pub fn shift_mask_val<'a, 'tcx>(
|
2016-12-31 17:00:24 -06:00
|
|
|
bcx: &Builder<'a, 'tcx>,
|
2016-12-10 21:32:44 -06:00
|
|
|
llty: Type,
|
|
|
|
mask_llty: Type,
|
|
|
|
invert: bool
|
|
|
|
) -> ValueRef {
|
2015-10-21 16:35:15 -05:00
|
|
|
let kind = llty.kind();
|
|
|
|
match kind {
|
|
|
|
TypeKind::Integer => {
|
|
|
|
// i8/u8 can shift by at most 7, i16/u16 by at most 15, etc.
|
|
|
|
let val = llty.int_width() - 1;
|
|
|
|
if invert {
|
2017-08-05 04:27:28 -05:00
|
|
|
C_int(mask_llty, !val as i64)
|
2015-10-21 16:35:15 -05:00
|
|
|
} else {
|
2017-08-05 04:27:28 -05:00
|
|
|
C_uint(mask_llty, val)
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
TypeKind::Vector => {
|
|
|
|
let mask = shift_mask_val(bcx, llty.element_type(), mask_llty.element_type(), invert);
|
2016-12-11 09:59:20 -06:00
|
|
|
bcx.vector_splat(mask_llty.vector_length(), mask)
|
2015-10-21 16:35:15 -05:00
|
|
|
},
|
2016-03-28 18:46:02 -05:00
|
|
|
_ => bug!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
}
|
2016-11-09 15:09:28 -06:00
|
|
|
|
2017-02-13 02:51:06 -06:00
|
|
|
pub fn ty_fn_sig<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
-> ty::PolyFnSig<'tcx>
|
2016-11-09 15:09:28 -06:00
|
|
|
{
|
|
|
|
match ty.sty {
|
2017-05-13 09:11:52 -05:00
|
|
|
ty::TyFnDef(..) |
|
2016-11-09 15:09:28 -06:00
|
|
|
// Shims currently have type TyFnPtr. Not sure this should remain.
|
2017-05-13 09:11:52 -05:00
|
|
|
ty::TyFnPtr(_) => ty.fn_sig(ccx.tcx()),
|
2016-11-09 15:09:28 -06:00
|
|
|
ty::TyClosure(def_id, substs) => {
|
|
|
|
let tcx = ccx.tcx();
|
2017-05-13 05:12:29 -05:00
|
|
|
let sig = tcx.fn_sig(def_id).subst(tcx, substs.substs);
|
2016-11-09 15:09:28 -06:00
|
|
|
|
|
|
|
let env_region = ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrEnv);
|
|
|
|
let env_ty = match tcx.closure_kind(def_id) {
|
|
|
|
ty::ClosureKind::Fn => tcx.mk_imm_ref(tcx.mk_region(env_region), ty),
|
|
|
|
ty::ClosureKind::FnMut => tcx.mk_mut_ref(tcx.mk_region(env_region), ty),
|
|
|
|
ty::ClosureKind::FnOnce => ty,
|
|
|
|
};
|
|
|
|
|
2017-02-13 02:51:06 -06:00
|
|
|
sig.map_bound(|sig| tcx.mk_fn_sig(
|
2016-11-28 21:25:33 -06:00
|
|
|
iter::once(env_ty).chain(sig.inputs().iter().cloned()),
|
2016-11-28 20:35:38 -06:00
|
|
|
sig.output(),
|
2017-02-13 02:51:06 -06:00
|
|
|
sig.variadic,
|
|
|
|
sig.unsafety,
|
|
|
|
sig.abi
|
|
|
|
))
|
2016-11-09 15:09:28 -06:00
|
|
|
}
|
2016-12-26 07:34:03 -06:00
|
|
|
ty::TyGenerator(def_id, substs, _) => {
|
|
|
|
let tcx = ccx.tcx();
|
|
|
|
let sig = tcx.generator_sig(def_id).unwrap().subst(tcx, substs.substs);
|
|
|
|
|
|
|
|
let env_region = ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrEnv);
|
|
|
|
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
|
|
|
|
|
|
|
|
sig.map_bound(|sig| {
|
2017-08-31 10:57:41 -05:00
|
|
|
let state_did = tcx.lang_items().gen_state().unwrap();
|
2016-12-26 07:34:03 -06:00
|
|
|
let state_adt_ref = tcx.adt_def(state_did);
|
2017-07-10 14:11:31 -05:00
|
|
|
let state_substs = tcx.mk_substs([Kind::from(sig.yield_ty),
|
2016-12-26 07:34:03 -06:00
|
|
|
Kind::from(sig.return_ty)].iter());
|
|
|
|
let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
|
|
|
|
|
2017-07-11 14:57:05 -05:00
|
|
|
tcx.mk_fn_sig(iter::once(env_ty),
|
2016-12-26 07:34:03 -06:00
|
|
|
ret_ty,
|
|
|
|
false,
|
|
|
|
hir::Unsafety::Normal,
|
|
|
|
Abi::Rust
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2016-11-09 15:09:28 -06:00
|
|
|
_ => bug!("unexpected type {:?} to ty_fn_sig", ty)
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 04:27:43 -06:00
|
|
|
|
2017-03-13 18:08:21 -05:00
|
|
|
pub fn is_inline_instance<'a, 'tcx>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
instance: &ty::Instance<'tcx>
|
2017-02-08 11:31:03 -06:00
|
|
|
) -> bool {
|
|
|
|
let def_id = match instance.def {
|
|
|
|
ty::InstanceDef::Item(def_id) => def_id,
|
2017-03-13 18:08:21 -05:00
|
|
|
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
|
2017-02-08 11:31:03 -06:00
|
|
|
_ => return true
|
|
|
|
};
|
2017-02-07 15:46:21 -06:00
|
|
|
match tcx.def_key(def_id).disambiguated_data.data {
|
|
|
|
DefPathData::StructCtor |
|
|
|
|
DefPathData::EnumVariant(..) |
|
|
|
|
DefPathData::ClosureExpr => true,
|
2017-03-13 18:08:21 -05:00
|
|
|
_ => false
|
2017-02-07 15:46:21 -06:00
|
|
|
}
|
2016-11-16 04:27:43 -06:00
|
|
|
}
|
2017-03-07 17:41:26 -06:00
|
|
|
|
2017-02-28 17:30:41 -06:00
|
|
|
/// Given a DefId and some Substs, produces the monomorphic item type.
|
2017-09-12 10:28:17 -05:00
|
|
|
pub fn def_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-02-28 17:30:41 -06:00
|
|
|
def_id: DefId,
|
|
|
|
substs: &'tcx Substs<'tcx>)
|
|
|
|
-> Ty<'tcx>
|
|
|
|
{
|
2017-09-12 10:28:17 -05:00
|
|
|
let ty = tcx.type_of(def_id);
|
|
|
|
tcx.trans_apply_param_substs(substs, &ty)
|
2017-02-28 17:30:41 -06:00
|
|
|
}
|
2017-02-08 11:31:03 -06:00
|
|
|
|
|
|
|
/// Return the substituted type of an instance.
|
2017-09-12 10:28:17 -05:00
|
|
|
pub fn instance_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-02-08 11:31:03 -06:00
|
|
|
instance: &ty::Instance<'tcx>)
|
|
|
|
-> Ty<'tcx>
|
|
|
|
{
|
2017-09-12 10:28:17 -05:00
|
|
|
let ty = instance.def.def_ty(tcx);
|
|
|
|
tcx.trans_apply_param_substs(instance.substs, &ty)
|
2017-02-08 11:31:03 -06:00
|
|
|
}
|