2019-06-11 08:43:22 -05:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
fn codegen_field<'tcx>(
|
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-06-11 08:43:22 -05:00
|
|
|
base: Value,
|
2019-09-02 12:50:21 -05:00
|
|
|
extra: Option<Value>,
|
2019-06-11 08:43:22 -05:00
|
|
|
layout: TyLayout<'tcx>,
|
|
|
|
field: mir::Field,
|
|
|
|
) -> (Value, TyLayout<'tcx>) {
|
|
|
|
let field_offset = layout.fields.offset(field.index());
|
2019-09-02 12:50:21 -05:00
|
|
|
let field_layout = layout.field(&*fx, field.index());
|
|
|
|
|
|
|
|
let simple = |fx: &mut FunctionCx<_>| {
|
|
|
|
if field_offset.bytes() > 0 {
|
|
|
|
(
|
|
|
|
fx.bcx.ins().iadd_imm(base, field_offset.bytes() as i64),
|
|
|
|
field_layout,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(base, field_layout)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(extra) = extra {
|
|
|
|
if !field_layout.is_unsized() {
|
|
|
|
return simple(fx);
|
|
|
|
}
|
2019-09-28 04:13:40 -05:00
|
|
|
match field_layout.ty.kind {
|
2019-09-02 12:50:21 -05:00
|
|
|
ty::Slice(..) | ty::Str | ty::Foreign(..) => return simple(fx),
|
|
|
|
ty::Adt(def, _) if def.repr.packed() => {
|
|
|
|
assert_eq!(layout.align.abi.bytes(), 1);
|
|
|
|
return simple(fx);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// We have to align the offset for DST's
|
|
|
|
let unaligned_offset = field_offset.bytes();
|
|
|
|
let (_, unsized_align) = crate::unsize::size_and_align_of_dst(fx, field_layout.ty, extra);
|
|
|
|
|
|
|
|
let one = fx.bcx.ins().iconst(pointer_ty(fx.tcx), 1);
|
|
|
|
let align_sub_1 = fx.bcx.ins().isub(unsized_align, one);
|
|
|
|
let and_lhs = fx.bcx.ins().iadd_imm(align_sub_1, unaligned_offset as i64);
|
|
|
|
let zero = fx.bcx.ins().iconst(pointer_ty(fx.tcx), 0);
|
|
|
|
let and_rhs = fx.bcx.ins().isub(zero, unsized_align);
|
|
|
|
let offset = fx.bcx.ins().band(and_lhs, and_rhs);
|
|
|
|
|
|
|
|
(
|
|
|
|
fx.bcx.ins().iadd(base, offset),
|
|
|
|
field_layout,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 08:43:22 -05:00
|
|
|
} else {
|
2019-09-02 12:50:21 -05:00
|
|
|
simple(fx)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 08:14:26 -05:00
|
|
|
fn scalar_pair_calculate_b_offset(tcx: TyCtxt<'_>, a_scalar: &Scalar, b_scalar: &Scalar) -> i32 {
|
2019-08-31 12:28:09 -05:00
|
|
|
let b_offset = a_scalar
|
|
|
|
.value
|
|
|
|
.size(&tcx)
|
|
|
|
.align_to(b_scalar.value.align(&tcx).abi);
|
2019-06-23 08:14:26 -05:00
|
|
|
b_offset.bytes().try_into().unwrap()
|
|
|
|
}
|
|
|
|
|
2019-06-11 08:43:22 -05:00
|
|
|
/// A read-only value
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
2019-06-11 09:41:40 -05:00
|
|
|
pub struct CValue<'tcx>(CValueInner, TyLayout<'tcx>);
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
enum CValueInner {
|
|
|
|
ByRef(Value),
|
|
|
|
ByVal(Value),
|
|
|
|
ByValPair(Value, Value),
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> CValue<'tcx> {
|
|
|
|
pub fn by_ref(value: Value, layout: TyLayout<'tcx>) -> CValue<'tcx> {
|
2019-06-11 09:41:40 -05:00
|
|
|
CValue(CValueInner::ByRef(value), layout)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn by_val(value: Value, layout: TyLayout<'tcx>) -> CValue<'tcx> {
|
2019-06-11 09:41:40 -05:00
|
|
|
CValue(CValueInner::ByVal(value), layout)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn by_val_pair(value: Value, extra: Value, layout: TyLayout<'tcx>) -> CValue<'tcx> {
|
2019-06-11 09:41:40 -05:00
|
|
|
CValue(CValueInner::ByValPair(value, extra), layout)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn layout(&self) -> TyLayout<'tcx> {
|
2019-06-11 09:41:40 -05:00
|
|
|
self.1
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn force_stack<'a>(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>) -> Value {
|
2019-06-11 09:41:40 -05:00
|
|
|
let layout = self.1;
|
|
|
|
match self.0 {
|
|
|
|
CValueInner::ByRef(value) => value,
|
2019-06-16 05:51:16 -05:00
|
|
|
CValueInner::ByVal(_) | CValueInner::ByValPair(_, _) => {
|
|
|
|
let cplace = CPlace::new_stack_slot(fx, layout.ty);
|
|
|
|
cplace.write_cvalue(fx, self);
|
|
|
|
cplace.to_addr(fx)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 08:41:33 -05:00
|
|
|
pub fn try_to_addr(self) -> Option<Value> {
|
|
|
|
match self.0 {
|
|
|
|
CValueInner::ByRef(addr) => Some(addr),
|
|
|
|
CValueInner::ByVal(_) | CValueInner::ByValPair(_, _) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 08:43:22 -05:00
|
|
|
/// Load a value with layout.abi of scalar
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn load_scalar<'a>(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>) -> Value {
|
2019-06-11 09:41:40 -05:00
|
|
|
let layout = self.1;
|
|
|
|
match self.0 {
|
|
|
|
CValueInner::ByRef(addr) => {
|
2019-06-11 08:43:22 -05:00
|
|
|
let scalar = match layout.abi {
|
|
|
|
layout::Abi::Scalar(ref scalar) => scalar.clone(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let clif_ty = scalar_to_clif_type(fx.tcx, scalar);
|
2019-07-24 10:23:23 -05:00
|
|
|
fx.bcx.ins().load(clif_ty, MemFlags::new(), addr, 0)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
2019-06-11 09:41:40 -05:00
|
|
|
CValueInner::ByVal(value) => value,
|
|
|
|
CValueInner::ByValPair(_, _) => bug!("Please use load_scalar_pair for ByValPair"),
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load a value pair with layout.abi of scalar pair
|
2019-08-31 12:28:09 -05:00
|
|
|
pub fn load_scalar_pair<'a>(
|
|
|
|
self,
|
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
|
|
|
) -> (Value, Value) {
|
2019-06-11 09:41:40 -05:00
|
|
|
let layout = self.1;
|
|
|
|
match self.0 {
|
|
|
|
CValueInner::ByRef(addr) => {
|
2019-06-23 08:14:26 -05:00
|
|
|
let (a_scalar, b_scalar) = match &layout.abi {
|
|
|
|
layout::Abi::ScalarPair(a, b) => (a, b),
|
2019-09-14 10:53:36 -05:00
|
|
|
_ => unreachable!("load_scalar_pair({:?})", self),
|
2019-06-11 08:43:22 -05:00
|
|
|
};
|
2019-06-23 08:14:26 -05:00
|
|
|
let b_offset = scalar_pair_calculate_b_offset(fx.tcx, a_scalar, b_scalar);
|
|
|
|
let clif_ty1 = scalar_to_clif_type(fx.tcx, a_scalar.clone());
|
|
|
|
let clif_ty2 = scalar_to_clif_type(fx.tcx, b_scalar.clone());
|
2019-07-24 10:23:23 -05:00
|
|
|
let val1 = fx.bcx.ins().load(clif_ty1, MemFlags::new(), addr, 0);
|
2019-08-31 12:28:09 -05:00
|
|
|
let val2 = fx.bcx.ins().load(clif_ty2, MemFlags::new(), addr, b_offset);
|
2019-06-11 08:43:22 -05:00
|
|
|
(val1, val2)
|
|
|
|
}
|
2019-06-11 09:41:40 -05:00
|
|
|
CValueInner::ByVal(_) => bug!("Please use load_scalar for ByVal"),
|
|
|
|
CValueInner::ByValPair(val1, val2) => (val1, val2),
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn value_field<'a>(
|
|
|
|
self,
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-06-11 08:43:22 -05:00
|
|
|
field: mir::Field,
|
2019-08-18 09:52:07 -05:00
|
|
|
) -> CValue<'tcx> {
|
2019-06-11 09:41:40 -05:00
|
|
|
let layout = self.1;
|
|
|
|
let base = match self.0 {
|
|
|
|
CValueInner::ByRef(addr) => addr,
|
2019-06-11 08:43:22 -05:00
|
|
|
_ => bug!("place_field for {:?}", self),
|
|
|
|
};
|
|
|
|
|
2019-09-02 12:50:21 -05:00
|
|
|
let (field_ptr, field_layout) = codegen_field(fx, base, None, layout, field);
|
2019-06-11 09:41:40 -05:00
|
|
|
CValue::by_ref(field_ptr, field_layout)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn unsize_value<'a>(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>, dest: CPlace<'tcx>) {
|
2019-06-11 08:43:22 -05:00
|
|
|
crate::unsize::coerce_unsized_into(fx, self, dest);
|
|
|
|
}
|
|
|
|
|
2019-07-24 10:16:31 -05:00
|
|
|
/// If `ty` is signed, `const_val` must already be sign extended.
|
2019-06-11 08:43:22 -05:00
|
|
|
pub fn const_val<'a>(
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-06-11 08:43:22 -05:00
|
|
|
ty: Ty<'tcx>,
|
2019-07-24 10:16:31 -05:00
|
|
|
const_val: u128,
|
2019-08-18 09:52:07 -05:00
|
|
|
) -> CValue<'tcx> {
|
2019-06-11 08:43:22 -05:00
|
|
|
let clif_ty = fx.clif_type(ty).unwrap();
|
|
|
|
let layout = fx.layout_of(ty);
|
2019-07-24 10:16:31 -05:00
|
|
|
|
2019-09-28 04:13:40 -05:00
|
|
|
let val = match ty.kind {
|
2019-07-24 10:16:31 -05:00
|
|
|
ty::TyKind::Uint(UintTy::U128) | ty::TyKind::Int(IntTy::I128) => {
|
|
|
|
let lsb = fx.bcx.ins().iconst(types::I64, const_val as u64 as i64);
|
2019-08-31 12:28:09 -05:00
|
|
|
let msb = fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.iconst(types::I64, (const_val >> 64) as u64 as i64);
|
2019-07-24 10:16:31 -05:00
|
|
|
fx.bcx.ins().iconcat(lsb, msb)
|
|
|
|
}
|
|
|
|
ty::TyKind::Bool => {
|
2019-08-31 12:28:09 -05:00
|
|
|
assert!(
|
|
|
|
const_val == 0 || const_val == 1,
|
|
|
|
"Invalid bool 0x{:032X}",
|
|
|
|
const_val
|
|
|
|
);
|
2019-07-24 10:16:31 -05:00
|
|
|
fx.bcx.ins().iconst(types::I8, const_val as i64)
|
|
|
|
}
|
2019-08-31 12:28:09 -05:00
|
|
|
ty::TyKind::Uint(_) | ty::TyKind::Ref(..) | ty::TyKind::RawPtr(..) => fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.iconst(clif_ty, u64::try_from(const_val).expect("uint") as i64),
|
|
|
|
ty::TyKind::Int(_) => fx.bcx.ins().iconst(clif_ty, const_val as i128 as i64),
|
|
|
|
_ => panic!(
|
|
|
|
"CValue::const_val for non bool/integer/pointer type {:?} is not allowed",
|
|
|
|
ty
|
|
|
|
),
|
2019-06-12 13:54:38 -05:00
|
|
|
};
|
2019-07-24 10:16:31 -05:00
|
|
|
|
2019-06-12 13:54:38 -05:00
|
|
|
CValue::by_val(val, layout)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
|
2019-06-11 09:41:40 -05:00
|
|
|
CValue(self.0, layout)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A place where you can write a value to or read a value from
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
2019-08-29 04:23:19 -05:00
|
|
|
pub struct CPlace<'tcx> {
|
|
|
|
inner: CPlaceInner,
|
|
|
|
layout: TyLayout<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum CPlaceInner {
|
|
|
|
Var(Local),
|
|
|
|
Addr(Value, Option<Value>),
|
|
|
|
Stack(StackSlot),
|
|
|
|
NoPlace,
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
impl<'tcx> CPlace<'tcx> {
|
2019-06-11 08:43:22 -05:00
|
|
|
pub fn layout(&self) -> TyLayout<'tcx> {
|
2019-08-29 04:23:19 -05:00
|
|
|
self.layout
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inner(&self) -> &CPlaceInner {
|
|
|
|
&self.inner
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn no_place(layout: TyLayout<'tcx>) -> CPlace<'tcx> {
|
2019-08-31 12:28:09 -05:00
|
|
|
CPlace {
|
2019-08-29 04:23:19 -05:00
|
|
|
inner: CPlaceInner::NoPlace,
|
2019-08-31 12:28:09 -05:00
|
|
|
layout,
|
2019-08-29 04:23:19 -05:00
|
|
|
}
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_stack_slot(
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-06-11 08:43:22 -05:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> CPlace<'tcx> {
|
|
|
|
let layout = fx.layout_of(ty);
|
|
|
|
assert!(!layout.is_unsized());
|
|
|
|
if layout.size.bytes() == 0 {
|
2019-08-29 04:23:19 -05:00
|
|
|
return CPlace {
|
|
|
|
inner: CPlaceInner::NoPlace,
|
|
|
|
layout,
|
|
|
|
};
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
|
|
|
|
kind: StackSlotKind::ExplicitSlot,
|
|
|
|
size: layout.size.bytes() as u32,
|
|
|
|
offset: None,
|
|
|
|
});
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlace {
|
|
|
|
inner: CPlaceInner::Stack(stack_slot),
|
|
|
|
layout,
|
|
|
|
}
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_var(
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-06-11 08:43:22 -05:00
|
|
|
local: Local,
|
|
|
|
layout: TyLayout<'tcx>,
|
|
|
|
) -> CPlace<'tcx> {
|
|
|
|
fx.bcx
|
|
|
|
.declare_var(mir_var(local), fx.clif_type(layout.ty).unwrap());
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlace {
|
|
|
|
inner: CPlaceInner::Var(local),
|
|
|
|
layout,
|
|
|
|
}
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 09:30:47 -05:00
|
|
|
pub fn for_addr(addr: Value, layout: TyLayout<'tcx>) -> CPlace<'tcx> {
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlace {
|
|
|
|
inner: CPlaceInner::Addr(addr, None),
|
|
|
|
layout,
|
|
|
|
}
|
2019-06-11 09:30:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn for_addr_with_extra(addr: Value, extra: Value, layout: TyLayout<'tcx>) -> CPlace<'tcx> {
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlace {
|
|
|
|
inner: CPlaceInner::Addr(addr, Some(extra)),
|
|
|
|
layout,
|
|
|
|
}
|
2019-06-11 09:30:47 -05:00
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn to_cvalue(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>) -> CValue<'tcx> {
|
2019-08-29 04:23:19 -05:00
|
|
|
let layout = self.layout();
|
|
|
|
match self.inner {
|
2019-11-09 09:42:21 -06:00
|
|
|
CPlaceInner::Var(var) => {
|
|
|
|
let val = fx.bcx.use_var(mir_var(var));
|
|
|
|
fx.bcx.set_val_label(val, cranelift::codegen::ir::ValueLabel::from_u32(var.as_u32()));
|
|
|
|
CValue::by_val(val, layout)
|
|
|
|
}
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlaceInner::Addr(addr, extra) => {
|
2019-06-11 08:43:22 -05:00
|
|
|
assert!(extra.is_none(), "unsized values are not yet supported");
|
2019-06-11 09:41:40 -05:00
|
|
|
CValue::by_ref(addr, layout)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlaceInner::Stack(stack_slot) => CValue::by_ref(
|
2019-06-11 08:43:22 -05:00
|
|
|
fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
|
|
|
|
layout,
|
|
|
|
),
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlaceInner::NoPlace => CValue::by_ref(
|
2019-06-11 08:43:22 -05:00
|
|
|
fx.bcx
|
|
|
|
.ins()
|
|
|
|
.iconst(fx.pointer_type, fx.pointer_type.bytes() as i64),
|
|
|
|
layout,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn to_addr(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>) -> Value {
|
2019-06-11 08:43:22 -05:00
|
|
|
match self.to_addr_maybe_unsized(fx) {
|
|
|
|
(addr, None) => addr,
|
|
|
|
(_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_addr_maybe_unsized(
|
|
|
|
self,
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-06-11 08:43:22 -05:00
|
|
|
) -> (Value, Option<Value>) {
|
2019-08-29 04:23:19 -05:00
|
|
|
match self.inner {
|
|
|
|
CPlaceInner::Addr(addr, extra) => (addr, extra),
|
|
|
|
CPlaceInner::Stack(stack_slot) => (
|
2019-06-11 08:43:22 -05:00
|
|
|
fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
|
|
|
|
None,
|
|
|
|
),
|
2019-11-24 07:56:51 -06:00
|
|
|
CPlaceInner::NoPlace => {
|
|
|
|
(
|
|
|
|
fx.bcx.ins().iconst(
|
|
|
|
fx.pointer_type,
|
|
|
|
i64::try_from(self.layout.align.pref.bytes()).unwrap(),
|
|
|
|
),
|
|
|
|
None
|
|
|
|
)
|
|
|
|
}
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlaceInner::Var(_) => bug!("Expected CPlace::Addr, found CPlace::Var"),
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn write_cvalue(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>, from: CValue<'tcx>) {
|
2019-06-11 08:43:22 -05:00
|
|
|
use rustc::hir::Mutability::*;
|
|
|
|
|
|
|
|
let from_ty = from.layout().ty;
|
|
|
|
let to_ty = self.layout().ty;
|
|
|
|
|
2019-08-31 12:28:09 -05:00
|
|
|
fn assert_assignable<'tcx>(
|
|
|
|
fx: &FunctionCx<'_, 'tcx, impl Backend>,
|
|
|
|
from_ty: Ty<'tcx>,
|
|
|
|
to_ty: Ty<'tcx>,
|
|
|
|
) {
|
2019-09-28 04:13:40 -05:00
|
|
|
match (&from_ty.kind, &to_ty.kind) {
|
2019-11-14 14:13:40 -06:00
|
|
|
(ty::Ref(_, t, Immutable), ty::Ref(_, u, Immutable))
|
|
|
|
| (ty::Ref(_, t, Mutable), ty::Ref(_, u, Immutable))
|
|
|
|
| (ty::Ref(_, t, Mutable), ty::Ref(_, u, Mutable)) => {
|
2019-06-11 08:43:22 -05:00
|
|
|
assert_assignable(fx, t, u);
|
|
|
|
// &mut T -> &T is allowed
|
|
|
|
// &'a T -> &'b T is allowed
|
|
|
|
}
|
2019-11-14 14:13:40 -06:00
|
|
|
(ty::Ref(_, _, Immutable), ty::Ref(_, _, Mutable)) => panic!(
|
2019-08-31 12:28:09 -05:00
|
|
|
"Cant assign value of type {} to place of type {}",
|
|
|
|
from_ty, to_ty
|
|
|
|
),
|
2019-06-11 08:43:22 -05:00
|
|
|
(ty::FnPtr(_), ty::FnPtr(_)) => {
|
|
|
|
let from_sig = fx.tcx.normalize_erasing_late_bound_regions(
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
&from_ty.fn_sig(fx.tcx),
|
|
|
|
);
|
|
|
|
let to_sig = fx.tcx.normalize_erasing_late_bound_regions(
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
&to_ty.fn_sig(fx.tcx),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
from_sig, to_sig,
|
|
|
|
"Can't write fn ptr with incompatible sig {:?} to place with sig {:?}\n\n{:#?}",
|
|
|
|
from_sig, to_sig, fx,
|
|
|
|
);
|
|
|
|
// fn(&T) -> for<'l> fn(&'l T) is allowed
|
|
|
|
}
|
|
|
|
(ty::Dynamic(from_traits, _), ty::Dynamic(to_traits, _)) => {
|
2019-08-31 12:28:09 -05:00
|
|
|
let from_traits = fx
|
|
|
|
.tcx
|
|
|
|
.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), from_traits);
|
|
|
|
let to_traits = fx
|
|
|
|
.tcx
|
|
|
|
.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to_traits);
|
2019-06-11 08:43:22 -05:00
|
|
|
assert_eq!(
|
|
|
|
from_traits, to_traits,
|
|
|
|
"Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}",
|
|
|
|
from_traits, to_traits, fx,
|
|
|
|
);
|
|
|
|
// dyn for<'r> Trait<'r> -> dyn Trait<'_> is allowed
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
assert_eq!(
|
|
|
|
from_ty,
|
|
|
|
to_ty,
|
|
|
|
"Can't write value with incompatible type {:?} to place with type {:?}\n\n{:#?}",
|
|
|
|
from_ty,
|
|
|
|
to_ty,
|
|
|
|
fx,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_assignable(fx, from_ty, to_ty);
|
|
|
|
|
2019-08-29 04:23:19 -05:00
|
|
|
let dst_layout = self.layout();
|
|
|
|
let addr = match self.inner {
|
|
|
|
CPlaceInner::Var(var) => {
|
2019-06-11 08:43:22 -05:00
|
|
|
let data = from.load_scalar(fx);
|
2019-11-15 14:07:54 -06:00
|
|
|
fx.bcx.set_val_label(data, cranelift::codegen::ir::ValueLabel::from_u32(var.as_u32()));
|
2019-06-11 08:43:22 -05:00
|
|
|
fx.bcx.def_var(mir_var(var), data);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlaceInner::Addr(addr, None) => addr,
|
|
|
|
CPlaceInner::Stack(stack_slot) => {
|
|
|
|
fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0)
|
|
|
|
}
|
|
|
|
CPlaceInner::NoPlace => {
|
|
|
|
if dst_layout.abi != Abi::Uninhabited {
|
|
|
|
assert_eq!(dst_layout.size.bytes(), 0, "{:?}", dst_layout);
|
2019-06-02 09:25:44 -05:00
|
|
|
}
|
2019-06-11 08:43:22 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlaceInner::Addr(_, Some(_)) => bug!("Can't write value to unsized place {:?}", self),
|
2019-06-11 08:43:22 -05:00
|
|
|
};
|
|
|
|
|
2019-06-11 09:41:40 -05:00
|
|
|
match from.0 {
|
|
|
|
CValueInner::ByVal(val) => {
|
2019-07-24 10:23:23 -05:00
|
|
|
fx.bcx.ins().store(MemFlags::new(), val, addr, 0);
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
2019-08-31 12:28:09 -05:00
|
|
|
CValueInner::ByValPair(value, extra) => match dst_layout.abi {
|
|
|
|
Abi::ScalarPair(ref a_scalar, ref b_scalar) => {
|
|
|
|
let b_offset = scalar_pair_calculate_b_offset(fx.tcx, a_scalar, b_scalar);
|
|
|
|
fx.bcx.ins().store(MemFlags::new(), value, addr, 0);
|
|
|
|
fx.bcx.ins().store(MemFlags::new(), extra, addr, b_offset);
|
2019-06-16 05:51:16 -05:00
|
|
|
}
|
2019-08-31 12:28:09 -05:00
|
|
|
_ => bug!(
|
|
|
|
"Non ScalarPair abi {:?} for ByValPair CValue",
|
|
|
|
dst_layout.abi
|
|
|
|
),
|
|
|
|
},
|
2019-06-11 09:41:40 -05:00
|
|
|
CValueInner::ByRef(from_addr) => {
|
|
|
|
let src_layout = from.1;
|
2019-06-11 08:43:22 -05:00
|
|
|
let size = dst_layout.size.bytes();
|
|
|
|
let src_align = src_layout.align.abi.bytes() as u8;
|
|
|
|
let dst_align = dst_layout.align.abi.bytes() as u8;
|
|
|
|
fx.bcx.emit_small_memcpy(
|
|
|
|
fx.module.target_config(),
|
|
|
|
addr,
|
2019-06-11 09:41:40 -05:00
|
|
|
from_addr,
|
2019-06-11 08:43:22 -05:00
|
|
|
size,
|
|
|
|
dst_align,
|
|
|
|
src_align,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn place_field(
|
|
|
|
self,
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-06-11 08:43:22 -05:00
|
|
|
field: mir::Field,
|
|
|
|
) -> CPlace<'tcx> {
|
|
|
|
let layout = self.layout();
|
|
|
|
let (base, extra) = self.to_addr_maybe_unsized(fx);
|
|
|
|
|
2019-09-02 12:50:21 -05:00
|
|
|
let (field_ptr, field_layout) = codegen_field(fx, base, extra, layout, field);
|
2019-08-29 04:23:19 -05:00
|
|
|
if field_layout.is_unsized() {
|
|
|
|
CPlace::for_addr_with_extra(field_ptr, extra.unwrap(), field_layout)
|
2019-06-11 08:43:22 -05:00
|
|
|
} else {
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlace::for_addr(field_ptr, field_layout)
|
|
|
|
}
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn place_index(
|
|
|
|
self,
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-06-11 08:43:22 -05:00
|
|
|
index: Value,
|
|
|
|
) -> CPlace<'tcx> {
|
2019-09-28 04:13:40 -05:00
|
|
|
let (elem_layout, addr) = match self.layout().ty.kind {
|
2019-06-11 08:43:22 -05:00
|
|
|
ty::Array(elem_ty, _) => (fx.layout_of(elem_ty), self.to_addr(fx)),
|
|
|
|
ty::Slice(elem_ty) => (fx.layout_of(elem_ty), self.to_addr_maybe_unsized(fx).0),
|
|
|
|
_ => bug!("place_index({:?})", self.layout().ty),
|
|
|
|
};
|
|
|
|
|
|
|
|
let offset = fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.imul_imm(index, elem_layout.size.bytes() as i64);
|
|
|
|
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlace::for_addr(fx.bcx.ins().iadd(addr, offset), elem_layout)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn place_deref(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>) -> CPlace<'tcx> {
|
2019-06-11 08:43:22 -05:00
|
|
|
let inner_layout = fx.layout_of(self.layout().ty.builtin_deref(true).unwrap().ty);
|
2019-09-14 10:53:36 -05:00
|
|
|
if has_ptr_meta(fx.tcx, inner_layout.ty) {
|
2019-06-23 08:14:26 -05:00
|
|
|
let (addr, extra) = self.to_cvalue(fx).load_scalar_pair(fx);
|
2019-08-29 04:23:19 -05:00
|
|
|
CPlace::for_addr_with_extra(addr, extra, inner_layout)
|
2019-09-14 10:53:36 -05:00
|
|
|
} else {
|
|
|
|
CPlace::for_addr(self.to_cvalue(fx).load_scalar(fx), inner_layout)
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn write_place_ref(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>, dest: CPlace<'tcx>) {
|
2019-09-14 10:53:36 -05:00
|
|
|
if has_ptr_meta(fx.tcx, self.layout().ty) {
|
2019-06-11 08:43:22 -05:00
|
|
|
let (value, extra) = self.to_addr_maybe_unsized(fx);
|
2019-08-31 12:28:09 -05:00
|
|
|
let ptr = CValue::by_val_pair(
|
|
|
|
value,
|
|
|
|
extra.expect("unsized type without metadata"),
|
|
|
|
dest.layout(),
|
|
|
|
);
|
2019-06-23 08:14:26 -05:00
|
|
|
dest.write_cvalue(fx, ptr);
|
2019-09-14 10:53:36 -05:00
|
|
|
} else {
|
|
|
|
let ptr = CValue::by_val(self.to_addr(fx), dest.layout());
|
|
|
|
dest.write_cvalue(fx, ptr);
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
|
|
|
|
assert!(!self.layout().is_unsized());
|
2019-08-29 04:23:19 -05:00
|
|
|
match self.inner {
|
|
|
|
CPlaceInner::NoPlace => {
|
2019-06-11 08:43:22 -05:00
|
|
|
assert!(layout.size.bytes() == 0);
|
|
|
|
}
|
2019-08-29 04:23:19 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
CPlace {
|
|
|
|
inner: self.inner,
|
|
|
|
layout,
|
2019-06-11 08:43:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn downcast_variant(
|
|
|
|
self,
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &FunctionCx<'_, 'tcx, impl Backend>,
|
2019-06-11 08:43:22 -05:00
|
|
|
variant: VariantIdx,
|
|
|
|
) -> Self {
|
|
|
|
let layout = self.layout().for_variant(fx, variant);
|
|
|
|
self.unchecked_cast_to(layout)
|
|
|
|
}
|
|
|
|
}
|