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