Skip to content

Commit f05b22e

Browse files
committed
Auto merge of #27845 - dylanmckay:abstract-pointer-size-away, r=alexcrichton
This patch rewrites code in several places which assume that the current target has either 32-bit or 64-bit pointers so that it can support arbitrary-width pointers. It does not completely remove all assumptions of pointer width, but it does reduce them significantly. There is a discussion [here](https://internals.rust-lang.org/t/adding-16-bit-pointer-support/2484/10) about the change.
2 parents 753a6a9 + ea7768c commit f05b22e

File tree

9 files changed

+47
-60
lines changed

9 files changed

+47
-60
lines changed

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#![feature(lang_items)]
8484
#![feature(no_std)]
8585
#![feature(nonzero)]
86+
#![feature(num_bits_bytes)]
8687
#![feature(optin_builtin_traits)]
8788
#![feature(placement_in_syntax)]
8889
#![feature(placement_new_protocol)]

src/liballoc/raw_vec.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use heap;
1515
use super::oom;
1616
use super::boxed::Box;
1717
use core::ops::Drop;
18+
use core;
1819

1920
/// A low-level utility for more ergonomically allocating, reallocating, and deallocating a
2021
/// a buffer of memory on the heap without having to worry about all the corner cases
@@ -443,11 +444,8 @@ impl<T> Drop for RawVec<T> {
443444
// user-space. e.g. PAE or x32
444445

445446
#[inline]
446-
#[cfg(target_pointer_width = "64")]
447-
fn alloc_guard(_alloc_size: usize) { }
448-
449-
#[inline]
450-
#[cfg(target_pointer_width = "32")]
451447
fn alloc_guard(alloc_size: usize) {
452-
assert!(alloc_size <= ::core::isize::MAX as usize, "capacity overflow");
448+
if core::usize::BITS < 64 {
449+
assert!(alloc_size <= ::core::isize::MAX as usize, "capacity overflow");
450+
}
453451
}

src/libcore/fmt/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1340,12 +1340,7 @@ impl<T> Pointer for *const T {
13401340
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
13411341

13421342
if let None = f.width {
1343-
// The formats need two extra bytes, for the 0x
1344-
if cfg!(target_pointer_width = "32") {
1345-
f.width = Some(10);
1346-
} else {
1347-
f.width = Some(18);
1348-
}
1343+
f.width = Some((::usize::BITS/4) + 2);
13491344
}
13501345
}
13511346
f.flags |= 1 << (FlagV1::Alternate as u32);

src/libcore/hash/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ pub trait Hasher {
144144
#[inline]
145145
#[stable(feature = "hasher_write", since = "1.3.0")]
146146
fn write_usize(&mut self, i: usize) {
147-
if cfg!(target_pointer_width = "32") {
148-
self.write_u32(i as u32)
149-
} else {
150-
self.write_u64(i as u64)
151-
}
147+
let bytes = unsafe {
148+
::slice::from_raw_parts(&i as *const usize as *const u8,
149+
mem::size_of::<usize>())
150+
};
151+
self.write(bytes);
152152
}
153153

154154
/// Write a single `i8` into this hasher.

src/libcore/iter.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,9 @@ step_impl_signed!(isize i8 i16 i32);
22362236
step_impl_unsigned!(u64);
22372237
#[cfg(target_pointer_width = "64")]
22382238
step_impl_signed!(i64);
2239-
#[cfg(target_pointer_width = "32")]
2239+
// If the target pointer width is not 64-bits, we
2240+
// assume here that it is less than 64-bits.
2241+
#[cfg(not(target_pointer_width = "64"))]
22402242
step_impl_no_between!(u64 i64);
22412243

22422244
/// An adapter for stepping range iterators by a custom amount.

src/librustc_trans/trans/base.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -943,11 +943,8 @@ pub fn call_lifetime_end(cx: Block, ptr: ValueRef) {
943943
pub fn call_memcpy(cx: Block, dst: ValueRef, src: ValueRef, n_bytes: ValueRef, align: u32) {
944944
let _icx = push_ctxt("call_memcpy");
945945
let ccx = cx.ccx();
946-
let key = match &ccx.sess().target.target.target_pointer_width[..] {
947-
"32" => "llvm.memcpy.p0i8.p0i8.i32",
948-
"64" => "llvm.memcpy.p0i8.p0i8.i64",
949-
tws => panic!("Unsupported target word size for memcpy: {}", tws),
950-
};
946+
let ptr_width = &ccx.sess().target.target.target_pointer_width[..];
947+
let key = format!("llvm.memcpy.p0i8.p0i8.i{}", ptr_width);
951948
let memcpy = ccx.get_intrinsic(&key);
952949
let src_ptr = PointerCast(cx, src, Type::i8p(ccx));
953950
let dst_ptr = PointerCast(cx, dst, Type::i8p(ccx));
@@ -996,12 +993,8 @@ fn memfill<'a, 'tcx>(b: &Builder<'a, 'tcx>, llptr: ValueRef, ty: Ty<'tcx>, byte:
996993
let ccx = b.ccx;
997994

998995
let llty = type_of::type_of(ccx, ty);
999-
1000-
let intrinsic_key = match &ccx.sess().target.target.target_pointer_width[..] {
1001-
"32" => "llvm.memset.p0i8.i32",
1002-
"64" => "llvm.memset.p0i8.i64",
1003-
tws => panic!("Unsupported target word size for memset: {}", tws),
1004-
};
996+
let ptr_width = &ccx.sess().target.target.target_pointer_width[..];
997+
let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
1005998

1006999
let llintrinsicfn = ccx.get_intrinsic(&intrinsic_key);
10071000
let llptr = b.pointercast(llptr, Type::i8(ccx).ptr_to());

src/librustc_trans/trans/common.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -833,10 +833,11 @@ pub fn C_u64(ccx: &CrateContext, i: u64) -> ValueRef {
833833
pub fn C_int<I: AsI64>(ccx: &CrateContext, i: I) -> ValueRef {
834834
let v = i.as_i64();
835835

836-
match machine::llbitsize_of_real(ccx, ccx.int_type()) {
837-
32 => assert!(v < (1<<31) && v >= -(1<<31)),
838-
64 => {},
839-
n => panic!("unsupported target size: {}", n)
836+
let bit_size = machine::llbitsize_of_real(ccx, ccx.int_type());
837+
838+
if bit_size < 64 {
839+
// make sure it doesn't overflow
840+
assert!(v < (1<<(bit_size-1)) && v >= -(1<<(bit_size-1)));
840841
}
841842

842843
C_integral(ccx.int_type(), v as u64, true)
@@ -845,10 +846,11 @@ pub fn C_int<I: AsI64>(ccx: &CrateContext, i: I) -> ValueRef {
845846
pub fn C_uint<I: AsU64>(ccx: &CrateContext, i: I) -> ValueRef {
846847
let v = i.as_u64();
847848

848-
match machine::llbitsize_of_real(ccx, ccx.int_type()) {
849-
32 => assert!(v < (1<<32)),
850-
64 => {},
851-
n => panic!("unsupported target size: {}", n)
849+
let bit_size = machine::llbitsize_of_real(ccx, ccx.int_type());
850+
851+
if bit_size < 64 {
852+
// make sure it doesn't overflow
853+
assert!(v < (1<<bit_size));
852854
}
853855

854856
C_integral(ccx.int_type(), v, false)

src/librustc_trans/trans/context.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,13 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
560560
self.local.builder.b
561561
}
562562

563-
pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
563+
pub fn get_intrinsic(&self, key: &str) -> ValueRef {
564564
if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
565565
return v;
566566
}
567567
match declare_intrinsic(self, key) {
568568
Some(v) => return v,
569-
None => panic!()
569+
None => panic!("unknown intrinsic '{}'", key)
570570
}
571571
}
572572

@@ -791,18 +791,18 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
791791
}
792792

793793
/// Declare any llvm intrinsics that you might need
794-
fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
794+
fn declare_intrinsic(ccx: &CrateContext, key: &str) -> Option<ValueRef> {
795795
macro_rules! ifn {
796796
($name:expr, fn() -> $ret:expr) => (
797-
if *key == $name {
797+
if key == $name {
798798
let f = declare::declare_cfn(ccx, $name, Type::func(&[], &$ret),
799799
ccx.tcx().mk_nil());
800800
ccx.intrinsics().borrow_mut().insert($name, f.clone());
801801
return Some(f);
802802
}
803803
);
804804
($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
805-
if *key == $name {
805+
if key == $name {
806806
let f = declare::declare_cfn(ccx, $name, Type::func(&[$($arg),*], &$ret),
807807
ccx.tcx().mk_nil());
808808
ccx.intrinsics().borrow_mut().insert($name, f.clone());
@@ -824,10 +824,13 @@ fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef
824824
let t_f32 = Type::f32(ccx);
825825
let t_f64 = Type::f64(ccx);
826826

827+
ifn!("llvm.memcpy.p0i8.p0i8.i16", fn(i8p, i8p, t_i16, t_i32, i1) -> void);
827828
ifn!("llvm.memcpy.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
828829
ifn!("llvm.memcpy.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
830+
ifn!("llvm.memmove.p0i8.p0i8.i16", fn(i8p, i8p, t_i16, t_i32, i1) -> void);
829831
ifn!("llvm.memmove.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
830832
ifn!("llvm.memmove.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
833+
ifn!("llvm.memset.p0i8.i16", fn(i8p, t_i8, t_i16, t_i32, i1) -> void);
831834
ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
832835
ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
833836

@@ -942,7 +945,7 @@ fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef
942945
if unsafe { llvm::LLVMVersionMinor() >= $llvm_version } {
943946
// The `if key == $name` is already in ifn!
944947
ifn!($name, fn($($arg),*) -> void);
945-
} else if *key == $name {
948+
} else if key == $name {
946949
let f = declare::declare_cfn(ccx, stringify!($cname),
947950
Type::func(&[$($arg),*], &void),
948951
ccx.tcx().mk_nil());
@@ -965,7 +968,7 @@ fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef
965968
if unsafe { llvm::LLVMVersionMinor() >= $llvm_version } {
966969
// The `if key == $name` is already in ifn!
967970
ifn!($name, fn($($arg),*) -> $ret);
968-
} else if *key == $name {
971+
} else if key == $name {
969972
let f = declare::declare_cfn(ccx, stringify!($cname),
970973
Type::func(&[$($arg),*], &$ret),
971974
ccx.tcx().mk_nil());

src/librustc_trans/trans/intrinsic.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -932,20 +932,15 @@ fn copy_intrinsic<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
932932
let align = C_i32(ccx, type_of::align_of(ccx, tp_ty) as i32);
933933
let size = machine::llsize_of(ccx, lltp_ty);
934934
let int_size = machine::llbitsize_of_real(ccx, ccx.int_type());
935-
let name = if allow_overlap {
936-
if int_size == 32 {
937-
"llvm.memmove.p0i8.p0i8.i32"
938-
} else {
939-
"llvm.memmove.p0i8.p0i8.i64"
940-
}
935+
936+
let operation = if allow_overlap {
937+
"memmove"
941938
} else {
942-
if int_size == 32 {
943-
"llvm.memcpy.p0i8.p0i8.i32"
944-
} else {
945-
"llvm.memcpy.p0i8.p0i8.i64"
946-
}
939+
"memcpy"
947940
};
948941

942+
let name = format!("llvm.{}.p0i8.p0i8.i{}", operation, int_size);
943+
949944
let dst_ptr = PointerCast(bcx, dst, Type::i8p(ccx));
950945
let src_ptr = PointerCast(bcx, src, Type::i8p(ccx));
951946
let llfn = ccx.get_intrinsic(&name);
@@ -973,11 +968,9 @@ fn memset_intrinsic<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
973968
let lltp_ty = type_of::type_of(ccx, tp_ty);
974969
let align = C_i32(ccx, type_of::align_of(ccx, tp_ty) as i32);
975970
let size = machine::llsize_of(ccx, lltp_ty);
976-
let name = if machine::llbitsize_of_real(ccx, ccx.int_type()) == 32 {
977-
"llvm.memset.p0i8.i32"
978-
} else {
979-
"llvm.memset.p0i8.i64"
980-
};
971+
let int_size = machine::llbitsize_of_real(ccx, ccx.int_type());
972+
973+
let name = format!("llvm.memset.p0i8.i{}", int_size);
981974

982975
let dst_ptr = PointerCast(bcx, dst, Type::i8p(ccx));
983976
let llfn = ccx.get_intrinsic(&name);

0 commit comments

Comments
 (0)