place.rs 28.7 KB
Newer Older
R
Ralf Jung 已提交
1 2 3 4 5 6 7
//! 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::hash::{Hash, Hasher};
use std::convert::TryFrom;

8
use rustc::mir;
R
Ralf Jung 已提交
9
use rustc::ty::{self, Ty};
10
use rustc::ty::layout::{self, Size, Align, LayoutOf, TyLayout, HasDataLayout};
O
Oliver Schneider 已提交
11 12
use rustc_data_structures::indexed_vec::Idx;

R
Ralf Jung 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26
use rustc::mir::interpret::{
    GlobalId, Scalar, EvalResult, Pointer, ScalarMaybeUndef
};
use super::{EvalContext, Machine, Value, ValTy, Operand, OpTy, MemoryKind};

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct MemPlace {
    /// 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.
    pub ptr: Scalar,
    pub align: Align,
    pub extra: PlaceExtra,
}
O
Oliver Schneider 已提交
27

28
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
O
Oliver Schneider 已提交
29
pub enum Place {
30
    /// A place referring to a value allocated in the `Memory` system.
R
Ralf Jung 已提交
31
    Ptr(MemPlace),
O
Oliver Schneider 已提交
32

R
Ralf Jung 已提交
33 34 35 36 37 38
    /// 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 已提交
39 40
}

R
Ralf Jung 已提交
41
// Extra information for fat pointers / places
42
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
O
Oliver Schneider 已提交
43 44 45
pub enum PlaceExtra {
    None,
    Length(u64),
46
    Vtable(Pointer),
O
Oliver Schneider 已提交
47 48
}

R
Ralf Jung 已提交
49 50 51 52 53 54 55 56
#[derive(Copy, Clone, Debug)]
pub struct PlaceTy<'tcx> {
    place: Place,
    pub layout: TyLayout<'tcx>,
}

impl<'tcx> ::std::ops::Deref for PlaceTy<'tcx> {
    type Target = Place;
57
    #[inline(always)]
R
Ralf Jung 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71
    fn deref(&self) -> &Place {
        &self.place
    }
}

/// A MemPlace with its layout. Constructing it is only possible in this module.
#[derive(Copy, Clone, Debug)]
pub struct MPlaceTy<'tcx> {
    mplace: MemPlace,
    pub layout: TyLayout<'tcx>,
}

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

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

R
Ralf Jung 已提交
88 89 90 91
impl MemPlace {
    #[inline(always)]
    pub fn from_scalar_ptr(ptr: Scalar, align: Align) -> Self {
        MemPlace {
92 93
            ptr,
            align,
O
Oliver Schneider 已提交
94 95 96 97
            extra: PlaceExtra::None,
        }
    }

R
Ralf Jung 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    #[inline(always)]
    pub fn from_ptr(ptr: Pointer, align: Align) -> Self {
        Self::from_scalar_ptr(ptr.into(), align)
    }

    #[inline(always)]
    pub fn to_scalar_ptr_align(self) -> (Scalar, Align) {
        assert_eq!(self.extra, PlaceExtra::None);
        (self.ptr, self.align)
    }

    /// Extract the ptr part of the mplace
    #[inline(always)]
    pub fn to_ptr(self) -> EvalResult<'tcx, Pointer> {
        // At this point, we forget about the alignment information -- the place has been turned into a reference,
        // and no matter where it came from, it now must be aligned.
        self.to_scalar_ptr_align().0.to_ptr()
    }

    /// 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`.
    pub fn to_ref(self, cx: impl HasDataLayout) -> Value {
        // We ignore the alignment of the place here -- special handling for packed structs ends
        // at the `&` operator.
        match self.extra {
            PlaceExtra::None => Value::Scalar(self.ptr.into()),
            PlaceExtra::Length(len) => Value::new_slice(self.ptr.into(), len, cx),
            PlaceExtra::Vtable(vtable) => Value::new_dyn_trait(self.ptr.into(), vtable),
        }
    }
}

