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