1
Fork 0

Rollup merge of #70508 - RalfJung:scalar-from, r=eddyb

Miri: use more specialized Scalar::from_ constructors where appropriate
This commit is contained in:
Dylan DPC 2020-03-29 01:32:22 +01:00 committed by GitHub
commit acc1dc2906
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 19 deletions

View file

@ -208,7 +208,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
Char => { Char => {
// `u8` to `char` cast // `u8` to `char` cast
Ok(Scalar::from_uint(u8::try_from(v).unwrap(), Size::from_bytes(4))) Ok(Scalar::from_u32(u8::try_from(v).unwrap().into()))
} }
// Casts to bool are not permitted by rustc, no need to handle them here. // Casts to bool are not permitted by rustc, no need to handle them here.

View file

@ -2,6 +2,8 @@
//! looking at their MIR. Intrinsics/functions supported here are shared by CTFE //! looking at their MIR. Intrinsics/functions supported here are shared by CTFE
//! and miri. //! and miri.
use std::convert::TryFrom;
use rustc::mir::{ use rustc::mir::{
self, self,
interpret::{ConstValue, GlobalId, InterpResult, Scalar}, interpret::{ConstValue, GlobalId, InterpResult, Scalar},
@ -220,7 +222,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
sym::discriminant_value => { sym::discriminant_value => {
let place = self.deref_operand(args[0])?; let place = self.deref_operand(args[0])?;
let discr_val = self.read_discriminant(place.into())?.0; let discr_val = self.read_discriminant(place.into())?.0;
self.write_scalar(Scalar::from_uint(discr_val, dest.layout.size), dest)?; self.write_scalar(Scalar::from_u64(u64::try_from(discr_val).unwrap()), dest)?;
} }
sym::unchecked_shl sym::unchecked_shl
| sym::unchecked_shr | sym::unchecked_shr
@ -275,7 +277,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
} }
sym::ptr_offset_from => { sym::ptr_offset_from => {
let isize_layout = self.layout_of(self.tcx.types.isize)?;
let a = self.read_immediate(args[0])?.to_scalar()?; let a = self.read_immediate(args[0])?.to_scalar()?;
let b = self.read_immediate(args[1])?.to_scalar()?; let b = self.read_immediate(args[1])?.to_scalar()?;
@ -292,7 +293,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let a = a.to_machine_usize(self)?; let a = a.to_machine_usize(self)?;
let b = b.to_machine_usize(self)?; let b = b.to_machine_usize(self)?;
if a == b && a != 0 { if a == b && a != 0 {
self.write_scalar(Scalar::from_int(0, isize_layout.size), dest)?; self.write_scalar(Scalar::from_machine_isize(0, self), dest)?;
true true
} else { } else {
false false
@ -312,6 +313,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
); );
} }
let usize_layout = self.layout_of(self.tcx.types.usize)?; let usize_layout = self.layout_of(self.tcx.types.usize)?;
let isize_layout = self.layout_of(self.tcx.types.isize)?;
let a_offset = ImmTy::from_uint(a.offset.bytes(), usize_layout); let a_offset = ImmTy::from_uint(a.offset.bytes(), usize_layout);
let b_offset = ImmTy::from_uint(b.offset.bytes(), usize_layout); let b_offset = ImmTy::from_uint(b.offset.bytes(), usize_layout);
let (val, _overflowed, _ty) = let (val, _overflowed, _ty) =

View file

