Skip to content

Commit 7b90ff9

Browse files
committed
add after_stack_push hook; add public ImmTy::from_immediate method, and make ImmTy::imm field private
1 parent 1a3bda6 commit 7b90ff9

File tree

6 files changed

+43
-28
lines changed

6 files changed

+43
-28
lines changed

src/librustc_mir/const_eval/eval_queries.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{error_to_const_error, CompileTimeEvalContext, CompileTimeInterpreter, MemoryExtra};
22
use crate::interpret::eval_nullary_intrinsic;
33
use crate::interpret::{
4-
intern_const_alloc_recursive, Allocation, ConstValue, GlobalId, ImmTy, Immediate, InternKind,
4+
intern_const_alloc_recursive, Allocation, ConstValue, GlobalId, Immediate, InternKind,
55
InterpCx, InterpResult, MPlaceTy, MemoryKind, OpTy, RawConst, RefTracking, Scalar,
66
ScalarMaybeUndef, StackPopCleanup,
77
};
@@ -147,24 +147,26 @@ pub(super) fn op_to_const<'tcx>(
147147
match immediate {
148148
Ok(mplace) => to_const_value(mplace),
149149
// see comment on `let try_as_immediate` above
150-
Err(ImmTy { imm: Immediate::Scalar(x), .. }) => match x {
151-
ScalarMaybeUndef::Scalar(s) => ConstValue::Scalar(s),
152-
ScalarMaybeUndef::Undef => to_const_value(op.assert_mem_place(ecx)),
153-
},
154-
Err(ImmTy { imm: Immediate::ScalarPair(a, b), .. }) => {
155-
let (data, start) = match a.not_undef().unwrap() {
156-
Scalar::Ptr(ptr) => {
157-
(ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id), ptr.offset.bytes())
158-
}
159-
Scalar::Raw { .. } => (
160-
ecx.tcx.intern_const_alloc(Allocation::from_byte_aligned_bytes(b"" as &[u8])),
161-
0,
162-
),
163-
};
164-
let len = b.to_machine_usize(&ecx.tcx.tcx).unwrap();
165-
let start = start.try_into().unwrap();
166-
let len: usize = len.try_into().unwrap();
167-
ConstValue::Slice { data, start, end: start + len }
150+
Err(imm) => match *imm {
151+
Immediate::Scalar(x) => match x {
152+
ScalarMaybeUndef::Scalar(s) => ConstValue::Scalar(s),
153+
ScalarMaybeUndef::Undef => to_const_value(op.assert_mem_place(ecx)),
154+
},
155+
Immediate::ScalarPair(a, b) => {
156+
let (data, start) = match a.not_undef().unwrap() {
157+
Scalar::Ptr(ptr) => {
158+
(ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id), ptr.offset.bytes())
159+
}
160+
Scalar::Raw { .. } => (
161+
ecx.tcx.intern_const_alloc(Allocation::from_byte_aligned_bytes(b"" as &[u8])),
162+
0,
163+
),
164+
};
165+
let len = b.to_machine_usize(&ecx.tcx.tcx).unwrap();
166+
let start = start.try_into().unwrap();
167+
let len: usize = len.try_into().unwrap();
168+
ConstValue::Slice { data, start, end: start + len }
169+
}
168170
}
169171
}
170172
}

src/librustc_mir/interpret/eval_context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
646646
self.frame_mut().locals = locals;
647647
}
648648

649+
M::after_stack_push(self)?;
649650
info!("ENTERING({}) {}", self.cur_frame(), self.frame().instance);
650651

651652
if self.stack.len() > *self.tcx.sess.recursion_limit.get() {
@@ -751,7 +752,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
751752
// We want to skip the `info!` below, hence early return.
752753
return Ok(());
753754
}
754-
// Normal return.
755+
// Normal return, figure out where to jump.
755756
if unwinding {
756757
// Follow the unwind edge.
757758
let unwind = next_block.expect("Encountered StackPopCleanup::None when unwinding!");

src/librustc_mir/interpret/machine.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ pub trait Machine<'mir, 'tcx>: Sized {
285285
frame: Frame<'mir, 'tcx, Self::PointerTag>,
286286
) -> InterpResult<'tcx, Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>>;
287287

288+
/// Called immediately after a stack frame got pushed and its locals got initialized.
289+
fn after_stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
290+
Ok(())
291+
}
292+
288293
/// Called immediately after a stack frame got popped, but before jumping back to the caller.
289294
fn after_stack_pop(
290295
_ecx: &mut InterpCx<'mir, 'tcx, Self>,

src/librustc_mir/interpret/operand.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'tcx, Tag> Immediate<Tag> {
8787
// as input for binary and cast operations.
8888
#[derive(Copy, Clone, Debug)]
8989
pub struct ImmTy<'tcx, Tag = ()> {
90-
pub(crate) imm: Immediate<Tag>,
90+
imm: Immediate<Tag>,
9191
pub layout: TyAndLayout<'tcx>,
9292
}
9393

@@ -183,6 +183,11 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
183183
ImmTy { imm: val.into(), layout }
184184
}
185185

186+
#[inline]
187+
pub fn from_immediate(imm: Immediate<Tag>, layout: TyAndLayout<'tcx>) -> Self {
188+
ImmTy { imm, layout }
189+
}
190+
186191
#[inline]
187192
pub fn try_from_uint(i: impl Into<u128>, layout: TyAndLayout<'tcx>) -> Option<Self> {
188193
Some(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout))
@@ -424,7 +429,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
424429
Ok(OpTy { op, layout })
425430
}
426431

427-
/// Every place can be read from, so we can turn them into an operand
432+
/// Every place can be read from, so we can turn them into an operand.
433+
/// This will definitely return `Indirect` if the place is a `Ptr`, i.e., this
434+
/// will never actually read from memory.
428435
#[inline(always)]
429436
pub fn place_to_op(
430437
&self,

src/librustc_mir/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'tcx, Tag: ::std::fmt::Debug + Copy> OpTy<'tcx, Tag> {
247247
Operand::Immediate(_) if self.layout.is_zst() => {
248248
Ok(MPlaceTy::dangling(self.layout, cx))
249249
}
250-
Operand::Immediate(imm) => Err(ImmTy { imm, layout: self.layout }),
250+
Operand::Immediate(imm) => Err(ImmTy::from_immediate(imm, self.layout)),
251251
}
252252
}
253253

src/librustc_mir/interpret/terminator.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
407407
let this_receiver_ptr = self.layout_of(receiver_ptr_ty)?.field(self, 0)?;
408408
// Adjust receiver argument.
409409
args[0] =
410-
OpTy::from(ImmTy { layout: this_receiver_ptr, imm: receiver_place.ptr.into() });
410+
OpTy::from(ImmTy::from_immediate(receiver_place.ptr.into(), this_receiver_ptr));
411411
trace!("Patched self operand to {:#?}", args[0]);
412412
// recurse with concrete function
413413
self.eval_fn_call(drop_fn, caller_abi, &args, ret, unwind)
@@ -436,10 +436,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
436436
_ => (instance, place),
437437
};
438438

439-
let arg = ImmTy {
440-
imm: place.to_ref(),
441-
layout: self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
442-
};
439+
let arg = ImmTy::from_immediate(
440+
place.to_ref(),
441+
self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
442+
);
443443

444444
let ty = self.tcx.mk_unit(); // return type is ()
445445
let dest = MPlaceTy::dangling(self.layout_of(ty)?, self);

0 commit comments

Comments
 (0)