• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   SYMBOLIC          = 8,  // Used to filter symbol names
46   DONT_SHOW         = DONT_ENUM | SYMBOLIC,
47   ABSENT            = 16  // Used in runtime to indicate a property is absent.
48   // ABSENT can never be stored in or returned from a descriptor's attributes
49   // bitfield.  It is only used as a return value meaning the attributes of
50   // a non-existent property.
51 };
52 
53 
54 namespace v8 {
55 namespace internal {
56 
57 class Smi;
58 class Type;
59 class TypeInfo;
60 
61 // Type of properties.
62 // Order of properties is significant.
63 // Must fit in the BitField PropertyDetails::TypeField.
64 // A copy of this is in mirror-debugger.js.
65 enum PropertyType {
66   // Only in slow mode.
67   NORMAL                    = 0,
68   // Only in fast mode.
69   FIELD                     = 1,
70   CONSTANT                  = 2,
71   CALLBACKS                 = 3,
72   // Only in lookup results, not in descriptors.
73   HANDLER                   = 4,
74   INTERCEPTOR               = 5,
75   TRANSITION                = 6,
76   // Only used as a marker in LookupResult.
77   NONEXISTENT               = 7
78 };
79 
80 
81 class Representation {
82  public:
83   enum Kind {
84     kNone,
85     kInteger8,
86     kUInteger8,
87     kInteger16,
88     kUInteger16,
89     kSmi,
90     kInteger32,
91     kDouble,
92     kHeapObject,
93     kTagged,
94     kExternal,
95     kNumRepresentations
96   };
97 
Representation()98   Representation() : kind_(kNone) { }
99 
None()100   static Representation None() { return Representation(kNone); }
Tagged()101   static Representation Tagged() { return Representation(kTagged); }
Integer8()102   static Representation Integer8() { return Representation(kInteger8); }
UInteger8()103   static Representation UInteger8() { return Representation(kUInteger8); }
Integer16()104   static Representation Integer16() { return Representation(kInteger16); }
UInteger16()105   static Representation UInteger16() {
106     return Representation(kUInteger16);
107   }
Smi()108   static Representation Smi() { return Representation(kSmi); }
Integer32()109   static Representation Integer32() { return Representation(kInteger32); }
Double()110   static Representation Double() { return Representation(kDouble); }
HeapObject()111   static Representation HeapObject() { return Representation(kHeapObject); }
External()112   static Representation External() { return Representation(kExternal); }
113 
FromKind(Kind kind)114   static Representation FromKind(Kind kind) { return Representation(kind); }
115 
116   // TODO(rossberg): this should die eventually.
117   static Representation FromType(TypeInfo info);
118   static Representation FromType(Handle<Type> type);
119 
Equals(const Representation & other)120   bool Equals(const Representation& other) const {
121     return kind_ == other.kind_;
122   }
123 
IsCompatibleForLoad(const Representation & other)124   bool IsCompatibleForLoad(const Representation& other) const {
125     return (IsDouble() && other.IsDouble()) ||
126         (!IsDouble() && !other.IsDouble());
127   }
128 
IsCompatibleForStore(const Representation & other)129   bool IsCompatibleForStore(const Representation& other) const {
130     return Equals(other);
131   }
132 
is_more_general_than(const Representation & other)133   bool is_more_general_than(const Representation& other) const {
134     if (kind_ == kExternal && other.kind_ == kNone) return true;
135     if (kind_ == kExternal && other.kind_ == kExternal) return false;
136     if (kind_ == kNone && other.kind_ == kExternal) return false;
137 
138     ASSERT(kind_ != kExternal);
139     ASSERT(other.kind_ != kExternal);
140     if (IsHeapObject()) return other.IsNone();
141     if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
142     if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
143     return kind_ > other.kind_;
144   }
145 
fits_into(const Representation & other)146   bool fits_into(const Representation& other) const {
147     return other.is_more_general_than(*this) || other.Equals(*this);
148   }
149 
generalize(Representation other)150   Representation generalize(Representation other) {
151     if (other.fits_into(*this)) return *this;
152     if (other.is_more_general_than(*this)) return other;
153     return Representation::Tagged();
154   }
155 
size()156   int size() const {
157     ASSERT(!IsNone());
158     if (IsInteger8() || IsUInteger8()) {
159       return sizeof(uint8_t);
160     }
161     if (IsInteger16() || IsUInteger16()) {
162       return sizeof(uint16_t);
163     }
164     if (IsInteger32()) {
165       return sizeof(uint32_t);
166     }
167     return kPointerSize;
168   }
169 
kind()170   Kind kind() const { return static_cast<Kind>(kind_); }
IsNone()171   bool IsNone() const { return kind_ == kNone; }
IsInteger8()172   bool IsInteger8() const { return kind_ == kInteger8; }
IsUInteger8()173   bool IsUInteger8() const { return kind_ == kUInteger8; }
IsInteger16()174   bool IsInteger16() const { return kind_ == kInteger16; }
IsUInteger16()175   bool IsUInteger16() const { return kind_ == kUInteger16; }
IsTagged()176   bool IsTagged() const { return kind_ == kTagged; }
IsSmi()177   bool IsSmi() const { return kind_ == kSmi; }
IsSmiOrTagged()178   bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
IsInteger32()179   bool IsInteger32() const { return kind_ == kInteger32; }
IsSmiOrInteger32()180   bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
IsDouble()181   bool IsDouble() const { return kind_ == kDouble; }
IsHeapObject()182   bool IsHeapObject() const { return kind_ == kHeapObject; }
IsExternal()183   bool IsExternal() const { return kind_ == kExternal; }
IsSpecialization()184   bool IsSpecialization() const {
185     return IsInteger8() || IsUInteger8() ||
186       IsInteger16() || IsUInteger16() ||
187       IsSmi() || IsInteger32() || IsDouble();
188   }
189   const char* Mnemonic() const;
190 
191  private:
Representation(Kind k)192   explicit Representation(Kind k) : kind_(k) { }
193 
194   // Make sure kind fits in int8.
195   STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
196 
197   int8_t kind_;
198 };
199 
200 
201 static const int kDescriptorIndexBitCount = 10;
202 // The maximum number of descriptors we want in a descriptor array (should
203 // fit in a page).
204 static const int kMaxNumberOfDescriptors =
205     (1 << kDescriptorIndexBitCount) - 2;
206 static const int kInvalidEnumCacheSentinel =
207     (1 << kDescriptorIndexBitCount) - 1;
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:
PropertyDetails(PropertyAttributes attributes,PropertyType type,int index)214   PropertyDetails(PropertyAttributes attributes,
215                   PropertyType type,
216                   int index) {
217     value_ = TypeField::encode(type)
218         | AttributesField::encode(attributes)
219         | DictionaryStorageField::encode(index);
220 
221     ASSERT(type == this->type());
222     ASSERT(attributes == this->attributes());
223   }
224 
225   PropertyDetails(PropertyAttributes attributes,
226                   PropertyType type,
227                   Representation representation,
228                   int field_index = 0) {
229     value_ = TypeField::encode(type)
230         | AttributesField::encode(attributes)
231         | RepresentationField::encode(EncodeRepresentation(representation))
232         | FieldIndexField::encode(field_index);
233   }
234 
pointer()235   int pointer() { return DescriptorPointer::decode(value_); }
236 
set_pointer(int i)237   PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); }
238 
CopyWithRepresentation(Representation representation)239   PropertyDetails CopyWithRepresentation(Representation representation) {
240     return PropertyDetails(value_, representation);
241   }
CopyAddAttributes(PropertyAttributes new_attributes)242   PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) {
243     new_attributes =
244         static_cast<PropertyAttributes>(attributes() | new_attributes);
245     return PropertyDetails(value_, new_attributes);
246   }
247 
248   // Conversion for storing details as Object*.
249   explicit inline PropertyDetails(Smi* smi);
250   inline Smi* AsSmi();
251 
EncodeRepresentation(Representation representation)252   static uint8_t EncodeRepresentation(Representation representation) {
253     return representation.kind();
254   }
255 
DecodeRepresentation(uint32_t bits)256   static Representation DecodeRepresentation(uint32_t bits) {
257     return Representation::FromKind(static_cast<Representation::Kind>(bits));
258   }
259 
type()260   PropertyType type() { return TypeField::decode(value_); }
261 
attributes()262   PropertyAttributes attributes() const {
263     return AttributesField::decode(value_);
264   }
265 
dictionary_index()266   int dictionary_index() {
267     return DictionaryStorageField::decode(value_);
268   }
269 
representation()270   Representation representation() {
271     ASSERT(type() != NORMAL);
272     return DecodeRepresentation(RepresentationField::decode(value_));
273   }
274 
field_index()275   int  field_index() {
276     return FieldIndexField::decode(value_);
277   }
278 
279   inline PropertyDetails AsDeleted();
280 
IsValidIndex(int index)281   static bool IsValidIndex(int index) {
282     return DictionaryStorageField::is_valid(index);
283   }
284 
IsReadOnly()285   bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
IsDontDelete()286   bool IsDontDelete() const { return (attributes() & DONT_DELETE) != 0; }
IsDontEnum()287   bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
IsDeleted()288   bool IsDeleted() const { return DeletedField::decode(value_) != 0;}
289 
290   // Bit fields in value_ (type, shift, size). Must be public so the
291   // constants can be embedded in generated code.
292   class TypeField:                public BitField<PropertyType,       0,  3> {};
293   class AttributesField:          public BitField<PropertyAttributes, 3,  3> {};
294 
295   // Bit fields for normalized objects.
296   class DeletedField:             public BitField<uint32_t,           6,  1> {};
297   class DictionaryStorageField:   public BitField<uint32_t,           7, 24> {};
298 
299   // Bit fields for fast objects.
300   class RepresentationField:      public BitField<uint32_t,           6,  4> {};
301   class DescriptorPointer:        public BitField<uint32_t, 10,
302       kDescriptorIndexBitCount> {};  // NOLINT
303   class FieldIndexField:          public BitField<uint32_t,
304       10 + kDescriptorIndexBitCount,
305       kDescriptorIndexBitCount> {};  // NOLINT
306   // All bits for fast objects must fix in a smi.
307   STATIC_ASSERT(10 + kDescriptorIndexBitCount + kDescriptorIndexBitCount <= 31);
308 
309   static const int kInitialIndex = 1;
310 
311  private:
PropertyDetails(int value,int pointer)312   PropertyDetails(int value, int pointer) {
313     value_ = DescriptorPointer::update(value, pointer);
314   }
PropertyDetails(int value,Representation representation)315   PropertyDetails(int value, Representation representation) {
316     value_ = RepresentationField::update(
317         value, EncodeRepresentation(representation));
318   }
PropertyDetails(int value,PropertyAttributes attributes)319   PropertyDetails(int value, PropertyAttributes attributes) {
320     value_ = AttributesField::update(value, attributes);
321   }
322 
323   uint32_t value_;
324 };
325 
326 } }  // namespace v8::internal
327 
328 #endif  // V8_PROPERTY_DETAILS_H_
329