Home | History | Annotate | Download | only in src
      1 // Copyright 2012 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #ifndef V8_PROPERTY_DETAILS_H_
     29 #define V8_PROPERTY_DETAILS_H_
     30 
     31 #include "../include/v8.h"
     32 #include "allocation.h"
     33 #include "utils.h"
     34 
     35 // Ecma-262 3rd 8.6.1
     36 enum PropertyAttributes {
     37   NONE              = v8::None,
     38   READ_ONLY         = v8::ReadOnly,
     39   DONT_ENUM         = v8::DontEnum,
     40   DONT_DELETE       = v8::DontDelete,
     41 
     42   SEALED            = DONT_DELETE,
     43   FROZEN            = SEALED | READ_ONLY,
     44 
     45   SYMBOLIC          = 8,  // Used to filter symbol names
     46   DONT_SHOW         = DONT_ENUM | SYMBOLIC,
     47   ABSENT            = 16  // Used in runtime to indicate a property is absent.
     48   // ABSENT can never be stored in or returned from a descriptor's attributes
     49   // bitfield.  It is only used as a return value meaning the attributes of
     50   // a non-existent property.
     51 };
     52 
     53 
     54 namespace v8 {
     55 namespace internal {
     56 
     57 class Smi;
     58 class Type;
     59 class TypeInfo;
     60 
     61 // Type of properties.
     62 // Order of properties is significant.
     63 // Must fit in the BitField PropertyDetails::TypeField.
     64 // A copy of this is in mirror-debugger.js.
     65 enum PropertyType {
     66   // Only in slow mode.
     67   NORMAL                    = 0,
     68   // Only in fast mode.
     69   FIELD                     = 1,
     70   CONSTANT                  = 2,
     71   CALLBACKS                 = 3,
     72   // Only in lookup results, not in descriptors.
     73   HANDLER                   = 4,
     74   INTERCEPTOR               = 5,
     75   TRANSITION                = 6,
     76   // Only used as a marker in LookupResult.
     77   NONEXISTENT               = 7
     78 };
     79 
     80 
     81 class Representation {
     82  public:
     83   enum Kind {
     84     kNone,
     85     kInteger8,
     86     kUInteger8,
     87     kInteger16,
     88     kUInteger16,
     89     kSmi,
     90     kInteger32,
     91     kDouble,
     92     kHeapObject,
     93     kTagged,
     94     kExternal,
     95     kNumRepresentations
     96   };
     97 
     98   Representation() : kind_(kNone) { }
     99 
    100   static Representation None() { return Representation(kNone); }
    101   static Representation Tagged() { return Representation(kTagged); }
    102   static Representation Integer8() { return Representation(kInteger8); }
    103   static Representation UInteger8() { return Representation(kUInteger8); }
    104   static Representation Integer16() { return Representation(kInteger16); }
    105   static Representation UInteger16() {
    106     return Representation(kUInteger16);
    107   }
    108   static Representation Smi() { return Representation(kSmi); }
    109   static Representation Integer32() { return Representation(kInteger32); }
    110   static Representation Double() { return Representation(kDouble); }
    111   static Representation HeapObject() { return Representation(kHeapObject); }
    112   static Representation External() { return Representation(kExternal); }
    113 
    114   static Representation FromKind(Kind kind) { return Representation(kind); }
    115 
    116   // TODO(rossberg): this should die eventually.
    117   static Representation FromType(TypeInfo info);
    118   static Representation FromType(Handle<Type> type);
    119 
    120   bool Equals(const Representation& other) const {
    121     return kind_ == other.kind_;
    122   }
    123 
    124   bool IsCompatibleForLoad(const Representation& other) const {
    125     return (IsDouble() && other.IsDouble()) ||
    126         (!IsDouble() && !other.IsDouble());
    127   }
    128 
    129   bool IsCompatibleForStore(const Representation& other) const {
    130     return Equals(other);
    131   }
    132 
    133   bool is_more_general_than(const Representation& other) const {
    134     if (kind_ == kExternal && other.kind_ == kNone) return true;
    135     if (kind_ == kExternal && other.kind_ == kExternal) return false;
    136     if (kind_ == kNone && other.kind_ == kExternal) return false;
    137 
    138     ASSERT(kind_ != kExternal);
    139     ASSERT(other.kind_ != kExternal);
    140     if (IsHeapObject()) return other.IsNone();
    141     if (kind_ == kUInteger8 && other.kind_ == kInteger8) return false;
    142     if (kind_ == kUInteger16 && other.kind_ == kInteger16) return false;
    143     return kind_ > other.kind_;
    144   }
    145 
    146   bool fits_into(const Representation& other) const {
    147     return other.is_more_general_than(*this) || other.Equals(*this);
    148   }
    149 
    150   Representation generalize(Representation other) {
    151     if (other.fits_into(*this)) return *this;
    152     if (other.is_more_general_than(*this)) return other;
    153     return Representation::Tagged();
    154   }
    155 
    156   int size() const {
    157     ASSERT(!IsNone());
    158     if (IsInteger8() || IsUInteger8()) {
    159       return sizeof(uint8_t);
    160     }
    161     if (IsInteger16() || IsUInteger16()) {
    162       return sizeof(uint16_t);
    163     }
    164     if (IsInteger32()) {
    165       return sizeof(uint32_t);
    166     }
    167     return kPointerSize;
    168   }
    169 
    170   Kind kind() const { return static_cast<Kind>(kind_); }
    171   bool IsNone() const { return kind_ == kNone; }
    172   bool IsInteger8() const { return kind_ == kInteger8; }
    173   bool IsUInteger8() const { return kind_ == kUInteger8; }
    174   bool IsInteger16() const { return kind_ == kInteger16; }
    175   bool IsUInteger16() const { return kind_ == kUInteger16; }
    176   bool IsTagged() const { return kind_ == kTagged; }
    177   bool IsSmi() const { return kind_ == kSmi; }
    178   bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
    179   bool IsInteger32() const { return kind_ == kInteger32; }
    180   bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
    181   bool IsDouble() const { return kind_ == kDouble; }
    182   bool IsHeapObject() const { return kind_ == kHeapObject; }
    183   bool IsExternal() const { return kind_ == kExternal; }
    184   bool IsSpecialization() const {
    185     return IsInteger8() || IsUInteger8() ||
    186       IsInteger16() || IsUInteger16() ||
    187       IsSmi() || IsInteger32() || IsDouble();
    188   }
    189   const char* Mnemonic() const;
    190 
    191  private:
    192   explicit Representation(Kind k) : kind_(k) { }
    193 
    194   // Make sure kind fits in int8.
    195   STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
    196 
    197   int8_t kind_;
    198 };
    199 
    200 
    201 static const int kDescriptorIndexBitCount = 10;
    202 // The maximum number of descriptors we want in a descriptor array (should
    203 // fit in a page).
    204 static const int kMaxNumberOfDescriptors =
    205     (1 << kDescriptorIndexBitCount) - 2;
    206 static const int kInvalidEnumCacheSentinel =
    207     (1 << kDescriptorIndexBitCount) - 1;
    208 
    209 
    210 // PropertyDetails captures type and attributes for a property.
    211 // They are used both in property dictionaries and instance descriptors.
    212 class PropertyDetails BASE_EMBEDDED {
    213  public:
    214   PropertyDetails(PropertyAttributes attributes,
    215                   PropertyType type,
    216                   int index) {
    217     value_ = TypeField::encode(type)
    218         | AttributesField::encode(attributes)
    219         | DictionaryStorageField::encode(index);
    220 
    221     ASSERT(type == this->type());
    222     ASSERT(attributes == this->attributes());
    223   }
    224 
    225   PropertyDetails(PropertyAttributes attributes,
    226                   PropertyType type,
    227                   Representation representation,
    228                   int field_index = 0) {
    229     value_ = TypeField::encode(type)
    230         | AttributesField::encode(attributes)
    231         | RepresentationField::encode(EncodeRepresentation(representation))
    232         | FieldIndexField::encode(field_index);
    233   }
    234 
    235   int pointer() { return DescriptorPointer::decode(value_); }
    236 
    237   PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); }
    238 
    239   PropertyDetails CopyWithRepresentation(Representation representation) {
    240     return PropertyDetails(value_, representation);
    241   }
    242   PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) {
    243     new_attributes =
    244         static_cast<PropertyAttributes>(attributes() | new_attributes);
    245     return PropertyDetails(value_, new_attributes);
    246   }
    247 
    248   // Conversion for storing details as Object*.
    249   explicit inline PropertyDetails(Smi* smi);
    250   inline Smi* AsSmi();
    251 
    252   static uint8_t EncodeRepresentation(Representation representation) {
    253     return representation.kind();
    254   }
    255 
    256   static Representation DecodeRepresentation(uint32_t bits) {
    257     return Representation::FromKind(static_cast<Representation::Kind>(bits));
    258   }
    259 
    260   PropertyType type() { return TypeField::decode(value_); }
    261 
    262   PropertyAttributes attributes() const {
    263     return AttributesField::decode(value_);
    264   }
    265 
    266   int dictionary_index() {
    267     return DictionaryStorageField::decode(value_);
    268   }
    269 
    270   Representation representation() {
    271     ASSERT(type() != NORMAL);
    272     return DecodeRepresentation(RepresentationField::decode(value_));
    273   }
    274 
    275   int  field_index() {
    276     return FieldIndexField::decode(value_);
    277   }
    278 
    279   inline PropertyDetails AsDeleted();
    280 
    281   static bool IsValidIndex(int index) {
    282     return DictionaryStorageField::is_valid(index);
    283   }
    284 
    285   bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
    286   bool IsDontDelete() const { return (attributes() & DONT_DELETE) != 0; }
    287   bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
    288   bool IsDeleted() const { return DeletedField::decode(value_) != 0;}
    289 
    290   // Bit fields in value_ (type, shift, size). Must be public so the
    291   // constants can be embedded in generated code.
    292   class TypeField:                public BitField<PropertyType,       0,  3> {};
    293   class AttributesField:          public BitField<PropertyAttributes, 3,  3> {};
    294 
    295   // Bit fields for normalized objects.
    296   class DeletedField:             public BitField<uint32_t,           6,  1> {};
    297   class DictionaryStorageField:   public BitField<uint32_t,           7, 24> {};
    298 
    299   // Bit fields for fast objects.
    300   class RepresentationField:      public BitField<uint32_t,           6,  4> {};
    301   class DescriptorPointer:        public BitField<uint32_t, 10,
    302       kDescriptorIndexBitCount> {};  // NOLINT
    303   class FieldIndexField:          public BitField<uint32_t,
    304       10 + kDescriptorIndexBitCount,
    305       kDescriptorIndexBitCount> {};  // NOLINT
    306   // All bits for fast objects must fix in a smi.
    307   STATIC_ASSERT(10 + kDescriptorIndexBitCount + kDescriptorIndexBitCount <= 31);
    308 
    309   static const int kInitialIndex = 1;
    310 
    311  private:
    312   PropertyDetails(int value, int pointer) {
    313     value_ = DescriptorPointer::update(value, pointer);
    314   }
    315   PropertyDetails(int value, Representation representation) {
    316     value_ = RepresentationField::update(
    317         value, EncodeRepresentation(representation));
    318   }
    319   PropertyDetails(int value, PropertyAttributes attributes) {
    320     value_ = AttributesField::update(value, attributes);
    321   }
    322 
    323   uint32_t value_;
    324 };
    325 
    326 } }  // namespace v8::internal
    327 
    328 #endif  // V8_PROPERTY_DETAILS_H_
    329