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 15 // ----------------------------------------------------------------------------- 16 // Shared operators. 17 18 namespace { 19 20 struct SharedOperator { 21 const Operator* (JSOperatorBuilder::*constructor)(); 22 IrOpcode::Value opcode; 23 Operator::Properties properties; 24 int value_input_count; 25 int frame_state_input_count; 26 int effect_input_count; 27 int control_input_count; 28 int value_output_count; 29 int effect_output_count; 30 int control_output_count; 31 }; 32 33 const SharedOperator kSharedOperators[] = { 34 #define SHARED(Name, properties, value_input_count, frame_state_input_count, \ 35 effect_input_count, control_input_count, value_output_count, \ 36 effect_output_count, control_output_count) \ 37 { \ 38 &JSOperatorBuilder::Name, IrOpcode::kJS##Name, properties, \ 39 value_input_count, frame_state_input_count, effect_input_count, \ 40 control_input_count, value_output_count, effect_output_count, \ 41 control_output_count \ 42 } 43 SHARED(ToNumber, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2), 44 SHARED(ToString, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2), 45 SHARED(ToName, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2), 46 SHARED(ToObject, Operator::kFoldable, 1, 1, 1, 1, 1, 1, 2), 47 SHARED(Create, Operator::kEliminatable, 2, 1, 1, 0, 1, 1, 0), 48 SHARED(TypeOf, Operator::kPure, 1, 0, 0, 0, 1, 0, 0), 49 SHARED(CreateWithContext, Operator::kNoProperties, 2, 0, 1, 1, 1, 1, 2), 50 SHARED(CreateModuleContext, Operator::kNoProperties, 2, 0, 1, 1, 1, 1, 2), 51 #undef SHARED 52 }; 53 54 55 std::ostream& operator<<(std::ostream& os, const SharedOperator& sop) { 56 return os << IrOpcode::Mnemonic(sop.opcode); 57 } 58 59 } // namespace 60 61 62 class JSSharedOperatorTest 63 : public TestWithZone, 64 public ::testing::WithParamInterface<SharedOperator> {}; 65 66 67 TEST_P(JSSharedOperatorTest, InstancesAreGloballyShared) { 68 const SharedOperator& sop = GetParam(); 69 JSOperatorBuilder javascript1(zone()); 70 JSOperatorBuilder javascript2(zone()); 71 EXPECT_EQ((javascript1.*sop.constructor)(), (javascript2.*sop.constructor)()); 72 } 73 74 75 TEST_P(JSSharedOperatorTest, NumberOfInputsAndOutputs) { 76 JSOperatorBuilder javascript(zone()); 77 const SharedOperator& sop = GetParam(); 78 const Operator* op = (javascript.*sop.constructor)(); 79 80 const int context_input_count = 1; 81 EXPECT_EQ(sop.value_input_count, op->ValueInputCount()); 82 EXPECT_EQ(context_input_count, OperatorProperties::GetContextInputCount(op)); 83 EXPECT_EQ(sop.frame_state_input_count, 84 OperatorProperties::GetFrameStateInputCount(op)); 85 EXPECT_EQ(sop.effect_input_count, op->EffectInputCount()); 86 EXPECT_EQ(sop.control_input_count, op->ControlInputCount()); 87 EXPECT_EQ(sop.value_input_count + context_input_count + 88 sop.frame_state_input_count + sop.effect_input_count + 89 sop.control_input_count, 90 OperatorProperties::GetTotalInputCount(op)); 91 92 EXPECT_EQ(sop.value_output_count, op->ValueOutputCount()); 93 EXPECT_EQ(sop.effect_output_count, op->EffectOutputCount()); 94 EXPECT_EQ(sop.control_output_count, op->ControlOutputCount()); 95 } 96 97 98 TEST_P(JSSharedOperatorTest, OpcodeIsCorrect) { 99 JSOperatorBuilder javascript(zone()); 100 const SharedOperator& sop = GetParam(); 101 const Operator* op = (javascript.*sop.constructor)(); 102 EXPECT_EQ(sop.opcode, op->opcode()); 103 } 104 105 106 TEST_P(JSSharedOperatorTest, Properties) { 107 JSOperatorBuilder javascript(zone()); 108 const SharedOperator& sop = GetParam(); 109 const Operator* op = (javascript.*sop.constructor)(); 110 EXPECT_EQ(sop.properties, op->properties()); 111 } 112 113 114 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSSharedOperatorTest, 115 ::testing::ValuesIn(kSharedOperators)); 116 117 } // namespace compiler 118 } // namespace internal 119 } // namespace v8 120