To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

op.rs 9.29 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
2
3
4
use ptr::P;
use types::*;
use inst::*;
use types::MuType_::*;
5

qinsoon's avatar
qinsoon committed
6
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
7
#[allow(non_camel_case_types)]
qinsoon's avatar
qinsoon committed
8
9
10
11
pub enum OpCode {
    // SSA
    RegI64,
    RegFP,
12

qinsoon's avatar
qinsoon committed
13
14
15
    // Constant
    IntImmI64,
    FPImm,
16

qinsoon's avatar
qinsoon committed
17
18
19
    // non-terminal
    Assign,
    Fence,
20

qinsoon's avatar
qinsoon committed
21
22
23
24
25
26
27
    //terminal
    Return,
    ThreadExit,
    Throw,
    TailCall,
    Branch1,
    Branch2,
qinsoon's avatar
qinsoon committed
28
    Select,
qinsoon's avatar
qinsoon committed
29
30
31
    Watchpoint,
    WPBranch,
    Call,
qinsoon's avatar
qinsoon committed
32
    CCall,
qinsoon's avatar
qinsoon committed
33
34
35
    SwapStack,
    Switch,
    ExnInstruction,
36

qinsoon's avatar
qinsoon committed
37
    // expression
38
    Binary(BinOp),
qinsoon's avatar
qinsoon committed
39
    BinaryWithStatus(BinOp),
40
    Comparison(CmpOp),
41
    Conversion(ConvOp),
42
43
    AtomicRMW(AtomicRMWOp),

qinsoon's avatar
qinsoon committed
44
    ExprCall,
qinsoon's avatar
qinsoon committed
45
    ExprCCall,
qinsoon's avatar
qinsoon committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    Load,
    Store,
    CmpXchg,
    New,
    AllocA,
    NewHybrid,
    AllocAHybrid,
    NewStack,
    NewThread,
    NewThreadExn,
    NewFrameCursor,
    GetIRef,
    GetFieldIRef,
    GetElementIRef,
    ShiftIRef,
61
62
63
    GetVarPartIRef,

    CommonInst_GetThreadLocal,
64
    CommonInst_SetThreadLocal,
qinsoon's avatar
qinsoon committed
65
66
    CommonInst_Pin,
    CommonInst_Unpin,
67

68
69
    Move,
    PrintHex
qinsoon's avatar
qinsoon committed
70
71
}

72
pub fn pick_op_code_for_ssa(ty: &P<MuType>) -> OpCode {
qinsoon's avatar
qinsoon committed
73
74
    let a : &MuType = ty;
    match a.v {
75
        // currently use i64 for all ints
qinsoon's avatar
qinsoon committed
76
        Int(_) => OpCode::RegI64,
77
        // currently do not differentiate float and double
qinsoon's avatar
qinsoon committed
78
79
        Float
        | Double => OpCode::RegFP,
80
        // ref and pointer types use RegI64
qinsoon's avatar
qinsoon committed
81
82
83
84
85
86
87
88
89
        Ref(_)
        | IRef(_)
        | WeakRef(_)
        | UPtr(_)
        | ThreadRef
        | StackRef
        | Tagref64
        | FuncRef(_)
        | UFuncPtr(_) => OpCode::RegI64,
90
        // we are not supposed to have these as SSA
qinsoon's avatar
qinsoon committed
91
92
        Struct(_)
        | Array(_, _)
93
        | Hybrid(_)
qinsoon's avatar
qinsoon committed
94
        | Void => panic!("Not expecting {} as SSA", ty),
95
        // unimplemented
qinsoon's avatar
qinsoon committed
96
        Vector(_, _) => unimplemented!()
97
98
99
    }
}

