Upstream version 11.40.271.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 };
55
56
57 class Representation {
58  public:
59   enum Kind {
60     kNone,
61     kInteger8,
62     kUInteger8,
63     kInteger16,
64     kUInteger16,
65     kSmi,
66     kInteger32,
67     kDouble,
68     kFloat32x4,
69     kFloat64x2,
70     kInt32x4,
71     kHeapObject,
72     kTagged,
73     kExternal,
74     kNumRepresentations
75   };
76
77   Representation() : kind_(kNone) { }
78
79   static Representation None() { return Representation(kNone); }
80   static Representation Tagged() { return Representation(kTagged); }
81   static Representation Integer8() { return Representation(kInteger8); }
82   static Representation UInteger8() { return Representation(kUInteger8); }
83   static Representation Integer16() { return Representation(kInteger16); }
84   static Representation UInteger16() { return Representation(kUInteger16); }
85   static Representation Smi() { return Representation(kSmi); }
86   static Representation Integer32() { return Representation(kInteger32); }
87   static Representation Double() { return Representation(kDouble); }
88   static Representation Float32x4() { return Representation(kFloat32x4); }
89   static Representation Float64x2() { return Representation(kFloat64x2); }
90   static Representation Int32x4() { return Representation(kInt32x4); }
91   static Representation HeapObject() { return Representation(kHeapObject); }
92   static Representation External() { return Representation(kExternal); }
93
94   static Representation FromKind(Kind kind) { return Representation(kind); }
95
96   static Representation FromType(Type* type);
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     if (IsSIMD128() && other.IsSIMD128()) return false;
122     return kind_ > other.kind_;
123   }
124
125   bool fits_into(const Representation& other) const {
126     return other.is_more_general_than(*this) || other.Equals(*this);
127   }
128
129   Representation generalize(Representation other) {
130     if (other.fits_into(*this)) return *this;
131     if (other.is_more_general_than(*this)) return other;
132     return Representation::Tagged();
133   }
134
135   int size() const {
136     DCHECK(!IsNone());
137     if (IsInteger8() || IsUInteger8()) {
138       return sizeof(uint8_t);
139     }
140     if (IsInteger16() || IsUInteger16()) {
141       return sizeof(uint16_t);
142     }
143     if (IsInteger32()) {
144       return sizeof(uint32_t);
145     }
146     return kPointerSize;
147   }
148
149   Kind kind() const { return static_cast<Kind>(kind_); }
150   bool IsNone() const { return kind_ == kNone; }
151   bool IsInteger8() const { return kind_ == kInteger8; }
152   bool IsUInteger8() const { return kind_ == kUInteger8; }
153   bool IsInteger16() const { return kind_ == kInteger16; }
154   bool IsUInteger16() const { return kind_ == kUInteger16; }
155   bool IsTagged() const { return kind_ == kTagged; }
156   bool IsSmi() const { return kind_ == kSmi; }
157   bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
158   bool IsInteger32() const { return kind_ == kInteger32; }
159   bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
160   bool IsDouble() const { return kind_ == kDouble; }
161   bool IsFloat32x4() const { return kind_ == kFloat32x4; }
162   bool IsFloat64x2() const { return kind_ == kFloat64x2; }
163   bool IsInt32x4() const { return kind_ == kInt32x4; }
164   bool IsSIMD128() const {
165     return IsFloat32x4() || IsFloat64x2() || IsInt32x4();
166   }
167   bool IsHeapObject() const { return kind_ == kHeapObject; }
168   bool IsExternal() const { return kind_ == kExternal; }
169   bool IsSpecialization() const {
170     return IsInteger8() || IsUInteger8() ||
171       IsInteger16() || IsUInteger16() ||
172       IsSmi() || IsInteger32() || IsDouble();
173   }
174   const char* Mnemonic() const;
175
176  private:
177   explicit Representation(Kind k) : kind_(k) { }
178
179   // Make sure kind fits in int8.
180   STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
181
182   int8_t kind_;
183 };
184
185
186 static const int kDescriptorIndexBitCount = 10;
187 // The maximum number of descriptors we want in a descriptor array (should
188 // fit in a page).
189 static const int kMaxNumberOfDescriptors =
190     (1 << kDescriptorIndexBitCount) - 2;
191 static const int kInvalidEnumCacheSentinel =
192     (1 << kDescriptorIndexBitCount) - 1;
193
194
195 // PropertyDetails captures type and attributes for a property.
196 // They are used both in property dictionaries and instance descriptors.
197 class PropertyDetails BASE_EMBEDDED {
198  public:
199   PropertyDetails(PropertyAttributes attributes,
200                   PropertyType type,
201                   int index) {
202     value_ = TypeField::encode(type)
203         | AttributesField::encode(attributes)
204         | DictionaryStorageField::encode(index);
205
206     DCHECK(type == this->type());
207     DCHECK(attributes == this->attributes());
208   }
209
210   PropertyDetails(PropertyAttributes attributes,
211                   PropertyType type,
212                   Representation representation,
213                   int field_index = 0) {
214     value_ = TypeField::encode(type)
215         | AttributesField::encode(attributes)
216         | RepresentationField::encode(EncodeRepresentation(representation))
217         | FieldIndexField::encode(field_index);
218   }
219
220   int pointer() const { return DescriptorPointer::decode(value_); }
221
222   PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); }
223
224   PropertyDetails CopyWithRepresentation(Representation representation) const {
225     return PropertyDetails(value_, representation);
226   }
227   PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) {
228     new_attributes =
229         static_cast<PropertyAttributes>(attributes() | new_attributes);
230     return PropertyDetails(value_, new_attributes);
231   }
232
233   // Conversion for storing details as Object*.
234   explicit inline PropertyDetails(Smi* smi);
235   inline Smi* AsSmi() const;
236
237   static uint8_t EncodeRepresentation(Representation representation) {
238     return representation.kind();
239   }
240
241   static Representation DecodeRepresentation(uint32_t bits) {
242     return Representation::FromKind(static_cast<Representation::Kind>(bits));
243   }
244
245   PropertyType type() const { return TypeField::decode(value_); }
246
247   PropertyAttributes attributes() const {
248     return AttributesField::decode(value_);
249   }
250
251   int dictionary_index() const {
252     return DictionaryStorageField::decode(value_);
253   }
254
255   Representation representation() const {
256     DCHECK(type() != NORMAL);
257     return DecodeRepresentation(RepresentationField::decode(value_));
258   }
259
260   int field_index() const {
261     return FieldIndexField::decode(value_);
262   }
263
264   inline PropertyDetails AsDeleted() const;
265
266   static bool IsValidIndex(int index) {
267     return DictionaryStorageField::is_valid(index);
268   }
269
270   bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
271   bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
272   bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
273   bool IsDeleted() const { return DeletedField::decode(value_) != 0;}
274
275   // Bit fields in value_ (type, shift, size). Must be public so the
276   // constants can be embedded in generated code.
277   class TypeField : public BitField<PropertyType, 0, 2> {};
278   class AttributesField : public BitField<PropertyAttributes, 2, 3> {};
279
280   // Bit fields for normalized objects.
281   class DeletedField : public BitField<uint32_t, 5, 1> {};
282   class DictionaryStorageField : public BitField<uint32_t, 6, 24> {};
283
284   // Bit fields for fast objects.
285   class RepresentationField : public BitField<uint32_t, 5, 4> {};
286   class DescriptorPointer
287       : public BitField<uint32_t, 9, kDescriptorIndexBitCount> {};  // NOLINT
288   class FieldIndexField
289       : public BitField<uint32_t, 9 + kDescriptorIndexBitCount,
290                         kDescriptorIndexBitCount> {};  // NOLINT
291   // All bits for fast objects must fix in a smi.
292   STATIC_ASSERT(9 + kDescriptorIndexBitCount + kDescriptorIndexBitCount <= 31);
293
294   static const int kInitialIndex = 1;
295
296  private:
297   PropertyDetails(int value, int pointer) {
298     value_ = DescriptorPointer::update(value, pointer);
299   }
300   PropertyDetails(int value, Representation representation) {
301     value_ = RepresentationField::update(
302         value, EncodeRepresentation(representation));
303   }
304   PropertyDetails(int value, PropertyAttributes attributes) {
305     value_ = AttributesField::update(value, attributes);
306   }
307
308   uint32_t value_;
309 };
310
311
312 std::ostream& operator<<(std::ostream& os,
313                          const PropertyAttributes& attributes);
314 std::ostream& operator<<(std::ostream& os, const PropertyDetails& details);
315 } }  // namespace v8::internal
316
317 #endif  // V8_PROPERTY_DETAILS_H_