| 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 (OperandScale operand_scale = OperandScale::kSingle; |
| 89 operand_scale <= OperandScale::kMaxValid; |
| 90 operand_scale = Bytecodes::NextOperandScale(operand_scale)) { |
| 91 int scale = static_cast<int>(operand_scale); |
| 92 CHECK_EQ(Bytecodes::Size(Bytecode::kCallRuntime, operand_scale), |
| 93 1 + 2 + 2 * scale); |
| 94 CHECK_EQ(Bytecodes::Size(Bytecode::kCreateObjectLiteral, operand_scale), |
| 95 1 + 2 * scale + 1); |
| 96 CHECK_EQ(Bytecodes::Size(Bytecode::kTestIn, operand_scale), 1 + scale); |
| 97 } |
| 98 } |
| 99 |
| 86 TEST(Bytecodes, HasAnyRegisterOperands) { | 100 TEST(Bytecodes, HasAnyRegisterOperands) { |
| 87 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kAdd), 1); | 101 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kAdd), 1); |
| 88 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCall), 2); | 102 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCall), 2); |
| 89 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntime), 1); | 103 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntime), 1); |
| 90 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeWide), 1); | |
| 91 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPair), | 104 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPair), |
| 92 2); | 105 2); |
| 93 CHECK_EQ( | |
| 94 Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPairWide), | |
| 95 2); | |
| 96 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kDeletePropertyStrict), | 106 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kDeletePropertyStrict), |
| 97 1); | 107 1); |
| 98 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepare), 1); | 108 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepare), 1); |
| 99 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepareWide), 1); | |
| 100 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kInc), 0); | 109 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kInc), 0); |
| 101 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kJumpIfTrue), 0); | 110 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kJumpIfTrue), 0); |
| 102 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kNew), 2); | 111 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kNew), 2); |
| 103 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kToName), 0); | 112 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kToName), 0); |
| 104 } | 113 } |
| 105 | 114 |
| 106 TEST(Bytecodes, RegisterOperandBitmaps) { | 115 TEST(Bytecodes, RegisterOperandBitmaps) { |
| 107 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kAdd), 1); | 116 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kAdd), 1); |
| 108 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kCallRuntimeForPair), | 117 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kCallRuntimeForPair), |
| 109 10); | 118 10); |
| 110 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kStar), 1); | 119 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kStar), 1); |
| 111 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kMov), 3); | 120 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kMov), 3); |
| 112 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kTestIn), 1); | 121 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kTestIn), 1); |
| 113 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInPrepare), 1); | 122 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInPrepare), 1); |
| 114 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInDone), 3); | 123 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInDone), 3); |
| 115 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInNext), 7); | 124 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInNext), 7); |
| 116 } | 125 } |
| 117 | 126 |
| 118 TEST(Bytecodes, RegisterOperands) { | 127 TEST(Bytecodes, RegisterOperands) { |
| 119 CHECK(Bytecodes::IsRegisterOperandType(OperandType::kReg8)); | 128 CHECK(Bytecodes::IsRegisterOperandType(OperandType::kReg)); |
| 120 CHECK(Bytecodes::IsRegisterInputOperandType(OperandType::kReg8)); | 129 CHECK(Bytecodes::IsRegisterInputOperandType(OperandType::kReg)); |
| 121 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::kReg8)); | 130 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::kReg)); |
| 122 CHECK(!Bytecodes::IsRegisterInputOperandType(OperandType::kRegOut8)); | 131 CHECK(!Bytecodes::IsRegisterInputOperandType(OperandType::kRegOut)); |
| 123 CHECK(Bytecodes::IsRegisterOutputOperandType(OperandType::kRegOut8)); | 132 CHECK(Bytecodes::IsRegisterOutputOperandType(OperandType::kRegOut)); |
| 124 | 133 |
| 125 #define IS_REGISTER_OPERAND_TYPE(Name, _) \ | 134 #define IS_REGISTER_OPERAND_TYPE(Name, _) \ |
| 126 CHECK(Bytecodes::IsRegisterOperandType(OperandType::k##Name)); | 135 CHECK(Bytecodes::IsRegisterOperandType(OperandType::k##Name)); |
| 127 REGISTER_OPERAND_TYPE_LIST(IS_REGISTER_OPERAND_TYPE) | 136 REGISTER_OPERAND_TYPE_LIST(IS_REGISTER_OPERAND_TYPE) |
| 128 #undef IS_REGISTER_OPERAND_TYPE | 137 #undef IS_REGISTER_OPERAND_TYPE |
| 129 | 138 |
| 130 #define IS_NOT_REGISTER_OPERAND_TYPE(Name, _) \ | 139 #define IS_NOT_REGISTER_OPERAND_TYPE(Name, _) \ |
| 131 CHECK(!Bytecodes::IsRegisterOperandType(OperandType::k##Name)); | 140 CHECK(!Bytecodes::IsRegisterOperandType(OperandType::k##Name)); |
| 132 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OPERAND_TYPE) | 141 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OPERAND_TYPE) |
| 133 #undef IS_NOT_REGISTER_OPERAND_TYPE | 142 #undef IS_NOT_REGISTER_OPERAND_TYPE |
| (...skipping 14 matching lines...) Expand all Loading... |
| 148 REGISTER_OUTPUT_OPERAND_TYPE_LIST(IS_REGISTER_OUTPUT_OPERAND_TYPE) | 157 REGISTER_OUTPUT_OPERAND_TYPE_LIST(IS_REGISTER_OUTPUT_OPERAND_TYPE) |
| 149 #undef IS_REGISTER_OUTPUT_OPERAND_TYPE | 158 #undef IS_REGISTER_OUTPUT_OPERAND_TYPE |
| 150 | 159 |
| 151 #define IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE(Name, _) \ | 160 #define IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE(Name, _) \ |
| 152 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::k##Name)); | 161 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::k##Name)); |
| 153 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE) | 162 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE) |
| 154 REGISTER_INPUT_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE) | 163 REGISTER_INPUT_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE) |
| 155 #undef IS_NOT_REGISTER_INPUT_OPERAND_TYPE | 164 #undef IS_NOT_REGISTER_INPUT_OPERAND_TYPE |
| 156 } | 165 } |
| 157 | 166 |
| 158 TEST(Bytecodes, DebugBreak) { | 167 TEST(Bytecodes, DebugBreakExistForEachBytecode) { |
| 159 for (uint32_t i = 0; i < Bytecodes::ToByte(Bytecode::kLast); i++) { | 168 static const OperandScale kOperandScale = OperandScale::kSingle; |
| 160 Bytecode bytecode = Bytecodes::FromByte(i); | 169 #define CHECK_DEBUG_BREAK_SIZE(Name, ...) \ |
| 161 Bytecode debugbreak = Bytecodes::GetDebugBreak(bytecode); | 170 if (!Bytecodes::IsDebugBreak(Bytecode::k##Name) && \ |
| 162 if (!Bytecodes::IsDebugBreak(debugbreak)) { | 171 !Bytecodes::IsPrefixScalingBytecode(Bytecode::k##Name)) { \ |
| 163 PrintF("Bytecode %s has no matching debug break with length %d\n", | 172 Bytecode debug_bytecode = Bytecodes::GetDebugBreak(Bytecode::k##Name); \ |
| 164 Bytecodes::ToString(bytecode), Bytecodes::Size(bytecode)); | 173 CHECK_EQ(Bytecodes::Size(Bytecode::k##Name, kOperandScale), \ |
| 165 CHECK(false); | 174 Bytecodes::Size(debug_bytecode, kOperandScale)); \ |
| 166 } | 175 } |
| 176 BYTECODE_LIST(CHECK_DEBUG_BREAK_SIZE) |
| 177 #undef CHECK_DEBUG_BREAK_SIZE |
| 178 } |
| 179 |
| 180 TEST(Bytecodes, DebugBreakForPrefixBytecodes) { |
| 181 CHECK_EQ(Bytecode::kDebugBreakWide, |
| 182 Bytecodes::GetDebugBreak(Bytecode::kWide)); |
| 183 CHECK_EQ(Bytecode::kDebugBreakExtraWide, |
| 184 Bytecodes::GetDebugBreak(Bytecode::kExtraWide)); |
| 185 } |
| 186 |
| 187 TEST(Bytecodes, PrefixMappings) { |
| 188 Bytecode prefixes[] = {Bytecode::kWide, Bytecode::kExtraWide}; |
| 189 TRACED_FOREACH(Bytecode, prefix, prefixes) { |
| 190 CHECK_EQ(prefix, Bytecodes::OperandScaleToPrefixBytecode( |
| 191 Bytecodes::PrefixBytecodeToOperandScale(prefix))); |
| 167 } | 192 } |
| 168 } | 193 } |
| 169 | 194 |
| 195 TEST(OperandScale, PrefixesScale) { |
| 196 CHECK(Bytecodes::NextOperandScale(OperandScale::kSingle) == |
| 197 OperandScale::kDouble); |
| 198 CHECK(Bytecodes::NextOperandScale(OperandScale::kDouble) == |
| 199 OperandScale::kQuadruple); |
| 200 CHECK(Bytecodes::NextOperandScale(OperandScale::kQuadruple) == |
| 201 OperandScale::kInvalid); |
| 202 } |
| 203 |
| 204 TEST(OperandScale, PrefixesRequired) { |
| 205 CHECK(!Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kSingle)); |
| 206 CHECK(Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kDouble)); |
| 207 CHECK( |
| 208 Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kQuadruple)); |
| 209 CHECK(Bytecodes::OperandScaleToPrefixBytecode(OperandScale::kDouble) == |
| 210 Bytecode::kWide); |
| 211 CHECK(Bytecodes::OperandScaleToPrefixBytecode(OperandScale::kQuadruple) == |
| 212 Bytecode::kExtraWide); |
| 213 } |
| 214 |
| 170 } // namespace interpreter | 215 } // namespace interpreter |
| 171 } // namespace internal | 216 } // namespace internal |
| 172 } // namespace v8 | 217 } // namespace v8 |
| OLD | NEW |