diff --git a/src/interpreter.rs b/src/interpreter.rs index 6f559e97688e..c33c7874fbfd 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -5,7 +5,7 @@ use rustc::mir::repr as mir; use std::error::Error; use std::fmt; -use memory::{FieldRepr, Memory, Pointer, Repr}; +use memory::{FieldRepr, IntRepr, Memory, Pointer, Repr}; const TRACE_EXECUTION: bool = true; @@ -321,13 +321,14 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> { ty::AdtKind::Struct => self.assign_to_product(dest, &dest_repr, operands), ty::AdtKind::Enum => match dest_repr { - Repr::Sum { discr_size, ref variants, .. } => + Repr::Sum { discr_size, ref variants, .. } => { // TODO(tsion): Write the discriminant value. self.assign_to_product( dest.offset(discr_size), &variants[variant_idx], operands - ), + ) + } _ => panic!("expected Repr::Sum target"), } }, @@ -416,7 +417,8 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> { match *const_val { Float(_f) => unimplemented!(), Int(n) => { - let ptr = self.memory.allocate(Repr::Int.size()); + // TODO(tsion): Check int constant type. + let ptr = self.memory.allocate(8); try!(self.memory.write_int(ptr, n)); Ok(ptr) } @@ -449,9 +451,16 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> { // TODO(tsion): Cache these outputs. fn ty_to_repr(&self, ty: ty::Ty<'tcx>) -> Repr { + use syntax::ast::IntTy; match ty.sty { ty::TyBool => Repr::Bool, - ty::TyInt(_) => Repr::Int, + + ty::TyInt(IntTy::Is) => unimplemented!(), + ty::TyInt(IntTy::I8) => Repr::Int(IntRepr::I8), + ty::TyInt(IntTy::I16) => Repr::Int(IntRepr::I16), + ty::TyInt(IntTy::I32) => Repr::Int(IntRepr::I32), + ty::TyInt(IntTy::I64) => Repr::Int(IntRepr::I64), + ty::TyTuple(ref fields) => self.make_product_repr(fields.iter().cloned()), ty::TyEnum(adt_def, ref subst) => { diff --git a/src/memory.rs b/src/memory.rs index 352f19dcdf02..9b4ed9b584a0 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,6 +1,5 @@ use byteorder::{self, ByteOrder}; use std::collections::HashMap; -use std::mem; use std::ptr; use interpreter::{EvalError, EvalResult}; @@ -26,6 +25,9 @@ pub struct Pointer { pub offset: usize, } +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum IntRepr { I8, I16, I32, I64 } + #[derive(Clone, Debug, PartialEq, Eq)] pub struct FieldRepr { pub offset: usize, @@ -35,7 +37,7 @@ pub struct FieldRepr { #[derive(Clone, Debug, PartialEq, Eq)] pub enum Repr { Bool, - Int, + Int(IntRepr), /// The representation for product types including tuples, structs, and the contents of enum /// variants. @@ -118,11 +120,11 @@ impl Memory { } pub fn read_int(&self, ptr: Pointer) -> EvalResult { - self.get_bytes(ptr, Repr::Int.size()).map(byteorder::NativeEndian::read_i64) + self.get_bytes(ptr, 8).map(byteorder::NativeEndian::read_i64) } pub fn write_int(&mut self, ptr: Pointer, n: i64) -> EvalResult<()> { - let bytes = try!(self.get_bytes_mut(ptr, Repr::Int.size())); + let bytes = try!(self.get_bytes_mut(ptr, 8)); byteorder::NativeEndian::write_i64(bytes, n); Ok(()) } @@ -164,7 +166,10 @@ impl Repr { pub fn size(&self) -> usize { match *self { Repr::Bool => 1, - Repr::Int => mem::size_of::(), + Repr::Int(IntRepr::I8) => 1, + Repr::Int(IntRepr::I16) => 2, + Repr::Int(IntRepr::I32) => 4, + Repr::Int(IntRepr::I64) => 8, Repr::Product { size, .. } => size, Repr::Sum { discr_size, max_variant_size, .. } => discr_size + max_variant_size, } diff --git a/test/ints.rs b/test/ints.rs index 80f842d3768d..1885ba48340d 100644 --- a/test/ints.rs +++ b/test/ints.rs @@ -30,7 +30,7 @@ fn arith() -> i64 { #[miri_run] fn match_int() -> i64 { - let n = 2; + let n = 2i64; match n { 0 => 0, 1 => 10,