1// Copyright 2018 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_BUILTINS_CONSTANTS_TABLE_BUILDER_H_
6#define V8_BUILTINS_CONSTANTS_TABLE_BUILDER_H_
7
8#include "src/base/macros.h"
9#include "src/utils/allocation.h"
10#include "src/utils/identity-map.h"
11#include "src/handles/handles.h"
12
13namespace v8 {
14namespace internal {
15
16class Isolate;
17class Object;
18
19// Utility class to build the builtins constants table and store it on the root
20// list. The constants table contains constants used by builtins, and is there
21// to avoid directly embedding them into code objects, which would not be
22// possible for off-heap (and thus immutable) code objects.
23class BuiltinsConstantsTableBuilder final {
24 public:
25  explicit BuiltinsConstantsTableBuilder(Isolate* isolate);
26
27  BuiltinsConstantsTableBuilder(const BuiltinsConstantsTableBuilder&) = delete;
28  BuiltinsConstantsTableBuilder& operator=(
29      const BuiltinsConstantsTableBuilder&) = delete;
30
31  // Returns the index within the builtins constants table for the given
32  // object, possibly adding the object to the table. Objects are deduplicated.
33  uint32_t AddObject(Handle<Object> object);
34
35  // Self-references during code generation start out by referencing a handle
36  // with a temporary dummy object. Once the final Code object exists, such
37  // entries in the constants map must be patched up.
38  void PatchSelfReference(Handle<Object> self_reference,
39                          Handle<Code> code_object);
40
41  // References to the array that stores basic block usage counters start out as
42  // references to a unique oddball. Once the actual array has been allocated,
43  // such entries in the constants map must be patched up.
44  void PatchBasicBlockCountersReference(Handle<ByteArray> counters);
45
46  // Should be called after all affected code (e.g. builtins and bytecode
47  // handlers) has been generated.
48  void Finalize();
49
50 private:
51  Isolate* isolate_;
52
53  // Maps objects to corresponding indices within the constants list.
54  using ConstantsMap = IdentityMap<uint32_t, FreeStoreAllocationPolicy>;
55  ConstantsMap map_;
56};
57
58}  // namespace internal
59}  // namespace v8
60
61#endif  // V8_BUILTINS_CONSTANTS_TABLE_BUILDER_H_
62