external-reference-table.h 3.93 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5 6
#ifndef V8_CODEGEN_EXTERNAL_REFERENCE_TABLE_H_
#define V8_CODEGEN_EXTERNAL_REFERENCE_TABLE_H_
7

8 9
#include <vector>

10
#include "src/builtins/accessors.h"
11
#include "src/builtins/builtins.h"
12
#include "src/codegen/external-reference.h"
13
#include "src/logging/counters-definitions.h"
14 15 16 17 18 19 20 21 22 23 24

namespace v8 {
namespace internal {

class Isolate;

// ExternalReferenceTable is a helper class that defines the relationship
// between external references and their encodings. It is used to build
// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
class ExternalReferenceTable {
 public:
25 26 27 28 29 30 31 32 33
  // For the nullptr ref, see the constructor.
  static constexpr int kSpecialReferenceCount = 1;
  static constexpr int kExternalReferenceCount =
      ExternalReference::kExternalReferenceCount;
  static constexpr int kBuiltinsReferenceCount =
#define COUNT_C_BUILTIN(...) +1
      BUILTIN_LIST_C(COUNT_C_BUILTIN);
#undef COUNT_C_BUILTIN
  static constexpr int kRuntimeReferenceCount =
34 35
      Runtime::kNumFunctions -
      Runtime::kNumInlineFunctions;  // Don't count dupe kInline... functions.
36 37 38 39 40
  static constexpr int kIsolateAddressReferenceCount = kIsolateAddressCount;
  static constexpr int kAccessorReferenceCount =
      Accessors::kAccessorInfoCount + Accessors::kAccessorSetterCount;
  // The number of stub cache external references, see AddStubCache.
  static constexpr int kStubCacheReferenceCount = 12;
41 42 43 44
  static constexpr int kStatsCountersReferenceCount =
#define SC(...) +1
      STATS_COUNTER_NATIVE_CODE_LIST(SC);
#undef SC
45 46 47 48
  static constexpr int kSize =
      kSpecialReferenceCount + kExternalReferenceCount +
      kBuiltinsReferenceCount + kRuntimeReferenceCount +
      kIsolateAddressReferenceCount + kAccessorReferenceCount +
49
      kStubCacheReferenceCount + kStatsCountersReferenceCount;
50 51
  static constexpr uint32_t kEntrySize =
      static_cast<uint32_t>(kSystemPointerSize);
52
  static constexpr uint32_t kSizeInBytes = kSize * kEntrySize + 2 * kUInt32Size;
53

54 55
  Address address(uint32_t i) const { return ref_addr_[i]; }
  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 68 69
    DCHECK_EQ(offset % kEntrySize, 0);
    DCHECK_LT(offset, kSizeInBytes);
    int index = offset / kEntrySize;
70
    return name(index);
71 72
  }

73
  ExternalReferenceTable() = default;
74 75
  void Init(Isolate* isolate);

76
 private:
77
  void Add(Address address, int* index);
78

79
  void AddReferences(Isolate* isolate, int* index);
80 81
  void AddBuiltins(int* index);
  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 87 88
  Address GetStatsCounterAddress(StatsCounter* counter);
  void AddNativeCodeStatsCounters(Isolate* isolate, int* index);

89 90 91 92
  STATIC_ASSERT(sizeof(Address) == kEntrySize);
  Address ref_addr_[kSize];
  static const char* const ref_name_[kSize];

93 94 95 96 97 98 99 100 101
  // Not bool to guarantee deterministic size.
  uint32_t is_initialized_ = 0;

  // Redirect disabled stats counters to this field. This is done to make sure
  // we can have a snapshot that includes native counters even when the embedder
  // isn't collecting them.
  // This field is uint32_t since the MacroAssembler and CodeStubAssembler
  // accesses this field as a uint32_t.
  uint32_t dummy_stats_counter_ = 0;
102

103 104 105
  DISALLOW_COPY_AND_ASSIGN(ExternalReferenceTable);
};

106 107 108
STATIC_ASSERT(ExternalReferenceTable::kSizeInBytes ==
              sizeof(ExternalReferenceTable));

109 110
}  // namespace internal
}  // namespace v8
111
#endif  // V8_CODEGEN_EXTERNAL_REFERENCE_TABLE_H_