place.rs 45.5 KB
Newer Older
R
Ralf Jung 已提交
1 2 3 4 5
//! Computations on places -- field projections, going from mir::Place, and writing
//! into a place.
//! All high-level functions to write to memory work on places as destinations.

use std::convert::TryFrom;
6
use std::hash::Hash;
R
Ralf Jung 已提交
7

8
use rustc::mir;
9
use rustc::mir::interpret::truncate;
R
Ralf Jung 已提交
10
use rustc::ty::{self, Ty};
11 12 13
use rustc::ty::layout::{
    self, Size, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx, IntegerExt
};
14
use rustc::ty::TypeFoldable;
O
Oliver Schneider 已提交
15

16
use super::{
17
    GlobalId, AllocId, Allocation, Scalar, InterpResult, Pointer, PointerArithmetic,
18
    InterpCx, Machine, AllocMap, AllocationExtra,
19
    RawConst, Immediate, ImmTy, ScalarMaybeUndef, Operand, OpTy, MemoryKind, LocalValue,
R
Ralf Jung 已提交
20 21 22
};

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
23
pub struct MemPlace<Tag=(), Id=AllocId> {
R
Ralf Jung 已提交
24 25 26
    /// A place may have an integral pointer for ZSTs, and since it might
    /// be turned back into a reference before ever being dereferenced.
    /// However, it may never be undef.
27
    pub ptr: Scalar<Tag, Id>,
28
    pub align: Align,
A
Alexander Regueiro 已提交
29
    /// Metadata for unsized places. Interpretation is up to the type.
R
Ralf Jung 已提交
30
    /// Must not be present for sized types, but can be missing for unsized types
31
    /// (e.g., `extern type`).
R
Ralf Jung 已提交
32
    pub meta: Option<Scalar<Tag, Id>>,
R
Ralf Jung 已提交
33
}
O
Oliver Schneider 已提交
34

35
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
36
pub enum Place<Tag=(), Id=AllocId> {
37
    /// A place referring to a value allocated in the `Memory` system.
38
    Ptr(MemPlace<Tag, Id>),
O
Oliver Schneider 已提交
39

R
Ralf Jung 已提交
40 41 42 43 44 45
    /// To support alloc-free locals, we are able to write directly to a local.
    /// (Without that optimization, we'd just always be a `MemPlace`.)
    Local {
        frame: usize,
        local: mir::Local,
    },
O
Oliver Schneider 已提交
46 47
}

R
Ralf Jung 已提交
48
#[derive(Copy, Clone, Debug)]
49
pub struct PlaceTy<'tcx, Tag=()> {
50
    place: Place<Tag>, // Keep this private, it helps enforce invariants
R
Ralf Jung 已提交
51 52 53
    pub layout: TyLayout<'tcx>,
}

54 55
impl<'tcx, Tag> ::std::ops::Deref for PlaceTy<'tcx, Tag> {
    type Target = Place<Tag>;
56
    #[inline(always)]
57
    fn deref(&self) -> &Place<Tag> {
R
Ralf Jung 已提交
58 59 60 61 62
        &self.place
    }
}

/// A MemPlace with its layout. Constructing it is only possible in this module.
63
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
64 65
pub struct MPlaceTy<'tcx, Tag=()> {
    mplace: MemPlace<Tag>,
R
Ralf Jung 已提交
66 67 68
    pub layout: TyLayout<'tcx>,
}

69 70
impl<'tcx, Tag> ::std::ops::Deref for MPlaceTy<'tcx, Tag> {
    type Target = MemPlace<Tag>;
71
    #[inline(always)]
72
    fn deref(&self) -> &MemPlace<Tag> {
R
Ralf Jung 已提交
73
        &self.mplace
O
Oliver Schneider 已提交
74
    }
R
Ralf Jung 已提交
75 76
}

77
impl<'tcx, Tag> From<MPlaceTy<'tcx, Tag>> for PlaceTy<'tcx, Tag> {
78
    #[inline(always)]
79
    fn from(mplace: MPlaceTy<'tcx, Tag>) -> Self {
R
Ralf Jung 已提交
80 81 82 83 84 85
        PlaceTy {
            place: Place::Ptr(mplace.mplace),
            layout: mplace.layout
        }
    }
}
O
Oliver Schneider 已提交
86

87 88
impl<Tag> MemPlace<Tag> {
    /// Replace ptr tag, maintain vtable tag (if any)
89
    #[inline]
90
    pub fn replace_tag(self, new_tag: Tag) -> Self {
91
        MemPlace {
92
            ptr: self.ptr.erase_tag().with_tag(new_tag),
93
            align: self.align,
94
            meta: self.meta,
95 96 97 98
        }
    }

    #[inline]
99
    pub fn erase_tag(self) -> MemPlace {
100 101 102
        MemPlace {
            ptr: self.ptr.erase_tag(),
            align: self.align,
R
Ralf Jung 已提交
103
            meta: self.meta.map(Scalar::erase_tag),
104 105 106
        }
    }

R
Ralf Jung 已提交
107
    #[inline(always)]
108
    pub fn from_scalar_ptr(ptr: Scalar<Tag>, align: Align) -> Self {
R
Ralf Jung 已提交
109
        MemPlace {
110 111
            ptr,
            align,
R
Ralf Jung 已提交
112
            meta: None,
O
Oliver Schneider 已提交
113 114 115
        }
    }

116 117
    /// Produces a Place that will error if attempted to be read from or written to
    #[inline(always)]
118
    pub fn null(cx: &impl HasDataLayout) -> Self {
119
        Self::from_scalar_ptr(Scalar::ptr_null(cx), Align::from_bytes(1).unwrap())
120 121
    }

R
Ralf Jung 已提交
122
    #[inline(always)]
123
    pub fn from_ptr(ptr: Pointer<Tag>, align: Align) -> Self {
R
Ralf Jung 已提交
124 125 126
        Self::from_scalar_ptr(ptr.into(), align)
    }

127 128 129 130 131 132 133 134 135
    /// Turn a mplace into a (thin or fat) pointer, as a reference, pointing to the same space.
    /// This is the inverse of `ref_to_mplace`.
    #[inline(always)]
    pub fn to_ref(self) -> Immediate<Tag> {
        match self.meta {
            None => Immediate::Scalar(self.ptr.into()),
            Some(meta) => Immediate::ScalarPair(self.ptr.into(), meta.into()),
        }
    }
136 137 138 139 140 141

    pub fn offset(
        self,
        offset: Size,
        meta: Option<Scalar<Tag>>,
        cx: &impl HasDataLayout,
142
    ) -> InterpResult<'tcx, Self> {
143 144 145 146 147 148
        Ok(MemPlace {
            ptr: self.ptr.ptr_offset(offset, cx)?,
            align: self.align.restrict_for_offset(offset),
            meta,
        })
    }
R
Ralf Jung 已提交
149 150
}

