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