]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/interpret/machine.rs
140bd946c78718aca9d97b2e12bbca8dd6b7e35c
[rust.git] / src / librustc_mir / interpret / machine.rs
1 //! This module contains everything needed to instantiate an interpreter.
2 //! This separation exists to ensure that no fancy miri features like
3 //! interpreting common C functions leak into CTFE.
4
5 use super::{
6     EvalResult,
7     EvalContext,
8     Lvalue,
9     PrimVal
10 };
11
12 use rustc::{mir, ty};
13 use syntax::codemap::Span;
14
15 /// Methods of this trait signifies a point where CTFE evaluation would fail
16 /// and some use case dependent behaviour can instead be applied
17 pub trait Machine<'tcx>: Sized {
18     /// Additional data that can be accessed via the EvalContext
19     type Data;
20
21     /// Additional data that can be accessed via the Memory
22     type MemoryData;
23
24     /// Additional memory kinds a machine wishes to distinguish from the builtin ones
25     type MemoryKinds: ::std::fmt::Debug + PartialEq + Copy + Clone;
26
27     /// Entry point to all function calls.
28     ///
29     /// Returns Ok(true) when the function was handled completely
30     /// e.g. due to missing mir
31     ///
32     /// Returns Ok(false) if a new stack frame was pushed
33     fn eval_fn_call<'a>(
34         ecx: &mut EvalContext<'a, 'tcx, Self>,
35         instance: ty::Instance<'tcx>,
36         destination: Option<(Lvalue<'tcx>, mir::BasicBlock)>,
37         arg_operands: &[mir::Operand<'tcx>],
38         span: Span,
39         sig: ty::FnSig<'tcx>,
40     ) -> EvalResult<'tcx, bool>;
41
42     /// directly process an intrinsic without pushing a stack frame.
43     fn call_intrinsic<'a>(
44         ecx: &mut EvalContext<'a, 'tcx, Self>,
45         instance: ty::Instance<'tcx>,
46         args: &[mir::Operand<'tcx>],
47         dest: Lvalue<'tcx>,
48         dest_ty: ty::Ty<'tcx>,
49         dest_layout: &'tcx ty::layout::Layout,
50         target: mir::BasicBlock,
51     ) -> EvalResult<'tcx>;
52
53     /// Called when operating on the value of pointers.
54     ///
55     /// Returns `None` if the operation should be handled by the integer
56     /// op code
57     ///
58     /// Returns a (value, overflowed) pair otherwise
59     fn ptr_op<'a>(
60         ecx: &EvalContext<'a, 'tcx, Self>,
61         bin_op: mir::BinOp,
62         left: PrimVal,
63         left_ty: ty::Ty<'tcx>,
64         right: PrimVal,
65         right_ty: ty::Ty<'tcx>,
66     ) -> EvalResult<'tcx, Option<(PrimVal, bool)>>;
67
68     /// Called when trying to mark machine defined `MemoryKinds` as static
69     fn mark_static_initialized(m: Self::MemoryKinds) -> EvalResult<'tcx>;
70
71     /// Heap allocations via the `box` keyword
72     ///
73     /// Returns a pointer to the allocated memory
74     fn box_alloc<'a>(
75         ecx: &mut EvalContext<'a, 'tcx, Self>,
76         ty: ty::Ty<'tcx>,
77     ) -> EvalResult<'tcx, PrimVal>;
78 }
79