151
impl<'tcx, Tag> MPlaceTy<'tcx, Tag> {
R
Ralf Jung 已提交
152 153
    /// Produces a MemPlace that works for ZST but nothing else
    #[inline]
154
    pub fn dangling(layout: TyLayout<'tcx>, cx: &impl HasDataLayout) -> Self {
R
Ralf Jung 已提交
155 156
        MPlaceTy {
            mplace: MemPlace::from_scalar_ptr(
157
                Scalar::from_uint(layout.align.abi.bytes(), cx.pointer_size()),
158
                layout.align.abi
R
Ralf Jung 已提交
159 160 161 162 163
            ),
            layout
        }
    }

164
    /// Replace ptr tag, maintain vtable tag (if any)
165
    #[inline]
166
    pub fn replace_tag(self, new_tag: Tag) -> Self {
167
        MPlaceTy {
168
            mplace: self.mplace.replace_tag(new_tag),
169 170 171 172 173
            layout: self.layout,
        }
    }

    #[inline]
174 175 176 177 178 179
    pub fn offset(
        self,
        offset: Size,
        meta: Option<Scalar<Tag>>,
        layout: TyLayout<'tcx>,
        cx: &impl HasDataLayout,
180
    ) -> InterpResult<'tcx, Self> {
181 182 183 184 185 186
        Ok(MPlaceTy {
            mplace: self.mplace.offset(offset, meta, cx)?,
            layout,
        })
    }

R
Ralf Jung 已提交
187
    #[inline]
188
    fn from_aligned_ptr(ptr: Pointer<Tag>, layout: TyLayout<'tcx>) -> Self {
189
        MPlaceTy { mplace: MemPlace::from_ptr(ptr, layout.align.abi), layout }
R
Ralf Jung 已提交
190 191 192
    }

    #[inline]
193
    pub(super) fn len(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
194
        if self.layout.is_unsized() {
R
Ralf Jung 已提交
195
            // We need to consult `meta` metadata
196 197
            match self.layout.ty.sty {
                ty::Slice(..) | ty::Str =>
R
Ralf Jung 已提交
198
                    return self.mplace.meta.unwrap().to_usize(cx),
199
                _ => bug!("len not supported on unsized type {:?}", self.layout.ty),
200
            }
201 202
        } else {
            // Go through the layout.  There are lots of types that support a length,
203
            // e.g., SIMD types.
204 205 206
            match self.layout.fields {
                layout::FieldPlacement::Array { count, .. } => Ok(count),
                _ => bug!("len not supported on sized type {:?}", self.layout.ty),
207 208 209 210 211
            }
        }
    }

    #[inline]
212
    pub(super) fn vtable(self) -> Scalar<Tag> {
213
        match self.layout.ty.sty {
214
            ty::Dynamic(..) => self.mplace.meta.unwrap(),
215
            _ => bug!("vtable not supported on type {:?}", self.layout.ty),
R
Ralf Jung 已提交
216 217 218 219
        }
    }
}

220
// These are defined here because they produce a place.
221
impl<'tcx, Tag: ::std::fmt::Debug + Copy> OpTy<'tcx, Tag> {
222
    #[inline(always)]
223
    pub fn try_as_mplace(self) -> Result<MPlaceTy<'tcx, Tag>, ImmTy<'tcx, Tag>> {
224
        match *self {
R
Ralf Jung 已提交
225
            Operand::Indirect(mplace) => Ok(MPlaceTy { mplace, layout: self.layout }),
226
            Operand::Immediate(imm) => Err(ImmTy { imm, layout: self.layout }),
R
Ralf Jung 已提交
227 228 229
        }
    }

230
    #[inline(always)]
231
    pub fn assert_mem_place(self) -> MPlaceTy<'tcx, Tag> {
R
Ralf Jung 已提交
232 233 234 235
        self.try_as_mplace().unwrap()
    }
}

R
Ralf Jung 已提交
236
impl<Tag: ::std::fmt::Debug> Place<Tag> {
R
Ralf Jung 已提交
237
    /// Produces a Place that will error if attempted to be read from or written to
238
    #[inline(always)]
239
    pub fn null(cx: &impl HasDataLayout) -> Self {
240
        Place::Ptr(MemPlace::null(cx))
R
Ralf Jung 已提交
241 242
    }

243
    #[inline(always)]
244
    pub fn from_scalar_ptr(ptr: Scalar<Tag>, align: Align) -> Self {
R
Ralf Jung 已提交
245 246 247
        Place::Ptr(MemPlace::from_scalar_ptr(ptr, align))
    }

248
    #[inline(always)]
249
    pub fn from_ptr(ptr: Pointer<Tag>, align: Align) -> Self {
R
Ralf Jung 已提交
250
        Place::Ptr(MemPlace::from_ptr(ptr, align))
O
Oliver Schneider 已提交
251 252
    }

R
Ralf Jung 已提交
253
    #[inline]
254
    pub fn assert_mem_place(self) -> MemPlace<Tag> {
O
Oliver Schneider 已提交
255
        match self {
R
Ralf Jung 已提交
256
            Place::Ptr(mplace) => mplace,
257
            _ => bug!("assert_mem_place: expected Place::Ptr, got {:?}", self),
O
Oliver Schneider 已提交
258 259 260

        }
    }
R
Ralf Jung 已提交
261
}
O
Oliver Schneider 已提交
262

263
impl<'tcx, Tag: ::std::fmt::Debug> PlaceTy<'tcx, Tag> {
R
Ralf Jung 已提交
264
    #[inline]
265 266
    pub fn assert_mem_place(self) -> MPlaceTy<'tcx, Tag> {
        MPlaceTy { mplace: self.place.assert_mem_place(), layout: self.layout }
O
Oliver Schneider 已提交
267 268 269
    }
}

270
// separating the pointer tag for `impl Trait`, see https://github.com/rust-lang/rust/issues/54385
R
Ralf Jung 已提交
271
impl<'mir, 'tcx, Tag, M> InterpCx<'mir, 'tcx, M>
272
where
R
Ralf Jung 已提交
273
    // FIXME: Working around https://github.com/rust-lang/rust/issues/54385
274
    Tag: ::std::fmt::Debug + Copy + Eq + Hash + 'static,
275
    M: Machine<'mir, 'tcx, PointerTag = Tag>,
R
Ralf Jung 已提交
276
    // FIXME: Working around https://github.com/rust-lang/rust/issues/24159
277
    M::MemoryMap: AllocMap<AllocId, (MemoryKind<M::MemoryKinds>, Allocation<Tag, M::AllocExtra>)>,
278
    M::AllocExtra: AllocationExtra<Tag>,
