1// Copyright 2020 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_ENCODER_H_
6#define V8_CODEGEN_EXTERNAL_REFERENCE_ENCODER_H_
7
8#include "src/base/bit-field.h"
9#include "src/common/globals.h"
10#include "src/utils/address-map.h"
11
12namespace v8 {
13namespace internal {
14
15class Isolate;
16
17class ExternalReferenceEncoder {
18 public:
19  class Value {
20   public:
21    explicit Value(uint32_t raw) : value_(raw) {}
22    Value() : value_(0) {}
23    static uint32_t Encode(uint32_t index, bool is_from_api) {
24      return Index::encode(index) | IsFromAPI::encode(is_from_api);
25    }
26
27    bool is_from_api() const { return IsFromAPI::decode(value_); }
28    uint32_t index() const { return Index::decode(value_); }
29
30   private:
31    using Index = base::BitField<uint32_t, 0, 31>;
32    using IsFromAPI = base::BitField<bool, 31, 1>;
33    uint32_t value_;
34  };
35
36  explicit ExternalReferenceEncoder(Isolate* isolate);
37  ExternalReferenceEncoder(const ExternalReferenceEncoder&) = delete;
38  ExternalReferenceEncoder& operator=(const ExternalReferenceEncoder&) = delete;
39#ifdef DEBUG
40  ~ExternalReferenceEncoder();
41#endif  // DEBUG
42
43  Value Encode(Address key);
44  Maybe<Value> TryEncode(Address key);
45
46  const char* NameOfAddress(Isolate* isolate, Address address) const;
47
48 private:
49  AddressToIndexHashMap* map_;
50
51#ifdef DEBUG
52  std::vector<int> count_;
53  const intptr_t* api_references_;
54#endif  // DEBUG
55};
56
57}  // namespace internal
58}  // namespace v8
59
60#endif  // V8_CODEGEN_EXTERNAL_REFERENCE_ENCODER_H_
61