@@ -129,19 +129,25 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
129129 pub fn new ( context : & ' gcc Context < ' gcc > , codegen_unit : & ' tcx CodegenUnit < ' tcx > , tcx : TyCtxt < ' tcx > , supports_128bit_integers : bool ) -> Self {
130130 let check_overflow = tcx. sess . overflow_checks ( ) ;
131131
132- let i8_type = context. new_c_type ( CType :: Int8t ) ;
133- let i16_type = context. new_c_type ( CType :: Int16t ) ;
134- let i32_type = context. new_c_type ( CType :: Int32t ) ;
135- let i64_type = context. new_c_type ( CType :: Int64t ) ;
136- let u8_type = context. new_c_type ( CType :: UInt8t ) ;
137- let u16_type = context. new_c_type ( CType :: UInt16t ) ;
138- let u32_type = context. new_c_type ( CType :: UInt32t ) ;
139- let u64_type = context. new_c_type ( CType :: UInt64t ) ;
132+ let create_type = |ctype, rust_type| {
133+ let layout = tcx. layout_of ( ParamEnv :: reveal_all ( ) . and ( rust_type) ) . unwrap ( ) ;
134+ let align = layout. align . abi . bytes ( ) ;
135+ context. new_c_type ( ctype) . get_aligned ( align)
136+ } ;
137+
138+ let i8_type = create_type ( CType :: Int8t , tcx. types . i8 ) ;
139+ let i16_type = create_type ( CType :: Int16t , tcx. types . i16 ) ;
140+ let i32_type = create_type ( CType :: Int32t , tcx. types . i32 ) ;
141+ let i64_type = create_type ( CType :: Int64t , tcx. types . i64 ) ;
142+ let u8_type = create_type ( CType :: UInt8t , tcx. types . u8 ) ;
143+ let u16_type = create_type ( CType :: UInt16t , tcx. types . u16 ) ;
144+ let u32_type = create_type ( CType :: UInt32t , tcx. types . u32 ) ;
145+ let u64_type = create_type ( CType :: UInt64t , tcx. types . u64 ) ;
140146
141147 let ( i128_type, u128_type) =
142148 if supports_128bit_integers {
143- let i128_type = context . new_c_type ( CType :: Int128t ) . get_aligned ( 8 ) ; // TODO(antoyo): should the alignment be hard-coded? ;
144- let u128_type = context . new_c_type ( CType :: UInt128t ) . get_aligned ( 8 ) ; // TODO(antoyo): should the alignment be hard-coded? ;
149+ let i128_type = create_type ( CType :: Int128t , tcx . types . i128 ) ;
150+ let u128_type = create_type ( CType :: UInt128t , tcx . types . u128 ) ;
145151 ( i128_type, u128_type)
146152 }
147153 else {
@@ -265,15 +271,16 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
265271 }
266272
267273 pub fn is_native_int_type ( & self , typ : Type < ' gcc > ) -> bool {
274+ // TODO: cache those types to not query libgccjit everytime this is called.
268275 let types = [
269- self . u8_type ,
270- self . u16_type ,
271- self . u32_type ,
272- self . u64_type ,
273- self . i8_type ,
274- self . i16_type ,
275- self . i32_type ,
276- self . i64_type ,
276+ self . context . new_c_type ( CType :: UInt8t ) ,
277+ self . context . new_c_type ( CType :: UInt16t ) ,
278+ self . context . new_c_type ( CType :: UInt32t ) ,
279+ self . context . new_c_type ( CType :: UInt64t ) ,
280+ self . context . new_c_type ( CType :: Int8t ) ,
281+ self . context . new_c_type ( CType :: Int16t ) ,
282+ self . context . new_c_type ( CType :: Int32t ) ,
283+ self . context . new_c_type ( CType :: Int64t ) ,
277284 ] ;
278285
279286 for native_type in types {
0 commit comments