1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 // Code relating to drop glue.
19 use context::CodegenCx;
21 use rustc::ty::layout::LayoutOf;
22 use rustc::ty::{self, Ty};
24 use interfaces::{BuilderMethods, CommonMethods};
26 pub fn size_and_align_of_dst(
27 bx: &Builder<'_, 'll, 'tcx>,
29 info: Option<&'ll Value>
30 ) -> (&'ll Value, &'ll Value) {
31 debug!("calculate size of DST: {}; with lost info: {:?}",
33 if bx.cx().type_is_sized(t) {
34 let (size, align) = bx.cx().size_and_align_of(t);
35 debug!("size_and_align_of_dst t={} info={:?} size: {:?} align: {:?}",
36 t, info, size, align);
37 let size = CodegenCx::c_usize(bx.cx(), size.bytes());
38 let align = CodegenCx::c_usize(bx.cx(), align.abi());
43 // load size/align from vtable
44 let vtable = info.unwrap();
45 (meth::SIZE.get_usize(bx, vtable), meth::ALIGN.get_usize(bx, vtable))
47 ty::Slice(_) | ty::Str => {
48 let unit = t.sequence_element_type(bx.tcx());
49 // The info in this case is the length of the str, so the size is that
50 // times the unit size.
51 let (size, align) = bx.cx().size_and_align_of(unit);
52 (bx.mul(info.unwrap(), CodegenCx::c_usize(bx.cx(), size.bytes())),
53 CodegenCx::c_usize(bx.cx(), align.abi()))
57 // First get the size of all statically known fields.
58 // Don't use size_of because it also rounds up to alignment, which we
59 // want to avoid, as the unsized field's alignment could be smaller.
60 assert!(!t.is_simd());
61 let layout = cx.layout_of(t);
62 debug!("DST {} layout: {:?}", t, layout);
64 let i = layout.fields.count() - 1;
65 let sized_size = layout.fields.offset(i).bytes();
66 let sized_align = layout.align.abi();
67 debug!("DST {} statically sized prefix size: {} align: {}",
68 t, sized_size, sized_align);
69 let sized_size = CodegenCx::c_usize(cx, sized_size);
70 let sized_align = CodegenCx::c_usize(cx, sized_align);
72 // Recurse to get the size of the dynamically sized field (must be
74 let field_ty = layout.field(cx, i).ty;
75 let (unsized_size, mut unsized_align) = size_and_align_of_dst(bx, field_ty, info);
77 // FIXME (#26403, #27023): We should be adding padding
78 // to `sized_size` (to accommodate the `unsized_align`
79 // required of the unsized field that follows) before
80 // summing it with `sized_size`. (Note that since #26403
81 // is unfixed, we do not yet add the necessary padding
82 // here. But this is where the add would go.)
84 // Return the sum of sizes and max of aligns.
85 let size = bx.add(sized_size, unsized_size);
87 // Packed types ignore the alignment of their fields.
88 if let ty::Adt(def, _) = t.sty {
89 if def.repr.packed() {
90 unsized_align = sized_align;
94 // Choose max of two known alignments (combined value must
95 // be aligned according to more restrictive of the two).
96 let align = match (CodegenCx::const_to_opt_u128(sized_align, false),
97 CodegenCx::const_to_opt_u128(unsized_align, false)) {
98 (Some(sized_align), Some(unsized_align)) => {
99 // If both alignments are constant, (the sized_align should always be), then
100 // pick the correct alignment statically.
101 CodegenCx::c_usize(cx, std::cmp::max(sized_align, unsized_align) as u64)
103 _ => bx.select(bx.icmp(IntPredicate::IntUGT, sized_align, unsized_align),
108 // Issue #27023: must add any necessary padding to `size`
109 // (to make it a multiple of `align`) before returning it.
111 // Namely, the returned size should be, in C notation:
113 // `size + ((size & (align-1)) ? align : 0)`
115 // emulated via the semi-standard fast bit trick:
117 // `(size + (align-1)) & -align`
119 let addend = bx.sub(align, CodegenCx::c_usize(bx.cx(), 1));
120 let size = bx.and(bx.add(size, addend), bx.neg(align));