impl<'tcx> MPlaceTy<'tcx> {
    #[inline]
    fn from_aligned_ptr(ptr: Pointer, layout: TyLayout<'tcx>) -> Self {
        MPlaceTy { mplace: MemPlace::from_ptr(ptr, layout.align), layout }
    }

    #[inline]
    pub(super) fn len(self) -> u64 {
        // Sanity check
        let ty_len = match self.layout.fields {
            layout::FieldPlacement::Array { count, .. } => count,
            _ => bug!("Length for non-array layout {:?} requested", self.layout),
        };
        if let PlaceExtra::Length(len) = self.extra {
            len
        } else {
            ty_len
        }
    }
}

// Validation needs to hash MPlaceTy, but we cannot hash Layout -- so we just hash the type
impl<'tcx> Hash for MPlaceTy<'tcx> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.mplace.hash(state);
        self.layout.ty.hash(state);
    }
}
impl<'tcx> PartialEq for MPlaceTy<'tcx> {
    fn eq(&self, other: &Self) -> bool {
        self.mplace == other.mplace && self.layout.ty == other.layout.ty
    }
}
impl<'tcx> Eq for MPlaceTy<'tcx> {}

impl<'tcx> OpTy<'tcx> {
166
    #[inline(always)]
R
Ralf Jung 已提交
167 168 169 170 171 172 173
    pub fn try_as_mplace(self) -> Result<MPlaceTy<'tcx>, Value> {
        match *self {
            Operand::Indirect(mplace) => Ok(MPlaceTy { mplace, layout: self.layout }),
            Operand::Immediate(value) => Err(value),
        }
    }

174
    #[inline(always)]
R
Ralf Jung 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    pub fn to_mem_place(self) -> MPlaceTy<'tcx> {
        self.try_as_mplace().unwrap()
    }
}

impl<'tcx> Place {
    /// Produces a Place that will error if attempted to be read from or written to
    #[inline]
    pub fn null(cx: impl HasDataLayout) -> Self {
        Self::from_scalar_ptr(Scalar::ptr_null(cx), Align::from_bytes(1, 1).unwrap())
    }

    #[inline]
    pub fn from_scalar_ptr(ptr: Scalar, align: Align) -> Self {
        Place::Ptr(MemPlace::from_scalar_ptr(ptr, align))
    }

    #[inline]
193
    pub fn from_ptr(ptr: Pointer, align: Align) -> Self {
R
Ralf Jung 已提交
194
        Place::Ptr(MemPlace::from_ptr(ptr, align))
O
Oliver Schneider 已提交
195 196
    }

R
Ralf Jung 已提交
197 198
    #[inline]
    pub fn to_mem_place(self) -> MemPlace {
O
Oliver Schneider 已提交
199
        match self {
R
Ralf Jung 已提交
200 201
            Place::Ptr(mplace) => mplace,
            _ => bug!("to_mem_place: expected Place::Ptr, got {:?}", self),
O
Oliver Schneider 已提交
202 203 204 205

        }
    }

R
Ralf Jung 已提交
206 207 208
    #[inline]
    pub fn to_scalar_ptr_align(self) -> (Scalar, Align) {
        self.to_mem_place().to_scalar_ptr_align()
209
    }
210

R
Ralf Jung 已提交
211
    #[inline]
212
    pub fn to_ptr(self) -> EvalResult<'tcx, Pointer> {
R
Ralf Jung 已提交
213 214 215
        self.to_mem_place().to_ptr()
    }
}
O
Oliver Schneider 已提交
216

R
Ralf Jung 已提交
217 218 219 220 221 222 223 224 225 226
impl<'tcx> PlaceTy<'tcx> {
    /// Produces a Place that will error if attempted to be read from or written to
    #[inline]
    pub fn null(cx: impl HasDataLayout, layout: TyLayout<'tcx>) -> Self {
        PlaceTy { place: Place::from_scalar_ptr(Scalar::ptr_null(cx), layout.align), layout }
    }

