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 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 namespace interpreter { | 14 namespace interpreter { |
15 | 15 |
16 TEST(OperandConversion, Registers) { | 16 TEST(OperandConversion, Registers) { |
17 int register_count = Register::MaxRegisterIndex() + 1; | 17 int register_count = 128; |
18 int step = register_count / 7; | 18 int step = register_count / 7; |
19 for (int i = 0; i < register_count; i += step) { | 19 for (int i = 0; i < register_count; i += step) { |
20 if (i <= kMaxInt8) { | 20 if (i <= kMaxInt8) { |
21 uint8_t operand0 = Register(i).ToOperand(); | 21 uint32_t operand0 = Register(i).ToOperand(); |
22 Register reg0 = Register::FromOperand(operand0); | 22 Register reg0 = Register::FromOperand(operand0); |
23 CHECK_EQ(i, reg0.index()); | 23 CHECK_EQ(i, reg0.index()); |
24 } | 24 } |
25 | 25 |
26 uint16_t operand1 = Register(i).ToWideOperand(); | 26 uint32_t operand1 = Register(i).ToOperand(); |
27 Register reg1 = Register::FromWideOperand(operand1); | 27 Register reg1 = Register::FromOperand(operand1); |
28 CHECK_EQ(i, reg1.index()); | 28 CHECK_EQ(i, reg1.index()); |
29 | 29 |
30 uint32_t operand2 = Register(i).ToRawOperand(); | 30 uint32_t operand2 = Register(i).ToOperand(); |
31 Register reg2 = Register::FromRawOperand(operand2); | 31 Register reg2 = Register::FromOperand(operand2); |
32 CHECK_EQ(i, reg2.index()); | 32 CHECK_EQ(i, reg2.index()); |
33 } | 33 } |
34 | 34 |
35 for (int i = 0; i <= kMaxUInt8; i++) { | 35 for (int i = 0; i <= kMaxUInt8; i++) { |
36 uint8_t operand = static_cast<uint8_t>(i); | 36 Register reg = Register::FromOperand(i); |
37 Register reg = Register::FromOperand(operand); | 37 if (i > 0) { |
38 if (i > 0 && i < -kMinInt8) { | |
39 CHECK(reg.is_parameter()); | 38 CHECK(reg.is_parameter()); |
40 } else { | 39 } else { |
41 CHECK(!reg.is_parameter()); | 40 CHECK(!reg.is_parameter()); |
42 } | 41 } |
43 } | 42 } |
44 } | 43 } |
45 | 44 |
46 TEST(OperandConversion, Parameters) { | 45 TEST(OperandConversion, Parameters) { |
47 int parameter_counts[] = {7, 13, 99}; | 46 int parameter_counts[] = {7, 13, 99}; |
48 | 47 |
49 size_t count = sizeof(parameter_counts) / sizeof(parameter_counts[0]); | 48 size_t count = sizeof(parameter_counts) / sizeof(parameter_counts[0]); |
50 for (size_t p = 0; p < count; p++) { | 49 for (size_t p = 0; p < count; p++) { |
51 int parameter_count = parameter_counts[p]; | 50 int parameter_count = parameter_counts[p]; |
52 for (int i = 0; i < parameter_count; i++) { | 51 for (int i = 0; i < parameter_count; i++) { |
53 Register r = Register::FromParameterIndex(i, parameter_count); | 52 Register r = Register::FromParameterIndex(i, parameter_count); |
54 uint8_t operand_value = r.ToOperand(); | 53 uint32_t operand_value = r.ToOperand(); |
55 Register s = Register::FromOperand(operand_value); | 54 Register s = Register::FromOperand(operand_value); |
56 CHECK_EQ(i, s.ToParameterIndex(parameter_count)); | 55 CHECK_EQ(i, s.ToParameterIndex(parameter_count)); |
57 } | 56 } |
58 } | 57 } |
59 } | 58 } |
60 | 59 |
61 TEST(OperandConversion, RegistersParametersNoOverlap) { | 60 TEST(OperandConversion, RegistersParametersNoOverlap) { |
62 int register_count = Register::MaxRegisterIndex() + 1; | 61 int register_count = 128; |
63 int parameter_count = Register::MaxParameterIndex() + 1; | 62 int parameter_count = 100; |
64 int32_t register_space_size = base::bits::RoundUpToPowerOfTwo32( | 63 int32_t register_space_size = base::bits::RoundUpToPowerOfTwo32( |
65 static_cast<uint32_t>(register_count + parameter_count)); | 64 static_cast<uint32_t>(register_count + parameter_count)); |
66 uint32_t range = static_cast<uint32_t>(register_space_size); | 65 uint32_t range = static_cast<uint32_t>(register_space_size); |
67 std::vector<uint8_t> operand_count(range); | 66 std::vector<uint8_t> operand_count(range); |
68 | 67 |
69 for (int i = 0; i < register_count; i += 1) { | 68 for (int i = 0; i < register_count; i += 1) { |
70 Register r = Register(i); | 69 Register r = Register(i); |
71 uint32_t operand = r.ToWideOperand(); | 70 int32_t operand = r.ToOperand(); |
72 CHECK_LT(operand, operand_count.size()); | 71 uint8_t index = static_cast<uint8_t>(operand); |
73 operand_count[operand] += 1; | 72 CHECK_LT(index, operand_count.size()); |
74 CHECK_EQ(operand_count[operand], 1); | 73 operand_count[index] += 1; |
| 74 CHECK_EQ(operand_count[index], 1); |
75 } | 75 } |
76 | 76 |
77 for (int i = 0; i < parameter_count; i += 1) { | 77 for (int i = 0; i < parameter_count; i += 1) { |
78 Register r = Register::FromParameterIndex(i, parameter_count); | 78 Register r = Register::FromParameterIndex(i, parameter_count); |
79 uint32_t operand = r.ToWideOperand(); | 79 uint32_t operand = r.ToOperand(); |
80 CHECK_LT(operand, operand_count.size()); | 80 uint8_t index = static_cast<uint8_t>(operand); |
81 operand_count[operand] += 1; | 81 CHECK_LT(index, operand_count.size()); |
82 CHECK_EQ(operand_count[operand], 1); | 82 operand_count[index] += 1; |
| 83 CHECK_EQ(operand_count[index], 1); |
83 } | 84 } |
84 } | 85 } |
85 | 86 |
| 87 TEST(OperandScaling, ScalableAndNonScalable) { |
| 88 for (int scale = 1; scale < 8; scale *= 2) { |
| 89 CHECK_EQ(Bytecodes::Size(Bytecode::kCallRuntime, scale), 1 + 2 + 2 * scale); |
| 90 CHECK_EQ(Bytecodes::Size(Bytecode::kCreateObjectLiteral, scale), |
| 91 1 + 2 * scale + 1); |
| 92 CHECK_EQ(Bytecodes::Size(Bytecode::kTestIn, scale), 1 + scale); |
| 93 } |
| 94 } |
| 95 |
86 TEST(Bytecodes, HasAnyRegisterOperands) { | 96 TEST(Bytecodes, HasAnyRegisterOperands) { |
87 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kAdd), 1); | 97 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kAdd), 1); |
88 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCall), 2); | 98 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCall), 2); |
89 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntime), 1); | 99 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntime), 1); |
90 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeWide), 1); | |
91 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPair), | 100 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPair), |
92 2); | 101 2); |
93 CHECK_EQ( | |
94 Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPairWide), | |
95 2); | |
96 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kDeletePropertyStrict), | 102 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kDeletePropertyStrict), |
97 1); | 103 1); |
98 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepare), 1); | 104 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepare), 1); |
99 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepareWide), 1); | |
100 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kInc), 0); | 105 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kInc), 0); |
101 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kJumpIfTrue), 0); | 106 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kJumpIfTrue), 0); |
102 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kNew), 2); | 107 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kNew), 2); |
103 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kToName), 0); | 108 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kToName), 0); |
104 } | 109 } |
105 | 110 |
106 TEST(Bytecodes, RegisterOperandBitmaps) { | 111 TEST(Bytecodes, RegisterOperandBitmaps) { |
107 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kAdd), 1); | 112 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kAdd), 1); |
108 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kCallRuntimeForPair), | 113 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kCallRuntimeForPair), |
109 10); | 114 10); |
110 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kStar), 1); | 115 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kStar), 1); |
111 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kMov), 3); | 116 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kMov), 3); |
112 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kTestIn), 1); | 117 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kTestIn), 1); |
113 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInPrepare), 1); | 118 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInPrepare), 1); |
114 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInDone), 3); | 119 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInDone), 3); |
115 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInNext), 7); | 120 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInNext), 7); |
116 } | 121 } |
117 | 122 |
118 TEST(Bytecodes, RegisterOperands) { | 123 TEST(Bytecodes, RegisterOperands) { |
119 CHECK(Bytecodes::IsRegisterOperandType(OperandType::kReg8)); | 124 CHECK(Bytecodes::IsRegisterOperandType(OperandType::kReg)); |
120 CHECK(Bytecodes::IsRegisterInputOperandType(OperandType::kReg8)); | 125 CHECK(Bytecodes::IsRegisterInputOperandType(OperandType::kReg)); |
121 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::kReg8)); | 126 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::kReg)); |
122 CHECK(!Bytecodes::IsRegisterInputOperandType(OperandType::kRegOut8)); | 127 CHECK(!Bytecodes::IsRegisterInputOperandType(OperandType::kRegOut)); |
123 CHECK(Bytecodes::IsRegisterOutputOperandType(OperandType::kRegOut8)); | 128 CHECK(Bytecodes::IsRegisterOutputOperandType(OperandType::kRegOut)); |
124 | 129 |
125 #define IS_REGISTER_OPERAND_TYPE(Name, _) \ | 130 #define IS_REGISTER_OPERAND_TYPE(Name, _) \ |
126 CHECK(Bytecodes::IsRegisterOperandType(OperandType::k##Name)); | 131 CHECK(Bytecodes::IsRegisterOperandType(OperandType::k##Name)); |
127 REGISTER_OPERAND_TYPE_LIST(IS_REGISTER_OPERAND_TYPE) | 132 REGISTER_OPERAND_TYPE_LIST(IS_REGISTER_OPERAND_TYPE) |
128 #undef IS_REGISTER_OPERAND_TYPE | 133 #undef IS_REGISTER_OPERAND_TYPE |
129 | 134 |
130 #define IS_NOT_REGISTER_OPERAND_TYPE(Name, _) \ | 135 #define IS_NOT_REGISTER_OPERAND_TYPE(Name, _) \ |
131 CHECK(!Bytecodes::IsRegisterOperandType(OperandType::k##Name)); | 136 CHECK(!Bytecodes::IsRegisterOperandType(OperandType::k##Name)); |
132 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OPERAND_TYPE) | 137 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OPERAND_TYPE) |
133 #undef IS_NOT_REGISTER_OPERAND_TYPE | 138 #undef IS_NOT_REGISTER_OPERAND_TYPE |
(...skipping 14 matching lines...) Expand all Loading... |
148 REGISTER_OUTPUT_OPERAND_TYPE_LIST(IS_REGISTER_OUTPUT_OPERAND_TYPE) | 153 REGISTER_OUTPUT_OPERAND_TYPE_LIST(IS_REGISTER_OUTPUT_OPERAND_TYPE) |
149 #undef IS_REGISTER_OUTPUT_OPERAND_TYPE | 154 #undef IS_REGISTER_OUTPUT_OPERAND_TYPE |
150 | 155 |
151 #define IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE(Name, _) \ | 156 #define IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE(Name, _) \ |
152 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::k##Name)); | 157 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::k##Name)); |
153 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE) | 158 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE) |
154 REGISTER_INPUT_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE) | 159 REGISTER_INPUT_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE) |
155 #undef IS_NOT_REGISTER_INPUT_OPERAND_TYPE | 160 #undef IS_NOT_REGISTER_INPUT_OPERAND_TYPE |
156 } | 161 } |
157 | 162 |
158 TEST(Bytecodes, DebugBreak) { | 163 TEST(Bytecodes, DebugBreakExistForEachBytecode) { |
159 for (uint32_t i = 0; i < Bytecodes::ToByte(Bytecode::kLast); i++) { | 164 for (int operand_scale = 1; operand_scale < 8; operand_scale *= 2) { |
160 Bytecode bytecode = Bytecodes::FromByte(i); | 165 #define CHECK_DEBUG_BREAK_SIZE(Name, ...) \ |
161 Bytecode debugbreak = Bytecodes::GetDebugBreak(bytecode); | 166 { \ |
162 if (!Bytecodes::IsDebugBreak(debugbreak)) { | 167 Bytecode debug_bytecode = \ |
163 PrintF("Bytecode %s has no matching debug break with length %d\n", | 168 Bytecodes::GetDebugBreak(Bytecode::k##Name, operand_scale); \ |
164 Bytecodes::ToString(bytecode), Bytecodes::Size(bytecode)); | 169 CHECK_EQ(Bytecodes::Size(Bytecode::k##Name, operand_scale), \ |
165 CHECK(false); | 170 Bytecodes::Size(debug_bytecode, operand_scale)); \ |
166 } | 171 } |
| 172 BYTECODE_LIST(CHECK_DEBUG_BREAK_SIZE) |
| 173 #undef CHECK_DEBUG_BREAK_SIZE |
167 } | 174 } |
168 } | 175 } |
169 | 176 |
170 } // namespace interpreter | 177 } // namespace interpreter |
171 } // namespace internal | 178 } // namespace internal |
172 } // namespace v8 | 179 } // namespace v8 |
OLD | NEW |