]>
Commit | Line | Data |
---|---|---|
c295e0f8 | 1 | use gccjit::LValue; |
5e7ed085 | 2 | use gccjit::{RValue, Type, ToRValue}; |
c295e0f8 XL |
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; | |
c295e0f8 | 12 | use rustc_middle::ty::layout::{TyAndLayout, LayoutOf}; |
5e7ed085 | 13 | use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar}; |
c295e0f8 XL |
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 | ||
c295e0f8 XL |
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)); | |
a2a8927a | 30 | global.global_set_initializer_rvalue(string); |
c295e0f8 XL |
31 | global |
32 | // TODO(antoyo): set linkage. | |
33 | } | |
c295e0f8 XL |
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(); | |
a2a8927a | 44 | context.new_array_constructor(None, typ, &elements) |
c295e0f8 XL |
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> { | |
5e7ed085 | 77 | self.gcc_int(typ, int) |
c295e0f8 XL |
78 | } |
79 | ||
80 | fn const_uint(&self, typ: Type<'gcc>, int: u64) -> RValue<'gcc> { | |
5e7ed085 | 81 | self.gcc_uint(typ, int) |
c295e0f8 XL |
82 | } |
83 | ||
84 | fn const_uint_big(&self, typ: Type<'gcc>, num: u128) -> RValue<'gcc> { | |
5e7ed085 | 85 | self.gcc_uint_big(typ, num) |
c295e0f8 XL |
86 | } |
87 | ||
88 | fn const_bool(&self, val: bool) -> RValue<'gcc> { | |
89 | self.const_uint(self.type_i1(), val as u64) | |
90 | } | |
91 | ||
5e7ed085 FG |
92 | fn const_i16(&self, i: i16) -> RValue<'gcc> { |
93 | self.const_int(self.type_i16(), i as i64) | |
94 | } | |
95 | ||
c295e0f8 XL |
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 | ||
923072b8 FG |
122 | fn const_real(&self, typ: Type<'gcc>, val: f64) -> RValue<'gcc> { |
123 | self.context.new_rvalue_from_double(typ, val) | |
c295e0f8 XL |
124 | } |
125 | ||
064997fb FG |
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(); | |
5e7ed085 | 135 | let cs = self.const_ptrcast(str_global.get_address(None), |
c295e0f8 XL |
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"); | |
a2a8927a | 148 | self.context.new_struct_constructor(None, struct_type.as_type(), None, values) |
c295e0f8 XL |
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 scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, ty: Type<'gcc>) -> RValue<'gcc> { | |
04454e1e | 162 | let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() }; |
c295e0f8 | 163 | match cv { |
c295e0f8 | 164 | Scalar::Int(int) => { |
04454e1e | 165 | let data = int.assert_bits(layout.size(self)); |
c295e0f8 XL |
166 | |
167 | // FIXME(antoyo): there's some issues with using the u128 code that follows, so hard-code | |
168 | // the paths for floating-point values. | |
169 | if ty == self.float_type { | |
170 | return self.context.new_rvalue_from_double(ty, f32::from_bits(data as u32) as f64); | |
171 | } | |
172 | else if ty == self.double_type { | |
173 | return self.context.new_rvalue_from_double(ty, f64::from_bits(data as u64)); | |
174 | } | |
175 | ||
176 | let value = self.const_uint_big(self.type_ix(bitsize), data); | |
5e7ed085 FG |
177 | // TODO(bjorn3): assert size is correct |
178 | self.const_bitcast(value, ty) | |
c295e0f8 XL |
179 | } |
180 | Scalar::Ptr(ptr, _size) => { | |
181 | let (alloc_id, offset) = ptr.into_parts(); | |
182 | let base_addr = | |
183 | match self.tcx.global_alloc(alloc_id) { | |
184 | GlobalAlloc::Memory(alloc) => { | |
185 | let init = const_alloc_to_gcc(self, alloc); | |
5e7ed085 | 186 | let alloc = alloc.inner(); |
c295e0f8 XL |
187 | let value = |
188 | match alloc.mutability { | |
189 | Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None), | |
190 | _ => self.static_addr_of(init, alloc.align, None), | |
191 | }; | |
192 | if !self.sess().fewer_names() { | |
193 | // TODO(antoyo): set value name. | |
194 | } | |
195 | value | |
196 | }, | |
197 | GlobalAlloc::Function(fn_instance) => { | |
198 | self.get_fn_addr(fn_instance) | |
199 | }, | |
064997fb FG |
200 | GlobalAlloc::VTable(ty, trait_ref) => { |
201 | let alloc = self.tcx.global_alloc(self.tcx.vtable_allocation((ty, trait_ref))).unwrap_memory(); | |
202 | let init = const_alloc_to_gcc(self, alloc); | |
203 | self.static_addr_of(init, alloc.inner().align, None) | |
204 | } | |
c295e0f8 XL |
205 | GlobalAlloc::Static(def_id) => { |
206 | assert!(self.tcx.is_static(def_id)); | |
207 | self.get_static(def_id).get_address(None) | |
208 | }, | |
209 | }; | |
210 | let ptr_type = base_addr.get_type(); | |
211 | let base_addr = self.const_bitcast(base_addr, self.usize_type); | |
212 | let offset = self.context.new_rvalue_from_long(self.usize_type, offset.bytes() as i64); | |
213 | let ptr = self.const_bitcast(base_addr + offset, ptr_type); | |
04454e1e | 214 | if layout.primitive() != Pointer { |
c295e0f8 XL |
215 | self.const_bitcast(ptr.dereference(None).to_rvalue(), ty) |
216 | } | |
217 | else { | |
218 | self.const_bitcast(ptr, ty) | |
219 | } | |
220 | } | |
221 | } | |
222 | } | |
223 | ||
5e7ed085 | 224 | fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value { |
c295e0f8 XL |
225 | const_alloc_to_gcc(self, alloc) |
226 | } | |
227 | ||
5e7ed085 FG |
228 | fn from_const_alloc(&self, layout: TyAndLayout<'tcx>, alloc: ConstAllocation<'tcx>, offset: Size) -> PlaceRef<'tcx, RValue<'gcc>> { |
229 | assert_eq!(alloc.inner().align, layout.align.abi); | |
c295e0f8 XL |
230 | let ty = self.type_ptr_to(layout.gcc_type(self, true)); |
231 | let value = | |
232 | if layout.size == Size::ZERO { | |
5e7ed085 | 233 | let value = self.const_usize(alloc.inner().align.bytes()); |
c295e0f8 XL |
234 | self.context.new_cast(None, value, ty) |
235 | } | |
236 | else { | |
237 | let init = const_alloc_to_gcc(self, alloc); | |
5e7ed085 | 238 | let base_addr = self.static_addr_of(init, alloc.inner().align, None); |
c295e0f8 XL |
239 | |
240 | let array = self.const_bitcast(base_addr, self.type_i8p()); | |
241 | let value = self.context.new_array_access(None, array, self.const_usize(offset.bytes())).get_address(None); | |
242 | self.const_bitcast(value, ty) | |
243 | }; | |
244 | PlaceRef::new_sized(value, layout) | |
245 | } | |
246 | ||
247 | fn const_ptrcast(&self, val: RValue<'gcc>, ty: Type<'gcc>) -> RValue<'gcc> { | |
248 | self.context.new_cast(None, val, ty) | |
249 | } | |
250 | } | |
251 | ||
252 | pub trait SignType<'gcc, 'tcx> { | |
253 | fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
254 | fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
255 | fn to_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>; | |
256 | fn to_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>; | |
257 | } | |
258 | ||
259 | impl<'gcc, 'tcx> SignType<'gcc, 'tcx> for Type<'gcc> { | |
260 | fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
261 | self.is_i8(cx) || self.is_i16(cx) || self.is_i32(cx) || self.is_i64(cx) || self.is_i128(cx) | |
262 | } | |
263 | ||
264 | fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
265 | self.is_u8(cx) || self.is_u16(cx) || self.is_u32(cx) || self.is_u64(cx) || self.is_u128(cx) | |
266 | } | |
267 | ||
268 | fn to_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> { | |
269 | if self.is_u8(cx) { | |
270 | cx.i8_type | |
271 | } | |
272 | else if self.is_u16(cx) { | |
273 | cx.i16_type | |
274 | } | |
275 | else if self.is_u32(cx) { | |
276 | cx.i32_type | |
277 | } | |
278 | else if self.is_u64(cx) { | |
279 | cx.i64_type | |
280 | } | |
281 | else if self.is_u128(cx) { | |
282 | cx.i128_type | |
283 | } | |
923072b8 FG |
284 | else if self.is_uchar(cx) { |
285 | cx.char_type | |
286 | } | |
287 | else if self.is_ushort(cx) { | |
288 | cx.short_type | |
289 | } | |
290 | else if self.is_uint(cx) { | |
291 | cx.int_type | |
292 | } | |
293 | else if self.is_ulong(cx) { | |
294 | cx.long_type | |
295 | } | |
296 | else if self.is_ulonglong(cx) { | |
297 | cx.longlong_type | |
298 | } | |
c295e0f8 XL |
299 | else { |
300 | self.clone() | |
301 | } | |
302 | } | |
303 | ||
304 | fn to_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> { | |
305 | if self.is_i8(cx) { | |
306 | cx.u8_type | |
307 | } | |
308 | else if self.is_i16(cx) { | |
309 | cx.u16_type | |
310 | } | |
311 | else if self.is_i32(cx) { | |
312 | cx.u32_type | |
313 | } | |
314 | else if self.is_i64(cx) { | |
315 | cx.u64_type | |
316 | } | |
317 | else if self.is_i128(cx) { | |
318 | cx.u128_type | |
319 | } | |
923072b8 FG |
320 | else if self.is_char(cx) { |
321 | cx.uchar_type | |
322 | } | |
323 | else if self.is_short(cx) { | |
324 | cx.ushort_type | |
325 | } | |
326 | else if self.is_int(cx) { | |
327 | cx.uint_type | |
328 | } | |
329 | else if self.is_long(cx) { | |
330 | cx.ulong_type | |
331 | } | |
332 | else if self.is_longlong(cx) { | |
333 | cx.ulonglong_type | |
334 | } | |
c295e0f8 XL |
335 | else { |
336 | self.clone() | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
341 | pub trait TypeReflection<'gcc, 'tcx> { | |
342 | fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
343 | fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
344 | fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
345 | fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
346 | fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
923072b8 FG |
347 | fn is_char(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; |
348 | fn is_short(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
349 | fn is_int(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
350 | fn is_long(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
351 | fn is_longlong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
c295e0f8 XL |
352 | |
353 | fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
354 | fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
355 | fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
356 | fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
357 | fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
358 | fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
359 | fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
360 | fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
361 | fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
362 | fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
363 | ||
364 | fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
365 | fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool; | |
923072b8 FG |
366 | |
367 | fn is_vector(&self) -> bool; | |
c295e0f8 XL |
368 | } |
369 | ||
370 | impl<'gcc, 'tcx> TypeReflection<'gcc, 'tcx> for Type<'gcc> { | |
371 | fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
923072b8 | 372 | self.unqualified() == cx.uchar_type |
c295e0f8 XL |
373 | } |
374 | ||
375 | fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
923072b8 | 376 | self.unqualified() == cx.ushort_type |
c295e0f8 XL |
377 | } |
378 | ||
379 | fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
380 | self.unqualified() == cx.uint_type | |
381 | } | |
382 | ||
383 | fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
384 | self.unqualified() == cx.ulong_type | |
385 | } | |
386 | ||
387 | fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
388 | self.unqualified() == cx.ulonglong_type | |
389 | } | |
390 | ||
923072b8 FG |
391 | fn is_char(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { |
392 | self.unqualified() == cx.char_type | |
393 | } | |
394 | ||
395 | fn is_short(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
396 | self.unqualified() == cx.short_type | |
397 | } | |
398 | ||
399 | fn is_int(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
400 | self.unqualified() == cx.int_type | |
401 | } | |
402 | ||
403 | fn is_long(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
404 | self.unqualified() == cx.long_type | |
405 | } | |
406 | ||
407 | fn is_longlong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
408 | self.unqualified() == cx.longlong_type | |
409 | } | |
410 | ||
c295e0f8 XL |
411 | fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { |
412 | self.unqualified() == cx.i8_type | |
413 | } | |
414 | ||
415 | fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
416 | self.unqualified() == cx.u8_type | |
417 | } | |
418 | ||
419 | fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
420 | self.unqualified() == cx.i16_type | |
421 | } | |
422 | ||
423 | fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
424 | self.unqualified() == cx.u16_type | |
425 | } | |
426 | ||
427 | fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
428 | self.unqualified() == cx.i32_type | |
429 | } | |
430 | ||
431 | fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
432 | self.unqualified() == cx.u32_type | |
433 | } | |
434 | ||
435 | fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
436 | self.unqualified() == cx.i64_type | |
437 | } | |
438 | ||
439 | fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
440 | self.unqualified() == cx.u64_type | |
441 | } | |
442 | ||
443 | fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
5e7ed085 | 444 | self.unqualified() == cx.i128_type.unqualified() |
c295e0f8 XL |
445 | } |
446 | ||
447 | fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
5e7ed085 | 448 | self.unqualified() == cx.u128_type.unqualified() |
c295e0f8 XL |
449 | } |
450 | ||
451 | fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
452 | self.unqualified() == cx.context.new_type::<f32>() | |
453 | } | |
454 | ||
455 | fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool { | |
456 | self.unqualified() == cx.context.new_type::<f64>() | |
457 | } | |
923072b8 FG |
458 | |
459 | fn is_vector(&self) -> bool { | |
460 | let mut typ = self.clone(); | |
461 | loop { | |
462 | if typ.dyncast_vector().is_some() { | |
463 | return true; | |
464 | } | |
465 | ||
466 | let old_type = typ; | |
467 | typ = typ.unqualified(); | |
468 | if old_type == typ { | |
469 | break; | |
470 | } | |
471 | } | |
472 | ||
473 | false | |
474 | } | |
c295e0f8 | 475 | } |