op.rs 4.51 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
// Copyright 2017 The Australian National University
2
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
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
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
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)]
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
16
pub enum BinOp {
qinsoon's avatar
qinsoon committed
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

qinsoon's avatar
qinsoon committed
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

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

42
43
44
45
impl BinOp {
    pub fn is_fp(self) -> bool {
        use op::BinOp::*;
        match self {
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
46
            FAdd | FSub | FMul | FDiv | FRem => true,
47
48
49
50
            _ => false
        }
    }
}
51
#[derive(Copy, Clone, Debug, PartialEq)]
qinsoon's avatar
qinsoon committed
52
53
54
55
56
57
58
59
60
61
62
63
pub enum CmpOp {
    // for Int comparison
    EQ,
    NE,
    SGE,
    SGT,
    SLE,
    SLT,
    UGE,
    UGT,
    ULE,
    ULT,
64

qinsoon's avatar
qinsoon committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    // for FP comparison
    FFALSE,
    FTRUE,
    FOEQ,
    FOGT,
    FOGE,
    FOLT,
    FOLE,
    FONE,
    FORD,
    FUEQ,
    FUGT,
    FUGE,
    FULT,
    FULE,
    FUNE,
81
    FUNO
qinsoon's avatar
qinsoon committed
82
83
}

84
impl CmpOp {
qinsoon's avatar
qinsoon committed
85
    /// returns the CmpOp X for CmpOp Y, such that (a Y b) is equivalent to (b X a)
86
87
88
    pub fn swap_operands(self) -> CmpOp {
        use op::CmpOp::*;
        match self {
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
            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,

109
            _ => self // all other comparisons are symmetric
110
111
        }
    }
qinsoon's avatar
qinsoon committed
112
113

    /// returns the CmpOp X for CmpOp Y, such that (a Y b) is equivalent to NOT(a X b)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    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,

135
            SGE => SLT,
136
137
138
139
140
            SLT => SGE,

            FOGE => FULT,
            FULT => FOGE,

141
142
            SGT => SLE,
            SLE => SGT,
143
144
145
146

            FOGT => FULE,
            FULE => FOGT,

147
148
149
            UGE => ULT,
            ULT => UGE,

150
151
152
153
            FUEQ => FONE,
            FONE => FUEQ,

            FFALSE => FTRUE,
154
            FTRUE => FFALSE
155
156
        }
    }
qinsoon's avatar
qinsoon committed
157

158
159
160
161
162
163
164
165
    // 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,
166
            _ => self
167
168
        }
    }
169

170
171
172
173
    pub fn is_signed(self) -> bool {
        use op::CmpOp::*;
        match self {
            SGE | SLT | SGT | SLE => true,
174
            _ => false
175
176
        }
    }
qinsoon's avatar
qinsoon committed
177
178
179
180

    pub fn is_int_cmp(self) -> bool {
        use op::CmpOp::*;
        match self {
qinsoon's avatar
qinsoon committed
181
            EQ | NE | SGE | SGT | SLE | SLT | UGE | UGT | ULE | ULT => true,
182
            _ => false
qinsoon's avatar
qinsoon committed
183
184
        }
    }
185

Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
186
187
188
    pub fn is_fp_cmp(self) -> bool {
        !self.is_int_cmp()
    }
189
190
191
    pub fn is_eq_cmp(self) -> bool {
        use op::CmpOp::*;
        match self {
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
192
            EQ | NE => true,
193
194
195
196
197
198
            _ => false
        }
    }
    pub fn is_ult_cmp(self) -> bool {
        use op::CmpOp::*;
        match self {
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
199
200
            UGE | UGT | ULE | ULT => true,
            _ => false
201
202
203
        }
    }

204
205
206
    pub fn is_symmetric(self) -> bool {
        use op::CmpOp::*;
        match self {
qinsoon's avatar
qinsoon committed
207
            EQ | NE | FORD | FUNO | FUNE | FUEQ | FONE | FOEQ => true,
208
            _ => false
209
210
        }
    }
211
212
}

213
#[derive(Copy, Clone, Debug, PartialEq)]
214
215
216
217
218
219
220
221
222
223
224
225
pub enum ConvOp {
    TRUNC,
    ZEXT,
    SEXT,
    FPTRUNC,
    FPEXT,
    FPTOUI,
    FPTOSI,
    UITOFP,
    SITOFP,
    BITCAST,
    REFCAST,
226
    PTRCAST
227
228
}

229
#[derive(Copy, Clone, Debug, PartialEq)]
qinsoon's avatar
qinsoon committed
230
231
232
233
234
235
236
237
238
239
240
pub enum AtomicRMWOp {
    XCHG,
    ADD,
    SUB,
    AND,
    NAND,
    OR,
    XOR,
    MAX,
    MIN,
    UMAX,
241
    UMIN
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
242
}