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.
57 class Representation {
77 Representation() : kind_(kNone) { }
79 static Representation None() { return Representation(kNone); }
80 static Representation Tagged() { return Representation(kTagged); }
81 static Representation Integer8() { return Representation(kInteger8); }
82 static Representation UInteger8() { return Representation(kUInteger8); }
83 static Representation Integer16() { return Representation(kInteger16); }
84 static Representation UInteger16() { return Representation(kUInteger16); }
85 static Representation Smi() { return Representation(kSmi); }
86 static Representation Integer32() { return Representation(kInteger32); }
87 static Representation Double() { return Representation(kDouble); }
88 static Representation Float32x4() { return Representation(kFloat32x4); }
89 static Representation Float64x2() { return Representation(kFloat64x2); }
90 static Representation Int32x4() { return Representation(kInt32x4); }
91 static Representation HeapObject() { return Representation(kHeapObject); }
92 static Representation External() { return Representation(kExternal); }
94 static Representation FromKind(Kind kind) { return Representation(kind); }
96 static Representation FromType(Type* type);
98 bool Equals(const Representation& other) const {
99 return kind_ == other.kind_;
102 bool IsCompatibleForLoad(const Representation& other) const {
103 return (IsDouble() && other.IsDouble()) ||
104 (!IsDouble() && !other.IsDouble());
107 bool IsCompatibleForStore(const Representation& other) const {
108 return Equals(other);
111 bool is_more_general_than(const Representation& other) const {
112 if (kind_ == kExternal && other.kind_ == kNone) return true;
113 if (kind_ == kExternal && other.kind_ == kExternal) return false;
114 if (kind_ == kNone && other.kind_ == kExternal) return false;
116 DCHECK(kind_ != kExternal);
117 DCHECK(other.kind_ != kExternal);
118 if (IsHeapObject()) return other.IsNone();
119 if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
120 if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
121 if (IsSIMD128() && other.IsSIMD128()) return false;
122 return kind_ > other.kind_;
125 bool fits_into(const Representation& other) const {
126 return other.is_more_general_than(*this) || other.Equals(*this);
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 IsFloat32x4() const { return kind_ == kFloat32x4; }
162 bool IsFloat64x2() const { return kind_ == kFloat64x2; }
163 bool IsInt32x4() const { return kind_ == kInt32x4; }
164 bool IsSIMD128() const {
165 return IsFloat32x4() || IsFloat64x2() || IsInt32x4();
167 bool IsHeapObject() const { return kind_ == kHeapObject; }
168 bool IsExternal() const { return kind_ == kExternal; }
169 bool IsSpecialization() const {
170 return IsInteger8() || IsUInteger8() ||
171 IsInteger16() || IsUInteger16() ||
172 IsSmi() || IsInteger32() || IsDouble();
174 const char* Mnemonic() const;
177 explicit Representation(Kind k) : kind_(k) { }
179 // Make sure kind fits in int8.
180 STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
186 static const int kDescriptorIndexBitCount = 10;
187 // The maximum number of descriptors we want in a descriptor array (should
189 static const int kMaxNumberOfDescriptors =
190 (1 << kDescriptorIndexBitCount) - 2;
191 static const int kInvalidEnumCacheSentinel =
192 (1 << kDescriptorIndexBitCount) - 1;
195 // PropertyDetails captures type and attributes for a property.
196 // They are used both in property dictionaries and instance descriptors.
197 class PropertyDetails BASE_EMBEDDED {
199 PropertyDetails(PropertyAttributes attributes,
202 value_ = TypeField::encode(type)
203 | AttributesField::encode(attributes)
204 | DictionaryStorageField::encode(index);
206 DCHECK(type == this->type());
207 DCHECK(attributes == this->attributes());
210 PropertyDetails(PropertyAttributes attributes,
212 Representation representation,
213 int field_index = 0) {
214 value_ = TypeField::encode(type)
215 | AttributesField::encode(attributes)
216 | RepresentationField::encode(EncodeRepresentation(representation))
217 | FieldIndexField::encode(field_index);
220 int pointer() const { return DescriptorPointer::decode(value_); }
222 PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); }
224 PropertyDetails CopyWithRepresentation(Representation representation) const {
225 return PropertyDetails(value_, representation);
227 PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) {
229 static_cast<PropertyAttributes>(attributes() | new_attributes);
230 return PropertyDetails(value_, new_attributes);
233 // Conversion for storing details as Object*.
234 explicit inline PropertyDetails(Smi* smi);
235 inline Smi* AsSmi() const;
237 static uint8_t EncodeRepresentation(Representation representation) {
238 return representation.kind();
241 static Representation DecodeRepresentation(uint32_t bits) {
242 return Representation::FromKind(static_cast<Representation::Kind>(bits));
245 PropertyType type() const { return TypeField::decode(value_); }
247 PropertyAttributes attributes() const {
248 return AttributesField::decode(value_);
251 int dictionary_index() const {
252 return DictionaryStorageField::decode(value_);
255 Representation representation() const {
256 DCHECK(type() != NORMAL);
257 return DecodeRepresentation(RepresentationField::decode(value_));
260 int field_index() const {
261 return FieldIndexField::decode(value_);
264 inline PropertyDetails AsDeleted() const;
266 static bool IsValidIndex(int index) {
267 return DictionaryStorageField::is_valid(index);
270 bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
271 bool IsConfigurable() const { return (attributes() & DONT_DELETE) == 0; }
272 bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
273 bool IsDeleted() const { return DeletedField::decode(value_) != 0;}
275 // Bit fields in value_ (type, shift, size). Must be public so the
276 // constants can be embedded in generated code.
277 class TypeField : public BitField<PropertyType, 0, 2> {};
278 class AttributesField : public BitField<PropertyAttributes, 2, 3> {};
280 // Bit fields for normalized objects.
281 class DeletedField : public BitField<uint32_t, 5, 1> {};
282 class DictionaryStorageField : public BitField<uint32_t, 6, 24> {};
284 // Bit fields for fast objects.
285 class RepresentationField : public BitField<uint32_t, 5, 4> {};
286 class DescriptorPointer
287 : public BitField<uint32_t, 9, kDescriptorIndexBitCount> {}; // NOLINT
288 class FieldIndexField
289 : public BitField<uint32_t, 9 + kDescriptorIndexBitCount,
290 kDescriptorIndexBitCount> {}; // NOLINT
291 // All bits for fast objects must fix in a smi.
292 STATIC_ASSERT(9 + kDescriptorIndexBitCount + kDescriptorIndexBitCount <= 31);
294 static const int kInitialIndex = 1;
297 PropertyDetails(int value, int pointer) {
298 value_ = DescriptorPointer::update(value, pointer);
300 PropertyDetails(int value, Representation representation) {
301 value_ = RepresentationField::update(
302 value, EncodeRepresentation(representation));
304 PropertyDetails(int value, PropertyAttributes attributes) {
305 value_ = AttributesField::update(value, attributes);
311 } } // namespace v8::internal
313 #endif // V8_PROPERTY_DETAILS_H_