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