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