    #[inline]
    pub fn to_mem_place(self) -> MPlaceTy<'tcx> {
        MPlaceTy { mplace: self.place.to_mem_place(), layout: self.layout }
O
Oliver Schneider 已提交
227 228 229
    }
}

O
Oliver Schneider 已提交
230
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
R
Ralf Jung 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    /// Take a value, which represents a (thin or fat) reference, and make it a place.
    /// Alignment is just based on the type.  This is the inverse of `MemPlace::to_ref`.
    pub fn ref_to_mplace(
        &self, val: ValTy<'tcx>
    ) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
        let pointee_type = val.layout.ty.builtin_deref(true).unwrap().ty;
        let layout = self.layout_of(pointee_type)?;
        let mplace = match self.tcx.struct_tail(pointee_type).sty {
            ty::TyDynamic(..) => {
                let (ptr, vtable) = val.to_scalar_dyn_trait()?;
                MemPlace {
                    ptr,
                    align: layout.align,
                    extra: PlaceExtra::Vtable(vtable),
                }
            }
            ty::TyStr | ty::TySlice(_) => {
                let (ptr, len) = val.to_scalar_slice(self)?;
                MemPlace {
                    ptr,
                    align: layout.align,
                    extra: PlaceExtra::Length(len),
                }
            }
            _ => MemPlace {
                ptr: val.to_scalar()?,
                align: layout.align,
                extra: PlaceExtra::None,
            },
        };
        Ok(MPlaceTy { mplace, layout })
    }

    /// 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.
    /// This supports both struct and array fields.
    #[inline(always)]
    pub fn mplace_field(
O
Oliver Schneider 已提交
269
        &self,
R
Ralf Jung 已提交
270 271 272 273 274 275 276 277 278 279 280 281
        base: MPlaceTy<'tcx>,
        field: u64,
    ) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
        // 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, .. } => {
                let len = base.len();
                assert!(field < len, "Tried to access element {} of array/slice with length {}", field, len);
                stride * field
            }
282 283 284 285 286
            layout::FieldPlacement::Union(count) => {
                assert!(field < count as u64, "Tried to access field {} of union with {} fields", field, count);
                // Offset is always 0
                Size::from_bytes(0)
            }
R
Ralf Jung 已提交
287 288 289
        };
        // 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 已提交
290
        let field_layout = base.layout.field(self, usize::try_from(field).unwrap_or(0))?;
R
Ralf Jung 已提交
291 292

        // Adjust offset
R
Ralf Jung 已提交
293 294 295 296 297 298 299 300 301 302 303
        let offset = match base.extra {
            PlaceExtra::Vtable(vtable) => {
                let (_, align) = self.read_size_and_align_from_vtable(vtable)?;
                // FIXME: Is this right? Should we always do this, or only when actually
                // accessing the field to which the vtable applies?
                offset.abi_align(align)
            }
            _ => {
                // No adjustment needed
                offset
            }
R
Ralf Jung 已提交
304 305 306
        };

        let ptr = base.ptr.ptr_offset(offset, self)?;
R
Ralf Jung 已提交
307 308
        let align = base.align.min(field_layout.align);
        let extra = if !field_layout.is_unsized() {
R
Ralf Jung 已提交
309 310 311 312 313 314
            PlaceExtra::None
        } else {
            assert!(base.extra != PlaceExtra::None, "Expected fat ptr");
            base.extra
        };

R
Ralf Jung 已提交
315
        Ok(MPlaceTy { mplace: MemPlace { ptr, align, extra }, layout: field_layout })
O
Oliver Schneider 已提交
316 317
    }

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    // 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,
        base: MPlaceTy<'tcx>,
    ) -> EvalResult<'tcx, impl Iterator<Item=EvalResult<'tcx, MPlaceTy<'tcx>>> + 'a> {
        let len = base.len();
        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;
        Ok((0..len).map(move |i| {
            let ptr = base.ptr.ptr_offset(i * stride, dl)?;
            Ok(MPlaceTy {
                mplace: MemPlace { ptr, align: base.align, extra: PlaceExtra::None },
                layout
            })
        }))
    }

