2016-03-05 00:52:14 -06:00
|
|
|
use byteorder::{self, ByteOrder};
|
2016-03-05 00:48:23 -06:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::ptr;
|
|
|
|
|
2016-03-13 01:43:28 -06:00
|
|
|
use interpreter::{EvalError, EvalResult};
|
|
|
|
use primval::PrimVal;
|
2016-03-05 00:48:23 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-07 07:10:52 -06:00
|
|
|
#[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-13 01:48:07 -06:00
|
|
|
I8,
|
|
|
|
I16,
|
|
|
|
I32,
|
|
|
|
I64,
|
2016-03-12 22:15:59 -06:00
|
|
|
|
|
|
|
/// The representation for product types including tuples, structs, and the contents of enum
|
|
|
|
/// variants.
|
|
|
|
Product {
|
|
|
|
/// Size in bytes.
|
2016-03-05 00:48:23 -06:00
|
|
|
size: usize,
|
|
|
|
fields: Vec<FieldRepr>,
|
|
|
|
},
|
2016-03-12 22:15:59 -06:00
|
|
|
|
|
|
|
/// The representation for a sum type, i.e. a Rust enum.
|
|
|
|
Sum {
|
|
|
|
/// The size of the largest variant in bytes.
|
|
|
|
max_variant_size: usize,
|
|
|
|
variants: Vec<Repr>,
|
2016-03-13 01:55:48 -06:00
|
|
|
discr: Box<Repr>,
|
2016-03-12 22:15:59 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
// Array {
|
|
|
|
// /// Number of elements.
|
|
|
|
// length: usize,
|
|
|
|
// elem: Repr,
|
|
|
|
// },
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Memory {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Memory { next_id: 0, alloc_map: HashMap::new() }
|
|
|
|
}
|
|
|
|
|
2016-03-07 07:19:43 -06:00
|
|
|
pub fn allocate(&mut self, size: usize) -> Pointer {
|
2016-03-05 00:48:23 -06:00
|
|
|
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;
|
|
|
|
Pointer {
|
2016-03-07 07:19:43 -06:00
|
|
|
alloc_id: id,
|
2016-03-05 00:48:23 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-03-07 07:10:52 -06:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
|
2016-03-07 07:10:52 -06:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
|
2016-03-07 07:10:52 -06:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2016-03-13 01:14:20 -06:00
|
|
|
pub fn read_primval(&self, ptr: Pointer, repr: &Repr) -> EvalResult<PrimVal> {
|
|
|
|
match *repr {
|
|
|
|
Repr::Bool => self.read_bool(ptr).map(PrimVal::Bool),
|
2016-03-13 01:48:07 -06:00
|
|
|
Repr::I8 => self.read_i8(ptr).map(PrimVal::I8),
|
|
|
|
Repr::I16 => self.read_i16(ptr).map(PrimVal::I16),
|
|
|
|
Repr::I32 => self.read_i32(ptr).map(PrimVal::I32),
|
|
|
|
Repr::I64 => self.read_i64(ptr).map(PrimVal::I64),
|
2016-03-13 01:14:20 -06:00
|
|
|
_ => panic!("primitive read of non-primitive: {:?}", repr),
|
|
|
|
}
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
2016-03-13 01:14:20 -06:00
|
|
|
pub fn write_primval(&mut self, ptr: Pointer, val: PrimVal) -> EvalResult<()> {
|
|
|
|
match val {
|
|
|
|
PrimVal::Bool(b) => self.write_bool(ptr, b),
|
|
|
|
PrimVal::I8(n) => self.write_i8(ptr, n),
|
|
|
|
PrimVal::I16(n) => self.write_i16(ptr, n),
|
|
|
|
PrimVal::I32(n) => self.write_i32(ptr, n),
|
|
|
|
PrimVal::I64(n) => self.write_i64(ptr, n),
|
|
|
|
}
|
2016-03-07 04:44:03 -06:00
|
|
|
}
|
|
|
|
|
2016-03-07 07:10:52 -06:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 07:10:52 -06:00
|
|
|
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
|
|
|
}
|
2016-03-13 01:14:20 -06:00
|
|
|
|
|
|
|
pub fn read_i8(&self, ptr: Pointer) -> EvalResult<i8> {
|
|
|
|
self.get_bytes(ptr, 1).map(|b| b[0] as i8)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_i8(&mut self, ptr: Pointer, n: i8) -> EvalResult<()> {
|
|
|
|
self.get_bytes_mut(ptr, 1).map(|b| b[0] = n as u8)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_i16(&self, ptr: Pointer) -> EvalResult<i16> {
|
|
|
|
self.get_bytes(ptr, 2).map(byteorder::NativeEndian::read_i16)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_i16(&mut self, ptr: Pointer, n: i16) -> EvalResult<()> {
|
|
|
|
let bytes = try!(self.get_bytes_mut(ptr, 2));
|
|
|
|
byteorder::NativeEndian::write_i16(bytes, n);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_i32(&self, ptr: Pointer) -> EvalResult<i32> {
|
|
|
|
self.get_bytes(ptr, 4).map(byteorder::NativeEndian::read_i32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_i32(&mut self, ptr: Pointer, n: i32) -> EvalResult<()> {
|
|
|
|
let bytes = try!(self.get_bytes_mut(ptr, 4));
|
|
|
|
byteorder::NativeEndian::write_i32(bytes, n);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_i64(&self, ptr: Pointer) -> EvalResult<i64> {
|
|
|
|
self.get_bytes(ptr, 8).map(byteorder::NativeEndian::read_i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_i64(&mut self, ptr: Pointer, n: i64) -> EvalResult<()> {
|
|
|
|
let bytes = try!(self.get_bytes_mut(ptr, 8));
|
|
|
|
byteorder::NativeEndian::write_i64(bytes, n);
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Allocation {
|
|
|
|
fn check_bytes(&self, start: usize, end: usize) -> EvalResult<()> {
|
2016-03-12 21:32:05 -06:00
|
|
|
if start <= self.bytes.len() && end <= self.bytes.len() {
|
2016-03-07 04:44:03 -06:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(EvalError::PointerOutOfBounds)
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Pointer {
|
2016-03-07 07:10:52 -06:00
|
|
|
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 {
|
|
|
|
pub fn size(&self) -> usize {
|
|
|
|
match *self {
|
2016-03-07 04:44:03 -06:00
|
|
|
Repr::Bool => 1,
|
2016-03-13 01:48:07 -06:00
|
|
|
Repr::I8 => 1,
|
|
|
|
Repr::I16 => 2,
|
|
|
|
Repr::I32 => 4,
|
|
|
|
Repr::I64 => 8,
|
2016-03-12 22:15:59 -06:00
|
|
|
Repr::Product { size, .. } => size,
|
2016-03-13 01:55:48 -06:00
|
|
|
Repr::Sum { ref discr, max_variant_size, .. } => discr.size() + max_variant_size,
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|