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
//! Condition codes.

use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fmt;

/// A condition code.
///
/// This is a special kind of immediate for `icmp` instructions that dictate
/// which parts of the comparison result we care about.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u32)]
pub enum ConditionCode {
    /// Equal.
    // NB: We convert `ConditionCode` into `NonZeroU32`s with unchecked
    // conversions; memory safety relies on no variant being zero.
    Eq = 1,

    /// Not equal.
    Ne,

    /// Signed less than.
    Slt,

    /// Unsigned less than.
    Ult,

    /// Signed greater than or equal.
    Sge,

    /// Unsigned greater than or equal.
    Uge,

    /// Signed greater than.
    Sgt,

    /// Unsigned greater than.
    Ugt,

    /// Signed less than or equal.
    Sle,

    /// Unsigned less than or equal.
    Ule,

    /// Overflow.
    Of,

    /// No overflow.
    Nof,
}

impl TryFrom<u32> for ConditionCode {
    type Error = &'static str;

    fn try_from(x: u32) -> Result<Self, Self::Error> {
        Ok(match x {
            x if Self::Eq as u32 == x => Self::Eq,
            x if Self::Ne as u32 == x => Self::Ne,
            x if Self::Slt as u32 == x => Self::Slt,
            x if Self::Ult as u32 == x => Self::Ult,
            x if Self::Sge as u32 == x => Self::Sge,
            x if Self::Uge as u32 == x => Self::Uge,
            x if Self::Sgt as u32 == x => Self::Sgt,
            x if Self::Ugt as u32 == x => Self::Ugt,
            x if Self::Sle as u32 == x => Self::Sle,
            x if Self::Ule as u32 == x => Self::Ule,
            x if Self::Of as u32 == x => Self::Of,
            x if Self::Nof as u32 == x => Self::Nof,
            _ => return Err("not a valid condition code value"),
        })
    }
}

impl fmt::Display for ConditionCode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Eq => write!(f, "eq"),
            Self::Ne => write!(f, "ne"),
            Self::Slt => write!(f, "slt"),
            Self::Ult => write!(f, "ult"),
            Self::Sge => write!(f, "sge"),
            Self::Uge => write!(f, "uge"),
            Self::Sgt => write!(f, "sgt"),
            Self::Ugt => write!(f, "ugt"),
            Self::Sle => write!(f, "sle"),
            Self::Ule => write!(f, "ule"),
            Self::Of => write!(f, "of"),
            Self::Nof => write!(f, "nof"),
        }
    }
}