From 4694fa4f3dc0044451764b6afa170a68f0125a57 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 14 Jul 2018 16:45:20 +0200 Subject: [PATCH] Move const val handling to constant.rs --- src/base.rs | 34 +--------------------------------- src/constant.rs | 37 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 39 insertions(+), 33 deletions(-) create mode 100644 src/constant.rs diff --git a/src/base.rs b/src/base.rs index c523c397921..c7f07542b0e 100644 --- a/src/base.rs +++ b/src/base.rs @@ -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_) } } } diff --git a/src/constant.rs b/src/constant.rs new file mode 100644 index 00000000000..ab3e4376a82 --- /dev/null +++ b/src/constant.rs @@ -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), + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 40ffaa5e0a0..e8cab41d810 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,6 +34,7 @@ use std::fs::File; use std::io::Write; mod base; +mod constant; mod common; mod pretty_clif;