Eval const values
This commit is contained in:
parent
4fad0f714f
commit
e40d025843
2 changed files with 28 additions and 8 deletions
|
@ -170,3 +170,9 @@ fn use_size_of() -> usize {
|
||||||
let copy2 = ©::<u8>;
|
let copy2 = ©::<u8>;
|
||||||
copy2(src, dst, 1);
|
copy2(src, dst, 1);
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
const Abc: u8 = 6 * 7;
|
||||||
|
|
||||||
|
fn use_const() -> u8 {
|
||||||
|
Abc
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
use rustc::mir::interpret::{GlobalId, AllocId, read_target_uint};
|
use rustc::mir::interpret::{ConstValue, GlobalId, AllocId, read_target_uint};
|
||||||
use rustc_mir::interpret::{CompileTimeEvaluator, Memory, MemoryKind};
|
use rustc_mir::interpret::{CompileTimeEvaluator, Memory, MemoryKind};
|
||||||
use cranelift_module::*;
|
use cranelift_module::*;
|
||||||
|
|
||||||
pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &Constant<'tcx>) -> CValue<'tcx> {
|
pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &Constant<'tcx>) -> CValue<'tcx> {
|
||||||
let value = match const_.literal {
|
let const_val = match const_.literal {
|
||||||
Literal::Value { value } => value,
|
Literal::Value { value } => fx.monomorphize(&value),
|
||||||
Literal::Promoted { index } => fx
|
Literal::Promoted { index } => fx
|
||||||
.tcx
|
.tcx
|
||||||
.const_eval(ParamEnv::reveal_all().and(GlobalId {
|
.const_eval(ParamEnv::reveal_all().and(GlobalId {
|
||||||
|
@ -14,21 +14,35 @@ pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &Cons
|
||||||
}))
|
}))
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
};
|
};
|
||||||
fx.tcx.sess.warn(&format!("const: {:?}", value));
|
|
||||||
|
let const_ = match const_val.val {
|
||||||
|
ConstValue::Unevaluated(def_id, ref substs) => {
|
||||||
|
let param_env = ParamEnv::reveal_all();
|
||||||
|
let instance = Instance::resolve(fx.tcx, param_env, def_id, substs).unwrap();
|
||||||
|
let cid = GlobalId {
|
||||||
|
instance,
|
||||||
|
promoted: None,
|
||||||
|
};
|
||||||
|
fx.tcx.const_eval(param_env.and(cid)).unwrap()
|
||||||
|
},
|
||||||
|
_ => const_val,
|
||||||
|
};
|
||||||
|
|
||||||
|
fx.tcx.sess.warn(&format!("const_val: {:?} const_: {:?}", const_val, const_));
|
||||||
|
|
||||||
let ty = fx.monomorphize(&const_.ty);
|
let ty = fx.monomorphize(&const_.ty);
|
||||||
let layout = fx.layout_of(ty);
|
let layout = fx.layout_of(ty);
|
||||||
match ty.sty {
|
match ty.sty {
|
||||||
TypeVariants::TyBool => {
|
TypeVariants::TyBool => {
|
||||||
let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
|
let bits = const_.val.to_bits(layout.size).unwrap();
|
||||||
CValue::const_val(fx, ty, bits as u64 as i64)
|
CValue::const_val(fx, ty, bits as u64 as i64)
|
||||||
}
|
}
|
||||||
TypeVariants::TyUint(_) => {
|
TypeVariants::TyUint(_) => {
|
||||||
let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
|
let bits = const_.val.to_bits(layout.size).unwrap();
|
||||||
CValue::const_val(fx, ty, bits as u64 as i64)
|
CValue::const_val(fx, ty, bits as u64 as i64)
|
||||||
}
|
}
|
||||||
TypeVariants::TyInt(_) => {
|
TypeVariants::TyInt(_) => {
|
||||||
let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
|
let bits = const_.val.to_bits(layout.size).unwrap();
|
||||||
CValue::const_val(fx, ty, bits as i128 as i64)
|
CValue::const_val(fx, ty, bits as i128 as i64)
|
||||||
}
|
}
|
||||||
TypeVariants::TyFnDef(def_id, substs) => {
|
TypeVariants::TyFnDef(def_id, substs) => {
|
||||||
|
@ -42,7 +56,7 @@ pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &Cons
|
||||||
return CValue::ByRef(fx.bcx.ins().iconst(types::I64, 0), layout);
|
return CValue::ByRef(fx.bcx.ins().iconst(types::I64, 0), layout);
|
||||||
}
|
}
|
||||||
let mut memory = Memory::<CompileTimeEvaluator>::new(fx.tcx.at(DUMMY_SP), ());
|
let mut memory = Memory::<CompileTimeEvaluator>::new(fx.tcx.at(DUMMY_SP), ());
|
||||||
let alloc = fx.tcx.const_value_to_allocation(value);
|
let alloc = fx.tcx.const_value_to_allocation(const_);
|
||||||
//println!("const value: {:?} allocation: {:?}", value, alloc);
|
//println!("const value: {:?} allocation: {:?}", value, alloc);
|
||||||
let alloc_id = memory.allocate_value(alloc.clone(), MemoryKind::Stack).unwrap();
|
let alloc_id = memory.allocate_value(alloc.clone(), MemoryKind::Stack).unwrap();
|
||||||
let data_id = get_global_for_alloc_id(fx, &memory, alloc_id);
|
let data_id = get_global_for_alloc_id(fx, &memory, alloc_id);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue