Skip to content

Commit e137c2a

Browse files
committed
[MIR] Translate ConstVal::Function
This moves back (essentially reverts rust-lang#30265) into MIR-specific translation code, but keeps the funcition split out, since it is expected to eventually become recursive.
1 parent 27a1834 commit e137c2a

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

src/librustc_trans/trans/consts.rs

-33
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use trans::{adt, closure, debuginfo, expr, inline, machine};
3131
use trans::base::{self, push_ctxt};
3232
use trans::common::{self, type_is_sized, ExprOrMethodCall, node_id_substs, C_nil, const_get_elt};
3333
use trans::common::{CrateContext, C_integral, C_floating, C_bool, C_str_slice, C_bytes, val_ty};
34-
use trans::common::C_floating_f64;
3534
use trans::common::{C_struct, C_undef, const_to_opt_int, const_to_opt_uint, VariantInfo, C_uint};
3635
use trans::common::{type_is_fat_ptr, Field, C_vector, C_array, C_null, ExprId, MethodCallKey};
3736
use trans::declare;
@@ -108,38 +107,6 @@ pub fn const_lit(cx: &CrateContext, e: &hir::Expr, lit: &ast::Lit)
108107
}
109108
}
110109

111-
pub fn trans_constval<'blk, 'tcx>(bcx: common::Block<'blk, 'tcx>,
112-
cv: &ConstVal,
113-
ty: Ty<'tcx>,
114-
param_substs: &'tcx Substs<'tcx>)
115-
-> ValueRef
116-
{
117-
let ccx = bcx.ccx();
118-
let llty = type_of::type_of(ccx, ty);
119-
match *cv {
120-
ConstVal::Float(v) => C_floating_f64(v, llty),
121-
ConstVal::Bool(v) => C_bool(ccx, v),
122-
ConstVal::Int(v) => C_integral(llty, v as u64, true),
123-
ConstVal::Uint(v) => C_integral(llty, v, false),
124-
ConstVal::Str(ref v) => C_str_slice(ccx, v.clone()),
125-
ConstVal::ByteStr(ref v) => addr_of(ccx, C_bytes(ccx, v), 1, "byte_str"),
126-
ConstVal::Struct(id) | ConstVal::Tuple(id) => {
127-
let expr = bcx.tcx().map.expect_expr(id);
128-
match const_expr(ccx, expr, param_substs, None, TrueConst::Yes) {
129-
Ok((val, _)) => val,
130-
Err(e) => panic!("const eval failure: {}", e.description()),
131-
}
132-
},
133-
ConstVal::Array(id, _) | ConstVal::Repeat(id, _) => {
134-
let expr = bcx.tcx().map.expect_expr(id);
135-
expr::trans(bcx, expr).datum.val
136-
},
137-
ConstVal::Function(_) => {
138-
unimplemented!()
139-
},
140-
}
141-
}
142-
143110
pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
144111
unsafe {
145112
llvm::LLVMConstPointerCast(val, ty.to_ref())

src/librustc_trans/trans/mir/constant.rs

+39-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
// except according to those terms.
1010

1111
use back::abi;
12+
use llvm::ValueRef;
13+
use middle::subst::Substs;
1214
use middle::ty::{Ty, HasTypeFlags};
1315
use rustc::middle::const_eval::ConstVal;
1416
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};
1720

1821

1922
use super::operand::{OperandRef, OperandValue};
@@ -27,7 +30,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
2730
-> OperandRef<'tcx>
2831
{
2932
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);
3134
let val = if common::type_is_immediate(ccx, ty) {
3235
OperandValue::Immediate(val)
3336
} else if common::type_is_fat_ptr(bcx.tcx(), ty) {
@@ -46,6 +49,39 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
4649
}
4750
}
4851

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+
4985
pub fn trans_constant(&mut self,
5086
bcx: Block<'bcx, 'tcx>,
5187
constant: &mir::Constant<'tcx>)

src/test/run-pass/mir_refs_correct.rs

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ enum CEnum {
6868
const C: u8 = 84;
6969
const C2: [u8; 5] = [42; 5];
7070
const C3: [u8; 3] = [42, 41, 40];
71+
const C4: fn(u8) -> S = S;
7172

7273
fn regular() -> u8 {
7374
21
@@ -198,6 +199,11 @@ fn t23() -> (CEnum, CEnum) {
198199
(CEnum::A, CEnum::B)
199200
}
200201

202+
#[rustc_mir]
203+
fn t24() -> fn(u8) -> S {
204+
C4
205+
}
206+
201207
fn main(){
202208
unsafe {
203209
assert_eq!(t1()(), regular());
@@ -240,5 +246,6 @@ fn main(){
240246
assert_eq!(t21(), Unit);
241247
assert_eq!(t22(), None);
242248
assert_eq!(t23(), (CEnum::A, CEnum::B));
249+
assert_eq!(t24(), C4);
243250
}
244251
}

0 commit comments

Comments
 (0)