1// Copyright 2016 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_CODEGEN_EXTERNAL_REFERENCE_TABLE_H_
6#define V8_CODEGEN_EXTERNAL_REFERENCE_TABLE_H_
7
8#include "src/builtins/accessors.h"
9#include "src/builtins/builtins.h"
10#include "src/codegen/external-reference.h"
11#include "src/logging/counters-definitions.h"
12
13namespace v8 {
14namespace internal {
15
16class Isolate;
17
18// ExternalReferenceTable is a helper class that defines the relationship
19// between external references and their encodings. It is used to build
20// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
21class ExternalReferenceTable {
22 public:
23  // For the nullptr ref, see the constructor.
24  static constexpr int kSpecialReferenceCount = 1;
25  static constexpr int kExternalReferenceCountIsolateIndependent =
26      ExternalReference::kExternalReferenceCountIsolateIndependent;
27  static constexpr int kExternalReferenceCountIsolateDependent =
28      ExternalReference::kExternalReferenceCountIsolateDependent;
29  static constexpr int kBuiltinsReferenceCount =
30#define COUNT_C_BUILTIN(...) +1
31      BUILTIN_LIST_C(COUNT_C_BUILTIN);
32#undef COUNT_C_BUILTIN
33  static constexpr int kRuntimeReferenceCount =
34      Runtime::kNumFunctions -
35      Runtime::kNumInlineFunctions;  // Don't count dupe kInline... functions.
36  static constexpr int kIsolateAddressReferenceCount = kIsolateAddressCount;
37  static constexpr int kAccessorReferenceCount =
38      Accessors::kAccessorInfoCount + Accessors::kAccessorSetterCount;
39  // The number of stub cache external references, see AddStubCache.
40  static constexpr int kStubCacheReferenceCount = 12;
41  static constexpr int kStatsCountersReferenceCount =
42#define SC(...) +1
43      STATS_COUNTER_NATIVE_CODE_LIST(SC);
44#undef SC
45  static constexpr int kSizeIsolateIndependent =
46      kSpecialReferenceCount + kExternalReferenceCountIsolateIndependent +
47      kBuiltinsReferenceCount + kRuntimeReferenceCount +
48      kAccessorReferenceCount;
49  static constexpr int kSize =
50      kSizeIsolateIndependent + kExternalReferenceCountIsolateDependent +
51      kIsolateAddressReferenceCount + kStubCacheReferenceCount +
52      kStatsCountersReferenceCount;
53  static constexpr uint32_t kEntrySize =
54      static_cast<uint32_t>(kSystemPointerSize);
55  static constexpr uint32_t kSizeInBytes = kSize * kEntrySize + 2 * kUInt32Size;
56
57  Address address(uint32_t i) const { return ref_addr_[i]; }
58  const char* name(uint32_t i) const { return ref_name_[i]; }
59
60  bool is_initialized() const { return is_initialized_ != 0; }
61
62  static const char* ResolveSymbol(void* address);
63
64  static constexpr uint32_t OffsetOfEntry(uint32_t i) {
65    // Used in CodeAssembler::LookupExternalReference.
66    return i * kEntrySize;
67  }
68
69  static void InitializeOncePerProcess();
70  static const char* NameOfIsolateIndependentAddress(Address address);
71
72  const char* NameFromOffset(uint32_t offset) {
73    DCHECK_EQ(offset % kEntrySize, 0);
74    DCHECK_LT(offset, kSizeInBytes);
75    int index = offset / kEntrySize;
76    return name(index);
77  }
78
79  ExternalReferenceTable() = default;
80  ExternalReferenceTable(const ExternalReferenceTable&) = delete;
81  ExternalReferenceTable& operator=(const ExternalReferenceTable&) = delete;
82  void Init(Isolate* isolate);
83
84 private:
85  static void AddIsolateIndependent(Address address, int* index);
86
87  static void AddIsolateIndependentReferences(int* index);
88  static void AddBuiltins(int* index);
89  static void AddRuntimeFunctions(int* index);
90  static void AddAccessors(int* index);
91
92  void Add(Address address, int* index);
93
94  void CopyIsolateIndependentReferences(int* index);
95  void AddIsolateDependentReferences(Isolate* isolate, int* index);
96  void AddIsolateAddresses(Isolate* isolate, int* index);
97  void AddStubCache(Isolate* isolate, int* index);
98
99  Address GetStatsCounterAddress(StatsCounter* counter);
100  void AddNativeCodeStatsCounters(Isolate* isolate, int* index);
101
102  STATIC_ASSERT(sizeof(Address) == kEntrySize);
103  Address ref_addr_[kSize];
104  static const char* const ref_name_[kSize];
105
106  // Not bool to guarantee deterministic size.
107  uint32_t is_initialized_ = 0;
108
109  // Redirect disabled stats counters to this field. This is done to make sure
110  // we can have a snapshot that includes native counters even when the embedder
111  // isn't collecting them.
112  // This field is uint32_t since the MacroAssembler and CodeStubAssembler
113  // accesses this field as a uint32_t.
114  uint32_t dummy_stats_counter_ = 0;
115};
116
117STATIC_ASSERT(ExternalReferenceTable::kSizeInBytes ==
118              sizeof(ExternalReferenceTable));
119
120}  // namespace internal
121}  // namespace v8
122#endif  // V8_CODEGEN_EXTERNAL_REFERENCE_TABLE_H_
123