1
1
use gccjit:: { LValue , RValue , ToRValue , Type } ;
2
2
use rustc_abi as abi;
3
- use rustc_abi:: HasDataLayout ;
4
- use rustc_abi:: Primitive :: Pointer ;
3
+ use rustc_abi:: { Align , HasDataLayout } ;
5
4
use rustc_codegen_ssa:: traits:: {
6
- BaseTypeCodegenMethods , ConstCodegenMethods , MiscCodegenMethods , StaticCodegenMethods ,
5
+ BaseTypeCodegenMethods , ConstCodegenMethods , StaticCodegenMethods ,
7
6
} ;
8
- use rustc_middle:: mir:: Mutability ;
9
- use rustc_middle:: mir:: interpret:: { ConstAllocation , GlobalAlloc , Scalar } ;
7
+ use rustc_middle:: mir:: interpret:: ConstAllocation ;
10
8
use rustc_middle:: ty:: layout:: LayoutOf ;
11
9
12
10
use crate :: context:: CodegenCx ;
@@ -110,7 +108,7 @@ pub fn type_is_pointer(typ: Type<'_>) -> bool {
110
108
typ. get_pointee ( ) . is_some ( )
111
109
}
112
110
113
- impl < ' gcc , ' tcx > ConstCodegenMethods for CodegenCx < ' gcc , ' tcx > {
111
+ impl < ' gcc , ' tcx > ConstCodegenMethods < ' tcx > for CodegenCx < ' gcc , ' tcx > {
114
112
fn const_null ( & self , typ : Type < ' gcc > ) -> RValue < ' gcc > {
115
113
if type_is_pointer ( typ) { self . context . new_null ( typ) } else { self . const_int ( typ, 0 ) }
116
114
}
@@ -229,99 +227,6 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
229
227
None
230
228
}
231
229
232
- fn scalar_to_backend ( & self , cv : Scalar , layout : abi:: Scalar , ty : Type < ' gcc > ) -> RValue < ' gcc > {
233
- let bitsize = if layout. is_bool ( ) { 1 } else { layout. size ( self ) . bits ( ) } ;
234
- match cv {
235
- Scalar :: Int ( int) => {
236
- let data = int. to_bits ( layout. size ( self ) ) ;
237
-
238
- // FIXME(antoyo): there's some issues with using the u128 code that follows, so hard-code
239
- // the paths for floating-point values.
240
- // TODO: Remove this code?
241
- /*if ty == self.float_type {
242
- return self
243
- .context
244
- .new_rvalue_from_double(ty, f32::from_bits(data as u32) as f64);
245
- }
246
- if ty == self.double_type {
247
- return self.context.new_rvalue_from_double(ty, f64::from_bits(data as u64));
248
- }*/
249
-
250
- let value = self . const_uint_big ( self . type_ix ( bitsize) , data) ;
251
- let bytesize = layout. size ( self ) . bytes ( ) ;
252
- if bitsize > 1 && ty. is_integral ( ) && bytesize as u32 == ty. get_size ( ) {
253
- // NOTE: since the intrinsic _xabort is called with a bitcast, which
254
- // is non-const, but expects a constant, do a normal cast instead of a bitcast.
255
- // FIXME(antoyo): fix bitcast to work in constant contexts.
256
- // TODO(antoyo): perhaps only use bitcast for pointers?
257
- self . context . new_cast ( None , value, ty)
258
- } else {
259
- // TODO(bjorn3): assert size is correct
260
- self . const_bitcast ( value, ty)
261
- }
262
- }
263
- Scalar :: Ptr ( ptr, _size) => {
264
- let ( prov, offset) = ptr. into_parts ( ) ; // we know the `offset` is relative
265
- let alloc_id = prov. alloc_id ( ) ;
266
- let base_addr = match self . tcx . global_alloc ( alloc_id) {
267
- GlobalAlloc :: Memory ( alloc) => {
268
- // For ZSTs directly codegen an aligned pointer.
269
- // This avoids generating a zero-sized constant value and actually needing a
270
- // real address at runtime.
271
- if alloc. inner ( ) . len ( ) == 0 {
272
- assert_eq ! ( offset. bytes( ) , 0 ) ;
273
- let val = self . const_usize ( alloc. inner ( ) . align . bytes ( ) ) ;
274
- return if matches ! ( layout. primitive( ) , Pointer ( _) ) {
275
- self . context . new_cast ( None , val, ty)
276
- } else {
277
- self . const_bitcast ( val, ty)
278
- } ;
279
- }
280
-
281
- let init = self . const_data_from_alloc ( alloc) ;
282
- let alloc = alloc. inner ( ) ;
283
- let value = match alloc. mutability {
284
- Mutability :: Mut => self . static_addr_of_mut ( init, alloc. align , None ) ,
285
- _ => self . static_addr_of ( init, alloc. align , None ) ,
286
- } ;
287
- if !self . sess ( ) . fewer_names ( ) {
288
- // TODO(antoyo): set value name.
289
- }
290
- value
291
- }
292
- GlobalAlloc :: Function { instance, .. } => self . get_fn_addr ( instance) ,
293
- GlobalAlloc :: VTable ( ty, dyn_ty) => {
294
- let alloc = self
295
- . tcx
296
- . global_alloc ( self . tcx . vtable_allocation ( (
297
- ty,
298
- dyn_ty. principal ( ) . map ( |principal| {
299
- self . tcx . instantiate_bound_regions_with_erased ( principal)
300
- } ) ,
301
- ) ) )
302
- . unwrap_memory ( ) ;
303
- let init = self . const_data_from_alloc ( alloc) ;
304
- self . static_addr_of ( init, alloc. inner ( ) . align , None )
305
- }
306
- GlobalAlloc :: Static ( def_id) => {
307
- assert ! ( self . tcx. is_static( def_id) ) ;
308
- self . get_static ( def_id) . get_address ( None )
309
- }
310
- } ;
311
- let ptr_type = base_addr. get_type ( ) ;
312
- let base_addr = self . context . new_cast ( None , base_addr, self . usize_type ) ;
313
- let offset =
314
- self . context . new_rvalue_from_long ( self . usize_type , offset. bytes ( ) as i64 ) ;
315
- let ptr = self . context . new_cast ( None , base_addr + offset, ptr_type) ;
316
- if !matches ! ( layout. primitive( ) , Pointer ( _) ) {
317
- self . const_bitcast ( ptr. dereference ( None ) . to_rvalue ( ) , ty)
318
- } else {
319
- self . context . new_cast ( None , ptr, ty)
320
- }
321
- }
322
- }
323
- }
324
-
325
230
fn const_data_from_alloc ( & self , alloc : ConstAllocation < ' _ > ) -> Self :: Value {
326
231
// We ignore the alignment for the purpose of deduping RValues
327
232
// The alignment is not handled / used in any way by `const_alloc_to_gcc`,
@@ -343,6 +248,31 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
343
248
. new_array_access ( None , base_addr, self . const_usize ( offset. bytes ( ) ) )
344
249
. get_address ( None )
345
250
}
251
+ fn const_bitcast ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
252
+ self . const_bitcast ( val, ty)
253
+ }
254
+ fn const_pointercast ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
255
+ self . context . new_cast ( None , val, ty)
256
+ }
257
+ fn const_int_to_ptr ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
258
+ self . context . new_cast ( None , val, ty)
259
+ }
260
+ fn const_ptr_to_int ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
261
+ self . context . new_cast ( None , val, ty)
262
+ }
263
+
264
+ fn static_addr_of_impl (
265
+ & self ,
266
+ cv : Self :: Value ,
267
+ align : Align ,
268
+ kind : Option < & str > ,
269
+ ) -> Self :: Value {
270
+ self . static_addr_of ( cv, align, kind)
271
+ }
272
+
273
+ fn static_addr_of_mut ( & self , cv : Self :: Value , align : Align , kind : Option < & str > ) -> Self :: Value {
274
+ self . static_addr_of_mut ( cv, align, kind)
275
+ }
346
276
}
347
277
348
278
pub trait SignType < ' gcc , ' tcx > {
0 commit comments