279
{
R
Ralf Jung 已提交
280
    /// Take a value, which represents a (thin or fat) reference, and make it a place.
281
    /// Alignment is just based on the type.  This is the inverse of `MemPlace::to_ref()`.
R
Ralf Jung 已提交
282 283 284 285
    ///
    /// Only call this if you are sure the place is "valid" (aligned and inbounds), or do not
    /// want to ever use the place for memory access!
    /// Generally prefer `deref_operand`.
R
Ralf Jung 已提交
286
    pub fn ref_to_mplace(
287
        &self,
288
        val: ImmTy<'tcx, M::PointerTag>,
289
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
290 291 292
        let pointee_type = val.layout.ty.builtin_deref(true).unwrap().ty;
        let layout = self.layout_of(pointee_type)?;

293 294
        let mplace = MemPlace {
            ptr: val.to_scalar_ptr()?,
295 296 297 298
            // We could use the run-time alignment here. For now, we do not, because
            // the point of tracking the alignment here is to make sure that the *static*
            // alignment information emitted with the loads is correct. The run-time
            // alignment can only be more restrictive.
299
            align: layout.align.abi,
300 301 302
            meta: val.to_meta()?,
        };
        Ok(MPlaceTy { mplace, layout })
R
Ralf Jung 已提交
303 304
    }

305 306
    /// Take an operand, representing a pointer, and dereference it to a place -- that
    /// will always be a MemPlace.  Lives in `place.rs` because it creates a place.
307 308 309
    pub fn deref_operand(
        &self,
        src: OpTy<'tcx, M::PointerTag>,
310
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
311 312
        let val = self.read_immediate(src)?;
        trace!("deref to {} on {:?}", val.layout.ty, *val);
313 314
        let place = self.ref_to_mplace(val)?;
        self.mplace_access_checked(place)
315 316
    }

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    /// Check if the given place is good for memory access with the given
    /// size, falling back to the layout's size if `None` (in the latter case,
    /// this must be a statically sized type).
    ///
    /// On success, returns `None` for zero-sized accesses (where nothing else is
    /// left to do) and a `Pointer` to use for the actual access otherwise.
    #[inline]
    pub fn check_mplace_access(
        &self,
        place: MPlaceTy<'tcx, M::PointerTag>,
        size: Option<Size>,
    ) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
        let size = size.unwrap_or_else(|| {
            assert!(!place.layout.is_unsized());
            assert!(place.meta.is_none());
            place.layout.size
        });
        self.memory.check_ptr_access(place.ptr, size, place.align)
    }

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    /// Return the "access-checked" version of this `MPlace`, where for non-ZST
    /// this is definitely a `Pointer`.
    pub fn mplace_access_checked(
        &self,
        mut place: MPlaceTy<'tcx, M::PointerTag>,
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
        let (size, align) = self.size_and_align_of_mplace(place)?
            .unwrap_or((place.layout.size, place.layout.align.abi));
        assert!(place.mplace.align <= align, "dynamic alignment less strict than static one?");
        place.mplace.align = align; // maximally strict checking
        // When dereferencing a pointer, it must be non-NULL, aligned, and live.
        if let Some(ptr) = self.check_mplace_access(place, Some(size))? {
            place.mplace.ptr = ptr.into();
        }
        Ok(place)
    }

R
Ralf Jung 已提交
354
    /// Force `place.ptr` to a `Pointer`.
355
    /// Can be helpful to avoid lots of `force_ptr` calls later, if this place is used a lot.
R
Ralf Jung 已提交
356
    pub fn force_mplace_ptr(
357 358 359
        &self,
        mut place: MPlaceTy<'tcx, M::PointerTag>,
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
R
Ralf Jung 已提交
360
        place.mplace.ptr = self.force_ptr(place.mplace.ptr)?.into();
361 362 363
        Ok(place)
    }

364 365
    /// Offset a pointer to project to a field. Unlike `place_field`, this is always
    /// possible without allocating, so it can take `&self`. Also return the field's layout.
R
Ralf Jung 已提交
366 367 368
    /// This supports both struct and array fields.
    #[inline(always)]
    pub fn mplace_field(
O
Oliver Schneider 已提交
369
        &self,
370
        base: MPlaceTy<'tcx, M::PointerTag>,
R
Ralf Jung 已提交
371
        field: u64,
372
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
R
Ralf Jung 已提交
373 374 375 376 377
        // Not using the layout method because we want to compute on u64
        let offset = match base.layout.fields {
            layout::FieldPlacement::Arbitrary { ref offsets, .. } =>
                offsets[usize::try_from(field).unwrap()],
            layout::FieldPlacement::Array { stride, .. } => {
378
                let len = base.len(self)?;
379 380 381
                if field >= len {
                    // This can be violated because this runs during promotion on code where the
                    // type system has not yet ensured that such things don't happen.
382
                    debug!("tried to access element {} of array/slice with length {}", field, len);
S
Saleem Jaffer 已提交
383
                    throw_panic!(BoundsCheck { len, index: field });
384
                }
R
Ralf Jung 已提交
385 386
                stride * field
            }
387
            layout::FieldPlacement::Union(count) => {
B
Bernardo Meurer 已提交
388 389
                assert!(field < count as u64,
                        "Tried to access field {} of union with {} fields", field, count);
390 391 392
                // Offset is always 0
                Size::from_bytes(0)
            }
R
Ralf Jung 已提交
393 394 395
        };
        // the only way conversion can fail if is this is an array (otherwise we already panicked
        // above). In that case, all fields are equal.
R
Ralf Jung 已提交
396
        let field_layout = base.layout.field(self, usize::try_from(field).unwrap_or(0))?;
R
Ralf Jung 已提交
397

398
        // Offset may need adjustment for unsized fields.
R
Ralf Jung 已提交
399
        let (meta, offset) = if field_layout.is_unsized() {
400 401 402
            // Re-use parent metadata to determine dynamic field layout.
            // With custom DSTS, this *will* execute user-defined code, but the same
            // happens at run-time so that's okay.
403 404 405 406 407 408 409
            let align = match self.size_and_align_of(base.meta, field_layout)? {
                Some((_, align)) => align,
                None if offset == Size::ZERO =>
                    // An extern type at offset 0, we fall back to its static alignment.
                    // FIXME: Once we have made decisions for how to handle size and alignment
                    // of `extern type`, this should be adapted.  It is just a temporary hack
                    // to get some code to work that probably ought to work.
410
                    field_layout.align.abi,
411 412 413
                None =>
                    bug!("Cannot compute offset for extern type field at non-0 offset"),
            };
414
            (base.meta, offset.align_to(align))
R
Ralf Jung 已提交
415
        } else {
R
Ralf Jung 已提交
416
            // base.meta could be present; we might be accessing a sized field of an unsized
417 418
            // struct.
            (None, offset)
R
Ralf Jung 已提交
419 420
        };

421 422 423
        // We do not look at `base.layout.align` nor `field_layout.align`, unlike
        // codegen -- mostly to see if we can get away with that
        base.offset(offset, meta, field_layout, self)
O
Oliver Schneider 已提交
424 425
    }

426 427 428 429
    // Iterates over all fields of an array. Much more efficient than doing the
    // same by repeatedly calling `mplace_array`.
    pub fn mplace_array_fields(
        &self,
430
        base: MPlaceTy<'tcx, Tag>,
431
    ) -> InterpResult<'tcx, impl Iterator<Item = InterpResult<'tcx, MPlaceTy<'tcx, Tag>>> + 'tcx>
