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
ForInObjectOffset(int offset,Encoding encoding,const Map * map)15 inline FieldIndex FieldIndex::ForInObjectOffset(int offset, Encoding encoding,
16 const Map* map) {
17 DCHECK(map == nullptr || offset < map->instance_size());
18 DCHECK(encoding == kWord32 ? (offset % kInt32Size) == 0
19 : (offset % kPointerSize) == 0);
20 return FieldIndex(true, offset, encoding, 0, 0);
21 }
22
ForPropertyIndex(const Map * map,int property_index,Representation representation)23 inline FieldIndex FieldIndex::ForPropertyIndex(const Map* map,
24 int property_index,
25 Representation representation) {
26 DCHECK(map->instance_type() >= FIRST_NONSTRING_TYPE);
27 int inobject_properties = map->GetInObjectProperties();
28 bool is_inobject = property_index < inobject_properties;
29 int first_inobject_offset;
30 int offset;
31 if (is_inobject) {
32 first_inobject_offset = map->GetInObjectPropertyOffset(0);
33 offset = map->GetInObjectPropertyOffset(property_index);
34 } else {
35 first_inobject_offset = FixedArray::kHeaderSize;
36 property_index -= inobject_properties;
37 offset = FixedArray::kHeaderSize + property_index * kPointerSize;
38 }
39 Encoding encoding = FieldEncoding(representation);
40 return FieldIndex(is_inobject, offset, encoding, inobject_properties,
41 first_inobject_offset);
42 }
43
44 // Returns the index format accepted by the HLoadFieldByIndex instruction.
45 // (In-object: zero-based from (object start + JSObject::kHeaderSize),
46 // out-of-object: zero-based from FixedArray::kHeaderSize.)
GetLoadByFieldIndex()47 inline int FieldIndex::GetLoadByFieldIndex() const {
48 // For efficiency, the LoadByFieldIndex instruction takes an index that is
49 // optimized for quick access. If the property is inline, the index is
50 // positive. If it's out-of-line, the encoded index is -raw_index - 1 to
51 // disambiguate the zero out-of-line index from the zero inobject case.
52 // The index itself is shifted up by one bit, the lower-most bit
53 // signifying if the field is a mutable double box (1) or not (0).
54 int result = index();
55 if (is_inobject()) {
56 result -= JSObject::kHeaderSize / kPointerSize;
57 } else {
58 result -= FixedArray::kHeaderSize / kPointerSize;
59 result = -result - 1;
60 }
61 result <<= 1;
62 return is_double() ? (result | 1) : result;
63 }
64
ForDescriptor(const Map * map,int descriptor_index)65 inline FieldIndex FieldIndex::ForDescriptor(const Map* map,
66 int descriptor_index) {
67 PropertyDetails details =
68 map->instance_descriptors()->GetDetails(descriptor_index);
69 int field_index = details.field_index();
70 return ForPropertyIndex(map, field_index, details.representation());
71 }
72
73 } // namespace internal
74 } // namespace v8
75
76 #endif // V8_FIELD_INDEX_INL_H_
77