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
//! Parts of instructions.

use crate::cc::ConditionCode;
use crate::r#type::BitWidth;
use std::fmt::Debug;

/// A constant value.
///
/// Whether an integer is interpreted as signed or unsigned depends on the
/// operations applied to it.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Constant {
    /// A boolean of the given width.
    Bool(bool, BitWidth),

    /// An integer constant of the given width,
    Int(u64, BitWidth),
}

/// A part of an instruction, or a whole instruction itself.
///
/// These are the different values that can be matched in an optimization's
/// left-hand side and then built up in its right-hand side.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Part<I>
where
    I: Copy + Debug + Eq,
{
    /// An instruction (or result of an instruction).
    Instruction(I),

    /// A constant value.
    Constant(Constant),

    /// A condition code.
    ConditionCode(ConditionCode),
}

impl<I> From<Constant> for Part<I>
where
    I: Copy + Debug + Eq,
{
    #[inline]
    fn from(c: Constant) -> Part<I> {
        Part::Constant(c)
    }
}

impl<I> From<ConditionCode> for Part<I>
where
    I: Copy + Debug + Eq,
{
    #[inline]
    fn from(c: ConditionCode) -> Part<I> {
        Part::ConditionCode(c)
    }
}

macro_rules! accessors {
    ( $( $variant:ident , $result:ty , $getter:ident , $is:ident , $unwrap:ident ; )* ) => {
        $(
            #[inline]
            #[allow(missing_docs)]
            pub fn $getter(&self) -> Option<$result> {
                match *self {
                    Self::$variant(x, ..) => Some(x),
                    _ => None
                }
            }

            #[inline]
            #[allow(missing_docs)]
            pub fn $is(&self) -> bool {
                self.$getter().is_some()
            }

            #[inline]
            #[allow(missing_docs)]
            pub fn $unwrap(&self) -> $result {
                self.$getter().expect(concat!("failed to unwrap `", stringify!($variant), "`"))
            }
        )*
    }
}

impl Constant {
    /// If this is any kind of integer constant, get it as a 64-bit unsigned
    /// integer.
    pub fn as_int(&self) -> Option<u64> {
        match *self {
            Constant::Bool(..) => None,
            Constant::Int(x, _) => Some(x),
        }
    }

    /// If this is any kind of boolean constant, get its value.
    pub fn as_bool(&self) -> Option<bool> {
        match *self {
            Constant::Bool(b, _) => Some(b),
            Constant::Int(..) => None,
        }
    }

    /// The number of bits required to represent this constant value's type.
    pub fn bit_width(&self, root_width: u8) -> u8 {
        match *self {
            Constant::Bool(_, w) | Constant::Int(_, w) => {
                if let Some(w) = w.fixed_width() {
                    w
                } else {
                    debug_assert!(w.is_polymorphic());
                    root_width
                }
            }
        }
    }
}

impl<I> Part<I>
where
    I: Copy + Debug + Eq,
{
    accessors! {
        Instruction, I, as_instruction, is_instruction, unwrap_instruction;
        Constant, Constant, as_constant, is_constant, unwrap_constant;
        ConditionCode, ConditionCode, as_condition_code, is_condition_code, unwrap_condition_code;
    }
}