qinsoon's avatar
qinsoon committed
100
pub fn pick_op_code_for_value(ty: &P<MuType>) -> OpCode {
qinsoon's avatar
qinsoon committed
101
102
    let a : &MuType = ty;
    match a.v {
103
        // currently use i64 for all ints
qinsoon's avatar
qinsoon committed
104
        Int(_) => OpCode::IntImmI64,
105
        // currently do not differentiate float and double
qinsoon's avatar
qinsoon committed
106
107
        Float
        | Double => OpCode::FPImm,
108
        // ref and pointer types use RegI64
qinsoon's avatar
qinsoon committed
109
110
111
112
113
114
115
116
117
        Ref(_)
        | IRef(_)
        | WeakRef(_)
        | UPtr(_)
        | ThreadRef
        | StackRef
        | Tagref64
        | FuncRef(_)
        | UFuncPtr(_) => OpCode::IntImmI64,
118
        // we are not supposed to have these as SSA
qinsoon's avatar
qinsoon committed
119
120
        Struct(_)
        | Array(_, _)
121
        | Hybrid(_)
qinsoon's avatar
qinsoon committed
122
        | Void => unimplemented!(),
123
        // unimplemented
qinsoon's avatar
qinsoon committed
124
        Vector(_, _) => unimplemented!()
125
126
127
    }
}

qinsoon's avatar
qinsoon committed
128
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
129
130
131
132
133
134
135
136
pub enum BinOp {
    // Int(n) BinOp Int(n) -> Int(n)
    Add,
    Sub,
    Mul,
    Sdiv,
    Srem,
    Udiv,
137
    Urem,
qinsoon's avatar
qinsoon committed
138
139
140
    And,
    Or,
    Xor,
141

qinsoon's avatar
qinsoon committed
142
143
144
    // Int(n) BinOp Int(m) -> Int(n)
    Shl,
    Lshr,
145
    Ashr,
qinsoon's avatar
qinsoon committed
146
147

    // FP BinOp FP -> FP
148
    FAdd,
qinsoon's avatar
qinsoon committed
149
150
151
152
153
154
    FSub,
    FMul,
    FDiv,
    FRem
}

qinsoon's avatar
qinsoon committed
155
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
156
157
158
159
160
161
162
163
164
165
166
167
pub enum CmpOp {
    // for Int comparison
    EQ,
    NE,
    SGE,
    SGT,
    SLE,
    SLT,
    UGE,
    UGT,
    ULE,
    ULT,
168

qinsoon's avatar
qinsoon committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    // for FP comparison
    FFALSE,
    FTRUE,
    FOEQ,
    FOGT,
    FOGE,
    FOLT,
    FOLE,
    FONE,
    FORD,
    FUEQ,
    FUGT,
    FUGE,
    FULT,
    FULE,
    FUNE,
    FUNO
}

188
impl CmpOp {
189
    // Returns the CmpOp c, such that (a self b) is equivelent to (b c a)
190
191
192
    pub fn swap_operands(self) -> CmpOp {
        use op::CmpOp::*;
        match self {
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
            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,

213
            _ => self, // all other comparisons are symmetric
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        }
    }
    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,

237
            SGE => SLT,
238
239
240
241
242
            SLT => SGE,

            FOGE => FULT,
            FULT => FOGE,

243
244
            SGT => SLE,
            SLE => SGT,
245
246
247
248

            FOGT => FULE,
            FULE => FOGT,

249
250
251
            UGE => ULT,
            ULT => UGE,

252
253
254
255
256
257
258
            FUEQ => FONE,
            FONE => FUEQ,

            FFALSE => FTRUE,
            FTRUE => FFALSE,
        }
    }
259
260
261
262
263
264
265
266
267
268
269
270

    // 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,
            _   => self,
        }
    }
271
272
273
274
275
    pub fn is_signed(self) -> bool {
        use op::CmpOp::*;
        match self {
            SGE | SLT | SGT | SLE => true,
            _ => false
276
277
        }
    }
278
279
280
281
282
283
284
285

    pub fn is_symmetric(self) -> bool {
        use op::CmpOp::*;
        match self {
            EQ | NE | FORD| FUNO| FUNE | FUEQ | FONE | FOEQ => true,
            _ => false
        }
    }
286
287
}

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
pub enum ConvOp {
    TRUNC,
    ZEXT,
    SEXT,
    FPTRUNC,
    FPEXT,
    FPTOUI,
    FPTOSI,
    UITOFP,
    SITOFP,
    BITCAST,
    REFCAST,
    PTRCAST
}

qinsoon's avatar
qinsoon committed
304
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
305
306
307
308
309
310
311
312
313
314
315
316
pub enum AtomicRMWOp {
    XCHG,
    ADD,
    SUB,
    AND,
    NAND,
    OR,
    XOR,
    MAX,
    MIN,
    UMAX,
    UMIN
317
318
}

