@@ -11,7 +11,7 @@ use rustc_codegen_ssa::traits::{
1111use rustc_middle:: mir:: Mutability ;
1212use rustc_middle:: ty:: ScalarInt ;
1313use rustc_middle:: ty:: layout:: { TyAndLayout , LayoutOf } ;
14- use rustc_middle:: mir:: interpret:: { Allocation , GlobalAlloc , Scalar } ;
14+ use rustc_middle:: mir:: interpret:: { ConstAllocation , GlobalAlloc , Scalar } ;
1515use rustc_span:: Symbol ;
1616use rustc_target:: abi:: { self , HasDataLayout , Pointer , Size } ;
1717
@@ -24,18 +24,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
2424 bytes_in_context ( self , bytes)
2525 }
2626
27- fn const_cstr ( & self , symbol : Symbol , _null_terminated : bool ) -> LValue < ' gcc > {
28- // TODO(antoyo): handle null_terminated.
29- if let Some ( & value) = self . const_cstr_cache . borrow ( ) . get ( & symbol) {
30- return value;
31- }
32-
33- let global = self . global_string ( symbol. as_str ( ) ) ;
34-
35- self . const_cstr_cache . borrow_mut ( ) . insert ( symbol, global) ;
36- global
37- }
38-
3927 fn global_string ( & self , string : & str ) -> LValue < ' gcc > {
4028 // TODO(antoyo): handle non-null-terminated strings.
4129 let string = self . context . new_string_literal ( & * string) ;
@@ -134,8 +122,12 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
134122 }
135123
136124 fn const_str ( & self , s : Symbol ) -> ( RValue < ' gcc > , RValue < ' gcc > ) {
137- let len = s. as_str ( ) . len ( ) ;
138- let cs = self . const_ptrcast ( self . const_cstr ( s, false ) . get_address ( None ) ,
125+ let s_str = s. as_str ( ) ;
126+ let str_global = * self . const_str_cache . borrow_mut ( ) . entry ( s) . or_insert_with ( || {
127+ self . global_string ( s_str)
128+ } ) ;
129+ let len = s_str. len ( ) ;
130+ let cs = self . const_ptrcast ( str_global. get_address ( None ) ,
139131 self . type_ptr_to ( self . layout_of ( self . tcx . types . str_ ) . gcc_type ( self , true ) ) ,
140132 ) ;
141133 ( cs, self . const_usize ( len as u64 ) )
@@ -190,6 +182,7 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
190182 match self . tcx . global_alloc ( alloc_id) {
191183 GlobalAlloc :: Memory ( alloc) => {
192184 let init = const_alloc_to_gcc ( self , alloc) ;
185+ let alloc = alloc. inner ( ) ;
193186 let value =
194187 match alloc. mutability {
195188 Mutability :: Mut => self . static_addr_of_mut ( init, alloc. align , None ) ,
@@ -222,21 +215,21 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
222215 }
223216 }
224217
225- fn const_data_from_alloc ( & self , alloc : & Allocation ) -> Self :: Value {
218+ fn const_data_from_alloc ( & self , alloc : ConstAllocation < ' tcx > ) -> Self :: Value {
226219 const_alloc_to_gcc ( self , alloc)
227220 }
228221
229- fn from_const_alloc ( & self , layout : TyAndLayout < ' tcx > , alloc : & Allocation , offset : Size ) -> PlaceRef < ' tcx , RValue < ' gcc > > {
230- assert_eq ! ( alloc. align, layout. align. abi) ;
222+ fn from_const_alloc ( & self , layout : TyAndLayout < ' tcx > , alloc : ConstAllocation < ' tcx > , offset : Size ) -> PlaceRef < ' tcx , RValue < ' gcc > > {
223+ assert_eq ! ( alloc. inner ( ) . align, layout. align. abi) ;
231224 let ty = self . type_ptr_to ( layout. gcc_type ( self , true ) ) ;
232225 let value =
233226 if layout. size == Size :: ZERO {
234- let value = self . const_usize ( alloc. align . bytes ( ) ) ;
227+ let value = self . const_usize ( alloc. inner ( ) . align . bytes ( ) ) ;
235228 self . context . new_cast ( None , value, ty)
236229 }
237230 else {
238231 let init = const_alloc_to_gcc ( self , alloc) ;
239- let base_addr = self . static_addr_of ( init, alloc. align , None ) ;
232+ let base_addr = self . static_addr_of ( init, alloc. inner ( ) . align , None ) ;
240233
241234 let array = self . const_bitcast ( base_addr, self . type_i8p ( ) ) ;
242235 let value = self . context . new_array_access ( None , array, self . const_usize ( offset. bytes ( ) ) ) . get_address ( None ) ;
0 commit comments