]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/common.rs
Auto merge of #99600 - tmiasko:subst-folder, r=petrochenkov
[rust.git] / compiler / rustc_codegen_gcc / src / common.rs
1 use gccjit::LValue;
2 use gccjit::{RValue, Type, ToRValue};
3 use rustc_codegen_ssa::mir::place::PlaceRef;
4 use rustc_codegen_ssa::traits::{
5     BaseTypeMethods,
6     ConstMethods,
7     DerivedTypeMethods,
8     MiscMethods,
9     StaticMethods,
10 };
11 use rustc_middle::mir::Mutability;
12 use rustc_middle::ty::layout::{TyAndLayout, LayoutOf};
13 use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
14 use rustc_target::abi::{self, HasDataLayout, Pointer, Size};
15
16 use crate::consts::const_alloc_to_gcc;
17 use crate::context::CodegenCx;
18 use crate::type_of::LayoutGccExt;
19
20 impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
21     pub fn const_bytes(&self, bytes: &[u8]) -> RValue<'gcc> {
22         bytes_in_context(self, bytes)
23     }
24
25     fn global_string(&self, string: &str) -> LValue<'gcc> {
26         // TODO(antoyo): handle non-null-terminated strings.
27         let string = self.context.new_string_literal(&*string);
28         let sym = self.generate_local_symbol_name("str");
29         let global = self.declare_private_global(&sym, self.val_ty(string));
30         global.global_set_initializer_rvalue(string);
31         global
32         // TODO(antoyo): set linkage.
33     }
34 }
35
36 pub fn bytes_in_context<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, bytes: &[u8]) -> RValue<'gcc> {
37     let context = &cx.context;
38     let byte_type = context.new_type::<u8>();
39     let typ = context.new_array_type(None, byte_type, bytes.len() as i32);
40     let elements: Vec<_> =
41         bytes.iter()
42         .map(|&byte| context.new_rvalue_from_int(byte_type, byte as i32))
43         .collect();
44     context.new_array_constructor(None, typ, &elements)
45 }
46
47 pub fn type_is_pointer<'gcc>(typ: Type<'gcc>) -> bool {
48     typ.get_pointee().is_some()
49 }
50
51 impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
52     fn const_null(&self, typ: Type<'gcc>) -> RValue<'gcc> {
53         if type_is_pointer(typ) {
54             self.context.new_null(typ)
55         }
56         else {
57             self.const_int(typ, 0)
58         }
59     }
60
61     fn const_undef(&self, typ: Type<'gcc>) -> RValue<'gcc> {
62         let local = self.current_func.borrow().expect("func")
63             .new_local(None, typ, "undefined");
64         if typ.is_struct().is_some() {
65             // NOTE: hack to workaround a limitation of the rustc API: see comment on
66             // CodegenCx.structs_as_pointer
67             let pointer = local.get_address(None);
68             self.structs_as_pointer.borrow_mut().insert(pointer);
69             pointer
70         }
71         else {
72             local.to_rvalue()
73         }
74     }
75
76     fn const_int(&self, typ: Type<'gcc>, int: i64) -> RValue<'gcc> {
77         self.gcc_int(typ, int)
78     }
79
80     fn const_uint(&self, typ: Type<'gcc>, int: u64) -> RValue<'gcc> {
81         self.gcc_uint(typ, int)
82     }
83
84     fn const_uint_big(&self, typ: Type<'gcc>, num: u128) -> RValue<'gcc> {
85         self.gcc_uint_big(typ, num)
86     }
87
88     fn const_bool(&self, val: bool) -> RValue<'gcc> {
89         self.const_uint(self.type_i1(), val as u64)
90     }
91
92     fn const_i16(&self, i: i16) -> RValue<'gcc> {
93         self.const_int(self.type_i16(), i as i64)
94     }
95
96     fn const_i32(&self, i: i32) -> RValue<'gcc> {
97         self.const_int(self.type_i32(), i as i64)
98     }
99
100     fn const_u32(&self, i: u32) -> RValue<'gcc> {
101         self.const_uint(self.type_u32(), i as u64)
102     }
103
104     fn const_u64(&self, i: u64) -> RValue<'gcc> {
105         self.const_uint(self.type_u64(), i)
106     }
107
108     fn const_usize(&self, i: u64) -> RValue<'gcc> {
109         let bit_size = self.data_layout().pointer_size.bits();
110         if bit_size < 64 {
111             // make sure it doesn't overflow
112             assert!(i < (1 << bit_size));
113         }
114
115         self.const_uint(self.usize_type, i)
116     }
117
118     fn const_u8(&self, _i: u8) -> RValue<'gcc> {
119         unimplemented!();
120     }
121
122     fn const_real(&self, typ: Type<'gcc>, val: f64) -> RValue<'gcc> {
123         self.context.new_rvalue_from_double(typ, val)
124     }
125
126     fn const_str(&self, s: &str) -> (RValue<'gcc>, RValue<'gcc>) {
127         let str_global = *self
128             .const_str_cache
129             .borrow_mut()
130             .raw_entry_mut()
131             .from_key(s)
132             .or_insert_with(|| (s.to_owned(), self.global_string(s)))
133             .1;
134         let len = s.len();
135         let cs = self.const_ptrcast(str_global.get_address(None),
136             self.type_ptr_to(self.layout_of(self.tcx.types.str_).gcc_type(self, true)),
137         );
138         (cs, self.const_usize(len as u64))
139     }
140
141     fn const_struct(&self, values: &[RValue<'gcc>], packed: bool) -> RValue<'gcc> {
142         let fields: Vec<_> = values.iter()
143             .map(|value| value.get_type())
144             .collect();
145         // TODO(antoyo): cache the type? It's anonymous, so probably not.
146         let typ = self.type_struct(&fields, packed);
147         let struct_type = typ.is_struct().expect("struct type");
148         self.context.new_struct_constructor(None, struct_type.as_type(), None, values)
149     }
150
151     fn const_to_opt_uint(&self, _v: RValue<'gcc>) -> Option<u64> {
152         // TODO(antoyo)
153         None
154     }
155
156     fn const_to_opt_u128(&self, _v: RValue<'gcc>, _sign_ext: bool) -> Option<u128> {
157         // TODO(antoyo)
158         None
159     }
160
161     fn zst_to_backend(&self, _ty: Type<'gcc>) -> RValue<'gcc> {
162         self.const_undef(self.type_ix(0))
163     }
164
165     fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, ty: Type<'gcc>) -> RValue<'gcc> {
166         let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() };
167         match cv {
168             Scalar::Int(int) => {
169                 let data = int.assert_bits(layout.size(self));
170
171                 // FIXME(antoyo): there's some issues with using the u128 code that follows, so hard-code
172                 // the paths for floating-point values.
173                 if ty == self.float_type {
174                     return self.context.new_rvalue_from_double(ty, f32::from_bits(data as u32) as f64);
175                 }
176                 else if ty == self.double_type {
177                     return self.context.new_rvalue_from_double(ty, f64::from_bits(data as u64));
178                 }
179
180                 let value = self.const_uint_big(self.type_ix(bitsize), data);
181                 // TODO(bjorn3): assert size is correct
182                 self.const_bitcast(value, ty)
183             }
184             Scalar::Ptr(ptr, _size) => {
185                 let (alloc_id, offset) = ptr.into_parts();
186                 let base_addr =
187                     match self.tcx.global_alloc(alloc_id) {
188                         GlobalAlloc::Memory(alloc) => {
189                             let init = const_alloc_to_gcc(self, alloc);
190                             let alloc = alloc.inner();
191                             let value =
192                                 match alloc.mutability {
193                                     Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
194                                     _ => self.static_addr_of(init, alloc.align, None),
195                                 };
196                             if !self.sess().fewer_names() {
197                                 // TODO(antoyo): set value name.
198                             }
199                             value
200                         },
201                         GlobalAlloc::Function(fn_instance) => {
202                             self.get_fn_addr(fn_instance)
203                         },
204                         GlobalAlloc::VTable(ty, trait_ref) => {
205                             let alloc = self.tcx.global_alloc(self.tcx.vtable_allocation((ty, trait_ref))).unwrap_memory();
206                             let init = const_alloc_to_gcc(self, alloc);
207                             self.static_addr_of(init, alloc.inner().align, None)
208                         }
209                         GlobalAlloc::Static(def_id) => {
210                             assert!(self.tcx.is_static(def_id));
211                             self.get_static(def_id).get_address(None)
212                         },
213                     };
214                 let ptr_type = base_addr.get_type();
215                 let base_addr = self.const_bitcast(base_addr, self.usize_type);
216                 let offset = self.context.new_rvalue_from_long(self.usize_type, offset.bytes() as i64);
217                 let ptr = self.const_bitcast(base_addr + offset, ptr_type);
218                 if layout.primitive() != Pointer {
219                     self.const_bitcast(ptr.dereference(None).to_rvalue(), ty)
220                 }
221                 else {
222                     self.const_bitcast(ptr, ty)
223                 }
224             }
225         }
226     }
227
228     fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value {
229         const_alloc_to_gcc(self, alloc)
230     }
231
232     fn from_const_alloc(&self, layout: TyAndLayout<'tcx>, alloc: ConstAllocation<'tcx>, offset: Size) -> PlaceRef<'tcx, RValue<'gcc>> {
233         assert_eq!(alloc.inner().align, layout.align.abi);
234         let ty = self.type_ptr_to(layout.gcc_type(self, true));
235         let value =
236             if layout.size == Size::ZERO {
237                 let value = self.const_usize(alloc.inner().align.bytes());
238                 self.context.new_cast(None, value, ty)
239             }
240             else {
241                 let init = const_alloc_to_gcc(self, alloc);
242                 let base_addr = self.static_addr_of(init, alloc.inner().align, None);
243
244                 let array = self.const_bitcast(base_addr, self.type_i8p());
245                 let value = self.context.new_array_access(None, array, self.const_usize(offset.bytes())).get_address(None);
246                 self.const_bitcast(value, ty)
247             };
248         PlaceRef::new_sized(value, layout)
249     }
250
251     fn const_ptrcast(&self, val: RValue<'gcc>, ty: Type<'gcc>) -> RValue<'gcc> {
252         self.context.new_cast(None, val, ty)
253     }
254 }
255
256 pub trait SignType<'gcc, 'tcx> {
257     fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
258     fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
259     fn to_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
260     fn to_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
261 }
262
263 impl<'gcc, 'tcx> SignType<'gcc, 'tcx> for Type<'gcc> {
264     fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
265         self.is_i8(cx) || self.is_i16(cx) || self.is_i32(cx) || self.is_i64(cx) || self.is_i128(cx)
266     }
267
268     fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
269         self.is_u8(cx) || self.is_u16(cx) || self.is_u32(cx) || self.is_u64(cx) || self.is_u128(cx)
270     }
271
272     fn to_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
273         if self.is_u8(cx) {
274             cx.i8_type
275         }
276         else if self.is_u16(cx) {
277             cx.i16_type
278         }
279         else if self.is_u32(cx) {
280             cx.i32_type
281         }
282         else if self.is_u64(cx) {
283             cx.i64_type
284         }
285         else if self.is_u128(cx) {
286             cx.i128_type
287         }
288         else if self.is_uchar(cx) {
289             cx.char_type
290         }
291         else if self.is_ushort(cx) {
292             cx.short_type
293         }
294         else if self.is_uint(cx) {
295             cx.int_type
296         }
297         else if self.is_ulong(cx) {
298             cx.long_type
299         }
300         else if self.is_ulonglong(cx) {
301             cx.longlong_type
302         }
303         else {
304             self.clone()
305         }
306     }
307
308     fn to_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
309         if self.is_i8(cx) {
310             cx.u8_type
311         }
312         else if self.is_i16(cx) {
313             cx.u16_type
314         }
315         else if self.is_i32(cx) {
316             cx.u32_type
317         }
318         else if self.is_i64(cx) {
319             cx.u64_type
320         }
321         else if self.is_i128(cx) {
322             cx.u128_type
323         }
324         else if self.is_char(cx) {
325             cx.uchar_type
326         }
327         else if self.is_short(cx) {
328             cx.ushort_type
329         }
330         else if self.is_int(cx) {
331             cx.uint_type
332         }
333         else if self.is_long(cx) {
334             cx.ulong_type
335         }
336         else if self.is_longlong(cx) {
337             cx.ulonglong_type
338         }
339         else {
340             self.clone()
341         }
342     }
343 }
344
345 pub trait TypeReflection<'gcc, 'tcx>  {
346     fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
347     fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
348     fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
349     fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
350     fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
351     fn is_char(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
352     fn is_short(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
353     fn is_int(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
354     fn is_long(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
355     fn is_longlong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
356
357     fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
358     fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
359     fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
360     fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
361     fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
362     fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
363     fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
364     fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
365     fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
366     fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
367
368     fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
369     fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
370
371     fn is_vector(&self) -> bool;
372 }
373
374 impl<'gcc, 'tcx> TypeReflection<'gcc, 'tcx> for Type<'gcc> {
375     fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
376         self.unqualified() == cx.uchar_type
377     }
378
379     fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
380         self.unqualified() == cx.ushort_type
381     }
382
383     fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
384         self.unqualified() == cx.uint_type
385     }
386
387     fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
388         self.unqualified() == cx.ulong_type
389     }
390
391     fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
392         self.unqualified() == cx.ulonglong_type
393     }
394
395     fn is_char(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
396         self.unqualified() == cx.char_type
397     }
398
399     fn is_short(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
400         self.unqualified() == cx.short_type
401     }
402
403     fn is_int(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
404         self.unqualified() == cx.int_type
405     }
406
407     fn is_long(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
408         self.unqualified() == cx.long_type
409     }
410
411     fn is_longlong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
412         self.unqualified() == cx.longlong_type
413     }
414
415     fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
416         self.unqualified() == cx.i8_type
417     }
418
419     fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
420         self.unqualified() == cx.u8_type
421     }
422
423     fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
424         self.unqualified() == cx.i16_type
425     }
426
427     fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
428         self.unqualified() == cx.u16_type
429     }
430
431     fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
432         self.unqualified() == cx.i32_type
433     }
434
435     fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
436         self.unqualified() == cx.u32_type
437     }
438
439     fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
440         self.unqualified() == cx.i64_type
441     }
442
443     fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
444         self.unqualified() == cx.u64_type
445     }
446
447     fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
448         self.unqualified() == cx.i128_type.unqualified()
449     }
450
451     fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
452         self.unqualified() == cx.u128_type.unqualified()
453     }
454
455     fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
456         self.unqualified() == cx.context.new_type::<f32>()
457     }
458
459     fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
460         self.unqualified() == cx.context.new_type::<f64>()
461     }
462
463     fn is_vector(&self) -> bool {
464         let mut typ = self.clone();
465         loop {
466             if typ.dyncast_vector().is_some() {
467                 return true;
468             }
469
470             let old_type = typ;
471             typ = typ.unqualified();
472             if old_type == typ {
473                 break;
474             }
475         }
476
477         false
478     }
479 }