//! Definitions for values used in DWARF expressions. use crate::constants; #[cfg(feature = "read")] use crate::read::{AttributeValue, DebuggingInformationEntry}; use crate::read::{Error, Reader, Result}; /// Convert a u64 to an i64, with sign extension if required. /// /// This is primarily used when needing to treat `Value::Generic` /// as a signed value. #[inline] fn sign_extend(value: u64, mask: u64) -> i64 { let value = (value & mask) as i64; let sign = ((mask >> 1) + 1) as i64; (value ^ sign).wrapping_sub(sign) } #[inline] fn mask_bit_size(addr_mask: u64) -> u32 { 64 - addr_mask.leading_zeros() } /// The type of an entry on the DWARF stack. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ValueType { /// The generic type, which is address-sized and of unspecified sign, /// as specified in the DWARF 5 standard, section 2.5.1. /// This type is also used to represent address base types. Generic, /// Signed 8-bit integer type. I8, /// Unsigned 8-bit integer type. U8, /// Signed 16-bit integer type. I16, /// Unsigned 16-bit integer type. U16, /// Signed 32-bit integer type. I32, /// Unsigned 32-bit integer type. U32, /// Signed 64-bit integer type. I64, /// Unsigned 64-bit integer type. U64, /// 32-bit floating point type. F32, /// 64-bit floating point type. F64, } /// The value of an entry on the DWARF stack. #[derive(Debug, Clone, Copy, PartialEq)] pub enum Value { /// A generic value, which is address-sized and of unspecified sign. Generic(u64), /// A signed 8-bit integer value. I8(i8), /// An unsigned 8-bit integer value. U8(u8), /// A signed 16-bit integer value. I16(i16), /// An unsigned 16-bit integer value. U16(u16), /// A signed 32-bit integer value. I32(i32), /// An unsigned 32-bit integer value. U32(u32), /// A signed 64-bit integer value. I64(i64), /// An unsigned 64-bit integer value. U64(u64), /// A 32-bit floating point value. F32(f32), /// A 64-bit floating point value. F64(f64), } impl ValueType { /// The size in bits of a value for this type. pub fn bit_size(self, addr_mask: u64) -> u32 { match self { ValueType::Generic => mask_bit_size(addr_mask), ValueType::I8 | ValueType::U8 => 8, ValueType::I16 | ValueType::U16 => 16, ValueType::I32 | ValueType::U32 | ValueType::F32 => 32, ValueType::I64 | ValueType::U64 | ValueType::F64 => 64, } } /// Construct a `ValueType` from the attributes of a base type DIE. pub fn from_encoding(encoding: constants::DwAte, byte_size: u64) -> Option { Some(match (encoding, byte_size) { (constants::DW_ATE_signed, 1) => ValueType::I8, (constants::DW_ATE_signed, 2) => ValueType::I16, (constants::DW_ATE_signed, 4) => ValueType::I32, (constants::DW_ATE_signed, 8) => ValueType::I64, (constants::DW_ATE_unsigned, 1) => ValueType::U8, (constants::DW_ATE_unsigned, 2) => ValueType::U16, (constants::DW_ATE_unsigned, 4) => ValueType::U32, (constants::DW_ATE_unsigned, 8) => ValueType::U64, (constants::DW_ATE_float, 4) => ValueType::F32, (constants::DW_ATE_float, 8) => ValueType::F64, _ => return None, }) } /// Construct a `ValueType` from a base type DIE. #[cfg(feature = "read")] pub fn from_entry( entry: &DebuggingInformationEntry, ) -> Result> { if entry.tag() != constants::DW_TAG_base_type { return Ok(None); } let mut encoding = None; let mut byte_size = None; let mut endianity = constants::DW_END_default; let mut attrs = entry.attrs(); while let Some(attr) = attrs.next()? { match attr.name() { constants::DW_AT_byte_size => byte_size = attr.udata_value(), constants::DW_AT_encoding => { if let AttributeValue::Encoding(x) = attr.value() { encoding = Some(x); } } constants::DW_AT_endianity => { if let AttributeValue::Endianity(x) = attr.value() { endianity = x; } } _ => {} } } if endianity != constants::DW_END_default { // TODO: we could check if it matches the reader endianity, // but normally it would use DW_END_default in that case. return Ok(None); } if let (Some(encoding), Some(byte_size)) = (encoding, byte_size) { Ok(ValueType::from_encoding(encoding, byte_size)) } else { Ok(None) } } } impl Value { /// Return the `ValueType` corresponding to this `Value`. pub fn value_type(&self) -> ValueType { match *self { Value::Generic(_) => ValueType::Generic, Value::I8(_) => ValueType::I8, Value::U8(_) => ValueType::U8, Value::I16(_) => ValueType::I16, Value::U16(_) => ValueType::U16, Value::I32(_) => ValueType::I32, Value::U32(_) => ValueType::U32, Value::I64(_) => ValueType::I64, Value::U64(_) => ValueType::U64, Value::F32(_) => ValueType::F32, Value::F64(_) => ValueType::F64, } } /// Read a `Value` with the given `value_type` from a `Reader`. pub fn parse(value_type: ValueType, mut bytes: R) -> Result { let value = match value_type { ValueType::I8 => Value::I8(bytes.read_i8()?), ValueType::U8 => Value::U8(bytes.read_u8()?), ValueType::I16 => Value::I16(bytes.read_i16()?), ValueType::U16 => Value::U16(bytes.read_u16()?), ValueType::I32 => Value::I32(bytes.read_i32()?), ValueType::U32 => Value::U32(bytes.read_u32()?), ValueType::I64 => Value::I64(bytes.read_i64()?), ValueType::U64 => Value::U64(bytes.read_u64()?), ValueType::F32 => Value::F32(bytes.read_f32()?), ValueType::F64 => Value::F64(bytes.read_f64()?), _ => return Err(Error::UnsupportedTypeOperation), }; Ok(value) } /// Convert a `Value` to a `u64`. /// /// The `ValueType` of `self` must be integral. /// Values are sign extended if the source value is signed. pub fn to_u64(self, addr_mask: u64) -> Result { let value = match self { Value::Generic(value) => value & addr_mask, Value::I8(value) => value as u64, Value::U8(value) => u64::from(value), Value::I16(value) => value as u64, Value::U16(value) => u64::from(value), Value::I32(value) => value as u64, Value::U32(value) => u64::from(value), Value::I64(value) => value as u64, Value::U64(value) => value as u64, _ => return Err(Error::IntegralTypeRequired), }; Ok(value) } /// Create a `Value` with the given `value_type` from a `u64` value. /// /// The `value_type` may be integral or floating point. /// The result is truncated if the `u64` value does /// not fit the bounds of the `value_type`. pub fn from_u64(value_type: ValueType, value: u64) -> Result { let value = match value_type { ValueType::Generic => Value::Generic(value), ValueType::I8 => Value::I8(value as i8), ValueType::U8 => Value::U8(value as u8), ValueType::I16 => Value::I16(value as i16), ValueType::U16 => Value::U16(value as u16), ValueType::I32 => Value::I32(value as i32), ValueType::U32 => Value::U32(value as u32), ValueType::I64 => Value::I64(value as i64), ValueType::U64 => Value::U64(value), ValueType::F32 => Value::F32(value as f32), ValueType::F64 => Value::F64(value as f64), }; Ok(value) } /// Create a `Value` with the given `value_type` from a `f32` value. /// /// The `value_type` may be integral or floating point. /// The result is not defined if the `f32` value does /// not fit the bounds of the `value_type`. fn from_f32(value_type: ValueType, value: f32) -> Result { let value = match value_type { ValueType::Generic => Value::Generic(value as u64), ValueType::I8 => Value::I8(value as i8), ValueType::U8 => Value::U8(value as u8), ValueType::I16 => Value::I16(value as i16), ValueType::U16 => Value::U16(value as u16), ValueType::I32 => Value::I32(value as i32), ValueType::U32 => Value::U32(value as u32), ValueType::I64 => Value::I64(value as i64), ValueType::U64 => Value::U64(value as u64), ValueType::F32 => Value::F32(value), ValueType::F64 => Value::F64(f64::from(value)), }; Ok(value) } /// Create a `Value` with the given `value_type` from a `f64` value. /// /// The `value_type` may be integral or floating point. /// The result is not defined if the `f64` value does /// not fit the bounds of the `value_type`. fn from_f64(value_type: ValueType, value: f64) -> Result { let value = match value_type { ValueType::Generic => Value::Generic(value as u64), ValueType::I8 => Value::I8(value as i8), ValueType::U8 => Value::U8(value as u8), ValueType::I16 => Value::I16(value as i16), ValueType::U16 => Value::U16(value as u16), ValueType::I32 => Value::I32(value as i32), ValueType::U32 => Value::U32(value as u32), ValueType::I64 => Value::I64(value as i64), ValueType::U64 => Value::U64(value as u64), ValueType::F32 => Value::F32(value as f32), ValueType::F64 => Value::F64(value), }; Ok(value) } /// Convert a `Value` to the given `value_type`. /// /// When converting between integral types, the result is truncated /// if the source value does not fit the bounds of the `value_type`. /// When converting from floating point types, the result is not defined /// if the source value does not fit the bounds of the `value_type`. /// /// This corresponds to the DWARF `DW_OP_convert` operation. pub fn convert(self, value_type: ValueType, addr_mask: u64) -> Result { match self { Value::F32(value) => Value::from_f32(value_type, value), Value::F64(value) => Value::from_f64(value_type, value), _ => Value::from_u64(value_type, self.to_u64(addr_mask)?), } } /// Reinterpret the bits in a `Value` as the given `value_type`. /// /// The source and result value types must have equal sizes. /// /// This corresponds to the DWARF `DW_OP_reinterpret` operation. pub fn reinterpret(self, value_type: ValueType, addr_mask: u64) -> Result { if self.value_type().bit_size(addr_mask) != value_type.bit_size(addr_mask) { return Err(Error::TypeMismatch); } let bits = match self { Value::Generic(value) => value, Value::I8(value) => value as u64, Value::U8(value) => u64::from(value), Value::I16(value) => value as u64, Value::U16(value) => u64::from(value), Value::I32(value) => value as u64, Value::U32(value) => u64::from(value), Value::I64(value) => value as u64, Value::U64(value) => value, Value::F32(value) => u64::from(f32::to_bits(value)), Value::F64(value) => f64::to_bits(value), }; let value = match value_type { ValueType::Generic => Value::Generic(bits), ValueType::I8 => Value::I8(bits as i8), ValueType::U8 => Value::U8(bits as u8), ValueType::I16 => Value::I16(bits as i16), ValueType::U16 => Value::U16(bits as u16), ValueType::I32 => Value::I32(bits as i32), ValueType::U32 => Value::U32(bits as u32), ValueType::I64 => Value::I64(bits as i64), ValueType::U64 => Value::U64(bits), ValueType::F32 => Value::F32(f32::from_bits(bits as u32)), ValueType::F64 => Value::F64(f64::from_bits(bits)), }; Ok(value) } /// Perform an absolute value operation. /// /// If the value type is `Generic`, then it is interpreted as a signed value. /// /// This corresponds to the DWARF `DW_OP_abs` operation. pub fn abs(self, addr_mask: u64) -> Result { // wrapping_abs() can be used because DWARF specifies that the result is undefined // for negative minimal values. let value = match self { Value::Generic(value) => { Value::Generic(sign_extend(value, addr_mask).wrapping_abs() as u64) } Value::I8(value) => Value::I8(value.wrapping_abs()), Value::I16(value) => Value::I16(value.wrapping_abs()), Value::I32(value) => Value::I32(value.wrapping_abs()), Value::I64(value) => Value::I64(value.wrapping_abs()), // f32/f64::abs() is not available in libcore Value::F32(value) => Value::F32(if value < 0. { -value } else { value }), Value::F64(value) => Value::F64(if value < 0. { -value } else { value }), Value::U8(_) | Value::U16(_) | Value::U32(_) | Value::U64(_) => self, }; Ok(value) } /// Perform a negation operation. /// /// If the value type is `Generic`, then it is interpreted as a signed value. /// /// This corresponds to the DWARF `DW_OP_neg` operation. pub fn neg(self, addr_mask: u64) -> Result { // wrapping_neg() can be used because DWARF specifies that the result is undefined // for negative minimal values. let value = match self { Value::Generic(value) => { Value::Generic(sign_extend(value, addr_mask).wrapping_neg() as u64) } Value::I8(value) => Value::I8(value.wrapping_neg()), Value::I16(value) => Value::I16(value.wrapping_neg()), Value::I32(value) => Value::I32(value.wrapping_neg()), Value::I64(value) => Value::I64(value.wrapping_neg()), Value::F32(value) => Value::F32(-value), Value::F64(value) => Value::F64(-value), // It's unclear if these should implicitly convert to a signed value. // For now, we don't support them. Value::U8(_) | Value::U16(_) | Value::U32(_) | Value::U64(_) => { return Err(Error::UnsupportedTypeOperation); } }; Ok(value) } /// Perform an addition operation. /// /// This operation requires matching types. /// /// This corresponds to the DWARF `DW_OP_plus` operation. pub fn add(self, rhs: Value, addr_mask: u64) -> Result { let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { Value::Generic(v1.wrapping_add(v2) & addr_mask) } (Value::I8(v1), Value::I8(v2)) => Value::I8(v1.wrapping_add(v2)), (Value::U8(v1), Value::U8(v2)) => Value::U8(v1.wrapping_add(v2)), (Value::I16(v1), Value::I16(v2)) => Value::I16(v1.wrapping_add(v2)), (Value::U16(v1), Value::U16(v2)) => Value::U16(v1.wrapping_add(v2)), (Value::I32(v1), Value::I32(v2)) => Value::I32(v1.wrapping_add(v2)), (Value::U32(v1), Value::U32(v2)) => Value::U32(v1.wrapping_add(v2)), (Value::I64(v1), Value::I64(v2)) => Value::I64(v1.wrapping_add(v2)), (Value::U64(v1), Value::U64(v2)) => Value::U64(v1.wrapping_add(v2)), (Value::F32(v1), Value::F32(v2)) => Value::F32(v1 + v2), (Value::F64(v1), Value::F64(v2)) => Value::F64(v1 + v2), _ => return Err(Error::TypeMismatch), }; Ok(value) } /// Perform a subtraction operation. /// /// This operation requires matching types. /// /// This corresponds to the DWARF `DW_OP_minus` operation. pub fn sub(self, rhs: Value, addr_mask: u64) -> Result { let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { Value::Generic(v1.wrapping_sub(v2) & addr_mask) } (Value::I8(v1), Value::I8(v2)) => Value::I8(v1.wrapping_sub(v2)), (Value::U8(v1), Value::U8(v2)) => Value::U8(v1.wrapping_sub(v2)), (Value::I16(v1), Value::I16(v2)) => Value::I16(v1.wrapping_sub(v2)), (Value::U16(v1), Value::U16(v2)) => Value::U16(v1.wrapping_sub(v2)), (Value::I32(v1), Value::I32(v2)) => Value::I32(v1.wrapping_sub(v2)), (Value::U32(v1), Value::U32(v2)) => Value::U32(v1.wrapping_sub(v2)), (Value::I64(v1), Value::I64(v2)) => Value::I64(v1.wrapping_sub(v2)), (Value::U64(v1), Value::U64(v2)) => Value::U64(v1.wrapping_sub(v2)), (Value::F32(v1), Value::F32(v2)) => Value::F32(v1 - v2), (Value::F64(v1), Value::F64(v2)) => Value::F64(v1 - v2), _ => return Err(Error::TypeMismatch), }; Ok(value) } /// Perform a multiplication operation. /// /// This operation requires matching types. /// /// This corresponds to the DWARF `DW_OP_mul` operation. pub fn mul(self, rhs: Value, addr_mask: u64) -> Result { let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { Value::Generic(v1.wrapping_mul(v2) & addr_mask) } (Value::I8(v1), Value::I8(v2)) => Value::I8(v1.wrapping_mul(v2)), (Value::U8(v1), Value::U8(v2)) => Value::U8(v1.wrapping_mul(v2)), (Value::I16(v1), Value::I16(v2)) => Value::I16(v1.wrapping_mul(v2)), (Value::U16(v1), Value::U16(v2)) => Value::U16(v1.wrapping_mul(v2)), (Value::I32(v1), Value::I32(v2)) => Value::I32(v1.wrapping_mul(v2)), (Value::U32(v1), Value::U32(v2)) => Value::U32(v1.wrapping_mul(v2)), (Value::I64(v1), Value::I64(v2)) => Value::I64(v1.wrapping_mul(v2)), (Value::U64(v1), Value::U64(v2)) => Value::U64(v1.wrapping_mul(v2)), (Value::F32(v1), Value::F32(v2)) => Value::F32(v1 * v2), (Value::F64(v1), Value::F64(v2)) => Value::F64(v1 * v2), _ => return Err(Error::TypeMismatch), }; Ok(value) } /// Perform a division operation. /// /// This operation requires matching types. /// If the value type is `Generic`, then it is interpreted as a signed value. /// /// This corresponds to the DWARF `DW_OP_div` operation. pub fn div(self, rhs: Value, addr_mask: u64) -> Result { match rhs { Value::Generic(v2) if sign_extend(v2, addr_mask) == 0 => { return Err(Error::DivisionByZero); } Value::I8(0) | Value::U8(0) | Value::I16(0) | Value::U16(0) | Value::I32(0) | Value::U32(0) | Value::I64(0) | Value::U64(0) => { return Err(Error::DivisionByZero); } _ => {} } let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { // Signed division Value::Generic( sign_extend(v1, addr_mask).wrapping_div(sign_extend(v2, addr_mask)) as u64, ) } (Value::I8(v1), Value::I8(v2)) => Value::I8(v1.wrapping_div(v2)), (Value::U8(v1), Value::U8(v2)) => Value::U8(v1.wrapping_div(v2)), (Value::I16(v1), Value::I16(v2)) => Value::I16(v1.wrapping_div(v2)), (Value::U16(v1), Value::U16(v2)) => Value::U16(v1.wrapping_div(v2)), (Value::I32(v1), Value::I32(v2)) => Value::I32(v1.wrapping_div(v2)), (Value::U32(v1), Value::U32(v2)) => Value::U32(v1.wrapping_div(v2)), (Value::I64(v1), Value::I64(v2)) => Value::I64(v1.wrapping_div(v2)), (Value::U64(v1), Value::U64(v2)) => Value::U64(v1.wrapping_div(v2)), (Value::F32(v1), Value::F32(v2)) => Value::F32(v1 / v2), (Value::F64(v1), Value::F64(v2)) => Value::F64(v1 / v2), _ => return Err(Error::TypeMismatch), }; Ok(value) } /// Perform a remainder operation. /// /// This operation requires matching integral types. /// If the value type is `Generic`, then it is interpreted as an unsigned value. /// /// This corresponds to the DWARF `DW_OP_mod` operation. pub fn rem(self, rhs: Value, addr_mask: u64) -> Result { match rhs { Value::Generic(rhs) if (rhs & addr_mask) == 0 => { return Err(Error::DivisionByZero); } Value::I8(0) | Value::U8(0) | Value::I16(0) | Value::U16(0) | Value::I32(0) | Value::U32(0) | Value::I64(0) | Value::U64(0) => { return Err(Error::DivisionByZero); } _ => {} } let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { // Unsigned modulus Value::Generic((v1 & addr_mask).wrapping_rem(v2 & addr_mask)) } (Value::I8(v1), Value::I8(v2)) => Value::I8(v1.wrapping_rem(v2)), (Value::U8(v1), Value::U8(v2)) => Value::U8(v1.wrapping_rem(v2)), (Value::I16(v1), Value::I16(v2)) => Value::I16(v1.wrapping_rem(v2)), (Value::U16(v1), Value::U16(v2)) => Value::U16(v1.wrapping_rem(v2)), (Value::I32(v1), Value::I32(v2)) => Value::I32(v1.wrapping_rem(v2)), (Value::U32(v1), Value::U32(v2)) => Value::U32(v1.wrapping_rem(v2)), (Value::I64(v1), Value::I64(v2)) => Value::I64(v1.wrapping_rem(v2)), (Value::U64(v1), Value::U64(v2)) => Value::U64(v1.wrapping_rem(v2)), (Value::F32(_), Value::F32(_)) => return Err(Error::IntegralTypeRequired), (Value::F64(_), Value::F64(_)) => return Err(Error::IntegralTypeRequired), _ => return Err(Error::TypeMismatch), }; Ok(value) } /// Perform a bitwise not operation. /// /// This operation requires matching integral types. /// /// This corresponds to the DWARF `DW_OP_not` operation. pub fn not(self, addr_mask: u64) -> Result { let value_type = self.value_type(); let v = self.to_u64(addr_mask)?; Value::from_u64(value_type, !v) } /// Perform a bitwise and operation. /// /// This operation requires matching integral types. /// /// This corresponds to the DWARF `DW_OP_and` operation. pub fn and(self, rhs: Value, addr_mask: u64) -> Result { let value_type = self.value_type(); if value_type != rhs.value_type() { return Err(Error::TypeMismatch); } let v1 = self.to_u64(addr_mask)?; let v2 = rhs.to_u64(addr_mask)?; Value::from_u64(value_type, v1 & v2) } /// Perform a bitwise or operation. /// /// This operation requires matching integral types. /// /// This corresponds to the DWARF `DW_OP_or` operation. pub fn or(self, rhs: Value, addr_mask: u64) -> Result { let value_type = self.value_type(); if value_type != rhs.value_type() { return Err(Error::TypeMismatch); } let v1 = self.to_u64(addr_mask)?; let v2 = rhs.to_u64(addr_mask)?; Value::from_u64(value_type, v1 | v2) } /// Perform a bitwise exclusive-or operation. /// /// This operation requires matching integral types. /// /// This corresponds to the DWARF `DW_OP_xor` operation. pub fn xor(self, rhs: Value, addr_mask: u64) -> Result { let value_type = self.value_type(); if value_type != rhs.value_type() { return Err(Error::TypeMismatch); } let v1 = self.to_u64(addr_mask)?; let v2 = rhs.to_u64(addr_mask)?; Value::from_u64(value_type, v1 ^ v2) } /// Convert value to bit length suitable for a shift operation. /// /// If the value is negative then an error is returned. fn shift_length(self) -> Result { let value = match self { Value::Generic(value) => value, Value::I8(value) if value >= 0 => value as u64, Value::U8(value) => u64::from(value), Value::I16(value) if value >= 0 => value as u64, Value::U16(value) => u64::from(value), Value::I32(value) if value >= 0 => value as u64, Value::U32(value) => u64::from(value), Value::I64(value) if value >= 0 => value as u64, Value::U64(value) => value, _ => return Err(Error::InvalidShiftExpression), }; Ok(value) } /// Perform a shift left operation. /// /// This operation requires integral types. /// If the shift length exceeds the type size, then 0 is returned. /// If the shift length is negative then an error is returned. /// /// This corresponds to the DWARF `DW_OP_shl` operation. pub fn shl(self, rhs: Value, addr_mask: u64) -> Result { let v2 = rhs.shift_length()?; let value = match self { Value::Generic(v1) => Value::Generic(if v2 >= u64::from(mask_bit_size(addr_mask)) { 0 } else { (v1 & addr_mask) << v2 }), Value::I8(v1) => Value::I8(if v2 >= 8 { 0 } else { v1 << v2 }), Value::U8(v1) => Value::U8(if v2 >= 8 { 0 } else { v1 << v2 }), Value::I16(v1) => Value::I16(if v2 >= 16 { 0 } else { v1 << v2 }), Value::U16(v1) => Value::U16(if v2 >= 16 { 0 } else { v1 << v2 }), Value::I32(v1) => Value::I32(if v2 >= 32 { 0 } else { v1 << v2 }), Value::U32(v1) => Value::U32(if v2 >= 32 { 0 } else { v1 << v2 }), Value::I64(v1) => Value::I64(if v2 >= 64 { 0 } else { v1 << v2 }), Value::U64(v1) => Value::U64(if v2 >= 64 { 0 } else { v1 << v2 }), _ => return Err(Error::IntegralTypeRequired), }; Ok(value) } /// Perform a logical shift right operation. /// /// This operation requires an unsigned integral type for the value. /// If the value type is `Generic`, then it is interpreted as an unsigned value. /// /// This operation requires an integral type for the shift length. /// If the shift length exceeds the type size, then 0 is returned. /// If the shift length is negative then an error is returned. /// /// This corresponds to the DWARF `DW_OP_shr` operation. pub fn shr(self, rhs: Value, addr_mask: u64) -> Result { let v2 = rhs.shift_length()?; let value = match self { Value::Generic(v1) => Value::Generic(if v2 >= u64::from(mask_bit_size(addr_mask)) { 0 } else { (v1 & addr_mask) >> v2 }), Value::U8(v1) => Value::U8(if v2 >= 8 { 0 } else { v1 >> v2 }), Value::U16(v1) => Value::U16(if v2 >= 16 { 0 } else { v1 >> v2 }), Value::U32(v1) => Value::U32(if v2 >= 32 { 0 } else { v1 >> v2 }), Value::U64(v1) => Value::U64(if v2 >= 64 { 0 } else { v1 >> v2 }), // It's unclear if signed values should implicitly convert to an unsigned value. // For now, we don't support them. Value::I8(_) | Value::I16(_) | Value::I32(_) | Value::I64(_) => { return Err(Error::UnsupportedTypeOperation); } _ => return Err(Error::IntegralTypeRequired), }; Ok(value) } /// Perform an arithmetic shift right operation. /// /// This operation requires a signed integral type for the value. /// If the value type is `Generic`, then it is interpreted as a signed value. /// /// This operation requires an integral type for the shift length. /// If the shift length exceeds the type size, then 0 is returned for positive values, /// and -1 is returned for negative values. /// If the shift length is negative then an error is returned. /// /// This corresponds to the DWARF `DW_OP_shra` operation. pub fn shra(self, rhs: Value, addr_mask: u64) -> Result { let v2 = rhs.shift_length()?; let value = match self { Value::Generic(v1) => { let v1 = sign_extend(v1, addr_mask); let value = if v2 >= u64::from(mask_bit_size(addr_mask)) { if v1 < 0 { !0 } else { 0 } } else { (v1 >> v2) as u64 }; Value::Generic(value) } Value::I8(v1) => Value::I8(if v2 >= 8 { if v1 < 0 { !0 } else { 0 } } else { v1 >> v2 }), Value::I16(v1) => Value::I16(if v2 >= 16 { if v1 < 0 { !0 } else { 0 } } else { v1 >> v2 }), Value::I32(v1) => Value::I32(if v2 >= 32 { if v1 < 0 { !0 } else { 0 } } else { v1 >> v2 }), Value::I64(v1) => Value::I64(if v2 >= 64 { if v1 < 0 { !0 } else { 0 } } else { v1 >> v2 }), // It's unclear if unsigned values should implicitly convert to a signed value. // For now, we don't support them. Value::U8(_) | Value::U16(_) | Value::U32(_) | Value::U64(_) => { return Err(Error::UnsupportedTypeOperation); } _ => return Err(Error::IntegralTypeRequired), }; Ok(value) } /// Perform the `==` relational operation. /// /// This operation requires matching integral types. /// If the value type is `Generic`, then it is interpreted as a signed value. /// /// This corresponds to the DWARF `DW_OP_eq` operation. pub fn eq(self, rhs: Value, addr_mask: u64) -> Result { let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { sign_extend(v1, addr_mask) == sign_extend(v2, addr_mask) } (Value::I8(v1), Value::I8(v2)) => v1 == v2, (Value::U8(v1), Value::U8(v2)) => v1 == v2, (Value::I16(v1), Value::I16(v2)) => v1 == v2, (Value::U16(v1), Value::U16(v2)) => v1 == v2, (Value::I32(v1), Value::I32(v2)) => v1 == v2, (Value::U32(v1), Value::U32(v2)) => v1 == v2, (Value::I64(v1), Value::I64(v2)) => v1 == v2, (Value::U64(v1), Value::U64(v2)) => v1 == v2, (Value::F32(v1), Value::F32(v2)) => v1 == v2, (Value::F64(v1), Value::F64(v2)) => v1 == v2, _ => return Err(Error::TypeMismatch), }; Ok(Value::Generic(value as u64)) } /// Perform the `>=` relational operation. /// /// This operation requires matching integral types. /// If the value type is `Generic`, then it is interpreted as a signed value. /// /// This corresponds to the DWARF `DW_OP_ge` operation. pub fn ge(self, rhs: Value, addr_mask: u64) -> Result { let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { sign_extend(v1, addr_mask) >= sign_extend(v2, addr_mask) } (Value::I8(v1), Value::I8(v2)) => v1 >= v2, (Value::U8(v1), Value::U8(v2)) => v1 >= v2, (Value::I16(v1), Value::I16(v2)) => v1 >= v2, (Value::U16(v1), Value::U16(v2)) => v1 >= v2, (Value::I32(v1), Value::I32(v2)) => v1 >= v2, (Value::U32(v1), Value::U32(v2)) => v1 >= v2, (Value::I64(v1), Value::I64(v2)) => v1 >= v2, (Value::U64(v1), Value::U64(v2)) => v1 >= v2, (Value::F32(v1), Value::F32(v2)) => v1 >= v2, (Value::F64(v1), Value::F64(v2)) => v1 >= v2, _ => return Err(Error::TypeMismatch), }; Ok(Value::Generic(value as u64)) } /// Perform the `>` relational operation. /// /// This operation requires matching integral types. /// If the value type is `Generic`, then it is interpreted as a signed value. /// /// This corresponds to the DWARF `DW_OP_gt` operation. pub fn gt(self, rhs: Value, addr_mask: u64) -> Result { let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { sign_extend(v1, addr_mask) > sign_extend(v2, addr_mask) } (Value::I8(v1), Value::I8(v2)) => v1 > v2, (Value::U8(v1), Value::U8(v2)) => v1 > v2, (Value::I16(v1), Value::I16(v2)) => v1 > v2, (Value::U16(v1), Value::U16(v2)) => v1 > v2, (Value::I32(v1), Value::I32(v2)) => v1 > v2, (Value::U32(v1), Value::U32(v2)) => v1 > v2, (Value::I64(v1), Value::I64(v2)) => v1 > v2, (Value::U64(v1), Value::U64(v2)) => v1 > v2, (Value::F32(v1), Value::F32(v2)) => v1 > v2, (Value::F64(v1), Value::F64(v2)) => v1 > v2, _ => return Err(Error::TypeMismatch), }; Ok(Value::Generic(value as u64)) } /// Perform the `<= relational operation. /// /// This operation requires matching integral types. /// If the value type is `Generic`, then it is interpreted as a signed value. /// /// This corresponds to the DWARF `DW_OP_le` operation. pub fn le(self, rhs: Value, addr_mask: u64) -> Result { let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { sign_extend(v1, addr_mask) <= sign_extend(v2, addr_mask) } (Value::I8(v1), Value::I8(v2)) => v1 <= v2, (Value::U8(v1), Value::U8(v2)) => v1 <= v2, (Value::I16(v1), Value::I16(v2)) => v1 <= v2, (Value::U16(v1), Value::U16(v2)) => v1 <= v2, (Value::I32(v1), Value::I32(v2)) => v1 <= v2, (Value::U32(v1), Value::U32(v2)) => v1 <= v2, (Value::I64(v1), Value::I64(v2)) => v1 <= v2, (Value::U64(v1), Value::U64(v2)) => v1 <= v2, (Value::F32(v1), Value::F32(v2)) => v1 <= v2, (Value::F64(v1), Value::F64(v2)) => v1 <= v2, _ => return Err(Error::TypeMismatch), }; Ok(Value::Generic(value as u64)) } /// Perform the `< relational operation. /// /// This operation requires matching integral types. /// If the value type is `Generic`, then it is interpreted as a signed value. /// /// This corresponds to the DWARF `DW_OP_lt` operation. pub fn lt(self, rhs: Value, addr_mask: u64) -> Result { let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { sign_extend(v1, addr_mask) < sign_extend(v2, addr_mask) } (Value::I8(v1), Value::I8(v2)) => v1 < v2, (Value::U8(v1), Value::U8(v2)) => v1 < v2, (Value::I16(v1), Value::I16(v2)) => v1 < v2, (Value::U16(v1), Value::U16(v2)) => v1 < v2, (Value::I32(v1), Value::I32(v2)) => v1 < v2, (Value::U32(v1), Value::U32(v2)) => v1 < v2, (Value::I64(v1), Value::I64(v2)) => v1 < v2, (Value::U64(v1), Value::U64(v2)) => v1 < v2, (Value::F32(v1), Value::F32(v2)) => v1 < v2, (Value::F64(v1), Value::F64(v2)) => v1 < v2, _ => return Err(Error::TypeMismatch), }; Ok(Value::Generic(value as u64)) } /// Perform the `!= relational operation. /// /// This operation requires matching integral types. /// If the value type is `Generic`, then it is interpreted as a signed value. /// /// This corresponds to the DWARF `DW_OP_ne` operation. pub fn ne(self, rhs: Value, addr_mask: u64) -> Result { let value = match (self, rhs) { (Value::Generic(v1), Value::Generic(v2)) => { sign_extend(v1, addr_mask) != sign_extend(v2, addr_mask) } (Value::I8(v1), Value::I8(v2)) => v1 != v2, (Value::U8(v1), Value::U8(v2)) => v1 != v2, (Value::I16(v1), Value::I16(v2)) => v1 != v2, (Value::U16(v1), Value::U16(v2)) => v1 != v2, (Value::I32(v1), Value::I32(v2)) => v1 != v2, (Value::U32(v1), Value::U32(v2)) => v1 != v2, (Value::I64(v1), Value::I64(v2)) => v1 != v2, (Value::U64(v1), Value::U64(v2)) => v1 != v2, (Value::F32(v1), Value::F32(v2)) => v1 != v2, (Value::F64(v1), Value::F64(v2)) => v1 != v2, _ => return Err(Error::TypeMismatch), }; Ok(Value::Generic(value as u64)) } } #[cfg(test)] mod tests { use super::*; use crate::common::{DebugAbbrevOffset, DebugInfoOffset, Encoding, Format}; use crate::endianity::LittleEndian; use crate::read::{ Abbreviation, AttributeSpecification, DebuggingInformationEntry, EndianSlice, UnitHeader, UnitOffset, UnitType, }; #[test] #[rustfmt::skip] fn valuetype_from_encoding() { let encoding = Encoding { format: Format::Dwarf32, version: 4, address_size: 4, }; let unit = UnitHeader::new( encoding, 7, UnitType::Compilation, DebugAbbrevOffset(0), DebugInfoOffset(0).into(), EndianSlice::new(&[], LittleEndian), ); let abbrev = Abbreviation::new( 42, constants::DW_TAG_base_type, constants::DW_CHILDREN_no, vec![ AttributeSpecification::new( constants::DW_AT_byte_size, constants::DW_FORM_udata, None, ), AttributeSpecification::new( constants::DW_AT_encoding, constants::DW_FORM_udata, None, ), AttributeSpecification::new( constants::DW_AT_endianity, constants::DW_FORM_udata, None, ), ].into(), ); for &(attrs, result) in &[ ([0x01, constants::DW_ATE_signed.0, constants::DW_END_default.0], ValueType::I8), ([0x02, constants::DW_ATE_signed.0, constants::DW_END_default.0], ValueType::I16), ([0x04, constants::DW_ATE_signed.0, constants::DW_END_default.0], ValueType::I32), ([0x08, constants::DW_ATE_signed.0, constants::DW_END_default.0], ValueType::I64), ([0x01, constants::DW_ATE_unsigned.0, constants::DW_END_default.0], ValueType::U8), ([0x02, constants::DW_ATE_unsigned.0, constants::DW_END_default.0], ValueType::U16), ([0x04, constants::DW_ATE_unsigned.0, constants::DW_END_default.0], ValueType::U32), ([0x08, constants::DW_ATE_unsigned.0, constants::DW_END_default.0], ValueType::U64), ([0x04, constants::DW_ATE_float.0, constants::DW_END_default.0], ValueType::F32), ([0x08, constants::DW_ATE_float.0, constants::DW_END_default.0], ValueType::F64), ] { let entry = DebuggingInformationEntry::new( UnitOffset(0), EndianSlice::new(&attrs, LittleEndian), &abbrev, &unit, ); assert_eq!(ValueType::from_entry(&entry), Ok(Some(result))); } for attrs in &[ [0x03, constants::DW_ATE_signed.0, constants::DW_END_default.0], [0x02, constants::DW_ATE_signed.0, constants::DW_END_big.0], ] { let entry = DebuggingInformationEntry::new( UnitOffset(0), EndianSlice::new(attrs, LittleEndian), &abbrev, &unit, ); assert_eq!(ValueType::from_entry(&entry), Ok(None)); } } #[test] fn value_convert() { let addr_mask = !0 >> 32; for &(v, t, result) in &[ (Value::Generic(1), ValueType::I8, Ok(Value::I8(1))), (Value::I8(1), ValueType::U8, Ok(Value::U8(1))), (Value::U8(1), ValueType::I16, Ok(Value::I16(1))), (Value::I16(1), ValueType::U16, Ok(Value::U16(1))), (Value::U16(1), ValueType::I32, Ok(Value::I32(1))), (Value::I32(1), ValueType::U32, Ok(Value::U32(1))), (Value::U32(1), ValueType::F32, Ok(Value::F32(1.))), (Value::F32(1.), ValueType::I64, Ok(Value::I64(1))), (Value::I64(1), ValueType::U64, Ok(Value::U64(1))), (Value::U64(1), ValueType::F64, Ok(Value::F64(1.))), (Value::F64(1.), ValueType::Generic, Ok(Value::Generic(1))), ] { assert_eq!(v.convert(t, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_reinterpret() { let addr_mask = !0 >> 32; for &(v, t, result) in &[ // 8-bit (Value::I8(-1), ValueType::U8, Ok(Value::U8(0xff))), (Value::U8(0xff), ValueType::I8, Ok(Value::I8(-1))), // 16-bit (Value::I16(1), ValueType::U16, Ok(Value::U16(1))), (Value::U16(1), ValueType::I16, Ok(Value::I16(1))), // 32-bit (Value::Generic(1), ValueType::I32, Ok(Value::I32(1))), (Value::I32(1), ValueType::U32, Ok(Value::U32(1))), (Value::U32(0x3f80_0000), ValueType::F32, Ok(Value::F32(1.0))), (Value::F32(1.0), ValueType::Generic, Ok(Value::Generic(0x3f80_0000))), // Type mismatches (Value::Generic(1), ValueType::U8, Err(Error::TypeMismatch)), (Value::U8(1), ValueType::U16, Err(Error::TypeMismatch)), (Value::U16(1), ValueType::U32, Err(Error::TypeMismatch)), (Value::U32(1), ValueType::U64, Err(Error::TypeMismatch)), (Value::U64(1), ValueType::Generic, Err(Error::TypeMismatch)), ] { assert_eq!(v.reinterpret(t, addr_mask), result); } let addr_mask = !0; for &(v, t, result) in &[ // 64-bit (Value::Generic(1), ValueType::I64, Ok(Value::I64(1))), (Value::I64(1), ValueType::U64, Ok(Value::U64(1))), (Value::U64(0x3ff0_0000_0000_0000), ValueType::F64, Ok(Value::F64(1.0))), (Value::F64(1.0), ValueType::Generic, Ok(Value::Generic(0x3ff0_0000_0000_0000))), ] { assert_eq!(v.reinterpret(t, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_abs() { let addr_mask = 0xffff_ffff; for &(v, result) in &[ (Value::Generic(0xffff_ffff), Ok(Value::Generic(1))), (Value::I8(-1), Ok(Value::I8(1))), (Value::U8(1), Ok(Value::U8(1))), (Value::I16(-1), Ok(Value::I16(1))), (Value::U16(1), Ok(Value::U16(1))), (Value::I32(-1), Ok(Value::I32(1))), (Value::U32(1), Ok(Value::U32(1))), (Value::I64(-1), Ok(Value::I64(1))), (Value::U64(1), Ok(Value::U64(1))), (Value::F32(-1.), Ok(Value::F32(1.))), (Value::F64(-1.), Ok(Value::F64(1.))), ] { assert_eq!(v.abs(addr_mask), result); } } #[test] #[rustfmt::skip] fn value_neg() { let addr_mask = 0xffff_ffff; for &(v, result) in &[ (Value::Generic(0xffff_ffff), Ok(Value::Generic(1))), (Value::I8(1), Ok(Value::I8(-1))), (Value::U8(1), Err(Error::UnsupportedTypeOperation)), (Value::I16(1), Ok(Value::I16(-1))), (Value::U16(1), Err(Error::UnsupportedTypeOperation)), (Value::I32(1), Ok(Value::I32(-1))), (Value::U32(1), Err(Error::UnsupportedTypeOperation)), (Value::I64(1), Ok(Value::I64(-1))), (Value::U64(1), Err(Error::UnsupportedTypeOperation)), (Value::F32(1.), Ok(Value::F32(-1.))), (Value::F64(1.), Ok(Value::F64(-1.))), ] { assert_eq!(v.neg(addr_mask), result); } } #[test] #[rustfmt::skip] fn value_add() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(1), Value::Generic(2), Ok(Value::Generic(3))), (Value::I8(-1), Value::I8(2), Ok(Value::I8(1))), (Value::U8(1), Value::U8(2), Ok(Value::U8(3))), (Value::I16(-1), Value::I16(2), Ok(Value::I16(1))), (Value::U16(1), Value::U16(2), Ok(Value::U16(3))), (Value::I32(-1), Value::I32(2), Ok(Value::I32(1))), (Value::U32(1), Value::U32(2), Ok(Value::U32(3))), (Value::I64(-1), Value::I64(2), Ok(Value::I64(1))), (Value::U64(1), Value::U64(2), Ok(Value::U64(3))), (Value::F32(-1.), Value::F32(2.), Ok(Value::F32(1.))), (Value::F64(-1.), Value::F64(2.), Ok(Value::F64(1.))), (Value::Generic(1), Value::U32(2), Err(Error::TypeMismatch)), ] { assert_eq!(v1.add(v2, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_sub() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(2), Ok(Value::Generic(1))), (Value::I8(-1), Value::I8(2), Ok(Value::I8(-3))), (Value::U8(3), Value::U8(2), Ok(Value::U8(1))), (Value::I16(-1), Value::I16(2), Ok(Value::I16(-3))), (Value::U16(3), Value::U16(2), Ok(Value::U16(1))), (Value::I32(-1), Value::I32(2), Ok(Value::I32(-3))), (Value::U32(3), Value::U32(2), Ok(Value::U32(1))), (Value::I64(-1), Value::I64(2), Ok(Value::I64(-3))), (Value::U64(3), Value::U64(2), Ok(Value::U64(1))), (Value::F32(-1.), Value::F32(2.), Ok(Value::F32(-3.))), (Value::F64(-1.), Value::F64(2.), Ok(Value::F64(-3.))), (Value::Generic(3), Value::U32(2), Err(Error::TypeMismatch)), ] { assert_eq!(v1.sub(v2, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_mul() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(2), Value::Generic(3), Ok(Value::Generic(6))), (Value::I8(-2), Value::I8(3), Ok(Value::I8(-6))), (Value::U8(2), Value::U8(3), Ok(Value::U8(6))), (Value::I16(-2), Value::I16(3), Ok(Value::I16(-6))), (Value::U16(2), Value::U16(3), Ok(Value::U16(6))), (Value::I32(-2), Value::I32(3), Ok(Value::I32(-6))), (Value::U32(2), Value::U32(3), Ok(Value::U32(6))), (Value::I64(-2), Value::I64(3), Ok(Value::I64(-6))), (Value::U64(2), Value::U64(3), Ok(Value::U64(6))), (Value::F32(-2.), Value::F32(3.), Ok(Value::F32(-6.))), (Value::F64(-2.), Value::F64(3.), Ok(Value::F64(-6.))), (Value::Generic(2), Value::U32(3), Err(Error::TypeMismatch)), ] { assert_eq!(v1.mul(v2, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_div() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(6), Value::Generic(3), Ok(Value::Generic(2))), (Value::I8(-6), Value::I8(3), Ok(Value::I8(-2))), (Value::U8(6), Value::U8(3), Ok(Value::U8(2))), (Value::I16(-6), Value::I16(3), Ok(Value::I16(-2))), (Value::U16(6), Value::U16(3), Ok(Value::U16(2))), (Value::I32(-6), Value::I32(3), Ok(Value::I32(-2))), (Value::U32(6), Value::U32(3), Ok(Value::U32(2))), (Value::I64(-6), Value::I64(3), Ok(Value::I64(-2))), (Value::U64(6), Value::U64(3), Ok(Value::U64(2))), (Value::F32(-6.), Value::F32(3.), Ok(Value::F32(-2.))), (Value::F64(-6.), Value::F64(3.), Ok(Value::F64(-2.))), (Value::Generic(6), Value::U32(3), Err(Error::TypeMismatch)), ] { assert_eq!(v1.div(v2, addr_mask), result); } for &(v1, v2, result) in &[ (Value::Generic(6), Value::Generic(0), Err(Error::DivisionByZero)), (Value::I8(-6), Value::I8(0), Err(Error::DivisionByZero)), (Value::U8(6), Value::U8(0), Err(Error::DivisionByZero)), (Value::I16(-6), Value::I16(0), Err(Error::DivisionByZero)), (Value::U16(6), Value::U16(0), Err(Error::DivisionByZero)), (Value::I32(-6), Value::I32(0), Err(Error::DivisionByZero)), (Value::U32(6), Value::U32(0), Err(Error::DivisionByZero)), (Value::I64(-6), Value::I64(0), Err(Error::DivisionByZero)), (Value::U64(6), Value::U64(0), Err(Error::DivisionByZero)), (Value::F32(-6.), Value::F32(0.), Ok(Value::F32(-6. / 0.))), (Value::F64(-6.), Value::F64(0.), Ok(Value::F64(-6. / 0.))), ] { assert_eq!(v1.div(v2, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_rem() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(2), Ok(Value::Generic(1))), (Value::I8(-3), Value::I8(2), Ok(Value::I8(-1))), (Value::U8(3), Value::U8(2), Ok(Value::U8(1))), (Value::I16(-3), Value::I16(2), Ok(Value::I16(-1))), (Value::U16(3), Value::U16(2), Ok(Value::U16(1))), (Value::I32(-3), Value::I32(2), Ok(Value::I32(-1))), (Value::U32(3), Value::U32(2), Ok(Value::U32(1))), (Value::I64(-3), Value::I64(2), Ok(Value::I64(-1))), (Value::U64(3), Value::U64(2), Ok(Value::U64(1))), (Value::F32(-3.), Value::F32(2.), Err(Error::IntegralTypeRequired)), (Value::F64(-3.), Value::F64(2.), Err(Error::IntegralTypeRequired)), (Value::Generic(3), Value::U32(2), Err(Error::TypeMismatch)), ] { assert_eq!(v1.rem(v2, addr_mask), result); } for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(0), Err(Error::DivisionByZero)), (Value::I8(-3), Value::I8(0), Err(Error::DivisionByZero)), (Value::U8(3), Value::U8(0), Err(Error::DivisionByZero)), (Value::I16(-3), Value::I16(0), Err(Error::DivisionByZero)), (Value::U16(3), Value::U16(0), Err(Error::DivisionByZero)), (Value::I32(-3), Value::I32(0), Err(Error::DivisionByZero)), (Value::U32(3), Value::U32(0), Err(Error::DivisionByZero)), (Value::I64(-3), Value::I64(0), Err(Error::DivisionByZero)), (Value::U64(3), Value::U64(0), Err(Error::DivisionByZero)), ] { assert_eq!(v1.rem(v2, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_not() { let addr_mask = 0xffff_ffff; for &(v, result) in &[ (Value::Generic(1), Ok(Value::Generic(!1))), (Value::I8(1), Ok(Value::I8(!1))), (Value::U8(1), Ok(Value::U8(!1))), (Value::I16(1), Ok(Value::I16(!1))), (Value::U16(1), Ok(Value::U16(!1))), (Value::I32(1), Ok(Value::I32(!1))), (Value::U32(1), Ok(Value::U32(!1))), (Value::I64(1), Ok(Value::I64(!1))), (Value::U64(1), Ok(Value::U64(!1))), (Value::F32(1.), Err(Error::IntegralTypeRequired)), (Value::F64(1.), Err(Error::IntegralTypeRequired)), ] { assert_eq!(v.not(addr_mask), result); } } #[test] #[rustfmt::skip] fn value_and() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(5), Ok(Value::Generic(1))), (Value::I8(3), Value::I8(5), Ok(Value::I8(1))), (Value::U8(3), Value::U8(5), Ok(Value::U8(1))), (Value::I16(3), Value::I16(5), Ok(Value::I16(1))), (Value::U16(3), Value::U16(5), Ok(Value::U16(1))), (Value::I32(3), Value::I32(5), Ok(Value::I32(1))), (Value::U32(3), Value::U32(5), Ok(Value::U32(1))), (Value::I64(3), Value::I64(5), Ok(Value::I64(1))), (Value::U64(3), Value::U64(5), Ok(Value::U64(1))), (Value::F32(3.), Value::F32(5.), Err(Error::IntegralTypeRequired)), (Value::F64(3.), Value::F64(5.), Err(Error::IntegralTypeRequired)), (Value::Generic(3), Value::U32(5), Err(Error::TypeMismatch)), ] { assert_eq!(v1.and(v2, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_or() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(5), Ok(Value::Generic(7))), (Value::I8(3), Value::I8(5), Ok(Value::I8(7))), (Value::U8(3), Value::U8(5), Ok(Value::U8(7))), (Value::I16(3), Value::I16(5), Ok(Value::I16(7))), (Value::U16(3), Value::U16(5), Ok(Value::U16(7))), (Value::I32(3), Value::I32(5), Ok(Value::I32(7))), (Value::U32(3), Value::U32(5), Ok(Value::U32(7))), (Value::I64(3), Value::I64(5), Ok(Value::I64(7))), (Value::U64(3), Value::U64(5), Ok(Value::U64(7))), (Value::F32(3.), Value::F32(5.), Err(Error::IntegralTypeRequired)), (Value::F64(3.), Value::F64(5.), Err(Error::IntegralTypeRequired)), (Value::Generic(3), Value::U32(5), Err(Error::TypeMismatch)), ] { assert_eq!(v1.or(v2, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_xor() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(5), Ok(Value::Generic(6))), (Value::I8(3), Value::I8(5), Ok(Value::I8(6))), (Value::U8(3), Value::U8(5), Ok(Value::U8(6))), (Value::I16(3), Value::I16(5), Ok(Value::I16(6))), (Value::U16(3), Value::U16(5), Ok(Value::U16(6))), (Value::I32(3), Value::I32(5), Ok(Value::I32(6))), (Value::U32(3), Value::U32(5), Ok(Value::U32(6))), (Value::I64(3), Value::I64(5), Ok(Value::I64(6))), (Value::U64(3), Value::U64(5), Ok(Value::U64(6))), (Value::F32(3.), Value::F32(5.), Err(Error::IntegralTypeRequired)), (Value::F64(3.), Value::F64(5.), Err(Error::IntegralTypeRequired)), (Value::Generic(3), Value::U32(5), Err(Error::TypeMismatch)), ] { assert_eq!(v1.xor(v2, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_shl() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ // One of each type (Value::Generic(3), Value::Generic(5), Ok(Value::Generic(96))), (Value::I8(3), Value::U8(5), Ok(Value::I8(96))), (Value::U8(3), Value::I8(5), Ok(Value::U8(96))), (Value::I16(3), Value::U16(5), Ok(Value::I16(96))), (Value::U16(3), Value::I16(5), Ok(Value::U16(96))), (Value::I32(3), Value::U32(5), Ok(Value::I32(96))), (Value::U32(3), Value::I32(5), Ok(Value::U32(96))), (Value::I64(3), Value::U64(5), Ok(Value::I64(96))), (Value::U64(3), Value::I64(5), Ok(Value::U64(96))), (Value::F32(3.), Value::U8(5), Err(Error::IntegralTypeRequired)), (Value::F64(3.), Value::U8(5), Err(Error::IntegralTypeRequired)), // Invalid shifts (Value::U8(3), Value::I8(-5), Err(Error::InvalidShiftExpression)), (Value::U8(3), Value::I16(-5), Err(Error::InvalidShiftExpression)), (Value::U8(3), Value::I32(-5), Err(Error::InvalidShiftExpression)), (Value::U8(3), Value::I64(-5), Err(Error::InvalidShiftExpression)), (Value::U8(3), Value::F32(5.), Err(Error::InvalidShiftExpression)), (Value::U8(3), Value::F64(5.), Err(Error::InvalidShiftExpression)), // Large shifts (Value::Generic(3), Value::Generic(32), Ok(Value::Generic(0))), (Value::I8(3), Value::U8(8), Ok(Value::I8(0))), (Value::U8(3), Value::I8(9), Ok(Value::U8(0))), (Value::I16(3), Value::U16(17), Ok(Value::I16(0))), (Value::U16(3), Value::I16(16), Ok(Value::U16(0))), (Value::I32(3), Value::U32(32), Ok(Value::I32(0))), (Value::U32(3), Value::I32(33), Ok(Value::U32(0))), (Value::I64(3), Value::U64(65), Ok(Value::I64(0))), (Value::U64(3), Value::I64(64), Ok(Value::U64(0))), ] { assert_eq!(v1.shl(v2, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_shr() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ // One of each type (Value::Generic(96), Value::Generic(5), Ok(Value::Generic(3))), (Value::I8(96), Value::U8(5), Err(Error::UnsupportedTypeOperation)), (Value::U8(96), Value::I8(5), Ok(Value::U8(3))), (Value::I16(96), Value::U16(5), Err(Error::UnsupportedTypeOperation)), (Value::U16(96), Value::I16(5), Ok(Value::U16(3))), (Value::I32(96), Value::U32(5), Err(Error::UnsupportedTypeOperation)), (Value::U32(96), Value::I32(5), Ok(Value::U32(3))), (Value::I64(96), Value::U64(5), Err(Error::UnsupportedTypeOperation)), (Value::U64(96), Value::I64(5), Ok(Value::U64(3))), (Value::F32(96.), Value::U8(5), Err(Error::IntegralTypeRequired)), (Value::F64(96.), Value::U8(5), Err(Error::IntegralTypeRequired)), // Invalid shifts (Value::U8(96), Value::I8(-5), Err(Error::InvalidShiftExpression)), (Value::U8(96), Value::I16(-5), Err(Error::InvalidShiftExpression)), (Value::U8(96), Value::I32(-5), Err(Error::InvalidShiftExpression)), (Value::U8(96), Value::I64(-5), Err(Error::InvalidShiftExpression)), (Value::U8(96), Value::F32(5.), Err(Error::InvalidShiftExpression)), (Value::U8(96), Value::F64(5.), Err(Error::InvalidShiftExpression)), // Large shifts (Value::Generic(96), Value::Generic(32), Ok(Value::Generic(0))), (Value::U8(96), Value::I8(9), Ok(Value::U8(0))), (Value::U16(96), Value::I16(16), Ok(Value::U16(0))), (Value::U32(96), Value::I32(33), Ok(Value::U32(0))), (Value::U64(96), Value::I64(64), Ok(Value::U64(0))), ] { assert_eq!(v1.shr(v2, addr_mask), result); } } #[test] #[rustfmt::skip] fn value_shra() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ // One of each type (Value::Generic(u64::from(-96i32 as u32)), Value::Generic(5), Ok(Value::Generic(-3i64 as u64))), (Value::I8(-96), Value::U8(5), Ok(Value::I8(-3))), (Value::U8(96), Value::I8(5), Err(Error::UnsupportedTypeOperation)), (Value::I16(-96), Value::U16(5), Ok(Value::I16(-3))), (Value::U16(96), Value::I16(5), Err(Error::UnsupportedTypeOperation)), (Value::I32(-96), Value::U32(5), Ok(Value::I32(-3))), (Value::U32(96), Value::I32(5), Err(Error::UnsupportedTypeOperation)), (Value::I64(-96), Value::U64(5), Ok(Value::I64(-3))), (Value::U64(96), Value::I64(5), Err(Error::UnsupportedTypeOperation)), (Value::F32(96.), Value::U8(5), Err(Error::IntegralTypeRequired)), (Value::F64(96.), Value::U8(5), Err(Error::IntegralTypeRequired)), // Invalid shifts (Value::U8(96), Value::I8(-5), Err(Error::InvalidShiftExpression)), (Value::U8(96), Value::I16(-5), Err(Error::InvalidShiftExpression)), (Value::U8(96), Value::I32(-5), Err(Error::InvalidShiftExpression)), (Value::U8(96), Value::I64(-5), Err(Error::InvalidShiftExpression)), (Value::U8(96), Value::F32(5.), Err(Error::InvalidShiftExpression)), (Value::U8(96), Value::F64(5.), Err(Error::InvalidShiftExpression)), // Large shifts (Value::Generic(96), Value::Generic(32), Ok(Value::Generic(0))), (Value::I8(96), Value::U8(8), Ok(Value::I8(0))), (Value::I8(-96), Value::U8(8), Ok(Value::I8(-1))), (Value::I16(96), Value::U16(17), Ok(Value::I16(0))), (Value::I16(-96), Value::U16(17), Ok(Value::I16(-1))), (Value::I32(96), Value::U32(32), Ok(Value::I32(0))), (Value::I32(-96), Value::U32(32), Ok(Value::I32(-1))), (Value::I64(96), Value::U64(65), Ok(Value::I64(0))), (Value::I64(-96), Value::U64(65), Ok(Value::I64(-1))), ] { assert_eq!(v1.shra(v2, addr_mask), result); } } #[test] fn value_eq() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(3), Ok(Value::Generic(1))), (Value::Generic(!3), Value::Generic(3), Ok(Value::Generic(0))), (Value::I8(3), Value::I8(3), Ok(Value::Generic(1))), (Value::I8(!3), Value::I8(3), Ok(Value::Generic(0))), (Value::U8(3), Value::U8(3), Ok(Value::Generic(1))), (Value::U8(!3), Value::U8(3), Ok(Value::Generic(0))), (Value::I16(3), Value::I16(3), Ok(Value::Generic(1))), (Value::I16(!3), Value::I16(3), Ok(Value::Generic(0))), (Value::U16(3), Value::U16(3), Ok(Value::Generic(1))), (Value::U16(!3), Value::U16(3), Ok(Value::Generic(0))), (Value::I32(3), Value::I32(3), Ok(Value::Generic(1))), (Value::I32(!3), Value::I32(3), Ok(Value::Generic(0))), (Value::U32(3), Value::U32(3), Ok(Value::Generic(1))), (Value::U32(!3), Value::U32(3), Ok(Value::Generic(0))), (Value::I64(3), Value::I64(3), Ok(Value::Generic(1))), (Value::I64(!3), Value::I64(3), Ok(Value::Generic(0))), (Value::U64(3), Value::U64(3), Ok(Value::Generic(1))), (Value::U64(!3), Value::U64(3), Ok(Value::Generic(0))), (Value::F32(3.), Value::F32(3.), Ok(Value::Generic(1))), (Value::F32(-3.), Value::F32(3.), Ok(Value::Generic(0))), (Value::F64(3.), Value::F64(3.), Ok(Value::Generic(1))), (Value::F64(-3.), Value::F64(3.), Ok(Value::Generic(0))), (Value::Generic(3), Value::U32(3), Err(Error::TypeMismatch)), ] { assert_eq!(v1.eq(v2, addr_mask), result); } } #[test] fn value_ne() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(3), Ok(Value::Generic(0))), (Value::Generic(!3), Value::Generic(3), Ok(Value::Generic(1))), (Value::I8(3), Value::I8(3), Ok(Value::Generic(0))), (Value::I8(!3), Value::I8(3), Ok(Value::Generic(1))), (Value::U8(3), Value::U8(3), Ok(Value::Generic(0))), (Value::U8(!3), Value::U8(3), Ok(Value::Generic(1))), (Value::I16(3), Value::I16(3), Ok(Value::Generic(0))), (Value::I16(!3), Value::I16(3), Ok(Value::Generic(1))), (Value::U16(3), Value::U16(3), Ok(Value::Generic(0))), (Value::U16(!3), Value::U16(3), Ok(Value::Generic(1))), (Value::I32(3), Value::I32(3), Ok(Value::Generic(0))), (Value::I32(!3), Value::I32(3), Ok(Value::Generic(1))), (Value::U32(3), Value::U32(3), Ok(Value::Generic(0))), (Value::U32(!3), Value::U32(3), Ok(Value::Generic(1))), (Value::I64(3), Value::I64(3), Ok(Value::Generic(0))), (Value::I64(!3), Value::I64(3), Ok(Value::Generic(1))), (Value::U64(3), Value::U64(3), Ok(Value::Generic(0))), (Value::U64(!3), Value::U64(3), Ok(Value::Generic(1))), (Value::F32(3.), Value::F32(3.), Ok(Value::Generic(0))), (Value::F32(-3.), Value::F32(3.), Ok(Value::Generic(1))), (Value::F64(3.), Value::F64(3.), Ok(Value::Generic(0))), (Value::F64(-3.), Value::F64(3.), Ok(Value::Generic(1))), (Value::Generic(3), Value::U32(3), Err(Error::TypeMismatch)), ] { assert_eq!(v1.ne(v2, addr_mask), result); } } #[test] fn value_ge() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(!3), Ok(Value::Generic(1))), (Value::Generic(!3), Value::Generic(3), Ok(Value::Generic(0))), (Value::I8(3), Value::I8(!3), Ok(Value::Generic(1))), (Value::I8(!3), Value::I8(3), Ok(Value::Generic(0))), (Value::U8(3), Value::U8(!3), Ok(Value::Generic(0))), (Value::U8(!3), Value::U8(3), Ok(Value::Generic(1))), (Value::I16(3), Value::I16(!3), Ok(Value::Generic(1))), (Value::I16(!3), Value::I16(3), Ok(Value::Generic(0))), (Value::U16(3), Value::U16(!3), Ok(Value::Generic(0))), (Value::U16(!3), Value::U16(3), Ok(Value::Generic(1))), (Value::I32(3), Value::I32(!3), Ok(Value::Generic(1))), (Value::I32(!3), Value::I32(3), Ok(Value::Generic(0))), (Value::U32(3), Value::U32(!3), Ok(Value::Generic(0))), (Value::U32(!3), Value::U32(3), Ok(Value::Generic(1))), (Value::I64(3), Value::I64(!3), Ok(Value::Generic(1))), (Value::I64(!3), Value::I64(3), Ok(Value::Generic(0))), (Value::U64(3), Value::U64(!3), Ok(Value::Generic(0))), (Value::U64(!3), Value::U64(3), Ok(Value::Generic(1))), (Value::F32(3.), Value::F32(-3.), Ok(Value::Generic(1))), (Value::F32(-3.), Value::F32(3.), Ok(Value::Generic(0))), (Value::F64(3.), Value::F64(-3.), Ok(Value::Generic(1))), (Value::F64(-3.), Value::F64(3.), Ok(Value::Generic(0))), (Value::Generic(3), Value::U32(3), Err(Error::TypeMismatch)), ] { assert_eq!(v1.ge(v2, addr_mask), result); } } #[test] fn value_gt() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(!3), Ok(Value::Generic(1))), (Value::Generic(!3), Value::Generic(3), Ok(Value::Generic(0))), (Value::I8(3), Value::I8(!3), Ok(Value::Generic(1))), (Value::I8(!3), Value::I8(3), Ok(Value::Generic(0))), (Value::U8(3), Value::U8(!3), Ok(Value::Generic(0))), (Value::U8(!3), Value::U8(3), Ok(Value::Generic(1))), (Value::I16(3), Value::I16(!3), Ok(Value::Generic(1))), (Value::I16(!3), Value::I16(3), Ok(Value::Generic(0))), (Value::U16(3), Value::U16(!3), Ok(Value::Generic(0))), (Value::U16(!3), Value::U16(3), Ok(Value::Generic(1))), (Value::I32(3), Value::I32(!3), Ok(Value::Generic(1))), (Value::I32(!3), Value::I32(3), Ok(Value::Generic(0))), (Value::U32(3), Value::U32(!3), Ok(Value::Generic(0))), (Value::U32(!3), Value::U32(3), Ok(Value::Generic(1))), (Value::I64(3), Value::I64(!3), Ok(Value::Generic(1))), (Value::I64(!3), Value::I64(3), Ok(Value::Generic(0))), (Value::U64(3), Value::U64(!3), Ok(Value::Generic(0))), (Value::U64(!3), Value::U64(3), Ok(Value::Generic(1))), (Value::F32(3.), Value::F32(-3.), Ok(Value::Generic(1))), (Value::F32(-3.), Value::F32(3.), Ok(Value::Generic(0))), (Value::F64(3.), Value::F64(-3.), Ok(Value::Generic(1))), (Value::F64(-3.), Value::F64(3.), Ok(Value::Generic(0))), (Value::Generic(3), Value::U32(3), Err(Error::TypeMismatch)), ] { assert_eq!(v1.gt(v2, addr_mask), result); } } #[test] fn value_le() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(!3), Ok(Value::Generic(0))), (Value::Generic(!3), Value::Generic(3), Ok(Value::Generic(1))), (Value::I8(3), Value::I8(!3), Ok(Value::Generic(0))), (Value::I8(!3), Value::I8(3), Ok(Value::Generic(1))), (Value::U8(3), Value::U8(!3), Ok(Value::Generic(1))), (Value::U8(!3), Value::U8(3), Ok(Value::Generic(0))), (Value::I16(3), Value::I16(!3), Ok(Value::Generic(0))), (Value::I16(!3), Value::I16(3), Ok(Value::Generic(1))), (Value::U16(3), Value::U16(!3), Ok(Value::Generic(1))), (Value::U16(!3), Value::U16(3), Ok(Value::Generic(0))), (Value::I32(3), Value::I32(!3), Ok(Value::Generic(0))), (Value::I32(!3), Value::I32(3), Ok(Value::Generic(1))), (Value::U32(3), Value::U32(!3), Ok(Value::Generic(1))), (Value::U32(!3), Value::U32(3), Ok(Value::Generic(0))), (Value::I64(3), Value::I64(!3), Ok(Value::Generic(0))), (Value::I64(!3), Value::I64(3), Ok(Value::Generic(1))), (Value::U64(3), Value::U64(!3), Ok(Value::Generic(1))), (Value::U64(!3), Value::U64(3), Ok(Value::Generic(0))), (Value::F32(3.), Value::F32(-3.), Ok(Value::Generic(0))), (Value::F32(-3.), Value::F32(3.), Ok(Value::Generic(1))), (Value::F64(3.), Value::F64(-3.), Ok(Value::Generic(0))), (Value::F64(-3.), Value::F64(3.), Ok(Value::Generic(1))), (Value::Generic(3), Value::U32(3), Err(Error::TypeMismatch)), ] { assert_eq!(v1.le(v2, addr_mask), result); } } #[test] fn value_lt() { let addr_mask = 0xffff_ffff; for &(v1, v2, result) in &[ (Value::Generic(3), Value::Generic(!3), Ok(Value::Generic(0))), (Value::Generic(!3), Value::Generic(3), Ok(Value::Generic(1))), (Value::I8(3), Value::I8(!3), Ok(Value::Generic(0))), (Value::I8(!3), Value::I8(3), Ok(Value::Generic(1))), (Value::U8(3), Value::U8(!3), Ok(Value::Generic(1))), (Value::U8(!3), Value::U8(3), Ok(Value::Generic(0))), (Value::I16(3), Value::I16(!3), Ok(Value::Generic(0))), (Value::I16(!3), Value::I16(3), Ok(Value::Generic(1))), (Value::U16(3), Value::U16(!3), Ok(Value::Generic(1))), (Value::U16(!3), Value::U16(3), Ok(Value::Generic(0))), (Value::I32(3), Value::I32(!3), Ok(Value::Generic(0))), (Value::I32(!3), Value::I32(3), Ok(Value::Generic(1))), (Value::U32(3), Value::U32(!3), Ok(Value::Generic(1))), (Value::U32(!3), Value::U32(3), Ok(Value::Generic(0))), (Value::I64(3), Value::I64(!3), Ok(Value::Generic(0))), (Value::I64(!3), Value::I64(3), Ok(Value::Generic(1))), (Value::U64(3), Value::U64(!3), Ok(Value::Generic(1))), (Value::U64(!3), Value::U64(3), Ok(Value::Generic(0))), (Value::F32(3.), Value::F32(-3.), Ok(Value::Generic(0))), (Value::F32(-3.), Value::F32(3.), Ok(Value::Generic(1))), (Value::F64(3.), Value::F64(-3.), Ok(Value::Generic(0))), (Value::F64(-3.), Value::F64(3.), Ok(Value::Generic(1))), (Value::Generic(3), Value::U32(3), Err(Error::TypeMismatch)), ] { assert_eq!(v1.lt(v2, addr_mask), result); } } }