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-11-15 19:30:33 -06:00
|
|
|
use session::Session;
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm;
|
2015-10-21 16:35:15 -05:00
|
|
|
use llvm::{ValueRef, BasicBlockRef, BuilderRef, ContextRef, TypeKind};
|
2015-10-23 20:18:44 -05:00
|
|
|
use llvm::{True, False, Bool, OperandBundleDef};
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::cfg;
|
2016-03-29 04:54:26 -05:00
|
|
|
use rustc::hir::def::Def;
|
|
|
|
use rustc::hir::def_id::DefId;
|
2016-05-10 20:14:41 -05:00
|
|
|
use rustc::infer::TransNormalize;
|
2016-05-05 23:47:28 -05:00
|
|
|
use rustc::util::common::MemoizationMap;
|
2013-07-15 22:42:13 -05:00
|
|
|
use middle::lang_items::LangItem;
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::subst::Substs;
|
2016-03-22 12:23:36 -05:00
|
|
|
use abi::{Abi, FnType};
|
|
|
|
use base;
|
|
|
|
use build;
|
|
|
|
use builder::Builder;
|
|
|
|
use callee::Callee;
|
|
|
|
use cleanup;
|
|
|
|
use consts;
|
|
|
|
use datum;
|
|
|
|
use debuginfo::{self, DebugLoc};
|
|
|
|
use declare;
|
|
|
|
use machine;
|
|
|
|
use mir::CachedMir;
|
|
|
|
use monomorphize;
|
|
|
|
use type_::Type;
|
|
|
|
use value::Value;
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
|
|
|
use rustc::traits::{self, SelectionContext, ProjectionMode};
|
2016-04-19 08:40:21 -05:00
|
|
|
use rustc::ty::fold::TypeFoldable;
|
2016-03-29 00:50:44 -05:00
|
|
|
use rustc::hir;
|
2016-02-23 13:57:22 -06:00
|
|
|
use util::nodemap::NodeMap;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2014-01-28 20:50:05 -06:00
|
|
|
use arena::TypedArena;
|
2014-10-14 15:36:11 -05:00
|
|
|
use libc::{c_uint, c_char};
|
2016-02-01 04:04:49 -06:00
|
|
|
use std::ops::Deref;
|
2014-11-25 15:28:35 -06:00
|
|
|
use std::ffi::CString;
|
2015-06-29 19:46:24 -05:00
|
|
|
use std::cell::{Cell, RefCell};
|
2016-02-23 13:57:22 -06:00
|
|
|
|
2014-02-13 23:07:09 -06:00
|
|
|
use syntax::ast;
|
2015-01-07 11:49:52 -06:00
|
|
|
use syntax::codemap::{DUMMY_SP, Span};
|
2014-01-10 16:02:36 -06:00
|
|
|
use syntax::parse::token::InternedString;
|
2013-06-04 14:34:25 -05:00
|
|
|
use syntax::parse::token;
|
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
|
|
|
|
2015-06-04 19:50:49 -05:00
|
|
|
/// Is the type's representation size known at compile time?
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn type_is_sized<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
|
2016-03-24 22:22:52 -05:00
|
|
|
ty.is_sized(tcx, &tcx.empty_parameter_environment(), DUMMY_SP)
|
2014-12-18 08:26:10 -06:00
|
|
|
}
|
|
|
|
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn type_is_fat_ptr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
|
2015-01-08 06:14:07 -06:00
|
|
|
match ty.sty {
|
2015-07-10 20:27:06 -05:00
|
|
|
ty::TyRawPtr(ty::TypeAndMut{ty, ..}) |
|
|
|
|
ty::TyRef(_, ty::TypeAndMut{ty, ..}) |
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyBox(ty) => {
|
2016-05-02 20:02:41 -05:00
|
|
|
!type_is_sized(tcx, ty)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
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 {
|
2016-03-22 12:23:36 -05:00
|
|
|
use machine::llsize_of_alloc;
|
|
|
|
use type_of::sizing_type_of;
|
2015-01-08 06:14:07 -06:00
|
|
|
|
|
|
|
let tcx = ccx.tcx();
|
2015-06-24 00:24:13 -05:00
|
|
|
let simple = ty.is_scalar() ||
|
|
|
|
ty.is_unique() || ty.is_region_ptr() ||
|
2015-08-06 10:25:15 -05:00
|
|
|
ty.is_simd();
|
2015-01-08 06:14:07 -06:00
|
|
|
if simple && !type_is_fat_ptr(tcx, ty) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if !type_is_sized(tcx, ty) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
match ty.sty {
|
2015-06-12 18:50:13 -05:00
|
|
|
ty::TyStruct(..) | ty::TyEnum(..) | ty::TyTuple(..) | ty::TyArray(_, _) |
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyClosure(..) => {
|
2015-01-08 06:14:07 -06:00
|
|
|
let llty = sizing_type_of(ccx, ty);
|
|
|
|
llsize_of_alloc(ccx, llty) <= llsize_of_alloc(ccx, ccx.int_type())
|
|
|
|
}
|
|
|
|
_ => type_is_zero_size(ccx, ty)
|
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
|
|
|
}
|
|
|
|
|
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 {
|
2016-03-22 12:23:36 -05:00
|
|
|
use machine::llsize_of_alloc;
|
|
|
|
use type_of::sizing_type_of;
|
2015-01-08 06:14:07 -06:00
|
|
|
let llty = sizing_type_of(ccx, ty);
|
|
|
|
llsize_of_alloc(ccx, llty) == 0
|
2014-01-16 14:11:22 -06:00
|
|
|
}
|
|
|
|
|
2014-03-13 18:24:46 -05:00
|
|
|
/// Generates a unique symbol based off the name given. This is used to create
|
|
|
|
/// unique symbols for things like closures.
|
2015-09-17 13:29:59 -05:00
|
|
|
pub fn gensym_name(name: &str) -> ast::Name {
|
2015-09-24 15:05:02 -05:00
|
|
|
let num = token::gensym(name).0;
|
2015-01-08 06:14:07 -06:00
|
|
|
// use one colon which will get translated to a period by the mangler, and
|
|
|
|
// we're guaranteed that `num` is globally unique for this crate.
|
2015-09-17 13:29:59 -05:00
|
|
|
token::gensym(&format!("{}:{}", name, num))
|
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
|
|
|
|
2016-03-22 12:23:36 -05:00
|
|
|
use Disr;
|
2016-01-16 09:03:09 -06:00
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2014-12-11 06:53:30 -06:00
|
|
|
pub struct NodeIdAndSpan {
|
2015-01-08 06:14:07 -06:00
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub span: Span,
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
pub fn expr_info(expr: &hir::Expr) -> NodeIdAndSpan {
|
2014-12-11 06:53:30 -06:00
|
|
|
NodeIdAndSpan { id: expr.id, span: expr.span }
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
2015-08-02 14:52:50 -05:00
|
|
|
/// The concrete version of ty::FieldDef. The name is the field index if
|
|
|
|
/// the field is numeric.
|
|
|
|
pub struct Field<'tcx>(pub ast::Name, pub Ty<'tcx>);
|
|
|
|
|
|
|
|
/// The concrete version of ty::VariantDef
|
|
|
|
pub struct VariantInfo<'tcx> {
|
2016-01-16 09:03:09 -06:00
|
|
|
pub discr: Disr,
|
2015-08-02 14:52:50 -05:00
|
|
|
pub fields: Vec<Field<'tcx>>
|
|
|
|
}
|
|
|
|
|
2016-05-02 20:56:42 -05:00
|
|
|
impl<'a, 'tcx> VariantInfo<'tcx> {
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn from_ty(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2015-08-02 14:52:50 -05:00
|
|
|
ty: Ty<'tcx>,
|
2016-01-20 13:31:10 -06:00
|
|
|
opt_def: Option<Def>)
|
2015-08-02 14:52:50 -05:00
|
|
|
-> Self
|
|
|
|
{
|
|
|
|
match ty.sty {
|
|
|
|
ty::TyStruct(adt, substs) | ty::TyEnum(adt, substs) => {
|
|
|
|
let variant = match opt_def {
|
|
|
|
None => adt.struct_variant(),
|
|
|
|
Some(def) => adt.variant_of_def(def)
|
|
|
|
};
|
|
|
|
|
|
|
|
VariantInfo {
|
2016-01-16 09:03:09 -06:00
|
|
|
discr: Disr::from(variant.disr_val),
|
2015-08-02 14:52:50 -05:00
|
|
|
fields: variant.fields.iter().map(|f| {
|
|
|
|
Field(f.name, monomorphize::field_ty(tcx, substs, f))
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TyTuple(ref v) => {
|
|
|
|
VariantInfo {
|
2016-01-16 09:03:09 -06:00
|
|
|
discr: Disr(0),
|
2015-08-02 14:52:50 -05:00
|
|
|
fields: v.iter().enumerate().map(|(i, &t)| {
|
|
|
|
Field(token::intern(&i.to_string()), t)
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
2016-03-28 18:46:02 -05:00
|
|
|
bug!("cannot get field types from the type {:?}", ty);
|
2015-08-02 14:52:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the variant corresponding to a given node (e.g. expr)
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn of_node(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>, id: ast::NodeId) -> Self {
|
2015-08-02 14:52:50 -05:00
|
|
|
let node_def = tcx.def_map.borrow().get(&id).map(|v| v.full_def());
|
|
|
|
Self::from_ty(tcx, ty, node_def)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn field_index(&self, name: ast::Name) -> usize {
|
|
|
|
self.fields.iter().position(|&Field(n,_)| n == name).unwrap_or_else(|| {
|
2016-03-28 18:46:02 -05:00
|
|
|
bug!("unknown field `{}`", name)
|
2015-08-02 14:52:50 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub struct BuilderRef_res {
|
2015-01-08 06:14:07 -06:00
|
|
|
pub b: BuilderRef,
|
2013-02-27 18:13:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for BuilderRef_res {
|
2015-01-08 06:14:07 -06:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMDisposeBuilder(self.b);
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-06-22 13:53:25 -05:00
|
|
|
}
|
2011-08-24 09:30:20 -05:00
|
|
|
|
2014-02-15 15:15:03 -06:00
|
|
|
pub fn BuilderRef_res(b: BuilderRef) -> BuilderRef_res {
|
2015-01-08 06:14:07 -06:00
|
|
|
BuilderRef_res {
|
|
|
|
b: b
|
|
|
|
}
|
2012-09-05 17:58:43 -05:00
|
|
|
}
|
|
|
|
|
2014-11-06 01:24:44 -06:00
|
|
|
pub fn validate_substs(substs: &Substs) {
|
2015-06-23 18:54:32 -05:00
|
|
|
assert!(!substs.types.needs_infer());
|
2014-05-07 06:20:15 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// work around bizarre resolve errors
|
2014-12-28 15:22:37 -06:00
|
|
|
type RvalueDatum<'tcx> = datum::Datum<'tcx, datum::Rvalue>;
|
2015-03-11 16:44:56 -05:00
|
|
|
pub type LvalueDatum<'tcx> = datum::Datum<'tcx, datum::Lvalue>;
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2015-06-05 14:34:03 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct HintEntry<'tcx> {
|
|
|
|
// The datum for the dropflag-hint itself; note that many
|
|
|
|
// source-level Lvalues will be associated with the same
|
|
|
|
// dropflag-hint datum.
|
|
|
|
datum: cleanup::DropHintDatum<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DropFlagHintsMap<'tcx> {
|
|
|
|
// Maps NodeId for expressions that read/write unfragmented state
|
|
|
|
// to that state's drop-flag "hint." (A stack-local hint
|
|
|
|
// indicates either that (1.) it is certain that no-drop is
|
|
|
|
// needed, or (2.) inline drop-flag must be consulted.)
|
|
|
|
node_map: NodeMap<HintEntry<'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> DropFlagHintsMap<'tcx> {
|
|
|
|
pub fn new() -> DropFlagHintsMap<'tcx> { DropFlagHintsMap { node_map: NodeMap() } }
|
|
|
|
pub fn has_hint(&self, id: ast::NodeId) -> bool { self.node_map.contains_key(&id) }
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
pub fn insert(&mut self, id: ast::NodeId, datum: cleanup::DropHintDatum<'tcx>) {
|
|
|
|
self.node_map.insert(id, HintEntry { datum: datum });
|
|
|
|
}
|
|
|
|
pub fn hint_datum(&self, id: ast::NodeId) -> Option<cleanup::DropHintDatum<'tcx>> {
|
|
|
|
self.node_map.get(&id).map(|t|t.datum)
|
|
|
|
}
|
2015-06-05 14:34:03 -05:00
|
|
|
}
|
|
|
|
|
2011-08-03 17:39:43 -05:00
|
|
|
// Function context. Every LLVM function we create will have one of
|
|
|
|
// these.
|
2014-04-22 07:56:37 -05:00
|
|
|
pub struct FunctionContext<'a, 'tcx: 'a> {
|
2015-10-21 16:35:15 -05:00
|
|
|
// The MIR for this function. At present, this is optional because
|
|
|
|
// we only have MIR available for things that are local to the
|
|
|
|
// crate.
|
2016-03-08 06:38:13 -06:00
|
|
|
pub mir: Option<CachedMir<'a, 'tcx>>,
|
2015-10-21 16:35:15 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// The ValueRef returned from a call to llvm::LLVMAddFunction; the
|
|
|
|
// address of the first instruction in the sequence of
|
|
|
|
// instructions for this function that will go in the .text
|
|
|
|
// section of the executable we're generating.
|
|
|
|
pub llfn: ValueRef,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2015-06-24 15:40:54 -05:00
|
|
|
// always an empty parameter-environment NOTE: @jroesch another use of ParamEnv
|
2016-03-24 22:22:52 -05:00
|
|
|
pub param_env: ty::ParameterEnvironment<'tcx>,
|
2015-01-02 03:09:35 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// A pointer to where to store the return value. If the return type is
|
|
|
|
// immediate, this points to an alloca in the function. Otherwise, it's a
|
|
|
|
// pointer to the hidden first parameter of the function. After function
|
|
|
|
// construction, this should always be Some.
|
|
|
|
pub llretslotptr: Cell<Option<ValueRef>>,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// These pub elements: "hoisted basic blocks" containing
|
|
|
|
// administrative activities that have to happen in only one place in
|
|
|
|
// the function, due to LLVM's quirks.
|
|
|
|
// A marker for the place where we want to insert the function's static
|
|
|
|
// allocas, so that LLVM will coalesce them into a single alloca call.
|
|
|
|
pub alloca_insert_pt: Cell<Option<ValueRef>>,
|
|
|
|
pub llreturn: Cell<Option<BasicBlockRef>>,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// If the function has any nested return's, including something like:
|
|
|
|
// fn foo() -> Option<Foo> { Some(Foo { x: return None }) }, then
|
|
|
|
// we use a separate alloca for each return
|
|
|
|
pub needs_ret_allocas: bool,
|
2014-08-11 21:16:00 -05:00
|
|
|
|
2015-10-23 20:18:44 -05:00
|
|
|
// When working with landingpad-based exceptions this value is alloca'd and
|
|
|
|
// later loaded when using the resume instruction. This ends up being
|
|
|
|
// critical to chaining landing pads and resuing already-translated
|
|
|
|
// cleanups.
|
|
|
|
//
|
|
|
|
// Note that for cleanuppad-based exceptions this is not used.
|
|
|
|
pub landingpad_alloca: Cell<Option<ValueRef>>,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// Maps the DefId's for local variables to the allocas created for
|
|
|
|
// them in llallocas.
|
|
|
|
pub lllocals: RefCell<NodeMap<LvalueDatum<'tcx>>>,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// Same as above, but for closure upvars
|
|
|
|
pub llupvars: RefCell<NodeMap<ValueRef>>,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2015-06-05 14:34:03 -05:00
|
|
|
// Carries info about drop-flags for local bindings (longer term,
|
|
|
|
// paths) for the code being compiled.
|
|
|
|
pub lldropflag_hints: RefCell<DropFlagHintsMap<'tcx>>,
|
|
|
|
|
2016-03-06 08:30:21 -06:00
|
|
|
// Describes the return/argument LLVM types and their ABI handling.
|
|
|
|
pub fn_ty: FnType,
|
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// If this function is being monomorphized, this contains the type
|
|
|
|
// substitutions used.
|
2015-01-29 06:03:34 -06:00
|
|
|
pub param_substs: &'tcx Substs<'tcx>,
|
2012-02-03 06:37:55 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// The source span and nesting context where this function comes from, for
|
|
|
|
// error reporting and symbol generation.
|
|
|
|
pub span: Option<Span>,
|
2011-08-02 17:13:08 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// The arena that blocks are allocated from.
|
|
|
|
pub block_arena: &'a TypedArena<BlockS<'a, 'tcx>>,
|
2014-01-07 10:54:58 -06:00
|
|
|
|
2016-02-08 04:53:06 -06:00
|
|
|
// The arena that landing pads are allocated from.
|
|
|
|
pub lpad_arena: TypedArena<LandingPad>,
|
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// This function's enclosing crate context.
|
|
|
|
pub ccx: &'a CrateContext<'a, 'tcx>,
|
2013-08-05 04:12:40 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// Used and maintained by the debuginfo module.
|
|
|
|
pub debug_context: debuginfo::FunctionDebugContext,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// Cleanup scopes.
|
|
|
|
pub scopes: RefCell<Vec<cleanup::CleanupScope<'a, 'tcx>>>,
|
2014-12-15 17:21:08 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub cfg: Option<cfg::CFG>,
|
2013-01-06 13:16:14 -06:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> FunctionContext<'a, 'tcx> {
|
2016-03-08 06:38:13 -06:00
|
|
|
pub fn mir(&self) -> CachedMir<'a, 'tcx> {
|
|
|
|
self.mir.clone().expect("fcx.mir was empty")
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn cleanup(&self) {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMInstructionEraseFromParent(self.alloca_insert_pt
|
|
|
|
.get()
|
|
|
|
.unwrap());
|
|
|
|
}
|
2013-07-12 21:09:57 -05:00
|
|
|
}
|
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn get_llreturn(&self) -> BasicBlockRef {
|
|
|
|
if self.llreturn.get().is_none() {
|
2014-03-15 15:29:34 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
self.llreturn.set(Some(unsafe {
|
|
|
|
llvm::LLVMAppendBasicBlockInContext(self.ccx.llcx(), self.llfn,
|
|
|
|
"return\0".as_ptr() as *const _)
|
|
|
|
}))
|
|
|
|
}
|
2012-06-26 15:50:43 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
self.llreturn.get().unwrap()
|
|
|
|
}
|
2014-08-11 21:16:00 -05:00
|
|
|
|
2016-03-06 08:30:21 -06:00
|
|
|
pub fn get_ret_slot(&self, bcx: Block<'a, 'tcx>, name: &str) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
if self.needs_ret_allocas {
|
2016-03-06 08:30:21 -06:00
|
|
|
base::alloca(bcx, self.fn_ty.ret.memory_ty(self.ccx), name)
|
2015-01-08 06:14:07 -06:00
|
|
|
} else {
|
|
|
|
self.llretslotptr.get().unwrap()
|
|
|
|
}
|
2013-07-02 14:47:32 -05:00
|
|
|
}
|
2013-06-16 07:51:50 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn new_block(&'a self,
|
|
|
|
name: &str,
|
2016-02-08 16:08:47 -06:00
|
|
|
opt_node_id: Option<ast::NodeId>)
|
2015-01-08 06:14:07 -06:00
|
|
|
-> Block<'a, 'tcx> {
|
|
|
|
unsafe {
|
2015-02-18 00:47:40 -06:00
|
|
|
let name = CString::new(name).unwrap();
|
2015-01-08 06:14:07 -06:00
|
|
|
let llbb = llvm::LLVMAppendBasicBlockInContext(self.ccx.llcx(),
|
|
|
|
self.llfn,
|
|
|
|
name.as_ptr());
|
2016-02-08 16:08:47 -06:00
|
|
|
BlockS::new(llbb, opt_node_id, self)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2012-03-23 19:52:20 -05:00
|
|
|
}
|
Implement scopes independent of LLVM basic blocks
Currently, scopes are tied to LLVM basic blocks. For each scope, there
are two new basic blocks, which means two extra jumps in the unoptimized
IR. These blocks aren't actually required, but only used to act as the
boundary for cleanups.
By keeping track of the current scope within a single basic block, we
can avoid those extra blocks and jumps, shrinking the pre-optimization
IR quite considerably. For example, the IR for trans_intrinsic goes
from ~22k lines to ~16k lines, almost 30% less.
The impact on the build times of optimized builds is rather small (about
1%), but unoptimized builds are about 11% faster. The testsuite for
unoptimized builds runs between 15% (CPU time) and 7.5% (wallclock time on
my i7) faster.
Also, in some situations this helps LLVM to generate better code by
inlining functions that it previously considered to be too large.
Likely because of the pointless blocks/jumps that were still present at
the time the inlining pass runs.
Refs #7462
2013-07-07 07:53:57 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn new_id_block(&'a self,
|
|
|
|
name: &str,
|
|
|
|
node_id: ast::NodeId)
|
|
|
|
-> Block<'a, 'tcx> {
|
2016-02-08 16:08:47 -06:00
|
|
|
self.new_block(name, Some(node_id))
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2013-12-30 20:57:48 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn new_temp_block(&'a self,
|
|
|
|
name: &str)
|
|
|
|
-> Block<'a, 'tcx> {
|
2016-02-08 16:08:47 -06:00
|
|
|
self.new_block(name, None)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2015-01-06 17:22:24 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn join_blocks(&'a self,
|
|
|
|
id: ast::NodeId,
|
|
|
|
in_cxs: &[Block<'a, 'tcx>])
|
|
|
|
-> Block<'a, 'tcx> {
|
|
|
|
let out = self.new_id_block("join", id);
|
|
|
|
let mut reachable = false;
|
2015-01-31 11:20:46 -06:00
|
|
|
for bcx in in_cxs {
|
2015-01-08 06:14:07 -06:00
|
|
|
if !bcx.unreachable.get() {
|
2014-12-11 06:53:30 -06:00
|
|
|
build::Br(*bcx, out.llbb, DebugLoc::None);
|
2015-01-08 06:14:07 -06:00
|
|
|
reachable = true;
|
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
2015-01-08 06:14:07 -06:00
|
|
|
if !reachable {
|
|
|
|
build::Unreachable(out);
|
|
|
|
}
|
|
|
|
return out;
|
2013-05-02 20:15:36 -05:00
|
|
|
}
|
2015-01-06 17:22:24 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn monomorphize<T>(&self, value: &T) -> T
|
2016-05-10 20:14:41 -05:00
|
|
|
where T: TransNormalize<'tcx>
|
2015-01-08 06:14:07 -06:00
|
|
|
{
|
|
|
|
monomorphize::apply_param_substs(self.ccx.tcx(),
|
|
|
|
self.param_substs,
|
|
|
|
value)
|
|
|
|
}
|
2015-02-25 16:09:58 -06:00
|
|
|
|
|
|
|
/// This is the same as `common::type_needs_drop`, except that it
|
|
|
|
/// may use or update caches within this `FunctionContext`.
|
|
|
|
pub fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
|
2015-12-31 15:56:40 -06:00
|
|
|
self.ccx.tcx().type_needs_drop_given_env(ty, &self.param_env)
|
2015-02-25 16:09:58 -06:00
|
|
|
}
|
2015-07-20 15:27:38 -05:00
|
|
|
|
|
|
|
pub fn eh_personality(&self) -> ValueRef {
|
|
|
|
// The exception handling personality function.
|
|
|
|
//
|
|
|
|
// If our compilation unit has the `eh_personality` lang item somewhere
|
|
|
|
// within it, then we just need to translate that. Otherwise, we're
|
|
|
|
// building an rlib which will depend on some upstream implementation of
|
|
|
|
// this function, so we just codegen a generic reference to it. We don't
|
|
|
|
// specify any of the types for the function, we just make it a symbol
|
|
|
|
// that LLVM can later use.
|
|
|
|
//
|
|
|
|
// Note that MSVC is a little special here in that we don't use the
|
|
|
|
// `eh_personality` lang item at all. Currently LLVM has support for
|
|
|
|
// both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
|
|
|
|
// *name of the personality function* to decide what kind of unwind side
|
|
|
|
// tables/landing pads to emit. It looks like Dwarf is used by default,
|
|
|
|
// injecting a dependency on the `_Unwind_Resume` symbol for resuming
|
|
|
|
// an "exception", but for MSVC we want to force SEH. This means that we
|
|
|
|
// can't actually have the personality function be our standard
|
|
|
|
// `rust_eh_personality` function, but rather we wired it up to the
|
|
|
|
// CRT's custom personality function, which forces LLVM to consider
|
|
|
|
// landing pads as "landing pads for SEH".
|
2016-02-23 13:21:50 -06:00
|
|
|
let ccx = self.ccx;
|
|
|
|
let tcx = ccx.tcx();
|
|
|
|
match tcx.lang_items.eh_personality() {
|
|
|
|
Some(def_id) if !base::wants_msvc_seh(ccx.sess()) => {
|
|
|
|
Callee::def(ccx, def_id, tcx.mk_substs(Substs::empty())).reify(ccx).val
|
2015-07-20 15:27:38 -05:00
|
|
|
}
|
rustc: Use C++ personalities on MSVC
Currently the compiler has two relatively critical bugs in the implementation of
MSVC unwinding:
* #33112 - faults like segfaults and illegal instructions will run destructors
in Rust, meaning we keep running code after a super-fatal exception
has happened.
* #33116 - When compiling with LTO plus `-Z no-landing-pads` (or `-C
panic=abort` with the previous commit) LLVM won't remove all `invoke`
instructions, meaning that some landing pads stick around and
cleanups may be run due to the previous bug.
These both stem from the flavor of "personality function" that Rust uses for
unwinding on MSVC. On 32-bit this is `_except_handler3` and on 64-bit this is
`__C_specific_handler`, but they both essentially are the "most generic"
personality functions for catching exceptions and running cleanups. That is,
thse two personalities will run cleanups for all exceptions unconditionally, so
when we use them we run cleanups for **all SEH exceptions** (include things like
segfaults).
Note that this also explains why LLVM won't optimize away `invoke` instructions.
These functions can legitimately still unwind (the `nounwind` attribute only
seems to apply to "C++ exception-like unwining"). Also note that the standard
library only *catches* Rust exceptions, not others like segfaults and illegal
instructions.
LLVM has support for another personality, `__CxxFrameHandler3`, which does not
run cleanups for general exceptions, only C++ exceptions thrown by
`_CxxThrowException`. This essentially ideally matches our use case, so this
commit moves us over to using this well-known personality function as well as
exception-throwing function.
This doesn't *seem* to pull in any extra runtime dependencies just yet, but if
it does we can perhaps try to work out how to implement more of it in Rust
rather than relying on MSVCRT runtime bits.
More details about how this is actually implemented can be found in the changes
itself, but this...
Closes #33112
Closes #33116
2016-04-26 16:30:01 -05:00
|
|
|
_ => {
|
|
|
|
if let Some(llpersonality) = ccx.eh_personality().get() {
|
|
|
|
return llpersonality
|
|
|
|
}
|
|
|
|
let name = if base::wants_msvc_seh(ccx.sess()) {
|
|
|
|
"__CxxFrameHandler3"
|
2016-02-23 13:46:08 -06:00
|
|
|
} else {
|
rustc: Use C++ personalities on MSVC
Currently the compiler has two relatively critical bugs in the implementation of
MSVC unwinding:
* #33112 - faults like segfaults and illegal instructions will run destructors
in Rust, meaning we keep running code after a super-fatal exception
has happened.
* #33116 - When compiling with LTO plus `-Z no-landing-pads` (or `-C
panic=abort` with the previous commit) LLVM won't remove all `invoke`
instructions, meaning that some landing pads stick around and
cleanups may be run due to the previous bug.
These both stem from the flavor of "personality function" that Rust uses for
unwinding on MSVC. On 32-bit this is `_except_handler3` and on 64-bit this is
`__C_specific_handler`, but they both essentially are the "most generic"
personality functions for catching exceptions and running cleanups. That is,
thse two personalities will run cleanups for all exceptions unconditionally, so
when we use them we run cleanups for **all SEH exceptions** (include things like
segfaults).
Note that this also explains why LLVM won't optimize away `invoke` instructions.
These functions can legitimately still unwind (the `nounwind` attribute only
seems to apply to "C++ exception-like unwining"). Also note that the standard
library only *catches* Rust exceptions, not others like segfaults and illegal
instructions.
LLVM has support for another personality, `__CxxFrameHandler3`, which does not
run cleanups for general exceptions, only C++ exceptions thrown by
`_CxxThrowException`. This essentially ideally matches our use case, so this
commit moves us over to using this well-known personality function as well as
exception-throwing function.
This doesn't *seem* to pull in any extra runtime dependencies just yet, but if
it does we can perhaps try to work out how to implement more of it in Rust
rather than relying on MSVCRT runtime bits.
More details about how this is actually implemented can be found in the changes
itself, but this...
Closes #33112
Closes #33116
2016-04-26 16:30:01 -05:00
|
|
|
"rust_eh_personality"
|
2016-02-23 13:46:08 -06:00
|
|
|
};
|
|
|
|
let fty = Type::variadic_func(&[], &Type::i32(ccx));
|
|
|
|
let f = declare::declare_cfn(ccx, name, fty);
|
|
|
|
ccx.eh_personality().set(Some(f));
|
|
|
|
f
|
2015-07-20 15:27:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 20:11:44 -05:00
|
|
|
|
2015-10-18 16:17:34 -05:00
|
|
|
// Returns a ValueRef of the "eh_unwind_resume" lang item if one is defined,
|
2016-02-09 10:52:39 -06:00
|
|
|
// otherwise declares it as an external function.
|
2016-02-23 13:57:22 -06:00
|
|
|
pub fn eh_unwind_resume(&self) -> Callee<'tcx> {
|
2016-03-22 12:23:36 -05:00
|
|
|
use attributes;
|
2016-02-23 13:57:22 -06:00
|
|
|
let ccx = self.ccx;
|
|
|
|
let tcx = ccx.tcx();
|
|
|
|
assert!(ccx.sess().target.target.options.custom_unwind_resume);
|
|
|
|
if let Some(def_id) = tcx.lang_items.eh_unwind_resume() {
|
|
|
|
return Callee::def(ccx, def_id, tcx.mk_substs(Substs::empty()));
|
|
|
|
}
|
|
|
|
|
2016-04-29 00:30:54 -05:00
|
|
|
let ty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
|
2016-02-23 13:57:22 -06:00
|
|
|
unsafety: hir::Unsafety::Unsafe,
|
|
|
|
abi: Abi::C,
|
|
|
|
sig: ty::Binder(ty::FnSig {
|
|
|
|
inputs: vec![tcx.mk_mut_ptr(tcx.types.u8)],
|
|
|
|
output: ty::FnDiverging,
|
|
|
|
variadic: false
|
|
|
|
}),
|
2016-04-29 00:30:54 -05:00
|
|
|
}));
|
2016-02-23 13:57:22 -06:00
|
|
|
|
|
|
|
let unwresume = ccx.eh_unwind_resume();
|
|
|
|
if let Some(llfn) = unwresume.get() {
|
|
|
|
return Callee::ptr(datum::immediate_rvalue(llfn, ty));
|
2015-07-13 20:11:44 -05:00
|
|
|
}
|
2016-02-23 13:57:22 -06:00
|
|
|
let llfn = declare::declare_fn(ccx, "rust_eh_unwind_resume", ty);
|
|
|
|
attributes::unwind(llfn, true);
|
|
|
|
unwresume.set(Some(llfn));
|
|
|
|
Callee::ptr(datum::immediate_rvalue(llfn, ty))
|
2015-07-13 20:11:44 -05:00
|
|
|
}
|
2013-05-02 20:15:36 -05:00
|
|
|
}
|
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
// Basic block context. We create a block context for each basic block
|
|
|
|
// (single-entry, single-exit sequence of instructions) we generate from Rust
|
|
|
|
// code. Each basic block we generate is attached to a function, typically
|
|
|
|
// with many basic blocks per function. All the basic blocks attached to a
|
|
|
|
// function are organized as a directed graph.
|
2014-09-06 11:13:04 -05:00
|
|
|
pub struct BlockS<'blk, 'tcx: 'blk> {
|
2015-01-08 06:14:07 -06:00
|
|
|
// The BasicBlockRef returned from a call to
|
|
|
|
// llvm::LLVMAppendBasicBlock(llfn, name), which adds a basic
|
|
|
|
// block to the function pointed to by llfn. We insert
|
|
|
|
// instructions into that block by way of this block context.
|
|
|
|
// The block pointing to this one in the function's digraph.
|
|
|
|
pub llbb: BasicBlockRef,
|
|
|
|
pub terminated: Cell<bool>,
|
|
|
|
pub unreachable: Cell<bool>,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2015-10-23 20:18:44 -05:00
|
|
|
// If this block part of a landing pad, then this is `Some` indicating what
|
|
|
|
// kind of landing pad its in, otherwise this is none.
|
2016-02-08 04:53:06 -06:00
|
|
|
pub lpad: Cell<Option<&'blk LandingPad>>,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// AST node-id associated with this block, if any. Used for
|
|
|
|
// debugging purposes only.
|
|
|
|
pub opt_node_id: Option<ast::NodeId>,
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
// The function context for the function to which this block is
|
|
|
|
// attached.
|
|
|
|
pub fcx: &'blk FunctionContext<'blk, 'tcx>,
|
2013-07-17 05:12:08 -05:00
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub type Block<'blk, 'tcx> = &'blk BlockS<'blk, 'tcx>;
|
|
|
|
|
|
|
|
impl<'blk, 'tcx> BlockS<'blk, 'tcx> {
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn new(llbb: BasicBlockRef,
|
|
|
|
opt_node_id: Option<ast::NodeId>,
|
|
|
|
fcx: &'blk FunctionContext<'blk, 'tcx>)
|
|
|
|
-> Block<'blk, 'tcx> {
|
|
|
|
fcx.block_arena.alloc(BlockS {
|
|
|
|
llbb: llbb,
|
|
|
|
terminated: Cell::new(false),
|
|
|
|
unreachable: Cell::new(false),
|
2016-02-08 04:53:06 -06:00
|
|
|
lpad: Cell::new(None),
|
2015-01-08 06:14:07 -06:00
|
|
|
opt_node_id: opt_node_id,
|
|
|
|
fcx: fcx
|
|
|
|
})
|
|
|
|
}
|
2013-07-17 05:12:08 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> {
|
|
|
|
self.fcx.ccx
|
|
|
|
}
|
2016-02-01 04:04:49 -06:00
|
|
|
pub fn fcx(&self) -> &'blk FunctionContext<'blk, 'tcx> {
|
|
|
|
self.fcx
|
|
|
|
}
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn tcx(&self) -> TyCtxt<'blk, 'tcx, 'tcx> {
|
2015-01-08 06:14:07 -06:00
|
|
|
self.fcx.ccx.tcx()
|
|
|
|
}
|
|
|
|
pub fn sess(&self) -> &'blk Session { self.fcx.ccx.sess() }
|
2012-09-05 17:58:43 -05:00
|
|
|
|
2016-02-08 04:53:06 -06:00
|
|
|
pub fn lpad(&self) -> Option<&'blk LandingPad> {
|
|
|
|
self.lpad.get()
|
|
|
|
}
|
|
|
|
|
2016-03-08 06:38:13 -06:00
|
|
|
pub fn mir(&self) -> CachedMir<'blk, 'tcx> {
|
2015-10-21 16:35:15 -05:00
|
|
|
self.fcx.mir()
|
|
|
|
}
|
|
|
|
|
2015-03-23 19:41:35 -05:00
|
|
|
pub fn name(&self, name: ast::Name) -> String {
|
2015-07-28 11:07:20 -05:00
|
|
|
name.to_string()
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2012-09-05 17:58:43 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn node_id_to_string(&self, id: ast::NodeId) -> String {
|
|
|
|
self.tcx().map.node_to_string(id).to_string()
|
|
|
|
}
|
2013-07-17 05:12:08 -05:00
|
|
|
|
2016-01-20 13:31:10 -06:00
|
|
|
pub fn def(&self, nid: ast::NodeId) -> Def {
|
2015-01-08 06:14:07 -06:00
|
|
|
match self.tcx().def_map.borrow().get(&nid) {
|
2015-02-16 22:44:23 -06:00
|
|
|
Some(v) => v.full_def(),
|
2015-01-08 06:14:07 -06:00
|
|
|
None => {
|
2016-03-28 18:46:02 -05:00
|
|
|
bug!("no def associated with node id {}", nid);
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2013-07-17 05:12:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn to_str(&self) -> String {
|
|
|
|
format!("[block {:p}]", self)
|
|
|
|
}
|
2014-12-17 13:16:28 -06:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn monomorphize<T>(&self, value: &T) -> T
|
2016-05-10 20:14:41 -05:00
|
|
|
where T: TransNormalize<'tcx>
|
2015-01-08 06:14:07 -06:00
|
|
|
{
|
|
|
|
monomorphize::apply_param_substs(self.tcx(),
|
|
|
|
self.fcx.param_substs,
|
|
|
|
value)
|
|
|
|
}
|
2016-02-01 04:04:49 -06:00
|
|
|
|
|
|
|
pub fn build(&'blk self) -> BlockAndBuilder<'blk, 'tcx> {
|
|
|
|
BlockAndBuilder::new(self, OwnedBuilder::new_with_ccx(self.ccx()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct OwnedBuilder<'blk, 'tcx: 'blk> {
|
|
|
|
builder: Builder<'blk, 'tcx>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'blk, 'tcx> OwnedBuilder<'blk, 'tcx> {
|
|
|
|
pub fn new_with_ccx(ccx: &'blk CrateContext<'blk, 'tcx>) -> Self {
|
|
|
|
// Create a fresh builder from the crate context.
|
|
|
|
let llbuilder = unsafe {
|
|
|
|
llvm::LLVMCreateBuilderInContext(ccx.llcx())
|
|
|
|
};
|
|
|
|
OwnedBuilder {
|
|
|
|
builder: Builder {
|
|
|
|
llbuilder: llbuilder,
|
|
|
|
ccx: ccx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'blk, 'tcx> Drop for OwnedBuilder<'blk, 'tcx> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMDisposeBuilder(self.builder.llbuilder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BlockAndBuilder<'blk, 'tcx: 'blk> {
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
owned_builder: OwnedBuilder<'blk, 'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'blk, 'tcx> BlockAndBuilder<'blk, 'tcx> {
|
|
|
|
pub fn new(bcx: Block<'blk, 'tcx>, owned_builder: OwnedBuilder<'blk, 'tcx>) -> Self {
|
|
|
|
// Set the builder's position to this block's end.
|
|
|
|
owned_builder.builder.position_at_end(bcx.llbb);
|
|
|
|
BlockAndBuilder {
|
|
|
|
bcx: bcx,
|
|
|
|
owned_builder: owned_builder,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_block<F, R>(&self, f: F) -> R
|
|
|
|
where F: FnOnce(Block<'blk, 'tcx>) -> R
|
|
|
|
{
|
|
|
|
let result = f(self.bcx);
|
|
|
|
self.position_at_end(self.bcx.llbb);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn map_block<F>(self, f: F) -> Self
|
|
|
|
where F: FnOnce(Block<'blk, 'tcx>) -> Block<'blk, 'tcx>
|
|
|
|
{
|
|
|
|
let BlockAndBuilder { bcx, owned_builder } = self;
|
|
|
|
let bcx = f(bcx);
|
|
|
|
BlockAndBuilder::new(bcx, owned_builder)
|
|
|
|
}
|
|
|
|
|
2016-02-04 11:40:28 -06:00
|
|
|
pub fn at_start<F, R>(&self, f: F) -> R
|
|
|
|
where F: FnOnce(&BlockAndBuilder<'blk, 'tcx>) -> R
|
|
|
|
{
|
|
|
|
self.position_at_start(self.bcx.llbb);
|
|
|
|
let r = f(self);
|
|
|
|
self.position_at_end(self.bcx.llbb);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2016-02-01 04:04:49 -06:00
|
|
|
// Methods delegated to bcx
|
|
|
|
|
2016-03-09 06:20:22 -06:00
|
|
|
pub fn is_unreachable(&self) -> bool {
|
|
|
|
self.bcx.unreachable.get()
|
|
|
|
}
|
|
|
|
|
2016-02-01 04:04:49 -06:00
|
|
|
pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> {
|
|
|
|
self.bcx.ccx()
|
|
|
|
}
|
|
|
|
pub fn fcx(&self) -> &'blk FunctionContext<'blk, 'tcx> {
|
|
|
|
self.bcx.fcx()
|
|
|
|
}
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn tcx(&self) -> TyCtxt<'blk, 'tcx, 'tcx> {
|
2016-02-01 04:04:49 -06:00
|
|
|
self.bcx.tcx()
|
|
|
|
}
|
|
|
|
pub fn sess(&self) -> &'blk Session {
|
|
|
|
self.bcx.sess()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn llbb(&self) -> BasicBlockRef {
|
|
|
|
self.bcx.llbb
|
|
|
|
}
|
|
|
|
|
2016-03-08 06:38:13 -06:00
|
|
|
pub fn mir(&self) -> CachedMir<'blk, 'tcx> {
|
2016-02-01 04:04:49 -06:00
|
|
|
self.bcx.mir()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn monomorphize<T>(&self, value: &T) -> T
|
2016-05-10 20:14:41 -05:00
|
|
|
where T: TransNormalize<'tcx>
|
2016-02-01 04:04:49 -06:00
|
|
|
{
|
|
|
|
self.bcx.monomorphize(value)
|
|
|
|
}
|
2016-02-11 14:57:09 -06:00
|
|
|
|
|
|
|
pub fn set_lpad(&self, lpad: Option<LandingPad>) {
|
|
|
|
self.bcx.lpad.set(lpad.map(|p| &*self.fcx().lpad_arena.alloc(p)))
|
|
|
|
}
|
2016-02-01 04:04:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'blk, 'tcx> Deref for BlockAndBuilder<'blk, 'tcx> {
|
|
|
|
type Target = Builder<'blk, 'tcx>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.owned_builder.builder
|
|
|
|
}
|
2012-06-12 16:55:44 -05:00
|
|
|
}
|
2011-07-21 19:27:34 -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.
|
|
|
|
pub struct LandingPad {
|
|
|
|
cleanuppad: Option<ValueRef>,
|
|
|
|
operand: Option<OperandBundleDef>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LandingPad {
|
|
|
|
pub fn gnu() -> LandingPad {
|
|
|
|
LandingPad { cleanuppad: None, operand: None }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn msvc(cleanuppad: ValueRef) -> LandingPad {
|
|
|
|
LandingPad {
|
|
|
|
cleanuppad: Some(cleanuppad),
|
|
|
|
operand: Some(OperandBundleDef::new("funclet", &[cleanuppad])),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bundle(&self) -> Option<&OperandBundleDef> {
|
|
|
|
self.operand.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for LandingPad {
|
|
|
|
fn clone(&self) -> LandingPad {
|
|
|
|
LandingPad {
|
|
|
|
cleanuppad: self.cleanuppad,
|
|
|
|
operand: self.cleanuppad.map(|p| {
|
|
|
|
OperandBundleDef::new("funclet", &[p])
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub struct Result<'blk, 'tcx: 'blk> {
|
2015-01-08 06:14:07 -06:00
|
|
|
pub bcx: Block<'blk, 'tcx>,
|
|
|
|
pub val: ValueRef
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'b, 'tcx> Result<'b, 'tcx> {
|
2015-01-08 06:14:07 -06:00
|
|
|
pub fn new(bcx: Block<'b, 'tcx>, val: ValueRef) -> Result<'b, 'tcx> {
|
|
|
|
Result {
|
|
|
|
bcx: bcx,
|
|
|
|
val: val,
|
|
|
|
}
|
2014-01-07 10:54:58 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2011-07-21 19:27:34 -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
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
pub fn C_integral(t: Type, u: u64, sign_extend: bool) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstInt(t.to_ref(), u, sign_extend as Bool)
|
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
pub fn C_floating(s: &str, t: Type) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
2015-02-18 00:47:40 -06:00
|
|
|
let s = CString::new(s).unwrap();
|
2015-01-08 06:14:07 -06:00
|
|
|
llvm::LLVMConstRealOfString(t.to_ref(), s.as_ptr())
|
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2015-10-21 16:42:25 -05:00
|
|
|
pub fn C_floating_f64(f: f64, t: Type) -> ValueRef {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstReal(t.to_ref(), f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_nil(ccx: &CrateContext) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
C_struct(ccx, &[], false)
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_bool(ccx: &CrateContext, val: bool) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
C_integral(Type::i1(ccx), val as u64, false)
|
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 {
|
2015-01-08 06:14:07 -06:00
|
|
|
C_integral(Type::i32(ccx), i as u64, true)
|
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 {
|
|
|
|
C_integral(Type::i32(ccx), i as u64, false)
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
pub fn C_u64(ccx: &CrateContext, i: u64) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
C_integral(Type::i64(ccx), i, false)
|
2013-09-29 04:20:11 -05:00
|
|
|
}
|
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn C_int<I: AsI64>(ccx: &CrateContext, i: I) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
let v = i.as_i64();
|
2014-10-15 12:26:43 -05:00
|
|
|
|
2015-08-15 01:43:39 -05:00
|
|
|
let bit_size = machine::llbitsize_of_real(ccx, ccx.int_type());
|
|
|
|
|
|
|
|
if bit_size < 64 {
|
|
|
|
// make sure it doesn't overflow
|
|
|
|
assert!(v < (1<<(bit_size-1)) && v >= -(1<<(bit_size-1)));
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2014-10-15 12:26:43 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
C_integral(ccx.int_type(), v as u64, true)
|
2011-10-14 18:45:25 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn C_uint<I: AsU64>(ccx: &CrateContext, i: I) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
let v = i.as_u64();
|
2014-10-15 12:26:43 -05:00
|
|
|
|
2015-08-15 01:43:39 -05:00
|
|
|
let bit_size = machine::llbitsize_of_real(ccx, ccx.int_type());
|
|
|
|
|
|
|
|
if bit_size < 64 {
|
|
|
|
// make sure it doesn't overflow
|
|
|
|
assert!(v < (1<<bit_size));
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2014-10-15 12:26:43 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
C_integral(ccx.int_type(), v, false)
|
2011-10-14 18:45:25 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub trait AsI64 { fn as_i64(self) -> i64; }
|
|
|
|
pub trait AsU64 { fn as_u64(self) -> u64; }
|
|
|
|
|
2014-10-15 12:26:43 -05:00
|
|
|
// FIXME: remove the intptr conversions, because they
|
|
|
|
// are host-architecture-dependent
|
2014-10-14 15:36:11 -05:00
|
|
|
impl AsI64 for i64 { fn as_i64(self) -> i64 { self as i64 }}
|
|
|
|
impl AsI64 for i32 { fn as_i64(self) -> i64 { self as i64 }}
|
2015-03-25 19:06:52 -05:00
|
|
|
impl AsI64 for isize { fn as_i64(self) -> i64 { self as i64 }}
|
2014-10-14 15:36:11 -05:00
|
|
|
|
|
|
|
impl AsU64 for u64 { fn as_u64(self) -> u64 { self as u64 }}
|
|
|
|
impl AsU64 for u32 { fn as_u64(self) -> u64 { self as u64 }}
|
2015-03-25 19:06:52 -05:00
|
|
|
impl AsU64 for usize { fn as_u64(self) -> u64 { self as u64 }}
|
2014-10-14 15:36:11 -05:00
|
|
|
|
2015-08-05 02:46:59 -05:00
|
|
|
pub fn C_u8(ccx: &CrateContext, i: u8) -> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
C_integral(Type::i8(ccx), i as u64, false)
|
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);
|
2013-06-15 22:45:48 -05:00
|
|
|
|
2015-01-08 06:14:07 -06:00
|
|
|
let gsym = token::gensym("str");
|
2015-09-24 15:05:02 -05:00
|
|
|
let sym = format!("str{}", gsym.0);
|
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);
|
|
|
|
llvm::SetLinkage(g, llvm::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();
|
2015-01-08 06:14:07 -06:00
|
|
|
let cs = consts::ptrcast(C_cstr(cx, s, false), Type::i8p(cx));
|
|
|
|
C_named_struct(cx.tn().find_type("str_slice").unwrap(), &[cs, C_uint(cx, len)])
|
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
|
|
|
}
|
|
|
|
|
2016-02-18 11:49:45 -06:00
|
|
|
pub fn const_get_elt(v: ValueRef, us: &[c_uint])
|
2015-01-06 17:22:24 -06:00
|
|
|
-> ValueRef {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2016-02-18 11:49:45 -06:00
|
|
|
debug!("const_get_elt(v={:?}, us={:?}, r={:?})",
|
|
|
|
Value(v), us, 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_int(v: ValueRef) -> i64 {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstIntGetSExtValue(v)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-03-26 19:37:10 -05:00
|
|
|
fn is_const_integral(v: ValueRef) -> bool {
|
|
|
|
unsafe {
|
|
|
|
!llvm::LLVMIsAConstantInt(v).is_null()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn const_to_opt_int(v: ValueRef) -> Option<i64> {
|
|
|
|
unsafe {
|
|
|
|
if is_const_integral(v) {
|
|
|
|
Some(llvm::LLVMConstIntGetSExtValue(v))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn const_to_opt_uint(v: ValueRef) -> Option<u64> {
|
|
|
|
unsafe {
|
|
|
|
if is_const_integral(v) {
|
|
|
|
Some(llvm::LLVMConstIntGetZExtValue(v))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 16:16:21 -06:00
|
|
|
pub fn is_undef(val: ValueRef) -> bool {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMIsUndef(val) != False
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2014-12-04 15:44:51 -06:00
|
|
|
#[allow(dead_code)] // potentially useful
|
2013-03-31 17:55:30 -05:00
|
|
|
pub fn is_null(val: ValueRef) -> bool {
|
2015-01-08 06:14:07 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMIsNull(val) != False
|
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn monomorphize_type<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, t: Ty<'tcx>) -> Ty<'tcx> {
|
2015-01-08 06:14:07 -06:00
|
|
|
bcx.fcx.monomorphize(&t)
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn node_id_type<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, id: ast::NodeId) -> Ty<'tcx> {
|
2015-01-08 06:14:07 -06:00
|
|
|
let tcx = bcx.tcx();
|
2015-06-25 15:42:17 -05:00
|
|
|
let t = tcx.node_id_to_type(id);
|
2015-01-08 06:14:07 -06:00
|
|
|
monomorphize_type(bcx, t)
|
2012-02-02 05:37:17 -06:00
|
|
|
}
|
2012-09-10 14:25:45 -05:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
pub fn expr_ty<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, ex: &hir::Expr) -> Ty<'tcx> {
|
2015-01-08 06:14:07 -06:00
|
|
|
node_id_type(bcx, ex.id)
|
2012-02-02 05:37:17 -06:00
|
|
|
}
|
2012-09-10 14:25:45 -05:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
pub fn expr_ty_adjusted<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, ex: &hir::Expr) -> Ty<'tcx> {
|
2015-06-25 15:42:17 -05:00
|
|
|
monomorphize_type(bcx, bcx.tcx().expr_ty_adjusted(ex))
|
2013-02-27 18:28:37 -06:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Attempts to resolve an obligation. The result is a shallow vtable resolution -- meaning that we
|
|
|
|
/// do not (necessarily) resolve all nested obligations on the impl. Note that type check should
|
|
|
|
/// guarantee to us that all nested obligations *could be* resolved if we wanted to.
|
2016-05-06 16:07:36 -05:00
|
|
|
pub fn fulfill_obligation<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
|
2015-03-17 05:24:11 -05:00
|
|
|
span: Span,
|
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>)
|
|
|
|
-> traits::Vtable<'tcx, ()>
|
2014-09-12 10:42:58 -05:00
|
|
|
{
|
2016-05-06 16:07:36 -05:00
|
|
|
let tcx = scx.tcx();
|
2015-01-08 06:14:07 -06:00
|
|
|
|
|
|
|
// Remove any references to regions; this helps improve caching.
|
2015-09-14 16:47:14 -05:00
|
|
|
let trait_ref = tcx.erase_regions(&trait_ref);
|
2015-01-08 06:14:07 -06:00
|
|
|
|
2016-05-06 16:07:36 -05:00
|
|
|
scx.trait_cache().memoize(trait_ref, || {
|
2016-05-05 23:47:28 -05:00
|
|
|
debug!("trans fulfill_obligation: trait_ref={:?} def_id={:?}",
|
|
|
|
trait_ref, trait_ref.def_id());
|
|
|
|
|
|
|
|
// Do the initial selection for the obligation. This yields the
|
|
|
|
// shallow result we are looking for -- that is, what specific impl.
|
2016-05-10 20:14:41 -05:00
|
|
|
tcx.normalizing_infer_ctxt(ProjectionMode::Any).enter(|infcx| {
|
2016-03-24 22:22:44 -05:00
|
|
|
let mut selcx = SelectionContext::new(&infcx);
|
2016-05-05 23:47:28 -05:00
|
|
|
|
2016-03-24 22:22:44 -05:00
|
|
|
let obligation_cause = traits::ObligationCause::misc(span,
|
2016-05-05 23:47:28 -05:00
|
|
|
ast::DUMMY_NODE_ID);
|
2016-03-24 22:22:44 -05:00
|
|
|
let obligation = traits::Obligation::new(obligation_cause,
|
|
|
|
trait_ref.to_poly_trait_predicate());
|
|
|
|
|
|
|
|
let selection = match selcx.select(&obligation) {
|
|
|
|
Ok(Some(selection)) => selection,
|
|
|
|
Ok(None) => {
|
|
|
|
// Ambiguity can happen when monomorphizing during trans
|
|
|
|
// expands to some humongo type that never occurred
|
|
|
|
// statically -- this humongo type can then overflow,
|
|
|
|
// leading to an ambiguous result. So report this as an
|
|
|
|
// overflow bug, since I believe this is the only case
|
|
|
|
// where ambiguity can result.
|
|
|
|
debug!("Encountered ambiguity selecting `{:?}` during trans, \
|
|
|
|
presuming due to overflow",
|
|
|
|
trait_ref);
|
|
|
|
tcx.sess.span_fatal(span,
|
|
|
|
"reached the recursion limit during monomorphization \
|
|
|
|
(selection ambiguity)");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
span_bug!(span, "Encountered error `{:?}` selecting `{:?}` during trans",
|
|
|
|
e, trait_ref)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Currently, we use a fulfillment context to completely resolve
|
|
|
|
// all nested obligations. This is because they can inform the
|
|
|
|
// inference of the impl's type parameters.
|
|
|
|
let mut fulfill_cx = traits::FulfillmentContext::new();
|
|
|
|
let vtable = selection.map(|predicate| {
|
|
|
|
fulfill_cx.register_predicate_obligation(&infcx, predicate);
|
|
|
|
});
|
|
|
|
let vtable = infcx.drain_fulfillment_cx_or_panic(span, &mut fulfill_cx, &vtable);
|
|
|
|
|
|
|
|
info!("Cache miss: {:?} => {:?}", trait_ref, vtable);
|
|
|
|
vtable
|
|
|
|
})
|
2016-05-10 20:14:41 -05:00
|
|
|
})
|
2014-12-31 13:42:06 -06:00
|
|
|
}
|
|
|
|
|
2015-03-18 14:26:38 -05:00
|
|
|
/// Normalizes the predicates and checks whether they hold. If this
|
|
|
|
/// returns false, then either normalize encountered an error or one
|
|
|
|
/// of the predicates did not hold. Used when creating vtables to
|
|
|
|
/// check for unsatisfiable methods.
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn normalize_and_test_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-02 20:56:42 -05:00
|
|
|
predicates: Vec<ty::Predicate<'tcx>>)
|
|
|
|
-> bool
|
2015-03-17 05:24:11 -05:00
|
|
|
{
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("normalize_and_test_predicates(predicates={:?})",
|
|
|
|
predicates);
|
2015-03-17 05:24:11 -05:00
|
|
|
|
2016-05-10 20:14:41 -05:00
|
|
|
tcx.normalizing_infer_ctxt(ProjectionMode::Any).enter(|infcx| {
|
2016-03-24 22:22:44 -05:00
|
|
|
let mut selcx = SelectionContext::new(&infcx);
|
|
|
|
let mut fulfill_cx = traits::FulfillmentContext::new();
|
|
|
|
let cause = traits::ObligationCause::dummy();
|
|
|
|
let traits::Normalized { value: predicates, obligations } =
|
|
|
|
traits::normalize(&mut selcx, cause.clone(), &predicates);
|
|
|
|
for obligation in obligations {
|
|
|
|
fulfill_cx.register_predicate_obligation(&infcx, obligation);
|
|
|
|
}
|
|
|
|
for predicate in predicates {
|
|
|
|
let obligation = traits::Obligation::new(cause.clone(), predicate);
|
|
|
|
fulfill_cx.register_predicate_obligation(&infcx, obligation);
|
|
|
|
}
|
2014-09-12 10:42:58 -05:00
|
|
|
|
2016-03-24 22:22:44 -05:00
|
|
|
infcx.drain_fulfillment_cx(&mut fulfill_cx, &()).is_ok()
|
|
|
|
})
|
2014-09-12 10:42:58 -05:00
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn langcall(bcx: Block,
|
2014-01-07 10:54:58 -06:00
|
|
|
span: Option<Span>,
|
|
|
|
msg: &str,
|
|
|
|
li: LangItem)
|
2015-08-16 05:32:28 -05:00
|
|
|
-> DefId {
|
2013-07-15 22:42:13 -05:00
|
|
|
match bcx.tcx().lang_items.require(li) {
|
|
|
|
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 {
|
2015-02-18 13:48:57 -06:00
|
|
|
Some(span) => bcx.tcx().sess.span_fatal(span, &msg[..]),
|
|
|
|
None => bcx.tcx().sess.fatal(&msg[..]),
|
2013-07-15 22:42:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-02 14:52:50 -05:00
|
|
|
|
|
|
|
/// Return the VariantDef corresponding to an inlined variant node
|
|
|
|
pub fn inlined_variant_def<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
inlined_vid: ast::NodeId)
|
2015-08-07 06:41:33 -05:00
|
|
|
-> ty::VariantDef<'tcx>
|
2015-08-02 14:52:50 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
let ctor_ty = ccx.tcx().node_id_to_type(inlined_vid);
|
|
|
|
debug!("inlined_variant_def: ctor_ty={:?} inlined_vid={:?}", ctor_ty,
|
|
|
|
inlined_vid);
|
|
|
|
let adt_def = match ctor_ty.sty {
|
2016-02-16 10:36:41 -06:00
|
|
|
ty::TyFnDef(_, _, &ty::BareFnTy { sig: ty::Binder(ty::FnSig {
|
2015-08-02 14:52:50 -05:00
|
|
|
output: ty::FnConverging(ty), ..
|
|
|
|
}), ..}) => ty,
|
|
|
|
_ => ctor_ty
|
|
|
|
}.ty_adt_def().unwrap();
|
2015-09-02 15:11:32 -05:00
|
|
|
let inlined_vid_def_id = ccx.tcx().map.local_def_id(inlined_vid);
|
2015-08-02 14:52:50 -05:00
|
|
|
adt_def.variants.iter().find(|v| {
|
2015-09-02 15:11:32 -05:00
|
|
|
inlined_vid_def_id == v.did ||
|
2015-08-02 14:52:50 -05:00
|
|
|
ccx.external().borrow().get(&v.did) == Some(&Some(inlined_vid))
|
|
|
|
}).unwrap_or_else(|| {
|
2016-03-28 18:46:02 -05:00
|
|
|
bug!("no variant for {:?}::{}", adt_def, inlined_vid)
|
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.)
|
|
|
|
|
|
|
|
pub fn build_unchecked_lshift<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
lhs: ValueRef,
|
|
|
|
rhs: ValueRef,
|
|
|
|
binop_debug_loc: DebugLoc) -> ValueRef {
|
|
|
|
let rhs = base::cast_shift_expr_rhs(bcx, hir::BinOp_::BiShl, lhs, rhs);
|
|
|
|
// #1877, #10183: Ensure that input is always valid
|
|
|
|
let rhs = shift_mask_rhs(bcx, rhs, binop_debug_loc);
|
|
|
|
build::Shl(bcx, lhs, rhs, binop_debug_loc)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_unchecked_rshift<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
lhs_t: Ty<'tcx>,
|
|
|
|
lhs: ValueRef,
|
|
|
|
rhs: ValueRef,
|
|
|
|
binop_debug_loc: DebugLoc) -> ValueRef {
|
|
|
|
let rhs = base::cast_shift_expr_rhs(bcx, hir::BinOp_::BiShr, lhs, rhs);
|
|
|
|
// #1877, #10183: Ensure that input is always valid
|
|
|
|
let rhs = shift_mask_rhs(bcx, rhs, binop_debug_loc);
|
|
|
|
let is_signed = lhs_t.is_signed();
|
|
|
|
if is_signed {
|
|
|
|
build::AShr(bcx, lhs, rhs, binop_debug_loc)
|
|
|
|
} else {
|
|
|
|
build::LShr(bcx, lhs, rhs, binop_debug_loc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shift_mask_rhs<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
rhs: ValueRef,
|
|
|
|
debug_loc: DebugLoc) -> ValueRef {
|
|
|
|
let rhs_llty = val_ty(rhs);
|
|
|
|
build::And(bcx, rhs, shift_mask_val(bcx, rhs_llty, rhs_llty, false), debug_loc)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shift_mask_val<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
llty: Type,
|
|
|
|
mask_llty: Type,
|
|
|
|
invert: bool) -> ValueRef {
|
|
|
|
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 {
|
|
|
|
C_integral(mask_llty, !val, true)
|
|
|
|
} else {
|
|
|
|
C_integral(mask_llty, val, false)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
TypeKind::Vector => {
|
|
|
|
let mask = shift_mask_val(bcx, llty.element_type(), mask_llty.element_type(), invert);
|
|
|
|
build::VectorSplat(bcx, mask_llty.vector_length(), mask)
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
}
|