rust/src/memory.rs

178 lines
4.9 KiB
Rust
Raw Normal View History

2016-03-05 00:52:14 -06:00
use byteorder::{self, ByteOrder};
2016-03-05 00:48:23 -06:00
use rustc::middle::ty;
use std::collections::HashMap;
use std::mem;
use std::ptr;
use interpreter::{EvalError, EvalResult};
pub struct Memory {
next_id: u64,
alloc_map: HashMap<u64, Allocation>,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct AllocId(u64);
2016-03-05 00:50:53 -06:00
#[derive(Debug)]
2016-03-05 00:48:23 -06:00
pub struct Allocation {
pub bytes: Vec<u8>,
// TODO(tsion): relocations
// TODO(tsion): undef mask
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2016-03-05 00:48:23 -06:00
pub struct Pointer {
pub alloc_id: AllocId,
pub offset: usize,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FieldRepr {
pub offset: usize,
pub repr: Repr,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Repr {
2016-03-07 04:44:03 -06:00
Bool,
2016-03-05 00:48:23 -06:00
Int,
Aggregate {
size: usize,
fields: Vec<FieldRepr>,
},
}
impl Memory {
pub fn new() -> Self {
Memory { next_id: 0, alloc_map: HashMap::new() }
}
pub fn allocate_raw(&mut self, size: usize) -> AllocId {
let id = AllocId(self.next_id);
let alloc = Allocation { bytes: vec![0; size] };
self.alloc_map.insert(self.next_id, alloc);
self.next_id += 1;
id
}
pub fn allocate(&mut self, repr: &Repr) -> Pointer {
2016-03-05 00:48:23 -06:00
Pointer {
alloc_id: self.allocate_raw(repr.size()),
offset: 0,
}
}
pub fn get(&self, id: AllocId) -> EvalResult<&Allocation> {
self.alloc_map.get(&id.0).ok_or(EvalError::DanglingPointerDeref)
}
pub fn get_mut(&mut self, id: AllocId) -> EvalResult<&mut Allocation> {
self.alloc_map.get_mut(&id.0).ok_or(EvalError::DanglingPointerDeref)
}
fn get_bytes(&self, ptr: Pointer, size: usize) -> EvalResult<&[u8]> {
2016-03-05 00:48:23 -06:00
let alloc = try!(self.get(ptr.alloc_id));
try!(alloc.check_bytes(ptr.offset, ptr.offset + size));
Ok(&alloc.bytes[ptr.offset..ptr.offset + size])
}
fn get_bytes_mut(&mut self, ptr: Pointer, size: usize) -> EvalResult<&mut [u8]> {
2016-03-05 00:48:23 -06:00
let alloc = try!(self.get_mut(ptr.alloc_id));
try!(alloc.check_bytes(ptr.offset, ptr.offset + size));
Ok(&mut alloc.bytes[ptr.offset..ptr.offset + size])
}
pub fn copy(&mut self, src: Pointer, dest: Pointer, size: usize) -> EvalResult<()> {
2016-03-05 00:48:23 -06:00
let src_bytes = try!(self.get_bytes_mut(src, size)).as_mut_ptr();
let dest_bytes = try!(self.get_bytes_mut(dest, size)).as_mut_ptr();
// SAFE: The above indexing would have panicked if there weren't at least `size` bytes
// behind `src` and `dest`. Also, we use the overlapping-safe `ptr::copy` if `src` and
// `dest` could possibly overlap.
unsafe {
if src.alloc_id == dest.alloc_id {
ptr::copy(src_bytes, dest_bytes, size);
} else {
ptr::copy_nonoverlapping(src_bytes, dest_bytes, size);
}
}
Ok(())
}
pub fn read_int(&self, ptr: Pointer) -> EvalResult<i64> {
2016-03-07 04:44:03 -06:00
self.get_bytes(ptr, Repr::Int.size()).map(byteorder::NativeEndian::read_i64)
2016-03-05 00:48:23 -06:00
}
pub fn write_int(&mut self, ptr: Pointer, n: i64) -> EvalResult<()> {
2016-03-05 00:48:23 -06:00
let bytes = try!(self.get_bytes_mut(ptr, Repr::Int.size()));
2016-03-07 04:44:03 -06:00
byteorder::NativeEndian::write_i64(bytes, n);
Ok(())
}
pub fn read_bool(&self, ptr: Pointer) -> EvalResult<bool> {
2016-03-07 04:44:03 -06:00
let bytes = try!(self.get_bytes(ptr, 1));
match bytes[0] {
0 => Ok(false),
1 => Ok(true),
_ => Err(EvalError::InvalidBool),
}
}
pub fn write_bool(&mut self, ptr: Pointer, b: bool) -> EvalResult<()> {
2016-03-07 04:44:03 -06:00
let bytes = try!(self.get_bytes_mut(ptr, 1));
bytes[0] = b as u8;
Ok(())
2016-03-05 00:48:23 -06:00
}
}
impl Allocation {
fn check_bytes(&self, start: usize, end: usize) -> EvalResult<()> {
2016-03-07 04:44:03 -06:00
if start < self.bytes.len() && end <= self.bytes.len() {
Ok(())
} else {
Err(EvalError::PointerOutOfBounds)
2016-03-05 00:48:23 -06:00
}
}
}
impl Pointer {
pub fn offset(self, i: usize) -> Self {
// TODO(tsion): Check for offset out of bounds.
Pointer { offset: self.offset + i, ..self }
2016-03-05 00:48:23 -06:00
}
}
impl Repr {
// TODO(tsion): Cache these outputs.
pub fn from_ty(ty: ty::Ty) -> Self {
match ty.sty {
2016-03-07 04:44:03 -06:00
ty::TyBool => Repr::Bool,
2016-03-05 00:48:23 -06:00
ty::TyInt(_) => Repr::Int,
ty::TyTuple(ref fields) => {
let mut size = 0;
let fields = fields.iter().map(|ty| {
let repr = Repr::from_ty(ty);
let old_size = size;
size += repr.size();
FieldRepr { offset: old_size, repr: repr }
}).collect();
Repr::Aggregate { size: size, fields: fields }
},
_ => unimplemented!(),
}
}
pub fn size(&self) -> usize {
match *self {
2016-03-07 04:44:03 -06:00
Repr::Bool => 1,
2016-03-05 00:48:23 -06:00
Repr::Int => mem::size_of::<i64>(),
Repr::Aggregate { size, .. } => size,
}
}
}