Line data Source code
1 : // Copyright 2012 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_PROPERTY_DETAILS_H_
6 : #define V8_PROPERTY_DETAILS_H_
7 :
8 : #include "include/v8.h"
9 : #include "src/allocation.h"
10 : // TODO(ishell): remove once FLAG_track_constant_fields is removed.
11 : #include "src/flags.h"
12 : #include "src/utils.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 :
17 : // ES6 6.1.7.1
18 : enum PropertyAttributes {
19 : NONE = ::v8::None,
20 : READ_ONLY = ::v8::ReadOnly,
21 : DONT_ENUM = ::v8::DontEnum,
22 : DONT_DELETE = ::v8::DontDelete,
23 :
24 : ALL_ATTRIBUTES_MASK = READ_ONLY | DONT_ENUM | DONT_DELETE,
25 :
26 : SEALED = DONT_DELETE,
27 : FROZEN = SEALED | READ_ONLY,
28 :
29 : ABSENT = 64, // Used in runtime to indicate a property is absent.
30 : // ABSENT can never be stored in or returned from a descriptor's attributes
31 : // bitfield. It is only used as a return value meaning the attributes of
32 : // a non-existent property.
33 : };
34 :
35 :
36 : enum PropertyFilter {
37 : ALL_PROPERTIES = 0,
38 : ONLY_WRITABLE = 1,
39 : ONLY_ENUMERABLE = 2,
40 : ONLY_CONFIGURABLE = 4,
41 : SKIP_STRINGS = 8,
42 : SKIP_SYMBOLS = 16,
43 : ONLY_ALL_CAN_READ = 32,
44 : ENUMERABLE_STRINGS = ONLY_ENUMERABLE | SKIP_SYMBOLS,
45 : };
46 : // Enable fast comparisons of PropertyAttributes against PropertyFilters.
47 : STATIC_ASSERT(ALL_PROPERTIES == static_cast<PropertyFilter>(NONE));
48 : STATIC_ASSERT(ONLY_WRITABLE == static_cast<PropertyFilter>(READ_ONLY));
49 : STATIC_ASSERT(ONLY_ENUMERABLE == static_cast<PropertyFilter>(DONT_ENUM));
50 : STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(DONT_DELETE));
51 : STATIC_ASSERT(((SKIP_STRINGS | SKIP_SYMBOLS | ONLY_ALL_CAN_READ) &
52 : ALL_ATTRIBUTES_MASK) == 0);
53 : STATIC_ASSERT(ALL_PROPERTIES ==
54 : static_cast<PropertyFilter>(v8::PropertyFilter::ALL_PROPERTIES));
55 : STATIC_ASSERT(ONLY_WRITABLE ==
56 : static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_WRITABLE));
57 : STATIC_ASSERT(ONLY_ENUMERABLE ==
58 : static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_ENUMERABLE));
59 : STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(
60 : v8::PropertyFilter::ONLY_CONFIGURABLE));
61 : STATIC_ASSERT(SKIP_STRINGS ==
62 : static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_STRINGS));
63 : STATIC_ASSERT(SKIP_SYMBOLS ==
64 : static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_SYMBOLS));
65 :
66 : class Smi;
67 : class TypeInfo;
68 :
69 : // Order of kinds is significant.
70 : // Must fit in the BitField PropertyDetails::KindField.
71 : enum PropertyKind { kData = 0, kAccessor = 1 };
72 :
73 : // Order of modes is significant.
74 : // Must fit in the BitField PropertyDetails::LocationField.
75 : enum PropertyLocation { kField = 0, kDescriptor = 1 };
76 :
77 : // Order of modes is significant.
78 : // Must fit in the BitField PropertyDetails::ConstnessField.
79 : enum class PropertyConstness { kMutable = 0, kConst = 1 };
80 :
81 : // TODO(ishell): remove once constant field tracking is done.
82 : const PropertyConstness kDefaultFieldConstness =
83 : FLAG_track_constant_fields ? PropertyConstness::kConst
84 : : PropertyConstness::kMutable;
85 :
86 : class Representation {
87 : public:
88 : enum Kind {
89 : kNone,
90 : kInteger8,
91 : kUInteger8,
92 : kInteger16,
93 : kUInteger16,
94 : kSmi,
95 : kInteger32,
96 : kDouble,
97 : kHeapObject,
98 : kTagged,
99 : kExternal,
100 : kNumRepresentations
101 : };
102 :
103 4500 : Representation() : kind_(kNone) { }
104 :
105 : static Representation None() { return Representation(kNone); }
106 : static Representation Tagged() { return Representation(kTagged); }
107 : static Representation Integer8() { return Representation(kInteger8); }
108 : static Representation UInteger8() { return Representation(kUInteger8); }
109 : static Representation Integer16() { return Representation(kInteger16); }
110 : static Representation UInteger16() { return Representation(kUInteger16); }
111 : static Representation Smi() { return Representation(kSmi); }
112 : static Representation Integer32() { return Representation(kInteger32); }
113 : static Representation Double() { return Representation(kDouble); }
114 : static Representation HeapObject() { return Representation(kHeapObject); }
115 : static Representation External() { return Representation(kExternal); }
116 :
117 : static Representation FromKind(Kind kind) { return Representation(kind); }
118 :
119 : bool Equals(const Representation& other) const {
120 562232 : return kind_ == other.kind_;
121 : }
122 :
123 : bool IsCompatibleForLoad(const Representation& other) const {
124 : return (IsDouble() && other.IsDouble()) ||
125 : (!IsDouble() && !other.IsDouble());
126 : }
127 :
128 : bool IsCompatibleForStore(const Representation& other) const {
129 : return Equals(other);
130 : }
131 :
132 615616 : bool is_more_general_than(const Representation& other) const {
133 615616 : if (kind_ == kExternal && other.kind_ == kNone) return true;
134 615614 : if (kind_ == kExternal && other.kind_ == kExternal) return false;
135 615609 : if (kind_ == kNone && other.kind_ == kExternal) return false;
136 :
137 : DCHECK_NE(kind_, kExternal);
138 : DCHECK_NE(other.kind_, kExternal);
139 957995 : if (IsHeapObject()) return other.IsNone();
140 273213 : if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
141 273208 : if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
142 273203 : return kind_ > other.kind_;
143 : }
144 :
145 : bool fits_into(const Representation& other) const {
146 731107 : return other.is_more_general_than(*this) || other.Equals(*this);
147 : }
148 :
149 349071 : Representation generalize(Representation other) {
150 349072 : if (other.fits_into(*this)) return *this;
151 43059 : if (other.is_more_general_than(*this)) return other;
152 : return Representation::Tagged();
153 : }
154 :
155 : int size() const {
156 : DCHECK(!IsNone());
157 : if (IsInteger8() || IsUInteger8()) return kUInt8Size;
158 : if (IsInteger16() || IsUInteger16()) return kUInt16Size;
159 : if (IsInteger32()) return kInt32Size;
160 : if (IsDouble()) return kDoubleSize;
161 : if (IsExternal()) return kSystemPointerSize;
162 : DCHECK(IsTagged() || IsSmi() || IsHeapObject());
163 : return kTaggedSize;
164 : }
165 :
166 133093144 : Kind kind() const { return static_cast<Kind>(kind_); }
167 620925 : bool IsNone() const { return kind_ == kNone; }
168 : bool IsInteger8() const { return kind_ == kInteger8; }
169 : bool IsUInteger8() const { return kind_ == kUInteger8; }
170 : bool IsInteger16() const { return kind_ == kInteger16; }
171 : bool IsUInteger16() const { return kind_ == kUInteger16; }
172 : bool IsTagged() const { return kind_ == kTagged; }
173 : bool IsSmi() const { return kind_ == kSmi; }
174 : bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
175 : bool IsInteger32() const { return kind_ == kInteger32; }
176 : bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
177 135854680 : bool IsDouble() const { return kind_ == kDouble; }
178 22502 : bool IsHeapObject() const { return kind_ == kHeapObject; }
179 : bool IsExternal() const { return kind_ == kExternal; }
180 : bool IsSpecialization() const {
181 : return IsInteger8() || IsUInteger8() ||
182 : IsInteger16() || IsUInteger16() ||
183 : IsSmi() || IsInteger32() || IsDouble();
184 : }
185 : const char* Mnemonic() const;
186 :
187 : private:
188 511215783 : explicit Representation(Kind k) : kind_(k) { }
189 :
190 : // Make sure kind fits in int8.
191 : STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
192 :
193 : int8_t kind_;
194 : };
195 :
196 :
197 : static const int kDescriptorIndexBitCount = 10;
198 : static const int kFirstInobjectPropertyOffsetBitCount = 7;
199 : // The maximum number of descriptors we want in a descriptor array. It should
200 : // fit in a page and also the following should hold:
201 : // kMaxNumberOfDescriptors + kFieldsAdded <= PropertyArray::kMaxLength.
202 : static const int kMaxNumberOfDescriptors = (1 << kDescriptorIndexBitCount) - 4;
203 : static const int kInvalidEnumCacheSentinel =
204 : (1 << kDescriptorIndexBitCount) - 1;
205 :
206 : enum class PropertyCellType {
207 : // Meaningful when a property cell does not contain the hole.
208 : kUndefined, // The PREMONOMORPHIC of property cells.
209 : kConstant, // Cell has been assigned only once.
210 : kConstantType, // Cell has been assigned only one type.
211 : kMutable, // Cell will no longer be tracked as constant.
212 :
213 : // Meaningful when a property cell contains the hole.
214 : kUninitialized = kUndefined, // Cell has never been initialized.
215 : kInvalidated = kConstant, // Cell has been deleted, invalidated or never
216 : // existed.
217 :
218 : // For dictionaries not holding cells.
219 : kNoCell = kMutable,
220 : };
221 :
222 : enum class PropertyCellConstantType {
223 : kSmi,
224 : kStableMap,
225 : };
226 :
227 :
228 : // PropertyDetails captures type and attributes for a property.
229 : // They are used both in property dictionaries and instance descriptors.
230 : class PropertyDetails {
231 : public:
232 : // Property details for dictionary mode properties/elements.
233 60036 : PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
234 : PropertyCellType cell_type, int dictionary_index = 0) {
235 3062695 : value_ = KindField::encode(kind) | LocationField::encode(kField) |
236 8353047 : AttributesField::encode(attributes) |
237 17604625 : DictionaryStorageField::encode(dictionary_index) |
238 12442807 : PropertyCellTypeField::encode(cell_type);
239 60036 : }
240 :
241 : // Property details for fast mode properties.
242 : PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
243 : PropertyLocation location, PropertyConstness constness,
244 : Representation representation, int field_index = 0) {
245 278208 : value_ = KindField::encode(kind) | AttributesField::encode(attributes) |
246 20117581 : LocationField::encode(location) |
247 20150090 : ConstnessField::encode(constness) |
248 36763093 : RepresentationField::encode(EncodeRepresentation(representation)) |
249 29779757 : FieldIndexField::encode(field_index);
250 : }
251 :
252 : static PropertyDetails Empty(
253 : PropertyCellType cell_type = PropertyCellType::kNoCell) {
254 : return PropertyDetails(kData, NONE, cell_type);
255 : }
256 :
257 2754695972 : int pointer() const { return DescriptorPointer::decode(value_); }
258 :
259 : PropertyDetails set_pointer(int i) const {
260 755068085 : return PropertyDetails(value_, i);
261 : }
262 :
263 : PropertyDetails set_cell_type(PropertyCellType type) const {
264 8238117 : PropertyDetails details = *this;
265 : details.value_ = PropertyCellTypeField::update(details.value_, type);
266 : return details;
267 : }
268 :
269 : PropertyDetails set_index(int index) const {
270 0 : PropertyDetails details = *this;
271 18682872 : details.value_ = DictionaryStorageField::update(details.value_, index);
272 : return details;
273 : }
274 :
275 : PropertyDetails CopyWithRepresentation(Representation representation) const {
276 : return PropertyDetails(value_, representation);
277 : }
278 : PropertyDetails CopyWithConstness(PropertyConstness constness) const {
279 : return PropertyDetails(value_, constness);
280 : }
281 : PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) const {
282 : new_attributes =
283 277920 : static_cast<PropertyAttributes>(attributes() | new_attributes);
284 : return PropertyDetails(value_, new_attributes);
285 : }
286 :
287 : // Conversion for storing details as Object.
288 : explicit inline PropertyDetails(Smi smi);
289 : inline Smi AsSmi() const;
290 :
291 : static uint8_t EncodeRepresentation(Representation representation) {
292 : return representation.kind();
293 : }
294 :
295 : static Representation DecodeRepresentation(uint32_t bits) {
296 : return Representation::FromKind(static_cast<Representation::Kind>(bits));
297 : }
298 :
299 57139003 : PropertyKind kind() const { return KindField::decode(value_); }
300 417126848 : PropertyLocation location() const { return LocationField::decode(value_); }
301 48668 : PropertyConstness constness() const { return ConstnessField::decode(value_); }
302 :
303 : PropertyAttributes attributes() const {
304 50283778 : return AttributesField::decode(value_);
305 : }
306 :
307 : bool HasKindAndAttributes(PropertyKind kind, PropertyAttributes attributes) {
308 5363537 : return (value_ & (KindField::kMask | AttributesField::kMask)) ==
309 5363537 : (KindField::encode(kind) | AttributesField::encode(attributes));
310 : }
311 :
312 : int dictionary_index() const {
313 363402873 : return DictionaryStorageField::decode(value_);
314 : }
315 :
316 45598368 : Representation representation() const {
317 245870573 : return DecodeRepresentation(RepresentationField::decode(value_));
318 : }
319 :
320 496139226 : int field_index() const { return FieldIndexField::decode(value_); }
321 :
322 : inline int field_width_in_words() const;
323 :
324 : static bool IsValidIndex(int index) {
325 14210586 : return DictionaryStorageField::is_valid(index);
326 : }
327 :
328 56402499 : bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
329 9830035 : bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
330 9423519 : bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
331 : bool IsEnumerable() const { return !IsDontEnum(); }
332 : PropertyCellType cell_type() const {
333 14552093 : return PropertyCellTypeField::decode(value_);
334 : }
335 :
336 : // Bit fields in value_ (type, shift, size). Must be public so the
337 : // constants can be embedded in generated code.
338 : class KindField : public BitField<PropertyKind, 0, 1> {};
339 : class LocationField : public BitField<PropertyLocation, KindField::kNext, 1> {
340 : };
341 : class ConstnessField
342 : : public BitField<PropertyConstness, LocationField::kNext, 1> {};
343 : class AttributesField
344 : : public BitField<PropertyAttributes, ConstnessField::kNext, 3> {};
345 : static const int kAttributesReadOnlyMask =
346 : (READ_ONLY << AttributesField::kShift);
347 : static const int kAttributesDontDeleteMask =
348 : (DONT_DELETE << AttributesField::kShift);
349 : static const int kAttributesDontEnumMask =
350 : (DONT_ENUM << AttributesField::kShift);
351 :
352 : // Bit fields for normalized objects.
353 : class PropertyCellTypeField
354 : : public BitField<PropertyCellType, AttributesField::kNext, 2> {};
355 : class DictionaryStorageField
356 : : public BitField<uint32_t, PropertyCellTypeField::kNext, 23> {};
357 :
358 : // Bit fields for fast objects.
359 : class RepresentationField
360 : : public BitField<uint32_t, AttributesField::kNext, 4> {};
361 : class DescriptorPointer
362 : : public BitField<uint32_t, RepresentationField::kNext,
363 : kDescriptorIndexBitCount> {}; // NOLINT
364 : class FieldIndexField : public BitField<uint32_t, DescriptorPointer::kNext,
365 : kDescriptorIndexBitCount> {
366 : }; // NOLINT
367 :
368 : // All bits for both fast and slow objects must fit in a smi.
369 : STATIC_ASSERT(DictionaryStorageField::kNext <= 31);
370 : STATIC_ASSERT(FieldIndexField::kNext <= 31);
371 :
372 : static const int kInitialIndex = 1;
373 :
374 : #ifdef OBJECT_PRINT
375 : // For our gdb macros, we should perhaps change these in the future.
376 : void Print(bool dictionary_mode);
377 : #endif
378 :
379 : enum PrintMode {
380 : kPrintAttributes = 1 << 0,
381 : kPrintFieldIndex = 1 << 1,
382 : kPrintRepresentation = 1 << 2,
383 : kPrintPointer = 1 << 3,
384 :
385 : kForProperties = kPrintFieldIndex,
386 : kForTransitions = kPrintAttributes,
387 : kPrintFull = -1,
388 : };
389 : void PrintAsSlowTo(std::ostream& out);
390 : void PrintAsFastTo(std::ostream& out, PrintMode mode = kPrintFull);
391 :
392 : private:
393 : PropertyDetails(int value, int pointer) {
394 754828891 : value_ = DescriptorPointer::update(value, pointer);
395 : }
396 : PropertyDetails(int value, Representation representation) {
397 : value_ = RepresentationField::update(
398 390155 : value, EncodeRepresentation(representation));
399 : }
400 : PropertyDetails(int value, PropertyConstness constness) {
401 : value_ = ConstnessField::update(value, constness);
402 : }
403 : PropertyDetails(int value, PropertyAttributes attributes) {
404 : value_ = AttributesField::update(value, attributes);
405 : }
406 :
407 : uint32_t value_;
408 : };
409 :
410 : // kField location is more general than kDescriptor, kDescriptor generalizes
411 : // only to itself.
412 : inline bool IsGeneralizableTo(PropertyLocation a, PropertyLocation b) {
413 220063 : return b == kField || a == kDescriptor;
414 : }
415 :
416 : // PropertyConstness::kMutable constness is more general than
417 : // VariableMode::kConst, VariableMode::kConst generalizes only to itself.
418 : inline bool IsGeneralizableTo(PropertyConstness a, PropertyConstness b) {
419 6734825 : return b == PropertyConstness::kMutable || a == PropertyConstness::kConst;
420 : }
421 :
422 : inline PropertyConstness GeneralizeConstness(PropertyConstness a,
423 : PropertyConstness b) {
424 349072 : return a == PropertyConstness::kMutable ? PropertyConstness::kMutable : b;
425 : }
426 :
427 : std::ostream& operator<<(std::ostream& os,
428 : const PropertyAttributes& attributes);
429 : } // namespace internal
430 : } // namespace v8
431 :
432 : #endif // V8_PROPERTY_DETAILS_H_
|