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