Skip to content

Commit 3bd1734

Browse files
committed
Add force_bits and force_ptr methods
1 parent 374c63e commit 3bd1734

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/librustc_mir/interpret/eval_context.rs

+13
Original file line numberDiff line numberDiff line change
@@ -765,4 +765,17 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
765765
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
766766
truncate(value, ty.size)
767767
}
768+
769+
#[inline(always)]
770+
pub fn force_ptr(
771+
&self,
772+
scalar: Scalar<M::PointerTag>,
773+
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
774+
self.memory.force_ptr(scalar)
775+
}
776+
777+
#[inline(always)]
778+
pub fn force_bits(&self, scalar: Scalar<M::PointerTag>) -> InterpResult<'tcx, u128> {
779+
self.memory.force_bits(scalar)
780+
}
768781
}

src/librustc_mir/interpret/machine.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use rustc::ty::{self, query::TyCtxtAt};
1111

1212
use super::{
1313
Allocation, AllocId, InterpResult, Scalar, AllocationExtra,
14-
InterpretCx, PlaceTy, OpTy, ImmTy, MemoryKind,
14+
InterpretCx, PlaceTy, OpTy, ImmTy, MemoryKind, Pointer,
15+
InterpErrorInfo, InterpError
1516
};
1617

1718
/// Whether this kind of memory is allowed to leak
@@ -208,4 +209,18 @@ pub trait Machine<'mir, 'tcx>: Sized {
208209
ecx: &mut InterpretCx<'mir, 'tcx, Self>,
209210
extra: Self::FrameExtra,
210211
) -> InterpResult<'tcx>;
212+
213+
fn int_to_ptr(
214+
_int: u64,
215+
_extra: &Self::MemoryExtra,
216+
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
217+
Err(InterpErrorInfo::from(InterpError::ReadBytesAsPointer))
218+
}
219+
220+
fn ptr_to_int(
221+
_ptr: Pointer<Self::PointerTag>,
222+
_extra: &Self::MemoryExtra,
223+
) -> InterpResult<'tcx, u64> {
224+
Err(InterpErrorInfo::from(InterpError::ReadPointerAsBytes))
225+
}
211226
}

src/librustc_mir/interpret/memory.rs

+17
Original file line numberDiff line numberDiff line change
@@ -874,4 +874,21 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
874874
}
875875
Ok(())
876876
}
877+
878+
pub fn force_ptr(
879+
&self,
880+
scalar: Scalar<M::PointerTag>,
881+
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
882+
match scalar {
883+
Scalar::Ptr(ptr) => Ok(ptr),
884+
_ => M::int_to_ptr(scalar.to_usize(self)?, &self.extra)
885+
}
886+
}
887+
888+
pub fn force_bits(&self, scalar: Scalar<M::PointerTag>) -> InterpResult<'tcx, u128> {
889+
match scalar.to_bits_or_ptr(self.pointer_size(), self) {
890+
Ok(bits) => Ok(bits),
891+
Err(ptr) => Ok(M::ptr_to_int(ptr, &self.extra)? as u128)
892+
}
893+
}
877894
}

0 commit comments

Comments
 (0)