Home | History | Annotate | Download | only in compiler
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "src/compiler/opcodes.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 namespace v8 {
      9 namespace internal {
     10 namespace compiler {
     11 
     12 namespace {
     13 
     14 bool IsCommonOpcode(IrOpcode::Value opcode) {
     15   switch (opcode) {
     16 #define OPCODE(Opcode)      \
     17   case IrOpcode::k##Opcode: \
     18     return true;
     19     COMMON_OP_LIST(OPCODE)
     20     CONTROL_OP_LIST(OPCODE)
     21 #undef OPCODE
     22     default:
     23       return false;
     24   }
     25 }
     26 
     27 
     28 bool IsControlOpcode(IrOpcode::Value opcode) {
     29   switch (opcode) {
     30 #define OPCODE(Opcode)      \
     31   case IrOpcode::k##Opcode: \
     32     return true;
     33     CONTROL_OP_LIST(OPCODE)
     34 #undef OPCODE
     35     default:
     36       return false;
     37   }
     38 }
     39 
     40 
     41 bool IsJsOpcode(IrOpcode::Value opcode) {
     42   switch (opcode) {
     43 #define OPCODE(Opcode)      \
     44   case IrOpcode::k##Opcode: \
     45     return true;
     46     JS_OP_LIST(OPCODE)
     47 #undef OPCODE
     48     default:
     49       return false;
     50   }
     51 }
     52 
     53 
     54 bool IsConstantOpcode(IrOpcode::Value opcode) {
     55   switch (opcode) {
     56 #define OPCODE(Opcode)      \
     57   case IrOpcode::k##Opcode: \
     58     return true;
     59     CONSTANT_OP_LIST(OPCODE)
     60 #undef OPCODE
     61     default:
     62       return false;
     63   }
     64 }
     65 
     66 
     67 bool IsComparisonOpcode(IrOpcode::Value opcode) {
     68   switch (opcode) {
     69 #define OPCODE(Opcode)      \
     70   case IrOpcode::k##Opcode: \
     71     return true;
     72     JS_COMPARE_BINOP_LIST(OPCODE)
     73     SIMPLIFIED_COMPARE_BINOP_LIST(OPCODE)
     74     MACHINE_COMPARE_BINOP_LIST(OPCODE)
     75 #undef OPCODE
     76     default:
     77       return false;
     78   }
     79 }
     80 
     81 
     82 const IrOpcode::Value kInvalidOpcode = static_cast<IrOpcode::Value>(123456789);
     83 
     84 }  // namespace
     85 
     86 
     87 TEST(IrOpcodeTest, IsCommonOpcode) {
     88   EXPECT_FALSE(IrOpcode::IsCommonOpcode(kInvalidOpcode));
     89 #define OPCODE(Opcode)                           \
     90   EXPECT_EQ(IsCommonOpcode(IrOpcode::k##Opcode), \
     91             IrOpcode::IsCommonOpcode(IrOpcode::k##Opcode));
     92   ALL_OP_LIST(OPCODE)
     93 #undef OPCODE
     94 }
     95 
     96 
     97 TEST(IrOpcodeTest, IsControlOpcode) {
     98   EXPECT_FALSE(IrOpcode::IsControlOpcode(kInvalidOpcode));
     99 #define OPCODE(Opcode)                            \
    100   EXPECT_EQ(IsControlOpcode(IrOpcode::k##Opcode), \
    101             IrOpcode::IsControlOpcode(IrOpcode::k##Opcode));
    102   ALL_OP_LIST(OPCODE)
    103 #undef OPCODE
    104 }
    105 
    106 
    107 TEST(IrOpcodeTest, IsJsOpcode) {
    108   EXPECT_FALSE(IrOpcode::IsJsOpcode(kInvalidOpcode));
    109 #define OPCODE(Opcode)                       \
    110   EXPECT_EQ(IsJsOpcode(IrOpcode::k##Opcode), \
    111             IrOpcode::IsJsOpcode(IrOpcode::k##Opcode));
    112   ALL_OP_LIST(OPCODE)
    113 #undef OPCODE
    114 }
    115 
    116 
    117 TEST(IrOpcodeTest, IsConstantOpcode) {
    118   EXPECT_FALSE(IrOpcode::IsConstantOpcode(kInvalidOpcode));
    119 #define OPCODE(Opcode)                             \
    120   EXPECT_EQ(IsConstantOpcode(IrOpcode::k##Opcode), \
    121             IrOpcode::IsConstantOpcode(IrOpcode::k##Opcode));
    122   ALL_OP_LIST(OPCODE)
    123 #undef OPCODE
    124 }
    125 
    126 
    127 TEST(IrOpcodeTest, IsComparisonOpcode) {
    128   EXPECT_FALSE(IrOpcode::IsComparisonOpcode(kInvalidOpcode));
    129 #define OPCODE(Opcode)                               \
    130   EXPECT_EQ(IsComparisonOpcode(IrOpcode::k##Opcode), \
    131             IrOpcode::IsComparisonOpcode(IrOpcode::k##Opcode));
    132   ALL_OP_LIST(OPCODE)
    133 #undef OPCODE
    134 }
    135 
    136 
    137 TEST(IrOpcodeTest, Mnemonic) {
    138   EXPECT_STREQ("UnknownOpcode", IrOpcode::Mnemonic(kInvalidOpcode));
    139 #define OPCODE(Opcode) \
    140   EXPECT_STREQ(#Opcode, IrOpcode::Mnemonic(IrOpcode::k##Opcode));
    141   ALL_OP_LIST(OPCODE)
    142 #undef OPCODE
    143 }
    144 
    145 }  // namespace compiler
    146 }  // namespace internal
    147 }  // namespace v8
    148