Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / v8 / src / property-details.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef V8_PROPERTY_DETAILS_H_
29 #define V8_PROPERTY_DETAILS_H_
30
31 #include "../include/v8.h"
32 #include "allocation.h"
33 #include "utils.h"
34
35 // Ecma-262 3rd 8.6.1
36 enum PropertyAttributes {
37   NONE              = v8::None,
38   READ_ONLY         = v8::ReadOnly,
39   DONT_ENUM         = v8::DontEnum,
40   DONT_DELETE       = v8::DontDelete,
41
42   SEALED            = DONT_DELETE,
43   FROZEN            = SEALED | READ_ONLY,
44
45   STRING            = 8,  // Used to filter symbols and string names
46   SYMBOLIC          = 16,
47   PRIVATE_SYMBOL    = 32,
48
49   DONT_SHOW         = DONT_ENUM | SYMBOLIC | PRIVATE_SYMBOL,
50   ABSENT            = 64  // Used in runtime to indicate a property is absent.
51   // ABSENT can never be stored in or returned from a descriptor's attributes
52   // bitfield.  It is only used as a return value meaning the attributes of
53   // a non-existent property.
54 };
55
56
57 namespace v8 {
58 namespace internal {
59
60 class Smi;
61 template<class> class TypeImpl;
62 struct ZoneTypeConfig;
63 typedef TypeImpl<ZoneTypeConfig> Type;
64 class TypeInfo;
65
66 // Type of properties.
67 // Order of properties is significant.
68 // Must fit in the BitField PropertyDetails::TypeField.
69 // A copy of this is in mirror-debugger.js.
70 enum PropertyType {
71   // Only in slow mode.
72   NORMAL                    = 0,
73   // Only in fast mode.
74   FIELD                     = 1,
75   CONSTANT                  = 2,
76   CALLBACKS                 = 3,
77   // Only in lookup results, not in descriptors.
78   HANDLER                   = 4,
79   INTERCEPTOR               = 5,
80   TRANSITION                = 6,
81   // Only used as a marker in LookupResult.
82   NONEXISTENT               = 7
83 };
84
85
86 class Representation {
87  public:
88   enum Kind {
89     kNone,
90     kInteger8,
91     kUInteger8,
92     kInteger16,
93     kUInteger16,
94     kSmi,
95     kInteger32,
96     kDouble,
97     kFloat32x4,
98     kInt32x4,
99     kHeapObject,
100     kTagged,
101     kExternal,
102     kNumRepresentations
103   };
104
105   Representation() : kind_(kNone) { }
106
107   static Representation None() { return Representation(kNone); }
108   static Representation Tagged() { return Representation(kTagged); }
109   static Representation Integer8() { return Representation(kInteger8); }
110   static Representation UInteger8() { return Representation(kUInteger8); }
111   static Representation Integer16() { return Representation(kInteger16); }
112   static Representation UInteger16() { return Representation(kUInteger16); }
113   static Representation Smi() { return Representation(kSmi); }
114   static Representation Integer32() { return Representation(kInteger32); }
115   static Representation Double() { return Representation(kDouble); }
116   static Representation Float32x4() { return Representation(kFloat32x4); }
117   static Representation Int32x4() { return Representation(kInt32x4); }
118   static Representation HeapObject() { return Representation(kHeapObject); }
119   static Representation External() { return Representation(kExternal); }
120
121   static Representation FromKind(Kind kind) { return Representation(kind); }
122
123   static Representation FromType(Type* type);
124
125   bool Equals(const Representation& other) const {
126     return kind_ == other.kind_;
127   }
128
129   bool IsCompatibleForLoad(const Representation& other) const {
130     return (IsDouble() && other.IsDouble()) ||
131         (!IsDouble() && !other.IsDouble());
132   }
133
134   bool IsCompatibleForStore(const Representation& other) const {
135     return Equals(other);
136   }
137
138   bool is_more_general_than(const Representation& other) const {
139     if (kind_ == kExternal && other.kind_ == kNone) return true;
140     if (kind_ == kExternal && other.kind_ == kExternal) return false;
141     if (kind_ == kNone && other.kind_ == kExternal) return false;
142
143     ASSERT(kind_ != kExternal);
144     ASSERT(other.kind_ != kExternal);
145     if (IsHeapObject()) return other.IsNone();
146     if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
147     if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
148     if (IsSIMD128() && other.IsSIMD128()) return false;
149     return kind_ > other.kind_;
150   }
151
152   bool fits_into(const Representation& other) const {
153     return other.is_more_general_than(*this) || other.Equals(*this);
154   }
155
156   Representation generalize(Representation other) {
157     if (other.fits_into(*this)) return *this;
158     if (other.is_more_general_than(*this)) return other;
159     return Representation::Tagged();
160   }
161
162   int size() const {
163     ASSERT(!IsNone());
164     if (IsInteger8() || IsUInteger8()) {
165       return sizeof(uint8_t);
166     }
167     if (IsInteger16() || IsUInteger16()) {
168       return sizeof(uint16_t);
169     }
170     if (IsInteger32()) {
171       return sizeof(uint32_t);
172     }
173     return kPointerSize;
174   }
175
176   Kind kind() const { return static_cast<Kind>(kind_); }
177   bool IsNone() const { return kind_ == kNone; }
178   bool IsInteger8() const { return kind_ == kInteger8; }
179   bool IsUInteger8() const { return kind_ == kUInteger8; }
180   bool IsInteger16() const { return kind_ == kInteger16; }
181   bool IsUInteger16() const { return kind_ == kUInteger16; }
182   bool IsTagged() const { return kind_ == kTagged; }
183   bool IsSmi() const { return kind_ == kSmi; }
184   bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
185   bool IsInteger32() const { return kind_ == kInteger32; }
186   bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
187   bool IsDouble() const { return kind_ == kDouble; }
188   bool IsFloat32x4() const { return kind_ == kFloat32x4; }
189   bool IsInt32x4() const { return kind_ == kInt32x4; }
190   bool IsSIMD128() const { return IsFloat32x4() || IsInt32x4(); }
191   bool IsHeapObject() const { return kind_ == kHeapObject; }
192   bool IsExternal() const { return kind_ == kExternal; }
193   bool IsSpecialization() const {
194     return IsInteger8() || IsUInteger8() ||
195       IsInteger16() || IsUInteger16() ||
196       IsSmi() || IsInteger32() || IsDouble();
197   }
198   const char* Mnemonic() const;
199
200  private:
201   explicit Representation(Kind k) : kind_(k) { }
202
203   // Make sure kind fits in int8.
204   STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
205
206   int8_t kind_;
207 };
208
209
210 static const int kDescriptorIndexBitCount = 10;
211 // The maximum number of descriptors we want in a descriptor array (should
212 // fit in a page).
213 static const int kMaxNumberOfDescriptors =
214     (1 << kDescriptorIndexBitCount) - 2;
215 static const int kInvalidEnumCacheSentinel =
216     (1 << kDescriptorIndexBitCount) - 1;
217
218
219 // PropertyDetails captures type and attributes for a property.
220 // They are used both in property dictionaries and instance descriptors.
221 class PropertyDetails BASE_EMBEDDED {
222  public:
223   PropertyDetails(PropertyAttributes attributes,
224                   PropertyType type,
225                   int index) {
226     value_ = TypeField::encode(type)
227         | AttributesField::encode(attributes)
228         | DictionaryStorageField::encode(index);
229
230     ASSERT(type == this->type());
231     ASSERT(attributes == this->attributes());
232   }
233
234   PropertyDetails(PropertyAttributes attributes,
235                   PropertyType type,
236                   Representation representation,
237                   int field_index = 0) {
238     value_ = TypeField::encode(type)
239         | AttributesField::encode(attributes)
240         | RepresentationField::encode(EncodeRepresentation(representation))
241         | FieldIndexField::encode(field_index);
242   }
243
244   int pointer() { return DescriptorPointer::decode(value_); }
245
246   PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); }
247
248   PropertyDetails CopyWithRepresentation(Representation representation) {
249     return PropertyDetails(value_, representation);
250   }
251   PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) {
252     new_attributes =
253         static_cast<PropertyAttributes>(attributes() | new_attributes);
254     return PropertyDetails(value_, new_attributes);
255   }
256
257   // Conversion for storing details as Object*.
258   explicit inline PropertyDetails(Smi* smi);
259   inline Smi* AsSmi();
260
261   static uint8_t EncodeRepresentation(Representation representation) {
262     return representation.kind();
263   }
264
265   static Representation DecodeRepresentation(uint32_t bits) {
266     return Representation::FromKind(static_cast<Representation::Kind>(bits));
267   }
268
269   PropertyType type() { return TypeField::decode(value_); }
270
271   PropertyAttributes attributes() const {
272     return AttributesField::decode(value_);
273   }
274
275   int dictionary_index() {
276     return DictionaryStorageField::decode(value_);
277   }
278
279   Representation representation() {
280     ASSERT(type() != NORMAL);
281     return DecodeRepresentation(RepresentationField::decode(value_));
282   }
283
284   int  field_index() {
285     return FieldIndexField::decode(value_);
286   }
287
288   inline PropertyDetails AsDeleted();
289
290   static bool IsValidIndex(int index) {
291     return DictionaryStorageField::is_valid(index);
292   }
293
294   bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
295   bool IsDontDelete() const { return (attributes() & DONT_DELETE) != 0; }
296   bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
297   bool IsDeleted() const { return DeletedField::decode(value_) != 0;}
298
299   // Bit fields in value_ (type, shift, size). Must be public so the
300   // constants can be embedded in generated code.
301   class TypeField:                public BitField<PropertyType,       0,  3> {};
302   class AttributesField:          public BitField<PropertyAttributes, 3,  3> {};
303
304   // Bit fields for normalized objects.
305   class DeletedField:             public BitField<uint32_t,           6,  1> {};
306   class DictionaryStorageField:   public BitField<uint32_t,           7, 24> {};
307
308   // Bit fields for fast objects.
309   class RepresentationField:      public BitField<uint32_t,           6,  4> {};
310   class DescriptorPointer:        public BitField<uint32_t, 10,
311       kDescriptorIndexBitCount> {};  // NOLINT
312   class FieldIndexField:          public BitField<uint32_t,
313       10 + kDescriptorIndexBitCount,
314       kDescriptorIndexBitCount> {};  // NOLINT
315   // All bits for fast objects must fix in a smi.
316   STATIC_ASSERT(10 + kDescriptorIndexBitCount + kDescriptorIndexBitCount <= 31);
317
318   static const int kInitialIndex = 1;
319
320  private:
321   PropertyDetails(int value, int pointer) {
322     value_ = DescriptorPointer::update(value, pointer);
323   }
324   PropertyDetails(int value, Representation representation) {
325     value_ = RepresentationField::update(
326         value, EncodeRepresentation(representation));
327   }
328   PropertyDetails(int value, PropertyAttributes attributes) {
329     value_ = AttributesField::update(value, attributes);
330   }
331
332   uint32_t value_;
333 };
334
335 } }  // namespace v8::internal
336
337 #endif  // V8_PROPERTY_DETAILS_H_