Skip to content

Commit

Permalink
Merge branch 'wip'
Browse files Browse the repository at this point in the history
  • Loading branch information
solson committed Jun 15, 2016
2 parents b24edd6 + 16f778a commit bac37e6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
33 changes: 26 additions & 7 deletions src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {

fn report(tcx: TyCtxt, ecx: &EvalContext, e: EvalError) {
let frame = ecx.stack().last().expect("stackframe was empty");
let block = &frame.mir.basic_blocks()[frame.next_block];
let block = &frame.mir.basic_blocks()[frame.block];
let span = if frame.stmt < block.statements.len() {
block.statements[frame.stmt].source_info.span
} else {
Expand All @@ -101,12 +101,6 @@ fn report(tcx: TyCtxt, ecx: &EvalContext, e: EvalError) {
err.emit();
}

fn main() {
init_logger();
let args: Vec<String> = std::env::args().collect();
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
}

fn init_logger() {
const NSPACES: usize = 40;
let format = |record: &log::LogRecord| {
Expand All @@ -130,3 +124,28 @@ fn init_logger() {

builder.init().unwrap();
}

fn find_sysroot() -> String {
// Taken from https://github.com/Manishearth/rust-clippy/pull/911.
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
match (home, toolchain) {
(Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
_ => option_env!("RUST_SYSROOT")
.expect("need to specify RUST_SYSROOT env var or use rustup or multirust")
.to_owned(),
}
}

fn main() {
init_logger();
let mut args: Vec<String> = std::env::args().collect();

let sysroot_flag = String::from("--sysroot");
if !args.contains(&sysroot_flag) {
args.push(sysroot_flag);
args.push(find_sysroot());
}

rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
}
51 changes: 32 additions & 19 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct EvalContext<'a, 'tcx: 'a> {
/// The virtual memory system.
memory: Memory<'tcx>,

/// Precomputed statics, constants and promoteds
/// Precomputed statics, constants and promoteds.
statics: HashMap<ConstantId<'tcx>, Pointer>,

/// The virtual call stack.
Expand All @@ -51,20 +51,25 @@ pub struct EvalContext<'a, 'tcx: 'a> {

/// A stack frame.
pub struct Frame<'a, 'tcx: 'a> {
/// The def_id of the current function
pub def_id: DefId,
////////////////////////////////////////////////////////////////////////////////
// Function and callsite information
////////////////////////////////////////////////////////////////////////////////

/// The span of the call site
pub span: codemap::Span,
/// The MIR for the function called on this frame.
pub mir: CachedMir<'a, 'tcx>,

/// The def_id of the current function.
pub def_id: DefId,

/// type substitutions for the current function invocation
/// type substitutions for the current function invocation.
pub substs: &'tcx Substs<'tcx>,

/// The MIR for the function called on this frame.
pub mir: CachedMir<'a, 'tcx>,
/// The span of the call site.
pub span: codemap::Span,

/// The block that is currently executed (or will be executed after the above call stacks return)
pub next_block: mir::BasicBlock,
////////////////////////////////////////////////////////////////////////////////
// Return pointer and local allocations
////////////////////////////////////////////////////////////////////////////////

/// A pointer for writing the return value of the current call if it's not a diverging call.
pub return_ptr: Option<Pointer>,
Expand All @@ -80,7 +85,15 @@ pub struct Frame<'a, 'tcx: 'a> {
/// The offset of the first temporary in `self.locals`.
pub temp_offset: usize,

/// The index of the currently evaluated statment
////////////////////////////////////////////////////////////////////////////////
// Current position within the function
////////////////////////////////////////////////////////////////////////////////

/// The block that is currently executed (or will be executed after the above call stacks
/// return).
pub block: mir::BasicBlock,

/// The index of the currently evaluated statment.
pub stmt: usize,
}

Expand Down Expand Up @@ -353,7 +366,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {

self.stack.push(Frame {
mir: mir.clone(),
next_block: mir::START_BLOCK,
block: mir::START_BLOCK,
return_ptr: return_ptr,
locals: locals,
var_offset: num_args,
Expand All @@ -378,13 +391,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
Return => self.pop_stack_frame(),

Goto { target } => {
self.frame_mut().next_block = target;
self.frame_mut().block = target;
},

If { ref cond, targets: (then_target, else_target) } => {
let cond_ptr = self.eval_operand(cond)?;
let cond_val = self.memory.read_bool(cond_ptr)?;
self.frame_mut().next_block = if cond_val { then_target } else { else_target };
self.frame_mut().block = if cond_val { then_target } else { else_target };
}

SwitchInt { ref discr, ref values, ref targets, .. } => {
Expand All @@ -407,7 +420,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
}
}

self.frame_mut().next_block = target_block;
self.frame_mut().block = target_block;
}

Switch { ref discr, ref targets, adt_def } => {
Expand All @@ -419,7 +432,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {

match matching {
Some(i) => {
self.frame_mut().next_block = targets[i];
self.frame_mut().block = targets[i];
},
None => return Err(EvalError::InvalidDiscriminant),
}
Expand All @@ -428,7 +441,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
Call { ref func, ref args, ref destination, .. } => {
let mut return_ptr = None;
if let Some((ref lv, target)) = *destination {
self.frame_mut().next_block = target;
self.frame_mut().block = target;
return_ptr = Some(self.eval_lvalue(lv)?.to_ptr());
}

Expand Down Expand Up @@ -458,14 +471,14 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
let ptr = self.eval_lvalue(location)?.to_ptr();
let ty = self.lvalue_ty(location);
self.drop(ptr, ty)?;
self.frame_mut().next_block = target;
self.frame_mut().block = target;
}

Assert { ref cond, expected, ref msg, target, cleanup } => {
let actual_ptr = self.eval_operand(cond)?;
let actual = self.memory.read_bool(actual_ptr)?;
if actual == expected {
self.frame_mut().next_block = target;
self.frame_mut().block = target;
} else {
panic!("unimplemented: jump to {:?} and print {:?}", cleanup, msg);
}
Expand Down
4 changes: 2 additions & 2 deletions src/interpreter/stepper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'ecx, 'a, 'tcx> Stepper<'ecx, 'a, 'tcx> {
trace!("{:?}", terminator.kind);
self.ecx.eval_terminator(terminator)?;
if !self.ecx.stack.is_empty() {
trace!("// {:?}", self.ecx.frame().next_block);
trace!("// {:?}", self.ecx.frame().block);
}
Ok(())
}
Expand All @@ -48,7 +48,7 @@ impl<'ecx, 'a, 'tcx> Stepper<'ecx, 'a, 'tcx> {
return Ok(false);
}

let block = self.ecx.frame().next_block;
let block = self.ecx.frame().block;
let stmt = self.ecx.frame().stmt;
let mir = self.ecx.mir();
let basic_block = &mir.basic_blocks()[block];
Expand Down

0 comments on commit bac37e6

Please sign in to comment.