OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ | 5 #ifndef V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ |
6 #define V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ | 6 #define V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
| 9 #include <ostream> // NOLINT(readability/streams) |
9 | 10 |
| 11 #include "src/base/utils/random-number-generator.h" |
10 #include "src/compiler/instruction-selector.h" | 12 #include "src/compiler/instruction-selector.h" |
11 #include "src/compiler/raw-machine-assembler.h" | 13 #include "src/compiler/raw-machine-assembler.h" |
12 #include "test/compiler-unittests/compiler-unittests.h" | 14 #include "test/compiler-unittests/compiler-unittests.h" |
13 | 15 |
14 namespace v8 { | 16 namespace v8 { |
15 namespace internal { | 17 namespace internal { |
16 namespace compiler { | 18 namespace compiler { |
17 | 19 |
18 class InstructionSelectorTest : public CompilerTest { | 20 class InstructionSelectorTest : public CompilerTest { |
19 public: | 21 public: |
20 InstructionSelectorTest() {} | 22 InstructionSelectorTest(); |
21 virtual ~InstructionSelectorTest() {} | 23 virtual ~InstructionSelectorTest() {} |
22 | 24 |
| 25 base::RandomNumberGenerator* rng() { return &rng_; } |
| 26 |
23 protected: | 27 protected: |
24 class Stream; | 28 class Stream; |
25 | 29 |
26 enum StreamBuilderMode { kAllInstructions, kTargetInstructions }; | 30 enum StreamBuilderMode { kAllInstructions, kTargetInstructions }; |
27 | 31 |
28 class StreamBuilder V8_FINAL : public RawMachineAssembler { | 32 class StreamBuilder V8_FINAL : public RawMachineAssembler { |
29 public: | 33 public: |
30 StreamBuilder(InstructionSelectorTest* test, MachineType return_type) | 34 StreamBuilder(InstructionSelectorTest* test, MachineType return_type) |
31 : RawMachineAssembler(new (test->zone()) Graph(test->zone()), | 35 : RawMachineAssembler(new (test->zone()) Graph(test->zone()), |
32 CallDescriptorBuilder(test->zone(), return_type)), | 36 CallDescriptorBuilder(test->zone(), return_type)), |
33 test_(test) {} | 37 test_(test) {} |
34 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, | 38 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, |
35 MachineType parameter0_type) | 39 MachineType parameter0_type) |
36 : RawMachineAssembler(new (test->zone()) Graph(test->zone()), | 40 : RawMachineAssembler(new (test->zone()) Graph(test->zone()), |
37 CallDescriptorBuilder(test->zone(), return_type, | 41 CallDescriptorBuilder(test->zone(), return_type, |
38 parameter0_type)), | 42 parameter0_type)), |
39 test_(test) {} | 43 test_(test) {} |
40 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, | 44 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, |
41 MachineType parameter0_type, MachineType parameter1_type) | 45 MachineType parameter0_type, MachineType parameter1_type) |
42 : RawMachineAssembler( | 46 : RawMachineAssembler( |
43 new (test->zone()) Graph(test->zone()), | 47 new (test->zone()) Graph(test->zone()), |
44 CallDescriptorBuilder(test->zone(), return_type, parameter0_type, | 48 CallDescriptorBuilder(test->zone(), return_type, parameter0_type, |
45 parameter1_type)), | 49 parameter1_type)), |
46 test_(test) {} | 50 test_(test) {} |
| 51 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, |
| 52 MachineType parameter0_type, MachineType parameter1_type, |
| 53 MachineType parameter2_type) |
| 54 : RawMachineAssembler( |
| 55 new (test->zone()) Graph(test->zone()), |
| 56 CallDescriptorBuilder(test->zone(), return_type, parameter0_type, |
| 57 parameter1_type, parameter2_type)), |
| 58 test_(test) {} |
47 | 59 |
48 Stream Build(CpuFeature feature) { | 60 Stream Build(CpuFeature feature) { |
49 return Build(InstructionSelector::Features(feature)); | 61 return Build(InstructionSelector::Features(feature)); |
50 } | 62 } |
51 Stream Build(CpuFeature feature1, CpuFeature feature2) { | 63 Stream Build(CpuFeature feature1, CpuFeature feature2) { |
52 return Build(InstructionSelector::Features(feature1, feature2)); | 64 return Build(InstructionSelector::Features(feature1, feature2)); |
53 } | 65 } |
54 Stream Build(StreamBuilderMode mode = kTargetInstructions) { | 66 Stream Build(StreamBuilderMode mode = kTargetInstructions) { |
55 return Build(InstructionSelector::Features(), mode); | 67 return Build(InstructionSelector::Features(), mode); |
56 } | 68 } |
(...skipping 17 matching lines...) Expand all Loading... |
74 MachineCallDescriptorBuilder* CallDescriptorBuilder( | 86 MachineCallDescriptorBuilder* CallDescriptorBuilder( |
75 Zone* zone, MachineType return_type, MachineType parameter0_type, | 87 Zone* zone, MachineType return_type, MachineType parameter0_type, |
76 MachineType parameter1_type) { | 88 MachineType parameter1_type) { |
77 MachineType* parameter_types = zone->NewArray<MachineType>(2); | 89 MachineType* parameter_types = zone->NewArray<MachineType>(2); |
78 parameter_types[0] = parameter0_type; | 90 parameter_types[0] = parameter0_type; |
79 parameter_types[1] = parameter1_type; | 91 parameter_types[1] = parameter1_type; |
80 return new (zone) | 92 return new (zone) |
81 MachineCallDescriptorBuilder(return_type, 2, parameter_types); | 93 MachineCallDescriptorBuilder(return_type, 2, parameter_types); |
82 } | 94 } |
83 | 95 |
| 96 MachineCallDescriptorBuilder* CallDescriptorBuilder( |
| 97 Zone* zone, MachineType return_type, MachineType parameter0_type, |
| 98 MachineType parameter1_type, MachineType parameter2_type) { |
| 99 MachineType* parameter_types = zone->NewArray<MachineType>(3); |
| 100 parameter_types[0] = parameter0_type; |
| 101 parameter_types[1] = parameter1_type; |
| 102 parameter_types[2] = parameter2_type; |
| 103 return new (zone) |
| 104 MachineCallDescriptorBuilder(return_type, 3, parameter_types); |
| 105 } |
| 106 |
84 private: | 107 private: |
85 InstructionSelectorTest* test_; | 108 InstructionSelectorTest* test_; |
86 }; | 109 }; |
87 | 110 |
88 class Stream V8_FINAL { | 111 class Stream V8_FINAL { |
89 public: | 112 public: |
90 size_t size() const { return instructions_.size(); } | 113 size_t size() const { return instructions_.size(); } |
91 const Instruction* operator[](size_t index) const { | 114 const Instruction* operator[](size_t index) const { |
92 EXPECT_LT(index, size()); | 115 EXPECT_LT(index, size()); |
93 return instructions_[index]; | 116 return instructions_[index]; |
94 } | 117 } |
95 | 118 |
96 int32_t ToInt32(const InstructionOperand* operand) const { | 119 int32_t ToInt32(const InstructionOperand* operand) const { |
97 return ToConstant(operand).ToInt32(); | 120 return ToConstant(operand).ToInt32(); |
98 } | 121 } |
99 | 122 |
| 123 int ToVreg(const InstructionOperand* operand) const { |
| 124 EXPECT_EQ(InstructionOperand::UNALLOCATED, operand->kind()); |
| 125 return UnallocatedOperand::cast(operand)->virtual_register(); |
| 126 } |
| 127 |
100 private: | 128 private: |
101 Constant ToConstant(const InstructionOperand* operand) const { | 129 Constant ToConstant(const InstructionOperand* operand) const { |
102 ConstantMap::const_iterator i; | 130 ConstantMap::const_iterator i; |
103 if (operand->IsConstant()) { | 131 if (operand->IsConstant()) { |
104 i = constants_.find(operand->index()); | 132 i = constants_.find(operand->index()); |
105 EXPECT_NE(constants_.end(), i); | 133 EXPECT_NE(constants_.end(), i); |
106 } else { | 134 } else { |
107 EXPECT_EQ(InstructionOperand::IMMEDIATE, operand->kind()); | 135 EXPECT_EQ(InstructionOperand::IMMEDIATE, operand->kind()); |
108 i = immediates_.find(operand->index()); | 136 i = immediates_.find(operand->index()); |
109 EXPECT_NE(immediates_.end(), i); | 137 EXPECT_NE(immediates_.end(), i); |
110 } | 138 } |
111 EXPECT_EQ(operand->index(), i->first); | 139 EXPECT_EQ(operand->index(), i->first); |
112 return i->second; | 140 return i->second; |
113 } | 141 } |
114 | 142 |
115 friend class StreamBuilder; | 143 friend class StreamBuilder; |
116 | 144 |
117 typedef std::map<int, Constant> ConstantMap; | 145 typedef std::map<int, Constant> ConstantMap; |
118 | 146 |
119 ConstantMap constants_; | 147 ConstantMap constants_; |
120 ConstantMap immediates_; | 148 ConstantMap immediates_; |
121 std::deque<Instruction*> instructions_; | 149 std::deque<Instruction*> instructions_; |
122 }; | 150 }; |
| 151 |
| 152 base::RandomNumberGenerator rng_; |
123 }; | 153 }; |
124 | 154 |
| 155 |
| 156 template <typename T> |
| 157 class InstructionSelectorTestWithParam |
| 158 : public InstructionSelectorTest, |
| 159 public ::testing::WithParamInterface<T> {}; |
| 160 |
125 } // namespace compiler | 161 } // namespace compiler |
126 } // namespace internal | 162 } // namespace internal |
127 } // namespace v8 | 163 } // namespace v8 |
128 | 164 |
129 #endif // V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ | 165 #endif // V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ |
OLD | NEW |