op.rs 7.57 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

    Move
qinsoon's avatar
qinsoon committed
69 70
}

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

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

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

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

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

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

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

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
impl CmpOp {
    pub fn swap_operands(self) -> CmpOp {
        use op::CmpOp::*;
        match self {
            EQ => EQ,
            NE => NE,
            SGE => SLT,
            SGT => SLE,
            SLE => SGT,
            SLT => SGE,
            UGE => ULT,
            UGT => ULE,
            ULE => UGT,
            ULT => UGE,

            _ => unimplemented!()
        }
    }
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
#[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
223
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
224 225 226 227 228 229 230 231 232 233 234 235
pub enum AtomicRMWOp {
    XCHG,
    ADD,
    SUB,
    AND,
    NAND,
    OR,
    XOR,
    MAX,
    MIN,
    UMAX,
    UMIN
236 237
}

qinsoon's avatar
qinsoon committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
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
    }
}

254 255
pub fn pick_op_code_for_inst(inst: &Instruction) -> OpCode {
    match inst.v {
256
        Instruction_::BinOp(op, _, _)               => OpCode::Binary(op),
qinsoon's avatar
qinsoon committed
257
        Instruction_::BinOpWithStatus(op, _, _, _)  => OpCode::BinaryWithStatus(op),
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        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,
295
        Instruction_::CommonInst_SetThreadLocal(_)  => OpCode::CommonInst_SetThreadLocal,
qinsoon's avatar
qinsoon committed
296 297
        Instruction_::CommonInst_Pin(_)             => OpCode::CommonInst_Pin,
        Instruction_::CommonInst_Unpin(_)           => OpCode::CommonInst_Unpin,
298
        Instruction_::Move(_)                    => OpCode::Move,
299 300
    }
}