Add arguments() and arguments_mut() methods.

Provide a generic way of accessing the value arguments on an
instruction. This is provided as two slice references. One for the fixed
arguments and one for any VariableArgs.

The arguments() methods return an array of two slices which is a bit
awkward. Also provide an each_arg() method which passes each argument
value to a closure.
This commit is contained in:
Jakob Stoklund Olesen
2016-11-04 13:25:40 -07:00
parent c995cb6f43
commit 152aabbfc0
2 changed files with 125 additions and 1 deletions

View File

@@ -15,6 +15,8 @@ use ir::immediates::{Imm64, Uimm8, Ieee32, Ieee64, ImmVector};
use ir::condcodes::*;
use ir::types;
use ref_slice::*;
// Include code generated by `lib/cretonne/meta/gen_instr.py`. This file contains:
//
// - The `pub enum InstructionFormat` enum with all the instruction formats.
@@ -339,6 +341,18 @@ pub struct BranchData {
pub varargs: VariableArgs,
}
impl BranchData {
/// Get references to the arguments.
pub fn arguments(&self) -> [&[Value]; 2] {
[ref_slice(&self.arg), &self.varargs]
}
/// Get mutable references to the arguments.
pub fn arguments_mut(&mut self) -> [&mut [Value]; 2] {
[ref_slice_mut(&mut self.arg), &mut self.varargs]
}
}
impl Display for BranchData {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
try!(write!(f, "{}, {}", self.arg, self.destination));
@@ -372,6 +386,18 @@ pub struct IndirectCallData {
pub varargs: VariableArgs,
}
impl IndirectCallData {
/// Get references to the arguments.
pub fn arguments(&self) -> [&[Value]; 2] {
[ref_slice(&self.arg), &self.varargs]
}
/// Get mutable references to the arguments.
pub fn arguments_mut(&mut self) -> [&mut [Value]; 2] {
[ref_slice_mut(&mut self.arg), &mut self.varargs]
}
}
/// Payload of a return instruction.
#[derive(Clone, Debug)]
pub struct ReturnData {
@@ -381,9 +407,33 @@ pub struct ReturnData {
/// Analyzing an instruction.
///
/// Avoid large matches on instruction formats by using the methods efined here to examine
/// Avoid large matches on instruction formats by using the methods defined here to examine
/// instructions.
impl InstructionData {
/// Execute a closure once for each argument to this instruction.
/// See also the `arguments()` method.
pub fn each_arg<F>(&self, func: F)
where F: Fn(Value)
{
for part in &self.arguments() {
for &arg in part.iter() {
func(arg);
}
}
}
/// Execute a closure with a mutable reference to each argument to this instruction.
/// See also the `arguments_mut()` method.
pub fn each_arg_mut<F>(&mut self, func: F)
where F: Fn(&mut Value)
{
for part in &mut self.arguments_mut() {
for arg in part.iter_mut() {
func(arg);
}
}
}
/// Return information about the destination of a branch or jump instruction.
///
/// Any instruction that can transfer control to another EBB reveals its possible destinations