]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/common.rs
Rollup merge of #99523 - cuviper:asfd_ptrs-1.64, r=jyn514
[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::Static(def_id) => {
205                             assert!(self.tcx.is_static(def_id));
206                             self.get_static(def_id).get_address(None)
207                         },
208                     };
209                 let ptr_type = base_addr.get_type();
210                 let base_addr = self.const_bitcast(base_addr, self.usize_type);
211                 let offset = self.context.new_rvalue_from_long(self.usize_type, offset.bytes() as i64);
212                 let ptr = self.const_bitcast(base_addr + offset, ptr_type);
213                 if layout.primitive() != Pointer {
214                     self.const_bitcast(ptr.dereference(None).to_rvalue(), ty)
215                 }
216                 else {
217                     self.const_bitcast(ptr, ty)
218                 }
219             }
220         }
221     }
222
223     fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value {
224         const_alloc_to_gcc(self, alloc)
225     }
226
227     fn from_const_alloc(&self, layout: TyAndLayout<'tcx>, alloc: ConstAllocation<'tcx>, offset: Size) -> PlaceRef<'tcx, RValue<'gcc>> {
228         assert_eq!(alloc.inner().align, layout.align.abi);
229         let ty = self.type_ptr_to(layout.gcc_type(self, true));
230         let value =
231             if layout.size == Size::ZERO {
232                 let value = self.const_usize(alloc.inner().align.bytes());
233                 self.context.new_cast(None, value, ty)
234             }
235             else {
236                 let init = const_alloc_to_gcc(self, alloc);
237                 let base_addr = self.static_addr_of(init, alloc.inner().align, None);
238
239                 let array = self.const_bitcast(base_addr, self.type_i8p());
240                 let value = self.context.new_array_access(None, array, self.const_usize(offset.bytes())).get_address(None);
241                 self.const_bitcast(value, ty)
242             };
243         PlaceRef::new_sized(value, layout)
244     }
245
246     fn const_ptrcast(&self, val: RValue<'gcc>, ty: Type<'gcc>) -> RValue<'gcc> {
247         self.context.new_cast(None, val, ty)
248     }
249 }
250
251 pub trait SignType<'gcc, 'tcx> {
252     fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
253     fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
254     fn to_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
255     fn to_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
256 }
257
258 impl<'gcc, 'tcx> SignType<'gcc, 'tcx> for Type<'gcc> {
259     fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
260         self.is_i8(cx) || self.is_i16(cx) || self.is_i32(cx) || self.is_i64(cx) || self.is_i128(cx)
261     }
262
263     fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
264         self.is_u8(cx) || self.is_u16(cx) || self.is_u32(cx) || self.is_u64(cx) || self.is_u128(cx)
265     }
266
267     fn to_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
268         if self.is_u8(cx) {
269             cx.i8_type
270         }
271         else if self.is_u16(cx) {
272             cx.i16_type
273         }
274         else if self.is_u32(cx) {
275             cx.i32_type
276         }
277         else if self.is_u64(cx) {
278             cx.i64_type
279         }
280         else if self.is_u128(cx) {
281             cx.i128_type
282         }
283         else if self.is_uchar(cx) {
284             cx.char_type
285         }
286         else if self.is_ushort(cx) {
287             cx.short_type
288         }
289         else if self.is_uint(cx) {
290             cx.int_type
291         }
292         else if self.is_ulong(cx) {
293             cx.long_type
294         }
295         else if self.is_ulonglong(cx) {
296             cx.longlong_type
297         }
298         else {
299             self.clone()
300         }
301     }
302
303     fn to_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
304         if self.is_i8(cx) {
305             cx.u8_type
306         }
307         else if self.is_i16(cx) {
308             cx.u16_type
309         }
310         else if self.is_i32(cx) {
311             cx.u32_type
312         }
313         else if self.is_i64(cx) {
314             cx.u64_type
315         }
316         else if self.is_i128(cx) {
317             cx.u128_type
318         }
319         else if self.is_char(cx) {
320             cx.uchar_type
321         }
322         else if self.is_short(cx) {
323             cx.ushort_type
324         }
325         else if self.is_int(cx) {
326             cx.uint_type
327         }
328         else if self.is_long(cx) {
329             cx.ulong_type
330         }
331         else if self.is_longlong(cx) {
332             cx.ulonglong_type
333         }
334         else {
335             self.clone()
336         }
337     }
338 }
339
340 pub trait TypeReflection<'gcc, 'tcx>  {
341     fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
342     fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
343     fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
344     fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
345     fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
346     fn is_char(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
347     fn is_short(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
348     fn is_int(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
349     fn is_long(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
350     fn is_longlong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
351
352     fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
353     fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
354     fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
355     fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
356     fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
357     fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
358     fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
359     fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
360     fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
361     fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
362
363     fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
364     fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
365
366     fn is_vector(&self) -> bool;
367 }
368
369 impl<'gcc, 'tcx> TypeReflection<'gcc, 'tcx> for Type<'gcc> {
370     fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
371         self.unqualified() == cx.uchar_type
372     }
373
374     fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
375         self.unqualified() == cx.ushort_type
376     }
377
378     fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
379         self.unqualified() == cx.uint_type
380     }
381
382     fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
383         self.unqualified() == cx.ulong_type
384     }
385
386     fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
387         self.unqualified() == cx.ulonglong_type
388     }
389
390     fn is_char(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
391         self.unqualified() == cx.char_type
392     }
393
394     fn is_short(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
395         self.unqualified() == cx.short_type
396     }
397
398     fn is_int(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
399         self.unqualified() == cx.int_type
400     }
401
402     fn is_long(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
403         self.unqualified() == cx.long_type
404     }
405
406     fn is_longlong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
407         self.unqualified() == cx.longlong_type
408     }
409
410     fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
411         self.unqualified() == cx.i8_type
412     }
413
414     fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
415         self.unqualified() == cx.u8_type
416     }
417
418     fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
419         self.unqualified() == cx.i16_type
420     }
421
422     fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
423         self.unqualified() == cx.u16_type
424     }
425
426     fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
427         self.unqualified() == cx.i32_type
428     }
429
430     fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
431         self.unqualified() == cx.u32_type
432     }
433
434     fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
435         self.unqualified() == cx.i64_type
436     }
437
438     fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
439         self.unqualified() == cx.u64_type
440     }
441
442     fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
443         self.unqualified() == cx.i128_type.unqualified()
444     }
445
446     fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
447         self.unqualified() == cx.u128_type.unqualified()
448     }
449
450     fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
451         self.unqualified() == cx.context.new_type::<f32>()
452     }
453
454     fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
455         self.unqualified() == cx.context.new_type::<f64>()
456     }
457
458     fn is_vector(&self) -> bool {
459         let mut typ = self.clone();
460         loop {
461             if typ.dyncast_vector().is_some() {
462                 return true;
463             }
464
465             let old_type = typ;
466             typ = typ.unqualified();
467             if old_type == typ {
468                 break;
469             }
470         }
471
472         false
473     }
474 }