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_INL_H_
6 : #define V8_FIELD_INDEX_INL_H_
7 :
8 : #include "src/field-index.h"
9 : #include "src/objects-inl.h"
10 : #include "src/objects/descriptor-array.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : inline FieldIndex FieldIndex::ForInObjectOffset(int offset, const Map* map) {
16 : DCHECK_EQ(offset % kPointerSize, 0);
17 96946 : int index = offset / kPointerSize;
18 : DCHECK(map == nullptr ||
19 : index < (map->GetInObjectPropertyOffset(0) / kPointerSize +
20 : map->GetInObjectProperties()));
21 : return FieldIndex(true, index, false, 0, 0, true);
22 : }
23 :
24 144778104 : inline FieldIndex FieldIndex::ForPropertyIndex(const Map* map,
25 : int property_index,
26 : bool is_double) {
27 : DCHECK(map->instance_type() >= FIRST_NONSTRING_TYPE);
28 : int inobject_properties = map->GetInObjectProperties();
29 144778104 : bool is_inobject = property_index < inobject_properties;
30 : int first_inobject_offset;
31 144778104 : if (is_inobject) {
32 66045338 : first_inobject_offset = map->GetInObjectPropertyOffset(0);
33 : } else {
34 : first_inobject_offset = FixedArray::kHeaderSize;
35 78732766 : property_index -= inobject_properties;
36 : }
37 : return FieldIndex(is_inobject,
38 144778103 : property_index + first_inobject_offset / kPointerSize,
39 289556206 : is_double, inobject_properties, first_inobject_offset);
40 : }
41 :
42 : // Takes an index as computed by GetLoadByFieldIndex and reconstructs a
43 : // FieldIndex object from it.
44 0 : inline FieldIndex FieldIndex::ForLoadByFieldIndex(const Map* map,
45 : int orig_index) {
46 : int field_index = orig_index;
47 : bool is_inobject = true;
48 0 : bool is_double = field_index & 1;
49 : int first_inobject_offset = 0;
50 0 : field_index >>= 1;
51 0 : if (field_index < 0) {
52 0 : field_index = -(field_index + 1);
53 : is_inobject = false;
54 : first_inobject_offset = FixedArray::kHeaderSize;
55 0 : field_index += FixedArray::kHeaderSize / kPointerSize;
56 : } else {
57 0 : first_inobject_offset = map->GetInObjectPropertyOffset(0);
58 0 : field_index += JSObject::kHeaderSize / kPointerSize;
59 : }
60 : FieldIndex result(is_inobject, field_index, is_double,
61 : map->GetInObjectProperties(), first_inobject_offset);
62 : DCHECK(result.GetLoadByFieldIndex() == orig_index);
63 0 : return result;
64 : }
65 :
66 :
67 : // Returns the index format accepted by the HLoadFieldByIndex instruction.
68 : // (In-object: zero-based from (object start + JSObject::kHeaderSize),
69 : // out-of-object: zero-based from FixedArray::kHeaderSize.)
70 295330 : inline int FieldIndex::GetLoadByFieldIndex() const {
71 : // For efficiency, the LoadByFieldIndex instruction takes an index that is
72 : // optimized for quick access. If the property is inline, the index is
73 : // positive. If it's out-of-line, the encoded index is -raw_index - 1 to
74 : // disambiguate the zero out-of-line index from the zero inobject case.
75 : // The index itself is shifted up by one bit, the lower-most bit
76 : // signifying if the field is a mutable double box (1) or not (0).
77 : int result = index();
78 295330 : if (is_inobject()) {
79 221544 : result -= JSObject::kHeaderSize / kPointerSize;
80 : } else {
81 73786 : result -= FixedArray::kHeaderSize / kPointerSize;
82 73786 : result = -result - 1;
83 : }
84 295330 : result <<= 1;
85 295330 : return is_double() ? (result | 1) : result;
86 : }
87 :
88 129689612 : inline FieldIndex FieldIndex::ForDescriptor(const Map* map,
89 : int descriptor_index) {
90 : PropertyDetails details =
91 129689612 : map->instance_descriptors()->GetDetails(descriptor_index);
92 : int field_index = details.field_index();
93 : return ForPropertyIndex(map, field_index,
94 129689615 : details.representation().IsDouble());
95 : }
96 :
97 : inline FieldIndex FieldIndex::FromFieldAccessStubKey(int key) {
98 : return FieldIndex(key);
99 : }
100 :
101 : } // namespace internal
102 : } // namespace v8
103 :
104 : #endif
|