@@ -11,6 +11,7 @@ use rustc_mir::interpret::{
11
11
StackPopCleanup , StackPopInfo ,
12
12
} ;
13
13
14
+ use cranelift_codegen:: ir:: GlobalValue ;
14
15
use cranelift_module:: * ;
15
16
16
17
use crate :: prelude:: * ;
@@ -47,7 +48,10 @@ fn codegen_static_ref<'tcx>(
47
48
) -> CPlace < ' tcx > {
48
49
let linkage = crate :: linkage:: get_static_ref_linkage ( fx. tcx , def_id) ;
49
50
let data_id = data_id_for_static ( fx. tcx , fx. module , def_id, linkage) ;
50
- cplace_for_dataid ( fx, layout, data_id)
51
+ let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
52
+ #[ cfg( debug_assertions) ]
53
+ fx. add_entity_comment ( local_data_id, format ! ( "{:?}" , def_id) ) ;
54
+ cplace_for_dataid ( fx, layout, local_data_id)
51
55
}
52
56
53
57
pub fn trans_constant < ' tcx > (
@@ -119,53 +123,52 @@ pub fn trans_const_value<'tcx>(
119
123
layout,
120
124
) ;
121
125
}
122
-
123
126
let const_val = match const_. val {
124
127
ConstKind :: Value ( const_val) => const_val,
125
128
_ => unreachable ! ( "Const {:?} should have been evaluated" , const_) ,
126
129
} ;
127
130
128
131
match const_val {
129
132
ConstValue :: Scalar ( x) => {
130
- let scalar = match layout. abi {
131
- layout:: Abi :: Scalar ( ref x) => x,
132
- _ => bug ! ( "from_const: invalid ByVal layout: {:#?}" , layout) ,
133
- } ;
133
+ if fx. clif_type ( layout. ty ) . is_none ( ) {
134
+ return trans_const_place ( fx, const_) . to_cvalue ( fx) ;
135
+ }
134
136
135
- match ty. kind {
136
- ty:: Bool | ty:: Uint ( _) => {
137
- let bits = const_. val . try_to_bits ( layout. size ) . unwrap_or_else ( || {
138
- panic ! ( "{:?}\n {:?}" , const_, layout) ;
139
- } ) ;
140
- CValue :: const_val ( fx, layout, bits)
137
+ match x {
138
+ Scalar :: Raw { data, size } => {
139
+ assert_eq ! ( u64 :: from( size) , layout. size. bytes( ) ) ;
140
+ return CValue :: const_val ( fx, layout, data) ;
141
141
}
142
- ty:: Int ( _) => {
143
- let bits = const_. val . try_to_bits ( layout. size ) . unwrap ( ) ;
144
- CValue :: const_val (
145
- fx,
146
- layout,
147
- rustc:: mir:: interpret:: sign_extend ( bits, layout. size ) ,
148
- )
149
- }
150
- ty:: Float ( fty) => {
151
- let bits = const_. val . try_to_bits ( layout. size ) . unwrap ( ) ;
152
- let val = match fty {
153
- FloatTy :: F32 => fx
154
- . bcx
155
- . ins ( )
156
- . f32const ( Ieee32 :: with_bits ( u32:: try_from ( bits) . unwrap ( ) ) ) ,
157
- FloatTy :: F64 => fx
158
- . bcx
159
- . ins ( )
160
- . f64const ( Ieee64 :: with_bits ( u64:: try_from ( bits) . unwrap ( ) ) ) ,
142
+ Scalar :: Ptr ( ptr) => {
143
+ let alloc_kind = fx. tcx . alloc_map . lock ( ) . get ( ptr. alloc_id ) ;
144
+ let base_addr = match alloc_kind {
145
+ Some ( GlobalAlloc :: Memory ( alloc) ) => {
146
+ fx. constants_cx . todo . insert ( TodoItem :: Alloc ( ptr. alloc_id ) ) ;
147
+ let data_id = data_id_for_alloc_id ( fx. module , ptr. alloc_id , alloc. align ) ;
148
+ let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
149
+ #[ cfg( debug_assertions) ]
150
+ fx. add_entity_comment ( local_data_id, format ! ( "{:?}" , ptr. alloc_id) ) ;
151
+ fx. bcx . ins ( ) . global_value ( fx. pointer_type , local_data_id)
152
+ }
153
+ Some ( GlobalAlloc :: Function ( instance) ) => {
154
+ let func_id = crate :: abi:: import_function ( fx. tcx , fx. module , instance) ;
155
+ let local_func_id = fx. module . declare_func_in_func ( func_id, & mut fx. bcx . func ) ;
156
+ fx. bcx . ins ( ) . func_addr ( fx. pointer_type , local_func_id)
157
+ }
158
+ Some ( GlobalAlloc :: Static ( def_id) ) => {
159
+ assert ! ( fx. tcx. is_static( def_id) ) ;
160
+ let linkage = crate :: linkage:: get_static_ref_linkage ( fx. tcx , def_id) ;
161
+ let data_id = data_id_for_static ( fx. tcx , fx. module , def_id, linkage) ;
162
+ let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
163
+ #[ cfg( debug_assertions) ]
164
+ fx. add_entity_comment ( local_data_id, format ! ( "{:?}" , def_id) ) ;
165
+ fx. bcx . ins ( ) . global_value ( fx. pointer_type , local_data_id)
166
+ }
167
+ None => bug ! ( "missing allocation {:?}" , ptr. alloc_id) ,
161
168
} ;
162
- CValue :: by_val ( val, layout)
169
+ let val = fx. bcx . ins ( ) . iadd_imm ( base_addr, i64:: try_from ( ptr. offset . bytes ( ) ) . unwrap ( ) ) ;
170
+ return CValue :: by_val ( val, layout) ;
163
171
}
164
- ty:: FnDef ( _def_id, _substs) => CValue :: by_ref (
165
- crate :: pointer:: Pointer :: const_addr ( fx, fx. pointer_type . bytes ( ) as i64 ) ,
166
- layout,
167
- ) ,
168
- _ => trans_const_place ( fx, const_) . to_cvalue ( fx) ,
169
172
}
170
173
}
171
174
ConstValue :: ByRef { alloc, offset } => {
@@ -228,7 +231,10 @@ fn trans_const_place<'tcx>(
228
231
let alloc_id = fx. tcx . alloc_map . lock ( ) . create_memory_alloc ( alloc) ;
229
232
fx. constants_cx . todo . insert ( TodoItem :: Alloc ( alloc_id) ) ;
230
233
let data_id = data_id_for_alloc_id ( fx. module , alloc_id, alloc. align ) ;
231
- cplace_for_dataid ( fx, fx. layout_of ( const_. ty ) , data_id)
234
+ let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
235
+ #[ cfg( debug_assertions) ]
236
+ fx. add_entity_comment ( local_data_id, format ! ( "{:?}" , alloc_id) ) ;
237
+ cplace_for_dataid ( fx, fx. layout_of ( const_. ty ) , local_data_id)
232
238
}
233
239
234
240
fn data_id_for_alloc_id < B : Backend > (
@@ -305,9 +311,8 @@ fn data_id_for_static(
305
311
fn cplace_for_dataid < ' tcx > (
306
312
fx : & mut FunctionCx < ' _ , ' tcx , impl Backend > ,
307
313
layout : TyLayout < ' tcx > ,
308
- data_id : DataId ,
314
+ local_data_id : GlobalValue ,
309
315
) -> CPlace < ' tcx > {
310
- let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
311
316
let global_ptr = fx. bcx . ins ( ) . global_value ( fx. pointer_type , local_data_id) ;
312
317
assert ! ( !layout. is_unsized( ) , "unsized statics aren't supported" ) ;
313
318
CPlace :: for_ptr ( crate :: pointer:: Pointer :: new ( global_ptr) , layout)
0 commit comments