2020-09-23 08:13:49 -05:00
|
|
|
//! Codegen of the [`PointerCast::Unsize`] operation.
|
|
|
|
//!
|
|
|
|
//! [`PointerCast::Unsize`]: `rustc_middle::ty::adjustment::PointerCast::Unsize`
|
|
|
|
|
2018-12-29 08:33:34 -06:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
// Adapted from https://github.com/rust-lang/rust/blob/2a663555ddf36f6b041445894a8c175cd1bc718c/src/librustc_codegen_ssa/base.rs#L159-L307
|
|
|
|
|
|
|
|
/// Retrieve the information we are losing (making dynamic) in an unsizing
|
|
|
|
/// adjustment.
|
|
|
|
///
|
|
|
|
/// The `old_info` argument is a bit funny. It is intended for use
|
|
|
|
/// in an upcast, where the new vtable for an object will be derived
|
|
|
|
/// from the old one.
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn unsized_info<'tcx>(
|
2021-03-05 12:12:59 -06:00
|
|
|
fx: &mut FunctionCx<'_, '_, 'tcx>,
|
2018-12-29 08:33:34 -06:00
|
|
|
source: Ty<'tcx>,
|
|
|
|
target: Ty<'tcx>,
|
|
|
|
old_info: Option<Value>,
|
|
|
|
) -> Value {
|
2019-07-20 06:04:02 -05:00
|
|
|
let (source, target) =
|
2021-03-05 12:12:59 -06:00
|
|
|
fx.tcx.struct_lockstep_tails_erasing_lifetimes(source, target, ParamEnv::reveal_all());
|
2020-09-05 03:38:49 -05:00
|
|
|
match (&source.kind(), &target.kind()) {
|
2021-03-05 12:12:59 -06:00
|
|
|
(&ty::Array(_, len), &ty::Slice(_)) => fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.iconst(fx.pointer_type, len.eval_usize(fx.tcx, ParamEnv::reveal_all()) as i64),
|
2021-07-31 09:46:23 -05:00
|
|
|
(&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => {
|
|
|
|
let old_info =
|
|
|
|
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
|
|
|
|
if data_a.principal_def_id() == data_b.principal_def_id() {
|
2022-08-06 16:18:59 -05:00
|
|
|
// A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables.
|
2021-07-31 09:46:23 -05:00
|
|
|
return old_info;
|
|
|
|
}
|
|
|
|
|
2021-08-17 23:45:18 -05:00
|
|
|
// trait upcasting coercion
|
|
|
|
let vptr_entry_idx =
|
|
|
|
fx.tcx.vtable_trait_upcasting_coercion_new_vptr_slot((source, target));
|
2021-07-31 09:46:23 -05:00
|
|
|
|
|
|
|
if let Some(entry_idx) = vptr_entry_idx {
|
|
|
|
let entry_idx = u32::try_from(entry_idx).unwrap();
|
|
|
|
let entry_offset = entry_idx * fx.pointer_type.bytes();
|
|
|
|
let vptr_ptr = Pointer::new(old_info).offset_i64(fx, entry_offset.into()).load(
|
|
|
|
fx,
|
|
|
|
fx.pointer_type,
|
|
|
|
crate::vtable::vtable_memflags(),
|
|
|
|
);
|
|
|
|
vptr_ptr
|
|
|
|
} else {
|
|
|
|
old_info
|
|
|
|
}
|
2018-12-29 08:33:34 -06:00
|
|
|
}
|
2021-06-20 04:43:25 -05:00
|
|
|
(_, &ty::Dynamic(ref data, ..)) => crate::vtable::get_vtable(fx, source, data.principal()),
|
2021-03-05 12:12:59 -06:00
|
|
|
_ => bug!("unsized_info: invalid unsizing {:?} -> {:?}", source, target),
|
2018-12-29 08:33:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 09:46:23 -05:00
|
|
|
/// Coerce `src` to `dst_ty`.
|
|
|
|
fn unsize_ptr<'tcx>(
|
2021-03-05 12:12:59 -06:00
|
|
|
fx: &mut FunctionCx<'_, '_, 'tcx>,
|
2018-12-29 08:33:34 -06:00
|
|
|
src: Value,
|
2020-03-30 12:00:24 -05:00
|
|
|
src_layout: TyAndLayout<'tcx>,
|
|
|
|
dst_layout: TyAndLayout<'tcx>,
|
2021-07-31 09:46:23 -05:00
|
|
|
old_info: Option<Value>,
|
2018-12-29 08:33:34 -06:00
|
|
|
) -> (Value, Value) {
|
2020-09-05 03:38:49 -05:00
|
|
|
match (&src_layout.ty.kind(), &dst_layout.ty.kind()) {
|
2019-02-21 08:06:09 -06:00
|
|
|
(&ty::Ref(_, a, _), &ty::Ref(_, b, _))
|
|
|
|
| (&ty::Ref(_, a, _), &ty::RawPtr(ty::TypeAndMut { ty: b, .. }))
|
|
|
|
| (&ty::RawPtr(ty::TypeAndMut { ty: a, .. }), &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-24 21:13:38 -06:00
|
|
|
(src, unsized_info(fx, *a, *b, old_info))
|
2018-12-29 08:33:34 -06:00
|
|
|
}
|
|
|
|
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
|
2020-01-15 06:17:09 -06:00
|
|
|
let (a, b) = (src_layout.ty.boxed_ty(), dst_layout.ty.boxed_ty());
|
2021-07-31 09:46:23 -05:00
|
|
|
(src, unsized_info(fx, a, b, old_info))
|
2018-12-29 08:33:34 -06:00
|
|
|
}
|
|
|
|
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
|
|
|
|
assert_eq!(def_a, def_b);
|
|
|
|
|
2021-07-31 09:46:23 -05:00
|
|
|
if src_layout == dst_layout {
|
|
|
|
return (src, old_info.unwrap());
|
|
|
|
}
|
|
|
|
|
2018-12-29 08:33:34 -06:00
|
|
|
let mut result = None;
|
|
|
|
for i in 0..src_layout.fields.count() {
|
|
|
|
let src_f = src_layout.field(fx, i);
|
|
|
|
assert_eq!(src_layout.fields.offset(i).bytes(), 0);
|
|
|
|
assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
|
|
|
|
if src_f.is_zst() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
assert_eq!(src_layout.size, src_f.size);
|
|
|
|
|
|
|
|
let dst_f = dst_layout.field(fx, i);
|
|
|
|
assert_ne!(src_f.ty, dst_f.ty);
|
|
|
|
assert_eq!(result, None);
|
2021-07-31 09:46:23 -05:00
|
|
|
result = Some(unsize_ptr(fx, src, src_f, dst_f, old_info));
|
2018-12-29 08:33:34 -06:00
|
|
|
}
|
|
|
|
result.unwrap()
|
|
|
|
}
|
2021-07-31 09:46:23 -05:00
|
|
|
_ => bug!("unsize_ptr: called on bad types"),
|
2018-12-29 08:33:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Coerce `src`, which is a reference to a value of type `src_ty`,
|
|
|
|
/// to a value of type `dst_ty` and store the result in `dst`
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn coerce_unsized_into<'tcx>(
|
2021-03-05 12:12:59 -06:00
|
|
|
fx: &mut FunctionCx<'_, '_, 'tcx>,
|
2018-12-29 08:33:34 -06:00
|
|
|
src: CValue<'tcx>,
|
|
|
|
dst: CPlace<'tcx>,
|
2019-02-21 08:06:09 -06:00
|
|
|
) {
|
2018-12-29 08:33:34 -06:00
|
|
|
let src_ty = src.layout().ty;
|
|
|
|
let dst_ty = dst.layout().ty;
|
|
|
|
let mut coerce_ptr = || {
|
2021-03-05 12:12:59 -06:00
|
|
|
let (base, info) =
|
|
|
|
if fx.layout_of(src.layout().ty.builtin_deref(true).unwrap().ty).is_unsized() {
|
2021-07-31 09:46:23 -05:00
|
|
|
let (old_base, old_info) = src.load_scalar_pair(fx);
|
|
|
|
unsize_ptr(fx, old_base, src.layout(), dst.layout(), Some(old_info))
|
2021-03-05 12:12:59 -06:00
|
|
|
} else {
|
|
|
|
let base = src.load_scalar(fx);
|
2021-07-31 09:46:23 -05:00
|
|
|
unsize_ptr(fx, base, src.layout(), dst.layout(), None)
|
2021-03-05 12:12:59 -06:00
|
|
|
};
|
2019-06-11 08:32:30 -05:00
|
|
|
dst.write_cvalue(fx, CValue::by_val_pair(base, info, dst.layout()));
|
2018-12-29 08:33:34 -06:00
|
|
|
};
|
2020-09-05 03:38:49 -05:00
|
|
|
match (&src_ty.kind(), &dst_ty.kind()) {
|
2019-02-21 08:06:09 -06:00
|
|
|
(&ty::Ref(..), &ty::Ref(..))
|
|
|
|
| (&ty::Ref(..), &ty::RawPtr(..))
|
|
|
|
| (&ty::RawPtr(..), &ty::RawPtr(..)) => coerce_ptr(),
|
2018-12-29 08:33:34 -06:00
|
|
|
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
|
|
|
|
assert_eq!(def_a, def_b);
|
|
|
|
|
2022-03-04 14:28:41 -06:00
|
|
|
for i in 0..def_a.variant(VariantIdx::new(0)).fields.len() {
|
2018-12-29 08:33:34 -06:00
|
|
|
let src_f = src.value_field(fx, mir::Field::new(i));
|
|
|
|
let dst_f = dst.place_field(fx, mir::Field::new(i));
|
|
|
|
|
|
|
|
if dst_f.layout().is_zst() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if src_f.layout().ty == dst_f.layout().ty {
|
|
|
|
dst_f.write_cvalue(fx, src_f);
|
|
|
|
} else {
|
|
|
|
coerce_unsized_into(fx, src_f, dst_f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 12:12:59 -06:00
|
|
|
_ => bug!("coerce_unsized_into: invalid coercion {:?} -> {:?}", src_ty, dst_ty),
|
2018-12-29 08:33:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 09:08:30 -05:00
|
|
|
pub(crate) fn coerce_dyn_star<'tcx>(
|
|
|
|
fx: &mut FunctionCx<'_, '_, 'tcx>,
|
|
|
|
src: CValue<'tcx>,
|
|
|
|
dst: CPlace<'tcx>,
|
|
|
|
) {
|
|
|
|
let data = src.load_scalar(fx);
|
|
|
|
|
|
|
|
let vtable = if let ty::Dynamic(data, _, ty::DynStar) = dst.layout().ty.kind() {
|
|
|
|
crate::vtable::get_vtable(fx, src.layout().ty, data.principal())
|
|
|
|
} else {
|
|
|
|
bug!("Only valid to do a DynStar cast into a DynStar type")
|
|
|
|
};
|
|
|
|
|
|
|
|
dst.write_cvalue(fx, CValue::by_val_pair(data, vtable, dst.layout()));
|
|
|
|
}
|
|
|
|
|
2018-12-29 08:33:34 -06:00
|
|
|
// Adapted from https://github.com/rust-lang/rust/blob/2a663555ddf36f6b041445894a8c175cd1bc718c/src/librustc_codegen_ssa/glue.rs
|
|
|
|
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn size_and_align_of_dst<'tcx>(
|
2021-03-05 12:12:59 -06:00
|
|
|
fx: &mut FunctionCx<'_, '_, 'tcx>,
|
2020-03-30 12:00:24 -05:00
|
|
|
layout: TyAndLayout<'tcx>,
|
2018-12-29 08:33:34 -06:00
|
|
|
info: Value,
|
|
|
|
) -> (Value, Value) {
|
2022-07-25 09:07:57 -05:00
|
|
|
assert!(layout.is_unsized() || layout.abi == Abi::Uninhabited);
|
2020-09-05 03:38:49 -05:00
|
|
|
match layout.ty.kind() {
|
2018-12-29 08:33:34 -06:00
|
|
|
ty::Dynamic(..) => {
|
|
|
|
// load size/align from vtable
|
2021-03-05 12:12:59 -06:00
|
|
|
(crate::vtable::size_of_obj(fx, info), crate::vtable::min_align_of_obj(fx, info))
|
2018-12-29 08:33:34 -06:00
|
|
|
}
|
|
|
|
ty::Slice(_) | ty::Str => {
|
|
|
|
let unit = layout.field(fx, 0);
|
|
|
|
// The info in this case is the length of the str, so the size is that
|
|
|
|
// times the unit size.
|
2019-02-21 08:06:09 -06:00
|
|
|
(
|
|
|
|
fx.bcx.ins().imul_imm(info, unit.size.bytes() as i64),
|
2021-03-05 12:12:59 -06:00
|
|
|
fx.bcx.ins().iconst(fx.pointer_type, unit.align.abi.bytes() as i64),
|
2019-02-21 08:06:09 -06:00
|
|
|
)
|
2018-12-29 08:33:34 -06:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// First get the size of all statically known fields.
|
|
|
|
// Don't use size_of because it also rounds up to alignment, which we
|
|
|
|
// want to avoid, as the unsized field's alignment could be smaller.
|
2020-01-15 06:17:09 -06:00
|
|
|
assert!(!layout.ty.is_simd());
|
2018-12-29 08:33:34 -06:00
|
|
|
|
|
|
|
let i = layout.fields.count() - 1;
|
|
|
|
let sized_size = layout.fields.offset(i).bytes();
|
|
|
|
let sized_align = layout.align.abi.bytes();
|
|
|
|
let sized_align = fx.bcx.ins().iconst(fx.pointer_type, sized_align as i64);
|
|
|
|
|
|
|
|
// Recurse to get the size of the dynamically sized field (must be
|
|
|
|
// the last field).
|
2020-01-15 06:17:09 -06:00
|
|
|
let field_layout = layout.field(fx, i);
|
|
|
|
let (unsized_size, mut unsized_align) = size_and_align_of_dst(fx, field_layout, info);
|
2018-12-29 08:33:34 -06:00
|
|
|
|
|
|
|
// FIXME (#26403, #27023): We should be adding padding
|
|
|
|
// to `sized_size` (to accommodate the `unsized_align`
|
|
|
|
// required of the unsized field that follows) before
|
|
|
|
// summing it with `sized_size`. (Note that since #26403
|
|
|
|
// is unfixed, we do not yet add the necessary padding
|
|
|
|
// here. But this is where the add would go.)
|
|
|
|
|
|
|
|
// Return the sum of sizes and max of aligns.
|
|
|
|
let size = fx.bcx.ins().iadd_imm(unsized_size, sized_size as i64);
|
|
|
|
|
|
|
|
// Packed types ignore the alignment of their fields.
|
2020-09-05 03:38:49 -05:00
|
|
|
if let ty::Adt(def, _) = layout.ty.kind() {
|
2022-03-04 14:28:41 -06:00
|
|
|
if def.repr().packed() {
|
2018-12-29 08:33:34 -06:00
|
|
|
unsized_align = sized_align;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Choose max of two known alignments (combined value must
|
|
|
|
// be aligned according to more restrictive of the two).
|
2021-03-05 12:12:59 -06:00
|
|
|
let cmp = fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, sized_align, unsized_align);
|
2018-12-29 08:33:34 -06:00
|
|
|
let align = fx.bcx.ins().select(cmp, sized_align, unsized_align);
|
|
|
|
|
|
|
|
// Issue #27023: must add any necessary padding to `size`
|
|
|
|
// (to make it a multiple of `align`) before returning it.
|
|
|
|
//
|
|
|
|
// Namely, the returned size should be, in C notation:
|
|
|
|
//
|
|
|
|
// `size + ((size & (align-1)) ? align : 0)`
|
|
|
|
//
|
|
|
|
// emulated via the semi-standard fast bit trick:
|
|
|
|
//
|
|
|
|
// `(size + (align-1)) & -align`
|
|
|
|
let addend = fx.bcx.ins().iadd_imm(align, -1);
|
|
|
|
let add = fx.bcx.ins().iadd(size, addend);
|
2020-02-15 04:59:38 -06:00
|
|
|
let neg = fx.bcx.ins().ineg(align);
|
2018-12-29 08:33:34 -06:00
|
|
|
let size = fx.bcx.ins().band(add, neg);
|
|
|
|
|
|
|
|
(size, align)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|