Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(569)

Side by Side Diff: test/unittests/interpreter/bytecodes-unittest.cc

Issue 1947403002: [interpreter] Introduce bytecode generation pipeline. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/interpreter/bytecode-utils.h"
10 #include "test/unittests/test-utils.h" 11 #include "test/unittests/test-utils.h"
11 12
12 namespace v8 { 13 namespace v8 {
13 namespace internal { 14 namespace internal {
14 namespace interpreter { 15 namespace interpreter {
15 16
16 TEST(OperandConversion, Registers) { 17 TEST(OperandConversion, Registers) {
17 int register_count = 128; 18 int register_count = 128;
18 int step = register_count / 7; 19 int step = register_count / 7;
19 for (int i = 0; i < register_count; i += step) { 20 for (int i = 0; i < register_count; i += step) {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 Register r = Register::FromParameterIndex(i, parameter_count); 70 Register r = Register::FromParameterIndex(i, parameter_count);
70 uint32_t operand = r.ToOperand(); 71 uint32_t operand = r.ToOperand();
71 uint8_t index = static_cast<uint8_t>(operand); 72 uint8_t index = static_cast<uint8_t>(operand);
72 CHECK_LT(index, operand_count.size()); 73 CHECK_LT(index, operand_count.size());
73 operand_count[index] += 1; 74 operand_count[index] += 1;
74 CHECK_EQ(operand_count[index], 1); 75 CHECK_EQ(operand_count[index], 1);
75 } 76 }
76 } 77 }
77 78
78 TEST(OperandScaling, ScalableAndNonScalable) { 79 TEST(OperandScaling, ScalableAndNonScalable) {
79 for (OperandScale operand_scale = OperandScale::kSingle; 80 const OperandScale kOperandScales[] = {
80 operand_scale <= OperandScale::kMaxValid; 81 #define VALUE(Name, _) OperandScale::k##Name,
81 operand_scale = Bytecodes::NextOperandScale(operand_scale)) { 82 OPERAND_SCALE_LIST(VALUE)
83 #undef VALUE
84 };
85
86 for (OperandScale operand_scale : kOperandScales) {
82 int scale = static_cast<int>(operand_scale); 87 int scale = static_cast<int>(operand_scale);
83 CHECK_EQ(Bytecodes::Size(Bytecode::kCallRuntime, operand_scale), 88 CHECK_EQ(Bytecodes::Size(Bytecode::kCallRuntime, operand_scale),
84 1 + 2 + 2 * scale); 89 1 + 2 + 2 * scale);
85 CHECK_EQ(Bytecodes::Size(Bytecode::kCreateObjectLiteral, operand_scale), 90 CHECK_EQ(Bytecodes::Size(Bytecode::kCreateObjectLiteral, operand_scale),
86 1 + 2 * scale + 1); 91 1 + 2 * scale + 1);
87 CHECK_EQ(Bytecodes::Size(Bytecode::kTestIn, operand_scale), 1 + scale); 92 CHECK_EQ(Bytecodes::Size(Bytecode::kTestIn, operand_scale), 1 + scale);
88 } 93 }
89 } 94 }
90 95
91 TEST(Bytecodes, HasAnyRegisterOperands) { 96 TEST(Bytecodes, HasAnyRegisterOperands) {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 } 174 }
170 175
171 TEST(Bytecodes, DecodeBytecodeAndOperands) { 176 TEST(Bytecodes, DecodeBytecodeAndOperands) {
172 struct BytecodesAndResult { 177 struct BytecodesAndResult {
173 const uint8_t bytecode[32]; 178 const uint8_t bytecode[32];
174 const size_t length; 179 const size_t length;
175 int parameter_count; 180 int parameter_count;
176 const char* output; 181 const char* output;
177 }; 182 };
178 183
179 #define B(Name) static_cast<uint8_t>(Bytecode::k##Name)
180 #define REG_OPERAND(i) \
181 (InterpreterFrameConstants::kRegisterFileFromFp / kPointerSize - (i))
182 #define REG8(i) static_cast<uint8_t>(REG_OPERAND(i))
183 #if V8_TARGET_LITTLE_ENDIAN
184 #define REG16(i) \
185 static_cast<uint8_t>(REG_OPERAND(i)), \
186 static_cast<uint8_t>(REG_OPERAND(i) >> 8)
187 #elif V8_TARGET_BIG_ENDIAN
188 #define REG16(i) \
189 static_cast<uint8_t>(REG_OPERAND(i) >> 8), \
190 static_cast<uint8_t>(REG_OPERAND(i))
191 #else
192 #error "Unknown Architecture"
193 #endif
194 const BytecodesAndResult cases[] = { 184 const BytecodesAndResult cases[] = {
195 #if V8_TARGET_LITTLE_ENDIAN 185 {{B(LdaSmi), U8(0x01)}, 2, 0, " LdaSmi [1]"},
196 {{B(LdaSmi), 0x01}, 2, 0, " LdaSmi [1]"}, 186 {{B(Wide), B(LdaSmi), U16(1000)}, 4, 0, " LdaSmi.Wide [1000]"},
197 {{B(Wide), B(LdaSmi), 0xe8, 0x03}, 4, 0, " LdaSmi.Wide [1000]"}, 187 {{B(ExtraWide), B(LdaSmi), U32(100000)},
198 {{B(ExtraWide), B(LdaSmi), 0xa0, 0x86, 0x01, 0x00}, 188 6,
199 6, 189 0,
200 0, 190 "LdaSmi.ExtraWide [100000]"},
201 "LdaSmi.ExtraWide [100000]"}, 191 {{B(LdaSmi), 0xff}, 2, 0, " LdaSmi [-1]"},
202 {{B(LdaSmi), 0xff}, 2, 0, " LdaSmi [-1]"}, 192 {{B(Wide), B(LdaSmi), 0x18, 0xfc}, 4, 0, " LdaSmi.Wide [-1000]"},
203 {{B(Wide), B(LdaSmi), 0x18, 0xfc}, 4, 0, " LdaSmi.Wide [-1000]"}, 193 {{B(ExtraWide), B(LdaSmi), U32(-100000)},
204 {{B(ExtraWide), B(LdaSmi), 0x60, 0x79, 0xfe, 0xff}, 194 6,
205 6, 195 0,
206 0, 196 "LdaSmi.ExtraWide [-100000]"},
207 "LdaSmi.ExtraWide [-100000]"}, 197 {{B(Star), R8(5)}, 2, 0, " Star r5"},
208 {{B(Star), REG8(5)}, 2, 0, " Star r5"}, 198 {{B(Wide), B(Star), R16(136)}, 4, 0, " Star.Wide r136"},
209 {{B(Wide), B(Star), REG16(136)}, 4, 0, " Star.Wide r136"}, 199 {{B(Wide), B(Call), R16(134), R16(135), U16(0x02), U16(177)},
210 {{B(Wide), B(Call), REG16(134), REG16(135), 0x02, 0x00, 0xb1, 0x00}, 200 10,
211 10, 201 0,
212 0, 202 "Call.Wide r134, r135, #2, [177]"},
213 "Call.Wide r134, r135, #2, [177]"}, 203 {{B(Ldar),
214 {{B(Ldar), 204 static_cast<uint8_t>(Register::FromParameterIndex(2, 3).ToOperand())},
215 static_cast<uint8_t>(Register::FromParameterIndex(2, 3).ToOperand())}, 205 2,
216 2, 206 3,
217 3, 207 " Ldar a1"},
218 " Ldar a1"}, 208 {{B(Wide), B(CreateObjectLiteral), U16(513), U16(1027), U16(165)},
219 {{B(Wide), B(CreateObjectLiteral), 0x01, 0x02, 0x03, 0x04, 0xa5}, 209 7,
220 7, 210 0,
221 0, 211 "CreateObjectLiteral.Wide [513], [1027], #165"},
222 "CreateObjectLiteral.Wide [513], [1027], #165"}, 212 {{B(ExtraWide), B(JumpIfNull), U32(123456789)},
223 {{B(ExtraWide), B(JumpIfNull), 0x15, 0xcd, 0x5b, 0x07}, 213 6,
224 6, 214 0,
225 0, 215 "JumpIfNull.ExtraWide [123456789]"},
226 "JumpIfNull.ExtraWide [123456789]"},
227 #elif V8_TARGET_BIG_ENDIAN
228 {{B(LdaSmi), 0x01}, 2, 0, " LdaSmi [1]"},
229 {{B(Wide), B(LdaSmi), 0x03, 0xe8}, 4, 0, " LdaSmi.Wide [1000]"},
230 {{B(ExtraWide), B(LdaSmi), 0x00, 0x01, 0x86, 0xa0},
231 6,
232 0,
233 "LdaSmi.ExtraWide [100000]"},
234 {{B(LdaSmi), 0xff}, 2, 0, " LdaSmi [-1]"},
235 {{B(Wide), B(LdaSmi), 0xfc, 0x18}, 4, 0, " LdaSmi.Wide [-1000]"},
236 {{B(ExtraWide), B(LdaSmi), 0xff, 0xfe, 0x79, 0x60},
237 6,
238 0,
239 "LdaSmi.ExtraWide [-100000]"},
240 {{B(Star), REG8(5)}, 2, 0, " Star r5"},
241 {{B(Wide), B(Star), REG16(136)}, 4, 0, " Star.Wide r136"},
242 {{B(Wide), B(Call), REG16(134), REG16(135), 0x00, 0x02, 0x00, 0xb1},
243 10,
244 0,
245 "Call.Wide r134, r135, #2, [177]"},
246 {{B(Ldar),
247 static_cast<uint8_t>(Register::FromParameterIndex(2, 3).ToOperand())},
248 2,
249 3,
250 " Ldar a1"},
251 {{B(Wide), B(CreateObjectLiteral), 0x02, 0x01, 0x04, 0x03, 0xa5},
252 7,
253 0,
254 "CreateObjectLiteral.Wide [513], [1027], #165"},
255 {{B(ExtraWide), B(JumpIfNull), 0x07, 0x5b, 0xcd, 0x15},
256 6,
257 0,
258 "JumpIfNull.ExtraWide [123456789]"},
259 #else
260 #error "Unknown Architecture"
261 #endif
262 }; 216 };
263 #undef B
264 #undef REG_OPERAND
265 #undef REG8
266 #undef REG16
267 217
268 for (size_t i = 0; i < arraysize(cases); ++i) { 218 for (size_t i = 0; i < arraysize(cases); ++i) {
269 // Generate reference string by prepending formatted bytes. 219 // Generate reference string by prepending formatted bytes.
270 std::stringstream expected_ss; 220 std::stringstream expected_ss;
271 std::ios default_format(nullptr); 221 std::ios default_format(nullptr);
272 default_format.copyfmt(expected_ss); 222 default_format.copyfmt(expected_ss);
273 // Match format of Bytecodes::Decode() for byte representations. 223 // Match format of Bytecodes::Decode() for byte representations.
274 expected_ss.fill('0'); 224 expected_ss.fill('0');
275 expected_ss.flags(std::ios::right | std::ios::hex); 225 expected_ss.flags(std::ios::right | std::ios::hex);
276 for (size_t b = 0; b < cases[i].length; b++) { 226 for (size_t b = 0; b < cases[i].length; b++) {
(...skipping 20 matching lines...) Expand all
297 } 247 }
298 248
299 TEST(Bytecodes, PrefixMappings) { 249 TEST(Bytecodes, PrefixMappings) {
300 Bytecode prefixes[] = {Bytecode::kWide, Bytecode::kExtraWide}; 250 Bytecode prefixes[] = {Bytecode::kWide, Bytecode::kExtraWide};
301 TRACED_FOREACH(Bytecode, prefix, prefixes) { 251 TRACED_FOREACH(Bytecode, prefix, prefixes) {
302 CHECK_EQ(prefix, Bytecodes::OperandScaleToPrefixBytecode( 252 CHECK_EQ(prefix, Bytecodes::OperandScaleToPrefixBytecode(
303 Bytecodes::PrefixBytecodeToOperandScale(prefix))); 253 Bytecodes::PrefixBytecodeToOperandScale(prefix)));
304 } 254 }
305 } 255 }
306 256
307 TEST(OperandScale, PrefixesScale) { 257 TEST(Bytecodes, OperandScales) {
308 CHECK(Bytecodes::NextOperandScale(OperandScale::kSingle) == 258 CHECK_EQ(Bytecodes::OperandSizesToScale(OperandSize::kByte),
309 OperandScale::kDouble); 259 OperandScale::kSingle);
310 CHECK(Bytecodes::NextOperandScale(OperandScale::kDouble) == 260 CHECK_EQ(Bytecodes::OperandSizesToScale(OperandSize::kShort),
311 OperandScale::kQuadruple); 261 OperandScale::kDouble);
312 CHECK(Bytecodes::NextOperandScale(OperandScale::kQuadruple) == 262 CHECK_EQ(Bytecodes::OperandSizesToScale(OperandSize::kQuad),
313 OperandScale::kInvalid); 263 OperandScale::kQuadruple);
264 CHECK_EQ(
265 Bytecodes::OperandSizesToScale(OperandSize::kShort, OperandSize::kShort,
266 OperandSize::kShort, OperandSize::kShort),
267 OperandScale::kDouble);
268 CHECK_EQ(
269 Bytecodes::OperandSizesToScale(OperandSize::kQuad, OperandSize::kShort,
270 OperandSize::kShort, OperandSize::kShort),
271 OperandScale::kQuadruple);
272 CHECK_EQ(
273 Bytecodes::OperandSizesToScale(OperandSize::kShort, OperandSize::kQuad,
274 OperandSize::kShort, OperandSize::kShort),
275 OperandScale::kQuadruple);
276 CHECK_EQ(
277 Bytecodes::OperandSizesToScale(OperandSize::kShort, OperandSize::kShort,
278 OperandSize::kQuad, OperandSize::kShort),
279 OperandScale::kQuadruple);
280 CHECK_EQ(
281 Bytecodes::OperandSizesToScale(OperandSize::kShort, OperandSize::kShort,
282 OperandSize::kShort, OperandSize::kQuad),
283 OperandScale::kQuadruple);
284 }
285
286 TEST(Bytecodes, SizesForSignedOperands) {
287 CHECK(Bytecodes::SizeForSignedOperand(0) == OperandSize::kByte);
288 CHECK(Bytecodes::SizeForSignedOperand(kMaxInt8) == OperandSize::kByte);
289 CHECK(Bytecodes::SizeForSignedOperand(kMinInt8) == OperandSize::kByte);
290 CHECK(Bytecodes::SizeForSignedOperand(kMaxInt8 + 1) == OperandSize::kShort);
291 CHECK(Bytecodes::SizeForSignedOperand(kMinInt8 - 1) == OperandSize::kShort);
292 CHECK(Bytecodes::SizeForSignedOperand(kMaxInt16) == OperandSize::kShort);
293 CHECK(Bytecodes::SizeForSignedOperand(kMinInt16) == OperandSize::kShort);
294 CHECK(Bytecodes::SizeForSignedOperand(kMaxInt16 + 1) == OperandSize::kQuad);
295 CHECK(Bytecodes::SizeForSignedOperand(kMinInt16 - 1) == OperandSize::kQuad);
296 CHECK(Bytecodes::SizeForSignedOperand(kMaxInt) == OperandSize::kQuad);
297 CHECK(Bytecodes::SizeForSignedOperand(kMinInt) == OperandSize::kQuad);
298 }
299
300 TEST(Bytecodes, SizesForUnsignedOperands) {
301 // int overloads
302 CHECK(Bytecodes::SizeForUnsignedOperand(0) == OperandSize::kByte);
303 CHECK(Bytecodes::SizeForUnsignedOperand(kMaxUInt8) == OperandSize::kByte);
304 CHECK(Bytecodes::SizeForUnsignedOperand(kMaxUInt8 + 1) ==
305 OperandSize::kShort);
306 CHECK(Bytecodes::SizeForUnsignedOperand(kMaxUInt16) == OperandSize::kShort);
307 CHECK(Bytecodes::SizeForUnsignedOperand(kMaxUInt16 + 1) ==
308 OperandSize::kQuad);
309 // size_t overloads
310 CHECK(Bytecodes::SizeForUnsignedOperand(static_cast<size_t>(0)) ==
311 OperandSize::kByte);
312 CHECK(Bytecodes::SizeForUnsignedOperand(static_cast<size_t>(kMaxUInt8)) ==
313 OperandSize::kByte);
314 CHECK(Bytecodes::SizeForUnsignedOperand(static_cast<size_t>(kMaxUInt8 + 1)) ==
315 OperandSize::kShort);
316 CHECK(Bytecodes::SizeForUnsignedOperand(static_cast<size_t>(kMaxUInt16)) ==
317 OperandSize::kShort);
318 CHECK(Bytecodes::SizeForUnsignedOperand(
319 static_cast<size_t>(kMaxUInt16 + 1)) == OperandSize::kQuad);
320 CHECK(Bytecodes::SizeForUnsignedOperand(static_cast<size_t>(kMaxUInt32)) ==
321 OperandSize::kQuad);
314 } 322 }
315 323
316 TEST(OperandScale, PrefixesRequired) { 324 TEST(OperandScale, PrefixesRequired) {
317 CHECK(!Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kSingle)); 325 CHECK(!Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kSingle));
318 CHECK(Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kDouble)); 326 CHECK(Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kDouble));
319 CHECK( 327 CHECK(
320 Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kQuadruple)); 328 Bytecodes::OperandScaleRequiresPrefixBytecode(OperandScale::kQuadruple));
321 CHECK(Bytecodes::OperandScaleToPrefixBytecode(OperandScale::kDouble) == 329 CHECK(Bytecodes::OperandScaleToPrefixBytecode(OperandScale::kDouble) ==
322 Bytecode::kWide); 330 Bytecode::kWide);
323 CHECK(Bytecodes::OperandScaleToPrefixBytecode(OperandScale::kQuadruple) == 331 CHECK(Bytecodes::OperandScaleToPrefixBytecode(OperandScale::kQuadruple) ==
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kNone)); 363 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kNone));
356 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kRead)); 364 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kRead));
357 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kWrite)); 365 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kWrite));
358 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kReadWrite)); 366 names.insert(Bytecodes::AccumulatorUseToString(AccumulatorUse::kReadWrite));
359 CHECK_EQ(names.size(), 4); 367 CHECK_EQ(names.size(), 4);
360 } 368 }
361 369
362 } // namespace interpreter 370 } // namespace interpreter
363 } // namespace internal 371 } // namespace internal
364 } // namespace v8 372 } // namespace v8
OLDNEW
« no previous file with comments | « test/unittests/interpreter/bytecode-utils.h ('k') | test/unittests/interpreter/source-position-table-unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698