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 #include <stack> | |
6 | |
7 #include "src/v8.h" | |
8 | |
9 #include "src/interpreter/register-translator.h" | |
10 #include "src/isolate.h" | |
11 #include "test/unittests/test-utils.h" | |
12 | |
13 namespace v8 { | |
14 namespace internal { | |
15 namespace interpreter { | |
16 | |
17 class RegisterTranslatorTest : public TestWithIsolateAndZone, | |
18 private RegisterMover { | |
19 public: | |
20 RegisterTranslatorTest() : translator_(this), move_count_(0) { | |
21 window_start_ = | |
22 RegisterTranslator::DistanceToTranslationWindow(Register(0)); | |
23 window_width_ = | |
24 Register::MaxRegisterIndexForByteOperand() - window_start_ + 1; | |
25 } | |
26 | |
27 ~RegisterTranslatorTest() override {} | |
28 | |
29 bool PopMoveAndMatch(Register from, Register to) { | |
30 bool match = false; | |
31 if (from.is_valid()) { | |
32 CHECK(to.is_valid()); | |
33 match = | |
34 (moves_.top().first.is_valid() && moves_.top().second.is_valid() && | |
rmcilroy
2016/01/26 11:01:45
nit - pull out moves_.top() as a local
oth
2016/01/26 13:39:57
Done.
| |
35 moves_.top().first == from && moves_.top().second == to); | |
36 } else { | |
37 CHECK(!to.is_valid()); | |
38 match = | |
39 (!moves_.top().first.is_valid() && !moves_.top().second.is_valid()); | |
40 } | |
41 moves_.pop(); | |
42 return match; | |
43 } | |
44 | |
45 int move_count() const { return move_count_; } | |
46 RegisterTranslator* translator() { return &translator_; } | |
47 | |
48 int window_start() const { return window_start_; } | |
49 int window_width() const { return window_width_; } | |
50 int window_limit() const { return window_start_ + window_width_; } | |
51 | |
52 protected: | |
53 static const char* const kBadOperandRegex; | |
54 | |
55 private: | |
56 void MoveRegisterUntranslated(Register from, Register to) override { | |
57 moves_.push(std::make_pair(from, to)); | |
58 move_count_++; | |
59 } | |
60 | |
61 bool RegisterOperandIsMovable(Bytecode bytecode, int operand_index) override { | |
62 OperandType operand_type = | |
63 Bytecodes::GetOperandType(bytecode, operand_index); | |
64 if (operand_type == OperandType::kReg8 || | |
65 operand_type == OperandType::kReg16) { | |
66 if (operand_index == Bytecodes::NumberOfOperands(bytecode) - 1) { | |
67 return true; | |
68 } | |
69 OperandType next_operand_type = | |
70 Bytecodes::GetOperandType(bytecode, operand_index + 1); | |
71 return (next_operand_type != OperandType::kRegCount8 && | |
72 next_operand_type != OperandType::kRegCount16); | |
73 } else { | |
74 return false; | |
75 } | |
76 } | |
77 | |
78 RegisterTranslator translator_; | |
79 std::stack<std::pair<Register, Register>> moves_; | |
80 int move_count_; | |
81 int window_start_; | |
82 int window_width_; | |
83 }; | |
84 | |
85 | |
86 const char* const RegisterTranslatorTest::kBadOperandRegex = | |
87 ".*OperandType::kReg8 && mover\\(\\)->RegisterOperandIsMovable.*"; | |
88 | |
89 | |
90 TEST_F(RegisterTranslatorTest, TestFrameSizeAdjustmentsForTranslationWindow) { | |
91 EXPECT_EQ(0, RegisterTranslator::RegisterCountAdjustment(0, 0)); | |
92 EXPECT_EQ(0, RegisterTranslator::RegisterCountAdjustment(10, 10)); | |
93 EXPECT_EQ(window_width(), | |
94 RegisterTranslator::RegisterCountAdjustment(173, 0)); | |
95 EXPECT_EQ(window_width(), | |
96 RegisterTranslator::RegisterCountAdjustment(173, 137)); | |
97 EXPECT_EQ(window_width(), | |
98 RegisterTranslator::RegisterCountAdjustment(173, 137)); | |
99 EXPECT_EQ(0, RegisterTranslator::RegisterCountAdjustment(0, 120)); | |
100 EXPECT_EQ(window_limit(), | |
101 RegisterTranslator::RegisterCountAdjustment(0, 128)); | |
102 EXPECT_EQ(window_limit(), | |
103 RegisterTranslator::RegisterCountAdjustment(0, 129)); | |
104 EXPECT_EQ(window_limit() - 32, | |
105 RegisterTranslator::RegisterCountAdjustment(32, 129)); | |
106 } | |
107 | |
108 | |
109 TEST_F(RegisterTranslatorTest, TestInTranslationWindow) { | |
110 EXPECT_GE(window_start(), 0); | |
111 EXPECT_FALSE( | |
112 RegisterTranslator::InTranslationWindow(Register(window_start() - 1))); | |
113 EXPECT_TRUE(RegisterTranslator::InTranslationWindow( | |
114 Register(Register::MaxRegisterIndexForByteOperand()))); | |
115 EXPECT_FALSE(RegisterTranslator::InTranslationWindow( | |
116 Register(Register::MaxRegisterIndexForByteOperand() + 1))); | |
117 for (int index = window_start(); index < window_limit(); index += 1) { | |
118 EXPECT_TRUE(RegisterTranslator::InTranslationWindow(Register(index))); | |
119 } | |
120 } | |
121 | |
122 | |
123 TEST_F(RegisterTranslatorTest, FitsInReg8Operand) { | |
124 EXPECT_GT(window_start(), 0); | |
125 EXPECT_TRUE(RegisterTranslator::FitsInReg8Operand( | |
126 Register::FromParameterIndex(0, 3))); | |
127 EXPECT_TRUE(RegisterTranslator::FitsInReg8Operand( | |
128 Register::FromParameterIndex(2, 3))); | |
129 EXPECT_TRUE(RegisterTranslator::FitsInReg8Operand(Register(0))); | |
130 EXPECT_TRUE( | |
131 RegisterTranslator::FitsInReg8Operand(Register(window_start() - 1))); | |
132 EXPECT_FALSE(RegisterTranslator::FitsInReg8Operand(Register(kMaxInt8))); | |
133 EXPECT_FALSE(RegisterTranslator::FitsInReg8Operand(Register(kMaxInt8 + 1))); | |
134 for (int index = window_start(); index < window_limit(); index += 1) { | |
135 EXPECT_FALSE(RegisterTranslator::FitsInReg8Operand(Register(index))); | |
136 } | |
137 } | |
138 | |
139 | |
140 TEST_F(RegisterTranslatorTest, FitsInReg16Operand) { | |
141 EXPECT_GT(window_start(), 0); | |
142 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand( | |
143 Register::FromParameterIndex(0, 3))); | |
144 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand( | |
145 Register::FromParameterIndex(2, 3))); | |
146 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand( | |
147 Register::FromParameterIndex(0, 999))); | |
148 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand( | |
149 Register::FromParameterIndex(0, Register::MaxParameterIndex() + 1))); | |
150 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand(Register(0))); | |
151 EXPECT_TRUE( | |
152 RegisterTranslator::FitsInReg16Operand(Register(window_start() - 1))); | |
153 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand(Register(kMaxInt8 + 1))); | |
154 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand(Register(kMaxInt8 + 2))); | |
155 for (int index = 0; index <= kMaxInt16 - window_width(); index += 1) { | |
156 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand(Register(index))); | |
157 } | |
158 for (int index = Register::MaxRegisterIndex() - window_width() + 1; | |
159 index < Register::MaxRegisterIndex() + 2; index += 1) { | |
160 EXPECT_FALSE(RegisterTranslator::FitsInReg16Operand(Register(index))); | |
161 } | |
162 } | |
163 | |
164 | |
165 TEST_F(RegisterTranslatorTest, NoTranslationRequired) { | |
166 Register window_reg(window_start()); | |
167 Register local_reg(57); | |
168 uint32_t operands[] = {local_reg.ToRawOperand()}; | |
169 translator()->TranslateInputRegisters(Bytecode::kLdar, operands, 1); | |
170 translator()->TranslateOutputRegisters(); | |
171 EXPECT_EQ(0, move_count()); | |
172 | |
173 Register param_reg = Register::FromParameterIndex(129, 130); | |
174 operands[0] = param_reg.ToRawOperand(); | |
175 translator()->TranslateInputRegisters(Bytecode::kLdar, operands, 1); | |
176 translator()->TranslateOutputRegisters(); | |
177 EXPECT_EQ(0, move_count()); | |
178 } | |
179 | |
180 | |
181 TEST_F(RegisterTranslatorTest, TranslationRequired) { | |
182 Register window_reg(window_start()); | |
183 Register local_reg(137); | |
184 Register local_reg_translated(local_reg.index() + window_width()); | |
185 | |
186 uint32_t operands[] = {local_reg.ToRawOperand()}; | |
187 translator()->TranslateInputRegisters(Bytecode::kLdar, operands, 1); | |
188 EXPECT_EQ(1, move_count()); | |
189 EXPECT_TRUE(PopMoveAndMatch(local_reg_translated, window_reg)); | |
190 translator()->TranslateOutputRegisters(); | |
191 EXPECT_EQ(2, move_count()); | |
192 EXPECT_TRUE(PopMoveAndMatch(window_reg, local_reg_translated)); | |
193 | |
194 Register param_reg = Register::FromParameterIndex(0, 130); | |
195 operands[0] = {param_reg.ToRawOperand()}; | |
196 translator()->TranslateInputRegisters(Bytecode::kLdar, operands, 1); | |
197 EXPECT_EQ(3, move_count()); | |
198 EXPECT_TRUE(PopMoveAndMatch(param_reg, window_reg)); | |
199 translator()->TranslateOutputRegisters(); | |
200 EXPECT_EQ(4, move_count()); | |
201 EXPECT_TRUE(PopMoveAndMatch(window_reg, param_reg)); | |
202 } | |
203 | |
204 | |
205 TEST_F(RegisterTranslatorTest, RangeTranslation) { | |
206 Register invalid; | |
207 Register window0(window_start()); | |
208 Register window1(window_start() + 1); | |
209 Register window2(window_start() + 2); | |
210 uint32_t operands[3]; | |
211 | |
212 // Bytecode::kNew with valid range operand. | |
213 Register constructor0(0); | |
214 Register args0(1); | |
215 operands[0] = constructor0.ToRawOperand(); | |
216 operands[1] = args0.ToRawOperand(); | |
217 operands[2] = 1; | |
218 translator()->TranslateInputRegisters(Bytecode::kNew, operands, 3); | |
219 translator()->TranslateOutputRegisters(); | |
220 EXPECT_EQ(0, move_count()); | |
221 | |
222 // Bytecode::kNewWide with valid range operand. | |
223 Register constructor1(128); | |
224 Register constructor1_translated(constructor1.index() + window_width()); | |
225 Register args1(129); | |
226 Register args1_translated(args1.index() + window_width()); | |
227 operands[0] = constructor1.ToRawOperand(); | |
228 operands[1] = args1.ToRawOperand(); | |
229 operands[2] = 3; | |
230 translator()->TranslateInputRegisters(Bytecode::kNewWide, operands, 3); | |
231 translator()->TranslateOutputRegisters(); | |
232 EXPECT_EQ(0, move_count()); | |
233 } | |
234 | |
235 | |
236 TEST_F(RegisterTranslatorTest, BadRange0) { | |
237 // Bytecode::kNew with invalid range operand (kMaybeReg8). | |
238 Register constructor1(128); | |
239 Register args1(129); | |
240 uint32_t operands[] = {constructor1.ToRawOperand(), args1.ToRawOperand(), 3}; | |
241 ASSERT_DEATH_IF_SUPPORTED( | |
242 translator()->TranslateInputRegisters(Bytecode::kNew, operands, 3), | |
243 kBadOperandRegex); | |
244 } | |
245 | |
246 | |
247 TEST_F(RegisterTranslatorTest, BadRange1) { | |
248 // Bytecode::kForInPrepare with invalid range operand (kRegTriple8) | |
249 Register for_in_state(160); | |
250 Register for_in_state_translated(for_in_state.index() + window_width()); | |
251 uint32_t operands[] = {for_in_state.ToRawOperand()}; | |
252 ASSERT_DEATH_IF_SUPPORTED(translator()->TranslateInputRegisters( | |
253 Bytecode::kForInPrepare, operands, 1), | |
254 kBadOperandRegex); | |
255 } | |
256 | |
257 | |
258 TEST_F(RegisterTranslatorTest, BadRange2) { | |
259 // Bytecode::kForInNext with invalid range operand (kRegPair8) | |
260 Register receiver(192); | |
261 Register receiver_translated(receiver.index() + window_width()); | |
262 Register index(193); | |
263 Register index_translated(index.index() + window_width()); | |
264 Register cache_info_pair(194); | |
265 Register cache_info_pair_translated(cache_info_pair.index() + window_width()); | |
266 uint32_t operands[] = {receiver.ToRawOperand(), index.ToRawOperand(), | |
267 cache_info_pair.ToRawOperand()}; | |
268 ASSERT_DEATH_IF_SUPPORTED( | |
269 translator()->TranslateInputRegisters(Bytecode::kForInNext, operands, 3), | |
270 kBadOperandRegex); | |
271 } | |
272 | |
273 } // namespace interpreter | |
274 } // namespace internal | |
275 } // namespace v8 | |
OLD | NEW |