Skip to content

Commit 67f6ac4

Browse files
committed
Add force_bits and force_ptr methods
1 parent 3d7a1c9 commit 67f6ac4

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/librustc_mir/interpret/eval_context.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,4 +773,17 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tc
773773
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
774774
truncate(value, ty.size)
775775
}
776+
777+
#[inline(always)]
778+
pub fn force_ptr(
779+
&self,
780+
scalar: Scalar<M::PointerTag>,
781+
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
782+
self.memory.force_ptr(scalar)
783+
}
784+
785+
#[inline(always)]
786+
pub fn force_bits(&self, scalar: Scalar<M::PointerTag>) -> InterpResult<'tcx, u128> {
787+
self.memory.force_bits(scalar)
788+
}
776789
}

src/librustc_mir/interpret/machine.rs

Lines changed: 16 additions & 1 deletion
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
@@ -210,4 +211,18 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
210211
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
211212
extra: Self::FrameExtra,
212213
) -> InterpResult<'tcx>;
214+
215+
fn int_to_ptr(
216+
_int: u64,
217+
_extra: &Self::MemoryExtra,
218+
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
219+
Err(InterpErrorInfo::from(InterpError::ReadBytesAsPointer))
220+
}
221+
222+
fn ptr_to_int(
223+
_ptr: Pointer<Self::PointerTag>,
224+
_extra: &Self::MemoryExtra,
225+
) -> InterpResult<'tcx, u64> {
226+
Err(InterpErrorInfo::from(InterpError::ReadPointerAsBytes))
227+
}
213228
}

src/librustc_mir/interpret/memory.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,4 +879,21 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
879879
}
880880
Ok(())
881881
}
882+
883+
pub fn force_ptr(
884+
&self,
885+
scalar: Scalar<M::PointerTag>,
886+
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
887+
match scalar {
888+
Scalar::Ptr(ptr) => Ok(ptr),
889+
_ => M::int_to_ptr(scalar.to_usize(self)?, &self.extra)
890+
}
891+
}
892+
893+
pub fn force_bits(&self, scalar: Scalar<M::PointerTag>) -> InterpResult<'tcx, u128> {
894+
match scalar.to_bits_or_ptr(self.pointer_size(), self) {
895+
Ok(bits) => Ok(bits),
896+
Err(ptr) => Ok(M::ptr_to_int(ptr, &self.extra)? as u128)
897+
}
898+
}
882899
}

0 commit comments

Comments
 (0)