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;
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<()>,
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 {
36 fn dummy(&mut self) -> EvalResult<()> { Ok(()) }
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;
49 fn terminator(&mut self) -> EvalResult<()> {
50 // after a terminator we go to a new block
51 self.fncx.frame_mut().stmt = 0;
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)?
60 TerminatorTarget::Block => {},
61 TerminatorTarget::Return => {
62 assert!(self.fncx.frame().constants.is_empty());
63 self.fncx.pop_stack_frame();
65 TerminatorTarget::Call => {},
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));
78 pub fn step(&mut self) -> EvalResult<Event> {
79 (self.process)(self)?;
81 if self.fncx.stack.is_empty() {
83 self.process = Self::dummy;
84 return Ok(Event::Done);
87 if !self.fncx.frame().constants.is_empty() {
88 self.process = Self::constant;
89 return Ok(Event::Constant);
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);
97 if let Some(ref stmt) = basic_block.statements.get(stmt) {
98 assert!(self.fncx.frame().constants.is_empty());
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);
108 self.process = Self::constant;
109 return Ok(Event::Constant);
113 let terminator = basic_block.terminator();
114 assert!(self.fncx.frame().constants.is_empty());
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)
124 self.process = Self::constant;
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]
134 /// returns the terminator of the current block
135 pub fn term(&self) -> &mir::Terminator {
136 self.fncx.basic_block().terminator()
139 pub fn block(&self) -> mir::BasicBlock {
140 self.fncx.frame().next_block
144 struct ConstantExtractor<'a, 'b: 'mir, 'mir: 'a, 'tcx: 'b> {
146 frame: &'a mut Frame<'mir, 'tcx>,
147 gecx: &'a mut GlobalEvalContext<'b, 'tcx>,
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 {
156 if self.gecx.statics.contains_key(&cid) {
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));
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() {
177 self.static_item(def_id, substs, constant.span);
180 mir::Literal::Promoted { index } => {
181 let cid = ConstantId::Promoted {
182 def_id: self.frame.def_id,
183 substs: self.frame.substs,
186 if self.gecx.statics.contains_key(&cid) {
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));
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);