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 ~RegisterTranslatorTest() override {} |
| 22 |
| 23 bool PopMoveAndMatch(Register from, Register to) { |
| 24 bool match = false; |
| 25 if (from.is_valid()) { |
| 26 CHECK(to.is_valid()); |
| 27 match = |
| 28 (moves_.top().first.is_valid() && moves_.top().second.is_valid() && |
| 29 moves_.top().first == from && moves_.top().second == to); |
| 30 } else { |
| 31 CHECK(!to.is_valid()); |
| 32 match = |
| 33 (!moves_.top().first.is_valid() && !moves_.top().second.is_valid()); |
| 34 } |
| 35 moves_.pop(); |
| 36 return match; |
| 37 } |
| 38 |
| 39 int move_count() const { return move_count_; } |
| 40 RegisterTranslator* translator() { return &translator_; } |
| 41 |
| 42 private: |
| 43 bool MoveRegisterUntranslated(Register from, Register to) override { |
| 44 moves_.push(std::make_pair(from, to)); |
| 45 move_count_++; |
| 46 return from.is_valid() && to.is_valid(); |
| 47 } |
| 48 |
| 49 RegisterTranslator translator_; |
| 50 std::stack<std::pair<Register, Register>> moves_; |
| 51 int move_count_; |
| 52 }; |
| 53 |
| 54 |
| 55 TEST_F(RegisterTranslatorTest, TestFrameSizeAdjustmentsForTranslationWindow) { |
| 56 int window_start_index = |
| 57 RegisterTranslator::DistanceToTranslationWindow(Register(0)); |
| 58 int window_width = kMaxInt8 - window_start_index + 1; |
| 59 |
| 60 EXPECT_EQ(0, RegisterTranslator::RegisterCountAdjustment(0, 0)); |
| 61 EXPECT_EQ(0, RegisterTranslator::RegisterCountAdjustment(10, 10)); |
| 62 EXPECT_EQ(window_width, RegisterTranslator::RegisterCountAdjustment(173, 0)); |
| 63 EXPECT_EQ(window_width, |
| 64 RegisterTranslator::RegisterCountAdjustment(173, 137)); |
| 65 EXPECT_EQ(window_width, |
| 66 RegisterTranslator::RegisterCountAdjustment(173, 137)); |
| 67 EXPECT_EQ(0, RegisterTranslator::RegisterCountAdjustment(0, 120)); |
| 68 EXPECT_EQ(kMaxInt8 + 1, RegisterTranslator::RegisterCountAdjustment(0, 128)); |
| 69 EXPECT_EQ(kMaxInt8 + 1, RegisterTranslator::RegisterCountAdjustment(0, 129)); |
| 70 EXPECT_EQ(kMaxInt8 + 1 - 32, |
| 71 RegisterTranslator::RegisterCountAdjustment(32, 129)); |
| 72 } |
| 73 |
| 74 |
| 75 TEST_F(RegisterTranslatorTest, TestInTranslationWindow) { |
| 76 int window_start_index = |
| 77 RegisterTranslator::DistanceToTranslationWindow(Register(0)); |
| 78 EXPECT_GE(window_start_index, 0); |
| 79 EXPECT_FALSE(RegisterTranslator::InTranslationWindow( |
| 80 Register(window_start_index - 1))); |
| 81 EXPECT_TRUE(RegisterTranslator::InTranslationWindow(Register(kMaxInt8))); |
| 82 EXPECT_FALSE(RegisterTranslator::InTranslationWindow(Register(kMaxInt8 + 1))); |
| 83 for (int index = window_start_index; index < kMaxInt8; index += 1) { |
| 84 EXPECT_TRUE(RegisterTranslator::InTranslationWindow(Register(index))); |
| 85 } |
| 86 } |
| 87 |
| 88 |
| 89 TEST_F(RegisterTranslatorTest, FitsInReg8Operand) { |
| 90 int window_start_index = |
| 91 RegisterTranslator::DistanceToTranslationWindow(Register(0)); |
| 92 EXPECT_GT(window_start_index, 0); |
| 93 EXPECT_TRUE(RegisterTranslator::FitsInReg8Operand( |
| 94 Register::FromParameterIndex(0, 3))); |
| 95 EXPECT_TRUE(RegisterTranslator::FitsInReg8Operand( |
| 96 Register::FromParameterIndex(2, 3))); |
| 97 EXPECT_TRUE(RegisterTranslator::FitsInReg8Operand(Register(0))); |
| 98 EXPECT_TRUE( |
| 99 RegisterTranslator::FitsInReg8Operand(Register(window_start_index - 1))); |
| 100 EXPECT_FALSE(RegisterTranslator::FitsInReg8Operand(Register(kMaxInt8))); |
| 101 EXPECT_FALSE(RegisterTranslator::FitsInReg8Operand(Register(kMaxInt8 + 1))); |
| 102 for (int index = window_start_index; index < kMaxInt8; index += 1) { |
| 103 EXPECT_FALSE(RegisterTranslator::FitsInReg8Operand(Register(index))); |
| 104 } |
| 105 } |
| 106 |
| 107 |
| 108 TEST_F(RegisterTranslatorTest, FitsInReg16Operand) { |
| 109 int window_start_index = |
| 110 RegisterTranslator::DistanceToTranslationWindow(Register(0)); |
| 111 int window_width = kMaxInt8 - window_start_index + 1; |
| 112 EXPECT_GT(window_start_index, 0); |
| 113 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand( |
| 114 Register::FromParameterIndex(0, 3))); |
| 115 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand( |
| 116 Register::FromParameterIndex(2, 3))); |
| 117 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand( |
| 118 Register::FromParameterIndex(0, 999))); |
| 119 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand( |
| 120 Register::FromParameterIndex(0, Register::MaxParameterIndex() + 1))); |
| 121 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand(Register(0))); |
| 122 EXPECT_TRUE( |
| 123 RegisterTranslator::FitsInReg16Operand(Register(window_start_index - 1))); |
| 124 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand(Register(kMaxInt8))); |
| 125 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand(Register(kMaxInt8 + 1))); |
| 126 for (int index = 0; index <= kMaxInt16 - window_width; index += 1) { |
| 127 EXPECT_TRUE(RegisterTranslator::FitsInReg16Operand(Register(index))); |
| 128 } |
| 129 for (int index = Register::MaxRegisterIndex() - window_width + 1; |
| 130 index < Register::MaxRegisterIndex() + 2; index += 1) { |
| 131 EXPECT_FALSE(RegisterTranslator::FitsInReg16Operand(Register(index))); |
| 132 } |
| 133 } |
| 134 |
| 135 |
| 136 TEST_F(RegisterTranslatorTest, NoTranslationRequired) { |
| 137 int window_start_index = |
| 138 RegisterTranslator::DistanceToTranslationWindow(Register(0)); |
| 139 |
| 140 Register window_reg(window_start_index); |
| 141 Register local_reg(57); |
| 142 EXPECT_EQ(translator()->Translate(Bytecode::kLdar, local_reg), local_reg); |
| 143 translator()->CompleteTranslations(); |
| 144 EXPECT_EQ(0, move_count()); |
| 145 |
| 146 Register param_reg = Register::FromParameterIndex(129, 130); |
| 147 EXPECT_EQ(translator()->Translate(Bytecode::kLdar, param_reg), param_reg); |
| 148 translator()->CompleteTranslations(); |
| 149 EXPECT_EQ(0, move_count()); |
| 150 } |
| 151 |
| 152 |
| 153 TEST_F(RegisterTranslatorTest, TranslationRequired) { |
| 154 int window_start_index = |
| 155 RegisterTranslator::DistanceToTranslationWindow(Register(0)); |
| 156 int window_width = kMaxInt8 - window_start_index + 1; |
| 157 |
| 158 Register window_reg(window_start_index); |
| 159 Register local_reg(137); |
| 160 Register local_reg_translated(local_reg.index() + window_width); |
| 161 EXPECT_EQ(translator()->Translate(Bytecode::kLdar, local_reg), window_reg); |
| 162 EXPECT_EQ(1, move_count()); |
| 163 EXPECT_TRUE(PopMoveAndMatch(local_reg_translated, window_reg)); |
| 164 translator()->CompleteTranslations(); |
| 165 EXPECT_EQ(2, move_count()); |
| 166 EXPECT_TRUE(PopMoveAndMatch(window_reg, local_reg_translated)); |
| 167 |
| 168 Register param_reg = Register::FromParameterIndex(0, 130); |
| 169 EXPECT_EQ(translator()->Translate(Bytecode::kLdar, param_reg), window_reg); |
| 170 EXPECT_EQ(3, move_count()); |
| 171 EXPECT_TRUE(PopMoveAndMatch(param_reg, window_reg)); |
| 172 translator()->CompleteTranslations(); |
| 173 EXPECT_EQ(4, move_count()); |
| 174 EXPECT_TRUE(PopMoveAndMatch(window_reg, param_reg)); |
| 175 } |
| 176 |
| 177 |
| 178 TEST_F(RegisterTranslatorTest, RangeTranslation) { |
| 179 int window_start_index = |
| 180 RegisterTranslator::DistanceToTranslationWindow(Register(0)); |
| 181 int window_width = kMaxInt8 - window_start_index + 1; |
| 182 |
| 183 Register window0(window_start_index); |
| 184 Register window1(window_start_index + 1); |
| 185 Register window2(window_start_index + 2); |
| 186 Register bad = Register::IllegalRegister(); |
| 187 |
| 188 // Bytecode::kNew with valid range operand. |
| 189 Register constructor0(0); |
| 190 Register args0(1); |
| 191 EXPECT_EQ(translator()->Translate(Bytecode::kNew, constructor0), |
| 192 constructor0); |
| 193 EXPECT_EQ(translator()->Translate(Bytecode::kNew, args0), args0); |
| 194 translator()->CompleteTranslations(); |
| 195 EXPECT_EQ(0, move_count()); |
| 196 |
| 197 // Bytecode::kNewWide with valid range operand. |
| 198 Register constructor1(128); |
| 199 Register constructor1_translated(constructor1.index() + window_width); |
| 200 Register args1(129); |
| 201 Register args1_translated(args1.index() + window_width); |
| 202 EXPECT_EQ(translator()->Translate(Bytecode::kNewWide, constructor1), |
| 203 constructor1_translated); |
| 204 EXPECT_EQ(translator()->Translate(Bytecode::kNewWide, args1), |
| 205 args1_translated); |
| 206 EXPECT_EQ(0, move_count()); |
| 207 translator()->CompleteTranslations(); |
| 208 EXPECT_EQ(0, move_count()); |
| 209 |
| 210 // Bytecode::kNew with invalid range operand (kMaybeReg8). |
| 211 EXPECT_EQ(window0, translator()->Translate(Bytecode::kNew, constructor1)); |
| 212 EXPECT_TRUE(PopMoveAndMatch(constructor1_translated, window0)); |
| 213 EXPECT_EQ(window1, translator()->Translate(Bytecode::kNew, args1)); |
| 214 EXPECT_TRUE(PopMoveAndMatch(bad, bad)); |
| 215 translator()->CompleteTranslations(); |
| 216 EXPECT_TRUE(PopMoveAndMatch(window0, constructor1_translated)); |
| 217 EXPECT_TRUE(PopMoveAndMatch(window1, args1_translated)); |
| 218 |
| 219 // Bytecode::kForInPrepare with invalid range operand (kRegTriple8) |
| 220 Register for_in_state(160); |
| 221 Register for_in_state_translated(for_in_state.index() + window_width); |
| 222 EXPECT_EQ(window0, |
| 223 translator()->Translate(Bytecode::kForInPrepare, for_in_state)); |
| 224 EXPECT_TRUE(PopMoveAndMatch(bad, bad)); |
| 225 translator()->CompleteTranslations(); |
| 226 EXPECT_TRUE(PopMoveAndMatch(window0, for_in_state_translated)); |
| 227 |
| 228 // Bytecode::kForInNext with invalid range operand (kRegPair8) |
| 229 Register receiver(192); |
| 230 Register receiver_translated(receiver.index() + window_width); |
| 231 Register index(193); |
| 232 Register index_translated(index.index() + window_width); |
| 233 Register cache_info_pair(194); |
| 234 Register cache_info_pair_translated(cache_info_pair.index() + window_width); |
| 235 EXPECT_EQ(window0, translator()->Translate(Bytecode::kForInNext, receiver)); |
| 236 EXPECT_TRUE(PopMoveAndMatch(receiver_translated, window0)); |
| 237 EXPECT_EQ(window1, translator()->Translate(Bytecode::kForInNext, index)); |
| 238 EXPECT_TRUE(PopMoveAndMatch(index_translated, window1)); |
| 239 EXPECT_EQ(window2, |
| 240 translator()->Translate(Bytecode::kForInNext, cache_info_pair)); |
| 241 EXPECT_TRUE(PopMoveAndMatch(bad, bad)); |
| 242 translator()->CompleteTranslations(); |
| 243 EXPECT_TRUE(PopMoveAndMatch(window0, receiver_translated)); |
| 244 EXPECT_TRUE(PopMoveAndMatch(window1, index_translated)); |
| 245 EXPECT_TRUE(PopMoveAndMatch(window2, cache_info_pair_translated)); |
| 246 } |
| 247 |
| 248 } // namespace interpreter |
| 249 } // namespace internal |
| 250 } // namespace v8 |
OLD | NEW |