2016-06-05 09:00:17 +03:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! This pass just dumps MIR at a specified point.
|
|
|
|
|
2017-04-25 18:23:33 -04:00
|
|
|
use std::borrow::Cow;
|
2016-06-08 21:03:06 +03:00
|
|
|
use std::fmt;
|
2017-02-16 16:59:09 -05:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
2016-06-08 21:03:06 +03:00
|
|
|
|
2017-04-28 19:29:16 -04:00
|
|
|
use rustc::mir::Mir;
|
2017-05-01 13:46:35 -04:00
|
|
|
use rustc::mir::transform::{MirPass, MirPassIndex, MirSource, MirSuite, PassHook};
|
2017-02-16 16:59:09 -05:00
|
|
|
use rustc::session::config::{OutputFilenames, OutputType};
|
2016-06-05 09:00:17 +03:00
|
|
|
use rustc::ty::TyCtxt;
|
2017-03-09 20:10:05 +02:00
|
|
|
use util as mir_util;
|
2016-06-05 09:00:17 +03:00
|
|
|
|
2017-04-25 18:23:33 -04:00
|
|
|
pub struct Marker(pub &'static str);
|
2016-06-08 21:03:06 +03:00
|
|
|
|
2017-05-01 13:46:35 -04:00
|
|
|
impl MirPass for Marker {
|
2017-04-25 18:23:33 -04:00
|
|
|
fn name<'a>(&'a self) -> Cow<'a, str> {
|
|
|
|
Cow::Borrowed(self.0)
|
|
|
|
}
|
2016-06-08 21:03:06 +03:00
|
|
|
|
2017-05-01 13:46:35 -04:00
|
|
|
fn run_pass<'a, 'tcx>(&self,
|
|
|
|
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
_source: MirSource,
|
|
|
|
_mir: &mut Mir<'tcx>)
|
|
|
|
{
|
2017-04-25 18:23:33 -04:00
|
|
|
}
|
2016-06-08 21:03:06 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 18:23:33 -04:00
|
|
|
pub struct Disambiguator {
|
2016-06-08 21:03:06 +03:00
|
|
|
is_after: bool
|
|
|
|
}
|
|
|
|
|
2017-04-25 18:23:33 -04:00
|
|
|
impl fmt::Display for Disambiguator {
|
2016-06-08 21:03:06 +03:00
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let title = if self.is_after { "after" } else { "before" };
|
2017-04-25 18:23:33 -04:00
|
|
|
write!(formatter, "{}", title)
|
2016-06-08 21:03:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DumpMir;
|
2016-06-05 09:00:17 +03:00
|
|
|
|
2017-04-25 18:23:33 -04:00
|
|
|
impl PassHook for DumpMir {
|
2017-04-27 16:48:48 -04:00
|
|
|
fn on_mir_pass<'a, 'tcx: 'a>(&self,
|
2017-05-01 13:46:35 -04:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
suite: MirSuite,
|
|
|
|
pass_num: MirPassIndex,
|
|
|
|
pass_name: &str,
|
|
|
|
source: MirSource,
|
|
|
|
mir: &Mir<'tcx>,
|
|
|
|
is_after: bool)
|
2016-06-08 21:03:06 +03:00
|
|
|
{
|
2017-05-01 13:46:35 -04:00
|
|
|
if mir_util::dump_enabled(tcx, pass_name, source) {
|
2017-04-27 16:48:48 -04:00
|
|
|
mir_util::dump_mir(tcx,
|
2017-04-28 06:00:48 -04:00
|
|
|
Some((suite, pass_num)),
|
2017-05-01 13:46:35 -04:00
|
|
|
pass_name,
|
|
|
|
&Disambiguator { is_after },
|
2017-04-27 16:48:48 -04:00
|
|
|
source,
|
2017-09-28 00:14:34 -04:00
|
|
|
mir,
|
|
|
|
|_, _| Ok(()) );
|
|
|
|
for (index, promoted_mir) in mir.promoted.iter_enumerated() {
|
|
|
|
let promoted_source = MirSource::Promoted(source.item_id(), index);
|
|
|
|
mir_util::dump_mir(tcx,
|
|
|
|
Some((suite, pass_num)),
|
|
|
|
pass_name,
|
|
|
|
&Disambiguator { is_after },
|
|
|
|
promoted_source,
|
|
|
|
promoted_mir,
|
|
|
|
|_, _| Ok(()) );
|
|
|
|
}
|
2017-04-25 18:23:33 -04:00
|
|
|
}
|
2016-06-05 09:00:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 16:59:09 -05:00
|
|
|
pub fn emit_mir<'a, 'tcx>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
outputs: &OutputFilenames)
|
|
|
|
-> io::Result<()>
|
|
|
|
{
|
|
|
|
let path = outputs.path(OutputType::Mir);
|
|
|
|
let mut f = File::create(&path)?;
|
2017-04-25 15:56:02 -04:00
|
|
|
mir_util::write_mir_pretty(tcx, None, &mut f)?;
|
2017-02-16 16:59:09 -05:00
|
|
|
Ok(())
|
|
|
|
}
|