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