R
Ralf Jung 已提交
340
    pub fn mplace_subslice(
O
Oliver Schneider 已提交
341
        &self,
R
Ralf Jung 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354
        base: MPlaceTy<'tcx>,
        from: u64,
        to: u64,
    ) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
        let len = base.len();
        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 已提交
355
        };
R
Ralf Jung 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
        let ptr = base.ptr.ptr_offset(from_offset, self)?;

        // Compute extra and new layout
        let inner_len = len - to - from;
        let (extra, ty) = match base.layout.ty.sty {
            ty::TyArray(inner, _) =>
                (PlaceExtra::None, self.tcx.mk_array(inner, inner_len)),
            ty::TySlice(..) =>
                (PlaceExtra::Length(inner_len), base.layout.ty),
            _ =>
                bug!("cannot subslice non-array type: `{:?}`", base.layout.ty),
        };
        let layout = self.layout_of(ty)?;

        Ok(MPlaceTy {
            mplace: MemPlace { ptr, align: base.align, extra },
            layout
        })
    }

    pub fn mplace_downcast(
        &self,
        base: MPlaceTy<'tcx>,
        variant: usize,
    ) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
        // Downcasts only change the layout
        assert_eq!(base.extra, PlaceExtra::None);
        Ok(MPlaceTy { layout: base.layout.for_variant(self, variant), ..base })
O
Oliver Schneider 已提交
384 385
    }

R
Ralf Jung 已提交
386 387
    /// Project into an mplace
    pub fn mplace_projection(
O
Oliver Schneider 已提交
388
        &self,
R
Ralf Jung 已提交
389 390 391
        base: MPlaceTy<'tcx>,
        proj_elem: &mir::PlaceElem<'tcx>,
    ) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
392
        use rustc::mir::ProjectionElem::*;
R
Ralf Jung 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
        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) => {
                let n = *self.frame().locals[local].access()?;
                let n_layout = self.layout_of(self.tcx.types.usize)?;
                let n = self.read_scalar(OpTy { op: n, layout: n_layout })?;
                let n = n.to_bits(self.tcx.data_layout.pointer_size)?;
                self.mplace_field(base, u64::try_from(n).unwrap())?
            }

            ConstantIndex {
                offset,
                min_length,
                from_end,
            } => {
                let n = base.len();
                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 已提交
426 427
    }

R
Ralf Jung 已提交
428 429 430
    /// Get the place of a field inside the place, and also the field's type.
    /// Just a convenience function, but used quite a bit.
    pub fn place_field(
O
Oliver Schneider 已提交
431
        &mut self,
R
Ralf Jung 已提交
432
        base: PlaceTy<'tcx>,
R
Ralf Jung 已提交
433 434 435 436 437 438
        field: u64,
    ) -> EvalResult<'tcx, PlaceTy<'tcx>> {
        // 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 已提交
439 440
    }

R
Ralf Jung 已提交
441 442
    pub fn place_downcast(
        &mut self,
R
Ralf Jung 已提交
443
        base: PlaceTy<'tcx>,
R
Ralf Jung 已提交
444 445 446 447 448 449 450 451 452
        variant: usize,
    ) -> EvalResult<'tcx, PlaceTy<'tcx>> {
        // Downcast just changes the layout
        Ok(match base.place {
            Place::Ptr(mplace) =>
                self.mplace_downcast(MPlaceTy { mplace, layout: base.layout }, variant)?.into(),
            Place::Local { .. } => {
                let layout = base.layout.for_variant(&self, variant);
                PlaceTy { layout, ..base }
O
Oliver Schneider 已提交
453
            }
R
Ralf Jung 已提交
454
        })
O
Oliver Schneider 已提交
455 456
    }

