about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2019-11-29 11:29:30 +0100
committerRalf Jung <post@ralfj.de>2019-12-02 08:55:42 +0100
commit56356a0745fe6b040400ebfaa636b8197395afc1 (patch)
tree565eb26ea87208799c3f622dd0c225ac7cfb6f94
parentcde17d915867d75169e8f7e8845ce705006c2ebb (diff)
downloadrust-56356a0745fe6b040400ebfaa636b8197395afc1.tar.gz
rust-56356a0745fe6b040400ebfaa636b8197395afc1.zip
Miri: add helper fn to allocate string; simplify alloc_caller_location
-rw-r--r--src/librustc_mir/const_eval.rs2
-rw-r--r--src/librustc_mir/interpret/intrinsics.rs2
-rw-r--r--src/librustc_mir/interpret/intrinsics/caller_location.rs45
-rw-r--r--src/librustc_mir/interpret/operand.rs2
-rw-r--r--src/librustc_mir/interpret/place.rs18
5 files changed, 36 insertions, 33 deletions
diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs
index 4b009111bf7..010738c5a05 100644
--- a/src/librustc_mir/const_eval.rs
+++ b/src/librustc_mir/const_eval.rs
@@ -552,7 +552,7 @@ pub fn const_caller_location<'tcx>(
         tcx.type_of(tcx.require_lang_item(PanicLocationLangItem, None))
             .subst(tcx, tcx.mk_substs([tcx.lifetimes.re_static.into()].iter())),
     );
-    let loc_place = ecx.alloc_caller_location(file, line, col).unwrap();
+    let loc_place = ecx.alloc_caller_location(file, line, col);
     intern_const_alloc_recursive(&mut ecx, None, loc_place).unwrap();
     let loc_const = ty::Const {
         ty: loc_ty,
diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs
index ea8bc968ccf..118dfcb3d9a 100644
--- a/src/librustc_mir/interpret/intrinsics.rs
+++ b/src/librustc_mir/interpret/intrinsics.rs
@@ -110,7 +110,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
 
         match intrinsic_name {
             "caller_location" => {
-                let location = self.alloc_caller_location_for_span(span)?;
+                let location = self.alloc_caller_location_for_span(span);
                 self.write_scalar(location.ptr, dest)?;
             }
 
diff --git a/src/librustc_mir/interpret/intrinsics/caller_location.rs b/src/librustc_mir/interpret/intrinsics/caller_location.rs
index 649fc65f642..ecf4b7a39b7 100644
--- a/src/librustc_mir/interpret/intrinsics/caller_location.rs
+++ b/src/librustc_mir/interpret/intrinsics/caller_location.rs
@@ -1,10 +1,9 @@
 use rustc::middle::lang_items::PanicLocationLangItem;
-use rustc::mir::interpret::{Pointer, PointerArithmetic, Scalar};
 use rustc::ty::subst::Subst;
-use rustc_target::abi::{LayoutOf, Size};
+use rustc_target::abi::LayoutOf;
 use syntax_pos::{Symbol, Span};
 
-use crate::interpret::{MemoryKind, MPlaceTy, intrinsics::{InterpCx, InterpResult, Machine}};
+use crate::interpret::{Scalar, MemoryKind, MPlaceTy, intrinsics::{InterpCx, Machine}};
 
 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
     crate fn alloc_caller_location(
@@ -12,46 +11,32 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         filename: Symbol,
         line: u32,
         col: u32,
-    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
+    ) -> MPlaceTy<'tcx, M::PointerTag> {
+        let file = self.allocate_str(&filename.as_str(), MemoryKind::CallerLocation);
         let line = Scalar::from_u32(line);
         let col = Scalar::from_u32(col);
 
-        let ptr_size = self.pointer_size();
-        let u32_size = Size::from_bits(32);
-
+        // Allocate memory for `CallerLocation` struct.
         let loc_ty = self.tcx.type_of(self.tcx.require_lang_item(PanicLocationLangItem, None))
             .subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_static.into()].iter()));
