]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/type_of.rs
Auto merge of #93873 - Stovent:big-ints, r=m-ou-se
[rust.git] / compiler / rustc_codegen_gcc / src / type_of.rs
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, TypeVisitable};
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, 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> {
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")]
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")]
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 pub fn uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout<'tcx>, defer: &mut Option<(Struct<'gcc>, TyAndLayout<'tcx>)>) -> Type<'gcc> {
54     match layout.abi {
55         Abi::Scalar(_) => bug!("handled elsewhere"),
56         Abi::Vector { ref element, count } => {
57             let element = layout.scalar_gcc_type_at(cx, element, Size::ZERO);
58             return cx.context.new_vector_type(element, count);
59         },
60         Abi::ScalarPair(..) => {
61             return cx.type_struct(
62                 &[
63                     layout.scalar_pair_element_gcc_type(cx, 0, false),
64                     layout.scalar_pair_element_gcc_type(cx, 1, false),
65                 ],
66                 false,
67             );
68         }
69         Abi::Uninhabited | Abi::Aggregate { .. } => {}
70     }
71
72     let name = match layout.ty.kind() {
73         // FIXME(eddyb) producing readable type names for trait objects can result
74         // in problematically distinct types due to HRTB and subtyping (see #47638).
75         // ty::Dynamic(..) |
76         ty::Adt(..) | ty::Closure(..) | ty::Foreign(..) | ty::Generator(..) | ty::Str
77             if !cx.sess().fewer_names() =>
78         {
79             let mut name = with_no_trimmed_paths!(layout.ty.to_string());
80             if let (&ty::Adt(def, _), &Variants::Single { index }) =
81                 (layout.ty.kind(), &layout.variants)
82             {
83                 if def.is_enum() && !def.variants().is_empty() {
84                     write!(&mut name, "::{}", def.variant(index).name).unwrap();
85                 }
86             }
87             if let (&ty::Generator(_, _, _), &Variants::Single { index }) =
88                 (layout.ty.kind(), &layout.variants)
89             {
90                 write!(&mut name, "::{}", ty::GeneratorSubsts::variant_name(index)).unwrap();
91             }
92             Some(name)
93         }
94         ty::Adt(..) => {
95             // If `Some` is returned then a named struct is created in LLVM. Name collisions are
96             // avoided by LLVM (with increasing suffixes). If rustc doesn't generate names then that
97             // can improve perf.
98             // FIXME(antoyo): I don't think that's true for libgccjit.
99             Some(String::new())
100         }
101         _ => None,
102     };
103
104     match layout.fields {
105         FieldsShape::Primitive | FieldsShape::Union(_) => {
106             let fill = cx.type_padding_filler(layout.size, layout.align.abi);
107             let packed = false;
108             match name {
109                 None => cx.type_struct(&[fill], packed),
110                 Some(ref name) => {
111                     let gcc_type = cx.type_named_struct(name);
112                     cx.set_struct_body(gcc_type, &[fill], packed);
113                     gcc_type.as_type()
114                 },
115             }
116         }
117         FieldsShape::Array { count, .. } => cx.type_array(layout.field(cx, 0).gcc_type(cx, true), count),
118         FieldsShape::Arbitrary { .. } =>
119             match name {
120                 None => {
121                     let (gcc_fields, packed) = struct_fields(cx, layout);
122                     cx.type_struct(&gcc_fields, packed)
123                 },
124                 Some(ref name) => {
125                     let gcc_type = cx.type_named_struct(name);
126                     *defer = Some((gcc_type, layout));
127                     gcc_type.as_type()
128                 },
129             },
130     }
131 }
132
133 pub trait LayoutGccExt<'tcx> {
134     fn is_gcc_immediate(&self) -> bool;
135     fn is_gcc_scalar_pair(&self) -> bool;
136     fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, set_fields: bool) -> Type<'gcc>;
137     fn immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
138     fn scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc>;
139     fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc>;
140     fn gcc_field_index(&self, index: usize) -> u64;
141     fn pointee_info_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, offset: Size) -> Option<PointeeInfo>;
142 }
143
144 impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
145     fn is_gcc_immediate(&self) -> bool {
146         match self.abi {
147             Abi::Scalar(_) | Abi::Vector { .. } => true,
148             Abi::ScalarPair(..) => false,
149             Abi::Uninhabited | Abi::Aggregate { .. } => self.is_zst(),
150         }
151     }
152
153     fn is_gcc_scalar_pair(&self) -> bool {
154         match self.abi {
155             Abi::ScalarPair(..) => true,
156             Abi::Uninhabited | Abi::Scalar(_) | Abi::Vector { .. } | Abi::Aggregate { .. } => false,
157         }
158     }
159
160     /// Gets the GCC type corresponding to a Rust type, i.e., `rustc_middle::ty::Ty`.
161     /// The pointee type of the pointer in `PlaceRef` is always this type.
162     /// For sized types, it is also the right LLVM type for an `alloca`
163     /// containing a value of that type, and most immediates (except `bool`).
164     /// Unsized types, however, are represented by a "minimal unit", e.g.
165     /// `[T]` becomes `T`, while `str` and `Trait` turn into `i8` - this
166     /// is useful for indexing slices, as `&[T]`'s data pointer is `T*`.
167     /// If the type is an unsized struct, the regular layout is generated,
168     /// with the inner-most trailing unsized field using the "minimal unit"
169     /// of that field's type - this is useful for taking the address of
170     /// that field and ensuring the struct has the right alignment.
171     //TODO(antoyo): do we still need the set_fields parameter?
172     fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, set_fields: bool) -> Type<'gcc> {
173         if let Abi::Scalar(ref scalar) = self.abi {
174             // Use a different cache for scalars because pointers to DSTs
175             // can be either fat or thin (data pointers of fat pointers).
176             if let Some(&ty) = cx.scalar_types.borrow().get(&self.ty) {
177                 return ty;
178             }
179             let ty =
180                 match *self.ty.kind() {
181                     ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
182                         cx.type_ptr_to(cx.layout_of(ty).gcc_type(cx, set_fields))
183                     }
184                     ty::Adt(def, _) if def.is_box() => {
185                         cx.type_ptr_to(cx.layout_of(self.ty.boxed_ty()).gcc_type(cx, true))
186                     }
187                     ty::FnPtr(sig) => cx.fn_ptr_backend_type(&cx.fn_abi_of_fn_ptr(sig, ty::List::empty())),
188                     _ => self.scalar_gcc_type_at(cx, scalar, Size::ZERO),
189                 };
190             cx.scalar_types.borrow_mut().insert(self.ty, ty);
191             return ty;
192         }
193
194         // Check the cache.
195         let variant_index =
196             match self.variants {
197                 Variants::Single { index } => Some(index),
198                 _ => None,
199             };
200         let cached_type = cx.types.borrow().get(&(self.ty, variant_index)).cloned();
201         if let Some(ty) = cached_type {
202             let type_to_set_fields = cx.types_with_fields_to_set.borrow_mut().remove(&ty);
203             if let Some((struct_type, layout)) = type_to_set_fields {
204                 // Since we might be trying to generate a type containing another type which is not
205                 // completely generated yet, we deferred setting the fields until now.
206                 let (fields, packed) = struct_fields(cx, layout);
207                 cx.set_struct_body(struct_type, &fields, packed);
208             }
209             return ty;
210         }
211
212         assert!(!self.ty.has_escaping_bound_vars(), "{:?} has escaping bound vars", self.ty);
213
214         // Make sure lifetimes are erased, to avoid generating distinct LLVM
215         // types for Rust types that only differ in the choice of lifetimes.
216         let normal_ty = cx.tcx.erase_regions(self.ty);
217
218         let mut defer = None;
219         let ty =
220             if self.ty != normal_ty {
221                 let mut layout = cx.layout_of(normal_ty);
222                 if let Some(v) = variant_index {
223                     layout = layout.for_variant(cx, v);
224                 }
225                 layout.gcc_type(cx, true)
226             }
227             else {
228                 uncached_gcc_type(cx, *self, &mut defer)
229             };
230
231         cx.types.borrow_mut().insert((self.ty, variant_index), ty);
232
233         if let Some((ty, layout)) = defer {
234             let (fields, packed) = struct_fields(cx, layout);
235             cx.set_struct_body(ty, &fields, packed);
236         }
237
238         ty
239     }
240
241     fn immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
242         if let Abi::Scalar(ref scalar) = self.abi {
243             if scalar.is_bool() {
244                 return cx.type_i1();
245             }
246         }
247         self.gcc_type(cx, true)
248     }
249
250     fn scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc> {
251         match scalar.primitive() {
252             Int(i, true) => cx.type_from_integer(i),
253             Int(i, false) => cx.type_from_unsigned_integer(i),
254             F32 => cx.type_f32(),
255             F64 => cx.type_f64(),
256             Pointer => {
257                 // If we know the alignment, pick something better than i8.
258                 let pointee =
259                     if let Some(pointee) = self.pointee_info_at(cx, offset) {
260                         cx.type_pointee_for_align(pointee.align)
261                     }
262                     else {
263                         cx.type_i8()
264                     };
265                 cx.type_ptr_to(pointee)
266             }
267         }
268     }
269
270     fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc> {
271         // TODO(antoyo): remove llvm hack:
272         // HACK(eddyb) special-case fat pointers until LLVM removes
273         // pointee types, to avoid bitcasting every `OperandRef::deref`.
274         match self.ty.kind() {
275             ty::Ref(..) | ty::RawPtr(_) => {
276                 return self.field(cx, index).gcc_type(cx, true);
277             }
278             // only wide pointer boxes are handled as pointers
279             // thin pointer boxes with scalar allocators are handled by the general logic below
280             ty::Adt(def, substs) if def.is_box() && cx.layout_of(substs.type_at(1)).is_zst() => {
281                 let ptr_ty = cx.tcx.mk_mut_ptr(self.ty.boxed_ty());
282                 return cx.layout_of(ptr_ty).scalar_pair_element_gcc_type(cx, index, immediate);
283             }
284             _ => {}
285         }
286
287         let (a, b) = match self.abi {
288             Abi::ScalarPair(ref a, ref b) => (a, b),
289             _ => bug!("TyAndLayout::scalar_pair_element_llty({:?}): not applicable", self),
290         };
291         let scalar = [a, b][index];
292
293         // Make sure to return the same type `immediate_gcc_type` would when
294         // dealing with an immediate pair.  This means that `(bool, bool)` is
295         // effectively represented as `{i8, i8}` in memory and two `i1`s as an
296         // immediate, just like `bool` is typically `i8` in memory and only `i1`
297         // when immediate.  We need to load/store `bool` as `i8` to avoid
298         // crippling LLVM optimizations or triggering other LLVM bugs with `i1`.
299         // TODO(antoyo): this bugs certainly don't happen in this case since the bool type is used instead of i1.
300         if scalar.is_bool() {
301             return cx.type_i1();
302         }
303
304         let offset =
305             if index == 0 {
306                 Size::ZERO
307             }
308             else {
309                 a.size(cx).align_to(b.align(cx).abi)
310             };
311         self.scalar_gcc_type_at(cx, scalar, offset)
312     }
313
314     fn gcc_field_index(&self, index: usize) -> u64 {
315         match self.abi {
316             Abi::Scalar(_) | Abi::ScalarPair(..) => {
317                 bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
318             }
319             _ => {}
320         }
321         match self.fields {
322             FieldsShape::Primitive | FieldsShape::Union(_) => {
323                 bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
324             }
325
326             FieldsShape::Array { .. } => index as u64,
327
328             FieldsShape::Arbitrary { .. } => 1 + (self.fields.memory_index(index) as u64) * 2,
329         }
330     }
331
332     fn pointee_info_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>, offset: Size) -> Option<PointeeInfo> {
333         if let Some(&pointee) = cx.pointee_infos.borrow().get(&(self.ty, offset)) {
334             return pointee;
335         }
336
337         let result = Ty::ty_and_layout_pointee_info_at(*self, cx, offset);
338
339         cx.pointee_infos.borrow_mut().insert((self.ty, offset), result);
340         result
341     }
342 }
343
344 impl<'gcc, 'tcx> LayoutTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
345     fn backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc> {
346         layout.gcc_type(self, true)
347     }
348
349     fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc> {
350         layout.immediate_gcc_type(self)
351     }
352
353     fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool {
354         layout.is_gcc_immediate()
355     }
356
357     fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool {
358         layout.is_gcc_scalar_pair()
359     }
360
361     fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64 {
362         layout.gcc_field_index(index)
363     }
364
365     fn scalar_pair_element_backend_type(&self, layout: TyAndLayout<'tcx>, index: usize, immediate: bool) -> Type<'gcc> {
366         layout.scalar_pair_element_gcc_type(self, index, immediate)
367     }
368
369     fn cast_backend_type(&self, ty: &CastTarget) -> Type<'gcc> {
370         ty.gcc_type(self)
371     }
372
373     fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
374         fn_abi.ptr_to_gcc_type(self)
375     }
376
377     fn reg_backend_type(&self, _ty: &Reg) -> Type<'gcc> {
378         unimplemented!();
379     }
380
381     fn fn_decl_backend_type(&self, _fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
382         // FIXME(antoyo): return correct type.
383         self.type_void()
384     }
385 }