2 use rustc::ty::{self, Ty};
3 use rustc::ty::layout::{self, Align, LayoutOf, TyLayout};
4 use rustc_data_structures::indexed_vec::Idx;
6 use rustc::mir::interpret::{GlobalId, Value, PrimVal, EvalResult, Pointer, MemoryPointer};
7 use super::{EvalContext, Machine, ValTy};
8 use interpret::memory::HasMemory;
10 #[derive(Copy, Clone, Debug)]
12 /// A place referring to a value allocated in the `Memory` system.
14 /// A place may have an invalid (integral or undef) pointer,
15 /// since it might be turned back into a reference
16 /// before ever being dereferenced.
22 /// A place referring to a value on the stack. Represented by a stack frame index paired with
23 /// a Mir local index.
24 Local { frame: usize, local: mir::Local },
27 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
31 Vtable(MemoryPointer),
32 DowncastVariant(usize),
36 /// Produces a Place that will error if attempted to be read from
37 pub fn undef() -> Self {
38 Self::from_primval_ptr(PrimVal::Undef.into(), Align::from_bytes(1, 1).unwrap())
41 pub fn from_primval_ptr(ptr: Pointer, align: Align) -> Self {
45 extra: PlaceExtra::None,
49 pub fn from_ptr(ptr: MemoryPointer, align: Align) -> Self {
50 Self::from_primval_ptr(ptr.into(), align)
53 pub fn to_ptr_align_extra(self) -> (Pointer, Align, PlaceExtra) {
55 Place::Ptr { ptr, align, extra } => (ptr, align, extra),
56 _ => bug!("to_ptr_and_extra: expected Place::Ptr, got {:?}", self),
61 pub fn to_ptr_align(self) -> (Pointer, Align) {
62 let (ptr, align, _extra) = self.to_ptr_align_extra();
66 pub fn to_ptr(self) -> EvalResult<'tcx, MemoryPointer> {
67 // At this point, we forget about the alignment information -- the place has been turned into a reference,
68 // and no matter where it came from, it now must be aligned.
69 self.to_ptr_align().0.to_ptr()
72 pub(super) fn elem_ty_and_len(self, ty: Ty<'tcx>) -> (Ty<'tcx>, u64) {
74 ty::TyArray(elem, n) => (elem, n.val.unwrap_u64() as u64),
76 ty::TySlice(elem) => {
78 Place::Ptr { extra: PlaceExtra::Length(len), .. } => (elem, len),
81 "elem_ty_and_len of a TySlice given non-slice place: {:?}",
88 _ => bug!("elem_ty_and_len expected array or slice, got {:?}", ty),
93 impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
94 /// Reads a value from the place without going through the intermediate step of obtaining
96 pub fn try_read_place(
98 place: &mir::Place<'tcx>,
99 ) -> EvalResult<'tcx, Option<Value>> {
100 use rustc::mir::Place::*;
102 // Might allow this in the future, right now there's no way to do this from Rust code anyway
103 Local(mir::RETURN_PLACE) => err!(ReadFromReturnPointer),
104 // Directly reading a local will always succeed
105 Local(local) => self.frame().get_local(local).map(Some),
106 // No fast path for statics. Reading from statics is rare and would require another
107 // Machine function to handle differently in miri.
108 Static(_) => Ok(None),
109 Projection(ref proj) => self.try_read_place_projection(proj),
116 variant: Option<usize>,
119 ) -> EvalResult<'tcx, Option<(Value, Ty<'tcx>)>> {
120 let mut base_layout = self.layout_of(base_ty)?;
121 if let Some(variant_index) = variant {
122 base_layout = base_layout.for_variant(self, variant_index);
124 let field_index = field.index();
125 let field = base_layout.field(self, field_index)?;
126 if field.size.bytes() == 0 {
127 return Ok(Some((Value::ByVal(PrimVal::Undef), field.ty)))
129 let offset = base_layout.fields.offset(field_index);
131 // the field covers the entire type
132 Value::ByValPair(..) |
133 Value::ByVal(_) if offset.bytes() == 0 && field.size == base_layout.size => Ok(Some((base, field.ty))),
134 // split fat pointers, 2 element tuples, ...
135 Value::ByValPair(a, b) if base_layout.fields.count() == 2 => {
136 let val = [a, b][field_index];
137 Ok(Some((Value::ByVal(val), field.ty)))
139 // FIXME(oli-obk): figure out whether we should be calling `try_read_value` here
144 fn try_read_place_projection(
146 proj: &mir::PlaceProjection<'tcx>,
147 ) -> EvalResult<'tcx, Option<Value>> {
148 use rustc::mir::ProjectionElem::*;
149 let base = match self.try_read_place(&proj.base)? {
151 None => return Ok(None),
153 let base_ty = self.place_ty(&proj.base);
155 Field(field, _) => Ok(self.read_field(base, None, field, base_ty)?.map(|(f, _)| f)),
156 // The NullablePointer cases should work fine, need to take care for normal enums
159 // reading index 0 or index 1 from a ByVal or ByVal pair could be optimized
160 ConstantIndex { .. } | Index(_) |
161 // No way to optimize this projection any better than the normal place path
166 /// Returns a value and (in case of a ByRef) if we are supposed to use aligned accesses.
167 pub(super) fn eval_and_read_place(
169 place: &mir::Place<'tcx>,
170 ) -> EvalResult<'tcx, Value> {
171 // Shortcut for things like accessing a fat pointer's field,
172 // which would otherwise (in the `eval_place` path) require moving a `ByValPair` to memory
173 // and returning an `Place::Ptr` to it
174 if let Some(val) = self.try_read_place(place)? {
177 let place = self.eval_place(place)?;
178 self.read_place(place)
181 pub fn read_place(&self, place: Place) -> EvalResult<'tcx, Value> {
183 Place::Ptr { ptr, align, extra } => {
184 assert_eq!(extra, PlaceExtra::None);
185 Ok(Value::ByRef(ptr, align))
187 Place::Local { frame, local } => self.stack[frame].get_local(local),
191 pub fn eval_place(&mut self, mir_place: &mir::Place<'tcx>) -> EvalResult<'tcx, Place> {
192 use rustc::mir::Place::*;
193 let place = match *mir_place {
194 Local(mir::RETURN_PLACE) => self.frame().return_place,
195 Local(local) => Place::Local {
196 frame: self.cur_frame(),
200 Static(ref static_) => {
201 let layout = self.layout_of(self.place_ty(mir_place))?;
202 let instance = ty::Instance::mono(*self.tcx, static_.def_id);
207 let alloc = Machine::init_static(self, cid)?;
209 ptr: MemoryPointer::new(alloc, 0).into(),
211 extra: PlaceExtra::None,
215 Projection(ref proj) => {
216 let ty = self.place_ty(&proj.base);
217 let place = self.eval_place(&proj.base)?;
218 return self.eval_place_projection(place, ty, &proj.elem);
222 self.dump_local(place);
231 mut base_layout: TyLayout<'tcx>,
232 ) -> EvalResult<'tcx, (Place, TyLayout<'tcx>)> {
234 Place::Ptr { extra: PlaceExtra::DowncastVariant(variant_index), .. } => {
235 base_layout = base_layout.for_variant(&self, variant_index);
239 let field_index = field.index();
240 let field = base_layout.field(&self, field_index)?;
241 let offset = base_layout.fields.offset(field_index);
243 // Do not allocate in trivial cases
244 let (base_ptr, base_align, base_extra) = match base {
245 Place::Ptr { ptr, align, extra } => (ptr, align, extra),
246 Place::Local { frame, local } => {
247 match (&self.stack[frame].get_local(local)?, &base_layout.abi) {
248 // in case the field covers the entire type, just return the value
249 (&Value::ByVal(_), &layout::Abi::Scalar(_)) |
250 (&Value::ByValPair(..), &layout::Abi::ScalarPair(..))
251 if offset.bytes() == 0 && field.size == base_layout.size =>
253 return Ok((base, field));
255 _ => self.force_allocation(base)?.to_ptr_align_extra(),
260 let offset = match base_extra {
261 PlaceExtra::Vtable(tab) => {
262 let (_, align) = self.size_and_align_of_dst(
264 base_ptr.to_value_with_vtable(tab),
266 offset.abi_align(align).bytes()
271 let ptr = base_ptr.offset(offset, &self)?;
272 let align = base_align.min(base_layout.align).min(field.align);
273 let extra = if !field.is_unsized() {
277 PlaceExtra::None => bug!("expected fat pointer"),
278 PlaceExtra::DowncastVariant(..) => {
279 bug!("Rust doesn't support unsized fields in enum variants")
281 PlaceExtra::Vtable(_) |
282 PlaceExtra::Length(_) => {}
287 Ok((Place::Ptr { ptr, align, extra }, field))
290 pub fn val_to_place(&self, val: Value, ty: Ty<'tcx>) -> EvalResult<'tcx, Place> {
291 let layout = self.layout_of(ty)?;
292 Ok(match self.tcx.struct_tail(ty).sty {
293 ty::TyDynamic(..) => {
294 let (ptr, vtable) = self.into_ptr_vtable_pair(val)?;
298 extra: PlaceExtra::Vtable(vtable),
301 ty::TyStr | ty::TySlice(_) => {
302 let (ptr, len) = self.into_slice(val)?;
306 extra: PlaceExtra::Length(len),
309 _ => Place::from_primval_ptr(self.into_ptr(val)?, layout.align),
318 ) -> EvalResult<'tcx, Place> {
319 // Taking the outer type here may seem odd; it's needed because for array types, the outer type gives away the length.
320 let base = self.force_allocation(base)?;
321 let (base_ptr, align) = base.to_ptr_align();
323 let (elem_ty, len) = base.elem_ty_and_len(outer_ty);
324 let elem_size = self.layout_of(elem_ty)?.size.bytes();
327 "Tried to access element {} of array/slice with length {}",
331 let ptr = base_ptr.offset(n * elem_size, &*self)?;
335 extra: PlaceExtra::None,
339 pub(super) fn place_downcast(
343 ) -> EvalResult<'tcx, Place> {
345 let base = self.force_allocation(base)?;
346 let (ptr, align) = base.to_ptr_align();
347 let extra = PlaceExtra::DowncastVariant(variant);
348 Ok(Place::Ptr { ptr, align, extra })
351 pub fn eval_place_projection(
355 proj_elem: &mir::ProjectionElem<'tcx, mir::Local, Ty<'tcx>>,
356 ) -> EvalResult<'tcx, Place> {
357 use rustc::mir::ProjectionElem::*;
360 let layout = self.layout_of(base_ty)?;
361 Ok(self.place_field(base, field, layout)?.0)
364 Downcast(_, variant) => {
365 self.place_downcast(base, variant)
369 let val = self.read_place(base)?;
371 let pointee_type = match base_ty.sty {
372 ty::TyRawPtr(ref tam) |
373 ty::TyRef(_, ref tam) => tam.ty,
374 ty::TyAdt(def, _) if def.is_box() => base_ty.boxed_ty(),
375 _ => bug!("can only deref pointer types"),
378 trace!("deref to {} on {:?}", pointee_type, val);
380 self.val_to_place(val, pointee_type)
384 let value = self.frame().get_local(local)?;
385 let ty = self.tcx.types.usize;
386 let n = self.value_to_primval(ValTy { value, ty })?.to_u64()?;
387 self.place_index(base, base_ty, n)
396 let base = self.force_allocation(base)?;
397 let (base_ptr, align) = base.to_ptr_align();
399 let (elem_ty, n) = base.elem_ty_and_len(base_ty);
400 let elem_size = self.layout_of(elem_ty)?.size.bytes();
401 assert!(n >= min_length as u64);
403 let index = if from_end {
404 n - u64::from(offset)
409 let ptr = base_ptr.offset(index * elem_size, &self)?;
410 Ok(Place::Ptr { ptr, align, extra: PlaceExtra::None })
413 Subslice { from, to } => {
415 let base = self.force_allocation(base)?;
416 let (base_ptr, align) = base.to_ptr_align();
418 let (elem_ty, n) = base.elem_ty_and_len(base_ty);
419 let elem_size = self.layout_of(elem_ty)?.size.bytes();
420 assert!(u64::from(from) <= n - u64::from(to));
421 let ptr = base_ptr.offset(u64::from(from) * elem_size, &self)?;
422 // sublicing arrays produces arrays
423 let extra = if self.type_is_sized(base_ty) {
426 PlaceExtra::Length(n - u64::from(to) - u64::from(from))
428 Ok(Place::Ptr { ptr, align, extra })
433 pub fn place_ty(&self, place: &mir::Place<'tcx>) -> Ty<'tcx> {
435 place.ty(self.mir(), *self.tcx).to_ty(*self.tcx),