1 #![feature(rustc_private)]
6 extern crate rustc_driver;
7 extern crate env_logger;
8 extern crate log_settings;
10 #[macro_use] extern crate log;
19 use rustc::session::Session;
20 use rustc_driver::{driver, CompilerCalls};
21 use rustc::ty::{TyCtxt, subst};
22 use rustc::mir::mir_map::MirMap;
23 use rustc::hir::def_id::DefId;
25 struct MiriCompilerCalls;
27 impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
32 ) -> driver::CompileController<'a> {
33 let mut control = driver::CompileController::basic();
35 control.after_analysis.callback = Box::new(|state| {
36 state.session.abort_if_errors();
37 interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap());
46 fn interpret_start_points<'a, 'tcx>(
47 tcx: TyCtxt<'a, 'tcx, 'tcx>,
48 mir_map: &MirMap<'tcx>,
50 let initial_indentation = ::log_settings::settings().indentation;
51 for (&id, mir) in &mir_map.map {
52 for attr in tcx.map.attrs(id) {
53 use syntax::attr::AttrMetaMethods;
54 if attr.check_name("miri_run") {
55 let item = tcx.map.expect_item(id);
57 ::log_settings::settings().indentation = initial_indentation;
59 debug!("Interpreting: {}", item.name);
61 let mut ecx = EvalContext::new(tcx, mir_map);
62 let substs = tcx.mk_substs(subst::Substs::empty());
63 let return_ptr = ecx.alloc_ret_ptr(mir.return_ty, substs);
65 ecx.push_stack_frame(tcx.map.local_def_id(id), mir.span, CachedMir::Ref(mir), substs, return_ptr);
68 match step(&mut ecx) {
72 Some(ptr) => if log_enabled!(::log::LogLevel::Debug) {
73 ecx.memory().dump(ptr.alloc_id);
75 None => warn!("diverging function returned"),
79 // FIXME: diverging functions can end up here in some future miri
91 fn report(tcx: TyCtxt, ecx: &EvalContext, e: EvalError) {
92 let frame = ecx.stack().last().expect("stackframe was empty");
93 let block = &frame.mir.basic_blocks()[frame.next_block];
94 let span = if frame.stmt < block.statements.len() {
95 block.statements[frame.stmt].source_info.span
97 block.terminator().source_info.span
99 let mut err = tcx.sess.struct_span_err(span, &e.to_string());
100 for &Frame { def_id, substs, span, .. } in ecx.stack().iter().rev() {
101 // FIXME(solson): Find a way to do this without this Display impl hack.
102 use rustc::util::ppaux;
104 struct Instance<'tcx>(DefId, &'tcx subst::Substs<'tcx>);
105 impl<'tcx> fmt::Display for Instance<'tcx> {
106 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107 ppaux::parameterized(f, self.1, self.0, ppaux::Ns::Value, &[],
108 |tcx| Some(tcx.lookup_item_type(self.0).generics))
111 err.span_note(span, &format!("inside call to {}", Instance(def_id, substs)));
118 let args: Vec<String> = std::env::args().collect();
119 rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
123 const NSPACES: usize = 40;
124 let format = |record: &log::LogRecord| {
125 // prepend spaces to indent the final string
126 let indentation = log_settings::settings().indentation;
127 format!("{lvl}:{module}{depth:2}{indent:<indentation$} {text}",
128 lvl = record.level(),
129 module = record.location().module_path(),
130 depth = indentation / NSPACES,
131 indentation = indentation % NSPACES,
133 text = record.args())
136 let mut builder = env_logger::LogBuilder::new();
137 builder.format(format).filter(None, log::LogLevelFilter::Info);
139 if std::env::var("MIRI_LOG").is_ok() {
140 builder.parse(&std::env::var("MIRI_LOG").unwrap());
143 builder.init().unwrap();