field-index.h 4.4 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_OBJECTS_FIELD_INDEX_H_
#define V8_OBJECTS_FIELD_INDEX_H_
7

8 9
// TODO(jkummerow): Consider forward-declaring instead.
#include "src/objects/internal-index.h"
10
#include "src/objects/property-details.h"
11
#include "src/utils/utils.h"
12 13 14 15 16 17 18 19 20 21

namespace v8 {
namespace internal {

class Map;

// Wrapper class to hold a field index, usually but not necessarily generated
// from a property index. When available, the wrapper class captures additional
// information to allow the field index to be translated back into the property
// index it was originally generated from.
22
class FieldIndex final {
23
 public:
24 25
  enum Encoding { kTagged, kDouble, kWord32 };

26 27
  FieldIndex() : bit_field_(0) {}

28
  static inline FieldIndex ForPropertyIndex(
29
      Map map, int index,
30
      Representation representation = Representation::Tagged());
31
  static inline FieldIndex ForInObjectOffset(int offset, Encoding encoding);
32 33
  static inline FieldIndex ForDescriptor(Map map,
                                         InternalIndex descriptor_index);
34
  static inline FieldIndex ForDescriptor(PtrComprCageBase cage_base, Map map,
35
                                         InternalIndex descriptor_index);
36

37
  inline int GetLoadByFieldIndex() const;
38

39
  bool is_inobject() const { return IsInObjectBits::decode(bit_field_); }
40

41
  bool is_double() const { return EncodingBits::decode(bit_field_) == kDouble; }
42

43
  int offset() const { return OffsetBits::decode(bit_field_); }
44

45 46
  uint64_t bit_field() const { return bit_field_; }

47
  // Zero-indexed from beginning of the object.
48
  int index() const {
49 50
    DCHECK(IsAligned(offset(), kTaggedSize));
    return offset() / kTaggedSize;
51 52 53
  }

  int outobject_array_index() const {
54
    DCHECK(!is_inobject());
55
    return index() - first_inobject_property_offset() / kTaggedSize;
56 57
  }

58 59
  // Zero-based from the first inobject property. Overflows to out-of-object
  // properties.
60
  int property_index() const {
61
    int result = index() - first_inobject_property_offset() / kTaggedSize;
62 63 64 65 66 67
    if (!is_inobject()) {
      result += InObjectPropertyBits::decode(bit_field_);
    }
    return result;
  }

68
  int GetFieldAccessStubKey() const {
69
    return bit_field_ &
70
           (IsInObjectBits::kMask | EncodingBits::kMask | OffsetBits::kMask);
71 72
  }

73 74 75 76 77
  bool operator==(FieldIndex const& other) const {
    return bit_field_ == other.bit_field_;
  }
  bool operator!=(FieldIndex const& other) const { return !(*this == other); }

78
 private:
79
  FieldIndex(bool is_inobject, int offset, Encoding encoding,
80
             int inobject_properties, int first_inobject_property_offset) {
81
    DCHECK(IsAligned(first_inobject_property_offset, kTaggedSize));
82
    bit_field_ = IsInObjectBits::encode(is_inobject) |
83 84 85
                 EncodingBits::encode(encoding) |
                 FirstInobjectPropertyOffsetBits::encode(
                     first_inobject_property_offset) |
86
                 OffsetBits::encode(offset) |
87
                 InObjectPropertyBits::encode(inobject_properties);
88 89
  }

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  static Encoding FieldEncoding(Representation representation) {
    switch (representation.kind()) {
      case Representation::kNone:
      case Representation::kSmi:
      case Representation::kHeapObject:
      case Representation::kTagged:
        return kTagged;
      case Representation::kDouble:
        return kDouble;
      default:
        break;
    }
    PrintF("%s\n", representation.Mnemonic());
    UNREACHABLE();
    return kTagged;
  }
106

107 108 109 110
  int first_inobject_property_offset() const {
    return FirstInobjectPropertyOffsetBits::decode(bit_field_);
  }

111
  static const int kOffsetBitsSize =
112
      (kDescriptorIndexBitCount + 1 + kTaggedSizeLog2);
113

114
  // Index from beginning of object.
115
  using OffsetBits = base::BitField64<int, 0, kOffsetBitsSize>;
116 117
  using IsInObjectBits = OffsetBits::Next<bool, 1>;
  using EncodingBits = IsInObjectBits::Next<Encoding, 2>;
118
  // Number of inobject properties.
119
  using InObjectPropertyBits =
120
      EncodingBits::Next<int, kDescriptorIndexBitCount>;
121
  // Offset of first inobject property from beginning of object.
122
  using FirstInobjectPropertyOffsetBits =
123
      InObjectPropertyBits::Next<int, kFirstInobjectPropertyOffsetBitCount>;
124
  static_assert(FirstInobjectPropertyOffsetBits::kLastUsedBit < 64);
125

126
  uint64_t bit_field_;
127 128
};

129 130
}  // namespace internal
}  // namespace v8
131

132
#endif  // V8_OBJECTS_FIELD_INDEX_H_