R
Ralf Jung 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    /// Project into a place
    pub fn place_projection(
        &mut self,
        base: PlaceTy<'tcx>,
        proj_elem: &mir::ProjectionElem<'tcx, mir::Local, Ty<'tcx>>,
    ) -> EvalResult<'tcx, PlaceTy<'tcx>> {
        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()
            }
        })
    }

    /// Compute a place.  You should only use this if you intend to write into this
    /// place; for reading, a more efficient alternative is `eval_place_for_read`.
    pub fn eval_place(&mut self, mir_place: &mir::Place<'tcx>) -> EvalResult<'tcx, PlaceTy<'tcx>> {
480
        use rustc::mir::Place::*;
O
Oliver Schneider 已提交
481
        let place = match *mir_place {
R
Ralf Jung 已提交
482 483 484 485 486 487 488 489 490 491
            Local(mir::RETURN_PLACE) => PlaceTy {
                place: self.frame().return_place,
                layout: self.layout_of_local(self.cur_frame(), mir::RETURN_PLACE)?,
            },
            Local(local) => PlaceTy {
                place: Place::Local {
                    frame: self.cur_frame(),
                    local,
                },
                layout: self.layout_of_local(self.cur_frame(), local)?,
O
Oliver Schneider 已提交
492 493
            },

494 495
            Promoted(ref promoted) => {
                let instance = self.frame().instance;
R
Ralf Jung 已提交
496
                let op = self.global_to_op(GlobalId {
497 498 499
                    instance,
                    promoted: Some(promoted.0),
                })?;
R
Ralf Jung 已提交
500 501 502 503 504
                let mplace = op.to_mem_place();
                let ty = self.monomorphize(promoted.1, self.substs());
                PlaceTy {
                    place: Place::Ptr(mplace),
                    layout: self.layout_of(ty)?,
505 506 507
                }
            }

O
Oliver Schneider 已提交
508
            Static(ref static_) => {
R
Ralf Jung 已提交
509 510
                let ty = self.monomorphize(static_.ty, self.substs());
                let layout = self.layout_of(ty)?;
511 512 513 514 515 516
                let instance = ty::Instance::mono(*self.tcx, static_.def_id);
                let cid = GlobalId {
                    instance,
                    promoted: None
                };
                let alloc = Machine::init_static(self, cid)?;
R
Ralf Jung 已提交
517
                MPlaceTy::from_aligned_ptr(alloc.into(), layout).into()
O
Oliver Schneider 已提交
518 519 520 521
            }

            Projection(ref proj) => {
                let place = self.eval_place(&proj.base)?;
R
Ralf Jung 已提交
522
                self.place_projection(place, &proj.elem)?
O
Oliver Schneider 已提交
523 524 525
            }
        };

R
Ralf Jung 已提交
526
        self.dump_place(place.place);
O
Oliver Schneider 已提交
527 528 529 530

        Ok(place)
    }

R
Ralf Jung 已提交
531 532
    /// Write a scalar to a place
    pub fn write_scalar(
O
Oliver Schneider 已提交
533
        &mut self,
R
Ralf Jung 已提交
534 535 536 537 538
        val: impl Into<ScalarMaybeUndef>,
        dest: PlaceTy<'tcx>,
    ) -> EvalResult<'tcx> {
        self.write_value(Value::Scalar(val.into()), dest)
    }
O
Oliver Schneider 已提交
539

R
Ralf Jung 已提交
540 541 542 543
    /// Write a value to a place
    pub fn write_value(
        &mut self,
        src_val: Value,
R
Ralf Jung 已提交
544
        dest: PlaceTy<'tcx>,
R
Ralf Jung 已提交
545
    ) -> EvalResult<'tcx> {
R
Ralf Jung 已提交
546
        trace!("write_value: {:?} <- {:?}", *dest, src_val);
R
Ralf Jung 已提交
547 548
        // See if we can avoid an allocation. This is the counterpart to `try_read_value`,
        // but not factored as a separate function.
