From 78312c6c46756441ee276fc9d432706a6888e384 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Wed, 19 Oct 2016 18:50:11 -0700 Subject: [PATCH] Add a Cursor::ins() method which constructs a Builder. Rewrite Builder uses in test cases to use this method and construct a new builder for each instruction. This pattern allows us to change the InstBuilder trait to a one-shot implementation that can only create a single instruction. Don't re-export the Builder struct, it is less important than the InstBuilder trait, and we may get more implementations. --- lib/cretonne/src/cfg.rs | 20 ++++++++++---------- lib/cretonne/src/dominator_tree.rs | 22 +++++++++++----------- lib/cretonne/src/ir/layout.rs | 7 +++++++ lib/cretonne/src/ir/mod.rs | 2 +- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/cretonne/src/cfg.rs b/lib/cretonne/src/cfg.rs index 59ced57d82..a50d4ff89f 100644 --- a/lib/cretonne/src/cfg.rs +++ b/lib/cretonne/src/cfg.rs @@ -139,7 +139,7 @@ impl ControlFlowGraph { #[cfg(test)] mod tests { use super::*; - use ir::{Function, Builder, InstBuilder, Cursor, VariableArgs, types}; + use ir::{Function, InstBuilder, Cursor, VariableArgs, types}; #[test] fn empty() { @@ -184,18 +184,18 @@ mod tests { let jmp_ebb1_ebb2; { - let mut cursor = Cursor::new(&mut func.layout); - let mut b = Builder::new(&mut func.dfg, &mut cursor); + let mut cur = Cursor::new(&mut func.layout); + let dfg = &mut func.dfg; - b.insert_ebb(ebb0); - br_ebb0_ebb2 = b.brnz(cond, ebb2, VariableArgs::new()); - jmp_ebb0_ebb1 = b.jump(ebb1, VariableArgs::new()); + cur.insert_ebb(ebb0); + br_ebb0_ebb2 = cur.ins(dfg).brnz(cond, ebb2, VariableArgs::new()); + jmp_ebb0_ebb1 = cur.ins(dfg).jump(ebb1, VariableArgs::new()); - b.insert_ebb(ebb1); - br_ebb1_ebb1 = b.brnz(cond, ebb1, VariableArgs::new()); - jmp_ebb1_ebb2 = b.jump(ebb2, VariableArgs::new()); + cur.insert_ebb(ebb1); + br_ebb1_ebb1 = cur.ins(dfg).brnz(cond, ebb1, VariableArgs::new()); + jmp_ebb1_ebb2 = cur.ins(dfg).jump(ebb2, VariableArgs::new()); - b.insert_ebb(ebb2); + cur.insert_ebb(ebb2); } let cfg = ControlFlowGraph::new(&func); diff --git a/lib/cretonne/src/dominator_tree.rs b/lib/cretonne/src/dominator_tree.rs index 85d224060d..d109cc9f9f 100644 --- a/lib/cretonne/src/dominator_tree.rs +++ b/lib/cretonne/src/dominator_tree.rs @@ -116,7 +116,7 @@ impl DominatorTree { #[cfg(test)] mod test { use super::*; - use ir::{Function, Builder, InstBuilder, Cursor, VariableArgs, types}; + use ir::{Function, InstBuilder, Cursor, VariableArgs, types}; use ir::entities::NO_INST; use cfg::ControlFlowGraph; @@ -142,20 +142,20 @@ mod test { let jmp_ebb1_ebb2; { - let mut cursor = Cursor::new(&mut func.layout); - let mut b = Builder::new(&mut func.dfg, &mut cursor); + let mut cur = Cursor::new(&mut func.layout); + let dfg = &mut func.dfg; - b.insert_ebb(ebb3); - jmp_ebb3_ebb1 = b.jump(ebb1, VariableArgs::new()); + cur.insert_ebb(ebb3); + jmp_ebb3_ebb1 = cur.ins(dfg).jump(ebb1, VariableArgs::new()); - b.insert_ebb(ebb1); - br_ebb1_ebb0 = b.brnz(cond, ebb0, VariableArgs::new()); - jmp_ebb1_ebb2 = b.jump(ebb2, VariableArgs::new()); + cur.insert_ebb(ebb1); + br_ebb1_ebb0 = cur.ins(dfg).brnz(cond, ebb0, VariableArgs::new()); + jmp_ebb1_ebb2 = cur.ins(dfg).jump(ebb2, VariableArgs::new()); - b.insert_ebb(ebb2); - b.jump(ebb0, VariableArgs::new()); + cur.insert_ebb(ebb2); + cur.ins(dfg).jump(ebb0, VariableArgs::new()); - b.insert_ebb(ebb0); + cur.insert_ebb(ebb0); } let cfg = ControlFlowGraph::new(&func); diff --git a/lib/cretonne/src/ir/layout.rs b/lib/cretonne/src/ir/layout.rs index 864420c4f5..3997b93349 100644 --- a/lib/cretonne/src/ir/layout.rs +++ b/lib/cretonne/src/ir/layout.rs @@ -6,6 +6,8 @@ use std::iter::{Iterator, IntoIterator}; use entity_map::{EntityMap, EntityRef}; use ir::entities::{Ebb, NO_EBB, Inst, NO_INST}; +use ir::dfg::DataFlowGraph; +use ir::builder::Builder; /// The `Layout` struct determines the layout of EBBs and instructions in a function. It does not /// contain definitions of instructions or EBBs, but depends on `Inst` and `Ebb` entity references @@ -615,6 +617,11 @@ impl<'f> Cursor<'f> { } } + /// Create a builder for inserting an instruction at the current position. + pub fn ins<'c, 'fd>(&'c mut self, dfg: &'fd mut DataFlowGraph) -> Builder<'c, 'f, 'fd> { + Builder::new(dfg, self) + } + /// Insert an EBB at the current position and switch to it. /// /// As far as possible, this method behaves as if the EBB header were an instruction inserted diff --git a/lib/cretonne/src/ir/mod.rs b/lib/cretonne/src/ir/mod.rs index 0812496032..b8cda7711c 100644 --- a/lib/cretonne/src/ir/mod.rs +++ b/lib/cretonne/src/ir/mod.rs @@ -24,4 +24,4 @@ pub use ir::jumptable::JumpTableData; pub use ir::dfg::{DataFlowGraph, ValueDef}; pub use ir::layout::{Layout, Cursor}; pub use ir::function::Function; -pub use ir::builder::{Builder, InstBuilder}; +pub use ir::builder::InstBuilder;