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/fixed-array.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. GC uses layout descriptors to iterate objects. Avoid heap pointers 25 // in a layout descriptor because they can lead to data races in GC when 26 // GC moves objects in parallel. 27 class LayoutDescriptor : public ByteArray { 28 public: 29 V8_INLINE bool IsTagged(int field_index); 30 31 // Queries the contiguous region of fields that are either tagged or not. 32 // Returns true if the given field is tagged or false otherwise and writes 33 // the length of the contiguous region to |out_sequence_length|. 34 // If the sequence is longer than |max_sequence_length| then 35 // |out_sequence_length| is set to |max_sequence_length|. 36 bool IsTagged(int field_index, int max_sequence_length, 37 int* out_sequence_length); 38 39 // Returns true if this is a layout of the object having only tagged fields. 40 V8_INLINE bool IsFastPointerLayout(); 41 V8_INLINE static bool IsFastPointerLayout(Object* layout_descriptor); 42 43 // Returns true if the layout descriptor is in non-Smi form. 44 V8_INLINE bool IsSlowLayout(); 45 46 V8_INLINE static LayoutDescriptor* cast(Object* object); 47 V8_INLINE static const LayoutDescriptor* cast(const Object* object); 48 49 V8_INLINE static LayoutDescriptor* cast_gc_safe(Object* object); 50 51 // Builds layout descriptor optimized for given |map| by |num_descriptors| 52 // elements of given descriptors array. The |map|'s descriptors could be 53 // different. 54 static Handle<LayoutDescriptor> New(Isolate* isolate, Handle<Map> map, 55 Handle<DescriptorArray> descriptors, 56 int num_descriptors); 57 58 // Modifies |map|'s layout descriptor or creates a new one if necessary by 59 // appending property with |details| to it. 60 static Handle<LayoutDescriptor> ShareAppend(Isolate* isolate, Handle<Map> map, 61 PropertyDetails details); 62 63 // Creates new layout descriptor by appending property with |details| to 64 // |map|'s layout descriptor and if it is still fast then returns it. 65 // Otherwise the |full_layout_descriptor| is returned. 66 static Handle<LayoutDescriptor> AppendIfFastOrUseFull( 67 Isolate* isolate, Handle<Map> map, PropertyDetails details, 68 Handle<LayoutDescriptor> full_layout_descriptor); 69 70 // Layout descriptor that corresponds to an object all fields of which are 71 // tagged (FastPointerLayout). 72 V8_INLINE static LayoutDescriptor* FastPointerLayout(); 73 74 // Check that this layout descriptor corresponds to given map. 75 bool IsConsistentWithMap(Map* map, bool check_tail = false); 76 77 // Trims this layout descriptor to given number of descriptors. This happens 78 // only when corresponding descriptors array is trimmed. 79 // The layout descriptor could be trimmed if it was slow or it could 80 // become fast. 81 LayoutDescriptor* Trim(Heap* heap, Map* map, DescriptorArray* descriptors, 82 int num_descriptors); 83 84 #ifdef OBJECT_PRINT 85 // For our gdb macros, we should perhaps change these in the future. 86 void Print(); 87 88 void ShortPrint(std::ostream& os); 89 void Print(std::ostream& os); // NOLINT 90 #endif 91 92 // Capacity of layout descriptors in bits. 93 V8_INLINE int capacity(); 94 95 static Handle<LayoutDescriptor> NewForTesting(Isolate* isolate, int length); 96 LayoutDescriptor* SetTaggedForTesting(int field_index, bool tagged); 97 98 private: 99 // Exclude sign-bit to simplify encoding. 100 static constexpr int kBitsInSmiLayout = 101 SmiValuesAre32Bits() ? 32 : kSmiValueSize - 1; 102 103 static const int kBitsPerLayoutWord = 32; 104 105 V8_INLINE int number_of_layout_words(); 106 V8_INLINE uint32_t get_layout_word(int index) const; 107 V8_INLINE void set_layout_word(int index, uint32_t value); 108 109 V8_INLINE static Handle<LayoutDescriptor> New(Isolate* isolate, int length); 110 V8_INLINE static LayoutDescriptor* FromSmi(Smi* smi); 111 112 V8_INLINE static bool InobjectUnboxedField(int inobject_properties, 113 PropertyDetails details); 114 115 // Calculates minimal layout descriptor capacity required for given 116 // |map|, |descriptors| and |num_descriptors|. 117 V8_INLINE static int CalculateCapacity(Map* map, DescriptorArray* descriptors, 118 int num_descriptors); 119 120 // Calculates the length of the slow-mode backing store array by given layout 121 // descriptor length. 122 V8_INLINE static int GetSlowModeBackingStoreLength(int length); 123 124 // Fills in clean |layout_descriptor| according to given |map|, |descriptors| 125 // and |num_descriptors|. 126 V8_INLINE static LayoutDescriptor* Initialize( 127 LayoutDescriptor* layout_descriptor, Map* map, 128 DescriptorArray* descriptors, int num_descriptors); 129 130 static Handle<LayoutDescriptor> EnsureCapacity( 131 Isolate* isolate, Handle<LayoutDescriptor> layout_descriptor, 132 int new_capacity); 133 134 // Returns false if requested field_index is out of bounds. 135 V8_INLINE bool GetIndexes(int field_index, int* layout_word_index, 136 int* layout_bit_index); 137 138 V8_INLINE V8_WARN_UNUSED_RESULT LayoutDescriptor* SetRawData(int field_index); 139 140 V8_INLINE V8_WARN_UNUSED_RESULT LayoutDescriptor* SetTagged(int field_index, 141 bool tagged); 142 }; 143 144 145 // LayoutDescriptorHelper is a helper class for querying layout descriptor 146 // about whether the field at given offset is tagged or not. 147 class LayoutDescriptorHelper { 148 public: 149 inline explicit LayoutDescriptorHelper(Map* map); 150 all_fields_tagged()151 bool all_fields_tagged() { return all_fields_tagged_; } 152 inline bool IsTagged(int offset_in_bytes); 153 154 // Queries the contiguous region of fields that are either tagged or not. 155 // Returns true if fields starting at |offset_in_bytes| are tagged or false 156 // otherwise and writes the offset of the end of the contiguous region to 157 // |out_end_of_contiguous_region_offset|. The |end_offset| value is the 158 // upper bound for |out_end_of_contiguous_region_offset|. 159 bool IsTagged(int offset_in_bytes, int end_offset, 160 int* out_end_of_contiguous_region_offset); 161 162 private: 163 bool all_fields_tagged_; 164 int header_size_; 165 LayoutDescriptor* layout_descriptor_; 166 }; 167 } // namespace internal 168 } // namespace v8 169 170 #endif // V8_LAYOUT_DESCRIPTOR_H_ 171