]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/glue.rs
Rollup merge of #44562 - eddyb:ugh-rustdoc, r=nikomatsakis
[rust.git] / src / librustc_trans / glue.rs
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.
4 //
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.
10
11 //!
12 //
13 // Code relating to drop glue.
14
15 use std;
16
17 use llvm;
18 use llvm::{ValueRef};
19 use rustc::ty::{self, Ty};
20 use rustc::ty::layout::LayoutTyper;
21 use common::*;
22 use meth;
23 use monomorphize;
24 use value::Value;
25 use builder::Builder;
26
27 pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, info: ValueRef)
28                                        -> (ValueRef, ValueRef) {
29     debug!("calculate size of DST: {}; with lost info: {:?}",
30            t, Value(info));
31     if bcx.ccx.shared().type_is_sized(t) {
32         let size = bcx.ccx.size_of(t);
33         let align = bcx.ccx.align_of(t);
34         debug!("size_and_align_of_dst t={} info={:?} size: {} align: {}",
35                t, Value(info), size, align);
36         let size = C_usize(bcx.ccx, size);
37         let align = C_usize(bcx.ccx, align as u64);
38         return (size, align);
39     }
40     assert!(!info.is_null());
41     match t.sty {
42         ty::TyAdt(..) | ty::TyTuple(..) => {
43             let ccx = bcx.ccx;
44             // First get the size of all statically known fields.
45             // Don't use size_of because it also rounds up to alignment, which we
46             // want to avoid, as the unsized field's alignment could be smaller.
47             assert!(!t.is_simd());
48             let layout = ccx.layout_of(t);
49             debug!("DST {} layout: {:?}", t, layout);
50
51             let (sized_size, sized_align) = match *layout {
52                 ty::layout::Layout::Univariant { ref variant, .. } => {
53                     (variant.offsets.last().map_or(0, |o| o.bytes()), variant.align.abi())
54                 }
55                 _ => {
56                     bug!("size_and_align_of_dst: expcted Univariant for `{}`, found {:#?}",
57                          t, layout);
58                 }
59             };
60             debug!("DST {} statically sized prefix size: {} align: {}",
61                    t, sized_size, sized_align);
62             let sized_size = C_usize(ccx, sized_size);
63             let sized_align = C_usize(ccx, sized_align);
64
65             // Recurse to get the size of the dynamically sized field (must be
66             // the last field).
67             let field_ty = match t.sty {
68                 ty::TyAdt(def, substs) => {
69                     let last_field = def.struct_variant().fields.last().unwrap();
70                     monomorphize::field_ty(bcx.tcx(), substs, last_field)
71                 },
72                 ty::TyTuple(tys, _) => tys.last().unwrap(),
73                 _ => unreachable!(),
74             };
75             let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
76
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.)
83
84             // Return the sum of sizes and max of aligns.
85             let size = bcx.add(sized_size, unsized_size);
86
87             // Choose max of two known alignments (combined value must
88             // be aligned according to more restrictive of the two).
89             let align = match (const_to_opt_u128(sized_align, false),
90                                const_to_opt_u128(unsized_align, false)) {
91                 (Some(sized_align), Some(unsized_align)) => {
92                     // If both alignments are constant, (the sized_align should always be), then
93                     // pick the correct alignment statically.
94                     C_usize(ccx, std::cmp::max(sized_align, unsized_align) as u64)
95                 }
96                 _ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
97                                 sized_align,
98                                 unsized_align)
99             };
100
101             // Issue #27023: must add any necessary padding to `size`
102             // (to make it a multiple of `align`) before returning it.
103             //
104             // Namely, the returned size should be, in C notation:
105             //
106             //   `size + ((size & (align-1)) ? align : 0)`
107             //
108             // emulated via the semi-standard fast bit trick:
109             //
110             //   `(size + (align-1)) & -align`
111
112             let addend = bcx.sub(align, C_usize(bcx.ccx, 1));
113             let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
114
115             (size, align)
116         }
117         ty::TyDynamic(..) => {
118             // load size/align from vtable
119             (meth::SIZE.get_usize(bcx, info), meth::ALIGN.get_usize(bcx, info))
120         }
121         ty::TySlice(_) | ty::TyStr => {
122             let unit = t.sequence_element_type(bcx.tcx());
123             // The info in this case is the length of the str, so the size is that
124             // times the unit size.
125             (bcx.mul(info, C_usize(bcx.ccx, bcx.ccx.size_of(unit))),
126              C_usize(bcx.ccx, bcx.ccx.align_of(unit) as u64))
127         }
128         _ => bug!("Unexpected unsized type, found {}", t)
129     }
130 }