blob: f2dcd7107cec3b64e3cb426a1de7db0b7cf14523 [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 "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
11namespace v8 {
12namespace internal {
13namespace interpreter {
14
15class BytecodeArrayIteratorTest : public TestWithIsolateAndZone {
16 public:
17 BytecodeArrayIteratorTest() {}
18 ~BytecodeArrayIteratorTest() override {}
19};
20
21
22TEST_F(BytecodeArrayIteratorTest, IteratesBytecodeArray) {
23 // Use a builder to create an array with containing multiple bytecodes
24 // with 0, 1 and 2 operands.
Ben Murdoch097c5b22016-05-18 11:27:45 +010025 BytecodeArrayBuilder builder(isolate(), zone(), 3, 2, 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026 Factory* factory = isolate()->factory();
27 Handle<HeapObject> heap_num_0 = factory->NewHeapNumber(2.718);
28 Handle<HeapObject> heap_num_1 = factory->NewHeapNumber(2147483647);
29 Smi* zero = Smi::FromInt(0);
30 Smi* smi_0 = Smi::FromInt(64);
31 Smi* smi_1 = Smi::FromInt(-65536);
32 Register reg_0(0);
33 Register reg_1(1);
34 Register reg_2 = Register::FromParameterIndex(2, builder.parameter_count());
35 Handle<String> name = factory->NewStringFromStaticChars("abc");
36 int name_index = 3;
37 int feedback_slot = 97;
38
39 builder.LoadLiteral(heap_num_0)
40 .LoadLiteral(heap_num_1)
41 .LoadLiteral(zero)
42 .LoadLiteral(smi_0)
43 .LoadLiteral(smi_1)
44 .LoadAccumulatorWithRegister(reg_0)
Ben Murdoch097c5b22016-05-18 11:27:45 +010045 .LoadNamedProperty(reg_1, name, feedback_slot)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046 .StoreAccumulatorInRegister(reg_2)
47 .CallRuntime(Runtime::kLoadIC_Miss, reg_0, 1)
Ben Murdoch097c5b22016-05-18 11:27:45 +010048 .Debugger()
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049 .Return();
50
51 // Test iterator sees the expected output from the builder.
52 BytecodeArrayIterator iterator(builder.ToBytecodeArray());
53 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaConstant);
54 CHECK(iterator.GetConstantForIndexOperand(0).is_identical_to(heap_num_0));
55 CHECK(!iterator.done());
56 iterator.Advance();
57
58 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaConstant);
59 CHECK(iterator.GetConstantForIndexOperand(0).is_identical_to(heap_num_1));
60 CHECK(!iterator.done());
61 iterator.Advance();
62
63 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaZero);
64 CHECK(!iterator.done());
65 iterator.Advance();
66
67 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaSmi8);
68 CHECK_EQ(Smi::FromInt(iterator.GetImmediateOperand(0)), smi_0);
69 CHECK(!iterator.done());
70 iterator.Advance();
71
72 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdaConstant);
73 CHECK_EQ(*iterator.GetConstantForIndexOperand(0), smi_1);
74 CHECK(!iterator.done());
75 iterator.Advance();
76
77 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLdar);
78 CHECK_EQ(iterator.GetRegisterOperand(0).index(), reg_0.index());
79 CHECK(!iterator.done());
80 iterator.Advance();
81
Ben Murdoch097c5b22016-05-18 11:27:45 +010082 CHECK_EQ(iterator.current_bytecode(), Bytecode::kLoadIC);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000083 CHECK_EQ(iterator.GetRegisterOperand(0).index(), reg_1.index());
84 CHECK_EQ(iterator.GetIndexOperand(1), name_index);
85 CHECK_EQ(iterator.GetIndexOperand(2), feedback_slot);
86 CHECK(!iterator.done());
87 iterator.Advance();
88
89 CHECK_EQ(iterator.current_bytecode(), Bytecode::kStar);
90 CHECK_EQ(iterator.GetRegisterOperand(0).index(), reg_2.index());
91 CHECK(!iterator.done());
92 iterator.Advance();
93
94 CHECK_EQ(iterator.current_bytecode(), Bytecode::kCallRuntime);
95 CHECK_EQ(static_cast<Runtime::FunctionId>(iterator.GetIndexOperand(0)),
96 Runtime::kLoadIC_Miss);
97 CHECK_EQ(iterator.GetRegisterOperand(1).index(), reg_0.index());
Ben Murdoch097c5b22016-05-18 11:27:45 +010098 CHECK_EQ(iterator.GetRegisterCountOperand(2), 1);
99 CHECK(!iterator.done());
100 iterator.Advance();
101
102 CHECK_EQ(iterator.current_bytecode(), Bytecode::kDebugger);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000103 CHECK(!iterator.done());
104 iterator.Advance();
105
106 CHECK_EQ(iterator.current_bytecode(), Bytecode::kReturn);
107 CHECK(!iterator.done());
108 iterator.Advance();
109 CHECK(iterator.done());
110}
111
112} // namespace interpreter
113} // namespace internal
114} // namespace v8