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:
@@ -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::*;
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user