property-details.h 15.5 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_PROPERTY_DETAILS_H_
#define V8_PROPERTY_DETAILS_H_

8 9
#include "include/v8.h"
#include "src/allocation.h"
10 11
// TODO(ishell): remove once FLAG_track_constant_fields is removed.
#include "src/flags.h"
12
#include "src/utils.h"
13

14 15 16 17
namespace v8 {
namespace internal {

// ES6 6.1.7.1
18
enum PropertyAttributes {
19 20 21 22 23 24
  NONE = ::v8::None,
  READ_ONLY = ::v8::ReadOnly,
  DONT_ENUM = ::v8::DontEnum,
  DONT_DELETE = ::v8::DontDelete,

  ALL_ATTRIBUTES_MASK = READ_ONLY | DONT_ENUM | DONT_DELETE,
25

26 27
  SEALED = DONT_DELETE,
  FROZEN = SEALED | READ_ONLY,
28

29
  ABSENT = 64,  // Used in runtime to indicate a property is absent.
30 31 32 33 34 35
  // ABSENT can never be stored in or returned from a descriptor's attributes
  // bitfield.  It is only used as a return value meaning the attributes of
  // a non-existent property.
};


36 37
enum PropertyFilter {
  ALL_PROPERTIES = 0,
38
  ONLY_WRITABLE = 1,
39
  ONLY_ENUMERABLE = 2,
40
  ONLY_CONFIGURABLE = 4,
41 42 43 44 45 46 47
  SKIP_STRINGS = 8,
  SKIP_SYMBOLS = 16,
  ONLY_ALL_CAN_READ = 32,
  ENUMERABLE_STRINGS = ONLY_ENUMERABLE | SKIP_SYMBOLS,
};
// Enable fast comparisons of PropertyAttributes against PropertyFilters.
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 52
STATIC_ASSERT(((SKIP_STRINGS | SKIP_SYMBOLS | ONLY_ALL_CAN_READ) &
               ALL_ATTRIBUTES_MASK) == 0);
53 54 55 56 57 58 59 60 61 62 63 64
STATIC_ASSERT(ALL_PROPERTIES ==
              static_cast<PropertyFilter>(v8::PropertyFilter::ALL_PROPERTIES));
STATIC_ASSERT(ONLY_WRITABLE ==
              static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_WRITABLE));
STATIC_ASSERT(ONLY_ENUMERABLE ==
              static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_ENUMERABLE));
STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(
                                       v8::PropertyFilter::ONLY_CONFIGURABLE));
STATIC_ASSERT(SKIP_STRINGS ==
              static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_STRINGS));
STATIC_ASSERT(SKIP_SYMBOLS ==
              static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_SYMBOLS));
65 66

class Smi;
67
class TypeInfo;
68

69 70
// Order of kinds is significant.
// 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 78
// Order of modes is significant.
// Must fit in the BitField PropertyDetails::ConstnessField.
79
enum class PropertyConstness { kMutable = 0, kConst = 1 };
80

81 82
// TODO(ishell): remove once constant field tracking is done.
const PropertyConstness kDefaultFieldConstness =
83 84
    FLAG_track_constant_fields ? PropertyConstness::kConst
                               : PropertyConstness::kMutable;
85

86 87 88 89
class Representation {
 public:
  enum Kind {
    kNone,
90 91 92 93
    kInteger8,
    kUInteger8,
    kInteger16,
    kUInteger16,
94 95 96
    kSmi,
    kInteger32,
    kDouble,
97
    kHeapObject,
98 99 100 101 102 103 104 105 106
    kTagged,
    kExternal,
    kNumRepresentations
  };

  Representation() : kind_(kNone) { }

  static Representation None() { return Representation(kNone); }
  static Representation Tagged() { return Representation(kTagged); }
107 108 109
  static Representation Integer8() { return Representation(kInteger8); }
  static Representation UInteger8() { return Representation(kUInteger8); }
  static Representation Integer16() { return Representation(kInteger16); }
110
  static Representation UInteger16() { return Representation(kUInteger16); }
111 112 113
  static Representation Smi() { return Representation(kSmi); }
  static Representation Integer32() { return Representation(kInteger32); }
  static Representation Double() { return Representation(kDouble); }
114
  static Representation HeapObject() { return Representation(kHeapObject); }
115 116 117 118
  static Representation External() { return Representation(kExternal); }

  static Representation FromKind(Kind kind) { return Representation(kind); }

119
  bool Equals(const Representation& other) const {
120 121 122
    return kind_ == other.kind_;
  }

123 124 125 126 127
  bool IsCompatibleForLoad(const Representation& other) const {
    return (IsDouble() && other.IsDouble()) ||
        (!IsDouble() && !other.IsDouble());
  }

128 129 130 131
  bool IsCompatibleForStore(const Representation& other) const {
    return Equals(other);
  }

132
  bool is_more_general_than(const Representation& other) const {
133 134 135 136
    if (kind_ == kExternal && other.kind_ == kNone) return true;
    if (kind_ == kExternal && other.kind_ == kExternal) return false;
    if (kind_ == kNone && other.kind_ == kExternal) return false;

137 138
    DCHECK_NE(kind_, kExternal);
    DCHECK_NE(other.kind_, kExternal);
139
    if (IsHeapObject()) return other.IsNone();
140 141
    if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
    if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
142 143 144
    return kind_ > other.kind_;
  }

145 146 147 148
  bool fits_into(const Representation& other) const {
    return other.is_more_general_than(*this) || other.Equals(*this);
  }

149
  Representation generalize(Representation other) {
150 151 152
    if (other.fits_into(*this)) return *this;
    if (other.is_more_general_than(*this)) return other;
    return Representation::Tagged();
153 154
  }

155
  int size() const {
156
    DCHECK(!IsNone());
157 158 159 160 161 162 163
    if (IsInteger8() || IsUInteger8()) return kUInt8Size;
    if (IsInteger16() || IsUInteger16()) return kUInt16Size;
    if (IsInteger32()) return kInt32Size;
    if (IsDouble()) return kDoubleSize;
    if (IsExternal()) return kSystemPointerSize;
    DCHECK(IsTagged() || IsSmi() || IsHeapObject());
    return kTaggedSize;
164 165
  }

166 167
  Kind kind() const { return static_cast<Kind>(kind_); }
  bool IsNone() const { return kind_ == kNone; }
168 169 170 171
  bool IsInteger8() const { return kind_ == kInteger8; }
  bool IsUInteger8() const { return kind_ == kUInteger8; }
  bool IsInteger16() const { return kind_ == kInteger16; }
  bool IsUInteger16() const { return kind_ == kUInteger16; }
172 173
  bool IsTagged() const { return kind_ == kTagged; }
  bool IsSmi() const { return kind_ == kSmi; }
174
  bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
175
  bool IsInteger32() const { return kind_ == kInteger32; }
176
  bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
177
  bool IsDouble() const { return kind_ == kDouble; }
178
  bool IsHeapObject() const { return kind_ == kHeapObject; }
179 180
  bool IsExternal() const { return kind_ == kExternal; }
  bool IsSpecialization() const {
181 182 183
    return IsInteger8() || IsUInteger8() ||
      IsInteger16() || IsUInteger16() ||
      IsSmi() || IsInteger32() || IsDouble();
184 185 186 187 188 189 190 191 192 193 194 195 196
  }
  const char* Mnemonic() const;

 private:
  explicit Representation(Kind k) : kind_(k) { }

  // Make sure kind fits in int8.
  STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));

  int8_t kind_;
};


197
static const int kDescriptorIndexBitCount = 10;
198
static const int kFirstInobjectPropertyOffsetBitCount = 7;
199 200 201 202
// The maximum number of descriptors we want in a descriptor array.  It should
// fit in a page and also the following should hold:
// kMaxNumberOfDescriptors + kFieldsAdded <= PropertyArray::kMaxLength.
static const int kMaxNumberOfDescriptors = (1 << kDescriptorIndexBitCount) - 4;
203 204 205
static const int kInvalidEnumCacheSentinel =
    (1 << kDescriptorIndexBitCount) - 1;

206
enum class PropertyCellType {
207 208 209 210 211 212 213 214
  // Meaningful when a property cell does not contain the hole.
  kUndefined,     // The PREMONOMORPHIC of property cells.
  kConstant,      // Cell has been assigned only once.
  kConstantType,  // Cell has been assigned only one type.
  kMutable,       // Cell will no longer be tracked as constant.

  // Meaningful when a property cell contains the hole.
  kUninitialized = kUndefined,  // Cell has never been initialized.
215 216
  kInvalidated = kConstant,     // Cell has been deleted, invalidated or never
                                // existed.
217 218 219 220 221 222 223 224

  // For dictionaries not holding cells.
  kNoCell = kMutable,
};

enum class PropertyCellConstantType {
  kSmi,
  kStableMap,
225 226 227
};


228 229
// PropertyDetails captures type and attributes for a property.
// They are used both in property dictionaries and instance descriptors.
230
class PropertyDetails {
231
 public:
232
  // Property details for dictionary mode properties/elements.
233 234
  PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
                  PropertyCellType cell_type, int dictionary_index = 0) {
235 236
    value_ = KindField::encode(kind) | LocationField::encode(kField) |
             AttributesField::encode(attributes) |
237
             DictionaryStorageField::encode(dictionary_index) |
238
             PropertyCellTypeField::encode(cell_type);
239 240
  }

241
  // Property details for fast mode properties.
242
  PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
243 244 245 246 247
                  PropertyLocation location, PropertyConstness constness,
                  Representation representation, int field_index = 0) {
    value_ = KindField::encode(kind) | AttributesField::encode(attributes) |
             LocationField::encode(location) |
             ConstnessField::encode(constness) |
248 249 250 251
             RepresentationField::encode(EncodeRepresentation(representation)) |
             FieldIndexField::encode(field_index);
  }

252 253
  static PropertyDetails Empty(
      PropertyCellType cell_type = PropertyCellType::kNoCell) {
254
    return PropertyDetails(kData, NONE, cell_type);
255 256
  }

257
  int pointer() const { return DescriptorPointer::decode(value_); }
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  PropertyDetails set_pointer(int i) const {
    return PropertyDetails(value_, i);
  }

  PropertyDetails set_cell_type(PropertyCellType type) const {
    PropertyDetails details = *this;
    details.value_ = PropertyCellTypeField::update(details.value_, type);
    return details;
  }

  PropertyDetails set_index(int index) const {
    PropertyDetails details = *this;
    details.value_ = DictionaryStorageField::update(details.value_, index);
    return details;
  }
274

275
  PropertyDetails CopyWithRepresentation(Representation representation) const {
276 277
    return PropertyDetails(value_, representation);
  }
278 279 280
  PropertyDetails CopyWithConstness(PropertyConstness constness) const {
    return PropertyDetails(value_, constness);
  }
281
  PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) const {
282 283 284 285
    new_attributes =
        static_cast<PropertyAttributes>(attributes() | new_attributes);
    return PropertyDetails(value_, new_attributes);
  }
286

287
  // Conversion for storing details as Object.
288 289
  explicit inline PropertyDetails(Smi smi);
  inline Smi AsSmi() const;
290

291
  static uint8_t EncodeRepresentation(Representation representation) {
292
    return representation.kind();
293 294 295 296 297 298
  }

  static Representation DecodeRepresentation(uint32_t bits) {
    return Representation::FromKind(static_cast<Representation::Kind>(bits));
  }

299 300
  PropertyKind kind() const { return KindField::decode(value_); }
  PropertyLocation location() const { return LocationField::decode(value_); }
301
  PropertyConstness constness() const { return ConstnessField::decode(value_); }
302

303 304 305
  PropertyAttributes attributes() const {
    return AttributesField::decode(value_);
  }
306

307 308 309 310 311
  bool HasKindAndAttributes(PropertyKind kind, PropertyAttributes attributes) {
    return (value_ & (KindField::kMask | AttributesField::kMask)) ==
           (KindField::encode(kind) | AttributesField::encode(attributes));
  }

312
  int dictionary_index() const {
313 314 315
    return DictionaryStorageField::decode(value_);
  }

316
  Representation representation() const {
317 318 319
    return DecodeRepresentation(RepresentationField::decode(value_));
  }

320
  int field_index() const { return FieldIndexField::decode(value_); }
321

322 323
  inline int field_width_in_words() const;

324
  static bool IsValidIndex(int index) {
325
    return DictionaryStorageField::is_valid(index);
326 327
  }

328
  bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
329
  bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
330
  bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
331
  bool IsEnumerable() const { return !IsDontEnum(); }
332 333 334
  PropertyCellType cell_type() const {
    return PropertyCellTypeField::decode(value_);
  }
