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_OBJECTS_PROPERTY_DETAILS_H_
6 #define V8_OBJECTS_PROPERTY_DETAILS_H_
7
8 #include "include/v8-object.h"
9 #include "src/base/bit-field.h"
10 #include "src/common/globals.h"
11 #include "src/flags/flags.h"
12 #include "src/utils/allocation.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 // Number of distinct bits in PropertyAttributes.
36 static const int kPropertyAttributesBitsCount = 3;
37
38 static const int kPropertyAttributesCombinationsCount =
39 1 << kPropertyAttributesBitsCount;
40
41 enum PropertyFilter {
42 ALL_PROPERTIES = 0,
43 ONLY_WRITABLE = 1,
44 ONLY_ENUMERABLE = 2,
45 ONLY_CONFIGURABLE = 4,
46 SKIP_STRINGS = 8,
47 SKIP_SYMBOLS = 16,
48 ONLY_ALL_CAN_READ = 32,
49 PRIVATE_NAMES_ONLY = 64,
50 ENUMERABLE_STRINGS = ONLY_ENUMERABLE | SKIP_SYMBOLS,
51 };
52 // Enable fast comparisons of PropertyAttributes against PropertyFilters.
53 STATIC_ASSERT(ALL_PROPERTIES == static_cast<PropertyFilter>(NONE));
54 STATIC_ASSERT(ONLY_WRITABLE == static_cast<PropertyFilter>(READ_ONLY));
55 STATIC_ASSERT(ONLY_ENUMERABLE == static_cast<PropertyFilter>(DONT_ENUM));
56 STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(DONT_DELETE));
57 STATIC_ASSERT(((SKIP_STRINGS | SKIP_SYMBOLS | ONLY_ALL_CAN_READ) &
58 ALL_ATTRIBUTES_MASK) == 0);
59 STATIC_ASSERT(ALL_PROPERTIES ==
60 static_cast<PropertyFilter>(v8::PropertyFilter::ALL_PROPERTIES));
61 STATIC_ASSERT(ONLY_WRITABLE ==
62 static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_WRITABLE));
63 STATIC_ASSERT(ONLY_ENUMERABLE ==
64 static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_ENUMERABLE));
65 STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(
66 v8::PropertyFilter::ONLY_CONFIGURABLE));
67 STATIC_ASSERT(SKIP_STRINGS ==
68 static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_STRINGS));
69 STATIC_ASSERT(SKIP_SYMBOLS ==
70 static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_SYMBOLS));
71
72 // Assert that kPropertyAttributesBitsCount value matches the definition of
73 // ALL_ATTRIBUTES_MASK.
74 STATIC_ASSERT((ALL_ATTRIBUTES_MASK == (READ_ONLY | DONT_ENUM | DONT_DELETE)) ==
75 (kPropertyAttributesBitsCount == 3));
76
77 class Smi;
78 class TypeInfo;
79
80 // Order of kinds is significant.
81 // Must fit in the BitField PropertyDetails::KindField.
82 enum PropertyKind { kData = 0, kAccessor = 1 };
83
84 // Order of modes is significant.
85 // Must fit in the BitField PropertyDetails::LocationField.
86 enum class PropertyLocation { kField = 0, kDescriptor = 1 };
87
88 // Order of modes is significant.
89 // Must fit in the BitField PropertyDetails::ConstnessField.
90 enum class PropertyConstness { kMutable = 0, kConst = 1 };
91
92 class Representation {
93 public:
94 enum Kind {
95 kNone,
96 kSmi,
97 kDouble,
98 kHeapObject,
99 kTagged,
100 // This representation is used for WasmObject fields and basically means
101 // that the actual field type information must be taken from the Wasm RTT
102 // associated with the map.
103 kWasmValue,
104 kNumRepresentations
105 };
106
Representation()107 Representation() : kind_(kNone) {}
108
None()109 static Representation None() { return Representation(kNone); }
Tagged()110 static Representation Tagged() { return Representation(kTagged); }
Smi()111 static Representation Smi() { return Representation(kSmi); }
Double()112 static Representation Double() { return Representation(kDouble); }
HeapObject()113 static Representation HeapObject() { return Representation(kHeapObject); }
WasmValue()114 static Representation WasmValue() { return Representation(kWasmValue); }
115
FromKind(Kind kind)116 static Representation FromKind(Kind kind) { return Representation(kind); }
117
Equals(const Representation & other)118 bool Equals(const Representation& other) const {
119 return kind_ == other.kind_;
120 }
121
IsCompatibleForLoad(const Representation & other)122 bool IsCompatibleForLoad(const Representation& other) const {
123 return IsDouble() == other.IsDouble();
124 }
125
IsCompatibleForStore(const Representation & other)126 bool IsCompatibleForStore(const Representation& other) const {
127 return Equals(other);
128 }
129
130 // Returns true if a change from this representation to a more general one
131 // might cause a map deprecation.
MightCauseMapDeprecation()132 bool MightCauseMapDeprecation() const {
133 // HeapObject to tagged representation change can be done in-place.
134 // Boxed double to tagged transition is always done in-place.
135 // Note that WasmValue is not supposed to be changed at all (the only
136 // representation it fits into is WasmValue), so for the sake of predicate
137 // correctness we treat it as in-place "changeable".
138 if (IsTagged() || IsHeapObject() || IsDouble() || IsWasmValue()) {
139 return false;
140 }
141 // None to double and smi to double representation changes require
142 // deprecation, because doubles might require box allocation, see
143 // CanBeInPlaceChangedTo().
144 DCHECK(IsNone() || IsSmi());
145 return true;
146 }
147
CanBeInPlaceChangedTo(const Representation & other)148 bool CanBeInPlaceChangedTo(const Representation& other) const {
149 if (Equals(other)) return true;
150 if (IsWasmValue() || other.IsWasmValue()) return false;
151 // If it's just a representation generalization case (i.e. property kind and
152 // attributes stays unchanged) it's fine to transition from None to anything
153 // but double without any modification to the object, because the default
154 // uninitialized value for representation None can be overwritten by both
155 // smi and tagged values. Doubles, however, would require a box allocation.
156 if (IsNone()) return !other.IsDouble();
157 if (!other.IsTagged()) return false;
158 DCHECK(IsSmi() || IsDouble() || IsHeapObject());
159 return true;
160 }
161
162 // Return the most generic representation that this representation can be
163 // changed to in-place. If an in-place representation change is not allowed,
164 // then this will return the current representation.
MostGenericInPlaceChange()165 Representation MostGenericInPlaceChange() const {
166 if (IsWasmValue()) return Representation::WasmValue();
167 return Representation::Tagged();
168 }
169
is_more_general_than(const Representation & other)170 bool is_more_general_than(const Representation& other) const {
171 if (IsWasmValue()) return false;
172 if (IsHeapObject()) return other.IsNone();
173 return kind_ > other.kind_;
174 }
175
fits_into(const Representation & other)176 bool fits_into(const Representation& other) const {
177 return other.is_more_general_than(*this) || other.Equals(*this);
178 }
179
generalize(Representation other)180 Representation generalize(Representation other) {
181 if (other.fits_into(*this)) return *this;
182 if (other.is_more_general_than(*this)) return other;
183 return Representation::Tagged();
184 }
185
size()186 int size() const {
187 DCHECK(!IsNone());
188 if (IsDouble()) return kDoubleSize;
189 DCHECK(IsTagged() || IsSmi() || IsHeapObject());
190 return kTaggedSize;
191 }
192
kind()193 Kind kind() const { return static_cast<Kind>(kind_); }
IsNone()194 bool IsNone() const { return kind_ == kNone; }
IsWasmValue()195 bool IsWasmValue() const { return kind_ == kWasmValue; }
IsTagged()196 bool IsTagged() const { return kind_ == kTagged; }
IsSmi()197 bool IsSmi() const { return kind_ == kSmi; }
IsSmiOrTagged()198 bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
IsDouble()199 bool IsDouble() const { return kind_ == kDouble; }
IsHeapObject()200 bool IsHeapObject() const { return kind_ == kHeapObject; }
201
Mnemonic()202 const char* Mnemonic() const {
203 switch (kind_) {
204 case kNone:
205 return "v";
206 case kTagged:
207 return "t";
208 case kSmi:
209 return "s";
210 case kDouble:
211 return "d";
212 case kHeapObject:
213 return "h";
214 case kWasmValue:
215 return "w";
216 }
217 UNREACHABLE();
218 }
219
220 private:
Representation(Kind k)221 explicit Representation(Kind k) : kind_(k) {}
222
223 // Make sure kind fits in int8.
224 STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
225
226 int8_t kind_;
227 };
228
229 static const int kDescriptorIndexBitCount = 10;
230 static const int kFirstInobjectPropertyOffsetBitCount = 7;
231 // The maximum number of descriptors we want in a descriptor array. It should
232 // fit in a page and also the following should hold:
233 // kMaxNumberOfDescriptors + kFieldsAdded <= PropertyArray::kMaxLength.
234 static const int kMaxNumberOfDescriptors = (1 << kDescriptorIndexBitCount) - 4;
235 static const int kInvalidEnumCacheSentinel =
236 (1 << kDescriptorIndexBitCount) - 1;
237
238 // A PropertyCell's property details contains a cell type that is meaningful if
239 // the cell is still valid (does not hold the hole).
240 enum class PropertyCellType {
241 kMutable, // Cell will no longer be tracked as constant.
242 kUndefined, // The PREMONOMORPHIC of property cells.
243 kConstant, // Cell has been assigned only once.
244 kConstantType, // Cell has been assigned only one type.
245 // Temporary value indicating an ongoing property cell state transition. Only
246 // observable by a background thread.
247 kInTransition,
248 // Value for dictionaries not holding cells, must be 0:
249 kNoCell = kMutable,
250 };
251
252 // PropertyDetails captures type and attributes for a property.
253 // They are used both in property dictionaries and instance descriptors.
254 class PropertyDetails {
255 public:
256 // Property details for global dictionary properties.
257 PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
258 PropertyCellType cell_type, int dictionary_index = 0) {
259 value_ = KindField::encode(kind) |
260 LocationField::encode(PropertyLocation::kField) |
261 AttributesField::encode(attributes) |
262 // We track PropertyCell constness via PropertyCellTypeField,
263 // so we set ConstnessField to kMutable to simplify DCHECKs related
264 // to non-global property constness tracking.
265 ConstnessField::encode(PropertyConstness::kMutable) |
266 DictionaryStorageField::encode(dictionary_index) |
267 PropertyCellTypeField::encode(cell_type);
268 }
269
270 // Property details for dictionary mode properties/elements.
271 PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
272 PropertyConstness constness, int dictionary_index = 0) {
273 value_ = KindField::encode(kind) |
274 LocationField::encode(PropertyLocation::kField) |
275 AttributesField::encode(attributes) |
276 ConstnessField::encode(constness) |
277 DictionaryStorageField::encode(dictionary_index) |
278 PropertyCellTypeField::encode(PropertyCellType::kNoCell);
279 }
280
281 // Property details for fast mode properties.
282 PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
283 PropertyLocation location, PropertyConstness constness,
284 Representation representation, int field_index = 0) {
285 value_ = KindField::encode(kind) | AttributesField::encode(attributes) |
286 LocationField::encode(location) |
287 ConstnessField::encode(constness) |
288 RepresentationField::encode(EncodeRepresentation(representation)) |
289 FieldIndexField::encode(field_index);
290 }
291
292 static PropertyDetails Empty(
293 PropertyCellType cell_type = PropertyCellType::kNoCell) {
294 return PropertyDetails(kData, NONE, cell_type);
295 }
296
297 bool operator==(PropertyDetails const& other) {
298 return value_ == other.value_;
299 }
300
301 bool operator!=(PropertyDetails const& other) {
302 return value_ != other.value_;
303 }
304
pointer()305 int pointer() const { return DescriptorPointer::decode(value_); }
306
set_pointer(int i)307 PropertyDetails set_pointer(int i) const {
308 return PropertyDetails(value_, i);
309 }
310
set_cell_type(PropertyCellType type)311 PropertyDetails set_cell_type(PropertyCellType type) const {
312 PropertyDetails details = *this;
313 details.value_ = PropertyCellTypeField::update(details.value_, type);
314 return details;
315 }
316
set_index(int index)317 PropertyDetails set_index(int index) const {
318 PropertyDetails details = *this;
319 details.value_ = DictionaryStorageField::update(details.value_, index);
320 return details;
321 }
322
CopyWithRepresentation(Representation representation)323 PropertyDetails CopyWithRepresentation(Representation representation) const {
324 return PropertyDetails(value_, representation);
325 }
CopyWithConstness(PropertyConstness constness)326 PropertyDetails CopyWithConstness(PropertyConstness constness) const {
327 return PropertyDetails(value_, constness);
328 }
CopyAddAttributes(PropertyAttributes new_attributes)329 PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) const {
330 new_attributes =
331 static_cast<PropertyAttributes>(attributes() | new_attributes);
332 return PropertyDetails(value_, new_attributes);
333 }
334
335 // Conversion for storing details as Object.
336 explicit inline PropertyDetails(Smi smi);
337 inline Smi AsSmi() const;
338
EncodeRepresentation(Representation representation)339 static uint8_t EncodeRepresentation(Representation representation) {
340 return representation.kind();
341 }
342
DecodeRepresentation(uint32_t bits)343 static Representation DecodeRepresentation(uint32_t bits) {
344 return Representation::FromKind(static_cast<Representation::Kind>(bits));
345 }
346
kind()347 PropertyKind kind() const { return KindField::decode(value_); }
location()348 PropertyLocation location() const { return LocationField::decode(value_); }
constness()349 PropertyConstness constness() const { return ConstnessField::decode(value_); }
350
attributes()351 PropertyAttributes attributes() const {
352 return AttributesField::decode(value_);
353 }
354
HasKindAndAttributes(PropertyKind kind,PropertyAttributes attributes)355 bool HasKindAndAttributes(PropertyKind kind, PropertyAttributes attributes) {
356 return (value_ & (KindField::kMask | AttributesField::kMask)) ==
357 (KindField::encode(kind) | AttributesField::encode(attributes));
358 }
359
dictionary_index()360 int dictionary_index() const {
361 return DictionaryStorageField::decode(value_);
362 }
363
representation()364 Representation representation() const {
365 return DecodeRepresentation(RepresentationField::decode(value_));
366 }
367
field_index()368 int field_index() const { return FieldIndexField::decode(value_); }
369
370 inline int field_width_in_words() const;
371
IsValidIndex(int index)372 static bool IsValidIndex(int index) {
373 return DictionaryStorageField::is_valid(index);
374 }
375
IsReadOnly()376 bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
IsConfigurable()377 bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
IsDontEnum()378 bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
IsEnumerable()379 bool IsEnumerable() const { return !IsDontEnum(); }
cell_type()380 PropertyCellType cell_type() const {
381 return PropertyCellTypeField::decode(value_);
382 }
383
384 bool operator==(const PropertyDetails& b) const { return value_ == b.value_; }
385
386 // Bit fields in value_ (type, shift, size). Must be public so the
387 // constants can be embedded in generated code.
388 using KindField = base::BitField<PropertyKind, 0, 1>;
389 using ConstnessField = KindField::Next<PropertyConstness, 1>;
390 using AttributesField = ConstnessField::Next<PropertyAttributes, 3>;
391 static const int kAttributesReadOnlyMask =
392 (READ_ONLY << AttributesField::kShift);
393 static const int kAttributesDontDeleteMask =
394 (DONT_DELETE << AttributesField::kShift);
395 static const int kAttributesDontEnumMask =
396 (DONT_ENUM << AttributesField::kShift);
397
398 // Bit fields for normalized/dictionary mode objects.
399 using PropertyCellTypeField = AttributesField::Next<PropertyCellType, 3>;
400 using DictionaryStorageField = PropertyCellTypeField::Next<uint32_t, 23>;
401
402 // Bit fields for fast objects.
403 using LocationField = AttributesField::Next<PropertyLocation, 1>;
404 using RepresentationField = LocationField::Next<uint32_t, 3>;
405 using DescriptorPointer =
406 RepresentationField::Next<uint32_t, kDescriptorIndexBitCount>;
407 using FieldIndexField =
408 DescriptorPointer::Next<uint32_t, kDescriptorIndexBitCount>;
409
410 // All bits for both fast and slow objects must fit in a smi.
411 STATIC_ASSERT(DictionaryStorageField::kLastUsedBit < 31);
412 STATIC_ASSERT(FieldIndexField::kLastUsedBit < 31);
413
414 // DictionaryStorageField must be the last field, so that overflowing it
415 // doesn't overwrite other fields.
416 STATIC_ASSERT(DictionaryStorageField::kLastUsedBit == 30);
417
418 // All bits for non-global dictionary mode objects except enumeration index
419 // must fit in a byte.
420 STATIC_ASSERT(KindField::kLastUsedBit < 8);
421 STATIC_ASSERT(ConstnessField::kLastUsedBit < 8);
422 STATIC_ASSERT(AttributesField::kLastUsedBit < 8);
423
424 static const int kInitialIndex = 1;
425
426 static constexpr PropertyConstness kConstIfDictConstnessTracking =
427 V8_DICT_PROPERTY_CONST_TRACKING_BOOL ? PropertyConstness::kConst
428 : PropertyConstness::kMutable;
429
430 #ifdef OBJECT_PRINT
431 // For our gdb macros, we should perhaps change these in the future.
432 void Print(bool dictionary_mode);
433 #endif
434
435 enum PrintMode {
436 kPrintAttributes = 1 << 0,
437 kPrintFieldIndex = 1 << 1,
438 kPrintRepresentation = 1 << 2,
439 kPrintPointer = 1 << 3,
440
441 kForProperties = kPrintFieldIndex,
442 kForTransitions = kPrintAttributes,
443 kPrintFull = -1,
444 };
445 void PrintAsSlowTo(std::ostream& out, bool print_dict_index);
446 void PrintAsFastTo(std::ostream& out, PrintMode mode = kPrintFull);
447
448 // Encodes those property details for non-global dictionary properties
449 // with an enumeration index of 0 as a single byte.
ToByte()450 uint8_t ToByte() {
451 // We only care about the value of KindField, ConstnessField, and
452 // AttributesField. We've statically asserted earlier that these fields fit
453 // into a byte together.
454
455 DCHECK_EQ(PropertyLocation::kField, location());
456 STATIC_ASSERT(static_cast<int>(PropertyLocation::kField) == 0);
457
458 DCHECK_EQ(PropertyCellType::kNoCell, cell_type());
459 STATIC_ASSERT(static_cast<int>(PropertyCellType::kNoCell) == 0);
460
461 // Only to be used when the enum index isn't actually maintained
462 // by the PropertyDetails:
463 DCHECK_EQ(0, dictionary_index());
464
465 return value_;
466 }
467
468 // Only to be used for bytes obtained by ToByte. In particular, only used for
469 // non-global dictionary properties.
FromByte(uint8_t encoded_details)470 static PropertyDetails FromByte(uint8_t encoded_details) {
471 // The 0-extension to 32bit sets PropertyLocation to kField,
472 // PropertyCellType to kNoCell, and enumeration index to 0, as intended.
473 // Everything else is obtained from |encoded_details|.
474 PropertyDetails details(encoded_details);
475 DCHECK_EQ(PropertyLocation::kField, details.location());
476 DCHECK_EQ(PropertyCellType::kNoCell, details.cell_type());
477 DCHECK_EQ(0, details.dictionary_index());
478 return details;
479 }
480
481 private:
PropertyDetails(int value,int pointer)482 PropertyDetails(int value, int pointer) {
483 value_ = DescriptorPointer::update(value, pointer);
484 }
PropertyDetails(int value,Representation representation)485 PropertyDetails(int value, Representation representation) {
486 value_ = RepresentationField::update(value,
487 EncodeRepresentation(representation));
488 }
PropertyDetails(int value,PropertyConstness constness)489 PropertyDetails(int value, PropertyConstness constness) {
490 value_ = ConstnessField::update(value, constness);
491 }
PropertyDetails(int value,PropertyAttributes attributes)492 PropertyDetails(int value, PropertyAttributes attributes) {
493 value_ = AttributesField::update(value, attributes);
494 }
495
PropertyDetails(uint32_t value)496 explicit PropertyDetails(uint32_t value) : value_{value} {}
497
498 uint32_t value_;
499 };
500
501 // kField location is more general than kDescriptor, kDescriptor generalizes
502 // only to itself.
IsGeneralizableTo(PropertyLocation a,PropertyLocation b)503 inline bool IsGeneralizableTo(PropertyLocation a, PropertyLocation b) {
504 return b == PropertyLocation::kField || a == PropertyLocation::kDescriptor;
505 }
506
507 // PropertyConstness::kMutable constness is more general than
508 // VariableMode::kConst, VariableMode::kConst generalizes only to itself.
IsGeneralizableTo(PropertyConstness a,PropertyConstness b)509 inline bool IsGeneralizableTo(PropertyConstness a, PropertyConstness b) {
510 return b == PropertyConstness::kMutable || a == PropertyConstness::kConst;
511 }
512
GeneralizeConstness(PropertyConstness a,PropertyConstness b)513 inline PropertyConstness GeneralizeConstness(PropertyConstness a,
514 PropertyConstness b) {
515 return a == PropertyConstness::kMutable ? PropertyConstness::kMutable : b;
516 }
517
518 V8_EXPORT_PRIVATE std::ostream& operator<<(
519 std::ostream& os, const Representation& representation);
520 V8_EXPORT_PRIVATE std::ostream& operator<<(
521 std::ostream& os, const PropertyAttributes& attributes);
522 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
523 PropertyConstness constness);
524 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
525 PropertyCellType type);
526 } // namespace internal
527 } // namespace v8
528
529 #endif // V8_OBJECTS_PROPERTY_DETAILS_H_
530