R
Ralf Jung 已提交
549
        let mplace = match dest.place {
O
Oliver Schneider 已提交
550
            Place::Local { frame, local } => {
R
Ralf Jung 已提交
551 552 553 554 555
                match *self.stack[frame].locals[local].access_mut()? {
                    Operand::Immediate(ref mut dest_val) => {
                        // Yay, we can just change the local directly.
                        *dest_val = src_val;
                        return Ok(());
556
                    },
R
Ralf Jung 已提交
557
                    Operand::Indirect(mplace) => mplace, // already in memory
O
Oliver Schneider 已提交
558
                }
R
Ralf Jung 已提交
559
            },
R
Ralf Jung 已提交
560
            Place::Ptr(mplace) => mplace, // already in memory
O
Oliver Schneider 已提交
561 562
        };

R
Ralf Jung 已提交
563 564
        // This is already in memory, write there.
        let dest = MPlaceTy { mplace, layout: dest.layout };
R
Ralf Jung 已提交
565
        self.write_value_to_mplace(src_val, dest)
O
Oliver Schneider 已提交
566 567
    }

R
Ralf Jung 已提交
568 569 570 571 572 573
    /// Write a value to memory
    fn write_value_to_mplace(
        &mut self,
        value: Value,
        dest: MPlaceTy<'tcx>,
    ) -> EvalResult<'tcx> {
574
        let (ptr, ptr_align) = dest.to_scalar_ptr_align();
R
Ralf Jung 已提交
575 576 577
        // 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.
578 579 580 581 582 583 584 585

        // Nothing to do for ZSTs, other than checking alignment
        if dest.layout.size.bytes() == 0 {
            self.memory.check_align(ptr, ptr_align)?;
            return Ok(());
        }

        let ptr = ptr.to_ptr()?;
R
Ralf Jung 已提交
586 587 588
        match value {
            Value::Scalar(scalar) => {
                self.memory.write_scalar(
589
                    ptr, ptr_align.min(dest.layout.align), scalar, dest.layout.size
R
Ralf Jung 已提交
590
                )
O
Oliver Schneider 已提交
591
            }
R
Ralf Jung 已提交
592 593 594
            Value::ScalarPair(a_val, b_val) => {
                let (a, b) = match dest.layout.abi {
                    layout::Abi::ScalarPair(ref a, ref b) => (&a.value, &b.value),
R
Ralf Jung 已提交
595
                    _ => bug!("write_value_to_mplace: invalid ScalarPair layout: {:#?}", dest.layout)
R
Ralf Jung 已提交
596 597 598 599
                };
                let (a_size, b_size) = (a.size(&self), b.size(&self));
                let (a_align, b_align) = (a.align(&self), b.align(&self));
                let b_offset = a_size.abi_align(b_align);
600
                let b_ptr = ptr.offset(b_offset, &self)?.into();
601

602 603
                self.memory.write_scalar(ptr, ptr_align.min(a_align), a_val, a_size)?;
                self.memory.write_scalar(b_ptr, ptr_align.min(b_align), b_val, b_size)
O
Oliver Schneider 已提交
604
            }
R
Ralf Jung 已提交
605
        }
O
Oliver Schneider 已提交
606 607
    }

R
Ralf Jung 已提交
608 609
    /// Copy the data from an operand to a place
    pub fn copy_op(
O
Oliver Schneider 已提交
610
        &mut self,
R
Ralf Jung 已提交
611 612 613
        src: OpTy<'tcx>,
        dest: PlaceTy<'tcx>,
    ) -> EvalResult<'tcx> {
R
Ralf Jung 已提交
614 615
        assert_eq!(src.layout.size, dest.layout.size,
            "Size mismatch when copying!\nsrc: {:#?}\ndest: {:#?}", src, dest);
R
Ralf Jung 已提交
616 617 618 619

        // Let us see if the layout is simple so we take a shortcut, avoid force_allocation.
        let (src_ptr, src_align) = match self.try_read_value(src)? {
            Ok(src_val) =>
R
Ralf Jung 已提交
620 621 622 623
                // Yay, we got a value that we can write directly.  We write with the
                // *source layout*, because that was used to load, and if they do not match
                // this is a transmute we want to support.
                return self.write_value(src_val, PlaceTy { place: *dest, layout: src.layout }),
R
Ralf Jung 已提交
624 625 626
            Err(mplace) => mplace.to_scalar_ptr_align(),
        };
        // Slow path, this does not fit into an immediate. Just memcpy.
