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.
5 #ifndef V8_PROPERTY_DETAILS_H_
6 #define V8_PROPERTY_DETAILS_H_
8 #include "include/v8.h"
9 #include "src/allocation.h"
10 #include "src/utils.h"
13 enum PropertyAttributes {
15 READ_ONLY = v8::ReadOnly,
16 DONT_ENUM = v8::DontEnum,
17 DONT_DELETE = v8::DontDelete,
20 FROZEN = SEALED | READ_ONLY,
22 STRING = 8, // Used to filter symbols and string names
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.
38 template<class> class TypeImpl;
39 struct ZoneTypeConfig;
40 typedef TypeImpl<ZoneTypeConfig> Type;
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.
54 // Only in lookup results, not in descriptors.
57 // Only used as a marker in LookupResult.
62 class Representation {
79 Representation() : kind_(kNone) { }
81 static Representation None() { return Representation(kNone); }
82 static Representation Tagged() { return Representation(kTagged); }
83 static Representation Integer8() { return Representation(kInteger8); }
84 static Representation UInteger8() { return Representation(kUInteger8); }
85 static Representation Integer16() { return Representation(kInteger16); }
86 static Representation UInteger16() { return Representation(kUInteger16); }
87 static Representation Smi() { return Representation(kSmi); }
88 static Representation Integer32() { return Representation(kInteger32); }
89 static Representation Double() { return Representation(kDouble); }
90 static Representation HeapObject() { return Representation(kHeapObject); }
91 static Representation External() { return Representation(kExternal); }
93 static Representation FromKind(Kind kind) { return Representation(kind); }
95 static Representation FromType(Type* type);
97 bool Equals(const Representation& other) const {
98 return kind_ == other.kind_;
101 bool IsCompatibleForLoad(const Representation& other) const {
102 return (IsDouble() && other.IsDouble()) ||
103 (!IsDouble() && !other.IsDouble());
106 bool IsCompatibleForStore(const Representation& other) const {
107 return Equals(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;
115 DCHECK(kind_ != kExternal);
116 DCHECK(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_;
123 bool fits_into(const Representation& other) const {
124 return other.is_more_general_than(*this) || other.Equals(*this);
127 bool CanContainDouble(double value);
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();
137 if (IsInteger8() || IsUInteger8()) {
138 return sizeof(uint8_t);
140 if (IsInteger16() || IsUInteger16()) {
141 return sizeof(uint16_t);
144 return sizeof(uint32_t);
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 IsHeapObject() const { return kind_ == kHeapObject; }
162 bool IsExternal() const { return kind_ == kExternal; }
163 bool IsSpecialization() const {
164 return IsInteger8() || IsUInteger8() ||
165 IsInteger16() || IsUInteger16() ||
166 IsSmi() || IsInteger32() || IsDouble();
168 const char* Mnemonic() const;
171 explicit Representation(Kind k) : kind_(k) { }
173 // Make sure kind fits in int8.
174 STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
180 static const int kDescriptorIndexBitCount = 10;
181 // The maximum number of descriptors we want in a descriptor array (should
183 static const int kMaxNumberOfDescriptors =
184 (1 << kDescriptorIndexBitCount) - 2;
185 static const int kInvalidEnumCacheSentinel =
186 (1 << kDescriptorIndexBitCount) - 1;
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 {
193 PropertyDetails(PropertyAttributes attributes,
196 value_ = TypeField::encode(type)
197 | AttributesField::encode(attributes)
198 | DictionaryStorageField::encode(index);
200 DCHECK(type == this->type());
201 DCHECK(attributes == this->attributes());
204 PropertyDetails(PropertyAttributes attributes,
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);
214 int pointer() const { return DescriptorPointer::decode(value_); }
216 PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); }
218 PropertyDetails CopyWithRepresentation(Representation representation) const {
219 return PropertyDetails(value_, representation);
221 PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) {
223 static_cast<PropertyAttributes>(attributes() | new_attributes);
224 return PropertyDetails(value_, new_attributes);
227 // Conversion for storing details as Object*.
228 explicit inline PropertyDetails(Smi* smi);
229 inline Smi* AsSmi() const;
231 static uint8_t EncodeRepresentation(Representation representation) {
232 return representation.kind();
235 static Representation DecodeRepresentation(uint32_t bits) {
236 return Representation::FromKind(static_cast<Representation::Kind>(bits));
239 PropertyType type() const { return TypeField::decode(value_); }
241 PropertyAttributes attributes() const {
242 return AttributesField::decode(value_);
245 int dictionary_index() const {
246 return DictionaryStorageField::decode(value_);
249 Representation representation() const {
250 DCHECK(type() != NORMAL);
251 return DecodeRepresentation(RepresentationField::decode(value_));
254 int field_index() const {
255 return FieldIndexField::decode(value_);
258 inline PropertyDetails AsDeleted() const;
260 static bool IsValidIndex(int index) {
261 return DictionaryStorageField::is_valid(index);
264 bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
265 bool IsDontDelete() const { return (attributes() & DONT_DELETE) != 0; }
266 bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
267 bool IsDeleted() const { return DeletedField::decode(value_) != 0;}
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> {};
274 // Bit fields for normalized objects.
275 class DeletedField: public BitField<uint32_t, 6, 1> {};
276 class DictionaryStorageField: public BitField<uint32_t, 7, 24> {};
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);
288 static const int kInitialIndex = 1;
291 PropertyDetails(int value, int pointer) {
292 value_ = DescriptorPointer::update(value, pointer);
294 PropertyDetails(int value, Representation representation) {
295 value_ = RepresentationField::update(
296 value, EncodeRepresentation(representation));
298 PropertyDetails(int value, PropertyAttributes attributes) {
299 value_ = AttributesField::update(value, attributes);
305 } } // namespace v8::internal
307 #endif // V8_PROPERTY_DETAILS_H_