diff --git a/cranelift/src/libcretonne/ir/function.rs b/cranelift/src/libcretonne/ir/function.rs index d6a44edfa6..415e8de9cc 100644 --- a/cranelift/src/libcretonne/ir/function.rs +++ b/cranelift/src/libcretonne/ir/function.rs @@ -6,7 +6,8 @@ use ir::{FunctionName, Signature, StackSlot, StackSlotData, JumpTable, JumpTableData, DataFlowGraph, Layout}; use entity_map::{EntityMap, PrimaryEntityData}; -use std::fmt::{self, Debug, Formatter}; +use std::fmt::{self, Display, Debug, Formatter}; +use write::write_function; /// A function. /// @@ -60,9 +61,14 @@ impl Function { } } -impl Debug for Function { +impl Display for Function { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { - use write::function_to_string; - fmt.write_str(&function_to_string(self)) + write_function(fmt, self) + } +} + +impl Debug for Function { + fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + write_function(fmt, self) } } diff --git a/cranelift/src/libcretonne/lib.rs b/cranelift/src/libcretonne/lib.rs index f338aba8ac..7b90a902c3 100644 --- a/cranelift/src/libcretonne/lib.rs +++ b/cranelift/src/libcretonne/lib.rs @@ -9,13 +9,13 @@ pub const VERSION: &'static str = env!("CARGO_PKG_VERSION"); pub mod ir; pub mod isa; -pub mod write; pub mod cfg; pub mod dominator_tree; pub mod entity_map; pub mod settings; pub mod verifier; +mod write; mod constant_hash; mod predicates; diff --git a/cranelift/src/libcretonne/write.rs b/cranelift/src/libcretonne/write.rs index a20007ed05..aa5076f5d5 100644 --- a/cranelift/src/libcretonne/write.rs +++ b/cranelift/src/libcretonne/write.rs @@ -5,9 +5,8 @@ //! `cretonne-reader` crate. use ir::{Function, Ebb, Inst, Value, Type}; -use std::io::{self, Write}; - -pub type Result = io::Result<()>; +use std::fmt::{Result, Error, Write}; +use std::result; /// Write `func` to `w` as equivalent text. pub fn write_function(w: &mut Write, func: &Function) -> Result { @@ -24,15 +23,6 @@ pub fn write_function(w: &mut Write, func: &Function) -> Result { writeln!(w, "}}") } -/// Convert `func` to a string. -pub fn function_to_string(func: &Function) -> String { - let mut buffer: Vec = Vec::new(); - // Any errors here would be out-of-memory, which should not happen with normal functions. - write_function(&mut buffer, func).unwrap(); - // A UTF-8 conversion error is a real bug. - String::from_utf8(buffer).unwrap() -} - // ====--------------------------------------------------------------------------------------====// // // Function spec. @@ -65,7 +55,7 @@ fn write_spec(w: &mut Write, func: &Function) -> Result { } } -fn write_preamble(w: &mut Write, func: &Function) -> io::Result { +fn write_preamble(w: &mut Write, func: &Function) -> result::Result { let mut any = false; for ss in func.stack_slots.keys() { @@ -218,7 +208,6 @@ pub fn write_instruction(w: &mut Write, func: &Function, inst: Inst) -> Result { #[cfg(test)] mod tests { - use super::*; use super::{needs_quotes, escaped}; use ir::{Function, StackSlotData}; use ir::types; @@ -244,26 +233,26 @@ mod tests { #[test] fn basic() { let mut f = Function::new(); - assert_eq!(function_to_string(&f), "function \"\"() {\n}\n"); + assert_eq!(f.to_string(), "function \"\"() {\n}\n"); f.name.push_str("foo"); - assert_eq!(function_to_string(&f), "function foo() {\n}\n"); + assert_eq!(f.to_string(), "function foo() {\n}\n"); f.stack_slots.push(StackSlotData::new(4)); - assert_eq!(function_to_string(&f), + assert_eq!(f.to_string(), "function foo() {\n ss0 = stack_slot 4\n}\n"); let ebb = f.dfg.make_ebb(); f.layout.append_ebb(ebb); - assert_eq!(function_to_string(&f), + assert_eq!(f.to_string(), "function foo() {\n ss0 = stack_slot 4\n\nebb0:\n}\n"); f.dfg.append_ebb_arg(ebb, types::I8); - assert_eq!(function_to_string(&f), + assert_eq!(f.to_string(), "function foo() {\n ss0 = stack_slot 4\n\nebb0(vx0: i8):\n}\n"); f.dfg.append_ebb_arg(ebb, types::F32.by(4).unwrap()); - assert_eq!(function_to_string(&f), + assert_eq!(f.to_string(), "function foo() {\n ss0 = stack_slot 4\n\nebb0(vx0: i8, vx1: f32x4):\n}\n"); } } diff --git a/cranelift/src/tools/cat.rs b/cranelift/src/tools/cat.rs index 566997c403..0db69b48db 100644 --- a/cranelift/src/tools/cat.rs +++ b/cranelift/src/tools/cat.rs @@ -3,11 +3,9 @@ //! Read a sequence of Cretonne IL files and print them again to stdout. This has the effect of //! normalizing formatting and removing comments. -use std::io; use CommandResult; use utils::read_to_string; use cton_reader::parse_functions; -use cretonne::write::write_function; pub fn run(files: Vec) -> CommandResult { for (i, f) in files.into_iter().enumerate() { @@ -27,9 +25,7 @@ fn cat_one(filename: String) -> CommandResult { 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!("{}", func); } Ok(())