• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INL_H_
6 #define V8_LAYOUT_DESCRIPTOR_INL_H_
7 
8 #include "src/layout-descriptor.h"
9 
10 namespace v8 {
11 namespace internal {
12 
FromSmi(Smi * smi)13 LayoutDescriptor* LayoutDescriptor::FromSmi(Smi* smi) {
14   return LayoutDescriptor::cast(smi);
15 }
16 
17 
New(Isolate * isolate,int length)18 Handle<LayoutDescriptor> LayoutDescriptor::New(Isolate* isolate, int length) {
19   if (length <= kSmiValueSize) {
20     // The whole bit vector fits into a smi.
21     return handle(LayoutDescriptor::FromSmi(Smi::FromInt(0)), isolate);
22   }
23   length = GetSlowModeBackingStoreLength(length);
24   return Handle<LayoutDescriptor>::cast(isolate->factory()->NewFixedTypedArray(
25       length, kExternalUint32Array, true));
26 }
27 
28 
InobjectUnboxedField(int inobject_properties,PropertyDetails details)29 bool LayoutDescriptor::InobjectUnboxedField(int inobject_properties,
30                                             PropertyDetails details) {
31   if (details.type() != DATA || !details.representation().IsDouble()) {
32     return false;
33   }
34   // We care only about in-object properties.
35   return details.field_index() < inobject_properties;
36 }
37 
38 
FastPointerLayout()39 LayoutDescriptor* LayoutDescriptor::FastPointerLayout() {
40   return LayoutDescriptor::FromSmi(Smi::FromInt(0));
41 }
42 
43 
GetIndexes(int field_index,int * layout_word_index,int * layout_bit_index)44 bool LayoutDescriptor::GetIndexes(int field_index, int* layout_word_index,
45                                   int* layout_bit_index) {
46   if (static_cast<unsigned>(field_index) >= static_cast<unsigned>(capacity())) {
47     return false;
48   }
49 
50   *layout_word_index = field_index / kNumberOfBits;
51   CHECK((!IsSmi() && (*layout_word_index < length())) ||
52         (IsSmi() && (*layout_word_index < 1)));
53 
54   *layout_bit_index = field_index % kNumberOfBits;
55   return true;
56 }
57 
58 
SetRawData(int field_index)59 LayoutDescriptor* LayoutDescriptor::SetRawData(int field_index) {
60   return SetTagged(field_index, false);
61 }
62 
63 
SetTagged(int field_index,bool tagged)64 LayoutDescriptor* LayoutDescriptor::SetTagged(int field_index, bool tagged) {
65   int layout_word_index = 0;
66   int layout_bit_index = 0;
67 
68   if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
69     CHECK(false);
70     return this;
71   }
72   uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
73 
74   if (IsSlowLayout()) {
75     uint32_t value = get_scalar(layout_word_index);
76     if (tagged) {
77       value &= ~layout_mask;
78     } else {
79       value |= layout_mask;
80     }
81     set(layout_word_index, value);
82     return this;
83   } else {
84     uint32_t value = static_cast<uint32_t>(Smi::cast(this)->value());
85     if (tagged) {
86       value &= ~layout_mask;
87     } else {
88       value |= layout_mask;
89     }
90     return LayoutDescriptor::FromSmi(Smi::FromInt(static_cast<int>(value)));
91   }
92 }
93 
94 
IsTagged(int field_index)95 bool LayoutDescriptor::IsTagged(int field_index) {
96   if (IsFastPointerLayout()) return true;
97 
98   int layout_word_index;
99   int layout_bit_index;
100 
101   if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
102     // All bits after Out of bounds queries
103     return true;
104   }
105   uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
106 
107   if (IsSlowLayout()) {
108     uint32_t value = get_scalar(layout_word_index);
109     return (value & layout_mask) == 0;
110   } else {
111     uint32_t value = static_cast<uint32_t>(Smi::cast(this)->value());
112     return (value & layout_mask) == 0;
113   }
114 }
115 
116 
IsFastPointerLayout()117 bool LayoutDescriptor::IsFastPointerLayout() {
118   return this == FastPointerLayout();
119 }
120 
121 
IsFastPointerLayout(Object * layout_descriptor)122 bool LayoutDescriptor::IsFastPointerLayout(Object* layout_descriptor) {
123   return layout_descriptor == FastPointerLayout();
124 }
125 
126 
IsSlowLayout()127 bool LayoutDescriptor::IsSlowLayout() { return !IsSmi(); }
128 
129 
capacity()130 int LayoutDescriptor::capacity() {
131   return IsSlowLayout() ? (length() * kNumberOfBits) : kSmiValueSize;
132 }
133 
134 
cast_gc_safe(Object * object)135 LayoutDescriptor* LayoutDescriptor::cast_gc_safe(Object* object) {
136   if (object->IsSmi()) {
137     // Fast mode layout descriptor.
138     return reinterpret_cast<LayoutDescriptor*>(object);
139   }
140 
141   // This is a mixed descriptor which is a fixed typed array.
142   MapWord map_word = reinterpret_cast<HeapObject*>(object)->map_word();
143   if (map_word.IsForwardingAddress()) {
144     // Mark-compact has already moved layout descriptor.
145     object = map_word.ToForwardingAddress();
146   }
147   return LayoutDescriptor::cast(object);
148 }
149 
150 
GetSlowModeBackingStoreLength(int length)151 int LayoutDescriptor::GetSlowModeBackingStoreLength(int length) {
152   length = (length + kNumberOfBits - 1) / kNumberOfBits;
153   DCHECK_LT(0, length);
154 
155   if (SmiValuesAre32Bits() && (length & 1)) {
156     // On 64-bit systems if the length is odd then the half-word space would be
157     // lost anyway (due to alignment and the fact that we are allocating
158     // uint32-typed array), so we increase the length of allocated array
159     // to utilize that "lost" space which could also help to avoid layout
160     // descriptor reallocations.
161     ++length;
162   }
163   return length;
164 }
165 
166 
CalculateCapacity(Map * map,DescriptorArray * descriptors,int num_descriptors)167 int LayoutDescriptor::CalculateCapacity(Map* map, DescriptorArray* descriptors,
168                                         int num_descriptors) {
169   int inobject_properties = map->GetInObjectProperties();
170   if (inobject_properties == 0) return 0;
171 
172   DCHECK_LE(num_descriptors, descriptors->number_of_descriptors());
173 
174   int layout_descriptor_length;
175   const int kMaxWordsPerField = kDoubleSize / kPointerSize;
176 
177   if (num_descriptors <= kSmiValueSize / kMaxWordsPerField) {
178     // Even in the "worst" case (all fields are doubles) it would fit into
179     // a Smi, so no need to calculate length.
180     layout_descriptor_length = kSmiValueSize;
181 
182   } else {
183     layout_descriptor_length = 0;
184 
185     for (int i = 0; i < num_descriptors; i++) {
186       PropertyDetails details = descriptors->GetDetails(i);
187       if (!InobjectUnboxedField(inobject_properties, details)) continue;
188       int field_index = details.field_index();
189       int field_width_in_words = details.field_width_in_words();
190       layout_descriptor_length =
191           Max(layout_descriptor_length, field_index + field_width_in_words);
192     }
193   }
194   layout_descriptor_length = Min(layout_descriptor_length, inobject_properties);
195   return layout_descriptor_length;
196 }
197 
198 
Initialize(LayoutDescriptor * layout_descriptor,Map * map,DescriptorArray * descriptors,int num_descriptors)199 LayoutDescriptor* LayoutDescriptor::Initialize(
200     LayoutDescriptor* layout_descriptor, Map* map, DescriptorArray* descriptors,
201     int num_descriptors) {
202   DisallowHeapAllocation no_allocation;
203   int inobject_properties = map->GetInObjectProperties();
204 
205   for (int i = 0; i < num_descriptors; i++) {
206     PropertyDetails details = descriptors->GetDetails(i);
207     if (!InobjectUnboxedField(inobject_properties, details)) {
208       DCHECK(details.location() != kField ||
209              layout_descriptor->IsTagged(details.field_index()));
210       continue;
211     }
212     int field_index = details.field_index();
213     layout_descriptor = layout_descriptor->SetRawData(field_index);
214     if (details.field_width_in_words() > 1) {
215       layout_descriptor = layout_descriptor->SetRawData(field_index + 1);
216     }
217   }
218   return layout_descriptor;
219 }
220 
221 
222 // LayoutDescriptorHelper is a helper class for querying whether inobject
223 // property at offset is Double or not.
LayoutDescriptorHelper(Map * map)224 LayoutDescriptorHelper::LayoutDescriptorHelper(Map* map)
225     : all_fields_tagged_(true),
226       header_size_(0),
227       layout_descriptor_(LayoutDescriptor::FastPointerLayout()) {
228   if (!FLAG_unbox_double_fields) return;
229 
230   layout_descriptor_ = map->layout_descriptor_gc_safe();
231   if (layout_descriptor_->IsFastPointerLayout()) {
232     return;
233   }
234 
235   int inobject_properties = map->GetInObjectProperties();
236   DCHECK(inobject_properties > 0);
237   header_size_ = map->instance_size() - (inobject_properties * kPointerSize);
238   DCHECK(header_size_ >= 0);
239 
240   all_fields_tagged_ = false;
241 }
242 
243 
IsTagged(int offset_in_bytes)244 bool LayoutDescriptorHelper::IsTagged(int offset_in_bytes) {
245   DCHECK(IsAligned(offset_in_bytes, kPointerSize));
246   if (all_fields_tagged_) return true;
247   // Object headers do not contain non-tagged fields.
248   if (offset_in_bytes < header_size_) return true;
249   int field_index = (offset_in_bytes - header_size_) / kPointerSize;
250 
251   return layout_descriptor_->IsTagged(field_index);
252 }
253 }  // namespace internal
254 }  // namespace v8
255 
256 #endif  // V8_LAYOUT_DESCRIPTOR_INL_H_
257