Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 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_LAYOUT_DESCRIPTOR_H_ |
| 6 | #define V8_LAYOUT_DESCRIPTOR_H_ |
| 7 | |
| 8 | #include <iosfwd> |
| 9 | |
| 10 | #include "src/objects.h" |
| 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
| 15 | // LayoutDescriptor is a bit vector defining which fields contain non-tagged |
| 16 | // values. It could either be a fixed typed array (slow form) or a Smi |
| 17 | // if the length fits (fast form). |
| 18 | // Each bit in the layout represents a FIELD. The bits are referenced by |
| 19 | // field_index which is a field number. If the bit is set then the corresponding |
| 20 | // field contains a non-tagged value and therefore must be skipped by GC. |
| 21 | // Otherwise the field is considered tagged. If the queried bit lays "outside" |
| 22 | // of the descriptor then the field is also considered tagged. |
| 23 | // Once a layout descriptor is created it is allowed only to append properties |
| 24 | // to it. |
| 25 | class LayoutDescriptor : public FixedTypedArray<Uint32ArrayTraits> { |
| 26 | public: |
| 27 | V8_INLINE bool IsTagged(int field_index); |
| 28 | |
| 29 | // Queries the contiguous region of fields that are either tagged or not. |
| 30 | // Returns true if the given field is tagged or false otherwise and writes |
| 31 | // the length of the contiguous region to |out_sequence_length|. |
| 32 | // If the sequence is longer than |max_sequence_length| then |
| 33 | // |out_sequence_length| is set to |max_sequence_length|. |
| 34 | bool IsTagged(int field_index, int max_sequence_length, |
| 35 | int* out_sequence_length); |
| 36 | |
| 37 | // Returns true if this is a layout of the object having only tagged fields. |
| 38 | V8_INLINE bool IsFastPointerLayout(); |
| 39 | V8_INLINE static bool IsFastPointerLayout(Object* layout_descriptor); |
| 40 | |
| 41 | // Returns true if the layout descriptor is in non-Smi form. |
| 42 | V8_INLINE bool IsSlowLayout(); |
| 43 | |
| 44 | V8_INLINE static LayoutDescriptor* cast(Object* object); |
| 45 | V8_INLINE static const LayoutDescriptor* cast(const Object* object); |
| 46 | |
| 47 | V8_INLINE static LayoutDescriptor* cast_gc_safe(Object* object); |
| 48 | |
| 49 | // Builds layout descriptor optimized for given |map| by |num_descriptors| |
| 50 | // elements of given descriptors array. The |map|'s descriptors could be |
| 51 | // different. |
| 52 | static Handle<LayoutDescriptor> New(Handle<Map> map, |
| 53 | Handle<DescriptorArray> descriptors, |
| 54 | int num_descriptors); |
| 55 | |
| 56 | // Creates new layout descriptor by appending property with |details| to |
| 57 | // |map|'s layout descriptor. |
| 58 | static Handle<LayoutDescriptor> Append(Handle<Map> map, |
| 59 | PropertyDetails details); |
| 60 | |
| 61 | // Creates new layout descriptor by appending property with |details| to |
| 62 | // |map|'s layout descriptor and if it is still fast then returns it. |
| 63 | // Otherwise the |full_layout_descriptor| is returned. |
| 64 | static Handle<LayoutDescriptor> AppendIfFastOrUseFull( |
| 65 | Handle<Map> map, PropertyDetails details, |
| 66 | Handle<LayoutDescriptor> full_layout_descriptor); |
| 67 | |
| 68 | // Layout descriptor that corresponds to an object all fields of which are |
| 69 | // tagged (FastPointerLayout). |
| 70 | V8_INLINE static LayoutDescriptor* FastPointerLayout(); |
| 71 | |
| 72 | #ifdef DEBUG |
| 73 | // Check that this layout descriptor corresponds to given map. |
| 74 | bool IsConsistentWithMap(Map* map); |
| 75 | #endif |
| 76 | |
| 77 | #ifdef OBJECT_PRINT |
| 78 | // For our gdb macros, we should perhaps change these in the future. |
| 79 | void Print(); |
| 80 | |
| 81 | void Print(std::ostream& os); // NOLINT |
| 82 | #endif |
| 83 | |
| 84 | // Capacity of layout descriptors in bits. |
| 85 | V8_INLINE int capacity(); |
| 86 | |
| 87 | static Handle<LayoutDescriptor> NewForTesting(Isolate* isolate, int length); |
| 88 | LayoutDescriptor* SetTaggedForTesting(int field_index, bool tagged); |
| 89 | |
| 90 | private: |
| 91 | static const int kNumberOfBits = 32; |
| 92 | |
| 93 | V8_INLINE static Handle<LayoutDescriptor> New(Isolate* isolate, int length); |
| 94 | V8_INLINE static LayoutDescriptor* FromSmi(Smi* smi); |
| 95 | |
| 96 | V8_INLINE static bool InobjectUnboxedField(int inobject_properties, |
| 97 | PropertyDetails details); |
| 98 | |
| 99 | static Handle<LayoutDescriptor> EnsureCapacity( |
| 100 | Isolate* isolate, Handle<LayoutDescriptor> layout_descriptor, |
| 101 | int new_capacity); |
| 102 | |
| 103 | // Returns false if requested field_index is out of bounds. |
| 104 | V8_INLINE bool GetIndexes(int field_index, int* layout_word_index, |
| 105 | int* layout_bit_index); |
| 106 | |
| 107 | V8_INLINE MUST_USE_RESULT LayoutDescriptor* SetRawData(int field_index) { |
| 108 | return SetTagged(field_index, false); |
| 109 | } |
| 110 | |
| 111 | V8_INLINE MUST_USE_RESULT LayoutDescriptor* SetTagged(int field_index, |
| 112 | bool tagged); |
| 113 | }; |
| 114 | |
| 115 | |
| 116 | // LayoutDescriptorHelper is a helper class for querying layout descriptor |
| 117 | // about whether the field at given offset is tagged or not. |
| 118 | class LayoutDescriptorHelper { |
| 119 | public: |
| 120 | inline explicit LayoutDescriptorHelper(Map* map); |
| 121 | |
| 122 | bool all_fields_tagged() { return all_fields_tagged_; } |
| 123 | inline bool IsTagged(int offset_in_bytes); |
| 124 | |
| 125 | // Queries the contiguous region of fields that are either tagged or not. |
| 126 | // Returns true if fields starting at |offset_in_bytes| are tagged or false |
| 127 | // otherwise and writes the offset of the end of the contiguous region to |
| 128 | // |out_end_of_contiguous_region_offset|. The |end_offset| value is the |
| 129 | // upper bound for |out_end_of_contiguous_region_offset|. |
| 130 | bool IsTagged(int offset_in_bytes, int end_offset, |
| 131 | int* out_end_of_contiguous_region_offset); |
| 132 | |
| 133 | private: |
| 134 | bool all_fields_tagged_; |
| 135 | int header_size_; |
| 136 | LayoutDescriptor* layout_descriptor_; |
| 137 | }; |
| 138 | } |
| 139 | } // namespace v8::internal |
| 140 | |
| 141 | #endif // V8_LAYOUT_DESCRIPTOR_H_ |