]> git.lizzy.rs Git - rust.git/blob - src/interpreter/stepper.rs
8807dbcafab6a7f5383420594d7fdd5a2ade0c4e
[rust.git] / src / interpreter / stepper.rs
1 use super::{
2     FnEvalContext,
3     CachedMir,
4     TerminatorTarget,
5     ConstantId,
6     GlobalEvalContext
7 };
8 use error::EvalResult;
9 use rustc::mir::repr as mir;
10 use rustc::ty::subst::{self, Subst};
11 use rustc::hir::def_id::DefId;
12 use rustc::mir::visit::{Visitor, LvalueContext};
13 use syntax::codemap::Span;
14 use std::rc::Rc;
15
16 pub enum Event {
17     Constant,
18     Assignment,
19     Terminator,
20     Done,
21 }
22
23 pub struct Stepper<'fncx, 'a: 'fncx, 'b: 'a + 'mir, 'mir: 'fncx, 'tcx: 'b>{
24     fncx: &'fncx mut FnEvalContext<'a, 'b, 'mir, 'tcx>,
25     process: fn (&mut Stepper<'fncx, 'a, 'b, 'mir, 'tcx>) -> EvalResult<()>,
26 }
27
28 impl<'fncx, 'a, 'b: 'a + 'mir, 'mir, 'tcx: 'b> Stepper<'fncx, 'a, 'b, 'mir, 'tcx> {
29     pub(super) fn new(fncx: &'fncx mut FnEvalContext<'a, 'b, 'mir, 'tcx>) -> Self {
30         Stepper {
31             fncx: fncx,
32             process: Self::dummy,
33         }
34     }
35
36     fn dummy(&mut self) -> EvalResult<()> { Ok(()) }
37
38     fn statement(&mut self) -> EvalResult<()> {
39         let mir = self.fncx.mir();
40         let block_data = mir.basic_block_data(self.fncx.frame().next_block);
41         let stmt = &block_data.statements[self.fncx.frame().stmt];
42         let mir::StatementKind::Assign(ref lvalue, ref rvalue) = stmt.kind;
43         let result = self.fncx.eval_assignment(lvalue, rvalue);
44         self.fncx.maybe_report(stmt.span, result)?;
45         self.fncx.frame_mut().stmt += 1;
46         Ok(())
47     }
48
49     fn terminator(&mut self) -> EvalResult<()> {
50         // after a terminator we go to a new block
51         self.fncx.frame_mut().stmt = 0;
52         let term = {
53             let mir = self.fncx.mir();
54             let block_data = mir.basic_block_data(self.fncx.frame().next_block);
55             let terminator = block_data.terminator();
56             let result = self.fncx.eval_terminator(terminator);
57             self.fncx.maybe_report(terminator.span, result)?
58         };
59         match term {
60             TerminatorTarget::Block => {},
61             TerminatorTarget::Return => {
62                 assert!(self.fncx.frame().constants.is_empty());
63                 self.fncx.pop_stack_frame();
64             },
65             TerminatorTarget::Call => {},
66         }
67         Ok(())
68     }
69
70     fn constant(&mut self) -> EvalResult<()> {
71         let (cid, span, return_ptr, mir) = self.fncx.frame_mut().constants.pop().expect("state machine broken");
72         let def_id = cid.def_id();
73         let substs = cid.substs();
74         self.fncx.push_stack_frame(def_id, span, mir, substs, Some(return_ptr));
75         Ok(())
76     }
77
78     pub fn step(&mut self) -> EvalResult<Event> {
79         (self.process)(self)?;
80
81         if self.fncx.stack.is_empty() {
82             // fuse the iterator
83             self.process = Self::dummy;
84             return Ok(Event::Done);
85         }
86
87         if !self.fncx.frame().constants.is_empty() {
88             self.process = Self::constant;
89             return Ok(Event::Constant);
90         }
91
92         let block = self.fncx.frame().next_block;
93         let stmt = self.fncx.frame().stmt;
94         let mir = self.fncx.mir();
95         let basic_block = mir.basic_block_data(block);
96
97         if let Some(ref stmt) = basic_block.statements.get(stmt) {
98             assert!(self.fncx.frame().constants.is_empty());
99             ConstantExtractor {
100                 span: stmt.span,
101                 gecx: self.fncx.gecx,
102                 frame: self.fncx.stack.last_mut().expect("stack empty"),
103             }.visit_statement(block, stmt);
104             if self.fncx.frame().constants.is_empty() {
105                 self.process = Self::statement;
106                 return Ok(Event::Assignment);
107             } else {
108                 self.process = Self::constant;
109                 return Ok(Event::Constant);
110             }
111         }
112
113         let terminator = basic_block.terminator();
114         assert!(self.fncx.frame().constants.is_empty());
115         ConstantExtractor {
116             span: terminator.span,
117             gecx: self.fncx.gecx,
118             frame: self.fncx.stack.last_mut().expect("stack empty"),
119         }.visit_terminator(block, terminator);
120         if self.fncx.frame().constants.is_empty() {
121             self.process = Self::terminator;
122             Ok(Event::Terminator)
123         } else {
124             self.process = Self::constant;
125             Ok(Event::Constant)
126         }
127     }
128
129     /// returns the statement that will be processed next
130     pub fn stmt(&self) -> &mir::Statement {
131         &self.fncx.basic_block().statements[self.fncx.frame().stmt]
132     }
133
134     /// returns the terminator of the current block
135     pub fn term(&self) -> &mir::Terminator {
136         self.fncx.basic_block().terminator()
137     }
138
139     pub fn block(&self) -> mir::BasicBlock {
140         self.fncx.frame().next_block
141     }
142 }
143
144 struct ConstantExtractor<'a, 'b: 'mir, 'mir: 'a, 'tcx: 'b> {
145     span: Span,
146     frame: &'a mut Frame<'mir, 'tcx>,
147     gecx: &'a mut GlobalEvalContext<'b, 'tcx>,
148 }
149
150 impl<'a, 'b, 'mir, 'tcx> ConstantExtractor<'a, 'b, 'mir, 'tcx> {
151     fn static_item(&mut self, def_id: DefId, substs: &'tcx subst::Substs<'tcx>, span: Span) {
152         let cid = ConstantId::Static {
153             def_id: def_id,
154             substs: substs,
155         };
156         if self.gecx.statics.contains_key(&cid) {
157             return;
158         }
159         let mir = self.gecx.load_mir(def_id);
160         let ptr = self.gecx.alloc_ret_ptr(mir.return_ty, substs).expect("there's no such thing as an unreachable static");
161         self.gecx.statics.insert(cid.clone(), ptr);
162         self.frame.constants.push((cid, span, ptr, mir));
163     }
164 }
165
166 impl<'a, 'b, 'mir, 'tcx> Visitor<'tcx> for ConstantExtractor<'a, 'b, 'mir, 'tcx> {
167     fn visit_constant(&mut self, constant: &mir::Constant<'tcx>) {
168         self.super_constant(constant);
169         match constant.literal {
170             // already computed by rustc
171             mir::Literal::Value { .. } => {}
172             mir::Literal::Item { def_id, substs } => {
173                 let item_ty = self.gecx.tcx.lookup_item_type(def_id).subst(self.gecx.tcx, substs);
174                 if item_ty.ty.is_fn() {
175                     // unimplemented
176                 } else {
177                     self.static_item(def_id, substs, constant.span);
178                 }
179             },
180             mir::Literal::Promoted { index } => {
181                 let cid = ConstantId::Promoted {
182                     def_id: self.frame.def_id,
183                     substs: self.frame.substs,
184                     index: index,
185                 };
186                 if self.gecx.statics.contains_key(&cid) {
187                     return;
188                 }
189                 let mir = self.frame.mir.promoted[index].clone();
190                 let return_ty = mir.return_ty;
191                 let return_ptr = self.gecx.alloc_ret_ptr(return_ty, cid.substs()).expect("there's no such thing as an unreachable static");
192                 let mir = CachedMir::Owned(Rc::new(mir));
193                 self.gecx.statics.insert(cid.clone(), return_ptr);
194                 self.frame.constants.push((cid, constant.span, return_ptr, mir));
195             }
196         }
197     }
198
199     fn visit_lvalue(&mut self, lvalue: &mir::Lvalue<'tcx>, context: LvalueContext) {
200         self.super_lvalue(lvalue, context);
201         if let mir::Lvalue::Static(def_id) = *lvalue {
202             let substs = self.gecx.tcx.mk_substs(subst::Substs::empty());
203             let span = self.span;
204             self.static_item(def_id, substs, span);
205         }
206     }
207 }