]>
Commit | Line | Data |
---|---|---|
ea8adc8c XL |
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 | ||
8faf50e0 XL |
5 | use std::hash::Hash; |
6 | ||
94b46f34 | 7 | use rustc::mir::interpret::{AllocId, EvalResult, Scalar, Pointer, AccessKind, GlobalId}; |
ff7c6d11 | 8 | use super::{EvalContext, Place, ValTy, Memory}; |
ea8adc8c | 9 | |
ff7c6d11 XL |
10 | use rustc::mir; |
11 | use rustc::ty::{self, Ty}; | |
94b46f34 | 12 | use rustc::ty::layout::Size; |
ea8adc8c XL |
13 | use syntax::codemap::Span; |
14 | use syntax::ast::Mutability; | |
15 | ||
16 | /// Methods of this trait signifies a point where CTFE evaluation would fail | |
17 | /// and some use case dependent behaviour can instead be applied | |
8faf50e0 | 18 | pub trait Machine<'mir, 'tcx>: Clone + Eq + Hash { |
ea8adc8c | 19 | /// Additional data that can be accessed via the Memory |
8faf50e0 | 20 | type MemoryData: Clone + Eq + Hash; |
ea8adc8c XL |
21 | |
22 | /// Additional memory kinds a machine wishes to distinguish from the builtin ones | |
23 | type MemoryKinds: ::std::fmt::Debug + PartialEq + Copy + Clone; | |
24 | ||
25 | /// Entry point to all function calls. | |
26 | /// | |
27 | /// Returns Ok(true) when the function was handled completely | |
28 | /// e.g. due to missing mir | |
29 | /// | |
30 | /// Returns Ok(false) if a new stack frame was pushed | |
31 | fn eval_fn_call<'a>( | |
0531ce1d | 32 | ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, |
ea8adc8c | 33 | instance: ty::Instance<'tcx>, |
ff7c6d11 | 34 | destination: Option<(Place, mir::BasicBlock)>, |
ea8adc8c XL |
35 | args: &[ValTy<'tcx>], |
36 | span: Span, | |
37 | sig: ty::FnSig<'tcx>, | |
38 | ) -> EvalResult<'tcx, bool>; | |
39 | ||
40 | /// directly process an intrinsic without pushing a stack frame. | |
41 | fn call_intrinsic<'a>( | |
0531ce1d | 42 | ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, |
ea8adc8c XL |
43 | instance: ty::Instance<'tcx>, |
44 | args: &[ValTy<'tcx>], | |
ff7c6d11 XL |
45 | dest: Place, |
46 | dest_layout: ty::layout::TyLayout<'tcx>, | |
ea8adc8c XL |
47 | target: mir::BasicBlock, |
48 | ) -> EvalResult<'tcx>; | |
49 | ||
50 | /// Called for all binary operations except on float types. | |
51 | /// | |
52 | /// Returns `None` if the operation should be handled by the integer | |
53 | /// op code in order to share more code between machines | |
54 | /// | |
55 | /// Returns a (value, overflowed) pair if the operation succeeded | |
56 | fn try_ptr_op<'a>( | |
0531ce1d | 57 | ecx: &EvalContext<'a, 'mir, 'tcx, Self>, |
ea8adc8c | 58 | bin_op: mir::BinOp, |
94b46f34 | 59 | left: Scalar, |
ff7c6d11 | 60 | left_ty: Ty<'tcx>, |
94b46f34 | 61 | right: Scalar, |
ff7c6d11 | 62 | right_ty: Ty<'tcx>, |
94b46f34 | 63 | ) -> EvalResult<'tcx, Option<(Scalar, bool)>>; |
ea8adc8c XL |
64 | |
65 | /// Called when trying to mark machine defined `MemoryKinds` as static | |
0531ce1d XL |
66 | fn mark_static_initialized<'a>( |
67 | _mem: &mut Memory<'a, 'mir, 'tcx, Self>, | |
68 | _id: AllocId, | |
69 | _mutability: Mutability, | |
70 | ) -> EvalResult<'tcx, bool>; | |
71 | ||
72 | /// Called when requiring a pointer to a static. Non const eval can | |
73 | /// create a mutable memory location for `static mut` | |
74 | fn init_static<'a>( | |
75 | ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, | |
76 | cid: GlobalId<'tcx>, | |
77 | ) -> EvalResult<'tcx, AllocId>; | |
ea8adc8c XL |
78 | |
79 | /// Heap allocations via the `box` keyword | |
80 | /// | |
81 | /// Returns a pointer to the allocated memory | |
82 | fn box_alloc<'a>( | |
0531ce1d | 83 | ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, |
ff7c6d11 XL |
84 | ty: Ty<'tcx>, |
85 | dest: Place, | |
86 | ) -> EvalResult<'tcx>; | |
ea8adc8c XL |
87 | |
88 | /// Called when trying to access a global declared with a `linkage` attribute | |
89 | fn global_item_with_linkage<'a>( | |
0531ce1d | 90 | ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, |
ea8adc8c XL |
91 | instance: ty::Instance<'tcx>, |
92 | mutability: Mutability, | |
93 | ) -> EvalResult<'tcx>; | |
ff7c6d11 XL |
94 | |
95 | fn check_locks<'a>( | |
0531ce1d | 96 | _mem: &Memory<'a, 'mir, 'tcx, Self>, |
94b46f34 XL |
97 | _ptr: Pointer, |
98 | _size: Size, | |
ff7c6d11 XL |
99 | _access: AccessKind, |
100 | ) -> EvalResult<'tcx> { | |
101 | Ok(()) | |
102 | } | |
103 | ||
104 | fn add_lock<'a>( | |
0531ce1d | 105 | _mem: &mut Memory<'a, 'mir, 'tcx, Self>, |
2c00a5a8 | 106 | _id: AllocId, |
ff7c6d11 XL |
107 | ) {} |
108 | ||
109 | fn free_lock<'a>( | |
0531ce1d | 110 | _mem: &mut Memory<'a, 'mir, 'tcx, Self>, |
2c00a5a8 | 111 | _id: AllocId, |
ff7c6d11 XL |
112 | _len: u64, |
113 | ) -> EvalResult<'tcx> { | |
114 | Ok(()) | |
115 | } | |
116 | ||
117 | fn end_region<'a>( | |
0531ce1d | 118 | _ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, |
ff7c6d11 XL |
119 | _reg: Option<::rustc::middle::region::Scope>, |
120 | ) -> EvalResult<'tcx> { | |
121 | Ok(()) | |
122 | } | |
123 | ||
124 | fn validation_op<'a>( | |
0531ce1d | 125 | _ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, |
ff7c6d11 XL |
126 | _op: ::rustc::mir::ValidationOp, |
127 | _operand: &::rustc::mir::ValidationOperand<'tcx, ::rustc::mir::Place<'tcx>>, | |
128 | ) -> EvalResult<'tcx> { | |
129 | Ok(()) | |
130 | } | |
ea8adc8c | 131 | } |