1
Fork 0

Rustup part 2/2

This commit is contained in:
Oliver Schneider 2017-09-13 13:46:54 +02:00
parent 79993e63a0
commit c47ee6b521
No known key found for this signature in database
GPG key ID: A69F8D225B3AD7D9
4 changed files with 48 additions and 37 deletions

View file

@ -38,36 +38,45 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
self.cast_from_int(val as u128, ty, val < 0)
}
fn int_to_int(&self, v: i128, ty: IntTy) -> u128 {
match ty {
IntTy::I8 => v as i8 as u128,
IntTy::I16 => v as i16 as u128,
IntTy::I32 => v as i32 as u128,
IntTy::I64 => v as i64 as u128,
IntTy::I128 => v as u128,
IntTy::Is => {
let ty = self.tcx.sess.target.isize_ty;
self.int_to_int(v, ty)
}
}
}
fn int_to_uint(&self, v: u128, ty: UintTy) -> u128 {
match ty {
UintTy::U8 => v as u8 as u128,
UintTy::U16 => v as u16 as u128,
UintTy::U32 => v as u32 as u128,
UintTy::U64 => v as u64 as u128,
UintTy::U128 => v,
UintTy::Us => {
let ty = self.tcx.sess.target.usize_ty;
self.int_to_uint(v, ty)
}
}
}
fn cast_from_int(
&self,
v: u128,
ty: ty::Ty<'tcx>,
negative: bool,
) -> EvalResult<'tcx, PrimVal> {
trace!("cast_from_int: {}, {}, {}", v, ty, negative);
use rustc::ty::TypeVariants::*;
match ty.sty {
// Casts to bool are not permitted by rustc, no need to handle them here.
TyInt(IntTy::I8) => Ok(PrimVal::Bytes(v as i128 as i8 as u128)),
TyInt(IntTy::I16) => Ok(PrimVal::Bytes(v as i128 as i16 as u128)),
TyInt(IntTy::I32) => Ok(PrimVal::Bytes(v as i128 as i32 as u128)),
TyInt(IntTy::I64) => Ok(PrimVal::Bytes(v as i128 as i64 as u128)),
TyInt(IntTy::I128) => Ok(PrimVal::Bytes(v as u128)),
TyUint(UintTy::U8) => Ok(PrimVal::Bytes(v as u8 as u128)),
TyUint(UintTy::U16) => Ok(PrimVal::Bytes(v as u16 as u128)),
TyUint(UintTy::U32) => Ok(PrimVal::Bytes(v as u32 as u128)),
TyUint(UintTy::U64) => Ok(PrimVal::Bytes(v as u64 as u128)),
TyUint(UintTy::U128) => Ok(PrimVal::Bytes(v)),
TyInt(IntTy::Is) => {
let ty = self.tcx.types.isize;
self.cast_from_int(v, ty, negative)
}
TyUint(UintTy::Us) => {
let ty = self.tcx.types.usize;
self.cast_from_int(v, ty, negative)
}
TyInt(ty) => Ok(PrimVal::Bytes(self.int_to_int(v as i128, ty))),
TyUint(ty) => Ok(PrimVal::Bytes(self.int_to_uint(v, ty))),
TyFloat(FloatTy::F64) if negative => Ok(PrimVal::from_f64(v as i128 as f64)),
TyFloat(FloatTy::F64) => Ok(PrimVal::from_f64(v as f64)),

View file

@ -240,10 +240,20 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
Str(ref s) => return self.str_to_value(s),
ByteStr(ref bs) => {
let ptr = self.memory.allocate_cached(bs)?;
let ptr = self.memory.allocate_cached(bs.data)?;
PrimVal::Ptr(ptr)
}
Unevaluated(def_id, substs) => {
let instance = self.resolve_associated_const(def_id, substs);
let cid = GlobalId {
instance,
promoted: None,
};
return Ok(Value::ByRef(*self.globals.get(&cid).expect("static/const not cached")));
}
Aggregate(..) |
Variant(_) => unimplemented!(),
// function items are zero sized and thus have no readable value
Function(..) => PrimVal::Undef,
@ -1284,16 +1294,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
use rustc::mir::Literal;
let mir::Constant { ref literal, .. } = **constant;
let value = match *literal {
Literal::Value { ref value } => self.const_to_value(value)?,
Literal::Item { def_id, substs } => {
let instance = self.resolve_associated_const(def_id, substs);
let cid = GlobalId {
instance,
promoted: None,
};
Value::ByRef(*self.globals.get(&cid).expect("static/const not cached"))
}
Literal::Value { ref value } => self.const_to_value(&value.val)?,
Literal::Promoted { index } => {
let cid = GlobalId {
@ -2501,7 +2502,7 @@ struct AssociatedTypeNormalizer<'a, 'tcx: 'a> {
impl<'a, 'tcx> AssociatedTypeNormalizer<'a, 'tcx> {
fn fold<T: TypeFoldable<'tcx>>(&mut self, value: &T) -> T {
if !value.has_projection_types() {
if !value.has_projections() {
value.clone()
} else {
value.fold_with(self)
@ -2515,7 +2516,7 @@ impl<'a, 'tcx> ::rustc::ty::fold::TypeFolder<'tcx, 'tcx> for AssociatedTypeNorma
}
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
if !ty.has_projection_types() {
if !ty.has_projections() {
ty
} else {
self.tcx.normalize_associated_type(&ty)

View file

@ -10,6 +10,7 @@ use rustc::traits::Reveal;
use rustc::ty;
use rustc::ty::layout::Layout;
use rustc::ty::subst::Substs;
use rustc::middle::const_val::ConstVal;
use super::{EvalResult, EvalContext, StackPopCleanup, PtrAndAlign, GlobalId, Lvalue,
MemoryKind, Machine, PrimVal};
@ -300,8 +301,7 @@ impl<'a, 'b, 'tcx, M: Machine<'tcx>> Visitor<'tcx> for ConstantExtractor<'a, 'b,
self.super_constant(constant, location);
match constant.literal {
// already computed by rustc
mir::Literal::Value { .. } => {}
mir::Literal::Item { def_id, substs } => {
mir::Literal::Value { value: &ty::Const { val: ConstVal::Unevaluated(def_id, substs), .. } } => {
self.try(|this| {
this.ecx.global_item(
def_id,
@ -311,6 +311,7 @@ impl<'a, 'b, 'tcx, M: Machine<'tcx>> Visitor<'tcx> for ConstantExtractor<'a, 'b,
)
});
}
mir::Literal::Value { .. } => {}
mir::Literal::Promoted { index } => {
let cid = GlobalId {
instance: self.instance,

View file

@ -246,7 +246,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
{
let param_env = ty::ParamEnv::empty(Reveal::All);
if !value.has_projection_types() {
if !value.has_projections() {
return value.clone();
}