Line data Source code
1 : // Copyright 2014 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/js-operator.h"
6 : #include "src/compiler/opcodes.h"
7 : #include "src/compiler/operator.h"
8 : #include "src/compiler/operator-properties.h"
9 : #include "test/unittests/test-utils.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 : 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 : IrOpcode::Value opcode;
24 : Operator::Properties properties;
25 : int value_input_count;
26 : int frame_state_input_count;
27 : int effect_input_count;
28 : int control_input_count;
29 : int value_output_count;
30 : int effect_output_count;
31 : int control_output_count;
32 : };
33 :
34 : const SharedOperator kSharedOperators[] = {
35 : #define SHARED(Name, properties, value_input_count, frame_state_input_count, \
36 : effect_input_count, control_input_count, value_output_count, \
37 : effect_output_count, control_output_count) \
38 : { \
39 : &JSOperatorBuilder::Name, IrOpcode::kJS##Name, properties, \
40 : value_input_count, frame_state_input_count, effect_input_count, \
41 : control_input_count, value_output_count, effect_output_count, \
42 : 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 : #undef SHARED
50 : };
51 :
52 :
53 : std::ostream& operator<<(std::ostream& os, const SharedOperator& sop) {
54 61760 : return os << IrOpcode::Mnemonic(sop.opcode);
55 : }
56 :
57 : } // namespace
58 :
59 :
60 80 : class JSSharedOperatorTest
61 : : public TestWithZone,
62 : public ::testing::WithParamInterface<SharedOperator> {};
63 :
64 :
65 18548 : TEST_P(JSSharedOperatorTest, InstancesAreGloballyShared) {
66 5 : const SharedOperator& sop = GetParam();
67 5 : JSOperatorBuilder javascript1(zone());
68 5 : JSOperatorBuilder javascript2(zone());
69 10 : EXPECT_EQ((javascript1.*sop.constructor)(), (javascript2.*sop.constructor)());
70 5 : }
71 :
72 :
73 18548 : TEST_P(JSSharedOperatorTest, NumberOfInputsAndOutputs) {
74 5 : JSOperatorBuilder javascript(zone());
75 5 : const SharedOperator& sop = GetParam();
76 5 : const Operator* op = (javascript.*sop.constructor)();
77 :
78 5 : const int context_input_count = 1;
79 10 : EXPECT_EQ(sop.value_input_count, op->ValueInputCount());
80 10 : EXPECT_EQ(context_input_count, OperatorProperties::GetContextInputCount(op));
81 10 : EXPECT_EQ(sop.frame_state_input_count,
82 0 : OperatorProperties::GetFrameStateInputCount(op));
83 10 : EXPECT_EQ(sop.effect_input_count, op->EffectInputCount());
84 10 : EXPECT_EQ(sop.control_input_count, op->ControlInputCount());
85 10 : EXPECT_EQ(sop.value_input_count + context_input_count +
86 : sop.frame_state_input_count + sop.effect_input_count +
87 : sop.control_input_count,
88 0 : OperatorProperties::GetTotalInputCount(op));
89 :
90 10 : EXPECT_EQ(sop.value_output_count, op->ValueOutputCount());
91 10 : EXPECT_EQ(sop.effect_output_count, op->EffectOutputCount());
92 10 : EXPECT_EQ(sop.control_output_count, op->ControlOutputCount());
93 5 : }
94 :
95 :
96 18548 : TEST_P(JSSharedOperatorTest, OpcodeIsCorrect) {
97 5 : JSOperatorBuilder javascript(zone());
98 5 : const SharedOperator& sop = GetParam();
99 5 : const Operator* op = (javascript.*sop.constructor)();
100 10 : EXPECT_EQ(sop.opcode, op->opcode());
101 5 : }
102 :
103 :
104 18548 : TEST_P(JSSharedOperatorTest, Properties) {
105 5 : JSOperatorBuilder javascript(zone());
106 5 : const SharedOperator& sop = GetParam();
107 5 : const Operator* op = (javascript.*sop.constructor)();
108 10 : EXPECT_EQ(sop.properties, op->properties());
109 5 : }
110 :
111 225424 : INSTANTIATE_TEST_SUITE_P(JSOperatorTest, JSSharedOperatorTest,
112 : ::testing::ValuesIn(kSharedOperators));
113 :
114 : } // namespace js_operator_unittest
115 : } // namespace compiler
116 : } // namespace internal
117 9264 : } // namespace v8
|