Add the interpreter from my rustc branch and hook it up to CompileController.
This commit is contained in:
commit
cf49d6b080
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
||||
[root]
|
||||
name = "miri"
|
||||
version = "0.1.0"
|
||||
|
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "miri"
|
||||
version = "0.1.0"
|
||||
authors = ["Scott Olson <scott@solson.me>"]
|
||||
description = "An experimental interpreter for Rust MIR."
|
||||
repository = "https://github.com/tsion/miri"
|
||||
license = "ISC"
|
109
src/interpreter.rs
Normal file
109
src/interpreter.rs
Normal file
@ -0,0 +1,109 @@
|
||||
use rustc::front;
|
||||
use rustc::middle::ty;
|
||||
use rustc_mir::repr::{self as mir, Mir};
|
||||
use rustc_mir::mir_map::MirMap;
|
||||
use syntax::attr::AttrMetaMethods;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum Value {
|
||||
Uninit,
|
||||
Int(i64),
|
||||
}
|
||||
|
||||
struct Interpreter<'tcx> {
|
||||
mir: &'tcx Mir<'tcx>,
|
||||
var_vals: Vec<Value>,
|
||||
temp_vals: Vec<Value>,
|
||||
result: Value,
|
||||
}
|
||||
|
||||
impl<'tcx> Interpreter<'tcx> {
|
||||
fn new(mir: &'tcx Mir<'tcx>) -> Self {
|
||||
Interpreter {
|
||||
mir: mir,
|
||||
var_vals: vec![Value::Uninit; mir.var_decls.len()],
|
||||
temp_vals: vec![Value::Uninit; mir.temp_decls.len()],
|
||||
result: Value::Uninit,
|
||||
}
|
||||
}
|
||||
|
||||
fn run(&mut self) {
|
||||
let start_block = self.mir.basic_block_data(mir::START_BLOCK);
|
||||
|
||||
for stmt in &start_block.statements {
|
||||
use rustc_mir::repr::Lvalue::*;
|
||||
use rustc_mir::repr::StatementKind::*;
|
||||
|
||||
println!(" {:?}", stmt);
|
||||
match stmt.kind {
|
||||
Assign(ref lv, ref rv) => {
|
||||
let val = self.eval_rvalue(rv);
|
||||
|
||||
let spot = match *lv {
|
||||
Var(i) => &mut self.var_vals[i as usize],
|
||||
Temp(i) => &mut self.temp_vals[i as usize],
|
||||
ReturnPointer => &mut self.result,
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
|
||||
*spot = val;
|
||||
}
|
||||
Drop(_kind, ref _lv) => { /* TODO */ },
|
||||
}
|
||||
}
|
||||
|
||||
println!(" {:?}", start_block.terminator);
|
||||
println!("=> {:?}", self.result);
|
||||
}
|
||||
|
||||
fn eval_rvalue(&mut self, rv: &mir::Rvalue) -> Value {
|
||||
use rustc_mir::repr::Rvalue::*;
|
||||
|
||||
match *rv {
|
||||
Use(ref op) => self.eval_operand(op),
|
||||
BinaryOp(mir::BinOp::Add, ref left, ref right) => {
|
||||
let left_val = self.eval_operand(left);
|
||||
let right_val = self.eval_operand(right);
|
||||
match (left_val, right_val) {
|
||||
(Value::Int(l), Value::Int(r)) => Value::Int(l + r),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_operand(&mut self, op: &mir::Operand) -> Value {
|
||||
use rustc::middle::const_eval::ConstVal::*;
|
||||
use rustc_mir::repr::Lvalue::*;
|
||||
use rustc_mir::repr::Operand::*;
|
||||
|
||||
match *op {
|
||||
Consume(Var(i)) => self.var_vals[i as usize].clone(),
|
||||
Consume(Temp(i)) => self.temp_vals[i as usize].clone(),
|
||||
Constant(ref constant) => {
|
||||
match constant.literal {
|
||||
mir::Literal::Value { value: Int(n) } => Value::Int(n),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interpret_start_points<'tcx>(tcx: &ty::ctxt<'tcx>, mir_map: &MirMap<'tcx>) {
|
||||
for (&id, mir) in mir_map {
|
||||
for attr in tcx.map.attrs(id) {
|
||||
if attr.check_name("miri_run") {
|
||||
let item = match tcx.map.get(id) {
|
||||
front::map::NodeItem(item) => item,
|
||||
_ => panic!(),
|
||||
};
|
||||
println!("Interpreting: {}", item.name);
|
||||
let mut interpreter = Interpreter::new(mir);
|
||||
interpreter.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
31
src/main.rs
Normal file
31
src/main.rs
Normal file
@ -0,0 +1,31 @@
|
||||
#![feature(rustc_private)]
|
||||
|
||||
extern crate rustc;
|
||||
extern crate rustc_driver;
|
||||
extern crate rustc_mir;
|
||||
extern crate syntax;
|
||||
|
||||
mod interpreter;
|
||||
|
||||
use rustc::session::Session;
|
||||
use rustc_driver::{driver, CompilerCalls, Compilation};
|
||||
|
||||
struct MiriCompilerCalls;
|
||||
|
||||
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
|
||||
fn build_controller(&mut self, _: &Session) -> driver::CompileController<'a> {
|
||||
let mut control = driver::CompileController::basic();
|
||||
control.after_analysis.stop = Compilation::Stop;
|
||||
|
||||
control.after_analysis.callback = Box::new(|state| {
|
||||
interpreter::interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap());
|
||||
});
|
||||
|
||||
control
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
|
||||
}
|
12
test/add.rs
Normal file
12
test/add.rs
Normal file
@ -0,0 +1,12 @@
|
||||
#![feature(custom_attribute, rustc_attrs)]
|
||||
#![allow(dead_code, unused_attributes)]
|
||||
|
||||
#[rustc_mir]
|
||||
#[miri_run]
|
||||
fn foo() -> i32 {
|
||||
let x = 1;
|
||||
let y = 2;
|
||||
x + y
|
||||
}
|
||||
|
||||
fn main() {}
|
Loading…
x
Reference in New Issue
Block a user