OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/v8.h" |
| 6 |
| 7 #include "src/interpreter/bytecode-array-builder.h" |
| 8 #include "src/interpreter/bytecode-array-iterator.h" |
| 9 #include "test/unittests/test-utils.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 namespace interpreter { |
| 14 |
| 15 class BytecodeArrayIteratorTest : public TestWithIsolateAndZone { |
| 16 public: |
| 17 BytecodeArrayIteratorTest() {} |
| 18 ~BytecodeArrayIteratorTest() override {} |
| 19 }; |
| 20 |
| 21 |
| 22 TEST_F(BytecodeArrayIteratorTest, IteratesBytecodeArray) { |
| 23 // Use a builder to create an array with containing multiple bytecodes |
| 24 // with 0, 1 and 2 operands. |
| 25 BytecodeArrayBuilder builder(isolate(), zone()); |
| 26 builder.set_parameter_count(3); |
| 27 builder.set_locals_count(2); |
| 28 |
| 29 Factory* factory = isolate()->factory(); |
| 30 Handle<HeapObject> heap_num_0 = factory->NewHeapNumber(2.718); |
| 31 Handle<HeapObject> heap_num_1 = factory->NewHeapNumber(2147483647); |
| 32 Smi* zero = Smi::FromInt(0); |
| 33 Smi* smi_0 = Smi::FromInt(64); |
| 34 Smi* smi_1 = Smi::FromInt(-65536); |
| 35 Register reg_0(0); |
| 36 Register reg_1(1); |
| 37 Register reg_2 = Register::FromParameterIndex(2, builder.parameter_count()); |
| 38 int feedback_slot = 97; |
| 39 |
| 40 builder.LoadLiteral(heap_num_0) |
| 41 .LoadLiteral(heap_num_1) |
| 42 .LoadLiteral(zero) |
| 43 .LoadLiteral(smi_0) |
| 44 .LoadLiteral(smi_1) |
| 45 .LoadAccumulatorWithRegister(reg_0) |
| 46 .LoadNamedProperty(reg_1, feedback_slot, LanguageMode::SLOPPY) |
| 47 .StoreAccumulatorInRegister(reg_2) |
| 48 .Return(); |
| 49 |
| 50 // Test iterator sees the expected output from the builder. |
| 51 BytecodeArrayIterator iterator(builder.ToBytecodeArray()); |
| 52 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaConstant); |
| 53 CHECK(iterator.GetConstantForIndexOperand(0).is_identical_to(heap_num_0)); |
| 54 CHECK(!iterator.done()); |
| 55 iterator.Advance(); |
| 56 |
| 57 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaConstant); |
| 58 CHECK(iterator.GetConstantForIndexOperand(0).is_identical_to(heap_num_1)); |
| 59 CHECK(!iterator.done()); |
| 60 iterator.Advance(); |
| 61 |
| 62 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaZero); |
| 63 CHECK(!iterator.done()); |
| 64 iterator.Advance(); |
| 65 |
| 66 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaSmi8); |
| 67 CHECK_EQ(Smi::FromInt(iterator.GetSmi8Operand(0)), smi_0); |
| 68 CHECK(!iterator.done()); |
| 69 iterator.Advance(); |
| 70 |
| 71 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaConstant); |
| 72 CHECK_EQ(*iterator.GetConstantForIndexOperand(0), smi_1); |
| 73 CHECK(!iterator.done()); |
| 74 iterator.Advance(); |
| 75 |
| 76 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdar); |
| 77 CHECK_EQ(iterator.GetRegisterOperand(0).index(), reg_0.index()); |
| 78 CHECK(!iterator.done()); |
| 79 iterator.Advance(); |
| 80 |
| 81 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLoadIC); |
| 82 CHECK_EQ(iterator.GetRegisterOperand(0).index(), reg_1.index()); |
| 83 CHECK_EQ(iterator.GetIndexOperand(1), feedback_slot); |
| 84 CHECK(!iterator.done()); |
| 85 iterator.Advance(); |
| 86 |
| 87 CHECK_EQ(iterator.current_bytecode(), Bytecode::kStar); |
| 88 CHECK_EQ(iterator.GetRegisterOperand(0).index(), reg_2.index()); |
| 89 CHECK(!iterator.done()); |
| 90 iterator.Advance(); |
| 91 |
| 92 CHECK_EQ(iterator.current_bytecode(), Bytecode::kReturn); |
| 93 CHECK(!iterator.done()); |
| 94 iterator.Advance(); |
| 95 CHECK(iterator.done()); |
| 96 } |
| 97 |
| 98 } // namespace interpreter |
| 99 } // namespace internal |
| 100 } // namespace v8 |
OLD | NEW |