2016-09-23 03:27:14 -05:00
|
|
|
use error::EvalResult;
|
2016-10-14 04:31:45 -05:00
|
|
|
use memory::{Memory, Pointer};
|
2016-10-21 04:17:53 -05:00
|
|
|
use primval::PrimVal;
|
2016-09-23 03:27:14 -05:00
|
|
|
|
|
|
|
/// A `Value` represents a single self-contained Rust value.
|
|
|
|
///
|
|
|
|
/// A `Value` can either refer to a block of memory inside an allocation (`ByRef`) or to a primitve
|
|
|
|
/// value held directly, outside of any allocation (`ByVal`).
|
|
|
|
///
|
|
|
|
/// For optimization of a few very common cases, there is also a representation for a pair of
|
|
|
|
/// primitive values (`ByValPair`). It allows Miri to avoid making allocations for checked binary
|
|
|
|
/// operations and fat pointers. This idea was taken from rustc's trans.
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
2016-10-14 04:31:45 -05:00
|
|
|
pub enum Value {
|
2016-09-23 03:27:14 -05:00
|
|
|
ByRef(Pointer),
|
|
|
|
ByVal(PrimVal),
|
2016-09-26 10:49:30 -05:00
|
|
|
ByValPair(PrimVal, PrimVal),
|
2016-09-23 03:27:14 -05:00
|
|
|
}
|
|
|
|
|
2016-10-14 04:31:45 -05:00
|
|
|
impl<'a, 'tcx: 'a> Value {
|
|
|
|
pub(super) fn read_ptr(&self, mem: &Memory<'a, 'tcx>) -> EvalResult<'tcx, Pointer> {
|
2016-09-23 03:27:14 -05:00
|
|
|
use self::Value::*;
|
|
|
|
match *self {
|
|
|
|
ByRef(ptr) => mem.read_ptr(ptr),
|
2016-11-15 07:12:49 -06:00
|
|
|
ByVal(ptr) | ByValPair(ptr, _) => Ok(ptr.to_ptr()),
|
2016-09-26 10:49:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 03:12:26 -05:00
|
|
|
pub(super) fn expect_ptr_vtable_pair(
|
|
|
|
&self,
|
|
|
|
mem: &Memory<'a, 'tcx>
|
|
|
|
) -> EvalResult<'tcx, (Pointer, Pointer)> {
|
2016-09-23 03:27:14 -05:00
|
|
|
use self::Value::*;
|
|
|
|
match *self {
|
2016-11-15 07:12:49 -06:00
|
|
|
ByRef(ref_ptr) => {
|
|
|
|
let ptr = mem.read_ptr(ref_ptr)?;
|
|
|
|
let vtable = mem.read_ptr(ref_ptr.offset(mem.pointer_size() as isize))?;
|
2016-10-16 03:12:26 -05:00
|
|
|
Ok((ptr, vtable))
|
|
|
|
}
|
2016-10-20 05:42:19 -05:00
|
|
|
|
2016-11-15 07:12:49 -06:00
|
|
|
ByValPair(ptr, vtable) => Ok((ptr.to_ptr(), vtable.to_ptr())),
|
2016-10-20 05:42:19 -05:00
|
|
|
|
2016-10-16 03:12:26 -05:00
|
|
|
_ => bug!("expected ptr and vtable, got {:?}", self),
|
2016-09-23 03:27:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 07:12:49 -06:00
|
|
|
pub(super) fn expect_slice(&self, mem: &Memory<'a, 'tcx>) -> EvalResult<'tcx, (Pointer, u64)> {
|
2016-09-23 03:27:14 -05:00
|
|
|
use self::Value::*;
|
|
|
|
match *self {
|
2016-11-15 07:12:49 -06:00
|
|
|
ByRef(ref_ptr) => {
|
|
|
|
let ptr = mem.read_ptr(ref_ptr)?;
|
|
|
|
let len = mem.read_usize(ref_ptr.offset(mem.pointer_size() as isize))?;
|
|
|
|
Ok((ptr, len))
|
|
|
|
},
|
|
|
|
ByValPair(ptr, val) => {
|
|
|
|
Ok((ptr.to_ptr(), val.try_as_uint()?))
|
|
|
|
},
|
2016-09-23 03:27:14 -05:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|