• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
9 #include "src/utils/allocation.h"
10 // TODO(bmeurer): Remove once FLAG_modify_field_representation_inplace is gone.
11 #include "src/base/bit-field.h"
12 #include "src/flags/flags.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 enum PropertyFilter {
36   ALL_PROPERTIES = 0,
37   ONLY_WRITABLE = 1,
38   ONLY_ENUMERABLE = 2,
39   ONLY_CONFIGURABLE = 4,
40   SKIP_STRINGS = 8,
41   SKIP_SYMBOLS = 16,
42   ONLY_ALL_CAN_READ = 32,
43   PRIVATE_NAMES_ONLY = 64,
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 class Representation {
82  public:
83   enum Kind { kNone, kSmi, kDouble, kHeapObject, kTagged, kNumRepresentations };
84 
Representation()85   Representation() : kind_(kNone) {}
86 
None()87   static Representation None() { return Representation(kNone); }
Tagged()88   static Representation Tagged() { return Representation(kTagged); }
Smi()89   static Representation Smi() { return Representation(kSmi); }
Double()90   static Representation Double() { return Representation(kDouble); }
HeapObject()91   static Representation HeapObject() { return Representation(kHeapObject); }
92 
FromKind(Kind kind)93   static Representation FromKind(Kind kind) { return Representation(kind); }
94 
Equals(const Representation & other)95   bool Equals(const Representation& other) const {
96     return kind_ == other.kind_;
97   }
98 
IsCompatibleForLoad(const Representation & other)99   bool IsCompatibleForLoad(const Representation& other) const {
100     return IsDouble() == other.IsDouble();
101   }
102 
IsCompatibleForStore(const Representation & other)103   bool IsCompatibleForStore(const Representation& other) const {
104     return Equals(other);
105   }
106 
CanBeInPlaceChangedTo(const Representation & other)107   bool CanBeInPlaceChangedTo(const Representation& other) const {
108     // If it's just a representation generalization case (i.e. property kind and
109     // attributes stays unchanged) it's fine to transition from None to anything
110     // but double without any modification to the object, because the default
111     // uninitialized value for representation None can be overwritten by both
112     // smi and tagged values. Doubles, however, would require a box allocation.
113     if (IsNone()) return !other.IsDouble();
114     if (!FLAG_modify_field_representation_inplace) return false;
115     return (IsSmi() || (!FLAG_unbox_double_fields && IsDouble()) ||
116             IsHeapObject()) &&
117            other.IsTagged();
118   }
119 
120   // Return the most generic representation that this representation can be
121   // changed to in-place. If in-place representation changes are disabled, then
122   // this will return the current representation.
MostGenericInPlaceChange()123   Representation MostGenericInPlaceChange() const {
124     if (!FLAG_modify_field_representation_inplace) return *this;
125     // Everything but unboxed doubles can be in-place changed to Tagged.
126     if (FLAG_unbox_double_fields && IsDouble()) return Representation::Double();
127     return Representation::Tagged();
128   }
129 
is_more_general_than(const Representation & other)130   bool is_more_general_than(const Representation& other) const {
131     if (IsHeapObject()) return other.IsNone();
132     return kind_ > other.kind_;
133   }
134 
fits_into(const Representation & other)135   bool fits_into(const Representation& other) const {
136     return other.is_more_general_than(*this) || other.Equals(*this);
137   }
138 
generalize(Representation other)139   Representation generalize(Representation other) {
140     if (other.fits_into(*this)) return *this;
141     if (other.is_more_general_than(*this)) return other;
142     return Representation::Tagged();
143   }
144 
size()145   int size() const {
146     DCHECK(!IsNone());
147     if (IsDouble()) return kDoubleSize;
148     DCHECK(IsTagged() || IsSmi() || IsHeapObject());
149     return kTaggedSize;
150   }
151 
kind()152   Kind kind() const { return static_cast<Kind>(kind_); }
IsNone()153   bool IsNone() const { return kind_ == kNone; }
IsTagged()154   bool IsTagged() const { return kind_ == kTagged; }
IsSmi()155   bool IsSmi() const { return kind_ == kSmi; }
IsSmiOrTagged()156   bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
IsDouble()157   bool IsDouble() const { return kind_ == kDouble; }
IsHeapObject()158   bool IsHeapObject() const { return kind_ == kHeapObject; }
159 
Mnemonic()160   const char* Mnemonic() const {
161     switch (kind_) {
162       case kNone:
163         return "v";
164       case kTagged:
165         return "t";
166       case kSmi:
167         return "s";
168       case kDouble:
169         return "d";
170       case kHeapObject:
171         return "h";
172     }
173     UNREACHABLE();
174   }
175 
176  private:
Representation(Kind k)177   explicit Representation(Kind k) : kind_(k) {}
178 
179   // Make sure kind fits in int8.
180   STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
181 
182   int8_t kind_;
183 };
184 
185 static const int kDescriptorIndexBitCount = 10;
186 static const int kFirstInobjectPropertyOffsetBitCount = 7;
187 // The maximum number of descriptors we want in a descriptor array.  It should
188 // fit in a page and also the following should hold:
189 // kMaxNumberOfDescriptors + kFieldsAdded <= PropertyArray::kMaxLength.
190 static const int kMaxNumberOfDescriptors = (1 << kDescriptorIndexBitCount) - 4;
191 static const int kInvalidEnumCacheSentinel =
192     (1 << kDescriptorIndexBitCount) - 1;
193 
194 enum class PropertyCellType {
195   // Meaningful when a property cell does not contain the hole.
196   kUndefined,     // The PREMONOMORPHIC of property cells.
197   kConstant,      // Cell has been assigned only once.
198   kConstantType,  // Cell has been assigned only one type.
199   kMutable,       // Cell will no longer be tracked as constant.
200 
201   // Meaningful when a property cell contains the hole.
202   kUninitialized = kUndefined,  // Cell has never been initialized.
203   kInvalidated = kConstant,     // Cell has been deleted, invalidated or never
204                                 // existed.
205 
206   // For dictionaries not holding cells.
207   kNoCell = kMutable,
208 };
209 
210 enum class PropertyCellConstantType {
211   kSmi,
212   kStableMap,
213 };
214 
215 // PropertyDetails captures type and attributes for a property.
216 // They are used both in property dictionaries and instance descriptors.
217 class PropertyDetails {
218  public:
219   // Property details for dictionary mode properties/elements.
220   PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
221                   PropertyCellType cell_type, int dictionary_index = 0) {
222     value_ = KindField::encode(kind) | LocationField::encode(kField) |
223              AttributesField::encode(attributes) |
224              DictionaryStorageField::encode(dictionary_index) |
225              PropertyCellTypeField::encode(cell_type);
226   }
227 
228   // Property details for fast mode properties.
229   PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
230                   PropertyLocation location, PropertyConstness constness,
231                   Representation representation, int field_index = 0) {
232     value_ = KindField::encode(kind) | AttributesField::encode(attributes) |
233              LocationField::encode(location) |
234              ConstnessField::encode(constness) |
235              RepresentationField::encode(EncodeRepresentation(representation)) |
236              FieldIndexField::encode(field_index);
237   }
238 
239   static PropertyDetails Empty(
240       PropertyCellType cell_type = PropertyCellType::kNoCell) {
241     return PropertyDetails(kData, NONE, cell_type);
242   }
243 
pointer()244   int pointer() const { return DescriptorPointer::decode(value_); }
245 
set_pointer(int i)246   PropertyDetails set_pointer(int i) const {
247     return PropertyDetails(value_, i);
248   }
249 
set_cell_type(PropertyCellType type)250   PropertyDetails set_cell_type(PropertyCellType type) const {
251     PropertyDetails details = *this;
252     details.value_ = PropertyCellTypeField::update(details.value_, type);
253     return details;
254   }
255 
set_index(int index)256   PropertyDetails set_index(int index) const {
257     PropertyDetails details = *this;
258     details.value_ = DictionaryStorageField::update(details.value_, index);
259     return details;
260   }
261 
CopyWithRepresentation(Representation representation)262   PropertyDetails CopyWithRepresentation(Representation representation) const {
263     return PropertyDetails(value_, representation);
264   }
CopyWithConstness(PropertyConstness constness)265   PropertyDetails CopyWithConstness(PropertyConstness constness) const {
266     return PropertyDetails(value_, constness);
267   }
CopyAddAttributes(PropertyAttributes new_attributes)268   PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) const {
269     new_attributes =
270         static_cast<PropertyAttributes>(attributes() | new_attributes);
271     return PropertyDetails(value_, new_attributes);
272   }
273 
274   // Conversion for storing details as Object.
275   explicit inline PropertyDetails(Smi smi);
276   inline Smi AsSmi() const;
277 
EncodeRepresentation(Representation representation)278   static uint8_t EncodeRepresentation(Representation representation) {
279     return representation.kind();
280   }
281 
DecodeRepresentation(uint32_t bits)282   static Representation DecodeRepresentation(uint32_t bits) {
283     return Representation::FromKind(static_cast<Representation::Kind>(bits));
284   }
285 
kind()286   PropertyKind kind() const { return KindField::decode(value_); }
location()287   PropertyLocation location() const { return LocationField::decode(value_); }
constness()288   PropertyConstness constness() const { return ConstnessField::decode(value_); }
289 
attributes()290   PropertyAttributes attributes() const {
291     return AttributesField::decode(value_);
292   }
293 
HasKindAndAttributes(PropertyKind kind,PropertyAttributes attributes)294   bool HasKindAndAttributes(PropertyKind kind, PropertyAttributes attributes) {
295     return (value_ & (KindField::kMask | AttributesField::kMask)) ==
296            (KindField::encode(kind) | AttributesField::encode(attributes));
297   }
298 
dictionary_index()299   int dictionary_index() const {
300     return DictionaryStorageField::decode(value_);
301   }
302 
representation()303   Representation representation() const {
304     return DecodeRepresentation(RepresentationField::decode(value_));
305   }
306 
field_index()307   int field_index() const { return FieldIndexField::decode(value_); }
308 
309   inline int field_width_in_words() const;
310 
IsValidIndex(int index)311   static bool IsValidIndex(int index) {
312     return DictionaryStorageField::is_valid(index);
313   }
314 
IsReadOnly()315   bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
IsConfigurable()316   bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
IsDontEnum()317   bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
IsEnumerable()318   bool IsEnumerable() const { return !IsDontEnum(); }
cell_type()319   PropertyCellType cell_type() const {
320     return PropertyCellTypeField::decode(value_);
321   }
322 
323   // Bit fields in value_ (type, shift, size). Must be public so the
324   // constants can be embedded in generated code.
325   using KindField = base::BitField<PropertyKind, 0, 1>;
326   using LocationField = KindField::Next<PropertyLocation, 1>;
327   using ConstnessField = LocationField::Next<PropertyConstness, 1>;
328   using AttributesField = ConstnessField::Next<PropertyAttributes, 3>;
329   static const int kAttributesReadOnlyMask =
330       (READ_ONLY << AttributesField::kShift);
331   static const int kAttributesDontDeleteMask =
332       (DONT_DELETE << AttributesField::kShift);
333   static const int kAttributesDontEnumMask =
334       (DONT_ENUM << AttributesField::kShift);
335 
336   // Bit fields for normalized objects.
337   using PropertyCellTypeField = AttributesField::Next<PropertyCellType, 2>;
338   using DictionaryStorageField = PropertyCellTypeField::Next<uint32_t, 23>;
339 
340   // Bit fields for fast objects.
341   using RepresentationField = AttributesField::Next<uint32_t, 3>;
342   using DescriptorPointer =
343       RepresentationField::Next<uint32_t, kDescriptorIndexBitCount>;
344   using FieldIndexField =
345       DescriptorPointer::Next<uint32_t, kDescriptorIndexBitCount>;
346 
347   // All bits for both fast and slow objects must fit in a smi.
348   STATIC_ASSERT(DictionaryStorageField::kLastUsedBit < 31);
349   STATIC_ASSERT(FieldIndexField::kLastUsedBit < 31);
350 
351   static const int kInitialIndex = 1;
352 
353 #ifdef OBJECT_PRINT
354   // For our gdb macros, we should perhaps change these in the future.
355   void Print(bool dictionary_mode);
356 #endif
357 
358   enum PrintMode {
359     kPrintAttributes = 1 << 0,
360     kPrintFieldIndex = 1 << 1,
361     kPrintRepresentation = 1 << 2,
362     kPrintPointer = 1 << 3,
363 
364     kForProperties = kPrintFieldIndex,
365     kForTransitions = kPrintAttributes,
366     kPrintFull = -1,
367   };
368   void PrintAsSlowTo(std::ostream& out);
369   void PrintAsFastTo(std::ostream& out, PrintMode mode = kPrintFull);
370 
371  private:
PropertyDetails(int value,int pointer)372   PropertyDetails(int value, int pointer) {
373     value_ = DescriptorPointer::update(value, pointer);
374   }
PropertyDetails(int value,Representation representation)375   PropertyDetails(int value, Representation representation) {
376     value_ = RepresentationField::update(value,
377                                          EncodeRepresentation(representation));
378   }
PropertyDetails(int value,PropertyConstness constness)379   PropertyDetails(int value, PropertyConstness constness) {
380     value_ = ConstnessField::update(value, constness);
381   }
PropertyDetails(int value,PropertyAttributes attributes)382   PropertyDetails(int value, PropertyAttributes attributes) {
383     value_ = AttributesField::update(value, attributes);
384   }
385 
386   uint32_t value_;
387 };
388 
389 // kField location is more general than kDescriptor, kDescriptor generalizes
390 // only to itself.
IsGeneralizableTo(PropertyLocation a,PropertyLocation b)391 inline bool IsGeneralizableTo(PropertyLocation a, PropertyLocation b) {
392   return b == kField || a == kDescriptor;
393 }
394 
395 // PropertyConstness::kMutable constness is more general than
396 // VariableMode::kConst, VariableMode::kConst generalizes only to itself.
IsGeneralizableTo(PropertyConstness a,PropertyConstness b)397 inline bool IsGeneralizableTo(PropertyConstness a, PropertyConstness b) {
398   return b == PropertyConstness::kMutable || a == PropertyConstness::kConst;
399 }
400 
GeneralizeConstness(PropertyConstness a,PropertyConstness b)401 inline PropertyConstness GeneralizeConstness(PropertyConstness a,
402                                              PropertyConstness b) {
403   return a == PropertyConstness::kMutable ? PropertyConstness::kMutable : b;
404 }
405 
406 V8_EXPORT_PRIVATE std::ostream& operator<<(
407     std::ostream& os, const PropertyAttributes& attributes);
408 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
409                                            PropertyConstness constness);
410 }  // namespace internal
411 }  // namespace v8
412 
413 #endif  // V8_OBJECTS_PROPERTY_DETAILS_H_
414