diff --git a/BUILD.gn b/BUILD.gn index a988ffad2d..78ed32655f 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -2390,6 +2390,7 @@ v8_source_set("v8_base") { "src/reloc-info.cc", "src/reloc-info.h", "src/roots-inl.h", + "src/roots.cc", "src/roots.h", "src/runtime-profiler.cc", "src/runtime-profiler.h", diff --git a/src/builtins/builtins-typed-array-gen.cc b/src/builtins/builtins-typed-array-gen.cc index 970136b11c..99979b0283 100644 --- a/src/builtins/builtins-typed-array-gen.cc +++ b/src/builtins/builtins-typed-array-gen.cc @@ -32,12 +32,12 @@ TNode TypedArrayBuiltinsAssembler::LoadMapForType( TVARIABLE(Map, var_typed_map); TNode array_map = LoadMap(array); TNode elements_kind = LoadMapElementsKind(array_map); + ReadOnlyRoots roots(isolate()); DispatchTypedArrayByElementsKind( elements_kind, [&](ElementsKind kind, int size, int typed_array_fun_index) { - Handle map(isolate()->heap()->MapForFixedTypedArray(kind), - isolate()); + Handle map(roots.MapForFixedTypedArray(kind), isolate()); var_typed_map = HeapConstant(map); }); diff --git a/src/elements-kind.h b/src/elements-kind.h index e5d55c246a..473c4ebd85 100644 --- a/src/elements-kind.h +++ b/src/elements-kind.h @@ -25,7 +25,7 @@ namespace internal { V(BigUint64, biguint64, BIGUINT64, uint64_t) \ V(BigInt64, bigint64, BIGINT64, int64_t) -enum ElementsKind { +enum ElementsKind : uint8_t { // The "fast" kind for elements that only contain SMI values. Must be first // to make it possible to efficiently check maps for this kind. PACKED_SMI_ELEMENTS, diff --git a/src/heap/factory.cc b/src/heap/factory.cc index cf765931b2..1c773d6479 100644 --- a/src/heap/factory.cc +++ b/src/heap/factory.cc @@ -1695,7 +1695,8 @@ Handle Factory::NewFixedTypedArrayWithExternalPointer( DCHECK(0 <= length && length <= Smi::kMaxValue); int size = FixedTypedArrayBase::kHeaderSize; HeapObject* result = AllocateRawWithImmortalMap( - size, pretenure, isolate()->heap()->MapForFixedTypedArray(array_type)); + size, pretenure, + ReadOnlyRoots(isolate()).MapForFixedTypedArray(array_type)); Handle elements(FixedTypedArrayBase::cast(result), isolate()); elements->set_base_pointer(Smi::kZero, SKIP_WRITE_BARRIER); @@ -1712,7 +1713,7 @@ Handle Factory::NewFixedTypedArray( CHECK(byte_length <= kMaxInt - FixedTypedArrayBase::kDataOffset); size_t size = OBJECT_POINTER_ALIGN(byte_length + FixedTypedArrayBase::kDataOffset); - Map* map = isolate()->heap()->MapForFixedTypedArray(array_type); + Map* map = ReadOnlyRoots(isolate()).MapForFixedTypedArray(array_type); AllocationAlignment alignment = array_type == kExternalFloat64Array ? kDoubleAligned : kWordAligned; HeapObject* object = AllocateRawWithImmortalMap(static_cast(size), diff --git a/src/heap/heap.cc b/src/heap/heap.cc index f557c5457e..3e01fcf39a 100644 --- a/src/heap/heap.cc +++ b/src/heap/heap.cc @@ -2426,60 +2426,6 @@ void Heap::FlushNumberStringCache() { } } -namespace { - -RootIndex RootIndexForFixedTypedArray(ExternalArrayType array_type) { - switch (array_type) { -#define ARRAY_TYPE_TO_ROOT_INDEX(Type, type, TYPE, ctype) \ - case kExternal##Type##Array: \ - return RootIndex::kFixed##Type##ArrayMap; - - TYPED_ARRAYS(ARRAY_TYPE_TO_ROOT_INDEX) -#undef ARRAY_TYPE_TO_ROOT_INDEX - } - UNREACHABLE(); -} - -RootIndex RootIndexForFixedTypedArray(ElementsKind elements_kind) { - switch (elements_kind) { -#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype) \ - case TYPE##_ELEMENTS: \ - return RootIndex::kFixed##Type##ArrayMap; - TYPED_ARRAYS(TYPED_ARRAY_CASE) - default: - UNREACHABLE(); -#undef TYPED_ARRAY_CASE - } -} - -RootIndex RootIndexForEmptyFixedTypedArray(ElementsKind elements_kind) { - switch (elements_kind) { -#define ELEMENT_KIND_TO_ROOT_INDEX(Type, type, TYPE, ctype) \ - case TYPE##_ELEMENTS: \ - return RootIndex::kEmptyFixed##Type##Array; - - TYPED_ARRAYS(ELEMENT_KIND_TO_ROOT_INDEX) -#undef ELEMENT_KIND_TO_ROOT_INDEX - default: - UNREACHABLE(); - } -} - -} // namespace - -Map* Heap::MapForFixedTypedArray(ExternalArrayType array_type) { - return Map::cast(roots_[RootIndexForFixedTypedArray(array_type)]); -} - -Map* Heap::MapForFixedTypedArray(ElementsKind elements_kind) { - return Map::cast(roots_[RootIndexForFixedTypedArray(elements_kind)]); -} - -FixedTypedArrayBase* Heap::EmptyFixedTypedArrayForMap(const Map* map) { - return FixedTypedArrayBase::cast( - roots_[RootIndexForEmptyFixedTypedArray(map->elements_kind())]); -} - HeapObject* Heap::CreateFillerObjectAt(Address addr, int size, ClearRecordedSlots clear_slots_mode, ClearFreedMemoryMode clear_memory_mode) { diff --git a/src/heap/heap.h b/src/heap/heap.h index 70649a3bd6..ddc0bf4502 100644 --- a/src/heap/heap.h +++ b/src/heap/heap.h @@ -771,6 +771,8 @@ class Heap { friend class ReadOnlyRoots; public: + RootsTable& roots_table() { return roots_; } + // Heap root getters. #define ROOT_ACCESSOR(type, name, CamelName) inline type* name(); MUTABLE_ROOT_LIST(ROOT_ACCESSOR) @@ -850,10 +852,6 @@ class Heap { // Generated code can treat direct references to this root as constant. bool RootCanBeTreatedAsConstant(RootIndex root_index); - Map* MapForFixedTypedArray(ExternalArrayType array_type); - Map* MapForFixedTypedArray(ElementsKind elements_kind); - FixedTypedArrayBase* EmptyFixedTypedArrayForMap(const Map* map); - void RegisterStrongRoots(Object** start, Object** end); void UnregisterStrongRoots(Object** start); diff --git a/src/heap/setup-heap-internal.cc b/src/heap/setup-heap-internal.cc index 7dcb66e50e..5cedc6dbd8 100644 --- a/src/heap/setup-heap-internal.cc +++ b/src/heap/setup-heap-internal.cc @@ -181,8 +181,9 @@ AllocationResult Heap::AllocateEmptyFixedTypedArray( array_type == kExternalFloat64Array ? kDoubleAligned : kWordAligned); if (!allocation.To(&object)) return allocation; - object->set_map_after_allocation(MapForFixedTypedArray(array_type), - SKIP_WRITE_BARRIER); + object->set_map_after_allocation( + ReadOnlyRoots(this).MapForFixedTypedArray(array_type), + SKIP_WRITE_BARRIER); FixedTypedArrayBase* elements = FixedTypedArrayBase::cast(object); elements->set_base_pointer(elements, SKIP_WRITE_BARRIER); elements->set_external_pointer( diff --git a/src/objects/allocation-site.h b/src/objects/allocation-site.h index 7d6caf651e..d923fd8f23 100644 --- a/src/objects/allocation-site.h +++ b/src/objects/allocation-site.h @@ -62,9 +62,9 @@ class AllocationSite : public Struct, public NeverReadOnlySpaceObject { bool IsNested(); // transition_info bitfields, for constructed array transition info. - class ElementsKindBits : public BitField {}; - class UnusedBits : public BitField {}; - class DoNotInlineBit : public BitField {}; + class ElementsKindBits : public BitField {}; + class DoNotInlineBit : public BitField {}; + // Unused bits 6-30. // Bitfields for pretenure_data class MementoFoundCountBits : public BitField {}; diff --git a/src/roots-inl.h b/src/roots-inl.h index ccebf4dbe6..a31c341952 100644 --- a/src/roots-inl.h +++ b/src/roots-inl.h @@ -8,7 +8,6 @@ #include "src/roots.h" #include "src/heap/heap-inl.h" -#include "src/objects/api-callbacks.h" namespace v8 { namespace internal { @@ -24,24 +23,37 @@ V8_INLINE RootIndex operator++(RootIndex& index) { return index; } -ReadOnlyRoots::ReadOnlyRoots(Isolate* isolate) : heap_(isolate->heap()) {} +ReadOnlyRoots::ReadOnlyRoots(Heap* heap) : roots_table_(heap->roots_table()) {} -#define ROOT_ACCESSOR(type, name, CamelName) \ - type* ReadOnlyRoots::name() { \ - return type::cast(heap_->roots_[RootIndex::k##CamelName]); \ - } \ - Handle ReadOnlyRoots::name##_handle() { \ - return Handle( \ - bit_cast(&heap_->roots_[RootIndex::k##CamelName])); \ +ReadOnlyRoots::ReadOnlyRoots(Isolate* isolate) + : roots_table_(isolate->heap()->roots_table()) {} + +#define ROOT_ACCESSOR(type, name, CamelName) \ + type* ReadOnlyRoots::name() { \ + return type::cast(roots_table_[RootIndex::k##CamelName]); \ + } \ + Handle ReadOnlyRoots::name##_handle() { \ + return Handle( \ + bit_cast(&roots_table_[RootIndex::k##CamelName])); \ } READ_ONLY_ROOT_LIST(ROOT_ACCESSOR) #undef ROOT_ACCESSOR +Map* ReadOnlyRoots::MapForFixedTypedArray(ExternalArrayType array_type) { + RootIndex root_index = RootsTable::RootIndexForFixedTypedArray(array_type); + return Map::cast(roots_table_[root_index]); +} + +Map* ReadOnlyRoots::MapForFixedTypedArray(ElementsKind elements_kind) { + RootIndex root_index = RootsTable::RootIndexForFixedTypedArray(elements_kind); + return Map::cast(roots_table_[root_index]); +} + FixedTypedArrayBase* ReadOnlyRoots::EmptyFixedTypedArrayForMap(const Map* map) { - // TODO(delphick): All of these empty fixed type arrays are in RO_SPACE so - // this the method below can be moved into ReadOnlyRoots. - return heap_->EmptyFixedTypedArrayForMap(map); + RootIndex root_index = + RootsTable::RootIndexForEmptyFixedTypedArray(map->elements_kind()); + return FixedTypedArrayBase::cast(roots_table_[root_index]); } } // namespace internal diff --git a/src/roots.cc b/src/roots.cc new file mode 100644 index 0000000000..529d2ec472 --- /dev/null +++ b/src/roots.cc @@ -0,0 +1,54 @@ +// Copyright 2018 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. + +#include "src/roots.h" +#include "src/elements-kind.h" + +namespace v8 { +namespace internal { + +// static +RootIndex RootsTable::RootIndexForFixedTypedArray( + ExternalArrayType array_type) { + switch (array_type) { +#define ARRAY_TYPE_TO_ROOT_INDEX(Type, type, TYPE, ctype) \ + case kExternal##Type##Array: \ + return RootIndex::kFixed##Type##ArrayMap; + + TYPED_ARRAYS(ARRAY_TYPE_TO_ROOT_INDEX) +#undef ARRAY_TYPE_TO_ROOT_INDEX + } + UNREACHABLE(); +} + +// static +RootIndex RootsTable::RootIndexForFixedTypedArray(ElementsKind elements_kind) { + switch (elements_kind) { +#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype) \ + case TYPE##_ELEMENTS: \ + return RootIndex::kFixed##Type##ArrayMap; + TYPED_ARRAYS(TYPED_ARRAY_CASE) + default: + UNREACHABLE(); +#undef TYPED_ARRAY_CASE + } +} + +// static +RootIndex RootsTable::RootIndexForEmptyFixedTypedArray( + ElementsKind elements_kind) { + switch (elements_kind) { +#define ELEMENT_KIND_TO_ROOT_INDEX(Type, type, TYPE, ctype) \ + case TYPE##_ELEMENTS: \ + return RootIndex::kEmptyFixed##Type##Array; + + TYPED_ARRAYS(ELEMENT_KIND_TO_ROOT_INDEX) +#undef ELEMENT_KIND_TO_ROOT_INDEX + default: + UNREACHABLE(); + } +} + +} // namespace internal +} // namespace v8 diff --git a/src/roots.h b/src/roots.h index b05929456f..a3388760bf 100644 --- a/src/roots.h +++ b/src/roots.h @@ -6,14 +6,23 @@ #define V8_ROOTS_H_ #include "src/accessors.h" +#include "src/globals.h" #include "src/handles.h" #include "src/heap-symbols.h" #include "src/objects-definitions.h" namespace v8 { - namespace internal { +// Forward declarations. +enum ElementsKind : uint8_t; +class FixedTypedArrayBase; +class Heap; +class Isolate; +class Map; +class String; +class Symbol; + // Defines all the read-only roots in Heap. #define STRONG_READ_ONLY_ROOT_LIST(V) \ /* Cluster the most popular ones in a few cache lines here at the top. */ \ @@ -371,6 +380,10 @@ class RootsTable { return roots_[index]; } + static RootIndex RootIndexForFixedTypedArray(ExternalArrayType array_type); + static RootIndex RootIndexForFixedTypedArray(ElementsKind elements_kind); + static RootIndex RootIndexForEmptyFixedTypedArray(ElementsKind elements_kind); + private: Object** strong_roots_begin() { return &roots_[static_cast(RootIndex::kFirstStrongRoot)]; @@ -399,29 +412,24 @@ class RootsTable { friend class ReadOnlyRoots; }; -class FixedTypedArrayBase; -class Heap; -class Isolate; -class Map; -class String; -class Symbol; - class ReadOnlyRoots { public: - explicit ReadOnlyRoots(Heap* heap) : heap_(heap) {} - inline explicit ReadOnlyRoots(Isolate* isolate); + V8_INLINE explicit ReadOnlyRoots(Heap* heap); + V8_INLINE explicit ReadOnlyRoots(Isolate* isolate); #define ROOT_ACCESSOR(type, name, CamelName) \ - inline class type* name(); \ - inline Handle name##_handle(); + V8_INLINE class type* name(); \ + V8_INLINE Handle name##_handle(); READ_ONLY_ROOT_LIST(ROOT_ACCESSOR) #undef ROOT_ACCESSOR - inline FixedTypedArrayBase* EmptyFixedTypedArrayForMap(const Map* map); + V8_INLINE Map* MapForFixedTypedArray(ExternalArrayType array_type); + V8_INLINE Map* MapForFixedTypedArray(ElementsKind elements_kind); + V8_INLINE FixedTypedArrayBase* EmptyFixedTypedArrayForMap(const Map* map); private: - Heap* heap_; + const RootsTable& roots_table_; }; } // namespace internal