129 lines
4.1 KiB
Rust
Raw Normal View History

2017-07-21 17:25:30 +02:00
//! This module contains everything needed to instantiate an interpreter.
//! This separation exists to ensure that no fancy miri features like
//! interpreting common C functions leak into CTFE.
use std::hash::Hash;
2018-05-21 00:37:44 +02:00
use rustc::mir::interpret::{AllocId, EvalResult, Scalar, Pointer, AccessKind, GlobalId};
use super::{EvalContext, PlaceTy, OpTy, Memory};
2017-07-21 17:25:30 +02:00
use rustc::mir;
use rustc::ty::{self, layout::TyLayout};
use rustc::ty::layout::Size;
2018-08-18 12:14:03 +02:00
use syntax::source_map::Span;
use syntax::ast::Mutability;
2017-07-21 17:25:30 +02:00
/// Methods of this trait signifies a point where CTFE evaluation would fail
/// and some use case dependent behaviour can instead be applied
pub trait Machine<'mir, 'tcx>: Clone + Eq + Hash {
2017-07-21 17:25:30 +02:00
/// Additional data that can be accessed via the Memory
type MemoryData: Clone + Eq + Hash;
2017-07-21 17:25:30 +02:00
/// Additional memory kinds a machine wishes to distinguish from the builtin ones
type MemoryKinds: ::std::fmt::Debug + PartialEq + Copy + Clone;
/// Entry point to all function calls.
///
/// Returns Ok(true) when the function was handled completely
2017-07-28 10:16:19 +02:00
/// e.g. due to missing mir
///
/// Returns Ok(false) if a new stack frame was pushed
fn eval_fn_call<'a>(
2018-01-16 09:31:48 +01:00
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
2017-07-21 17:25:30 +02:00
instance: ty::Instance<'tcx>,
destination: Option<(PlaceTy<'tcx>, mir::BasicBlock)>,
args: &[OpTy<'tcx>],
span: Span,
) -> EvalResult<'tcx, bool>;
/// directly process an intrinsic without pushing a stack frame.
fn call_intrinsic<'a>(
2018-01-16 09:31:48 +01:00
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
instance: ty::Instance<'tcx>,
args: &[OpTy<'tcx>],
dest: PlaceTy<'tcx>,
target: mir::BasicBlock,
) -> EvalResult<'tcx>;
2017-08-01 11:11:57 +02:00
/// Called for all binary operations except on float types.
///
/// Returns `None` if the operation should be handled by the integer
2017-08-01 11:11:57 +02:00
/// op code in order to share more code between machines
///
2017-08-01 11:11:57 +02:00
/// Returns a (value, overflowed) pair if the operation succeeded
fn try_ptr_op<'a>(
2018-01-16 09:31:48 +01:00
ecx: &EvalContext<'a, 'mir, 'tcx, Self>,
bin_op: mir::BinOp,
2018-05-20 23:43:16 +02:00
left: Scalar,
left_layout: TyLayout<'tcx>,
2018-05-20 23:43:16 +02:00
right: Scalar,
right_layout: TyLayout<'tcx>,
2018-05-20 23:43:16 +02:00
) -> EvalResult<'tcx, Option<(Scalar, bool)>>;
/// Called when trying to mark machine defined `MemoryKinds` as static
2018-01-16 09:31:48 +01:00
fn mark_static_initialized<'a>(
_mem: &mut Memory<'a, 'mir, 'tcx, Self>,
_id: AllocId,
_mutability: Mutability,
) -> EvalResult<'tcx, bool>;
/// Called when requiring a pointer to a static. Non const eval can
/// create a mutable memory location for `static mut`
fn init_static<'a>(
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
cid: GlobalId<'tcx>,
) -> EvalResult<'tcx, AllocId>;
/// Heap allocations via the `box` keyword
///
/// Returns a pointer to the allocated memory
fn box_alloc<'a>(
2018-01-16 09:31:48 +01:00
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
dest: PlaceTy<'tcx>,
) -> EvalResult<'tcx>;
/// Called when trying to access a global declared with a `linkage` attribute
fn global_item_with_linkage<'a>(
2018-01-16 09:31:48 +01:00
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
instance: ty::Instance<'tcx>,
mutability: Mutability,
) -> EvalResult<'tcx>;
2017-12-14 11:36:28 +01:00
fn check_locks<'a>(
2018-01-16 09:31:48 +01:00
_mem: &Memory<'a, 'mir, 'tcx, Self>,
2018-05-21 00:37:44 +02:00
_ptr: Pointer,
_size: Size,
2017-12-14 11:36:28 +01:00
_access: AccessKind,
) -> EvalResult<'tcx> {
Ok(())
}
fn add_lock<'a>(
2018-01-16 09:31:48 +01:00
_mem: &mut Memory<'a, 'mir, 'tcx, Self>,
2018-01-05 05:12:38 +02:00
_id: AllocId,
2017-12-14 11:36:28 +01:00
) {}
fn free_lock<'a>(
2018-01-16 09:31:48 +01:00
_mem: &mut Memory<'a, 'mir, 'tcx, Self>,
2018-01-05 05:12:38 +02:00
_id: AllocId,
2017-12-14 11:36:28 +01:00
_len: u64,
) -> EvalResult<'tcx> {
Ok(())
}
fn end_region<'a>(
2018-01-16 09:31:48 +01:00
_ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
2017-12-14 11:36:28 +01:00
_reg: Option<::rustc::middle::region::Scope>,
) -> EvalResult<'tcx> {
Ok(())
}
fn validation_op<'a>(
2018-01-16 09:31:48 +01:00
_ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
2017-12-14 11:36:28 +01:00
_op: ::rustc::mir::ValidationOp,
_operand: &::rustc::mir::ValidationOperand<'tcx, ::rustc::mir::Place<'tcx>>,
) -> EvalResult<'tcx> {
Ok(())
}
2017-07-21 17:25:30 +02:00
}