33d3b8d7efd6ef20b235efb070202cfaeb6cce2b
[platform/upstream/v8.git] / src / property-details.h
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 #include "src/utils.h"
11
12 // Ecma-262 3rd 8.6.1
13 enum PropertyAttributes {
14   NONE              = v8::None,
15   READ_ONLY         = v8::ReadOnly,
16   DONT_ENUM         = v8::DontEnum,
17   DONT_DELETE       = v8::DontDelete,
18
19   SEALED            = DONT_DELETE,
20   FROZEN            = SEALED | READ_ONLY,
21
22   STRING            = 8,  // Used to filter symbols and string names
23   SYMBOLIC          = 16,
24   PRIVATE_SYMBOL    = 32,
25
26   DONT_SHOW         = DONT_ENUM | SYMBOLIC | PRIVATE_SYMBOL,
27   ABSENT            = 64  // Used in runtime to indicate a property is absent.
28   // 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
34 namespace v8 {
35 namespace internal {
36
37 class Smi;
38 template<class> class TypeImpl;
39 struct ZoneTypeConfig;
40 typedef TypeImpl<ZoneTypeConfig> Type;
41 class TypeInfo;
42
43 // Type of properties.
44 // Order of kinds is significant.
45 // Must fit in the BitField PropertyDetails::KindField.
46 enum PropertyKind { kData = 0, kAccessor = 1 };
47
48
49 // Order of modes is significant.
50 // Must fit in the BitField PropertyDetails::StoreModeField.
51 enum PropertyLocation { kField = 0, kDescriptor = 1 };
52
53
54 // Order of properties is significant.
55 // Must fit in the BitField PropertyDetails::TypeField.
56 // A copy of this is in debug/mirrors.js.
57 enum PropertyType {
58   DATA = (kField << 1) | kData,
59   DATA_CONSTANT = (kDescriptor << 1) | kData,
60   ACCESSOR = (kField << 1) | kAccessor,
61   ACCESSOR_CONSTANT = (kDescriptor << 1) | kAccessor
62 };
63
64
65 class Representation {
66  public:
67   enum Kind {
68     kNone,
69     kInteger8,
70     kUInteger8,
71     kInteger16,
72     kUInteger16,
73     kSmi,
74     kInteger32,
75     kDouble,
76     kHeapObject,
77     kTagged,
78     kExternal,
79     kNumRepresentations
80   };
81
82   Representation() : kind_(kNone) { }
83
84   static Representation None() { return Representation(kNone); }
85   static Representation Tagged() { return Representation(kTagged); }
86   static Representation Integer8() { return Representation(kInteger8); }
87   static Representation UInteger8() { return Representation(kUInteger8); }
88   static Representation Integer16() { return Representation(kInteger16); }
89   static Representation UInteger16() { return Representation(kUInteger16); }
90   static Representation Smi() { return Representation(kSmi); }
91   static Representation Integer32() { return Representation(kInteger32); }
92   static Representation Double() { return Representation(kDouble); }
93   static Representation HeapObject() { return Representation(kHeapObject); }
94   static Representation External() { return Representation(kExternal); }
95
96   static Representation FromKind(Kind kind) { return Representation(kind); }
97
98   bool Equals(const Representation& other) const {
99     return kind_ == other.kind_;
100   }
101
102   bool IsCompatibleForLoad(const Representation& other) const {
103     return (IsDouble() && other.IsDouble()) ||
104         (!IsDouble() && !other.IsDouble());
105   }
106
107   bool IsCompatibleForStore(const Representation& other) const {
108     return Equals(other);
109   }
110
111   bool is_more_general_than(const Representation& other) const {
112     if (kind_ == kExternal && other.kind_ == kNone) return true;
113     if (kind_ == kExternal && other.kind_ == kExternal) return false;
114     if (kind_ == kNone && other.kind_ == kExternal) return false;
115
116     DCHECK(kind_ != kExternal);
117     DCHECK(other.kind_ != kExternal);
118     if (IsHeapObject()) return other.IsNone();
119     if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
120     if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
121     return kind_ > other.kind_;
122   }
123
124   bool fits_into(const Representation& other) const {
125     return other.is_more_general_than(*this) || other.Equals(*this);
126   }
127
128   Representation generalize(Representation other) {
129     if (other.fits_into(*this)) return *this;
130     if (other.is_more_general_than(*this)) return other;
131     return Representation::Tagged();
132   }
133
134   int size() const {
135     DCHECK(!IsNone());
136     if (IsInteger8() || IsUInteger8()) {
137       return sizeof(uint8_t);
138     }
139     if (IsInteger16() || IsUInteger16()) {
140       return sizeof(uint16_t);
141     }
142     if (IsInteger32()) {
143       return sizeof(uint32_t);
144     }
145     return kPointerSize;
146   }
147
148   Kind kind() const { return static_cast<Kind>(kind_); }
149   bool IsNone() const { return kind_ == kNone; }
150   bool IsInteger8() const { return kind_ == kInteger8; }
151   bool IsUInteger8() const { return kind_ == kUInteger8; }
152   bool IsInteger16() const { return kind_ == kInteger16; }
153   bool IsUInteger16() const { return kind_ == kUInteger16; }
154   bool IsTagged() const { return kind_ == kTagged; }
155   bool IsSmi() const { return kind_ == kSmi; }
156   bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
157   bool IsInteger32() const { return kind_ == kInteger32; }
158   bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
159   bool IsDouble() const { return kind_ == kDouble; }
160   bool IsHeapObject() const { return kind_ == kHeapObject; }
161   bool IsExternal() const { return kind_ == kExternal; }
162   bool IsSpecialization() const {
163     return IsInteger8() || IsUInteger8() ||
164       IsInteger16() || IsUInteger16() ||
165       IsSmi() || IsInteger32() || IsDouble();
166   }
167   const char* Mnemonic() const;
168
169  private:
170   explicit Representation(Kind k) : kind_(k) { }
171
172   // Make sure kind fits in int8.
173   STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
174
175   int8_t kind_;
176 };
177
178
179 static const int kDescriptorIndexBitCount = 10;
180 // The maximum number of descriptors we want in a descriptor array (should
181 // fit in a page).
182 static const int kMaxNumberOfDescriptors =
183     (1 << kDescriptorIndexBitCount) - 2;
184 static const int kInvalidEnumCacheSentinel =
185     (1 << kDescriptorIndexBitCount) - 1;
186
187
188 enum class PropertyCellType {
189   // Meaningful when a property cell does not contain the hole.
190   kUndefined,     // The PREMONOMORPHIC of property cells.
191   kConstant,      // Cell has been assigned only once.
192   kConstantType,  // Cell has been assigned only one type.
193   kMutable,       // Cell will no longer be tracked as constant.
194
195   // Meaningful when a property cell contains the hole.
196   kUninitialized = kUndefined,  // Cell has never been initialized.
197   kInvalidated = kConstant,     // Cell has been deleted or invalidated.
198
199   // For dictionaries not holding cells.
200   kNoCell = kMutable,
201 };
202
203
204 enum class PropertyCellConstantType {
205   kSmi,
206   kStableMap,
207 };
208
209
210 // PropertyDetails captures type and attributes for a property.
211 // They are used both in property dictionaries and instance descriptors.
212 class PropertyDetails BASE_EMBEDDED {
213  public:
214   PropertyDetails(PropertyAttributes attributes, PropertyType type, int index,
215                   PropertyCellType cell_type) {
216     value_ = TypeField::encode(type) | AttributesField::encode(attributes) |
217              DictionaryStorageField::encode(index) |
218              PropertyCellTypeField::encode(cell_type);
219
220     DCHECK(type == this->type());
221     DCHECK(attributes == this->attributes());
222   }
223
224   PropertyDetails(PropertyAttributes attributes,
225                   PropertyType type,
226                   Representation representation,
227                   int field_index = 0) {
228     value_ = TypeField::encode(type)
229         | AttributesField::encode(attributes)
230         | RepresentationField::encode(EncodeRepresentation(representation))
231         | FieldIndexField::encode(field_index);
232   }
233
234   PropertyDetails(PropertyAttributes attributes, PropertyKind kind,
235                   PropertyLocation location, Representation representation,
236                   int field_index = 0) {
237     value_ = KindField::encode(kind) | LocationField::encode(location) |
238              AttributesField::encode(attributes) |
239              RepresentationField::encode(EncodeRepresentation(representation)) |
240              FieldIndexField::encode(field_index);
241   }
242
243   static PropertyDetails Empty() {
244     return PropertyDetails(NONE, DATA, 0, PropertyCellType::kNoCell);
245   }
246
247   int pointer() const { return DescriptorPointer::decode(value_); }
248
249   PropertyDetails set_pointer(int i) const {
250     return PropertyDetails(value_, i);
251   }
252
253   PropertyDetails set_cell_type(PropertyCellType type) const {
254     PropertyDetails details = *this;
255     details.value_ = PropertyCellTypeField::update(details.value_, type);
256     return details;
257   }
258
259   PropertyDetails set_index(int index) const {
260     PropertyDetails details = *this;
261     details.value_ = DictionaryStorageField::update(details.value_, index);
262     return details;
263   }
264
265   PropertyDetails CopyWithRepresentation(Representation representation) const {
266     return PropertyDetails(value_, representation);
267   }
268   PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) const {
269     new_attributes =
270         static_cast<PropertyAttributes>(attributes() | new_attributes);
271     return PropertyDetails(value_, new_attributes);
272   }
273
274   // Conversion for storing details as Object*.
275   explicit inline PropertyDetails(Smi* smi);
276   inline Smi* AsSmi() const;
277
278   static uint8_t EncodeRepresentation(Representation representation) {
279     return representation.kind();
280   }
281
282   static Representation DecodeRepresentation(uint32_t bits) {
283     return Representation::FromKind(static_cast<Representation::Kind>(bits));
284   }
285
286   PropertyKind kind() const { return KindField::decode(value_); }
287   PropertyLocation location() const { return LocationField::decode(value_); }
288
289   PropertyType type() const { return TypeField::decode(value_); }
290
291   PropertyAttributes attributes() const {
292     return AttributesField::decode(value_);
293   }
294
295   int dictionary_index() const {
296     return DictionaryStorageField::decode(value_);
297   }
298
299   Representation representation() const {
300     return DecodeRepresentation(RepresentationField::decode(value_));
301   }
302
303   int field_index() const { return FieldIndexField::decode(value_); }
304
305   inline int field_width_in_words() const;
306
307   static bool IsValidIndex(int index) {
308     return DictionaryStorageField::is_valid(index);
309   }
310
311   bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
312   bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
313   bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
314   PropertyCellType cell_type() const {
315     return PropertyCellTypeField::decode(value_);
316   }
317
318   // Bit fields in value_ (type, shift, size). Must be public so the
319   // constants can be embedded in generated code.
320   class KindField : public BitField<PropertyKind, 0, 1> {};
321   class LocationField : public BitField<PropertyLocation, 1, 1> {};
322   class AttributesField : public BitField<PropertyAttributes, 2, 3> {};
323   static const int kAttributesReadOnlyMask =
324       (READ_ONLY << AttributesField::kShift);
325
326   // Bit fields for normalized objects.
327   class PropertyCellTypeField : public BitField<PropertyCellType, 5, 2> {};
328   class DictionaryStorageField : public BitField<uint32_t, 7, 24> {};
329
330   // Bit fields for fast objects.
331   class RepresentationField : public BitField<uint32_t, 5, 4> {};
332   class DescriptorPointer
333       : public BitField<uint32_t, 9, kDescriptorIndexBitCount> {};  // NOLINT
334   class FieldIndexField
335       : public BitField<uint32_t, 9 + kDescriptorIndexBitCount,
336                         kDescriptorIndexBitCount> {};  // NOLINT
337
338   // NOTE: TypeField overlaps with KindField and LocationField.
339   class TypeField : public BitField<PropertyType, 0, 2> {};
340   STATIC_ASSERT(KindField::kNext == LocationField::kShift);
341   STATIC_ASSERT(TypeField::kShift == KindField::kShift);
342   STATIC_ASSERT(TypeField::kNext == LocationField::kNext);
343
344   // All bits for both fast and slow objects must fit in a smi.
345   STATIC_ASSERT(DictionaryStorageField::kNext <= 31);
346   STATIC_ASSERT(FieldIndexField::kNext <= 31);
347
348   static const int kInitialIndex = 1;
349
350 #ifdef OBJECT_PRINT
351   // For our gdb macros, we should perhaps change these in the future.
352   void Print(bool dictionary_mode);
353 #endif
354
355  private:
356   PropertyDetails(int value, int pointer) {
357     value_ = DescriptorPointer::update(value, pointer);
358   }
359   PropertyDetails(int value, Representation representation) {
360     value_ = RepresentationField::update(
361         value, EncodeRepresentation(representation));
362   }
363   PropertyDetails(int value, PropertyAttributes attributes) {
364     value_ = AttributesField::update(value, attributes);
365   }
366
367   uint32_t value_;
368 };
369
370
371 std::ostream& operator<<(std::ostream& os,
372                          const PropertyAttributes& attributes);
373 std::ostream& operator<<(std::ostream& os, const PropertyDetails& details);
374 } }  // namespace v8::internal
375
376 #endif  // V8_PROPERTY_DETAILS_H_