interpret: remove Readable trait, we can use Projectable instead

This commit is contained in:
Ralf Jung 2024-08-28 17:39:09 +02:00
parent 7b18b3eb6d
commit fa60ea7d38
5 changed files with 21 additions and 47 deletions

View File

@ -7,7 +7,7 @@
use tracing::{instrument, trace}; use tracing::{instrument, trace};
use super::{ use super::{
err_ub, throw_ub, ImmTy, InterpCx, InterpResult, Machine, Readable, Scalar, Writeable, err_ub, throw_ub, ImmTy, InterpCx, InterpResult, Machine, Projectable, Scalar, Writeable,
}; };
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
@ -60,7 +60,7 @@ pub fn write_discriminant(
#[instrument(skip(self), level = "trace")] #[instrument(skip(self), level = "trace")]
pub fn read_discriminant( pub fn read_discriminant(
&self, &self,
op: &impl Readable<'tcx, M::Provenance>, op: &impl Projectable<'tcx, M::Provenance>,
) -> InterpResult<'tcx, VariantIdx> { ) -> InterpResult<'tcx, VariantIdx> {
let ty = op.layout().ty; let ty = op.layout().ty;
trace!("read_discriminant_value {:#?}", op.layout()); trace!("read_discriminant_value {:#?}", op.layout());

View File

@ -33,7 +33,7 @@
pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, ReturnAction}; pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, ReturnAction};
pub use self::memory::{AllocKind, AllocRef, AllocRefMut, FnVal, Memory, MemoryKind}; pub use self::memory::{AllocKind, AllocRef, AllocRefMut, FnVal, Memory, MemoryKind};
use self::operand::Operand; use self::operand::Operand;
pub use self::operand::{ImmTy, Immediate, OpTy, Readable}; pub use self::operand::{ImmTy, Immediate, OpTy};
pub use self::place::{MPlaceTy, MemPlaceMeta, PlaceTy, Writeable}; pub use self::place::{MPlaceTy, MemPlaceMeta, PlaceTy, Writeable};
use self::place::{MemPlace, Place}; use self::place::{MemPlace, Place};
pub use self::projection::{OffsetMode, Projectable}; pub use self::projection::{OffsetMode, Projectable};

View File

@ -490,32 +490,6 @@ fn to_op<M: Machine<'tcx, Provenance = Prov>>(
} }
} }
/// The `Readable` trait describes interpreter values that one can read from.
pub trait Readable<'tcx, Prov: Provenance>: Projectable<'tcx, Prov> {
fn as_mplace_or_imm(&self) -> Either<MPlaceTy<'tcx, Prov>, ImmTy<'tcx, Prov>>;
}
impl<'tcx, Prov: Provenance> Readable<'tcx, Prov> for OpTy<'tcx, Prov> {
#[inline(always)]
fn as_mplace_or_imm(&self) -> Either<MPlaceTy<'tcx, Prov>, ImmTy<'tcx, Prov>> {
self.as_mplace_or_imm()
}
}
impl<'tcx, Prov: Provenance> Readable<'tcx, Prov> for MPlaceTy<'tcx, Prov> {
#[inline(always)]
fn as_mplace_or_imm(&self) -> Either<MPlaceTy<'tcx, Prov>, ImmTy<'tcx, Prov>> {
Left(self.clone())
}
}
impl<'tcx, Prov: Provenance> Readable<'tcx, Prov> for ImmTy<'tcx, Prov> {
#[inline(always)]
fn as_mplace_or_imm(&self) -> Either<MPlaceTy<'tcx, Prov>, ImmTy<'tcx, Prov>> {
Right(self.clone())
}
}
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
/// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`. /// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`.
/// Returns `None` if the layout does not permit loading this as a value. /// Returns `None` if the layout does not permit loading this as a value.
@ -588,9 +562,9 @@ fn read_immediate_from_mplace_raw(
/// ConstProp needs it, though. /// ConstProp needs it, though.
pub fn read_immediate_raw( pub fn read_immediate_raw(
&self, &self,
src: &impl Readable<'tcx, M::Provenance>, src: &impl Projectable<'tcx, M::Provenance>,
) -> InterpResult<'tcx, Either<MPlaceTy<'tcx, M::Provenance>, ImmTy<'tcx, M::Provenance>>> { ) -> InterpResult<'tcx, Either<MPlaceTy<'tcx, M::Provenance>, ImmTy<'tcx, M::Provenance>>> {
Ok(match src.as_mplace_or_imm() { Ok(match src.to_op(self)?.as_mplace_or_imm() {
Left(ref mplace) => { Left(ref mplace) => {
if let Some(val) = self.read_immediate_from_mplace_raw(mplace)? { if let Some(val) = self.read_immediate_from_mplace_raw(mplace)? {
Right(val) Right(val)
@ -608,7 +582,7 @@ pub fn read_immediate_raw(
#[inline(always)] #[inline(always)]
pub fn read_immediate( pub fn read_immediate(
&self, &self,
op: &impl Readable<'tcx, M::Provenance>, op: &impl Projectable<'tcx, M::Provenance>,
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> { ) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
if !matches!( if !matches!(
op.layout().abi, op.layout().abi,
@ -627,7 +601,7 @@ pub fn read_immediate(
/// Read a scalar from a place /// Read a scalar from a place
pub fn read_scalar( pub fn read_scalar(
&self, &self,
op: &impl Readable<'tcx, M::Provenance>, op: &impl Projectable<'tcx, M::Provenance>,
) -> InterpResult<'tcx, Scalar<M::Provenance>> { ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
Ok(self.read_immediate(op)?.to_scalar()) Ok(self.read_immediate(op)?.to_scalar())
} }
@ -638,21 +612,21 @@ pub fn read_scalar(
/// Read a pointer from a place. /// Read a pointer from a place.
pub fn read_pointer( pub fn read_pointer(
&self, &self,
op: &impl Readable<'tcx, M::Provenance>, op: &impl Projectable<'tcx, M::Provenance>,
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> { ) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
self.read_scalar(op)?.to_pointer(self) self.read_scalar(op)?.to_pointer(self)
} }
/// Read a pointer-sized unsigned integer from a place. /// Read a pointer-sized unsigned integer from a place.
pub fn read_target_usize( pub fn read_target_usize(
&self, &self,
op: &impl Readable<'tcx, M::Provenance>, op: &impl Projectable<'tcx, M::Provenance>,
) -> InterpResult<'tcx, u64> { ) -> InterpResult<'tcx, u64> {
self.read_scalar(op)?.to_target_usize(self) self.read_scalar(op)?.to_target_usize(self)
} }
/// Read a pointer-sized signed integer from a place. /// Read a pointer-sized signed integer from a place.
pub fn read_target_isize( pub fn read_target_isize(
&self, &self,
op: &impl Readable<'tcx, M::Provenance>, op: &impl Projectable<'tcx, M::Provenance>,
) -> InterpResult<'tcx, i64> { ) -> InterpResult<'tcx, i64> {
self.read_scalar(op)?.to_target_isize(self) self.read_scalar(op)?.to_target_isize(self)
} }

View File

@ -15,7 +15,7 @@
use super::{ use super::{
alloc_range, mir_assign_valid_types, AllocRef, AllocRefMut, CheckAlignMsg, CtfeProvenance, alloc_range, mir_assign_valid_types, AllocRef, AllocRefMut, CheckAlignMsg, CtfeProvenance,
ImmTy, Immediate, InterpCx, InterpResult, Machine, MemoryKind, Misalignment, OffsetMode, OpTy, ImmTy, Immediate, InterpCx, InterpResult, Machine, MemoryKind, Misalignment, OffsetMode, OpTy,
Operand, Pointer, Projectable, Provenance, Readable, Scalar, Operand, Pointer, Projectable, Provenance, Scalar,
}; };
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)] #[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
@ -436,7 +436,7 @@ pub fn mplace_to_ref(
#[instrument(skip(self), level = "trace")] #[instrument(skip(self), level = "trace")]
pub fn deref_pointer( pub fn deref_pointer(
&self, &self,
src: &impl Readable<'tcx, M::Provenance>, src: &impl Projectable<'tcx, M::Provenance>,
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> { ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
if src.layout().ty.is_box() { if src.layout().ty.is_box() {
// Derefer should have removed all Box derefs. // Derefer should have removed all Box derefs.
@ -768,7 +768,7 @@ pub fn write_uninit(
#[inline(always)] #[inline(always)]
pub(super) fn copy_op_no_dest_validation( pub(super) fn copy_op_no_dest_validation(
&mut self, &mut self,
src: &impl Readable<'tcx, M::Provenance>, src: &impl Projectable<'tcx, M::Provenance>,
dest: &impl Writeable<'tcx, M::Provenance>, dest: &impl Writeable<'tcx, M::Provenance>,
) -> InterpResult<'tcx> { ) -> InterpResult<'tcx> {
self.copy_op_inner( self.copy_op_inner(
@ -781,7 +781,7 @@ pub(super) fn copy_op_no_dest_validation(
#[inline(always)] #[inline(always)]
pub fn copy_op_allow_transmute( pub fn copy_op_allow_transmute(
&mut self, &mut self,
src: &impl Readable<'tcx, M::Provenance>, src: &impl Projectable<'tcx, M::Provenance>,
dest: &impl Writeable<'tcx, M::Provenance>, dest: &impl Writeable<'tcx, M::Provenance>,
) -> InterpResult<'tcx> { ) -> InterpResult<'tcx> {
self.copy_op_inner( self.copy_op_inner(
@ -794,7 +794,7 @@ pub fn copy_op_allow_transmute(
#[inline(always)] #[inline(always)]
pub fn copy_op( pub fn copy_op(
&mut self, &mut self,
src: &impl Readable<'tcx, M::Provenance>, src: &impl Projectable<'tcx, M::Provenance>,
dest: &impl Writeable<'tcx, M::Provenance>, dest: &impl Writeable<'tcx, M::Provenance>,
) -> InterpResult<'tcx> { ) -> InterpResult<'tcx> {
self.copy_op_inner( self.copy_op_inner(
@ -808,7 +808,7 @@ pub fn copy_op(
#[instrument(skip(self), level = "trace")] #[instrument(skip(self), level = "trace")]
fn copy_op_inner( fn copy_op_inner(
&mut self, &mut self,
src: &impl Readable<'tcx, M::Provenance>, src: &impl Projectable<'tcx, M::Provenance>,
dest: &impl Writeable<'tcx, M::Provenance>, dest: &impl Writeable<'tcx, M::Provenance>,
allow_transmute: bool, allow_transmute: bool,
validate_dest: bool, validate_dest: bool,
@ -843,7 +843,7 @@ fn copy_op_inner(
#[instrument(skip(self), level = "trace")] #[instrument(skip(self), level = "trace")]
fn copy_op_no_validate( fn copy_op_no_validate(
&mut self, &mut self,
src: &impl Readable<'tcx, M::Provenance>, src: &impl Projectable<'tcx, M::Provenance>,
dest: &impl Writeable<'tcx, M::Provenance>, dest: &impl Writeable<'tcx, M::Provenance>,
allow_transmute: bool, allow_transmute: bool,
) -> InterpResult<'tcx> { ) -> InterpResult<'tcx> {

View File

@ -869,7 +869,7 @@ fn try_unwrap_io_result<T: From<i32>>(
/// Dereference a pointer operand to a place using `layout` instead of the pointer's declared type /// Dereference a pointer operand to a place using `layout` instead of the pointer's declared type
fn deref_pointer_as( fn deref_pointer_as(
&self, &self,
op: &impl Readable<'tcx, Provenance>, op: &impl Projectable<'tcx, Provenance>,
layout: TyAndLayout<'tcx>, layout: TyAndLayout<'tcx>,
) -> InterpResult<'tcx, MPlaceTy<'tcx>> { ) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
let this = self.eval_context_ref(); let this = self.eval_context_ref();
@ -880,7 +880,7 @@ fn deref_pointer_as(
/// Calculates the MPlaceTy given the offset and layout of an access on an operand /// Calculates the MPlaceTy given the offset and layout of an access on an operand
fn deref_pointer_and_offset( fn deref_pointer_and_offset(
&self, &self,
op: &impl Readable<'tcx, Provenance>, op: &impl Projectable<'tcx, Provenance>,
offset: u64, offset: u64,
base_layout: TyAndLayout<'tcx>, base_layout: TyAndLayout<'tcx>,
value_layout: TyAndLayout<'tcx>, value_layout: TyAndLayout<'tcx>,
@ -897,7 +897,7 @@ fn deref_pointer_and_offset(
fn deref_pointer_and_read( fn deref_pointer_and_read(
&self, &self,
op: &impl Readable<'tcx, Provenance>, op: &impl Projectable<'tcx, Provenance>,
offset: u64, offset: u64,
base_layout: TyAndLayout<'tcx>, base_layout: TyAndLayout<'tcx>,
value_layout: TyAndLayout<'tcx>, value_layout: TyAndLayout<'tcx>,
@ -909,7 +909,7 @@ fn deref_pointer_and_read(
fn deref_pointer_and_write( fn deref_pointer_and_write(
&mut self, &mut self,
op: &impl Readable<'tcx, Provenance>, op: &impl Projectable<'tcx, Provenance>,
offset: u64, offset: u64,
value: impl Into<Scalar>, value: impl Into<Scalar>,
base_layout: TyAndLayout<'tcx>, base_layout: TyAndLayout<'tcx>,