use std::fmt;
use either::{Either, Left, Right};
use rustc_apfloat::{
ieee::{Double, Single},
Float,
};
use rustc_macros::HashStable;
use rustc_target::abi::{HasDataLayout, Size};
use crate::ty::ScalarInt;
use super::{AllocId, InterpResult, Pointer, PointerArithmetic, Provenance, ScalarSizeMismatch};
#[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, Hash)]
#[derive(HashStable)]
pub enum Scalar<Prov = AllocId> {
Int(ScalarInt),
Ptr(Pointer<Prov>, u8),
}
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
static_assert_size!(Scalar, 24);
impl<Prov: Provenance> fmt::Debug for Scalar<Prov> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Scalar::Ptr(ptr, _size) => write!(f, "{ptr:?}"),
Scalar::Int(int) => write!(f, "{int:?}"),
}
}
}
impl<Prov: Provenance> fmt::Display for Scalar<Prov> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Scalar::Ptr(ptr, _size) => write!(f, "pointer to {ptr:?}"),
Scalar::Int(int) => write!(f, "{int}"),
}
}
}
impl<Prov: Provenance> fmt::LowerHex for Scalar<Prov> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Scalar::Ptr(ptr, _size) => write!(f, "pointer to {ptr:?}"),
Scalar::Int(int) => write!(f, "{int:#x}"),
}
}
}
impl<Prov> From<Single> for Scalar<Prov> {
#[inline(always)]
fn from(f: Single) -> Self {
Scalar::from_f32(f)
}
}
impl<Prov> From<Double> for Scalar<Prov> {
#[inline(always)]
fn from(f: Double) -> Self {
Scalar::from_f64(f)
}
}
impl<Prov> From<ScalarInt> for Scalar<Prov> {
#[inline(always)]
fn from(ptr: ScalarInt) -> Self {
Scalar::Int(ptr)
}
}
impl<Prov> Scalar<Prov> {
#[inline(always)]
pub fn from_pointer(ptr: Pointer<Prov>, cx: &impl HasDataLayout) -> Self {
Scalar::Ptr(ptr, u8::try_from(cx.pointer_size().bytes()).unwrap())
}
pub fn from_maybe_pointer(ptr: Pointer<Option<Prov>>, cx: &impl HasDataLayout) -> Self {
match ptr.into_parts() {
(Some(prov), offset) => Scalar::from_pointer(Pointer::new(prov, offset), cx),
(None, offset) => {
Scalar::Int(ScalarInt::try_from_uint(offset.bytes(), cx.pointer_size()).unwrap())
}
}
}
#[inline]
pub fn null_ptr(cx: &impl HasDataLayout) -> Self {
Scalar::Int(ScalarInt::null(cx.pointer_size()))
}
#[inline]
pub fn from_bool(b: bool) -> Self {
Scalar::Int(b.into())
}
#[inline]
pub fn from_char(c: char) -> Self {
Scalar::Int(c.into())
}
#[inline]
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
ScalarInt::try_from_uint(i, size).map(Scalar::Int)
}
#[inline]
pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
let i = i.into();
Self::try_from_uint(i, size)
.unwrap_or_else(|| bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits()))
}
#[inline]
pub fn from_u8(i: u8) -> Self {
Scalar::Int(i.into())
}
#[inline]
pub fn from_u16(i: u16) -> Self {
Scalar::Int(i.into())
}
#[inline]
pub fn from_u32(i: u32) -> Self {
Scalar::Int(i.into())
}
#[inline]
pub fn from_u64(i: u64) -> Self {
Scalar::Int(i.into())
}
#[inline]
pub fn from_u128(i: u128) -> Self {
Scalar::Int(i.into())
}
#[inline]
pub fn from_target_usize(i: u64, cx: &impl HasDataLayout) -> Self {
Self::from_uint(i, cx.data_layout().pointer_size)
}
#[inline]
pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
ScalarInt::try_from_int(i, size).map(Scalar::Int)
}
#[inline]
pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
let i = i.into();
Self::try_from_int(i, size)
.unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
}
#[inline]
pub fn from_i8(i: i8) -> Self {
Self::from_int(i, Size::from_bits(8))
}
#[inline]
pub fn from_i16(i: i16) -> Self {
Self::from_int(i, Size::from_bits(16))
}
#[inline]
pub fn from_i32(i: i32) -> Self {
Self::from_int(i, Size::from_bits(32))
}
#[inline]
pub fn from_i64(i: i64) -> Self {
Self::from_int(i, Size::from_bits(64))
}
#[inline]
pub fn from_target_isize(i: i64, cx: &impl HasDataLayout) -> Self {
Self::from_int(i, cx.data_layout().pointer_size)
}
#[inline]
pub fn from_f32(f: Single) -> Self {
Scalar::Int(f.into())
}
#[inline]
pub fn from_f64(f: Double) -> Self {
Scalar::Int(f.into())
}
#[inline]
pub fn to_bits_or_ptr_internal(
self,
target_size: Size,
) -> Result<Either<u128, Pointer<Prov>>, ScalarSizeMismatch> {
assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
Ok(match self {
Scalar::Int(int) => Left(int.to_bits(target_size).map_err(|size| {
ScalarSizeMismatch { target_size: target_size.bytes(), data_size: size.bytes() }
})?),
Scalar::Ptr(ptr, sz) => {
if target_size.bytes() != u64::from(sz) {
return Err(ScalarSizeMismatch {
target_size: target_size.bytes(),
data_size: sz.into(),
});
}
Right(ptr)
}
})
}
#[inline]
pub fn size(self) -> Size {
match self {
Scalar::Int(int) => int.size(),
Scalar::Ptr(_ptr, sz) => Size::from_bytes(sz),
}
}
}
impl<'tcx, Prov: Provenance> Scalar<Prov> {
pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>> {
match self
.to_bits_or_ptr_internal(cx.pointer_size())
.map_err(|s| err_ub!(ScalarSizeMismatch(s)))?
{
Right(ptr) => Ok(ptr.into()),
Left(bits) => {
let addr = u64::try_from(bits).unwrap();
Ok(Pointer::from_addr_invalid(addr))
}
}
}
#[inline]
pub fn try_to_int(self) -> Result<ScalarInt, Scalar<AllocId>> {
match self {
Scalar::Int(int) => Ok(int),
Scalar::Ptr(ptr, sz) => {
if Prov::OFFSET_IS_ADDR {
Ok(ScalarInt::try_from_uint(ptr.offset.bytes(), Size::from_bytes(sz)).unwrap())
} else {
let (prov, offset) = ptr.into_parts();
Err(Scalar::Ptr(Pointer::new(prov.get_alloc_id().unwrap(), offset), sz))
}
}
}
}
#[inline(always)]
#[cfg_attr(debug_assertions, track_caller)] pub fn assert_int(self) -> ScalarInt {
self.try_to_int().unwrap()
}
#[inline]
pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
self.try_to_int()
.map_err(|_| err_unsup!(ReadPointerAsInt(None)))?
.to_bits(target_size)
.map_err(|size| {
err_ub!(ScalarSizeMismatch(ScalarSizeMismatch {
target_size: target_size.bytes(),
data_size: size.bytes(),
}))
.into()
})
}
#[inline(always)]
#[cfg_attr(debug_assertions, track_caller)] pub fn assert_bits(self, target_size: Size) -> u128 {
self.to_bits(target_size)
.unwrap_or_else(|_| panic!("assertion failed: {self:?} fits {target_size:?}"))
}
pub fn to_bool(self) -> InterpResult<'tcx, bool> {
let val = self.to_u8()?;
match val {
0 => Ok(false),
1 => Ok(true),
_ => throw_ub!(InvalidBool(val)),
}
}
pub fn to_char(self) -> InterpResult<'tcx, char> {
let val = self.to_u32()?;
match std::char::from_u32(val) {
Some(c) => Ok(c),
None => throw_ub!(InvalidChar(val)),
}
}
#[inline]
pub fn to_uint(self, size: Size) -> InterpResult<'tcx, u128> {
self.to_bits(size)
}
pub fn to_u8(self) -> InterpResult<'tcx, u8> {
self.to_uint(Size::from_bits(8)).map(|v| u8::try_from(v).unwrap())
}
pub fn to_u16(self) -> InterpResult<'tcx, u16> {
self.to_uint(Size::from_bits(16)).map(|v| u16::try_from(v).unwrap())
}
pub fn to_u32(self) -> InterpResult<'tcx, u32> {
self.to_uint(Size::from_bits(32)).map(|v| u32::try_from(v).unwrap())
}
pub fn to_u64(self) -> InterpResult<'tcx, u64> {
self.to_uint(Size::from_bits(64)).map(|v| u64::try_from(v).unwrap())
}
pub fn to_u128(self) -> InterpResult<'tcx, u128> {
self.to_uint(Size::from_bits(128))
}
pub fn to_target_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
let b = self.to_uint(cx.data_layout().pointer_size)?;
Ok(u64::try_from(b).unwrap())
}
#[inline]
pub fn to_int(self, size: Size) -> InterpResult<'tcx, i128> {
let b = self.to_bits(size)?;
Ok(size.sign_extend(b) as i128)
}
pub fn to_i8(self) -> InterpResult<'tcx, i8> {
self.to_int(Size::from_bits(8)).map(|v| i8::try_from(v).unwrap())
}
pub fn to_i16(self) -> InterpResult<'tcx, i16> {
self.to_int(Size::from_bits(16)).map(|v| i16::try_from(v).unwrap())
}
pub fn to_i32(self) -> InterpResult<'tcx, i32> {
self.to_int(Size::from_bits(32)).map(|v| i32::try_from(v).unwrap())
}
pub fn to_i64(self) -> InterpResult<'tcx, i64> {
self.to_int(Size::from_bits(64)).map(|v| i64::try_from(v).unwrap())
}
pub fn to_i128(self) -> InterpResult<'tcx, i128> {
self.to_int(Size::from_bits(128))
}
pub fn to_target_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
let b = self.to_int(cx.data_layout().pointer_size)?;
Ok(i64::try_from(b).unwrap())
}
#[inline]
pub fn to_float<F: Float>(self) -> InterpResult<'tcx, F> {
Ok(F::from_bits(self.to_uint(Size::from_bits(F::BITS))?))
}
#[inline]
pub fn to_f32(self) -> InterpResult<'tcx, Single> {
self.to_float()
}
#[inline]
pub fn to_f64(self) -> InterpResult<'tcx, Double> {
self.to_float()
}
}