1 // Copyright 2014 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_FIELD_INDEX_H_
6 #define V8_FIELD_INDEX_H_
8 #include "src/property-details.h"
16 // Wrapper class to hold a field index, usually but not necessarily generated
17 // from a property index. When available, the wrapper class captures additional
18 // information to allow the field index to be translated back into the property
19 // index it was originally generated from.
20 class FieldIndex V8_FINAL {
22 static FieldIndex ForPropertyIndex(Map* map,
24 bool is_double = false);
25 static FieldIndex ForInObjectOffset(int offset, Map* map = NULL);
26 static FieldIndex ForLookupResult(const LookupResult* result);
27 static FieldIndex ForDescriptor(Map* map, int descriptor_index);
28 static FieldIndex ForLoadByFieldIndex(Map* map, int index);
29 static FieldIndex ForKeyedLookupCacheIndex(Map* map, int index);
31 int GetLoadByFieldIndex() const;
33 bool is_inobject() const {
34 return IsInObjectBits::decode(bit_field_);
37 bool is_double() const {
38 return IsDoubleBits::decode(bit_field_);
42 return index() * kPointerSize;
45 // Zero-indexed from beginning of the object.
47 return IndexBits::decode(bit_field_);
50 int outobject_array_index() const {
51 DCHECK(!is_inobject());
52 return index() - first_inobject_property_offset() / kPointerSize;
55 // Zero-based from the first inobject property. Overflows to out-of-object
57 int property_index() const {
58 DCHECK(!IsHiddenField::decode(bit_field_));
59 int result = index() - first_inobject_property_offset() / kPointerSize;
61 result += InObjectPropertyBits::decode(bit_field_);
66 int GetKeyedLookupCacheIndex() const;
68 int GetFieldAccessStubKey() const {
70 (IsInObjectBits::kMask | IsDoubleBits::kMask | IndexBits::kMask);
74 FieldIndex(bool is_inobject, int local_index, bool is_double,
75 int inobject_properties, int first_inobject_property_offset,
76 bool is_hidden = false) {
77 DCHECK((first_inobject_property_offset & (kPointerSize - 1)) == 0);
78 bit_field_ = IsInObjectBits::encode(is_inobject) |
79 IsDoubleBits::encode(is_double) |
80 FirstInobjectPropertyOffsetBits::encode(first_inobject_property_offset) |
81 IsHiddenField::encode(is_hidden) |
82 IndexBits::encode(local_index) |
83 InObjectPropertyBits::encode(inobject_properties);
86 int first_inobject_property_offset() const {
87 DCHECK(!IsHiddenField::decode(bit_field_));
88 return FirstInobjectPropertyOffsetBits::decode(bit_field_);
91 static const int kIndexBitsSize = kDescriptorIndexBitCount + 1;
93 // Index from beginning of object.
94 class IndexBits: public BitField<int, 0, kIndexBitsSize> {};
95 class IsInObjectBits: public BitField<bool, IndexBits::kNext, 1> {};
96 class IsDoubleBits: public BitField<bool, IsInObjectBits::kNext, 1> {};
97 // Number of inobject properties.
98 class InObjectPropertyBits
99 : public BitField<int, IsDoubleBits::kNext, kDescriptorIndexBitCount> {};
100 // Offset of first inobject property from beginning of object.
101 class FirstInobjectPropertyOffsetBits
102 : public BitField<int, InObjectPropertyBits::kNext, 7> {};
104 : public BitField<bool, FirstInobjectPropertyOffsetBits::kNext, 1> {};
105 STATIC_ASSERT(IsHiddenField::kNext <= 32);
110 } } // namespace v8::internal