-        let loc_layout = self.layout_of(loc_ty)?;
-
-        let file_alloc = self.tcx.allocate_bytes(filename.as_str().as_bytes());
-        let file_ptr = Pointer::new(file_alloc, Size::ZERO);
-        let file = Scalar::Ptr(self.tag_static_base_pointer(file_ptr));
-        let file_len = Scalar::from_uint(filename.as_str().len() as u128, ptr_size);
-
+        let loc_layout = self.layout_of(loc_ty).unwrap();
         let location = self.allocate(loc_layout, MemoryKind::CallerLocation);
 
-        let file_out = self.mplace_field(location, 0)?;
-        let file_ptr_out = self.force_ptr(self.mplace_field(file_out, 0)?.ptr)?;
-        let file_len_out = self.force_ptr(self.mplace_field(file_out, 1)?.ptr)?;
-        let line_out = self.force_ptr(self.mplace_field(location, 1)?.ptr)?;
-        let col_out = self.force_ptr(self.mplace_field(location, 2)?.ptr)?;
-
-        let layout = &self.tcx.data_layout;
-        // We just allocated this, so we can skip the bounds checks.
-        let alloc = self.memory.get_raw_mut(file_ptr_out.alloc_id)?;
-
-        alloc.write_scalar(layout, file_ptr_out, file.into(), ptr_size)?;
-        alloc.write_scalar(layout, file_len_out, file_len.into(), ptr_size)?;
-        alloc.write_scalar(layout, line_out, line.into(), u32_size)?;
-        alloc.write_scalar(layout, col_out, col.into(), u32_size)?;
+        // Initialize fields.
+        self.write_immediate(file.to_ref(), self.mplace_field(location, 0).unwrap().into())
+            .expect("writing to memory we just allocated cannot fail");
+        self.write_scalar(line, self.mplace_field(location, 1).unwrap().into())
+            .expect("writing to memory we just allocated cannot fail");
+        self.write_scalar(col, self.mplace_field(location, 2).unwrap().into())
+            .expect("writing to memory we just allocated cannot fail");
 
-        Ok(location)
+        location
     }
 
     pub fn alloc_caller_location_for_span(
         &mut self,
         span: Span,
-    ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
+    ) -> MPlaceTy<'tcx, M::PointerTag> {
         let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
         let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
         self.alloc_caller_location(
diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs
index 96bad0deafe..2ec1613bce2 100644
--- a/src/librustc_mir/interpret/operand.rs
+++ b/src/librustc_mir/interpret/operand.rs
@@ -331,7 +331,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         Ok(self.read_immediate(op)?.to_scalar_or_undef())
     }
 
-    // Turn the MPlace into a string (must already be dereferenced!)
+    // Turn the fat MPlace into a string (must already be dereferenced!)
     pub fn read_str(
         &self,
         mplace: MPlaceTy<'tcx, M::PointerTag>,
diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs
index 11caf20acc5..ea7b53b3dda 100644
--- a/src/librustc_mir/interpret/place.rs
+++ b/src/librustc_mir/interpret/place.rs
@@ -1034,6 +1034,24 @@ where
         MPlaceTy::from_aligned_ptr(ptr, layout)
     }
 
+    /// Returns a fat MPlace.
+    pub fn allocate_str(
+        &mut self,
+        str: &str,
+        kind: MemoryKind<M::MemoryKinds>,
+    ) -> MPlaceTy<'tcx, M::PointerTag> {
+        let ptr = self.memory.allocate_static_bytes(str.as_bytes(), kind);
+        let meta = Scalar::from_uint(str.len() as u128, self.pointer_size());
+        let mplace = MemPlace {
+            ptr: ptr.into(),
+            align: Align::from_bytes(1).unwrap(),
+            meta: Some(meta),
+        };
+
+        let layout = self.layout_of(self.tcx.mk_static_str()).unwrap();
+        MPlaceTy { mplace, layout }
+    }
+
     pub fn write_discriminant_index(
         &mut self,
         variant_index: VariantIdx,