diff --git a/src/interpreter.rs b/src/interpreter.rs index e9c581a5681..8741beb8735 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -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" => { diff --git a/src/memory.rs b/src/memory.rs index 73bdc4ea49a..17e9439de6d 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -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 { + self.get_bytes(ptr, size).map(|mut b| b.read_int::(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::(n, size).unwrap()) + } + pub fn read_u8(&self, ptr: Pointer) -> EvalResult { self.get_bytes(ptr, 1).map(|b| b[0] as u8) } diff --git a/src/primval.rs b/src/primval.rs index 88bd88b1746..a34da06d597 100644 --- a/src/primval.rs +++ b/src/primval.rs @@ -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) => ({