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.
5 #ifndef V8_LAYOUT_DESCRIPTOR_INL_H_
6 #define V8_LAYOUT_DESCRIPTOR_INL_H_
8 #include "src/layout-descriptor.h"
13 LayoutDescriptor* LayoutDescriptor::FromSmi(Smi* smi) {
14 return LayoutDescriptor::cast(smi);
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);
23 length = GetSlowModeBackingStoreLength(length);
24 return Handle<LayoutDescriptor>::cast(isolate->factory()->NewFixedTypedArray(
25 length, kExternalUint32Array, true));
29 bool LayoutDescriptor::InobjectUnboxedField(int inobject_properties,
30 PropertyDetails details) {
31 if (details.type() != DATA || !details.representation().IsDouble()) {
34 // We care only about in-object properties.
35 return details.field_index() < inobject_properties;
39 LayoutDescriptor* LayoutDescriptor::FastPointerLayout() {
40 return LayoutDescriptor::FromSmi(Smi::FromInt(0));
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())) {
50 *layout_word_index = field_index / kNumberOfBits;
51 CHECK((!IsSmi() && (*layout_word_index < length())) ||
52 (IsSmi() && (*layout_word_index < 1)));
54 *layout_bit_index = field_index % kNumberOfBits;
59 LayoutDescriptor* LayoutDescriptor::SetRawData(int field_index) {
60 return SetTagged(field_index, false);
64 LayoutDescriptor* LayoutDescriptor::SetTagged(int field_index, bool tagged) {
65 int layout_word_index;
68 if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
72 uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
75 uint32_t value = get_scalar(layout_word_index);
77 value &= ~layout_mask;
81 set(layout_word_index, value);
84 uint32_t value = static_cast<uint32_t>(Smi::cast(this)->value());
86 value &= ~layout_mask;
90 return LayoutDescriptor::FromSmi(Smi::FromInt(static_cast<int>(value)));
95 bool LayoutDescriptor::IsTagged(int field_index) {
96 if (IsFastPointerLayout()) return true;
98 int layout_word_index;
101 if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
102 // All bits after Out of bounds queries
105 uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
107 if (IsSlowLayout()) {
108 uint32_t value = get_scalar(layout_word_index);
109 return (value & layout_mask) == 0;
111 uint32_t value = static_cast<uint32_t>(Smi::cast(this)->value());
112 return (value & layout_mask) == 0;
117 bool LayoutDescriptor::IsFastPointerLayout() {
118 return this == FastPointerLayout();
122 bool LayoutDescriptor::IsFastPointerLayout(Object* layout_descriptor) {
123 return layout_descriptor == FastPointerLayout();
127 bool LayoutDescriptor::IsSlowLayout() { return !IsSmi(); }
130 int LayoutDescriptor::capacity() {
131 return IsSlowLayout() ? (length() * kNumberOfBits) : kSmiValueSize;
135 LayoutDescriptor* LayoutDescriptor::cast_gc_safe(Object* object) {
136 if (object->IsSmi()) {
137 // Fast mode layout descriptor.
138 return reinterpret_cast<LayoutDescriptor*>(object);
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();
147 return LayoutDescriptor::cast(object);
151 int LayoutDescriptor::GetSlowModeBackingStoreLength(int length) {
152 length = (length + kNumberOfBits - 1) / kNumberOfBits;
153 DCHECK_LT(0, length);
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.
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;
172 DCHECK_LE(num_descriptors, descriptors->number_of_descriptors());
174 int layout_descriptor_length;
175 const int kMaxWordsPerField = kDoubleSize / kPointerSize;
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;
183 layout_descriptor_length = 0;
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);
194 layout_descriptor_length = Min(layout_descriptor_length, inobject_properties);
195 return layout_descriptor_length;
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();
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()));
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);
218 return layout_descriptor;
222 // LayoutDescriptorHelper is a helper class for querying whether inobject
223 // property at offset is Double or not.
224 LayoutDescriptorHelper::LayoutDescriptorHelper(Map* map)
225 : all_fields_tagged_(true),
227 layout_descriptor_(LayoutDescriptor::FastPointerLayout()) {
228 if (!FLAG_unbox_double_fields) return;
230 layout_descriptor_ = map->layout_descriptor_gc_safe();
231 if (layout_descriptor_->IsFastPointerLayout()) {
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);
240 all_fields_tagged_ = false;
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;
251 return layout_descriptor_->IsTagged(field_index);
253 } // namespace internal
256 #endif // V8_LAYOUT_DESCRIPTOR_INL_H_