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;
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)
66 impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
67 fn type_i1(&self) -> Type<'gcc> {
71 fn type_i8(&self) -> Type<'gcc> {
75 fn type_i16(&self) -> Type<'gcc> {
79 fn type_i32(&self) -> Type<'gcc> {
83 fn type_i64(&self) -> Type<'gcc> {
87 fn type_i128(&self) -> Type<'gcc> {
91 fn type_isize(&self) -> Type<'gcc> {
95 fn type_f32(&self) -> Type<'gcc> {
96 self.context.new_type::<f32>()
99 fn type_f64(&self) -> Type<'gcc> {
100 self.context.new_type::<f64>()
103 fn type_func(&self, params: &[Type<'gcc>], return_type: Type<'gcc>) -> Type<'gcc> {
104 self.context.new_function_pointer_type(None, return_type, params, false)
107 fn type_struct(&self, fields: &[Type<'gcc>], _packed: bool) -> Type<'gcc> {
108 let types = fields.to_vec();
109 if let Some(typ) = self.struct_types.borrow().get(fields) {
112 let fields: Vec<_> = fields.iter().enumerate()
113 .map(|(index, field)| self.context.new_field(None, *field, &format!("field{}_TODO", index)))
115 // TODO(antoyo): use packed.
116 let typ = self.context.new_struct_type(None, "struct", &fields).as_type();
117 self.struct_types.borrow_mut().insert(types, typ);
121 fn type_kind(&self, typ: Type<'gcc>) -> TypeKind {
122 if typ.is_integral() {
125 else if typ.is_vector().is_some() {
129 // TODO(antoyo): support other types.
134 fn type_ptr_to(&self, ty: Type<'gcc>) -> Type<'gcc> {
138 fn type_ptr_to_ext(&self, ty: Type<'gcc>, _address_space: AddressSpace) -> Type<'gcc> {
139 // TODO(antoyo): use address_space
143 fn element_type(&self, ty: Type<'gcc>) -> Type<'gcc> {
144 if let Some(typ) = ty.is_array() {
147 else if let Some(vector_type) = ty.is_vector() {
148 vector_type.get_element_type()
150 else if let Some(typ) = ty.get_pointee() {
158 fn vector_length(&self, _ty: Type<'gcc>) -> usize {
162 fn float_width(&self, typ: Type<'gcc>) -> usize {
163 let f32 = self.context.new_type::<f32>();
164 let f64 = self.context.new_type::<f64>();
172 panic!("Cannot get width of float type {:?}", typ);
174 // TODO(antoyo): support other sizes.
177 fn int_width(&self, typ: Type<'gcc>) -> u64 {
178 if typ.is_i8(self) || typ.is_u8(self) {
181 else if typ.is_i16(self) || typ.is_u16(self) {
184 else if typ.is_i32(self) || typ.is_u32(self) {
187 else if typ.is_i64(self) || typ.is_u64(self) {
190 else if typ.is_i128(self) || typ.is_u128(self) {
194 panic!("Cannot get width of int type {:?}", typ);
198 fn val_ty(&self, value: RValue<'gcc>) -> Type<'gcc> {
203 impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
204 pub fn type_padding_filler(&self, size: Size, align: Align) -> Type<'gcc> {
205 let unit = Integer::approximate_align(self, align);
206 let size = size.bytes();
207 let unit_size = unit.size().bytes();
208 assert_eq!(size % unit_size, 0);
209 self.type_array(self.type_from_integer(unit), size / unit_size)
212 pub fn set_struct_body(&self, typ: Struct<'gcc>, fields: &[Type<'gcc>], _packed: bool) {
213 // TODO(antoyo): use packed.
214 let fields: Vec<_> = fields.iter().enumerate()
215 .map(|(index, field)| self.context.new_field(None, *field, &format!("field_{}", index)))
217 typ.set_fields(None, &fields);
220 pub fn type_named_struct(&self, name: &str) -> Struct<'gcc> {
221 self.context.new_opaque_struct_type(None, name)
224 pub fn type_array(&self, ty: Type<'gcc>, mut len: u64) -> Type<'gcc> {
225 if let Some(struct_type) = ty.is_struct() {
226 if struct_type.get_field_count() == 0 {
227 // NOTE: since gccjit only supports i32 for the array size and libcore's tests uses a
228 // size of usize::MAX in test_binary_search, we workaround this by setting the size to
230 // FIXME(antoyo): fix gccjit API.
235 // NOTE: see note above. Some other test uses usize::MAX.
240 let len: i32 = len.try_into().expect("array len");
242 self.context.new_array_type(None, ty, len)
246 pub fn struct_fields<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout<'tcx>) -> (Vec<Type<'gcc>>, bool) {
247 let field_count = layout.fields.count();
249 let mut packed = false;
250 let mut offset = Size::ZERO;
251 let mut prev_effective_align = layout.align.abi;
252 let mut result: Vec<_> = Vec::with_capacity(1 + field_count * 2);
253 for i in layout.fields.index_by_increasing_offset() {
254 let target_offset = layout.fields.offset(i as usize);
255 let field = layout.field(cx, i);
256 let effective_field_align =
257 layout.align.abi.min(field.align.abi).restrict_for_offset(target_offset);
258 packed |= effective_field_align < field.align.abi;
260 assert!(target_offset >= offset);
261 let padding = target_offset - offset;
262 let padding_align = prev_effective_align.min(effective_field_align);
263 assert_eq!(offset.align_to(padding_align) + padding, target_offset);
264 result.push(cx.type_padding_filler(padding, padding_align));
266 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>.
267 offset = target_offset + field.size;
268 prev_effective_align = effective_field_align;
270 if !layout.is_unsized() && field_count > 0 {
271 if offset > layout.size {
272 bug!("layout: {:#?} stride: {:?} offset: {:?}", layout, layout.size, offset);
274 let padding = layout.size - offset;
275 let padding_align = prev_effective_align;
276 assert_eq!(offset.align_to(padding_align) + padding, layout.size);
277 result.push(cx.type_padding_filler(padding, padding_align));
278 assert_eq!(result.len(), 1 + field_count * 2);