|
| 1 | +use crate::interpret::{self, HasStaticRootDefId, ImmTy, Immediate, InterpCx, PointerArithmetic}; |
| 2 | +use rustc_middle::mir::interpret::{AllocId, ConstAllocation, InterpResult}; |
| 3 | +use rustc_middle::mir::*; |
| 4 | +use rustc_middle::query::TyCtxtAt; |
| 5 | +use rustc_middle::ty; |
| 6 | +use rustc_middle::ty::layout::TyAndLayout; |
| 7 | +use rustc_span::def_id::DefId; |
| 8 | + |
| 9 | +/// Macro for machine-specific `InterpError` without allocation. |
| 10 | +/// (These will never be shown to the user, but they help diagnose ICEs.) |
| 11 | +pub macro throw_machine_stop_str($($tt:tt)*) {{ |
| 12 | + // We make a new local type for it. The type itself does not carry any information, |
| 13 | + // but its vtable (for the `MachineStopType` trait) does. |
| 14 | + #[derive(Debug)] |
| 15 | + struct Zst; |
| 16 | + // Printing this type shows the desired string. |
| 17 | + impl std::fmt::Display for Zst { |
| 18 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 19 | + write!(f, $($tt)*) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + impl rustc_middle::mir::interpret::MachineStopType for Zst { |
| 24 | + fn diagnostic_message(&self) -> rustc_errors::DiagMessage { |
| 25 | + self.to_string().into() |
| 26 | + } |
| 27 | + |
| 28 | + fn add_args( |
| 29 | + self: Box<Self>, |
| 30 | + _: &mut dyn FnMut(rustc_errors::DiagArgName, rustc_errors::DiagArgValue), |
| 31 | + ) {} |
| 32 | + } |
| 33 | + throw_machine_stop!(Zst) |
| 34 | +}} |
| 35 | + |
| 36 | +pub struct DummyMachine; |
| 37 | + |
| 38 | +impl HasStaticRootDefId for DummyMachine { |
| 39 | + fn static_def_id(&self) -> Option<rustc_hir::def_id::LocalDefId> { |
| 40 | + None |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<'mir, 'tcx: 'mir> interpret::Machine<'mir, 'tcx> for DummyMachine { |
| 45 | + interpret::compile_time_machine!(<'mir, 'tcx>); |
| 46 | + type MemoryKind = !; |
| 47 | + const PANIC_ON_ALLOC_FAIL: bool = true; |
| 48 | + |
| 49 | + #[inline(always)] |
| 50 | + fn enforce_alignment(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool { |
| 51 | + false // no reason to enforce alignment |
| 52 | + } |
| 53 | + |
| 54 | + fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>, _layout: TyAndLayout<'tcx>) -> bool { |
| 55 | + false |
| 56 | + } |
| 57 | + |
| 58 | + fn before_access_global( |
| 59 | + _tcx: TyCtxtAt<'tcx>, |
| 60 | + _machine: &Self, |
| 61 | + _alloc_id: AllocId, |
| 62 | + alloc: ConstAllocation<'tcx>, |
| 63 | + _static_def_id: Option<DefId>, |
| 64 | + is_write: bool, |
| 65 | + ) -> InterpResult<'tcx> { |
| 66 | + if is_write { |
| 67 | + throw_machine_stop_str!("can't write to global"); |
| 68 | + } |
| 69 | + |
| 70 | + // If the static allocation is mutable, then we can't const prop it as its content |
| 71 | + // might be different at runtime. |
| 72 | + if alloc.inner().mutability.is_mut() { |
| 73 | + throw_machine_stop_str!("can't access mutable globals in ConstProp"); |
| 74 | + } |
| 75 | + |
| 76 | + Ok(()) |
| 77 | + } |
| 78 | + |
| 79 | + fn find_mir_or_eval_fn( |
| 80 | + _ecx: &mut InterpCx<'mir, 'tcx, Self>, |
| 81 | + _instance: ty::Instance<'tcx>, |
| 82 | + _abi: rustc_target::spec::abi::Abi, |
| 83 | + _args: &[interpret::FnArg<'tcx, Self::Provenance>], |
| 84 | + _destination: &interpret::MPlaceTy<'tcx, Self::Provenance>, |
| 85 | + _target: Option<BasicBlock>, |
| 86 | + _unwind: UnwindAction, |
| 87 | + ) -> interpret::InterpResult<'tcx, Option<(&'mir Body<'tcx>, ty::Instance<'tcx>)>> { |
| 88 | + unimplemented!() |
| 89 | + } |
| 90 | + |
| 91 | + fn panic_nounwind( |
| 92 | + _ecx: &mut InterpCx<'mir, 'tcx, Self>, |
| 93 | + _msg: &str, |
| 94 | + ) -> interpret::InterpResult<'tcx> { |
| 95 | + unimplemented!() |
| 96 | + } |
| 97 | + |
| 98 | + fn call_intrinsic( |
| 99 | + _ecx: &mut InterpCx<'mir, 'tcx, Self>, |
| 100 | + _instance: ty::Instance<'tcx>, |
| 101 | + _args: &[interpret::OpTy<'tcx, Self::Provenance>], |
| 102 | + _destination: &interpret::MPlaceTy<'tcx, Self::Provenance>, |
| 103 | + _target: Option<BasicBlock>, |
| 104 | + _unwind: UnwindAction, |
| 105 | + ) -> interpret::InterpResult<'tcx> { |
| 106 | + unimplemented!() |
| 107 | + } |
| 108 | + |
| 109 | + fn assert_panic( |
| 110 | + _ecx: &mut InterpCx<'mir, 'tcx, Self>, |
| 111 | + _msg: &rustc_middle::mir::AssertMessage<'tcx>, |
| 112 | + _unwind: UnwindAction, |
| 113 | + ) -> interpret::InterpResult<'tcx> { |
| 114 | + unimplemented!() |
| 115 | + } |
| 116 | + |
| 117 | + fn binary_ptr_op( |
| 118 | + ecx: &InterpCx<'mir, 'tcx, Self>, |
| 119 | + bin_op: BinOp, |
| 120 | + left: &interpret::ImmTy<'tcx, Self::Provenance>, |
| 121 | + right: &interpret::ImmTy<'tcx, Self::Provenance>, |
| 122 | + ) -> interpret::InterpResult<'tcx, (ImmTy<'tcx, Self::Provenance>, bool)> { |
| 123 | + use rustc_middle::mir::BinOp::*; |
| 124 | + Ok(match bin_op { |
| 125 | + Eq | Ne | Lt | Le | Gt | Ge => { |
| 126 | + // Types can differ, e.g. fn ptrs with different `for`. |
| 127 | + assert_eq!(left.layout.abi, right.layout.abi); |
| 128 | + let size = ecx.pointer_size(); |
| 129 | + // Just compare the bits. ScalarPairs are compared lexicographically. |
| 130 | + // We thus always compare pairs and simply fill scalars up with 0. |
| 131 | + // If the pointer has provenance, `to_bits` will return `Err` and we bail out. |
| 132 | + let left = match **left { |
| 133 | + Immediate::Scalar(l) => (l.to_bits(size)?, 0), |
| 134 | + Immediate::ScalarPair(l1, l2) => (l1.to_bits(size)?, l2.to_bits(size)?), |
| 135 | + Immediate::Uninit => panic!("we should never see uninit data here"), |
| 136 | + }; |
| 137 | + let right = match **right { |
| 138 | + Immediate::Scalar(r) => (r.to_bits(size)?, 0), |
| 139 | + Immediate::ScalarPair(r1, r2) => (r1.to_bits(size)?, r2.to_bits(size)?), |
| 140 | + Immediate::Uninit => panic!("we should never see uninit data here"), |
| 141 | + }; |
| 142 | + let res = match bin_op { |
| 143 | + Eq => left == right, |
| 144 | + Ne => left != right, |
| 145 | + Lt => left < right, |
| 146 | + Le => left <= right, |
| 147 | + Gt => left > right, |
| 148 | + Ge => left >= right, |
| 149 | + _ => bug!(), |
| 150 | + }; |
| 151 | + (ImmTy::from_bool(res, *ecx.tcx), false) |
| 152 | + } |
| 153 | + |
| 154 | + // Some more operations are possible with atomics. |
| 155 | + // The return value always has the provenance of the *left* operand. |
| 156 | + Add | Sub | BitOr | BitAnd | BitXor => { |
| 157 | + throw_machine_stop_str!("pointer arithmetic is not handled") |
| 158 | + } |
| 159 | + |
| 160 | + _ => span_bug!(ecx.cur_span(), "Invalid operator on pointers: {:?}", bin_op), |
| 161 | + }) |
| 162 | + } |
| 163 | + |
| 164 | + fn expose_ptr( |
| 165 | + _ecx: &mut InterpCx<'mir, 'tcx, Self>, |
| 166 | + _ptr: interpret::Pointer<Self::Provenance>, |
| 167 | + ) -> interpret::InterpResult<'tcx> { |
| 168 | + unimplemented!() |
| 169 | + } |
| 170 | + |
| 171 | + fn init_frame_extra( |
| 172 | + _ecx: &mut InterpCx<'mir, 'tcx, Self>, |
| 173 | + _frame: interpret::Frame<'mir, 'tcx, Self::Provenance>, |
| 174 | + ) -> interpret::InterpResult< |
| 175 | + 'tcx, |
| 176 | + interpret::Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>, |
| 177 | + > { |
| 178 | + unimplemented!() |
| 179 | + } |
| 180 | + |
| 181 | + fn stack<'a>( |
| 182 | + _ecx: &'a InterpCx<'mir, 'tcx, Self>, |
| 183 | + ) -> &'a [interpret::Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>] { |
| 184 | + // Return an empty stack instead of panicking, as `cur_span` uses it to evaluate constants. |
| 185 | + &[] |
| 186 | + } |
| 187 | + |
| 188 | + fn stack_mut<'a>( |
| 189 | + _ecx: &'a mut InterpCx<'mir, 'tcx, Self>, |
| 190 | + ) -> &'a mut Vec<interpret::Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>> { |
| 191 | + unimplemented!() |
| 192 | + } |
| 193 | +} |
0 commit comments