Upstream version 9.38.207.0
[platform/framework/web/crosswalk.git] / src / v8 / 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 properties is significant.
45 // Must fit in the BitField PropertyDetails::TypeField.
46 // A copy of this is in mirror-debugger.js.
47 enum PropertyType {
48   // Only in slow mode.
49   NORMAL                    = 0,
50   // Only in fast mode.
51   FIELD                     = 1,
52   CONSTANT                  = 2,
53   CALLBACKS                 = 3,
54   // Only in lookup results, not in descriptors.
55   HANDLER                   = 4,
56   INTERCEPTOR               = 5,
57   // Only used as a marker in LookupResult.
58   NONEXISTENT               = 6
59 };
60
61
62 class Representation {
63  public:
64   enum Kind {
65     kNone,
66     kInteger8,
67     kUInteger8,
68     kInteger16,
69     kUInteger16,
70     kSmi,
71     kInteger32,
72     kDouble,
73     kFloat32x4,
74     kFloat64x2,
75     kInt32x4,
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 Float32x4() { return Representation(kFloat32x4); }
94   static Representation Float64x2() { return Representation(kFloat64x2); }
95   static Representation Int32x4() { return Representation(kInt32x4); }
96   static Representation HeapObject() { return Representation(kHeapObject); }
97   static Representation External() { return Representation(kExternal); }
98
99   static Representation FromKind(Kind kind) { return Representation(kind); }
100
101   static Representation FromType(Type* type);
102
103   bool Equals(const Representation& other) const {
104     return kind_ == other.kind_;
105   }
106
107   bool IsCompatibleForLoad(const Representation& other) const {
108     return (IsDouble() && other.IsDouble()) ||
109         (!IsDouble() && !other.IsDouble());
110   }
111
112   bool IsCompatibleForStore(const Representation& other) const {
113     return Equals(other);
114   }
115
116   bool is_more_general_than(const Representation& other) const {
117     if (kind_ == kExternal && other.kind_ == kNone) return true;
118     if (kind_ == kExternal && other.kind_ == kExternal) return false;
119     if (kind_ == kNone && other.kind_ == kExternal) return false;
120
121     DCHECK(kind_ != kExternal);
122     DCHECK(other.kind_ != kExternal);
123     if (IsHeapObject()) return other.IsNone();
124     if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
125     if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
126     if (IsSIMD128() && other.IsSIMD128()) return false;
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   bool CanContainDouble(double value);
135
136   Representation generalize(Representation other) {
137     if (other.fits_into(*this)) return *this;
138     if (other.is_more_general_than(*this)) return other;
139     return Representation::Tagged();
140   }
141
142   int size() const {
143     DCHECK(!IsNone());
144     if (IsInteger8() || IsUInteger8()) {
145       return sizeof(uint8_t);
146     }
147     if (IsInteger16() || IsUInteger16()) {
148       return sizeof(uint16_t);
149     }
150     if (IsInteger32()) {
151       return sizeof(uint32_t);
152     }
153     return kPointerSize;
154   }
155
156   Kind kind() const { return static_cast<Kind>(kind_); }
157   bool IsNone() const { return kind_ == kNone; }
158   bool IsInteger8() const { return kind_ == kInteger8; }
159   bool IsUInteger8() const { return kind_ == kUInteger8; }
160   bool IsInteger16() const { return kind_ == kInteger16; }
161   bool IsUInteger16() const { return kind_ == kUInteger16; }
162   bool IsTagged() const { return kind_ == kTagged; }
163   bool IsSmi() const { return kind_ == kSmi; }
164   bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
165   bool IsInteger32() const { return kind_ == kInteger32; }
166   bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
167   bool IsDouble() const { return kind_ == kDouble; }
168   bool IsFloat32x4() const { return kind_ == kFloat32x4; }
169   bool IsFloat64x2() const { return kind_ == kFloat64x2; }
170   bool IsInt32x4() const { return kind_ == kInt32x4; }
171   bool IsSIMD128() const {
172     return IsFloat32x4() || IsFloat64x2() || IsInt32x4();
173   }
174   bool IsHeapObject() const { return kind_ == kHeapObject; }
175   bool IsExternal() const { return kind_ == kExternal; }
176   bool IsSpecialization() const {
177     return IsInteger8() || IsUInteger8() ||
178       IsInteger16() || IsUInteger16() ||
179       IsSmi() || IsInteger32() || IsDouble();
180   }
181   const char* Mnemonic() const;
182
183  private:
184   explicit Representation(Kind k) : kind_(k) { }
185
186   // Make sure kind fits in int8.
187   STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
188
189   int8_t kind_;
190 };
191
192
193 static const int kDescriptorIndexBitCount = 10;
194 // The maximum number of descriptors we want in a descriptor array (should
195 // fit in a page).
196 static const int kMaxNumberOfDescriptors =
197     (1 << kDescriptorIndexBitCount) - 2;
198 static const int kInvalidEnumCacheSentinel =
199     (1 << kDescriptorIndexBitCount) - 1;
200
201
202 // PropertyDetails captures type and attributes for a property.
203 // They are used both in property dictionaries and instance descriptors.
204 class PropertyDetails BASE_EMBEDDED {
205  public:
206   PropertyDetails(PropertyAttributes attributes,
207                   PropertyType type,
208                   int index) {
209     value_ = TypeField::encode(type)
210         | AttributesField::encode(attributes)
211         | DictionaryStorageField::encode(index);
212
213     DCHECK(type == this->type());
214     DCHECK(attributes == this->attributes());
215   }
216
217   PropertyDetails(PropertyAttributes attributes,
218                   PropertyType type,
219                   Representation representation,
220                   int field_index = 0) {
221     value_ = TypeField::encode(type)
222         | AttributesField::encode(attributes)
223         | RepresentationField::encode(EncodeRepresentation(representation))
224         | FieldIndexField::encode(field_index);
225   }
226
227   int pointer() const { return DescriptorPointer::decode(value_); }
228
229   PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); }
230
231   PropertyDetails CopyWithRepresentation(Representation representation) const {
232     return PropertyDetails(value_, representation);
233   }
234   PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) {
235     new_attributes =
236         static_cast<PropertyAttributes>(attributes() | new_attributes);
237     return PropertyDetails(value_, new_attributes);
238   }
239
240   // Conversion for storing details as Object*.
241   explicit inline PropertyDetails(Smi* smi);
242   inline Smi* AsSmi() const;
243
244   static uint8_t EncodeRepresentation(Representation representation) {
245     return representation.kind();
246   }
247
248   static Representation DecodeRepresentation(uint32_t bits) {
249     return Representation::FromKind(static_cast<Representation::Kind>(bits));
250   }
251
252   PropertyType type() const { return TypeField::decode(value_); }
253
254   PropertyAttributes attributes() const {
255     return AttributesField::decode(value_);
256   }
257
258   int dictionary_index() const {
259     return DictionaryStorageField::decode(value_);
260   }
261
262   Representation representation() const {
263     DCHECK(type() != NORMAL);
264     return DecodeRepresentation(RepresentationField::decode(value_));
265   }
266
267   int field_index() const {
268     return FieldIndexField::decode(value_);
269   }
270
271   inline PropertyDetails AsDeleted() const;
272
273   static bool IsValidIndex(int index) {
274     return DictionaryStorageField::is_valid(index);
275   }
276
277   bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
278   bool IsDontDelete() const { return (attributes() & DONT_DELETE) != 0; }
279   bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
280   bool IsDeleted() const { return DeletedField::decode(value_) != 0;}
281
282   // Bit fields in value_ (type, shift, size). Must be public so the
283   // constants can be embedded in generated code.
284   class TypeField:                public BitField<PropertyType,       0,  3> {};
285   class AttributesField:          public BitField<PropertyAttributes, 3,  3> {};
286
287   // Bit fields for normalized objects.
288   class DeletedField:             public BitField<uint32_t,           6,  1> {};
289   class DictionaryStorageField:   public BitField<uint32_t,           7, 24> {};
290
291   // Bit fields for fast objects.
292   class RepresentationField:      public BitField<uint32_t,           6,  4> {};
293   class DescriptorPointer:        public BitField<uint32_t, 10,
294       kDescriptorIndexBitCount> {};  // NOLINT
295   class FieldIndexField:          public BitField<uint32_t,
296       10 + kDescriptorIndexBitCount,
297       kDescriptorIndexBitCount> {};  // NOLINT
298   // All bits for fast objects must fix in a smi.
299   STATIC_ASSERT(10 + kDescriptorIndexBitCount + kDescriptorIndexBitCount <= 31);
300
301   static const int kInitialIndex = 1;
302
303  private:
304   PropertyDetails(int value, int pointer) {
305     value_ = DescriptorPointer::update(value, pointer);
306   }
307   PropertyDetails(int value, Representation representation) {
308     value_ = RepresentationField::update(
309         value, EncodeRepresentation(representation));
310   }
311   PropertyDetails(int value, PropertyAttributes attributes) {
312     value_ = AttributesField::update(value, attributes);
313   }
314
315   uint32_t value_;
316 };
317
318 } }  // namespace v8::internal
319
320 #endif  // V8_PROPERTY_DETAILS_H_