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/factory.h" | 7 #include "src/factory.h" |
8 #include "src/handles-inl.h" | 8 #include "src/handles-inl.h" |
9 #include "src/interpreter/constant-array-builder.h" | 9 #include "src/interpreter/constant-array-builder.h" |
10 #include "src/isolate.h" | 10 #include "src/isolate.h" |
11 #include "test/unittests/test-utils.h" | 11 #include "test/unittests/test-utils.h" |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
15 namespace interpreter { | 15 namespace interpreter { |
16 | 16 |
17 class ConstantArrayBuilderTest : public TestWithIsolateAndZone { | 17 class ConstantArrayBuilderTest : public TestWithIsolateAndZone { |
18 public: | 18 public: |
19 ConstantArrayBuilderTest() {} | 19 ConstantArrayBuilderTest() {} |
20 ~ConstantArrayBuilderTest() override {} | 20 ~ConstantArrayBuilderTest() override {} |
21 | 21 |
22 static const size_t kLowCapacity = ConstantArrayBuilder::kLowCapacity; | 22 static const size_t k8BitCapacity = ConstantArrayBuilder::k8BitCapacity; |
23 static const size_t kMaxCapacity = ConstantArrayBuilder::kMaxCapacity; | 23 static const size_t k16BitCapacity = ConstantArrayBuilder::k16BitCapacity; |
24 }; | 24 }; |
25 | 25 |
26 | |
27 STATIC_CONST_MEMBER_DEFINITION const size_t | 26 STATIC_CONST_MEMBER_DEFINITION const size_t |
28 ConstantArrayBuilderTest::kMaxCapacity; | 27 ConstantArrayBuilderTest::k16BitCapacity; |
29 STATIC_CONST_MEMBER_DEFINITION const size_t | 28 STATIC_CONST_MEMBER_DEFINITION const size_t |
30 ConstantArrayBuilderTest::kLowCapacity; | 29 ConstantArrayBuilderTest::k8BitCapacity; |
31 | |
32 | 30 |
33 TEST_F(ConstantArrayBuilderTest, AllocateAllEntries) { | 31 TEST_F(ConstantArrayBuilderTest, AllocateAllEntries) { |
34 ConstantArrayBuilder builder(isolate(), zone()); | 32 ConstantArrayBuilder builder(isolate(), zone()); |
35 for (size_t i = 0; i < kMaxCapacity; i++) { | 33 for (size_t i = 0; i < k16BitCapacity; i++) { |
36 builder.Insert(handle(Smi::FromInt(static_cast<int>(i)), isolate())); | 34 builder.Insert(handle(Smi::FromInt(static_cast<int>(i)), isolate())); |
37 } | 35 } |
38 CHECK_EQ(builder.size(), kMaxCapacity); | 36 CHECK_EQ(builder.size(), k16BitCapacity); |
39 for (size_t i = 0; i < kMaxCapacity; i++) { | 37 for (size_t i = 0; i < k16BitCapacity; i++) { |
40 CHECK_EQ(Handle<Smi>::cast(builder.At(i))->value(), i); | 38 CHECK_EQ(Handle<Smi>::cast(builder.At(i))->value(), i); |
41 } | 39 } |
42 } | 40 } |
43 | 41 |
44 | 42 |
45 TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithIdx8Reservations) { | 43 TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithIdx8Reservations) { |
46 for (size_t reserved = 1; reserved < kLowCapacity; reserved *= 3) { | 44 for (size_t reserved = 1; reserved < k8BitCapacity; reserved *= 3) { |
47 ConstantArrayBuilder builder(isolate(), zone()); | 45 ConstantArrayBuilder builder(isolate(), zone()); |
48 for (size_t i = 0; i < reserved; i++) { | 46 for (size_t i = 0; i < reserved; i++) { |
49 OperandSize operand_size = builder.CreateReservedEntry(); | 47 OperandSize operand_size = builder.CreateReservedEntry(); |
50 CHECK(operand_size == OperandSize::kByte); | 48 CHECK(operand_size == OperandSize::kByte); |
51 } | 49 } |
52 for (size_t i = 0; i < 2 * kLowCapacity; i++) { | 50 for (size_t i = 0; i < 2 * k8BitCapacity; i++) { |
53 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 51 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
54 builder.Insert(object); | 52 builder.Insert(object); |
55 if (i + reserved < kLowCapacity) { | 53 if (i + reserved < k8BitCapacity) { |
56 CHECK_LE(builder.size(), kLowCapacity); | 54 CHECK_LE(builder.size(), k8BitCapacity); |
57 CHECK_EQ(builder.size(), i + 1); | 55 CHECK_EQ(builder.size(), i + 1); |
58 CHECK(builder.At(i)->SameValue(*object)); | 56 CHECK(builder.At(i)->SameValue(*object)); |
59 } else { | 57 } else { |
60 CHECK_GE(builder.size(), kLowCapacity); | 58 CHECK_GE(builder.size(), k8BitCapacity); |
61 CHECK_EQ(builder.size(), i + reserved + 1); | 59 CHECK_EQ(builder.size(), i + reserved + 1); |
62 CHECK(builder.At(i + reserved)->SameValue(*object)); | 60 CHECK(builder.At(i + reserved)->SameValue(*object)); |
63 } | 61 } |
64 } | 62 } |
65 CHECK_EQ(builder.size(), 2 * kLowCapacity + reserved); | 63 CHECK_EQ(builder.size(), 2 * k8BitCapacity + reserved); |
66 | 64 |
67 // Check reserved values represented by the hole. | 65 // Check reserved values represented by the hole. |
68 for (size_t i = 0; i < reserved; i++) { | 66 for (size_t i = 0; i < reserved; i++) { |
69 Handle<Object> empty = builder.At(kLowCapacity - reserved + i); | 67 Handle<Object> empty = builder.At(k8BitCapacity - reserved + i); |
70 CHECK(empty->SameValue(isolate()->heap()->the_hole_value())); | 68 CHECK(empty->SameValue(isolate()->heap()->the_hole_value())); |
71 } | 69 } |
72 | 70 |
73 // Commmit reserved entries with duplicates and check size does not change. | 71 // Commmit reserved entries with duplicates and check size does not change. |
74 DCHECK_EQ(reserved + 2 * kLowCapacity, builder.size()); | 72 DCHECK_EQ(reserved + 2 * k8BitCapacity, builder.size()); |
75 size_t duplicates_in_idx8_space = | 73 size_t duplicates_in_idx8_space = |
76 std::min(reserved, kLowCapacity - reserved); | 74 std::min(reserved, k8BitCapacity - reserved); |
77 for (size_t i = 0; i < duplicates_in_idx8_space; i++) { | 75 for (size_t i = 0; i < duplicates_in_idx8_space; i++) { |
78 builder.CommitReservedEntry(OperandSize::kByte, | 76 builder.CommitReservedEntry(OperandSize::kByte, |
79 isolate()->factory()->NewNumberFromSize(i)); | 77 isolate()->factory()->NewNumberFromSize(i)); |
80 DCHECK_EQ(reserved + 2 * kLowCapacity, builder.size()); | 78 DCHECK_EQ(reserved + 2 * k8BitCapacity, builder.size()); |
81 } | 79 } |
82 | 80 |
83 // Check all committed values match expected (holes where | 81 // Check all committed values match expected (holes where |
84 // duplicates_in_idx8_space allocated). | 82 // duplicates_in_idx8_space allocated). |
85 for (size_t i = 0; i < kLowCapacity - reserved; i++) { | 83 for (size_t i = 0; i < k8BitCapacity - reserved; i++) { |
86 Smi* smi = Smi::FromInt(static_cast<int>(i)); | 84 Smi* smi = Smi::FromInt(static_cast<int>(i)); |
87 CHECK(Handle<Smi>::cast(builder.At(i))->SameValue(smi)); | 85 CHECK(Handle<Smi>::cast(builder.At(i))->SameValue(smi)); |
88 } | 86 } |
89 for (size_t i = kLowCapacity; i < 2 * kLowCapacity + reserved; i++) { | 87 for (size_t i = k8BitCapacity; i < 2 * k8BitCapacity + reserved; i++) { |
90 Smi* smi = Smi::FromInt(static_cast<int>(i - reserved)); | 88 Smi* smi = Smi::FromInt(static_cast<int>(i - reserved)); |
91 CHECK(Handle<Smi>::cast(builder.At(i))->SameValue(smi)); | 89 CHECK(Handle<Smi>::cast(builder.At(i))->SameValue(smi)); |
92 } | 90 } |
93 for (size_t i = 0; i < reserved; i++) { | 91 for (size_t i = 0; i < reserved; i++) { |
94 size_t index = kLowCapacity - reserved + i; | 92 size_t index = k8BitCapacity - reserved + i; |
95 CHECK(builder.At(index)->IsTheHole()); | 93 CHECK(builder.At(index)->IsTheHole()); |
96 } | 94 } |
97 | 95 |
98 // Now make reservations, and commit them with unique entries. | 96 // Now make reservations, and commit them with unique entries. |
99 for (size_t i = 0; i < duplicates_in_idx8_space; i++) { | 97 for (size_t i = 0; i < duplicates_in_idx8_space; i++) { |
100 OperandSize operand_size = builder.CreateReservedEntry(); | 98 OperandSize operand_size = builder.CreateReservedEntry(); |
101 CHECK(operand_size == OperandSize::kByte); | 99 CHECK(operand_size == OperandSize::kByte); |
102 } | 100 } |
103 for (size_t i = 0; i < duplicates_in_idx8_space; i++) { | 101 for (size_t i = 0; i < duplicates_in_idx8_space; i++) { |
104 Handle<Object> object = | 102 Handle<Object> object = |
105 isolate()->factory()->NewNumberFromSize(2 * kLowCapacity + i); | 103 isolate()->factory()->NewNumberFromSize(2 * k8BitCapacity + i); |
106 size_t index = builder.CommitReservedEntry(OperandSize::kByte, object); | 104 size_t index = builder.CommitReservedEntry(OperandSize::kByte, object); |
107 CHECK_EQ(static_cast<int>(index), kLowCapacity - reserved + i); | 105 CHECK_EQ(static_cast<int>(index), k8BitCapacity - reserved + i); |
108 CHECK(builder.At(static_cast<int>(index))->SameValue(*object)); | 106 CHECK(builder.At(static_cast<int>(index))->SameValue(*object)); |
109 } | 107 } |
110 CHECK_EQ(builder.size(), 2 * kLowCapacity + reserved); | 108 CHECK_EQ(builder.size(), 2 * k8BitCapacity + reserved); |
111 } | 109 } |
112 } | 110 } |
113 | 111 |
114 | 112 |
115 TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithIdx16Reservations) { | 113 TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithIdx16Reservations) { |
116 for (size_t reserved = 1; reserved < kLowCapacity; reserved *= 3) { | 114 for (size_t reserved = 1; reserved < k8BitCapacity; reserved *= 3) { |
117 ConstantArrayBuilder builder(isolate(), zone()); | 115 ConstantArrayBuilder builder(isolate(), zone()); |
118 for (size_t i = 0; i < kLowCapacity; i++) { | 116 for (size_t i = 0; i < k8BitCapacity; i++) { |
119 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 117 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
120 builder.Insert(object); | 118 builder.Insert(object); |
121 CHECK(builder.At(i)->SameValue(*object)); | 119 CHECK(builder.At(i)->SameValue(*object)); |
122 CHECK_EQ(builder.size(), i + 1); | 120 CHECK_EQ(builder.size(), i + 1); |
123 } | 121 } |
124 for (size_t i = 0; i < reserved; i++) { | 122 for (size_t i = 0; i < reserved; i++) { |
125 OperandSize operand_size = builder.CreateReservedEntry(); | 123 OperandSize operand_size = builder.CreateReservedEntry(); |
126 CHECK(operand_size == OperandSize::kShort); | 124 CHECK(operand_size == OperandSize::kShort); |
127 CHECK_EQ(builder.size(), kLowCapacity); | 125 CHECK_EQ(builder.size(), k8BitCapacity); |
128 } | 126 } |
129 for (size_t i = 0; i < reserved; i++) { | 127 for (size_t i = 0; i < reserved; i++) { |
130 builder.DiscardReservedEntry(OperandSize::kShort); | 128 builder.DiscardReservedEntry(OperandSize::kShort); |
131 CHECK_EQ(builder.size(), kLowCapacity); | 129 CHECK_EQ(builder.size(), k8BitCapacity); |
132 } | 130 } |
133 for (size_t i = 0; i < reserved; i++) { | 131 for (size_t i = 0; i < reserved; i++) { |
134 OperandSize operand_size = builder.CreateReservedEntry(); | 132 OperandSize operand_size = builder.CreateReservedEntry(); |
135 CHECK(operand_size == OperandSize::kShort); | 133 CHECK(operand_size == OperandSize::kShort); |
136 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 134 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
137 builder.CommitReservedEntry(operand_size, object); | 135 builder.CommitReservedEntry(operand_size, object); |
138 CHECK_EQ(builder.size(), kLowCapacity); | 136 CHECK_EQ(builder.size(), k8BitCapacity); |
139 } | 137 } |
140 for (size_t i = kLowCapacity; i < kLowCapacity + reserved; i++) { | 138 for (size_t i = k8BitCapacity; i < k8BitCapacity + reserved; i++) { |
141 OperandSize operand_size = builder.CreateReservedEntry(); | 139 OperandSize operand_size = builder.CreateReservedEntry(); |
142 CHECK(operand_size == OperandSize::kShort); | 140 CHECK(operand_size == OperandSize::kShort); |
143 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 141 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
144 builder.CommitReservedEntry(operand_size, object); | 142 builder.CommitReservedEntry(operand_size, object); |
145 CHECK_EQ(builder.size(), i + 1); | 143 CHECK_EQ(builder.size(), i + 1); |
146 } | 144 } |
147 } | 145 } |
148 } | 146 } |
149 | 147 |
150 | 148 |
151 TEST_F(ConstantArrayBuilderTest, ToFixedArray) { | 149 TEST_F(ConstantArrayBuilderTest, ToFixedArray) { |
152 ConstantArrayBuilder builder(isolate(), zone()); | 150 ConstantArrayBuilder builder(isolate(), zone()); |
153 static const size_t kNumberOfElements = 37; | 151 static const size_t kNumberOfElements = 37; |
154 for (size_t i = 0; i < kNumberOfElements; i++) { | 152 for (size_t i = 0; i < kNumberOfElements; i++) { |
155 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 153 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
156 builder.Insert(object); | 154 builder.Insert(object); |
157 CHECK(builder.At(i)->SameValue(*object)); | 155 CHECK(builder.At(i)->SameValue(*object)); |
158 } | 156 } |
159 Handle<FixedArray> constant_array = builder.ToFixedArray(); | 157 Handle<FixedArray> constant_array = builder.ToFixedArray(); |
160 CHECK_EQ(constant_array->length(), kNumberOfElements); | 158 CHECK_EQ(constant_array->length(), kNumberOfElements); |
161 for (size_t i = 0; i < kNumberOfElements; i++) { | 159 for (size_t i = 0; i < kNumberOfElements; i++) { |
162 CHECK(constant_array->get(static_cast<int>(i))->SameValue(*builder.At(i))); | 160 CHECK(constant_array->get(static_cast<int>(i))->SameValue(*builder.At(i))); |
163 } | 161 } |
164 } | 162 } |
165 | 163 |
166 | 164 |
167 TEST_F(ConstantArrayBuilderTest, GapFilledWhenLowReservationCommitted) { | 165 TEST_F(ConstantArrayBuilderTest, GapFilledWhenLowReservationCommitted) { |
168 ConstantArrayBuilder builder(isolate(), zone()); | 166 ConstantArrayBuilder builder(isolate(), zone()); |
169 for (size_t i = 0; i < kLowCapacity; i++) { | 167 for (size_t i = 0; i < k8BitCapacity; i++) { |
170 OperandSize operand_size = builder.CreateReservedEntry(); | 168 OperandSize operand_size = builder.CreateReservedEntry(); |
171 CHECK(OperandSize::kByte == operand_size); | 169 CHECK(OperandSize::kByte == operand_size); |
172 CHECK_EQ(builder.size(), 0); | 170 CHECK_EQ(builder.size(), 0); |
173 } | 171 } |
174 for (size_t i = 0; i < kLowCapacity; i++) { | 172 for (size_t i = 0; i < k8BitCapacity; i++) { |
175 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 173 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
176 builder.Insert(object); | 174 builder.Insert(object); |
177 CHECK_EQ(builder.size(), i + kLowCapacity + 1); | 175 CHECK_EQ(builder.size(), i + k8BitCapacity + 1); |
178 } | 176 } |
179 for (size_t i = 0; i < kLowCapacity; i++) { | 177 for (size_t i = 0; i < k8BitCapacity; i++) { |
180 builder.CommitReservedEntry(OperandSize::kByte, | 178 builder.CommitReservedEntry(OperandSize::kByte, |
181 builder.At(i + kLowCapacity)); | 179 builder.At(i + k8BitCapacity)); |
182 CHECK_EQ(builder.size(), 2 * kLowCapacity); | 180 CHECK_EQ(builder.size(), 2 * k8BitCapacity); |
183 } | 181 } |
184 for (size_t i = 0; i < kLowCapacity; i++) { | 182 for (size_t i = 0; i < k8BitCapacity; i++) { |
185 Handle<Object> original = builder.At(kLowCapacity + i); | 183 Handle<Object> original = builder.At(k8BitCapacity + i); |
186 Handle<Object> duplicate = builder.At(i); | 184 Handle<Object> duplicate = builder.At(i); |
187 CHECK(original->SameValue(*duplicate)); | 185 CHECK(original->SameValue(*duplicate)); |
188 Handle<Object> reference = isolate()->factory()->NewNumberFromSize(i); | 186 Handle<Object> reference = isolate()->factory()->NewNumberFromSize(i); |
189 CHECK(original->SameValue(*reference)); | 187 CHECK(original->SameValue(*reference)); |
190 } | 188 } |
191 } | 189 } |
192 | 190 |
193 | 191 |
194 TEST_F(ConstantArrayBuilderTest, GapNotFilledWhenLowReservationDiscarded) { | 192 TEST_F(ConstantArrayBuilderTest, GapNotFilledWhenLowReservationDiscarded) { |
195 ConstantArrayBuilder builder(isolate(), zone()); | 193 ConstantArrayBuilder builder(isolate(), zone()); |
196 for (size_t i = 0; i < kLowCapacity; i++) { | 194 for (size_t i = 0; i < k8BitCapacity; i++) { |
197 OperandSize operand_size = builder.CreateReservedEntry(); | 195 OperandSize operand_size = builder.CreateReservedEntry(); |
198 CHECK(OperandSize::kByte == operand_size); | 196 CHECK(OperandSize::kByte == operand_size); |
199 CHECK_EQ(builder.size(), 0); | 197 CHECK_EQ(builder.size(), 0); |
200 } | 198 } |
201 for (size_t i = 0; i < kLowCapacity; i++) { | 199 for (size_t i = 0; i < k8BitCapacity; i++) { |
202 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 200 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
203 builder.Insert(object); | 201 builder.Insert(object); |
204 CHECK_EQ(builder.size(), i + kLowCapacity + 1); | 202 CHECK_EQ(builder.size(), i + k8BitCapacity + 1); |
205 } | 203 } |
206 for (size_t i = 0; i < kLowCapacity; i++) { | 204 for (size_t i = 0; i < k8BitCapacity; i++) { |
207 builder.DiscardReservedEntry(OperandSize::kByte); | 205 builder.DiscardReservedEntry(OperandSize::kByte); |
208 builder.Insert(builder.At(i + kLowCapacity)); | 206 builder.Insert(builder.At(i + k8BitCapacity)); |
209 CHECK_EQ(builder.size(), 2 * kLowCapacity); | 207 CHECK_EQ(builder.size(), 2 * k8BitCapacity); |
210 } | 208 } |
211 for (size_t i = 0; i < kLowCapacity; i++) { | 209 for (size_t i = 0; i < k8BitCapacity; i++) { |
212 Handle<Object> reference = isolate()->factory()->NewNumberFromSize(i); | 210 Handle<Object> reference = isolate()->factory()->NewNumberFromSize(i); |
213 Handle<Object> original = builder.At(kLowCapacity + i); | 211 Handle<Object> original = builder.At(k8BitCapacity + i); |
214 CHECK(original->SameValue(*reference)); | 212 CHECK(original->SameValue(*reference)); |
215 Handle<Object> duplicate = builder.At(i); | 213 Handle<Object> duplicate = builder.At(i); |
216 CHECK(duplicate->SameValue(*isolate()->factory()->the_hole_value())); | 214 CHECK(duplicate->SameValue(*isolate()->factory()->the_hole_value())); |
217 } | 215 } |
218 } | 216 } |
219 | 217 |
220 } // namespace interpreter | 218 } // namespace interpreter |
221 } // namespace internal | 219 } // namespace internal |
222 } // namespace v8 | 220 } // namespace v8 |
OLD | NEW |