2016-06-01 17:05:20 +02:00
|
|
|
use super::{
|
|
|
|
FnEvalContext,
|
|
|
|
CachedMir,
|
|
|
|
TerminatorTarget,
|
2016-06-02 17:05:17 +02:00
|
|
|
ConstantId,
|
2016-06-06 15:22:33 +02:00
|
|
|
GlobalEvalContext
|
2016-06-01 17:05:20 +02:00
|
|
|
};
|
|
|
|
use error::EvalResult;
|
|
|
|
use rustc::mir::repr as mir;
|
2016-06-03 15:48:56 +02:00
|
|
|
use rustc::ty::subst::{self, Subst};
|
|
|
|
use rustc::hir::def_id::DefId;
|
|
|
|
use rustc::mir::visit::{Visitor, LvalueContext};
|
2016-06-02 17:05:17 +02:00
|
|
|
use syntax::codemap::Span;
|
|
|
|
use std::rc::Rc;
|
2016-06-01 17:05:20 +02:00
|
|
|
|
2016-06-02 17:05:17 +02:00
|
|
|
pub enum Event {
|
2016-06-03 15:48:56 +02:00
|
|
|
Constant,
|
2016-06-02 17:05:17 +02:00
|
|
|
Assignment,
|
|
|
|
Terminator,
|
2016-06-01 17:05:20 +02:00
|
|
|
Done,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Stepper<'fncx, 'a: 'fncx, 'b: 'a + 'mir, 'mir: 'fncx, 'tcx: 'b>{
|
|
|
|
fncx: &'fncx mut FnEvalContext<'a, 'b, 'mir, 'tcx>,
|
|
|
|
mir: CachedMir<'mir, 'tcx>,
|
|
|
|
process: fn (&mut Stepper<'fncx, 'a, 'b, 'mir, 'tcx>) -> EvalResult<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'fncx, 'a, 'b: 'a + 'mir, 'mir, 'tcx: 'b> Stepper<'fncx, 'a, 'b, 'mir, 'tcx> {
|
|
|
|
pub(super) fn new(fncx: &'fncx mut FnEvalContext<'a, 'b, 'mir, 'tcx>) -> Self {
|
2016-06-03 15:48:56 +02:00
|
|
|
Stepper {
|
2016-06-01 17:05:20 +02:00
|
|
|
mir: fncx.mir(),
|
|
|
|
fncx: fncx,
|
|
|
|
process: Self::dummy,
|
2016-06-03 15:48:56 +02:00
|
|
|
}
|
2016-06-01 17:05:20 +02:00
|
|
|
}
|
2016-06-01 19:01:40 +02:00
|
|
|
|
2016-06-01 17:05:20 +02:00
|
|
|
fn dummy(&mut self) -> EvalResult<()> { Ok(()) }
|
2016-06-01 19:01:40 +02:00
|
|
|
|
2016-06-01 17:05:20 +02:00
|
|
|
fn statement(&mut self) -> EvalResult<()> {
|
2016-06-03 16:51:51 +02:00
|
|
|
let block_data = self.mir.basic_block_data(self.fncx.frame().next_block);
|
2016-06-03 16:57:47 +02:00
|
|
|
let stmt = &block_data.statements[self.fncx.frame().stmt];
|
2016-06-01 17:05:20 +02:00
|
|
|
let mir::StatementKind::Assign(ref lvalue, ref rvalue) = stmt.kind;
|
|
|
|
let result = self.fncx.eval_assignment(lvalue, rvalue);
|
|
|
|
self.fncx.maybe_report(stmt.span, result)?;
|
2016-06-03 16:57:47 +02:00
|
|
|
self.fncx.frame_mut().stmt += 1;
|
2016-06-01 17:05:20 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-01 19:01:40 +02:00
|
|
|
|
2016-06-01 17:05:20 +02:00
|
|
|
fn terminator(&mut self) -> EvalResult<()> {
|
2016-06-03 16:57:47 +02:00
|
|
|
// after a terminator we go to a new block
|
|
|
|
self.fncx.frame_mut().stmt = 0;
|
2016-06-01 17:05:20 +02:00
|
|
|
let term = {
|
2016-06-03 16:51:51 +02:00
|
|
|
let block_data = self.mir.basic_block_data(self.fncx.frame().next_block);
|
2016-06-01 17:05:20 +02:00
|
|
|
let terminator = block_data.terminator();
|
|
|
|
let result = self.fncx.eval_terminator(terminator);
|
|
|
|
self.fncx.maybe_report(terminator.span, result)?
|
|
|
|
};
|
|
|
|
match term {
|
2016-06-03 16:51:51 +02:00
|
|
|
TerminatorTarget::Block => {},
|
2016-06-01 17:05:20 +02:00
|
|
|
TerminatorTarget::Return => {
|
2016-06-03 17:08:51 +02:00
|
|
|
assert!(self.fncx.frame().constants.is_empty());
|
2016-06-01 17:05:20 +02:00
|
|
|
self.fncx.pop_stack_frame();
|
|
|
|
if !self.fncx.stack.is_empty() {
|
|
|
|
self.mir = self.fncx.mir();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
TerminatorTarget::Call => {
|
|
|
|
self.mir = self.fncx.mir();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-01 19:01:40 +02:00
|
|
|
|
2016-06-03 15:48:56 +02:00
|
|
|
fn constant(&mut self) -> EvalResult<()> {
|
2016-06-03 17:41:36 +02:00
|
|
|
let (cid, span, return_ptr, mir) = self.fncx.frame_mut().constants.pop().expect("state machine broken");
|
|
|
|
let def_id = cid.def_id();
|
|
|
|
let substs = cid.substs();
|
|
|
|
self.fncx.push_stack_frame(def_id, span, mir, substs, Some(return_ptr));
|
|
|
|
self.mir = self.fncx.mir();
|
2016-06-03 15:48:56 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step(&mut self) -> EvalResult<Event> {
|
|
|
|
(self.process)(self)?;
|
|
|
|
|
|
|
|
if self.fncx.stack.is_empty() {
|
|
|
|
// fuse the iterator
|
|
|
|
self.process = Self::dummy;
|
|
|
|
return Ok(Event::Done);
|
|
|
|
}
|
|
|
|
|
2016-06-03 17:08:51 +02:00
|
|
|
if !self.fncx.frame().constants.is_empty() {
|
2016-06-03 15:48:56 +02:00
|
|
|
self.process = Self::constant;
|
|
|
|
return Ok(Event::Constant);
|
2016-06-02 17:05:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 16:51:51 +02:00
|
|
|
let block = self.fncx.frame().next_block;
|
2016-06-03 16:57:47 +02:00
|
|
|
let stmt = self.fncx.frame().stmt;
|
2016-06-03 16:51:51 +02:00
|
|
|
let basic_block = self.mir.basic_block_data(block);
|
2016-06-01 17:05:20 +02:00
|
|
|
|
2016-06-03 16:57:47 +02:00
|
|
|
if let Some(ref stmt) = basic_block.statements.get(stmt) {
|
2016-06-03 17:08:51 +02:00
|
|
|
assert!(self.fncx.frame().constants.is_empty());
|
2016-06-03 15:48:56 +02:00
|
|
|
ConstantExtractor {
|
|
|
|
span: stmt.span,
|
|
|
|
mir: &self.mir,
|
2016-06-06 15:22:33 +02:00
|
|
|
gecx: self.fncx.gecx,
|
|
|
|
frame: self.fncx.stack.last_mut().expect("stack empty"),
|
2016-06-03 16:51:51 +02:00
|
|
|
}.visit_statement(block, stmt);
|
2016-06-03 17:08:51 +02:00
|
|
|
if self.fncx.frame().constants.is_empty() {
|
2016-06-03 15:48:56 +02:00
|
|
|
self.process = Self::statement;
|
|
|
|
return Ok(Event::Assignment);
|
|
|
|
} else {
|
|
|
|
self.process = Self::constant;
|
|
|
|
return Ok(Event::Constant);
|
|
|
|
}
|
2016-06-01 17:05:20 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 15:48:56 +02:00
|
|
|
let terminator = basic_block.terminator();
|
2016-06-06 15:22:33 +02:00
|
|
|
assert!(self.fncx.frame().constants.is_empty());
|
2016-06-03 15:48:56 +02:00
|
|
|
ConstantExtractor {
|
|
|
|
span: terminator.span,
|
|
|
|
mir: &self.mir,
|
2016-06-06 15:22:33 +02:00
|
|
|
gecx: self.fncx.gecx,
|
|
|
|
frame: self.fncx.stack.last_mut().expect("stack empty"),
|
2016-06-03 16:51:51 +02:00
|
|
|
}.visit_terminator(block, terminator);
|
2016-06-03 17:08:51 +02:00
|
|
|
if self.fncx.frame().constants.is_empty() {
|
2016-06-03 15:48:56 +02:00
|
|
|
self.process = Self::terminator;
|
|
|
|
Ok(Event::Terminator)
|
|
|
|
} else {
|
|
|
|
self.process = Self::constant;
|
2016-06-06 15:24:56 +02:00
|
|
|
Ok(Event::Constant)
|
2016-06-03 15:48:56 +02:00
|
|
|
}
|
2016-06-01 17:05:20 +02:00
|
|
|
}
|
2016-06-02 17:05:17 +02:00
|
|
|
|
|
|
|
/// returns the statement that will be processed next
|
|
|
|
pub fn stmt(&self) -> &mir::Statement {
|
2016-06-03 16:57:47 +02:00
|
|
|
&self.fncx.basic_block().statements[self.fncx.frame().stmt]
|
2016-06-02 17:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// returns the terminator of the current block
|
|
|
|
pub fn term(&self) -> &mir::Terminator {
|
2016-06-03 16:51:51 +02:00
|
|
|
self.fncx.basic_block().terminator()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn block(&self) -> mir::BasicBlock {
|
|
|
|
self.fncx.frame().next_block
|
2016-06-02 17:05:17 +02:00
|
|
|
}
|
2016-06-03 15:48:56 +02:00
|
|
|
}
|
2016-06-02 17:05:17 +02:00
|
|
|
|
2016-06-06 15:22:33 +02:00
|
|
|
struct ConstantExtractor<'a, 'b: 'mir, 'mir: 'a, 'tcx: 'b> {
|
2016-06-03 15:48:56 +02:00
|
|
|
span: Span,
|
2016-06-06 15:22:33 +02:00
|
|
|
mir: &'a CachedMir<'mir, 'tcx>,
|
|
|
|
frame: &'a mut Frame<'mir, 'tcx>,
|
|
|
|
gecx: &'a mut GlobalEvalContext<'b, 'tcx>,
|
2016-06-02 17:05:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 15:22:33 +02:00
|
|
|
impl<'a, 'b, 'mir, 'tcx> ConstantExtractor<'a, 'b, 'mir, 'tcx> {
|
2016-06-03 17:41:36 +02:00
|
|
|
fn static_item(&mut self, def_id: DefId, substs: &'tcx subst::Substs<'tcx>, span: Span) {
|
2016-06-03 15:48:56 +02:00
|
|
|
let cid = ConstantId::Static {
|
|
|
|
def_id: def_id,
|
|
|
|
substs: substs,
|
|
|
|
};
|
2016-06-06 15:22:33 +02:00
|
|
|
if self.gecx.statics.contains_key(&cid) {
|
2016-06-03 17:41:36 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-06-06 15:22:33 +02:00
|
|
|
let mir = self.gecx.load_mir(def_id);
|
|
|
|
let ptr = self.gecx.alloc_ret_ptr(mir.return_ty, substs).expect("there's no such thing as an unreachable static");
|
|
|
|
self.gecx.statics.insert(cid.clone(), ptr);
|
|
|
|
self.frame.constants.push((cid, span, ptr, mir));
|
2016-06-03 15:48:56 +02:00
|
|
|
}
|
2016-06-02 17:05:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 15:22:33 +02:00
|
|
|
impl<'a, 'b, 'mir, 'tcx> Visitor<'tcx> for ConstantExtractor<'a, 'b, 'mir, 'tcx> {
|
2016-06-02 17:05:17 +02:00
|
|
|
fn visit_constant(&mut self, constant: &mir::Constant<'tcx>) {
|
|
|
|
self.super_constant(constant);
|
|
|
|
match constant.literal {
|
|
|
|
// already computed by rustc
|
|
|
|
mir::Literal::Value { .. } => {}
|
2016-06-03 15:48:56 +02:00
|
|
|
mir::Literal::Item { def_id, substs } => {
|
2016-06-06 15:22:33 +02:00
|
|
|
let item_ty = self.gecx.tcx.lookup_item_type(def_id).subst(self.gecx.tcx, substs);
|
2016-06-03 15:48:56 +02:00
|
|
|
if item_ty.ty.is_fn() {
|
|
|
|
// unimplemented
|
|
|
|
} else {
|
2016-06-03 17:41:36 +02:00
|
|
|
self.static_item(def_id, substs, constant.span);
|
2016-06-03 15:48:56 +02:00
|
|
|
}
|
|
|
|
},
|
2016-06-02 17:05:17 +02:00
|
|
|
mir::Literal::Promoted { index } => {
|
2016-06-03 17:41:36 +02:00
|
|
|
let cid = ConstantId::Promoted {
|
2016-06-06 15:22:33 +02:00
|
|
|
def_id: self.frame.def_id,
|
|
|
|
substs: self.frame.substs,
|
2016-06-03 17:41:36 +02:00
|
|
|
index: index,
|
|
|
|
};
|
2016-06-06 15:22:33 +02:00
|
|
|
if self.gecx.statics.contains_key(&cid) {
|
2016-06-03 15:48:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let mir = self.mir.promoted[index].clone();
|
|
|
|
let return_ty = mir.return_ty;
|
2016-06-06 15:22:33 +02:00
|
|
|
let return_ptr = self.gecx.alloc_ret_ptr(return_ty, cid.substs()).expect("there's no such thing as an unreachable static");
|
2016-06-03 15:48:56 +02:00
|
|
|
let mir = CachedMir::Owned(Rc::new(mir));
|
2016-06-06 15:22:33 +02:00
|
|
|
self.gecx.statics.insert(cid.clone(), return_ptr);
|
|
|
|
self.frame.constants.push((cid, constant.span, return_ptr, mir));
|
2016-06-02 17:05:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 15:48:56 +02:00
|
|
|
fn visit_lvalue(&mut self, lvalue: &mir::Lvalue<'tcx>, context: LvalueContext) {
|
|
|
|
self.super_lvalue(lvalue, context);
|
|
|
|
if let mir::Lvalue::Static(def_id) = *lvalue {
|
2016-06-06 15:22:33 +02:00
|
|
|
let substs = self.gecx.tcx.mk_substs(subst::Substs::empty());
|
2016-06-03 15:48:56 +02:00
|
|
|
let span = self.span;
|
2016-06-03 17:41:36 +02:00
|
|
|
self.static_item(def_id, substs, span);
|
2016-06-02 17:05:17 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-01 17:05:20 +02:00
|
|
|
}
|