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,86 +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
- let value = self . const_uint_big ( self . type_ix ( bitsize) , data) ;
238
- let bytesize = layout. size ( self ) . bytes ( ) ;
239
- if bitsize > 1 && ty. is_integral ( ) && bytesize as u32 == ty. get_size ( ) {
240
- // NOTE: since the intrinsic _xabort is called with a bitcast, which
241
- // is non-const, but expects a constant, do a normal cast instead of a bitcast.
242
- // FIXME(antoyo): fix bitcast to work in constant contexts.
243
- // TODO(antoyo): perhaps only use bitcast for pointers?
244
- self . context . new_cast ( None , value, ty)
245
- } else {
246
- // TODO(bjorn3): assert size is correct
247
- self . const_bitcast ( value, ty)
248
- }
249
- }
250
- Scalar :: Ptr ( ptr, _size) => {
251
- let ( prov, offset) = ptr. into_parts ( ) ; // we know the `offset` is relative
252
- let alloc_id = prov. alloc_id ( ) ;
253
- let base_addr = match self . tcx . global_alloc ( alloc_id) {
254
- GlobalAlloc :: Memory ( alloc) => {
255
- // For ZSTs directly codegen an aligned pointer.
256
- // This avoids generating a zero-sized constant value and actually needing a
257
- // real address at runtime.
258
- if alloc. inner ( ) . len ( ) == 0 {
259
- assert_eq ! ( offset. bytes( ) , 0 ) ;
260
- let val = self . const_usize ( alloc. inner ( ) . align . bytes ( ) ) ;
261
- return if matches ! ( layout. primitive( ) , Pointer ( _) ) {
262
- self . context . new_cast ( None , val, ty)
263
- } else {
264
- self . const_bitcast ( val, ty)
265
- } ;
266
- }
267
-
268
- let init = self . const_data_from_alloc ( alloc) ;
269
- let alloc = alloc. inner ( ) ;
270
- let value = match alloc. mutability {
271
- Mutability :: Mut => self . static_addr_of_mut ( init, alloc. align , None ) ,
272
- _ => self . static_addr_of ( init, alloc. align , None ) ,
273
- } ;
274
- if !self . sess ( ) . fewer_names ( ) {
275
- // TODO(antoyo): set value name.
276
- }
277
- value
278
- }
279
- GlobalAlloc :: Function { instance, .. } => self . get_fn_addr ( instance) ,
280
- GlobalAlloc :: VTable ( ty, dyn_ty) => {
281
- let alloc = self
282
- . tcx
283
- . global_alloc ( self . tcx . vtable_allocation ( (
284
- ty,
285
- dyn_ty. principal ( ) . map ( |principal| {
286
- self . tcx . instantiate_bound_regions_with_erased ( principal)
287
- } ) ,
288
- ) ) )
289
- . unwrap_memory ( ) ;
290
- let init = self . const_data_from_alloc ( alloc) ;
291
- self . static_addr_of ( init, alloc. inner ( ) . align , None )
292
- }
293
- GlobalAlloc :: Static ( def_id) => {
294
- assert ! ( self . tcx. is_static( def_id) ) ;
295
- self . get_static ( def_id) . get_address ( None )
296
- }
297
- } ;
298
- let ptr_type = base_addr. get_type ( ) ;
299
- let base_addr = self . context . new_cast ( None , base_addr, self . usize_type ) ;
300
- let offset =
301
- self . context . new_rvalue_from_long ( self . usize_type , offset. bytes ( ) as i64 ) ;
302
- let ptr = self . context . new_cast ( None , base_addr + offset, ptr_type) ;
303
- if !matches ! ( layout. primitive( ) , Pointer ( _) ) {
304
- self . const_bitcast ( ptr. dereference ( None ) . to_rvalue ( ) , ty)
305
- } else {
306
- self . context . new_cast ( None , ptr, ty)
307
- }
308
- }
309
- }
310
- }
311
-
312
230
fn const_data_from_alloc ( & self , alloc : ConstAllocation < ' _ > ) -> Self :: Value {
313
231
// We ignore the alignment for the purpose of deduping RValues
314
232
// The alignment is not handled / used in any way by `const_alloc_to_gcc`,
@@ -330,6 +248,31 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
330
248
. new_array_access ( None , base_addr, self . const_usize ( offset. bytes ( ) ) )
331
249
. get_address ( None )
332
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
+ }
333
276
}
334
277
335
278
pub trait SignType < ' gcc , ' tcx > {
0 commit comments