5 #include "src/interpreter/constant-array-builder.h" 11 #include "src/ast/ast-value-factory.h" 12 #include "src/ast/ast.h" 13 #include "src/ast/scopes.h" 14 #include "src/base/functional.h" 15 #include "src/isolate.h" 16 #include "src/objects-inl.h" 20 namespace interpreter {
22 ConstantArrayBuilder::ConstantArraySlice::ConstantArraySlice(
23 Zone* zone,
size_t start_index,
size_t capacity, OperandSize operand_size)
24 : start_index_(start_index),
27 operand_size_(operand_size),
30 void ConstantArrayBuilder::ConstantArraySlice::Reserve() {
31 DCHECK_GT(available(), 0u);
33 DCHECK_LE(reserved_, capacity() - constants_.size());
36 void ConstantArrayBuilder::ConstantArraySlice::Unreserve() {
37 DCHECK_GT(reserved_, 0u);
41 size_t ConstantArrayBuilder::ConstantArraySlice::Allocate(
42 ConstantArrayBuilder::Entry entry,
size_t count) {
43 DCHECK_GE(available(), count);
44 size_t index = constants_.size();
45 DCHECK_LT(index, capacity());
46 for (
size_t i = 0;
i < count; ++
i) {
47 constants_.push_back(entry);
49 return index + start_index();
52 ConstantArrayBuilder::Entry& ConstantArrayBuilder::ConstantArraySlice::At(
54 DCHECK_GE(index, start_index());
55 DCHECK_LT(index, start_index() + size());
56 return constants_[index - start_index()];
59 const ConstantArrayBuilder::Entry& ConstantArrayBuilder::ConstantArraySlice::At(
61 DCHECK_GE(index, start_index());
62 DCHECK_LT(index, start_index() + size());
63 return constants_[index - start_index()];
67 void ConstantArrayBuilder::ConstantArraySlice::CheckAllElementsAreUnique(
68 Isolate* isolate)
const {
70 std::set<double> heap_numbers;
71 std::set<const AstRawString*> strings;
72 std::set<const char*> bigints;
73 std::set<const Scope*> scopes;
74 std::set<Object*> deferred_objects;
75 for (
const Entry& entry : constants_) {
76 bool duplicate =
false;
78 case Entry::Tag::kSmi:
79 duplicate = !smis.insert(entry.smi_).second;
81 case Entry::Tag::kHeapNumber:
82 duplicate = !heap_numbers.insert(entry.heap_number_).second;
84 case Entry::Tag::kRawString:
85 duplicate = !strings.insert(entry.raw_string_).second;
87 case Entry::Tag::kBigInt:
88 duplicate = !bigints.insert(entry.bigint_.c_str()).second;
90 case Entry::Tag::kScope:
91 duplicate = !scopes.insert(entry.scope_).second;
93 case Entry::Tag::kHandle:
94 duplicate = !deferred_objects.insert(*entry.handle_).second;
96 case Entry::Tag::kDeferred:
98 case Entry::Tag::kJumpTableSmi:
99 case Entry::Tag::kUninitializedJumpTableSmi:
103 #define CASE_TAG(NAME, ...) case Entry::Tag::k##NAME: 104 SINGLETON_CONSTANT_ENTRY_TYPES(CASE_TAG)
110 std::ostringstream os;
111 os <<
"Duplicate constant found: " << Brief(*entry.ToHandle(isolate))
114 size_t i = start_index();
115 for (
const Entry& prev_entry : constants_) {
116 os <<
i++ <<
": " << Brief(*prev_entry.ToHandle(isolate)) << std::endl;
118 FATAL(
"%s", os.str().c_str());
124 STATIC_CONST_MEMBER_DEFINITION
const size_t ConstantArrayBuilder::k8BitCapacity;
125 STATIC_CONST_MEMBER_DEFINITION
const size_t 126 ConstantArrayBuilder::k16BitCapacity;
127 STATIC_CONST_MEMBER_DEFINITION
const size_t 128 ConstantArrayBuilder::k32BitCapacity;
130 ConstantArrayBuilder::ConstantArrayBuilder(Zone* zone)
131 : constants_map_(16, base::KeyEqualityMatcher<intptr_t>(),
132 ZoneAllocationPolicy(zone)),
135 heap_number_map_(zone),
136 #define INIT_SINGLETON_ENTRY_FIELD(NAME, LOWER_NAME) LOWER_NAME##_(-1),
137 SINGLETON_CONSTANT_ENTRY_TYPES(INIT_SINGLETON_ENTRY_FIELD)
138 #undef INIT_SINGLETON_ENTRY_FIELD
141 new (zone) ConstantArraySlice(zone, 0, k8BitCapacity, OperandSize::kByte);
142 idx_slice_[1] =
new (zone) ConstantArraySlice(
143 zone, k8BitCapacity, k16BitCapacity, OperandSize::kShort);
144 idx_slice_[2] =
new (zone) ConstantArraySlice(
145 zone, k8BitCapacity + k16BitCapacity, k32BitCapacity, OperandSize::kQuad);
148 size_t ConstantArrayBuilder::size()
const {
149 size_t i = arraysize(idx_slice_);
151 ConstantArraySlice* slice = idx_slice_[--
i];
152 if (slice->size() > 0) {
153 return slice->start_index() + slice->size();
156 return idx_slice_[0]->size();
159 ConstantArrayBuilder::ConstantArraySlice* ConstantArrayBuilder::IndexToSlice(
160 size_t index)
const {
161 for (ConstantArraySlice* slice : idx_slice_) {
162 if (index <= slice->max_index()) {
169 MaybeHandle<Object> ConstantArrayBuilder::At(
size_t index,
170 Isolate* isolate)
const {
171 const ConstantArraySlice* slice = IndexToSlice(index);
172 DCHECK_LT(index, slice->capacity());
173 if (index < slice->start_index() + slice->size()) {
174 const Entry& entry = slice->At(index);
175 if (!entry.IsDeferred())
return entry.ToHandle(isolate);
177 return MaybeHandle<Object>();
180 Handle<FixedArray> ConstantArrayBuilder::ToFixedArray(Isolate* isolate) {
181 Handle<FixedArray> fixed_array = isolate->factory()->NewFixedArrayWithHoles(
182 static_cast<int>(size()), PretenureFlag::TENURED);
184 for (
const ConstantArraySlice* slice : idx_slice_) {
185 DCHECK_EQ(slice->reserved(), 0);
186 DCHECK(array_index == 0 ||
187 base::bits::IsPowerOfTwo(static_cast<uint32_t>(array_index)));
191 slice->CheckAllElementsAreUnique(isolate);
194 for (
size_t i = 0;
i < slice->size(); ++
i) {
195 Handle<Object> value =
196 slice->At(slice->start_index() +
i).ToHandle(isolate);
197 fixed_array->set(array_index++, *value);
200 size_t padding = slice->capacity() - slice->size();
201 if (static_cast<size_t>(fixed_array->length() - array_index) <= padding) {
204 array_index += padding;
206 DCHECK_GE(array_index, fixed_array->length());
210 size_t ConstantArrayBuilder::Insert(Smi smi) {
211 auto entry = smi_map_.find(smi);
212 if (entry == smi_map_.end()) {
213 return AllocateReservedEntry(smi);
215 return entry->second;
218 size_t ConstantArrayBuilder::Insert(
double number) {
219 if (std::isnan(number))
return InsertNaN();
220 auto entry = heap_number_map_.find(number);
221 if (entry == heap_number_map_.end()) {
222 index_t index =
static_cast<index_t
>(AllocateIndex(Entry(number)));
223 heap_number_map_[number] = index;
226 return entry->second;
229 size_t ConstantArrayBuilder::Insert(
const AstRawString* raw_string) {
230 return constants_map_
231 .LookupOrInsert(reinterpret_cast<intptr_t>(raw_string),
233 [&]() {
return AllocateIndex(Entry(raw_string)); },
234 ZoneAllocationPolicy(zone_))
238 size_t ConstantArrayBuilder::Insert(AstBigInt bigint) {
239 return constants_map_
240 .LookupOrInsert(reinterpret_cast<intptr_t>(bigint.c_str()),
241 static_cast<uint32_t>(base::hash_value(bigint.c_str())),
242 [&]() {
return AllocateIndex(Entry(bigint)); },
243 ZoneAllocationPolicy(zone_))
247 size_t ConstantArrayBuilder::Insert(
const Scope* scope) {
248 return constants_map_
249 .LookupOrInsert(reinterpret_cast<intptr_t>(scope),
250 static_cast<uint32_t>(base::hash_value(scope)),
251 [&]() {
return AllocateIndex(Entry(scope)); },
252 ZoneAllocationPolicy(zone_))
256 #define INSERT_ENTRY(NAME, LOWER_NAME) \ 257 size_t ConstantArrayBuilder::Insert##NAME() { \ 258 if (LOWER_NAME##_ < 0) { \ 259 LOWER_NAME##_ = AllocateIndex(Entry::NAME()); \ 261 return LOWER_NAME##_; \ 263 SINGLETON_CONSTANT_ENTRY_TYPES(INSERT_ENTRY)
266 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateIndex(
267 ConstantArrayBuilder::Entry entry) {
268 return AllocateIndexArray(entry, 1);
271 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateIndexArray(
272 ConstantArrayBuilder::Entry entry,
size_t count) {
273 for (
size_t i = 0;
i < arraysize(idx_slice_); ++
i) {
274 if (idx_slice_[
i]->available() >= count) {
275 return static_cast<index_t
>(idx_slice_[
i]->Allocate(entry, count));
281 ConstantArrayBuilder::ConstantArraySlice*
282 ConstantArrayBuilder::OperandSizeToSlice(OperandSize operand_size)
const {
283 ConstantArraySlice* slice =
nullptr;
284 switch (operand_size) {
285 case OperandSize::kNone:
288 case OperandSize::kByte:
289 slice = idx_slice_[0];
291 case OperandSize::kShort:
292 slice = idx_slice_[1];
294 case OperandSize::kQuad:
295 slice = idx_slice_[2];
298 DCHECK(slice->operand_size() == operand_size);
302 size_t ConstantArrayBuilder::InsertDeferred() {
303 return AllocateIndex(Entry::Deferred());
306 size_t ConstantArrayBuilder::InsertJumpTable(
size_t size) {
307 return AllocateIndexArray(Entry::UninitializedJumpTableSmi(), size);
310 void ConstantArrayBuilder::SetDeferredAt(
size_t index, Handle<Object>
object) {
311 ConstantArraySlice* slice = IndexToSlice(index);
312 return slice->At(index).SetDeferred(
object);
315 void ConstantArrayBuilder::SetJumpTableSmi(
size_t index, Smi smi) {
316 ConstantArraySlice* slice = IndexToSlice(index);
320 smi_map_.emplace(smi, static_cast<index_t>(index));
321 return slice->At(index).SetJumpTableSmi(smi);
324 OperandSize ConstantArrayBuilder::CreateReservedEntry() {
325 for (
size_t i = 0;
i < arraysize(idx_slice_); ++
i) {
326 if (idx_slice_[
i]->available() > 0) {
327 idx_slice_[
i]->Reserve();
328 return idx_slice_[
i]->operand_size();
334 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateReservedEntry(
336 index_t index =
static_cast<index_t
>(AllocateIndex(Entry(value)));
337 smi_map_[value] = index;
341 size_t ConstantArrayBuilder::CommitReservedEntry(OperandSize operand_size,
343 DiscardReservedEntry(operand_size);
345 auto entry = smi_map_.find(value);
346 if (entry == smi_map_.end()) {
347 index = AllocateReservedEntry(value);
349 ConstantArraySlice* slice = OperandSizeToSlice(operand_size);
350 index = entry->second;
351 if (index > slice->max_index()) {
355 index = AllocateReservedEntry(value);
357 DCHECK_LE(index, slice->max_index());
362 void ConstantArrayBuilder::DiscardReservedEntry(OperandSize operand_size) {
363 OperandSizeToSlice(operand_size)->Unreserve();
366 Handle<Object> ConstantArrayBuilder::Entry::ToHandle(Isolate* isolate)
const {
374 case Tag::kJumpTableSmi:
375 return handle(smi_, isolate);
376 case Tag::kUninitializedJumpTableSmi:
378 return isolate->factory()->the_hole_value();
379 case Tag::kRawString:
380 return raw_string_->string();
381 case Tag::kHeapNumber:
382 return isolate->factory()->NewNumber(heap_number_, TENURED);
386 return BigIntLiteral(isolate, bigint_.c_str()).ToHandleChecked();
388 return scope_->scope_info();
389 #define ENTRY_LOOKUP(Name, name) \ 391 return isolate->factory()->name(); 392 SINGLETON_CONSTANT_ENTRY_TYPES(ENTRY_LOOKUP);