11use std:: convert:: TryFrom ;
22use std:: convert:: TryInto ;
33
4+ use gccjit:: LValue ;
45use gccjit:: { Block , CType , RValue , Type , ToRValue } ;
56use rustc_codegen_ssa:: mir:: place:: PlaceRef ;
67use rustc_codegen_ssa:: traits:: {
@@ -10,7 +11,6 @@ use rustc_codegen_ssa::traits::{
1011 MiscMethods ,
1112 StaticMethods ,
1213} ;
13- use rustc_middle:: bug;
1414use rustc_middle:: mir:: Mutability ;
1515use rustc_middle:: ty:: ScalarInt ;
1616use rustc_middle:: ty:: layout:: { TyAndLayout , LayoutOf } ;
@@ -27,28 +27,27 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
2727 bytes_in_context ( self , bytes)
2828 }
2929
30- fn const_cstr ( & self , symbol : Symbol , _null_terminated : bool ) -> RValue < ' gcc > {
30+ fn const_cstr ( & self , symbol : Symbol , _null_terminated : bool ) -> LValue < ' gcc > {
3131 // TODO(antoyo): handle null_terminated.
3232 if let Some ( & value) = self . const_cstr_cache . borrow ( ) . get ( & symbol) {
33- return value. to_rvalue ( ) ;
33+ return value;
3434 }
3535
3636 let global = self . global_string ( & * symbol. as_str ( ) ) ;
3737
38- self . const_cstr_cache . borrow_mut ( ) . insert ( symbol, global. dereference ( None ) ) ;
38+ self . const_cstr_cache . borrow_mut ( ) . insert ( symbol, global) ;
3939 global
4040 }
4141
42- fn global_string ( & self , string : & str ) -> RValue < ' gcc > {
42+ fn global_string ( & self , string : & str ) -> LValue < ' gcc > {
4343 // TODO(antoyo): handle non-null-terminated strings.
4444 let string = self . context . new_string_literal ( & * string) ;
4545 let sym = self . generate_local_symbol_name ( "str" ) ;
4646 // NOTE: TLS is always off for a string litteral.
4747 // NOTE: string litterals do not have a link section.
48- let global = self . define_global ( & sym, self . val_ty ( string) , false , None )
49- . unwrap_or_else ( || bug ! ( "symbol `{}` is already defined" , sym) ) ;
50- self . global_init_block . add_assignment ( None , global. dereference ( None ) , string) ;
51- global. to_rvalue ( )
48+ let global = self . declare_private_global ( & sym, self . val_ty ( string) ) ;
49+ global. global_set_initializer_value ( string) ; // TODO: only set if not imported?
50+ global
5251 // TODO(antoyo): set linkage.
5352 }
5453
@@ -76,10 +75,13 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
7675
7776pub fn bytes_in_context < ' gcc , ' tcx > ( cx : & CodegenCx < ' gcc , ' tcx > , bytes : & [ u8 ] ) -> RValue < ' gcc > {
7877 let context = & cx. context ;
79- let typ = context. new_array_type ( None , context. new_type :: < u8 > ( ) , bytes. len ( ) as i32 ) ;
80- let global = cx. declare_unnamed_global ( typ) ;
81- global. global_set_initializer ( bytes) ;
82- global. to_rvalue ( )
78+ let byte_type = context. new_type :: < u8 > ( ) ;
79+ let typ = context. new_array_type ( None , byte_type, bytes. len ( ) as i32 ) ;
80+ let elements: Vec < _ > =
81+ bytes. iter ( )
82+ . map ( |& byte| context. new_rvalue_from_int ( byte_type, byte as i32 ) )
83+ . collect ( ) ;
84+ context. new_rvalue_from_array ( None , typ, & elements)
8385}
8486
8587pub fn type_is_pointer < ' gcc > ( typ : Type < ' gcc > ) -> bool {
@@ -180,7 +182,7 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
180182
181183 fn const_str ( & self , s : Symbol ) -> ( RValue < ' gcc > , RValue < ' gcc > ) {
182184 let len = s. as_str ( ) . len ( ) ;
183- let cs = self . const_ptrcast ( self . const_cstr ( s, false ) ,
185+ let cs = self . const_ptrcast ( self . const_cstr ( s, false ) . get_address ( None ) ,
184186 self . type_ptr_to ( self . layout_of ( self . tcx . types . str_ ) . gcc_type ( self , true ) ) ,
185187 ) ;
186188 ( cs, self . const_usize ( len as u64 ) )
@@ -191,16 +193,9 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
191193 . map ( |value| value. get_type ( ) )
192194 . collect ( ) ;
193195 // TODO(antoyo): cache the type? It's anonymous, so probably not.
194- let name = fields. iter ( ) . map ( |typ| format ! ( "{:?}" , typ) ) . collect :: < Vec < _ > > ( ) . join ( "_" ) ;
195196 let typ = self . type_struct ( & fields, packed) ;
196- let structure = self . global_init_func . new_local ( None , typ, & name) ;
197197 let struct_type = typ. is_struct ( ) . expect ( "struct type" ) ;
198- for ( index, value) in values. iter ( ) . enumerate ( ) {
199- let field = struct_type. get_field ( index as i32 ) ;
200- let field_lvalue = structure. access_field ( None , field) ;
201- self . global_init_block . add_assignment ( None , field_lvalue, * value) ;
202- }
203- self . lvalue_to_rvalue ( structure)
198+ self . context . new_rvalue_from_struct ( None , struct_type, values)
204199 }
205200
206201 fn const_to_opt_uint ( & self , _v : RValue < ' gcc > ) -> Option < u64 > {
@@ -260,19 +255,18 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
260255 } ,
261256 GlobalAlloc :: Static ( def_id) => {
262257 assert ! ( self . tcx. is_static( def_id) ) ;
263- self . get_static ( def_id)
258+ self . get_static ( def_id) . get_address ( None )
264259 } ,
265260 } ;
266261 let ptr_type = base_addr. get_type ( ) ;
267262 let base_addr = self . const_bitcast ( base_addr, self . usize_type ) ;
268263 let offset = self . context . new_rvalue_from_long ( self . usize_type , offset. bytes ( ) as i64 ) ;
269264 let ptr = self . const_bitcast ( base_addr + offset, ptr_type) ;
270- let value = ptr. dereference ( None ) ;
271265 if layout. value != Pointer {
272- self . const_bitcast ( value . to_rvalue ( ) , ty)
266+ self . const_bitcast ( ptr . dereference ( None ) . to_rvalue ( ) , ty)
273267 }
274268 else {
275- self . const_bitcast ( value . get_address ( None ) , ty)
269+ self . const_bitcast ( ptr , ty)
276270 }
277271 }
278272 }
0 commit comments