432
    {
433
        let len = base.len(self)?; // also asserts that we have a type where this makes sense
434 435 436 437 438 439
        let stride = match base.layout.fields {
            layout::FieldPlacement::Array { stride, .. } => stride,
            _ => bug!("mplace_array_fields: expected an array layout"),
        };
        let layout = base.layout.field(self, 0)?;
        let dl = &self.tcx.data_layout;
440
        Ok((0..len).map(move |i| base.offset(i * stride, None, layout, dl)))
441 442
    }

R
Ralf Jung 已提交
443
    pub fn mplace_subslice(
O
Oliver Schneider 已提交
444
        &self,
445
        base: MPlaceTy<'tcx, M::PointerTag>,
R
Ralf Jung 已提交
446 447
        from: u64,
        to: u64,
448
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
449
        let len = base.len(self)?; // also asserts that we have a type where this makes sense
R
Ralf Jung 已提交
450 451 452 453 454 455 456 457
        assert!(from <= len - to);

        // Not using layout method because that works with usize, and does not work with slices
        // (that have count 0 in their layout).
        let from_offset = match base.layout.fields {
            layout::FieldPlacement::Array { stride, .. } =>
                stride * from,
            _ => bug!("Unexpected layout of index access: {:#?}", base.layout),
O
Oliver Schneider 已提交
458
        };
R
Ralf Jung 已提交
459

R
Ralf Jung 已提交
460
        // Compute meta and new layout
R
Ralf Jung 已提交
461
        let inner_len = len - to - from;
R
Ralf Jung 已提交
462
        let (meta, ty) = match base.layout.ty.sty {
R
Ralf Jung 已提交
463 464
            // It is not nice to match on the type, but that seems to be the only way to
            // implement this.
V
varkor 已提交
465
            ty::Array(inner, _) =>
466 467
                (None, self.tcx.mk_array(inner, inner_len)),
            ty::Slice(..) => {
468
                let len = Scalar::from_uint(inner_len, self.pointer_size());
469 470
                (Some(len), base.layout.ty)
            }
R
Ralf Jung 已提交
471 472 473 474
            _ =>
                bug!("cannot subslice non-array type: `{:?}`", base.layout.ty),
        };
        let layout = self.layout_of(ty)?;
475
        base.offset(from_offset, meta, layout, self)
R
Ralf Jung 已提交
476 477 478 479
    }

    pub fn mplace_downcast(
        &self,
480
        base: MPlaceTy<'tcx, M::PointerTag>,
481
        variant: VariantIdx,
482
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
R
Ralf Jung 已提交
483
        // Downcasts only change the layout
R
Ralf Jung 已提交
484
        assert!(base.meta.is_none());
R
Ralf Jung 已提交
485
        Ok(MPlaceTy { layout: base.layout.for_variant(self, variant), ..base })
O
Oliver Schneider 已提交
486 487
    }

R
Ralf Jung 已提交
488 489
    /// Project into an mplace
    pub fn mplace_projection(
O
Oliver Schneider 已提交
490
        &self,
491
        base: MPlaceTy<'tcx, M::PointerTag>,
R
Ralf Jung 已提交
492
        proj_elem: &mir::PlaceElem<'tcx>,
493
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
494
        use rustc::mir::ProjectionElem::*;
R
Ralf Jung 已提交
495 496 497 498 499 500
        Ok(match *proj_elem {
            Field(field, _) => self.mplace_field(base, field.index() as u64)?,
            Downcast(_, variant) => self.mplace_downcast(base, variant)?,
            Deref => self.deref_operand(base.into())?,

            Index(local) => {
R
Ralf Jung 已提交
501
                let layout = self.layout_of(self.tcx.types.usize)?;
502 503
                let n = self.access_local(self.frame(), local, Some(layout))?;
                let n = self.read_scalar(n)?;
504
                let n = self.force_bits(n.not_undef()?, self.tcx.data_layout.pointer_size)?;
R
Ralf Jung 已提交
505 506 507 508 509 510 511 512
                self.mplace_field(base, u64::try_from(n).unwrap())?
            }

            ConstantIndex {
                offset,
                min_length,
                from_end,
            } => {
513
                let n = base.len(self)?;
R
Ralf Jung 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527
                assert!(n >= min_length as u64);

                let index = if from_end {
                    n - u64::from(offset)
                } else {
                    u64::from(offset)
                };

                self.mplace_field(base, index)?
            }

            Subslice { from, to } =>
                self.mplace_subslice(base, u64::from(from), u64::from(to))?,
        })
O
Oliver Schneider 已提交
528 529
    }

A
Alexander Regueiro 已提交
530
    /// Gets the place of a field inside the place, and also the field's type.
R
Ralf Jung 已提交
531
    /// Just a convenience function, but used quite a bit.
532 533
    /// This is the only projection that might have a side-effect: We cannot project
    /// into the field of a local `ScalarPair`, we have to first allocate it.
R
Ralf Jung 已提交
534
    pub fn place_field(
O
Oliver Schneider 已提交
535
        &mut self,
536
        base: PlaceTy<'tcx, M::PointerTag>,
R
Ralf Jung 已提交
537
        field: u64,
538
    ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
R
Ralf Jung 已提交
539 540 541 542
        // FIXME: We could try to be smarter and avoid allocation for fields that span the
        // entire place.
        let mplace = self.force_allocation(base)?;
        Ok(self.mplace_field(mplace, field)?.into())
O
Oliver Schneider 已提交
543 544
    }

R
Ralf Jung 已提交
545
    pub fn place_downcast(
546
        &self,
547
        base: PlaceTy<'tcx, M::PointerTag>,
548
        variant: VariantIdx,
549
    ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
R
Ralf Jung 已提交
550 551 552 553 554
        // Downcast just changes the layout
        Ok(match base.place {
            Place::Ptr(mplace) =>
                self.mplace_downcast(MPlaceTy { mplace, layout: base.layout }, variant)?.into(),
            Place::Local { .. } => {
555
                let layout = base.layout.for_variant(self, variant);
R
Ralf Jung 已提交
556
                PlaceTy { layout, ..base }
O
Oliver Schneider 已提交
557
            }
R
Ralf Jung 已提交
558
        })
O
Oliver Schneider 已提交
559 560
    }

A
Alexander Regueiro 已提交
561
    /// Projects into a place.
R
Ralf Jung 已提交
562 563
    pub fn place_projection(
        &mut self,
564
        base: PlaceTy<'tcx, M::PointerTag>,
565
        proj_elem: &mir::ProjectionElem<mir::Local, Ty<'tcx>>,
566
    ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
R
Ralf Jung 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580
        use rustc::mir::ProjectionElem::*;
        Ok(match *proj_elem {
            Field(field, _) =>  self.place_field(base, field.index() as u64)?,
            Downcast(_, variant) => self.place_downcast(base, variant)?,
            Deref => self.deref_operand(self.place_to_op(base)?)?.into(),
            // For the other variants, we have to force an allocation.
            // This matches `operand_projection`.
            Subslice { .. } | ConstantIndex { .. } | Index(_) => {
                let mplace = self.force_allocation(base)?;
                self.mplace_projection(mplace, proj_elem)?.into()
            }
        })
    }

