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
14namespace v8 {
15namespace internal {
16
17// ES6 6.1.7.1
18enum 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
35enum 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.
47STATIC_ASSERT(ALL_PROPERTIES == static_cast<PropertyFilter>(NONE));
48STATIC_ASSERT(ONLY_WRITABLE == static_cast<PropertyFilter>(READ_ONLY));
49STATIC_ASSERT(ONLY_ENUMERABLE == static_cast<PropertyFilter>(DONT_ENUM));
50STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(DONT_DELETE));
51STATIC_ASSERT(((SKIP_STRINGS | SKIP_SYMBOLS | ONLY_ALL_CAN_READ) &
52 ALL_ATTRIBUTES_MASK) == 0);
53STATIC_ASSERT(ALL_PROPERTIES ==
54 static_cast<PropertyFilter>(v8::PropertyFilter::ALL_PROPERTIES));
55STATIC_ASSERT(ONLY_WRITABLE ==
56 static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_WRITABLE));
57STATIC_ASSERT(ONLY_ENUMERABLE ==
58 static_cast<PropertyFilter>(v8::PropertyFilter::ONLY_ENUMERABLE));
59STATIC_ASSERT(ONLY_CONFIGURABLE == static_cast<PropertyFilter>(
60 v8::PropertyFilter::ONLY_CONFIGURABLE));
61STATIC_ASSERT(SKIP_STRINGS ==
62 static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_STRINGS));
63STATIC_ASSERT(SKIP_SYMBOLS ==
64 static_cast<PropertyFilter>(v8::PropertyFilter::SKIP_SYMBOLS));
65
66class Smi;
67class TypeInfo;
68
69// Order of kinds is significant.
70// Must fit in the BitField PropertyDetails::KindField.
71enum PropertyKind { kData = 0, kAccessor = 1 };
72
73// Order of modes is significant.
74// Must fit in the BitField PropertyDetails::LocationField.
75enum PropertyLocation { kField = 0, kDescriptor = 1 };
76
77// Order of modes is significant.
78// Must fit in the BitField PropertyDetails::ConstnessField.
79enum class PropertyConstness { kMutable = 0, kConst = 1 };
80
81// TODO(ishell): remove once constant field tracking is done.
82const PropertyConstness kDefaultFieldConstness =
83 FLAG_track_constant_fields ? PropertyConstness::kConst
84 : PropertyConstness::kMutable;
85
86class Representation {
87 public:
88 enum Kind {
89 kNone,
90 kSmi,
91 kDouble,
92 kHeapObject,
93 kTagged,
94 kNumRepresentations
95 };
96
97 Representation() : kind_(kNone) { }
98
99 static Representation None() { return Representation(kNone); }
100 static Representation Tagged() { return Representation(kTagged); }
101 static Representation Smi() { return Representation(kSmi); }
102 static Representation Double() { return Representation(kDouble); }
103 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 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 CanBeInPlaceChangedTo(const Representation& other) const {
120 if (IsNone()) return true;
121 if (!FLAG_modify_field_representation_inplace) return false;
122 return (IsSmi() || IsHeapObject()) && other.IsTagged();
123 }
124
125 bool is_more_general_than(const Representation& other) const {
126 if (IsHeapObject()) return other.IsNone();
127 return kind_ > other.kind_;
128 }
129
130 bool fits_into(const Representation& other) const {
131 return other.is_more_general_than(*this) || other.Equals(*this);
132 }
133
134 Representation generalize(Representation other) {
135 if (other.fits_into(*this)) return *this;
136 if (other.is_more_general_than(*this)) return other;
137 return Representation::Tagged();
138 }
139
140 int size() const {
141 DCHECK(!IsNone());
142 if (IsDouble()) return kDoubleSize;
143 DCHECK(IsTagged() || IsSmi() || IsHeapObject());
144 return kTaggedSize;
145 }
146
147 Kind kind() const { return static_cast<Kind>(kind_); }
148 bool IsNone() const { return kind_ == kNone; }
149 bool IsTagged() const { return kind_ == kTagged; }
150 bool IsSmi() const { return kind_ == kSmi; }
151 bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
152 bool IsDouble() const { return kind_ == kDouble; }
153 bool IsHeapObject() const { return kind_ == kHeapObject; }
154
155 const char* Mnemonic() const {
156 switch (kind_) {
157 case kNone:
158 return "v";
159 case kTagged:
160 return "t";
161 case kSmi:
162 return "s";
163 case kDouble:
164 return "d";
165 case kHeapObject:
166 return "h";
167 }
168 UNREACHABLE();
169 }
170
171 private:
172 explicit Representation(Kind k) : kind_(k) { }
173
174 // Make sure kind fits in int8.
175 STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
176
177 int8_t kind_;
178};
179
180
181static const int kDescriptorIndexBitCount = 10;
182static const int kFirstInobjectPropertyOffsetBitCount = 7;
183// The maximum number of descriptors we want in a descriptor array. It should
184// fit in a page and also the following should hold:
185// kMaxNumberOfDescriptors + kFieldsAdded <= PropertyArray::kMaxLength.
186static const int kMaxNumberOfDescriptors = (1 << kDescriptorIndexBitCount) - 4;
187static const int kInvalidEnumCacheSentinel =
188 (1 << kDescriptorIndexBitCount) - 1;
189
190enum class PropertyCellType {
191 // Meaningful when a property cell does not contain the hole.
192 kUndefined, // The PREMONOMORPHIC of property cells.
193 kConstant, // Cell has been assigned only once.
194 kConstantType, // Cell has been assigned only one type.
195 kMutable, // Cell will no longer be tracked as constant.
196
197 // Meaningful when a property cell contains the hole.
198 kUninitialized = kUndefined, // Cell has never been initialized.
199 kInvalidated = kConstant, // Cell has been deleted, invalidated or never
200 // existed.
201
202 // For dictionaries not holding cells.
203 kNoCell = kMutable,
204};
205
206enum class PropertyCellConstantType {
207 kSmi,
208 kStableMap,
209};
210
211
212// PropertyDetails captures type and attributes for a property.
213// They are used both in property dictionaries and instance descriptors.
214class PropertyDetails {
215 public:
216 // Property details for dictionary mode properties/elements.
217 PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
218 PropertyCellType cell_type, int dictionary_index = 0) {
219 value_ = KindField::encode(kind) | LocationField::encode(kField) |
220 AttributesField::encode(attributes) |
221 DictionaryStorageField::encode(dictionary_index) |
222 PropertyCellTypeField::encode(cell_type);
223 }
224
225 // Property details for fast mode properties.
226 PropertyDetails(PropertyKind kind, PropertyAttributes attributes,
227 PropertyLocation location, PropertyConstness constness,
228 Representation representation, int field_index = 0) {
229 value_ = KindField::encode(kind) | AttributesField::encode(attributes) |
230 LocationField::encode(location) |
231 ConstnessField::encode(constness) |
232 RepresentationField::encode(EncodeRepresentation(representation)) |
233 FieldIndexField::encode(field_index);
234 }
235
236 static PropertyDetails Empty(
237 PropertyCellType cell_type = PropertyCellType::kNoCell) {
238 return PropertyDetails(kData, NONE, cell_type);
239 }
240
241 int pointer() const { return DescriptorPointer::decode(value_); }
242
243 PropertyDetails set_pointer(int i) const {
244 return PropertyDetails(value_, i);
245 }
246
247 PropertyDetails set_cell_type(PropertyCellType type) const {
248 PropertyDetails details = *this;
249 details.value_ = PropertyCellTypeField::update(details.value_, type);
250 return details;
251 }
252
253 PropertyDetails set_index(int index) const {
254 PropertyDetails details = *this;
255 details.value_ = DictionaryStorageField::update(details.value_, index);
256 return details;
257 }
258
259 PropertyDetails CopyWithRepresentation(Representation representation) const {
260 return PropertyDetails(value_, representation);
261 }
262 PropertyDetails CopyWithConstness(PropertyConstness constness) const {
263 return PropertyDetails(value_, constness);
264 }
265 PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) const {
266 new_attributes =
267 static_cast<PropertyAttributes>(attributes() | new_attributes);
268 return PropertyDetails(value_, new_attributes);
269 }
270
271 // Conversion for storing details as Object.
272 explicit inline PropertyDetails(Smi smi);
273 inline Smi AsSmi() const;
274
275 static uint8_t EncodeRepresentation(Representation representation) {
276 return representation.kind();
277 }
278
279 static Representation DecodeRepresentation(uint32_t bits) {
280 return Representation::FromKind(static_cast<Representation::Kind>(bits));
281 }
282
283 PropertyKind kind() const { return KindField::decode(value_); }
284 PropertyLocation location() const { return LocationField::decode(value_); }
285 PropertyConstness constness() const { return ConstnessField::decode(value_); }
286
287 PropertyAttributes attributes() const {
288 return AttributesField::decode(value_);
289 }
290
291 bool HasKindAndAttributes(PropertyKind kind, PropertyAttributes attributes) {
292 return (value_ & (KindField::kMask | AttributesField::kMask)) ==
293 (KindField::encode(kind) | AttributesField::encode(attributes));
294 }
295
296 int dictionary_index() const {
297 return DictionaryStorageField::decode(value_);
298 }
299
300 Representation representation() const {
301 return DecodeRepresentation(RepresentationField::decode(value_));
302 }
303
304 int field_index() const { return FieldIndexField::decode(value_); }
305
306 inline int field_width_in_words() const;
307
308 static bool IsValidIndex(int index) {
309 return DictionaryStorageField::is_valid(index);
310 }
311
312 bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
313 bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
314 bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
315 bool IsEnumerable() const { return !IsDontEnum(); }
316 PropertyCellType cell_type() const {
317 return PropertyCellTypeField::decode(value_);
318 }
319
320 // Bit fields in value_ (type, shift, size). Must be public so the
321 // constants can be embedded in generated code.
322 class KindField : public BitField<PropertyKind, 0, 1> {};
323 class LocationField : public BitField<PropertyLocation, KindField::kNext, 1> {
324 };
325 class ConstnessField
326 : public BitField<PropertyConstness, LocationField::kNext, 1> {};
327 class AttributesField
328 : public BitField<PropertyAttributes, ConstnessField::kNext, 3> {};
329 static const int kAttributesReadOnlyMask =
330 (READ_ONLY << AttributesField::kShift);
331 static const int kAttributesDontDeleteMask =
332 (DONT_DELETE << AttributesField::kShift);
333 static const int kAttributesDontEnumMask =
334 (DONT_ENUM << AttributesField::kShift);
335
336 // Bit fields for normalized objects.
337 class PropertyCellTypeField
338 : public BitField<PropertyCellType, AttributesField::kNext, 2> {};
339 class DictionaryStorageField
340 : public BitField<uint32_t, PropertyCellTypeField::kNext, 23> {};
341
342 // Bit fields for fast objects.
343 class RepresentationField
344 : public BitField<uint32_t, AttributesField::kNext, 3> {};
345 class DescriptorPointer
346 : public BitField<uint32_t, RepresentationField::kNext,
347 kDescriptorIndexBitCount> {}; // NOLINT
348 class FieldIndexField : public BitField<uint32_t, DescriptorPointer::kNext,
349 kDescriptorIndexBitCount> {
350 }; // NOLINT
351
352 // All bits for both fast and slow objects must fit in a smi.
353 STATIC_ASSERT(DictionaryStorageField::kNext <= 31);
354 STATIC_ASSERT(FieldIndexField::kNext <= 31);
355
356 static const int kInitialIndex = 1;
357
358#ifdef OBJECT_PRINT
359 // For our gdb macros, we should perhaps change these in the future.
360 void Print(bool dictionary_mode);
361#endif
362
363 enum PrintMode {
364 kPrintAttributes = 1 << 0,
365 kPrintFieldIndex = 1 << 1,
366 kPrintRepresentation = 1 << 2,
367 kPrintPointer = 1 << 3,
368
369 kForProperties = kPrintFieldIndex,
370 kForTransitions = kPrintAttributes,
371 kPrintFull = -1,
372 };
373 void PrintAsSlowTo(std::ostream& out);
374 void PrintAsFastTo(std::ostream& out, PrintMode mode = kPrintFull);
375
376 private:
377 PropertyDetails(int value, int pointer) {
378 value_ = DescriptorPointer::update(value, pointer);
379 }
380 PropertyDetails(int value, Representation representation) {
381 value_ = RepresentationField::update(
382 value, EncodeRepresentation(representation));
383 }
384 PropertyDetails(int value, PropertyConstness constness) {
385 value_ = ConstnessField::update(value, constness);
386 }
387 PropertyDetails(int value, PropertyAttributes attributes) {
388 value_ = AttributesField::update(value, attributes);
389 }
390
391 uint32_t value_;
392};
393
394// kField location is more general than kDescriptor, kDescriptor generalizes
395// only to itself.
396inline bool IsGeneralizableTo(PropertyLocation a, PropertyLocation b) {
397 return b == kField || a == kDescriptor;
398}
399
400// PropertyConstness::kMutable constness is more general than
401// VariableMode::kConst, VariableMode::kConst generalizes only to itself.
402inline bool IsGeneralizableTo(PropertyConstness a, PropertyConstness b) {
403 return b == PropertyConstness::kMutable || a == PropertyConstness::kConst;
404}
405
406inline PropertyConstness GeneralizeConstness(PropertyConstness a,
407 PropertyConstness b) {
408 return a == PropertyConstness::kMutable ? PropertyConstness::kMutable : b;
409}
410
411V8_EXPORT_PRIVATE std::ostream& operator<<(
412 std::ostream& os, const PropertyAttributes& attributes);
413} // namespace internal
414} // namespace v8
415
416#endif // V8_PROPERTY_DETAILS_H_
417