Line data Source code
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
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-register-allocator.h"
9 : #include "src/objects-inl.h"
10 : #include "test/unittests/test-utils.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 : namespace interpreter {
15 :
16 : class BytecodeRegisterAllocatorTest : public TestWithIsolateAndZone {
17 : public:
18 3 : BytecodeRegisterAllocatorTest() : allocator_(0) {}
19 6 : ~BytecodeRegisterAllocatorTest() override = default;
20 :
21 3 : BytecodeRegisterAllocator* allocator() { return &allocator_; }
22 :
23 : private:
24 : BytecodeRegisterAllocator allocator_;
25 : };
26 :
27 15419 : TEST_F(BytecodeRegisterAllocatorTest, SimpleAllocations) {
28 1 : CHECK_EQ(allocator()->maximum_register_count(), 0);
29 1 : Register reg0 = allocator()->NewRegister();
30 1 : CHECK_EQ(reg0.index(), 0);
31 1 : CHECK_EQ(allocator()->maximum_register_count(), 1);
32 1 : CHECK_EQ(allocator()->next_register_index(), 1);
33 1 : CHECK(allocator()->RegisterIsLive(reg0));
34 :
35 : allocator()->ReleaseRegisters(0);
36 1 : CHECK(!allocator()->RegisterIsLive(reg0));
37 1 : CHECK_EQ(allocator()->maximum_register_count(), 1);
38 1 : CHECK_EQ(allocator()->next_register_index(), 0);
39 :
40 1 : reg0 = allocator()->NewRegister();
41 1 : Register reg1 = allocator()->NewRegister();
42 1 : CHECK_EQ(reg0.index(), 0);
43 1 : CHECK_EQ(reg1.index(), 1);
44 1 : CHECK(allocator()->RegisterIsLive(reg0));
45 1 : CHECK(allocator()->RegisterIsLive(reg1));
46 1 : CHECK_EQ(allocator()->maximum_register_count(), 2);
47 1 : CHECK_EQ(allocator()->next_register_index(), 2);
48 :
49 : allocator()->ReleaseRegisters(1);
50 1 : CHECK(allocator()->RegisterIsLive(reg0));
51 1 : CHECK(!allocator()->RegisterIsLive(reg1));
52 1 : CHECK_EQ(allocator()->maximum_register_count(), 2);
53 1 : CHECK_EQ(allocator()->next_register_index(), 1);
54 1 : }
55 :
56 15419 : TEST_F(BytecodeRegisterAllocatorTest, RegisterListAllocations) {
57 1 : CHECK_EQ(allocator()->maximum_register_count(), 0);
58 1 : RegisterList reg_list = allocator()->NewRegisterList(3);
59 1 : CHECK_EQ(reg_list.first_register().index(), 0);
60 1 : CHECK_EQ(reg_list.register_count(), 3);
61 1 : CHECK_EQ(reg_list[0].index(), 0);
62 1 : CHECK_EQ(reg_list[1].index(), 1);
63 1 : CHECK_EQ(reg_list[2].index(), 2);
64 1 : CHECK_EQ(allocator()->maximum_register_count(), 3);
65 1 : CHECK_EQ(allocator()->next_register_index(), 3);
66 1 : CHECK(allocator()->RegisterIsLive(reg_list[2]));
67 :
68 1 : Register reg = allocator()->NewRegister();
69 1 : RegisterList reg_list_2 = allocator()->NewRegisterList(2);
70 1 : CHECK_EQ(reg.index(), 3);
71 1 : CHECK_EQ(reg_list_2.first_register().index(), 4);
72 1 : CHECK_EQ(reg_list_2.register_count(), 2);
73 1 : CHECK_EQ(reg_list_2[0].index(), 4);
74 1 : CHECK_EQ(reg_list_2[1].index(), 5);
75 1 : CHECK_EQ(allocator()->maximum_register_count(), 6);
76 1 : CHECK_EQ(allocator()->next_register_index(), 6);
77 1 : CHECK(allocator()->RegisterIsLive(reg));
78 1 : CHECK(allocator()->RegisterIsLive(reg_list_2[1]));
79 :
80 : allocator()->ReleaseRegisters(reg.index());
81 1 : CHECK(!allocator()->RegisterIsLive(reg));
82 1 : CHECK(!allocator()->RegisterIsLive(reg_list_2[0]));
83 1 : CHECK(!allocator()->RegisterIsLive(reg_list_2[1]));
84 1 : CHECK(allocator()->RegisterIsLive(reg_list[2]));
85 1 : CHECK_EQ(allocator()->maximum_register_count(), 6);
86 1 : CHECK_EQ(allocator()->next_register_index(), 3);
87 :
88 1 : RegisterList empty_reg_list = allocator()->NewRegisterList(0);
89 1 : CHECK_EQ(empty_reg_list.first_register().index(), 0);
90 1 : CHECK_EQ(empty_reg_list.register_count(), 0);
91 1 : CHECK_EQ(allocator()->maximum_register_count(), 6);
92 1 : CHECK_EQ(allocator()->next_register_index(), 3);
93 1 : }
94 :
95 15419 : TEST_F(BytecodeRegisterAllocatorTest, GrowableRegisterListAllocations) {
96 1 : CHECK_EQ(allocator()->maximum_register_count(), 0);
97 1 : Register reg = allocator()->NewRegister();
98 1 : CHECK_EQ(reg.index(), 0);
99 1 : RegisterList reg_list = allocator()->NewGrowableRegisterList();
100 : CHECK_EQ(reg_list.register_count(), 0);
101 1 : allocator()->GrowRegisterList(®_list);
102 1 : allocator()->GrowRegisterList(®_list);
103 1 : allocator()->GrowRegisterList(®_list);
104 1 : CHECK_EQ(reg_list.register_count(), 3);
105 1 : CHECK_EQ(reg_list[0].index(), 1);
106 1 : CHECK_EQ(reg_list[1].index(), 2);
107 1 : CHECK_EQ(reg_list[2].index(), 3);
108 1 : CHECK_EQ(allocator()->maximum_register_count(), 4);
109 1 : CHECK_EQ(allocator()->next_register_index(), 4);
110 1 : }
111 :
112 : } // namespace interpreter
113 : } // namespace internal
114 9249 : } // namespace v8
|