blob: 212e02996b65c98a8674dbd453a60a0ef68fcd26 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
3// found in the LICENSE file.
4
5#include <vector>
6
7#include "src/v8.h"
8
9#include "src/interpreter/bytecodes.h"
10#include "test/unittests/test-utils.h"
11
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012namespace v8 {
13namespace internal {
14namespace interpreter {
15
16TEST(OperandConversion, Registers) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010017 int register_count = Register::MaxRegisterIndex() + 1;
18 int step = register_count / 7;
19 for (int i = 0; i < register_count; i += step) {
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 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 }
44}
45
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046TEST(OperandConversion, Parameters) {
47 int parameter_counts[] = {7, 13, 99};
48
49 size_t count = sizeof(parameter_counts) / sizeof(parameter_counts[0]);
50 for (size_t p = 0; p < count; p++) {
51 int parameter_count = parameter_counts[p];
52 for (int i = 0; i < parameter_count; i++) {
53 Register r = Register::FromParameterIndex(i, parameter_count);
54 uint8_t operand_value = r.ToOperand();
55 Register s = Register::FromOperand(operand_value);
56 CHECK_EQ(i, s.ToParameterIndex(parameter_count));
57 }
58 }
59}
60
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061TEST(OperandConversion, RegistersParametersNoOverlap) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010062 int register_count = Register::MaxRegisterIndex() + 1;
63 int parameter_count = Register::MaxParameterIndex() + 1;
64 int32_t register_space_size = base::bits::RoundUpToPowerOfTwo32(
65 static_cast<uint32_t>(register_count + parameter_count));
66 uint32_t range = static_cast<uint32_t>(register_space_size);
67 std::vector<uint8_t> operand_count(range);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068
Ben Murdoch097c5b22016-05-18 11:27:45 +010069 for (int i = 0; i < register_count; i += 1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 Register r = Register(i);
Ben Murdoch097c5b22016-05-18 11:27:45 +010071 uint32_t operand = r.ToWideOperand();
72 CHECK_LT(operand, operand_count.size());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000073 operand_count[operand] += 1;
74 CHECK_EQ(operand_count[operand], 1);
75 }
76
Ben Murdoch097c5b22016-05-18 11:27:45 +010077 for (int i = 0; i < parameter_count; i += 1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 Register r = Register::FromParameterIndex(i, parameter_count);
Ben Murdoch097c5b22016-05-18 11:27:45 +010079 uint32_t operand = r.ToWideOperand();
80 CHECK_LT(operand, operand_count.size());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 operand_count[operand] += 1;
82 CHECK_EQ(operand_count[operand], 1);
83 }
84}
85
Ben Murdoch097c5b22016-05-18 11:27:45 +010086TEST(Bytecodes, HasAnyRegisterOperands) {
87 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kAdd), 1);
88 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCall), 2);
89 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntime), 1);
90 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeWide), 1);
91 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPair),
92 2);
93 CHECK_EQ(
94 Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPairWide),
95 2);
96 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kDeletePropertyStrict),
97 1);
98 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepare), 1);
99 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepareWide), 1);
100 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kInc), 0);
101 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kJumpIfTrue), 0);
102 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kNew), 2);
103 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kToName), 0);
104}
105
106TEST(Bytecodes, RegisterOperandBitmaps) {
107 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kAdd), 1);
108 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kCallRuntimeForPair),
109 10);
110 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kStar), 1);
111 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kMov), 3);
112 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kTestIn), 1);
113 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInPrepare), 1);
114 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInDone), 3);
115 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInNext), 7);
116}
117
118TEST(Bytecodes, RegisterOperands) {
119 CHECK(Bytecodes::IsRegisterOperandType(OperandType::kReg8));
120 CHECK(Bytecodes::IsRegisterInputOperandType(OperandType::kReg8));
121 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::kReg8));
122 CHECK(!Bytecodes::IsRegisterInputOperandType(OperandType::kRegOut8));
123 CHECK(Bytecodes::IsRegisterOutputOperandType(OperandType::kRegOut8));
124
125#define IS_REGISTER_OPERAND_TYPE(Name, _) \
126 CHECK(Bytecodes::IsRegisterOperandType(OperandType::k##Name));
127 REGISTER_OPERAND_TYPE_LIST(IS_REGISTER_OPERAND_TYPE)
128#undef IS_REGISTER_OPERAND_TYPE
129
130#define IS_NOT_REGISTER_OPERAND_TYPE(Name, _) \
131 CHECK(!Bytecodes::IsRegisterOperandType(OperandType::k##Name));
132 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OPERAND_TYPE)
133#undef IS_NOT_REGISTER_OPERAND_TYPE
134
135#define IS_REGISTER_INPUT_OPERAND_TYPE(Name, _) \
136 CHECK(Bytecodes::IsRegisterInputOperandType(OperandType::k##Name));
137 REGISTER_INPUT_OPERAND_TYPE_LIST(IS_REGISTER_INPUT_OPERAND_TYPE)
138#undef IS_REGISTER_INPUT_OPERAND_TYPE
139
140#define IS_NOT_REGISTER_INPUT_OPERAND_TYPE(Name, _) \
141 CHECK(!Bytecodes::IsRegisterInputOperandType(OperandType::k##Name));
142 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_INPUT_OPERAND_TYPE);
143 REGISTER_OUTPUT_OPERAND_TYPE_LIST(IS_NOT_REGISTER_INPUT_OPERAND_TYPE)
144#undef IS_NOT_REGISTER_INPUT_OPERAND_TYPE
145
146#define IS_REGISTER_OUTPUT_OPERAND_TYPE(Name, _) \
147 CHECK(Bytecodes::IsRegisterOutputOperandType(OperandType::k##Name));
148 REGISTER_OUTPUT_OPERAND_TYPE_LIST(IS_REGISTER_OUTPUT_OPERAND_TYPE)
149#undef IS_REGISTER_OUTPUT_OPERAND_TYPE
150
151#define IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE(Name, _) \
152 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::k##Name));
153 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE)
154 REGISTER_INPUT_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE)
155#undef IS_NOT_REGISTER_INPUT_OPERAND_TYPE
156}
157
158TEST(Bytecodes, DebugBreak) {
159 for (uint32_t i = 0; i < Bytecodes::ToByte(Bytecode::kLast); i++) {
160 Bytecode bytecode = Bytecodes::FromByte(i);
161 Bytecode debugbreak = Bytecodes::GetDebugBreak(bytecode);
162 if (!Bytecodes::IsDebugBreak(debugbreak)) {
163 PrintF("Bytecode %s has no matching debug break with length %d\n",
164 Bytecodes::ToString(bytecode), Bytecodes::Size(bytecode));
165 CHECK(false);
166 }
167 }
168}
169
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170} // namespace interpreter
171} // namespace internal
172} // namespace v8