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