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     kSmi,
     86     kInteger32,
     87     kDouble,
     88     kHeapObject,
     89     kTagged,
     90     kExternal,
     91     kNumRepresentations
     92   };
     93 
     94   Representation() : kind_(kNone) { }
     95 
     96   static Representation None() { return Representation(kNone); }
     97   static Representation Tagged() { return Representation(kTagged); }
     98   static Representation Smi() { return Representation(kSmi); }
     99   static Representation Integer32() { return Representation(kInteger32); }
    100   static Representation Double() { return Representation(kDouble); }
    101   static Representation HeapObject() { return Representation(kHeapObject); }
    102   static Representation External() { return Representation(kExternal); }
    103 
    104   static Representation FromKind(Kind kind) { return Representation(kind); }
    105 
    106   // TODO(rossberg): this should die eventually.
    107   static Representation FromType(TypeInfo info);
    108   static Representation FromType(Handle<Type> type);
    109 
    110   bool Equals(const Representation& other) const {
    111     return kind_ == other.kind_;
    112   }
    113 
    114   bool IsCompatibleForLoad(const Representation& other) const {
    115     return (IsDouble() && other.IsDouble()) ||
    116         (!IsDouble() && !other.IsDouble());
    117   }
    118 
    119   bool IsCompatibleForStore(const Representation& other) const {
    120     return Equals(other);
    121   }
    122 
    123   bool is_more_general_than(const Representation& other) const {
    124     ASSERT(kind_ != kExternal);
    125     ASSERT(other.kind_ != kExternal);
    126     if (IsHeapObject()) return other.IsDouble() || other.IsNone();
    127     return kind_ > other.kind_;
    128   }
    129 
    130   bool fits_into(const Representation& other) const {
    131     return other.is_more_general_than(*this) || other.Equals(*this);
    132   }
    133 
    134   Representation generalize(Representation other) {
    135     if (other.fits_into(*this)) return *this;
    136     if (other.is_more_general_than(*this)) return other;
    137     return Representation::Tagged();
    138   }
    139 
    140   Kind kind() const { return static_cast<Kind>(kind_); }
    141   bool IsNone() const { return kind_ == kNone; }
    142   bool IsTagged() const { return kind_ == kTagged; }
    143   bool IsSmi() const { return kind_ == kSmi; }
    144   bool IsSmiOrTagged() const { return IsSmi() || IsTagged(); }
    145   bool IsInteger32() const { return kind_ == kInteger32; }
    146   bool IsSmiOrInteger32() const { return IsSmi() || IsInteger32(); }
    147   bool IsDouble() const { return kind_ == kDouble; }
    148   bool IsHeapObject() const { return kind_ == kHeapObject; }
    149   bool IsExternal() const { return kind_ == kExternal; }
    150   bool IsSpecialization() const {
    151     return kind_ == kInteger32 || kind_ == kDouble;
    152   }
    153   const char* Mnemonic() const;
    154 
    155  private:
    156   explicit Representation(Kind k) : kind_(k) { }
    157 
    158   // Make sure kind fits in int8.
    159   STATIC_ASSERT(kNumRepresentations <= (1 << kBitsPerByte));
    160 
    161   int8_t kind_;
    162 };
    163 
    164 
    165 // PropertyDetails captures type and attributes for a property.
    166 // They are used both in property dictionaries and instance descriptors.
    167 class PropertyDetails BASE_EMBEDDED {
    168  public:
    169   PropertyDetails(PropertyAttributes attributes,
    170                   PropertyType type,
    171                   int index) {
    172     value_ = TypeField::encode(type)
    173         | AttributesField::encode(attributes)
    174         | DictionaryStorageField::encode(index);
    175 
    176     ASSERT(type == this->type());
    177     ASSERT(attributes == this->attributes());
    178   }
    179 
    180   PropertyDetails(PropertyAttributes attributes,
    181                   PropertyType type,
    182                   Representation representation,
    183                   int field_index = 0) {
    184     value_ = TypeField::encode(type)
    185         | AttributesField::encode(attributes)
    186         | RepresentationField::encode(EncodeRepresentation(representation))
    187         | FieldIndexField::encode(field_index);
    188   }
    189 
    190   int pointer() { return DescriptorPointer::decode(value_); }
    191 
    192   PropertyDetails set_pointer(int i) { return PropertyDetails(value_, i); }
    193 
    194   PropertyDetails CopyWithRepresentation(Representation representation) {
    195     return PropertyDetails(value_, representation);
    196   }
    197   PropertyDetails CopyAddAttributes(PropertyAttributes new_attributes) {
    198     new_attributes =
    199         static_cast<PropertyAttributes>(attributes() | new_attributes);
    200     return PropertyDetails(value_, new_attributes);
    201   }
    202 
    203   // Conversion for storing details as Object*.
    204   explicit inline PropertyDetails(Smi* smi);
    205   inline Smi* AsSmi();
    206 
    207   static uint8_t EncodeRepresentation(Representation representation) {
    208     return representation.kind();
    209   }
    210 
    211   static Representation DecodeRepresentation(uint32_t bits) {
    212     return Representation::FromKind(static_cast<Representation::Kind>(bits));
    213   }
    214 
    215   PropertyType type() { return TypeField::decode(value_); }
    216 
    217   PropertyAttributes attributes() const {
    218     return AttributesField::decode(value_);
    219   }
    220 
    221   int dictionary_index() {
    222     return DictionaryStorageField::decode(value_);
    223   }
    224 
    225   Representation representation() {
    226     ASSERT(type() != NORMAL);
    227     return DecodeRepresentation(RepresentationField::decode(value_));
    228   }
    229 
    230   int  field_index() {
    231     return FieldIndexField::decode(value_);
    232   }
    233 
    234   inline PropertyDetails AsDeleted();
    235 
    236   static bool IsValidIndex(int index) {
    237     return DictionaryStorageField::is_valid(index);
    238   }
    239 
    240   bool IsReadOnly() const { return (attributes() & READ_ONLY) != 0; }
    241   bool IsDontDelete() const { return (attributes() & DONT_DELETE) != 0; }
    242   bool IsDontEnum() const { return (attributes() & DONT_ENUM) != 0; }
    243   bool IsDeleted() const { return DeletedField::decode(value_) != 0;}
    244 
    245   // Bit fields in value_ (type, shift, size). Must be public so the
    246   // constants can be embedded in generated code.
    247   class TypeField:                public BitField<PropertyType,       0,  3> {};
    248   class AttributesField:          public BitField<PropertyAttributes, 3,  3> {};
    249 
    250   // Bit fields for normalized objects.
    251   class DeletedField:             public BitField<uint32_t,           6,  1> {};
    252   class DictionaryStorageField:   public BitField<uint32_t,           7, 24> {};
    253 
    254   // Bit fields for fast objects.
    255   class DescriptorPointer:        public BitField<uint32_t,           6, 11> {};
    256   class RepresentationField:      public BitField<uint32_t,          17,  3> {};
    257   class FieldIndexField:          public BitField<uint32_t,          20, 11> {};
    258 
    259   static const int kInitialIndex = 1;
    260 
    261  private:
    262   PropertyDetails(int value, int pointer) {
    263     value_ = DescriptorPointer::update(value, pointer);
    264   }
    265   PropertyDetails(int value, Representation representation) {
    266     value_ = RepresentationField::update(
    267         value, EncodeRepresentation(representation));
    268   }
    269   PropertyDetails(int value, PropertyAttributes attributes) {
    270     value_ = AttributesField::update(value, attributes);
    271   }
    272 
    273   uint32_t value_;
    274 };
    275 
    276 } }  // namespace v8::internal
    277 
    278 #endif  // V8_PROPERTY_DETAILS_H_
    279