1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef V8_PROPERTY_DETAILS_H_
29 #define V8_PROPERTY_DETAILS_H_
31 #include "../include/v8.h"
32 #include "allocation.h"
36 enum PropertyAttributes {
38 READ_ONLY = v8::ReadOnly,
39 DONT_ENUM = v8::DontEnum,
40 DONT_DELETE = v8::DontDelete,
43 FROZEN = SEALED | READ_ONLY,
45 STRING = 8, // Used to filter symbols and string names
49 DONT_SHOW = DONT_ENUM | SYMBOLIC | PRIVATE_SYMBOL,
50 ABSENT = 64 // Used in runtime to indicate a property is absent.
51 // ABSENT can never be stored in or returned from a descriptor's attributes
52 // bitfield. It is only used as a return value meaning the attributes of
53 // a non-existent property.
61 template<class> class TypeImpl;
62 struct ZoneTypeConfig;
63 typedef TypeImpl<ZoneTypeConfig> Type;
66 // Type of properties.
67 // Order of properties is significant.
68 // Must fit in the BitField PropertyDetails::TypeField.
69 // A copy of this is in mirror-debugger.js.
77 // Only in lookup results, not in descriptors.
81 // Only used as a marker in LookupResult.
86 class Representation {
105 Representation() : kind_(kNone) { }
107 static Representation None() { return Representation(kNone); }
108 static Representation Tagged() { return Representation(kTagged); }
109 static Representation Integer8() { return Representation(kInteger8); }
110 static Representation UInteger8() { return Representation(kUInteger8); }
111 static Representation Integer16() { return Representation(kInteger16); }
112 static Representation UInteger16() { return Representation(kUInteger16); }
113 static Representation Smi() { return Representation(kSmi); }
114 static Representation Integer32() { return Representation(kInteger32); }
115 static Representation Double() { return Representation(kDouble); }
116 static Representation Float32x4() { return Representation(kFloat32x4); }
117 static Representation Int32x4() { return Representation(kInt32x4); }
118 static Representation HeapObject() { return Representation(kHeapObject); }
119 static Representation External() { return Representation(kExternal); }
121 static Representation FromKind(Kind kind) { return Representation(kind); }
123 static Representation FromType(Type* type);
125 bool Equals(const Representation& other) const {
126 return kind_ == other.kind_;
129 bool IsCompatibleForLoad(const Representation& other) const {
130 return (IsDouble() && other.IsDouble()) ||
131 (!IsDouble() && !other.IsDouble());
134 bool IsCompatibleForStore(const Representation& other) const {
135 return Equals(other);
138 bool is_more_general_than(const Representation& other) const {
139 if (kind_ == kExternal && other.kind_ == kNone) return true;
140 if (kind_ == kExternal && other.kind_ == kExternal) return false;
141 if (kind_ == kNone && other.kind_ == kExternal) return false;
143 ASSERT(kind_ != kExternal);
144 ASSERT(other.kind_ != kExternal);
145 if (IsHeapObject()) return other.IsNone();
146 if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
147 if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
148 if (IsSIMD128() && other.IsSIMD128()) return false;
149 return kind_ > other.kind_;
152 bool fits_into(const Representation& other) const {
153 return other.is_more_general_than(*this) || other.Equals(*this);
156 Representation generalize(Representation other) {
157 if (other.fits_into(*this)) return *this;
158 if (other.is_more_general_than(*this)) return other;
159 return Representation::Tagged();
164 if (IsInteger8() || IsUInteger8()) {
165 return sizeof(uint8_t);
167 if (IsInteger16() || IsUInteger16()) {
168 return sizeof(uint16_t);
171 return sizeof(uint32_t);
176 Kind kind() const { return static_cast<Kind>(kind_); }
177 bool IsNone() const { return kind_ == kNone; }
178 bool IsInteger8() const { return kind_ == kInteger8; }
179 bool IsUInteger8() const { return kind_ == kUInteger8; }
180 bool IsInteger16() const { return kind_ == kInteger16; }
181 bool IsUInteger16() const { return kind_ == kUInteger16; }
182 bool IsTagged() const { return kind_ == kTagged; }
183 bool IsSmi() const { return kind_ == kSmi; }
184 bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
185 bool IsInteger32() const { return kind_ == kInteger32; }
186 bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
187 bool IsDouble() const { return kind_ == kDouble; }
188 bool IsFloat32x4() const { return kind_ == kFloat32x4; }
189 bool IsInt32x4() const { return kind_ == kInt32x4; }
190 bool IsSIMD128() const { return IsFloat32x4() || IsInt32x4(); }
191 bool IsHeapObject() const { return kind_ == kHeapObject; }
192 bool IsExternal() const { return kind_ == kExternal; }
193 bool IsSpecialization() const {
194 return IsInteger8() || IsUInteger8() ||
195 IsInteger16() || IsUInteger16() ||
196 IsSmi() || IsInteger32() || IsDouble();
198 const char* Mnemonic() const;
201 explicit Representation(Kind k) : kind_(k) { }
203 // Make sure kind fits in int8.
204 STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
210 static const int kDescriptorIndexBitCount = 10;
211 // The maximum number of descriptors we want in a descriptor array (should
213 static const int kMaxNumberOfDescriptors =
214 (1 << kDescriptorIndexBitCount) - 2;
215 static const int kInvalidEnumCacheSentinel =
216 (1 << kDescriptorIndexBitCount) - 1;
219 // PropertyDetails captures type and attributes for a property.
220 // They are used both in property dictionaries and instance descriptors.
221 class PropertyDetails BASE_EMBEDDED {
223 PropertyDetails(PropertyAttributes attributes,
226 value_ = TypeField::encode(type)
227 | AttributesField::encode(attributes)
228 | DictionaryStorageField::encode(index);
230 ASSERT(type == this->type());
231 ASSERT(attributes == this->attributes());
234 PropertyDetails(PropertyAttributes attributes,
236 Representation representation,
237 int field_index = 0) {
238 value_ = TypeField::encode(type)
239 | AttributesField::encode(attributes)
240 | RepresentationField::encode(EncodeRepresentation(representation))
241 | FieldIndexField::encode(field_index);
244 int pointer() { return DescriptorPointer::decode(value_); }
246 PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); }
248 PropertyDetails CopyWithRepresentation(Representation representation) {
249 return PropertyDetails(value_, representation);
251 PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) {
253 static_cast<PropertyAttributes>(attributes() | new_attributes);
254 return PropertyDetails(value_, new_attributes);
257 // Conversion for storing details as Object*.
258 explicit inline PropertyDetails(Smi* smi);
261 static uint8_t EncodeRepresentation(Representation representation) {
262 return representation.kind();
265 static Representation DecodeRepresentation(uint32_t bits) {
266 return Representation::FromKind(static_cast<Representation::Kind>(bits));
269 PropertyType type() { return TypeField::decode(value_); }
271 PropertyAttributes attributes() const {
272 return AttributesField::decode(value_);
275 int dictionary_index() {
276 return DictionaryStorageField::decode(value_);
279 Representation representation() {
280 ASSERT(type() != NORMAL);
281 return DecodeRepresentation(RepresentationField::decode(value_));
285 return FieldIndexField::decode(value_);
288 inline PropertyDetails AsDeleted();
290 static bool IsValidIndex(int index) {
291 return DictionaryStorageField::is_valid(index);
294 bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
295 bool IsDontDelete() const { return (attributes() & DONT_DELETE) != 0; }
296 bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
297 bool IsDeleted() const { return DeletedField::decode(value_) != 0;}
299 // Bit fields in value_ (type, shift, size). Must be public so the
300 // constants can be embedded in generated code.
301 class TypeField: public BitField<PropertyType, 0, 3> {};
302 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
304 // Bit fields for normalized objects.
305 class DeletedField: public BitField<uint32_t, 6, 1> {};
306 class DictionaryStorageField: public BitField<uint32_t, 7, 24> {};
308 // Bit fields for fast objects.
309 class RepresentationField: public BitField<uint32_t, 6, 4> {};
310 class DescriptorPointer: public BitField<uint32_t, 10,
311 kDescriptorIndexBitCount> {}; // NOLINT
312 class FieldIndexField: public BitField<uint32_t,
313 10 + kDescriptorIndexBitCount,
314 kDescriptorIndexBitCount> {}; // NOLINT
315 // All bits for fast objects must fix in a smi.
316 STATIC_ASSERT(10 + kDescriptorIndexBitCount + kDescriptorIndexBitCount <= 31);
318 static const int kInitialIndex = 1;
321 PropertyDetails(int value, int pointer) {
322 value_ = DescriptorPointer::update(value, pointer);
324 PropertyDetails(int value, Representation representation) {
325 value_ = RepresentationField::update(
326 value, EncodeRepresentation(representation));
328 PropertyDetails(int value, PropertyAttributes attributes) {
329 value_ = AttributesField::update(value, attributes);
335 } } // namespace v8::internal
337 #endif // V8_PROPERTY_DETAILS_H_