From 415ecc848d776e1629840420d3a48fbf8d817a76 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 25 Dec 2019 08:42:14 -0500 Subject: [PATCH 1/2] Add Scalar::to_(u|i)16 methods --- src/librustc/mir/interpret/value.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs index 49b542af0a034..4c59ef5851c72 100644 --- a/src/librustc/mir/interpret/value.rs +++ b/src/librustc/mir/interpret/value.rs @@ -421,6 +421,12 @@ impl<'tcx, Tag> Scalar { Ok(b as u8) } + pub fn to_u16(self) -> InterpResult<'static, u16> { + let sz = Size::from_bits(16); + let b = self.to_bits(sz)?; + Ok(b as u16) + } + pub fn to_u32(self) -> InterpResult<'static, u32> { let sz = Size::from_bits(32); let b = self.to_bits(sz)?; @@ -445,6 +451,13 @@ impl<'tcx, Tag> Scalar { Ok(b as i8) } + pub fn to_i16(self) -> InterpResult<'static, i16> { + let sz = Size::from_bits(16); + let b = self.to_bits(sz)?; + let b = sign_extend(b, sz) as i128; + Ok(b as i16) + } + pub fn to_i32(self) -> InterpResult<'static, i32> { let sz = Size::from_bits(32); let b = self.to_bits(sz)?; From dfcc44d76903fe68ceab8461cc55efe475c26b8b Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 25 Dec 2019 10:41:50 -0500 Subject: [PATCH 2/2] rewrite scalar to integer methods --- src/librustc/mir/interpret/value.rs | 57 +++++++++++++++-------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs index 4c59ef5851c72..e8e1678561096 100644 --- a/src/librustc/mir/interpret/value.rs +++ b/src/librustc/mir/interpret/value.rs @@ -415,28 +415,30 @@ impl<'tcx, Tag> Scalar { } } + #[inline] + fn to_unsigned_with_bit_width(self, bits: u64) -> InterpResult<'static, u128> { + let sz = Size::from_bits(bits); + self.to_bits(sz) + } + + /// Converts the scalar to produce an `u8`. Fails if the scalar is a pointer. pub fn to_u8(self) -> InterpResult<'static, u8> { - let sz = Size::from_bits(8); - let b = self.to_bits(sz)?; - Ok(b as u8) + self.to_unsigned_with_bit_width(8).map(|v| v as u8) } + /// Converts the scalar to produce an `u16`. Fails if the scalar is a pointer. pub fn to_u16(self) -> InterpResult<'static, u16> { - let sz = Size::from_bits(16); - let b = self.to_bits(sz)?; - Ok(b as u16) + self.to_unsigned_with_bit_width(16).map(|v| v as u16) } + /// Converts the scalar to produce an `u32`. Fails if the scalar is a pointer. pub fn to_u32(self) -> InterpResult<'static, u32> { - let sz = Size::from_bits(32); - let b = self.to_bits(sz)?; - Ok(b as u32) + self.to_unsigned_with_bit_width(32).map(|v| v as u32) } + /// Converts the scalar to produce an `u64`. Fails if the scalar is a pointer. pub fn to_u64(self) -> InterpResult<'static, u64> { - let sz = Size::from_bits(64); - let b = self.to_bits(sz)?; - Ok(b as u64) + self.to_unsigned_with_bit_width(64).map(|v| v as u64) } pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> { @@ -444,32 +446,31 @@ impl<'tcx, Tag> Scalar { Ok(b as u64) } - pub fn to_i8(self) -> InterpResult<'static, i8> { - let sz = Size::from_bits(8); + #[inline] + fn to_signed_with_bit_width(self, bits: u64) -> InterpResult<'static, i128> { + let sz = Size::from_bits(bits); let b = self.to_bits(sz)?; - let b = sign_extend(b, sz) as i128; - Ok(b as i8) + Ok(sign_extend(b, sz) as i128) } + /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer. + pub fn to_i8(self) -> InterpResult<'static, i8> { + self.to_signed_with_bit_width(8).map(|v| v as i8) + } + + /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer. pub fn to_i16(self) -> InterpResult<'static, i16> { - let sz = Size::from_bits(16); - let b = self.to_bits(sz)?; - let b = sign_extend(b, sz) as i128; - Ok(b as i16) + self.to_signed_with_bit_width(16).map(|v| v as i16) } + /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer. pub fn to_i32(self) -> InterpResult<'static, i32> { - let sz = Size::from_bits(32); - let b = self.to_bits(sz)?; - let b = sign_extend(b, sz) as i128; - Ok(b as i32) + self.to_signed_with_bit_width(32).map(|v| v as i32) } + /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer. pub fn to_i64(self) -> InterpResult<'static, i64> { - let sz = Size::from_bits(64); - let b = self.to_bits(sz)?; - let b = sign_extend(b, sz) as i128; - Ok(b as i64) + self.to_signed_with_bit_width(64).map(|v| v as i64) } pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {