field-index.h 3.82 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

#ifndef V8_FIELD_INDEX_H_
#define V8_FIELD_INDEX_H_

#include "src/property-details.h"
9
#include "src/utils.h"
10 11 12 13 14 15 16 17 18 19

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.
20
class FieldIndex final {
21
 public:
22 23
  FieldIndex() : bit_field_(0) {}

24 25 26 27 28 29
  static FieldIndex ForPropertyIndex(Map* map,
                                     int index,
                                     bool is_double = false);
  static FieldIndex ForInObjectOffset(int offset, Map* map = NULL);
  static FieldIndex ForDescriptor(Map* map, int descriptor_index);
  static FieldIndex ForLoadByFieldIndex(Map* map, int index);
30
  static FieldIndex ForKeyedLookupCacheIndex(Map* map, int index);
31
  static FieldIndex FromFieldAccessStubKey(int key);
32

33 34
  int GetLoadByFieldIndex() const;

35 36 37 38
  bool is_inobject() const {
    return IsInObjectBits::decode(bit_field_);
  }

39 40
  bool is_hidden_field() const { return IsHiddenField::decode(bit_field_); }

41 42 43 44 45 46 47 48
  bool is_double() const {
    return IsDoubleBits::decode(bit_field_);
  }

  int offset() const {
    return index() * kPointerSize;
  }

49
  // Zero-indexed from beginning of the object.
50 51 52 53 54
  int index() const {
    return IndexBits::decode(bit_field_);
  }

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

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

70
  int GetKeyedLookupCacheIndex() const;
71

72
  int GetFieldAccessStubKey() const {
73 74 75 76 77 78 79 80
    return bit_field_ &
        (IsInObjectBits::kMask | IsDoubleBits::kMask | IndexBits::kMask);
  }

 private:
  FieldIndex(bool is_inobject, int local_index, bool is_double,
             int inobject_properties, int first_inobject_property_offset,
             bool is_hidden = false) {
81
    DCHECK((first_inobject_property_offset & (kPointerSize - 1)) == 0);
82 83 84 85 86 87 88 89
    bit_field_ = IsInObjectBits::encode(is_inobject) |
      IsDoubleBits::encode(is_double) |
      FirstInobjectPropertyOffsetBits::encode(first_inobject_property_offset) |
      IsHiddenField::encode(is_hidden) |
      IndexBits::encode(local_index) |
      InObjectPropertyBits::encode(inobject_properties);
  }

90 91
  explicit FieldIndex(int bit_field) : bit_field_(bit_field) {}

92
  int first_inobject_property_offset() const {
93
    DCHECK(!is_hidden_field());
94 95 96 97 98
    return FirstInobjectPropertyOffsetBits::decode(bit_field_);
  }

  static const int kIndexBitsSize = kDescriptorIndexBitCount + 1;

99
  // Index from beginning of object.
100 101 102
  class IndexBits: public BitField<int, 0, kIndexBitsSize> {};
  class IsInObjectBits: public BitField<bool, IndexBits::kNext, 1> {};
  class IsDoubleBits: public BitField<bool, IsInObjectBits::kNext, 1> {};
103
  // Number of inobject properties.
104 105
  class InObjectPropertyBits
      : public BitField<int, IsDoubleBits::kNext, kDescriptorIndexBitCount> {};
106
  // Offset of first inobject property from beginning of object.
107 108 109 110
  class FirstInobjectPropertyOffsetBits
      : public BitField<int, InObjectPropertyBits::kNext, 7> {};
  class IsHiddenField
      : public BitField<bool, FirstInobjectPropertyOffsetBits::kNext, 1> {};
111 112 113 114 115
  STATIC_ASSERT(IsHiddenField::kNext <= 32);

  int bit_field_;
};

116 117
}  // namespace internal
}  // namespace v8
118 119

#endif