Closed
Description
Miri, CTFE, and const-prop are implemented by using a MIR interpreter that can be customized by implementing the Machine
trait. Currently, the CTFE and const-prop machines have quite a bit of common code that is duplicated. It would be nice to reduce the code duplication.
One idea that could be used to reduce the code duplication would be to add a new trait CommonMachine
and put all the common implementation into it:
pub trait CommonMachine<'mir, 'tcx>: Sized {
/// Borrow the current thread's stack.
fn stack(
ecx: &'a InterpCx<'mir, 'tcx, Self>,
) -> &'a [Frame<'mir, 'tcx, (), ()>];
/// Mutably borrow the current thread's stack.
fn stack_mut(
ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
) -> &'a mut Vec<Frame<'mir, 'tcx, (), ()>>;
// Other methods that require custom implementations for the CTFE and const-prop machines.
}
impl<'mir, 'tcx, T: CommonMachine<'mir, 'tcx>> super::machine::Machine<'mir, 'tcx> for T {
type MemoryKind = !;
type PointerTag = ();
type ExtraFnVal = !;
type FrameExtra = ();
type MemoryExtra = ();
type AllocExtra = ();
#[inline(always)]
fn enforce_alignment(_memory_extra: &Self::MemoryExtra) -> bool {
false
}
/// Borrow the current thread's stack.
fn stack(
ecx: &'a InterpCx<'mir, 'tcx, Self>,
) -> &'a [Frame<'mir, 'tcx, (), ()>] {
CommonMachine::stack(ecx)
}
/// Mutably borrow the current thread's stack.
fn stack_mut(
ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
) -> &'a mut Vec<Frame<'mir, 'tcx, (), ()>> {
CommonMachine::stack_mut(ecx)
}
// TODO: other methods
}