LCOV - code coverage report
Current view: top level - src - property-details.h (source / functions) Hit Total Coverage
Test: app.info Lines: 59 61 96.7 %
Date: 2019-03-21 Functions: 14 14 100.0 %

          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             : 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             : // 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             :     kSmi,
      91             :     kDouble,
      92             :     kHeapObject,
      93             :     kTagged,
      94             :     kNumRepresentations
      95             :   };
      96             : 
      97        6400 :   Representation() : kind_(kNone) { }
      98             : 
      99             :   static Representation None() { return Representation(kNone); }
     100         168 :   static Representation Tagged() { return Representation(kTagged); }
     101         168 :   static Representation Smi() { return Representation(kSmi); }
     102         168 :   static Representation Double() { return Representation(kDouble); }
     103         168 :   static Representation HeapObject() { return Representation(kHeapObject); }
     104             : 
     105             :   static Representation FromKind(Kind kind) { return Representation(kind); }
     106             : 
     107             :   bool Equals(const Representation& other) const {
     108      669202 :     return kind_ == other.kind_;
     109             :   }
     110             : 
     111             :   bool IsCompatibleForLoad(const Representation& other) const {
     112             :     return IsDouble() == other.IsDouble();
     113             :   }
     114             : 
     115             :   bool IsCompatibleForStore(const Representation& other) const {
     116             :     return Equals(other);
     117             :   }
     118             : 
     119             :   bool is_more_general_than(const Representation& other) const {
     120      939496 :     if (IsHeapObject()) return other.IsNone();
     121      370674 :     return kind_ > other.kind_;
     122             :   }
     123             : 
     124             :   bool fits_into(const Representation& other) const {
     125      910982 :     return other.is_more_general_than(*this) || other.Equals(*this);
     126             :   }
     127             : 
     128      350796 :   Representation generalize(Representation other) {
     129      350796 :     if (other.fits_into(*this)) return *this;
     130       40218 :     if (other.is_more_general_than(*this)) return other;
     131             :     return Representation::Tagged();
     132             :   }
     133             : 
     134             :   int size() const {
     135             :     DCHECK(!IsNone());
     136             :     if (IsDouble()) return kDoubleSize;
     137             :     DCHECK(IsTagged() || IsSmi() || IsHeapObject());
     138             :     return kTaggedSize;
     139             :   }
     140             : 
     141   224656132 :   Kind kind() const { return static_cast<Kind>(kind_); }
     142      869690 :   bool IsNone() const { return kind_ == kNone; }
     143             :   bool IsTagged() const { return kind_ == kTagged; }
     144        1344 :   bool IsSmi() const { return kind_ == kSmi; }
     145             :   bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
     146    40250986 :   bool IsDouble() const { return kind_ == kDouble; }
     147      479811 :   bool IsHeapObject() const { return kind_ == kHeapObject; }
     148             : 
     149       32201 :   const char* Mnemonic() const {
     150       32201 :     switch (kind_) {
     151             :       case kNone:
     152             :         return "v";
     153             :       case kTagged:
     154        5869 :         return "t";
     155             :       case kSmi:
     156        5783 :         return "s";
     157             :       case kDouble:
     158         508 :         return "d";
     159             :       case kHeapObject:
     160       20041 :         return "h";
     161             :     }
     162           0 :     UNREACHABLE();
     163             :   }
     164             : 
     165             :  private:
     166   577204017 :   explicit Representation(Kind k) : kind_(k) { }
     167             : 
     168             :   // Make sure kind fits in int8.
     169             :   STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
     170             : 
     171             :   int8_t kind_;
     172             : };
     173             : 
     174             : 
     175             : static const int kDescriptorIndexBitCount = 10;
     176             : static const int kFirstInobjectPropertyOffsetBitCount = 7;
     177             : // The maximum number of descriptors we want in a descriptor array.  It should
     178             : // fit in a page and also the following should hold:
     179             : // kMaxNumberOfDescriptors + kFieldsAdded <= PropertyArray::kMaxLength.
     180             : static const int kMaxNumberOfDescriptors = (1 << kDescriptorIndexBitCount) - 4;
     181             : static const int kInvalidEnumCacheSentinel =
     182             :     (1 << kDescriptorIndexBitCount) - 1;
     183             : 
     184             : enum class PropertyCellType {
     185             :   // Meaningful when a property cell does not contain the hole.
     186             :   kUndefined,     // The PREMONOMORPHIC of property cells.
     187             :   kConstant,      // Cell has been assigned only once.
     188             :   kConstantType,  // Cell has been assigned only one type.
     189             :   kMutable,       // Cell will no longer be tracked as constant.
     190             : 
     191             :   // Meaningful when a property cell contains the hole.
     192             :   kUninitialized = kUndefined,  // Cell has never been initialized.
     193             :   kInvalidated = kConstant,     // Cell has been deleted, invalidated or never
     194             :                                 // existed.
     195             : 
     196             :   // For dictionaries not holding cells.
     197             :   kNoCell = kMutable,
     198             : };
     199             : 
     200             : enum class PropertyCellConstantType {
     201             :   kSmi,
     202             :   kStableMap,
     203             : };
     204             : 
     205             : 
     206             : // PropertyDetails captures type and attributes for a property.
     207             : // They are used both in property dictionaries and instance descriptors.
     208             : class PropertyDetails {
     209             :  public:
     210             :   // Property details for dictionary mode properties/elements.
     211        1008 :   PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
     212             :                   PropertyCellType cell_type, int dictionary_index = 0) {
     213     2027921 :     value_ = KindField::encode(kind) | LocationField::encode(kField) |
     214     8278986 :              AttributesField::encode(attributes) |
     215    16080409 :              DictionaryStorageField::encode(dictionary_index) |
     216    11787584 :              PropertyCellTypeField::encode(cell_type);
     217        1008 :   }
     218             : 
     219             :   // Property details for fast mode properties.
     220             :   PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
     221             :                   PropertyLocation location, PropertyConstness constness,
     222             :                   Representation representation, int field_index = 0) {
     223      677806 :     value_ = KindField::encode(kind) | AttributesField::encode(attributes) |
     224    21099006 :              LocationField::encode(location) |
     225    21118520 :              ConstnessField::encode(constness) |
     226    18867615 :              RepresentationField::encode(EncodeRepresentation(representation)) |
     227    20988732 :              FieldIndexField::encode(field_index);
     228             :   }
     229             : 
     230             :   static PropertyDetails Empty(
     231             :       PropertyCellType cell_type = PropertyCellType::kNoCell) {
     232             :     return PropertyDetails(kData, NONE, cell_type);
     233             :   }
     234             : 
     235  3185401470 :   int pointer() const { return DescriptorPointer::decode(value_); }
     236             : 
     237             :   PropertyDetails set_pointer(int i) const {
     238   851593708 :     return PropertyDetails(value_, i);
     239             :   }
     240             : 
     241             :   PropertyDetails set_cell_type(PropertyCellType type) const {
     242     8222042 :     PropertyDetails details = *this;
     243             :     details.value_ = PropertyCellTypeField::update(details.value_, type);
     244             :     return details;
     245             :   }
     246             : 
     247             :   PropertyDetails set_index(int index) const {
     248           0 :     PropertyDetails details = *this;
     249    17294609 :     details.value_ = DictionaryStorageField::update(details.value_, index);
     250             :     return details;
     251             :   }
     252             : 
     253             :   PropertyDetails CopyWithRepresentation(Representation representation) const {
     254             :     return PropertyDetails(value_, representation);
     255             :   }
     256             :   PropertyDetails CopyWithConstness(PropertyConstness constness) const {
     257             :     return PropertyDetails(value_, constness);
     258             :   }
     259             :   PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) const {
     260             :     new_attributes =
     261      253489 :         static_cast<PropertyAttributes>(attributes() | new_attributes);
     262             :     return PropertyDetails(value_, new_attributes);
     263             :   }
     264             : 
     265             :   // Conversion for storing details as Object.
     266             :   explicit inline PropertyDetails(Smi smi);
     267             :   inline Smi AsSmi() const;
     268             : 
     269             :   static uint8_t EncodeRepresentation(Representation representation) {
     270             :     return representation.kind();
     271             :   }
     272             : 
     273             :   static Representation DecodeRepresentation(uint32_t bits) {
     274             :     return Representation::FromKind(static_cast<Representation::Kind>(bits));
     275             :   }
     276             : 
     277    36906006 :   PropertyKind kind() const { return KindField::decode(value_); }
     278   283003585 :   PropertyLocation location() const { return LocationField::decode(value_); }
     279      249058 :   PropertyConstness constness() const { return ConstnessField::decode(value_); }
     280             : 
     281             :   PropertyAttributes attributes() const {
     282    26736819 :     return AttributesField::decode(value_);
     283             :   }
     284             : 
     285             :   bool HasKindAndAttributes(PropertyKind kind, PropertyAttributes attributes) {
     286     5702688 :     return (value_ & (KindField::kMask | AttributesField::kMask)) ==
     287     5702688 :            (KindField::encode(kind) | AttributesField::encode(attributes));
     288             :   }
     289             : 
     290             :   int dictionary_index() const {
     291   373112239 :     return DictionaryStorageField::decode(value_);
     292             :   }
     293             : 
     294      236717 :   Representation representation() const {
     295   130320704 :     return DecodeRepresentation(RepresentationField::decode(value_));
     296             :   }
     297             : 
     298   957728754 :   int field_index() const { return FieldIndexField::decode(value_); }
     299             : 
     300             :   inline int field_width_in_words() const;
     301             : 
     302             :   static bool IsValidIndex(int index) {
     303    12698685 :     return DictionaryStorageField::is_valid(index);
     304             :   }
     305             : 
     306    46582601 :   bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
     307     9392287 :   bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
     308     8846782 :   bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
     309             :   bool IsEnumerable() const { return !IsDontEnum(); }
     310             :   PropertyCellType cell_type() const {
     311    13037913 :     return PropertyCellTypeField::decode(value_);
     312             :   }
     313             : 
     314             :   // Bit fields in value_ (type, shift, size). Must be public so the
     315             :   // constants can be embedded in generated code.
     316             :   class KindField : public BitField<PropertyKind, 0, 1> {};
     317             :   class LocationField : public BitField<PropertyLocation, KindField::kNext, 1> {
     318             :   };
     319             :   class ConstnessField
     320             :       : public BitField<PropertyConstness, LocationField::kNext, 1> {};
     321             :   class AttributesField
     322             :       : public BitField<PropertyAttributes, ConstnessField::kNext, 3> {};
     323             :   static const int kAttributesReadOnlyMask =
     324             :       (READ_ONLY << AttributesField::kShift);
     325             :   static const int kAttributesDontDeleteMask =
     326             :       (DONT_DELETE << AttributesField::kShift);
     327             :   static const int kAttributesDontEnumMask =
     328             :       (DONT_ENUM << AttributesField::kShift);
     329             : 
     330             :   // Bit fields for normalized objects.
     331             :   class PropertyCellTypeField
     332             :       : public BitField<PropertyCellType, AttributesField::kNext, 2> {};
     333             :   class DictionaryStorageField
     334             :       : public BitField<uint32_t, PropertyCellTypeField::kNext, 23> {};
     335             : 
     336             :   // Bit fields for fast objects.
     337             :   class RepresentationField
     338             :       : public BitField<uint32_t, AttributesField::kNext, 3> {};
     339             :   class DescriptorPointer
     340             :       : public BitField<uint32_t, RepresentationField::kNext,
     341             :                         kDescriptorIndexBitCount> {};  // NOLINT
     342             :   class FieldIndexField : public BitField<uint32_t, DescriptorPointer::kNext,
     343             :                                           kDescriptorIndexBitCount> {
     344             :   };  // NOLINT
     345             : 
     346             :   // All bits for both fast and slow objects must fit in a smi.
     347             :   STATIC_ASSERT(DictionaryStorageField::kNext <= 31);
     348             :   STATIC_ASSERT(FieldIndexField::kNext <= 31);
     349             : 
     350             :   static const int kInitialIndex = 1;
     351             : 
     352             : #ifdef OBJECT_PRINT
     353             :   // For our gdb macros, we should perhaps change these in the future.
     354             :   void Print(bool dictionary_mode);
     355             : #endif
     356             : 
     357             :   enum PrintMode {
     358             :     kPrintAttributes = 1 << 0,
     359             :     kPrintFieldIndex = 1 << 1,
     360             :     kPrintRepresentation = 1 << 2,
     361             :     kPrintPointer = 1 << 3,
     362             : 
     363             :     kForProperties = kPrintFieldIndex,
     364             :     kForTransitions = kPrintAttributes,
     365             :     kPrintFull = -1,
     366             :   };
     367             :   void PrintAsSlowTo(std::ostream& out);
     368             :   void PrintAsFastTo(std::ostream& out, PrintMode mode = kPrintFull);
     369             : 
     370             :  private:
     371             :   PropertyDetails(int value, int pointer) {
     372   851322150 :     value_ = DescriptorPointer::update(value, pointer);
     373             :   }
     374             :   PropertyDetails(int value, Representation representation) {
     375             :     value_ = RepresentationField::update(
     376             :         value, EncodeRepresentation(representation));
     377             :   }
     378             :   PropertyDetails(int value, PropertyConstness constness) {
     379             :     value_ = ConstnessField::update(value, constness);
     380             :   }
     381             :   PropertyDetails(int value, PropertyAttributes attributes) {
     382             :     value_ = AttributesField::update(value, attributes);
     383             :   }
     384             : 
     385             :   uint32_t value_;
     386             : };
     387             : 
     388             : // kField location is more general than kDescriptor, kDescriptor generalizes
     389             : // only to itself.
     390             : inline bool IsGeneralizableTo(PropertyLocation a, PropertyLocation b) {
     391      532930 :   return b == kField || a == kDescriptor;
     392             : }
     393             : 
     394             : // PropertyConstness::kMutable constness is more general than
     395             : // VariableMode::kConst, VariableMode::kConst generalizes only to itself.
     396             : inline bool IsGeneralizableTo(PropertyConstness a, PropertyConstness b) {
     397     7995710 :   return b == PropertyConstness::kMutable || a == PropertyConstness::kConst;
     398             : }
     399             : 
     400             : inline PropertyConstness GeneralizeConstness(PropertyConstness a,
     401             :                                              PropertyConstness b) {
     402      605391 :   return a == PropertyConstness::kMutable ? PropertyConstness::kMutable : b;
     403             : }
     404             : 
     405             : std::ostream& operator<<(std::ostream& os,
     406             :                          const PropertyAttributes& attributes);
     407             : }  // namespace internal
     408             : }  // namespace v8
     409             : 
     410             : #endif  // V8_PROPERTY_DETAILS_H_

Generated by: LCOV version 1.10