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 61094f6909
commit 5ce1a4f0e8
2 changed files with 20 additions and 7 deletions

View File

@@ -16,7 +16,7 @@ pub fn write_function(w: &mut Write, func: &Function) -> Result {
try!(writeln!(w, " {{"));
let mut any = try!(write_preamble(w, func));
for ebb in func.ebbs_numerically() {
if !any {
if any {
try!(writeln!(w, ""));
}
try!(write_ebb(w, func, ebb));
@@ -209,14 +209,14 @@ mod tests {
let ebb = f.make_ebb();
assert_eq!(function_to_string(&f),
"function foo() {\n ss0 = stack_slot 4\nebb0:\n}\n");
"function foo() {\n ss0 = stack_slot 4\n\nebb0:\n}\n");
f.append_ebb_arg(ebb, types::I8);
assert_eq!(function_to_string(&f),
"function foo() {\n ss0 = stack_slot 4\nebb0(vx0: i8):\n}\n");
"function foo() {\n ss0 = stack_slot 4\n\nebb0(vx0: i8):\n}\n");
f.append_ebb_arg(ebb, types::F32.by(4).unwrap());
assert_eq!(function_to_string(&f),
"function foo() {\n ss0 = stack_slot 4\nebb0(vx0: i8, vx1: f32x4):\n}\n");
"function foo() {\n ss0 = stack_slot 4\n\nebb0(vx0: i8, vx1: f32x4):\n}\n");
}
}

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(())
}