summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/interpret
diff options
context:
space:
mode:
authorb-naber <bn263@gmx.de>2022-03-29 11:38:08 +0200
committerb-naber <bn263@gmx.de>2022-03-29 11:41:01 +0200
commitfcc4d8ce98ef61586fccfb7efae7563788453b73 (patch)
treefbcfc3b6c6bfc1065229c5d8e6ef8eb74bf8b915 /compiler/rustc_const_eval/src/interpret
parent51aa3f86a040599dad36a75c22fa0321f7de0741 (diff)
downloadrust-fcc4d8ce98ef61586fccfb7efae7563788453b73.tar.gz
rust-fcc4d8ce98ef61586fccfb7efae7563788453b73.zip
include refs in valtree creation
Diffstat (limited to 'compiler/rustc_const_eval/src/interpret')
-rw-r--r--compiler/rustc_const_eval/src/interpret/operand.rs37
-rw-r--r--compiler/rustc_const_eval/src/interpret/place.rs12
2 files changed, 45 insertions, 4 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs
index 9000567558b..d271bf53eac 100644
--- a/compiler/rustc_const_eval/src/interpret/operand.rs
+++ b/compiler/rustc_const_eval/src/interpret/operand.rs
@@ -14,9 +14,9 @@ use rustc_target::abi::{Abi, HasDataLayout, Size, TagEncoding};
 use rustc_target::abi::{VariantIdx, Variants};
 
 use super::{
-    alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, GlobalId,
-    InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, Place, PlaceTy, Pointer, Provenance,
-    Scalar, ScalarMaybeUninit,
+    alloc_range, from_known_layout, mir_assign_valid_types, AllocId, AllocRange, Allocation,
+    ConstValue, GlobalId, InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, Place, PlaceTy,
+    Pointer, Provenance, Scalar, ScalarMaybeUninit,
 };
 
 /// An `Immediate` represents a single immediate self-contained Rust value.
@@ -248,7 +248,7 @@ impl<'tcx, Tag: Provenance> ImmTy<'tcx, Tag> {
 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
     /// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`.
     /// Returns `None` if the layout does not permit loading this as a value.
-    fn try_read_immediate_from_mplace(
+    pub(crate) fn try_read_immediate_from_mplace(
         &self,
         mplace: &MPlaceTy<'tcx, M::PointerTag>,
     ) -> InterpResult<'tcx, Option<ImmTy<'tcx, M::PointerTag>>> {
@@ -777,3 +777,32 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         })
     }
 }
+
+impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx, PointerTag = AllocId>> InterpCx<'mir, 'tcx, M> {
+    pub fn get_alloc_from_imm_scalar_pair(
+        &self,
+        imm: ImmTy<'tcx, M::PointerTag>,
+    ) -> (&Allocation, AllocRange) {
+        match imm.imm {
+            Immediate::ScalarPair(a, b) => {
+                // We know `offset` is relative to the allocation, so we can use `into_parts`.
+                let (data, start) = match self.scalar_to_ptr(a.check_init().unwrap()).into_parts() {
+                    (Some(alloc_id), offset) => {
+                        (self.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes())
+                    }
+                    (None, _offset) => (
+                        self.tcx.intern_const_alloc(Allocation::from_bytes_byte_aligned_immutable(
+                            b"" as &[u8],
+                        )),
+                        0,
+                    ),
+                };
+                let len = b.to_machine_usize(self).unwrap();
+                let size = Size::from_bytes(len);
+                let start = Size::from_bytes(start);
+                (data.inner(), AllocRange { start, size })
+            }
+            _ => bug!("{:?} not a ScalarPair", imm),
+        }
+    }
+}
diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs
index b1784b12c65..ad7620d83e6 100644
--- a/compiler/rustc_const_eval/src/interpret/place.rs
+++ b/compiler/rustc_const_eval/src/interpret/place.rs
@@ -305,6 +305,18 @@ where
         Ok(mplace)
     }
 
+    #[instrument(skip(self), level = "debug")]
+    pub fn deref_mplace(
+        &self,
+        src: &MPlaceTy<'tcx, M::PointerTag>,
+    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
+        let val = self.try_read_immediate_from_mplace(src)?;
+        let mplace = self.ref_to_mplace(&val.unwrap())?;
+        self.check_mplace_access(mplace, CheckInAllocMsg::DerefTest)?;
+
+        Ok(mplace)
+    }
+
     #[inline]
     pub(super) fn get_alloc(
         &self,