1
Fork 0

Add initial support for different int sizes.

This commit is contained in:
Scott Olson 2016-03-12 23:15:53 -06:00
parent 3f96b3a122
commit 397dbd909a
3 changed files with 25 additions and 11 deletions

View file

@ -5,7 +5,7 @@ use rustc::mir::repr as mir;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use memory::{FieldRepr, Memory, Pointer, Repr}; use memory::{FieldRepr, IntRepr, Memory, Pointer, Repr};
const TRACE_EXECUTION: bool = true; 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::Struct => self.assign_to_product(dest, &dest_repr, operands),
ty::AdtKind::Enum => match dest_repr { ty::AdtKind::Enum => match dest_repr {
Repr::Sum { discr_size, ref variants, .. } => Repr::Sum { discr_size, ref variants, .. } => {
// TODO(tsion): Write the discriminant value. // TODO(tsion): Write the discriminant value.
self.assign_to_product( self.assign_to_product(
dest.offset(discr_size), dest.offset(discr_size),
&variants[variant_idx], &variants[variant_idx],
operands operands
), )
}
_ => panic!("expected Repr::Sum target"), _ => panic!("expected Repr::Sum target"),
} }
}, },
@ -416,7 +417,8 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> {
match *const_val { match *const_val {
Float(_f) => unimplemented!(), Float(_f) => unimplemented!(),
Int(n) => { 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)); try!(self.memory.write_int(ptr, n));
Ok(ptr) Ok(ptr)
} }
@ -449,9 +451,16 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> {
// TODO(tsion): Cache these outputs. // TODO(tsion): Cache these outputs.
fn ty_to_repr(&self, ty: ty::Ty<'tcx>) -> Repr { fn ty_to_repr(&self, ty: ty::Ty<'tcx>) -> Repr {
use syntax::ast::IntTy;
match ty.sty { match ty.sty {
ty::TyBool => Repr::Bool, 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::TyTuple(ref fields) => self.make_product_repr(fields.iter().cloned()),
ty::TyEnum(adt_def, ref subst) => { ty::TyEnum(adt_def, ref subst) => {

View file

@ -1,6 +1,5 @@
use byteorder::{self, ByteOrder}; use byteorder::{self, ByteOrder};
use std::collections::HashMap; use std::collections::HashMap;
use std::mem;
use std::ptr; use std::ptr;
use interpreter::{EvalError, EvalResult}; use interpreter::{EvalError, EvalResult};
@ -26,6 +25,9 @@ pub struct Pointer {
pub offset: usize, pub offset: usize,
} }
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum IntRepr { I8, I16, I32, I64 }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct FieldRepr { pub struct FieldRepr {
pub offset: usize, pub offset: usize,
@ -35,7 +37,7 @@ pub struct FieldRepr {
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum Repr { pub enum Repr {
Bool, Bool,
Int, Int(IntRepr),
/// The representation for product types including tuples, structs, and the contents of enum /// The representation for product types including tuples, structs, and the contents of enum
/// variants. /// variants.
@ -118,11 +120,11 @@ impl Memory {
} }
pub fn read_int(&self, ptr: Pointer) -> EvalResult<i64> { pub fn read_int(&self, ptr: Pointer) -> EvalResult<i64> {
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<()> { 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); byteorder::NativeEndian::write_i64(bytes, n);
Ok(()) Ok(())
} }
@ -164,7 +166,10 @@ impl Repr {
pub fn size(&self) -> usize { pub fn size(&self) -> usize {
match *self { match *self {
Repr::Bool => 1, Repr::Bool => 1,
Repr::Int => mem::size_of::<i64>(), 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::Product { size, .. } => size,
Repr::Sum { discr_size, max_variant_size, .. } => discr_size + max_variant_size, Repr::Sum { discr_size, max_variant_size, .. } => discr_size + max_variant_size,
} }

View file

@ -30,7 +30,7 @@ fn arith() -> i64 {
#[miri_run] #[miri_run]
fn match_int() -> i64 { fn match_int() -> i64 {
let n = 2; let n = 2i64;
match n { match n {
0 => 0, 0 => 0,
1 => 10, 1 => 10,