roots.h 29.7 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef V8_ROOTS_H_
#define V8_ROOTS_H_

8
#include "src/accessors.h"
9
#include "src/globals.h"
10
#include "src/heap-symbols.h"
11
#include "src/objects-definitions.h"
12
#include "src/objects.h"
13
#include "src/objects/slots.h"
14 15 16 17

namespace v8 {
namespace internal {

18 19 20
// Forward declarations.
enum ElementsKind : uint8_t;
class FixedTypedArrayBase;
21 22
template <typename T>
class Handle;
23 24 25
class Heap;
class Isolate;
class Map;
26
class PropertyCell;
27 28
class String;
class Symbol;
Dan Elphick's avatar
Dan Elphick committed
29
class RootVisitor;
30

31 32 33 34 35
// 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.    */ \
  /* The first 32 entries are most often used in the startup snapshot and   */ \
  /* can use a shorter representation in the serialization format.          */ \
36 37 38
  V(Map, free_space_map, FreeSpaceMap)                                         \
  V(Map, one_pointer_filler_map, OnePointerFillerMap)                          \
  V(Map, two_pointer_filler_map, TwoPointerFillerMap)                          \
39 40 41 42 43 44
  V(Oddball, uninitialized_value, UninitializedValue)                          \
  V(Oddball, undefined_value, UndefinedValue)                                  \
  V(Oddball, the_hole_value, TheHoleValue)                                     \
  V(Oddball, null_value, NullValue)                                            \
  V(Oddball, true_value, TrueValue)                                            \
  V(Oddball, false_value, FalseValue)                                          \
45
  V(String, empty_string, empty_string)                                        \
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  V(Map, meta_map, MetaMap)                                                    \
  V(Map, byte_array_map, ByteArrayMap)                                         \
  V(Map, fixed_array_map, FixedArrayMap)                                       \
  V(Map, fixed_cow_array_map, FixedCOWArrayMap)                                \
  V(Map, hash_table_map, HashTableMap)                                         \
  V(Map, symbol_map, SymbolMap)                                                \
  V(Map, one_byte_string_map, OneByteStringMap)                                \
  V(Map, one_byte_internalized_string_map, OneByteInternalizedStringMap)       \
  V(Map, scope_info_map, ScopeInfoMap)                                         \
  V(Map, shared_function_info_map, SharedFunctionInfoMap)                      \
  V(Map, code_map, CodeMap)                                                    \
  V(Map, function_context_map, FunctionContextMap)                             \
  V(Map, cell_map, CellMap)                                                    \
  V(Map, global_property_cell_map, GlobalPropertyCellMap)                      \
  V(Map, foreign_map, ForeignMap)                                              \
  V(Map, heap_number_map, HeapNumberMap)                                       \
  V(Map, transition_array_map, TransitionArrayMap)                             \
  V(Map, feedback_vector_map, FeedbackVectorMap)                               \
64
  V(ScopeInfo, empty_scope_info, EmptyScopeInfo)                               \
65
  V(FixedArray, empty_fixed_array, EmptyFixedArray)                            \
66
  V(DescriptorArray, empty_descriptor_array, EmptyDescriptorArray)             \
67 68
  /* Entries beyond the first 32                                            */ \
  /* Oddballs */                                                               \
69 70 71 72 73
  V(Oddball, arguments_marker, ArgumentsMarker)                                \
  V(Oddball, exception, Exception)                                             \
  V(Oddball, termination_exception, TerminationException)                      \
  V(Oddball, optimized_out, OptimizedOut)                                      \
  V(Oddball, stale_register, StaleRegister)                                    \
74
  /* Context maps */                                                           \
75 76 77 78 79 80 81 82 83 84
  V(Map, native_context_map, NativeContextMap)                                 \
  V(Map, module_context_map, ModuleContextMap)                                 \
  V(Map, eval_context_map, EvalContextMap)                                     \
  V(Map, script_context_map, ScriptContextMap)                                 \
  V(Map, await_context_map, AwaitContextMap)                                   \
  V(Map, block_context_map, BlockContextMap)                                   \
  V(Map, catch_context_map, CatchContextMap)                                   \
  V(Map, with_context_map, WithContextMap)                                     \
  V(Map, debug_evaluate_context_map, DebugEvaluateContextMap)                  \
  V(Map, script_context_table_map, ScriptContextTableMap)                      \
85
  /* Maps */                                                                   \
86 87 88 89 90 91 92 93 94 95 96 97 98 99
  V(Map, feedback_metadata_map, FeedbackMetadataArrayMap)                      \
  V(Map, array_list_map, ArrayListMap)                                         \
  V(Map, bigint_map, BigIntMap)                                                \
  V(Map, object_boilerplate_description_map, ObjectBoilerplateDescriptionMap)  \
  V(Map, bytecode_array_map, BytecodeArrayMap)                                 \
  V(Map, code_data_container_map, CodeDataContainerMap)                        \
  V(Map, descriptor_array_map, DescriptorArrayMap)                             \
  V(Map, fixed_double_array_map, FixedDoubleArrayMap)                          \
  V(Map, global_dictionary_map, GlobalDictionaryMap)                           \
  V(Map, many_closures_cell_map, ManyClosuresCellMap)                          \
  V(Map, module_info_map, ModuleInfoMap)                                       \
  V(Map, mutable_heap_number_map, MutableHeapNumberMap)                        \
  V(Map, name_dictionary_map, NameDictionaryMap)                               \
  V(Map, no_closures_cell_map, NoClosuresCellMap)                              \
100
  V(Map, no_feedback_cell_map, NoFeedbackCellMap)                              \
101 102 103 104 105
  V(Map, number_dictionary_map, NumberDictionaryMap)                           \
  V(Map, one_closure_cell_map, OneClosureCellMap)                              \
  V(Map, ordered_hash_map_map, OrderedHashMapMap)                              \
  V(Map, ordered_hash_set_map, OrderedHashSetMap)                              \
  V(Map, ordered_name_dictionary_map, OrderedNameDictionaryMap)                \
106
  V(Map, preparse_data_map, PreparseDataMap)                                   \
107 108 109
  V(Map, property_array_map, PropertyArrayMap)                                 \
  V(Map, side_effect_call_handler_info_map, SideEffectCallHandlerInfoMap)      \
  V(Map, side_effect_free_call_handler_info_map,                               \
110
    SideEffectFreeCallHandlerInfoMap)                                          \
111
  V(Map, next_call_side_effect_free_call_handler_info_map,                     \
112
    NextCallSideEffectFreeCallHandlerInfoMap)                                  \
113 114 115 116 117 118
  V(Map, simple_number_dictionary_map, SimpleNumberDictionaryMap)              \
  V(Map, sloppy_arguments_elements_map, SloppyArgumentsElementsMap)            \
  V(Map, small_ordered_hash_map_map, SmallOrderedHashMapMap)                   \
  V(Map, small_ordered_hash_set_map, SmallOrderedHashSetMap)                   \
  V(Map, small_ordered_name_dictionary_map, SmallOrderedNameDictionaryMap)     \
  V(Map, string_table_map, StringTableMap)                                     \
119 120 121 122
  V(Map, uncompiled_data_without_preparse_data_map,                            \
    UncompiledDataWithoutPreparseDataMap)                                      \
  V(Map, uncompiled_data_with_preparse_data_map,                               \
    UncompiledDataWithPreparseDataMap)                                         \
123 124 125
  V(Map, weak_fixed_array_map, WeakFixedArrayMap)                              \
  V(Map, weak_array_list_map, WeakArrayListMap)                                \
  V(Map, ephemeron_hash_table_map, EphemeronHashTableMap)                      \
126
  V(Map, embedder_data_array_map, EmbedderDataArrayMap)                        \
127
  /* String maps */                                                            \
128 129 130 131 132 133 134 135 136 137
  V(Map, native_source_string_map, NativeSourceStringMap)                      \
  V(Map, string_map, StringMap)                                                \
  V(Map, cons_one_byte_string_map, ConsOneByteStringMap)                       \
  V(Map, cons_string_map, ConsStringMap)                                       \
  V(Map, thin_one_byte_string_map, ThinOneByteStringMap)                       \
  V(Map, thin_string_map, ThinStringMap)                                       \
  V(Map, sliced_string_map, SlicedStringMap)                                   \
  V(Map, sliced_one_byte_string_map, SlicedOneByteStringMap)                   \
  V(Map, external_string_map, ExternalStringMap)                               \
  V(Map, external_string_with_one_byte_data_map,                               \
138
    ExternalStringWithOneByteDataMap)                                          \
139 140 141
  V(Map, external_one_byte_string_map, ExternalOneByteStringMap)               \
  V(Map, uncached_external_string_map, UncachedExternalStringMap)              \
  V(Map, uncached_external_string_with_one_byte_data_map,                      \
142
    UncachedExternalStringWithOneByteDataMap)                                  \
143 144 145
  V(Map, internalized_string_map, InternalizedStringMap)                       \
  V(Map, external_internalized_string_map, ExternalInternalizedStringMap)      \
  V(Map, external_internalized_string_with_one_byte_data_map,                  \
146
    ExternalInternalizedStringWithOneByteDataMap)                              \
147
  V(Map, external_one_byte_internalized_string_map,                            \
148
    ExternalOneByteInternalizedStringMap)                                      \
149
  V(Map, uncached_external_internalized_string_map,                            \
150
    UncachedExternalInternalizedStringMap)                                     \
151
  V(Map, uncached_external_internalized_string_with_one_byte_data_map,         \
152
    UncachedExternalInternalizedStringWithOneByteDataMap)                      \
153
  V(Map, uncached_external_one_byte_internalized_string_map,                   \
154
    UncachedExternalOneByteInternalizedStringMap)                              \
155
  V(Map, uncached_external_one_byte_string_map,                                \
156
    UncachedExternalOneByteStringMap)                                          \
157
  /* Array element maps */                                                     \
158 159 160 161 162 163 164 165 166 167 168
  V(Map, fixed_uint8_array_map, FixedUint8ArrayMap)                            \
  V(Map, fixed_int8_array_map, FixedInt8ArrayMap)                              \
  V(Map, fixed_uint16_array_map, FixedUint16ArrayMap)                          \
  V(Map, fixed_int16_array_map, FixedInt16ArrayMap)                            \
  V(Map, fixed_uint32_array_map, FixedUint32ArrayMap)                          \
  V(Map, fixed_int32_array_map, FixedInt32ArrayMap)                            \
  V(Map, fixed_float32_array_map, FixedFloat32ArrayMap)                        \
  V(Map, fixed_float64_array_map, FixedFloat64ArrayMap)                        \
  V(Map, fixed_uint8_clamped_array_map, FixedUint8ClampedArrayMap)             \
  V(Map, fixed_biguint64_array_map, FixedBigUint64ArrayMap)                    \
  V(Map, fixed_bigint64_array_map, FixedBigInt64ArrayMap)                      \
169
  /* Oddball maps */                                                           \
170 171 172 173 174 175 176 177 178 179 180
  V(Map, undefined_map, UndefinedMap)                                          \
  V(Map, the_hole_map, TheHoleMap)                                             \
  V(Map, null_map, NullMap)                                                    \
  V(Map, boolean_map, BooleanMap)                                              \
  V(Map, uninitialized_map, UninitializedMap)                                  \
  V(Map, arguments_marker_map, ArgumentsMarkerMap)                             \
  V(Map, exception_map, ExceptionMap)                                          \
  V(Map, termination_exception_map, TerminationExceptionMap)                   \
  V(Map, optimized_out_map, OptimizedOutMap)                                   \
  V(Map, stale_register_map, StaleRegisterMap)                                 \
  V(Map, self_reference_marker_map, SelfReferenceMarkerMap)                    \
181
  /* Canonical empty values */                                                 \
182
  V(EnumCache, empty_enum_cache, EmptyEnumCache)                               \
183
  V(PropertyArray, empty_property_array, EmptyPropertyArray)                   \
184
  V(ByteArray, empty_byte_array, EmptyByteArray)                               \
185
  V(ObjectBoilerplateDescription, empty_object_boilerplate_description,        \
186
    EmptyObjectBoilerplateDescription)                                         \
187
  V(ArrayBoilerplateDescription, empty_array_boilerplate_description,          \
188
    EmptyArrayBoilerplateDescription)                                          \
189 190 191 192 193 194 195 196 197
  V(FixedTypedArrayBase, empty_fixed_uint8_array, EmptyFixedUint8Array)        \
  V(FixedTypedArrayBase, empty_fixed_int8_array, EmptyFixedInt8Array)          \
  V(FixedTypedArrayBase, empty_fixed_uint16_array, EmptyFixedUint16Array)      \
  V(FixedTypedArrayBase, empty_fixed_int16_array, EmptyFixedInt16Array)        \
  V(FixedTypedArrayBase, empty_fixed_uint32_array, EmptyFixedUint32Array)      \
  V(FixedTypedArrayBase, empty_fixed_int32_array, EmptyFixedInt32Array)        \
  V(FixedTypedArrayBase, empty_fixed_float32_array, EmptyFixedFloat32Array)    \
  V(FixedTypedArrayBase, empty_fixed_float64_array, EmptyFixedFloat64Array)    \
  V(FixedTypedArrayBase, empty_fixed_uint8_clamped_array,                      \
198
    EmptyFixedUint8ClampedArray)                                               \
199
  V(FixedTypedArrayBase, empty_fixed_biguint64_array,                          \
200
    EmptyFixedBigUint64Array)                                                  \
201
  V(FixedTypedArrayBase, empty_fixed_bigint64_array, EmptyFixedBigInt64Array)  \
202
  V(FixedArray, empty_sloppy_arguments_elements, EmptySloppyArgumentsElements) \
203
  V(NumberDictionary, empty_slow_element_dictionary,                           \
204
    EmptySlowElementDictionary)                                                \
205 206
  V(FixedArray, empty_ordered_hash_map, EmptyOrderedHashMap)                   \
  V(FixedArray, empty_ordered_hash_set, EmptyOrderedHashSet)                   \
207
  V(FeedbackMetadata, empty_feedback_metadata, EmptyFeedbackMetadata)          \
208
  V(PropertyCell, empty_property_cell, EmptyPropertyCell)                      \
209
  V(NameDictionary, empty_property_dictionary, EmptyPropertyDictionary)        \
210
  V(InterceptorInfo, noop_interceptor_info, NoOpInterceptorInfo)               \
211
  V(WeakFixedArray, empty_weak_fixed_array, EmptyWeakFixedArray)               \
212
  V(WeakArrayList, empty_weak_array_list, EmptyWeakArrayList)                  \
213
  /* Special numbers */                                                        \
214 215 216 217 218
  V(HeapNumber, nan_value, NanValue)                                           \
  V(HeapNumber, hole_nan_value, HoleNanValue)                                  \
  V(HeapNumber, infinity_value, InfinityValue)                                 \
  V(HeapNumber, minus_zero_value, MinusZeroValue)                              \
  V(HeapNumber, minus_infinity_value, MinusInfinityValue)                      \
219
  /* Marker for self-references during code-generation */                      \
220
  V(HeapObject, self_reference_marker, SelfReferenceMarker)                    \
221
  /* Canonical trampoline RelocInfo */                                         \
222
  V(ByteArray, off_heap_trampoline_relocation_info,                            \
223 224
    OffHeapTrampolineRelocationInfo)                                           \
  /* Hash seed */                                                              \
225
  V(ByteArray, hash_seed, HashSeed)
226

227 228
// Mutable roots that are known to be immortal immovable, for which we can
// safely skip write barriers.
229 230 231 232 233 234 235 236 237
#define STRONG_MUTABLE_IMMOVABLE_ROOT_LIST(V)                                \
  ACCESSOR_INFO_ROOT_LIST(V)                                                 \
  /* Maps */                                                                 \
  V(Map, external_map, ExternalMap)                                          \
  V(Map, message_object_map, JSMessageObjectMap)                             \
  /* Canonical empty values */                                               \
  V(Script, empty_script, EmptyScript)                                       \
  V(FeedbackCell, many_closures_cell, ManyClosuresCell)                      \
  V(FeedbackCell, no_feedback_cell, NoFeedbackCell)                          \
238
  V(Cell, invalid_prototype_validity_cell, InvalidPrototypeValidityCell)     \
239
  /* Protectors */                                                           \
240
  V(Cell, array_constructor_protector, ArrayConstructorProtector)            \
241
  V(PropertyCell, no_elements_protector, NoElementsProtector)                \
242
  V(Cell, is_concat_spreadable_protector, IsConcatSpreadableProtector)       \
243 244 245 246
  V(PropertyCell, array_species_protector, ArraySpeciesProtector)            \
  V(PropertyCell, typed_array_species_protector, TypedArraySpeciesProtector) \
  V(PropertyCell, regexp_species_protector, RegExpSpeciesProtector)          \
  V(PropertyCell, promise_species_protector, PromiseSpeciesProtector)        \
247
  V(Cell, string_length_protector, StringLengthProtector)                    \
248 249 250 251
  V(PropertyCell, array_iterator_protector, ArrayIteratorProtector)          \
  V(PropertyCell, array_buffer_detaching_protector,                          \
    ArrayBufferDetachingProtector)                                           \
  V(PropertyCell, promise_hook_protector, PromiseHookProtector)              \
252
  V(Cell, promise_resolve_protector, PromiseResolveProtector)                \
253 254 255 256 257 258 259 260 261
  V(PropertyCell, map_iterator_protector, MapIteratorProtector)              \
  V(PropertyCell, promise_then_protector, PromiseThenProtector)              \
  V(PropertyCell, set_iterator_protector, SetIteratorProtector)              \
  V(PropertyCell, string_iterator_protector, StringIteratorProtector)        \
  /* Caches */                                                               \
  V(FixedArray, single_character_string_cache, SingleCharacterStringCache)   \
  V(FixedArray, string_split_cache, StringSplitCache)                        \
  V(FixedArray, regexp_multiple_cache, RegExpMultipleCache)                  \
  /* Indirection lists for isolate-independent builtins */                   \
262
  V(FixedArray, builtins_constants_table, BuiltinsConstantsTable)
263

264
// These root references can be updated by the mutator.
265 266 267 268 269 270 271
#define STRONG_MUTABLE_MOVABLE_ROOT_LIST(V)                                \
  /* Caches */                                                             \
  V(FixedArray, number_string_cache, NumberStringCache)                    \
  /* Lists and dictionaries */                                             \
  V(NameDictionary, public_symbol_table, PublicSymbolTable)                \
  V(NameDictionary, api_symbol_table, ApiSymbolTable)                      \
  V(NameDictionary, api_private_symbol_table, ApiPrivateSymbolTable)       \
272
  V(WeakArrayList, script_list, ScriptList)                                \
273
  V(FixedArray, materialized_objects, MaterializedObjects)                 \
274 275 276
  V(WeakArrayList, detached_contexts, DetachedContexts)                    \
  V(WeakArrayList, retaining_path_targets, RetainingPathTargets)           \
  V(WeakArrayList, retained_maps, RetainedMaps)                            \
277
  /* Feedback vectors that we need for code coverage or type profile */    \
278
  V(Object, feedback_vectors_for_profiling_tools,                          \
279
    FeedbackVectorsForProfilingTools)                                      \
280
  V(WeakArrayList, noscript_shared_function_infos,                         \
281 282 283 284 285
    NoScriptSharedFunctionInfos)                                           \
  V(FixedArray, serialized_objects, SerializedObjects)                     \
  V(FixedArray, serialized_global_proxy_sizes, SerializedGlobalProxySizes) \
  V(TemplateList, message_listeners, MessageListeners)                     \
  /* Support for async stack traces */                                     \
286
  V(HeapObject, current_microtask, CurrentMicrotask)                       \
287
  /* JSWeakFactory objects which need cleanup */                           \
288
  V(Object, dirty_js_weak_factories, DirtyJSWeakFactories)                 \
289
  /* KeepDuringJob set for JS WeakRefs */                                  \
290 291
  V(HeapObject, weak_refs_keep_during_job, WeakRefsKeepDuringJob)          \
  V(HeapObject, interpreter_entry_trampoline_for_profiling,                \
292
    InterpreterEntryTrampolineForProfiling)
293

294 295
// Entries in this list are limited to Smis and are not visited during GC.
#define SMI_ROOT_LIST(V)                                                       \
296 297 298 299
  V(Smi, stack_limit, StackLimit)                                              \
  V(Smi, real_stack_limit, RealStackLimit)                                     \
  V(Smi, last_script_id, LastScriptId)                                         \
  V(Smi, last_debugging_id, LastDebuggingId)                                   \
300 301
  /* To distinguish the function templates, so that we can find them in the */ \
  /* function cache of the native context. */                                  \
302 303 304
  V(Smi, next_template_serial_number, NextTemplateSerialNumber)                \
  V(Smi, arguments_adaptor_deopt_pc_offset, ArgumentsAdaptorDeoptPCOffset)     \
  V(Smi, construct_stub_create_deopt_pc_offset,                                \
305
    ConstructStubCreateDeoptPCOffset)                                          \
306
  V(Smi, construct_stub_invoke_deopt_pc_offset,                                \
307
    ConstructStubInvokeDeoptPCOffset)                                          \
308
  V(Smi, interpreter_entry_return_pc_offset, InterpreterEntryReturnPCOffset)
309

310 311
// Adapts one INTERNALIZED_STRING_LIST_GENERATOR entry to
// the ROOT_LIST-compatible entry
312
#define INTERNALIZED_STRING_LIST_ADAPTER(V, name, ...) V(String, name, name)
313

314
// Produces (String, name, CamelCase) entries
315 316 317 318
#define INTERNALIZED_STRING_ROOT_LIST(V) \
  INTERNALIZED_STRING_LIST_GENERATOR(INTERNALIZED_STRING_LIST_ADAPTER, V)

// Adapts one XXX_SYMBOL_LIST_GENERATOR entry to the ROOT_LIST-compatible entry
319
#define SYMBOL_ROOT_LIST_ADAPTER(V, name, ...) V(Symbol, name, name)
320

321
// Produces (Symbol, name, CamelCase) entries
322
#define PRIVATE_SYMBOL_ROOT_LIST(V) \
323
  PRIVATE_SYMBOL_LIST_GENERATOR(SYMBOL_ROOT_LIST_ADAPTER, V)
324
#define PUBLIC_SYMBOL_ROOT_LIST(V) \
325
  PUBLIC_SYMBOL_LIST_GENERATOR(SYMBOL_ROOT_LIST_ADAPTER, V)
326
#define WELL_KNOWN_SYMBOL_ROOT_LIST(V) \
327 328 329 330 331
  WELL_KNOWN_SYMBOL_LIST_GENERATOR(SYMBOL_ROOT_LIST_ADAPTER, V)

// Adapts one ACCESSOR_INFO_LIST_GENERATOR entry to the ROOT_LIST-compatible
// entry
#define ACCESSOR_INFO_ROOT_LIST_ADAPTER(V, name, CamelName, ...) \
332
  V(AccessorInfo, name##_accessor, CamelName##Accessor)
333

334
// Produces (AccessorInfo, name, CamelCase) entries
335 336
#define ACCESSOR_INFO_ROOT_LIST(V) \
  ACCESSOR_INFO_LIST_GENERATOR(ACCESSOR_INFO_ROOT_LIST_ADAPTER, V)
337

338 339 340 341 342 343 344 345 346 347
#define READ_ONLY_ROOT_LIST(V)     \
  STRONG_READ_ONLY_ROOT_LIST(V)    \
  INTERNALIZED_STRING_ROOT_LIST(V) \
  PRIVATE_SYMBOL_ROOT_LIST(V)      \
  PUBLIC_SYMBOL_ROOT_LIST(V)       \
  WELL_KNOWN_SYMBOL_ROOT_LIST(V)   \
  STRUCT_MAPS_LIST(V)              \
  ALLOCATION_SITE_MAPS_LIST(V)     \
  DATA_HANDLER_MAPS_LIST(V)

348 349 350 351
#define MUTABLE_ROOT_LIST(V)                \
  STRONG_MUTABLE_IMMOVABLE_ROOT_LIST(V)     \
  STRONG_MUTABLE_MOVABLE_ROOT_LIST(V)       \
  V(StringTable, string_table, StringTable) \
352 353 354 355 356 357
  SMI_ROOT_LIST(V)

#define ROOT_LIST(V)     \
  READ_ONLY_ROOT_LIST(V) \
  MUTABLE_ROOT_LIST(V)

358 359
// Declare all the root indices.  This defines the root list order.
// clang-format off
360
enum class RootIndex : uint16_t {
361
#define DECL(type, name, CamelName) k##CamelName,
362
  ROOT_LIST(DECL)
363 364 365 366
#undef DECL

  kRootListLength,

367 368 369 370
  // Helper aliases for inclusive regions of root indices.
  kFirstRoot = 0,
  kLastRoot = kRootListLength - 1,

371 372
#define ROOT(...) +1
  kReadOnlyRootsCount = 0 READ_ONLY_ROOT_LIST(ROOT),
373 374
  kImmortalImmovableRootsCount =
      kReadOnlyRootsCount STRONG_MUTABLE_IMMOVABLE_ROOT_LIST(ROOT),
375 376 377 378
#undef ROOT
  kFirstReadOnlyRoot = kFirstRoot,
  kLastReadOnlyRoot = kFirstReadOnlyRoot + kReadOnlyRootsCount - 1,

Dan Elphick's avatar
Dan Elphick committed
379 380 381 382 383 384 385 386 387 388
  // The strong roots visited by the garbage collector (not including read-only
  // roots).
  kFirstStrongRoot = kLastReadOnlyRoot + 1,
  // (kStringTable is not a strong root).
  kLastStrongRoot = kStringTable - 1,

  // All of the strong roots plus the read-only roots.
  kFirstStrongOrReadOnlyRoot = kFirstRoot,
  kLastStrongOrReadOnlyRoot = kLastStrongRoot,

389
  // All immortal immovable roots including read only ones.
390
  kFirstImmortalImmovableRoot = kFirstReadOnlyRoot,
391 392
  kLastImmortalImmovableRoot =
      kFirstImmortalImmovableRoot + kImmortalImmovableRootsCount - 1,
393

394 395
  kFirstSmiRoot = kStringTable + 1,
  kLastSmiRoot = kLastRoot
396 397 398 399 400 401 402 403 404 405 406
};
// clang-format on

// Represents a storage of V8 heap roots.
class RootsTable {
 public:
  static constexpr size_t kEntriesCount =
      static_cast<size_t>(RootIndex::kRootListLength);

  RootsTable() : roots_{} {}

407 408
  inline bool IsRootHandleLocation(Address* handle_location,
                                   RootIndex* index) const;
409

410
  template <typename T>
411
  bool IsRootHandle(Handle<T> handle, RootIndex* index) const;
412

413
  Address const& operator[](RootIndex root_index) const {
414 415 416 417 418
    size_t index = static_cast<size_t>(root_index);
    DCHECK_LT(index, kEntriesCount);
    return roots_[index];
  }

419 420 421 422 423 424
  static const char* name(RootIndex root_index) {
    size_t index = static_cast<size_t>(root_index);
    DCHECK_LT(index, kEntriesCount);
    return root_names_[index];
  }

425 426 427 428
  static constexpr int offset_of(RootIndex root_index) {
    return static_cast<int>(root_index) * kPointerSize;
  }

429 430 431 432
  static RootIndex RootIndexForFixedTypedArray(ExternalArrayType array_type);
  static RootIndex RootIndexForFixedTypedArray(ElementsKind elements_kind);
  static RootIndex RootIndexForEmptyFixedTypedArray(ElementsKind elements_kind);

433 434 435 436 437 438 439 440 441 442 443 444
  // Immortal immovable root objects are allocated in OLD space and GC never
  // moves them and the root table entries are guaranteed to not be modified
  // after initialization. Note, however, that contents of those root objects
  // that are allocated in writable space can still be modified after
  // initialization.
  // Generated code can treat direct references to these roots as constants.
  static constexpr bool IsImmortalImmovable(RootIndex root_index) {
    STATIC_ASSERT(static_cast<int>(RootIndex::kFirstImmortalImmovableRoot) ==
                  0);
    return static_cast<unsigned>(root_index) <=
           static_cast<unsigned>(RootIndex::kLastImmortalImmovableRoot);
  }
445

446
 private:
447 448
  FullObjectSlot begin() {
    return FullObjectSlot(&roots_[static_cast<size_t>(RootIndex::kFirstRoot)]);
Dan Elphick's avatar
Dan Elphick committed
449
  }
450 451 452
  FullObjectSlot end() {
    return FullObjectSlot(
        &roots_[static_cast<size_t>(RootIndex::kLastRoot) + 1]);
Dan Elphick's avatar
Dan Elphick committed
453 454 455
  }

  // Used for iterating over all of the read-only and mutable strong roots.
456
  FullObjectSlot strong_or_read_only_roots_begin() {
Dan Elphick's avatar
Dan Elphick committed
457 458
    STATIC_ASSERT(static_cast<size_t>(RootIndex::kLastReadOnlyRoot) ==
                  static_cast<size_t>(RootIndex::kFirstStrongRoot) - 1);
459
    return FullObjectSlot(
460
        &roots_[static_cast<size_t>(RootIndex::kFirstStrongOrReadOnlyRoot)]);
Dan Elphick's avatar
Dan Elphick committed
461
  }
462 463
  FullObjectSlot strong_or_read_only_roots_end() {
    return FullObjectSlot(
464
        &roots_[static_cast<size_t>(RootIndex::kLastStrongOrReadOnlyRoot) + 1]);
Dan Elphick's avatar
Dan Elphick committed
465 466 467 468
  }

  // The read-only, strong and Smi roots as defined by these accessors are all
  // disjoint.
469 470
  FullObjectSlot read_only_roots_begin() {
    return FullObjectSlot(
471
        &roots_[static_cast<size_t>(RootIndex::kFirstReadOnlyRoot)]);
472
  }
473 474
  FullObjectSlot read_only_roots_end() {
    return FullObjectSlot(
475
        &roots_[static_cast<size_t>(RootIndex::kLastReadOnlyRoot) + 1]);
476 477
  }

478 479
  FullObjectSlot strong_roots_begin() {
    return FullObjectSlot(
480
        &roots_[static_cast<size_t>(RootIndex::kFirstStrongRoot)]);
481
  }
482 483
  FullObjectSlot strong_roots_end() {
    return FullObjectSlot(
484
        &roots_[static_cast<size_t>(RootIndex::kLastStrongRoot) + 1]);
485 486
  }

487 488 489
  FullObjectSlot smi_roots_begin() {
    return FullObjectSlot(
        &roots_[static_cast<size_t>(RootIndex::kFirstSmiRoot)]);
490
  }
491 492
  FullObjectSlot smi_roots_end() {
    return FullObjectSlot(
493
        &roots_[static_cast<size_t>(RootIndex::kLastSmiRoot) + 1]);
494
  }
495

496
  Address& operator[](RootIndex root_index) {
497 498 499 500 501
    size_t index = static_cast<size_t>(root_index);
    DCHECK_LT(index, kEntriesCount);
    return roots_[index];
  }

502
  Address roots_[kEntriesCount];
503
  static const char* root_names_[kEntriesCount];
504

505
  friend class Isolate;
506 507 508
  friend class Heap;
  friend class Factory;
  friend class ReadOnlyRoots;
Dan Elphick's avatar
Dan Elphick committed
509
  friend class RootsSerializer;
510 511
};

512 513
class ReadOnlyRoots {
 public:
514 515
  V8_INLINE explicit ReadOnlyRoots(Heap* heap);
  V8_INLINE explicit ReadOnlyRoots(Isolate* isolate);
516

517 518
#define ROOT_ACCESSOR(Type, name, CamelName) \
  V8_INLINE class Type name() const;         \
519
  V8_INLINE Handle<Type> name##_handle() const;
520

521
  READ_ONLY_ROOT_LIST(ROOT_ACCESSOR)
522
#undef ROOT_ACCESSOR
523

524 525
  V8_INLINE Map MapForFixedTypedArray(ExternalArrayType array_type);
  V8_INLINE Map MapForFixedTypedArray(ElementsKind elements_kind);
526
  V8_INLINE FixedTypedArrayBase EmptyFixedTypedArrayForMap(const Map map);
527

Dan Elphick's avatar
Dan Elphick committed
528 529 530 531 532
  // Iterate over all the read-only roots. This is not necessary for garbage
  // collection and is usually only performed as part of (de)serialization or
  // heap verification.
  void Iterate(RootVisitor* visitor);

533
 private:
Dan Elphick's avatar
Dan Elphick committed
534
  RootsTable& roots_table_;
535 536 537 538 539 540
};

}  // namespace internal
}  // namespace v8

#endif  // V8_ROOTS_H_