3 use gccjit::{Struct, Type};
4 use crate::rustc_codegen_ssa::traits::{BaseTypeMethods, DerivedTypeMethods, LayoutTypeMethods};
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};
12 use crate::abi::{FnAbiGccExt, GccType};
13 use crate::context::CodegenCx;
14 use crate::type_::struct_fields;
16 impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
17 fn type_from_unsigned_integer(&self, i: Integer) -> Type<'gcc> {
21 I16 => self.type_u16(),
22 I32 => self.type_u32(),
23 I64 => self.type_u64(),
24 I128 => self.type_u128(),
28 #[cfg(feature="master")]
29 pub fn type_int_from_ty(&self, t: ty::IntTy) -> Type<'gcc> {
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(),
40 #[cfg(feature="master")]
41 pub fn type_uint_from_ty(&self, t: ty::UintTy) -> Type<'gcc> {
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(),
53 pub fn uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout<'tcx>, defer: &mut Option<(Struct<'gcc>, TyAndLayout<'tcx>)>) -> Type<'gcc> {
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);
60 Abi::ScalarPair(..) => {
61 return cx.type_struct(
63 layout.scalar_pair_element_gcc_type(cx, 0, false),
64 layout.scalar_pair_element_gcc_type(cx, 1, false),
69 Abi::Uninhabited | Abi::Aggregate { .. } => {}
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).
76 ty::Adt(..) | ty::Closure(..) | ty::Foreign(..) | ty::Generator(..) | ty::Str
77 if !cx.sess().fewer_names() =>
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)
83 if def.is_enum() && !def.variants().is_empty() {
84 write!(&mut name, "::{}", def.variant(index).name).unwrap();
87 if let (&ty::Generator(_, _, _), &Variants::Single { index }) =
88 (layout.ty.kind(), &layout.variants)
90 write!(&mut name, "::{}", ty::GeneratorSubsts::variant_name(index)).unwrap();
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
98 // FIXME(antoyo): I don't think that's true for libgccjit.
104 match layout.fields {
105 FieldsShape::Primitive | FieldsShape::Union(_) => {
106 let fill = cx.type_padding_filler(layout.size, layout.align.abi);
109 None => cx.type_struct(&[fill], packed),
111 let gcc_type = cx.type_named_struct(name);
112 cx.set_struct_body(gcc_type, &[fill], packed);
117 FieldsShape::Array { count, .. } => cx.type_array(layout.field(cx, 0).gcc_type(cx, true), count),
118 FieldsShape::Arbitrary { .. } =>
121 let (gcc_fields, packed) = struct_fields(cx, layout);
122 cx.type_struct(&gcc_fields, packed)
125 let gcc_type = cx.type_named_struct(name);
126 *defer = Some((gcc_type, layout));
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>;
144 impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
145 fn is_gcc_immediate(&self) -> bool {
147 Abi::Scalar(_) | Abi::Vector { .. } => true,
148 Abi::ScalarPair(..) => false,
149 Abi::Uninhabited | Abi::Aggregate { .. } => self.is_zst(),
153 fn is_gcc_scalar_pair(&self) -> bool {
155 Abi::ScalarPair(..) => true,
156 Abi::Uninhabited | Abi::Scalar(_) | Abi::Vector { .. } | Abi::Aggregate { .. } => false,
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) {
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))
184 ty::Adt(def, _) if def.is_box() => {
185 cx.type_ptr_to(cx.layout_of(self.ty.boxed_ty()).gcc_type(cx, true))
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),
190 cx.scalar_types.borrow_mut().insert(self.ty, ty);
196 match self.variants {
197 Variants::Single { index } => Some(index),
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);
212 assert!(!self.ty.has_escaping_bound_vars(), "{:?} has escaping bound vars", self.ty);
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);
218 let mut defer = None;
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);
225 layout.gcc_type(cx, true)
228 uncached_gcc_type(cx, *self, &mut defer)
231 cx.types.borrow_mut().insert((self.ty, variant_index), ty);
233 if let Some((ty, layout)) = defer {
234 let (fields, packed) = struct_fields(cx, layout);
235 cx.set_struct_body(ty, &fields, packed);
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() {
247 self.gcc_type(cx, true)
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(),
257 // If we know the alignment, pick something better than i8.
259 if let Some(pointee) = self.pointee_info_at(cx, offset) {
260 cx.type_pointee_for_align(pointee.align)
265 cx.type_ptr_to(pointee)
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);
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);
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),
291 let scalar = [a, b][index];
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() {
309 a.size(cx).align_to(b.align(cx).abi)
311 self.scalar_gcc_type_at(cx, scalar, offset)
314 fn gcc_field_index(&self, index: usize) -> u64 {
316 Abi::Scalar(_) | Abi::ScalarPair(..) => {
317 bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
322 FieldsShape::Primitive | FieldsShape::Union(_) => {
323 bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
326 FieldsShape::Array { .. } => index as u64,
328 FieldsShape::Arbitrary { .. } => 1 + (self.fields.memory_index(index) as u64) * 2,
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)) {
337 let result = Ty::ty_and_layout_pointee_info_at(*self, cx, offset);
339 cx.pointee_infos.borrow_mut().insert((self.ty, offset), result);
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)
349 fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc> {
350 layout.immediate_gcc_type(self)
353 fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool {
354 layout.is_gcc_immediate()
357 fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool {
358 layout.is_gcc_scalar_pair()
361 fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64 {
362 layout.gcc_field_index(index)
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)
369 fn cast_backend_type(&self, ty: &CastTarget) -> Type<'gcc> {
373 fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
374 fn_abi.ptr_to_gcc_type(self)
377 fn reg_backend_type(&self, _ty: &Reg) -> Type<'gcc> {
381 fn fn_decl_backend_type(&self, _fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
382 // FIXME(antoyo): return correct type.