Home | History | Annotate | Download | only in src
      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_H_
      6 #define V8_FIELD_INDEX_H_
      7 
      8 #include "src/property-details.h"
      9 #include "src/utils.h"
     10 
     11 namespace v8 {
     12 namespace internal {
     13 
     14 class Map;
     15 
     16 // Wrapper class to hold a field index, usually but not necessarily generated
     17 // from a property index. When available, the wrapper class captures additional
     18 // information to allow the field index to be translated back into the property
     19 // index it was originally generated from.
     20 class FieldIndex final {
     21  public:
     22   FieldIndex() : bit_field_(0) {}
     23 
     24   static FieldIndex ForPropertyIndex(Map* map,
     25                                      int index,
     26                                      bool is_double = false);
     27   static FieldIndex ForInObjectOffset(int offset, Map* map = NULL);
     28   static FieldIndex ForDescriptor(Map* map, int descriptor_index);
     29   static FieldIndex ForLoadByFieldIndex(Map* map, int index);
     30   static FieldIndex ForKeyedLookupCacheIndex(Map* map, int index);
     31   static FieldIndex FromFieldAccessStubKey(int key);
     32 
     33   int GetLoadByFieldIndex() const;
     34 
     35   bool is_inobject() const {
     36     return IsInObjectBits::decode(bit_field_);
     37   }
     38 
     39   bool is_hidden_field() const { return IsHiddenField::decode(bit_field_); }
     40 
     41   bool is_double() const {
     42     return IsDoubleBits::decode(bit_field_);
     43   }
     44 
     45   int offset() const {
     46     return index() * kPointerSize;
     47   }
     48 
     49   // Zero-indexed from beginning of the object.
     50   int index() const {
     51     return IndexBits::decode(bit_field_);
     52   }
     53 
     54   int outobject_array_index() const {
     55     DCHECK(!is_inobject());
     56     return index() - first_inobject_property_offset() / kPointerSize;
     57   }
     58 
     59   // Zero-based from the first inobject property. Overflows to out-of-object
     60   // properties.
     61   int property_index() const {
     62     DCHECK(!is_hidden_field());
     63     int result = index() - first_inobject_property_offset() / kPointerSize;
     64     if (!is_inobject()) {
     65       result += InObjectPropertyBits::decode(bit_field_);
     66     }
     67     return result;
     68   }
     69 
     70   int GetKeyedLookupCacheIndex() const;
     71 
     72   int GetFieldAccessStubKey() const {
     73     return bit_field_ &
     74         (IsInObjectBits::kMask | IsDoubleBits::kMask | IndexBits::kMask);
     75   }
     76 
     77  private:
     78   FieldIndex(bool is_inobject, int local_index, bool is_double,
     79              int inobject_properties, int first_inobject_property_offset,
     80              bool is_hidden = false) {
     81     DCHECK((first_inobject_property_offset & (kPointerSize - 1)) == 0);
     82     bit_field_ = IsInObjectBits::encode(is_inobject) |
     83       IsDoubleBits::encode(is_double) |
     84       FirstInobjectPropertyOffsetBits::encode(first_inobject_property_offset) |
     85       IsHiddenField::encode(is_hidden) |
     86       IndexBits::encode(local_index) |
     87       InObjectPropertyBits::encode(inobject_properties);
     88   }
     89 
     90   explicit FieldIndex(int bit_field) : bit_field_(bit_field) {}
     91 
     92   int first_inobject_property_offset() const {
     93     DCHECK(!is_hidden_field());
     94     return FirstInobjectPropertyOffsetBits::decode(bit_field_);
     95   }
     96 
     97   static const int kIndexBitsSize = kDescriptorIndexBitCount + 1;
     98 
     99   // Index from beginning of object.
    100   class IndexBits: public BitField<int, 0, kIndexBitsSize> {};
    101   class IsInObjectBits: public BitField<bool, IndexBits::kNext, 1> {};
    102   class IsDoubleBits: public BitField<bool, IsInObjectBits::kNext, 1> {};
    103   // Number of inobject properties.
    104   class InObjectPropertyBits
    105       : public BitField<int, IsDoubleBits::kNext, kDescriptorIndexBitCount> {};
    106   // Offset of first inobject property from beginning of object.
    107   class FirstInobjectPropertyOffsetBits
    108       : public BitField<int, InObjectPropertyBits::kNext, 7> {};
    109   class IsHiddenField
    110       : public BitField<bool, FirstInobjectPropertyOffsetBits::kNext, 1> {};
    111   STATIC_ASSERT(IsHiddenField::kNext <= 32);
    112 
    113   int bit_field_;
    114 };
    115 
    116 }  // namespace internal
    117 }  // namespace v8
    118 
    119 #endif
    120