layout-descriptor-inl.h 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_LAYOUT_DESCRIPTOR_INL_H_
#define V8_LAYOUT_DESCRIPTOR_INL_H_

#include "src/layout-descriptor.h"

namespace v8 {
namespace internal {

LayoutDescriptor* LayoutDescriptor::FromSmi(Smi* smi) {
  return LayoutDescriptor::cast(smi);
}


Handle<LayoutDescriptor> LayoutDescriptor::New(Isolate* isolate, int length) {
  if (length <= kSmiValueSize) {
    // The whole bit vector fits into a smi.
21
    return handle(LayoutDescriptor::FromSmi(Smi::kZero), isolate);
22
  }
23 24
  int backing_store_length = GetSlowModeBackingStoreLength(length);
  Handle<LayoutDescriptor> result = Handle<LayoutDescriptor>::cast(
25
      isolate->factory()->NewByteArray(backing_store_length, TENURED));
26 27
  memset(result->GetDataStartAddress(), 0, result->DataSize());
  return result;
28 29 30 31 32
}


bool LayoutDescriptor::InobjectUnboxedField(int inobject_properties,
                                            PropertyDetails details) {
33
  if (details.location() != kField || !details.representation().IsDouble()) {
34 35 36 37 38 39 40 41
    return false;
  }
  // We care only about in-object properties.
  return details.field_index() < inobject_properties;
}


LayoutDescriptor* LayoutDescriptor::FastPointerLayout() {
42
  return LayoutDescriptor::FromSmi(Smi::kZero);
43 44 45 46
}


bool LayoutDescriptor::GetIndexes(int field_index, int* layout_word_index,
47
                                  int* layout_bit_index) {
48 49 50 51
  if (static_cast<unsigned>(field_index) >= static_cast<unsigned>(capacity())) {
    return false;
  }

52
  *layout_word_index = field_index / kBitsPerLayoutWord;
53
  CHECK((!IsSmi() && (*layout_word_index < length())) ||
54 55
        (IsSmi() && (*layout_word_index < 1)));

56
  *layout_bit_index = field_index % kBitsPerLayoutWord;
57 58 59 60
  return true;
}


61 62 63 64 65
LayoutDescriptor* LayoutDescriptor::SetRawData(int field_index) {
  return SetTagged(field_index, false);
}


66
LayoutDescriptor* LayoutDescriptor::SetTagged(int field_index, bool tagged) {
67 68
  int layout_word_index = 0;
  int layout_bit_index = 0;
69

70
  CHECK(GetIndexes(field_index, &layout_word_index, &layout_bit_index));
71
  uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
72 73

  if (IsSlowLayout()) {
74
    uint32_t value = get_layout_word(layout_word_index);
75 76 77 78 79
    if (tagged) {
      value &= ~layout_mask;
    } else {
      value |= layout_mask;
    }
80
    set_layout_word(layout_word_index, value);
81 82
    return this;
  } else {
jgruber's avatar
jgruber committed
83
    uint32_t value = static_cast<uint32_t>(Smi::ToInt(this));
84 85 86 87 88 89 90 91 92 93 94 95 96 97
    if (tagged) {
      value &= ~layout_mask;
    } else {
      value |= layout_mask;
    }
    return LayoutDescriptor::FromSmi(Smi::FromInt(static_cast<int>(value)));
  }
}


bool LayoutDescriptor::IsTagged(int field_index) {
  if (IsFastPointerLayout()) return true;

  int layout_word_index;
98
  int layout_bit_index;
99

100
  if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
101 102 103
    // All bits after Out of bounds queries
    return true;
  }
104
  uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
105 106