A
Alexander Regueiro 已提交
581
    /// Evaluate statics and promoteds to an `MPlace`. Used to share some code between
582
    /// `eval_place` and `eval_place_to_op`.
583
    pub(super) fn eval_static_to_mplace(
584
        &self,
585
        place_static: &mir::Static<'tcx>
586
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
587 588 589
        use rustc::mir::StaticKind;

        Ok(match place_static.kind {
590 591 592
            StaticKind::Promoted(promoted, promoted_substs) => {
                let substs = self.subst_from_frame_and_normalize_erasing_regions(promoted_substs);
                let instance = ty::Instance::new(place_static.def_id, substs);
S
Saleem Jaffer 已提交
593 594 595 596 597 598
                self.const_eval_raw(GlobalId {
                    instance,
                    promoted: Some(promoted),
                })?
            }

W
Wesley Wiser 已提交
599
            StaticKind::Static => {
600
                let ty = place_static.ty;
S
Saleem Jaffer 已提交
601 602
                assert!(!ty.needs_subst());
                let layout = self.layout_of(ty)?;
W
Wesley Wiser 已提交
603
                let instance = ty::Instance::mono(*self.tcx, place_static.def_id);
S
Saleem Jaffer 已提交
604 605 606 607 608
                let cid = GlobalId {
                    instance,
                    promoted: None
                };
                // Just create a lazy reference, so we can support recursive statics.
609
                // tcx takes care of assigning every static one and only one unique AllocId.
S
Saleem Jaffer 已提交
610 611 612 613 614
                // When the data here is ever actually used, memory will notice,
                // and it knows how to deal with alloc_id that are present in the
                // global table but not in its local memory: It calls back into tcx through
                // a query, triggering the CTFE machinery to actually turn this lazy reference
                // into a bunch of bytes.  IOW, statics are evaluated with CTFE even when
R
Ralf Jung 已提交
615
                // this InterpCx uses another Machine (e.g., in miri).  This is what we
616
                // want!  This way, computing statics works consistently between codegen
S
Saleem Jaffer 已提交
617 618
                // and miri: They use the same query to eventually obtain a `ty::Const`
                // and use that for further computation.
619 620 621 622 623 624 625
                //
                // Notice that statics have *two* AllocIds: the lazy one, and the resolved
                // one.  Here we make sure that the interpreted program never sees the
                // resolved ID.  Also see the doc comment of `Memory::get_static_alloc`.
                let alloc_id = self.tcx.alloc_map.lock().create_static_alloc(cid.instance.def_id());
                let ptr = self.tag_static_base_pointer(Pointer::from(alloc_id));
                MPlaceTy::from_aligned_ptr(ptr, layout)
O
Oliver Schneider 已提交
626
            }
627 628 629
        })
    }

A
Alexander Regueiro 已提交
630
    /// Computes a place. You should only use this if you intend to write into this
631
    /// place; for reading, a more efficient alternative is `eval_place_for_read`.
R
Ralf Jung 已提交
632 633
    pub fn eval_place(
        &mut self,
634
        place: &mir::Place<'tcx>,
635
    ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
636
        use rustc::mir::PlaceBase;
637

638 639 640 641 642 643 644 645 646 647 648 649
        let mut place_ty = match &place.base {
            PlaceBase::Local(mir::RETURN_PLACE) => match self.frame().return_place {
                Some(return_place) => {
                    // We use our layout to verify our assumption; caller will validate
                    // their layout on return.
                    PlaceTy {
                        place: *return_place,
                        layout: self.layout_of(
                            self.subst_from_frame_and_normalize_erasing_regions(
                                self.frame().body.return_ty()
                            )
                        )?,
650
                    }
651 652 653 654 655 656 657 658
                }
                None => throw_unsup!(InvalidNullPointerUsage),
            },
            PlaceBase::Local(local) => PlaceTy {
                // This works even for dead/uninitialized locals; we check further when writing
                place: Place::Local {
                    frame: self.cur_frame(),
                    local: *local,
659
                },
660 661 662 663
                layout: self.layout_of_local(self.frame(), *local, None)?,
            },
            PlaceBase::Static(place_static) => self.eval_static_to_mplace(&place_static)?.into(),
        };
664

665 666 667
        for elem in place.projection.iter() {
            place_ty = self.place_projection(place_ty, elem)?
        }
O
Oliver Schneider 已提交
668

669 670
        self.dump_place(place_ty.place);
        Ok(place_ty)
O
Oliver Schneider 已提交
671 672
    }

R
Ralf Jung 已提交
673 674
    /// Write a scalar to a place
    pub fn write_scalar(
O
Oliver Schneider 已提交
675
        &mut self,
676 677
        val: impl Into<ScalarMaybeUndef<M::PointerTag>>,
        dest: PlaceTy<'tcx, M::PointerTag>,
678
    ) -> InterpResult<'tcx> {
679
        self.write_immediate(Immediate::Scalar(val.into()), dest)
R
Ralf Jung 已提交
680
    }
O
Oliver Schneider 已提交
681

682
    /// Write an immediate to a place
683
    #[inline(always)]
684
    pub fn write_immediate(
R
Ralf Jung 已提交
685
        &mut self,
686
        src: Immediate<M::PointerTag>,
687
        dest: PlaceTy<'tcx, M::PointerTag>,
688
    ) -> InterpResult<'tcx> {
689
        self.write_immediate_no_validate(src, dest)?;
690

R
Ralf Jung 已提交
691
        if M::enforce_validity(self) {
692
            // Data got changed, better make sure it matches the type!
693
            self.validate_operand(self.place_to_op(dest)?, vec![], None)?;
694 695
        }

696 697 698
        Ok(())
    }

W
Wesley Wiser 已提交
699 700 701 702 703 704
    /// Write an `Immediate` to memory.
    #[inline(always)]
    pub fn write_immediate_to_mplace(
        &mut self,
        src: Immediate<M::PointerTag>,
        dest: MPlaceTy<'tcx, M::PointerTag>,
R
Ralf Jung 已提交
705
    ) -> InterpResult<'tcx> {
W
Wesley Wiser 已提交
706 707 708 709
        self.write_immediate_to_mplace_no_validate(src, dest)?;

        if M::enforce_validity(self) {
            // Data got changed, better make sure it matches the type!
O
Oliver Scherer 已提交
710
            self.validate_operand(dest.into(), vec![], None)?;
W
Wesley Wiser 已提交
711 712 713 714 715
        }

        Ok(())
    }

716
    /// Write an immediate to a place.
717 718
    /// If you use this you are responsible for validating that things got copied at the
    /// right type.
719
    fn write_immediate_no_validate(
720
        &mut self,
721
        src: Immediate<M::PointerTag>,
722
        dest: PlaceTy<'tcx, M::PointerTag>,
723
    ) -> InterpResult<'tcx> {
