diff --git a/cranelift/src/libcretonne/cfg.rs b/cranelift/src/libcretonne/cfg.rs index 8c423c23ac..668f5b51ef 100644 --- a/cranelift/src/libcretonne/cfg.rs +++ b/cranelift/src/libcretonne/cfg.rs @@ -94,37 +94,10 @@ impl ControlFlowGraph { #[cfg(test)] mod tests { - use instructions::*; - use entity_map::EntityRef; - use entities::{Ebb, Inst, NO_VALUE}; - use repr::Function; use super::*; - use types; + use repr::Function; - // Some instructions will be re-used in several tests. - - fn jump(func: &mut Function, dest: Ebb) -> Inst { - func.make_inst(InstructionData::Jump { - opcode: Opcode::Jump, - ty: types::VOID, - data: Box::new(JumpData { - destination: dest, - arguments: VariableArgs::new(), - }), - }) - } - - fn branch(func: &mut Function, dest: Ebb) -> Inst { - func.make_inst(InstructionData::Branch { - opcode: Opcode::Brz, - ty: types::VOID, - data: Box::new(BranchData { - arg: NO_VALUE, - destination: dest, - arguments: VariableArgs::new(), - }), - }) - } + use test_utils::make_inst; #[test] fn empty() { @@ -145,7 +118,7 @@ mod tests { let mut fun_ebbs = func.ebbs_numerically(); for (ebb, predecessors) in nodes { - assert_eq!(ebb.index(), fun_ebbs.next().unwrap().index()); + assert_eq!(*ebb, fun_ebbs.next().unwrap()); assert_eq!(predecessors.len(), 0); } } @@ -157,16 +130,16 @@ mod tests { let ebb1 = func.make_ebb(); let ebb2 = func.make_ebb(); - let br_ebb0_ebb2 = branch(&mut func, ebb2); + let br_ebb0_ebb2 = make_inst::branch(&mut func, ebb2); func.append_inst(ebb0, br_ebb0_ebb2); - let jmp_ebb0_ebb1 = jump(&mut func, ebb1); + let jmp_ebb0_ebb1 = make_inst::jump(&mut func, ebb1); func.append_inst(ebb0, jmp_ebb0_ebb1); - let br_ebb1_ebb1 = branch(&mut func, ebb1); + let br_ebb1_ebb1 = make_inst::branch(&mut func, ebb1); func.append_inst(ebb1, br_ebb1_ebb1); - let jmp_ebb1_ebb2 = jump(&mut func, ebb2); + let jmp_ebb1_ebb2 = make_inst::jump(&mut func, ebb2); func.append_inst(ebb1, jmp_ebb1_ebb2); let cfg = ControlFlowGraph::new(&func); diff --git a/cranelift/src/libcretonne/lib.rs b/cranelift/src/libcretonne/lib.rs index 100d3121e2..d5c501477c 100644 --- a/cranelift/src/libcretonne/lib.rs +++ b/cranelift/src/libcretonne/lib.rs @@ -18,3 +18,5 @@ pub mod write; pub mod cfg; pub mod entity_map; + +#[cfg(test)] pub mod test_utils; diff --git a/cranelift/src/libcretonne/test_utils/make_inst.rs b/cranelift/src/libcretonne/test_utils/make_inst.rs new file mode 100644 index 0000000000..167512b8b5 --- /dev/null +++ b/cranelift/src/libcretonne/test_utils/make_inst.rs @@ -0,0 +1,35 @@ +///! Helper functions for generating dummy instructions. + +use repr::Function; +use entities::{Ebb, Inst, NO_VALUE}; +use instructions::{ + InstructionData, + Opcode, + VariableArgs, + JumpData, + BranchData, +}; +use types; + +pub fn jump(func: &mut Function, dest: Ebb) -> Inst { + func.make_inst(InstructionData::Jump { + opcode: Opcode::Jump, + ty: types::VOID, + data: Box::new(JumpData { + destination: dest, + arguments: VariableArgs::new(), + }), + }) +} + +pub fn branch(func: &mut Function, dest: Ebb) -> Inst { + func.make_inst(InstructionData::Branch { + opcode: Opcode::Brz, + ty: types::VOID, + data: Box::new(BranchData { + arg: NO_VALUE, + destination: dest, + arguments: VariableArgs::new(), + }), + }) +} diff --git a/cranelift/src/libcretonne/test_utils/mod.rs b/cranelift/src/libcretonne/test_utils/mod.rs new file mode 100644 index 0000000000..49e6b23be8 --- /dev/null +++ b/cranelift/src/libcretonne/test_utils/mod.rs @@ -0,0 +1,3 @@ +///! Test utility functions. + +pub mod make_inst;