Commit e5648b2a authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[cleanup] Move some helper methods from Heap to ReadOnlyRoots

In particular: MapForFixedTypedArray() and EmptyFixedTypedArrayForMap().

And make ReadOnlyRoots object independent of the Heap.

Bug: v8:8015
Change-Id: Ifd17294661fac21c8e7545145280c8a2dedfe8c3
Reviewed-on: https://chromium-review.googlesource.com/1243131Reviewed-by: 's avatarHannes Payer <hpayer@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56234}
parent 647e0c23
......@@ -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",
......
......@@ -32,12 +32,12 @@ TNode<Map> TypedArrayBuiltinsAssembler::LoadMapForType(
TVARIABLE(Map, var_typed_map);
TNode<Map> array_map = LoadMap(array);
TNode<Int32T> elements_kind = LoadMapElementsKind(array_map);
ReadOnlyRoots roots(isolate());
DispatchTypedArrayByElementsKind(
elements_kind,
[&](ElementsKind kind, int size, int typed_array_fun_index) {
Handle<Map> map(isolate()->heap()->MapForFixedTypedArray(kind),
isolate());
Handle<Map> map(roots.MapForFixedTypedArray(kind), isolate());
var_typed_map = HeapConstant(map);
});
......
......@@ -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,
......
......@@ -1695,7 +1695,8 @@ Handle<FixedTypedArrayBase> 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<FixedTypedArrayBase> elements(FixedTypedArrayBase::cast(result),
isolate());
elements->set_base_pointer(Smi::kZero, SKIP_WRITE_BARRIER);
......@@ -1712,7 +1713,7 @@ Handle<FixedTypedArrayBase> 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<int>(size),
......
......@@ -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) {
......
......@@ -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);
......
......@@ -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(
......
......@@ -62,9 +62,9 @@ class AllocationSite : public Struct, public NeverReadOnlySpaceObject {
bool IsNested();
// transition_info bitfields, for constructed array transition info.
class ElementsKindBits : public BitField<ElementsKind, 0, 15> {};
class UnusedBits : public BitField<int, 15, 14> {};
class DoNotInlineBit : public BitField<bool, 29, 1> {};
class ElementsKindBits : public BitField<ElementsKind, 0, 5> {};
class DoNotInlineBit : public BitField<bool, 5, 1> {};
// Unused bits 6-30.
// Bitfields for pretenure_data
class MementoFoundCountBits : public BitField<int, 0, 26> {};
......
......@@ -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<type> ReadOnlyRoots::name##_handle() { \
return Handle<type>( \
bit_cast<type**>(&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<type> ReadOnlyRoots::name##_handle() { \
return Handle<type>( \
bit_cast<type**>(&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
......
// 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
......@@ -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<size_t>(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<type> name##_handle();
V8_INLINE class type* name(); \
V8_INLINE Handle<type> 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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment