property-details.h 13.7 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 10
#include "include/v8.h"
#include "src/allocation.h"
#include "src/utils.h"
11

12 13 14 15
namespace v8 {
namespace internal {

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

  ALL_ATTRIBUTES_MASK = READ_ONLY | DONT_ENUM | DONT_DELETE,
23

24 25
  SEALED = DONT_DELETE,
  FROZEN = SEALED | READ_ONLY,
26

27
  ABSENT = 64,  // Used in runtime to indicate a property is absent.
28 29 30 31 32 33
  // 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.
};


34 35
enum PropertyFilter {
  ALL_PROPERTIES = 0,
36
  ONLY_WRITABLE = 1,
37
  ONLY_ENUMERABLE = 2,
38
  ONLY_CONFIGURABLE = 4,
39 40 41 42 43 44 45
  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));
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 50
STATIC_ASSERT(((SKIP_STRINGS | SKIP_SYMBOLS | ONLY_ALL_CAN_READ) &
               ALL_ATTRIBUTES_MASK) == 0);
51 52 53 54 55 56 57 58 59 60 61 62
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));
63 64

class Smi;
65
class TypeInfo;
66 67

// Type of properties.
68 69
// Order of kinds is significant.
// Must fit in the BitField PropertyDetails::KindField.
70
enum PropertyKind { kData = 0, kAccessor = 1 };
71 72 73 74


// Order of modes is significant.
// Must fit in the BitField PropertyDetails::StoreModeField.
75
enum PropertyLocation { kField = 0, kDescriptor = 1 };
76 77


78 79
// Order of properties is significant.
// Must fit in the BitField PropertyDetails::TypeField.
80
// A copy of this is in debug/mirrors.js.
81
enum PropertyType {
82 83 84 85
  DATA = (kField << 1) | kData,
  DATA_CONSTANT = (kDescriptor << 1) | kData,
  ACCESSOR = (kField << 1) | kAccessor,
  ACCESSOR_CONSTANT = (kDescriptor << 1) | kAccessor
86
};
87 88


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

  Representation() : kind_(kNone) { }

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

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

122
  bool Equals(const Representation& other) const {
123 124 125
    return kind_ == other.kind_;
  }

126 127 128 129 130
  bool IsCompatibleForLoad(const Representation& other) const {
    return (IsDouble() && other.IsDouble()) ||
        (!IsDouble() && !other.IsDouble());
  }

131 132 133 134
  bool IsCompatibleForStore(const Representation& other) const {
    return Equals(other);
  }

135
  bool is_more_general_than(const Representation& other) const {
136 137 138 139
    if (kind_ == kExternal && other.kind_ == kNone) return true;
    if (kind_ == kExternal && other.kind_ == kExternal) return false;
    if (kind_ == kNone && other.kind_ == kExternal) return false;

140 141
    DCHECK(kind_ != kExternal);
    DCHECK(other.kind_ != kExternal);
142
    if (IsHeapObject()) return other.IsNone();
143 144
    if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
    if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
145 146 147
    return kind_ > other.kind_;
  }

148 149 150 151
  bool fits_into(const Representation& other) const {
    return other.is_more_general_than(*this) || other.Equals(*this);
  }

152
  Representation generalize(Representation other) {
153 154 155
    if (other.fits_into(*this)) return *this;
    if (other.is_more_general_than(*this)) return other;
    return Representation::Tagged();
156 157
  }

158
  int size() const {
159
    DCHECK(!IsNone());
160 161 162 163 164 165 166 167 168 169 170 171
    if (IsInteger8() || IsUInteger8()) {
      return sizeof(uint8_t);
    }
    if (IsInteger16() || IsUInteger16()) {
      return sizeof(uint16_t);
    }
    if (IsInteger32()) {
      return sizeof(uint32_t);
    }
    return kPointerSize;
  }

172 173
  Kind kind() const { return static_cast<Kind>(kind_); }
  bool IsNone() const { return kind_ == kNone; }
174 175 176 177
  bool IsInteger8() const { return kind_ == kInteger8; }
  bool IsUInteger8() const { return kind_ == kUInteger8; }
  bool IsInteger16() const { return kind_ == kInteger16; }
  bool IsUInteger16() const { return kind_ == kUInteger16; }
178 179
  bool IsTagged() const { return kind_ == kTagged; }
  bool IsSmi() const { return kind_ == kSmi; }
180
  bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
181
  bool IsInteger32() const { return kind_ == kInteger32; }
182
  bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
183
  bool IsDouble() const { return kind_ == kDouble; }
184
  bool IsHeapObject() const { return kind_ == kHeapObject; }
185 186
  bool IsExternal() const { return kind_ == kExternal; }
  bool IsSpecialization() const {
187 188 189
    return IsInteger8() || IsUInteger8() ||
      IsInteger16() || IsUInteger16() ||
      IsSmi() || IsInteger32() || IsDouble();
190 191 192 193 194 195 196 197 198 199 200 201 202
  }
  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_;
};


203 204 205 206 207 208 209 210
static const int kDescriptorIndexBitCount = 10;
// The maximum number of descriptors we want in a descriptor array (should
// fit in a page).
static const int kMaxNumberOfDescriptors =
    (1 << kDescriptorIndexBitCount) - 2;
static const int kInvalidEnumCacheSentinel =
    (1 << kDescriptorIndexBitCount) - 1;

211
enum class PropertyCellType {
212 213 214 215 216 217 218 219
  // 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.
220 221
  kInvalidated = kConstant,     // Cell has been deleted, invalidated or never
                                // existed.
222 223 224 225 226 227 228 229

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

enum class PropertyCellConstantType {
  kSmi,
  kStableMap,
230 231 232
};


233 234 235 236
// PropertyDetails captures type and attributes for a property.
// They are used both in property dictionaries and instance descriptors.
class PropertyDetails BASE_EMBEDDED {
 public:
237 238 239 240 241
  PropertyDetails(PropertyAttributes attributes, PropertyType type, int index,
                  PropertyCellType cell_type) {
    value_ = TypeField::encode(type) | AttributesField::encode(attributes) |
             DictionaryStorageField::encode(index) |
             PropertyCellTypeField::encode(cell_type);
242

243 244
    DCHECK(type == this->type());
    DCHECK(attributes == this->attributes());
245 246 247 248
  }

  PropertyDetails(PropertyAttributes attributes,
                  PropertyType type,
249 250
                  Representation representation,
                  int field_index = 0) {
251 252
    value_ = TypeField::encode(type)
        | AttributesField::encode(attributes)
253 254
        | RepresentationField::encode(EncodeRepresentation(representation))
        | FieldIndexField::encode(field_index);
255 256
  }

257 258 259 260 261 262 263 264 265
  PropertyDetails(PropertyAttributes attributes, PropertyKind kind,
                  PropertyLocation location, Representation representation,
                  int field_index = 0) {
    value_ = KindField::encode(kind) | LocationField::encode(location) |
             AttributesField::encode(attributes) |
             RepresentationField::encode(EncodeRepresentation(representation)) |
             FieldIndexField::encode(field_index);
  }

266 267 268
  static PropertyDetails Empty(
      PropertyCellType cell_type = PropertyCellType::kNoCell) {
    return PropertyDetails(NONE, DATA, 0, cell_type);
269 270
  }

271
  int pointer() const { return DescriptorPointer::decode(value_); }
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
  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;
  }
288

289
  PropertyDetails CopyWithRepresentation(Representation representation) const {
290 291
    return PropertyDetails(value_, representation);
  }
292
  PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) const {
293 294 295 296
    new_attributes =
        static_cast<PropertyAttributes>(attributes() | new_attributes);
    return PropertyDetails(value_, new_attributes);
  }
297

298 299
  // Conversion for storing details as Object*.
  explicit inline PropertyDetails(Smi* smi);
300
  inline Smi* AsSmi() const;
301

302
  static uint8_t EncodeRepresentation(Representation representation) {
303
    return representation.kind();
304 305 306 307 308 309
  }

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

310 311 312
  PropertyKind kind() const { return KindField::decode(value_); }
  PropertyLocation location() const { return LocationField::decode(value_); }

313
  PropertyType type() const { return TypeField::decode(value_); }
314

315 316 317
  PropertyAttributes attributes() const {
    return AttributesField::decode(value_);
  }
318

319
  int dictionary_index() const {
320 321 322
    return DictionaryStorageField::decode(value_);
  }

323
  Representation representation() const {
324 325 326
    return DecodeRepresentation(RepresentationField::decode(value_));
  }

327
  int field_index() const { return FieldIndexField::decode(value_); }
328

329 330
  inline int field_width_in_words() const;

331
  static bool IsValidIndex(int index) {
332
    return DictionaryStorageField::is_valid(index);
333 334
  }

335
  bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
336
  bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
337
  bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
338
  bool IsEnumerable() const { return !IsDontEnum(); }
339 340 341
  PropertyCellType cell_type() const {
    return PropertyCellTypeField::decode(value_);
  }
342 343 344

  // Bit fields in value_ (type, shift, size). Must be public so the
  // constants can be embedded in generated code.
345 346
  class KindField : public BitField<PropertyKind, 0, 1> {};
  class LocationField : public BitField<PropertyLocation, 1, 1> {};
347
  class AttributesField : public BitField<PropertyAttributes, 2, 3> {};
348 349
  static const int kAttributesReadOnlyMask =
      (READ_ONLY << AttributesField::kShift);
350 351

  // Bit fields for normalized objects.
352 353
  class PropertyCellTypeField : public BitField<PropertyCellType, 5, 2> {};
  class DictionaryStorageField : public BitField<uint32_t, 7, 24> {};
354 355

  // Bit fields for fast objects.
356 357 358 359 360 361
  class RepresentationField : public BitField<uint32_t, 5, 4> {};
  class DescriptorPointer
      : public BitField<uint32_t, 9, kDescriptorIndexBitCount> {};  // NOLINT
  class FieldIndexField
      : public BitField<uint32_t, 9 + kDescriptorIndexBitCount,
                        kDescriptorIndexBitCount> {};  // NOLINT
362

363 364 365 366 367 368
  // NOTE: TypeField overlaps with KindField and LocationField.
  class TypeField : public BitField<PropertyType, 0, 2> {};
  STATIC_ASSERT(KindField::kNext == LocationField::kShift);
  STATIC_ASSERT(TypeField::kShift == KindField::kShift);
  STATIC_ASSERT(TypeField::kNext == LocationField::kNext);

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

  static const int kInitialIndex = 1;

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

380
 private:
381
  PropertyDetails(int value, int pointer) {
382 383 384 385 386
    value_ = DescriptorPointer::update(value, pointer);
  }
  PropertyDetails(int value, Representation representation) {
    value_ = RepresentationField::update(
        value, EncodeRepresentation(representation));
387
  }
388 389 390
  PropertyDetails(int value, PropertyAttributes attributes) {
    value_ = AttributesField::update(value, attributes);
  }
391

392 393 394
  uint32_t value_;
};

395 396 397 398

std::ostream& operator<<(std::ostream& os,
                         const PropertyAttributes& attributes);
std::ostream& operator<<(std::ostream& os, const PropertyDetails& details);
399 400
}  // namespace internal
}  // namespace v8
401 402

#endif  // V8_PROPERTY_DETAILS_H_