1 use std::convert::TryInto;
3 use gccjit::{RValue, Struct, Type};
4 use rustc_codegen_ssa::traits::{BaseTypeMethods, DerivedTypeMethods};
5 use rustc_codegen_ssa::common::TypeKind;
6 use rustc_middle::{bug, ty};
7 use rustc_middle::ty::layout::TyAndLayout;
8 use rustc_target::abi::{AddressSpace, Align, Integer, Size};
10 use crate::common::TypeReflection;
11 use crate::context::CodegenCx;
12 use crate::type_of::LayoutGccExt;
14 impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
15 pub fn type_ix(&self, num_bits: u64) -> Type<'gcc> {
16 // gcc only supports 1, 2, 4 or 8-byte integers.
17 // FIXME(antoyo): this is misleading to use the next power of two as rustc_codegen_ssa
18 // sometimes use 96-bit numbers and the following code will give an integer of a different
20 let bytes = (num_bits / 8).next_power_of_two() as i32;
27 _ => panic!("unexpected num_bits: {}", num_bits),
31 pub fn type_void(&self) -> Type<'gcc> {
32 self.context.new_type::<()>()
35 pub fn type_size_t(&self) -> Type<'gcc> {
36 self.context.new_type::<usize>()
39 pub fn type_u8(&self) -> Type<'gcc> {
43 pub fn type_u16(&self) -> Type<'gcc> {
47 pub fn type_u32(&self) -> Type<'gcc> {
51 pub fn type_u64(&self) -> Type<'gcc> {
55 pub fn type_u128(&self) -> Type<'gcc> {
59 pub fn type_pointee_for_align(&self, align: Align) -> Type<'gcc> {
60 // FIXME(eddyb) We could find a better approximation if ity.align < align.
61 let ity = Integer::approximate_align(self, align);
62 self.type_from_integer(ity)
65 pub fn type_vector(&self, ty: Type<'gcc>, len: u64) -> Type<'gcc> {
66 self.context.new_vector_type(ty, len)
69 pub fn type_float_from_ty(&self, t: ty::FloatTy) -> Type<'gcc> {
71 ty::FloatTy::F32 => self.type_f32(),
72 ty::FloatTy::F64 => self.type_f64(),
77 impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
78 fn type_i1(&self) -> Type<'gcc> {
82 fn type_i8(&self) -> Type<'gcc> {
86 fn type_i16(&self) -> Type<'gcc> {
90 fn type_i32(&self) -> Type<'gcc> {
94 fn type_i64(&self) -> Type<'gcc> {
98 fn type_i128(&self) -> Type<'gcc> {
102 fn type_isize(&self) -> Type<'gcc> {
106 fn type_f32(&self) -> Type<'gcc> {
107 self.context.new_type::<f32>()
110 fn type_f64(&self) -> Type<'gcc> {
111 self.context.new_type::<f64>()
114 fn type_func(&self, params: &[Type<'gcc>], return_type: Type<'gcc>) -> Type<'gcc> {
115 self.context.new_function_pointer_type(None, return_type, params, false)
118 fn type_struct(&self, fields: &[Type<'gcc>], packed: bool) -> Type<'gcc> {
119 let types = fields.to_vec();
120 if let Some(typ) = self.struct_types.borrow().get(fields) {
123 let fields: Vec<_> = fields.iter().enumerate()
124 .map(|(index, field)| self.context.new_field(None, *field, &format!("field{}_TODO", index)))
126 let typ = self.context.new_struct_type(None, "struct", &fields).as_type();
128 #[cfg(feature="master")]
131 self.struct_types.borrow_mut().insert(types, typ);
135 fn type_kind(&self, typ: Type<'gcc>) -> TypeKind {
136 if self.is_int_type_or_bool(typ) {
139 else if typ.is_compatible_with(self.float_type) {
142 else if typ.is_compatible_with(self.double_type) {
145 else if typ.is_vector() {
149 // TODO(antoyo): support other types.
154 fn type_ptr_to(&self, ty: Type<'gcc>) -> Type<'gcc> {
158 fn type_ptr_to_ext(&self, ty: Type<'gcc>, _address_space: AddressSpace) -> Type<'gcc> {
159 // TODO(antoyo): use address_space, perhaps with TYPE_ADDR_SPACE?
163 fn element_type(&self, ty: Type<'gcc>) -> Type<'gcc> {
164 if let Some(typ) = ty.dyncast_array() {
167 else if let Some(vector_type) = ty.dyncast_vector() {
168 vector_type.get_element_type()
170 else if let Some(typ) = ty.get_pointee() {
178 fn vector_length(&self, _ty: Type<'gcc>) -> usize {
182 fn float_width(&self, typ: Type<'gcc>) -> usize {
183 let f32 = self.context.new_type::<f32>();
184 let f64 = self.context.new_type::<f64>();
185 if typ.is_compatible_with(f32) {
188 else if typ.is_compatible_with(f64) {
192 panic!("Cannot get width of float type {:?}", typ);
194 // TODO(antoyo): support other sizes.
197 fn int_width(&self, typ: Type<'gcc>) -> u64 {
198 self.gcc_int_width(typ)
201 fn val_ty(&self, value: RValue<'gcc>) -> Type<'gcc> {
206 impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
207 pub fn type_padding_filler(&self, size: Size, align: Align) -> Type<'gcc> {
208 let unit = Integer::approximate_align(self, align);
209 let size = size.bytes();
210 let unit_size = unit.size().bytes();
211 assert_eq!(size % unit_size, 0);
212 self.type_array(self.type_from_integer(unit), size / unit_size)
215 pub fn set_struct_body(&self, typ: Struct<'gcc>, fields: &[Type<'gcc>], packed: bool) {
216 let fields: Vec<_> = fields.iter().enumerate()
217 .map(|(index, field)| self.context.new_field(None, *field, &format!("field_{}", index)))
219 typ.set_fields(None, &fields);
221 #[cfg(feature="master")]
222 typ.as_type().set_packed();
226 pub fn type_named_struct(&self, name: &str) -> Struct<'gcc> {
227 self.context.new_opaque_struct_type(None, name)
230 pub fn type_array(&self, ty: Type<'gcc>, mut len: u64) -> Type<'gcc> {
231 if let Some(struct_type) = ty.is_struct() {
232 if struct_type.get_field_count() == 0 {
233 // NOTE: since gccjit only supports i32 for the array size and libcore's tests uses a
234 // size of usize::MAX in test_binary_search, we workaround this by setting the size to
236 // FIXME(antoyo): fix gccjit API.
241 // NOTE: see note above. Some other test uses usize::MAX.
246 let len: i32 = len.try_into().expect("array len");
248 self.context.new_array_type(None, ty, len)
251 pub fn type_bool(&self) -> Type<'gcc> {
252 self.context.new_type::<bool>()
256 pub fn struct_fields<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout<'tcx>) -> (Vec<Type<'gcc>>, bool) {
257 let field_count = layout.fields.count();
259 let mut packed = false;
260 let mut offset = Size::ZERO;
261 let mut prev_effective_align = layout.align.abi;
262 let mut result: Vec<_> = Vec::with_capacity(1 + field_count * 2);
263 for i in layout.fields.index_by_increasing_offset() {
264 let target_offset = layout.fields.offset(i as usize);
265 let field = layout.field(cx, i);
266 let effective_field_align =
267 layout.align.abi.min(field.align.abi).restrict_for_offset(target_offset);
268 packed |= effective_field_align < field.align.abi;
270 assert!(target_offset >= offset);
271 let padding = target_offset - offset;
272 let padding_align = prev_effective_align.min(effective_field_align);
273 assert_eq!(offset.align_to(padding_align) + padding, target_offset);
274 result.push(cx.type_padding_filler(padding, padding_align));
276 result.push(field.gcc_type(cx, !field.ty.is_any_ptr())); // FIXME(antoyo): might need to check if the type is inside another, like Box<Type>.
277 offset = target_offset + field.size;
278 prev_effective_align = effective_field_align;
280 if !layout.is_unsized() && field_count > 0 {
281 if offset > layout.size {
282 bug!("layout: {:#?} stride: {:?} offset: {:?}", layout, layout.size, offset);
284 let padding = layout.size - offset;
285 let padding_align = prev_effective_align;
286 assert_eq!(offset.align_to(padding_align) + padding, layout.size);
287 result.push(cx.type_padding_filler(padding, padding_align));
288 assert_eq!(result.len(), 1 + field_count * 2);