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_EXTERNAL_REFERENCE_TABLE_H_
6#define V8_EXTERNAL_REFERENCE_TABLE_H_
7
8#include <vector>
9
10#include "src/accessors.h"
11#include "src/builtins/builtins.h"
12#include "src/counters-definitions.h"
13#include "src/external-reference.h"
14
15namespace v8 {
16namespace internal {
17
18class Isolate;
19
20// ExternalReferenceTable is a helper class that defines the relationship
21// between external references and their encodings. It is used to build
22// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
23class ExternalReferenceTable {
24 public:
25 // For the nullptr ref, see the constructor.
26 static constexpr int kSpecialReferenceCount = 1;
27 static constexpr int kExternalReferenceCount =
28 ExternalReference::kExternalReferenceCount;
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 kSize =
46 kSpecialReferenceCount + kExternalReferenceCount +
47 kBuiltinsReferenceCount + kRuntimeReferenceCount +
48 kIsolateAddressReferenceCount + kAccessorReferenceCount +
49 kStubCacheReferenceCount + kStatsCountersReferenceCount;
50 static constexpr uint32_t kEntrySize =
51 static_cast<uint32_t>(kSystemPointerSize);
52 static constexpr uint32_t kSizeInBytes = kSize * kEntrySize + 2 * kUInt32Size;
53
54 Address address(uint32_t i) const { return ref_addr_[i]; }
55 const char* name(uint32_t i) const { return ref_name_[i]; }
56
57 bool is_initialized() const { return is_initialized_ != 0; }
58
59 static const char* ResolveSymbol(void* address);
60
61 static constexpr uint32_t OffsetOfEntry(uint32_t i) {
62 // Used in CodeAssembler::LookupExternalReference.
63 return i * kEntrySize;
64 }
65
66 const char* NameFromOffset(uint32_t offset) {
67 DCHECK_EQ(offset % kEntrySize, 0);
68 DCHECK_LT(offset, kSizeInBytes);
69 int index = offset / kEntrySize;
70 return name(index);
71 }
72
73 ExternalReferenceTable() = default;
74 void Init(Isolate* isolate);
75
76 private:
77 void Add(Address address, int* index);
78
79 void AddReferences(Isolate* isolate, int* index);
80 void AddBuiltins(int* index);
81 void AddRuntimeFunctions(int* index);
82 void AddIsolateAddresses(Isolate* isolate, int* index);
83 void AddAccessors(int* index);
84 void AddStubCache(Isolate* isolate, int* index);
85
86 Address GetStatsCounterAddress(StatsCounter* counter);
87 void AddNativeCodeStatsCounters(Isolate* isolate, int* index);
88
89 STATIC_ASSERT(sizeof(Address) == kEntrySize);
90 Address ref_addr_[kSize];
91 static const char* const ref_name_[kSize];
92
93 // Not bool to guarantee deterministic size.
94 uint32_t is_initialized_ = 0;
95
96 // Redirect disabled stats counters to this field. This is done to make sure
97 // we can have a snapshot that includes native counters even when the embedder
98 // isn't collecting them.
99 // This field is uint32_t since the MacroAssembler and CodeStubAssembler
100 // accesses this field as a uint32_t.
101 uint32_t dummy_stats_counter_ = 0;
102
103 DISALLOW_COPY_AND_ASSIGN(ExternalReferenceTable);
104};
105
106STATIC_ASSERT(ExternalReferenceTable::kSizeInBytes ==
107 sizeof(ExternalReferenceTable));
108
109} // namespace internal
110} // namespace v8
111#endif // V8_EXTERNAL_REFERENCE_TABLE_H_
112