js-operator-unittest.cc 4.08 KB
Newer Older
1 2 3 4 5
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/js-operator.h"
6 7 8
#include "src/compiler/opcodes.h"
#include "src/compiler/operator.h"
#include "src/compiler/operator-properties.h"
9
#include "test/unittests/test-utils.h"
10 11 12 13

namespace v8 {
namespace internal {
namespace compiler {
14
namespace js_operator_unittest {
15

16
// -----------------------------------------------------------------------------
17
// Shared operators.
18 19

namespace {
20

21
struct SharedOperator {
22
  const Operator* (JSOperatorBuilder::*constructor)();
23 24 25 26 27 28 29 30
  IrOpcode::Value opcode;
  Operator::Properties properties;
  int value_input_count;
  int frame_state_input_count;
  int effect_input_count;
  int control_input_count;
  int value_output_count;
  int effect_output_count;
31
  int control_output_count;
32 33
};

34
const SharedOperator kSharedOperators[] = {
35 36
#define SHARED(Name, properties, value_input_count, frame_state_input_count, \
               effect_input_count, control_input_count, value_output_count,  \
37
               effect_output_count, control_output_count)                    \
38 39 40
  {                                                                          \
    &JSOperatorBuilder::Name, IrOpcode::kJS##Name, properties,               \
        value_input_count, frame_state_input_count, effect_input_count,      \
41 42
        control_input_count, value_output_count, effect_output_count,        \
        control_output_count                                                 \
43
  }
44
    SHARED(ToNumber, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
45
    SHARED(ToString, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
46
    SHARED(ToName, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
47
    SHARED(ToObject, Operator::kFoldable, 1, 1, 1, 1, 1, 1, 2),
48
    SHARED(Create, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
49 50 51 52
#undef SHARED
};


53
std::ostream& operator<<(std::ostream& os, const SharedOperator& sop) {
54 55 56 57 58
  return os << IrOpcode::Mnemonic(sop.opcode);
}

class JSSharedOperatorTest
    : public TestWithZone,
59
      public ::testing::WithParamInterface<SharedOperator> {};
60 61 62


TEST_P(JSSharedOperatorTest, InstancesAreGloballyShared) {
63
  const SharedOperator& sop = GetParam();
64 65 66 67 68 69 70 71
  JSOperatorBuilder javascript1(zone());
  JSOperatorBuilder javascript2(zone());
  EXPECT_EQ((javascript1.*sop.constructor)(), (javascript2.*sop.constructor)());
}


TEST_P(JSSharedOperatorTest, NumberOfInputsAndOutputs) {
  JSOperatorBuilder javascript(zone());
72
  const SharedOperator& sop = GetParam();
73
  const Operator* op = (javascript.*sop.constructor)();
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

  const int context_input_count = 1;
  EXPECT_EQ(sop.value_input_count, op->ValueInputCount());
  EXPECT_EQ(context_input_count, OperatorProperties::GetContextInputCount(op));
  EXPECT_EQ(sop.frame_state_input_count,
            OperatorProperties::GetFrameStateInputCount(op));
  EXPECT_EQ(sop.effect_input_count, op->EffectInputCount());
  EXPECT_EQ(sop.control_input_count, op->ControlInputCount());
  EXPECT_EQ(sop.value_input_count + context_input_count +
                sop.frame_state_input_count + sop.effect_input_count +
                sop.control_input_count,
            OperatorProperties::GetTotalInputCount(op));

  EXPECT_EQ(sop.value_output_count, op->ValueOutputCount());
  EXPECT_EQ(sop.effect_output_count, op->EffectOutputCount());
  EXPECT_EQ(sop.control_output_count, op->ControlOutputCount());
90 91 92
}


93 94
TEST_P(JSSharedOperatorTest, OpcodeIsCorrect) {
  JSOperatorBuilder javascript(zone());
95
  const SharedOperator& sop = GetParam();
96 97 98 99 100 101 102
  const Operator* op = (javascript.*sop.constructor)();
  EXPECT_EQ(sop.opcode, op->opcode());
}


TEST_P(JSSharedOperatorTest, Properties) {
  JSOperatorBuilder javascript(zone());
103
  const SharedOperator& sop = GetParam();
104 105 106 107
  const Operator* op = (javascript.*sop.constructor)();
  EXPECT_EQ(sop.properties, op->properties());
}

108 109
INSTANTIATE_TEST_SUITE_P(JSOperatorTest, JSSharedOperatorTest,
                         ::testing::ValuesIn(kSharedOperators));
110

111
}  // namespace
112
}  // namespace js_operator_unittest
113 114 115
}  // namespace compiler
}  // namespace internal
}  // namespace v8