2022-07-04 07:48:05 -05:00
|
|
|
//! This file implements "place projections"; basically a symmetric API for 3 types: MPlaceTy, OpTy, PlaceTy.
|
|
|
|
//!
|
2022-08-17 21:13:37 -05:00
|
|
|
//! OpTy and PlaceTy generally work by "let's see if we are actually an MPlaceTy, and do something custom if not".
|
2022-07-04 07:48:05 -05:00
|
|
|
//! For PlaceTy, the custom thing is basically always to call `force_allocation` and then use the MPlaceTy logic anyway.
|
|
|
|
//! For OpTy, the custom thing on field pojections has to be pretty clever (since `Operand::Immediate` can have fields),
|
|
|
|
//! but for array/slice operations it only has to worry about `Operand::Uninit`. That makes the value part trivial,
|
|
|
|
//! but we still need to do bounds checking and adjust the layout. To not duplicate that with MPlaceTy, we actually
|
|
|
|
//! implement the logic on OpTy, and MPlaceTy calls that.
|
|
|
|
|
|
|
|
use rustc_middle::mir;
|
|
|
|
use rustc_middle::ty;
|
2023-07-23 14:35:54 -05:00
|
|
|
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
|
2023-07-05 14:13:26 -05:00
|
|
|
use rustc_middle::ty::Ty;
|
2023-07-23 14:35:54 -05:00
|
|
|
use rustc_target::abi::Size;
|
|
|
|
use rustc_target::abi::{self, VariantIdx};
|
2022-07-04 07:48:05 -05:00
|
|
|
|
|
|
|
use super::{
|
2023-07-23 14:35:54 -05:00
|
|
|
InterpCx, InterpResult, MPlaceTy, Machine, MemPlaceMeta, OpTy, PlaceTy, Provenance, Scalar,
|
2022-07-04 07:48:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: Working around https://github.com/rust-lang/rust/issues/54385
|
2022-07-18 17:47:31 -05:00
|
|
|
impl<'mir, 'tcx: 'mir, Prov, M> InterpCx<'mir, 'tcx, M>
|
2022-07-04 07:48:05 -05:00
|
|
|
where
|
2022-08-27 13:54:02 -05:00
|
|
|
Prov: Provenance + 'static,
|
2022-07-18 17:47:31 -05:00
|
|
|
M: Machine<'mir, 'tcx, Provenance = Prov>,
|
2022-07-04 07:48:05 -05:00
|
|
|
{
|
|
|
|
//# Field access
|
|
|
|
|
2023-07-23 14:35:54 -05:00
|
|
|
fn project_field(
|
2022-07-04 07:48:05 -05:00
|
|
|
&self,
|
2023-07-23 14:35:54 -05:00
|
|
|
base_layout: TyAndLayout<'tcx>,
|
|
|
|
base_meta: MemPlaceMeta<M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
field: usize,
|
2023-07-23 14:35:54 -05:00
|
|
|
) -> InterpResult<'tcx, (Size, MemPlaceMeta<M::Provenance>, TyAndLayout<'tcx>)> {
|
|
|
|
let offset = base_layout.fields.offset(field);
|
|
|
|
let field_layout = base_layout.field(self, field);
|
2022-07-04 07:48:05 -05:00
|
|
|
|
|
|
|
// Offset may need adjustment for unsized fields.
|
|
|
|
let (meta, offset) = if field_layout.is_unsized() {
|
2023-07-23 14:35:54 -05:00
|
|
|
if base_layout.is_sized() {
|
|
|
|
// An unsized field of a sized type? Sure...
|
|
|
|
// But const-prop actually feeds us such nonsense MIR!
|
|
|
|
throw_inval!(ConstPropNonsense);
|
|
|
|
}
|
2022-07-04 07:48:05 -05:00
|
|
|
// Re-use parent metadata to determine dynamic field layout.
|
|
|
|
// With custom DSTS, this *will* execute user-defined code, but the same
|
|
|
|
// happens at run-time so that's okay.
|
2023-07-23 14:35:54 -05:00
|
|
|
match self.size_and_align_of(&base_meta, &field_layout)? {
|
|
|
|
Some((_, align)) => (base_meta, offset.align_to(align)),
|
2022-07-04 07:48:05 -05:00
|
|
|
None => {
|
|
|
|
// For unsized types with an extern type tail we perform no adjustments.
|
|
|
|
// NOTE: keep this in sync with `PlaceRef::project_field` in the codegen backend.
|
2023-07-23 14:35:54 -05:00
|
|
|
assert!(matches!(base_meta, MemPlaceMeta::None));
|
|
|
|
(base_meta, offset)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-23 14:35:54 -05:00
|
|
|
// base_meta could be present; we might be accessing a sized field of an unsized
|
2022-07-04 07:48:05 -05:00
|
|
|
// struct.
|
|
|
|
(MemPlaceMeta::None, offset)
|
|
|
|
};
|
|
|
|
|
2023-07-23 14:35:54 -05:00
|
|
|
Ok((offset, meta, field_layout))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Offset a pointer to project to a field of a struct/union. Unlike `place_field`, this is
|
|
|
|
/// always possible without allocating, so it can take `&self`. Also return the field's layout.
|
|
|
|
/// This supports both struct and array fields.
|
|
|
|
///
|
|
|
|
/// This also works for arrays, but then the `usize` index type is restricting.
|
|
|
|
/// For indexing into arrays, use `mplace_index`.
|
|
|
|
pub fn mplace_field(
|
|
|
|
&self,
|
|
|
|
base: &MPlaceTy<'tcx, M::Provenance>,
|
|
|
|
field: usize,
|
|
|
|
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
|
|
|
|
let (offset, meta, field_layout) = self.project_field(base.layout, base.meta, field)?;
|
|
|
|
|
2022-07-04 07:48:05 -05:00
|
|
|
// We do not look at `base.layout.align` nor `field_layout.align`, unlike
|
|
|
|
// codegen -- mostly to see if we can get away with that
|
2022-07-14 19:32:45 -05:00
|
|
|
base.offset_with_meta(offset, meta, field_layout, self)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the place of a field inside the place, and also the field's type.
|
|
|
|
pub fn place_field(
|
2023-07-23 14:35:54 -05:00
|
|
|
&self,
|
2022-07-18 17:47:31 -05:00
|
|
|
base: &PlaceTy<'tcx, M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
field: usize,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> InterpResult<'tcx, PlaceTy<'tcx, M::Provenance>> {
|
2023-07-23 14:35:54 -05:00
|
|
|
let (offset, meta, field_layout) =
|
|
|
|
self.project_field(base.layout, self.place_meta(base)?, field)?;
|
|
|
|
base.offset_with_meta(offset, meta, field_layout, self)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn operand_field(
|
|
|
|
&self,
|
2022-07-18 17:47:31 -05:00
|
|
|
base: &OpTy<'tcx, M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
field: usize,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2023-07-23 14:35:54 -05:00
|
|
|
let (offset, meta, field_layout) = self.project_field(base.layout, base.meta()?, field)?;
|
|
|
|
base.offset_with_meta(offset, meta, field_layout, self)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//# Downcasting
|
|
|
|
|
|
|
|
pub fn mplace_downcast(
|
|
|
|
&self,
|
2022-07-18 17:47:31 -05:00
|
|
|
base: &MPlaceTy<'tcx, M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
variant: VariantIdx,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
|
2022-07-04 07:48:05 -05:00
|
|
|
// Downcasts only change the layout.
|
|
|
|
// (In particular, no check about whether this is even the active variant -- that's by design,
|
|
|
|
// see https://github.com/rust-lang/rust/issues/93688#issuecomment-1032929496.)
|
|
|
|
assert!(!base.meta.has_meta());
|
|
|
|
let mut base = *base;
|
|
|
|
base.layout = base.layout.for_variant(self, variant);
|
|
|
|
Ok(base)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn place_downcast(
|
|
|
|
&self,
|
2022-07-18 17:47:31 -05:00
|
|
|
base: &PlaceTy<'tcx, M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
variant: VariantIdx,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> InterpResult<'tcx, PlaceTy<'tcx, M::Provenance>> {
|
2022-07-04 07:48:05 -05:00
|
|
|
// Downcast just changes the layout
|
2022-07-15 21:58:20 -05:00
|
|
|
let mut base = base.clone();
|
2022-07-04 07:48:05 -05:00
|
|
|
base.layout = base.layout.for_variant(self, variant);
|
|
|
|
Ok(base)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn operand_downcast(
|
|
|
|
&self,
|
2022-07-18 17:47:31 -05:00
|
|
|
base: &OpTy<'tcx, M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
variant: VariantIdx,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-07-04 07:48:05 -05:00
|
|
|
// Downcast just changes the layout
|
2022-07-15 21:58:20 -05:00
|
|
|
let mut base = base.clone();
|
2022-07-04 07:48:05 -05:00
|
|
|
base.layout = base.layout.for_variant(self, variant);
|
|
|
|
Ok(base)
|
|
|
|
}
|
|
|
|
|
2023-07-23 14:35:54 -05:00
|
|
|
//# Slice and array indexing
|
2022-07-04 07:48:05 -05:00
|
|
|
|
2023-07-23 14:35:54 -05:00
|
|
|
/// Compute the offset and field layout for accessing the given index.
|
|
|
|
fn project_index(
|
2022-07-04 07:48:05 -05:00
|
|
|
&self,
|
2023-07-23 14:35:54 -05:00
|
|
|
base_layout: TyAndLayout<'tcx>,
|
|
|
|
base_meta: MemPlaceMeta<M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
index: u64,
|
2023-07-23 14:35:54 -05:00
|
|
|
) -> InterpResult<'tcx, (Size, TyAndLayout<'tcx>)> {
|
2022-07-04 07:48:05 -05:00
|
|
|
// Not using the layout method because we want to compute on u64
|
2023-07-23 14:35:54 -05:00
|
|
|
match base_layout.fields {
|
2022-07-04 07:48:05 -05:00
|
|
|
abi::FieldsShape::Array { stride, count: _ } => {
|
|
|
|
// `count` is nonsense for slices, use the dynamic length instead.
|
2023-07-23 14:35:54 -05:00
|
|
|
let len = base_meta.len(base_layout, self)?;
|
2022-07-04 07:48:05 -05:00
|
|
|
if index >= len {
|
|
|
|
// This can only be reached in ConstProp and non-rustc-MIR.
|
|
|
|
throw_ub!(BoundsCheckFailed { len, index });
|
|
|
|
}
|
|
|
|
let offset = stride * index; // `Size` multiplication
|
|
|
|
// All fields have the same layout.
|
2023-07-23 14:35:54 -05:00
|
|
|
let field_layout = base_layout.field(self, 0);
|
|
|
|
Ok((offset, field_layout))
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
_ => span_bug!(
|
|
|
|
self.cur_span(),
|
|
|
|
"`mplace_index` called on non-array type {:?}",
|
2023-07-23 14:35:54 -05:00
|
|
|
base_layout.ty
|
2022-07-04 07:48:05 -05:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-23 14:35:54 -05:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn operand_index(
|
|
|
|
&self,
|
|
|
|
base: &OpTy<'tcx, M::Provenance>,
|
|
|
|
index: u64,
|
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
|
|
|
let (offset, field_layout) = self.project_index(base.layout, base.meta()?, index)?;
|
|
|
|
base.offset(offset, field_layout, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Index into an array.
|
|
|
|
pub fn mplace_index(
|
|
|
|
&self,
|
|
|
|
base: &MPlaceTy<'tcx, M::Provenance>,
|
|
|
|
index: u64,
|
|
|
|
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
|
|
|
|
let (offset, field_layout) = self.project_index(base.layout, base.meta, index)?;
|
|
|
|
base.offset(offset, field_layout, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn place_index(
|
|
|
|
&self,
|
|
|
|
base: &PlaceTy<'tcx, M::Provenance>,
|
|
|
|
index: u64,
|
|
|
|
) -> InterpResult<'tcx, PlaceTy<'tcx, M::Provenance>> {
|
|
|
|
let (offset, field_layout) =
|
|
|
|
self.project_index(base.layout, self.place_meta(base)?, index)?;
|
|
|
|
base.offset(offset, field_layout, self)
|
|
|
|
}
|
|
|
|
|
2022-11-27 05:15:06 -06:00
|
|
|
/// Iterates over all fields of an array. Much more efficient than doing the
|
|
|
|
/// same by repeatedly calling `operand_index`.
|
2022-07-04 07:48:05 -05:00
|
|
|
pub fn operand_array_fields<'a>(
|
|
|
|
&self,
|
2022-07-18 17:47:31 -05:00
|
|
|
base: &'a OpTy<'tcx, Prov>,
|
|
|
|
) -> InterpResult<'tcx, impl Iterator<Item = InterpResult<'tcx, OpTy<'tcx, Prov>>> + 'a> {
|
2022-07-04 07:48:05 -05:00
|
|
|
let abi::FieldsShape::Array { stride, .. } = base.layout.fields else {
|
|
|
|
span_bug!(self.cur_span(), "operand_array_fields: expected an array layout");
|
|
|
|
};
|
2023-07-23 14:35:54 -05:00
|
|
|
let len = base.len(self)?;
|
2022-07-14 19:32:45 -05:00
|
|
|
let field_layout = base.layout.field(self, 0);
|
2022-07-04 07:48:05 -05:00
|
|
|
let dl = &self.tcx.data_layout;
|
|
|
|
// `Size` multiplication
|
2022-07-14 19:32:45 -05:00
|
|
|
Ok((0..len).map(move |i| base.offset(stride * i, field_layout, dl)))
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
|
2023-07-23 14:35:54 -05:00
|
|
|
/// Iterates over all fields of an array. Much more efficient than doing the
|
|
|
|
/// same by repeatedly calling `place_index`.
|
|
|
|
pub fn place_array_fields<'a>(
|
2022-07-04 07:48:05 -05:00
|
|
|
&self,
|
2023-07-23 14:35:54 -05:00
|
|
|
base: &'a PlaceTy<'tcx, Prov>,
|
|
|
|
) -> InterpResult<'tcx, impl Iterator<Item = InterpResult<'tcx, PlaceTy<'tcx, Prov>>> + 'a> {
|
|
|
|
let abi::FieldsShape::Array { stride, .. } = base.layout.fields else {
|
|
|
|
span_bug!(self.cur_span(), "place_array_fields: expected an array layout");
|
|
|
|
};
|
|
|
|
let len = self.place_meta(base)?.len(base.layout, self)?;
|
|
|
|
let field_layout = base.layout.field(self, 0);
|
|
|
|
let dl = &self.tcx.data_layout;
|
|
|
|
// `Size` multiplication
|
|
|
|
Ok((0..len).map(move |i| base.offset(stride * i, field_layout, dl)))
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//# ConstantIndex support
|
|
|
|
|
2023-07-23 14:35:54 -05:00
|
|
|
fn project_constant_index(
|
2022-07-04 07:48:05 -05:00
|
|
|
&self,
|
2023-07-23 14:35:54 -05:00
|
|
|
base_layout: TyAndLayout<'tcx>,
|
|
|
|
base_meta: MemPlaceMeta<M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
offset: u64,
|
|
|
|
min_length: u64,
|
|
|
|
from_end: bool,
|
2023-07-23 14:35:54 -05:00
|
|
|
) -> InterpResult<'tcx, (Size, TyAndLayout<'tcx>)> {
|
|
|
|
let n = base_meta.len(base_layout, self)?;
|
2022-07-04 07:48:05 -05:00
|
|
|
if n < min_length {
|
|
|
|
// This can only be reached in ConstProp and non-rustc-MIR.
|
|
|
|
throw_ub!(BoundsCheckFailed { len: min_length, index: n });
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = if from_end {
|
|
|
|
assert!(0 < offset && offset <= min_length);
|
|
|
|
n.checked_sub(offset).unwrap()
|
|
|
|
} else {
|
|
|
|
assert!(offset < min_length);
|
|
|
|
offset
|
|
|
|
};
|
|
|
|
|
2023-07-23 14:35:54 -05:00
|
|
|
self.project_index(base_layout, base_meta, index)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn operand_constant_index(
|
|
|
|
&self,
|
|
|
|
base: &OpTy<'tcx, M::Provenance>,
|
|
|
|
offset: u64,
|
|
|
|
min_length: u64,
|
|
|
|
from_end: bool,
|
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
|
|
|
let (offset, layout) =
|
|
|
|
self.project_constant_index(base.layout, base.meta()?, offset, min_length, from_end)?;
|
|
|
|
base.offset(offset, layout, self)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn place_constant_index(
|
2023-07-23 14:35:54 -05:00
|
|
|
&self,
|
2022-07-18 17:47:31 -05:00
|
|
|
base: &PlaceTy<'tcx, M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
offset: u64,
|
|
|
|
min_length: u64,
|
|
|
|
from_end: bool,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> InterpResult<'tcx, PlaceTy<'tcx, M::Provenance>> {
|
2023-07-23 14:35:54 -05:00
|
|
|
let (offset, layout) = self.project_constant_index(
|
|
|
|
base.layout,
|
|
|
|
self.place_meta(base)?,
|
|
|
|
offset,
|
|
|
|
min_length,
|
|
|
|
from_end,
|
|
|
|
)?;
|
|
|
|
base.offset(offset, layout, self)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//# Subslicing
|
|
|
|
|
2023-07-23 14:35:54 -05:00
|
|
|
fn project_subslice(
|
2022-07-04 07:48:05 -05:00
|
|
|
&self,
|
2023-07-23 14:35:54 -05:00
|
|
|
base_layout: TyAndLayout<'tcx>,
|
|
|
|
base_meta: MemPlaceMeta<M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
from: u64,
|
|
|
|
to: u64,
|
|
|
|
from_end: bool,
|
2023-07-23 14:35:54 -05:00
|
|
|
) -> InterpResult<'tcx, (Size, MemPlaceMeta<M::Provenance>, TyAndLayout<'tcx>)> {
|
|
|
|
let len = base_meta.len(base_layout, self)?; // also asserts that we have a type where this makes sense
|
2022-07-04 07:48:05 -05:00
|
|
|
let actual_to = if from_end {
|
|
|
|
if from.checked_add(to).map_or(true, |to| to > len) {
|
|
|
|
// This can only be reached in ConstProp and non-rustc-MIR.
|
|
|
|
throw_ub!(BoundsCheckFailed { len: len, index: from.saturating_add(to) });
|
|
|
|
}
|
|
|
|
len.checked_sub(to).unwrap()
|
|
|
|
} else {
|
|
|
|
to
|
|
|
|
};
|
|
|
|
|
|
|
|
// Not using layout method because that works with usize, and does not work with slices
|
|
|
|
// (that have count 0 in their layout).
|
2023-07-23 14:35:54 -05:00
|
|
|
let from_offset = match base_layout.fields {
|
2022-07-04 07:48:05 -05:00
|
|
|
abi::FieldsShape::Array { stride, .. } => stride * from, // `Size` multiplication is checked
|
|
|
|
_ => {
|
2023-07-23 14:35:54 -05:00
|
|
|
span_bug!(self.cur_span(), "unexpected layout of index access: {:#?}", base_layout)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compute meta and new layout
|
|
|
|
let inner_len = actual_to.checked_sub(from).unwrap();
|
2023-07-23 14:35:54 -05:00
|
|
|
let (meta, ty) = match base_layout.ty.kind() {
|
2022-07-04 07:48:05 -05:00
|
|
|
// It is not nice to match on the type, but that seems to be the only way to
|
|
|
|
// implement this.
|
2023-07-05 14:13:26 -05:00
|
|
|
ty::Array(inner, _) => {
|
|
|
|
(MemPlaceMeta::None, Ty::new_array(self.tcx.tcx, *inner, inner_len))
|
|
|
|
}
|
2022-07-04 07:48:05 -05:00
|
|
|
ty::Slice(..) => {
|
2023-02-14 08:31:26 -06:00
|
|
|
let len = Scalar::from_target_usize(inner_len, self);
|
2023-07-23 14:35:54 -05:00
|
|
|
(MemPlaceMeta::Meta(len), base_layout.ty)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2023-07-23 14:35:54 -05:00
|
|
|
span_bug!(self.cur_span(), "cannot subslice non-array type: `{:?}`", base_layout.ty)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let layout = self.layout_of(ty)?;
|
2023-07-23 14:35:54 -05:00
|
|
|
Ok((from_offset, meta, layout))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn operand_subslice(
|
|
|
|
&self,
|
|
|
|
base: &OpTy<'tcx, M::Provenance>,
|
|
|
|
from: u64,
|
|
|
|
to: u64,
|
|
|
|
from_end: bool,
|
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
|
|
|
let (from_offset, meta, layout) =
|
|
|
|
self.project_subslice(base.layout, base.meta()?, from, to, from_end)?;
|
2022-07-14 19:32:45 -05:00
|
|
|
base.offset_with_meta(from_offset, meta, layout, self)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn place_subslice(
|
2023-07-23 14:35:54 -05:00
|
|
|
&self,
|
2022-07-18 17:47:31 -05:00
|
|
|
base: &PlaceTy<'tcx, M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
from: u64,
|
|
|
|
to: u64,
|
|
|
|
from_end: bool,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> InterpResult<'tcx, PlaceTy<'tcx, M::Provenance>> {
|
2023-07-23 14:35:54 -05:00
|
|
|
let (from_offset, meta, layout) =
|
|
|
|
self.project_subslice(base.layout, self.place_meta(base)?, from, to, from_end)?;
|
|
|
|
base.offset_with_meta(from_offset, meta, layout, self)
|
2022-07-04 07:48:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//# Applying a general projection
|
|
|
|
|
|
|
|
/// Projects into a place.
|
|
|
|
#[instrument(skip(self), level = "trace")]
|
|
|
|
pub fn place_projection(
|
2023-07-23 14:35:54 -05:00
|
|
|
&self,
|
2022-07-18 17:47:31 -05:00
|
|
|
base: &PlaceTy<'tcx, M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
proj_elem: mir::PlaceElem<'tcx>,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> InterpResult<'tcx, PlaceTy<'tcx, M::Provenance>> {
|
2022-07-04 07:48:05 -05:00
|
|
|
use rustc_middle::mir::ProjectionElem::*;
|
|
|
|
Ok(match proj_elem {
|
2022-07-27 06:58:34 -05:00
|
|
|
OpaqueCast(ty) => {
|
|
|
|
let mut place = base.clone();
|
|
|
|
place.layout = self.layout_of(ty)?;
|
|
|
|
place
|
|
|
|
}
|
2022-07-04 07:48:05 -05:00
|
|
|
Field(field, _) => self.place_field(base, field.index())?,
|
|
|
|
Downcast(_, variant) => self.place_downcast(base, variant)?,
|
|
|
|
Deref => self.deref_operand(&self.place_to_op(base)?)?.into(),
|
|
|
|
Index(local) => {
|
|
|
|
let layout = self.layout_of(self.tcx.types.usize)?;
|
|
|
|
let n = self.local_to_op(self.frame(), local, Some(layout))?;
|
2023-02-14 08:31:26 -06:00
|
|
|
let n = self.read_target_usize(&n)?;
|
2022-07-04 07:48:05 -05:00
|
|
|
self.place_index(base, n)?
|
|
|
|
}
|
|
|
|
ConstantIndex { offset, min_length, from_end } => {
|
|
|
|
self.place_constant_index(base, offset, min_length, from_end)?
|
|
|
|
}
|
|
|
|
Subslice { from, to, from_end } => self.place_subslice(base, from, to, from_end)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self), level = "trace")]
|
|
|
|
pub fn operand_projection(
|
|
|
|
&self,
|
2022-07-18 17:47:31 -05:00
|
|
|
base: &OpTy<'tcx, M::Provenance>,
|
2022-07-04 07:48:05 -05:00
|
|
|
proj_elem: mir::PlaceElem<'tcx>,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2022-07-04 07:48:05 -05:00
|
|
|
use rustc_middle::mir::ProjectionElem::*;
|
|
|
|
Ok(match proj_elem {
|
2022-07-27 06:58:34 -05:00
|
|
|
OpaqueCast(ty) => {
|
|
|
|
let mut op = base.clone();
|
|
|
|
op.layout = self.layout_of(ty)?;
|
|
|
|
op
|
|
|
|
}
|
2022-07-04 07:48:05 -05:00
|
|
|
Field(field, _) => self.operand_field(base, field.index())?,
|
|
|
|
Downcast(_, variant) => self.operand_downcast(base, variant)?,
|
|
|
|
Deref => self.deref_operand(base)?.into(),
|
|
|
|
Index(local) => {
|
|
|
|
let layout = self.layout_of(self.tcx.types.usize)?;
|
|
|
|
let n = self.local_to_op(self.frame(), local, Some(layout))?;
|
2023-02-14 08:31:26 -06:00
|
|
|
let n = self.read_target_usize(&n)?;
|
2022-07-04 07:48:05 -05:00
|
|
|
self.operand_index(base, n)?
|
|
|
|
}
|
|
|
|
ConstantIndex { offset, min_length, from_end } => {
|
|
|
|
self.operand_constant_index(base, offset, min_length, from_end)?
|
|
|
|
}
|
|
|
|
Subslice { from, to, from_end } => self.operand_subslice(base, from, to, from_end)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|