Skip to content

Commit

Permalink
Teach rustc --emit=mir
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed Mar 22, 2017
1 parent 58c701f commit 9218f97
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/librustc/session/config.rs
Expand Up @@ -82,6 +82,7 @@ pub enum OutputType {
Bitcode,
Assembly,
LlvmAssembly,
Mir,
Metadata,
Object,
Exe,
Expand All @@ -96,6 +97,7 @@ impl OutputType {
OutputType::Bitcode |
OutputType::Assembly |
OutputType::LlvmAssembly |
OutputType::Mir |
OutputType::Object |
OutputType::Metadata => false,
}
Expand All @@ -106,6 +108,7 @@ impl OutputType {
OutputType::Bitcode => "llvm-bc",
OutputType::Assembly => "asm",
OutputType::LlvmAssembly => "llvm-ir",
OutputType::Mir => "mir",
OutputType::Object => "obj",
OutputType::Metadata => "metadata",
OutputType::Exe => "link",
Expand All @@ -118,6 +121,7 @@ impl OutputType {
OutputType::Bitcode => "bc",
OutputType::Assembly => "s",
OutputType::LlvmAssembly => "ll",
OutputType::Mir => "mir",
OutputType::Object => "o",
OutputType::Metadata => "rmeta",
OutputType::DepInfo => "d",
Expand Down Expand Up @@ -172,6 +176,7 @@ impl OutputTypes {
OutputType::Bitcode |
OutputType::Assembly |
OutputType::LlvmAssembly |
OutputType::Mir |
OutputType::Object |
OutputType::Exe => true,
OutputType::Metadata |
Expand Down Expand Up @@ -1370,6 +1375,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
let output_type = match parts.next().unwrap() {
"asm" => OutputType::Assembly,
"llvm-ir" => OutputType::LlvmAssembly,
"mir" => OutputType::Mir,
"llvm-bc" => OutputType::Bitcode,
"obj" => OutputType::Object,
"metadata" => OutputType::Metadata,
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_driver/driver.rs
Expand Up @@ -209,6 +209,13 @@ pub fn compile_input(sess: &Session,
tcx.print_debug_stats();
}

if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
if let Err(e) = mir::transform::dump_mir::emit_mir(tcx, &outputs) {
sess.err(&format!("could not emit MIR: {}", e));
sess.abort_if_errors();
}
}

Ok((outputs, trans))
})??
};
Expand Down
14 changes: 14 additions & 0 deletions src/librustc_mir/transform/dump_mir.rs
Expand Up @@ -11,7 +11,10 @@
//! This pass just dumps MIR at a specified point.

use std::fmt;
use std::fs::File;
use std::io;

use rustc::session::config::{OutputFilenames, OutputType};
use rustc::ty::TyCtxt;
use rustc::mir::*;
use rustc::mir::transform::{Pass, MirPass, MirPassHook, MirSource};
Expand Down Expand Up @@ -70,3 +73,14 @@ impl<'tcx> MirPassHook<'tcx> for DumpMir {
}

impl<'b> Pass for DumpMir {}

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)?;
mir_util::write_mir_pretty(tcx, tcx.maps.mir.borrow().keys().into_iter(), &mut f)?;
Ok(())
}
2 changes: 2 additions & 0 deletions src/librustc_trans/back/write.rs
Expand Up @@ -741,6 +741,7 @@ pub fn run_passes(sess: &Session,
modules_config.emit_obj = true;
metadata_config.emit_obj = true;
},
OutputType::Mir => {}
OutputType::DepInfo => {}
}
}
Expand Down Expand Up @@ -880,6 +881,7 @@ pub fn run_passes(sess: &Session,
user_wants_objects = true;
copy_if_one_unit(OutputType::Object, true);
}
OutputType::Mir |
OutputType::Metadata |
OutputType::Exe |
OutputType::DepInfo => {}
Expand Down

0 comments on commit 9218f97

Please sign in to comment.