67515be04745426179cc41560e41ff5a86013bab
[platform/upstream/v8.git] / src / field-index.h
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   static FieldIndex ForPropertyIndex(Map* map,
23                                      int index,
24                                      bool is_double = false);
25   static FieldIndex ForInObjectOffset(int offset, Map* map = NULL);
26   static FieldIndex ForDescriptor(Map* map, int descriptor_index);
27   static FieldIndex ForLoadByFieldIndex(Map* map, int index);
28   static FieldIndex ForKeyedLookupCacheIndex(Map* map, int index);
29   static FieldIndex FromFieldAccessStubKey(int key);
30
31   int GetLoadByFieldIndex() const;
32
33   bool is_inobject() const {
34     return IsInObjectBits::decode(bit_field_);
35   }
36
37   bool is_hidden_field() const { return IsHiddenField::decode(bit_field_); }
38
39   bool is_double() const {
40     return IsDoubleBits::decode(bit_field_);
41   }
42
43   int offset() const {
44     return index() * kPointerSize;
45   }
46
47   // Zero-indexed from beginning of the object.
48   int index() const {
49     return IndexBits::decode(bit_field_);
50   }
51
52   int outobject_array_index() const {
53     DCHECK(!is_inobject());
54     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   int property_index() const {
60     DCHECK(!is_hidden_field());
61     int result = index() - first_inobject_property_offset() / kPointerSize;
62     if (!is_inobject()) {
63       result += InObjectPropertyBits::decode(bit_field_);
64     }
65     return result;
66   }
67
68   int GetKeyedLookupCacheIndex() const;
69
70   int GetFieldAccessStubKey() const {
71     return bit_field_ &
72         (IsInObjectBits::kMask | IsDoubleBits::kMask | IndexBits::kMask);
73   }
74
75  private:
76   FieldIndex(bool is_inobject, int local_index, bool is_double,
77              int inobject_properties, int first_inobject_property_offset,
78              bool is_hidden = false) {
79     DCHECK((first_inobject_property_offset & (kPointerSize - 1)) == 0);
80     bit_field_ = IsInObjectBits::encode(is_inobject) |
81       IsDoubleBits::encode(is_double) |
82       FirstInobjectPropertyOffsetBits::encode(first_inobject_property_offset) |
83       IsHiddenField::encode(is_hidden) |
84       IndexBits::encode(local_index) |
85       InObjectPropertyBits::encode(inobject_properties);
86   }
87
88   explicit FieldIndex(int bit_field) : bit_field_(bit_field) {}
89
90   int first_inobject_property_offset() const {
91     DCHECK(!is_hidden_field());
92     return FirstInobjectPropertyOffsetBits::decode(bit_field_);
93   }
94
95   static const int kIndexBitsSize = kDescriptorIndexBitCount + 1;
96
97   // Index from beginning of object.
98   class IndexBits: public BitField<int, 0, kIndexBitsSize> {};
99   class IsInObjectBits: public BitField<bool, IndexBits::kNext, 1> {};
100   class IsDoubleBits: public BitField<bool, IsInObjectBits::kNext, 1> {};
101   // Number of inobject properties.
102   class InObjectPropertyBits
103       : public BitField<int, IsDoubleBits::kNext, kDescriptorIndexBitCount> {};
104   // Offset of first inobject property from beginning of object.
105   class FirstInobjectPropertyOffsetBits
106       : public BitField<int, InObjectPropertyBits::kNext, 7> {};
107   class IsHiddenField
108       : public BitField<bool, FirstInobjectPropertyOffsetBits::kNext, 1> {};
109   STATIC_ASSERT(IsHiddenField::kNext <= 32);
110
111   int bit_field_;
112 };
113
114 } }  // namespace v8::internal
115
116 #endif