Small refactorings for miri.

This commit is contained in:
Charles Lew 2021-08-01 20:09:22 +08:00
parent 63ed625313
commit a1cff1cd49
2 changed files with 17 additions and 18 deletions

View file

@ -63,15 +63,19 @@ impl<'tcx, Tag: Provenance> Immediate<Tag> {
Immediate::ScalarPair(val.into(), Scalar::from_machine_usize(len, cx).into()) Immediate::ScalarPair(val.into(), Scalar::from_machine_usize(len, cx).into())
} }
pub fn new_dyn_trait(val: Scalar<Tag>, vtable: Pointer<Tag>, cx: &impl HasDataLayout) -> Self { pub fn new_dyn_trait(
Immediate::ScalarPair(val.into(), ScalarMaybeUninit::from_pointer(vtable, cx)) val: Scalar<Tag>,
vtable: Pointer<Option<Tag>>,
cx: &impl HasDataLayout,
) -> Self {
Immediate::ScalarPair(val.into(), ScalarMaybeUninit::from_maybe_pointer(vtable, cx))
} }
#[inline] #[inline]
pub fn to_scalar_or_uninit(self) -> ScalarMaybeUninit<Tag> { pub fn to_scalar_or_uninit(self) -> ScalarMaybeUninit<Tag> {
match self { match self {
Immediate::Scalar(val) => val, Immediate::Scalar(val) => val,
Immediate::ScalarPair(..) => bug!("Got a wide pointer where a scalar was expected"), Immediate::ScalarPair(..) => bug!("Got a scalar pair where a scalar was expected"),
} }
} }
@ -85,7 +89,7 @@ impl<'tcx, Tag: Provenance> Immediate<Tag> {
match self { match self {
Immediate::ScalarPair(val1, val2) => Ok((val1.check_init()?, val2.check_init()?)), Immediate::ScalarPair(val1, val2) => Ok((val1.check_init()?, val2.check_init()?)),
Immediate::Scalar(..) => { Immediate::Scalar(..) => {
bug!("Got a scalar where a wide pointer was expected") bug!("Got a scalar where a scalar pair was expected")
} }
} }
} }

View file

@ -21,7 +21,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
&mut self, &mut self,
ty: Ty<'tcx>, ty: Ty<'tcx>,
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>, poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
) -> InterpResult<'tcx, Pointer<M::PointerTag>> { ) -> InterpResult<'tcx, Pointer<Option<M::PointerTag>>> {
trace!("get_vtable(trait_ref={:?})", poly_trait_ref); trace!("get_vtable(trait_ref={:?})", poly_trait_ref);
let (ty, poly_trait_ref) = self.tcx.erase_regions((ty, poly_trait_ref)); let (ty, poly_trait_ref) = self.tcx.erase_regions((ty, poly_trait_ref));
@ -34,7 +34,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_allocation))?; let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_allocation))?;
Ok(vtable_ptr) Ok(vtable_ptr.into())
} }
/// Resolves the function at the specified slot in the provided /// Resolves the function at the specified slot in the provided
@ -126,21 +126,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
&self, &self,
vtable: Pointer<Option<M::PointerTag>>, vtable: Pointer<Option<M::PointerTag>>,
idx: u64, idx: u64,
) -> InterpResult<'tcx, Pointer<M::PointerTag>> { ) -> InterpResult<'tcx, Pointer<Option<M::PointerTag>>> {
let pointer_size = self.pointer_size(); let pointer_size = self.pointer_size();
let vtable = self let vtable_slot = vtable.offset(pointer_size * idx, self)?;
.memory
.get(
vtable,
pointer_size * idx.checked_add(1).unwrap(),
self.tcx.data_layout.pointer_align.abi,
)?
.expect("cannot be a ZST");
let new_vtable = self let new_vtable = self
.scalar_to_ptr(vtable.read_ptr_sized(pointer_size * idx)?.check_init()?) .memory
.into_pointer_or_addr() .get(vtable_slot, pointer_size, self.tcx.data_layout.pointer_align.abi)?
.expect("should be a pointer"); .expect("cannot be a ZST");
let new_vtable = self.scalar_to_ptr(new_vtable.read_ptr_sized(Size::ZERO)?.check_init()?);
Ok(new_vtable) Ok(new_vtable)
} }