]> git.lizzy.rs Git - rust.git/commitdiff
Always allocate for globals
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 8 Aug 2017 12:22:11 +0000 (14:22 +0200)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 8 Aug 2017 15:09:26 +0000 (17:09 +0200)
statics are rare and constants are copied anyway.
Reading from a constant should then yield a `ByVal` again if possible.

15 files changed:
miri/fn_call.rs
miri/intrinsic.rs
miri/lib.rs
src/librustc_mir/interpret/const_eval.rs
src/librustc_mir/interpret/eval_context.rs
src/librustc_mir/interpret/lvalue.rs
src/librustc_mir/interpret/machine.rs
src/librustc_mir/interpret/memory.rs
src/librustc_mir/interpret/mod.rs
src/librustc_mir/interpret/operator.rs
src/librustc_mir/interpret/step.rs
src/librustc_mir/interpret/terminator/drop.rs
src/librustc_mir/interpret/terminator/mod.rs
src/librustc_mir/interpret/validation.rs
src/librustc_mir/interpret/value.rs

index 81db48fe1296b4a9417f12e6fb244abe92c3414e..5cbad8c4f124f90120b5ba91f0b086b09726b6a9 100644 (file)
@@ -23,7 +23,7 @@ fn call_c_abi(
         &mut self,
         def_id: DefId,
         arg_operands: &[mir::Operand<'tcx>],
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
         dest_block: mir::BasicBlock,
     ) -> EvalResult<'tcx>;
@@ -33,7 +33,7 @@ fn call_c_abi(
     fn call_missing_fn(
         &mut self,
         instance: ty::Instance<'tcx>,
-        destination: Option<(Lvalue<'tcx>, mir::BasicBlock)>,
+        destination: Option<(Lvalue, mir::BasicBlock)>,
         arg_operands: &[mir::Operand<'tcx>],
         sig: ty::FnSig<'tcx>,
         path: String,
@@ -42,7 +42,7 @@ fn call_missing_fn(
     fn eval_fn_call(
         &mut self,
         instance: ty::Instance<'tcx>,
-        destination: Option<(Lvalue<'tcx>, mir::BasicBlock)>,
+        destination: Option<(Lvalue, mir::BasicBlock)>,
         arg_operands: &[mir::Operand<'tcx>],
         span: Span,
         sig: ty::FnSig<'tcx>,
@@ -53,7 +53,7 @@ impl<'a, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, super::Evaluator>
     fn eval_fn_call(
         &mut self,
         instance: ty::Instance<'tcx>,
-        destination: Option<(Lvalue<'tcx>, mir::BasicBlock)>,
+        destination: Option<(Lvalue, mir::BasicBlock)>,
         arg_operands: &[mir::Operand<'tcx>],
         span: Span,
         sig: ty::FnSig<'tcx>,
@@ -89,7 +89,7 @@ fn call_c_abi(
         &mut self,
         def_id: DefId,
         arg_operands: &[mir::Operand<'tcx>],
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
         dest_block: mir::BasicBlock,
     ) -> EvalResult<'tcx> {
@@ -329,8 +329,8 @@ fn call_c_abi(
                     if let Ok(instance) = self.resolve_path(path) {
                         let cid = GlobalId { instance, promoted: None };
                         // compute global if not cached
-                        let val = match self.globals.get(&cid).map(|glob| glob.value) {
-                            Some(value) => self.value_to_primval(value, usize)?.to_u64()?,
+                        let val = match self.globals.get(&cid).map(|&ptr| ptr) {
+                            Some(ptr) => self.value_to_primval(Value::by_ref(ptr.into()), usize)?.to_u64()?,
                             None => eval_body_as_primval(self.tcx, instance)?.0.to_u64()?,
                         };
                         if val == name {
@@ -459,7 +459,7 @@ fn resolve_path(&self, path: &[&str]) -> EvalResult<'tcx, ty::Instance<'tcx>> {
     fn call_missing_fn(
         &mut self,
         instance: ty::Instance<'tcx>,
-        destination: Option<(Lvalue<'tcx>, mir::BasicBlock)>,
+        destination: Option<(Lvalue, mir::BasicBlock)>,
         arg_operands: &[mir::Operand<'tcx>],
         sig: ty::FnSig<'tcx>,
         path: String,
index 4cdad350b43e60635e573a1d1f8ae0490af15e7c..053b2da86f70bec3f4a68b20b994ef5407a92d3e 100644 (file)
@@ -18,7 +18,7 @@ fn call_intrinsic(
         &mut self,
         instance: ty::Instance<'tcx>,
         args: &[mir::Operand<'tcx>],
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
         dest_layout: &'tcx Layout,
         target: mir::BasicBlock,
@@ -30,7 +30,7 @@ fn call_intrinsic(
         &mut self,
         instance: ty::Instance<'tcx>,
         args: &[mir::Operand<'tcx>],
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
         dest_layout: &'tcx Layout,
         target: mir::BasicBlock,
@@ -291,7 +291,6 @@ fn call_intrinsic(
                     Lvalue::Local { frame, local } => self.modify_local(frame, local, init)?,
                     Lvalue::Ptr { ptr, extra: LvalueExtra::None, aligned: true } => self.memory.write_repeat(ptr, 0, size)?,
                     Lvalue::Ptr { .. } => bug!("init intrinsic tried to write to fat or unaligned ptr target"),
-                    Lvalue::Global(cid) => self.modify_global(cid, init)?,
                 }
             }
 
@@ -469,7 +468,6 @@ fn call_intrinsic(
                     Lvalue::Ptr { ptr, extra: LvalueExtra::None, aligned: true } =>
                         self.memory.mark_definedness(ptr, size, false)?,
                     Lvalue::Ptr { .. } => bug!("uninit intrinsic tried to write to fat or unaligned ptr target"),
-                    Lvalue::Global(cid) => self.modify_global(cid, uninit)?,
                 }
             }
 
index c887890fabff315e74d5354b5efc6f7ab9b9d321..1dc1682b17f350a0a4be460bddc4c700a368296d 100644 (file)
@@ -167,7 +167,7 @@ impl<'tcx> Machine<'tcx> for Evaluator {
     fn eval_fn_call<'a>(
         ecx: &mut EvalContext<'a, 'tcx, Self>,
         instance: ty::Instance<'tcx>,
-        destination: Option<(Lvalue<'tcx>, mir::BasicBlock)>,
+        destination: Option<(Lvalue, mir::BasicBlock)>,
         arg_operands: &[mir::Operand<'tcx>],
         span: Span,
         sig: ty::FnSig<'tcx>,
@@ -179,7 +179,7 @@ fn call_intrinsic<'a>(
         ecx: &mut rustc_miri::interpret::EvalContext<'a, 'tcx, Self>,
         instance: ty::Instance<'tcx>,
         args: &[mir::Operand<'tcx>],
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: ty::Ty<'tcx>,
         dest_layout: &'tcx Layout,
         target: mir::BasicBlock,
index 51f18bccf43fb377ab22066e061055acbd8224c9..f55c94e67c4747512afc907e0d5909b7ee05e19a 100644 (file)
@@ -7,9 +7,10 @@
 
 use super::{
     EvalResult, EvalError, EvalErrorKind,
-    Global, GlobalId, Lvalue,
+    GlobalId, Lvalue, Value,
     PrimVal,
     EvalContext, StackPopCleanup,
+    Kind,
 };
 
 use rustc_const_math::ConstInt;
@@ -30,7 +31,10 @@ pub fn eval_body_as_primval<'a, 'tcx>(
     
     let mir = ecx.load_mir(instance.def)?;
     if !ecx.globals.contains_key(&cid) {
-        ecx.globals.insert(cid, Global::uninitialized(mir.return_ty));
+        let size = ecx.type_size_with_substs(mir.return_ty, instance.substs)?.expect("unsized global");
+        let align = ecx.type_align_with_substs(mir.return_ty, instance.substs)?;
+        let ptr = ecx.memory.allocate(size, align, Kind::UninitializedStatic)?;
+        ecx.globals.insert(cid, ptr);
         let mutable = !mir.return_ty.is_freeze(
                 ecx.tcx,
                 ty::ParamEnv::empty(Reveal::All),
@@ -47,13 +51,13 @@ pub fn eval_body_as_primval<'a, 'tcx>(
             instance,
             mir.span,
             mir,
-            Lvalue::Global(cid),
+            Lvalue::from_ptr(ptr),
             cleanup,
         )?;
 
         while ecx.step()? {}
     }
-    let value = ecx.globals.get(&cid).expect("global not cached").value;
+    let value = Value::by_ref(ecx.globals.get(&cid).expect("global not cached").into());
     Ok((ecx.value_to_primval(value, mir.return_ty)?, mir.return_ty))
 }
 
@@ -132,7 +136,7 @@ impl<'tcx> super::Machine<'tcx> for CompileTimeFunctionEvaluator {
     fn eval_fn_call<'a>(
         ecx: &mut EvalContext<'a, 'tcx, Self>,
         instance: ty::Instance<'tcx>,
-        destination: Option<(Lvalue<'tcx>, mir::BasicBlock)>,
+        destination: Option<(Lvalue, mir::BasicBlock)>,
         _arg_operands: &[mir::Operand<'tcx>],
         span: Span,
         _sig: ty::FnSig<'tcx>,
@@ -168,7 +172,7 @@ fn call_intrinsic<'a>(
         _ecx: &mut EvalContext<'a, 'tcx, Self>,
         _instance: ty::Instance<'tcx>,
         _args: &[mir::Operand<'tcx>],
-        _dest: Lvalue<'tcx>,
+        _dest: Lvalue,
         _dest_ty: Ty<'tcx>,
         _dest_layout: &'tcx layout::Layout,
         _target: mir::BasicBlock,
index c5ca9712955e45cc0ef9b7817a96652d846035e5..beeb07b4ce1b1fad23c6c6ed08a67e2a8b81c748 100644 (file)
@@ -18,7 +18,7 @@
 
 use super::{
     EvalError, EvalResult, EvalErrorKind,
-    Global, GlobalId, Lvalue, LvalueExtra,
+    GlobalId, Lvalue, LvalueExtra,
     Memory, MemoryPointer, HasMemory,
     Kind as MemoryKind,
     operator,
@@ -41,7 +41,7 @@ pub struct EvalContext<'a, 'tcx: 'a, M: Machine<'tcx>> {
     pub(crate) suspended: HashMap<DynamicLifetime, Vec<ValidationQuery<'tcx>>>,
 
     /// Precomputed statics, constants and promoteds.
-    pub globals: HashMap<GlobalId<'tcx>, Global<'tcx>>,
+    pub globals: HashMap<GlobalId<'tcx>, MemoryPointer>,
 
     /// The virtual call stack.
     pub(crate) stack: Vec<Frame<'tcx>>,
@@ -78,7 +78,7 @@ pub struct Frame<'tcx> {
     pub return_to_block: StackPopCleanup,
 
     /// The location where the result of the current stack frame should be written to.
-    pub return_lvalue: Lvalue<'tcx>,
+    pub return_lvalue: Lvalue,
 
     /// The list of locals for this stack frame, stored in order as
     /// `[arguments..., variables..., temporaries...]`. The locals are stored as `Option<Value>`s.
@@ -386,7 +386,7 @@ pub fn type_align(&self, ty: Ty<'tcx>) -> EvalResult<'tcx, u64> {
         self.type_align_with_substs(ty, self.substs())
     }
 
-    fn type_size_with_substs(
+    pub fn type_size_with_substs(
         &self,
         ty: Ty<'tcx>,
         substs: &'tcx Substs<'tcx>,
@@ -399,7 +399,7 @@ fn type_size_with_substs(
         }
     }
 
-    fn type_align_with_substs(&self, ty: Ty<'tcx>, substs: &'tcx Substs<'tcx>) -> EvalResult<'tcx, u64> {
+    pub fn type_align_with_substs(&self, ty: Ty<'tcx>, substs: &'tcx Substs<'tcx>) -> EvalResult<'tcx, u64> {
         self.type_layout_with_substs(ty, substs).map(|layout| layout.align(&self.tcx.data_layout).abi())
     }
 
@@ -419,7 +419,7 @@ pub fn push_stack_frame(
         instance: ty::Instance<'tcx>,
         span: codemap::Span,
         mir: &'tcx mir::Mir<'tcx>,
-        return_lvalue: Lvalue<'tcx>,
+        return_lvalue: Lvalue,
         return_to_block: StackPopCleanup,
     ) -> EvalResult<'tcx> {
         ::log_settings::settings().indentation += 1;
@@ -485,31 +485,9 @@ pub(super) fn pop_stack_frame(&mut self) -> EvalResult<'tcx> {
             self.memory.set_cur_frame(cur_frame);
         }
         match frame.return_to_block {
-            StackPopCleanup::MarkStatic(mutable) => if let Lvalue::Global(id) = frame.return_lvalue {
-                let global_value = self.globals.get_mut(&id)
-                    .expect("global should have been cached (static)");
-                match global_value.value {
-                    // FIXME: to_ptr()? might be too extreme here, static zsts might reach this under certain conditions
-                    Value::ByRef { ptr, aligned: _aligned } =>
-                        // Alignment does not matter for this call
-                        self.memory.mark_static_initalized(ptr.to_ptr()?.alloc_id, mutable)?,
-                    Value::ByVal(val) => if let PrimVal::Ptr(ptr) = val {
-                        self.memory.mark_inner_allocation(ptr.alloc_id, mutable)?;
-                    },
-                    Value::ByValPair(val1, val2) => {
-                        if let PrimVal::Ptr(ptr) = val1 {
-                            self.memory.mark_inner_allocation(ptr.alloc_id, mutable)?;
-                        }
-                        if let PrimVal::Ptr(ptr) = val2 {
-                            self.memory.mark_inner_allocation(ptr.alloc_id, mutable)?;
-                        }
-                    },
-                }
-                // see comment on `initialized` field
-                assert!(!global_value.initialized);
-                global_value.initialized = true;
-                assert_eq!(global_value.mutable, Mutability::Mutable);
-                global_value.mutable = mutable;
+            StackPopCleanup::MarkStatic(mutable) => if let Lvalue::Ptr{ ptr, .. } = frame.return_lvalue {
+                // FIXME: to_ptr()? might be too extreme here, static zsts might reach this under certain conditions
+                self.memory.mark_static_initalized(ptr.to_ptr()?.alloc_id, mutable)?
             } else {
                 bug!("StackPopCleanup::MarkStatic on: {:?}", frame.return_lvalue);
             },
@@ -543,7 +521,7 @@ pub fn deallocate_local(&mut self, local: Option<Value>) -> EvalResult<'tcx> {
 
     pub fn assign_discr_and_fields(
         &mut self,
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
         discr_offset: u64,
         operands: &[mir::Operand<'tcx>],
@@ -568,7 +546,7 @@ pub fn assign_discr_and_fields(
 
     pub fn assign_fields(
         &mut self,
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
         operands: &[mir::Operand<'tcx>],
     ) -> EvalResult<'tcx> {
@@ -1046,7 +1024,7 @@ pub fn eval_operand(&mut self, op: &mir::Operand<'tcx>) -> EvalResult<'tcx, Valu
                     Literal::Item { def_id, substs } => {
                         let instance = self.resolve_associated_const(def_id, substs);
                         let cid = GlobalId { instance, promoted: None };
-                        self.globals.get(&cid).expect("static/const not cached").value
+                        Value::by_ref(self.globals.get(&cid).expect("static/const not cached").into())
                     }
 
                     Literal::Promoted { index } => {
@@ -1054,7 +1032,7 @@ pub fn eval_operand(&mut self, op: &mir::Operand<'tcx>) -> EvalResult<'tcx, Valu
                             instance: self.frame().instance,
                             promoted: Some(index),
                         };
-                        self.globals.get(&cid).expect("promoted not cached").value
+                        Value::by_ref(self.globals.get(&cid).expect("promoted not cached").into())
                     }
                 };
 
@@ -1076,8 +1054,8 @@ fn copy(&mut self, src: Pointer, dest: Pointer, ty: Ty<'tcx>) -> EvalResult<'tcx
 
     pub fn force_allocation(
         &mut self,
-        lvalue: Lvalue<'tcx>,
-    ) -> EvalResult<'tcx, Lvalue<'tcx>> {
+        lvalue: Lvalue,
+    ) -> EvalResult<'tcx, Lvalue> {
         let new_lvalue = match lvalue {
             Lvalue::Local { frame, local } => {
                 // -1 since we don't store the return value
@@ -1098,28 +1076,6 @@ pub fn force_allocation(
                 }
             }
             Lvalue::Ptr { .. } => lvalue,
-            Lvalue::Global(cid) => {
-                let global_val = self.globals.get(&cid).expect("global not cached").clone();
-                match global_val.value {
-                    Value::ByRef { ptr, aligned } =>
-                        Lvalue::Ptr { ptr, aligned, extra: LvalueExtra::None },
-                    _ => {
-                        let ptr = self.alloc_ptr_with_substs(global_val.ty, cid.instance.substs)?;
-                        self.memory.mark_static(ptr.alloc_id);
-                        self.write_value_to_ptr(global_val.value, ptr.into(), global_val.ty)?;
-                        // see comment on `initialized` field
-                        if global_val.initialized {
-                            self.memory.mark_static_initalized(ptr.alloc_id, global_val.mutable)?;
-                        }
-                        let lval = self.globals.get_mut(&cid).expect("already checked");
-                        *lval = Global {
-                            value: Value::by_ref(ptr.into()),
-                            .. global_val
-                        };
-                        Lvalue::from_ptr(ptr)
-                    },
-                }
-            }
         };
         Ok(new_lvalue)
     }
@@ -1149,7 +1105,7 @@ pub fn value_to_primval(&mut self, value: Value, ty: Ty<'tcx>) -> EvalResult<'tc
 
     pub fn write_null(
         &mut self,
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
     ) -> EvalResult<'tcx> {
         self.write_primval(dest, PrimVal::Bytes(0), dest_ty)
@@ -1157,7 +1113,7 @@ pub fn write_null(
 
     pub fn write_ptr(
         &mut self,
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         val: Pointer,
         dest_ty: Ty<'tcx>,
     ) -> EvalResult<'tcx> {
@@ -1166,7 +1122,7 @@ pub fn write_ptr(
 
     pub fn write_primval(
         &mut self,
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         val: PrimVal,
         dest_ty: Ty<'tcx>,
     ) -> EvalResult<'tcx> {
@@ -1176,7 +1132,7 @@ pub fn write_primval(
     pub fn write_value(
         &mut self,
         src_val: Value,
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
     ) -> EvalResult<'tcx> {
         //trace!("Writing {:?} to {:?} at type {:?}", src_val, dest, dest_ty);
@@ -1185,21 +1141,6 @@ pub fn write_value(
         // correct if we never look at this data with the wrong type.
 
         match dest {
-            Lvalue::Global(cid) => {
-                let dest = self.globals.get_mut(&cid).expect("global should be cached").clone();
-                if dest.mutable == Mutability::Immutable {
-                    return err!(ModifiedConstantMemory);
-                }
-                let write_dest = |this: &mut Self, val| {
-                    *this.globals.get_mut(&cid).expect("already checked") = Global {
-                        value: val,
-                        ..dest
-                    };
-                    Ok(())
-                };
-                self.write_value_possibly_by_val(src_val, write_dest, dest.value, dest_ty)
-            },
-
             Lvalue::Ptr { ptr, extra, aligned } => {
                 assert_eq!(extra, LvalueExtra::None);
                 self.write_maybe_aligned_mut(aligned,
@@ -1542,7 +1483,7 @@ fn unsize_into_ptr(
         &mut self,
         src: Value,
         src_ty: Ty<'tcx>,
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
         sty: Ty<'tcx>,
         dty: Ty<'tcx>,
@@ -1578,7 +1519,7 @@ fn unsize_into(
         &mut self,
         src: Value,
         src_ty: Ty<'tcx>,
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
     ) -> EvalResult<'tcx> {
         match (&src_ty.sty, &dest_ty.sty) {
@@ -1640,7 +1581,7 @@ fn unsize_into(
         }
     }
 
-    pub fn dump_local(&self, lvalue: Lvalue<'tcx>) {
+    pub fn dump_local(&self, lvalue: Lvalue) {
         // Debug output
         if let Lvalue::Local { frame, local } = lvalue {
             let mut allocs = Vec::new();
@@ -1680,20 +1621,7 @@ pub fn dump_local(&self, lvalue: Lvalue<'tcx>) {
         }
     }
 
-    /// Convenience function to ensure correct usage of globals and code-sharing with locals.
-    pub fn modify_global<F>(&mut self, cid: GlobalId<'tcx>, f: F) -> EvalResult<'tcx>
-        where F: FnOnce(&mut Self, Value) -> EvalResult<'tcx, Value>,
-    {
-        let mut val = self.globals.get(&cid).expect("global not cached").clone();
-        if val.mutable == Mutability::Immutable {
-            return err!(ModifiedConstantMemory);
-        }
-        val.value = f(self, val.value)?;
-        *self.globals.get_mut(&cid).expect("already checked") = val;
-        Ok(())
-    }
-
-    /// Convenience function to ensure correct usage of locals and code-sharing with globals.
+    /// Convenience function to ensure correct usage of locals
     pub fn modify_local<F>(
         &mut self,
         frame: usize,
index 8722c96dbecd435e4863870f8c4df34a114cbf97..4485e936b1e7efe60d3bafaf127edd3026f4c5f3 100644 (file)
@@ -2,7 +2,6 @@
 use rustc::ty::layout::{Size, Align};
 use rustc::ty::{self, Ty};
 use rustc_data_structures::indexed_vec::Idx;
-use syntax::ast::Mutability;
 
 use super::{
     EvalResult,
@@ -13,7 +12,7 @@
 };
 
 #[derive(Copy, Clone, Debug)]
-pub enum Lvalue<'tcx> {
+pub enum Lvalue {
     /// An lvalue referring to a value allocated in the `Memory` system.
     Ptr {
         /// An lvalue may have an invalid (integral or undef) pointer,
@@ -31,9 +30,6 @@ pub enum Lvalue<'tcx> {
         frame: usize,
         local: mir::Local,
     },
-
-    /// An lvalue referring to a global
-    Global(GlobalId<'tcx>),
 }
 
 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
@@ -55,19 +51,7 @@ pub struct GlobalId<'tcx> {
     pub promoted: Option<mir::Promoted>,
 }
 
-#[derive(Clone, Debug)]
-pub struct Global<'tcx> {
-    pub value: Value,
-    /// Only used in `force_allocation` to ensure we don't mark the memory
-    /// before the static is initialized. It is possible to convert a
-    /// global which initially is `Value::ByVal(PrimVal::Undef)` and gets
-    /// lifted to an allocation before the static is fully initialized
-    pub(super) initialized: bool,
-    pub(super) mutable: Mutability,
-    pub(super) ty: Ty<'tcx>,
-}
-
-impl<'tcx> Lvalue<'tcx> {
+impl<'tcx> Lvalue {
     /// Produces an Lvalue that will error if attempted to be read from
     pub fn undef() -> Self {
         Self::from_primval_ptr(PrimVal::Undef.into())
@@ -113,26 +97,6 @@ pub(super) fn elem_ty_and_len(self, ty: Ty<'tcx>) -> (Ty<'tcx>, u64) {
     }
 }
 
-impl<'tcx> Global<'tcx> {
-    pub(super) fn uninitialized(ty: Ty<'tcx>) -> Self {
-        Global {
-            value: Value::ByVal(PrimVal::Undef),
-            mutable: Mutability::Mutable,
-            ty,
-            initialized: false,
-        }
-    }
-
-    pub(super) fn initialized(ty: Ty<'tcx>, value: Value, mutable: Mutability) -> Self {
-        Global {
-            value,
-            mutable,
-            ty,
-            initialized: true,
-        }
-    }
-}
-
 impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
     /// Reads a value from the lvalue without going through the intermediate step of obtaining
     /// a `miri::Lvalue`
@@ -147,7 +111,7 @@ pub fn try_read_lvalue(&mut self, lvalue: &mir::Lvalue<'tcx>) -> EvalResult<'tcx
             Static(ref static_) => {
                 let instance = ty::Instance::mono(self.tcx, static_.def_id);
                 let cid = GlobalId { instance, promoted: None };
-                Ok(Some(self.globals.get(&cid).expect("global not cached").value))
+                Ok(Some(Value::by_ref(self.globals.get(&cid).expect("global not cached").into())))
             },
             Projection(ref proj) => self.try_read_lvalue_projection(proj),
         }
@@ -195,7 +159,7 @@ pub(super) fn eval_and_read_lvalue(&mut self, lvalue: &mir::Lvalue<'tcx>) -> Eva
         self.read_lvalue(lvalue)
     }
 
-    pub fn read_lvalue(&self, lvalue: Lvalue<'tcx>) -> EvalResult<'tcx, Value> {
+    pub fn read_lvalue(&self, lvalue: Lvalue) -> EvalResult<'tcx, Value> {
         match lvalue {
             Lvalue::Ptr { ptr, extra, aligned } => {
                 assert_eq!(extra, LvalueExtra::None);
@@ -204,13 +168,10 @@ pub fn read_lvalue(&self, lvalue: Lvalue<'tcx>) -> EvalResult<'tcx, Value> {
             Lvalue::Local { frame, local } => {
                 self.stack[frame].get_local(local)
             }
-            Lvalue::Global(cid) => {
-                Ok(self.globals.get(&cid).expect("global not cached").value)
-            }
         }
     }
 
-    pub fn eval_lvalue(&mut self, mir_lvalue: &mir::Lvalue<'tcx>) -> EvalResult<'tcx, Lvalue<'tcx>> {
+    pub fn eval_lvalue(&mut self, mir_lvalue: &mir::Lvalue<'tcx>) -> EvalResult<'tcx, Lvalue> {
         use rustc::mir::Lvalue::*;
         let lvalue = match *mir_lvalue {
             Local(mir::RETURN_POINTER) => self.frame().return_lvalue,
@@ -218,7 +179,8 @@ pub fn eval_lvalue(&mut self, mir_lvalue: &mir::Lvalue<'tcx>) -> EvalResult<'tcx
 
             Static(ref static_) => {
                 let instance = ty::Instance::mono(self.tcx, static_.def_id);
-                Lvalue::Global(GlobalId { instance, promoted: None })
+                let gid = GlobalId { instance, promoted: None };
+                Lvalue::from_ptr(*self.globals.get(&gid).expect("uncached global"))
             }
 
             Projection(ref proj) => {
@@ -237,11 +199,11 @@ pub fn eval_lvalue(&mut self, mir_lvalue: &mir::Lvalue<'tcx>) -> EvalResult<'tcx
 
     pub fn lvalue_field(
         &mut self,
-        base: Lvalue<'tcx>,
+        base: Lvalue,
         field_index: usize,
         base_ty: Ty<'tcx>,
         field_ty: Ty<'tcx>,
-    ) -> EvalResult<'tcx, Lvalue<'tcx>> {
+    ) -> EvalResult<'tcx, Lvalue> {
         let base_layout = self.type_layout(base_ty)?;
         use rustc::ty::layout::Layout::*;
         let (offset, packed) = match *base_layout {
@@ -312,16 +274,6 @@ pub fn lvalue_field(
                 Value::ByValPair(..) |
                 Value::ByVal(_) => self.force_allocation(base)?.to_ptr_extra_aligned(),
             },
-            Lvalue::Global(cid) => match self.globals.get(&cid).expect("uncached global").value {
-                // in case the type has a single field, just return the value
-                Value::ByVal(_) if self.get_field_count(base_ty).map(|c| c == 1).unwrap_or(false) => {
-                    assert_eq!(offset.bytes(), 0, "ByVal can only have 1 non zst field with offset 0");
-                    return Ok(base);
-                },
-                Value::ByRef{..} |
-                Value::ByValPair(..) |
-                Value::ByVal(_) => self.force_allocation(base)?.to_ptr_extra_aligned(),
-            },
         };
 
         let offset = match base_extra {
@@ -352,7 +304,7 @@ pub fn lvalue_field(
         Ok(Lvalue::Ptr { ptr, extra, aligned: aligned && !packed })
     }
 
-    pub(super) fn val_to_lvalue(&self, val: Value, ty: Ty<'tcx>) -> EvalResult<'tcx, Lvalue<'tcx>> {
+    pub(super) fn val_to_lvalue(&self, val: Value, ty: Ty<'tcx>) -> EvalResult<'tcx, Lvalue> {
         Ok(match self.tcx.struct_tail(ty).sty {
             ty::TyDynamic(..) => {
                 let (ptr, vtable) = val.into_ptr_vtable_pair(&self.memory)?;
@@ -366,7 +318,7 @@ pub(super) fn val_to_lvalue(&self, val: Value, ty: Ty<'tcx>) -> EvalResult<'tcx,
         })
     }
 
-    pub(super) fn lvalue_index(&mut self, base: Lvalue<'tcx>, outer_ty: Ty<'tcx>, n: u64) -> EvalResult<'tcx, Lvalue<'tcx>> {
+    pub(super) fn lvalue_index(&mut self, base: Lvalue, outer_ty: Ty<'tcx>, n: u64) -> EvalResult<'tcx, Lvalue> {
         // Taking the outer type here may seem odd; it's needed because for array types, the outer type gives away the length.
         let base = self.force_allocation(base)?;
         let (base_ptr, _, aligned) = base.to_ptr_extra_aligned();
@@ -380,10 +332,10 @@ pub(super) fn lvalue_index(&mut self, base: Lvalue<'tcx>, outer_ty: Ty<'tcx>, n:
 
     pub(super) fn eval_lvalue_projection(
         &mut self,
-        base: Lvalue<'tcx>,
+        base: Lvalue,
         base_ty: Ty<'tcx>,
         proj_elem: &mir::ProjectionElem<'tcx, mir::Operand<'tcx>, Ty<'tcx>>,
-    ) -> EvalResult<'tcx, Lvalue<'tcx>> {
+    ) -> EvalResult<'tcx, Lvalue> {
         use rustc::mir::ProjectionElem::*;
         let (ptr, extra, aligned) = match *proj_elem {
             Field(field, field_ty) => {
index 0150a6c836d683058c19e1272d705a7e38bbaa15..c65c3f2e1038c2aed10e28b4ef0809a0806eb537 100644 (file)
@@ -33,7 +33,7 @@ pub trait Machine<'tcx>: Sized {
     fn eval_fn_call<'a>(
         ecx: &mut EvalContext<'a, 'tcx, Self>,
         instance: ty::Instance<'tcx>,
-        destination: Option<(Lvalue<'tcx>, mir::BasicBlock)>,
+        destination: Option<(Lvalue, mir::BasicBlock)>,
         arg_operands: &[mir::Operand<'tcx>],
         span: Span,
         sig: ty::FnSig<'tcx>,
@@ -44,7 +44,7 @@ fn call_intrinsic<'a>(
         ecx: &mut EvalContext<'a, 'tcx, Self>,
         instance: ty::Instance<'tcx>,
         args: &[mir::Operand<'tcx>],
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: ty::Ty<'tcx>,
         dest_layout: &'tcx ty::layout::Layout,
         target: mir::BasicBlock,
index c476046d3854ad2dbc090bd14ac53d4680b91f3f..1ea814d57141b3f31bfd0378a0982b4320d9b008 100644 (file)
@@ -234,12 +234,6 @@ pub struct Memory<'a, 'tcx, M: Machine<'tcx>> {
     /// The AllocId to assign to the next new regular allocation. Always incremented, never gets smaller.
     next_alloc_id: u64,
 
-    /// Set of statics, constants, promoteds, vtables, ... to prevent `mark_static_initalized` from
-    /// stepping out of its own allocations. This set only contains statics backed by an
-    /// allocation. If they are ByVal or ByValPair they are not here, but will be inserted once
-    /// they become ByRef.
-    static_alloc: HashSet<AllocId>,
-
     /// Number of virtual bytes allocated.
     memory_usage: u64,
 
@@ -280,7 +274,6 @@ pub fn new(layout: &'a TargetDataLayout, max_memory: u64, data: M::MemoryData) -
             layout,
             memory_size: max_memory,
             memory_usage: 0,
-            static_alloc: HashSet::new(),
             literal_alloc_cache: HashMap::new(),
             reads_are_aligned: Cell::new(true),
             writes_are_aligned: Cell::new(true),
@@ -859,18 +852,11 @@ fn get_bytes_mut(&mut self, ptr: MemoryPointer, size: u64, align: u64) -> EvalRe
 
 /// Reading and writing
 impl<'a, 'tcx, M: Machine<'tcx>> Memory<'a, 'tcx, M> {
-    /// mark an allocation as being the entry point to a static (see `static_alloc` field)
-    pub fn mark_static(&mut self, alloc_id: AllocId) {
-        trace!("mark_static: {:?}", alloc_id);
-        if !self.static_alloc.insert(alloc_id) {
-            bug!("tried to mark an allocation ({:?}) as static twice", alloc_id);
-        }
-    }
 
     /// mark an allocation pointed to by a static as static and initialized
     pub fn mark_inner_allocation(&mut self, alloc: AllocId, mutability: Mutability) -> EvalResult<'tcx> {
         // relocations into other statics are not "inner allocations"
-        if !self.static_alloc.contains(&alloc) {
+        if self.get(alloc).ok().map_or(false, |alloc| alloc.kind != Kind::UninitializedStatic) {
             self.mark_static_initalized(alloc, mutability)?;
         }
         Ok(())
index 39a0c7d25f9e7a3c3dfa9bf5f28584cfb41d3514..b2e5f134d3473672ac96ca0d1643bbc0e36a58eb 100644 (file)
@@ -38,7 +38,6 @@ macro_rules! err {
 pub use self::lvalue::{
     Lvalue,
     LvalueExtra,
-    Global,
     GlobalId,
 };
 
index a9675d148d6643971342be1d9330eca311a66376..8880be6e848fb2b4a3896842461138cb550a3049 100644 (file)
@@ -39,7 +39,7 @@ pub fn intrinsic_with_overflow(
         op: mir::BinOp,
         left: &mir::Operand<'tcx>,
         right: &mir::Operand<'tcx>,
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
     ) -> EvalResult<'tcx> {
         let (val, overflowed) = self.binop_with_overflow(op, left, right)?;
@@ -54,7 +54,7 @@ pub fn intrinsic_overflowing(
         op: mir::BinOp,
         left: &mir::Operand<'tcx>,
         right: &mir::Operand<'tcx>,
-        dest: Lvalue<'tcx>,
+        dest: Lvalue,
         dest_ty: Ty<'tcx>,
     ) -> EvalResult<'tcx, bool> {
         let (val, overflowed) = self.binop_with_overflow(op, left, right)?;
index 56195998b9e7a0ac8e17539b508a561d6f9709f1..ca3c8490e7a7888b739ea3628cfff9e8a79296b6 100644 (file)
@@ -14,9 +14,8 @@
 use super::{
     EvalResult,
     EvalContext, StackPopCleanup, TyAndPacked,
-    Global, GlobalId, Lvalue,
-    Value, PrimVal,
-    HasMemory,
+    GlobalId, Lvalue,
+    HasMemory, Kind,
     Machine,
 };
 
@@ -179,11 +178,18 @@ fn global_item(
         if self.tcx.has_attr(def_id, "linkage") {
             // FIXME: check that it's `#[linkage = "extern_weak"]`
             trace!("Initializing an extern global with NULL");
-            self.globals.insert(cid, Global::initialized(self.tcx.type_of(def_id), Value::ByVal(PrimVal::Bytes(0)), mutability));
+            let ptr_size = self.memory.pointer_size();
+            let ptr = self.memory.allocate(ptr_size, ptr_size, Kind::UninitializedStatic)?;
+            self.memory.write_usize(ptr, 0)?;
+            self.memory.mark_static_initalized(ptr.alloc_id, mutability)?;
+            self.globals.insert(cid, ptr);
             return Ok(false);
         }
         let mir = self.load_mir(instance.def)?;
-        self.globals.insert(cid, Global::uninitialized(mir.return_ty));
+        let size = self.type_size_with_substs(mir.return_ty, substs)?.expect("unsized global");
+        let align = self.type_align_with_substs(mir.return_ty, substs)?;
+        let ptr = self.memory.allocate(size, align, Kind::UninitializedStatic)?;
+        self.globals.insert(cid, ptr);
         let internally_mutable = !mir.return_ty.is_freeze(
                 self.tcx,
                 ty::ParamEnv::empty(Reveal::All),
@@ -200,7 +206,7 @@ fn global_item(
             instance,
             span,
             mir,
-            Lvalue::Global(cid),
+            Lvalue::from_ptr(ptr),
             cleanup,
         )?;
         Ok(true)
@@ -256,13 +262,15 @@ fn visit_constant(&mut self, constant: &mir::Constant<'tcx>, location: mir::Loca
                 }
                 let mir = &self.mir.promoted[index];
                 self.try(|this| {
-                    let ty = this.ecx.monomorphize(mir.return_ty, this.instance.substs);
-                    this.ecx.globals.insert(cid, Global::uninitialized(ty));
+                    let size = this.ecx.type_size_with_substs(mir.return_ty, this.instance.substs)?.expect("unsized global");
+                    let align = this.ecx.type_align_with_substs(mir.return_ty, this.instance.substs)?;
+                    let ptr = this.ecx.memory.allocate(size, align, Kind::UninitializedStatic)?;
+                    this.ecx.globals.insert(cid, ptr);
                     trace!("pushing stack frame for {:?}", index);
                     this.ecx.push_stack_frame(this.instance,
                                               constant.span,
                                               mir,
-                                              Lvalue::Global(cid),
+                                              Lvalue::from_ptr(ptr),
                                               StackPopCleanup::MarkStatic(Mutability::Immutable),
                     )?;
                     Ok(true)
index 5f4bc0b1bd9d5258e42dc3e6b0c11a17278b691d..527d5917b6682eb8df52f213fa95fbf2190d68b9 100644 (file)
@@ -11,7 +11,7 @@
 };
 
 impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
-    pub(crate) fn drop_lvalue(&mut self, lval: Lvalue<'tcx>, instance: ty::Instance<'tcx>, ty: Ty<'tcx>, span: Span) -> EvalResult<'tcx> {
+    pub(crate) fn drop_lvalue(&mut self, lval: Lvalue, instance: ty::Instance<'tcx>, ty: Ty<'tcx>, span: Span) -> EvalResult<'tcx> {
         trace!("drop_lvalue: {:#?}", lval);
         // We take the address of the object.  This may well be unaligned, which is fine for us here.
         // However, unaligned accesses will probably make the actual drop implementation fail -- a problem shared
index b9447a30ab154361baef1007e2b4f0b452f8d24e..a6412dedd3bfe481bbaf71223d138e654160a89e 100644 (file)
@@ -204,7 +204,7 @@ fn check_ty_compat<'tcx>(
     fn eval_fn_call(
         &mut self,
         instance: ty::Instance<'tcx>,
-        destination: Option<(Lvalue<'tcx>, mir::BasicBlock)>,
+        destination: Option<(Lvalue, mir::BasicBlock)>,
         arg_operands: &[mir::Operand<'tcx>],
         span: Span,
         sig: ty::FnSig<'tcx>,
index 83931535e599e143dc556ab39c6075bc0fee923e..459309abda7b7f214bbb61d133c1bd9bb25811d4 100644 (file)
@@ -16,7 +16,7 @@
     Machine,
 };
 
-pub type ValidationQuery<'tcx> = ValidationOperand<'tcx, Lvalue<'tcx>>;
+pub type ValidationQuery<'tcx> = ValidationOperand<'tcx, Lvalue>;
 
 #[derive(Copy, Clone, Debug)]
 enum ValidationMode {
@@ -242,8 +242,8 @@ fn try_validate(&mut self, mut query: ValidationQuery<'tcx>, mode: ValidationMod
                         }
                     }
                 }
-                Lvalue::Local { .. } | Lvalue::Global(..) => {
-                    // These are not backed by memory, so we have nothing to do.
+                Lvalue::Local { .. }  => {
+                    // Not backed by memory, so we have nothing to do.
                 }
             }
         }
index 88ffc57a8f0688df78c95d2df3b7120c62050b0f..cd1bde987fe50cdba207a4e99434e40945c94d49 100644 (file)
@@ -133,6 +133,12 @@ fn from(ptr: MemoryPointer) -> Self {
     }
 }
 
+impl<'a> ::std::convert::From<&'a MemoryPointer> for Pointer {
+    fn from(ptr: &'a MemoryPointer) -> Self {
+        PrimVal::Ptr(*ptr).into()
+    }
+}
+
 /// A `PrimVal` represents an immediate, primitive value existing outside of a
 /// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
 /// size. Like a range of bytes in an `Allocation`, a `PrimVal` can either represent the raw bytes