2015-10-21 17:42:25 -04:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// 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.
|
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
use llvm::{self, LLVMConstInBoundsGEP};
|
2017-09-20 18:17:23 +03:00
|
|
|
use rustc::ty::{self, Ty};
|
2018-11-01 19:03:38 +01:00
|
|
|
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, Size, VariantIdx};
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir;
|
2017-12-01 14:31:47 +02:00
|
|
|
use rustc::mir::tcx::PlaceTy;
|
2017-04-25 14:39:00 +03:00
|
|
|
use base;
|
2018-09-10 17:59:20 +02:00
|
|
|
use builder::{Builder, MemFlags};
|
2018-08-28 17:03:46 +02:00
|
|
|
use common::{CodegenCx, IntPredicate};
|
2017-09-21 20:40:50 +03:00
|
|
|
use type_of::LayoutLlvmExt;
|
2017-01-02 12:13:59 -07:00
|
|
|
use value::Value;
|
|
|
|
|
use glue;
|
2018-07-22 01:01:07 +02:00
|
|
|
use mir::constant::const_alloc_to_llvm;
|
2015-11-10 22:05:11 +02:00
|
|
|
|
2018-09-07 15:39:39 -07:00
|
|
|
use interfaces::{
|
|
|
|
|
BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods, DerivedIntrinsicMethods,
|
2018-09-10 16:28:47 +02:00
|
|
|
StaticMethods,
|
2018-09-07 15:39:39 -07:00
|
|
|
};
|
2018-08-07 17:14:40 +02:00
|
|
|
|
2018-01-05 07:34:28 +02:00
|
|
|
use super::{FunctionCx, LocalRef};
|
2017-06-25 12:41:24 +03:00
|
|
|
use super::operand::{OperandRef, OperandValue};
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-03-11 12:54:59 +02:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-08-02 17:48:44 +03:00
|
|
|
pub struct PlaceRef<'tcx, V> {
|
2017-12-01 14:39:51 +02:00
|
|
|
/// Pointer to the contents of the place
|
2018-08-02 17:48:44 +03:00
|
|
|
pub llval: V,
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
/// This place's extra data if it is unsized, or null
|
2018-08-02 17:48:44 +03:00
|
|
|
pub llextra: Option<V>,
|
2015-11-10 22:05:11 +02:00
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
/// Monomorphized type of this place, including variant information
|
2017-09-21 20:40:50 +03:00
|
|
|
pub layout: TyLayout<'tcx>,
|
2017-02-06 17:27:09 +01:00
|
|
|
|
2017-12-01 19:16:39 +02:00
|
|
|
/// What alignment we know for this place
|
|
|
|
|
pub align: Align,
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
2018-08-02 17:48:44 +03:00
|
|
|
impl PlaceRef<'tcx, &'ll Value> {
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn new_sized(
|
|
|
|
|
llval: &'ll Value,
|
|
|
|
|
layout: TyLayout<'tcx>,
|
|
|
|
|
align: Align,
|
2018-08-02 17:48:44 +03:00
|
|
|
) -> PlaceRef<'tcx, &'ll Value> {
|
2018-05-29 00:12:55 +09:00
|
|
|
assert!(!layout.is_unsized());
|
2017-12-01 14:31:47 +02:00
|
|
|
PlaceRef {
|
2017-09-20 18:17:23 +03:00
|
|
|
llval,
|
2018-07-10 13:28:39 +03:00
|
|
|
llextra: None,
|
2017-09-20 18:17:23 +03:00
|
|
|
layout,
|
2017-12-01 19:16:39 +02:00
|
|
|
align
|
2017-09-20 18:17:23 +03:00
|
|
|
}
|
2017-01-01 15:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-22 01:01:07 +02:00
|
|
|
pub fn from_const_alloc(
|
2018-07-02 17:52:53 +03:00
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
2018-07-22 01:01:07 +02:00
|
|
|
layout: TyLayout<'tcx>,
|
|
|
|
|
alloc: &mir::interpret::Allocation,
|
|
|
|
|
offset: Size,
|
2018-08-02 17:48:44 +03:00
|
|
|
) -> PlaceRef<'tcx, &'ll Value> {
|
2018-08-28 17:50:57 +02:00
|
|
|
let init = const_alloc_to_llvm(bx.cx(), alloc);
|
2018-09-10 16:28:47 +02:00
|
|
|
let base_addr = bx.cx().static_addr_of(init, layout.align, None);
|
2018-07-22 01:01:07 +02:00
|
|
|
|
|
|
|
|
let llval = unsafe { LLVMConstInBoundsGEP(
|
2018-09-10 16:28:47 +02:00
|
|
|
bx.cx().static_bitcast(base_addr, bx.cx().type_i8p()),
|
2018-09-06 11:57:42 -07:00
|
|
|
&bx.cx().const_usize(offset.bytes()),
|
2018-07-22 01:01:07 +02:00
|
|
|
1,
|
|
|
|
|
)};
|
2018-09-10 16:28:47 +02:00
|
|
|
let llval = bx.cx().static_bitcast(llval, bx.cx().type_ptr_to(layout.llvm_type(bx.cx())));
|
2018-07-22 01:01:07 +02:00
|
|
|
PlaceRef::new_sized(llval, layout, alloc.align)
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-02 17:52:53 +03:00
|
|
|
pub fn alloca(bx: &Builder<'a, 'll, 'tcx>, layout: TyLayout<'tcx>, name: &str)
|
2018-08-02 17:48:44 +03:00
|
|
|
-> PlaceRef<'tcx, &'ll Value> {
|
2017-09-20 18:17:23 +03:00
|
|
|
debug!("alloca({:?}: {:?})", name, layout);
|
2018-05-29 00:12:55 +09:00
|
|
|
assert!(!layout.is_unsized(), "tried to statically allocate unsized place");
|
2018-08-28 17:50:57 +02:00
|
|
|
let tmp = bx.alloca(layout.llvm_type(bx.cx()), name, layout.align);
|
2017-12-01 19:16:39 +02:00
|
|
|
Self::new_sized(tmp, layout, layout.align)
|
2017-02-06 17:27:09 +01:00
|
|
|
}
|
|
|
|
|
|
2018-05-29 00:12:55 +09:00
|
|
|
/// Returns a place for an indirect reference to an unsized place.
|
2018-08-02 17:48:44 +03:00
|
|
|
pub fn alloca_unsized_indirect(
|
|
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
|
|
|
|
layout: TyLayout<'tcx>,
|
|
|
|
|
name: &str,
|
|
|
|
|
) -> PlaceRef<'tcx, &'ll Value> {
|
2018-05-29 00:12:55 +09:00
|
|
|
debug!("alloca_unsized_indirect({:?}: {:?})", name, layout);
|
|
|
|
|
assert!(layout.is_unsized(), "tried to allocate indirect place for sized values");
|
2018-08-28 17:50:57 +02:00
|
|
|
let ptr_ty = bx.cx().tcx.mk_mut_ptr(layout.ty);
|
|
|
|
|
let ptr_layout = bx.cx().layout_of(ptr_ty);
|
2018-05-29 00:12:55 +09:00
|
|
|
Self::alloca(bx, ptr_layout, name)
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn len(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Value {
|
2017-09-21 20:40:50 +03:00
|
|
|
if let layout::FieldPlacement::Array { count, .. } = self.layout.fields {
|
2017-09-20 18:17:23 +03:00
|
|
|
if self.layout.is_unsized() {
|
|
|
|
|
assert_eq!(count, 0);
|
2018-07-10 13:28:39 +03:00
|
|
|
self.llextra.unwrap()
|
2017-09-20 18:17:23 +03:00
|
|
|
} else {
|
2018-09-06 11:57:42 -07:00
|
|
|
cx.const_usize(count)
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
} else {
|
2017-12-01 14:31:47 +02:00
|
|
|
bug!("unexpected layout `{:#?}` in PlaceRef::len", self.layout)
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
}
|
2017-01-01 15:50:15 -07:00
|
|
|
|
2018-08-02 17:48:44 +03:00
|
|
|
pub fn load(&self, bx: &Builder<'a, 'll, 'tcx>) -> OperandRef<'tcx, &'ll Value> {
|
2017-12-01 14:31:47 +02:00
|
|
|
debug!("PlaceRef::load: {:?}", self);
|
2017-06-25 12:41:24 +03:00
|
|
|
|
2018-05-29 00:12:55 +09:00
|
|
|
assert_eq!(self.llextra.is_some(), self.layout.is_unsized());
|
2017-06-25 12:41:24 +03:00
|
|
|
|
2017-09-20 18:17:23 +03:00
|
|
|
if self.layout.is_zst() {
|
2018-08-28 17:50:57 +02:00
|
|
|
return OperandRef::new_zst(bx.cx(), self.layout);
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
|
|
|
|
|
2017-10-09 19:56:41 +03:00
|
|
|
let scalar_load_metadata = |load, scalar: &layout::Scalar| {
|
2018-04-22 18:40:54 +02:00
|
|
|
let vr = scalar.valid_range.clone();
|
2017-10-09 19:56:41 +03:00
|
|
|
match scalar.value {
|
2018-04-22 18:40:54 +02:00
|
|
|
layout::Int(..) => {
|
2018-08-28 17:50:57 +02:00
|
|
|
let range = scalar.valid_range_exclusive(bx.cx());
|
2018-04-22 18:40:54 +02:00
|
|
|
if range.start != range.end {
|
|
|
|
|
bx.range_metadata(load, range);
|
|
|
|
|
}
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
2018-04-06 05:21:47 +08:00
|
|
|
layout::Pointer if vr.start() < vr.end() && !vr.contains(&0) => {
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.nonnull_metadata(load);
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-05-29 00:12:55 +09:00
|
|
|
let val = if let Some(llextra) = self.llextra {
|
2018-08-03 23:50:13 +09:00
|
|
|
OperandValue::Ref(self.llval, Some(llextra), self.align)
|
2018-05-29 00:12:55 +09:00
|
|
|
} else if self.layout.is_llvm_immediate() {
|
2018-07-10 13:28:39 +03:00
|
|
|
let mut const_llval = None;
|
2017-06-25 12:41:24 +03:00
|
|
|
unsafe {
|
2018-07-10 13:28:39 +03:00
|
|
|
if let Some(global) = llvm::LLVMIsAGlobalVariable(self.llval) {
|
|
|
|
|
if llvm::LLVMIsGlobalConstant(global) == llvm::True {
|
|
|
|
|
const_llval = llvm::LLVMGetInitializer(global);
|
|
|
|
|
}
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
2018-07-10 13:28:39 +03:00
|
|
|
let llval = const_llval.unwrap_or_else(|| {
|
2018-01-05 07:12:32 +02:00
|
|
|
let load = bx.load(self.llval, self.align);
|
2017-09-26 14:41:06 +03:00
|
|
|
if let layout::Abi::Scalar(ref scalar) = self.layout.abi {
|
2017-10-09 19:56:41 +03:00
|
|
|
scalar_load_metadata(load, scalar);
|
2017-09-23 15:04:37 +03:00
|
|
|
}
|
|
|
|
|
load
|
2018-07-10 13:28:39 +03:00
|
|
|
});
|
2018-01-05 07:12:32 +02:00
|
|
|
OperandValue::Immediate(base::to_immediate(bx, llval, self.layout))
|
2017-10-09 19:56:41 +03:00
|
|
|
} else if let layout::Abi::ScalarPair(ref a, ref b) = self.layout.abi {
|
|
|
|
|
let load = |i, scalar: &layout::Scalar| {
|
Store scalar pair bools as i8 in memory
We represent `bool` as `i1` in a `ScalarPair`, unlike other aggregates,
to optimize IR for checked operators and the like. With this patch, we
still do so when the pair is an immediate value, but we use the `i8`
memory type when the value is loaded or stored as an LLVM aggregate.
So `(bool, bool)` looks like an `{ i1, i1 }` immediate, but `{ i8, i8 }`
in memory. When a pair is a direct function argument, `PassMode::Pair`,
it is still passed using the immediate `i1` type, but as a return value
it will use the `i8` memory type. Also, `bool`-like` enum tags will now
use scalar pairs when possible, where they were previously excluded due
to optimization issues.
2018-06-15 15:47:54 -07:00
|
|
|
let llptr = bx.struct_gep(self.llval, i as u64);
|
2018-01-05 07:12:32 +02:00
|
|
|
let load = bx.load(llptr, self.align);
|
2017-10-09 19:56:41 +03:00
|
|
|
scalar_load_metadata(load, scalar);
|
|
|
|
|
if scalar.is_bool() {
|
2018-09-06 13:52:15 -07:00
|
|
|
bx.trunc(load, bx.cx().type_i1())
|
2017-10-09 19:56:41 +03:00
|
|
|
} else {
|
|
|
|
|
load
|
|
|
|
|
}
|
2017-10-06 10:25:35 +03:00
|
|
|
};
|
2017-10-09 19:56:41 +03:00
|
|
|
OperandValue::Pair(load(0, a), load(1, b))
|
2017-06-25 12:41:24 +03:00
|
|
|
} else {
|
2018-08-03 23:50:13 +09:00
|
|
|
OperandValue::Ref(self.llval, None, self.align)
|
2017-06-25 12:41:24 +03:00
|
|
|
};
|
|
|
|
|
|
2017-09-20 18:17:23 +03:00
|
|
|
OperandRef { val, layout: self.layout }
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
|
|
|
|
|
2017-04-25 14:39:00 +03:00
|
|
|
/// Access a field, at a point when the value's case is known.
|
2018-08-02 17:48:44 +03:00
|
|
|
pub fn project_field(
|
|
|
|
|
self,
|
|
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
|
|
|
|
ix: usize,
|
|
|
|
|
) -> PlaceRef<'tcx, &'ll Value> {
|
2018-08-28 17:50:57 +02:00
|
|
|
let cx = bx.cx();
|
2018-01-05 07:04:08 +02:00
|
|
|
let field = self.layout.field(cx, ix);
|
2017-10-06 10:25:35 +03:00
|
|
|
let offset = self.layout.fields.offset(ix);
|
2018-09-24 23:09:44 -04:00
|
|
|
let effective_field_align = self.align.restrict_for_offset(offset);
|
2017-06-25 12:41:24 +03:00
|
|
|
|
|
|
|
|
let simple = || {
|
2017-10-06 10:25:35 +03:00
|
|
|
// Unions and newtypes only use an offset of 0.
|
|
|
|
|
let llval = if offset.bytes() == 0 {
|
|
|
|
|
self.llval
|
|
|
|
|
} else if let layout::Abi::ScalarPair(ref a, ref b) = self.layout.abi {
|
|
|
|
|
// Offsets have to match either first or second field.
|
2018-01-05 07:04:08 +02:00
|
|
|
assert_eq!(offset, a.value.size(cx).abi_align(b.value.align(cx)));
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.struct_gep(self.llval, 1)
|
2017-10-06 10:25:35 +03:00
|
|
|
} else {
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.struct_gep(self.llval, self.layout.llvm_field_index(ix))
|
2017-10-06 10:25:35 +03:00
|
|
|
};
|
2017-12-01 14:31:47 +02:00
|
|
|
PlaceRef {
|
2017-10-06 10:25:35 +03:00
|
|
|
// HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
|
2018-09-06 13:52:15 -07:00
|
|
|
llval: bx.pointercast(llval, cx.type_ptr_to(field.llvm_type(cx))),
|
2018-01-05 07:04:08 +02:00
|
|
|
llextra: if cx.type_has_metadata(field.ty) {
|
2017-06-25 12:41:24 +03:00
|
|
|
self.llextra
|
2017-09-14 00:02:53 +03:00
|
|
|
} else {
|
2018-07-10 13:28:39 +03:00
|
|
|
None
|
2017-06-25 12:41:24 +03:00
|
|
|
},
|
2017-09-20 18:17:23 +03:00
|
|
|
layout: field,
|
2018-09-06 18:42:15 +03:00
|
|
|
align: effective_field_align,
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
|
|
|
|
};
|
2017-02-06 17:27:09 +01:00
|
|
|
|
2017-12-01 18:29:35 +02:00
|
|
|
// Simple cases, which don't need DST adjustment:
|
|
|
|
|
// * no metadata available - just log the case
|
|
|
|
|
// * known alignment - sized types, [T], str or a foreign type
|
|
|
|
|
// * packed struct - there is no alignment padding
|
2017-09-14 00:02:53 +03:00
|
|
|
match field.ty.sty {
|
2018-07-10 13:28:39 +03:00
|
|
|
_ if self.llextra.is_none() => {
|
2017-12-01 18:29:35 +02:00
|
|
|
debug!("Unsized field `{}`, of `{:?}` has no metadata for adjustment",
|
2018-07-10 13:28:39 +03:00
|
|
|
ix, self.llval);
|
2017-12-01 18:29:35 +02:00
|
|
|
return simple();
|
|
|
|
|
}
|
|
|
|
|
_ if !field.is_unsized() => return simple(),
|
2018-08-22 01:35:55 +01:00
|
|
|
ty::Slice(..) | ty::Str | ty::Foreign(..) => return simple(),
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Adt(def, _) => {
|
2017-12-01 18:29:35 +02:00
|
|
|
if def.repr.packed() {
|
|
|
|
|
// FIXME(eddyb) generalize the adjustment when we
|
|
|
|
|
// start supporting packing to larger alignments.
|
|
|
|
|
assert_eq!(self.layout.align.abi(), 1);
|
|
|
|
|
return simple();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
2017-01-02 12:13:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We need to get the pointer manually now.
|
|
|
|
|
// We do this by casting to a *i8, then offsetting it by the appropriate amount.
|
|
|
|
|
// We do this instead of, say, simply adjusting the pointer from the result of a GEP
|
|
|
|
|
// because the field may have an arbitrary alignment in the LLVM representation
|
|
|
|
|
// anyway.
|
|
|
|
|
//
|
|
|
|
|
// To demonstrate:
|
|
|
|
|
// struct Foo<T: ?Sized> {
|
|
|
|
|
// x: u16,
|
|
|
|
|
// y: T
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// The type Foo<Foo<Trait>> is represented in LLVM as { u16, { u16, u8 }}, meaning that
|
|
|
|
|
// the `y` field has 16-bit alignment.
|
|
|
|
|
|
|
|
|
|
let meta = self.llextra;
|
|
|
|
|
|
2018-09-06 11:57:42 -07:00
|
|
|
let unaligned_offset = cx.const_usize(offset.bytes());
|
2017-01-02 12:13:59 -07:00
|
|
|
|
|
|
|
|
// Get the alignment of the field
|
2018-01-05 07:12:32 +02:00
|
|
|
let (_, unsized_align) = glue::size_and_align_of_dst(bx, field.ty, meta);
|
2017-01-02 12:13:59 -07:00
|
|
|
|
|
|
|
|
// Bump the unaligned offset up to the appropriate alignment using the
|
|
|
|
|
// following expression:
|
|
|
|
|
//
|
|
|
|
|
// (unaligned offset + (align - 1)) & -align
|
|
|
|
|
|
|
|
|
|
// Calculate offset
|
2018-09-06 11:57:42 -07:00
|
|
|
let align_sub_1 = bx.sub(unsized_align, cx.const_usize(1u64));
|
2018-01-05 07:12:32 +02:00
|
|
|
let offset = bx.and(bx.add(unaligned_offset, align_sub_1),
|
|
|
|
|
bx.neg(unsized_align));
|
2017-01-02 12:13:59 -07:00
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
debug!("struct_field_ptr: DST field offset: {:?}", offset);
|
2017-01-02 12:13:59 -07:00
|
|
|
|
|
|
|
|
// Cast and adjust pointer
|
2018-09-06 13:52:15 -07:00
|
|
|
let byte_ptr = bx.pointercast(self.llval, cx.type_i8p());
|
2018-01-05 07:12:32 +02:00
|
|
|
let byte_ptr = bx.gep(byte_ptr, &[offset]);
|
2017-01-02 12:13:59 -07:00
|
|
|
|
|
|
|
|
// Finally, cast back to the type expected
|
2018-01-05 07:04:08 +02:00
|
|
|
let ll_fty = field.llvm_type(cx);
|
2017-01-02 12:13:59 -07:00
|
|
|
debug!("struct_field_ptr: Field type is {:?}", ll_fty);
|
2017-06-25 12:41:24 +03:00
|
|
|
|
2017-12-01 14:31:47 +02:00
|
|
|
PlaceRef {
|
2018-09-06 13:52:15 -07:00
|
|
|
llval: bx.pointercast(byte_ptr, bx.cx().type_ptr_to(ll_fty)),
|
2017-06-25 12:41:24 +03:00
|
|
|
llextra: self.llextra,
|
2017-09-20 18:17:23 +03:00
|
|
|
layout: field,
|
2018-09-06 18:42:15 +03:00
|
|
|
align: effective_field_align,
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
2017-01-02 12:13:59 -07:00
|
|
|
}
|
|
|
|
|
|
2017-04-25 14:39:00 +03:00
|
|
|
/// Obtain the actual discriminant of a value.
|
2018-08-07 17:14:40 +02:00
|
|
|
pub fn codegen_get_discr(
|
|
|
|
|
self,
|
2018-08-22 17:48:32 +02:00
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
2018-08-07 17:14:40 +02:00
|
|
|
cast_to: Ty<'tcx>
|
|
|
|
|
) -> &'ll Value {
|
2018-08-28 17:50:57 +02:00
|
|
|
let cast_to = bx.cx().layout_of(cast_to).immediate_llvm_type(bx.cx());
|
2018-08-23 16:34:38 +02:00
|
|
|
if self.layout.abi.is_uninhabited() {
|
2018-09-06 11:57:42 -07:00
|
|
|
return bx.cx().const_undef(cast_to);
|
2018-04-11 17:25:18 +02:00
|
|
|
}
|
2017-09-23 01:54:45 +03:00
|
|
|
match self.layout.variants {
|
|
|
|
|
layout::Variants::Single { index } => {
|
2018-03-30 15:49:56 +02:00
|
|
|
let discr_val = self.layout.ty.ty_adt_def().map_or(
|
2018-11-01 19:03:38 +01:00
|
|
|
index.as_u32() as u128,
|
2018-08-28 17:50:57 +02:00
|
|
|
|def| def.discriminant_for_variant(bx.cx().tcx, index).val);
|
2018-09-06 11:57:42 -07:00
|
|
|
return bx.cx().const_uint_big(cast_to, discr_val);
|
2017-09-23 01:54:45 +03:00
|
|
|
}
|
|
|
|
|
layout::Variants::Tagged { .. } |
|
|
|
|
|
layout::Variants::NicheFilling { .. } => {},
|
2017-09-16 16:40:29 +03:00
|
|
|
}
|
|
|
|
|
|
2018-01-05 07:12:32 +02:00
|
|
|
let discr = self.project_field(bx, 0);
|
|
|
|
|
let lldiscr = discr.load(bx).immediate();
|
2017-09-23 01:54:45 +03:00
|
|
|
match self.layout.variants {
|
|
|
|
|
layout::Variants::Single { .. } => bug!(),
|
2018-05-04 22:32:08 +12:00
|
|
|
layout::Variants::Tagged { ref tag, .. } => {
|
|
|
|
|
let signed = match tag.value {
|
2018-06-16 14:19:05 +03:00
|
|
|
// We use `i1` for bytes that are always `0` or `1`,
|
|
|
|
|
// e.g. `#[repr(i8)] enum E { A, B }`, but we can't
|
|
|
|
|
// let LLVM interpret the `i1` as signed, because
|
|
|
|
|
// then `i1 1` (i.e. E::B) is effectively `i8 -1`.
|
|
|
|
|
layout::Int(_, signed) => !tag.is_bool() && signed,
|
2017-09-16 16:40:29 +03:00
|
|
|
_ => false
|
|
|
|
|
};
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.intcast(lldiscr, cast_to, signed)
|
2017-01-02 12:13:59 -07:00
|
|
|
}
|
2017-09-26 21:34:10 +03:00
|
|
|
layout::Variants::NicheFilling {
|
|
|
|
|
dataful_variant,
|
2017-10-12 03:55:49 +03:00
|
|
|
ref niche_variants,
|
|
|
|
|
niche_start,
|
2017-09-26 21:34:10 +03:00
|
|
|
..
|
|
|
|
|
} => {
|
2018-08-28 17:50:57 +02:00
|
|
|
let niche_llty = discr.layout.immediate_llvm_type(bx.cx());
|
2018-04-06 05:21:47 +08:00
|
|
|
if niche_variants.start() == niche_variants.end() {
|
2017-10-12 03:55:49 +03:00
|
|
|
// FIXME(eddyb) Check the actual primitive type here.
|
|
|
|
|
let niche_llval = if niche_start == 0 {
|
2018-08-28 17:03:46 +02:00
|
|
|
// HACK(eddyb) Using `c_null` as it works on all types.
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.cx().const_null(niche_llty)
|
2017-10-12 03:55:49 +03:00
|
|
|
} else {
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.cx().const_uint_big(niche_llty, niche_start)
|
2017-10-12 03:55:49 +03:00
|
|
|
};
|
2018-08-20 18:16:51 +02:00
|
|
|
bx.select(bx.icmp(IntPredicate::IntEQ, lldiscr, niche_llval),
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.cx().const_uint(cast_to, niche_variants.start().as_u32() as u64),
|
|
|
|
|
bx.cx().const_uint(cast_to, dataful_variant.as_u32() as u64))
|
2017-09-24 12:12:26 +03:00
|
|
|
} else {
|
2017-10-12 03:55:49 +03:00
|
|
|
// Rebase from niche values to discriminant values.
|
2018-11-01 19:03:38 +01:00
|
|
|
let delta = niche_start.wrapping_sub(niche_variants.start().as_u32() as u128);
|
2018-09-06 11:57:42 -07:00
|
|
|
let lldiscr = bx.sub(lldiscr, bx.cx().const_uint_big(niche_llty, delta));
|
2018-08-28 17:03:46 +02:00
|
|
|
let lldiscr_max =
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.cx().const_uint(niche_llty, niche_variants.end().as_u32() as u64);
|
2018-08-20 18:16:51 +02:00
|
|
|
bx.select(bx.icmp(IntPredicate::IntULE, lldiscr, lldiscr_max),
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.intcast(lldiscr, cast_to, false),
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.cx().const_uint(cast_to, dataful_variant.as_u32() as u64))
|
2017-10-12 03:55:49 +03:00
|
|
|
}
|
2017-09-16 16:40:29 +03:00
|
|
|
}
|
|
|
|
|
}
|
2017-04-25 14:39:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set the discriminant for a new value of the given case of the given
|
|
|
|
|
/// representation.
|
2018-11-01 19:03:38 +01:00
|
|
|
pub fn codegen_set_discr(&self, bx: &Builder<'a, 'll, 'tcx>, variant_index: VariantIdx) {
|
2018-08-28 17:50:57 +02:00
|
|
|
if self.layout.for_variant(bx.cx(), variant_index).abi.is_uninhabited() {
|
2017-12-09 20:53:10 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
match self.layout.variants {
|
2017-09-23 01:54:45 +03:00
|
|
|
layout::Variants::Single { index } => {
|
2017-12-05 22:37:51 +01:00
|
|
|
assert_eq!(index, variant_index);
|
2017-09-23 01:54:45 +03:00
|
|
|
}
|
|
|
|
|
layout::Variants::Tagged { .. } => {
|
2018-01-05 07:12:32 +02:00
|
|
|
let ptr = self.project_field(bx, 0);
|
2017-09-24 12:12:26 +03:00
|
|
|
let to = self.layout.ty.ty_adt_def().unwrap()
|
2018-01-05 07:12:32 +02:00
|
|
|
.discriminant_for_variant(bx.tcx(), variant_index)
|
2018-04-11 17:30:49 +02:00
|
|
|
.val;
|
|
|
|
|
bx.store(
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.cx().const_uint_big(ptr.layout.llvm_type(bx.cx()), to),
|
2018-04-11 17:30:49 +02:00
|
|
|
ptr.llval,
|
|
|
|
|
ptr.align);
|
2017-01-02 12:13:59 -07:00
|
|
|
}
|
2017-10-12 03:55:49 +03:00
|
|
|
layout::Variants::NicheFilling {
|
|
|
|
|
dataful_variant,
|
|
|
|
|
ref niche_variants,
|
|
|
|
|
niche_start,
|
|
|
|
|
..
|
|
|
|
|
} => {
|
2017-09-24 12:12:26 +03:00
|
|
|
if variant_index != dataful_variant {
|
2018-01-05 07:12:32 +02:00
|
|
|
if bx.sess().target.target.arch == "arm" ||
|
|
|
|
|
bx.sess().target.target.arch == "aarch64" {
|
2017-09-24 12:12:26 +03:00
|
|
|
// Issue #34427: As workaround for LLVM bug on ARM,
|
|
|
|
|
// use memset of 0 before assigning niche value.
|
2018-09-06 11:57:42 -07:00
|
|
|
let fill_byte = bx.cx().const_u8(0);
|
2017-09-22 22:44:40 +03:00
|
|
|
let (size, align) = self.layout.size_and_align();
|
2018-09-06 11:57:42 -07:00
|
|
|
let size = bx.cx().const_usize(size.bytes());
|
2018-09-10 17:59:20 +02:00
|
|
|
bx.memset(self.llval, fill_byte, size, align, MemFlags::empty());
|
2017-04-25 14:39:00 +03:00
|
|
|
}
|
2017-09-24 12:12:26 +03:00
|
|
|
|
2018-01-05 07:12:32 +02:00
|
|
|
let niche = self.project_field(bx, 0);
|
2018-08-28 17:50:57 +02:00
|
|
|
let niche_llty = niche.layout.immediate_llvm_type(bx.cx());
|
2018-11-01 19:03:38 +01:00
|
|
|
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
|
|
|
|
|
let niche_value = (niche_value as u128)
|
2017-10-12 03:55:49 +03:00
|
|
|
.wrapping_add(niche_start);
|
2017-09-24 12:12:26 +03:00
|
|
|
// FIXME(eddyb) Check the actual primitive type here.
|
|
|
|
|
let niche_llval = if niche_value == 0 {
|
2018-08-28 17:03:46 +02:00
|
|
|
// HACK(eddyb) Using `c_null` as it works on all types.
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.cx().const_null(niche_llty)
|
2017-09-24 12:12:26 +03:00
|
|
|
} else {
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.cx().const_uint_big(niche_llty, niche_value)
|
2017-09-24 12:12:26 +03:00
|
|
|
};
|
2018-01-05 07:12:32 +02:00
|
|
|
OperandValue::Immediate(niche_llval).store(bx, niche);
|
2017-04-25 14:39:00 +03:00
|
|
|
}
|
2017-01-02 12:13:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-14 01:08:21 +02:00
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn project_index(&self, bx: &Builder<'a, 'll, 'tcx>, llindex: &'ll Value)
|
2018-08-02 17:48:44 +03:00
|
|
|
-> PlaceRef<'tcx, &'ll Value> {
|
2017-12-01 14:31:47 +02:00
|
|
|
PlaceRef {
|
2018-09-06 11:57:42 -07:00
|
|
|
llval: bx.inbounds_gep(self.llval, &[bx.cx().const_usize(0), llindex]),
|
2018-07-10 13:28:39 +03:00
|
|
|
llextra: None,
|
2018-08-28 17:50:57 +02:00
|
|
|
layout: self.layout.field(bx.cx(), 0),
|
2017-12-01 19:16:39 +02:00
|
|
|
align: self.align
|
2017-09-20 18:17:23 +03:00
|
|
|
}
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
|
|
|
|
|
2018-11-01 19:03:38 +01:00
|
|
|
pub fn project_downcast(&self, bx: &Builder<'a, 'll, 'tcx>, variant_index: VariantIdx)
|
2018-08-02 17:48:44 +03:00
|
|
|
-> PlaceRef<'tcx, &'ll Value> {
|
2017-09-20 18:17:23 +03:00
|
|
|
let mut downcast = *self;
|
2018-08-28 17:50:57 +02:00
|
|
|
downcast.layout = self.layout.for_variant(bx.cx(), variant_index);
|
2017-06-25 12:41:24 +03:00
|
|
|
|
2017-09-21 20:40:50 +03:00
|
|
|
// Cast to the appropriate variant struct type.
|
2018-08-28 17:50:57 +02:00
|
|
|
let variant_ty = downcast.layout.llvm_type(bx.cx());
|
2018-09-06 13:52:15 -07:00
|
|
|
downcast.llval = bx.pointercast(downcast.llval, bx.cx().type_ptr_to(variant_ty));
|
2017-09-20 18:17:23 +03:00
|
|
|
|
|
|
|
|
downcast
|
2017-03-14 01:08:21 +02:00
|
|
|
}
|
2017-06-01 21:50:53 +03:00
|
|
|
|
2018-07-02 17:52:53 +03:00
|
|
|
pub fn storage_live(&self, bx: &Builder<'a, 'll, 'tcx>) {
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.lifetime_start(self.llval, self.layout.size);
|
2017-06-01 21:50:53 +03:00
|
|
|
}
|
|
|
|
|
|
2018-07-02 17:52:53 +03:00
|
|
|
pub fn storage_dead(&self, bx: &Builder<'a, 'll, 'tcx>) {
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.lifetime_end(self.llval, self.layout.size);
|
2017-06-01 21:50:53 +03:00
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
2018-08-03 14:20:10 +02:00
|
|
|
impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
|
2018-05-08 16:10:16 +03:00
|
|
|
pub fn codegen_place(&mut self,
|
2018-07-02 17:52:53 +03:00
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
2017-12-01 14:39:51 +02:00
|
|
|
place: &mir::Place<'tcx>)
|
2018-08-02 17:48:44 +03:00
|
|
|
-> PlaceRef<'tcx, &'ll Value> {
|
2018-05-08 16:10:16 +03:00
|
|
|
debug!("codegen_place(place={:?})", place);
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2018-08-28 17:50:57 +02:00
|
|
|
let cx = bx.cx();
|
2018-01-05 07:04:08 +02:00
|
|
|
let tcx = cx.tcx;
|
2016-06-20 23:55:14 +03:00
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
if let mir::Place::Local(index) = *place {
|
2016-06-20 23:55:14 +03:00
|
|
|
match self.locals[index] {
|
2017-12-01 14:39:51 +02:00
|
|
|
LocalRef::Place(place) => {
|
|
|
|
|
return place;
|
2016-06-20 23:55:14 +03:00
|
|
|
}
|
2018-05-29 00:12:55 +09:00
|
|
|
LocalRef::UnsizedPlace(place) => {
|
|
|
|
|
return place.load(bx).deref(&cx);
|
|
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Operand(..) => {
|
2017-12-01 14:39:51 +02:00
|
|
|
bug!("using operand local {:?} as place", place);
|
2016-06-20 23:55:14 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
let result = match *place {
|
2017-12-01 14:31:47 +02:00
|
|
|
mir::Place::Local(_) => bug!(), // handled above
|
2018-07-22 01:01:07 +02:00
|
|
|
mir::Place::Promoted(box (index, ty)) => {
|
|
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
|
|
|
|
let cid = mir::interpret::GlobalId {
|
|
|
|
|
instance: self.instance,
|
|
|
|
|
promoted: Some(index),
|
|
|
|
|
};
|
|
|
|
|
let layout = cx.layout_of(self.monomorphize(&ty));
|
|
|
|
|
match bx.tcx().const_eval(param_env.and(cid)) {
|
|
|
|
|
Ok(val) => match val.val {
|
2018-08-23 19:04:33 +02:00
|
|
|
mir::interpret::ConstValue::ByRef(_, alloc, offset) => {
|
2018-07-22 01:01:07 +02:00
|
|
|
PlaceRef::from_const_alloc(bx, layout, alloc, offset)
|
|
|
|
|
}
|
|
|
|
|
_ => bug!("promoteds should have an allocation: {:?}", val),
|
|
|
|
|
},
|
|
|
|
|
Err(_) => {
|
|
|
|
|
// this is unreachable as long as runtime
|
|
|
|
|
// and compile-time agree on values
|
|
|
|
|
// With floats that won't always be true
|
|
|
|
|
// so we generate an abort
|
2018-08-28 17:50:57 +02:00
|
|
|
let fnname = bx.cx().get_intrinsic(&("llvm.trap"));
|
2018-07-22 01:01:07 +02:00
|
|
|
bx.call(fnname, &[], None);
|
2018-09-06 13:52:15 -07:00
|
|
|
let llval = bx.cx().const_undef(
|
|
|
|
|
bx.cx().type_ptr_to(layout.llvm_type(bx.cx()))
|
|
|
|
|
);
|
2018-07-22 12:16:12 +02:00
|
|
|
PlaceRef::new_sized(llval, layout, layout.align)
|
2018-07-22 01:01:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-01 14:31:47 +02:00
|
|
|
mir::Place::Static(box mir::Static { def_id, ty }) => {
|
2018-01-05 07:04:08 +02:00
|
|
|
let layout = cx.layout_of(self.monomorphize(&ty));
|
2018-09-10 16:28:47 +02:00
|
|
|
PlaceRef::new_sized(cx.get_static(def_id), layout, layout.align)
|
2015-11-10 23:22:57 +02:00
|
|
|
},
|
2017-12-01 14:31:47 +02:00
|
|
|
mir::Place::Projection(box mir::Projection {
|
2016-06-09 18:14:47 +03:00
|
|
|
ref base,
|
|
|
|
|
elem: mir::ProjectionElem::Deref
|
|
|
|
|
}) => {
|
|
|
|
|
// Load the pointer from its location.
|
2018-08-28 17:50:57 +02:00
|
|
|
self.codegen_consume(bx, base).deref(bx.cx())
|
2016-06-09 18:14:47 +03:00
|
|
|
}
|
2017-12-01 14:31:47 +02:00
|
|
|
mir::Place::Projection(ref projection) => {
|
2018-05-08 16:10:16 +03:00
|
|
|
let cg_base = self.codegen_place(bx, &projection.base);
|
2016-03-08 14:13:56 +02:00
|
|
|
|
2017-06-25 12:41:24 +03:00
|
|
|
match projection.elem {
|
2016-06-09 18:14:47 +03:00
|
|
|
mir::ProjectionElem::Deref => bug!(),
|
2016-02-11 18:31:42 +02:00
|
|
|
mir::ProjectionElem::Field(ref field, _) => {
|
2018-05-08 16:10:16 +03:00
|
|
|
cg_base.project_field(bx, field.index())
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
2017-09-03 21:55:41 +03:00
|
|
|
mir::ProjectionElem::Index(index) => {
|
2017-12-01 14:31:47 +02:00
|
|
|
let index = &mir::Operand::Copy(mir::Place::Local(index));
|
2018-05-08 16:10:16 +03:00
|
|
|
let index = self.codegen_operand(bx, index);
|
2017-06-01 21:50:53 +03:00
|
|
|
let llindex = index.immediate();
|
2018-05-08 16:10:16 +03:00
|
|
|
cg_base.project_index(bx, llindex)
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
mir::ProjectionElem::ConstantIndex { offset,
|
|
|
|
|
from_end: false,
|
|
|
|
|
min_length: _ } => {
|
2018-09-06 11:57:42 -07:00
|
|
|
let lloffset = bx.cx().const_usize(offset as u64);
|
2018-05-08 16:10:16 +03:00
|
|
|
cg_base.project_index(bx, lloffset)
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
mir::ProjectionElem::ConstantIndex { offset,
|
|
|
|
|
from_end: true,
|
|
|
|
|
min_length: _ } => {
|
2018-09-06 11:57:42 -07:00
|
|
|
let lloffset = bx.cx().const_usize(offset as u64);
|
2018-08-28 17:50:57 +02:00
|
|
|
let lllen = cg_base.len(bx.cx());
|
2018-01-05 07:12:32 +02:00
|
|
|
let llindex = bx.sub(lllen, lloffset);
|
2018-05-08 16:10:16 +03:00
|
|
|
cg_base.project_index(bx, llindex)
|
2016-03-11 12:54:59 +02:00
|
|
|
}
|
|
|
|
|
mir::ProjectionElem::Subslice { from, to } => {
|
2018-05-08 16:10:16 +03:00
|
|
|
let mut subslice = cg_base.project_index(bx,
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.cx().const_usize(from as u64));
|
2018-05-08 16:10:16 +03:00
|
|
|
let projected_ty = PlaceTy::Ty { ty: cg_base.layout.ty }
|
2018-10-22 22:50:10 +02:00
|
|
|
.projection_ty(tcx, &projection.elem)
|
|
|
|
|
.to_ty(bx.tcx());
|
2018-08-28 17:50:57 +02:00
|
|
|
subslice.layout = bx.cx().layout_of(self.monomorphize(&projected_ty));
|
2017-09-20 18:17:23 +03:00
|
|
|
|
|
|
|
|
if subslice.layout.is_unsized() {
|
2018-07-10 13:28:39 +03:00
|
|
|
subslice.llextra = Some(bx.sub(cg_base.llextra.unwrap(),
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.cx().const_usize((from as u64) + (to as u64))));
|
2016-03-11 12:54:59 +02:00
|
|
|
}
|
2017-06-25 12:41:24 +03:00
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
// Cast the place pointer type to the new
|
2017-06-26 18:33:50 +03:00
|
|
|
// array or slice type (*[%_; new_len]).
|
2018-01-05 07:12:32 +02:00
|
|
|
subslice.llval = bx.pointercast(subslice.llval,
|
2018-09-06 13:52:15 -07:00
|
|
|
bx.cx().type_ptr_to(subslice.layout.llvm_type(bx.cx())));
|
2017-06-26 18:33:50 +03:00
|
|
|
|
2017-06-25 12:41:24 +03:00
|
|
|
subslice
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
2017-06-25 12:41:24 +03:00
|
|
|
mir::ProjectionElem::Downcast(_, v) => {
|
2018-05-08 16:10:16 +03:00
|
|
|
cg_base.project_downcast(bx, v)
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-11 12:54:59 +02:00
|
|
|
};
|
2018-05-08 16:10:16 +03:00
|
|
|
debug!("codegen_place(place={:?}) => {:?}", place, result);
|
2016-03-11 12:54:59 +02:00
|
|
|
result
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
pub fn monomorphized_place_ty(&self, place: &mir::Place<'tcx>) -> Ty<'tcx> {
|
2018-01-05 07:04:08 +02:00
|
|
|
let tcx = self.cx.tcx;
|
2017-12-01 14:39:51 +02:00
|
|
|
let place_ty = place.ty(self.mir, tcx);
|
|
|
|
|
self.monomorphize(&place_ty.to_ty(tcx))
|
2016-06-05 19:03:30 +03:00
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|