Skip to content

Commit 39cfe70

Browse files
committed
CTFE: move target_{i, u}size_{min, max) to rustc_abi::TargetDataLayout
1 parent bf2f8ff commit 39cfe70

File tree

8 files changed

+37
-49
lines changed

8 files changed

+37
-49
lines changed

compiler/rustc_abi/src/lib.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,19 @@ impl TargetDataLayout {
354354
}
355355
}
356356

357-
/// Returns the theoretical maximum address.
358-
///
359-
/// Note that this doesn't take into account target-specific limitations.
360357
#[inline]
361-
pub fn max_address(&self) -> u64 {
362-
match self.pointer_size.bits() {
363-
16 => u16::MAX.into(),
364-
32 => u32::MAX.into(),
365-
64 => u64::MAX,
366-
bits => panic!("max_address: unknown pointer bit size {}", bits),
367-
}
358+
pub fn target_usize_max(&self) -> u64 {
359+
self.pointer_size.unsigned_int_max().try_into().unwrap()
360+
}
361+
362+
#[inline]
363+
pub fn target_isize_min(&self) -> i64 {
364+
self.pointer_size.signed_int_min().try_into().unwrap()
365+
}
366+
367+
#[inline]
368+
pub fn target_isize_max(&self) -> i64 {
369+
self.pointer_size.signed_int_max().try_into().unwrap()
368370
}
369371

370372
/// Returns the (inclusive) range of possible addresses for an allocation with
@@ -373,7 +375,7 @@ impl TargetDataLayout {
373375
/// Note that this doesn't take into account target-specific limitations.
374376
#[inline]
375377
pub fn address_range_for(&self, size: Size, align: Align) -> (u64, u64) {
376-
let end = Size::from_bytes(self.max_address());
378+
let end = Size::from_bytes(self.target_usize_max());
377379
let min = align.bytes();
378380
let max = (end - size).align_down_to(align).bytes();
379381
(min, max)

compiler/rustc_const_eval/src/const_eval/machine.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc_hir::def::DefKind;
22
use rustc_hir::{LangItem, CRATE_HIR_ID};
33
use rustc_middle::mir;
4-
use rustc_middle::mir::interpret::PointerArithmetic;
54
use rustc_middle::ty::layout::{FnAbiOf, TyAndLayout};
65
use rustc_middle::ty::{self, Ty, TyCtxt};
76
use rustc_session::lint::builtin::INVALID_ALIGNMENT;
@@ -17,7 +16,7 @@ use rustc_ast::Mutability;
1716
use rustc_hir::def_id::DefId;
1817
use rustc_middle::mir::AssertMessage;
1918
use rustc_span::symbol::{sym, Symbol};
20-
use rustc_target::abi::{Align, Size};
19+
use rustc_target::abi::{Align, HasDataLayout as _, Size};
2120
use rustc_target::spec::abi::Abi as CallAbi;
2221

2322
use crate::errors::{LongRunning, LongRunningWarn};
@@ -304,8 +303,8 @@ impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> {
304303
Ok(ControlFlow::Break(()))
305304
} else {
306305
// Not alignable in const, return `usize::MAX`.
307-
let usize_max = Scalar::from_target_usize(self.target_usize_max(), self);
308-
self.write_scalar(usize_max, dest)?;
306+
let usize_max = self.data_layout().target_usize_max();
307+
self.write_scalar(Scalar::from_target_usize(usize_max, self), dest)?;
309308
self.return_to_block(ret)?;
310309
Ok(ControlFlow::Break(()))
311310
}

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
use rustc_hir::def_id::DefId;
66
use rustc_middle::mir::{
77
self,
8-
interpret::{
9-
Allocation, ConstAllocation, ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar,
10-
},
8+
interpret::{Allocation, ConstAllocation, ConstValue, GlobalId, InterpResult, Scalar},
119
BinOp, NonDivergingIntrinsic,
1210
};
1311
use rustc_middle::ty;
1412
use rustc_middle::ty::layout::{LayoutOf as _, ValidityRequirement};
1513
use rustc_middle::ty::GenericArgsRef;
1614
use rustc_middle::ty::{Ty, TyCtxt};
1715
use rustc_span::symbol::{sym, Symbol};
18-
use rustc_target::abi::{Abi, Align, Primitive, Size};
16+
use rustc_target::abi::{Abi, Align, HasDataLayout as _, Primitive, Size};
1917

2018
use super::{
2119
util::ensure_monomorphic_enough, CheckInAllocMsg, ImmTy, InterpCx, Machine, OpTy, PlaceTy,
@@ -361,11 +359,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
361359
)?;
362360

363361
// Perform division by size to compute return value.
362+
let dl = self.data_layout();
364363
let ret_layout = if intrinsic_name == sym::ptr_offset_from_unsigned {
365-
assert!(0 <= dist && dist <= self.target_isize_max());
364+
assert!(0 <= dist && dist <= dl.target_isize_max());
366365
usize_layout
367366
} else {
368-
assert!(self.target_isize_min() <= dist && dist <= self.target_isize_max());
367+
assert!(dl.target_isize_min() <= dist && dist <= dl.target_isize_max());
369368
isize_layout
370369
};
371370
let pointee_layout = self.layout_of(instance_args.type_at(0))?;

compiler/rustc_const_eval/src/interpret/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
12441244
if offset > size.bytes() {
12451245
// If the pointer is out-of-bounds, we do not have a
12461246
// meaningful range to return.
1247-
0..=dl.max_address()
1247+
0..=dl.target_usize_max()
12481248
} else {
12491249
let (min, max) = dl.address_range_for(size, align);
12501250
(min + offset)..=(max + offset)

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

+8-22
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,19 @@ pub trait PointerArithmetic: HasDataLayout {
1919

2020
#[inline(always)]
2121
fn max_size_of_val(&self) -> Size {
22-
Size::from_bytes(self.target_isize_max())
23-
}
24-
25-
#[inline]
26-
fn target_usize_max(&self) -> u64 {
27-
self.pointer_size().unsigned_int_max().try_into().unwrap()
28-
}
29-
30-
#[inline]
31-
fn target_isize_min(&self) -> i64 {
32-
self.pointer_size().signed_int_min().try_into().unwrap()
33-
}
34-
35-
#[inline]
36-
fn target_isize_max(&self) -> i64 {
37-
self.pointer_size().signed_int_max().try_into().unwrap()
22+
Size::from_bytes(self.data_layout().target_isize_max())
3823
}
3924

4025
#[inline]
4126
fn target_usize_to_isize(&self, val: u64) -> i64 {
27+
let dl = self.data_layout();
4228
let val = val as i64;
4329
// Now wrap-around into the machine_isize range.
44-
if val > self.target_isize_max() {
30+
if val > dl.target_isize_max() {
4531
// This can only happen if the ptr size is < 64, so we know max_usize_plus_1 fits into
4632
// i64.
47-
debug_assert!(self.pointer_size().bits() < 64);
48-
let max_usize_plus_1 = 1u128 << self.pointer_size().bits();
33+
debug_assert!(dl.pointer_size.bits() < 64);
34+
let max_usize_plus_1 = 1u128 << dl.pointer_size.bits();
4935
val - i64::try_from(max_usize_plus_1).unwrap()
5036
} else {
5137
val
@@ -58,7 +44,7 @@ pub trait PointerArithmetic: HasDataLayout {
5844
#[inline]
5945
fn truncate_to_ptr(&self, (val, over): (u64, bool)) -> (u64, bool) {
6046
let val = u128::from(val);
61-
let max_ptr_plus_1 = 1u128 << self.pointer_size().bits();
47+
let max_ptr_plus_1 = 1u128 << self.data_layout().pointer_size.bits();
6248
(u64::try_from(val % max_ptr_plus_1).unwrap(), over || val >= max_ptr_plus_1)
6349
}
6450

@@ -76,11 +62,11 @@ pub trait PointerArithmetic: HasDataLayout {
7662
let n = i.unsigned_abs();
7763
if i >= 0 {
7864
let (val, over) = self.overflowing_offset(val, n);
79-
(val, over || i > self.target_isize_max())
65+
(val, over || i > self.data_layout().target_isize_max())
8066
} else {
8167
let res = val.overflowing_sub(n);
8268
let (val, over) = self.truncate_to_ptr(res);
83-
(val, over || i < self.target_isize_min())
69+
(val, over || i < self.data_layout().target_isize_min())
8470
}
8571
}
8672

src/tools/miri/src/intptrcast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl<'mir, 'tcx> GlobalStateInner {
207207
.checked_add(max(size.bytes(), 1))
208208
.ok_or_else(|| err_exhaust!(AddressSpaceFull))?;
209209
// Even if `Size` didn't overflow, we might still have filled up the address space.
210-
if global_state.next_base_addr > ecx.target_usize_max() {
210+
if global_state.next_base_addr > ecx.data_layout().target_usize_max() {
211211
throw_exhaust!(AddressSpaceFull);
212212
}
213213
// Given that `next_base_addr` increases in each allocation, pushing the

src/tools/miri/src/shims/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use log::trace;
2121

2222
use rustc_middle::{mir, ty};
2323
use rustc_target::spec::abi::Abi;
24+
use rustc_target::abi::HasDataLayout as _;
2425

2526
use crate::*;
2627
use helpers::check_arg_count;
@@ -108,7 +109,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
108109
}
109110

110111
// Return error result (usize::MAX), and jump to caller.
111-
this.write_scalar(Scalar::from_target_usize(this.target_usize_max(), this), dest)?;
112+
let usize_max = this.data_layout().target_usize_max();
113+
this.write_scalar(Scalar::from_target_usize(usize_max, this), dest)?;
112114
this.go_to_block(ret);
113115
Ok(true)
114116
}

src/tools/miri/src/shims/unix/fs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use log::trace;
1212

1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_middle::ty::TyCtxt;
15-
use rustc_target::abi::{Align, Size};
15+
use rustc_target::abi::{Align, Size, HasDataLayout as _};
1616

1717
use crate::shims::os_str::bytes_to_os_str;
1818
use crate::*;
@@ -753,7 +753,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
753753
// We cap the number of read bytes to the largest value that we are able to fit in both the
754754
// host's and target's `isize`. This saves us from having to handle overflows later.
755755
let count = count
756-
.min(u64::try_from(this.target_isize_max()).unwrap())
756+
.min(u64::try_from(this.data_layout().target_isize_max()).unwrap())
757757
.min(u64::try_from(isize::MAX).unwrap());
758758
let communicate = this.machine.communicate();
759759

@@ -807,7 +807,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
807807
// We cap the number of written bytes to the largest value that we are able to fit in both the
808808
// host's and target's `isize`. This saves us from having to handle overflows later.
809809
let count = count
810-
.min(u64::try_from(this.target_isize_max()).unwrap())
810+
.min(u64::try_from(this.data_layout().target_isize_max()).unwrap())
811811
.min(u64::try_from(isize::MAX).unwrap());
812812
let communicate = this.machine.communicate();
813813

0 commit comments

Comments
 (0)