Move const val handling to constant.rs

This commit is contained in:
bjorn3 2018-07-14 16:45:20 +02:00
parent a95a6729b1
commit 4694fa4f3d
3 changed files with 39 additions and 33 deletions

View File

@ -497,8 +497,6 @@ fn trans_place<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, place: &Place<'tcx>)
}
fn trans_operand<'a, 'tcx>(fx: &mut FunctionCx<'a, 'tcx>, operand: &Operand<'tcx>) -> CValue<'tcx> {
use rustc::mir::interpret::{Scalar, ConstValue, GlobalId};
match operand {
Operand::Move(place) |
Operand::Copy(place) => {
@ -506,37 +504,7 @@ fn trans_operand<'a, 'tcx>(fx: &mut FunctionCx<'a, 'tcx>, operand: &Operand<'tcx
cplace.to_cvalue(fx)
},
Operand::Constant(const_) => {
let value = match const_.literal {
Literal::Value { value } => value,
Literal::Promoted { index } => fx
.tcx
.const_eval(ParamEnv::reveal_all().and(GlobalId {
instance: fx.instance,
promoted: Some(index),
}))
.unwrap(),
};
let layout = fx.layout_of(const_.ty);
match const_.ty.sty {
TypeVariants::TyBool => {
let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
CValue::const_val(fx, const_.ty, bits as u64 as i64)
}
TypeVariants::TyUint(_) => {
let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
CValue::const_val(fx, const_.ty, bits as u64 as i64)
}
TypeVariants::TyInt(_) => {
let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
CValue::const_val(fx, const_.ty, bits as i128 as i64)
}
TypeVariants::TyFnDef(def_id, substs) => {
let func_ref = fx.get_function_ref(Instance::new(def_id, substs));
CValue::Func(func_ref, fx.layout_of(const_.ty))
}
_ => unimplemented!("value {:?} ty {:?}", value, const_.ty),
}
::constant::trans_constant(fx, const_)
}
}
}

37
src/constant.rs Normal file
View File

@ -0,0 +1,37 @@
use prelude::*;
pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &Constant<'tcx>) -> CValue<'tcx> {
use rustc::mir::interpret::{Scalar, ConstValue, GlobalId};
let value = match const_.literal {
Literal::Value { value } => value,
Literal::Promoted { index } => fx
.tcx
.const_eval(ParamEnv::reveal_all().and(GlobalId {
instance: fx.instance,
promoted: Some(index),
}))
.unwrap(),
};
let layout = fx.layout_of(const_.ty);
match const_.ty.sty {
TypeVariants::TyBool => {
let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
CValue::const_val(fx, const_.ty, bits as u64 as i64)
}
TypeVariants::TyUint(_) => {
let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
CValue::const_val(fx, const_.ty, bits as u64 as i64)
}
TypeVariants::TyInt(_) => {
let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
CValue::const_val(fx, const_.ty, bits as i128 as i64)
}
TypeVariants::TyFnDef(def_id, substs) => {
let func_ref = fx.get_function_ref(Instance::new(def_id, substs));
CValue::Func(func_ref, fx.layout_of(const_.ty))
}
_ => unimplemented!("value {:?} ty {:?}", value, const_.ty),
}
}

View File

@ -34,6 +34,7 @@ use std::fs::File;
use std::io::Write;
mod base;
mod constant;
mod common;
mod pretty_clif;