Skip to content

Commit f62e43d

Browse files
committed
rustc: track validity ranges for layout::Abi::Scalar values.
1 parent 5df25c4 commit f62e43d

File tree

15 files changed

+294
-219
lines changed

15 files changed

+294
-219
lines changed

src/librustc/ty/layout.rs

Lines changed: 186 additions & 115 deletions
Large diffs are not rendered by default.

src/librustc_lint/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VariantSizeDifferences {
753753
bug!("failed to get layout for `{}`: {}", t, e)
754754
});
755755

756-
if let layout::Variants::Tagged { ref variants, discr, .. } = layout.variants {
757-
let discr_size = discr.size(cx.tcx).bytes();
756+
if let layout::Variants::Tagged { ref variants, ref discr, .. } = layout.variants {
757+
let discr_size = discr.value.size(cx.tcx).bytes();
758758

759759
debug!("enum `{}` is {} bytes large with layout:\n{:#?}",
760760
t, layout.size.bytes(), layout);

src/librustc_trans/abi.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ impl<'tcx> LayoutExt<'tcx> for TyLayout<'tcx> {
287287
fn homogeneous_aggregate<'a>(&self, ccx: &CrateContext<'a, 'tcx>) -> Option<Reg> {
288288
match self.abi {
289289
// The primitive for this algorithm.
290-
layout::Abi::Scalar(value) => {
291-
let kind = match value {
290+
layout::Abi::Scalar(ref scalar) => {
291+
let kind = match scalar.value {
292292
layout::Int(..) |
293293
layout::Pointer => RegKind::Integer,
294294
layout::F32 |
@@ -471,8 +471,8 @@ impl<'a, 'tcx> ArgType<'tcx> {
471471

472472
pub fn extend_integer_width_to(&mut self, bits: u64) {
473473
// Only integers have signedness
474-
match self.layout.abi {
475-
layout::Abi::Scalar(layout::Int(i, signed)) => {
474+
if let layout::Abi::Scalar(ref scalar) = self.layout.abi {
475+
if let layout::Int(i, signed) = scalar.value {
476476
if i.size().bits() < bits {
477477
self.attrs.set(if signed {
478478
ArgAttribute::SExt
@@ -481,8 +481,6 @@ impl<'a, 'tcx> ArgType<'tcx> {
481481
});
482482
}
483483
}
484-
485-
_ => {}
486484
}
487485
}
488486

@@ -695,9 +693,12 @@ impl<'a, 'tcx> FnType<'tcx> {
695693

696694
let arg_of = |ty: Ty<'tcx>, is_return: bool| {
697695
let mut arg = ArgType::new(ccx.layout_of(ty));
698-
if let layout::Abi::Scalar(layout::Int(layout::I1, _)) = arg.layout.abi {
699-
arg.attrs.set(ArgAttribute::ZExt);
700-
} else if arg.layout.is_zst() {
696+
if let layout::Abi::Scalar(ref scalar) = arg.layout.abi {
697+
if scalar.is_bool() {
698+
arg.attrs.set(ArgAttribute::ZExt);
699+
}
700+
}
701+
if arg.layout.is_zst() {
701702
// For some forsaken reason, x86_64-pc-windows-gnu
702703
// doesn't ignore zero-sized struct arguments.
703704
// The same is true for s390x-unknown-linux-gnu.

src/librustc_trans/base.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,12 @@ pub fn from_immediate(bcx: &Builder, val: ValueRef) -> ValueRef {
375375
}
376376

377377
pub fn to_immediate(bcx: &Builder, val: ValueRef, layout: layout::TyLayout) -> ValueRef {
378-
if let layout::Abi::Scalar(layout::Int(layout::I1, _)) = layout.abi {
379-
bcx.trunc(val, Type::i1(bcx.ccx))
380-
} else {
381-
val
378+
if let layout::Abi::Scalar(ref scalar) = layout.abi {
379+
if scalar.is_bool() {
380+
return bcx.trunc(val, Type::i1(bcx.ccx));
381+
}
382382
}
383+
val
383384
}
384385

385386
pub fn call_memcpy(b: &Builder,

src/librustc_trans/cabi_s390x.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ fn classify_ret_ty(ret: &mut ArgType) {
2727
fn is_single_fp_element<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
2828
layout: TyLayout<'tcx>) -> bool {
2929
match layout.abi {
30-
layout::Abi::Scalar(layout::F32) |
31-
layout::Abi::Scalar(layout::F64) => true,
30+
layout::Abi::Scalar(ref scalar) => {
31+
match scalar.value {
32+
layout::F32 | layout::F64 => true,
33+
_ => false
34+
}
35+
}
3236
layout::Abi::Aggregate { .. } => {
3337
if layout.fields.count() == 1 && layout.fields.offset(0).bytes() == 0 {
3438
is_single_fp_element(ccx, layout.field(ccx, 0))

src/librustc_trans/cabi_x86.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ pub enum Flavor {
2222
fn is_single_fp_element<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
2323
layout: TyLayout<'tcx>) -> bool {
2424
match layout.abi {
25-
layout::Abi::Scalar(layout::F32) |
26-
layout::Abi::Scalar(layout::F64) => true,
25+
layout::Abi::Scalar(ref scalar) => {
26+
match scalar.value {
27+
layout::F32 | layout::F64 => true,
28+
_ => false
29+
}
30+
}
2731
layout::Abi::Aggregate { .. } => {
2832
if layout.fields.count() == 1 && layout.fields.offset(0).bytes() == 0 {
2933
is_single_fp_element(ccx, layout.field(ccx, 0))

src/librustc_trans/cabi_x86_64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ fn classify_arg<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, arg: &ArgType<'tcx>)
6565
}
6666

6767
match layout.abi {
68-
layout::Abi::Scalar(value) => {
69-
let reg = match value {
68+
layout::Abi::Scalar(ref scalar) => {
69+
let reg = match scalar.value {
7070
layout::Int(..) |
7171
layout::Pointer => Class::Int,
7272
layout::F32 |

src/librustc_trans/debuginfo/metadata.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,11 +1429,13 @@ fn prepare_enum_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
14291429
let discriminant_type_metadata = match layout.variants {
14301430
layout::Variants::Single { .. } |
14311431
layout::Variants::NicheFilling { .. } => None,
1432-
layout::Variants::Tagged { discr, .. } => Some(discriminant_type_metadata(discr)),
1432+
layout::Variants::Tagged { ref discr, .. } => {
1433+
Some(discriminant_type_metadata(discr.value))
1434+
}
14331435
};
14341436

1435-
match (layout.abi, discriminant_type_metadata) {
1436-
(layout::Abi::Scalar(_), Some(discr)) => return FinalMetadata(discr),
1437+
match (&layout.abi, discriminant_type_metadata) {
1438+
(&layout::Abi::Scalar(_), Some(discr)) => return FinalMetadata(discr),
14371439
_ => {}
14381440
}
14391441

src/librustc_trans/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#![feature(i128_type)]
2727
#![feature(i128)]
2828
#![feature(inclusive_range)]
29+
#![feature(inclusive_range_syntax)]
2930
#![feature(libc)]
3031
#![feature(quote)]
3132
#![feature(rustc_diagnostic_macros)]

src/librustc_trans/mir/block.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,17 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
671671
(align | Alignment::Packed(arg.layout.align))
672672
.non_abi());
673673
} else {
674+
// We can't use `LvalueRef::load` here because the argument
675+
// may have a type we don't treat as immediate, but the ABI
676+
// used for this call is passing it by-value. In that case,
677+
// the load would just produce `OperandValue::Ref` instead
678+
// of the `OperandValue::Immediate` we need for the call.
674679
llval = bcx.load(llval, align.non_abi());
675-
}
676-
if let layout::Abi::Scalar(layout::Int(layout::I1, _)) = arg.layout.abi {
677-
bcx.range_metadata(llval, 0..2);
680+
if let layout::Abi::Scalar(ref scalar) = arg.layout.abi {
681+
if scalar.is_bool() {
682+
bcx.range_metadata(llval, 0..2);
683+
}
684+
}
678685
// We store bools as i8 so we need to truncate to i1.
679686
llval = base::to_immediate(bcx, llval, arg.layout);
680687
}

0 commit comments

Comments
 (0)