Skip to content

Commit b31f860

Browse files
committed
interpret: simplify pointer arithmetic logic
1 parent 5802bda commit b31f860

File tree

27 files changed

+73
-187
lines changed

27 files changed

+73
-187
lines changed

compiler/rustc_abi/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ impl Size {
517517
/// Truncates `value` to `self` bits and then sign-extends it to 128 bits
518518
/// (i.e., if it is negative, fill with 1's on the left).
519519
#[inline]
520-
pub fn sign_extend(self, value: u128) -> u128 {
520+
pub fn sign_extend(self, value: u128) -> i128 {
521521
let size = self.bits();
522522
if size == 0 {
523523
// Truncated until nothing is left.
@@ -527,7 +527,7 @@ impl Size {
527527
let shift = 128 - size;
528528
// Shift the unsigned value to the left, then shift back to the right as signed
529529
// (essentially fills with sign bit on the left).
530-
(((value << shift) as i128) >> shift) as u128
530+
((value << shift) as i128) >> shift
531531
}
532532

533533
/// Truncates `value` to `self` bits.
@@ -545,7 +545,7 @@ impl Size {
545545

546546
#[inline]
547547
pub fn signed_int_min(&self) -> i128 {
548-
self.sign_extend(1_u128 << (self.bits() - 1)) as i128
548+
self.sign_extend(1_u128 << (self.bits() - 1))
549549
}
550550

551551
#[inline]

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -561,17 +561,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
561561
self.frame().body
562562
}
563563

564-
#[inline(always)]
565-
pub fn sign_extend(&self, value: u128, ty: TyAndLayout<'_>) -> u128 {
566-
assert!(ty.abi.is_signed());
567-
ty.size.sign_extend(value)
568-
}
569-
570-
#[inline(always)]
571-
pub fn truncate(&self, value: u128, ty: TyAndLayout<'_>) -> u128 {
572-
ty.size.truncate(value)
573-
}
574-
575564
#[inline]
576565
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
577566
ty.is_freeze(*self.tcx, self.param_env)

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
211211
} else {
212212
(val_bits >> shift_bits) | (val_bits << inv_shift_bits)
213213
};
214-
let truncated_bits = self.truncate(result_bits, layout_val);
214+
let truncated_bits = layout_val.size.truncate(result_bits);
215215
let result = Scalar::from_uint(truncated_bits, layout_val.size);
216216
self.write_scalar(result, dest)?;
217217
}
@@ -585,13 +585,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
585585
ptr: Pointer<Option<M::Provenance>>,
586586
offset_bytes: i64,
587587
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
588-
// We first compute the pointer with overflow checks, to get a specific error for when it
589-
// overflows (though technically this is redundant with the following inbounds check).
590-
let result = ptr.signed_offset(offset_bytes, self)?;
591588
// The offset must be in bounds starting from `ptr`.
592589
self.check_ptr_access_signed(ptr, offset_bytes, CheckInAllocMsg::PointerArithmeticTest)?;
593-
// Done.
594-
Ok(result)
590+
// This also implies that there is no overflow, so we are done.
591+
Ok(ptr.wrapping_signed_offset(offset_bytes, self))
595592
}
596593

597594
/// Copy `count*size_of::<T>()` many bytes from `*src` to `*dst`.

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
477477
throw_ub!(PointerOutOfBounds {
478478
alloc_id,
479479
alloc_size,
480-
ptr_offset: self.target_usize_to_isize(offset),
480+
ptr_offset: self.sign_extend_to_target_isize(offset),
481481
inbounds_size: size,
482482
msg,
483483
})

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,8 @@ impl<'tcx, Prov: Provenance> Projectable<'tcx, Prov> for PlaceTy<'tcx, Prov> {
291291
// projections are type-checked and bounds-checked.
292292
assert!(offset + layout.size <= self.layout.size);
293293

294-
let new_offset = Size::from_bytes(
295-
ecx.data_layout()
296-
.offset(old_offset.unwrap_or(Size::ZERO).bytes(), offset.bytes())?,
297-
);
294+
// Size `+`, ensures no overflow.
295+
let new_offset = old_offset.unwrap_or(Size::ZERO) + offset;
298296

299297
PlaceTy {
300298
place: Place::Local { local, offset: Some(new_offset), locals_addr },

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
364364
// of the first element.
365365
let elem_size = first.layout.size;
366366
let first_ptr = first.ptr();
367-
let rest_ptr = first_ptr.offset(elem_size, self)?;
367+
let rest_ptr = first_ptr.wrapping_offset(elem_size, self);
368368
// No alignment requirement since `copy_op` above already checked it.
369369
self.mem_copy_repeatedly(
370370
first_ptr,

compiler/rustc_lint/src/types.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,7 @@ fn report_bin_hex_error(
315315
) {
316316
let (t, actually) = match ty {
317317
attr::IntType::SignedInt(t) => {
318-
let actually = if negative {
319-
-(size.sign_extend(val) as i128)
320-
} else {
321-
size.sign_extend(val) as i128
322-
};
318+
let actually = if negative { -(size.sign_extend(val)) } else { size.sign_extend(val) };
323319
(t.name_str(), actually.to_string())
324320
}
325321
attr::IntType::UnsignedInt(t) => {

compiler/rustc_middle/src/mir/interpret/pointer.rs

Lines changed: 11 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{AllocId, InterpResult};
1+
use super::AllocId;
22

33
use rustc_data_structures::static_assert_size;
44
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
@@ -39,62 +39,13 @@ pub trait PointerArithmetic: HasDataLayout {
3939
}
4040

4141
#[inline]
42-
fn target_usize_to_isize(&self, val: u64) -> i64 {
43-
let val = val as i64;
44-
// Now wrap-around into the machine_isize range.
45-
if val > self.target_isize_max() {
46-
// This can only happen if the ptr size is < 64, so we know max_usize_plus_1 fits into
47-
// i64.
48-
debug_assert!(self.pointer_size().bits() < 64);
49-
let max_usize_plus_1 = 1u128 << self.pointer_size().bits();
50-
val - i64::try_from(max_usize_plus_1).unwrap()
51-
} else {
52-
val
53-
}
54-
}
55-
56-
/// Helper function: truncate given value-"overflowed flag" pair to pointer size and
57-
/// update "overflowed flag" if there was an overflow.
58-
/// This should be called by all the other methods before returning!
59-
#[inline]
60-
fn truncate_to_ptr(&self, (val, over): (u64, bool)) -> (u64, bool) {
61-
let val = u128::from(val);
62-
let max_ptr_plus_1 = 1u128 << self.pointer_size().bits();
63-
(u64::try_from(val % max_ptr_plus_1).unwrap(), over || val >= max_ptr_plus_1)
64-
}
65-
66-
#[inline]
67-
fn overflowing_offset(&self, val: u64, i: u64) -> (u64, bool) {
68-
// We do not need to check if i fits in a machine usize. If it doesn't,
69-
// either the wrapping_add will wrap or res will not fit in a pointer.
70-
let res = val.overflowing_add(i);
71-
self.truncate_to_ptr(res)
72-
}
73-
74-
#[inline]
75-
fn overflowing_signed_offset(&self, val: u64, i: i64) -> (u64, bool) {
76-
// We need to make sure that i fits in a machine isize.
77-
let n = i.unsigned_abs();
78-
if i >= 0 {
79-
let (val, over) = self.overflowing_offset(val, n);
80-
(val, over || i > self.target_isize_max())
81-
} else {
82-
let res = val.overflowing_sub(n);
83-
let (val, over) = self.truncate_to_ptr(res);
84-
(val, over || i < self.target_isize_min())
85-
}
86-
}
87-
88-
#[inline]
89-
fn offset<'tcx>(&self, val: u64, i: u64) -> InterpResult<'tcx, u64> {
90-
let (res, over) = self.overflowing_offset(val, i);
91-
if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
42+
fn truncate_to_target_usize(&self, val: u64) -> u64 {
43+
self.pointer_size().truncate(val.into()).try_into().unwrap()
9244
}
9345

9446
#[inline]
95-
fn signed_offset<'tcx>(&self, val: u64, i: i64) -> InterpResult<'tcx, u64> {
96-
let (res, over) = self.overflowing_signed_offset(val, i);
97-
if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
47+
fn sign_extend_to_target_isize(&self, val: u64) -> i64 {
48+
self.pointer_size().sign_extend(val.into()).try_into().unwrap()
9849
}
9950
}
10051

@@ -330,7 +281,7 @@ impl<Prov> Pointer<Option<Prov>> {
330281
}
331282
}
332283

333-
impl<'tcx, Prov> Pointer<Prov> {
284+
impl<Prov> Pointer<Prov> {
334285
#[inline(always)]
335286
pub fn new(provenance: Prov, offset: Size) -> Self {
336287
Pointer { provenance, offset }
@@ -348,43 +299,16 @@ impl<'tcx, Prov> Pointer<Prov> {
348299
Pointer { provenance: f(self.provenance), ..self }
349300
}
350301

351-
#[inline]
352-
pub fn offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
353-
Ok(Pointer {
354-
offset: Size::from_bytes(cx.data_layout().offset(self.offset.bytes(), i.bytes())?),
355-
..self
356-
})
357-
}
358-
359-
#[inline]
360-
pub fn overflowing_offset(self, i: Size, cx: &impl HasDataLayout) -> (Self, bool) {
361-
let (res, over) = cx.data_layout().overflowing_offset(self.offset.bytes(), i.bytes());
362-
let ptr = Pointer { offset: Size::from_bytes(res), ..self };
363-
(ptr, over)
364-
}
365-
366302
#[inline(always)]
367303
pub fn wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
368-
self.overflowing_offset(i, cx).0
369-
}
370-
371-
#[inline]
372-
pub fn signed_offset(self, i: i64, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
373-
Ok(Pointer {
374-
offset: Size::from_bytes(cx.data_layout().signed_offset(self.offset.bytes(), i)?),
375-
..self
376-
})
377-
}
378-
379-
#[inline]
380-
pub fn overflowing_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> (Self, bool) {
381-
let (res, over) = cx.data_layout().overflowing_signed_offset(self.offset.bytes(), i);
382-
let ptr = Pointer { offset: Size::from_bytes(res), ..self };
383-
(ptr, over)
304+
let res =
305+
cx.data_layout().truncate_to_target_usize(self.offset.bytes().wrapping_add(i.bytes()));
306+
Pointer { offset: Size::from_bytes(res), ..self }
384307
}
385308

386309
#[inline(always)]
387310
pub fn wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
388-
self.overflowing_signed_offset(i, cx).0
311+
// It's wrapping anyway, so we can just cast to `u64`.
312+
self.wrapping_offset(Size::from_bytes(i as u64), cx)
389313
}
390314
}

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
397397
#[inline]
398398
pub fn to_int(self, size: Size) -> InterpResult<'tcx, i128> {
399399
let b = self.to_bits(size)?;
400-
Ok(size.sign_extend(b) as i128)
400+
Ok(size.sign_extend(b))
401401
}
402402

403403
/// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.

compiler/rustc_middle/src/ty/consts/int.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl ScalarInt {
233233
let data = i.into();
234234
// `into` performed sign extension, we have to truncate
235235
let r = Self::raw(size.truncate(data as u128), size);
236-
(r, size.sign_extend(r.data) as i128 != data)
236+
(r, size.sign_extend(r.data) != data)
237237
}
238238

239239
#[inline]
@@ -334,7 +334,7 @@ impl ScalarInt {
334334
#[inline]
335335
pub fn to_int(self, size: Size) -> i128 {
336336
let b = self.to_bits(size);
337-
size.sign_extend(b) as i128
337+
size.sign_extend(b)
338338
}
339339

340340
/// Converts the `ScalarInt` to i8.

0 commit comments

Comments
 (0)