Add CursorBase builder methods.

The CursorBase::at_* are convenience methods for building a cursor that
points to a specific instruction.
This commit is contained in:
Jakob Stoklund Olesen
2017-08-03 18:03:58 -07:00
parent dba0df787c
commit aa0c37235a
2 changed files with 43 additions and 5 deletions

View File

@@ -672,6 +672,47 @@ pub trait CursorBase {
/// Borrow a mutable reference to the function layout that this cursor is navigating. /// Borrow a mutable reference to the function layout that this cursor is navigating.
fn layout_mut(&mut self) -> &mut Layout; fn layout_mut(&mut self) -> &mut Layout;
/// Rebuild this cursor positioned at `inst`.
///
/// This is intended to be used as a builder method:
///
/// ```
/// # use cretonne::ir::{Function, Ebb, Inst};
/// # use cretonne::ir::layout::{Cursor, CursorBase};
/// fn edit_func(func: &mut Function, inst: Inst) {
/// let mut pos = Cursor::new(&mut func.layout).at_inst(inst);
///
/// // Use `pos`...
/// }
/// ```
fn at_inst(mut self, inst: Inst) -> Self
where Self: Sized
{
self.goto_inst(inst);
self
}
/// Rebuild this cursor positioned at the first instruction in `ebb`.
///
/// This is intended to be used as a builder method:
///
/// ```
/// # use cretonne::ir::{Function, Ebb, Inst};
/// # use cretonne::ir::layout::{Cursor, CursorBase};
/// fn edit_func(func: &mut Function, ebb: Ebb) {
/// let mut pos = Cursor::new(&mut func.layout).at_first_inst(ebb);
///
/// // Use `pos`...
/// }
/// ```
fn at_first_inst(mut self, ebb: Ebb) -> Self
where Self: Sized
{
let inst = self.layout().ebbs[ebb].first_inst.expect("Empty EBB");
self.goto_inst(inst);
self
}
/// Get the EBB corresponding to the current position. /// Get the EBB corresponding to the current position.
fn current_ebb(&self) -> Option<Ebb> { fn current_ebb(&self) -> Option<Ebb> {
use self::CursorPosition::*; use self::CursorPosition::*;

View File

@@ -466,8 +466,7 @@ impl<'a> Context<'a> {
-> Value { -> Value {
let copy; let copy;
{ {
let mut pos = Cursor::new(&mut self.func.layout); let mut pos = Cursor::new(&mut self.func.layout).at_inst(pred_inst);
pos.goto_inst(pred_inst);
copy = self.func.dfg.ins(&mut pos).copy(pred_val); copy = self.func.dfg.ins(&mut pos).copy(pred_val);
} }
let inst = self.func.dfg.value_def(copy).unwrap_inst(); let inst = self.func.dfg.value_def(copy).unwrap_inst();
@@ -507,9 +506,7 @@ impl<'a> Context<'a> {
// Insert a copy instruction at the top of ebb. // Insert a copy instruction at the top of ebb.
{ {
let mut pos = Cursor::new(&mut self.func.layout); let mut pos = Cursor::new(&mut self.func.layout).at_first_inst(ebb);
pos.goto_top(ebb);
pos.next_inst();
self.func self.func
.dfg .dfg
.ins(&mut pos) .ins(&mut pos)