OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 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/interpreter/constant-array-builder.h" | 5 #include "src/interpreter/constant-array-builder.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "src/isolate.h" | 9 #include "src/isolate.h" |
10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 } | 63 } |
64 return true; | 64 return true; |
65 } | 65 } |
66 | 66 |
67 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::k8BitCapacity; | 67 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::k8BitCapacity; |
68 STATIC_CONST_MEMBER_DEFINITION const size_t | 68 STATIC_CONST_MEMBER_DEFINITION const size_t |
69 ConstantArrayBuilder::k16BitCapacity; | 69 ConstantArrayBuilder::k16BitCapacity; |
70 STATIC_CONST_MEMBER_DEFINITION const size_t | 70 STATIC_CONST_MEMBER_DEFINITION const size_t |
71 ConstantArrayBuilder::k32BitCapacity; | 71 ConstantArrayBuilder::k32BitCapacity; |
72 | 72 |
73 ConstantArrayBuilder::ConstantArrayBuilder(Isolate* isolate, Zone* zone) | 73 ConstantArrayBuilder::ConstantArrayBuilder(Zone* zone, |
74 : isolate_(isolate), | 74 Handle<Object> the_hole_value) |
75 constants_map_(zone), | 75 : constants_map_(zone), |
76 smi_map_(zone), | 76 smi_map_(zone), |
77 smi_pairs_(zone) { | 77 smi_pairs_(zone), |
| 78 the_hole_value_(the_hole_value) { |
78 idx_slice_[0] = | 79 idx_slice_[0] = |
79 new (zone) ConstantArraySlice(zone, 0, k8BitCapacity, OperandSize::kByte); | 80 new (zone) ConstantArraySlice(zone, 0, k8BitCapacity, OperandSize::kByte); |
80 idx_slice_[1] = new (zone) ConstantArraySlice( | 81 idx_slice_[1] = new (zone) ConstantArraySlice( |
81 zone, k8BitCapacity, k16BitCapacity, OperandSize::kShort); | 82 zone, k8BitCapacity, k16BitCapacity, OperandSize::kShort); |
82 idx_slice_[2] = new (zone) ConstantArraySlice( | 83 idx_slice_[2] = new (zone) ConstantArraySlice( |
83 zone, k8BitCapacity + k16BitCapacity, k32BitCapacity, OperandSize::kQuad); | 84 zone, k8BitCapacity + k16BitCapacity, k32BitCapacity, OperandSize::kQuad); |
84 } | 85 } |
85 | 86 |
86 size_t ConstantArrayBuilder::size() const { | 87 size_t ConstantArrayBuilder::size() const { |
87 size_t i = arraysize(idx_slice_); | 88 size_t i = arraysize(idx_slice_); |
(...skipping 16 matching lines...) Expand all Loading... |
104 UNREACHABLE(); | 105 UNREACHABLE(); |
105 return nullptr; | 106 return nullptr; |
106 } | 107 } |
107 | 108 |
108 Handle<Object> ConstantArrayBuilder::At(size_t index) const { | 109 Handle<Object> ConstantArrayBuilder::At(size_t index) const { |
109 const ConstantArraySlice* slice = IndexToSlice(index); | 110 const ConstantArraySlice* slice = IndexToSlice(index); |
110 if (index < slice->start_index() + slice->size()) { | 111 if (index < slice->start_index() + slice->size()) { |
111 return slice->At(index); | 112 return slice->At(index); |
112 } else { | 113 } else { |
113 DCHECK_LT(index, slice->capacity()); | 114 DCHECK_LT(index, slice->capacity()); |
114 return isolate_->factory()->the_hole_value(); | 115 return the_hole_value(); |
115 } | 116 } |
116 } | 117 } |
117 | 118 |
118 Handle<FixedArray> ConstantArrayBuilder::ToFixedArray() { | 119 Handle<FixedArray> ConstantArrayBuilder::ToFixedArray(Isolate* isolate) { |
119 // First insert reserved SMI values. | 120 // First insert reserved SMI values. |
120 for (auto reserved_smi : smi_pairs_) { | 121 for (auto reserved_smi : smi_pairs_) { |
121 InsertAllocatedEntry(reserved_smi.second, | 122 InsertAllocatedEntry(reserved_smi.second, |
122 handle(reserved_smi.first, isolate_)); | 123 handle(reserved_smi.first, isolate)); |
123 } | 124 } |
124 | 125 |
125 Handle<FixedArray> fixed_array = isolate_->factory()->NewFixedArray( | 126 Handle<FixedArray> fixed_array = isolate->factory()->NewFixedArray( |
126 static_cast<int>(size()), PretenureFlag::TENURED); | 127 static_cast<int>(size()), PretenureFlag::TENURED); |
127 int array_index = 0; | 128 int array_index = 0; |
128 for (const ConstantArraySlice* slice : idx_slice_) { | 129 for (const ConstantArraySlice* slice : idx_slice_) { |
129 if (array_index == fixed_array->length()) { | 130 if (array_index == fixed_array->length()) { |
130 break; | 131 break; |
131 } | 132 } |
132 DCHECK(array_index == 0 || | 133 DCHECK(array_index == 0 || |
133 base::bits::IsPowerOfTwo32(static_cast<uint32_t>(array_index))); | 134 base::bits::IsPowerOfTwo32(static_cast<uint32_t>(array_index))); |
134 // Different slices might contain the same element due to reservations, but | 135 // Different slices might contain the same element due to reservations, but |
135 // all elements within a slice should be unique. If this DCHECK fails, then | 136 // all elements within a slice should be unique. If this DCHECK fails, then |
136 // the AST nodes are not being internalized within a CanonicalHandleScope. | 137 // the AST nodes are not being internalized within a CanonicalHandleScope. |
137 DCHECK(slice->AllElementsAreUnique()); | 138 DCHECK(slice->AllElementsAreUnique()); |
138 // Copy objects from slice into array. | 139 // Copy objects from slice into array. |
139 for (size_t i = 0; i < slice->size(); ++i) { | 140 for (size_t i = 0; i < slice->size(); ++i) { |
140 fixed_array->set(array_index++, *slice->At(slice->start_index() + i)); | 141 fixed_array->set(array_index++, *slice->At(slice->start_index() + i)); |
141 } | 142 } |
142 // Insert holes where reservations led to unused slots. | 143 // Insert holes where reservations led to unused slots. |
143 size_t padding = | 144 size_t padding = |
144 std::min(static_cast<size_t>(fixed_array->length() - array_index), | 145 std::min(static_cast<size_t>(fixed_array->length() - array_index), |
145 slice->capacity() - slice->size()); | 146 slice->capacity() - slice->size()); |
146 for (size_t i = 0; i < padding; i++) { | 147 for (size_t i = 0; i < padding; i++) { |
147 fixed_array->set(array_index++, *isolate_->factory()->the_hole_value()); | 148 fixed_array->set(array_index++, *the_hole_value()); |
148 } | 149 } |
149 } | 150 } |
150 DCHECK_EQ(array_index, fixed_array->length()); | 151 DCHECK_EQ(array_index, fixed_array->length()); |
151 return fixed_array; | 152 return fixed_array; |
152 } | 153 } |
153 | 154 |
154 size_t ConstantArrayBuilder::Insert(Handle<Object> object) { | 155 size_t ConstantArrayBuilder::Insert(Handle<Object> object) { |
155 auto entry = constants_map_.find(object.address()); | 156 auto entry = constants_map_.find(object.address()); |
156 return (entry == constants_map_.end()) ? AllocateEntry(object) | 157 return (entry == constants_map_.end()) ? AllocateEntry(object) |
157 : entry->second; | 158 : entry->second; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 break; | 191 break; |
191 case OperandSize::kQuad: | 192 case OperandSize::kQuad: |
192 slice = idx_slice_[2]; | 193 slice = idx_slice_[2]; |
193 break; | 194 break; |
194 } | 195 } |
195 DCHECK(slice->operand_size() == operand_size); | 196 DCHECK(slice->operand_size() == operand_size); |
196 return slice; | 197 return slice; |
197 } | 198 } |
198 | 199 |
199 size_t ConstantArrayBuilder::AllocateEntry() { | 200 size_t ConstantArrayBuilder::AllocateEntry() { |
200 return AllocateIndex(isolate_->factory()->the_hole_value()); | 201 return AllocateIndex(the_hole_value()); |
201 } | 202 } |
202 | 203 |
203 void ConstantArrayBuilder::InsertAllocatedEntry(size_t index, | 204 void ConstantArrayBuilder::InsertAllocatedEntry(size_t index, |
204 Handle<Object> object) { | 205 Handle<Object> object) { |
205 DCHECK_EQ(isolate_->heap()->the_hole_value(), *At(index)); | 206 DCHECK_EQ(the_hole_value().address(), At(index).address()); |
206 ConstantArraySlice* slice = IndexToSlice(index); | 207 ConstantArraySlice* slice = IndexToSlice(index); |
207 slice->InsertAt(index, object); | 208 slice->InsertAt(index, object); |
208 } | 209 } |
209 | 210 |
210 OperandSize ConstantArrayBuilder::CreateReservedEntry() { | 211 OperandSize ConstantArrayBuilder::CreateReservedEntry() { |
211 for (size_t i = 0; i < arraysize(idx_slice_); ++i) { | 212 for (size_t i = 0; i < arraysize(idx_slice_); ++i) { |
212 if (idx_slice_[i]->available() > 0) { | 213 if (idx_slice_[i]->available() > 0) { |
213 idx_slice_[i]->Reserve(); | 214 idx_slice_[i]->Reserve(); |
214 return idx_slice_[i]->operand_size(); | 215 return idx_slice_[i]->operand_size(); |
215 } | 216 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 return index; | 248 return index; |
248 } | 249 } |
249 | 250 |
250 void ConstantArrayBuilder::DiscardReservedEntry(OperandSize operand_size) { | 251 void ConstantArrayBuilder::DiscardReservedEntry(OperandSize operand_size) { |
251 OperandSizeToSlice(operand_size)->Unreserve(); | 252 OperandSizeToSlice(operand_size)->Unreserve(); |
252 } | 253 } |
253 | 254 |
254 } // namespace interpreter | 255 } // namespace interpreter |
255 } // namespace internal | 256 } // namespace internal |
256 } // namespace v8 | 257 } // namespace v8 |
OLD | NEW |