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 kHeapObject, 74 kTagged, 75 kExternal, 76 kNumRepresentations 77 }; 78 Representation()79 Representation() : kind_(kNone) { } 80 None()81 static Representation None() { return Representation(kNone); } Tagged()82 static Representation Tagged() { return Representation(kTagged); } Integer8()83 static Representation Integer8() { return Representation(kInteger8); } UInteger8()84 static Representation UInteger8() { return Representation(kUInteger8); } Integer16()85 static Representation Integer16() { return Representation(kInteger16); } UInteger16()86 static Representation UInteger16() { return Representation(kUInteger16); } Smi()87 static Representation Smi() { return Representation(kSmi); } Integer32()88 static Representation Integer32() { return Representation(kInteger32); } Double()89 static Representation Double() { return Representation(kDouble); } HeapObject()90 static Representation HeapObject() { return Representation(kHeapObject); } External()91 static Representation External() { return Representation(kExternal); } 92 FromKind(Kind kind)93 static Representation FromKind(Kind kind) { return Representation(kind); } 94 95 static Representation FromType(Type* type); 96 Equals(const Representation & other)97 bool Equals(const Representation& other) const { 98 return kind_ == other.kind_; 99 } 100 IsCompatibleForLoad(const Representation & other)101 bool IsCompatibleForLoad(const Representation& other) const { 102 return (IsDouble() && other.IsDouble()) || 103 (!IsDouble() && !other.IsDouble()); 104 } 105 IsCompatibleForStore(const Representation & other)106 bool IsCompatibleForStore(const Representation& other) const { 107 return Equals(other); 108 } 109 is_more_general_than(const Representation & other)110 bool is_more_general_than(const Representation& other) const { 111 if (kind_ == kExternal && other.kind_ == kNone) return true; 112 if (kind_ == kExternal && other.kind_ == kExternal) return false; 113 if (kind_ == kNone && other.kind_ == kExternal) return false; 114 115 ASSERT(kind_ != kExternal); 116 ASSERT(other.kind_ != kExternal); 117 if (IsHeapObject()) return other.IsNone(); 118 if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false; 119 if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false; 120 return kind_ > other.kind_; 121 } 122 fits_into(const Representation & other)123 bool fits_into(const Representation& other) const { 124 return other.is_more_general_than(*this) || other.Equals(*this); 125 } 126 127 bool CanContainDouble(double value); 128 generalize(Representation other)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 size()135 int size() const { 136 ASSERT(!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 kind()149 Kind kind() const { return static_cast<Kind>(kind_); } IsNone()150 bool IsNone() const { return kind_ == kNone; } IsInteger8()151 bool IsInteger8() const { return kind_ == kInteger8; } IsUInteger8()152 bool IsUInteger8() const { return kind_ == kUInteger8; } IsInteger16()153 bool IsInteger16() const { return kind_ == kInteger16; } IsUInteger16()154 bool IsUInteger16() const { return kind_ == kUInteger16; } IsTagged()155 bool IsTagged() const { return kind_ == kTagged; } IsSmi()156 bool IsSmi() const { return kind_ == kSmi; } IsSmiOrTagged()157 bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); } IsInteger32()158 bool IsInteger32() const { return kind_ == kInteger32; } IsSmiOrInteger32()159 bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); } IsDouble()160 bool IsDouble() const { return kind_ == kDouble; } IsHeapObject()161 bool IsHeapObject() const { return kind_ == kHeapObject; } IsExternal()162 bool IsExternal() const { return kind_ == kExternal; } IsSpecialization()163 bool IsSpecialization() const { 164 return IsInteger8() || IsUInteger8() || 165 IsInteger16() || IsUInteger16() || 166 IsSmi() || IsInteger32() || IsDouble(); 167 } 168 const char* Mnemonic() const; 169 170 private: Representation(Kind k)171 explicit Representation(Kind k) : kind_(k) { } 172 173 // Make sure kind fits in int8. 174 STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte)); 175 176 int8_t kind_; 177 }; 178 179 180 static const int kDescriptorIndexBitCount = 10; 181 // The maximum number of descriptors we want in a descriptor array (should 182 // fit in a page). 183 static const int kMaxNumberOfDescriptors = 184 (1 << kDescriptorIndexBitCount) - 2; 185 static const int kInvalidEnumCacheSentinel = 186 (1 << kDescriptorIndexBitCount) - 1; 187 188 189 // PropertyDetails captures type and attributes for a property. 190 // They are used both in property dictionaries and instance descriptors. 191 class PropertyDetails BASE_EMBEDDED { 192 public: PropertyDetails(PropertyAttributes attributes,PropertyType type,int index)193 PropertyDetails(PropertyAttributes attributes, 194 PropertyType type, 195 int index) { 196 value_ = TypeField::encode(type) 197 | AttributesField::encode(attributes) 198 | DictionaryStorageField::encode(index); 199 200 ASSERT(type == this->type()); 201 ASSERT(attributes == this->attributes()); 202 } 203 204 PropertyDetails(PropertyAttributes attributes, 205 PropertyType type, 206 Representation representation, 207 int field_index = 0) { 208 value_ = TypeField::encode(type) 209 | AttributesField::encode(attributes) 210 | RepresentationField::encode(EncodeRepresentation(representation)) 211 | FieldIndexField::encode(field_index); 212 } 213 pointer()214 int pointer() const { return DescriptorPointer::decode(value_); } 215 set_pointer(int i)216 PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); } 217 CopyWithRepresentation(Representation representation)218 PropertyDetails CopyWithRepresentation(Representation representation) const { 219 return PropertyDetails(value_, representation); 220 } CopyAddAttributes(PropertyAttributes new_attributes)221 PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) { 222 new_attributes = 223 static_cast<PropertyAttributes>(attributes() | new_attributes); 224 return PropertyDetails(value_, new_attributes); 225 } 226 227 // Conversion for storing details as Object*. 228 explicit inline PropertyDetails(Smi* smi); 229 inline Smi* AsSmi() const; 230 EncodeRepresentation(Representation representation)231 static uint8_t EncodeRepresentation(Representation representation) { 232 return representation.kind(); 233 } 234 DecodeRepresentation(uint32_t bits)235 static Representation DecodeRepresentation(uint32_t bits) { 236 return Representation::FromKind(static_cast<Representation::Kind>(bits)); 237 } 238 type()239 PropertyType type() const { return TypeField::decode(value_); } 240 attributes()241 PropertyAttributes attributes() const { 242 return AttributesField::decode(value_); 243 } 244 dictionary_index()245 int dictionary_index() const { 246 return DictionaryStorageField::decode(value_); 247 } 248 representation()249 Representation representation() const { 250 ASSERT(type() != NORMAL); 251 return DecodeRepresentation(RepresentationField::decode(value_)); 252 } 253 field_index()254 int field_index() const { 255 return FieldIndexField::decode(value_); 256 } 257 258 inline PropertyDetails AsDeleted() const; 259 IsValidIndex(int index)260 static bool IsValidIndex(int index) { 261 return DictionaryStorageField::is_valid(index); 262 } 263 IsReadOnly()264 bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; } IsDontDelete()265 bool IsDontDelete() const { return (attributes() & DONT_DELETE) != 0; } IsDontEnum()266 bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; } IsDeleted()267 bool IsDeleted() const { return DeletedField::decode(value_) != 0;} 268 269 // Bit fields in value_ (type, shift, size). Must be public so the 270 // constants can be embedded in generated code. 271 class TypeField: public BitField<PropertyType, 0, 3> {}; 272 class AttributesField: public BitField<PropertyAttributes, 3, 3> {}; 273 274 // Bit fields for normalized objects. 275 class DeletedField: public BitField<uint32_t, 6, 1> {}; 276 class DictionaryStorageField: public BitField<uint32_t, 7, 24> {}; 277 278 // Bit fields for fast objects. 279 class RepresentationField: public BitField<uint32_t, 6, 4> {}; 280 class DescriptorPointer: public BitField<uint32_t, 10, 281 kDescriptorIndexBitCount> {}; // NOLINT 282 class FieldIndexField: public BitField<uint32_t, 283 10 + kDescriptorIndexBitCount, 284 kDescriptorIndexBitCount> {}; // NOLINT 285 // All bits for fast objects must fix in a smi. 286 STATIC_ASSERT(10 + kDescriptorIndexBitCount + kDescriptorIndexBitCount <= 31); 287 288 static const int kInitialIndex = 1; 289 290 private: PropertyDetails(int value,int pointer)291 PropertyDetails(int value, int pointer) { 292 value_ = DescriptorPointer::update(value, pointer); 293 } PropertyDetails(int value,Representation representation)294 PropertyDetails(int value, Representation representation) { 295 value_ = RepresentationField::update( 296 value, EncodeRepresentation(representation)); 297 } PropertyDetails(int value,PropertyAttributes attributes)298 PropertyDetails(int value, PropertyAttributes attributes) { 299 value_ = AttributesField::update(value, attributes); 300 } 301 302 uint32_t value_; 303 }; 304 305 } } // namespace v8::internal 306 307 #endif // V8_PROPERTY_DETAILS_H_ 308