11use gccjit:: { LValue , RValue , ToRValue , Type } ;
2- use rustc_abi:: Primitive :: Pointer ;
3- use rustc_abi:: { self as abi , HasDataLayout } ;
2+ use rustc_abi as abi ;
3+ use rustc_abi:: { Align , HasDataLayout } ;
44use rustc_codegen_ssa:: traits:: {
5- BaseTypeCodegenMethods , ConstCodegenMethods , MiscCodegenMethods , StaticCodegenMethods ,
5+ BaseTypeCodegenMethods , ConstCodegenMethods , StaticCodegenMethods ,
66} ;
7- use rustc_middle:: mir:: Mutability ;
8- use rustc_middle:: mir:: interpret:: { ConstAllocation , GlobalAlloc , Scalar } ;
7+ use rustc_middle:: mir:: interpret:: ConstAllocation ;
98use rustc_middle:: ty:: layout:: LayoutOf ;
109
1110use crate :: context:: CodegenCx ;
@@ -109,7 +108,7 @@ pub fn type_is_pointer(typ: Type<'_>) -> bool {
109108 typ. get_pointee ( ) . is_some ( )
110109}
111110
112- impl < ' gcc , ' tcx > ConstCodegenMethods for CodegenCx < ' gcc , ' tcx > {
111+ impl < ' gcc , ' tcx > ConstCodegenMethods < ' tcx > for CodegenCx < ' gcc , ' tcx > {
113112 fn const_null ( & self , typ : Type < ' gcc > ) -> RValue < ' gcc > {
114113 if type_is_pointer ( typ) { self . context . new_null ( typ) } else { self . const_int ( typ, 0 ) }
115114 }
@@ -220,93 +219,6 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
220219 None
221220 }
222221
223- fn scalar_to_backend ( & self , cv : Scalar , layout : abi:: Scalar , ty : Type < ' gcc > ) -> RValue < ' gcc > {
224- let bitsize = if layout. is_bool ( ) { 1 } else { layout. size ( self ) . bits ( ) } ;
225- match cv {
226- Scalar :: Int ( int) => {
227- let data = int. to_bits ( layout. size ( self ) ) ;
228- let value = self . const_uint_big ( self . type_ix ( bitsize) , data) ;
229- let bytesize = layout. size ( self ) . bytes ( ) ;
230- if bitsize > 1 && ty. is_integral ( ) && bytesize as u32 == ty. get_size ( ) {
231- // NOTE: since the intrinsic _xabort is called with a bitcast, which
232- // is non-const, but expects a constant, do a normal cast instead of a bitcast.
233- // FIXME(antoyo): fix bitcast to work in constant contexts.
234- // TODO(antoyo): perhaps only use bitcast for pointers?
235- self . context . new_cast ( None , value, ty)
236- } else {
237- // TODO(bjorn3): assert size is correct
238- self . const_bitcast ( value, ty)
239- }
240- }
241- Scalar :: Ptr ( ptr, _size) => {
242- let ( prov, offset) = ptr. prov_and_relative_offset ( ) ;
243- let alloc_id = prov. alloc_id ( ) ;
244- let base_addr = match self . tcx . global_alloc ( alloc_id) {
245- GlobalAlloc :: Memory ( alloc) => {
246- // For ZSTs directly codegen an aligned pointer.
247- // This avoids generating a zero-sized constant value and actually needing a
248- // real address at runtime.
249- if alloc. inner ( ) . len ( ) == 0 {
250- assert_eq ! ( offset. bytes( ) , 0 ) ;
251- let val = self . const_usize ( alloc. inner ( ) . align . bytes ( ) ) ;
252- return if matches ! ( layout. primitive( ) , Pointer ( _) ) {
253- self . context . new_cast ( None , val, ty)
254- } else {
255- self . const_bitcast ( val, ty)
256- } ;
257- }
258-
259- let init = self . const_data_from_alloc ( alloc) ;
260- let alloc = alloc. inner ( ) ;
261- let value = match alloc. mutability {
262- Mutability :: Mut => self . static_addr_of_mut ( init, alloc. align , None ) ,
263- _ => self . static_addr_of ( init, alloc. align , None ) ,
264- } ;
265- if !self . sess ( ) . fewer_names ( ) {
266- // TODO(antoyo): set value name.
267- }
268- value
269- }
270- GlobalAlloc :: Function { instance, .. } => self . get_fn_addr ( instance) ,
271- GlobalAlloc :: VTable ( ty, dyn_ty) => {
272- let alloc = self
273- . tcx
274- . global_alloc ( self . tcx . vtable_allocation ( (
275- ty,
276- dyn_ty. principal ( ) . map ( |principal| {
277- self . tcx . instantiate_bound_regions_with_erased ( principal)
278- } ) ,
279- ) ) )
280- . unwrap_memory ( ) ;
281- let init = self . const_data_from_alloc ( alloc) ;
282- self . static_addr_of ( init, alloc. inner ( ) . align , None )
283- }
284- GlobalAlloc :: TypeId { .. } => {
285- let val = self . const_usize ( offset. bytes ( ) ) ;
286- // This is still a variable of pointer type, even though we only use the provenance
287- // of that pointer in CTFE and Miri. But to make LLVM's type system happy,
288- // we need an int-to-ptr cast here (it doesn't matter at all which provenance that picks).
289- return self . context . new_cast ( None , val, ty) ;
290- }
291- GlobalAlloc :: Static ( def_id) => {
292- assert ! ( self . tcx. is_static( def_id) ) ;
293- self . get_static ( def_id) . get_address ( None )
294- }
295- } ;
296- let ptr_type = base_addr. get_type ( ) ;
297- let base_addr = self . context . new_cast ( None , base_addr, self . usize_type ) ;
298- let offset =
299- self . context . new_rvalue_from_long ( self . usize_type , offset. bytes ( ) as i64 ) ;
300- let ptr = self . context . new_cast ( None , base_addr + offset, ptr_type) ;
301- if !matches ! ( layout. primitive( ) , Pointer ( _) ) {
302- self . const_bitcast ( ptr. dereference ( None ) . to_rvalue ( ) , ty)
303- } else {
304- self . context . new_cast ( None , ptr, ty)
305- }
306- }
307- }
308- }
309-
310222 fn const_data_from_alloc ( & self , alloc : ConstAllocation < ' _ > ) -> Self :: Value {
311223 // We ignore the alignment for the purpose of deduping RValues
312224 // The alignment is not handled / used in any way by `const_alloc_to_gcc`,
@@ -328,6 +240,31 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
328240 . new_array_access ( None , base_addr, self . const_usize ( offset. bytes ( ) ) )
329241 . get_address ( None )
330242 }
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+ }
331268}
332269
333270pub trait SignType < ' gcc , ' tcx > {
0 commit comments