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