op.rs 6.2 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 8 9 10
pub enum OpCode {
    // SSA
    RegI64,
    RegFP,
11

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

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

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

qinsoon's avatar
qinsoon committed
35
    // expression
36 37
    Binary(BinOp),
    Comparison(CmpOp),
38
    Conversion(ConvOp),
39 40
    AtomicRMW(AtomicRMWOp),

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

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

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

qinsoon's avatar
qinsoon committed
116
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
117 118 119 120 121 122 123 124
pub enum BinOp {
    // Int(n) BinOp Int(n) -> Int(n)
    Add,
    Sub,
    Mul,
    Sdiv,
    Srem,
    Udiv,
125
    Urem,
qinsoon's avatar
qinsoon committed
126 127 128
    And,
    Or,
    Xor,
129

qinsoon's avatar
qinsoon committed
130 131 132
    // Int(n) BinOp Int(m) -> Int(n)
    Shl,
    Lshr,
133
    Ashr,
qinsoon's avatar
qinsoon committed
134 135

    // FP BinOp FP -> FP
136
    FAdd,
qinsoon's avatar
qinsoon committed
137 138 139 140 141 142
    FSub,
    FMul,
    FDiv,
    FRem
}

qinsoon's avatar
qinsoon committed
143
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
144 145 146 147 148 149 150 151 152 153 154 155
pub enum CmpOp {
    // for Int comparison
    EQ,
    NE,
    SGE,
    SGT,
    SLE,
    SLT,
    UGE,
    UGT,
    ULE,
    ULT,
156

qinsoon's avatar
qinsoon committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    // for FP comparison
    FFALSE,
    FTRUE,
    FOEQ,
    FOGT,
    FOGE,
    FOLT,
    FOLE,
    FONE,
    FORD,
    FUEQ,
    FUGT,
    FUGE,
    FULT,
    FULE,
    FUNE,
    FUNO
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
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!()
        }
    }
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
#[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
212
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
213 214 215 216 217 218 219 220 221 222 223 224
pub enum AtomicRMWOp {
    XCHG,
    ADD,
    SUB,
    AND,
    NAND,
    OR,
    XOR,
    MAX,
    MIN,
    UMAX,
    UMIN
225 226
}

qinsoon's avatar
qinsoon committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
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
    }
}

243 244 245 246
pub fn pick_op_code_for_inst(inst: &Instruction) -> OpCode {
    match inst.v {
        Instruction_::BinOp(op, _, _) => OpCode::Binary(op),
        Instruction_::CmpOp(op, _, _) => OpCode::Comparison(op),
qinsoon's avatar
qinsoon committed
247
        Instruction_::ConvOp{operation, ..} => OpCode::Conversion(operation),
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        Instruction_::AtomicRMW{op, ..} => OpCode::AtomicRMW(op),
        Instruction_::ExprCall{..} => OpCode::ExprCall,
        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_::Watchpoint{..} => OpCode::Watchpoint,
        Instruction_::WPBranch{..} => OpCode::WPBranch,
        Instruction_::Call{..} => OpCode::Call,
qinsoon's avatar
qinsoon committed
276
        Instruction_::CCall{..} => OpCode::CCall,
277 278 279 280 281
        Instruction_::SwapStack{..} => OpCode::SwapStack,
        Instruction_::Switch{..} => OpCode::Switch,
        Instruction_::ExnInstruction{..} => OpCode::ExnInstruction
    }
}