724 725 726
        if cfg!(debug_assertions) {
            // This is a very common path, avoid some checks in release mode
            assert!(!dest.layout.is_unsized(), "Cannot write unsized data");
727 728
            match src {
                Immediate::Scalar(ScalarMaybeUndef::Scalar(Scalar::Ptr(_))) =>
729 730
                    assert_eq!(self.pointer_size(), dest.layout.size,
                        "Size mismatch when writing pointer"),
731
                Immediate::Scalar(ScalarMaybeUndef::Scalar(Scalar::Raw { size, .. })) =>
732 733
                    assert_eq!(Size::from_bytes(size.into()), dest.layout.size,
                        "Size mismatch when writing bits"),
734 735
                Immediate::Scalar(ScalarMaybeUndef::Undef) => {}, // undef can have any size
                Immediate::ScalarPair(_, _) => {
736 737 738 739
                    // FIXME: Can we check anything here?
                }
            }
        }
740
        trace!("write_immediate: {:?} <- {:?}: {}", *dest, src, dest.layout.ty);
741

742
        // See if we can avoid an allocation. This is the counterpart to `try_read_immediate`,
R
Ralf Jung 已提交
743
        // but not factored as a separate function.
R
Ralf Jung 已提交
744
        let mplace = match dest.place {
O
Oliver Schneider 已提交
745
            Place::Local { frame, local } => {
746 747 748 749
                match self.stack[frame].locals[local].access_mut()? {
                    Ok(local) => {
                        // Local can be updated in-place.
                        *local = LocalValue::Live(Operand::Immediate(src));
R
Ralf Jung 已提交
750
                        return Ok(());
751 752 753 754 755
                    }
                    Err(mplace) => {
                        // The local is in memory, go on below.
                        mplace
                    }
O
Oliver Schneider 已提交
756
                }
R
Ralf Jung 已提交
757
            },
758
            Place::Ptr(mplace) => mplace, // already referring to memory
O
Oliver Schneider 已提交
759
        };
760
        let dest = MPlaceTy { mplace, layout: dest.layout };
O
Oliver Schneider 已提交
761

R
Ralf Jung 已提交
762
        // This is already in memory, write there.
763
        self.write_immediate_to_mplace_no_validate(src, dest)
O
Oliver Schneider 已提交
764 765
    }

766
    /// Write an immediate to memory.
767
    /// If you use this you are responsible for validating that things got copied at the
768
    /// right type.
769
    fn write_immediate_to_mplace_no_validate(
R
Ralf Jung 已提交
770
        &mut self,
771
        value: Immediate<M::PointerTag>,
772
        dest: MPlaceTy<'tcx, M::PointerTag>,
773
    ) -> InterpResult<'tcx> {
B
Bernardo Meurer 已提交
774 775 776 777
        // Note that it is really important that the type here is the right one, and matches the
        // type things are read at. In case `src_val` is a `ScalarPair`, we don't do any magic here
        // to handle padding properly, which is only correct if we never look at this data with the
        // wrong type.
778

779 780 781
        let ptr = match self.check_mplace_access(dest, None)
            .expect("places should be checked on creation")
        {
782 783 784
            Some(ptr) => ptr,
            None => return Ok(()), // zero-sized access
        };
785

786
        let tcx = &*self.tcx;
787 788 789
        // FIXME: We should check that there are dest.layout.size many bytes available in
        // memory.  The code below is not sufficient, with enough padding it might not
        // cover all the bytes!
R
Ralf Jung 已提交
790
        match value {
791
            Immediate::Scalar(scalar) => {
792 793
                match dest.layout.abi {
                    layout::Abi::Scalar(_) => {}, // fine
794
                    _ => bug!("write_immediate_to_mplace: invalid Scalar layout: {:#?}",
795 796
                            dest.layout)
                }
797
                self.memory.get_mut(ptr.alloc_id)?.write_scalar(
798
                    tcx, ptr, scalar, dest.layout.size
R
Ralf Jung 已提交
799
                )
O
Oliver Schneider 已提交
800
            }
801
            Immediate::ScalarPair(a_val, b_val) => {
802 803 804
                // We checked `ptr_align` above, so all fields will have the alignment they need.
                // We would anyway check against `ptr_align.restrict_for_offset(b_offset)`,
                // which `ptr.offset(b_offset)` cannot possibly fail to satisfy.
R
Ralf Jung 已提交
805 806
                let (a, b) = match dest.layout.abi {
                    layout::Abi::ScalarPair(ref a, ref b) => (&a.value, &b.value),
807
                    _ => bug!("write_immediate_to_mplace: invalid ScalarPair layout: {:#?}",
B
Bernardo Meurer 已提交
808
                              dest.layout)
R
Ralf Jung 已提交
809
                };
810
                let (a_size, b_size) = (a.size(self), b.size(self));
811
                let b_offset = a_size.align_to(b.align(self).abi);
812
                let b_ptr = ptr.offset(b_offset, self)?;
813

814 815 816 817
                // It is tempting to verify `b_offset` against `layout.fields.offset(1)`,
                // but that does not work: We could be a newtype around a pair, then the
                // fields do not match the `ScalarPair` components.

818 819
                self.memory
                    .get_mut(ptr.alloc_id)?
820
                    .write_scalar(tcx, ptr, a_val, a_size)?;
821 822
                self.memory
                    .get_mut(b_ptr.alloc_id)?
823
                    .write_scalar(tcx, b_ptr, b_val, b_size)
O
Oliver Schneider 已提交
824
            }
R
Ralf Jung 已提交
825
        }
O
Oliver Schneider 已提交
826 827
    }

A
Alexander Regueiro 已提交
828
    /// Copies the data from an operand to a place. This does not support transmuting!
829 830
    /// Use `copy_op_transmute` if the layouts could disagree.
    #[inline(always)]
R
Ralf Jung 已提交
831
    pub fn copy_op(
O
Oliver Schneider 已提交
832
        &mut self,
833 834
        src: OpTy<'tcx, M::PointerTag>,
        dest: PlaceTy<'tcx, M::PointerTag>,
835
    ) -> InterpResult<'tcx> {
836 837
        self.copy_op_no_validate(src, dest)?;

R
Ralf Jung 已提交
838
        if M::enforce_validity(self) {
839
            // Data got changed, better make sure it matches the type!
840
            self.validate_operand(self.place_to_op(dest)?, vec![], None)?;
841 842 843 844 845
        }

        Ok(())
    }

A
Alexander Regueiro 已提交
846
    /// Copies the data from an operand to a place. This does not support transmuting!
847
    /// Use `copy_op_transmute` if the layouts could disagree.
848
    /// Also, if you use this you are responsible for validating that things get copied at the
849 850 851 852 853
    /// right type.
    fn copy_op_no_validate(
        &mut self,
        src: OpTy<'tcx, M::PointerTag>,
        dest: PlaceTy<'tcx, M::PointerTag>,
854
    ) -> InterpResult<'tcx> {
