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 {
82 Representation() : kind_(kNone) { }
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 Float32x4() { return Representation(kFloat32x4); }
94 static Representation Float64x2() { return Representation(kFloat64x2); }
95 static Representation Int32x4() { return Representation(kInt32x4); }
96 static Representation HeapObject() { return Representation(kHeapObject); }
97 static Representation External() { return Representation(kExternal); }
99 static Representation FromKind(Kind kind) { return Representation(kind); }
101 static Representation FromType(Type* type);
103 bool Equals(const Representation& other) const {
104 return kind_ == other.kind_;
107 bool IsCompatibleForLoad(const Representation& other) const {
108 return (IsDouble() && other.IsDouble()) ||
109 (!IsDouble() && !other.IsDouble());
112 bool IsCompatibleForStore(const Representation& other) const {
113 return Equals(other);
116 bool is_more_general_than(const Representation& other) const {
117 if (kind_ == kExternal && other.kind_ == kNone) return true;
118 if (kind_ == kExternal && other.kind_ == kExternal) return false;
119 if (kind_ == kNone && other.kind_ == kExternal) return false;
121 DCHECK(kind_ != kExternal);
122 DCHECK(other.kind_ != kExternal);
123 if (IsHeapObject()) return other.IsNone();
124 if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
125 if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
126 if (IsSIMD128() && other.IsSIMD128()) return false;
127 return kind_ > other.kind_;
130 bool fits_into(const Representation& other) const {
131 return other.is_more_general_than(*this) || other.Equals(*this);
134 bool CanContainDouble(double value);
136 Representation generalize(Representation other) {
137 if (other.fits_into(*this)) return *this;
138 if (other.is_more_general_than(*this)) return other;
139 return Representation::Tagged();
144 if (IsInteger8() || IsUInteger8()) {
145 return sizeof(uint8_t);
147 if (IsInteger16() || IsUInteger16()) {
148 return sizeof(uint16_t);
151 return sizeof(uint32_t);
156 Kind kind() const { return static_cast<Kind>(kind_); }
157 bool IsNone() const { return kind_ == kNone; }
158 bool IsInteger8() const { return kind_ == kInteger8; }
159 bool IsUInteger8() const { return kind_ == kUInteger8; }
160 bool IsInteger16() const { return kind_ == kInteger16; }
161 bool IsUInteger16() const { return kind_ == kUInteger16; }
162 bool IsTagged() const { return kind_ == kTagged; }
163 bool IsSmi() const { return kind_ == kSmi; }
164 bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
165 bool IsInteger32() const { return kind_ == kInteger32; }
166 bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
167 bool IsDouble() const { return kind_ == kDouble; }
168 bool IsFloat32x4() const { return kind_ == kFloat32x4; }
169 bool IsFloat64x2() const { return kind_ == kFloat64x2; }
170 bool IsInt32x4() const { return kind_ == kInt32x4; }
171 bool IsSIMD128() const {
172 return IsFloat32x4() || IsFloat64x2() || IsInt32x4();
174 bool IsHeapObject() const { return kind_ == kHeapObject; }
175 bool IsExternal() const { return kind_ == kExternal; }
176 bool IsSpecialization() const {
177 return IsInteger8() || IsUInteger8() ||
178 IsInteger16() || IsUInteger16() ||
179 IsSmi() || IsInteger32() || IsDouble();
181 const char* Mnemonic() const;
184 explicit Representation(Kind k) : kind_(k) { }
186 // Make sure kind fits in int8.
187 STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
193 static const int kDescriptorIndexBitCount = 10;
194 // The maximum number of descriptors we want in a descriptor array (should
196 static const int kMaxNumberOfDescriptors =
197 (1 << kDescriptorIndexBitCount) - 2;
198 static const int kInvalidEnumCacheSentinel =
199 (1 << kDescriptorIndexBitCount) - 1;
202 // PropertyDetails captures type and attributes for a property.
203 // They are used both in property dictionaries and instance descriptors.
204 class PropertyDetails BASE_EMBEDDED {
206 PropertyDetails(PropertyAttributes attributes,
209 value_ = TypeField::encode(type)
210 | AttributesField::encode(attributes)
211 | DictionaryStorageField::encode(index);
213 DCHECK(type == this->type());
214 DCHECK(attributes == this->attributes());
217 PropertyDetails(PropertyAttributes attributes,
219 Representation representation,
220 int field_index = 0) {
221 value_ = TypeField::encode(type)
222 | AttributesField::encode(attributes)
223 | RepresentationField::encode(EncodeRepresentation(representation))
224 | FieldIndexField::encode(field_index);
227 int pointer() const { return DescriptorPointer::decode(value_); }
229 PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); }
231 PropertyDetails CopyWithRepresentation(Representation representation) const {
232 return PropertyDetails(value_, representation);
234 PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) {
236 static_cast<PropertyAttributes>(attributes() | new_attributes);
237 return PropertyDetails(value_, new_attributes);
240 // Conversion for storing details as Object*.
241 explicit inline PropertyDetails(Smi* smi);
242 inline Smi* AsSmi() const;
244 static uint8_t EncodeRepresentation(Representation representation) {
245 return representation.kind();
248 static Representation DecodeRepresentation(uint32_t bits) {
249 return Representation::FromKind(static_cast<Representation::Kind>(bits));
252 PropertyType type() const { return TypeField::decode(value_); }
254 PropertyAttributes attributes() const {
255 return AttributesField::decode(value_);
258 int dictionary_index() const {
259 return DictionaryStorageField::decode(value_);
262 Representation representation() const {
263 DCHECK(type() != NORMAL);
264 return DecodeRepresentation(RepresentationField::decode(value_));
267 int field_index() const {
268 return FieldIndexField::decode(value_);
271 inline PropertyDetails AsDeleted() const;
273 static bool IsValidIndex(int index) {
274 return DictionaryStorageField::is_valid(index);
277 bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
278 bool IsDontDelete() const { return (attributes() & DONT_DELETE) != 0; }
279 bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
280 bool IsDeleted() const { return DeletedField::decode(value_) != 0;}
282 // Bit fields in value_ (type, shift, size). Must be public so the
283 // constants can be embedded in generated code.
284 class TypeField: public BitField<PropertyType, 0, 3> {};
285 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
287 // Bit fields for normalized objects.
288 class DeletedField: public BitField<uint32_t, 6, 1> {};
289 class DictionaryStorageField: public BitField<uint32_t, 7, 24> {};
291 // Bit fields for fast objects.
292 class RepresentationField: public BitField<uint32_t, 6, 4> {};
293 class DescriptorPointer: public BitField<uint32_t, 10,
294 kDescriptorIndexBitCount> {}; // NOLINT
295 class FieldIndexField: public BitField<uint32_t,
296 10 + kDescriptorIndexBitCount,
297 kDescriptorIndexBitCount> {}; // NOLINT
298 // All bits for fast objects must fix in a smi.
299 STATIC_ASSERT(10 + kDescriptorIndexBitCount + kDescriptorIndexBitCount <= 31);
301 static const int kInitialIndex = 1;
304 PropertyDetails(int value, int pointer) {
305 value_ = DescriptorPointer::update(value, pointer);
307 PropertyDetails(int value, Representation representation) {
308 value_ = RepresentationField::update(
309 value, EncodeRepresentation(representation));
311 PropertyDetails(int value, PropertyAttributes attributes) {
312 value_ = AttributesField::update(value, attributes);
318 } } // namespace v8::internal
320 #endif // V8_PROPERTY_DETAILS_H_