OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/interpreter/bytecode-array-builder.h" | 7 #include "src/interpreter/bytecode-array-builder.h" |
8 #include "src/interpreter/bytecode-array-iterator.h" | 8 #include "src/interpreter/bytecode-array-iterator.h" |
9 #include "test/unittests/test-utils.h" | 9 #include "test/unittests/test-utils.h" |
10 | 10 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 builder.Jump(&start).JumpIfTrue(&start).JumpIfFalse(&start); | 87 builder.Jump(&start).JumpIfTrue(&start).JumpIfFalse(&start); |
88 builder.Return(); | 88 builder.Return(); |
89 | 89 |
90 // Generate BytecodeArray. | 90 // Generate BytecodeArray. |
91 Handle<BytecodeArray> the_array = builder.ToBytecodeArray(); | 91 Handle<BytecodeArray> the_array = builder.ToBytecodeArray(); |
92 CHECK_EQ(the_array->frame_size(), builder.locals_count() * kPointerSize); | 92 CHECK_EQ(the_array->frame_size(), builder.locals_count() * kPointerSize); |
93 | 93 |
94 // Build scorecard of bytecodes encountered in the BytecodeArray. | 94 // Build scorecard of bytecodes encountered in the BytecodeArray. |
95 std::vector<int> scorecard(Bytecodes::ToByte(Bytecode::kLast) + 1); | 95 std::vector<int> scorecard(Bytecodes::ToByte(Bytecode::kLast) + 1); |
96 Bytecode final_bytecode = Bytecode::kLdaZero; | 96 Bytecode final_bytecode = Bytecode::kLdaZero; |
97 for (int i = 0; i < the_array->length(); i++) { | 97 int i = 0; |
| 98 while (i < the_array->length()) { |
98 uint8_t code = the_array->get(i); | 99 uint8_t code = the_array->get(i); |
99 scorecard[code] += 1; | 100 scorecard[code] += 1; |
100 int operands = Bytecodes::NumberOfOperands(Bytecodes::FromByte(code)); | |
101 CHECK_LE(operands, Bytecodes::MaximumNumberOfOperands()); | |
102 final_bytecode = Bytecodes::FromByte(code); | 101 final_bytecode = Bytecodes::FromByte(code); |
103 i += operands; | 102 i += Bytecodes::Size(Bytecodes::FromByte(code)); |
104 } | 103 } |
105 | 104 |
106 // Check return occurs at the end and only once in the BytecodeArray. | 105 // Check return occurs at the end and only once in the BytecodeArray. |
107 CHECK_EQ(final_bytecode, Bytecode::kReturn); | 106 CHECK_EQ(final_bytecode, Bytecode::kReturn); |
108 CHECK_EQ(scorecard[Bytecodes::ToByte(final_bytecode)], 1); | 107 CHECK_EQ(scorecard[Bytecodes::ToByte(final_bytecode)], 1); |
109 | 108 |
110 #define CHECK_BYTECODE_PRESENT(Name, ...) \ | 109 #define CHECK_BYTECODE_PRESENT(Name, ...) \ |
111 /* Check Bytecode is marked in scorecard */ \ | 110 /* Check Bytecode is marked in scorecard */ \ |
112 CHECK_GE(scorecard[Bytecodes::ToByte(Bytecode::k##Name)], 1); | 111 CHECK_GE(scorecard[Bytecodes::ToByte(Bytecode::k##Name)], 1); |
113 BYTECODE_LIST(CHECK_BYTECODE_PRESENT) | 112 BYTECODE_LIST(CHECK_BYTECODE_PRESENT) |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump); | 384 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump); |
386 CHECK_EQ(iterator.GetSmi8Operand(0), -2); | 385 CHECK_EQ(iterator.GetSmi8Operand(0), -2); |
387 iterator.Advance(); | 386 iterator.Advance(); |
388 } | 387 } |
389 CHECK_EQ(iterator.current_bytecode(), Bytecode::kReturn); | 388 CHECK_EQ(iterator.current_bytecode(), Bytecode::kReturn); |
390 iterator.Advance(); | 389 iterator.Advance(); |
391 CHECK(iterator.done()); | 390 CHECK(iterator.done()); |
392 } | 391 } |
393 | 392 |
394 | 393 |
| 394 TEST_F(BytecodeArrayBuilderTest, ToBoolean) { |
| 395 BytecodeArrayBuilder builder(isolate(), zone()); |
| 396 builder.set_parameter_count(0); |
| 397 builder.set_locals_count(0); |
| 398 |
| 399 // Check ToBoolean emitted at start of block. |
| 400 builder.EnterBlock().CastAccumulatorToBoolean(); |
| 401 |
| 402 // Check ToBoolean emitted preceding bytecode is non-boolean. |
| 403 builder.LoadNull().CastAccumulatorToBoolean(); |
| 404 |
| 405 // Check ToBoolean omitted if preceding bytecode is boolean. |
| 406 builder.LoadFalse().CastAccumulatorToBoolean(); |
| 407 |
| 408 // Check ToBoolean emitted if it is at the start of the next block. |
| 409 builder.LoadFalse() |
| 410 .LeaveBlock() |
| 411 .EnterBlock() |
| 412 .CastAccumulatorToBoolean() |
| 413 .LeaveBlock(); |
| 414 |
| 415 builder.Return(); |
| 416 |
| 417 Handle<BytecodeArray> array = builder.ToBytecodeArray(); |
| 418 BytecodeArrayIterator iterator(array); |
| 419 CHECK_EQ(iterator.current_bytecode(), Bytecode::kToBoolean); |
| 420 iterator.Advance(); |
| 421 |
| 422 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaNull); |
| 423 iterator.Advance(); |
| 424 CHECK_EQ(iterator.current_bytecode(), Bytecode::kToBoolean); |
| 425 iterator.Advance(); |
| 426 |
| 427 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaFalse); |
| 428 iterator.Advance(); |
| 429 |
| 430 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaFalse); |
| 431 iterator.Advance(); |
| 432 CHECK_EQ(iterator.current_bytecode(), Bytecode::kToBoolean); |
| 433 iterator.Advance(); |
| 434 |
| 435 CHECK_EQ(iterator.current_bytecode(), Bytecode::kReturn); |
| 436 iterator.Advance(); |
| 437 CHECK(iterator.done()); |
| 438 } |
| 439 |
| 440 |
395 } // namespace interpreter | 441 } // namespace interpreter |
396 } // namespace internal | 442 } // namespace internal |
397 } // namespace v8 | 443 } // namespace v8 |
OLD | NEW |