Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_PROPERTY_DETAILS_H_ |
| 6 | #define V8_PROPERTY_DETAILS_H_ |
| 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "include/v8.h" |
| 9 | #include "src/allocation.h" |
| 10 | #include "src/utils.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 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, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 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; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 38 | template<class> class TypeImpl; |
| 39 | struct ZoneTypeConfig; |
| 40 | typedef TypeImpl<ZoneTypeConfig> Type; |
| 41 | class TypeInfo; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 42 | |
| 43 | // Type of properties. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 44 | // Order of kinds is significant. |
| 45 | // Must fit in the BitField PropertyDetails::KindField. |
| 46 | enum PropertyKind { DATA = 0, ACCESSOR = 1 }; |
| 47 | |
| 48 | |
| 49 | // Order of modes is significant. |
| 50 | // Must fit in the BitField PropertyDetails::StoreModeField. |
| 51 | enum PropertyLocation { IN_OBJECT = 0, IN_DESCRIPTOR = 1 }; |
| 52 | |
| 53 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 54 | // Order of properties is significant. |
| 55 | // Must fit in the BitField PropertyDetails::TypeField. |
| 56 | // A copy of this is in mirror-debugger.js. |
| 57 | enum PropertyType { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 58 | FIELD = (IN_OBJECT << 1) | DATA, |
| 59 | CONSTANT = (IN_DESCRIPTOR << 1) | DATA, |
| 60 | ACCESSOR_FIELD = (IN_OBJECT << 1) | ACCESSOR, |
| 61 | CALLBACKS = (IN_DESCRIPTOR << 1) | ACCESSOR |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 65 | class Representation { |
| 66 | public: |
| 67 | enum Kind { |
| 68 | kNone, |
| 69 | kInteger8, |
| 70 | kUInteger8, |
| 71 | kInteger16, |
| 72 | kUInteger16, |
| 73 | kSmi, |
| 74 | kInteger32, |
| 75 | kDouble, |
| 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 HeapObject() { return Representation(kHeapObject); } |
| 94 | static Representation External() { return Representation(kExternal); } |
| 95 | |
| 96 | static Representation FromKind(Kind kind) { return Representation(kind); } |
| 97 | |
| 98 | static Representation FromType(Type* type); |
| 99 | |
| 100 | bool Equals(const Representation& other) const { |
| 101 | return kind_ == other.kind_; |
| 102 | } |
| 103 | |
| 104 | bool IsCompatibleForLoad(const Representation& other) const { |
| 105 | return (IsDouble() && other.IsDouble()) || |
| 106 | (!IsDouble() && !other.IsDouble()); |
| 107 | } |
| 108 | |
| 109 | bool IsCompatibleForStore(const Representation& other) const { |
| 110 | return Equals(other); |
| 111 | } |
| 112 | |
| 113 | bool is_more_general_than(const Representation& other) const { |
| 114 | if (kind_ == kExternal && other.kind_ == kNone) return true; |
| 115 | if (kind_ == kExternal && other.kind_ == kExternal) return false; |
| 116 | if (kind_ == kNone && other.kind_ == kExternal) return false; |
| 117 | |
| 118 | DCHECK(kind_ != kExternal); |
| 119 | DCHECK(other.kind_ != kExternal); |
| 120 | if (IsHeapObject()) return other.IsNone(); |
| 121 | if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false; |
| 122 | if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false; |
| 123 | return kind_ > other.kind_; |
| 124 | } |
| 125 | |
| 126 | bool fits_into(const Representation& other) const { |
| 127 | return other.is_more_general_than(*this) || other.Equals(*this); |
| 128 | } |
| 129 | |
| 130 | Representation generalize(Representation other) { |
| 131 | if (other.fits_into(*this)) return *this; |
| 132 | if (other.is_more_general_than(*this)) return other; |
| 133 | return Representation::Tagged(); |
| 134 | } |
| 135 | |
| 136 | int size() const { |
| 137 | DCHECK(!IsNone()); |
| 138 | if (IsInteger8() || IsUInteger8()) { |
| 139 | return sizeof(uint8_t); |
| 140 | } |
| 141 | if (IsInteger16() || IsUInteger16()) { |
| 142 | return sizeof(uint16_t); |
| 143 | } |
| 144 | if (IsInteger32()) { |
| 145 | return sizeof(uint32_t); |
| 146 | } |
| 147 | return kPointerSize; |
| 148 | } |
| 149 | |
| 150 | Kind kind() const { return static_cast<Kind>(kind_); } |
| 151 | bool IsNone() const { return kind_ == kNone; } |
| 152 | bool IsInteger8() const { return kind_ == kInteger8; } |
| 153 | bool IsUInteger8() const { return kind_ == kUInteger8; } |
| 154 | bool IsInteger16() const { return kind_ == kInteger16; } |
| 155 | bool IsUInteger16() const { return kind_ == kUInteger16; } |
| 156 | bool IsTagged() const { return kind_ == kTagged; } |
| 157 | bool IsSmi() const { return kind_ == kSmi; } |
| 158 | bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); } |
| 159 | bool IsInteger32() const { return kind_ == kInteger32; } |
| 160 | bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); } |
| 161 | bool IsDouble() const { return kind_ == kDouble; } |
| 162 | bool IsHeapObject() const { return kind_ == kHeapObject; } |
| 163 | bool IsExternal() const { return kind_ == kExternal; } |
| 164 | bool IsSpecialization() const { |
| 165 | return IsInteger8() || IsUInteger8() || |
| 166 | IsInteger16() || IsUInteger16() || |
| 167 | IsSmi() || IsInteger32() || IsDouble(); |
| 168 | } |
| 169 | const char* Mnemonic() const; |
| 170 | |
| 171 | private: |
| 172 | explicit Representation(Kind k) : kind_(k) { } |
| 173 | |
| 174 | // Make sure kind fits in int8. |
| 175 | STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte)); |
| 176 | |
| 177 | int8_t kind_; |
| 178 | }; |
| 179 | |
| 180 | |
| 181 | static const int kDescriptorIndexBitCount = 10; |
| 182 | // The maximum number of descriptors we want in a descriptor array (should |
| 183 | // fit in a page). |
| 184 | static const int kMaxNumberOfDescriptors = |
| 185 | (1 << kDescriptorIndexBitCount) - 2; |
| 186 | static const int kInvalidEnumCacheSentinel = |
| 187 | (1 << kDescriptorIndexBitCount) - 1; |
| 188 | |
| 189 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 190 | // PropertyDetails captures type and attributes for a property. |
| 191 | // They are used both in property dictionaries and instance descriptors. |
| 192 | class PropertyDetails BASE_EMBEDDED { |
| 193 | public: |
| 194 | PropertyDetails(PropertyAttributes attributes, |
| 195 | PropertyType type, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 196 | int index) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 197 | value_ = TypeField::encode(type) |
| 198 | | AttributesField::encode(attributes) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 199 | | DictionaryStorageField::encode(index); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 200 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 201 | DCHECK(type == this->type()); |
| 202 | DCHECK(attributes == this->attributes()); |
| 203 | } |
| 204 | |
| 205 | PropertyDetails(PropertyAttributes attributes, |
| 206 | PropertyType type, |
| 207 | Representation representation, |
| 208 | int field_index = 0) { |
| 209 | value_ = TypeField::encode(type) |
| 210 | | AttributesField::encode(attributes) |
| 211 | | RepresentationField::encode(EncodeRepresentation(representation)) |
| 212 | | FieldIndexField::encode(field_index); |
| 213 | } |
| 214 | |
| 215 | int pointer() const { return DescriptorPointer::decode(value_); } |
| 216 | |
| 217 | PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); } |
| 218 | |
| 219 | PropertyDetails CopyWithRepresentation(Representation representation) const { |
| 220 | return PropertyDetails(value_, representation); |
| 221 | } |
| 222 | PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) { |
| 223 | new_attributes = |
| 224 | static_cast<PropertyAttributes>(attributes() | new_attributes); |
| 225 | return PropertyDetails(value_, new_attributes); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // Conversion for storing details as Object*. |
| 229 | explicit inline PropertyDetails(Smi* smi); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 230 | inline Smi* AsSmi() const; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 231 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 232 | static uint8_t EncodeRepresentation(Representation representation) { |
| 233 | return representation.kind(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 236 | static Representation DecodeRepresentation(uint32_t bits) { |
| 237 | return Representation::FromKind(static_cast<Representation::Kind>(bits)); |
| 238 | } |
| 239 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 240 | PropertyKind kind() const { return KindField::decode(value_); } |
| 241 | PropertyLocation location() const { return LocationField::decode(value_); } |
| 242 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 243 | PropertyType type() const { return TypeField::decode(value_); } |
| 244 | |
| 245 | PropertyAttributes attributes() const { |
| 246 | return AttributesField::decode(value_); |
| 247 | } |
| 248 | |
| 249 | int dictionary_index() const { |
| 250 | return DictionaryStorageField::decode(value_); |
| 251 | } |
| 252 | |
| 253 | Representation representation() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 254 | return DecodeRepresentation(RepresentationField::decode(value_)); |
| 255 | } |
| 256 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 257 | int field_index() const { return FieldIndexField::decode(value_); } |
| 258 | |
| 259 | inline int field_width_in_words() const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 260 | |
| 261 | inline PropertyDetails AsDeleted() const; |
| 262 | |
| 263 | static bool IsValidIndex(int index) { |
| 264 | return DictionaryStorageField::is_valid(index); |
| 265 | } |
| 266 | |
| 267 | bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; } |
| 268 | bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; } |
| 269 | bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 270 | bool IsDeleted() const { return DeletedField::decode(value_) != 0; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 271 | |
| 272 | // Bit fields in value_ (type, shift, size). Must be public so the |
| 273 | // constants can be embedded in generated code. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 274 | class KindField : public BitField<PropertyKind, 0, 1> {}; |
| 275 | class LocationField : public BitField<PropertyLocation, 1, 1> {}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 276 | class AttributesField : public BitField<PropertyAttributes, 2, 3> {}; |
| 277 | |
| 278 | // Bit fields for normalized objects. |
| 279 | class DeletedField : public BitField<uint32_t, 5, 1> {}; |
| 280 | class DictionaryStorageField : public BitField<uint32_t, 6, 24> {}; |
| 281 | |
| 282 | // Bit fields for fast objects. |
| 283 | class RepresentationField : public BitField<uint32_t, 5, 4> {}; |
| 284 | class DescriptorPointer |
| 285 | : public BitField<uint32_t, 9, kDescriptorIndexBitCount> {}; // NOLINT |
| 286 | class FieldIndexField |
| 287 | : public BitField<uint32_t, 9 + kDescriptorIndexBitCount, |
| 288 | kDescriptorIndexBitCount> {}; // NOLINT |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 289 | |
| 290 | // NOTE: TypeField overlaps with KindField and LocationField. |
| 291 | class TypeField : public BitField<PropertyType, 0, 2> {}; |
| 292 | STATIC_ASSERT(KindField::kNext == LocationField::kShift); |
| 293 | STATIC_ASSERT(TypeField::kShift == KindField::kShift); |
| 294 | STATIC_ASSERT(TypeField::kNext == LocationField::kNext); |
| 295 | |
| 296 | // All bits for both fast and slow objects must fit in a smi. |
| 297 | STATIC_ASSERT(DictionaryStorageField::kNext <= 31); |
| 298 | STATIC_ASSERT(FieldIndexField::kNext <= 31); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 299 | |
| 300 | static const int kInitialIndex = 1; |
| 301 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 302 | #ifdef OBJECT_PRINT |
| 303 | // For our gdb macros, we should perhaps change these in the future. |
| 304 | void Print(bool dictionary_mode); |
| 305 | #endif |
| 306 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 307 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 308 | PropertyDetails(int value, int pointer) { |
| 309 | value_ = DescriptorPointer::update(value, pointer); |
| 310 | } |
| 311 | PropertyDetails(int value, Representation representation) { |
| 312 | value_ = RepresentationField::update( |
| 313 | value, EncodeRepresentation(representation)); |
| 314 | } |
| 315 | PropertyDetails(int value, PropertyAttributes attributes) { |
| 316 | value_ = AttributesField::update(value, attributes); |
| 317 | } |
| 318 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 319 | uint32_t value_; |
| 320 | }; |
| 321 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 322 | |
| 323 | std::ostream& operator<<(std::ostream& os, |
| 324 | const PropertyAttributes& attributes); |
| 325 | std::ostream& operator<<(std::ostream& os, const PropertyDetails& details); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 326 | } } // namespace v8::internal |
| 327 | |
| 328 | #endif // V8_PROPERTY_DETAILS_H_ |