Skip to content

Commit f80ff7d

Browse files
authored
Auto merge of #34174 - shepmaster:16-bit-mir, r=Aatch
Support 16-bit pointers in MIR
2 parents 0554aba + 1858cfb commit f80ff7d

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

src/librustc_const_eval/eval.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -945,32 +945,23 @@ fn infer<'a, 'tcx>(i: ConstInt,
945945
(&ty::TyInt(IntTy::I32), Infer(i)) => Ok(I32(i as i64 as i32)),
946946
(&ty::TyInt(IntTy::I64), Infer(i)) => Ok(I64(i as i64)),
947947
(&ty::TyInt(IntTy::Is), Infer(i)) => {
948-
match ConstIsize::new(i as i64, tcx.sess.target.int_type) {
949-
Ok(val) => Ok(Isize(val)),
950-
Err(_) => Ok(Isize(ConstIsize::Is32(i as i64 as i32))),
951-
}
948+
Ok(Isize(ConstIsize::new_truncating(i as i64, tcx.sess.target.int_type)))
952949
},
953950

954951
(&ty::TyInt(IntTy::I8), InferSigned(i)) => Ok(I8(i as i8)),
955952
(&ty::TyInt(IntTy::I16), InferSigned(i)) => Ok(I16(i as i16)),
956953
(&ty::TyInt(IntTy::I32), InferSigned(i)) => Ok(I32(i as i32)),
957954
(&ty::TyInt(IntTy::I64), InferSigned(i)) => Ok(I64(i)),
958955
(&ty::TyInt(IntTy::Is), InferSigned(i)) => {
959-
match ConstIsize::new(i, tcx.sess.target.int_type) {
960-
Ok(val) => Ok(Isize(val)),
961-
Err(_) => Ok(Isize(ConstIsize::Is32(i as i32))),
962-
}
956+
Ok(Isize(ConstIsize::new_truncating(i, tcx.sess.target.int_type)))
963957
},
964958

965959
(&ty::TyUint(UintTy::U8), Infer(i)) => Ok(U8(i as u8)),
966960
(&ty::TyUint(UintTy::U16), Infer(i)) => Ok(U16(i as u16)),
967961
(&ty::TyUint(UintTy::U32), Infer(i)) => Ok(U32(i as u32)),
968962
(&ty::TyUint(UintTy::U64), Infer(i)) => Ok(U64(i)),
969963
(&ty::TyUint(UintTy::Us), Infer(i)) => {
970-
match ConstUsize::new(i, tcx.sess.target.uint_type) {
971-
Ok(val) => Ok(Usize(val)),
972-
Err(_) => Ok(Usize(ConstUsize::Us32(i as u32))),
973-
}
964+
Ok(Usize(ConstUsize::new_truncating(i, tcx.sess.target.uint_type)))
974965
},
975966
(&ty::TyUint(_), InferSigned(_)) => Err(IntermediateUnsignedNegative),
976967

@@ -1052,20 +1043,14 @@ fn cast_const_int<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, val: ConstInt, ty: ty::
10521043
ty::TyInt(ast::IntTy::I32) => Ok(Integral(I32(v as i64 as i32))),
10531044
ty::TyInt(ast::IntTy::I64) => Ok(Integral(I64(v as i64))),
10541045
ty::TyInt(ast::IntTy::Is) => {
1055-
match ConstIsize::new(v as i64, tcx.sess.target.int_type) {
1056-
Ok(val) => Ok(Integral(Isize(val))),
1057-
Err(_) => Ok(Integral(Isize(ConstIsize::Is32(v as i64 as i32)))),
1058-
}
1046+
Ok(Integral(Isize(ConstIsize::new_truncating(v as i64, tcx.sess.target.int_type))))
10591047
},
10601048
ty::TyUint(ast::UintTy::U8) => Ok(Integral(U8(v as u8))),
10611049
ty::TyUint(ast::UintTy::U16) => Ok(Integral(U16(v as u16))),
10621050
ty::TyUint(ast::UintTy::U32) => Ok(Integral(U32(v as u32))),
10631051
ty::TyUint(ast::UintTy::U64) => Ok(Integral(U64(v))),
10641052
ty::TyUint(ast::UintTy::Us) => {
1065-
match ConstUsize::new(v, tcx.sess.target.uint_type) {
1066-
Ok(val) => Ok(Integral(Usize(val))),
1067-
Err(_) => Ok(Integral(Usize(ConstUsize::Us32(v as u32)))),
1068-
}
1053+
Ok(Integral(Usize(ConstUsize::new_truncating(v, tcx.sess.target.uint_type))))
10691054
},
10701055
ty::TyFloat(ast::FloatTy::F64) => match val.erase_type() {
10711056
Infer(u) => Ok(Float(F64(u as f64))),

src/librustc_const_math/is.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ impl ConstIsize {
2727
(Is16(i), ast::IntTy::I16) => i as i64,
2828
(Is32(i), ast::IntTy::I32) => i as i64,
2929
(Is64(i), ast::IntTy::I64) => i,
30-
_ => panic!("got invalid isize size for target"),
30+
_ => panic!("unable to convert self ({:?}) to target isize ({:?})",
31+
self, target_int_ty),
3132
}
3233
}
3334
pub fn new(i: i64, target_int_ty: ast::IntTy) -> Result<Self, ConstMathErr> {
@@ -40,4 +41,12 @@ impl ConstIsize {
4041
_ => unreachable!(),
4142
}
4243
}
44+
pub fn new_truncating(i: i64, target_int_ty: ast::IntTy) -> Self {
45+
match target_int_ty {
46+
ast::IntTy::I16 => Is16(i as i16),
47+
ast::IntTy::I32 => Is32(i as i32),
48+
ast::IntTy::I64 => Is64(i),
49+
_ => unreachable!(),
50+
}
51+
}
4352
}

src/librustc_const_math/us.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ impl ConstUsize {
2727
(Us16(i), ast::UintTy::U16) => i as u64,
2828
(Us32(i), ast::UintTy::U32) => i as u64,
2929
(Us64(i), ast::UintTy::U64) => i,
30-
_ => panic!("got invalid usize size for target"),
30+
_ => panic!("unable to convert self ({:?}) to target usize ({:?})",
31+
self, target_uint_ty),
3132
}
3233
}
3334
pub fn new(i: u64, target_uint_ty: ast::UintTy) -> Result<Self, ConstMathErr> {
@@ -40,4 +41,12 @@ impl ConstUsize {
4041
_ => unreachable!(),
4142
}
4243
}
44+
pub fn new_truncating(i: u64, target_uint_ty: ast::UintTy) -> Self {
45+
match target_uint_ty {
46+
ast::UintTy::U16 => Us16(i as u16),
47+
ast::UintTy::U32 => Us32(i as u32),
48+
ast::UintTy::U64 => Us64(i),
49+
_ => unreachable!(),
50+
}
51+
}
4352
}

src/librustc_mir/build/expr/as_rvalue.rs

+1
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
368368
ast::IntTy::Is => {
369369
let int_ty = self.hir.tcx().sess.target.int_type;
370370
let min = match int_ty {
371+
ast::IntTy::I16 => std::i16::MIN as i64,
371372
ast::IntTy::I32 => std::i32::MIN as i64,
372373
ast::IntTy::I64 => std::i64::MIN,
373374
_ => unreachable!()

0 commit comments

Comments
 (0)