9
9
// except according to those terms.
10
10
11
11
use back:: abi;
12
+ use llvm:: ValueRef ;
13
+ use middle:: subst:: Substs ;
12
14
use middle:: ty:: { Ty , HasTypeFlags } ;
13
15
use rustc:: middle:: const_eval:: ConstVal ;
14
16
use rustc:: mir:: repr as mir;
15
- use trans:: consts;
16
- use trans:: common:: { self , Block } ;
17
+ use trans:: common:: { self , Block , C_bool , C_bytes , C_floating_f64 , C_integral , C_str_slice } ;
18
+ use trans:: consts:: { self , TrueConst } ;
19
+ use trans:: { type_of, expr} ;
17
20
18
21
19
22
use super :: operand:: { OperandRef , OperandValue } ;
@@ -27,7 +30,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
27
30
-> OperandRef < ' tcx >
28
31
{
29
32
let ccx = bcx. ccx ( ) ;
30
- let val = consts :: trans_constval ( bcx, cv, ty, bcx. fcx . param_substs ) ;
33
+ let val = self . trans_constval_inner ( bcx, cv, ty, bcx. fcx . param_substs ) ;
31
34
let val = if common:: type_is_immediate ( ccx, ty) {
32
35
OperandValue :: Immediate ( val)
33
36
} else if common:: type_is_fat_ptr ( bcx. tcx ( ) , ty) {
@@ -46,6 +49,39 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
46
49
}
47
50
}
48
51
52
+ /// Translate ConstVal into a bare LLVM ValueRef.
53
+ fn trans_constval_inner ( & mut self ,
54
+ bcx : common:: Block < ' bcx , ' tcx > ,
55
+ cv : & ConstVal ,
56
+ ty : Ty < ' tcx > ,
57
+ param_substs : & ' tcx Substs < ' tcx > )
58
+ -> ValueRef
59
+ {
60
+ let ccx = bcx. ccx ( ) ;
61
+ let llty = type_of:: type_of ( ccx, ty) ;
62
+ match * cv {
63
+ ConstVal :: Float ( v) => C_floating_f64 ( v, llty) ,
64
+ ConstVal :: Bool ( v) => C_bool ( ccx, v) ,
65
+ ConstVal :: Int ( v) => C_integral ( llty, v as u64 , true ) ,
66
+ ConstVal :: Uint ( v) => C_integral ( llty, v, false ) ,
67
+ ConstVal :: Str ( ref v) => C_str_slice ( ccx, v. clone ( ) ) ,
68
+ ConstVal :: ByteStr ( ref v) => consts:: addr_of ( ccx, C_bytes ( ccx, v) , 1 , "byte_str" ) ,
69
+ ConstVal :: Struct ( id) | ConstVal :: Tuple ( id) => {
70
+ let expr = bcx. tcx ( ) . map . expect_expr ( id) ;
71
+ match consts:: const_expr ( ccx, expr, param_substs, None , TrueConst :: Yes ) {
72
+ Ok ( ( val, _) ) => val,
73
+ Err ( e) => panic ! ( "const eval failure: {}" , e. description( ) ) ,
74
+ }
75
+ } ,
76
+ ConstVal :: Array ( id, _) | ConstVal :: Repeat ( id, _) => {
77
+ let expr = bcx. tcx ( ) . map . expect_expr ( id) ;
78
+ expr:: trans ( bcx, expr) . datum . val
79
+ } ,
80
+ ConstVal :: Function ( did) =>
81
+ self . trans_fn_ref ( bcx, ty, param_substs, did) . immediate ( )
82
+ }
83
+ }
84
+
49
85
pub fn trans_constant ( & mut self ,
50
86
bcx : Block < ' bcx , ' tcx > ,
51
87
constant : & mir:: Constant < ' tcx > )
0 commit comments