rust/src/interpreter/stepper.rs

180 lines
6.5 KiB
Rust
Raw Normal View History

2016-06-01 17:05:20 +02:00
use super::{
CachedMir,
ConstantId,
2016-06-08 11:11:08 +02:00
GlobalEvalContext,
ConstantKind,
2016-06-01 17:05:20 +02:00
};
use error::EvalResult;
use rustc::mir::repr as mir;
use rustc::ty::{subst, self};
2016-06-03 15:48:56 +02:00
use rustc::hir::def_id::DefId;
use rustc::mir::visit::{Visitor, LvalueContext};
use syntax::codemap::Span;
use std::rc::Rc;
use memory::Pointer;
2016-06-01 17:05:20 +02:00
pub struct Stepper<'fncx, 'a: 'fncx, 'tcx: 'a>{
gecx: &'fncx mut GlobalEvalContext<'a, 'tcx>,
// a cache of the constants to be computed before the next statement/terminator
// this is an optimization, so we don't have to allocate a new vector for every statement
constants: Vec<(ConstantId<'tcx>, Span, Pointer, CachedMir<'a, 'tcx>)>,
2016-06-01 17:05:20 +02:00
}
impl<'fncx, 'a, 'tcx> Stepper<'fncx, 'a, 'tcx> {
pub(super) fn new(gecx: &'fncx mut GlobalEvalContext<'a, 'tcx>) -> Self {
2016-06-03 15:48:56 +02:00
Stepper {
gecx: gecx,
constants: Vec::new(),
2016-06-03 15:48:56 +02:00
}
2016-06-01 17:05:20 +02:00
}
2016-06-01 19:01:40 +02:00
fn statement(&mut self, stmt: &mir::Statement<'tcx>) -> EvalResult<()> {
trace!("{:?}", stmt);
2016-06-01 17:05:20 +02:00
let mir::StatementKind::Assign(ref lvalue, ref rvalue) = stmt.kind;
2016-06-09 16:13:42 +02:00
self.gecx.eval_assignment(lvalue, rvalue)?;
self.gecx.frame_mut().stmt += 1;
2016-06-01 17:05:20 +02:00
Ok(())
}
2016-06-01 19:01:40 +02:00
fn terminator(&mut self, terminator: &mir::Terminator<'tcx>) -> EvalResult<()> {
// after a terminator we go to a new block
self.gecx.frame_mut().stmt = 0;
2016-06-09 16:08:34 +02:00
trace!("{:?}", terminator.kind);
2016-06-09 16:13:42 +02:00
self.gecx.eval_terminator(terminator)?;
2016-06-09 16:08:34 +02:00
if !self.gecx.stack.is_empty() {
trace!("// {:?}", self.gecx.frame().next_block);
2016-06-01 17:05:20 +02:00
}
Ok(())
}
2016-06-01 19:01:40 +02:00
2016-06-08 11:34:56 +02:00
// returns true as long as there are more things to do
pub fn step(&mut self) -> EvalResult<bool> {
if self.gecx.stack.is_empty() {
return Ok(false);
2016-06-03 15:48:56 +02:00
}
let block = self.gecx.frame().next_block;
let stmt = self.gecx.frame().stmt;
let mir = self.gecx.mir();
2016-06-08 10:17:26 +02:00
let basic_block = mir.basic_block_data(block);
2016-06-01 17:05:20 +02:00
if let Some(ref stmt) = basic_block.statements.get(stmt) {
assert!(self.constants.is_empty());
2016-06-03 15:48:56 +02:00
ConstantExtractor {
span: stmt.span,
substs: self.gecx.substs(),
def_id: self.gecx.frame().def_id,
gecx: self.gecx,
constants: &mut self.constants,
mir: &mir,
2016-06-03 16:51:51 +02:00
}.visit_statement(block, stmt);
if self.constants.is_empty() {
self.statement(stmt)?;
2016-06-03 15:48:56 +02:00
} else {
self.extract_constants()?;
2016-06-03 15:48:56 +02:00
}
return Ok(true);
2016-06-01 17:05:20 +02:00
}
2016-06-03 15:48:56 +02:00
let terminator = basic_block.terminator();
assert!(self.constants.is_empty());
2016-06-03 15:48:56 +02:00
ConstantExtractor {
span: terminator.span,
substs: self.gecx.substs(),
def_id: self.gecx.frame().def_id,
gecx: self.gecx,
constants: &mut self.constants,
mir: &mir,
2016-06-03 16:51:51 +02:00
}.visit_terminator(block, terminator);
if self.constants.is_empty() {
self.terminator(terminator)?;
2016-06-03 15:48:56 +02:00
} else {
self.extract_constants()?;
2016-06-03 15:48:56 +02:00
}
Ok(true)
2016-06-01 17:05:20 +02:00
}
fn extract_constants(&mut self) -> EvalResult<()> {
assert!(!self.constants.is_empty());
for (cid, span, return_ptr, mir) in self.constants.drain(..) {
trace!("queuing a constant");
self.gecx.push_stack_frame(cid.def_id, span, mir, cid.substs, Some(return_ptr));
}
// self.step() can't be "done", so it can't return false
assert!(self.step()?);
Ok(())
}
2016-06-03 15:48:56 +02:00
}
struct ConstantExtractor<'a, 'b: 'mir, 'mir: 'a, 'tcx: 'b> {
2016-06-03 15:48:56 +02:00
span: Span,
// FIXME: directly push the new stackframes instead of doing this intermediate caching
constants: &'a mut Vec<(ConstantId<'tcx>, Span, Pointer, CachedMir<'mir, 'tcx>)>,
gecx: &'a mut GlobalEvalContext<'b, 'tcx>,
mir: &'a mir::Mir<'tcx>,
def_id: DefId,
substs: &'tcx subst::Substs<'tcx>,
}
impl<'a, 'b, 'mir, 'tcx> ConstantExtractor<'a, 'b, 'mir, 'tcx> {
2016-06-09 11:27:12 +02:00
fn global_item(&mut self, def_id: DefId, substs: &'tcx subst::Substs<'tcx>, span: Span) {
let cid = ConstantId {
2016-06-03 15:48:56 +02:00
def_id: def_id,
substs: substs,
2016-06-08 12:35:15 +02:00
kind: ConstantKind::Global,
2016-06-03 15:48:56 +02:00
};
if self.gecx.statics.contains_key(&cid) {
2016-06-03 17:41:36 +02:00
return;
}
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.constants.push((cid, span, ptr, mir));
2016-06-03 15:48:56 +02:00
}
}
impl<'a, 'b, 'mir, 'tcx> Visitor<'tcx> for ConstantExtractor<'a, 'b, 'mir, 'tcx> {
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 } => {
if let ty::TyFnDef(..) = constant.ty.sty {
// No need to do anything here, even if function pointers are implemented,
// because the type is the actual function, not the signature of the function.
// Thus we can simply create a zero sized allocation in `evaluate_operand`
2016-06-03 15:48:56 +02:00
} else {
2016-06-09 11:27:12 +02:00
self.global_item(def_id, substs, constant.span);
2016-06-03 15:48:56 +02:00
}
},
mir::Literal::Promoted { index } => {
let cid = ConstantId {
def_id: self.def_id,
substs: self.substs,
kind: ConstantKind::Promoted(index),
2016-06-03 17:41:36 +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();
2016-06-03 15:48:56 +02:00
let return_ty = mir.return_ty;
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));
self.gecx.statics.insert(cid.clone(), return_ptr);
self.constants.push((cid, constant.span, return_ptr, mir));
}
}
}
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 {
let substs = self.gecx.tcx.mk_substs(subst::Substs::empty());
2016-06-03 15:48:56 +02:00
let span = self.span;
2016-06-09 11:27:12 +02:00
self.global_item(def_id, substs, span);
}
}
2016-06-01 17:05:20 +02:00
}