cranelift_interpreter/
instruction.rs

1//! The [InstructionContext] trait describes a Cranelift instruction; a default implementation is
2//! provided with [DfgInstructionContext]
3use cranelift_codegen::ir::{DataFlowGraph, Inst, InstructionData, Type, Value};
4
5/// Exposes the necessary information for understanding a single Cranelift instruction. It would be
6/// nice if [InstructionData] contained everything necessary for interpreting the instruction, but
7/// Cranelift's current design requires looking at other structures. A default implementation using
8/// a reference to a [DataFlowGraph] is provided in [DfgInstructionContext].
9pub trait InstructionContext {
10    fn data(&self) -> InstructionData;
11    fn args(&self) -> &[Value];
12    fn type_of(&self, v: Value) -> Option<Type>;
13    fn controlling_type(&self) -> Option<Type>;
14}
15
16/// Since [InstructionContext] is likely used within a Cranelift context in which a [DataFlowGraph]
17/// is available, a default implementation is provided--[DfgInstructionContext].
18pub struct DfgInstructionContext<'a>(Inst, &'a DataFlowGraph);
19
20impl<'a> DfgInstructionContext<'a> {
21    pub fn new(inst: Inst, dfg: &'a DataFlowGraph) -> Self {
22        Self(inst, dfg)
23    }
24}
25
26impl InstructionContext for DfgInstructionContext<'_> {
27    fn data(&self) -> InstructionData {
28        self.1.insts[self.0]
29    }
30
31    fn args(&self) -> &[Value] {
32        self.1.inst_args(self.0)
33    }
34
35    fn type_of(&self, v: Value) -> Option<Type> {
36        Some(self.1.value_type(v))
37    }
38
39    fn controlling_type(&self) -> Option<Type> {
40        Some(self.1.ctrl_typevar(self.0))
41    }
42}