2012-08-28 17:54:45 -05:00
|
|
|
//!
|
|
|
|
//
|
2015-03-14 21:01:57 -05:00
|
|
|
// Code relating to drop glue.
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2019-02-09 08:31:47 -06:00
|
|
|
use crate::common::IntPredicate;
|
|
|
|
use crate::meth;
|
|
|
|
use crate::traits::*;
|
2019-12-22 16:42:04 -06:00
|
|
|
use rustc::ty::{self, Ty};
|
2013-06-16 05:52:44 -05:00
|
|
|
|
2019-06-14 11:39:39 -05:00
|
|
|
pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
|
2018-10-05 08:08:49 -05:00
|
|
|
bx: &mut Bx,
|
2018-08-07 10:14:40 -05:00
|
|
|
t: Ty<'tcx>,
|
2019-06-16 04:41:24 -05:00
|
|
|
info: Option<Bx::Value>,
|
2018-09-14 10:48:57 -05:00
|
|
|
) -> (Bx::Value, Bx::Value) {
|
2018-11-27 12:00:25 -06:00
|
|
|
let layout = bx.layout_of(t);
|
2019-12-22 16:42:04 -06:00
|
|
|
debug!("size_and_align_of_dst(ty={}, info={:?}): layout: {:?}", t, info, layout);
|
2018-09-08 14:14:55 -05:00
|
|
|
if !layout.is_unsized() {
|
2018-11-27 12:00:25 -06:00
|
|
|
let size = bx.const_usize(layout.size.bytes());
|
|
|
|
let align = bx.const_usize(layout.align.abi.bytes());
|
2014-08-06 04:59:40 -05:00
|
|
|
return (size, align);
|
|
|
|
}
|
2019-09-16 13:08:35 -05:00
|
|
|
match t.kind {
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Dynamic(..) => {
|
2017-06-25 04:41:24 -05:00
|
|
|
// load size/align from vtable
|
2018-07-10 05:28:39 -05:00
|
|
|
let vtable = info.unwrap();
|
|
|
|
(meth::SIZE.get_usize(bx, vtable), meth::ALIGN.get_usize(bx, vtable))
|
2017-06-25 04:41:24 -05:00
|
|
|
}
|
2018-08-21 19:35:55 -05:00
|
|
|
ty::Slice(_) | ty::Str => {
|
2018-11-27 12:00:25 -06:00
|
|
|
let unit = layout.field(bx, 0);
|
2017-06-25 04:41:24 -05:00
|
|
|
// The info in this case is the length of the str, so the size is that
|
|
|
|
// times the unit size.
|
2019-12-22 16:42:04 -06:00
|
|
|
(
|
|
|
|
bx.mul(info.unwrap(), bx.const_usize(unit.size.bytes())),
|
|
|
|
bx.const_usize(unit.align.abi.bytes()),
|
|
|
|
)
|
2017-06-25 04:41:24 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2014-08-06 04:59:40 -05:00
|
|
|
// First get the size of all statically known fields.
|
2017-03-01 21:35:25 -06:00
|
|
|
// Don't use size_of because it also rounds up to alignment, which we
|
|
|
|
// want to avoid, as the unsized field's alignment could be smaller.
|
2015-08-06 10:25:15 -05:00
|
|
|
assert!(!t.is_simd());
|
2016-08-27 00:51:55 -05:00
|
|
|
debug!("DST {} layout: {:?}", t, layout);
|
|
|
|
|
2017-09-17 15:37:18 -05:00
|
|
|
let i = layout.fields.count() - 1;
|
|
|
|
let sized_size = layout.fields.offset(i).bytes();
|
2018-09-08 16:22:22 -05:00
|
|
|
let sized_align = layout.align.abi.bytes();
|
2019-12-22 16:42:04 -06:00
|
|
|
debug!("DST {} statically sized prefix size: {} align: {}", t, sized_size, sized_align);
|
2018-11-27 12:00:25 -06:00
|
|
|
let sized_size = bx.const_usize(sized_size);
|
|
|
|
let sized_align = bx.const_usize(sized_align);
|
2014-08-06 04:59:40 -05:00
|
|
|
|
|
|
|
// Recurse to get the size of the dynamically sized field (must be
|
|
|
|
// the last field).
|
2018-11-27 12:00:25 -06:00
|
|
|
let field_ty = layout.field(bx, i).ty;
|
2018-01-04 23:12:32 -06:00
|
|
|
let (unsized_size, mut unsized_align) = size_and_align_of_dst(bx, field_ty, info);
|
2014-08-06 04:59:40 -05:00
|
|
|
|
2015-07-29 12:43:26 -05:00
|
|
|
// FIXME (#26403, #27023): We should be adding padding
|
2015-07-27 07:45:20 -05:00
|
|
|
// to `sized_size` (to accommodate the `unsized_align`
|
|
|
|
// required of the unsized field that follows) before
|
2015-07-29 12:43:26 -05:00
|
|
|
// summing it with `sized_size`. (Note that since #26403
|
|
|
|
// is unfixed, we do not yet add the necessary padding
|
|
|
|
// here. But this is where the add would go.)
|
2015-07-27 07:45:20 -05:00
|
|
|
|
2014-08-06 04:59:40 -05:00
|
|
|
// Return the sum of sizes and max of aligns.
|
2018-01-04 23:12:32 -06:00
|
|
|
let size = bx.add(sized_size, unsized_size);
|
2015-07-27 07:45:20 -05:00
|
|
|
|
2017-12-01 10:29:35 -06:00
|
|
|
// Packed types ignore the alignment of their fields.
|
2019-09-16 13:08:35 -05:00
|
|
|
if let ty::Adt(def, _) = t.kind {
|
2017-12-01 10:29:35 -06:00
|
|
|
if def.repr.packed() {
|
|
|
|
unsized_align = sized_align;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 09:53:57 -05:00
|
|
|
// Choose max of two known alignments (combined value must
|
|
|
|
// be aligned according to more restrictive of the two).
|
2019-12-22 16:42:04 -06:00
|
|
|
let align = match (
|
|
|
|
bx.const_to_opt_u128(sized_align, false),
|
|
|
|
bx.const_to_opt_u128(unsized_align, false),
|
|
|
|
) {
|
2015-12-06 07:38:29 -06:00
|
|
|
(Some(sized_align), Some(unsized_align)) => {
|
|
|
|
// If both alignments are constant, (the sized_align should always be), then
|
|
|
|
// pick the correct alignment statically.
|
2018-11-27 12:00:25 -06:00
|
|
|
bx.const_usize(std::cmp::max(sized_align, unsized_align) as u64)
|
2018-10-05 08:08:49 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let cmp = bx.icmp(IntPredicate::IntUGT, sized_align, unsized_align);
|
|
|
|
bx.select(cmp, sized_align, unsized_align)
|
2015-12-06 07:38:29 -06:00
|
|
|
}
|
|
|
|
};
|
2015-07-27 07:45:20 -05:00
|
|
|
|
2015-07-27 09:53:57 -05:00
|
|
|
// Issue #27023: must add any necessary padding to `size`
|
|
|
|
// (to make it a multiple of `align`) before returning it.
|
|
|
|
//
|
|
|
|
// Namely, the returned size should be, in C notation:
|
|
|
|
//
|
|
|
|
// `size + ((size & (align-1)) ? align : 0)`
|
|
|
|
//
|
2015-07-29 15:18:39 -05:00
|
|
|
// emulated via the semi-standard fast bit trick:
|
2015-07-27 09:53:57 -05:00
|
|
|
//
|
2015-12-06 07:38:29 -06:00
|
|
|
// `(size + (align-1)) & -align`
|
2018-11-27 12:00:25 -06:00
|
|
|
let one = bx.const_usize(1);
|
2018-10-05 08:08:49 -05:00
|
|
|
let addend = bx.sub(align, one);
|
|
|
|
let add = bx.add(size, addend);
|
2019-12-22 16:42:04 -06:00
|
|
|
let neg = bx.neg(align);
|
2018-10-05 08:08:49 -05:00
|
|
|
let size = bx.and(add, neg);
|
2015-07-27 07:45:20 -05:00
|
|
|
|
2014-08-06 04:59:40 -05:00
|
|
|
(size, align)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|