try to avoid some layout_of calls
This commit is contained in:
parent
23fd2860fa
commit
0eff07b748
@ -24,40 +24,43 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
cast_ty: Ty<'tcx>,
|
cast_ty: Ty<'tcx>,
|
||||||
dest: &PlaceTy<'tcx, M::Provenance>,
|
dest: &PlaceTy<'tcx, M::Provenance>,
|
||||||
) -> InterpResult<'tcx> {
|
) -> InterpResult<'tcx> {
|
||||||
|
// `cast_ty` will often be the same as `dest.ty`, but not always, since subtyping is still
|
||||||
|
// possible.
|
||||||
|
let cast_layout =
|
||||||
|
if cast_ty == dest.layout.ty { dest.layout } else { self.layout_of(cast_ty)? };
|
||||||
// FIXME: In which cases should we trigger UB when the source is uninit?
|
// FIXME: In which cases should we trigger UB when the source is uninit?
|
||||||
match cast_kind {
|
match cast_kind {
|
||||||
CastKind::PointerCoercion(PointerCoercion::Unsize) => {
|
CastKind::PointerCoercion(PointerCoercion::Unsize) => {
|
||||||
let cast_ty = self.layout_of(cast_ty)?;
|
self.unsize_into(src, cast_layout, dest)?;
|
||||||
self.unsize_into(src, cast_ty, dest)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CastKind::PointerExposeAddress => {
|
CastKind::PointerExposeAddress => {
|
||||||
let src = self.read_immediate(src)?;
|
let src = self.read_immediate(src)?;
|
||||||
let res = self.pointer_expose_address_cast(&src, cast_ty)?;
|
let res = self.pointer_expose_address_cast(&src, cast_layout)?;
|
||||||
self.write_immediate(*res, dest)?;
|
self.write_immediate(*res, dest)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
CastKind::PointerFromExposedAddress => {
|
CastKind::PointerFromExposedAddress => {
|
||||||
let src = self.read_immediate(src)?;
|
let src = self.read_immediate(src)?;
|
||||||
let res = self.pointer_from_exposed_address_cast(&src, cast_ty)?;
|
let res = self.pointer_from_exposed_address_cast(&src, cast_layout)?;
|
||||||
self.write_immediate(*res, dest)?;
|
self.write_immediate(*res, dest)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
CastKind::IntToInt | CastKind::IntToFloat => {
|
CastKind::IntToInt | CastKind::IntToFloat => {
|
||||||
let src = self.read_immediate(src)?;
|
let src = self.read_immediate(src)?;
|
||||||
let res = self.int_to_int_or_float(&src, cast_ty)?;
|
let res = self.int_to_int_or_float(&src, cast_layout)?;
|
||||||
self.write_immediate(*res, dest)?;
|
self.write_immediate(*res, dest)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
CastKind::FloatToFloat | CastKind::FloatToInt => {
|
CastKind::FloatToFloat | CastKind::FloatToInt => {
|
||||||
let src = self.read_immediate(src)?;
|
let src = self.read_immediate(src)?;
|
||||||
let res = self.float_to_float_or_int(&src, cast_ty)?;
|
let res = self.float_to_float_or_int(&src, cast_layout)?;
|
||||||
self.write_immediate(*res, dest)?;
|
self.write_immediate(*res, dest)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
|
CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
|
||||||
let src = self.read_immediate(src)?;
|
let src = self.read_immediate(src)?;
|
||||||
let res = self.ptr_to_ptr(&src, cast_ty)?;
|
let res = self.ptr_to_ptr(&src, cast_layout)?;
|
||||||
self.write_immediate(*res, dest)?;
|
self.write_immediate(*res, dest)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,6 +143,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
CastKind::Transmute => {
|
CastKind::Transmute => {
|
||||||
assert!(src.layout.is_sized());
|
assert!(src.layout.is_sized());
|
||||||
assert!(dest.layout.is_sized());
|
assert!(dest.layout.is_sized());
|
||||||
|
assert_eq!(cast_ty, dest.layout.ty); // we otherwise ignore `cast_ty` enirely...
|
||||||
if src.layout.size != dest.layout.size {
|
if src.layout.size != dest.layout.size {
|
||||||
let src_bytes = src.layout.size.bytes();
|
let src_bytes = src.layout.size.bytes();
|
||||||
let dest_bytes = dest.layout.size.bytes();
|
let dest_bytes = dest.layout.size.bytes();
|
||||||
@ -164,15 +168,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
pub fn int_to_int_or_float(
|
pub fn int_to_int_or_float(
|
||||||
&self,
|
&self,
|
||||||
src: &ImmTy<'tcx, M::Provenance>,
|
src: &ImmTy<'tcx, M::Provenance>,
|
||||||
cast_ty: Ty<'tcx>,
|
cast_to: TyAndLayout<'tcx>,
|
||||||
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
||||||
assert!(src.layout.ty.is_integral() || src.layout.ty.is_char() || src.layout.ty.is_bool());
|
assert!(src.layout.ty.is_integral() || src.layout.ty.is_char() || src.layout.ty.is_bool());
|
||||||
assert!(cast_ty.is_floating_point() || cast_ty.is_integral() || cast_ty.is_char());
|
assert!(cast_to.ty.is_floating_point() || cast_to.ty.is_integral() || cast_to.ty.is_char());
|
||||||
|
|
||||||
let layout = self.layout_of(cast_ty)?;
|
|
||||||
Ok(ImmTy::from_scalar(
|
Ok(ImmTy::from_scalar(
|
||||||
self.cast_from_int_like(src.to_scalar(), src.layout, cast_ty)?,
|
self.cast_from_int_like(src.to_scalar(), src.layout, cast_to.ty)?,
|
||||||
layout,
|
cast_to,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,48 +183,46 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
pub fn float_to_float_or_int(
|
pub fn float_to_float_or_int(
|
||||||
&self,
|
&self,
|
||||||
src: &ImmTy<'tcx, M::Provenance>,
|
src: &ImmTy<'tcx, M::Provenance>,
|
||||||
cast_ty: Ty<'tcx>,
|
cast_to: TyAndLayout<'tcx>,
|
||||||
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
||||||
use rustc_type_ir::sty::TyKind::*;
|
use rustc_type_ir::sty::TyKind::*;
|
||||||
|
|
||||||
let layout = self.layout_of(cast_ty)?;
|
|
||||||
let val = match src.layout.ty.kind() {
|
let val = match src.layout.ty.kind() {
|
||||||
// Floating point
|
// Floating point
|
||||||
Float(FloatTy::F32) => self.cast_from_float(src.to_scalar().to_f32()?, cast_ty),
|
Float(FloatTy::F32) => self.cast_from_float(src.to_scalar().to_f32()?, cast_to.ty),
|
||||||
Float(FloatTy::F64) => self.cast_from_float(src.to_scalar().to_f64()?, cast_ty),
|
Float(FloatTy::F64) => self.cast_from_float(src.to_scalar().to_f64()?, cast_to.ty),
|
||||||
_ => {
|
_ => {
|
||||||
bug!("Can't cast 'Float' type into {}", cast_ty);
|
bug!("Can't cast 'Float' type into {}", cast_to.ty);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(ImmTy::from_scalar(val, layout))
|
Ok(ImmTy::from_scalar(val, cast_to))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles 'FnPtrToPtr' and 'PtrToPtr' casts.
|
/// Handles 'FnPtrToPtr' and 'PtrToPtr' casts.
|
||||||
pub fn ptr_to_ptr(
|
pub fn ptr_to_ptr(
|
||||||
&self,
|
&self,
|
||||||
src: &ImmTy<'tcx, M::Provenance>,
|
src: &ImmTy<'tcx, M::Provenance>,
|
||||||
cast_ty: Ty<'tcx>,
|
cast_to: TyAndLayout<'tcx>,
|
||||||
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
||||||
assert!(src.layout.ty.is_any_ptr());
|
assert!(src.layout.ty.is_any_ptr());
|
||||||
assert!(cast_ty.is_unsafe_ptr());
|
assert!(cast_to.ty.is_unsafe_ptr());
|
||||||
// Handle casting any ptr to raw ptr (might be a fat ptr).
|
// Handle casting any ptr to raw ptr (might be a fat ptr).
|
||||||
let dest_layout = self.layout_of(cast_ty)?;
|
if cast_to.size == src.layout.size {
|
||||||
if dest_layout.size == src.layout.size {
|
|
||||||
// Thin or fat pointer that just hast the ptr kind of target type changed.
|
// Thin or fat pointer that just hast the ptr kind of target type changed.
|
||||||
return Ok(ImmTy::from_immediate(**src, dest_layout));
|
return Ok(ImmTy::from_immediate(**src, cast_to));
|
||||||
} else {
|
} else {
|
||||||
// Casting the metadata away from a fat ptr.
|
// Casting the metadata away from a fat ptr.
|
||||||
assert_eq!(src.layout.size, 2 * self.pointer_size());
|
assert_eq!(src.layout.size, 2 * self.pointer_size());
|
||||||
assert_eq!(dest_layout.size, self.pointer_size());
|
assert_eq!(cast_to.size, self.pointer_size());
|
||||||
assert!(src.layout.ty.is_unsafe_ptr());
|
assert!(src.layout.ty.is_unsafe_ptr());
|
||||||
return match **src {
|
return match **src {
|
||||||
Immediate::ScalarPair(data, _) => Ok(ImmTy::from_scalar(data, dest_layout)),
|
Immediate::ScalarPair(data, _) => Ok(ImmTy::from_scalar(data, cast_to)),
|
||||||
Immediate::Scalar(..) => span_bug!(
|
Immediate::Scalar(..) => span_bug!(
|
||||||
self.cur_span(),
|
self.cur_span(),
|
||||||
"{:?} input to a fat-to-thin cast ({} -> {})",
|
"{:?} input to a fat-to-thin cast ({} -> {})",
|
||||||
*src,
|
*src,
|
||||||
src.layout.ty,
|
src.layout.ty,
|
||||||
cast_ty
|
cast_to.ty
|
||||||
),
|
),
|
||||||
Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)),
|
Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)),
|
||||||
};
|
};
|
||||||
@ -231,10 +232,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
pub fn pointer_expose_address_cast(
|
pub fn pointer_expose_address_cast(
|
||||||
&mut self,
|
&mut self,
|
||||||
src: &ImmTy<'tcx, M::Provenance>,
|
src: &ImmTy<'tcx, M::Provenance>,
|
||||||
cast_ty: Ty<'tcx>,
|
cast_to: TyAndLayout<'tcx>,
|
||||||
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
||||||
assert_matches!(src.layout.ty.kind(), ty::RawPtr(_) | ty::FnPtr(_));
|
assert_matches!(src.layout.ty.kind(), ty::RawPtr(_) | ty::FnPtr(_));
|
||||||
assert!(cast_ty.is_integral());
|
assert!(cast_to.ty.is_integral());
|
||||||
|
|
||||||
let scalar = src.to_scalar();
|
let scalar = src.to_scalar();
|
||||||
let ptr = scalar.to_pointer(self)?;
|
let ptr = scalar.to_pointer(self)?;
|
||||||
@ -242,17 +243,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
Ok(ptr) => M::expose_ptr(self, ptr)?,
|
Ok(ptr) => M::expose_ptr(self, ptr)?,
|
||||||
Err(_) => {} // Do nothing, exposing an invalid pointer (`None` provenance) is a NOP.
|
Err(_) => {} // Do nothing, exposing an invalid pointer (`None` provenance) is a NOP.
|
||||||
};
|
};
|
||||||
let layout = self.layout_of(cast_ty)?;
|
Ok(ImmTy::from_scalar(self.cast_from_int_like(scalar, src.layout, cast_to.ty)?, cast_to))
|
||||||
Ok(ImmTy::from_scalar(self.cast_from_int_like(scalar, src.layout, cast_ty)?, layout))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pointer_from_exposed_address_cast(
|
pub fn pointer_from_exposed_address_cast(
|
||||||
&self,
|
&self,
|
||||||
src: &ImmTy<'tcx, M::Provenance>,
|
src: &ImmTy<'tcx, M::Provenance>,
|
||||||
cast_ty: Ty<'tcx>,
|
cast_to: TyAndLayout<'tcx>,
|
||||||
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
||||||
assert!(src.layout.ty.is_integral());
|
assert!(src.layout.ty.is_integral());
|
||||||
assert_matches!(cast_ty.kind(), ty::RawPtr(_));
|
assert_matches!(cast_to.ty.kind(), ty::RawPtr(_));
|
||||||
|
|
||||||
// First cast to usize.
|
// First cast to usize.
|
||||||
let scalar = src.to_scalar();
|
let scalar = src.to_scalar();
|
||||||
@ -261,8 +261,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
|
|
||||||
// Then turn address into pointer.
|
// Then turn address into pointer.
|
||||||
let ptr = M::ptr_from_addr_cast(&self, addr)?;
|
let ptr = M::ptr_from_addr_cast(&self, addr)?;
|
||||||
let layout = self.layout_of(cast_ty)?;
|
Ok(ImmTy::from_scalar(Scalar::from_maybe_pointer(ptr, self), cast_to))
|
||||||
Ok(ImmTy::from_scalar(Scalar::from_maybe_pointer(ptr, self), layout))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Low-level cast helper function. This works directly on scalars and can take 'int-like' input
|
/// Low-level cast helper function. This works directly on scalars and can take 'int-like' input
|
||||||
|
@ -163,7 +163,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
// Cast bits from tag layout to discriminant layout.
|
// Cast bits from tag layout to discriminant layout.
|
||||||
// After the checks we did above, this cannot fail, as
|
// After the checks we did above, this cannot fail, as
|
||||||
// discriminants are int-like.
|
// discriminants are int-like.
|
||||||
let discr_val = self.int_to_int_or_float(&tag_val, discr_layout.ty).unwrap();
|
let discr_val = self.int_to_int_or_float(&tag_val, discr_layout).unwrap();
|
||||||
let discr_bits = discr_val.to_scalar().assert_bits(discr_layout.size);
|
let discr_bits = discr_val.to_scalar().assert_bits(discr_layout.size);
|
||||||
// Convert discriminant to variant index, and catch invalid discriminants.
|
// Convert discriminant to variant index, and catch invalid discriminants.
|
||||||
let index = match *ty.kind() {
|
let index = match *ty.kind() {
|
||||||
|
@ -234,20 +234,26 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Rvalue::Cast(CastKind::IntToInt | CastKind::IntToFloat, operand, ty) => {
|
Rvalue::Cast(CastKind::IntToInt | CastKind::IntToFloat, operand, ty) => {
|
||||||
|
let Ok(layout) = self.tcx.layout_of(self.param_env.and(*ty)) else {
|
||||||
|
return ValueOrPlace::Value(FlatSet::Top);
|
||||||
|
};
|
||||||
match self.eval_operand(operand, state) {
|
match self.eval_operand(operand, state) {
|
||||||
FlatSet::Elem(op) => self
|
FlatSet::Elem(op) => self
|
||||||
.ecx
|
.ecx
|
||||||
.int_to_int_or_float(&op, *ty)
|
.int_to_int_or_float(&op, layout)
|
||||||
.map_or(FlatSet::Top, |result| self.wrap_immediate(*result)),
|
.map_or(FlatSet::Top, |result| self.wrap_immediate(*result)),
|
||||||
FlatSet::Bottom => FlatSet::Bottom,
|
FlatSet::Bottom => FlatSet::Bottom,
|
||||||
FlatSet::Top => FlatSet::Top,
|
FlatSet::Top => FlatSet::Top,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Rvalue::Cast(CastKind::FloatToInt | CastKind::FloatToFloat, operand, ty) => {
|
Rvalue::Cast(CastKind::FloatToInt | CastKind::FloatToFloat, operand, ty) => {
|
||||||
|
let Ok(layout) = self.tcx.layout_of(self.param_env.and(*ty)) else {
|
||||||
|
return ValueOrPlace::Value(FlatSet::Top);
|
||||||
|
};
|
||||||
match self.eval_operand(operand, state) {
|
match self.eval_operand(operand, state) {
|
||||||
FlatSet::Elem(op) => self
|
FlatSet::Elem(op) => self
|
||||||
.ecx
|
.ecx
|
||||||
.float_to_float_or_int(&op, *ty)
|
.float_to_float_or_int(&op, layout)
|
||||||
.map_or(FlatSet::Top, |result| self.wrap_immediate(*result)),
|
.map_or(FlatSet::Top, |result| self.wrap_immediate(*result)),
|
||||||
FlatSet::Bottom => FlatSet::Bottom,
|
FlatSet::Bottom => FlatSet::Bottom,
|
||||||
FlatSet::Top => FlatSet::Top,
|
FlatSet::Top => FlatSet::Top,
|
||||||
|
@ -1013,7 +1013,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
fn float_to_int_checked<F>(
|
fn float_to_int_checked<F>(
|
||||||
&self,
|
&self,
|
||||||
f: F,
|
f: F,
|
||||||
dest_ty: Ty<'tcx>,
|
cast_to: TyAndLayout<'tcx>,
|
||||||
round: rustc_apfloat::Round,
|
round: rustc_apfloat::Round,
|
||||||
) -> Option<ImmTy<'tcx, Provenance>>
|
) -> Option<ImmTy<'tcx, Provenance>>
|
||||||
where
|
where
|
||||||
@ -1021,7 +1021,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
{
|
{
|
||||||
let this = self.eval_context_ref();
|
let this = self.eval_context_ref();
|
||||||
|
|
||||||
let val = match dest_ty.kind() {
|
let val = match cast_to.ty.kind() {
|
||||||
// Unsigned
|
// Unsigned
|
||||||
ty::Uint(t) => {
|
ty::Uint(t) => {
|
||||||
let size = Integer::from_uint_ty(this, *t).size();
|
let size = Integer::from_uint_ty(this, *t).size();
|
||||||
@ -1062,10 +1062,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
_ =>
|
_ =>
|
||||||
span_bug!(
|
span_bug!(
|
||||||
this.cur_span(),
|
this.cur_span(),
|
||||||
"attempted float-to-int conversion with non-int output type {dest_ty:?}"
|
"attempted float-to-int conversion with non-int output type {}",
|
||||||
|
cast_to.ty,
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
Some(ImmTy::from_scalar(val, this.layout_of(dest_ty).unwrap()))
|
Some(ImmTy::from_scalar(val, cast_to))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an integer type that is twice wide as `ty`
|
/// Returns an integer type that is twice wide as `ty`
|
||||||
|
@ -368,7 +368,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
ty::Float(FloatTy::F32) => {
|
ty::Float(FloatTy::F32) => {
|
||||||
let f = val.to_scalar().to_f32()?;
|
let f = val.to_scalar().to_f32()?;
|
||||||
this
|
this
|
||||||
.float_to_int_checked(f, dest.layout.ty, Round::TowardZero)
|
.float_to_int_checked(f, dest.layout, Round::TowardZero)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
err_ub_format!(
|
err_ub_format!(
|
||||||
"`float_to_int_unchecked` intrinsic called on {f} which cannot be represented in target type `{:?}`",
|
"`float_to_int_unchecked` intrinsic called on {f} which cannot be represented in target type `{:?}`",
|
||||||
@ -379,7 +379,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
ty::Float(FloatTy::F64) => {
|
ty::Float(FloatTy::F64) => {
|
||||||
let f = val.to_scalar().to_f64()?;
|
let f = val.to_scalar().to_f64()?;
|
||||||
this
|
this
|
||||||
.float_to_int_checked(f, dest.layout.ty, Round::TowardZero)
|
.float_to_int_checked(f, dest.layout, Round::TowardZero)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
err_ub_format!(
|
err_ub_format!(
|
||||||
"`float_to_int_unchecked` intrinsic called on {f} which cannot be represented in target type `{:?}`",
|
"`float_to_int_unchecked` intrinsic called on {f} which cannot be represented in target type `{:?}`",
|
||||||
|
@ -441,17 +441,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
// Int-to-(int|float): always safe
|
// Int-to-(int|float): always safe
|
||||||
(ty::Int(_) | ty::Uint(_), ty::Int(_) | ty::Uint(_) | ty::Float(_))
|
(ty::Int(_) | ty::Uint(_), ty::Int(_) | ty::Uint(_) | ty::Float(_))
|
||||||
if safe_cast || unsafe_cast =>
|
if safe_cast || unsafe_cast =>
|
||||||
this.int_to_int_or_float(&op, dest.layout.ty)?,
|
this.int_to_int_or_float(&op, dest.layout)?,
|
||||||
// Float-to-float: always safe
|
// Float-to-float: always safe
|
||||||
(ty::Float(_), ty::Float(_)) if safe_cast || unsafe_cast =>
|
(ty::Float(_), ty::Float(_)) if safe_cast || unsafe_cast =>
|
||||||
this.float_to_float_or_int(&op, dest.layout.ty)?,
|
this.float_to_float_or_int(&op, dest.layout)?,
|
||||||
// Float-to-int in safe mode
|
// Float-to-int in safe mode
|
||||||
(ty::Float(_), ty::Int(_) | ty::Uint(_)) if safe_cast =>
|
(ty::Float(_), ty::Int(_) | ty::Uint(_)) if safe_cast =>
|
||||||
this.float_to_float_or_int(&op, dest.layout.ty)?,
|
this.float_to_float_or_int(&op, dest.layout)?,
|
||||||
// Float-to-int in unchecked mode
|
// Float-to-int in unchecked mode
|
||||||
(ty::Float(FloatTy::F32), ty::Int(_) | ty::Uint(_)) if unsafe_cast => {
|
(ty::Float(FloatTy::F32), ty::Int(_) | ty::Uint(_)) if unsafe_cast => {
|
||||||
let f = op.to_scalar().to_f32()?;
|
let f = op.to_scalar().to_f32()?;
|
||||||
this.float_to_int_checked(f, dest.layout.ty, Round::TowardZero)
|
this.float_to_int_checked(f, dest.layout, Round::TowardZero)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
err_ub_format!(
|
err_ub_format!(
|
||||||
"`simd_cast` intrinsic called on {f} which cannot be represented in target type `{:?}`",
|
"`simd_cast` intrinsic called on {f} which cannot be represented in target type `{:?}`",
|
||||||
@ -462,7 +462,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
}
|
}
|
||||||
(ty::Float(FloatTy::F64), ty::Int(_) | ty::Uint(_)) if unsafe_cast => {
|
(ty::Float(FloatTy::F64), ty::Int(_) | ty::Uint(_)) if unsafe_cast => {
|
||||||
let f = op.to_scalar().to_f64()?;
|
let f = op.to_scalar().to_f64()?;
|
||||||
this.float_to_int_checked(f, dest.layout.ty, Round::TowardZero)
|
this.float_to_int_checked(f, dest.layout, Round::TowardZero)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
err_ub_format!(
|
err_ub_format!(
|
||||||
"`simd_cast` intrinsic called on {f} which cannot be represented in target type `{:?}`",
|
"`simd_cast` intrinsic called on {f} which cannot be represented in target type `{:?}`",
|
||||||
@ -473,12 +473,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
}
|
}
|
||||||
// Ptr-to-ptr cast
|
// Ptr-to-ptr cast
|
||||||
(ty::RawPtr(..), ty::RawPtr(..)) if ptr_cast =>
|
(ty::RawPtr(..), ty::RawPtr(..)) if ptr_cast =>
|
||||||
this.ptr_to_ptr(&op, dest.layout.ty)?,
|
this.ptr_to_ptr(&op, dest.layout)?,
|
||||||
// Ptr/Int casts
|
// Ptr/Int casts
|
||||||
(ty::RawPtr(..), ty::Int(_) | ty::Uint(_)) if expose_cast =>
|
(ty::RawPtr(..), ty::Int(_) | ty::Uint(_)) if expose_cast =>
|
||||||
this.pointer_expose_address_cast(&op, dest.layout.ty)?,
|
this.pointer_expose_address_cast(&op, dest.layout)?,
|
||||||
(ty::Int(_) | ty::Uint(_), ty::RawPtr(..)) if from_exposed_cast =>
|
(ty::Int(_) | ty::Uint(_), ty::RawPtr(..)) if from_exposed_cast =>
|
||||||
this.pointer_from_exposed_address_cast(&op, dest.layout.ty)?,
|
this.pointer_from_exposed_address_cast(&op, dest.layout)?,
|
||||||
// Error otherwise
|
// Error otherwise
|
||||||
_ =>
|
_ =>
|
||||||
throw_unsup_format!(
|
throw_unsup_format!(
|
||||||
|
@ -173,7 +173,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
|
let res = this.float_to_int_checked(op, dest.layout, rnd).unwrap_or_else(|| {
|
||||||
// Fallback to minimum acording to SSE semantics.
|
// Fallback to minimum acording to SSE semantics.
|
||||||
ImmTy::from_int(dest.layout.size.signed_int_min(), dest.layout)
|
ImmTy::from_int(dest.layout.size.signed_int_min(), dest.layout)
|
||||||
});
|
});
|
||||||
@ -196,7 +196,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
|
|
||||||
let right = this.read_immediate(right)?;
|
let right = this.read_immediate(right)?;
|
||||||
let dest0 = this.project_index(&dest, 0)?;
|
let dest0 = this.project_index(&dest, 0)?;
|
||||||
let res0 = this.int_to_int_or_float(&right, dest0.layout.ty)?;
|
let res0 = this.int_to_int_or_float(&right, dest0.layout)?;
|
||||||
this.write_immediate(*res0, &dest0)?;
|
this.write_immediate(*res0, &dest0)?;
|
||||||
|
|
||||||
for i in 1..dest_len {
|
for i in 1..dest_len {
|
||||||
|
@ -56,10 +56,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
let dest = this.project_index(&dest, i)?;
|
let dest = this.project_index(&dest, i)?;
|
||||||
|
|
||||||
// Widen the operands to avoid overflow
|
// Widen the operands to avoid overflow
|
||||||
let twice_wide_ty = this.get_twice_wide_int_ty(left.layout.ty);
|
let twice_wide = this.layout_of(this.get_twice_wide_int_ty(left.layout.ty))?;
|
||||||
let twice_wide_layout = this.layout_of(twice_wide_ty)?;
|
let left = this.int_to_int_or_float(&left, twice_wide)?;
|
||||||
let left = this.int_to_int_or_float(&left, twice_wide_ty)?;
|
let right = this.int_to_int_or_float(&right, twice_wide)?;
|
||||||
let right = this.int_to_int_or_float(&right, twice_wide_ty)?;
|
|
||||||
|
|
||||||
// Calculate left + right + 1
|
// Calculate left + right + 1
|
||||||
let added = this.wrapping_binary_op(
|
let added = this.wrapping_binary_op(
|
||||||
@ -70,20 +69,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
let added = this.wrapping_binary_op(
|
let added = this.wrapping_binary_op(
|
||||||
mir::BinOp::Add,
|
mir::BinOp::Add,
|
||||||
&added,
|
&added,
|
||||||
&ImmTy::from_uint(1u32, twice_wide_layout),
|
&ImmTy::from_uint(1u32, twice_wide),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Calculate (left + right + 1) / 2
|
// Calculate (left + right + 1) / 2
|
||||||
let divided = this.wrapping_binary_op(
|
let divided = this.wrapping_binary_op(
|
||||||
mir::BinOp::Div,
|
mir::BinOp::Div,
|
||||||
&added,
|
&added,
|
||||||
&ImmTy::from_uint(2u32, twice_wide_layout),
|
&ImmTy::from_uint(2u32, twice_wide),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Narrow back to the original type
|
// Narrow back to the original type
|
||||||
let res = this.int_to_int_or_float(
|
let res = this.int_to_int_or_float(
|
||||||
÷d,
|
÷d,
|
||||||
dest.layout.ty,
|
dest.layout,
|
||||||
)?;
|
)?;
|
||||||
this.write_immediate(*res, &dest)?;
|
this.write_immediate(*res, &dest)?;
|
||||||
}
|
}
|
||||||
@ -106,10 +105,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
let dest = this.project_index(&dest, i)?;
|
let dest = this.project_index(&dest, i)?;
|
||||||
|
|
||||||
// Widen the operands to avoid overflow
|
// Widen the operands to avoid overflow
|
||||||
let twice_wide_ty = this.get_twice_wide_int_ty(left.layout.ty);
|
let twice_wide = this.layout_of(this.get_twice_wide_int_ty(left.layout.ty))?;
|
||||||
let twice_wide_layout = this.layout_of(twice_wide_ty)?;
|
let left = this.int_to_int_or_float(&left, twice_wide)?;
|
||||||
let left = this.int_to_int_or_float(&left, twice_wide_ty)?;
|
let right = this.int_to_int_or_float(&right, twice_wide)?;
|
||||||
let right = this.int_to_int_or_float(&right, twice_wide_ty)?;
|
|
||||||
|
|
||||||
// Multiply
|
// Multiply
|
||||||
let multiplied = this.wrapping_binary_op(
|
let multiplied = this.wrapping_binary_op(
|
||||||
@ -121,13 +119,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
let high = this.wrapping_binary_op(
|
let high = this.wrapping_binary_op(
|
||||||
mir::BinOp::Shr,
|
mir::BinOp::Shr,
|
||||||
&multiplied,
|
&multiplied,
|
||||||
&ImmTy::from_uint(dest.layout.size.bits(), twice_wide_layout),
|
&ImmTy::from_uint(dest.layout.size.bits(), twice_wide),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Narrow back to the original type
|
// Narrow back to the original type
|
||||||
let res = this.int_to_int_or_float(
|
let res = this.int_to_int_or_float(
|
||||||
&high,
|
&high,
|
||||||
dest.layout.ty,
|
dest.layout,
|
||||||
)?;
|
)?;
|
||||||
this.write_immediate(*res, &dest)?;
|
this.write_immediate(*res, &dest)?;
|
||||||
}
|
}
|
||||||
@ -392,7 +390,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
let dest = this.project_index(&dest, i)?;
|
let dest = this.project_index(&dest, i)?;
|
||||||
|
|
||||||
let res =
|
let res =
|
||||||
this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
|
this.float_to_int_checked(op, dest.layout, rnd).unwrap_or_else(|| {
|
||||||
// Fallback to minimum acording to SSE2 semantics.
|
// Fallback to minimum acording to SSE2 semantics.
|
||||||
ImmTy::from_int(i32::MIN, this.machine.layouts.i32)
|
ImmTy::from_int(i32::MIN, this.machine.layouts.i32)
|
||||||
});
|
});
|
||||||
@ -648,7 +646,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
let op = this.read_immediate(&this.project_index(&op, i)?)?;
|
let op = this.read_immediate(&this.project_index(&op, i)?)?;
|
||||||
let dest = this.project_index(&dest, i)?;
|
let dest = this.project_index(&dest, i)?;
|
||||||
|
|
||||||
let res = this.float_to_float_or_int(&op, dest.layout.ty)?;
|
let res = this.float_to_float_or_int(&op, dest.layout)?;
|
||||||
this.write_immediate(*res, &dest)?;
|
this.write_immediate(*res, &dest)?;
|
||||||
}
|
}
|
||||||
// For f32 -> f64, ignore the remaining
|
// For f32 -> f64, ignore the remaining
|
||||||
@ -685,7 +683,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
let dest = this.project_index(&dest, i)?;
|
let dest = this.project_index(&dest, i)?;
|
||||||
|
|
||||||
let res =
|
let res =
|
||||||
this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
|
this.float_to_int_checked(op, dest.layout, rnd).unwrap_or_else(|| {
|
||||||
// Fallback to minimum acording to SSE2 semantics.
|
// Fallback to minimum acording to SSE2 semantics.
|
||||||
ImmTy::from_int(i32::MIN, this.machine.layouts.i32)
|
ImmTy::from_int(i32::MIN, this.machine.layouts.i32)
|
||||||
});
|
});
|
||||||
@ -716,7 +714,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
|
let res = this.float_to_int_checked(op, dest.layout, rnd).unwrap_or_else(|| {
|
||||||
// Fallback to minimum acording to SSE semantics.
|
// Fallback to minimum acording to SSE semantics.
|
||||||
ImmTy::from_int(dest.layout.size.signed_int_min(), dest.layout)
|
ImmTy::from_int(dest.layout.size.signed_int_min(), dest.layout)
|
||||||
});
|
});
|
||||||
@ -741,7 +739,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
|||||||
let dest0 = this.project_index(&dest, 0)?;
|
let dest0 = this.project_index(&dest, 0)?;
|
||||||
// `float_to_float_or_int` here will convert from f64 to f32 (cvtsd2ss) or
|
// `float_to_float_or_int` here will convert from f64 to f32 (cvtsd2ss) or
|
||||||
// from f32 to f64 (cvtss2sd).
|
// from f32 to f64 (cvtss2sd).
|
||||||
let res0 = this.float_to_float_or_int(&right0, dest0.layout.ty)?;
|
let res0 = this.float_to_float_or_int(&right0, dest0.layout)?;
|
||||||
this.write_immediate(*res0, &dest0)?;
|
this.write_immediate(*res0, &dest0)?;
|
||||||
|
|
||||||
// Copy remianing from `left`
|
// Copy remianing from `left`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user