blob: 2558529070b8a09a787208dc897486d0d4d69d27 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
11namespace v8 {
12namespace internal {
13
14class 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.
20class 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_double() const {
38 return IsDoubleBits::decode(bit_field_);
39 }
40
41 int offset() const {
42 return index() * kPointerSize;
43 }
44
45 // Zero-indexed from beginning of the object.
46 int index() const {
47 return IndexBits::decode(bit_field_);
48 }
49
50 int outobject_array_index() const {
51 DCHECK(!is_inobject());
52 return index() - first_inobject_property_offset() / kPointerSize;
53 }
54
55 // Zero-based from the first inobject property. Overflows to out-of-object
56 // properties.
57 int property_index() const {
58 DCHECK(!IsHiddenField::decode(bit_field_));
59 int result = index() - first_inobject_property_offset() / kPointerSize;
60 if (!is_inobject()) {
61 result += InObjectPropertyBits::decode(bit_field_);
62 }
63 return result;
64 }
65
66 int GetKeyedLookupCacheIndex() const;
67
68 int GetFieldAccessStubKey() const {
69 return bit_field_ &
70 (IsInObjectBits::kMask | IsDoubleBits::kMask | IndexBits::kMask);
71 }
72
73 private:
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);
84 }
85
86 explicit FieldIndex(int bit_field) : bit_field_(bit_field) {}
87
88 int first_inobject_property_offset() const {
89 DCHECK(!IsHiddenField::decode(bit_field_));
90 return FirstInobjectPropertyOffsetBits::decode(bit_field_);
91 }
92
93 static const int kIndexBitsSize = kDescriptorIndexBitCount + 1;
94
95 // Index from beginning of object.
96 class IndexBits: public BitField<int, 0, kIndexBitsSize> {};
97 class IsInObjectBits: public BitField<bool, IndexBits::kNext, 1> {};
98 class IsDoubleBits: public BitField<bool, IsInObjectBits::kNext, 1> {};
99 // Number of inobject properties.
100 class InObjectPropertyBits
101 : public BitField<int, IsDoubleBits::kNext, kDescriptorIndexBitCount> {};
102 // Offset of first inobject property from beginning of object.
103 class FirstInobjectPropertyOffsetBits
104 : public BitField<int, InObjectPropertyBits::kNext, 7> {};
105 class IsHiddenField
106 : public BitField<bool, FirstInobjectPropertyOffsetBits::kNext, 1> {};
107 STATIC_ASSERT(IsHiddenField::kNext <= 32);
108
109 int bit_field_;
110};
111
112} } // namespace v8::internal
113
114#endif