335 336 337

  // Bit fields in value_ (type, shift, size). Must be public so the
  // constants can be embedded in generated code.
338
  class KindField : public BitField<PropertyKind, 0, 1> {};
339 340 341 342 343 344
  class LocationField : public BitField<PropertyLocation, KindField::kNext, 1> {
  };
  class ConstnessField
      : public BitField<PropertyConstness, LocationField::kNext, 1> {};
  class AttributesField
      : public BitField<PropertyAttributes, ConstnessField::kNext, 3> {};
345 346
  static const int kAttributesReadOnlyMask =
      (READ_ONLY << AttributesField::kShift);
347 348
  static const int kAttributesDontDeleteMask =
      (DONT_DELETE << AttributesField::kShift);
349 350
  static const int kAttributesDontEnumMask =
      (DONT_ENUM << AttributesField::kShift);
351 352

  // Bit fields for normalized objects.
353 354 355 356
  class PropertyCellTypeField
      : public BitField<PropertyCellType, AttributesField::kNext, 2> {};
  class DictionaryStorageField
      : public BitField<uint32_t, PropertyCellTypeField::kNext, 23> {};
357 358

  // Bit fields for fast objects.
359 360
  class RepresentationField
      : public BitField<uint32_t, AttributesField::kNext, 4> {};
361
  class DescriptorPointer
362
      : public BitField<uint32_t, RepresentationField::kNext,
363
                        kDescriptorIndexBitCount> {};  // NOLINT
364 365 366
  class FieldIndexField : public BitField<uint32_t, DescriptorPointer::kNext,
                                          kDescriptorIndexBitCount> {
  };  // NOLINT
367 368 369 370

  // All bits for both fast and slow objects must fit in a smi.
  STATIC_ASSERT(DictionaryStorageField::kNext <= 31);
  STATIC_ASSERT(FieldIndexField::kNext <= 31);
371 372 373

  static const int kInitialIndex = 1;

374 375 376 377 378
#ifdef OBJECT_PRINT
  // For our gdb macros, we should perhaps change these in the future.
  void Print(bool dictionary_mode);
#endif

379 380 381 382 383 384 385 386 387 388 389 390 391
  enum PrintMode {
    kPrintAttributes = 1 << 0,
    kPrintFieldIndex = 1 << 1,
    kPrintRepresentation = 1 << 2,
    kPrintPointer = 1 << 3,

    kForProperties = kPrintFieldIndex,
    kForTransitions = kPrintAttributes,
    kPrintFull = -1,
  };
  void PrintAsSlowTo(std::ostream& out);
  void PrintAsFastTo(std::ostream& out, PrintMode mode = kPrintFull);

392
 private:
393
  PropertyDetails(int value, int pointer) {
394 395 396 397 398
    value_ = DescriptorPointer::update(value, pointer);
  }
  PropertyDetails(int value, Representation representation) {
    value_ = RepresentationField::update(
        value, EncodeRepresentation(representation));
399
  }
400 401 402
  PropertyDetails(int value, PropertyConstness constness) {
    value_ = ConstnessField::update(value, constness);
  }
403 404 405
  PropertyDetails(int value, PropertyAttributes attributes) {
    value_ = AttributesField::update(value, attributes);
  }
406

407 408 409
  uint32_t value_;
};

410 411 412 413 414 415
// kField location is more general than kDescriptor, kDescriptor generalizes
// only to itself.
inline bool IsGeneralizableTo(PropertyLocation a, PropertyLocation b) {
  return b == kField || a == kDescriptor;
}

416 417
// PropertyConstness::kMutable constness is more general than
// VariableMode::kConst, VariableMode::kConst generalizes only to itself.
418
inline bool IsGeneralizableTo(PropertyConstness a, PropertyConstness b) {
419
  return b == PropertyConstness::kMutable || a == PropertyConstness::kConst;
420 421 422 423
}

inline PropertyConstness GeneralizeConstness(PropertyConstness a,
                                             PropertyConstness b) {
424
  return a == PropertyConstness::kMutable ? PropertyConstness::kMutable : b;
425
}
426 427 428

std::ostream& operator<<(std::ostream& os,
                         const PropertyAttributes& attributes);
429 430
}  // namespace internal
}  // namespace v8
431 432

#endif  // V8_PROPERTY_DETAILS_H_