blob: 8df7307be5af7f7d4da3bb75ad20d2506e709145 [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.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031
32 // When creating a property, EVAL_DECLARED used to indicate that the property
33 // came from a sloppy-mode direct eval, and certain checks need to be done.
34 // Cannot be stored in or returned from a descriptor's attributes bitfield.
35 EVAL_DECLARED = 128
Ben Murdoch3ef787d2012-04-12 10:51:47 +010036};
37
38
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000039enum PropertyFilter {
40 ALL_PROPERTIES = 0,
41 ONLY_WRITABLE = 1,
42 ONLY_ENUMERABLE = 2,
43 ONLY_CONFIGURABLE = 4,
44 SKIP_STRINGS = 8,
45 SKIP_SYMBOLS = 16,
46 ONLY_ALL_CAN_READ = 32,
47 ENUMERABLE_STRINGS = ONLY_ENUMERABLE | SKIP_SYMBOLS,
48};
49// Enable fast comparisons of PropertyAttributes against PropertyFilters.
50STATIC_ASSERT(ALL_PROPERTIES == static_cast<PropertyFilter>(NONE));
51STATIC_ASSERT(ONLY_WRITABLE == static_cast<PropertyFilter>(READ_ONLY));
52STATIC_ASSERT(ONLY_ENUMERABLE == static_cast<PropertyFilter>(DONT_ENUM));
53STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(DONT_DELETE));
54STATIC_ASSERT(((SKIP_STRINGS | SKIP_SYMBOLS | ONLY_ALL_CAN_READ) &
55 ALL_ATTRIBUTES_MASK) == 0);
Ben Murdochc5610432016-08-08 18:44:38 +010056STATIC_ASSERT(ALL_PROPERTIES ==
57 static_cast<PropertyFilter>(v8::PropertyFilter::ALL_PROPERTIES));
58STATIC_ASSERT(ONLY_WRITABLE ==
59 static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_WRITABLE));
60STATIC_ASSERT(ONLY_ENUMERABLE ==
61 static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_ENUMERABLE));
62STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(
63 v8::PropertyFilter::ONLY_CONFIGURABLE));
64STATIC_ASSERT(SKIP_STRINGS ==
65 static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_STRINGS));
66STATIC_ASSERT(SKIP_SYMBOLS ==
67 static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_SYMBOLS));
Ben Murdoch3ef787d2012-04-12 10:51:47 +010068
69class Smi;
Ben Murdoch097c5b22016-05-18 11:27:45 +010070class Type;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071class TypeInfo;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010072
73// Type of properties.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040074// Order of kinds is significant.
75// Must fit in the BitField PropertyDetails::KindField.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076enum PropertyKind { kData = 0, kAccessor = 1 };
Emily Bernierd0a1eb72015-03-24 16:35:39 -040077
78
79// Order of modes is significant.
80// Must fit in the BitField PropertyDetails::StoreModeField.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081enum PropertyLocation { kField = 0, kDescriptor = 1 };
Emily Bernierd0a1eb72015-03-24 16:35:39 -040082
83
Ben Murdoch3ef787d2012-04-12 10:51:47 +010084// Order of properties is significant.
85// Must fit in the BitField PropertyDetails::TypeField.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086// A copy of this is in debug/mirrors.js.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010087enum PropertyType {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088 DATA = (kField << 1) | kData,
89 DATA_CONSTANT = (kDescriptor << 1) | kData,
90 ACCESSOR = (kField << 1) | kAccessor,
91 ACCESSOR_CONSTANT = (kDescriptor << 1) | kAccessor
Ben Murdoch3ef787d2012-04-12 10:51:47 +010092};
93
94
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095class Representation {
96 public:
97 enum Kind {
98 kNone,
99 kInteger8,
100 kUInteger8,
101 kInteger16,
102 kUInteger16,
103 kSmi,
104 kInteger32,
105 kDouble,
106 kHeapObject,
107 kTagged,
108 kExternal,
109 kNumRepresentations
110 };
111
112 Representation() : kind_(kNone) { }
113
114 static Representation None() { return Representation(kNone); }
115 static Representation Tagged() { return Representation(kTagged); }
116 static Representation Integer8() { return Representation(kInteger8); }
117 static Representation UInteger8() { return Representation(kUInteger8); }
118 static Representation Integer16() { return Representation(kInteger16); }
119 static Representation UInteger16() { return Representation(kUInteger16); }
120 static Representation Smi() { return Representation(kSmi); }
121 static Representation Integer32() { return Representation(kInteger32); }
122 static Representation Double() { return Representation(kDouble); }
123 static Representation HeapObject() { return Representation(kHeapObject); }
124 static Representation External() { return Representation(kExternal); }
125
126 static Representation FromKind(Kind kind) { return Representation(kind); }
127
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 bool Equals(const Representation& other) const {
129 return kind_ == other.kind_;
130 }
131
132 bool IsCompatibleForLoad(const Representation& other) const {
133 return (IsDouble() && other.IsDouble()) ||
134 (!IsDouble() && !other.IsDouble());
135 }
136
137 bool IsCompatibleForStore(const Representation& other) const {
138 return Equals(other);
139 }
140
141 bool is_more_general_than(const Representation& other) const {
142 if (kind_ == kExternal && other.kind_ == kNone) return true;
143 if (kind_ == kExternal && other.kind_ == kExternal) return false;
144 if (kind_ == kNone && other.kind_ == kExternal) return false;
145
146 DCHECK(kind_ != kExternal);
147 DCHECK(other.kind_ != kExternal);
148 if (IsHeapObject()) return other.IsNone();
149 if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
150 if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
151 return kind_ > other.kind_;
152 }
153
154 bool fits_into(const Representation& other) const {
155 return other.is_more_general_than(*this) || other.Equals(*this);
156 }
157
158 Representation generalize(Representation other) {
159 if (other.fits_into(*this)) return *this;
160 if (other.is_more_general_than(*this)) return other;
161 return Representation::Tagged();
162 }
163
164 int size() const {
165 DCHECK(!IsNone());
166 if (IsInteger8() || IsUInteger8()) {
167 return sizeof(uint8_t);
168 }
169 if (IsInteger16() || IsUInteger16()) {
170 return sizeof(uint16_t);
171 }
172 if (IsInteger32()) {
173 return sizeof(uint32_t);
174 }
175 return kPointerSize;
176 }
177
178 Kind kind() const { return static_cast<Kind>(kind_); }
179 bool IsNone() const { return kind_ == kNone; }
180 bool IsInteger8() const { return kind_ == kInteger8; }
181 bool IsUInteger8() const { return kind_ == kUInteger8; }
182 bool IsInteger16() const { return kind_ == kInteger16; }
183 bool IsUInteger16() const { return kind_ == kUInteger16; }
184 bool IsTagged() const { return kind_ == kTagged; }
185 bool IsSmi() const { return kind_ == kSmi; }
186 bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
187 bool IsInteger32() const { return kind_ == kInteger32; }
188 bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
189 bool IsDouble() const { return kind_ == kDouble; }
190 bool IsHeapObject() const { return kind_ == kHeapObject; }
191 bool IsExternal() const { return kind_ == kExternal; }
192 bool IsSpecialization() const {
193 return IsInteger8() || IsUInteger8() ||
194 IsInteger16() || IsUInteger16() ||
195 IsSmi() || IsInteger32() || IsDouble();
196 }
197 const char* Mnemonic() const;
198
199 private:
200 explicit Representation(Kind k) : kind_(k) { }
201
202 // Make sure kind fits in int8.
203 STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
204
205 int8_t kind_;
206};
207
208
209static const int kDescriptorIndexBitCount = 10;
210// The maximum number of descriptors we want in a descriptor array (should
211// fit in a page).
212static const int kMaxNumberOfDescriptors =
213 (1 << kDescriptorIndexBitCount) - 2;
214static const int kInvalidEnumCacheSentinel =
215 (1 << kDescriptorIndexBitCount) - 1;
216
217
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000218enum class PropertyCellType {
219 // Meaningful when a property cell does not contain the hole.
220 kUndefined, // The PREMONOMORPHIC of property cells.
221 kConstant, // Cell has been assigned only once.
222 kConstantType, // Cell has been assigned only one type.
223 kMutable, // Cell will no longer be tracked as constant.
224
225 // Meaningful when a property cell contains the hole.
226 kUninitialized = kUndefined, // Cell has never been initialized.
227 kInvalidated = kConstant, // Cell has been deleted or invalidated.
228
229 // For dictionaries not holding cells.
230 kNoCell = kMutable,
231};
232
233
234enum class PropertyCellConstantType {
235 kSmi,
236 kStableMap,
237};
238
239
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100240// PropertyDetails captures type and attributes for a property.
241// They are used both in property dictionaries and instance descriptors.
242class PropertyDetails BASE_EMBEDDED {
243 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 PropertyDetails(PropertyAttributes attributes, PropertyType type, int index,
245 PropertyCellType cell_type) {
246 value_ = TypeField::encode(type) | AttributesField::encode(attributes) |
247 DictionaryStorageField::encode(index) |
248 PropertyCellTypeField::encode(cell_type);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100249
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 DCHECK(type == this->type());
251 DCHECK(attributes == this->attributes());
252 }
253
254 PropertyDetails(PropertyAttributes attributes,
255 PropertyType type,
256 Representation representation,
257 int field_index = 0) {
258 value_ = TypeField::encode(type)
259 | AttributesField::encode(attributes)
260 | RepresentationField::encode(EncodeRepresentation(representation))
261 | FieldIndexField::encode(field_index);
262 }
263
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000264 PropertyDetails(PropertyAttributes attributes, PropertyKind kind,
265 PropertyLocation location, Representation representation,
266 int field_index = 0) {
267 value_ = KindField::encode(kind) | LocationField::encode(location) |
268 AttributesField::encode(attributes) |
269 RepresentationField::encode(EncodeRepresentation(representation)) |
270 FieldIndexField::encode(field_index);
271 }
272
273 static PropertyDetails Empty() {
274 return PropertyDetails(NONE, DATA, 0, PropertyCellType::kNoCell);
275 }
276
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 int pointer() const { return DescriptorPointer::decode(value_); }
278
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000279 PropertyDetails set_pointer(int i) const {
280 return PropertyDetails(value_, i);
281 }
282
283 PropertyDetails set_cell_type(PropertyCellType type) const {
284 PropertyDetails details = *this;
285 details.value_ = PropertyCellTypeField::update(details.value_, type);
286 return details;
287 }
288
289 PropertyDetails set_index(int index) const {
290 PropertyDetails details = *this;
291 details.value_ = DictionaryStorageField::update(details.value_, index);
292 return details;
293 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294
295 PropertyDetails CopyWithRepresentation(Representation representation) const {
296 return PropertyDetails(value_, representation);
297 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000298 PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299 new_attributes =
300 static_cast<PropertyAttributes>(attributes() | new_attributes);
301 return PropertyDetails(value_, new_attributes);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100302 }
303
304 // Conversion for storing details as Object*.
305 explicit inline PropertyDetails(Smi* smi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 inline Smi* AsSmi() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100307
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 static uint8_t EncodeRepresentation(Representation representation) {
309 return representation.kind();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100310 }
311
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 static Representation DecodeRepresentation(uint32_t bits) {
313 return Representation::FromKind(static_cast<Representation::Kind>(bits));
314 }
315
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400316 PropertyKind kind() const { return KindField::decode(value_); }
317 PropertyLocation location() const { return LocationField::decode(value_); }
318
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319 PropertyType type() const { return TypeField::decode(value_); }
320
321 PropertyAttributes attributes() const {
322 return AttributesField::decode(value_);
323 }
324
325 int dictionary_index() const {
326 return DictionaryStorageField::decode(value_);
327 }
328
329 Representation representation() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000330 return DecodeRepresentation(RepresentationField::decode(value_));
331 }
332
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400333 int field_index() const { return FieldIndexField::decode(value_); }
334
335 inline int field_width_in_words() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337 static bool IsValidIndex(int index) {
338 return DictionaryStorageField::is_valid(index);
339 }
340
341 bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
342 bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
343 bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100344 bool IsEnumerable() const { return !IsDontEnum(); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000345 PropertyCellType cell_type() const {
346 return PropertyCellTypeField::decode(value_);
347 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100348
349 // Bit fields in value_ (type, shift, size). Must be public so the
350 // constants can be embedded in generated code.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400351 class KindField : public BitField<PropertyKind, 0, 1> {};
352 class LocationField : public BitField<PropertyLocation, 1, 1> {};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353 class AttributesField : public BitField<PropertyAttributes, 2, 3> {};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000354 static const int kAttributesReadOnlyMask =
355 (READ_ONLY << AttributesField::kShift);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356
357 // Bit fields for normalized objects.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000358 class PropertyCellTypeField : public BitField<PropertyCellType, 5, 2> {};
359 class DictionaryStorageField : public BitField<uint32_t, 7, 24> {};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360
361 // Bit fields for fast objects.
362 class RepresentationField : public BitField<uint32_t, 5, 4> {};
363 class DescriptorPointer
364 : public BitField<uint32_t, 9, kDescriptorIndexBitCount> {}; // NOLINT
365 class FieldIndexField
366 : public BitField<uint32_t, 9 + kDescriptorIndexBitCount,
367 kDescriptorIndexBitCount> {}; // NOLINT
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400368
369 // NOTE: TypeField overlaps with KindField and LocationField.
370 class TypeField : public BitField<PropertyType, 0, 2> {};
371 STATIC_ASSERT(KindField::kNext == LocationField::kShift);
372 STATIC_ASSERT(TypeField::kShift == KindField::kShift);
373 STATIC_ASSERT(TypeField::kNext == LocationField::kNext);
374
375 // All bits for both fast and slow objects must fit in a smi.
376 STATIC_ASSERT(DictionaryStorageField::kNext <= 31);
377 STATIC_ASSERT(FieldIndexField::kNext <= 31);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100378
379 static const int kInitialIndex = 1;
380
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400381#ifdef OBJECT_PRINT
382 // For our gdb macros, we should perhaps change these in the future.
383 void Print(bool dictionary_mode);
384#endif
385
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100386 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387 PropertyDetails(int value, int pointer) {
388 value_ = DescriptorPointer::update(value, pointer);
389 }
390 PropertyDetails(int value, Representation representation) {
391 value_ = RepresentationField::update(
392 value, EncodeRepresentation(representation));
393 }
394 PropertyDetails(int value, PropertyAttributes attributes) {
395 value_ = AttributesField::update(value, attributes);
396 }
397
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100398 uint32_t value_;
399};
400
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400401
402std::ostream& operator<<(std::ostream& os,
403 const PropertyAttributes& attributes);
404std::ostream& operator<<(std::ostream& os, const PropertyDetails& details);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000405} // namespace internal
406} // namespace v8
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100407
408#endif // V8_PROPERTY_DETAILS_H_