@ -54,10 +54,7 @@ impl<Tag> From<Pointer<Tag>> for Immediate<Tag> {
impl<'tcx, Tag> Immediate<Tag> { impl<'tcx, Tag> Immediate<Tag> {
pub fn new_slice(val: Scalar<Tag>, len: u64, cx: &impl HasDataLayout) -> Self { pub fn new_slice(val: Scalar<Tag>, len: u64, cx: &impl HasDataLayout) -> Self {
Immediate::ScalarPair( Immediate::ScalarPair(val.into(), Scalar::from_machine_usize(len, cx).into())
val.into(),
Scalar::from_uint(len, cx.data_layout().pointer_size).into(),
)
} }
pub fn new_dyn_trait(val: Scalar<Tag>, vtable: Pointer<Tag>) -> Self { pub fn new_dyn_trait(val: Scalar<Tag>, vtable: Pointer<Tag>) -> Self {
@ -621,7 +618,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let real_discr = if discr_val.layout.abi.is_signed() { let real_discr = if discr_val.layout.abi.is_signed() {
// going from layout tag type to typeck discriminant type // going from layout tag type to typeck discriminant type
// requires first sign extending with the discriminant layout // requires first sign extending with the discriminant layout
let sexted = sign_extend(bits_discr, discr_val.layout.size) as i128; let sexted = sign_extend(bits_discr, discr_val.layout.size);
// and then zeroing with the typeck discriminant type // and then zeroing with the typeck discriminant type
let discr_ty = rval let discr_ty = rval
.layout .layout
@ -631,8 +628,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
.repr .repr
.discr_type(); .discr_type();
let size = layout::Integer::from_attr(self, discr_ty).size(); let size = layout::Integer::from_attr(self, discr_ty).size();
let truncatee = sexted as u128; truncate(sexted, size)
truncate(truncatee, size)
} else { } else {
bits_discr bits_discr
}; };

View file

@ -180,7 +180,7 @@ impl<'tcx, Tag> MPlaceTy<'tcx, Tag> {
#[inline] #[inline]
pub fn dangling(layout: TyLayout<'tcx>, cx: &impl HasDataLayout) -> Self { pub fn dangling(layout: TyLayout<'tcx>, cx: &impl HasDataLayout) -> Self {
let align = layout.align.abi; let align = layout.align.abi;
let ptr = Scalar::from_uint(align.bytes(), cx.pointer_size()); let ptr = Scalar::from_machine_usize(align.bytes(), cx);
// `Poison` this to make sure that the pointer value `ptr` is never observable by the program. // `Poison` this to make sure that the pointer value `ptr` is never observable by the program.
MPlaceTy { mplace: MemPlace { ptr, align, meta: MemPlaceMeta::Poison }, layout } MPlaceTy { mplace: MemPlace { ptr, align, meta: MemPlaceMeta::Poison }, layout }
} }
@ -504,7 +504,7 @@ where
// implement this. // implement this.
ty::Array(inner, _) => (MemPlaceMeta::None, self.tcx.mk_array(inner, inner_len)), ty::Array(inner, _) => (MemPlaceMeta::None, self.tcx.mk_array(inner, inner_len)),
ty::Slice(..) => { ty::Slice(..) => {
let len = Scalar::from_uint(inner_len, self.pointer_size()); let len = Scalar::from_machine_usize(inner_len, self);
(MemPlaceMeta::Meta(len), base.layout.ty) (MemPlaceMeta::Meta(len), base.layout.ty)
} }
_ => bug!("cannot subslice non-array type: `{:?}`", base.layout.ty), _ => bug!("cannot subslice non-array type: `{:?}`", base.layout.ty),
@ -1044,7 +1044,7 @@ where
kind: MemoryKind<M::MemoryKind>, kind: MemoryKind<M::MemoryKind>,
) -> MPlaceTy<'tcx, M::PointerTag> { ) -> MPlaceTy<'tcx, M::PointerTag> {
let ptr = self.memory.allocate_bytes(str.as_bytes(), kind); let ptr = self.memory.allocate_bytes(str.as_bytes(), kind);
let meta = Scalar::from_uint(u128::try_from(str.len()).unwrap(), self.pointer_size()); let meta = Scalar::from_machine_usize(u64::try_from(str.len()).unwrap(), self);
let mplace = MemPlace { let mplace = MemPlace {
ptr: ptr.into(), ptr: ptr.into(),
align: Align::from_bytes(1).unwrap(), align: Align::from_bytes(1).unwrap(),

View file

@ -3,7 +3,7 @@
//! The main entry point is the `step` method. //! The main entry point is the `step` method.
use rustc::mir; use rustc::mir;
use rustc::mir::interpret::{InterpResult, PointerArithmetic, Scalar}; use rustc::mir::interpret::{InterpResult, Scalar};
use rustc::ty::layout::LayoutOf; use rustc::ty::layout::LayoutOf;
use super::{InterpCx, Machine}; use super::{InterpCx, Machine};
@ -229,8 +229,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let src = self.eval_place(place)?; let src = self.eval_place(place)?;
let mplace = self.force_allocation(src)?; let mplace = self.force_allocation(src)?;
let len = mplace.len(self)?; let len = mplace.len(self)?;
let size = self.pointer_size(); self.write_scalar(Scalar::from_machine_usize(len, self), dest)?;
self.write_scalar(Scalar::from_uint(len, size), dest)?;
} }
AddressOf(_, ref place) | Ref(_, _, ref place) => { AddressOf(_, ref place) | Ref(_, _, ref place) => {
@ -254,8 +253,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
!layout.is_unsized(), !layout.is_unsized(),
"SizeOf nullary MIR operator called for unsized type" "SizeOf nullary MIR operator called for unsized type"
); );
let size = self.pointer_size(); self.write_scalar(Scalar::from_machine_usize(layout.size.bytes(), self), dest)?;
self.write_scalar(Scalar::from_uint(layout.size.bytes(), size), dest)?;
} }
Cast(kind, ref operand, _) => { Cast(kind, ref operand, _) => {