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 "src/isolate.h" | 7 #include "src/isolate.h" |
8 #include "src/objects-inl.h" | 8 #include "src/objects-inl.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
(...skipping 28 matching lines...) Expand all Loading... |
39 return index + start_index(); | 39 return index + start_index(); |
40 } | 40 } |
41 | 41 |
42 Handle<Object> ConstantArrayBuilder::ConstantArraySlice::At( | 42 Handle<Object> ConstantArrayBuilder::ConstantArraySlice::At( |
43 size_t index) const { | 43 size_t index) const { |
44 DCHECK_GE(index, start_index()); | 44 DCHECK_GE(index, start_index()); |
45 DCHECK_LT(index, start_index() + size()); | 45 DCHECK_LT(index, start_index() + size()); |
46 return constants_[index - start_index()]; | 46 return constants_[index - start_index()]; |
47 } | 47 } |
48 | 48 |
| 49 void ConstantArrayBuilder::ConstantArraySlice::InsertAt(size_t index, |
| 50 Handle<Object> object) { |
| 51 DCHECK_GE(index, start_index()); |
| 52 DCHECK_LT(index, start_index() + size()); |
| 53 constants_[index - start_index()] = object; |
| 54 } |
| 55 |
49 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::k8BitCapacity; | 56 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::k8BitCapacity; |
50 STATIC_CONST_MEMBER_DEFINITION const size_t | 57 STATIC_CONST_MEMBER_DEFINITION const size_t |
51 ConstantArrayBuilder::k16BitCapacity; | 58 ConstantArrayBuilder::k16BitCapacity; |
52 STATIC_CONST_MEMBER_DEFINITION const size_t | 59 STATIC_CONST_MEMBER_DEFINITION const size_t |
53 ConstantArrayBuilder::k32BitCapacity; | 60 ConstantArrayBuilder::k32BitCapacity; |
54 | 61 |
55 ConstantArrayBuilder::ConstantArrayBuilder(Isolate* isolate, Zone* zone) | 62 ConstantArrayBuilder::ConstantArrayBuilder(Isolate* isolate, Zone* zone) |
56 : isolate_(isolate), constants_map_(isolate->heap(), zone) { | 63 : isolate_(isolate), constants_map_(isolate->heap(), zone) { |
57 idx_slice_[0] = | 64 idx_slice_[0] = |
58 new (zone) ConstantArraySlice(zone, 0, k8BitCapacity, OperandSize::kByte); | 65 new (zone) ConstantArraySlice(zone, 0, k8BitCapacity, OperandSize::kByte); |
59 idx_slice_[1] = new (zone) ConstantArraySlice( | 66 idx_slice_[1] = new (zone) ConstantArraySlice( |
60 zone, k8BitCapacity, k16BitCapacity, OperandSize::kShort); | 67 zone, k8BitCapacity, k16BitCapacity, OperandSize::kShort); |
61 idx_slice_[2] = new (zone) ConstantArraySlice( | 68 idx_slice_[2] = new (zone) ConstantArraySlice( |
62 zone, k8BitCapacity + k16BitCapacity, k32BitCapacity, OperandSize::kQuad); | 69 zone, k8BitCapacity + k16BitCapacity, k32BitCapacity, OperandSize::kQuad); |
63 } | 70 } |
64 | 71 |
65 size_t ConstantArrayBuilder::size() const { | 72 size_t ConstantArrayBuilder::size() const { |
66 size_t i = arraysize(idx_slice_); | 73 size_t i = arraysize(idx_slice_); |
67 while (i > 0) { | 74 while (i > 0) { |
68 ConstantArraySlice* slice = idx_slice_[--i]; | 75 ConstantArraySlice* slice = idx_slice_[--i]; |
69 if (slice->size() > 0) { | 76 if (slice->size() > 0) { |
70 return slice->start_index() + slice->size(); | 77 return slice->start_index() + slice->size(); |
71 } | 78 } |
72 } | 79 } |
73 return idx_slice_[0]->size(); | 80 return idx_slice_[0]->size(); |
74 } | 81 } |
75 | 82 |
76 const ConstantArrayBuilder::ConstantArraySlice* | 83 ConstantArrayBuilder::ConstantArraySlice* ConstantArrayBuilder::IndexToSlice( |
77 ConstantArrayBuilder::IndexToSlice(size_t index) const { | 84 size_t index) const { |
78 for (const ConstantArraySlice* slice : idx_slice_) { | 85 for (ConstantArraySlice* slice : idx_slice_) { |
79 if (index <= slice->max_index()) { | 86 if (index <= slice->max_index()) { |
80 return slice; | 87 return slice; |
81 } | 88 } |
82 } | 89 } |
83 UNREACHABLE(); | 90 UNREACHABLE(); |
84 return nullptr; | 91 return nullptr; |
85 } | 92 } |
86 | 93 |
87 Handle<Object> ConstantArrayBuilder::At(size_t index) const { | 94 Handle<Object> ConstantArrayBuilder::At(size_t index) const { |
88 const ConstantArraySlice* slice = IndexToSlice(index); | 95 const ConstantArraySlice* slice = IndexToSlice(index); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 } | 129 } |
123 | 130 |
124 size_t ConstantArrayBuilder::Insert(Handle<Object> object) { | 131 size_t ConstantArrayBuilder::Insert(Handle<Object> object) { |
125 index_t* entry = constants_map()->Find(object); | 132 index_t* entry = constants_map()->Find(object); |
126 return (entry == nullptr) ? AllocateEntry(object) : *entry; | 133 return (entry == nullptr) ? AllocateEntry(object) : *entry; |
127 } | 134 } |
128 | 135 |
129 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateEntry( | 136 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateEntry( |
130 Handle<Object> object) { | 137 Handle<Object> object) { |
131 DCHECK(!object->IsOddball()); | 138 DCHECK(!object->IsOddball()); |
| 139 index_t index = AllocateIndex(object); |
132 index_t* entry = constants_map()->Get(object); | 140 index_t* entry = constants_map()->Get(object); |
| 141 *entry = index; |
| 142 return index; |
| 143 } |
| 144 |
| 145 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateIndex( |
| 146 Handle<Object> object) { |
133 for (size_t i = 0; i < arraysize(idx_slice_); ++i) { | 147 for (size_t i = 0; i < arraysize(idx_slice_); ++i) { |
134 if (idx_slice_[i]->available() > 0) { | 148 if (idx_slice_[i]->available() > 0) { |
135 size_t index = idx_slice_[i]->Allocate(object); | 149 return static_cast<index_t>(idx_slice_[i]->Allocate(object)); |
136 *entry = static_cast<index_t>(index); | |
137 return *entry; | |
138 break; | |
139 } | 150 } |
140 } | 151 } |
141 UNREACHABLE(); | 152 UNREACHABLE(); |
142 return kMaxUInt32; | 153 return kMaxUInt32; |
143 } | 154 } |
144 | 155 |
145 OperandSize ConstantArrayBuilder::CreateReservedEntry() { | |
146 for (size_t i = 0; i < arraysize(idx_slice_); ++i) { | |
147 if (idx_slice_[i]->available() > 0) { | |
148 idx_slice_[i]->Reserve(); | |
149 return idx_slice_[i]->operand_size(); | |
150 } | |
151 } | |
152 UNREACHABLE(); | |
153 return OperandSize::kNone; | |
154 } | |
155 | |
156 ConstantArrayBuilder::ConstantArraySlice* | 156 ConstantArrayBuilder::ConstantArraySlice* |
157 ConstantArrayBuilder::OperandSizeToSlice(OperandSize operand_size) const { | 157 ConstantArrayBuilder::OperandSizeToSlice(OperandSize operand_size) const { |
158 ConstantArraySlice* slice = nullptr; | 158 ConstantArraySlice* slice = nullptr; |
159 switch (operand_size) { | 159 switch (operand_size) { |
160 case OperandSize::kNone: | 160 case OperandSize::kNone: |
161 UNREACHABLE(); | 161 UNREACHABLE(); |
162 break; | 162 break; |
163 case OperandSize::kByte: | 163 case OperandSize::kByte: |
164 slice = idx_slice_[0]; | 164 slice = idx_slice_[0]; |
165 break; | 165 break; |
166 case OperandSize::kShort: | 166 case OperandSize::kShort: |
167 slice = idx_slice_[1]; | 167 slice = idx_slice_[1]; |
168 break; | 168 break; |
169 case OperandSize::kQuad: | 169 case OperandSize::kQuad: |
170 slice = idx_slice_[2]; | 170 slice = idx_slice_[2]; |
171 break; | 171 break; |
172 } | 172 } |
173 DCHECK(slice->operand_size() == operand_size); | 173 DCHECK(slice->operand_size() == operand_size); |
174 return slice; | 174 return slice; |
175 } | 175 } |
176 | 176 |
| 177 size_t ConstantArrayBuilder::AllocateEntry() { |
| 178 return AllocateIndex(isolate_->factory()->the_hole_value()); |
| 179 } |
| 180 |
| 181 void ConstantArrayBuilder::InsertAllocatedEntry(size_t index, |
| 182 Handle<Object> object) { |
| 183 DCHECK_EQ(isolate_->heap()->the_hole_value(), *At(index)); |
| 184 ConstantArraySlice* slice = IndexToSlice(index); |
| 185 slice->InsertAt(index, object); |
| 186 } |
| 187 |
| 188 OperandSize ConstantArrayBuilder::CreateReservedEntry() { |
| 189 for (size_t i = 0; i < arraysize(idx_slice_); ++i) { |
| 190 if (idx_slice_[i]->available() > 0) { |
| 191 idx_slice_[i]->Reserve(); |
| 192 return idx_slice_[i]->operand_size(); |
| 193 } |
| 194 } |
| 195 UNREACHABLE(); |
| 196 return OperandSize::kNone; |
| 197 } |
| 198 |
177 size_t ConstantArrayBuilder::CommitReservedEntry(OperandSize operand_size, | 199 size_t ConstantArrayBuilder::CommitReservedEntry(OperandSize operand_size, |
178 Handle<Object> object) { | 200 Handle<Object> object) { |
179 DiscardReservedEntry(operand_size); | 201 DiscardReservedEntry(operand_size); |
180 size_t index; | 202 size_t index; |
181 index_t* entry = constants_map()->Find(object); | 203 index_t* entry = constants_map()->Find(object); |
182 if (nullptr == entry) { | 204 if (nullptr == entry) { |
183 index = AllocateEntry(object); | 205 index = AllocateEntry(object); |
184 } else { | 206 } else { |
185 ConstantArraySlice* slice = OperandSizeToSlice(operand_size); | 207 ConstantArraySlice* slice = OperandSizeToSlice(operand_size); |
186 if (*entry > slice->max_index()) { | 208 if (*entry > slice->max_index()) { |
187 // The object is already in the constant array, but may have an | 209 // The object is already in the constant array, but may have an |
188 // index too big for the reserved operand_size. So, duplicate | 210 // index too big for the reserved operand_size. So, duplicate |
189 // entry with the smaller operand size. | 211 // entry with the smaller operand size. |
190 *entry = static_cast<index_t>(slice->Allocate(object)); | 212 *entry = static_cast<index_t>(slice->Allocate(object)); |
191 } | 213 } |
192 index = *entry; | 214 index = *entry; |
193 } | 215 } |
194 return index; | 216 return index; |
195 } | 217 } |
196 | 218 |
197 void ConstantArrayBuilder::DiscardReservedEntry(OperandSize operand_size) { | 219 void ConstantArrayBuilder::DiscardReservedEntry(OperandSize operand_size) { |
198 OperandSizeToSlice(operand_size)->Unreserve(); | 220 OperandSizeToSlice(operand_size)->Unreserve(); |
199 } | 221 } |
200 | 222 |
201 } // namespace interpreter | 223 } // namespace interpreter |
202 } // namespace internal | 224 } // namespace internal |
203 } // namespace v8 | 225 } // namespace v8 |
OLD | NEW |