external-reference-table.h 3.6 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef V8_EXTERNAL_REFERENCE_TABLE_H_
#define V8_EXTERNAL_REFERENCE_TABLE_H_

8 9
#include <vector>

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

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:
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  // 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 =
      Runtime::kNumFunctions / 2;  // Don't count dupe kInline... functions.
  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;
  static constexpr int kSize =
      kSpecialReferenceCount + kExternalReferenceCount +
      kBuiltinsReferenceCount + kRuntimeReferenceCount +
      kIsolateAddressReferenceCount + kAccessorReferenceCount +
      kStubCacheReferenceCount;

45
  static constexpr uint32_t size() { return static_cast<uint32_t>(kSize); }
46 47
  Address address(uint32_t i) { return refs_[i].address; }
  const char* name(uint32_t i) { return refs_[i].name; }
48

49
  bool is_initialized() const { return is_initialized_ != 0; }
50

51
  static const char* ResolveSymbol(void* address);
52

53 54 55 56
  static constexpr uint32_t EntrySize() {
    return sizeof(ExternalReferenceEntry);
  }

57
  static constexpr uint32_t OffsetOfEntry(uint32_t i) {
58 59
    // Used in CodeAssembler::LookupExternalReference.
    STATIC_ASSERT(offsetof(ExternalReferenceEntry, address) == 0);
60 61 62 63 64 65 66 67
    return i * EntrySize();
  }

  const char* NameFromOffset(uint32_t offset) {
    DCHECK_EQ(offset % EntrySize(), 0);
    DCHECK_LT(offset, SizeInBytes());
    int index = offset / EntrySize();
    return name(index);
68 69
  }

70 71 72 73 74 75
  static constexpr uint32_t SizeInBytes() {
    STATIC_ASSERT(OffsetOfEntry(size()) + 2 * kUInt32Size ==
                  sizeof(ExternalReferenceTable));
    return OffsetOfEntry(size()) + 2 * kUInt32Size;
  }

76 77 78
  ExternalReferenceTable() {}
  void Init(Isolate* isolate);

79 80 81 82
 private:
  struct ExternalReferenceEntry {
    Address address;
    const char* name;
83

84
    ExternalReferenceEntry() : address(kNullAddress), name(nullptr) {}
85 86
    ExternalReferenceEntry(Address address, const char* name)
        : address(address), name(name) {}
87 88
  };

89
  void Add(Address address, const char* name, int* index);
90

91
  void AddReferences(Isolate* isolate, int* index);
92 93
  void AddBuiltins(int* index);
  void AddRuntimeFunctions(int* index);
94
  void AddIsolateAddresses(Isolate* isolate, int* index);
95
  void AddAccessors(int* index);
96
  void AddStubCache(Isolate* isolate, int* index);
97

98
  ExternalReferenceEntry refs_[kSize];
99 100
  uint32_t is_initialized_ = 0;  // Not bool to guarantee deterministic size.
  uint32_t unused_padding_ = 0;  // For alignment.
101

102 103 104 105 106 107
  DISALLOW_COPY_AND_ASSIGN(ExternalReferenceTable);
};

}  // namespace internal
}  // namespace v8
#endif  // V8_EXTERNAL_REFERENCE_TABLE_H_