V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
constants-table-builder.cc
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 #include "src/builtins/constants-table-builder.h"
6 
7 #include "src/heap/heap-inl.h"
8 #include "src/isolate.h"
9 #include "src/roots-inl.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 BuiltinsConstantsTableBuilder::BuiltinsConstantsTableBuilder(Isolate* isolate)
15  : isolate_(isolate), map_(isolate->heap()) {
16  // Ensure this is only called once per Isolate.
17  DCHECK_EQ(ReadOnlyRoots(isolate_).empty_fixed_array(),
18  isolate_->heap()->builtins_constants_table());
19 
20  // And that the initial value of the builtins constants table can be treated
21  // as a constant, which means that codegen will load it using the root
22  // register.
23  DCHECK(RootsTable::IsImmortalImmovable(RootIndex::kEmptyFixedArray));
24 }
25 
26 uint32_t BuiltinsConstantsTableBuilder::AddObject(Handle<Object> object) {
27 #ifdef DEBUG
28  // Roots must not be inserted into the constants table as they are already
29  // accessibly from the root list.
30  RootIndex root_list_index;
31  DCHECK(!isolate_->roots_table().IsRootHandle(object, &root_list_index));
32 
33  // Not yet finalized.
34  DCHECK_EQ(ReadOnlyRoots(isolate_).empty_fixed_array(),
35  isolate_->heap()->builtins_constants_table());
36 
37  // Must be on the main thread.
38  DCHECK(ThreadId::Current().Equals(isolate_->thread_id()));
39 
40  // Must be generating embedded builtin code.
41  DCHECK(isolate_->ShouldLoadConstantsFromRootList());
42 #endif
43 
44  uint32_t* maybe_key = map_.Find(object);
45  if (maybe_key == nullptr) {
46  DCHECK(object->IsHeapObject());
47  uint32_t index = map_.size();
48  map_.Set(object, index);
49  return index;
50  } else {
51  return *maybe_key;
52  }
53 }
54 
55 void BuiltinsConstantsTableBuilder::PatchSelfReference(
56  Handle<Object> self_reference, Handle<Code> code_object) {
57 #ifdef DEBUG
58  // Roots must not be inserted into the constants table as they are already
59  // accessibly from the root list.
60  RootIndex root_list_index;
61  DCHECK(!isolate_->roots_table().IsRootHandle(code_object, &root_list_index));
62 
63  // Not yet finalized.
64  DCHECK_EQ(ReadOnlyRoots(isolate_).empty_fixed_array(),
65  isolate_->heap()->builtins_constants_table());
66 
67  DCHECK(isolate_->ShouldLoadConstantsFromRootList());
68 
69  DCHECK(self_reference->IsOddball());
70  DCHECK(Oddball::cast(*self_reference)->kind() ==
71  Oddball::kSelfReferenceMarker);
72 
73  // During indirection generation, we always create a distinct marker for each
74  // macro assembler. The canonical marker is only used when not generating a
75  // snapshot.
76  DCHECK(*self_reference != ReadOnlyRoots(isolate_).self_reference_marker());
77 #endif
78 
79  uint32_t key;
80  if (map_.Delete(self_reference, &key)) {
81  DCHECK(code_object->IsCode());
82  map_.Set(code_object, key);
83  }
84 }
85 
86 void BuiltinsConstantsTableBuilder::Finalize() {
87  HandleScope handle_scope(isolate_);
88 
89  DCHECK_EQ(ReadOnlyRoots(isolate_).empty_fixed_array(),
90  isolate_->heap()->builtins_constants_table());
91  DCHECK(isolate_->ShouldLoadConstantsFromRootList());
92 
93  // An empty map means there's nothing to do.
94  if (map_.size() == 0) return;
95 
96  Handle<FixedArray> table =
97  isolate_->factory()->NewFixedArray(map_.size(), TENURED);
98 
99  Builtins* builtins = isolate_->builtins();
100  ConstantsMap::IteratableScope it_scope(&map_);
101  for (auto it = it_scope.begin(); it != it_scope.end(); ++it) {
102  uint32_t index = *it.entry();
103  Object* value = it.key();
104  if (value->IsCode() && Code::cast(value)->kind() == Code::BUILTIN) {
105  // Replace placeholder code objects with the real builtin.
106  // See also: SetupIsolateDelegate::PopulateWithPlaceholders.
107  // TODO(jgruber): Deduplicate placeholders and their corresponding
108  // builtin.
109  value = builtins->builtin(Code::cast(value)->builtin_index());
110  }
111  DCHECK(value->IsHeapObject());
112  table->set(index, value);
113  }
114 
115 #ifdef DEBUG
116  for (int i = 0; i < map_.size(); i++) {
117  DCHECK(table->get(i)->IsHeapObject());
118  DCHECK_NE(ReadOnlyRoots(isolate_).undefined_value(), table->get(i));
119  DCHECK_NE(ReadOnlyRoots(isolate_).self_reference_marker(), table->get(i));
120  }
121 #endif
122 
123  isolate_->heap()->SetBuiltinsConstantsTable(*table);
124 }
125 
126 } // namespace internal
127 } // namespace v8
Definition: libplatform.h:13