3 // Adapted from https://github.com/rust-lang/rust/blob/2a663555ddf36f6b041445894a8c175cd1bc718c/src/librustc_codegen_ssa/base.rs#L159-L307
5 /// Retrieve the information we are losing (making dynamic) in an unsizing
8 /// The `old_info` argument is a bit funny. It is intended for use
9 /// in an upcast, where the new vtable for an object will be derived
11 pub fn unsized_info<'a, 'tcx: 'a>(
12 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
15 old_info: Option<Value>,
17 let (source, target) = fx.tcx.struct_lockstep_tails(source, target);
18 match (&source.sty, &target.sty) {
19 (&ty::Array(_, len), &ty::Slice(_)) => {
20 fx.bcx.ins().iconst(fx.pointer_type, len.unwrap_usize(fx.tcx) as i64)
22 (&ty::Dynamic(..), &ty::Dynamic(..)) => {
23 // For now, upcasts are limited to changes in marker
24 // traits, and hence never actually require an actual
25 // change to the vtable.
26 old_info.expect("unsized_info: missing old info for trait upcast")
28 (_, &ty::Dynamic(ref data, ..)) => {
29 crate::vtable::get_vtable(fx, source, data.principal())
31 _ => bug!("unsized_info: invalid unsizing {:?} -> {:?}",
37 /// Coerce `src` to `dst_ty`. `src_ty` must be a thin pointer.
38 pub fn unsize_thin_ptr<'a, 'tcx: 'a>(
39 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
44 match (&src_ty.sty, &dst_ty.sty) {
48 &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) |
49 (&ty::RawPtr(ty::TypeAndMut { ty: a, .. }),
50 &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
51 assert!(!fx.layout_of(a).is_unsized());
52 (src, unsized_info(fx, a, b, None))
54 (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
55 let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
56 assert!(!fx.layout_of(a).is_unsized());
57 (src, unsized_info(fx, a, b, None))
59 (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
60 assert_eq!(def_a, def_b);
62 let src_layout = fx.layout_of(src_ty);
63 let dst_layout = fx.layout_of(dst_ty);
64 let mut result = None;
65 for i in 0..src_layout.fields.count() {
66 let src_f = src_layout.field(fx, i);
67 assert_eq!(src_layout.fields.offset(i).bytes(), 0);
68 assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
72 assert_eq!(src_layout.size, src_f.size);
74 let dst_f = dst_layout.field(fx, i);
75 assert_ne!(src_f.ty, dst_f.ty);
76 assert_eq!(result, None);
77 result = Some(unsize_thin_ptr(fx, src, src_f.ty, dst_f.ty));
81 _ => bug!("unsize_thin_ptr: called on bad types"),
85 /// Coerce `src`, which is a reference to a value of type `src_ty`,
86 /// to a value of type `dst_ty` and store the result in `dst`
87 pub fn coerce_unsized_into<'a, 'tcx: 'a>(
88 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
92 let src_ty = src.layout().ty;
93 let dst_ty = dst.layout().ty;
94 let mut coerce_ptr = || {
95 let (base, info) = if fx.layout_of(src.layout().ty.builtin_deref(true).unwrap().ty).is_unsized() {
96 // fat-ptr to fat-ptr unsize preserves the vtable
97 // i.e., &'a fmt::Debug+Send => &'a fmt::Debug
98 src.load_value_pair(fx)
100 let base = src.load_scalar(fx);
101 unsize_thin_ptr(fx, base, src_ty, dst_ty)
103 dst.write_cvalue(fx, CValue::ByValPair(base, info, dst.layout()));
105 match (&src_ty.sty, &dst_ty.sty) {
106 (&ty::Ref(..), &ty::Ref(..)) |
107 (&ty::Ref(..), &ty::RawPtr(..)) |
108 (&ty::RawPtr(..), &ty::RawPtr(..)) => {
111 (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
112 assert_eq!(def_a, def_b);
114 for i in 0..def_a.variants[VariantIdx::new(0)].fields.len() {
115 let src_f = src.value_field(fx, mir::Field::new(i));
116 let dst_f = dst.place_field(fx, mir::Field::new(i));
118 if dst_f.layout().is_zst() {
122 if src_f.layout().ty == dst_f.layout().ty {
123 dst_f.write_cvalue(fx, src_f);
125 coerce_unsized_into(fx, src_f, dst_f);
129 _ => bug!("coerce_unsized_into: invalid coercion {:?} -> {:?}",
136 // Adapted from https://github.com/rust-lang/rust/blob/2a663555ddf36f6b041445894a8c175cd1bc718c/src/librustc_codegen_ssa/glue.rs
138 pub fn size_and_align_of_dst<'a, 'tcx: 'a>(
139 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
142 ) -> (Value, Value) {
143 let layout = fx.layout_of(ty);
144 if !layout.is_unsized() {
145 let size = fx.bcx.ins().iconst(fx.pointer_type, layout.size.bytes() as i64);
146 let align = fx.bcx.ins().iconst(fx.pointer_type, layout.align.abi.bytes() as i64);
147 return (size, align);
151 // load size/align from vtable
153 crate::vtable::size_of_obj(fx, info),
154 crate::vtable::min_align_of_obj(fx, info),
157 ty::Slice(_) | ty::Str => {
158 let unit = layout.field(fx, 0);
159 // The info in this case is the length of the str, so the size is that
160 // times the unit size.
161 (fx.bcx.ins().imul_imm(info, unit.size.bytes() as i64),
162 fx.bcx.ins().iconst(fx.pointer_type, unit.align.abi.bytes() as i64))
165 // First get the size of all statically known fields.
166 // Don't use size_of because it also rounds up to alignment, which we
167 // want to avoid, as the unsized field's alignment could be smaller.
168 assert!(!ty.is_simd());
170 let i = layout.fields.count() - 1;
171 let sized_size = layout.fields.offset(i).bytes();
172 let sized_align = layout.align.abi.bytes();
173 let sized_align = fx.bcx.ins().iconst(fx.pointer_type, sized_align as i64);
175 // Recurse to get the size of the dynamically sized field (must be
177 let field_ty = layout.field(fx, i).ty;
178 let (unsized_size, mut unsized_align) = size_and_align_of_dst(fx, field_ty, info);
180 // FIXME (#26403, #27023): We should be adding padding
181 // to `sized_size` (to accommodate the `unsized_align`
182 // required of the unsized field that follows) before
183 // summing it with `sized_size`. (Note that since #26403
184 // is unfixed, we do not yet add the necessary padding
185 // here. But this is where the add would go.)
187 // Return the sum of sizes and max of aligns.
188 let size = fx.bcx.ins().iadd_imm(unsized_size, sized_size as i64);
190 // Packed types ignore the alignment of their fields.
191 if let ty::Adt(def, _) = ty.sty {
192 if def.repr.packed() {
193 unsized_align = sized_align;
197 // Choose max of two known alignments (combined value must
198 // be aligned according to more restrictive of the two).
199 let cmp = fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, sized_align, unsized_align);
200 let align = fx.bcx.ins().select(cmp, sized_align, unsized_align);
202 // Issue #27023: must add any necessary padding to `size`
203 // (to make it a multiple of `align`) before returning it.
205 // Namely, the returned size should be, in C notation:
207 // `size + ((size & (align-1)) ? align : 0)`
209 // emulated via the semi-standard fast bit trick:
211 // `(size + (align-1)) & -align`
212 let addend = fx.bcx.ins().iadd_imm(align, -1);
213 let add = fx.bcx.ins().iadd(size, addend);
214 let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
215 let neg = fx.bcx.ins().isub(zero, align);
216 let size = fx.bcx.ins().band(add, neg);