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 "src/address-map.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 class Isolate;
14 
15 // ExternalReferenceTable is a helper class that defines the relationship
16 // between external references and their encodings. It is used to build
17 // hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
18 class ExternalReferenceTable {
19  public:
20   static ExternalReferenceTable* instance(Isolate* isolate);
21 
22   uint32_t size() const { return static_cast<uint32_t>(refs_.length()); }
23   Address address(uint32_t i) { return refs_[i].address; }
24   const char* name(uint32_t i) { return refs_[i].name; }
25   bool is_api_reference(uint32_t i) { return i >= api_refs_start_; }
26 
27 #ifdef DEBUG
28   void increment_count(uint32_t i) { refs_[i].count++; }
29   int count(uint32_t i) { return refs_[i].count; }
30   void ResetCount();
31   void PrintCount();
32 #endif  // DEBUG
33 
34   static const char* ResolveSymbol(void* address);
35 
36   static const int kDeoptTableSerializeEntryCount = 64;
37 
38  private:
39   struct ExternalReferenceEntry {
40     Address address;
41     const char* name;
42 #ifdef DEBUG
43     int count;
44 #endif  // DEBUG
45   };
46 
47   explicit ExternalReferenceTable(Isolate* isolate);
48 
49   void Add(Address address, const char* name) {
50 #ifdef DEBUG
51     ExternalReferenceEntry entry = {address, name, 0};
52 #else
53     ExternalReferenceEntry entry = {address, name};
54 #endif  // DEBUG
55     refs_.Add(entry);
56   }
57 
58   void AddReferences(Isolate* isolate);
59   void AddBuiltins(Isolate* isolate);
60   void AddRuntimeFunctions(Isolate* isolate);
61   void AddIsolateAddresses(Isolate* isolate);
62   void AddAccessors(Isolate* isolate);
63   void AddStubCache(Isolate* isolate);
64   void AddDeoptEntries(Isolate* isolate);
65   void AddApiReferences(Isolate* isolate);
66 
67   List<ExternalReferenceEntry> refs_;
68   uint32_t api_refs_start_;
69 
70   DISALLOW_COPY_AND_ASSIGN(ExternalReferenceTable);
71 };
72 
73 }  // namespace internal
74 }  // namespace v8
75 #endif  // V8_EXTERNAL_REFERENCE_TABLE_H_
76