field-index.h 4.47 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
  static inline FieldIndex ForSmiLoadHandler(Map map, int32_t handler);
33 34
  static inline FieldIndex ForDescriptor(Map map,
                                         InternalIndex descriptor_index);
35
  static inline FieldIndex ForDescriptor(PtrComprCageBase cage_base, Map map,
36
                                         InternalIndex descriptor_index);
37

38
  inline int GetLoadByFieldIndex() const;
39

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

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

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

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

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

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

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

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

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

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

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  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;
  }
107

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

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

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

127
  uint64_t bit_field_;
128 129
};

130 131
}  // namespace internal
}  // namespace v8
132

133
#endif  // V8_OBJECTS_FIELD_INDEX_H_