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 { |
11 namespace internal { | 11 namespace internal { |
12 namespace interpreter { | 12 namespace interpreter { |
13 | 13 |
14 ConstantArrayBuilder::ConstantArraySlice::ConstantArraySlice(Zone* zone, | 14 ConstantArrayBuilder::ConstantArraySlice::ConstantArraySlice( |
15 size_t start_index, | 15 Zone* zone, size_t start_index, size_t capacity, OperandSize operand_size) |
16 size_t capacity) | |
17 : start_index_(start_index), | 16 : start_index_(start_index), |
18 capacity_(capacity), | 17 capacity_(capacity), |
19 reserved_(0), | 18 reserved_(0), |
| 19 operand_size_(operand_size), |
20 constants_(zone) {} | 20 constants_(zone) {} |
21 | 21 |
22 | |
23 void ConstantArrayBuilder::ConstantArraySlice::Reserve() { | 22 void ConstantArrayBuilder::ConstantArraySlice::Reserve() { |
24 DCHECK_GT(available(), 0u); | 23 DCHECK_GT(available(), 0u); |
25 reserved_++; | 24 reserved_++; |
26 DCHECK_LE(reserved_, capacity() - constants_.size()); | 25 DCHECK_LE(reserved_, capacity() - constants_.size()); |
27 } | 26 } |
28 | 27 |
29 | |
30 void ConstantArrayBuilder::ConstantArraySlice::Unreserve() { | 28 void ConstantArrayBuilder::ConstantArraySlice::Unreserve() { |
31 DCHECK_GT(reserved_, 0u); | 29 DCHECK_GT(reserved_, 0u); |
32 reserved_--; | 30 reserved_--; |
33 } | 31 } |
34 | 32 |
35 | |
36 size_t ConstantArrayBuilder::ConstantArraySlice::Allocate( | 33 size_t ConstantArrayBuilder::ConstantArraySlice::Allocate( |
37 Handle<Object> object) { | 34 Handle<Object> object) { |
38 DCHECK_GT(available(), 0u); | 35 DCHECK_GT(available(), 0u); |
39 size_t index = constants_.size(); | 36 size_t index = constants_.size(); |
40 DCHECK_LT(index, capacity()); | 37 DCHECK_LT(index, capacity()); |
41 constants_.push_back(object); | 38 constants_.push_back(object); |
42 return index + start_index(); | 39 return index + start_index(); |
43 } | 40 } |
44 | 41 |
45 | |
46 Handle<Object> ConstantArrayBuilder::ConstantArraySlice::At( | 42 Handle<Object> ConstantArrayBuilder::ConstantArraySlice::At( |
47 size_t index) const { | 43 size_t index) const { |
| 44 DCHECK_GE(index, start_index()); |
| 45 DCHECK_LT(index, start_index() + size()); |
48 return constants_[index - start_index()]; | 46 return constants_[index - start_index()]; |
49 } | 47 } |
50 | 48 |
51 | 49 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::k8BitCapacity; |
52 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::kMaxCapacity; | 50 STATIC_CONST_MEMBER_DEFINITION const size_t |
53 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::kLowCapacity; | 51 ConstantArrayBuilder::k16BitCapacity; |
54 | 52 STATIC_CONST_MEMBER_DEFINITION const size_t |
| 53 ConstantArrayBuilder::k32BitCapacity; |
55 | 54 |
56 ConstantArrayBuilder::ConstantArrayBuilder(Isolate* isolate, Zone* zone) | 55 ConstantArrayBuilder::ConstantArrayBuilder(Isolate* isolate, Zone* zone) |
57 : isolate_(isolate), | 56 : isolate_(isolate), constants_map_(isolate->heap(), zone) { |
58 idx8_slice_(zone, 0, kLowCapacity), | 57 idx_slice_[0] = |
59 idx16_slice_(zone, kLowCapacity, kHighCapacity), | 58 new (zone) ConstantArraySlice(zone, 0, k8BitCapacity, OperandSize::kByte); |
60 constants_map_(isolate->heap(), zone) { | 59 idx_slice_[1] = new (zone) ConstantArraySlice( |
61 STATIC_ASSERT(kMaxCapacity == static_cast<size_t>(kMaxUInt16 + 1)); | 60 zone, k8BitCapacity, k16BitCapacity, OperandSize::kShort); |
62 DCHECK_EQ(idx8_slice_.start_index(), 0u); | |
63 DCHECK_EQ(idx8_slice_.capacity(), kLowCapacity); | |
64 DCHECK_EQ(idx16_slice_.start_index(), kLowCapacity); | |
65 DCHECK_EQ(idx16_slice_.capacity(), kMaxCapacity - kLowCapacity); | |
66 } | 61 } |
67 | 62 |
68 | |
69 size_t ConstantArrayBuilder::size() const { | 63 size_t ConstantArrayBuilder::size() const { |
70 if (idx16_slice_.size() > 0) { | 64 size_t i = arraysize(idx_slice_); |
71 return idx16_slice_.start_index() + idx16_slice_.size(); | 65 while (i > 0) { |
72 } else { | 66 ConstantArraySlice* slice = idx_slice_[--i]; |
73 return idx8_slice_.size(); | 67 if (slice->size() > 0) { |
| 68 return slice->start_index() + slice->size(); |
| 69 } |
74 } | 70 } |
| 71 return idx_slice_[0]->size(); |
75 } | 72 } |
76 | 73 |
| 74 const ConstantArrayBuilder::ConstantArraySlice* |
| 75 ConstantArrayBuilder::IndexToSlice(size_t index) const { |
| 76 for (const ConstantArraySlice* slice : idx_slice_) { |
| 77 if (index <= slice->max_index()) { |
| 78 return slice; |
| 79 } |
| 80 } |
| 81 UNREACHABLE(); |
| 82 return nullptr; |
| 83 } |
77 | 84 |
78 Handle<Object> ConstantArrayBuilder::At(size_t index) const { | 85 Handle<Object> ConstantArrayBuilder::At(size_t index) const { |
79 if (index >= idx16_slice_.start_index()) { | 86 const ConstantArraySlice* slice = IndexToSlice(index); |
80 return idx16_slice_.At(index); | 87 if (index < slice->start_index() + slice->size()) { |
81 } else if (index < idx8_slice_.size()) { | 88 return slice->At(index); |
82 return idx8_slice_.At(index); | |
83 } else { | 89 } else { |
84 return isolate_->factory()->the_hole_value(); | 90 return isolate_->factory()->the_hole_value(); |
85 } | 91 } |
86 } | 92 } |
87 | 93 |
88 Handle<FixedArray> ConstantArrayBuilder::ToFixedArray() { | 94 Handle<FixedArray> ConstantArrayBuilder::ToFixedArray() { |
89 Handle<FixedArray> fixed_array = isolate_->factory()->NewFixedArray( | 95 Handle<FixedArray> fixed_array = isolate_->factory()->NewFixedArray( |
90 static_cast<int>(size()), PretenureFlag::TENURED); | 96 static_cast<int>(size()), PretenureFlag::TENURED); |
91 for (int i = 0; i < fixed_array->length(); i++) { | 97 int array_index = 0; |
92 fixed_array->set(i, *At(static_cast<size_t>(i))); | 98 for (const ConstantArraySlice* slice : idx_slice_) { |
| 99 if (array_index == fixed_array->length()) { |
| 100 break; |
| 101 } |
| 102 // Copy objects from slice into array. |
| 103 for (size_t i = 0; i < slice->size(); ++i) { |
| 104 fixed_array->set(array_index++, *slice->At(slice->start_index() + i)); |
| 105 } |
| 106 // Insert holes where reservations led to unused slots. |
| 107 size_t padding = |
| 108 std::min(static_cast<size_t>(fixed_array->length() - array_index), |
| 109 slice->capacity() - slice->size()); |
| 110 for (size_t i = slice->start_index() + slice->size(); |
| 111 i < slice->start_index() + padding; ++i) { |
| 112 fixed_array->set(array_index++, *isolate_->factory()->the_hole_value()); |
| 113 } |
93 } | 114 } |
| 115 DCHECK_EQ(array_index, fixed_array->length()); |
94 constants_map()->Clear(); | 116 constants_map()->Clear(); |
95 return fixed_array; | 117 return fixed_array; |
96 } | 118 } |
97 | 119 |
98 | |
99 size_t ConstantArrayBuilder::Insert(Handle<Object> object) { | 120 size_t ConstantArrayBuilder::Insert(Handle<Object> object) { |
100 index_t* entry = constants_map()->Find(object); | 121 index_t* entry = constants_map()->Find(object); |
101 return (entry == nullptr) ? AllocateEntry(object) : *entry; | 122 return (entry == nullptr) ? AllocateEntry(object) : *entry; |
102 } | 123 } |
103 | 124 |
104 | |
105 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateEntry( | 125 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateEntry( |
106 Handle<Object> object) { | 126 Handle<Object> object) { |
107 DCHECK(!object->IsOddball()); | 127 DCHECK(!object->IsOddball()); |
108 size_t index; | |
109 index_t* entry = constants_map()->Get(object); | 128 index_t* entry = constants_map()->Get(object); |
110 if (idx8_slice_.available() > 0) { | 129 for (size_t i = 0; i < arraysize(idx_slice_); ++i) { |
111 index = idx8_slice_.Allocate(object); | 130 if (idx_slice_[i]->available() > 0) { |
112 } else { | 131 size_t index = idx_slice_[i]->Allocate(object); |
113 index = idx16_slice_.Allocate(object); | 132 *entry = static_cast<index_t>(index); |
| 133 return *entry; |
| 134 break; |
| 135 } |
114 } | 136 } |
115 CHECK_LT(index, kMaxCapacity); | 137 UNREACHABLE(); |
116 *entry = static_cast<index_t>(index); | 138 return kMaxUInt32; |
117 return *entry; | |
118 } | 139 } |
119 | 140 |
120 | |
121 OperandSize ConstantArrayBuilder::CreateReservedEntry() { | 141 OperandSize ConstantArrayBuilder::CreateReservedEntry() { |
122 if (idx8_slice_.available() > 0) { | 142 for (size_t i = 0; i < arraysize(idx_slice_); ++i) { |
123 idx8_slice_.Reserve(); | 143 if (idx_slice_[i]->available() > 0) { |
124 return OperandSize::kByte; | 144 idx_slice_[i]->Reserve(); |
125 } else if (idx16_slice_.available() > 0) { | 145 return idx_slice_[i]->operand_size(); |
126 idx16_slice_.Reserve(); | 146 } |
127 return OperandSize::kShort; | |
128 } else { | |
129 UNREACHABLE(); | |
130 return OperandSize::kNone; | |
131 } | 147 } |
| 148 UNREACHABLE(); |
| 149 return OperandSize::kNone; |
132 } | 150 } |
133 | 151 |
| 152 ConstantArrayBuilder::ConstantArraySlice* |
| 153 ConstantArrayBuilder::OperandSizeToSlice(OperandSize operand_size) const { |
| 154 ConstantArraySlice* slice = nullptr; |
| 155 switch (operand_size) { |
| 156 case OperandSize::kNone: |
| 157 UNREACHABLE(); |
| 158 break; |
| 159 case OperandSize::kByte: |
| 160 slice = idx_slice_[0]; |
| 161 break; |
| 162 case OperandSize::kShort: |
| 163 slice = idx_slice_[1]; |
| 164 break; |
| 165 } |
| 166 DCHECK(slice->operand_size() == operand_size); |
| 167 return slice; |
| 168 } |
134 | 169 |
135 size_t ConstantArrayBuilder::CommitReservedEntry(OperandSize operand_size, | 170 size_t ConstantArrayBuilder::CommitReservedEntry(OperandSize operand_size, |
136 Handle<Object> object) { | 171 Handle<Object> object) { |
137 DiscardReservedEntry(operand_size); | 172 DiscardReservedEntry(operand_size); |
138 size_t index; | 173 size_t index; |
139 index_t* entry = constants_map()->Find(object); | 174 index_t* entry = constants_map()->Find(object); |
140 if (nullptr == entry) { | 175 if (nullptr == entry) { |
141 index = AllocateEntry(object); | 176 index = AllocateEntry(object); |
142 } else { | 177 } else { |
143 if (operand_size == OperandSize::kByte && | 178 ConstantArraySlice* slice = OperandSizeToSlice(operand_size); |
144 *entry >= idx8_slice_.capacity()) { | 179 if (*entry > slice->max_index()) { |
145 // The object is already in the constant array, but has an index | 180 // The object is already in the constant array, but may have an |
146 // outside the range of an idx8 operand so we need to create a | 181 // index too big for the reserved operand_size. So, duplicate |
147 // duplicate entry in the idx8 operand range to satisfy the | 182 // entry with the smaller operand size. |
148 // commitment. | 183 *entry = static_cast<index_t>(slice->Allocate(object)); |
149 *entry = static_cast<index_t>(idx8_slice_.Allocate(object)); | |
150 } | 184 } |
151 index = *entry; | 185 index = *entry; |
152 } | 186 } |
153 DCHECK(operand_size == OperandSize::kShort || index < idx8_slice_.capacity()); | |
154 DCHECK_LT(index, kMaxCapacity); | |
155 return index; | 187 return index; |
156 } | 188 } |
157 | 189 |
158 | |
159 void ConstantArrayBuilder::DiscardReservedEntry(OperandSize operand_size) { | 190 void ConstantArrayBuilder::DiscardReservedEntry(OperandSize operand_size) { |
160 switch (operand_size) { | 191 OperandSizeToSlice(operand_size)->Unreserve(); |
161 case OperandSize::kByte: | |
162 idx8_slice_.Unreserve(); | |
163 return; | |
164 case OperandSize::kShort: | |
165 idx16_slice_.Unreserve(); | |
166 return; | |
167 default: | |
168 UNREACHABLE(); | |
169 } | |
170 } | 192 } |
171 | 193 |
172 } // namespace interpreter | 194 } // namespace interpreter |
173 } // namespace internal | 195 } // namespace internal |
174 } // namespace v8 | 196 } // namespace v8 |
OLD | NEW |