]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/glue.rs
Merge remote-tracking branch 'upstream/master'
[rust.git] / src / librustc_codegen_ssa / glue.rs
1 //!
2 //
3 // Code relating to drop glue.
4
5 use std;
6
7 use common::IntPredicate;
8 use meth;
9 use rustc::ty::{self, Ty};
10 use traits::*;
11
12 pub fn size_and_align_of_dst<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
13     bx: &mut Bx,
14     t: Ty<'tcx>,
15     info: Option<Bx::Value>
16 ) -> (Bx::Value, Bx::Value) {
17     let layout = bx.layout_of(t);
18     debug!("size_and_align_of_dst(ty={}, info={:?}): layout: {:?}",
19            t, info, layout);
20     if !layout.is_unsized() {
21         let size = bx.const_usize(layout.size.bytes());
22         let align = bx.const_usize(layout.align.abi.bytes());
23         return (size, align);
24     }
25     match t.sty {
26         ty::Dynamic(..) => {
27             // load size/align from vtable
28             let vtable = info.unwrap();
29             (meth::SIZE.get_usize(bx, vtable), meth::ALIGN.get_usize(bx, vtable))
30         }
31         ty::Slice(_) | ty::Str => {
32             let unit = layout.field(bx, 0);
33             // The info in this case is the length of the str, so the size is that
34             // times the unit size.
35             (bx.mul(info.unwrap(), bx.const_usize(unit.size.bytes())),
36              bx.const_usize(unit.align.abi.bytes()))
37         }
38         _ => {
39             // First get the size of all statically known fields.
40             // Don't use size_of because it also rounds up to alignment, which we
41             // want to avoid, as the unsized field's alignment could be smaller.
42             assert!(!t.is_simd());
43             debug!("DST {} layout: {:?}", t, layout);
44
45             let i = layout.fields.count() - 1;
46             let sized_size = layout.fields.offset(i).bytes();
47             let sized_align = layout.align.abi.bytes();
48             debug!("DST {} statically sized prefix size: {} align: {}",
49                    t, sized_size, sized_align);
50             let sized_size = bx.const_usize(sized_size);
51             let sized_align = bx.const_usize(sized_align);
52
53             // Recurse to get the size of the dynamically sized field (must be
54             // the last field).
55             let field_ty = layout.field(bx, i).ty;
56             let (unsized_size, mut unsized_align) = size_and_align_of_dst(bx, field_ty, info);
57
58             // FIXME (#26403, #27023): We should be adding padding
59             // to `sized_size` (to accommodate the `unsized_align`
60             // required of the unsized field that follows) before
61             // summing it with `sized_size`. (Note that since #26403
62             // is unfixed, we do not yet add the necessary padding
63             // here. But this is where the add would go.)
64
65             // Return the sum of sizes and max of aligns.
66             let size = bx.add(sized_size, unsized_size);
67
68             // Packed types ignore the alignment of their fields.
69             if let ty::Adt(def, _) = t.sty {
70                 if def.repr.packed() {
71                     unsized_align = sized_align;
72                 }
73             }
74
75             // Choose max of two known alignments (combined value must
76             // be aligned according to more restrictive of the two).
77             let align = match (bx.const_to_opt_u128(sized_align, false),
78                                bx.const_to_opt_u128(unsized_align, false)) {
79                 (Some(sized_align), Some(unsized_align)) => {
80                     // If both alignments are constant, (the sized_align should always be), then
81                     // pick the correct alignment statically.
82                     bx.const_usize(std::cmp::max(sized_align, unsized_align) as u64)
83                 }
84                 _ => {
85                     let cmp = bx.icmp(IntPredicate::IntUGT, sized_align, unsized_align);
86                     bx.select(cmp, sized_align, unsized_align)
87                 }
88             };
89
90             // Issue #27023: must add any necessary padding to `size`
91             // (to make it a multiple of `align`) before returning it.
92             //
93             // Namely, the returned size should be, in C notation:
94             //
95             //   `size + ((size & (align-1)) ? align : 0)`
96             //
97             // emulated via the semi-standard fast bit trick:
98             //
99             //   `(size + (align-1)) & -align`
100             let one = bx.const_usize(1);
101             let addend = bx.sub(align, one);
102             let add = bx.add(size, addend);
103             let neg =  bx.neg(align);
104             let size = bx.and(add, neg);
105
106             (size, align)
107         }
108     }
109 }