2014-11-11 10:24:52 +00:00
|
|
|
// 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.
|
|
|
|
return handle(LayoutDescriptor::FromSmi(Smi::FromInt(0)), isolate);
|
|
|
|
}
|
2015-03-30 17:03:41 +00:00
|
|
|
length = GetSlowModeBackingStoreLength(length);
|
2014-11-11 10:24:52 +00:00
|
|
|
return Handle<LayoutDescriptor>::cast(
|
2015-04-21 15:21:57 +00:00
|
|
|
isolate->factory()->NewJSTypedArray(UINT32_ELEMENTS, length));
|
2014-11-11 10:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LayoutDescriptor::InobjectUnboxedField(int inobject_properties,
|
|
|
|
PropertyDetails details) {
|
2015-01-19 17:49:13 +00:00
|
|
|
if (details.type() != DATA || !details.representation().IsDouble()) {
|
2014-11-11 10:24:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We care only about in-object properties.
|
|
|
|
return details.field_index() < inobject_properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LayoutDescriptor* LayoutDescriptor::FastPointerLayout() {
|
|
|
|
return LayoutDescriptor::FromSmi(Smi::FromInt(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LayoutDescriptor::GetIndexes(int field_index, int* layout_word_index,
|
2014-12-15 12:13:08 +00:00
|
|
|
int* layout_bit_index) {
|
2014-11-11 10:24:52 +00:00
|
|
|
if (static_cast<unsigned>(field_index) >= static_cast<unsigned>(capacity())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*layout_word_index = field_index / kNumberOfBits;
|
2015-04-21 15:21:57 +00:00
|
|
|
CHECK((!IsSmi() && (*layout_word_index < Smi::cast(length())->value())) ||
|
2014-11-11 10:24:52 +00:00
|
|
|
(IsSmi() && (*layout_word_index < 1)));
|
|
|
|
|
2014-12-15 12:13:08 +00:00
|
|
|
*layout_bit_index = field_index % kNumberOfBits;
|
2014-11-11 10:24:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LayoutDescriptor* LayoutDescriptor::SetTagged(int field_index, bool tagged) {
|
|
|
|
int layout_word_index;
|
2014-12-15 12:13:08 +00:00
|
|
|
int layout_bit_index;
|
2014-11-11 10:24:52 +00:00
|
|
|
|
2014-12-15 12:13:08 +00:00
|
|
|
if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
|
2014-11-11 10:24:52 +00:00
|
|
|
CHECK(false);
|
|
|
|
return this;
|
|
|
|
}
|
2014-12-15 12:13:08 +00:00
|
|
|
uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
|
2014-11-11 10:24:52 +00:00
|
|
|
|
|
|
|
if (IsSlowLayout()) {
|
|
|
|
uint32_t value = get_scalar(layout_word_index);
|
|
|
|
if (tagged) {
|
|
|
|
value &= ~layout_mask;
|
|
|
|
} else {
|
|
|
|
value |= layout_mask;
|
|
|
|
}
|
|
|
|
set(layout_word_index, value);
|
|
|
|
return this;
|
|
|
|
} else {
|
|
|
|
uint32_t value = static_cast<uint32_t>(Smi::cast(this)->value());
|
|
|
|
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;
|
2014-12-15 12:13:08 +00:00
|
|
|
int layout_bit_index;
|
2014-11-11 10:24:52 +00:00
|
|
|
|
2014-12-15 12:13:08 +00:00
|
|
|
if (!GetIndexes(field_index, &layout_word_index, &layout_bit_index)) {
|
2014-11-11 10:24:52 +00:00
|
|
|
// All bits after Out of bounds queries
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-15 12:13:08 +00:00
|
|
|
uint32_t layout_mask = static_cast<uint32_t>(1) << layout_bit_index;
|
2014-11-11 10:24:52 +00:00
|
|
|
|
|
|
|
if (IsSlowLayout()) {
|
|
|
|
uint32_t value = get_scalar(layout_word_index);
|
|
|
|
return (value & layout_mask) == 0;
|
|
|
|
} else {
|
|
|
|
uint32_t value = static_cast<uint32_t>(Smi::cast(this)->value());
|
|
|
|
return (value & layout_mask) == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LayoutDescriptor::IsFastPointerLayout() {
|
2014-11-24 14:54:26 +00:00
|
|
|
return this == FastPointerLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LayoutDescriptor::IsFastPointerLayout(Object* layout_descriptor) {
|
|
|
|
return layout_descriptor == FastPointerLayout();
|
2014-11-11 10:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LayoutDescriptor::IsSlowLayout() { return !IsSmi(); }
|
|
|
|
|
|
|
|
|
|
|
|
int LayoutDescriptor::capacity() {
|
2015-04-21 15:21:57 +00:00
|
|
|
return IsSlowLayout() ? (Smi::cast(length())->value() * kNumberOfBits)
|
|
|
|
: kSmiValueSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t LayoutDescriptor::get_scalar(int index) {
|
|
|
|
DCHECK(IsSlowLayout());
|
|
|
|
return GcSafeElements()->get_scalar(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LayoutDescriptor::set(int index, uint32_t value) {
|
|
|
|
DCHECK(IsSlowLayout());
|
|
|
|
GcSafeElements()->set(index, value);
|
2014-11-11 10:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LayoutDescriptor* LayoutDescriptor::cast_gc_safe(Object* object) {
|
|
|
|
if (object->IsSmi()) {
|
|
|
|
// Fast mode layout descriptor.
|
|
|
|
return reinterpret_cast<LayoutDescriptor*>(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a mixed descriptor which is a fixed typed array.
|
|
|
|
MapWord map_word = reinterpret_cast<HeapObject*>(object)->map_word();
|
|
|
|
if (map_word.IsForwardingAddress()) {
|
|
|
|
// Mark-compact has already moved layout descriptor.
|
|
|
|
object = map_word.ToForwardingAddress();
|
|
|
|
}
|
|
|
|
return LayoutDescriptor::cast(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-30 17:03:41 +00:00
|
|
|
int LayoutDescriptor::GetSlowModeBackingStoreLength(int length) {
|
|
|
|
length = (length + kNumberOfBits - 1) / kNumberOfBits;
|
|
|
|
DCHECK_LT(0, length);
|
|
|
|
|
|
|
|
if (SmiValuesAre32Bits() && (length & 1)) {
|
|
|
|
// On 64-bit systems if the length is odd then the half-word space would be
|
|
|
|
// lost anyway (due to alignment and the fact that we are allocating
|
|
|
|
// uint32-typed array), so we increase the length of allocated array
|
|
|
|
// to utilize that "lost" space which could also help to avoid layout
|
|
|
|
// descriptor reallocations.
|
|
|
|
++length;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LayoutDescriptor::CalculateCapacity(Map* map, DescriptorArray* descriptors,
|
|
|
|
int num_descriptors) {
|
|
|
|
int inobject_properties = map->inobject_properties();
|
|
|
|
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;
|
|
|
|
int inobject_properties = map->inobject_properties();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-21 15:21:57 +00:00
|
|
|
FixedTypedArray<Uint32ArrayTraits>* LayoutDescriptor::GcSafeElements() {
|
|
|
|
MapWord map_word = HeapObject::cast(elements())->map_word();
|
|
|
|
if (map_word.IsForwardingAddress()) {
|
|
|
|
return FixedTypedArray<Uint32ArrayTraits>::cast(
|
|
|
|
map_word.ToForwardingAddress());
|
|
|
|
}
|
|
|
|
return FixedTypedArray<Uint32ArrayTraits>::cast(elements());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 10:24:52 +00:00
|
|
|
// InobjectPropertiesHelper is a helper class for querying whether inobject
|
|
|
|
// property at offset is Double or not.
|
2014-12-15 12:13:08 +00:00
|
|
|
LayoutDescriptorHelper::LayoutDescriptorHelper(Map* map)
|
2014-11-11 10:24:52 +00:00
|
|
|
: 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int inobject_properties = map->inobject_properties();
|
|
|
|
DCHECK(inobject_properties > 0);
|
|
|
|
header_size_ = map->instance_size() - (inobject_properties * kPointerSize);
|
|
|
|
DCHECK(header_size_ >= 0);
|
|
|
|
|
|
|
|
all_fields_tagged_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-15 12:13:08 +00:00
|
|
|
bool LayoutDescriptorHelper::IsTagged(int offset_in_bytes) {
|
2014-11-11 10:24:52 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_LAYOUT_DESCRIPTOR_INL_H_
|