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