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