blob: b3554c385372869de67591df26a31a14b0e6ead8 [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 Murdochda12d292016-06-02 14:46:10 +010017 int register_count = 128;
Ben Murdoch097c5b22016-05-18 11:27:45 +010018 int step = register_count / 7;
19 for (int i = 0; i < register_count; i += step) {
20 if (i <= kMaxInt8) {
Ben Murdochda12d292016-06-02 14:46:10 +010021 uint32_t operand0 = Register(i).ToOperand();
Ben Murdoch097c5b22016-05-18 11:27:45 +010022 Register reg0 = Register::FromOperand(operand0);
23 CHECK_EQ(i, reg0.index());
24 }
25
Ben Murdochda12d292016-06-02 14:46:10 +010026 uint32_t operand1 = Register(i).ToOperand();
27 Register reg1 = Register::FromOperand(operand1);
Ben Murdoch097c5b22016-05-18 11:27:45 +010028 CHECK_EQ(i, reg1.index());
29
Ben Murdochda12d292016-06-02 14:46:10 +010030 uint32_t operand2 = Register(i).ToOperand();
31 Register reg2 = Register::FromOperand(operand2);
Ben Murdoch097c5b22016-05-18 11:27:45 +010032 CHECK_EQ(i, reg2.index());
33 }
34
35 for (int i = 0; i <= kMaxUInt8; i++) {
Ben Murdochda12d292016-06-02 14:46:10 +010036 Register reg = Register::FromOperand(i);
37 if (i > 0) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010038 CHECK(reg.is_parameter());
39 } else {
40 CHECK(!reg.is_parameter());
41 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042 }
43}
44
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045TEST(OperandConversion, Parameters) {
46 int parameter_counts[] = {7, 13, 99};
47
48 size_t count = sizeof(parameter_counts) / sizeof(parameter_counts[0]);
49 for (size_t p = 0; p < count; p++) {
50 int parameter_count = parameter_counts[p];
51 for (int i = 0; i < parameter_count; i++) {
52 Register r = Register::FromParameterIndex(i, parameter_count);
Ben Murdochda12d292016-06-02 14:46:10 +010053 uint32_t operand_value = r.ToOperand();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 Register s = Register::FromOperand(operand_value);
55 CHECK_EQ(i, s.ToParameterIndex(parameter_count));
56 }
57 }
58}
59
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060TEST(OperandConversion, RegistersParametersNoOverlap) {
Ben Murdochda12d292016-06-02 14:46:10 +010061 int register_count = 128;
62 int parameter_count = 100;
Ben Murdoch097c5b22016-05-18 11:27:45 +010063 int32_t register_space_size = base::bits::RoundUpToPowerOfTwo32(
64 static_cast<uint32_t>(register_count + parameter_count));
65 uint32_t range = static_cast<uint32_t>(register_space_size);
66 std::vector<uint8_t> operand_count(range);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067
Ben Murdoch097c5b22016-05-18 11:27:45 +010068 for (int i = 0; i < register_count; i += 1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069 Register r = Register(i);
Ben Murdochda12d292016-06-02 14:46:10 +010070 int32_t operand = r.ToOperand();
71 uint8_t index = static_cast<uint8_t>(operand);
72 CHECK_LT(index, operand_count.size());
73 operand_count[index] += 1;
74 CHECK_EQ(operand_count[index], 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 }
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 Murdochda12d292016-06-02 14:46:10 +010079 uint32_t operand = r.ToOperand();
80 uint8_t index = static_cast<uint8_t>(operand);
81 CHECK_LT(index, operand_count.size());
82 operand_count[index] += 1;
83 CHECK_EQ(operand_count[index], 1);
84 }
85}
86
87TEST(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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 }
98}
99
Ben Murdoch097c5b22016-05-18 11:27:45 +0100100TEST(Bytecodes, HasAnyRegisterOperands) {
101 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kAdd), 1);
102 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCall), 2);
103 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntime), 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100104 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPair),
105 2);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100106 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kDeletePropertyStrict),
107 1);
108 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepare), 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100109 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kInc), 0);
110 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kJumpIfTrue), 0);
111 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kNew), 2);
112 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kToName), 0);
113}
114
115TEST(Bytecodes, RegisterOperandBitmaps) {
116 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kAdd), 1);
117 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kCallRuntimeForPair),
118 10);
119 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kStar), 1);
120 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kMov), 3);
121 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kTestIn), 1);
122 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInPrepare), 1);
123 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInDone), 3);
124 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInNext), 7);
125}
126
127TEST(Bytecodes, RegisterOperands) {
Ben Murdochda12d292016-06-02 14:46:10 +0100128 CHECK(Bytecodes::IsRegisterOperandType(OperandType::kReg));
129 CHECK(Bytecodes::IsRegisterInputOperandType(OperandType::kReg));
130 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::kReg));
131 CHECK(!Bytecodes::IsRegisterInputOperandType(OperandType::kRegOut));
132 CHECK(Bytecodes::IsRegisterOutputOperandType(OperandType::kRegOut));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100133
134#define IS_REGISTER_OPERAND_TYPE(Name, _) \
135 CHECK(Bytecodes::IsRegisterOperandType(OperandType::k##Name));
136 REGISTER_OPERAND_TYPE_LIST(IS_REGISTER_OPERAND_TYPE)
137#undef IS_REGISTER_OPERAND_TYPE
138
139#define IS_NOT_REGISTER_OPERAND_TYPE(Name, _) \
140 CHECK(!Bytecodes::IsRegisterOperandType(OperandType::k##Name));
141 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OPERAND_TYPE)
142#undef IS_NOT_REGISTER_OPERAND_TYPE
143
144#define IS_REGISTER_INPUT_OPERAND_TYPE(Name, _) \
145 CHECK(Bytecodes::IsRegisterInputOperandType(OperandType::k##Name));
146 REGISTER_INPUT_OPERAND_TYPE_LIST(IS_REGISTER_INPUT_OPERAND_TYPE)
147#undef IS_REGISTER_INPUT_OPERAND_TYPE
148
149#define IS_NOT_REGISTER_INPUT_OPERAND_TYPE(Name, _) \
150 CHECK(!Bytecodes::IsRegisterInputOperandType(OperandType::k##Name));
151 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_INPUT_OPERAND_TYPE);
152 REGISTER_OUTPUT_OPERAND_TYPE_LIST(IS_NOT_REGISTER_INPUT_OPERAND_TYPE)
153#undef IS_NOT_REGISTER_INPUT_OPERAND_TYPE
154
155#define IS_REGISTER_OUTPUT_OPERAND_TYPE(Name, _) \
156 CHECK(Bytecodes::IsRegisterOutputOperandType(OperandType::k##Name));
157 REGISTER_OUTPUT_OPERAND_TYPE_LIST(IS_REGISTER_OUTPUT_OPERAND_TYPE)
158#undef IS_REGISTER_OUTPUT_OPERAND_TYPE
159
160#define IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE(Name, _) \
161 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::k##Name));
162 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE)
163 REGISTER_INPUT_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE)
164#undef IS_NOT_REGISTER_INPUT_OPERAND_TYPE
165}
166
Ben Murdochda12d292016-06-02 14:46:10 +0100167TEST(Bytecodes, DebugBreakExistForEachBytecode) {
168 static const OperandScale kOperandScale = OperandScale::kSingle;
169#define CHECK_DEBUG_BREAK_SIZE(Name, ...) \
170 if (!Bytecodes::IsDebugBreak(Bytecode::k##Name) && \
171 !Bytecodes::IsPrefixScalingBytecode(Bytecode::k##Name)) { \
172 Bytecode debug_bytecode = Bytecodes::GetDebugBreak(Bytecode::k##Name); \
173 CHECK_EQ(Bytecodes::Size(Bytecode::k##Name, kOperandScale), \
174 Bytecodes::Size(debug_bytecode, kOperandScale)); \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100175 }
Ben Murdochda12d292016-06-02 14:46:10 +0100176 BYTECODE_LIST(CHECK_DEBUG_BREAK_SIZE)
177#undef CHECK_DEBUG_BREAK_SIZE
178}
179
180TEST(Bytecodes, DecodeBytecodeAndOperands) {
181 struct BytecodesAndResult {
182 const uint8_t bytecode[32];
183 const size_t length;
184 int parameter_count;
185 const char* output;
186 };
187
188#define B(Name) static_cast<uint8_t>(Bytecode::k##Name)
189 const BytecodesAndResult cases[] = {
190 {{B(LdaSmi), 0x01}, 2, 0, " LdaSmi [1]"},
191 {{B(Wide), B(LdaSmi), 0xe8, 0x03}, 4, 0, " LdaSmi.Wide [1000]"},
192 {{B(ExtraWide), B(LdaSmi), 0xa0, 0x86, 0x01, 0x00},
193 6,
194 0,
195 "LdaSmi.ExtraWide [100000]"},
196 {{B(LdaSmi), 0xff}, 2, 0, " LdaSmi [-1]"},
197 {{B(Wide), B(LdaSmi), 0x18, 0xfc}, 4, 0, " LdaSmi.Wide [-1000]"},
198 {{B(ExtraWide), B(LdaSmi), 0x60, 0x79, 0xfe, 0xff},
199 6,
200 0,
201 "LdaSmi.ExtraWide [-100000]"},
202 {{B(Star), 0xfb}, 2, 0, " Star r5"},
203 {{B(Wide), B(Star), 0x78, 0xff}, 4, 0, " Star.Wide r136"},
204 {{B(Wide), B(Call), 0x7a, 0xff, 0x79, 0xff, 0x02, 0x00, 0xb1, 0x00},
205 10,
206 0,
207 "Call.Wide r134, r135, #2, [177]"},
208 {{B(Ldar),
209 static_cast<uint8_t>(Register::FromParameterIndex(2, 3).ToOperand())},
210 2,
211 3,
212 " Ldar a1"},
213 {{B(Wide), B(CreateObjectLiteral), 0x01, 0x02, 0x03, 0x04, 0xa5},
214 7,
215 0,
216 "CreateObjectLiteral.Wide [513], [1027], #165"},
217 {{B(ExtraWide), B(JumpIfNull), 0x15, 0xcd, 0x5b, 0x07},
218 6,
219 0,
220 "JumpIfNull.ExtraWide [123456789]"},
221 };
222#undef B
223
224 for (size_t i = 0; i < arraysize(cases); ++i) {
225 // Generate reference string by prepending formatted bytes.
226 std::stringstream expected_ss;
227 std::ios default_format(nullptr);
228 default_format.copyfmt(expected_ss);
229 // Match format of Bytecodes::Decode() for byte representations.
230 expected_ss.fill('0');
231 expected_ss.flags(std::ios::right | std::ios::hex);
232 for (size_t b = 0; b < cases[i].length; b++) {
233 expected_ss << std::setw(2) << static_cast<uint32_t>(cases[i].bytecode[b])
234 << ' ';
235 }
236 expected_ss.copyfmt(default_format);
237 expected_ss << cases[i].output;
238
239 // Generate decoded byte output.
240 std::stringstream actual_ss;
241 Bytecodes::Decode(actual_ss, cases[i].bytecode, cases[i].parameter_count);
242
243 // Compare.
244 CHECK_EQ(actual_ss.str(), expected_ss.str());
245 }
246}
247
248TEST(Bytecodes, DebugBreakForPrefixBytecodes) {
249 CHECK_EQ(Bytecode::kDebugBreakWide,
250 Bytecodes::GetDebugBreak(Bytecode::kWide));
251 CHECK_EQ(Bytecode::kDebugBreakExtraWide,
252 Bytecodes::GetDebugBreak(Bytecode::kExtraWide));
253}
254
255TEST(Bytecodes, PrefixMappings) {
256 Bytecode prefixes[] = {Bytecode::kWide, Bytecode::kExtraWide};
257 TRACED_FOREACH(Bytecode, prefix, prefixes) {
258 CHECK_EQ(prefix, Bytecodes::OperandScaleToPrefixBytecode(
259 Bytecodes::PrefixBytecodeToOperandScale(prefix)));
260 }
261}
262
263TEST(OperandScale, PrefixesScale) {
264 CHECK(Bytecodes::NextOperandScale(OperandScale::kSingle) ==
265 OperandScale::kDouble);
266 CHECK(Bytecodes::NextOperandScale(OperandScale::kDouble) ==
267 OperandScale::kQuadruple);
268 CHECK(Bytecodes::NextOperandScale(OperandScale::kQuadruple) ==
269 OperandScale::kInvalid);
270}
271
272TEST(OperandScale, PrefixesRequired) {
273 CHECK(!Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kSingle));
274 CHECK(Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kDouble));
275 CHECK(
276 Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kQuadruple));
277 CHECK(Bytecodes::OperandScaleToPrefixBytecode(OperandScale::kDouble) ==
278 Bytecode::kWide);
279 CHECK(Bytecodes::OperandScaleToPrefixBytecode(OperandScale::kQuadruple) ==
280 Bytecode::kExtraWide);
281}
282
283TEST(AccumulatorUse, LogicalOperators) {
284 CHECK_EQ(AccumulatorUse::kNone | AccumulatorUse::kRead,
285 AccumulatorUse::kRead);
286 CHECK_EQ(AccumulatorUse::kRead | AccumulatorUse::kWrite,
287 AccumulatorUse::kReadWrite);
288 CHECK_EQ(AccumulatorUse::kRead & AccumulatorUse::kReadWrite,
289 AccumulatorUse::kRead);
290 CHECK_EQ(AccumulatorUse::kRead & AccumulatorUse::kWrite,
291 AccumulatorUse::kNone);
292}
293
294TEST(AccumulatorUse, SampleBytecodes) {
295 CHECK(Bytecodes::ReadsAccumulator(Bytecode::kStar));
296 CHECK(!Bytecodes::WritesAccumulator(Bytecode::kStar));
297 CHECK_EQ(Bytecodes::GetAccumulatorUse(Bytecode::kStar),
298 AccumulatorUse::kRead);
299 CHECK(!Bytecodes::ReadsAccumulator(Bytecode::kLdar));
300 CHECK(Bytecodes::WritesAccumulator(Bytecode::kLdar));
301 CHECK_EQ(Bytecodes::GetAccumulatorUse(Bytecode::kLdar),
302 AccumulatorUse::kWrite);
303 CHECK(Bytecodes::ReadsAccumulator(Bytecode::kAdd));
304 CHECK(Bytecodes::WritesAccumulator(Bytecode::kAdd));
305 CHECK_EQ(Bytecodes::GetAccumulatorUse(Bytecode::kAdd),
306 AccumulatorUse::kReadWrite);
307}
308
309TEST(AccumulatorUse, AccumulatorUseToString) {
310 std::set<std::string> names;
311 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kNone));
312 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kRead));
313 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kWrite));
314 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kReadWrite));
315 CHECK_EQ(names.size(), 4);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100316}
317
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000318} // namespace interpreter
319} // namespace internal
320} // namespace v8