  if (IsSlowLayout()) {
107
    uint32_t value = get_layout_word(layout_word_index);
108 109
    return (value & layout_mask) == 0;
  } else {
jgruber's avatar
jgruber committed
110
    uint32_t value = static_cast<uint32_t>(Smi::ToInt(this));
111 112 113 114 115 116
    return (value & layout_mask) == 0;
  }
}


bool LayoutDescriptor::IsFastPointerLayout() {
117 118 119 120 121 122
  return this == FastPointerLayout();
}


bool LayoutDescriptor::IsFastPointerLayout(Object* layout_descriptor) {
  return layout_descriptor == FastPointerLayout();
123 124 125 126 127 128 129
}


bool LayoutDescriptor::IsSlowLayout() { return !IsSmi(); }


int LayoutDescriptor::capacity() {
130
  return IsSlowLayout() ? (length() * kBitsPerByte) : kSmiValueSize;
131 132 133 134
}


LayoutDescriptor* LayoutDescriptor::cast_gc_safe(Object* object) {
135 136 137 138 139
  // The map word of the object can be a forwarding pointer during
  // object evacuation phase of GC. Since the layout descriptor methods
  // for checking whether a field is tagged or not do not depend on the
  // object map, it should be safe.
  return reinterpret_cast<LayoutDescriptor*>(object);
140 141
}

142 143
int LayoutDescriptor::GetSlowModeBackingStoreLength(int length) {
  DCHECK_LT(0, length);
144 145 146 147
  // We allocate kPointerSize rounded blocks of memory anyway so we increase
  // the length  of allocated array to utilize that "lost" space which could
  // also help to avoid layout descriptor reallocations.
  return RoundUp(length, kBitsPerByte * kPointerSize) / kBitsPerByte;
148 149 150 151 152
}


int LayoutDescriptor::CalculateCapacity(Map* map, DescriptorArray* descriptors,
                                        int num_descriptors) {
153
  int inobject_properties = map->GetInObjectProperties();
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  if (inobject_properties == 0) return 0;

  DCHECK_LE(num_descriptors, descriptors->number_of_descriptors());

  int layout_descriptor_length;
  const int kMaxWordsPerField = kDoubleSize / kPointerSize;

  if (num_descriptors <= kSmiValueSize / kMaxWordsPerField) {
    // Even in the "worst" case (all fields are doubles) it would fit into
    // a Smi, so no need to calculate length.
    layout_descriptor_length = kSmiValueSize;

  } else {
    layout_descriptor_length = 0;

    for (int i = 0; i < num_descriptors; i++) {
      PropertyDetails details = descriptors->GetDetails(i);
      if (!InobjectUnboxedField(inobject_properties, details)) continue;
      int field_index = details.field_index();
      int field_width_in_words = details.field_width_in_words();
      layout_descriptor_length =
          Max(layout_descriptor_length, field_index + field_width_in_words);
    }
  }
  layout_descriptor_length = Min(layout_descriptor_length, inobject_properties);
  return layout_descriptor_length;
}


LayoutDescriptor* LayoutDescriptor::Initialize(
    LayoutDescriptor* layout_descriptor, Map* map, DescriptorArray* descriptors,
    int num_descriptors) {
  DisallowHeapAllocation no_allocation;
187
  int inobject_properties = map->GetInObjectProperties();
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

  for (int i = 0; i < num_descriptors; i++) {
    PropertyDetails details = descriptors->GetDetails(i);
    if (!InobjectUnboxedField(inobject_properties, details)) {
      DCHECK(details.location() != kField ||
             layout_descriptor->IsTagged(details.field_index()));
      continue;
    }
    int field_index = details.field_index();
    layout_descriptor = layout_descriptor->SetRawData(field_index);
    if (details.field_width_in_words() > 1) {
      layout_descriptor = layout_descriptor->SetRawData(field_index + 1);
    }
  }
  return layout_descriptor;
}


206
// LayoutDescriptorHelper is a helper class for querying whether inobject
207
// property at offset is Double or not.
208
LayoutDescriptorHelper::LayoutDescriptorHelper(Map* map)
209 210 211 212 213 214 215 216 217 218
    : all_fields_tagged_(true),
      header_size_(0),
      layout_descriptor_(LayoutDescriptor::FastPointerLayout()) {
  if (!FLAG_unbox_double_fields) return;

  layout_descriptor_ = map->layout_descriptor_gc_safe();
  if (layout_descriptor_->IsFastPointerLayout()) {
    return;
  }

219
  header_size_ = map->GetInObjectPropertiesStartInWords() * kPointerSize;
220
  DCHECK_GE(header_size_, 0);
221 222 223 224 225

  all_fields_tagged_ = false;
}


226
bool LayoutDescriptorHelper::IsTagged(int offset_in_bytes) {
227 228 229 230 231 232 233 234
  DCHECK(IsAligned(offset_in_bytes, kPointerSize));
  if (all_fields_tagged_) return true;
  // Object headers do not contain non-tagged fields.
  if (offset_in_bytes < header_size_) return true;
  int field_index = (offset_in_bytes - header_size_) / kPointerSize;

  return layout_descriptor_->IsTagged(field_index);
}
235 236
}  // namespace internal
}  // namespace v8
237 238

#endif  // V8_LAYOUT_DESCRIPTOR_INL_H_