Parse and write IR in the 'cat' subcommand.

The 'cton-util cat' command parses the given files and writes them out again to
stdout. This has the effect of reformatting and stripping comments.

Fix a writer bug that inverted the blank line before the first EBB.
This commit is contained in:
Jakob Stoklund Olesen
2016-07-01 14:09:34 -07:00
parent 96e88893be
commit 7519475f91
2 changed files with 20 additions and 7 deletions

View File

@@ -3,9 +3,13 @@
//! Read a sequence of Cretonne IL files and print them again to stdout. This has the effect of
//! normalizing formatting and removing comments.
use CommandResult;
use std::fs::File;
use std::io::Read;
use std::io::{self, Read};
use CommandResult;
use cton_reader::parser::Parser;
use cretonne::write::write_function;
pub fn run(files: Vec<String>) -> CommandResult {
for (i, f) in files.into_iter().enumerate() {
@@ -22,7 +26,16 @@ fn cat_one(filename: String) -> CommandResult {
let mut buffer = String::new();
try!(file.read_to_string(&mut buffer)
.map_err(|e| format!("Couldn't read {}: {}", filename, e)));
let items = try!(Parser::parse(&buffer).map_err(|e| format!("{}: {}", filename, e)));
for (idx, func) in items.into_iter().enumerate() {
if idx != 0 {
println!("");
}
let stdout = io::stdout();
let mut handle = stdout.lock();
try!(write_function(&mut handle, &func).map_err(|e| format!("{}: {}", filename, e)));
}
print!("{}", buffer);
Ok(())
}