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> |
| 8 |
7 #include "src/isolate.h" | 9 #include "src/isolate.h" |
8 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
9 | 11 |
10 namespace v8 { | 12 namespace v8 { |
11 namespace internal { | 13 namespace internal { |
12 namespace interpreter { | 14 namespace interpreter { |
13 | 15 |
14 ConstantArrayBuilder::ConstantArraySlice::ConstantArraySlice( | 16 ConstantArrayBuilder::ConstantArraySlice::ConstantArraySlice( |
15 Zone* zone, size_t start_index, size_t capacity, OperandSize operand_size) | 17 Zone* zone, size_t start_index, size_t capacity, OperandSize operand_size) |
16 : start_index_(start_index), | 18 : start_index_(start_index), |
(...skipping 29 matching lines...) Expand all Loading... |
46 return constants_[index - start_index()]; | 48 return constants_[index - start_index()]; |
47 } | 49 } |
48 | 50 |
49 void ConstantArrayBuilder::ConstantArraySlice::InsertAt(size_t index, | 51 void ConstantArrayBuilder::ConstantArraySlice::InsertAt(size_t index, |
50 Handle<Object> object) { | 52 Handle<Object> object) { |
51 DCHECK_GE(index, start_index()); | 53 DCHECK_GE(index, start_index()); |
52 DCHECK_LT(index, start_index() + size()); | 54 DCHECK_LT(index, start_index() + size()); |
53 constants_[index - start_index()] = object; | 55 constants_[index - start_index()] = object; |
54 } | 56 } |
55 | 57 |
| 58 bool ConstantArrayBuilder::ConstantArraySlice::AllElementsAreUnique() const { |
| 59 std::set<Object*> elements; |
| 60 for (auto constant : constants_) { |
| 61 if (elements.find(*constant) != elements.end()) return false; |
| 62 elements.insert(*constant); |
| 63 } |
| 64 return true; |
| 65 } |
| 66 |
56 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::k8BitCapacity; | 67 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::k8BitCapacity; |
57 STATIC_CONST_MEMBER_DEFINITION const size_t | 68 STATIC_CONST_MEMBER_DEFINITION const size_t |
58 ConstantArrayBuilder::k16BitCapacity; | 69 ConstantArrayBuilder::k16BitCapacity; |
59 STATIC_CONST_MEMBER_DEFINITION const size_t | 70 STATIC_CONST_MEMBER_DEFINITION const size_t |
60 ConstantArrayBuilder::k32BitCapacity; | 71 ConstantArrayBuilder::k32BitCapacity; |
61 | 72 |
62 ConstantArrayBuilder::ConstantArrayBuilder(Isolate* isolate, Zone* zone) | 73 ConstantArrayBuilder::ConstantArrayBuilder(Isolate* isolate, Zone* zone) |
63 : isolate_(isolate), constants_map_(isolate->heap(), zone) { | 74 : isolate_(isolate), constants_map_(zone) { |
64 idx_slice_[0] = | 75 idx_slice_[0] = |
65 new (zone) ConstantArraySlice(zone, 0, k8BitCapacity, OperandSize::kByte); | 76 new (zone) ConstantArraySlice(zone, 0, k8BitCapacity, OperandSize::kByte); |
66 idx_slice_[1] = new (zone) ConstantArraySlice( | 77 idx_slice_[1] = new (zone) ConstantArraySlice( |
67 zone, k8BitCapacity, k16BitCapacity, OperandSize::kShort); | 78 zone, k8BitCapacity, k16BitCapacity, OperandSize::kShort); |
68 idx_slice_[2] = new (zone) ConstantArraySlice( | 79 idx_slice_[2] = new (zone) ConstantArraySlice( |
69 zone, k8BitCapacity + k16BitCapacity, k32BitCapacity, OperandSize::kQuad); | 80 zone, k8BitCapacity + k16BitCapacity, k32BitCapacity, OperandSize::kQuad); |
70 } | 81 } |
71 | 82 |
72 size_t ConstantArrayBuilder::size() const { | 83 size_t ConstantArrayBuilder::size() const { |
73 size_t i = arraysize(idx_slice_); | 84 size_t i = arraysize(idx_slice_); |
(...skipping 30 matching lines...) Expand all Loading... |
104 Handle<FixedArray> ConstantArrayBuilder::ToFixedArray() { | 115 Handle<FixedArray> ConstantArrayBuilder::ToFixedArray() { |
105 Handle<FixedArray> fixed_array = isolate_->factory()->NewFixedArray( | 116 Handle<FixedArray> fixed_array = isolate_->factory()->NewFixedArray( |
106 static_cast<int>(size()), PretenureFlag::TENURED); | 117 static_cast<int>(size()), PretenureFlag::TENURED); |
107 int array_index = 0; | 118 int array_index = 0; |
108 for (const ConstantArraySlice* slice : idx_slice_) { | 119 for (const ConstantArraySlice* slice : idx_slice_) { |
109 if (array_index == fixed_array->length()) { | 120 if (array_index == fixed_array->length()) { |
110 break; | 121 break; |
111 } | 122 } |
112 DCHECK(array_index == 0 || | 123 DCHECK(array_index == 0 || |
113 base::bits::IsPowerOfTwo32(static_cast<uint32_t>(array_index))); | 124 base::bits::IsPowerOfTwo32(static_cast<uint32_t>(array_index))); |
| 125 // Different slices might contain the same element due to reservations, but |
| 126 // all elements within a slice should be unique. If this DCHECK fails, then |
| 127 // the AST nodes are not being internalized within a CanonicalHandleScope. |
| 128 DCHECK(slice->AllElementsAreUnique()); |
114 // Copy objects from slice into array. | 129 // Copy objects from slice into array. |
115 for (size_t i = 0; i < slice->size(); ++i) { | 130 for (size_t i = 0; i < slice->size(); ++i) { |
116 fixed_array->set(array_index++, *slice->At(slice->start_index() + i)); | 131 fixed_array->set(array_index++, *slice->At(slice->start_index() + i)); |
117 } | 132 } |
118 // Insert holes where reservations led to unused slots. | 133 // Insert holes where reservations led to unused slots. |
119 size_t padding = | 134 size_t padding = |
120 std::min(static_cast<size_t>(fixed_array->length() - array_index), | 135 std::min(static_cast<size_t>(fixed_array->length() - array_index), |
121 slice->capacity() - slice->size()); | 136 slice->capacity() - slice->size()); |
122 for (size_t i = 0; i < padding; i++) { | 137 for (size_t i = 0; i < padding; i++) { |
123 fixed_array->set(array_index++, *isolate_->factory()->the_hole_value()); | 138 fixed_array->set(array_index++, *isolate_->factory()->the_hole_value()); |
124 } | 139 } |
125 } | 140 } |
126 DCHECK_EQ(array_index, fixed_array->length()); | 141 DCHECK_EQ(array_index, fixed_array->length()); |
127 constants_map()->Clear(); | |
128 return fixed_array; | 142 return fixed_array; |
129 } | 143 } |
130 | 144 |
131 size_t ConstantArrayBuilder::Insert(Handle<Object> object) { | 145 size_t ConstantArrayBuilder::Insert(Handle<Object> object) { |
132 index_t* entry = constants_map()->Find(object); | 146 auto entry = constants_map_.find(object.address()); |
133 return (entry == nullptr) ? AllocateEntry(object) : *entry; | 147 return (entry == constants_map_.end()) ? AllocateEntry(object) |
| 148 : entry->second; |
134 } | 149 } |
135 | 150 |
136 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateEntry( | 151 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateEntry( |
137 Handle<Object> object) { | 152 Handle<Object> object) { |
138 DCHECK(!object->IsOddball()); | |
139 index_t index = AllocateIndex(object); | 153 index_t index = AllocateIndex(object); |
140 index_t* entry = constants_map()->Get(object); | 154 constants_map_[object.address()] = index; |
141 *entry = index; | |
142 return index; | 155 return index; |
143 } | 156 } |
144 | 157 |
145 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateIndex( | 158 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateIndex( |
146 Handle<Object> object) { | 159 Handle<Object> object) { |
147 for (size_t i = 0; i < arraysize(idx_slice_); ++i) { | 160 for (size_t i = 0; i < arraysize(idx_slice_); ++i) { |
148 if (idx_slice_[i]->available() > 0) { | 161 if (idx_slice_[i]->available() > 0) { |
149 return static_cast<index_t>(idx_slice_[i]->Allocate(object)); | 162 return static_cast<index_t>(idx_slice_[i]->Allocate(object)); |
150 } | 163 } |
151 } | 164 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 } | 206 } |
194 } | 207 } |
195 UNREACHABLE(); | 208 UNREACHABLE(); |
196 return OperandSize::kNone; | 209 return OperandSize::kNone; |
197 } | 210 } |
198 | 211 |
199 size_t ConstantArrayBuilder::CommitReservedEntry(OperandSize operand_size, | 212 size_t ConstantArrayBuilder::CommitReservedEntry(OperandSize operand_size, |
200 Handle<Object> object) { | 213 Handle<Object> object) { |
201 DiscardReservedEntry(operand_size); | 214 DiscardReservedEntry(operand_size); |
202 size_t index; | 215 size_t index; |
203 index_t* entry = constants_map()->Find(object); | 216 auto entry = constants_map_.find(object.address()); |
204 if (nullptr == entry) { | 217 if (entry == constants_map_.end()) { |
205 index = AllocateEntry(object); | 218 index = AllocateEntry(object); |
206 } else { | 219 } else { |
207 ConstantArraySlice* slice = OperandSizeToSlice(operand_size); | 220 ConstantArraySlice* slice = OperandSizeToSlice(operand_size); |
208 if (*entry > slice->max_index()) { | 221 index = entry->second; |
| 222 if (index > slice->max_index()) { |
209 // The object is already in the constant array, but may have an | 223 // The object is already in the constant array, but may have an |
210 // index too big for the reserved operand_size. So, duplicate | 224 // index too big for the reserved operand_size. So, duplicate |
211 // entry with the smaller operand size. | 225 // entry with the smaller operand size. |
212 *entry = static_cast<index_t>(slice->Allocate(object)); | 226 index = slice->Allocate(object); |
| 227 constants_map_[object.address()] = static_cast<index_t>(index); |
213 } | 228 } |
214 index = *entry; | |
215 } | 229 } |
216 return index; | 230 return index; |
217 } | 231 } |
218 | 232 |
219 void ConstantArrayBuilder::DiscardReservedEntry(OperandSize operand_size) { | 233 void ConstantArrayBuilder::DiscardReservedEntry(OperandSize operand_size) { |
220 OperandSizeToSlice(operand_size)->Unreserve(); | 234 OperandSizeToSlice(operand_size)->Unreserve(); |
221 } | 235 } |
222 | 236 |
223 } // namespace interpreter | 237 } // namespace interpreter |
224 } // namespace internal | 238 } // namespace internal |
225 } // namespace v8 | 239 } // namespace v8 |
OLD | NEW |