• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::fmt::Write;
2 
3 use gccjit::{Struct, Type};
4 use crate::rustc_codegen_ssa::traits::{BaseTypeMethods, DerivedTypeMethods, LayoutTypeMethods};
5 use rustc_middle::bug;
6 use rustc_middle::ty::{self, Ty, TypeVisitableExt};
7 use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout};
8 use rustc_middle::ty::print::with_no_trimmed_paths;
9 use rustc_target::abi::{self, Abi, Align, F32, F64, FieldsShape, Int, Integer, Pointer, PointeeInfo, Size, TyAbiInterface, Variants};
10 use rustc_target::abi::call::{CastTarget, FnAbi, Reg};
11 
12 use crate::abi::{FnAbiGccExt, GccType};
13 use crate::context::CodegenCx;
14 use crate::type_::struct_fields;
15 
16 impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
type_from_unsigned_integer(&self, i: Integer) -> Type<'gcc>17     fn type_from_unsigned_integer(&self, i: Integer) -> Type<'gcc> {
18         use Integer::*;
19         match i {
20             I8 => self.type_u8(),
21             I16 => self.type_u16(),
22             I32 => self.type_u32(),
23             I64 => self.type_u64(),
24             I128 => self.type_u128(),
25         }
26     }
27 
28     #[cfg(feature="master")]
type_int_from_ty(&self, t: ty::IntTy) -> Type<'gcc>29     pub fn type_int_from_ty(&self, t: ty::IntTy) -> Type<'gcc> {
30         match t {
31             ty::IntTy::Isize => self.type_isize(),
32             ty::IntTy::I8 => self.type_i8(),
33             ty::IntTy::I16 => self.type_i16(),
34             ty::IntTy::I32 => self.type_i32(),
35             ty::IntTy::I64 => self.type_i64(),
36             ty::IntTy::I128 => self.type_i128(),
37         }
38     }
39 
40     #[cfg(feature="master")]
type_uint_from_ty(&self, t: ty::UintTy) -> Type<'gcc>41     pub fn type_uint_from_ty(&self, t: ty::UintTy) -> Type<'gcc> {
42         match t {
43             ty::UintTy::Usize => self.type_isize(),
44             ty::UintTy::U8 => self.type_i8(),
45             ty::UintTy::U16 => self.type_i16(),
46             ty::UintTy::U32 => self.type_i32(),
47             ty::UintTy::U64 => self.type_i64(),
48             ty::UintTy::U128 => self.type_i128(),
49         }
50     }
51 }
52 
53 impl<'a, 'tcx> CodegenCx<'a, 'tcx> {
align_of(&self, ty: Ty<'tcx>) -> Align54     pub fn align_of(&self, ty: Ty<'tcx>) -> Align {
55         self.layout_of(ty).align.abi
56     }
57 }
58 
uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout<'tcx>, defer: &mut Option<(Struct<'gcc>, TyAndLayout<'tcx>)>) -> Type<'gcc>59 fn uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout<'tcx>, defer: &mut Option<(Struct<'gcc>, TyAndLayout<'tcx>)>) -> Type<'gcc> {
60     match layout.abi {
61         Abi::Scalar(_) => bug!("handled elsewhere"),
62         Abi::Vector { ref element, count } => {
63             let element = layout.scalar_gcc_type_at(cx, element, Size::ZERO);
64             let element =
65                 // NOTE: gcc doesn't allow pointer types in vectors.
66                 if element.get_pointee().is_some() {
67                     cx.usize_type
68                 }
69                 else {
70                     element
71                 };
72             return cx.context.new_vector_type(element, count);
73         },
74         Abi::ScalarPair(..) => {
75             return cx.type_struct(
76                 &[
77                     layout.scalar_pair_element_gcc_type(cx, 0, false),
78                     layout.scalar_pair_element_gcc_type(cx, 1, false),
79                 ],
80                 false,
81             );
82         }
83         Abi::Uninhabited | Abi::Aggregate { .. } => {}
84     }
85 
86     let name = match layout.ty.kind() {
87         // FIXME(eddyb) producing readable type names for trait objects can result
88         // in problematically distinct types due to HRTB and subtyping (see #47638).
89         // ty::Dynamic(..) |
90         ty::Adt(..) | ty::Closure(..) | ty::Foreign(..) | ty::Generator(..) | ty::Str
91             if !cx.sess().fewer_names() =>
92         {
93             let mut name = with_no_trimmed_paths!(layout.ty.to_string());
94             if let (&ty::Adt(def, _), &Variants::Single { index }) =
95                 (layout.ty.kind(), &layout.variants)
96             {
97                 if def.is_enum() && !def.variants().is_empty() {
98                     write!(&mut name, "::{}", def.variant(index).name).unwrap();
99                 }
100             }
101             if let (&ty::Generator(_, _, _), &Variants::Single { index }) =
102                 (layout.ty.kind(), &layout.variants)
103             {
104                 write!(&mut name, "::{}", ty::GeneratorSubsts::variant_name(index)).unwrap();
105             }
106             Some(name)
107         }
108         ty::Adt(..) => {
109             // If `Some` is returned then a named struct is created in LLVM. Name collisions are
110             // avoided by LLVM (with increasing suffixes). If rustc doesn't generate names then that
111             // can improve perf.
112             // FIXME(antoyo): I don't think that's true for libgccjit.
113             Some(String::new())
114         }
115         _ => None,
116     };
117 
118     match layout.fields {
119         FieldsShape::Primitive | FieldsShape::Union(_) => {
120             let fill = cx.type_padding_filler(layout.size, layout.align.abi);
121             let packed = false;
122             match name {
123                 None => cx.type_struct(&[fill], packed),
124                 Some(ref name) => {
125                     let gcc_type = cx.type_named_struct(name);
126                     cx.set_struct_body(gcc_type, &[fill], packed);
127                     gcc_type.as_type()
128                 },
129             }
130         }
131         FieldsShape::Array { count, .. } => cx.type_array(layout.field(cx, 0).gcc_type(cx), count),
132         FieldsShape::Arbitrary { .. } =>
133             match name {
134                 None => {
135                     let (gcc_fields, packed) = struct_fields(cx, layout);
136                     cx.type_struct(&gcc_fields, packed)
137                 },
138                 Some(ref name) => {
139                     let gcc_type = cx.type_named_struct(name);
140                     *defer = Some((gcc_type, layout));
141                     gcc_type.as_type()
142                 },
143             },
144     }
145 }
146 
147 pub trait LayoutGccExt<'tcx> {
is_gcc_immediate(&self) -> bool148     fn is_gcc_immediate(&self) -> bool;
is_gcc_scalar_pair(&self) -> bool149     fn is_gcc_scalar_pair(&self) -> bool;
gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>150     fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>151     fn immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc>152     fn scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc>;
scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc>153     fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc>;
gcc_field_index(&self, index: usize) -> u64154     fn gcc_field_index(&self, index: usize) -> u64;
pointee_info_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, offset: Size) -> Option<PointeeInfo>155     fn pointee_info_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, offset: Size) -> Option<PointeeInfo>;
156 }
157 
158 impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
is_gcc_immediate(&self) -> bool159     fn is_gcc_immediate(&self) -> bool {
160         match self.abi {
161             Abi::Scalar(_) | Abi::Vector { .. } => true,
162             Abi::ScalarPair(..) | Abi::Uninhabited | Abi::Aggregate { .. } => false,
163         }
164     }
165 
is_gcc_scalar_pair(&self) -> bool166     fn is_gcc_scalar_pair(&self) -> bool {
167         match self.abi {
168             Abi::ScalarPair(..) => true,
169             Abi::Uninhabited | Abi::Scalar(_) | Abi::Vector { .. } | Abi::Aggregate { .. } => false,
170         }
171     }
172 
173     /// Gets the GCC type corresponding to a Rust type, i.e., `rustc_middle::ty::Ty`.
174     /// The pointee type of the pointer in `PlaceRef` is always this type.
175     /// For sized types, it is also the right LLVM type for an `alloca`
176     /// containing a value of that type, and most immediates (except `bool`).
177     /// Unsized types, however, are represented by a "minimal unit", e.g.
178     /// `[T]` becomes `T`, while `str` and `Trait` turn into `i8` - this
179     /// is useful for indexing slices, as `&[T]`'s data pointer is `T*`.
180     /// If the type is an unsized struct, the regular layout is generated,
181     /// with the inner-most trailing unsized field using the "minimal unit"
182     /// of that field's type - this is useful for taking the address of
183     /// that field and ensuring the struct has the right alignment.
gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>184     fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
185         if let Abi::Scalar(ref scalar) = self.abi {
186             // Use a different cache for scalars because pointers to DSTs
187             // can be either fat or thin (data pointers of fat pointers).
188             if let Some(&ty) = cx.scalar_types.borrow().get(&self.ty) {
189                 return ty;
190             }
191             let ty =
192                 match *self.ty.kind() {
193                     ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
194                         cx.type_ptr_to(cx.layout_of(ty).gcc_type(cx))
195                     }
196                     ty::Adt(def, _) if def.is_box() => {
197                         cx.type_ptr_to(cx.layout_of(self.ty.boxed_ty()).gcc_type(cx))
198                     }
199                     ty::FnPtr(sig) => cx.fn_ptr_backend_type(&cx.fn_abi_of_fn_ptr(sig, ty::List::empty())),
200                     _ => self.scalar_gcc_type_at(cx, scalar, Size::ZERO),
201                 };
202             cx.scalar_types.borrow_mut().insert(self.ty, ty);
203             return ty;
204         }
205 
206         // Check the cache.
207         let variant_index =
208             match self.variants {
209                 Variants::Single { index } => Some(index),
210                 _ => None,
211             };
212         let cached_type = cx.types.borrow().get(&(self.ty, variant_index)).cloned();
213         if let Some(ty) = cached_type {
214             return ty;
215         }
216 
217         assert!(!self.ty.has_escaping_bound_vars(), "{:?} has escaping bound vars", self.ty);
218 
219         // Make sure lifetimes are erased, to avoid generating distinct LLVM
220         // types for Rust types that only differ in the choice of lifetimes.
221         let normal_ty = cx.tcx.erase_regions(self.ty);
222 
223         let mut defer = None;
224         let ty =
225             if self.ty != normal_ty {
226                 let mut layout = cx.layout_of(normal_ty);
227                 if let Some(v) = variant_index {
228                     layout = layout.for_variant(cx, v);
229                 }
230                 layout.gcc_type(cx)
231             }
232             else {
233                 uncached_gcc_type(cx, *self, &mut defer)
234             };
235 
236         cx.types.borrow_mut().insert((self.ty, variant_index), ty);
237 
238         if let Some((deferred_ty, layout)) = defer {
239             let (fields, packed) = struct_fields(cx, layout);
240             cx.set_struct_body(deferred_ty, &fields, packed);
241         }
242 
243         ty
244     }
245 
immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>246     fn immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
247         if let Abi::Scalar(ref scalar) = self.abi {
248             if scalar.is_bool() {
249                 return cx.type_i1();
250             }
251         }
252         self.gcc_type(cx)
253     }
254 
scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc>255     fn scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc> {
256         match scalar.primitive() {
257             Int(i, true) => cx.type_from_integer(i),
258             Int(i, false) => cx.type_from_unsigned_integer(i),
259             F32 => cx.type_f32(),
260             F64 => cx.type_f64(),
261             Pointer(address_space) => {
262                 // If we know the alignment, pick something better than i8.
263                 let pointee =
264                     if let Some(pointee) = self.pointee_info_at(cx, offset) {
265                         cx.type_pointee_for_align(pointee.align)
266                     }
267                     else {
268                         cx.type_i8()
269                     };
270                 cx.type_ptr_to_ext(pointee, address_space)
271             }
272         }
273     }
274 
scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc>275     fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc> {
276         // TODO(antoyo): remove llvm hack:
277         // HACK(eddyb) special-case fat pointers until LLVM removes
278         // pointee types, to avoid bitcasting every `OperandRef::deref`.
279         match self.ty.kind() {
280             ty::Ref(..) | ty::RawPtr(_) => {
281                 return self.field(cx, index).gcc_type(cx);
282             }
283             // only wide pointer boxes are handled as pointers
284             // thin pointer boxes with scalar allocators are handled by the general logic below
285             ty::Adt(def, substs) if def.is_box() && cx.layout_of(substs.type_at(1)).is_zst() => {
286                 let ptr_ty = Ty::new_mut_ptr(cx.tcx,self.ty.boxed_ty());
287                 return cx.layout_of(ptr_ty).scalar_pair_element_gcc_type(cx, index, immediate);
288             }
289             _ => {}
290         }
291 
292         let (a, b) = match self.abi {
293             Abi::ScalarPair(ref a, ref b) => (a, b),
294             _ => bug!("TyAndLayout::scalar_pair_element_llty({:?}): not applicable", self),
295         };
296         let scalar = [a, b][index];
297 
298         // Make sure to return the same type `immediate_gcc_type` would when
299         // dealing with an immediate pair.  This means that `(bool, bool)` is
300         // effectively represented as `{i8, i8}` in memory and two `i1`s as an
301         // immediate, just like `bool` is typically `i8` in memory and only `i1`
302         // when immediate.  We need to load/store `bool` as `i8` to avoid
303         // crippling LLVM optimizations or triggering other LLVM bugs with `i1`.
304         // TODO(antoyo): this bugs certainly don't happen in this case since the bool type is used instead of i1.
305         if scalar.is_bool() {
306             return cx.type_i1();
307         }
308 
309         let offset =
310             if index == 0 {
311                 Size::ZERO
312             }
313             else {
314                 a.size(cx).align_to(b.align(cx).abi)
315             };
316         self.scalar_gcc_type_at(cx, scalar, offset)
317     }
318 
gcc_field_index(&self, index: usize) -> u64319     fn gcc_field_index(&self, index: usize) -> u64 {
320         match self.abi {
321             Abi::Scalar(_) | Abi::ScalarPair(..) => {
322                 bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
323             }
324             _ => {}
325         }
326         match self.fields {
327             FieldsShape::Primitive | FieldsShape::Union(_) => {
328                 bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
329             }
330 
331             FieldsShape::Array { .. } => index as u64,
332 
333             FieldsShape::Arbitrary { .. } => 1 + (self.fields.memory_index(index) as u64) * 2,
334         }
335     }
336 
pointee_info_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>, offset: Size) -> Option<PointeeInfo>337     fn pointee_info_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>, offset: Size) -> Option<PointeeInfo> {
338         if let Some(&pointee) = cx.pointee_infos.borrow().get(&(self.ty, offset)) {
339             return pointee;
340         }
341 
342         let result = Ty::ty_and_layout_pointee_info_at(*self, cx, offset);
343 
344         cx.pointee_infos.borrow_mut().insert((self.ty, offset), result);
345         result
346     }
347 }
348 
349 impl<'gcc, 'tcx> LayoutTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc>350     fn backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc> {
351         layout.gcc_type(self)
352     }
353 
immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc>354     fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc> {
355         layout.immediate_gcc_type(self)
356     }
357 
is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool358     fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool {
359         layout.is_gcc_immediate()
360     }
361 
is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool362     fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool {
363         layout.is_gcc_scalar_pair()
364     }
365 
backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64366     fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64 {
367         layout.gcc_field_index(index)
368     }
369 
scalar_pair_element_backend_type(&self, layout: TyAndLayout<'tcx>, index: usize, immediate: bool) -> Type<'gcc>370     fn scalar_pair_element_backend_type(&self, layout: TyAndLayout<'tcx>, index: usize, immediate: bool) -> Type<'gcc> {
371         layout.scalar_pair_element_gcc_type(self, index, immediate)
372     }
373 
cast_backend_type(&self, ty: &CastTarget) -> Type<'gcc>374     fn cast_backend_type(&self, ty: &CastTarget) -> Type<'gcc> {
375         ty.gcc_type(self)
376     }
377 
fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc>378     fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
379         fn_abi.ptr_to_gcc_type(self)
380     }
381 
reg_backend_type(&self, _ty: &Reg) -> Type<'gcc>382     fn reg_backend_type(&self, _ty: &Reg) -> Type<'gcc> {
383         unimplemented!();
384     }
385 
fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc>386     fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
387         let (return_type, param_types, variadic, _) = fn_abi.gcc_type(self);
388         self.context.new_function_pointer_type(None, return_type, &param_types, variadic)
389     }
390 }
391