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::SetTagged(int field_index, bool tagged) {
60 int layout_word_index;
63 if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
67 uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
70 uint32_t value = get_scalar(layout_word_index);
72 value &= ~layout_mask;
76 set(layout_word_index, value);
79 uint32_t value = static_cast<uint32_t>(Smi::cast(this)->value());
81 value &= ~layout_mask;
85 return LayoutDescriptor::FromSmi(Smi::FromInt(static_cast<int>(value)));
90 bool LayoutDescriptor::IsTagged(int field_index) {
91 if (IsFastPointerLayout()) return true;
93 int layout_word_index;
96 if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
97 // All bits after Out of bounds queries
100 uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
102 if (IsSlowLayout()) {
103 uint32_t value = get_scalar(layout_word_index);
104 return (value & layout_mask) == 0;
106 uint32_t value = static_cast<uint32_t>(Smi::cast(this)->value());
107 return (value & layout_mask) == 0;
112 bool LayoutDescriptor::IsFastPointerLayout() {
113 return this == FastPointerLayout();
117 bool LayoutDescriptor::IsFastPointerLayout(Object* layout_descriptor) {
118 return layout_descriptor == FastPointerLayout();
122 bool LayoutDescriptor::IsSlowLayout() { return !IsSmi(); }
125 int LayoutDescriptor::capacity() {
126 return IsSlowLayout() ? (length() * kNumberOfBits) : kSmiValueSize;
130 LayoutDescriptor* LayoutDescriptor::cast_gc_safe(Object* object) {
131 if (object->IsSmi()) {
132 // Fast mode layout descriptor.
133 return reinterpret_cast<LayoutDescriptor*>(object);
136 // This is a mixed descriptor which is a fixed typed array.
137 MapWord map_word = reinterpret_cast<HeapObject*>(object)->map_word();
138 if (map_word.IsForwardingAddress()) {
139 // Mark-compact has already moved layout descriptor.
140 object = map_word.ToForwardingAddress();
142 return LayoutDescriptor::cast(object);
146 int LayoutDescriptor::GetSlowModeBackingStoreLength(int length) {
147 length = (length + kNumberOfBits - 1) / kNumberOfBits;
148 DCHECK_LT(0, length);
150 if (SmiValuesAre32Bits() && (length & 1)) {
151 // On 64-bit systems if the length is odd then the half-word space would be
152 // lost anyway (due to alignment and the fact that we are allocating
153 // uint32-typed array), so we increase the length of allocated array
154 // to utilize that "lost" space which could also help to avoid layout
155 // descriptor reallocations.
162 int LayoutDescriptor::CalculateCapacity(Map* map, DescriptorArray* descriptors,
163 int num_descriptors) {
164 int inobject_properties = map->inobject_properties();
165 if (inobject_properties == 0) return 0;
167 DCHECK_LE(num_descriptors, descriptors->number_of_descriptors());
169 int layout_descriptor_length;
170 const int kMaxWordsPerField = kDoubleSize / kPointerSize;
172 if (num_descriptors <= kSmiValueSize / kMaxWordsPerField) {
173 // Even in the "worst" case (all fields are doubles) it would fit into
174 // a Smi, so no need to calculate length.
175 layout_descriptor_length = kSmiValueSize;
178 layout_descriptor_length = 0;
180 for (int i = 0; i < num_descriptors; i++) {
181 PropertyDetails details = descriptors->GetDetails(i);
182 if (!InobjectUnboxedField(inobject_properties, details)) continue;
183 int field_index = details.field_index();
184 int field_width_in_words = details.field_width_in_words();
185 layout_descriptor_length =
186 Max(layout_descriptor_length, field_index + field_width_in_words);
189 layout_descriptor_length = Min(layout_descriptor_length, inobject_properties);
190 return layout_descriptor_length;
194 LayoutDescriptor* LayoutDescriptor::Initialize(
195 LayoutDescriptor* layout_descriptor, Map* map, DescriptorArray* descriptors,
196 int num_descriptors) {
197 DisallowHeapAllocation no_allocation;
198 int inobject_properties = map->inobject_properties();
200 for (int i = 0; i < num_descriptors; i++) {
201 PropertyDetails details = descriptors->GetDetails(i);
202 if (!InobjectUnboxedField(inobject_properties, details)) {
203 DCHECK(details.location() != kField ||
204 layout_descriptor->IsTagged(details.field_index()));
207 int field_index = details.field_index();
208 layout_descriptor = layout_descriptor->SetRawData(field_index);
209 if (details.field_width_in_words() > 1) {
210 layout_descriptor = layout_descriptor->SetRawData(field_index + 1);
213 return layout_descriptor;
217 // InobjectPropertiesHelper is a helper class for querying whether inobject
218 // property at offset is Double or not.
219 LayoutDescriptorHelper::LayoutDescriptorHelper(Map* map)
220 : all_fields_tagged_(true),
222 layout_descriptor_(LayoutDescriptor::FastPointerLayout()) {
223 if (!FLAG_unbox_double_fields) return;
225 layout_descriptor_ = map->layout_descriptor_gc_safe();
226 if (layout_descriptor_->IsFastPointerLayout()) {
230 int inobject_properties = map->inobject_properties();
231 DCHECK(inobject_properties > 0);
232 header_size_ = map->instance_size() - (inobject_properties * kPointerSize);
233 DCHECK(header_size_ >= 0);
235 all_fields_tagged_ = false;
239 bool LayoutDescriptorHelper::IsTagged(int offset_in_bytes) {
240 DCHECK(IsAligned(offset_in_bytes, kPointerSize));
241 if (all_fields_tagged_) return true;
242 // Object headers do not contain non-tagged fields.
243 if (offset_in_bytes < header_size_) return true;
244 int field_index = (offset_in_bytes - header_size_) / kPointerSize;
246 return layout_descriptor_->IsTagged(field_index);
249 } // namespace v8::internal
251 #endif // V8_LAYOUT_DESCRIPTOR_INL_H_