Skip to content

Commit 7738479

Browse files
committed
Auto merge of #33460 - shepmaster:16-bit-pointers, r=Aatch
Support 16-bit pointers as well as i/usize I'm opening this pull request to get some feedback from the community. Although Rust doesn't support any platforms with a native 16-bit pointer at the moment, the [AVR-Rust][ar] fork is working towards that goal. Keeping this forked logic up-to-date with the changes in master has been onerous so I'd like to merge these changes so that they get carried along when refactoring happens. I do not believe this should increase the maintenance burden. This is based on the original work of Dylan McKay (@dylanmckay). [ar]: https://github.com/avr-rust/rust
2 parents c81c750 + bc7595c commit 7738479

File tree

18 files changed

+123
-3
lines changed

18 files changed

+123
-3
lines changed

src/liballoc/raw_vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,9 @@ impl<T> Drop for RawVec<T> {
578578
// * We don't overflow `usize::MAX` and actually allocate too little
579579
//
580580
// On 64-bit we just need to check for overflow since trying to allocate
581-
// `> isize::MAX` bytes will surely fail. On 32-bit we need to add an extra
582-
// guard for this in case we're running on a platform which can use all 4GB in
583-
// user-space. e.g. PAE or x32
581+
// `> isize::MAX` bytes will surely fail. On 32-bit and 16-bit we need to add
582+
// an extra guard for this in case we're running on a platform which can use
583+
// all 4GB in user-space. e.g. PAE or x32
584584

585585
#[inline]
586586
fn alloc_guard(alloc_size: usize) {

src/libcore/fmt/num.rs

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ trait Int: Zero + PartialEq + PartialOrd + Div<Output=Self> + Rem<Output=Self> +
2929
Sub<Output=Self> + Copy {
3030
fn from_u8(u: u8) -> Self;
3131
fn to_u8(&self) -> u8;
32+
fn to_u16(&self) -> u16;
3233
fn to_u32(&self) -> u32;
3334
fn to_u64(&self) -> u64;
3435
}
@@ -37,6 +38,7 @@ macro_rules! doit {
3738
($($t:ident)*) => ($(impl Int for $t {
3839
fn from_u8(u: u8) -> $t { u as $t }
3940
fn to_u8(&self) -> u8 { *self as u8 }
41+
fn to_u16(&self) -> u16 { *self as u16 }
4042
fn to_u32(&self) -> u32 { *self as u32 }
4143
fn to_u64(&self) -> u64 { *self as u64 }
4244
})*)
@@ -256,6 +258,8 @@ macro_rules! impl_Display {
256258

257259
impl_Display!(i8, u8, i16, u16, i32, u32: to_u32);
258260
impl_Display!(i64, u64: to_u64);
261+
#[cfg(target_pointer_width = "16")]
262+
impl_Display!(isize, usize: to_u16);
259263
#[cfg(target_pointer_width = "32")]
260264
impl_Display!(isize, usize: to_u32);
261265
#[cfg(target_pointer_width = "64")]

src/libcore/mem.rs

+11
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,10 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
518518
#[stable(feature = "rust1", since = "1.0.0")]
519519
pub fn drop<T>(_x: T) { }
520520

521+
macro_rules! repeat_u8_as_u16 {
522+
($name:expr) => { (($name as u16) << 8 |
523+
($name as u16)) }
524+
}
521525
macro_rules! repeat_u8_as_u32 {
522526
($name:expr) => { (($name as u32) << 24 |
523527
($name as u32) << 16 |
@@ -543,11 +547,18 @@ macro_rules! repeat_u8_as_u64 {
543547
pub const POST_DROP_U8: u8 = 0x1d;
544548
#[unstable(feature = "filling_drop", issue = "5016")]
545549
#[allow(missing_docs)]
550+
pub const POST_DROP_U16: u16 = repeat_u8_as_u16!(POST_DROP_U8);
551+
#[unstable(feature = "filling_drop", issue = "5016")]
552+
#[allow(missing_docs)]
546553
pub const POST_DROP_U32: u32 = repeat_u8_as_u32!(POST_DROP_U8);
547554
#[unstable(feature = "filling_drop", issue = "5016")]
548555
#[allow(missing_docs)]
549556
pub const POST_DROP_U64: u64 = repeat_u8_as_u64!(POST_DROP_U8);
550557

558+
#[cfg(target_pointer_width = "16")]
559+
#[unstable(feature = "filling_drop", issue = "5016")]
560+
#[allow(missing_docs)]
561+
pub const POST_DROP_USIZE: usize = POST_DROP_U16 as usize;
551562
#[cfg(target_pointer_width = "32")]
552563
#[unstable(feature = "filling_drop", issue = "5016")]
553564
#[allow(missing_docs)]

src/libcore/num/isize.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
1515
#![stable(feature = "rust1", since = "1.0.0")]
1616

17+
#[cfg(target_pointer_width = "16")]
18+
int_module! { isize, 16 }
1719
#[cfg(target_pointer_width = "32")]
1820
int_module! { isize, 32 }
1921
#[cfg(target_pointer_width = "64")]

src/libcore/num/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,15 @@ impl i64 {
11761176
intrinsics::mul_with_overflow }
11771177
}
11781178

1179+
#[cfg(target_pointer_width = "16")]
1180+
#[lang = "isize"]
1181+
impl isize {
1182+
int_impl! { i16, u16, 16,
1183+
intrinsics::add_with_overflow,
1184+
intrinsics::sub_with_overflow,
1185+
intrinsics::mul_with_overflow }
1186+
}
1187+
11791188
#[cfg(target_pointer_width = "32")]
11801189
#[lang = "isize"]
11811190
impl isize {
@@ -2188,6 +2197,18 @@ impl u64 {
21882197
intrinsics::mul_with_overflow }
21892198
}
21902199

2200+
#[cfg(target_pointer_width = "16")]
2201+
#[lang = "usize"]
2202+
impl usize {
2203+
uint_impl! { u16, 16,
2204+
intrinsics::ctpop,
2205+
intrinsics::ctlz,
2206+
intrinsics::cttz,
2207+
intrinsics::bswap,
2208+
intrinsics::add_with_overflow,
2209+
intrinsics::sub_with_overflow,
2210+
intrinsics::mul_with_overflow }
2211+
}
21912212
#[cfg(target_pointer_width = "32")]
21922213
#[lang = "usize"]
21932214
impl usize {

src/libcore/num/usize.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
1515
#![stable(feature = "rust1", since = "1.0.0")]
1616

17+
#[cfg(target_pointer_width = "16")]
18+
uint_module! { usize, 16 }
1719
#[cfg(target_pointer_width = "32")]
1820
uint_module! { usize, 32 }
1921
#[cfg(target_pointer_width = "64")]

src/libcore/num/wrapping.rs

+6
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ wrapping_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
292292
mod shift_max {
293293
#![allow(non_upper_case_globals)]
294294

295+
#[cfg(target_pointer_width = "16")]
296+
mod platform {
297+
pub const usize: u32 = super::u16;
298+
pub const isize: u32 = super::i16;
299+
}
300+
295301
#[cfg(target_pointer_width = "32")]
296302
mod platform {
297303
pub const usize: u32 = super::u32;

src/libcoretest/mem.rs

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ fn size_of_basic() {
1818
assert_eq!(size_of::<u64>(), 8);
1919
}
2020

21+
#[test]
22+
#[cfg(target_pointer_width = "16")]
23+
fn size_of_16() {
24+
assert_eq!(size_of::<usize>(), 2);
25+
assert_eq!(size_of::<*const usize>(), 2);
26+
}
27+
2128
#[test]
2229
#[cfg(target_pointer_width = "32")]
2330
fn size_of_32() {
@@ -47,6 +54,13 @@ fn align_of_basic() {
4754
assert_eq!(align_of::<u32>(), 4);
4855
}
4956

57+
#[test]
58+
#[cfg(target_pointer_width = "16")]
59+
fn align_of_16() {
60+
assert_eq!(align_of::<usize>(), 2);
61+
assert_eq!(align_of::<*const usize>(), 2);
62+
}
63+
5064
#[test]
5165
#[cfg(target_pointer_width = "32")]
5266
fn align_of_32() {

src/librustc/session/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
796796
};
797797

798798
let (int_type, uint_type) = match &target.target_pointer_width[..] {
799+
"16" => (ast::IntTy::I16, ast::UintTy::U16),
799800
"32" => (ast::IntTy::I32, ast::UintTy::U32),
800801
"64" => (ast::IntTy::I64, ast::UintTy::U64),
801802
w => panic!(sp.fatal(&format!("target specification was invalid: \

src/librustc/ty/layout.rs

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ impl TargetDataLayout {
170170
/// address space on 64-bit ARMv8 and x86_64.
171171
pub fn obj_size_bound(&self) -> u64 {
172172
match self.pointer_size.bits() {
173+
16 => 1 << 15,
173174
32 => 1 << 31,
174175
64 => 1 << 47,
175176
bits => bug!("obj_size_bound: unknown pointer bit size {}", bits)
@@ -178,6 +179,7 @@ impl TargetDataLayout {
178179

179180
pub fn ptr_sized_integer(&self) -> Integer {
180181
match self.pointer_size.bits() {
182+
16 => I16,
181183
32 => I32,
182184
64 => I64,
183185
bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits)

src/librustc/ty/util.rs

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ impl IntTypeExt for attr::IntType {
6262
SignedInt(ast::IntTy::I32) => ConstInt::I32(0),
6363
SignedInt(ast::IntTy::I64) => ConstInt::I64(0),
6464
SignedInt(ast::IntTy::Is) => match tcx.sess.target.int_type {
65+
ast::IntTy::I16 => ConstInt::Isize(ConstIsize::Is16(0)),
6566
ast::IntTy::I32 => ConstInt::Isize(ConstIsize::Is32(0)),
6667
ast::IntTy::I64 => ConstInt::Isize(ConstIsize::Is64(0)),
6768
_ => bug!(),
@@ -71,6 +72,7 @@ impl IntTypeExt for attr::IntType {
7172
UnsignedInt(ast::UintTy::U32) => ConstInt::U32(0),
7273
UnsignedInt(ast::UintTy::U64) => ConstInt::U64(0),
7374
UnsignedInt(ast::UintTy::Us) => match tcx.sess.target.uint_type {
75+
ast::UintTy::U16 => ConstInt::Usize(ConstUsize::Us16(0)),
7476
ast::UintTy::U32 => ConstInt::Usize(ConstUsize::Us32(0)),
7577
ast::UintTy::U64 => ConstInt::Usize(ConstUsize::Us64(0)),
7678
_ => bug!(),

src/librustc_const_eval/eval.rs

+3
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,9 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
583583
(&LitKind::Int(n, Unsuffixed), Some(&ty::TyInt(IntTy::Is))) |
584584
(&LitKind::Int(n, Signed(IntTy::Is)), _) => {
585585
match tcx.sess.target.int_type {
586+
IntTy::I16 => if n == I16_OVERFLOW {
587+
return Ok(Integral(Isize(Is16(::std::i16::MIN))));
588+
},
586589
IntTy::I32 => if n == I32_OVERFLOW {
587590
return Ok(Integral(Isize(Is32(::std::i32::MIN))));
588591
},

0 commit comments

Comments
 (0)