@@ -40,7 +40,7 @@ pub fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
40
40
constants_cx. todo . insert ( TodoItem :: Static ( def_id) ) ;
41
41
}
42
42
43
- pub fn codegen_static_ref < ' tcx > (
43
+ fn codegen_static_ref < ' tcx > (
44
44
fx : & mut FunctionCx < ' _ , ' tcx , impl Backend > ,
45
45
def_id : DefId ,
46
46
ty : Ty < ' tcx > ,
@@ -50,31 +50,37 @@ pub fn codegen_static_ref<'tcx>(
50
50
cplace_for_dataid ( fx, ty, data_id)
51
51
}
52
52
53
- pub fn trans_promoted < ' tcx > (
54
- fx : & mut FunctionCx < ' _ , ' tcx , impl Backend > ,
55
- instance : Instance < ' tcx > ,
56
- promoted : Promoted ,
57
- dest_ty : Ty < ' tcx > ,
58
- ) -> CPlace < ' tcx > {
59
- match fx. tcx . const_eval_promoted ( instance, promoted) {
60
- Ok ( const_) => {
61
- let cplace = trans_const_place ( fx, const_) ;
62
- debug_assert_eq ! ( cplace. layout( ) , fx. layout_of( dest_ty) ) ;
63
- cplace
64
- }
65
- Err ( _) => crate :: trap:: trap_unreachable_ret_place (
66
- fx,
67
- fx. layout_of ( dest_ty) ,
68
- "[panic] Tried to get value of promoted value with errored during const eval." ,
69
- ) ,
70
- }
71
- }
72
-
73
53
pub fn trans_constant < ' tcx > (
74
54
fx : & mut FunctionCx < ' _ , ' tcx , impl Backend > ,
75
55
constant : & Constant < ' tcx > ,
76
56
) -> CValue < ' tcx > {
77
- let const_ = force_eval_const ( fx, & constant. literal ) ;
57
+ let const_ = match constant. literal . val {
58
+ ConstKind :: Unevaluated ( def_id, ref substs, promoted) if fx. tcx . is_static ( def_id) => {
59
+ assert ! ( substs. is_empty( ) ) ;
60
+ assert ! ( promoted. is_none( ) ) ;
61
+
62
+ return codegen_static_ref (
63
+ fx,
64
+ def_id,
65
+ fx. monomorphize ( & constant. literal . ty ) ,
66
+ ) . to_cvalue ( fx) ;
67
+ }
68
+ ConstKind :: Unevaluated ( def_id, ref substs, promoted) => {
69
+ let substs = fx. monomorphize ( substs) ;
70
+ fx. tcx . const_eval_resolve (
71
+ ParamEnv :: reveal_all ( ) ,
72
+ def_id,
73
+ substs,
74
+ promoted,
75
+ None , // FIXME use correct span
76
+ ) . unwrap_or_else ( |_| {
77
+ fx. tcx . sess . abort_if_errors ( ) ;
78
+ unreachable ! ( ) ;
79
+ } )
80
+ }
81
+ _ => fx. monomorphize ( & constant. literal ) ,
82
+ } ;
83
+
78
84
trans_const_value ( fx, const_)
79
85
}
80
86
@@ -83,9 +89,15 @@ pub fn force_eval_const<'tcx>(
83
89
const_ : & ' tcx Const ,
84
90
) -> & ' tcx Const < ' tcx > {
85
91
match const_. val {
86
- ConstKind :: Unevaluated ( def_id, ref substs) => {
92
+ ConstKind :: Unevaluated ( def_id, ref substs, promoted ) => {
87
93
let substs = fx. monomorphize ( substs) ;
88
- fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , def_id, substs, None ) . unwrap_or_else ( |_| {
94
+ fx. tcx . const_eval_resolve (
95
+ ParamEnv :: reveal_all ( ) ,
96
+ def_id,
97
+ substs,
98
+ promoted,
99
+ None , // FIXME pass correct span
100
+ ) . unwrap_or_else ( |_| {
89
101
fx. tcx . sess . abort_if_errors ( ) ;
90
102
unreachable ! ( ) ;
91
103
} )
@@ -100,38 +112,78 @@ pub fn trans_const_value<'tcx>(
100
112
) -> CValue < ' tcx > {
101
113
let ty = fx. monomorphize ( & const_. ty ) ;
102
114
let layout = fx. layout_of ( ty) ;
103
- match ty. kind {
104
- ty:: Bool | ty:: Uint ( _) => {
105
- let bits = const_. val . try_to_bits ( layout. size ) . unwrap ( ) ;
106
- CValue :: const_val ( fx, ty, bits)
115
+
116
+ if layout. is_zst ( ) {
117
+ return CValue :: by_ref (
118
+ crate :: Pointer :: const_addr ( fx, i64:: try_from ( layout. align . pref . bytes ( ) ) . unwrap ( ) ) ,
119
+ layout,
120
+ ) ;
121
+ }
122
+
123
+ let const_val = match const_. val {
124
+ ConstKind :: Value ( const_val) => const_val,
125
+ _ => unreachable ! ( "Const {:?} should have been evaluated" , const_) ,
126
+ } ;
127
+
128
+ match const_val {
129
+ 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
+ } ;
134
+
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, ty, bits)
141
+ }
142
+ ty:: Int ( _) => {
143
+ let bits = const_. val . try_to_bits ( layout. size ) . unwrap ( ) ;
144
+ CValue :: const_val (
145
+ fx,
146
+ ty,
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 ( ) ) ) ,
161
+ } ;
162
+ CValue :: by_val ( val, layout)
163
+ }
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
+ }
107
170
}
108
- ty:: Int ( _) => {
109
- let bits = const_. val . try_to_bits ( layout. size ) . unwrap ( ) ;
110
- CValue :: const_val (
111
- fx,
112
- ty,
113
- rustc:: mir:: interpret:: sign_extend ( bits, layout. size ) ,
171
+ ConstValue :: ByRef { alloc, offset } => {
172
+ let alloc_id = fx. tcx . alloc_map . lock ( ) . create_memory_alloc ( alloc) ;
173
+ fx. constants_cx . todo . insert ( TodoItem :: Alloc ( alloc_id) ) ;
174
+ let data_id = data_id_for_alloc_id ( fx. module , alloc_id, alloc. align ) ;
175
+ let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
176
+ let global_ptr = fx. bcx . ins ( ) . global_value ( fx. pointer_type , local_data_id) ;
177
+ assert ! ( !layout. is_unsized( ) , "unsized ConstValue::ByRef not supported" ) ;
178
+ CValue :: by_ref (
179
+ crate :: pointer:: Pointer :: new ( global_ptr)
180
+ . offset_i64 ( fx, i64:: try_from ( offset. bytes ( ) ) . unwrap ( ) ) ,
181
+ layout,
114
182
)
115
183
}
116
- ty:: Float ( fty) => {
117
- let bits = const_. val . try_to_bits ( layout. size ) . unwrap ( ) ;
118
- let val = match fty {
119
- FloatTy :: F32 => fx
120
- . bcx
121
- . ins ( )
122
- . f32const ( Ieee32 :: with_bits ( u32:: try_from ( bits) . unwrap ( ) ) ) ,
123
- FloatTy :: F64 => fx
124
- . bcx
125
- . ins ( )
126
- . f64const ( Ieee64 :: with_bits ( u64:: try_from ( bits) . unwrap ( ) ) ) ,
127
- } ;
128
- CValue :: by_val ( val, layout)
184
+ ConstValue :: Slice { data : _, start : _, end : _ } => {
185
+ trans_const_place ( fx, const_) . to_cvalue ( fx)
129
186
}
130
- ty:: FnDef ( _def_id, _substs) => CValue :: by_ref (
131
- crate :: pointer:: Pointer :: const_addr ( fx, fx. pointer_type . bytes ( ) as i64 ) ,
132
- layout,
133
- ) ,
134
- _ => trans_const_place ( fx, const_) . to_cvalue ( fx) ,
135
187
}
136
188
}
137
189
@@ -480,22 +532,8 @@ pub fn mir_operand_get_const_val<'tcx>(
480
532
fx : & FunctionCx < ' _ , ' tcx , impl Backend > ,
481
533
operand : & Operand < ' tcx > ,
482
534
) -> Option < & ' tcx Const < ' tcx > > {
483
- let place = match operand {
484
- Operand :: Copy ( place ) | Operand :: Move ( place ) => place ,
535
+ match operand {
536
+ Operand :: Copy ( _ ) | Operand :: Move ( _ ) => return None ,
485
537
Operand :: Constant ( const_) => return Some ( force_eval_const ( fx, const_. literal ) ) ,
486
- } ;
487
-
488
- assert ! ( place. projection. is_empty( ) ) ;
489
- let static_ = match & place. base {
490
- PlaceBase :: Static ( static_) => static_,
491
- PlaceBase :: Local ( _) => return None ,
492
- } ;
493
-
494
- Some ( match & static_. kind {
495
- StaticKind :: Static => unimplemented ! ( ) ,
496
- StaticKind :: Promoted ( promoted, substs) => {
497
- let instance = Instance :: new ( static_. def_id , fx. monomorphize ( substs) ) ;
498
- fx. tcx . const_eval_promoted ( instance, * promoted) . unwrap ( )
499
- }
500
- } )
538
+ }
501
539
}
0 commit comments