blob: 74a2bb3793dfd8daff54bd7d763ef6e375d6da80 [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"
Ben Murdochc5610432016-08-08 18:44:38 +010010#include "test/unittests/interpreter/bytecode-utils.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "test/unittests/test-utils.h"
12
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013namespace v8 {
14namespace internal {
15namespace interpreter {
16
17TEST(OperandConversion, Registers) {
Ben Murdochda12d292016-06-02 14:46:10 +010018 int register_count = 128;
Ben Murdoch097c5b22016-05-18 11:27:45 +010019 int step = register_count / 7;
20 for (int i = 0; i < register_count; i += step) {
21 if (i <= kMaxInt8) {
Ben Murdochda12d292016-06-02 14:46:10 +010022 uint32_t operand0 = Register(i).ToOperand();
Ben Murdoch097c5b22016-05-18 11:27:45 +010023 Register reg0 = Register::FromOperand(operand0);
24 CHECK_EQ(i, reg0.index());
25 }
26
Ben Murdochda12d292016-06-02 14:46:10 +010027 uint32_t operand1 = Register(i).ToOperand();
28 Register reg1 = Register::FromOperand(operand1);
Ben Murdoch097c5b22016-05-18 11:27:45 +010029 CHECK_EQ(i, reg1.index());
30
Ben Murdochda12d292016-06-02 14:46:10 +010031 uint32_t operand2 = Register(i).ToOperand();
32 Register reg2 = Register::FromOperand(operand2);
Ben Murdoch097c5b22016-05-18 11:27:45 +010033 CHECK_EQ(i, reg2.index());
34 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035}
36
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037TEST(OperandConversion, Parameters) {
38 int parameter_counts[] = {7, 13, 99};
39
40 size_t count = sizeof(parameter_counts) / sizeof(parameter_counts[0]);
41 for (size_t p = 0; p < count; p++) {
42 int parameter_count = parameter_counts[p];
43 for (int i = 0; i < parameter_count; i++) {
44 Register r = Register::FromParameterIndex(i, parameter_count);
Ben Murdochda12d292016-06-02 14:46:10 +010045 uint32_t operand_value = r.ToOperand();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046 Register s = Register::FromOperand(operand_value);
47 CHECK_EQ(i, s.ToParameterIndex(parameter_count));
48 }
49 }
50}
51
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052TEST(OperandConversion, RegistersParametersNoOverlap) {
Ben Murdochda12d292016-06-02 14:46:10 +010053 int register_count = 128;
54 int parameter_count = 100;
Ben Murdoch097c5b22016-05-18 11:27:45 +010055 int32_t register_space_size = base::bits::RoundUpToPowerOfTwo32(
56 static_cast<uint32_t>(register_count + parameter_count));
57 uint32_t range = static_cast<uint32_t>(register_space_size);
58 std::vector<uint8_t> operand_count(range);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059
Ben Murdoch097c5b22016-05-18 11:27:45 +010060 for (int i = 0; i < register_count; i += 1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061 Register r = Register(i);
Ben Murdochda12d292016-06-02 14:46:10 +010062 int32_t operand = r.ToOperand();
63 uint8_t index = static_cast<uint8_t>(operand);
64 CHECK_LT(index, operand_count.size());
65 operand_count[index] += 1;
66 CHECK_EQ(operand_count[index], 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067 }
68
Ben Murdoch097c5b22016-05-18 11:27:45 +010069 for (int i = 0; i < parameter_count; i += 1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 Register r = Register::FromParameterIndex(i, parameter_count);
Ben Murdochda12d292016-06-02 14:46:10 +010071 uint32_t operand = r.ToOperand();
72 uint8_t index = static_cast<uint8_t>(operand);
73 CHECK_LT(index, operand_count.size());
74 operand_count[index] += 1;
75 CHECK_EQ(operand_count[index], 1);
76 }
77}
78
79TEST(OperandScaling, ScalableAndNonScalable) {
Ben Murdochc5610432016-08-08 18:44:38 +010080 const OperandScale kOperandScales[] = {
81#define VALUE(Name, _) OperandScale::k##Name,
82 OPERAND_SCALE_LIST(VALUE)
83#undef VALUE
84 };
85
86 for (OperandScale operand_scale : kOperandScales) {
Ben Murdochda12d292016-06-02 14:46:10 +010087 int scale = static_cast<int>(operand_scale);
88 CHECK_EQ(Bytecodes::Size(Bytecode::kCallRuntime, operand_scale),
89 1 + 2 + 2 * scale);
90 CHECK_EQ(Bytecodes::Size(Bytecode::kCreateObjectLiteral, operand_scale),
91 1 + 2 * scale + 1);
92 CHECK_EQ(Bytecodes::Size(Bytecode::kTestIn, operand_scale), 1 + scale);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093 }
94}
95
Ben Murdoch097c5b22016-05-18 11:27:45 +010096TEST(Bytecodes, HasAnyRegisterOperands) {
97 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kAdd), 1);
98 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCall), 2);
99 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntime), 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100100 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kCallRuntimeForPair),
101 2);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100102 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kDeletePropertyStrict),
103 1);
104 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kForInPrepare), 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100105 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kInc), 0);
106 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kJumpIfTrue), 0);
107 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kNew), 2);
108 CHECK_EQ(Bytecodes::NumberOfRegisterOperands(Bytecode::kToName), 0);
109}
110
111TEST(Bytecodes, RegisterOperandBitmaps) {
112 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kAdd), 1);
113 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kCallRuntimeForPair),
114 10);
115 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kStar), 1);
116 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kMov), 3);
117 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kTestIn), 1);
118 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInPrepare), 1);
119 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInDone), 3);
120 CHECK_EQ(Bytecodes::GetRegisterOperandBitmap(Bytecode::kForInNext), 7);
121}
122
123TEST(Bytecodes, RegisterOperands) {
Ben Murdochda12d292016-06-02 14:46:10 +0100124 CHECK(Bytecodes::IsRegisterOperandType(OperandType::kReg));
125 CHECK(Bytecodes::IsRegisterInputOperandType(OperandType::kReg));
126 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::kReg));
127 CHECK(!Bytecodes::IsRegisterInputOperandType(OperandType::kRegOut));
128 CHECK(Bytecodes::IsRegisterOutputOperandType(OperandType::kRegOut));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100129
130#define IS_REGISTER_OPERAND_TYPE(Name, _) \
131 CHECK(Bytecodes::IsRegisterOperandType(OperandType::k##Name));
132 REGISTER_OPERAND_TYPE_LIST(IS_REGISTER_OPERAND_TYPE)
133#undef IS_REGISTER_OPERAND_TYPE
134
135#define IS_NOT_REGISTER_OPERAND_TYPE(Name, _) \
136 CHECK(!Bytecodes::IsRegisterOperandType(OperandType::k##Name));
137 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OPERAND_TYPE)
138#undef IS_NOT_REGISTER_OPERAND_TYPE
139
140#define IS_REGISTER_INPUT_OPERAND_TYPE(Name, _) \
141 CHECK(Bytecodes::IsRegisterInputOperandType(OperandType::k##Name));
142 REGISTER_INPUT_OPERAND_TYPE_LIST(IS_REGISTER_INPUT_OPERAND_TYPE)
143#undef IS_REGISTER_INPUT_OPERAND_TYPE
144
145#define IS_NOT_REGISTER_INPUT_OPERAND_TYPE(Name, _) \
146 CHECK(!Bytecodes::IsRegisterInputOperandType(OperandType::k##Name));
147 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_INPUT_OPERAND_TYPE);
148 REGISTER_OUTPUT_OPERAND_TYPE_LIST(IS_NOT_REGISTER_INPUT_OPERAND_TYPE)
149#undef IS_NOT_REGISTER_INPUT_OPERAND_TYPE
150
151#define IS_REGISTER_OUTPUT_OPERAND_TYPE(Name, _) \
152 CHECK(Bytecodes::IsRegisterOutputOperandType(OperandType::k##Name));
153 REGISTER_OUTPUT_OPERAND_TYPE_LIST(IS_REGISTER_OUTPUT_OPERAND_TYPE)
154#undef IS_REGISTER_OUTPUT_OPERAND_TYPE
155
156#define IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE(Name, _) \
157 CHECK(!Bytecodes::IsRegisterOutputOperandType(OperandType::k##Name));
158 NON_REGISTER_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE)
159 REGISTER_INPUT_OPERAND_TYPE_LIST(IS_NOT_REGISTER_OUTPUT_OPERAND_TYPE)
160#undef IS_NOT_REGISTER_INPUT_OPERAND_TYPE
161}
162
Ben Murdochda12d292016-06-02 14:46:10 +0100163TEST(Bytecodes, DebugBreakExistForEachBytecode) {
164 static const OperandScale kOperandScale = OperandScale::kSingle;
165#define CHECK_DEBUG_BREAK_SIZE(Name, ...) \
166 if (!Bytecodes::IsDebugBreak(Bytecode::k##Name) && \
167 !Bytecodes::IsPrefixScalingBytecode(Bytecode::k##Name)) { \
168 Bytecode debug_bytecode = Bytecodes::GetDebugBreak(Bytecode::k##Name); \
169 CHECK_EQ(Bytecodes::Size(Bytecode::k##Name, kOperandScale), \
170 Bytecodes::Size(debug_bytecode, kOperandScale)); \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100171 }
Ben Murdochda12d292016-06-02 14:46:10 +0100172 BYTECODE_LIST(CHECK_DEBUG_BREAK_SIZE)
173#undef CHECK_DEBUG_BREAK_SIZE
174}
175
176TEST(Bytecodes, DecodeBytecodeAndOperands) {
177 struct BytecodesAndResult {
178 const uint8_t bytecode[32];
179 const size_t length;
180 int parameter_count;
181 const char* output;
182 };
183
Ben Murdochda12d292016-06-02 14:46:10 +0100184 const BytecodesAndResult cases[] = {
Ben Murdochc5610432016-08-08 18:44:38 +0100185 {{B(LdaSmi), U8(1)}, 2, 0, " LdaSmi [1]"},
186 {{B(Wide), B(LdaSmi), U16(1000)}, 4, 0, " LdaSmi.Wide [1000]"},
187 {{B(ExtraWide), B(LdaSmi), U32(100000)},
Ben Murdochda12d292016-06-02 14:46:10 +0100188 6,
189 0,
190 "LdaSmi.ExtraWide [100000]"},
Ben Murdochc5610432016-08-08 18:44:38 +0100191 {{B(LdaSmi), U8(-1)}, 2, 0, " LdaSmi [-1]"},
192 {{B(Wide), B(LdaSmi), U16(-1000)}, 4, 0, " LdaSmi.Wide [-1000]"},
193 {{B(ExtraWide), B(LdaSmi), U32(-100000)},
Ben Murdochda12d292016-06-02 14:46:10 +0100194 6,
195 0,
196 "LdaSmi.ExtraWide [-100000]"},
Ben Murdochc5610432016-08-08 18:44:38 +0100197 {{B(Star), R8(5)}, 2, 0, " Star r5"},
198 {{B(Wide), B(Star), R16(136)}, 4, 0, " Star.Wide r136"},
199 {{B(Wide), B(Call), R16(134), R16(135), U16(2), U16(177)},
Ben Murdochda12d292016-06-02 14:46:10 +0100200 10,
201 0,
202 "Call.Wide r134, r135, #2, [177]"},
203 {{B(Ldar),
204 static_cast<uint8_t>(Register::FromParameterIndex(2, 3).ToOperand())},
205 2,
206 3,
207 " Ldar a1"},
Ben Murdochc5610432016-08-08 18:44:38 +0100208 {{B(Wide), B(CreateObjectLiteral), U16(513), U16(1027), U8(165)},
Ben Murdochda12d292016-06-02 14:46:10 +0100209 7,
210 0,
211 "CreateObjectLiteral.Wide [513], [1027], #165"},
Ben Murdochc5610432016-08-08 18:44:38 +0100212 {{B(ExtraWide), B(JumpIfNull), U32(123456789)},
Ben Murdochda12d292016-06-02 14:46:10 +0100213 6,
214 0,
215 "JumpIfNull.ExtraWide [123456789]"},
216 };
Ben Murdochda12d292016-06-02 14:46:10 +0100217
218 for (size_t i = 0; i < arraysize(cases); ++i) {
219 // Generate reference string by prepending formatted bytes.
220 std::stringstream expected_ss;
221 std::ios default_format(nullptr);
222 default_format.copyfmt(expected_ss);
223 // Match format of Bytecodes::Decode() for byte representations.
224 expected_ss.fill('0');
225 expected_ss.flags(std::ios::right | std::ios::hex);
226 for (size_t b = 0; b < cases[i].length; b++) {
227 expected_ss << std::setw(2) << static_cast<uint32_t>(cases[i].bytecode[b])
228 << ' ';
229 }
230 expected_ss.copyfmt(default_format);
231 expected_ss << cases[i].output;
232
233 // Generate decoded byte output.
234 std::stringstream actual_ss;
235 Bytecodes::Decode(actual_ss, cases[i].bytecode, cases[i].parameter_count);
236
237 // Compare.
238 CHECK_EQ(actual_ss.str(), expected_ss.str());
239 }
240}
241
242TEST(Bytecodes, DebugBreakForPrefixBytecodes) {
243 CHECK_EQ(Bytecode::kDebugBreakWide,
244 Bytecodes::GetDebugBreak(Bytecode::kWide));
245 CHECK_EQ(Bytecode::kDebugBreakExtraWide,
246 Bytecodes::GetDebugBreak(Bytecode::kExtraWide));
247}
248
249TEST(Bytecodes, PrefixMappings) {
250 Bytecode prefixes[] = {Bytecode::kWide, Bytecode::kExtraWide};
251 TRACED_FOREACH(Bytecode, prefix, prefixes) {
252 CHECK_EQ(prefix, Bytecodes::OperandScaleToPrefixBytecode(
253 Bytecodes::PrefixBytecodeToOperandScale(prefix)));
254 }
255}
256
Ben Murdochc5610432016-08-08 18:44:38 +0100257TEST(Bytecodes, SizesForSignedOperands) {
258 CHECK(Bytecodes::SizeForSignedOperand(0) == OperandSize::kByte);
259 CHECK(Bytecodes::SizeForSignedOperand(kMaxInt8) == OperandSize::kByte);
260 CHECK(Bytecodes::SizeForSignedOperand(kMinInt8) == OperandSize::kByte);
261 CHECK(Bytecodes::SizeForSignedOperand(kMaxInt8 + 1) == OperandSize::kShort);
262 CHECK(Bytecodes::SizeForSignedOperand(kMinInt8 - 1) == OperandSize::kShort);
263 CHECK(Bytecodes::SizeForSignedOperand(kMaxInt16) == OperandSize::kShort);
264 CHECK(Bytecodes::SizeForSignedOperand(kMinInt16) == OperandSize::kShort);
265 CHECK(Bytecodes::SizeForSignedOperand(kMaxInt16 + 1) == OperandSize::kQuad);
266 CHECK(Bytecodes::SizeForSignedOperand(kMinInt16 - 1) == OperandSize::kQuad);
267 CHECK(Bytecodes::SizeForSignedOperand(kMaxInt) == OperandSize::kQuad);
268 CHECK(Bytecodes::SizeForSignedOperand(kMinInt) == OperandSize::kQuad);
269}
270
271TEST(Bytecodes, SizesForUnsignedOperands) {
272 // int overloads
273 CHECK(Bytecodes::SizeForUnsignedOperand(0) == OperandSize::kByte);
274 CHECK(Bytecodes::SizeForUnsignedOperand(kMaxUInt8) == OperandSize::kByte);
275 CHECK(Bytecodes::SizeForUnsignedOperand(kMaxUInt8 + 1) ==
276 OperandSize::kShort);
277 CHECK(Bytecodes::SizeForUnsignedOperand(kMaxUInt16) == OperandSize::kShort);
278 CHECK(Bytecodes::SizeForUnsignedOperand(kMaxUInt16 + 1) ==
279 OperandSize::kQuad);
280 // size_t overloads
281 CHECK(Bytecodes::SizeForUnsignedOperand(static_cast<size_t>(0)) ==
282 OperandSize::kByte);
283 CHECK(Bytecodes::SizeForUnsignedOperand(static_cast<size_t>(kMaxUInt8)) ==
284 OperandSize::kByte);
285 CHECK(Bytecodes::SizeForUnsignedOperand(static_cast<size_t>(kMaxUInt8 + 1)) ==
286 OperandSize::kShort);
287 CHECK(Bytecodes::SizeForUnsignedOperand(static_cast<size_t>(kMaxUInt16)) ==
288 OperandSize::kShort);
289 CHECK(Bytecodes::SizeForUnsignedOperand(
290 static_cast<size_t>(kMaxUInt16 + 1)) == OperandSize::kQuad);
291 CHECK(Bytecodes::SizeForUnsignedOperand(static_cast<size_t>(kMaxUInt32)) ==
292 OperandSize::kQuad);
Ben Murdochda12d292016-06-02 14:46:10 +0100293}
294
295TEST(OperandScale, PrefixesRequired) {
296 CHECK(!Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kSingle));
297 CHECK(Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kDouble));
298 CHECK(
299 Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kQuadruple));
300 CHECK(Bytecodes::OperandScaleToPrefixBytecode(OperandScale::kDouble) ==
301 Bytecode::kWide);
302 CHECK(Bytecodes::OperandScaleToPrefixBytecode(OperandScale::kQuadruple) ==
303 Bytecode::kExtraWide);
304}
305
306TEST(AccumulatorUse, LogicalOperators) {
307 CHECK_EQ(AccumulatorUse::kNone | AccumulatorUse::kRead,
308 AccumulatorUse::kRead);
309 CHECK_EQ(AccumulatorUse::kRead | AccumulatorUse::kWrite,
310 AccumulatorUse::kReadWrite);
311 CHECK_EQ(AccumulatorUse::kRead & AccumulatorUse::kReadWrite,
312 AccumulatorUse::kRead);
313 CHECK_EQ(AccumulatorUse::kRead & AccumulatorUse::kWrite,
314 AccumulatorUse::kNone);
315}
316
317TEST(AccumulatorUse, SampleBytecodes) {
318 CHECK(Bytecodes::ReadsAccumulator(Bytecode::kStar));
319 CHECK(!Bytecodes::WritesAccumulator(Bytecode::kStar));
320 CHECK_EQ(Bytecodes::GetAccumulatorUse(Bytecode::kStar),
321 AccumulatorUse::kRead);
322 CHECK(!Bytecodes::ReadsAccumulator(Bytecode::kLdar));
323 CHECK(Bytecodes::WritesAccumulator(Bytecode::kLdar));
324 CHECK_EQ(Bytecodes::GetAccumulatorUse(Bytecode::kLdar),
325 AccumulatorUse::kWrite);
326 CHECK(Bytecodes::ReadsAccumulator(Bytecode::kAdd));
327 CHECK(Bytecodes::WritesAccumulator(Bytecode::kAdd));
328 CHECK_EQ(Bytecodes::GetAccumulatorUse(Bytecode::kAdd),
329 AccumulatorUse::kReadWrite);
330}
331
332TEST(AccumulatorUse, AccumulatorUseToString) {
333 std::set<std::string> names;
334 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kNone));
335 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kRead));
336 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kWrite));
337 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kReadWrite));
338 CHECK_EQ(names.size(), 4);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100339}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000340} // namespace interpreter
341} // namespace internal
342} // namespace v8