33use std:: borrow:: Borrow ;
44
55use libc:: { c_char, c_uint} ;
6- use rustc_abi:: Primitive :: Pointer ;
7- use rustc_abi:: { self as abi, HasDataLayout as _} ;
8- use rustc_ast:: Mutability ;
6+ use rustc_abi as abi;
7+ use rustc_abi:: HasDataLayout ;
98use rustc_codegen_ssa:: common:: TypeKind ;
109use rustc_codegen_ssa:: traits:: * ;
11- use rustc_data_structures:: stable_hash:: { StableHash , StableHasher } ;
12- use rustc_hashes:: Hash128 ;
1310use rustc_hir:: def_id:: DefId ;
1411use rustc_middle:: bug;
15- use rustc_middle:: mir:: interpret:: { GlobalAlloc , PointerArithmetic , Scalar } ;
12+ use rustc_middle:: mir:: interpret:: ConstAllocation ;
1613use rustc_middle:: ty:: TyCtxt ;
1714use rustc_session:: cstore:: DllImport ;
1815use tracing:: debug;
@@ -130,7 +127,7 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
130127 }
131128}
132129
133- impl < ' ll , ' tcx > ConstCodegenMethods for CodegenCx < ' ll , ' tcx > {
130+ impl < ' ll , ' tcx > ConstCodegenMethods < ' tcx > for CodegenCx < ' ll , ' tcx > {
134131 fn const_null ( & self , t : & ' ll Type ) -> & ' ll Value {
135132 unsafe { llvm:: LLVMConstNull ( t) }
136133 }
@@ -268,101 +265,6 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
268265 } )
269266 }
270267
271- fn scalar_to_backend ( & self , cv : Scalar , layout : abi:: Scalar , llty : & ' ll Type ) -> & ' ll Value {
272- let bitsize = if layout. is_bool ( ) { 1 } else { layout. size ( self ) . bits ( ) } ;
273- match cv {
274- Scalar :: Int ( int) => {
275- let data = int. to_bits ( layout. size ( self ) ) ;
276- let llval = self . const_uint_big ( self . type_ix ( bitsize) , data) ;
277- if matches ! ( layout. primitive( ) , Pointer ( _) ) {
278- unsafe { llvm:: LLVMConstIntToPtr ( llval, llty) }
279- } else {
280- self . const_bitcast ( llval, llty)
281- }
282- }
283- Scalar :: Ptr ( ptr, _size) => {
284- let ( prov, offset) = ptr. prov_and_relative_offset ( ) ;
285- let global_alloc = self . tcx . global_alloc ( prov. alloc_id ( ) ) ;
286- let base_addr = match global_alloc {
287- GlobalAlloc :: Memory ( alloc) => {
288- // For ZSTs directly codegen an aligned pointer.
289- // This avoids generating a zero-sized constant value and actually needing a
290- // real address at runtime.
291- if alloc. inner ( ) . len ( ) == 0 {
292- let val = alloc. inner ( ) . align . bytes ( ) . wrapping_add ( offset. bytes ( ) ) ;
293- let llval = self . const_usize ( self . tcx . truncate_to_target_usize ( val) ) ;
294- return if matches ! ( layout. primitive( ) , Pointer ( _) ) {
295- unsafe { llvm:: LLVMConstIntToPtr ( llval, llty) }
296- } else {
297- self . const_bitcast ( llval, llty)
298- } ;
299- } else {
300- let init =
301- const_alloc_to_llvm ( self , alloc. inner ( ) , /*static*/ false ) ;
302- let alloc = alloc. inner ( ) ;
303- let value = match alloc. mutability {
304- Mutability :: Mut => self . static_addr_of_mut ( init, alloc. align , None ) ,
305- _ => self . static_addr_of_impl ( init, alloc. align , None ) ,
306- } ;
307- if !self . sess ( ) . fewer_names ( ) && llvm:: get_value_name ( value) . is_empty ( )
308- {
309- let hash = self . tcx . with_stable_hashing_context ( |mut hcx| {
310- let mut hasher = StableHasher :: new ( ) ;
311- alloc. stable_hash ( & mut hcx, & mut hasher) ;
312- hasher. finish :: < Hash128 > ( )
313- } ) ;
314- llvm:: set_value_name (
315- value,
316- format ! ( "alloc_{hash:032x}" ) . as_bytes ( ) ,
317- ) ;
318- }
319- value
320- }
321- }
322- GlobalAlloc :: Function { instance, .. } => self . get_fn_addr ( instance) ,
323- GlobalAlloc :: VTable ( ty, dyn_ty) => {
324- let alloc = self
325- . tcx
326- . global_alloc ( self . tcx . vtable_allocation ( (
327- ty,
328- dyn_ty. principal ( ) . map ( |principal| {
329- self . tcx . instantiate_bound_regions_with_erased ( principal)
330- } ) ,
331- ) ) )
332- . unwrap_memory ( ) ;
333- let init = const_alloc_to_llvm ( self , alloc. inner ( ) , /*static*/ false ) ;
334- self . static_addr_of_impl ( init, alloc. inner ( ) . align , None )
335- }
336- GlobalAlloc :: Static ( def_id) => {
337- assert ! ( self . tcx. is_static( def_id) ) ;
338- assert ! ( !self . tcx. is_thread_local_static( def_id) ) ;
339- self . get_static ( def_id)
340- }
341- GlobalAlloc :: TypeId { .. } => {
342- // Drop the provenance, the offset contains the bytes of the hash
343- let llval = self . const_usize ( offset. bytes ( ) ) ;
344- return unsafe { llvm:: LLVMConstIntToPtr ( llval, llty) } ;
345- }
346- } ;
347- let base_addr_space = global_alloc. address_space ( self ) ;
348- let llval = unsafe {
349- llvm:: LLVMConstInBoundsGEP2 (
350- self . type_i8 ( ) ,
351- // Cast to the required address space if necessary
352- self . const_pointercast ( base_addr, self . type_ptr_ext ( base_addr_space) ) ,
353- & self . const_usize ( offset. bytes ( ) ) ,
354- 1 ,
355- )
356- } ;
357- if !matches ! ( layout. primitive( ) , Pointer ( _) ) {
358- unsafe { llvm:: LLVMConstPtrToInt ( llval, llty) }
359- } else {
360- self . const_bitcast ( llval, llty)
361- }
362- }
363- }
364- }
365-
366268 fn const_ptr_byte_offset ( & self , base_addr : Self :: Value , offset : abi:: Size ) -> Self :: Value {
367269 unsafe {
368270 llvm:: LLVMConstInBoundsGEP2 (
@@ -373,6 +275,29 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
373275 )
374276 }
375277 }
278+
279+ fn const_bitcast ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
280+ unsafe { llvm:: LLVMConstBitCast ( val, ty) }
281+ }
282+ fn const_pointercast ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
283+ unsafe { llvm:: LLVMConstPointerCast ( val, ty) }
284+ }
285+ fn const_int_to_ptr ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
286+ unsafe { llvm:: LLVMConstIntToPtr ( val, ty) }
287+ }
288+ fn const_ptr_to_int ( & self , val : Self :: Value , ty : Self :: Type ) -> Self :: Value {
289+ unsafe { llvm:: LLVMConstPtrToInt ( val, ty) }
290+ }
291+
292+ fn static_addr_of_const ( & self , alloc : ConstAllocation < ' _ > , kind : Option < & str > ) -> Self :: Value {
293+ let cv = const_alloc_to_llvm ( self , alloc. inner ( ) , /*static*/ false ) ;
294+ self . static_addr_of_const ( cv, alloc. inner ( ) . align , kind)
295+ }
296+
297+ fn static_addr_of_mut ( & self , alloc : ConstAllocation < ' _ > , kind : Option < & str > ) -> Self :: Value {
298+ let cv = const_alloc_to_llvm ( self , alloc. inner ( ) , /*static*/ false ) ;
299+ self . static_addr_of_mut ( cv, alloc. inner ( ) . align , kind)
300+ }
376301}
377302
378303/// Get the [LLVM type][Type] of a [`Value`].
0 commit comments