blob: e30d6681aaa0cb59adff90fedba93a5a5e580214 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004
5#ifndef V8_PROPERTY_DETAILS_H_
6#define V8_PROPERTY_DETAILS_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "include/v8.h"
9#include "src/allocation.h"
10#include "src/utils.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012namespace v8 {
13namespace internal {
14
15// ES6 6.1.7.1
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016enum PropertyAttributes {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017 NONE = ::v8::None,
18 READ_ONLY = ::v8::ReadOnly,
19 DONT_ENUM = ::v8::DontEnum,
20 DONT_DELETE = ::v8::DontDelete,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022 ALL_ATTRIBUTES_MASK = READ_ONLY | DONT_ENUM | DONT_DELETE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024 SEALED = DONT_DELETE,
25 FROZEN = SEALED | READ_ONLY,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027 ABSENT = 64, // Used in runtime to indicate a property is absent.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010028 // 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034enum 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.
45STATIC_ASSERT(ALL_PROPERTIES == static_cast<PropertyFilter>(NONE));
46STATIC_ASSERT(ONLY_WRITABLE == static_cast<PropertyFilter>(READ_ONLY));
47STATIC_ASSERT(ONLY_ENUMERABLE == static_cast<PropertyFilter>(DONT_ENUM));
48STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(DONT_DELETE));
49STATIC_ASSERT(((SKIP_STRINGS | SKIP_SYMBOLS | ONLY_ALL_CAN_READ) &
50 ALL_ATTRIBUTES_MASK) == 0);
Ben Murdochc5610432016-08-08 18:44:38 +010051STATIC_ASSERT(ALL_PROPERTIES ==
52 static_cast<PropertyFilter>(v8::PropertyFilter::ALL_PROPERTIES));
53STATIC_ASSERT(ONLY_WRITABLE ==
54 static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_WRITABLE));
55STATIC_ASSERT(ONLY_ENUMERABLE ==
56 static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_ENUMERABLE));
57STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(
58 v8::PropertyFilter::ONLY_CONFIGURABLE));
59STATIC_ASSERT(SKIP_STRINGS ==
60 static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_STRINGS));
61STATIC_ASSERT(SKIP_SYMBOLS ==
62 static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_SYMBOLS));
Ben Murdoch3ef787d2012-04-12 10:51:47 +010063
64class Smi;
Ben Murdoch097c5b22016-05-18 11:27:45 +010065class Type;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066class TypeInfo;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010067
68// Type of properties.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040069// Order of kinds is significant.
70// Must fit in the BitField PropertyDetails::KindField.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071enum PropertyKind { kData = 0, kAccessor = 1 };
Emily Bernierd0a1eb72015-03-24 16:35:39 -040072
73
74// Order of modes is significant.
75// Must fit in the BitField PropertyDetails::StoreModeField.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076enum PropertyLocation { kField = 0, kDescriptor = 1 };
Emily Bernierd0a1eb72015-03-24 16:35:39 -040077
78
Ben Murdoch3ef787d2012-04-12 10:51:47 +010079// Order of properties is significant.
80// Must fit in the BitField PropertyDetails::TypeField.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081// A copy of this is in debug/mirrors.js.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010082enum PropertyType {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000083 DATA = (kField << 1) | kData,
84 DATA_CONSTANT = (kDescriptor << 1) | kData,
85 ACCESSOR = (kField << 1) | kAccessor,
86 ACCESSOR_CONSTANT = (kDescriptor << 1) | kAccessor
Ben Murdoch3ef787d2012-04-12 10:51:47 +010087};
88
89
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090class 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 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
204static const int kDescriptorIndexBitCount = 10;
205// The maximum number of descriptors we want in a descriptor array (should
206// fit in a page).
207static const int kMaxNumberOfDescriptors =
208 (1 << kDescriptorIndexBitCount) - 2;
209static const int kInvalidEnumCacheSentinel =
210 (1 << kDescriptorIndexBitCount) - 1;
211
212
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213enum 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
229enum class PropertyCellConstantType {
230 kSmi,
231 kStableMap,
232};
233
234
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100235// PropertyDetails captures type and attributes for a property.
236// They are used both in property dictionaries and instance descriptors.
237class PropertyDetails BASE_EMBEDDED {
238 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000239 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);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100244
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000259 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272 int pointer() const { return DescriptorPointer::decode(value_); }
273
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000274 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 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289
290 PropertyDetails CopyWithRepresentation(Representation representation) const {
291 return PropertyDetails(value_, representation);
292 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293 PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 new_attributes =
295 static_cast<PropertyAttributes>(attributes() | new_attributes);
296 return PropertyDetails(value_, new_attributes);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100297 }
298
299 // Conversion for storing details as Object*.
300 explicit inline PropertyDetails(Smi* smi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301 inline Smi* AsSmi() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100302
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000303 static uint8_t EncodeRepresentation(Representation representation) {
304 return representation.kind();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100305 }
306
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000307 static Representation DecodeRepresentation(uint32_t bits) {
308 return Representation::FromKind(static_cast<Representation::Kind>(bits));
309 }
310
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400311 PropertyKind kind() const { return KindField::decode(value_); }
312 PropertyLocation location() const { return LocationField::decode(value_); }
313
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 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 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325 return DecodeRepresentation(RepresentationField::decode(value_));
326 }
327
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400328 int field_index() const { return FieldIndexField::decode(value_); }
329
330 inline int field_width_in_words() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000331
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 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; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100339 bool IsEnumerable() const { return !IsDontEnum(); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000340 PropertyCellType cell_type() const {
341 return PropertyCellTypeField::decode(value_);
342 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100343
344 // Bit fields in value_ (type, shift, size). Must be public so the
345 // constants can be embedded in generated code.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400346 class KindField : public BitField<PropertyKind, 0, 1> {};
347 class LocationField : public BitField<PropertyLocation, 1, 1> {};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 class AttributesField : public BitField<PropertyAttributes, 2, 3> {};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000349 static const int kAttributesReadOnlyMask =
350 (READ_ONLY << AttributesField::kShift);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000351
352 // Bit fields for normalized objects.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000353 class PropertyCellTypeField : public BitField<PropertyCellType, 5, 2> {};
354 class DictionaryStorageField : public BitField<uint32_t, 7, 24> {};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355
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
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400363
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);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100373
374 static const int kInitialIndex = 1;
375
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400376#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
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100381 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382 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
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100393 uint32_t value_;
394};
395
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400396
397std::ostream& operator<<(std::ostream& os,
398 const PropertyAttributes& attributes);
399std::ostream& operator<<(std::ostream& os, const PropertyDetails& details);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000400} // namespace internal
401} // namespace v8
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100402
403#endif // V8_PROPERTY_DETAILS_H_