Line data Source code
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 : #ifndef V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_
6 : #define V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_
7 :
8 : #include "src/ast/ast-value-factory.h"
9 : #include "src/globals.h"
10 : #include "src/identity-map.h"
11 : #include "src/interpreter/bytecodes.h"
12 : #include "src/objects/smi.h"
13 : #include "src/zone/zone-containers.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 : class Isolate;
19 : class AstRawString;
20 : class AstValue;
21 :
22 : namespace interpreter {
23 :
24 : // Constant array entries that represent singletons.
25 : #define SINGLETON_CONSTANT_ENTRY_TYPES(V) \
26 : V(AsyncIteratorSymbol, async_iterator_symbol) \
27 : V(ClassFieldsSymbol, class_fields_symbol) \
28 : V(EmptyObjectBoilerplateDescription, empty_object_boilerplate_description) \
29 : V(EmptyArrayBoilerplateDescription, empty_array_boilerplate_description) \
30 : V(EmptyFixedArray, empty_fixed_array) \
31 : V(HomeObjectSymbol, home_object_symbol) \
32 : V(IteratorSymbol, iterator_symbol) \
33 : V(InterpreterTrampolineSymbol, interpreter_trampoline_symbol) \
34 : V(NaN, nan_value)
35 :
36 : // A helper class for constructing constant arrays for the
37 : // interpreter. Each instance of this class is intended to be used to
38 : // generate exactly one FixedArray of constants via the ToFixedArray
39 : // method.
40 4273073 : class V8_EXPORT_PRIVATE ConstantArrayBuilder final {
41 : public:
42 : // Capacity of the 8-bit operand slice.
43 : static const size_t k8BitCapacity = 1u << kBitsPerByte;
44 :
45 : // Capacity of the 16-bit operand slice.
46 : static const size_t k16BitCapacity = (1u << 2 * kBitsPerByte) - k8BitCapacity;
47 :
48 : // Capacity of the 32-bit operand slice.
49 : static const size_t k32BitCapacity =
50 : kMaxUInt32 - k16BitCapacity - k8BitCapacity + 1;
51 :
52 : explicit ConstantArrayBuilder(Zone* zone);
53 :
54 : // Generate a fixed array of constant handles based on inserted objects.
55 : Handle<FixedArray> ToFixedArray(Isolate* isolate);
56 :
57 : // Returns the object, as a handle in |isolate|, that is in the constant pool
58 : // array at index |index|. Returns null if there is no handle at this index.
59 : // Only expected to be used in tests.
60 : MaybeHandle<Object> At(size_t index, Isolate* isolate) const;
61 :
62 : // Returns the number of elements in the array.
63 : size_t size() const;
64 :
65 : // Insert an object into the constants array if it is not already present.
66 : // Returns the array index associated with the object.
67 : size_t Insert(Smi smi);
68 : size_t Insert(double number);
69 : size_t Insert(const AstRawString* raw_string);
70 : size_t Insert(AstBigInt bigint);
71 : size_t Insert(const Scope* scope);
72 : #define INSERT_ENTRY(NAME, ...) size_t Insert##NAME();
73 : SINGLETON_CONSTANT_ENTRY_TYPES(INSERT_ENTRY)
74 : #undef INSERT_ENTRY
75 :
76 : // Inserts an empty entry and returns the array index associated with the
77 : // reservation. The entry's handle value can be inserted by calling
78 : // SetDeferredAt().
79 : size_t InsertDeferred();
80 :
81 : // Inserts |size| consecutive empty entries and returns the array index
82 : // associated with the first reservation. Each entry's Smi value can be
83 : // inserted by calling SetJumpTableSmi().
84 : size_t InsertJumpTable(size_t size);
85 :
86 : // Sets the deferred value at |index| to |object|.
87 : void SetDeferredAt(size_t index, Handle<Object> object);
88 :
89 : // Sets the jump table entry at |index| to |smi|. Note that |index| is the
90 : // constant pool index, not the switch case value.
91 : void SetJumpTableSmi(size_t index, Smi smi);
92 :
93 : // Creates a reserved entry in the constant pool and returns
94 : // the size of the operand that'll be required to hold the entry
95 : // when committed.
96 : OperandSize CreateReservedEntry();
97 :
98 : // Commit reserved entry and returns the constant pool index for the
99 : // SMI value.
100 : size_t CommitReservedEntry(OperandSize operand_size, Smi value);
101 :
102 : // Discards constant pool reservation.
103 : void DiscardReservedEntry(OperandSize operand_size);
104 :
105 : private:
106 : using index_t = uint32_t;
107 :
108 : struct ConstantArraySlice;
109 :
110 : class Entry {
111 : private:
112 : enum class Tag : uint8_t;
113 :
114 : public:
115 : explicit Entry(Smi smi) : smi_(smi), tag_(Tag::kSmi) {}
116 : explicit Entry(double heap_number)
117 : : heap_number_(heap_number), tag_(Tag::kHeapNumber) {}
118 : explicit Entry(const AstRawString* raw_string)
119 : : raw_string_(raw_string), tag_(Tag::kRawString) {}
120 : explicit Entry(AstBigInt bigint) : bigint_(bigint), tag_(Tag::kBigInt) {}
121 : explicit Entry(const Scope* scope) : scope_(scope), tag_(Tag::kScope) {}
122 :
123 : #define CONSTRUCT_ENTRY(NAME, LOWER_NAME) \
124 : static Entry NAME() { return Entry(Tag::k##NAME); }
125 : SINGLETON_CONSTANT_ENTRY_TYPES(CONSTRUCT_ENTRY)
126 : #undef CONSTRUCT_ENTRY
127 :
128 : static Entry Deferred() { return Entry(Tag::kDeferred); }
129 :
130 : static Entry UninitializedJumpTableSmi() {
131 : return Entry(Tag::kUninitializedJumpTableSmi);
132 : }
133 :
134 : bool IsDeferred() const { return tag_ == Tag::kDeferred; }
135 :
136 : bool IsJumpTableEntry() const {
137 : return tag_ == Tag::kUninitializedJumpTableSmi ||
138 : tag_ == Tag::kJumpTableSmi;
139 : }
140 :
141 : void SetDeferred(Handle<Object> handle) {
142 : DCHECK_EQ(tag_, Tag::kDeferred);
143 2943342 : tag_ = Tag::kHandle;
144 2943342 : handle_ = handle;
145 : }
146 :
147 : void SetJumpTableSmi(Smi smi) {
148 : DCHECK_EQ(tag_, Tag::kUninitializedJumpTableSmi);
149 46832 : tag_ = Tag::kJumpTableSmi;
150 46832 : smi_ = smi;
151 : }
152 :
153 : Handle<Object> ToHandle(Isolate* isolate) const;
154 :
155 : private:
156 : explicit Entry(Tag tag) : tag_(tag) {}
157 :
158 : union {
159 : Handle<Object> handle_;
160 : Smi smi_;
161 : double heap_number_;
162 : const AstRawString* raw_string_;
163 : AstBigInt bigint_;
164 : const Scope* scope_;
165 : };
166 :
167 : enum class Tag : uint8_t {
168 : kDeferred,
169 : kHandle,
170 : kSmi,
171 : kRawString,
172 : kHeapNumber,
173 : kBigInt,
174 : kScope,
175 : kUninitializedJumpTableSmi,
176 : kJumpTableSmi,
177 : #define ENTRY_TAG(NAME, ...) k##NAME,
178 : SINGLETON_CONSTANT_ENTRY_TYPES(ENTRY_TAG)
179 : #undef ENTRY_TAG
180 : } tag_;
181 :
182 : #if DEBUG
183 : // Required by CheckAllElementsAreUnique().
184 : friend struct ConstantArraySlice;
185 : #endif
186 : };
187 :
188 : index_t AllocateIndex(Entry constant_entry);
189 : index_t AllocateIndexArray(Entry constant_entry, size_t size);
190 : index_t AllocateReservedEntry(Smi value);
191 :
192 : struct ConstantArraySlice final : public ZoneObject {
193 : ConstantArraySlice(Zone* zone, size_t start_index, size_t capacity,
194 : OperandSize operand_size);
195 : void Reserve();
196 : void Unreserve();
197 : size_t Allocate(Entry entry, size_t count = 1);
198 : Entry& At(size_t index);
199 : const Entry& At(size_t index) const;
200 :
201 : #if DEBUG
202 : void CheckAllElementsAreUnique(Isolate* isolate) const;
203 : #endif
204 :
205 28951818 : inline size_t available() const { return capacity() - reserved() - size(); }
206 : inline size_t reserved() const { return reserved_; }
207 : inline size_t capacity() const { return capacity_; }
208 : inline size_t size() const { return constants_.size(); }
209 : inline size_t start_index() const { return start_index_; }
210 3781643 : inline size_t max_index() const { return start_index_ + capacity() - 1; }
211 : inline OperandSize operand_size() const { return operand_size_; }
212 :
213 : private:
214 : const size_t start_index_;
215 : const size_t capacity_;
216 : size_t reserved_;
217 : OperandSize operand_size_;
218 : ZoneVector<Entry> constants_;
219 :
220 : DISALLOW_COPY_AND_ASSIGN(ConstantArraySlice);
221 : };
222 :
223 : ConstantArraySlice* IndexToSlice(size_t index) const;
224 : ConstantArraySlice* OperandSizeToSlice(OperandSize operand_size) const;
225 :
226 : ConstantArraySlice* idx_slice_[3];
227 : base::TemplateHashMapImpl<intptr_t, index_t,
228 : base::KeyEqualityMatcher<intptr_t>,
229 : ZoneAllocationPolicy>
230 : constants_map_;
231 : ZoneMap<Smi, index_t> smi_map_;
232 : ZoneVector<std::pair<Smi, index_t>> smi_pairs_;
233 : ZoneMap<double, index_t> heap_number_map_;
234 :
235 : #define SINGLETON_ENTRY_FIELD(NAME, LOWER_NAME) int LOWER_NAME##_;
236 : SINGLETON_CONSTANT_ENTRY_TYPES(SINGLETON_ENTRY_FIELD)
237 : #undef SINGLETON_ENTRY_FIELD
238 :
239 : Zone* zone_;
240 : };
241 :
242 : } // namespace interpreter
243 : } // namespace internal
244 : } // namespace v8
245 :
246 : #endif // V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_
|