855 856 857 858
        // We do NOT compare the types for equality, because well-typed code can
        // actually "transmute" `&mut T` to `&T` in an assignment without a cast.
        assert!(src.layout.details == dest.layout.details,
            "Layout mismatch when copying!\nsrc: {:#?}\ndest: {:#?}", src, dest);
R
Ralf Jung 已提交
859 860

        // Let us see if the layout is simple so we take a shortcut, avoid force_allocation.
861
        let src = match self.try_read_immediate(src)? {
862
            Ok(src_val) => {
863
                assert!(!src.layout.is_unsized(), "cannot have unsized immediates");
864
                // Yay, we got a value that we can write directly.
865 866
                // FIXME: Add a check to make sure that if `src` is indirect,
                // it does not overlap with `dest`.
867
                return self.write_immediate_no_validate(*src_val, dest);
868 869
            }
            Err(mplace) => mplace,
R
Ralf Jung 已提交
870 871
        };
        // Slow path, this does not fit into an immediate. Just memcpy.
872 873
        trace!("copy_op: {:?} <- {:?}: {}", *dest, src, dest.layout.ty);

874 875 876 877 878 879 880 881 882
        // This interprets `src.meta` with the `dest` local's layout, if an unsized local
        // is being initialized!
        let (dest, size) = self.force_allocation_maybe_sized(dest, src.meta)?;
        let size = size.unwrap_or_else(|| {
            assert!(!dest.layout.is_unsized(),
                "Cannot copy into already initialized unsized place");
            dest.layout.size
        });
        assert_eq!(src.meta, dest.meta, "Can only copy between equally-sized instances");
883

884 885 886 887
        let src = self.check_mplace_access(src, Some(size))
            .expect("places should be checked on creation");
        let dest = self.check_mplace_access(dest, Some(size))
            .expect("places should be checked on creation");
888 889 890 891 892 893
        let (src_ptr, dest_ptr) = match (src, dest) {
            (Some(src_ptr), Some(dest_ptr)) => (src_ptr, dest_ptr),
            (None, None) => return Ok(()), // zero-sized copy
            _ => bug!("The pointers should both be Some or both None"),
        };

R
Ralf Jung 已提交
894
        self.memory.copy(
895 896
            src_ptr,
            dest_ptr,
897
            size,
898
            /*nonoverlapping*/ true,
899
        )
900 901
    }

A
Alexander Regueiro 已提交
902
    /// Copies the data from an operand to a place. The layouts may disagree, but they must
903 904 905 906 907
    /// have the same size.
    pub fn copy_op_transmute(
        &mut self,
        src: OpTy<'tcx, M::PointerTag>,
        dest: PlaceTy<'tcx, M::PointerTag>,
908
    ) -> InterpResult<'tcx> {
909 910 911 912
        if src.layout.details == dest.layout.details {
            // Fast path: Just use normal `copy_op`
            return self.copy_op(src, dest);
        }
913
        // We still require the sizes to match.
914 915
        assert!(src.layout.size == dest.layout.size,
            "Size mismatch when transmuting!\nsrc: {:#?}\ndest: {:#?}", src, dest);
916 917 918 919
        // Unsized copies rely on interpreting `src.meta` with `dest.layout`, we want
        // to avoid that here.
        assert!(!src.layout.is_unsized() && !dest.layout.is_unsized(),
            "Cannot transmute unsized data");
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934

        // The hard case is `ScalarPair`.  `src` is already read from memory in this case,
        // using `src.layout` to figure out which bytes to use for the 1st and 2nd field.
        // We have to write them to `dest` at the offsets they were *read at*, which is
        // not necessarily the same as the offsets in `dest.layout`!
        // Hence we do the copy with the source layout on both sides.  We also make sure to write
        // into memory, because if `dest` is a local we would not even have a way to write
        // at the `src` offsets; the fact that we came from a different layout would
        // just be lost.
        let dest = self.force_allocation(dest)?;
        self.copy_op_no_validate(
            src,
            PlaceTy::from(MPlaceTy { mplace: *dest, layout: src.layout }),
        )?;

R
Ralf Jung 已提交
935
        if M::enforce_validity(self) {
936
            // Data got changed, better make sure it matches the type!
937
            self.validate_operand(dest.into(), vec![], None)?;
938
        }
939

940
        Ok(())
O
Oliver Schneider 已提交
941 942
    }

A
Alexander Regueiro 已提交
943
    /// Ensures that a place is in memory, and returns where it is.
944 945
    /// If the place currently refers to a local that doesn't yet have a matching allocation,
    /// create such an allocation.
R
Ralf Jung 已提交
946
    /// This is essentially `force_to_memplace`.
947
    ///
R
Ralf Jung 已提交
948
    /// This supports unsized types and returns the computed size to avoid some
949 950 951
    /// redundant computation when copying; use `force_allocation` for a simpler, sized-only
    /// version.
    pub fn force_allocation_maybe_sized(
O
Oliver Schneider 已提交
952
        &mut self,
953
        place: PlaceTy<'tcx, M::PointerTag>,
954
        meta: Option<Scalar<M::PointerTag>>,
955
    ) -> InterpResult<'tcx, (MPlaceTy<'tcx, M::PointerTag>, Option<Size>)> {
956
        let (mplace, size) = match place.place {
R
Ralf Jung 已提交
957
            Place::Local { frame, local } => {
958 959
                match self.stack[frame].locals[local].access_mut()? {
                    Ok(local_val) => {
960 961 962 963
                        // We need to make an allocation.
                        // FIXME: Consider not doing anything for a ZST, and just returning
                        // a fake pointer?  Are we even called for ZST?

964 965 966 967 968 969 970 971 972
                        // We cannot hold on to the reference `local_val` while allocating,
                        // but we can hold on to the value in there.
                        let old_val =
                            if let LocalValue::Live(Operand::Immediate(value)) = *local_val {
                                Some(value)
                            } else {
                                None
                            };

973
                        // We need the layout of the local.  We can NOT use the layout we got,
974
                        // that might e.g., be an inner field of a struct with `Scalar` layout,
R
Ralf Jung 已提交
975
                        // that has different alignment than the outer field.
976
                        // We also need to support unsized types, and hence cannot use `allocate`.
977
                        let local_layout = self.layout_of_local(&self.stack[frame], local, None)?;
978 979 980 981
                        let (size, align) = self.size_and_align_of(meta, local_layout)?
                            .expect("Cannot allocate for non-dyn-sized type");
                        let ptr = self.memory.allocate(size, align, MemoryKind::Stack);
                        let mplace = MemPlace { ptr: ptr.into(), align, meta };
982 983 984 985
                        if let Some(value) = old_val {
                            // Preserve old value.
                            // We don't have to validate as we can assume the local
                            // was already valid for its type.
986 987
                            let mplace = MPlaceTy { mplace, layout: local_layout };
                            self.write_immediate_to_mplace_no_validate(value, mplace)?;
988 989 990 991 992
                        }
                        // Now we can call `access_mut` again, asserting it goes well,
                        // and actually overwrite things.
                        *self.stack[frame].locals[local].access_mut().unwrap().unwrap() =
                            LocalValue::Live(Operand::Indirect(mplace));
993
                        (mplace, Some(size))
994
                    }
995
                    Err(mplace) => (mplace, None), // this already was an indirect local
996
                }
R
Ralf Jung 已提交
997
            }
998
            Place::Ptr(mplace) => (mplace, None)
R
Ralf Jung 已提交
999 1000
        };
        // Return with the original layout, so that the caller can go on
1001 1002 1003 1004 1005 1006 1007
        Ok((MPlaceTy { mplace, layout: place.layout }, size))
    }

    #[inline(always)]
    pub fn force_allocation(
        &mut self,
        place: PlaceTy<'tcx, M::PointerTag>,
1008
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
1009
        Ok(self.force_allocation_maybe_sized(place, None)?.0)
O
Oliver Schneider 已提交
1010 1011
    }