R
Ralf Jung 已提交
627
        trace!("copy_op: {:?} <- {:?}", *dest, *src);
R
Ralf Jung 已提交
628 629 630 631 632 633
        let (dest_ptr, dest_align) = self.force_allocation(dest)?.to_scalar_ptr_align();
        self.memory.copy(
            src_ptr, src_align,
            dest_ptr, dest_align,
            src.layout.size, false
        )
O
Oliver Schneider 已提交
634 635
    }

R
Ralf Jung 已提交
636 637 638
    /// Make sure that a place is in memory, and return where it is.
    /// This is essentially `force_to_memplace`.
    pub fn force_allocation(
O
Oliver Schneider 已提交
639
        &mut self,
R
Ralf Jung 已提交
640 641 642 643
        place: PlaceTy<'tcx>,
    ) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
        let mplace = match place.place {
            Place::Local { frame, local } => {
644 645 646
                // FIXME: Consider not doing anything for a ZST, and just returning
                // a fake pointer?

R
Ralf Jung 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
                // We need the layout of the local.  We can NOT use the layout we got,
                // that might e.g. be a downcast variant!
                let local_layout = self.layout_of_local(frame, local)?;
                // Make sure it has a place
                let rval = *self.stack[frame].locals[local].access()?;
                let mplace = self.allocate_op(OpTy { op: rval, layout: local_layout })?.mplace;
                // This might have allocated the flag
                *self.stack[frame].locals[local].access_mut()? =
                    Operand::Indirect(mplace);
                // done
                mplace
            }
            Place::Ptr(mplace) => mplace
        };
        // Return with the original layout, so that the caller can go on
        Ok(MPlaceTy { mplace, layout: place.layout })
O
Oliver Schneider 已提交
663 664
    }

R
Ralf Jung 已提交
665
    pub fn allocate(
O
Oliver Schneider 已提交
666
        &mut self,
R
Ralf Jung 已提交
667 668 669 670 671 672 673
        layout: TyLayout<'tcx>,
        kind: MemoryKind<M::MemoryKinds>,
    ) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
        assert!(!layout.is_unsized(), "cannot alloc memory for unsized type");
        let ptr = self.memory.allocate(layout.size, layout.align, kind)?;
        Ok(MPlaceTy::from_aligned_ptr(ptr, layout))
    }
O
Oliver Schneider 已提交
674

R
Ralf Jung 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    /// Make a place for an operand, allocating if needed
    pub fn allocate_op(
        &mut self,
        OpTy { op, layout }: OpTy<'tcx>,
    ) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
        Ok(match op {
            Operand::Indirect(mplace) => MPlaceTy { mplace, layout },
            Operand::Immediate(value) => {
                // FIXME: Is stack always right here?
                let ptr = self.allocate(layout, MemoryKind::Stack)?;
                self.write_value_to_mplace(value, ptr)?;
                ptr
            },
        })
    }
O
Oliver Schneider 已提交
690

