diff --git a/src/libcretonne/write.rs b/src/libcretonne/write.rs index 0d615c545b..38800bb48a 100644 --- a/src/libcretonne/write.rs +++ b/src/libcretonne/write.rs @@ -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"); } } diff --git a/src/tools/cat.rs b/src/tools/cat.rs index c8f30d0a99..6d740da701 100644 --- a/src/tools/cat.rs +++ b/src/tools/cat.rs @@ -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) -> 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(()) }