OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "src/interpreter/constant-array-builder.h" | |
6 | |
7 #include "src/isolate.h" | |
8 #include "src/objects-inl.h" | |
9 | |
10 namespace v8 { | |
11 namespace internal { | |
12 namespace interpreter { | |
13 | |
14 ConstantArrayBuilder::ConstantArraySlice::ConstantArraySlice(Zone* zone, | |
15 int width) | |
rmcilroy
2016/01/04 17:05:48
The width thing is a bit complex - I think it woul
oth
2016/01/05 09:30:08
Yes, this shouldn't have been left in.
| |
16 : width_(width), reserved_(0), constants_(zone) {} | |
17 | |
18 | |
19 size_t ConstantArrayBuilder::ConstantArraySlice::reserved() const { | |
20 return reserved_; | |
21 } | |
22 | |
23 | |
24 size_t ConstantArrayBuilder::ConstantArraySlice::size() const { | |
25 return constants_.size(); | |
26 } | |
27 | |
28 | |
29 size_t ConstantArrayBuilder::ConstantArraySlice::free() const { | |
rmcilroy
2016/01/04 17:05:48
free is a bit of an overloaded name. How about rem
oth
2016/01/05 09:30:08
Glass half-full? available() :-)
rmcilroy
2016/01/05 13:46:10
SGTM :).
| |
30 return capacity() - reserved_ - constants_.size(); | |
31 } | |
32 | |
33 | |
34 size_t ConstantArrayBuilder::ConstantArraySlice::start_index() const { | |
35 size_t shift = 8u * (width_ - 1u); | |
36 return (static_cast<size_t>(1) << shift) & ~0xffu; | |
37 } | |
38 | |
39 | |
40 size_t ConstantArrayBuilder::ConstantArraySlice::capacity() const { | |
41 size_t shift = 8u * width_; | |
42 return (static_cast<size_t>(1) << shift) - start_index(); | |
43 } | |
44 | |
45 | |
46 void ConstantArrayBuilder::ConstantArraySlice::reserve() { | |
47 DCHECK_GT(free(), 0u); | |
48 reserved_++; | |
49 DCHECK_LE(reserved_, capacity() - constants_.size()); | |
50 } | |
51 | |
52 | |
53 void ConstantArrayBuilder::ConstantArraySlice::unreserve() { | |
54 DCHECK_GT(reserved_, 0u); | |
55 reserved_--; | |
56 } | |
57 | |
58 | |
59 size_t ConstantArrayBuilder::ConstantArraySlice::allocate( | |
60 Handle<Object> object) { | |
61 DCHECK_GT(free(), 0u); | |
62 size_t index = constants_.size(); | |
63 DCHECK_LT(index, capacity()); | |
64 constants_.push_back(object); | |
65 return index + start_index(); | |
66 } | |
67 | |
68 | |
69 Handle<Object> ConstantArrayBuilder::ConstantArraySlice::at( | |
70 size_t index) const { | |
71 return constants_[index - start_index()]; | |
72 } | |
73 | |
74 | |
75 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::kMaxCapacity; | |
76 STATIC_CONST_MEMBER_DEFINITION const size_t ConstantArrayBuilder::kLowCapacity; | |
rmcilroy
2016/01/04 17:05:48
Do you need these? I didn't think you needed them
oth
2016/01/05 09:30:08
Yes, both external linkage and taking the constant
| |
77 | |
78 | |
79 ConstantArrayBuilder::ConstantArrayBuilder(Isolate* isolate, Zone* zone) | |
80 : isolate_(isolate), | |
81 idx8_slice_(zone, 1), | |
82 idx16_slice_(zone, 2), | |
83 constants_map_(isolate->heap(), zone) { | |
84 DCHECK_EQ(idx8_slice_.start_index(), 0u); | |
85 DCHECK_EQ(idx8_slice_.capacity(), kLowCapacity); | |
86 DCHECK_EQ(idx16_slice_.start_index(), kLowCapacity); | |
87 DCHECK_EQ(idx16_slice_.capacity(), kMaxCapacity - kLowCapacity); | |
88 } | |
89 | |
90 | |
91 size_t ConstantArrayBuilder::size() const { | |
92 if (idx16_slice_.size() > 0) { | |
93 return idx16_slice_.start_index() + idx16_slice_.size(); | |
94 } else { | |
95 return idx8_slice_.size(); | |
96 } | |
97 } | |
98 | |
99 | |
100 Handle<Object> ConstantArrayBuilder::at(size_t index) const { | |
101 if (index >= idx16_slice_.start_index()) { | |
102 return idx16_slice_.at(index); | |
103 } else if (index < idx8_slice_.size()) { | |
104 return idx8_slice_.at(index); | |
105 } else { | |
106 return isolate_->factory()->the_hole_value(); | |
107 } | |
108 } | |
109 | |
110 | |
111 Handle<FixedArray> ConstantArrayBuilder::ToFixedArray( | |
112 Factory* factory, PretenureFlag pretenure) const { | |
113 Handle<FixedArray> fixed_array = | |
114 factory->NewFixedArray(static_cast<int>(size()), pretenure); | |
115 for (int i = 0; i < fixed_array->length(); i++) { | |
116 fixed_array->set(i, *at(static_cast<size_t>(i))); | |
117 } | |
118 return fixed_array; | |
119 } | |
120 | |
121 | |
122 size_t ConstantArrayBuilder::Insert(Handle<Object> object) { | |
123 index_t* entry = constants_map_.Find(object); | |
124 return (entry == nullptr) ? AllocateEntry(object) : *entry; | |
125 } | |
126 | |
127 | |
128 ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateEntry( | |
129 Handle<Object> object) { | |
130 DCHECK(!object->IsOddball()); | |
131 size_t index; | |
132 index_t* entry = constants_map_.Get(object); | |
133 if (idx8_slice_.free() > 0) { | |
134 index = idx8_slice_.allocate(object); | |
135 DCHECK_LT(index, idx8_slice_.capacity()); | |
136 } else { | |
137 index = idx16_slice_.allocate(object); | |
138 DCHECK_LT(index, idx8_slice_.capacity() + idx16_slice_.capacity()); | |
139 } | |
140 *entry = static_cast<index_t>(index); | |
141 return *entry; | |
142 } | |
143 | |
144 | |
145 ConstantArrayBuilder::ReservationToken | |
146 ConstantArrayBuilder::CreateReservedEntry() { | |
147 if (idx8_slice_.free() > 0) { | |
148 idx8_slice_.reserve(); | |
149 return ReservationToken::kIdx8; | |
150 } else { | |
151 idx16_slice_.reserve(); | |
152 return ReservationToken::kIdx16; | |
153 } | |
154 } | |
155 | |
156 | |
157 size_t ConstantArrayBuilder::CommitReservedEntry(ReservationToken token, | |
158 Handle<Object> object) { | |
159 DiscardReservedEntry(token); | |
160 size_t index; | |
161 index_t* entry = constants_map_.Find(object); | |
162 if (nullptr == entry) { | |
163 index = AllocateEntry(object); | |
164 } else { | |
165 if (token == ReservationToken::kIdx8 && *entry >= idx8_slice_.capacity()) { | |
rmcilroy
2016/01/04 17:05:48
Could the opposite happen (token == kIdx16 but we
oth
2016/01/05 09:30:08
If the opposite happens it doesn't matter. The byt
| |
166 // The object is already in the constant array, but has an index | |
167 // outside the range of an idx8 operand so we need to create a | |
168 // duplicate entry in the idx8 operand range to satisfy the | |
169 // commitment. | |
170 *entry = static_cast<index_t>(idx8_slice_.allocate(object)); | |
171 } | |
172 index = *entry; | |
173 } | |
174 DCHECK(token == ReservationToken::kIdx16 || index < idx8_slice_.capacity()); | |
175 DCHECK_LT(index, kMaxCapacity); | |
176 return index; | |
177 } | |
178 | |
179 | |
180 void ConstantArrayBuilder::DiscardReservedEntry(ReservationToken token) { | |
181 switch (token) { | |
182 case ReservationToken::kIdx8: | |
183 idx8_slice_.unreserve(); | |
184 return; | |
185 case ReservationToken::kIdx16: | |
186 idx16_slice_.unreserve(); | |
187 return; | |
188 } | |
189 } | |
190 | |
191 } // namespace interpreter | |
192 } // namespace internal | |
193 } // namespace v8 | |
OLD | NEW |