2013-05-30 10:16:33 +00:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-08-28 22:54:45 +00:00
|
|
|
//!
|
|
|
|
//
|
2015-03-15 02:01:57 +00:00
|
|
|
// Code relating to drop glue.
|
2012-08-28 22:54:45 +00:00
|
|
|
|
2015-12-06 13:38:29 +00:00
|
|
|
use std;
|
2013-05-17 22:28:44 +00:00
|
|
|
|
2017-09-12 15:28:17 +00:00
|
|
|
use builder::Builder;
|
2016-03-22 17:23:36 +00:00
|
|
|
use common::*;
|
2017-09-12 15:28:17 +00:00
|
|
|
use llvm::{ValueRef};
|
|
|
|
use llvm;
|
2017-03-13 23:08:21 +00:00
|
|
|
use meth;
|
2017-09-12 21:33:56 +00:00
|
|
|
use rustc::ty::layout::LayoutOf;
|
2017-09-14 03:26:39 +00:00
|
|
|
use rustc::ty::{self, Ty};
|
2016-03-22 17:23:36 +00:00
|
|
|
use value::Value;
|
2013-06-16 10:52:44 +00:00
|
|
|
|
2016-12-31 23:00:24 +00:00
|
|
|
pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, info: ValueRef)
|
2016-12-18 02:54:32 +00:00
|
|
|
-> (ValueRef, ValueRef) {
|
2016-02-18 17:49:45 +00:00
|
|
|
debug!("calculate size of DST: {}; with lost info: {:?}",
|
|
|
|
t, Value(info));
|
2018-01-05 05:04:08 +00:00
|
|
|
if bcx.cx.type_is_sized(t) {
|
|
|
|
let (size, align) = bcx.cx.size_and_align_of(t);
|
2017-06-01 18:50:53 +00:00
|
|
|
debug!("size_and_align_of_dst t={} info={:?} size: {:?} align: {:?}",
|
2016-02-18 17:49:45 +00:00
|
|
|
t, Value(info), size, align);
|
2018-01-05 05:04:08 +00:00
|
|
|
let size = C_usize(bcx.cx, size.bytes());
|
|
|
|
let align = C_usize(bcx.cx, align.abi());
|
2014-08-06 09:59:40 +00:00
|
|
|
return (size, align);
|
|
|
|
}
|
2017-05-18 15:43:52 +00:00
|
|
|
assert!(!info.is_null());
|
2014-10-31 08:51:16 +00:00
|
|
|
match t.sty {
|
2017-06-25 09:41:24 +00:00
|
|
|
ty::TyDynamic(..) => {
|
|
|
|
// load size/align from vtable
|
|
|
|
(meth::SIZE.get_usize(bcx, info), meth::ALIGN.get_usize(bcx, info))
|
|
|
|
}
|
|
|
|
ty::TySlice(_) | ty::TyStr => {
|
|
|
|
let unit = t.sequence_element_type(bcx.tcx());
|
|
|
|
// The info in this case is the length of the str, so the size is that
|
|
|
|
// times the unit size.
|
2018-01-05 05:04:08 +00:00
|
|
|
let (size, align) = bcx.cx.size_and_align_of(unit);
|
|
|
|
(bcx.mul(info, C_usize(bcx.cx, size.bytes())),
|
|
|
|
C_usize(bcx.cx, align.abi()))
|
2017-06-25 09:41:24 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2018-01-05 05:04:08 +00:00
|
|
|
let cx = bcx.cx;
|
2014-08-06 09:59:40 +00:00
|
|
|
// First get the size of all statically known fields.
|
2017-03-02 03:35:25 +00: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 15:25:15 +00:00
|
|
|
assert!(!t.is_simd());
|
2018-01-05 05:04:08 +00:00
|
|
|
let layout = cx.layout_of(t);
|
2016-08-27 05:51:55 +00:00
|
|
|
debug!("DST {} layout: {:?}", t, layout);
|
|
|
|
|
2017-09-17 20:37:18 +00:00
|
|
|
let i = layout.fields.count() - 1;
|
|
|
|
let sized_size = layout.fields.offset(i).bytes();
|
2017-09-22 19:44:40 +00:00
|
|
|
let sized_align = layout.align.abi();
|
2015-07-27 12:45:20 +00:00
|
|
|
debug!("DST {} statically sized prefix size: {} align: {}",
|
|
|
|
t, sized_size, sized_align);
|
2018-01-05 05:04:08 +00:00
|
|
|
let sized_size = C_usize(cx, sized_size);
|
|
|
|
let sized_align = C_usize(cx, sized_align);
|
2014-08-06 09:59:40 +00:00
|
|
|
|
|
|
|
// Recurse to get the size of the dynamically sized field (must be
|
|
|
|
// the last field).
|
2018-01-05 05:04:08 +00:00
|
|
|
let field_ty = layout.field(cx, i).ty;
|
2017-12-01 16:29:35 +00:00
|
|
|
let (unsized_size, mut unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
|
2014-08-06 09:59:40 +00:00
|
|
|
|
2015-07-29 17:43:26 +00:00
|
|
|
// FIXME (#26403, #27023): We should be adding padding
|
2015-07-27 12:45:20 +00:00
|
|
|
// to `sized_size` (to accommodate the `unsized_align`
|
|
|
|
// required of the unsized field that follows) before
|
2015-07-29 17:43:26 +00: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 12:45:20 +00:00
|
|
|
|
2014-08-06 09:59:40 +00:00
|
|
|
// Return the sum of sizes and max of aligns.
|
2016-08-23 07:39:30 +00:00
|
|
|
let size = bcx.add(sized_size, unsized_size);
|
2015-07-27 12:45:20 +00:00
|
|
|
|
2017-12-01 16:29:35 +00:00
|
|
|
// Packed types ignore the alignment of their fields.
|
|
|
|
if let ty::TyAdt(def, _) = t.sty {
|
|
|
|
if def.repr.packed() {
|
|
|
|
unsized_align = sized_align;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 14:53:57 +00:00
|
|
|
// Choose max of two known alignments (combined value must
|
|
|
|
// be aligned according to more restrictive of the two).
|
2016-08-25 22:32:46 +00:00
|
|
|
let align = match (const_to_opt_u128(sized_align, false),
|
|
|
|
const_to_opt_u128(unsized_align, false)) {
|
2015-12-06 13:38:29 +00: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-01-05 05:04:08 +00:00
|
|
|
C_usize(cx, std::cmp::max(sized_align, unsized_align) as u64)
|
2015-12-06 13:38:29 +00:00
|
|
|
}
|
2016-03-09 12:20:22 +00:00
|
|
|
_ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
|
|
|
|
sized_align,
|
|
|
|
unsized_align)
|
2015-12-06 13:38:29 +00:00
|
|
|
};
|
2015-07-27 12:45:20 +00:00
|
|
|
|
2015-07-27 14:53:57 +00: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 20:18:39 +00:00
|
|
|
// emulated via the semi-standard fast bit trick:
|
2015-07-27 14:53:57 +00:00
|
|
|
//
|
2015-12-06 13:38:29 +00:00
|
|
|
// `(size + (align-1)) & -align`
|
2015-07-29 20:18:39 +00:00
|
|
|
|
2018-01-05 05:04:08 +00:00
|
|
|
let addend = bcx.sub(align, C_usize(bcx.cx, 1));
|
2016-03-09 12:20:22 +00:00
|
|
|
let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
|
2015-07-27 12:45:20 +00:00
|
|
|
|
2014-08-06 09:59:40 +00:00
|
|
|
(size, align)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|