R
Ralf Jung 已提交
1012
    pub fn allocate(
O
Oliver Schneider 已提交
1013
        &mut self,
R
Ralf Jung 已提交
1014 1015
        layout: TyLayout<'tcx>,
        kind: MemoryKind<M::MemoryKinds>,
R
Ralf Jung 已提交
1016
    ) -> MPlaceTy<'tcx, M::PointerTag> {
1017 1018
        let ptr = self.memory.allocate(layout.size, layout.align.abi, kind);
        MPlaceTy::from_aligned_ptr(ptr, layout)
R
Ralf Jung 已提交
1019
    }
O
Oliver Schneider 已提交
1020

1021
    pub fn write_discriminant_index(
R
Ralf Jung 已提交
1022
        &mut self,
1023
        variant_index: VariantIdx,
1024
        dest: PlaceTy<'tcx, M::PointerTag>,
1025
    ) -> InterpResult<'tcx> {
R
Ralf Jung 已提交
1026 1027
        match dest.layout.variants {
            layout::Variants::Single { index } => {
R
Ralf Jung 已提交
1028
                assert_eq!(index, variant_index);
O
Oliver Schneider 已提交
1029
            }
1030 1031
            layout::Variants::Multiple {
                discr_kind: layout::DiscriminantKind::Tag,
1032
                discr: ref discr_layout,
1033
                discr_index,
1034 1035
                ..
            } => {
1036 1037 1038
                assert!(dest.layout.ty.variant_range(*self.tcx).unwrap().contains(&variant_index));
                let discr_val =
                    dest.layout.ty.discriminant_for_variant(*self.tcx, variant_index).unwrap().val;
R
Ralf Jung 已提交
1039 1040 1041 1042

                // raw discriminants for enums are isize or bigger during
                // their computation, but the in-memory tag is the smallest possible
                // representation
1043
                let size = discr_layout.value.size(self);
1044
                let discr_val = truncate(discr_val, size);
R
Ralf Jung 已提交
1045

1046
                let discr_dest = self.place_field(dest, discr_index as u64)?;
1047
                self.write_scalar(Scalar::from_uint(discr_val, size), discr_dest)?;
O
Oliver Schneider 已提交
1048
            }
1049 1050 1051 1052 1053 1054
            layout::Variants::Multiple {
                discr_kind: layout::DiscriminantKind::Niche {
                    dataful_variant,
                    ref niche_variants,
                    niche_start,
                },
1055
                discr: ref discr_layout,
1056
                discr_index,
R
Ralf Jung 已提交
1057
                ..
O
Oliver Schneider 已提交
1058
            } => {
1059 1060 1061
                assert!(
                    variant_index.as_usize() < dest.layout.ty.ty_adt_def().unwrap().variants.len(),
                );
R
Ralf Jung 已提交
1062
                if variant_index != dataful_variant {
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
                    // FIXME: WTF, some discriminants don't have integer type.
                    use layout::Primitive;
                    let discr_layout = self.layout_of(match discr_layout.value {
                        Primitive::Int(int, signed) => int.to_ty(*self.tcx, signed),
                        Primitive::Pointer => self.tcx.types.usize,
                        Primitive::Float(..) => bug!("there are no float discriminants"),
                    })?;

                    // We need to use machine arithmetic.
                    let variants_start = niche_variants.start().as_u32();
                    let variants_start_val = ImmTy::from_uint(variants_start, discr_layout);
                    let niche_start_val = ImmTy::from_uint(niche_start, discr_layout);
                    let variant_index_val = ImmTy::from_uint(variant_index.as_u32(), discr_layout);
                    let niche_val = self.binary_op(
                        mir::BinOp::Sub,
                        variant_index_val,
                        variants_start_val,
                    )?;
                    let niche_val = self.binary_op(
                        mir::BinOp::Add,
                        niche_val,
                        niche_start_val,
1085
                    )?;
1086 1087 1088
                    // Write result.
                    let niche_dest = self.place_field(dest, discr_index as u64)?;
                    self.write_immediate(*niche_val, niche_dest)?;
R
Ralf Jung 已提交
1089
                }
O
Oliver Schneider 已提交
1090
            }
1091
        }
R
Ralf Jung 已提交
1092 1093

        Ok(())
O
Oliver Schneider 已提交
1094 1095
    }

1096 1097 1098
    pub fn raw_const_to_mplace(
        &self,
        raw: RawConst<'tcx>,
1099
    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
1100 1101
        // This must be an allocation in `tcx`
        assert!(self.tcx.alloc_map.lock().get(raw.alloc_id).is_some());
1102
        let ptr = self.tag_static_base_pointer(Pointer::from(raw.alloc_id));
1103
        let layout = self.layout_of(raw.ty)?;
1104
        Ok(MPlaceTy::from_aligned_ptr(ptr, layout))
1105 1106
    }

1107 1108
    /// Turn a place with a `dyn Trait` type into a place with the actual dynamic type.
    /// Also return some more information so drop doesn't have to run the same code twice.
1109
    pub(super) fn unpack_dyn_trait(&self, mplace: MPlaceTy<'tcx, M::PointerTag>)
1110
    -> InterpResult<'tcx, (ty::Instance<'tcx>, MPlaceTy<'tcx, M::PointerTag>)> {
1111
        let vtable = mplace.vtable(); // also sanity checks the type
1112 1113 1114 1115
        let (instance, ty) = self.read_drop_type_from_vtable(vtable)?;
        let layout = self.layout_of(ty)?;

        // More sanity checks
1116 1117 1118
        if cfg!(debug_assertions) {
            let (size, align) = self.read_size_and_align_from_vtable(vtable)?;
            assert_eq!(size, layout.size);
1119
            // only ABI alignment is preserved
1120
            assert_eq!(align, layout.align.abi);
1121
        }
1122 1123

        let mplace = MPlaceTy {
R
Ralf Jung 已提交
1124
            mplace: MemPlace { meta: None, ..*mplace },
1125
            layout
1126 1127
        };
        Ok((instance, mplace))
1128
    }
O
Oliver Schneider 已提交
1129
}