1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! Interfacing with actual instructions.

use crate::operator::Operator;
use crate::part::{Constant, Part};
use crate::paths::Path;
use crate::r#type::Type;
use std::fmt::Debug;

/// A trait for interfacing with actual instruction sequences.
///
/// This trait enables both:
///
/// * `peepmatic-runtime` to be used by `cranelift-codegen` without a circular
///   dependency from `peepmatic-runtime` to `cranelift-codegen` to get access
///   to Cranelift's IR types, and
///
/// * enables us to write local tests that exercise peephole optimizers on a
///  simple, testing-only instruction set without pulling in all of Cranelift.
///
/// Finally, this should also make the task of adding support for Cranelift's
/// new `MachInst` and vcode backend easier, since all that needs to be done is
/// "just" implementing this trait. (And probably add/modify some
/// `peepmatic_runtime::operation::Operation`s as well).
///
/// ## Safety
///
/// See doc comment for `instruction_result_bit_width`.
pub unsafe trait InstructionSet<'a> {
    /// Mutable context passed into all trait methods. Can be whatever you want!
    ///
    /// In practice, this is a `FuncCursor` for `cranelift-codegen`'s trait
    /// implementation.
    type Context;

    /// An instruction (or identifier for an instruction).
    type Instruction: Copy + Debug + Eq;

    /// Replace the `old` instruction with `new`.
    ///
    /// `new` is either a `Part::Instruction` or a constant `Part::Boolean` or
    /// `Part::Integer`. In the former case, it can directly replace `old`. In
    /// the latter case, implementations of this trait should transparently
    /// create an `iconst` or `bconst` instruction to wrap the given constant.
    ///
    /// `new` will never be `Part::ConditionCode`.
    fn replace_instruction(
        &self,
        context: &mut Self::Context,
        old: Self::Instruction,
        new: Part<Self::Instruction>,
    ) -> Self::Instruction;

    /// Get the instruction, constant, or condition code at the given path.
    ///
    /// If there is no such entity at the given path (e.g. we run into a
    /// function parameter and can't traverse the path any further) then `None`
    /// should be returned.
    fn get_part_at_path(
        &self,
        context: &mut Self::Context,
        root: Self::Instruction,
        path: Path,
    ) -> Option<Part<Self::Instruction>>;

    /// Get the given instruction's operator.
    ///
    /// If the instruction's opcode does not have an associated
    /// `peepmatic_runtime::operator::Operator` variant (i.e. that instruction
    /// isn't supported by `peepmatic` yet) then `None` should be returned.
    fn operator(&self, context: &mut Self::Context, instr: Self::Instruction) -> Option<Operator>;

    /// Make a unary instruction.
    ///
    /// If the type is not given, then it should be inferred.
    fn make_inst_1(
        &self,
        context: &mut Self::Context,
        root: Self::Instruction,
        operator: Operator,
        r#type: Type,
        a: Part<Self::Instruction>,
    ) -> Self::Instruction;

    /// Make a binary instruction.
    ///
    /// Operands are given as immediates first and arguments following
    /// them. Condition codes are treated as immediates. So if we are creating
    /// an `iadd_imm` instruction, then `a` will be the constant integer
    /// immediate and `b` will be the instruction whose result is the dynamic
    /// argument.
    fn make_inst_2(
        &self,
        context: &mut Self::Context,
        root: Self::Instruction,
        operator: Operator,
        r#type: Type,
        a: Part<Self::Instruction>,
        b: Part<Self::Instruction>,
    ) -> Self::Instruction;

    /// Make a ternary instruction.
    ///
    /// Operands are given as immediates first and arguments following
    /// them. Condition codes are treated as immediates. So if we are creating
    /// an `icmp` instruction, then `a` will be the condition code, and `b` and
    /// `c` will be instructions whose results are the dynamic arguments.
    fn make_inst_3(
        &self,
        context: &mut Self::Context,
        root: Self::Instruction,
        operator: Operator,
        r#type: Type,
        a: Part<Self::Instruction>,
        b: Part<Self::Instruction>,
        c: Part<Self::Instruction>,
    ) -> Self::Instruction;

    /// Try to resolve the given instruction into a constant value.
    ///
    /// If we can tell that the instruction returns a constant value, then
    /// return that constant value as either a `Part::Boolean` or
    /// `Part::Integer`. Otherwise, return `None`.
    fn instruction_to_constant(
        &self,
        context: &mut Self::Context,
        inst: Self::Instruction,
    ) -> Option<Constant>;

    /// Get the bit width of the given instruction's result.
    ///
    /// ## Safety
    ///
    /// There is code that makes memory-safety assumptions that the result is
    /// always one of 1, 8, 16, 32, 64, or 128. Implementors must uphold this.
    fn instruction_result_bit_width(
        &self,
        context: &mut Self::Context,
        inst: Self::Instruction,
    ) -> u8;

    /// Get the size of a native word in bits.
    fn native_word_size_in_bits(&self, context: &mut Self::Context) -> u8;
}