OLD | NEW |
| (Empty) |
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 #ifndef V8_COMPILER_INSTRUCTION_SELECTOR_UNITTEST_H_ | |
6 #define V8_COMPILER_INSTRUCTION_SELECTOR_UNITTEST_H_ | |
7 | |
8 #include <deque> | |
9 #include <set> | |
10 | |
11 #include "src/base/utils/random-number-generator.h" | |
12 #include "src/compiler/instruction-selector.h" | |
13 #include "src/compiler/raw-machine-assembler.h" | |
14 #include "src/test/test-utils.h" | |
15 | |
16 namespace v8 { | |
17 namespace internal { | |
18 namespace compiler { | |
19 | |
20 class InstructionSelectorTest : public TestWithContext, public TestWithZone { | |
21 public: | |
22 InstructionSelectorTest(); | |
23 virtual ~InstructionSelectorTest(); | |
24 | |
25 base::RandomNumberGenerator* rng() { return &rng_; } | |
26 | |
27 class Stream; | |
28 | |
29 enum StreamBuilderMode { | |
30 kAllInstructions, | |
31 kTargetInstructions, | |
32 kAllExceptNopInstructions | |
33 }; | |
34 | |
35 class StreamBuilder FINAL : public RawMachineAssembler { | |
36 public: | |
37 StreamBuilder(InstructionSelectorTest* test, MachineType return_type) | |
38 : RawMachineAssembler(new (test->zone()) Graph(test->zone()), | |
39 MakeMachineSignature(test->zone(), return_type)), | |
40 test_(test) {} | |
41 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, | |
42 MachineType parameter0_type) | |
43 : RawMachineAssembler( | |
44 new (test->zone()) Graph(test->zone()), | |
45 MakeMachineSignature(test->zone(), return_type, parameter0_type)), | |
46 test_(test) {} | |
47 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, | |
48 MachineType parameter0_type, MachineType parameter1_type) | |
49 : RawMachineAssembler( | |
50 new (test->zone()) Graph(test->zone()), | |
51 MakeMachineSignature(test->zone(), return_type, parameter0_type, | |
52 parameter1_type)), | |
53 test_(test) {} | |
54 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, | |
55 MachineType parameter0_type, MachineType parameter1_type, | |
56 MachineType parameter2_type) | |
57 : RawMachineAssembler( | |
58 new (test->zone()) Graph(test->zone()), | |
59 MakeMachineSignature(test->zone(), return_type, parameter0_type, | |
60 parameter1_type, parameter2_type)), | |
61 test_(test) {} | |
62 | |
63 Stream Build(CpuFeature feature) { | |
64 return Build(InstructionSelector::Features(feature)); | |
65 } | |
66 Stream Build(CpuFeature feature1, CpuFeature feature2) { | |
67 return Build(InstructionSelector::Features(feature1, feature2)); | |
68 } | |
69 Stream Build(StreamBuilderMode mode = kTargetInstructions) { | |
70 return Build(InstructionSelector::Features(), mode); | |
71 } | |
72 Stream Build(InstructionSelector::Features features, | |
73 StreamBuilderMode mode = kTargetInstructions); | |
74 | |
75 private: | |
76 MachineSignature* MakeMachineSignature(Zone* zone, | |
77 MachineType return_type) { | |
78 MachineSignature::Builder builder(zone, 1, 0); | |
79 builder.AddReturn(return_type); | |
80 return builder.Build(); | |
81 } | |
82 | |
83 MachineSignature* MakeMachineSignature(Zone* zone, MachineType return_type, | |
84 MachineType parameter0_type) { | |
85 MachineSignature::Builder builder(zone, 1, 1); | |
86 builder.AddReturn(return_type); | |
87 builder.AddParam(parameter0_type); | |
88 return builder.Build(); | |
89 } | |
90 | |
91 MachineSignature* MakeMachineSignature(Zone* zone, MachineType return_type, | |
92 MachineType parameter0_type, | |
93 MachineType parameter1_type) { | |
94 MachineSignature::Builder builder(zone, 1, 2); | |
95 builder.AddReturn(return_type); | |
96 builder.AddParam(parameter0_type); | |
97 builder.AddParam(parameter1_type); | |
98 return builder.Build(); | |
99 } | |
100 | |
101 MachineSignature* MakeMachineSignature(Zone* zone, MachineType return_type, | |
102 MachineType parameter0_type, | |
103 MachineType parameter1_type, | |
104 MachineType parameter2_type) { | |
105 MachineSignature::Builder builder(zone, 1, 3); | |
106 builder.AddReturn(return_type); | |
107 builder.AddParam(parameter0_type); | |
108 builder.AddParam(parameter1_type); | |
109 builder.AddParam(parameter2_type); | |
110 return builder.Build(); | |
111 } | |
112 | |
113 private: | |
114 InstructionSelectorTest* test_; | |
115 }; | |
116 | |
117 class Stream FINAL { | |
118 public: | |
119 size_t size() const { return instructions_.size(); } | |
120 const Instruction* operator[](size_t index) const { | |
121 EXPECT_LT(index, size()); | |
122 return instructions_[index]; | |
123 } | |
124 | |
125 bool IsDouble(const InstructionOperand* operand) const { | |
126 return IsDouble(ToVreg(operand)); | |
127 } | |
128 bool IsDouble(int virtual_register) const { | |
129 return doubles_.find(virtual_register) != doubles_.end(); | |
130 } | |
131 | |
132 bool IsInteger(const InstructionOperand* operand) const { | |
133 return IsInteger(ToVreg(operand)); | |
134 } | |
135 bool IsInteger(int virtual_register) const { | |
136 return !IsDouble(virtual_register) && !IsReference(virtual_register); | |
137 } | |
138 | |
139 bool IsReference(const InstructionOperand* operand) const { | |
140 return IsReference(ToVreg(operand)); | |
141 } | |
142 bool IsReference(int virtual_register) const { | |
143 return references_.find(virtual_register) != references_.end(); | |
144 } | |
145 | |
146 float ToFloat32(const InstructionOperand* operand) const { | |
147 return ToConstant(operand).ToFloat32(); | |
148 } | |
149 | |
150 int32_t ToInt32(const InstructionOperand* operand) const { | |
151 return ToConstant(operand).ToInt32(); | |
152 } | |
153 | |
154 int64_t ToInt64(const InstructionOperand* operand) const { | |
155 return ToConstant(operand).ToInt64(); | |
156 } | |
157 | |
158 int ToVreg(const InstructionOperand* operand) const { | |
159 if (operand->IsConstant()) return operand->index(); | |
160 EXPECT_EQ(InstructionOperand::UNALLOCATED, operand->kind()); | |
161 return UnallocatedOperand::cast(operand)->virtual_register(); | |
162 } | |
163 | |
164 FrameStateDescriptor* GetFrameStateDescriptor(int deoptimization_id) { | |
165 EXPECT_LT(deoptimization_id, GetFrameStateDescriptorCount()); | |
166 return deoptimization_entries_[deoptimization_id]; | |
167 } | |
168 | |
169 int GetFrameStateDescriptorCount() { | |
170 return static_cast<int>(deoptimization_entries_.size()); | |
171 } | |
172 | |
173 private: | |
174 Constant ToConstant(const InstructionOperand* operand) const { | |
175 ConstantMap::const_iterator i; | |
176 if (operand->IsConstant()) { | |
177 i = constants_.find(operand->index()); | |
178 EXPECT_FALSE(constants_.end() == i); | |
179 } else { | |
180 EXPECT_EQ(InstructionOperand::IMMEDIATE, operand->kind()); | |
181 i = immediates_.find(operand->index()); | |
182 EXPECT_FALSE(immediates_.end() == i); | |
183 } | |
184 EXPECT_EQ(operand->index(), i->first); | |
185 return i->second; | |
186 } | |
187 | |
188 friend class StreamBuilder; | |
189 | |
190 typedef std::map<int, Constant> ConstantMap; | |
191 | |
192 ConstantMap constants_; | |
193 ConstantMap immediates_; | |
194 std::deque<Instruction*> instructions_; | |
195 std::set<int> doubles_; | |
196 std::set<int> references_; | |
197 std::deque<FrameStateDescriptor*> deoptimization_entries_; | |
198 }; | |
199 | |
200 base::RandomNumberGenerator rng_; | |
201 }; | |
202 | |
203 | |
204 template <typename T> | |
205 class InstructionSelectorTestWithParam | |
206 : public InstructionSelectorTest, | |
207 public ::testing::WithParamInterface<T> {}; | |
208 | |
209 } // namespace compiler | |
210 } // namespace internal | |
211 } // namespace v8 | |
212 | |
213 #endif // V8_COMPILER_INSTRUCTION_SELECTOR_UNITTEST_H_ | |
OLD | NEW |