op.rs 3.96 KB
Newer Older
1
// Copyright 2017 The Australian National University
2
//
3 4 5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9 10 11 12 13 14
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
#[derive(Copy, Clone, Debug, PartialEq)]
qinsoon's avatar
qinsoon committed
16
pub enum BinOp {
17
    // BinOp Int(n) Int(n) -> Int(n)
qinsoon's avatar
qinsoon committed
18 19 20 21 22 23
    Add,
    Sub,
    Mul,
    Sdiv,
    Srem,
    Udiv,
24
    Urem,
qinsoon's avatar
qinsoon committed
25 26 27
    And,
    Or,
    Xor,
28

29
    // BinOp Int(n) Int(m) -> Int(n)
qinsoon's avatar
qinsoon committed
30 31
    Shl,
    Lshr,
32
    Ashr,
qinsoon's avatar
qinsoon committed
33

34
    // BinOp FP FP -> FP
35
    FAdd,
qinsoon's avatar
qinsoon committed
36 37 38
    FSub,
    FMul,
    FDiv,
qinsoon's avatar
qinsoon committed
39
    FRem,
qinsoon's avatar
qinsoon committed
40 41
}

42
#[derive(Copy, Clone, Debug, PartialEq)]
qinsoon's avatar
qinsoon committed
43 44 45 46 47 48 49 50 51 52 53 54
pub enum CmpOp {
    // for Int comparison
    EQ,
    NE,
    SGE,
    SGT,
    SLE,
    SLT,
    UGE,
    UGT,
    ULE,
    ULT,
55

qinsoon's avatar
qinsoon committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    // for FP comparison
    FFALSE,
    FTRUE,
    FOEQ,
    FOGT,
    FOGE,
    FOLT,
    FOLE,
    FONE,
    FORD,
    FUEQ,
    FUGT,
    FUGE,
    FULT,
    FULE,
    FUNE,
qinsoon's avatar
qinsoon committed
72
    FUNO,
qinsoon's avatar
qinsoon committed
73 74
}

75
impl CmpOp {
76
    /// returns the CmpOp X for CmpOp Y, such that (a Y b) is equivalent to (b X a)
77 78 79
    pub fn swap_operands(self) -> CmpOp {
        use op::CmpOp::*;
        match self {
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            SGE => SLE,
            SLE => SGE,
            SGT => SLT,
            SLT => SGT,

            UGE => ULE,
            ULE => UGE,
            UGT => ULT,
            ULT => UGT,

            FOGE => FOLE,
            FOLE => FOGE,
            FOGT => FOLT,
            FOLT => FOGT,

            FUGE => FULE,
            FULE => FUGE,
            FUGT => FULT,
            FULT => FUGT,

100
            _ => self, // all other comparisons are symmetric
101 102
        }
    }
103 104

    /// returns the CmpOp X for CmpOp Y, such that (a Y b) is equivalent to NOT(a X b)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    pub fn invert(self) -> CmpOp {
        use op::CmpOp::*;
        match self {
            EQ => NE,
            NE => EQ,

            FOEQ => FUNE,
            FUNE => FOEQ,

            FUGE => FOLT,
            FOLT => FUGE,

            FUNO => FORD,
            FORD => FUNO,

            UGT => ULE,
            ULE => UGT,

            FUGT => FOLE,
            FOLE => FUGT,

126
            SGE => SLT,
127 128 129 130 131
            SLT => SGE,

            FOGE => FULT,
            FULT => FOGE,

132 133
            SGT => SLE,
            SLE => SGT,
134 135 136 137

            FOGT => FULE,
            FULE => FOGT,

138 139 140
            UGE => ULT,
            ULT => UGE,

141 142 143 144 145 146 147
            FUEQ => FONE,
            FONE => FUEQ,

            FFALSE => FTRUE,
            FTRUE => FFALSE,
        }
    }
148

149 150 151 152 153 154 155 156
    // gets the unsigned version of the comparison
    pub fn get_unsigned(self) -> CmpOp {
        use op::CmpOp::*;
        match self {
            SGE => UGE,
            SLT => ULT,
            SGT => UGT,
            SLE => ULE,
qinsoon's avatar
qinsoon committed
157
            _ => self,
158 159
        }
    }
160

161 162 163 164
    pub fn is_signed(self) -> bool {
        use op::CmpOp::*;
        match self {
            SGE | SLT | SGT | SLE => true,
qinsoon's avatar
qinsoon committed
165
            _ => false,
166 167
        }
    }
168 169 170 171

    pub fn is_int_cmp(self) -> bool {
        use op::CmpOp::*;
        match self {
qinsoon's avatar
qinsoon committed
172 173
            EQ | NE | SGE | SGT | SLE | SLT | UGE | UGT | ULE | ULT => true,
            _ => false,
174 175
        }
    }
176

177 178 179
    pub fn is_symmetric(self) -> bool {
        use op::CmpOp::*;
        match self {
qinsoon's avatar
qinsoon committed
180 181
            EQ | NE | FORD | FUNO | FUNE | FUEQ | FONE | FOEQ => true,
            _ => false,
182 183
        }
    }
184 185
}

186
#[derive(Copy, Clone, Debug, PartialEq)]
187 188 189 190 191 192 193 194 195 196 197 198
pub enum ConvOp {
    TRUNC,
    ZEXT,
    SEXT,
    FPTRUNC,
    FPEXT,
    FPTOUI,
    FPTOSI,
    UITOFP,
    SITOFP,
    BITCAST,
    REFCAST,
qinsoon's avatar
qinsoon committed
199
    PTRCAST,
200 201
}

202
#[derive(Copy, Clone, Debug, PartialEq)]
qinsoon's avatar
qinsoon committed
203 204 205 206 207 208 209 210 211 212 213
pub enum AtomicRMWOp {
    XCHG,
    ADD,
    SUB,
    AND,
    NAND,
    OR,
    XOR,
    MAX,
    MIN,
    UMAX,
qinsoon's avatar
qinsoon committed
214 215
    UMIN,
}