Line data Source code
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.
4 :
5 : #ifndef V8_FIELD_INDEX_H_
6 : #define V8_FIELD_INDEX_H_
7 :
8 : #include "src/property-details.h"
9 : #include "src/utils.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 :
14 : class Map;
15 :
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 final {
21 : public:
22 197861 : FieldIndex() : bit_field_(0) {}
23 :
24 : static FieldIndex ForPropertyIndex(const Map* map, int index,
25 : bool is_double = false);
26 : static FieldIndex ForInObjectOffset(int offset, const Map* map = nullptr);
27 : static FieldIndex ForDescriptor(const Map* map, int descriptor_index);
28 : static FieldIndex ForLoadByFieldIndex(const Map* map, int index);
29 : static FieldIndex FromFieldAccessStubKey(int key);
30 :
31 : int GetLoadByFieldIndex() const;
32 :
33 0 : bool is_inobject() const {
34 155298351 : return IsInObjectBits::decode(bit_field_);
35 : }
36 :
37 84172323 : bool is_hidden_field() const { return IsHiddenField::decode(bit_field_); }
38 :
39 : bool is_double() const {
40 0 : return IsDoubleBits::decode(bit_field_);
41 : }
42 :
43 : int offset() const {
44 66236735 : return index() * kPointerSize;
45 : }
46 :
47 : // Zero-indexed from beginning of the object.
48 : int index() const {
49 299085 : return IndexBits::decode(bit_field_);
50 : }
51 :
52 0 : int outobject_array_index() const {
53 : DCHECK(!is_inobject());
54 78431632 : return index() - first_inobject_property_offset() / kPointerSize;
55 : }
56 :
57 : // Zero-based from the first inobject property. Overflows to out-of-object
58 : // properties.
59 0 : int property_index() const {
60 : DCHECK(!is_hidden_field());
61 32649650 : int result = index() - first_inobject_property_offset() / kPointerSize;
62 32649650 : if (!is_inobject()) {
63 0 : result += InObjectPropertyBits::decode(bit_field_);
64 : }
65 0 : return result;
66 : }
67 :
68 : int GetFieldAccessStubKey() const {
69 : return bit_field_ &
70 11267 : (IsInObjectBits::kMask | IsDoubleBits::kMask | IndexBits::kMask);
71 : }
72 :
73 : bool operator==(FieldIndex const& other) const {
74 : return bit_field_ == other.bit_field_;
75 : }
76 : bool operator!=(FieldIndex const& other) const { return !(*this == other); }
77 :
78 : private:
79 : FieldIndex(bool is_inobject, int local_index, bool is_double,
80 : int inobject_properties, int first_inobject_property_offset,
81 : bool is_hidden = false) {
82 : DCHECK_EQ(first_inobject_property_offset & (kPointerSize - 1), 0);
83 144778103 : bit_field_ = IsInObjectBits::encode(is_inobject) |
84 144778103 : IsDoubleBits::encode(is_double) |
85 : FirstInobjectPropertyOffsetBits::encode(first_inobject_property_offset) |
86 144875049 : IsHiddenField::encode(is_hidden) |
87 144778103 : IndexBits::encode(local_index) |
88 144875049 : InObjectPropertyBits::encode(inobject_properties);
89 : }
90 :
91 : explicit FieldIndex(int bit_field) : bit_field_(bit_field) {}
92 :
93 : int first_inobject_property_offset() const {
94 : DCHECK(!is_hidden_field());
95 0 : return FirstInobjectPropertyOffsetBits::decode(bit_field_);
96 : }
97 :
98 : static const int kIndexBitsSize = kDescriptorIndexBitCount + 1;
99 :
100 : // Index from beginning of object.
101 : class IndexBits: public BitField<int, 0, kIndexBitsSize> {};
102 : class IsInObjectBits: public BitField<bool, IndexBits::kNext, 1> {};
103 : class IsDoubleBits: public BitField<bool, IsInObjectBits::kNext, 1> {};
104 : // Number of inobject properties.
105 : class InObjectPropertyBits
106 : : public BitField<int, IsDoubleBits::kNext, kDescriptorIndexBitCount> {};
107 : // Offset of first inobject property from beginning of object.
108 : class FirstInobjectPropertyOffsetBits
109 : : public BitField<int, InObjectPropertyBits::kNext, 7> {};
110 : class IsHiddenField
111 : : public BitField<bool, FirstInobjectPropertyOffsetBits::kNext, 1> {};
112 : STATIC_ASSERT(IsHiddenField::kNext <= 32);
113 :
114 : int bit_field_;
115 : };
116 :
117 : } // namespace internal
118 : } // namespace v8
119 :
120 : #endif
|