Derive Macro InstrMatch
#[derive(InstrMatch)]
{
// Attributes available to this derive:
#[pattern]
}
Expand description
Derive macro for mapping instruction pattern matching to structs.
This macro allows you to annotate a struct with a #[pattern(...)]
attribute,
specifying a Rust pattern that matches a LowLevelILInstruction
.
An implementation of the TryFrom
trait will be generated to support converting
from LowLevelILInstruction
. This enables ergonomic and type-safe matching and
extraction of instructions and their subexpressions.
§Details
Bindings in the pattern correspond to the struct’s fields. Each binding must have a matching field of the appropriate type.
Fields are initialized using Into
conversions from the matched instruction and expressions.
This allows you to bind an expression to a field of type Expression
, ExpressionKind
, or
LowLevelILExpression
depending on your needs, and similarly for the instruction itself.
If the struct binds to an instruction or expression type that requires generic parameters,
struct must be defined with the necessary generic parameters for the object it captures.
For instructions and expressions this will often be the function mutability and form traits.
These MUST use the names 'func
, M
, and F
to avoid conflicting with the generated implementations.
§Example
#[derive(InstrMatch)]
#[pattern(instr @ SetRegSsa(dest, Lsr(RegSsa(source), Const(5))))]
struct SetToLsrBy5<'func, M, F>
where
M: FunctionMutability,
F: FunctionForm,
{
instr: LowLevelILInstruction<'func, M, F>,
dest: LowLevelILSSARegisterKind<CoreRegister>,
source: LowLevelILSSARegisterKind<CoreRegister>,
}
// Usage:
let match_result = SetToLsrBy5::try_from(instr);
if let Ok(matched) = match_result {
println!(
"Matched SetRegSsa({:?}, Lsr(RegSsa({:?}), Const(5))) at address: {:#x}",
matched.dest, matched.source, matched.instr.address(),
);
// You can now use `matched.instr` and other fields as needed.
} else {
println!("Instruction did not match");
}