11use gccjit:: { LValue , RValue , ToRValue , Type } ;
22use rustc_abi as abi;
3- use rustc_abi:: HasDataLayout ;
4- use rustc_abi:: Primitive :: Pointer ;
3+ use rustc_abi:: { Align , HasDataLayout } ;
54use rustc_codegen_ssa:: traits:: {
6- BaseTypeCodegenMethods , ConstCodegenMethods , MiscCodegenMethods , StaticCodegenMethods ,
5+ BaseTypeCodegenMethods , ConstCodegenMethods , StaticCodegenMethods ,
76} ;
8- use rustc_middle:: mir:: Mutability ;
9- use rustc_middle:: mir:: interpret:: { ConstAllocation , GlobalAlloc , Scalar } ;
7+ use rustc_middle:: mir:: interpret:: ConstAllocation ;
108use rustc_middle:: ty:: layout:: LayoutOf ;
119
1210use crate :: context:: CodegenCx ;
@@ -110,7 +108,7 @@ pub fn type_is_pointer(typ: Type<'_>) -> bool {
110108 typ. get_pointee ( ) . is_some ( )
111109}
112110
113- impl < ' gcc , ' tcx > ConstCodegenMethods for CodegenCx < ' gcc , ' tcx > {
111+ impl < ' gcc , ' tcx > ConstCodegenMethods < ' tcx > for CodegenCx < ' gcc , ' tcx > {
114112 fn const_null ( & self , typ : Type < ' gcc > ) -> RValue < ' gcc > {
115113 if type_is_pointer ( typ) { self . context . new_null ( typ) } else { self . const_int ( typ, 0 ) }
116114 }
@@ -221,86 +219,6 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
221219 None
222220 }
223221
224- fn scalar_to_backend ( & self , cv : Scalar , layout : abi:: Scalar , ty : Type < ' gcc > ) -> RValue < ' gcc > {
225- let bitsize = if layout. is_bool ( ) { 1 } else { layout. size ( self ) . bits ( ) } ;
226- match cv {
227- Scalar :: Int ( int) => {
228- let data = int. to_bits ( layout. size ( self ) ) ;
229- let value = self . const_uint_big ( self . type_ix ( bitsize) , data) ;
230- let bytesize = layout. size ( self ) . bytes ( ) ;
231- if bitsize > 1 && ty. is_integral ( ) && bytesize as u32 == ty. get_size ( ) {
232- // NOTE: since the intrinsic _xabort is called with a bitcast, which
233- // is non-const, but expects a constant, do a normal cast instead of a bitcast.
234- // FIXME(antoyo): fix bitcast to work in constant contexts.
235- // TODO(antoyo): perhaps only use bitcast for pointers?
236- self . context . new_cast ( None , value, ty)
237- } else {
238- // TODO(bjorn3): assert size is correct
239- self . const_bitcast ( value, ty)
240- }
241- }
242- Scalar :: Ptr ( ptr, _size) => {
243- let ( prov, offset) = ptr. prov_and_relative_offset ( ) ;
244- let alloc_id = prov. alloc_id ( ) ;
245- let base_addr = match self . tcx . global_alloc ( alloc_id) {
246- GlobalAlloc :: Memory ( alloc) => {
247- // For ZSTs directly codegen an aligned pointer.
248- // This avoids generating a zero-sized constant value and actually needing a
249- // real address at runtime.
250- if alloc. inner ( ) . len ( ) == 0 {
251- assert_eq ! ( offset. bytes( ) , 0 ) ;
252- let val = self . const_usize ( alloc. inner ( ) . align . bytes ( ) ) ;
253- return if matches ! ( layout. primitive( ) , Pointer ( _) ) {
254- self . context . new_cast ( None , val, ty)
255- } else {
256- self . const_bitcast ( val, ty)
257- } ;
258- }
259-
260- let init = self . const_data_from_alloc ( alloc) ;
261- let alloc = alloc. inner ( ) ;
262- let value = match alloc. mutability {
263- Mutability :: Mut => self . static_addr_of_mut ( init, alloc. align , None ) ,
264- _ => self . static_addr_of ( init, alloc. align , None ) ,
265- } ;
266- if !self . sess ( ) . fewer_names ( ) {
267- // TODO(antoyo): set value name.
268- }
269- value
270- }
271- GlobalAlloc :: Function { instance, .. } => self . get_fn_addr ( instance) ,
272- GlobalAlloc :: VTable ( ty, dyn_ty) => {
273- let alloc = self
274- . tcx
275- . global_alloc ( self . tcx . vtable_allocation ( (
276- ty,
277- dyn_ty. principal ( ) . map ( |principal| {
278- self . tcx . instantiate_bound_regions_with_erased ( principal)
279- } ) ,
280- ) ) )
281- . unwrap_memory ( ) ;
282- let init = self . const_data_from_alloc ( alloc) ;
283- self . static_addr_of ( init, alloc. inner ( ) . align , None )
284- }
285- GlobalAlloc :: Static ( def_id) => {
286- assert ! ( self . tcx. is_static( def_id) ) ;
287- self . get_static ( def_id) . get_address ( None )
288- }
289- } ;
290- let ptr_type = base_addr. get_type ( ) ;
291- let base_addr = self . context . new_cast ( None , base_addr, self . usize_type ) ;
292- let offset =
293- self . context . new_rvalue_from_long ( self . usize_type , offset. bytes ( ) as i64 ) ;
294- let ptr = self . context . new_cast ( None , base_addr + offset, ptr_type) ;
295- if !matches ! ( layout. primitive( ) , Pointer ( _) ) {
296- self . const_bitcast ( ptr. dereference ( None ) . to_rvalue ( ) , ty)
297- } else {
298- self . context . new_cast ( None , ptr, ty)
299- }
300- }
301- }
302- }
303-
304222 fn const_data_from_alloc ( & self , alloc : ConstAllocation < ' _ > ) -> Self :: Value {
305223 // We ignore the alignment for the purpose of deduping RValues
306224 // The alignment is not handled / used in any way by `const_alloc_to_gcc`,
@@ -322,6 +240,31 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
322240 . new_array_access ( None , base_addr, self . const_usize ( offset. bytes ( ) ) )
323241 . get_address ( None )
324242 }
243+ fn const_bitcast ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
244+ self . const_bitcast ( val, ty)
245+ }
246+ fn const_pointercast ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
247+ self . context . new_cast ( None , val, ty)
248+ }
249+ fn const_int_to_ptr ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
250+ self . context . new_cast ( None , val, ty)
251+ }
252+ fn const_ptr_to_int ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
253+ self . context . new_cast ( None , val, ty)
254+ }
255+
256+ fn static_addr_of_impl (
257+ & self ,
258+ cv : Self :: Value ,
259+ align : Align ,
260+ kind : Option < & str > ,
261+ ) -> Self :: Value {
262+ self . static_addr_of ( cv, align, kind)
263+ }
264+
265+ fn static_addr_of_mut ( & self , cv : Self :: Value , align : Align , kind : Option < & str > ) -> Self :: Value {
266+ self . static_addr_of_mut ( cv, align, kind)
267+ }
325268}
326269
327270pub trait SignType < ' gcc , ' tcx > {
0 commit comments