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