Simplify PrimVals.

This commit is contained in:
Scott Olson 2016-03-17 03:12:15 -06:00
parent abd235837a
commit 4704bdca8d
3 changed files with 15 additions and 31 deletions

View File

@ -13,7 +13,7 @@ use syntax::codemap::DUMMY_SP;
use error::EvalResult;
use memory::{self, FieldRepr, Memory, Pointer, Repr};
use primval::{self, PrimVal};
use primval;
const TRACE_EXECUTION: bool = true;
@ -242,11 +242,8 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> {
match &self.tcx.item_name(def_id).as_str()[..] {
"size_of" => {
let ty = *substs.types.get(subst::FnSpace, 0);
let size = PrimVal::from_usize(
self.ty_to_repr(ty).size(),
&dest_repr
);
try!(self.memory.write_primval(dest, size));
let size = self.ty_to_repr(ty).size() as u64;
try!(self.memory.write_uint(dest, size, dest_repr.size()));
}
"offset" => {

View File

@ -184,6 +184,10 @@ impl Memory {
ty::TyUint(UintTy::U16) => self.read_u16(ptr).map(PrimVal::U16),
ty::TyUint(UintTy::U32) => self.read_u32(ptr).map(PrimVal::U32),
ty::TyUint(UintTy::U64) => self.read_u64(ptr).map(PrimVal::U64),
// TODO(tsion): Pick the PrimVal dynamically.
ty::TyInt(IntTy::Is) => self.read_int(ptr, POINTER_SIZE).map(PrimVal::I64),
ty::TyUint(UintTy::Us) => self.read_uint(ptr, POINTER_SIZE).map(PrimVal::U64),
_ => panic!("primitive read of non-primitive type: {:?}", ty),
}
}
@ -255,6 +259,14 @@ impl Memory {
Ok(())
}
pub fn read_int(&self, ptr: Pointer, size: usize) -> EvalResult<i64> {
self.get_bytes(ptr, size).map(|mut b| b.read_int::<NativeEndian>(size).unwrap())
}
pub fn write_int(&mut self, ptr: Pointer, n: i64, size: usize) -> EvalResult<()> {
self.get_bytes_mut(ptr, size).map(|mut b| b.write_int::<NativeEndian>(n, size).unwrap())
}
pub fn read_u8(&self, ptr: Pointer) -> EvalResult<u8> {
self.get_bytes(ptr, 1).map(|b| b[0] as u8)
}

View File

@ -1,7 +1,5 @@
use rustc::mir::repr as mir;
use memory::Repr;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PrimVal {
Bool(bool),
@ -9,29 +7,6 @@ pub enum PrimVal {
U8(u8), U16(u16), U32(u32), U64(u64),
}
impl PrimVal {
pub fn from_usize(n: usize, repr: &Repr) -> Self {
// TODO(tsion): Use checked casts.
match *repr {
Repr::U8 => PrimVal::U8(n as u8),
Repr::U16 => PrimVal::U16(n as u16),
Repr::U32 => PrimVal::U32(n as u32),
Repr::U64 => PrimVal::U64(n as u64),
_ => panic!("attempted to make usize primval from non-uint repr"),
}
}
pub fn to_usize(self) -> usize {
match self {
PrimVal::U8(n) => n as usize,
PrimVal::U16(n) => n as usize,
PrimVal::U32(n) => n as usize,
PrimVal::U64(n) => n as usize,
_ => panic!("attempted to make usize from non-uint primval"),
}
}
}
pub fn binary_op(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> PrimVal {
macro_rules! int_binops {
($v:ident, $l:ident, $r:ident) => ({