Path: blob/main/cranelift/interpreter/src/instruction.rs
1692 views
//! The [InstructionContext] trait describes a Cranelift instruction; a default implementation is1//! provided with [DfgInstructionContext]2use cranelift_codegen::ir::{DataFlowGraph, Inst, InstructionData, Type, Value};34/// Exposes the necessary information for understanding a single Cranelift instruction. It would be5/// nice if [InstructionData] contained everything necessary for interpreting the instruction, but6/// Cranelift's current design requires looking at other structures. A default implementation using7/// a reference to a [DataFlowGraph] is provided in [DfgInstructionContext].8pub trait InstructionContext {9fn data(&self) -> InstructionData;10fn args(&self) -> &[Value];11fn type_of(&self, v: Value) -> Option<Type>;12fn controlling_type(&self) -> Option<Type>;13}1415/// Since [InstructionContext] is likely used within a Cranelift context in which a [DataFlowGraph]16/// is available, a default implementation is provided--[DfgInstructionContext].17pub struct DfgInstructionContext<'a>(Inst, &'a DataFlowGraph);1819impl<'a> DfgInstructionContext<'a> {20pub fn new(inst: Inst, dfg: &'a DataFlowGraph) -> Self {21Self(inst, dfg)22}23}2425impl InstructionContext for DfgInstructionContext<'_> {26fn data(&self) -> InstructionData {27self.1.insts[self.0]28}2930fn args(&self) -> &[Value] {31self.1.inst_args(self.0)32}3334fn type_of(&self, v: Value) -> Option<Type> {35Some(self.1.value_type(v))36}3738fn controlling_type(&self) -> Option<Type> {39Some(self.1.ctrl_typevar(self.0))40}41}424344