R
Ralf Jung 已提交
691 692 693 694 695 696 697 698 699 700 701 702 703
    pub fn write_discriminant_value(
        &mut self,
        variant_index: usize,
        dest: PlaceTy<'tcx>,
    ) -> EvalResult<'tcx> {
        match dest.layout.variants {
            layout::Variants::Single { index } => {
                if index != variant_index {
                    // If the layout of an enum is `Single`, all
                    // other variants are necessarily uninhabited.
                    assert_eq!(dest.layout.for_variant(&self, variant_index).abi,
                               layout::Abi::Uninhabited);
                }
O
Oliver Schneider 已提交
704
            }
R
Ralf Jung 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
            layout::Variants::Tagged { ref tag, .. } => {
                let discr_val = dest.layout.ty.ty_adt_def().unwrap()
                    .discriminant_for_variant(*self.tcx, variant_index)
                    .val;

                // raw discriminants for enums are isize or bigger during
                // their computation, but the in-memory tag is the smallest possible
                // representation
                let size = tag.value.size(self.tcx.tcx);
                let shift = 128 - size.bits();
                let discr_val = (discr_val << shift) >> shift;

                let discr_dest = self.place_field(dest, 0)?;
                self.write_scalar(Scalar::Bits {
                    bits: discr_val,
                    size: size.bytes() as u8,
                }, discr_dest)?;
O
Oliver Schneider 已提交
722
            }
R
Ralf Jung 已提交
723 724 725 726 727
            layout::Variants::NicheFilling {
                dataful_variant,
                ref niche_variants,
                niche_start,
                ..
O
Oliver Schneider 已提交
728
            } => {
R
Ralf Jung 已提交
729 730 731 732 733 734 735 736 737 738
                if variant_index != dataful_variant {
                    let niche_dest =
                        self.place_field(dest, 0)?;
                    let niche_value = ((variant_index - niche_variants.start()) as u128)
                        .wrapping_add(niche_start);
                    self.write_scalar(Scalar::Bits {
                        bits: niche_value,
                        size: niche_dest.layout.size.bytes() as u8,
                    }, niche_dest)?;
                }
O
Oliver Schneider 已提交
739
            }
740
        }
R
Ralf Jung 已提交
741 742

        Ok(())
O
Oliver Schneider 已提交
743 744
    }

R
Ralf Jung 已提交
745
    /// Every place can be read from, so we can turm them into an operand
746
    #[inline(always)]
R
Ralf Jung 已提交
747 748 749 750 751 752 753 754 755
    pub fn place_to_op(&self, place: PlaceTy<'tcx>) -> EvalResult<'tcx, OpTy<'tcx>> {
        let op = match place.place {
            Place::Ptr(mplace) => {
                Operand::Indirect(mplace)
            }
            Place::Local { frame, local } =>
                *self.stack[frame].locals[local].access()?
        };
        Ok(OpTy { op, layout: place.layout })
O
Oliver Schneider 已提交
756
    }
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

    /// Turn a place that is a dyn trait (i.e., PlaceExtra::Vtable and the appropriate layout)
    /// or a slice into the specific fixed-size place and layout that is given by the vtable/len.
    /// This "unpacks" the existential quantifier, so to speak.
    pub fn unpack_unsized_mplace(&self, mplace: MPlaceTy<'tcx>) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
        trace!("Unpacking {:?} ({:?})", *mplace, mplace.layout.ty);
        let layout = match mplace.extra {
            PlaceExtra::Vtable(vtable) => {
                // the drop function signature
                let drop_instance = self.read_drop_type_from_vtable(vtable)?;
                trace!("Found drop fn: {:?}", drop_instance);
                let fn_sig = drop_instance.ty(*self.tcx).fn_sig(*self.tcx);
                let fn_sig = self.tcx.normalize_erasing_late_bound_regions(self.param_env, &fn_sig);
                // the drop function takes *mut T where T is the type being dropped, so get that
                let ty = fn_sig.inputs()[0].builtin_deref(true).unwrap().ty;
                let layout = self.layout_of(ty)?;
                // Sanity checks
                let (size, align) = self.read_size_and_align_from_vtable(vtable)?;
                assert_eq!(size, layout.size);
                assert_eq!(align.abi(), layout.align.abi()); // only ABI alignment is preserved
777 778
                // FIXME: More checks for the vtable? We could make sure it is exactly
                // the one one would expect for this type.
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
                // Done!
                layout
            },
            PlaceExtra::Length(len) => {
                let ty = self.tcx.mk_array(mplace.layout.field(self, 0)?.ty, len);
                self.layout_of(ty)?
            }
            PlaceExtra::None => bug!("Expected a fat pointer"),
        };
        trace!("Unpacked type: {:?}", layout.ty);
        Ok(MPlaceTy {
            mplace: MemPlace { extra: PlaceExtra::None, ..*mplace },
            layout
        })
    }
O
Oliver Schneider 已提交
794
}