]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/type_.rs
Merge commit 'e228f0c16ea8c34794a6285bf57aab627c26b147' into libgccjit-codegen
[rust.git] / compiler / rustc_codegen_gcc / src / type_.rs
1 use std::convert::TryInto;
2
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;
7 use rustc_middle::ty::layout::TyAndLayout;
8 use rustc_target::abi::{AddressSpace, Align, Integer, Size};
9
10 use crate::common::TypeReflection;
11 use crate::context::CodegenCx;
12 use crate::type_of::LayoutGccExt;
13
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
19         // size.
20         let bytes = (num_bits / 8).next_power_of_two() as i32;
21         match bytes {
22             1 => self.i8_type,
23             2 => self.i16_type,
24             4 => self.i32_type,
25             8 => self.i64_type,
26             16 => self.i128_type,
27             _ => panic!("unexpected num_bits: {}", num_bits),
28         }
29     }
30
31     pub fn type_void(&self) -> Type<'gcc> {
32         self.context.new_type::<()>()
33     }
34
35     pub fn type_size_t(&self) -> Type<'gcc> {
36         self.context.new_type::<usize>()
37     }
38
39     pub fn type_u8(&self) -> Type<'gcc> {
40         self.u8_type
41     }
42
43     pub fn type_u16(&self) -> Type<'gcc> {
44         self.u16_type
45     }
46
47     pub fn type_u32(&self) -> Type<'gcc> {
48         self.u32_type
49     }
50
51     pub fn type_u64(&self) -> Type<'gcc> {
52         self.u64_type
53     }
54
55     pub fn type_u128(&self) -> Type<'gcc> {
56         self.u128_type
57     }
58
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)
63     }
64 }
65
66 impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
67     fn type_i1(&self) -> Type<'gcc> {
68         self.bool_type
69     }
70
71     fn type_i8(&self) -> Type<'gcc> {
72         self.i8_type
73     }
74
75     fn type_i16(&self) -> Type<'gcc> {
76         self.i16_type
77     }
78
79     fn type_i32(&self) -> Type<'gcc> {
80         self.i32_type
81     }
82
83     fn type_i64(&self) -> Type<'gcc> {
84         self.i64_type
85     }
86
87     fn type_i128(&self) -> Type<'gcc> {
88         self.i128_type
89     }
90
91     fn type_isize(&self) -> Type<'gcc> {
92         self.isize_type
93     }
94
95     fn type_f32(&self) -> Type<'gcc> {
96         self.context.new_type::<f32>()
97     }
98
99     fn type_f64(&self) -> Type<'gcc> {
100         self.context.new_type::<f64>()
101     }
102
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)
105     }
106
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) {
110             return typ.clone();
111         }
112         let fields: Vec<_> = fields.iter().enumerate()
113             .map(|(index, field)| self.context.new_field(None, *field, &format!("field{}_TODO", index)))
114             .collect();
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);
118         typ
119     }
120
121     fn type_kind(&self, typ: Type<'gcc>) -> TypeKind {
122         if typ.is_integral() {
123             TypeKind::Integer
124         }
125         else if typ.is_vector().is_some() {
126             TypeKind::Vector
127         }
128         else {
129             // TODO(antoyo): support other types.
130             TypeKind::Void
131         }
132     }
133
134     fn type_ptr_to(&self, ty: Type<'gcc>) -> Type<'gcc> {
135         ty.make_pointer()
136     }
137
138     fn type_ptr_to_ext(&self, ty: Type<'gcc>, _address_space: AddressSpace) -> Type<'gcc> {
139         // TODO(antoyo): use address_space
140         ty.make_pointer()
141     }
142
143     fn element_type(&self, ty: Type<'gcc>) -> Type<'gcc> {
144         if let Some(typ) = ty.is_array() {
145             typ
146         }
147         else if let Some(vector_type) = ty.is_vector() {
148             vector_type.get_element_type()
149         }
150         else if let Some(typ) = ty.get_pointee() {
151             typ
152         }
153         else {
154             unreachable!()
155         }
156     }
157
158     fn vector_length(&self, _ty: Type<'gcc>) -> usize {
159         unimplemented!();
160     }
161
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>();
165         if typ == f32 {
166             32
167         }
168         else if typ == f64 {
169             64
170         }
171         else {
172             panic!("Cannot get width of float type {:?}", typ);
173         }
174         // TODO(antoyo): support other sizes.
175     }
176
177     fn int_width(&self, typ: Type<'gcc>) -> u64 {
178         if typ.is_i8(self) || typ.is_u8(self) {
179             8
180         }
181         else if typ.is_i16(self) || typ.is_u16(self) {
182             16
183         }
184         else if typ.is_i32(self) || typ.is_u32(self) {
185             32
186         }
187         else if typ.is_i64(self) || typ.is_u64(self) {
188             64
189         }
190         else if typ.is_i128(self) || typ.is_u128(self) {
191             128
192         }
193         else {
194             panic!("Cannot get width of int type {:?}", typ);
195         }
196     }
197
198     fn val_ty(&self, value: RValue<'gcc>) -> Type<'gcc> {
199         value.get_type()
200     }
201 }
202
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)
210     }
211
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)))
216             .collect();
217         typ.set_fields(None, &fields);
218     }
219
220     pub fn type_named_struct(&self, name: &str) -> Struct<'gcc> {
221         self.context.new_opaque_struct_type(None, name)
222     }
223
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
229                 // zero for ZSTs.
230                 // FIXME(antoyo): fix gccjit API.
231                 len = 0;
232             }
233         }
234
235         // NOTE: see note above. Some other test uses usize::MAX.
236         if len == u64::MAX {
237             len = 0;
238         }
239
240         let len: i32 = len.try_into().expect("array len");
241
242         self.context.new_array_type(None, ty, len)
243     }
244 }
245
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();
248
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;
259
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));
265
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;
269     }
270     if !layout.is_unsized() && field_count > 0 {
271         if offset > layout.size {
272             bug!("layout: {:#?} stride: {:?} offset: {:?}", layout, layout.size, offset);
273         }
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);
279     }
280
281     (result, packed)
282 }