qinsoon's avatar
qinsoon committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
pub fn is_int_cmp(op: CmpOp) -> bool {
    match op {
        CmpOp::EQ
        | CmpOp::NE
        | CmpOp::SGE
        | CmpOp::SGT
        | CmpOp::SLE
        | CmpOp::SLT
        | CmpOp::UGE
        | CmpOp::UGT
        | CmpOp::ULE
        | CmpOp::ULT => true,
        _ => false
    }
}

335
336
pub fn pick_op_code_for_inst(inst: &Instruction) -> OpCode {
    match inst.v {
337
        Instruction_::BinOp(op, _, _)               => OpCode::Binary(op),
qinsoon's avatar
qinsoon committed
338
        Instruction_::BinOpWithStatus(op, _, _, _)  => OpCode::BinaryWithStatus(op),
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
        Instruction_::CmpOp(op, _, _)               => OpCode::Comparison(op),
        Instruction_::ConvOp{operation, ..}         => OpCode::Conversion(operation),
        Instruction_::AtomicRMW{op, ..}             => OpCode::AtomicRMW(op),
        Instruction_::ExprCall{..}                  => OpCode::ExprCall,
        Instruction_::ExprCCall{..}                 => OpCode::ExprCCall,
        Instruction_::Load{..}                      => OpCode::Load,
        Instruction_::Store{..}                     => OpCode::Store,
        Instruction_::CmpXchg{..}                   => OpCode::CmpXchg,
        Instruction_::New(_)                        => OpCode::New,
        Instruction_::AllocA(_)                     => OpCode::AllocA,
        Instruction_::NewHybrid(_, _)               => OpCode::NewHybrid,
        Instruction_::AllocAHybrid(_, _)            => OpCode::AllocAHybrid,
        Instruction_::NewStack(_)                   => OpCode::NewStack,
        Instruction_::NewThread(_, _)               => OpCode::NewThread,
        Instruction_::NewThreadExn(_, _)            => OpCode::NewThreadExn,
        Instruction_::NewFrameCursor(_)             => OpCode::NewFrameCursor,
        Instruction_::GetIRef(_)                    => OpCode::GetIRef,
        Instruction_::GetFieldIRef{..}              => OpCode::GetFieldIRef,
        Instruction_::GetElementIRef{..}            => OpCode::GetElementIRef,
        Instruction_::ShiftIRef{..}                 => OpCode::ShiftIRef,
        Instruction_::GetVarPartIRef{..}            => OpCode::GetVarPartIRef,
        Instruction_::Fence(_)                      => OpCode::Fence,
        Instruction_::Return(_)                     => OpCode::Return,
        Instruction_::ThreadExit                    => OpCode::ThreadExit,
        Instruction_::Throw(_)                      => OpCode::Throw,
        Instruction_::TailCall(_)                   => OpCode::TailCall,
        Instruction_::Branch1(_)                    => OpCode::Branch1,
        Instruction_::Branch2{..}                   => OpCode::Branch2,
        Instruction_::Select{..}                    => OpCode::Select,
        Instruction_::Watchpoint{..}                => OpCode::Watchpoint,
        Instruction_::WPBranch{..}                  => OpCode::WPBranch,
        Instruction_::Call{..}                      => OpCode::Call,
        Instruction_::CCall{..}                     => OpCode::CCall,
        Instruction_::SwapStack{..}                 => OpCode::SwapStack,
        Instruction_::Switch{..}                    => OpCode::Switch,
        Instruction_::ExnInstruction{..}            => OpCode::ExnInstruction,
        Instruction_::CommonInst_GetThreadLocal     => OpCode::CommonInst_GetThreadLocal,
376
        Instruction_::CommonInst_SetThreadLocal(_)  => OpCode::CommonInst_SetThreadLocal,
qinsoon's avatar
qinsoon committed
377
378
        Instruction_::CommonInst_Pin(_)             => OpCode::CommonInst_Pin,
        Instruction_::CommonInst_Unpin(_)           => OpCode::CommonInst_Unpin,
379
        Instruction_::Move(_)                    => OpCode::Move,
380
        Instruction_::PrintHex(_)                => OpCode::PrintHex,
381
382
    }
}