OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "src/v8.h" | 7 #include "src/v8.h" |
8 | 8 |
9 #include "src/interpreter/bytecodes.h" | 9 #include "src/interpreter/bytecodes.h" |
10 #include "test/unittests/test-utils.h" | 10 #include "test/unittests/test-utils.h" |
11 | 11 |
12 | |
13 namespace v8 { | 12 namespace v8 { |
14 namespace internal { | 13 namespace internal { |
15 namespace interpreter { | 14 namespace interpreter { |
16 | 15 |
17 TEST(OperandConversion, Registers) { | 16 TEST(OperandConversion, Registers) { |
18 for (int i = 0; i < 128; i++) { | 17 int register_count = Register::MaxRegisterIndex() + 1; |
19 uint8_t operand_value = Register(i).ToOperand(); | 18 int step = register_count / 7; |
20 Register r = Register::FromOperand(operand_value); | 19 for (int i = 0; i < register_count; i += step) { |
21 CHECK_EQ(i, r.index()); | 20 if (i <= kMaxInt8) { |
| 21 uint8_t operand0 = Register(i).ToOperand(); |
| 22 Register reg0 = Register::FromOperand(operand0); |
| 23 CHECK_EQ(i, reg0.index()); |
| 24 } |
| 25 |
| 26 uint16_t operand1 = Register(i).ToWideOperand(); |
| 27 Register reg1 = Register::FromWideOperand(operand1); |
| 28 CHECK_EQ(i, reg1.index()); |
| 29 |
| 30 uint32_t operand2 = Register(i).ToRawOperand(); |
| 31 Register reg2 = Register::FromRawOperand(operand2); |
| 32 CHECK_EQ(i, reg2.index()); |
| 33 } |
| 34 |
| 35 for (int i = 0; i <= kMaxUInt8; i++) { |
| 36 uint8_t operand = static_cast<uint8_t>(i); |
| 37 Register reg = Register::FromOperand(operand); |
| 38 if (i > 0 && i < -kMinInt8) { |
| 39 CHECK(reg.is_parameter()); |
| 40 } else { |
| 41 CHECK(!reg.is_parameter()); |
| 42 } |
22 } | 43 } |
23 } | 44 } |
24 | 45 |
25 | 46 |
26 TEST(OperandConversion, Parameters) { | 47 TEST(OperandConversion, Parameters) { |
27 int parameter_counts[] = {7, 13, 99}; | 48 int parameter_counts[] = {7, 13, 99}; |
28 | 49 |
29 size_t count = sizeof(parameter_counts) / sizeof(parameter_counts[0]); | 50 size_t count = sizeof(parameter_counts) / sizeof(parameter_counts[0]); |
30 for (size_t p = 0; p < count; p++) { | 51 for (size_t p = 0; p < count; p++) { |
31 int parameter_count = parameter_counts[p]; | 52 int parameter_count = parameter_counts[p]; |
32 for (int i = 0; i < parameter_count; i++) { | 53 for (int i = 0; i < parameter_count; i++) { |
33 Register r = Register::FromParameterIndex(i, parameter_count); | 54 Register r = Register::FromParameterIndex(i, parameter_count); |
34 uint8_t operand_value = r.ToOperand(); | 55 uint8_t operand_value = r.ToOperand(); |
35 Register s = Register::FromOperand(operand_value); | 56 Register s = Register::FromOperand(operand_value); |
36 CHECK_EQ(i, s.ToParameterIndex(parameter_count)); | 57 CHECK_EQ(i, s.ToParameterIndex(parameter_count)); |
37 } | 58 } |
38 } | 59 } |
39 } | 60 } |
40 | 61 |
41 | 62 |
42 TEST(OperandConversion, RegistersParametersNoOverlap) { | 63 TEST(OperandConversion, RegistersParametersNoOverlap) { |
43 std::vector<uint8_t> operand_count(256); | 64 int register_count = Register::MaxRegisterIndex() + 1; |
| 65 int parameter_count = Register::MaxParameterIndex() + 1; |
| 66 int32_t register_space_size = base::bits::RoundUpToPowerOfTwo32( |
| 67 static_cast<uint32_t>(register_count + parameter_count)); |
| 68 uint32_t range = static_cast<uint32_t>(register_space_size); |
| 69 std::vector<uint8_t> operand_count(range); |
44 | 70 |
45 for (int i = 0; i <= kMaxInt8; i++) { | 71 for (int i = 0; i < register_count; i += 1) { |
46 Register r = Register(i); | 72 Register r = Register(i); |
47 uint8_t operand = r.ToOperand(); | 73 uint32_t operand = r.ToWideOperand(); |
| 74 CHECK_LT(operand, operand_count.size()); |
48 operand_count[operand] += 1; | 75 operand_count[operand] += 1; |
49 CHECK_EQ(operand_count[operand], 1); | 76 CHECK_EQ(operand_count[operand], 1); |
50 } | 77 } |
51 | 78 |
52 int parameter_count = Register::MaxParameterIndex() + 1; | 79 for (int i = 0; i < parameter_count; i += 1) { |
53 for (int i = 0; i < parameter_count; i++) { | |
54 Register r = Register::FromParameterIndex(i, parameter_count); | 80 Register r = Register::FromParameterIndex(i, parameter_count); |
55 uint8_t operand = r.ToOperand(); | 81 uint32_t operand = r.ToWideOperand(); |
| 82 CHECK_LT(operand, operand_count.size()); |
56 operand_count[operand] += 1; | 83 operand_count[operand] += 1; |
57 CHECK_EQ(operand_count[operand], 1); | 84 CHECK_EQ(operand_count[operand], 1); |
58 } | 85 } |
59 } | 86 } |
60 | 87 |
| 88 |
| 89 TEST(Bytecodes, HasAnyRegisterOperands) { |
| 90 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kAdd), 1); |
| 91 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCall), 2); |
| 92 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntime), 1); |
| 93 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeWide), 1); |
| 94 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPair), |
| 95 2); |
| 96 CHECK_EQ( |
| 97 Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPairWide), |
| 98 2); |
| 99 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kDeletePropertyStrict), |
| 100 1); |
| 101 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kDeleteLookupSlot), 0); |
| 102 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepare), 1); |
| 103 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepareWide), 1); |
| 104 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kInc), 0); |
| 105 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kJumpIfTrue), 0); |
| 106 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kNew), 2); |
| 107 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kToName), 0); |
| 108 } |
| 109 |
61 } // namespace interpreter | 110 } // namespace interpreter |
62 } // namespace internal | 111 } // namespace internal |
63 } // namespace v8 | 112 } // namespace v8 |
OLD | NEW |