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