Browse Source

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.
pull/1019/head
Jakob Stoklund Olesen 8 years ago
parent
commit
5ce1a4f0e8
  1. 8
      cranelift/src/libcretonne/write.rs
  2. 19
      cranelift/src/tools/cat.rs

8
cranelift/src/libcretonne/write.rs

@ -16,7 +16,7 @@ pub fn write_function(w: &mut Write, func: &Function) -> Result {
try!(writeln!(w, " {{")); try!(writeln!(w, " {{"));
let mut any = try!(write_preamble(w, func)); let mut any = try!(write_preamble(w, func));
for ebb in func.ebbs_numerically() { for ebb in func.ebbs_numerically() {
if !any { if any {
try!(writeln!(w, "")); try!(writeln!(w, ""));
} }
try!(write_ebb(w, func, ebb)); try!(write_ebb(w, func, ebb));
@ -209,14 +209,14 @@ mod tests {
let ebb = f.make_ebb(); let ebb = f.make_ebb();
assert_eq!(function_to_string(&f), 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); f.append_ebb_arg(ebb, types::I8);
assert_eq!(function_to_string(&f), 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()); f.append_ebb_arg(ebb, types::F32.by(4).unwrap());
assert_eq!(function_to_string(&f), 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");
} }
} }

19
cranelift/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 //! Read a sequence of Cretonne IL files and print them again to stdout. This has the effect of
//! normalizing formatting and removing comments. //! normalizing formatting and removing comments.
use CommandResult;
use std::fs::File; 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 { pub fn run(files: Vec<String>) -> CommandResult {
for (i, f) in files.into_iter().enumerate() { for (i, f) in files.into_iter().enumerate() {
@ -22,7 +26,16 @@ fn cat_one(filename: String) -> CommandResult {
let mut buffer = String::new(); let mut buffer = String::new();
try!(file.read_to_string(&mut buffer) try!(file.read_to_string(&mut buffer)
.map_err(|e| format!("Couldn't read {}: {}", filename, e))); .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(()) Ok(())
} }

Loading…
Cancel
Save