11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_CODEGEN_EXTERNAL_REFERENCE_TABLE_H_
61cb0ef41Sopenharmony_ci#define V8_CODEGEN_EXTERNAL_REFERENCE_TABLE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/builtins/accessors.h"
91cb0ef41Sopenharmony_ci#include "src/builtins/builtins.h"
101cb0ef41Sopenharmony_ci#include "src/codegen/external-reference.h"
111cb0ef41Sopenharmony_ci#include "src/logging/counters-definitions.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciclass Isolate;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// ExternalReferenceTable is a helper class that defines the relationship
191cb0ef41Sopenharmony_ci// between external references and their encodings. It is used to build
201cb0ef41Sopenharmony_ci// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
211cb0ef41Sopenharmony_ciclass ExternalReferenceTable {
221cb0ef41Sopenharmony_ci public:
231cb0ef41Sopenharmony_ci  // For the nullptr ref, see the constructor.
241cb0ef41Sopenharmony_ci  static constexpr int kSpecialReferenceCount = 1;
251cb0ef41Sopenharmony_ci  static constexpr int kExternalReferenceCountIsolateIndependent =
261cb0ef41Sopenharmony_ci      ExternalReference::kExternalReferenceCountIsolateIndependent;
271cb0ef41Sopenharmony_ci  static constexpr int kExternalReferenceCountIsolateDependent =
281cb0ef41Sopenharmony_ci      ExternalReference::kExternalReferenceCountIsolateDependent;
291cb0ef41Sopenharmony_ci  static constexpr int kBuiltinsReferenceCount =
301cb0ef41Sopenharmony_ci#define COUNT_C_BUILTIN(...) +1
311cb0ef41Sopenharmony_ci      BUILTIN_LIST_C(COUNT_C_BUILTIN);
321cb0ef41Sopenharmony_ci#undef COUNT_C_BUILTIN
331cb0ef41Sopenharmony_ci  static constexpr int kRuntimeReferenceCount =
341cb0ef41Sopenharmony_ci      Runtime::kNumFunctions -
351cb0ef41Sopenharmony_ci      Runtime::kNumInlineFunctions;  // Don't count dupe kInline... functions.
361cb0ef41Sopenharmony_ci  static constexpr int kIsolateAddressReferenceCount = kIsolateAddressCount;
371cb0ef41Sopenharmony_ci  static constexpr int kAccessorReferenceCount =
381cb0ef41Sopenharmony_ci      Accessors::kAccessorInfoCount + Accessors::kAccessorSetterCount;
391cb0ef41Sopenharmony_ci  // The number of stub cache external references, see AddStubCache.
401cb0ef41Sopenharmony_ci  static constexpr int kStubCacheReferenceCount = 12;
411cb0ef41Sopenharmony_ci  static constexpr int kStatsCountersReferenceCount =
421cb0ef41Sopenharmony_ci#define SC(...) +1
431cb0ef41Sopenharmony_ci      STATS_COUNTER_NATIVE_CODE_LIST(SC);
441cb0ef41Sopenharmony_ci#undef SC
451cb0ef41Sopenharmony_ci  static constexpr int kSizeIsolateIndependent =
461cb0ef41Sopenharmony_ci      kSpecialReferenceCount + kExternalReferenceCountIsolateIndependent +
471cb0ef41Sopenharmony_ci      kBuiltinsReferenceCount + kRuntimeReferenceCount +
481cb0ef41Sopenharmony_ci      kAccessorReferenceCount;
491cb0ef41Sopenharmony_ci  static constexpr int kSize =
501cb0ef41Sopenharmony_ci      kSizeIsolateIndependent + kExternalReferenceCountIsolateDependent +
511cb0ef41Sopenharmony_ci      kIsolateAddressReferenceCount + kStubCacheReferenceCount +
521cb0ef41Sopenharmony_ci      kStatsCountersReferenceCount;
531cb0ef41Sopenharmony_ci  static constexpr uint32_t kEntrySize =
541cb0ef41Sopenharmony_ci      static_cast<uint32_t>(kSystemPointerSize);
551cb0ef41Sopenharmony_ci  static constexpr uint32_t kSizeInBytes = kSize * kEntrySize + 2 * kUInt32Size;
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  Address address(uint32_t i) const { return ref_addr_[i]; }
581cb0ef41Sopenharmony_ci  const char* name(uint32_t i) const { return ref_name_[i]; }
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  bool is_initialized() const { return is_initialized_ != 0; }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  static const char* ResolveSymbol(void* address);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  static constexpr uint32_t OffsetOfEntry(uint32_t i) {
651cb0ef41Sopenharmony_ci    // Used in CodeAssembler::LookupExternalReference.
661cb0ef41Sopenharmony_ci    return i * kEntrySize;
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  static void InitializeOncePerProcess();
701cb0ef41Sopenharmony_ci  static const char* NameOfIsolateIndependentAddress(Address address);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  const char* NameFromOffset(uint32_t offset) {
731cb0ef41Sopenharmony_ci    DCHECK_EQ(offset % kEntrySize, 0);
741cb0ef41Sopenharmony_ci    DCHECK_LT(offset, kSizeInBytes);
751cb0ef41Sopenharmony_ci    int index = offset / kEntrySize;
761cb0ef41Sopenharmony_ci    return name(index);
771cb0ef41Sopenharmony_ci  }
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  ExternalReferenceTable() = default;
801cb0ef41Sopenharmony_ci  ExternalReferenceTable(const ExternalReferenceTable&) = delete;
811cb0ef41Sopenharmony_ci  ExternalReferenceTable& operator=(const ExternalReferenceTable&) = delete;
821cb0ef41Sopenharmony_ci  void Init(Isolate* isolate);
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci private:
851cb0ef41Sopenharmony_ci  static void AddIsolateIndependent(Address address, int* index);
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  static void AddIsolateIndependentReferences(int* index);
881cb0ef41Sopenharmony_ci  static void AddBuiltins(int* index);
891cb0ef41Sopenharmony_ci  static void AddRuntimeFunctions(int* index);
901cb0ef41Sopenharmony_ci  static void AddAccessors(int* index);
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  void Add(Address address, int* index);
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  void CopyIsolateIndependentReferences(int* index);
951cb0ef41Sopenharmony_ci  void AddIsolateDependentReferences(Isolate* isolate, int* index);
961cb0ef41Sopenharmony_ci  void AddIsolateAddresses(Isolate* isolate, int* index);
971cb0ef41Sopenharmony_ci  void AddStubCache(Isolate* isolate, int* index);
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  Address GetStatsCounterAddress(StatsCounter* counter);
1001cb0ef41Sopenharmony_ci  void AddNativeCodeStatsCounters(Isolate* isolate, int* index);
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  STATIC_ASSERT(sizeof(Address) == kEntrySize);
1031cb0ef41Sopenharmony_ci  Address ref_addr_[kSize];
1041cb0ef41Sopenharmony_ci  static const char* const ref_name_[kSize];
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  // Not bool to guarantee deterministic size.
1071cb0ef41Sopenharmony_ci  uint32_t is_initialized_ = 0;
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  // Redirect disabled stats counters to this field. This is done to make sure
1101cb0ef41Sopenharmony_ci  // we can have a snapshot that includes native counters even when the embedder
1111cb0ef41Sopenharmony_ci  // isn't collecting them.
1121cb0ef41Sopenharmony_ci  // This field is uint32_t since the MacroAssembler and CodeStubAssembler
1131cb0ef41Sopenharmony_ci  // accesses this field as a uint32_t.
1141cb0ef41Sopenharmony_ci  uint32_t dummy_stats_counter_ = 0;
1151cb0ef41Sopenharmony_ci};
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ciSTATIC_ASSERT(ExternalReferenceTable::kSizeInBytes ==
1181cb0ef41Sopenharmony_ci              sizeof(ExternalReferenceTable));
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci}  // namespace internal
1211cb0ef41Sopenharmony_ci}  // namespace v8
1221cb0ef41Sopenharmony_ci#endif  // V8_CODEGEN_EXTERNAL_REFERENCE_TABLE_H_
123