v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
static-roots-gen.cc
Go to the documentation of this file.
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
6
7#include <fstream>
8
16#include "src/roots/roots-inl.h"
17#include "src/roots/roots.h"
18
19namespace v8 {
20namespace internal {
21
23 public:
24 explicit StaticRootsTableGenImpl(Isolate* isolate) {
25 // Collect all roots
26 ReadOnlyRoots ro_roots(isolate);
27 {
29#define ADD_ROOT(_, value, CamelName) \
30 { \
31 Tagged_t ptr = V8HeapCompressionScheme::CompressObject( \
32 ro_roots.unchecked_##value().ptr()); \
33 sorted_roots_[ptr].push_back(pos); \
34 camel_names_[RootIndex::k##CamelName] = #CamelName; \
35 ++pos; \
36 }
38#undef ADD_ROOT
39 }
40 }
41
42 const std::map<Tagged_t, std::list<RootIndex>>& sorted_roots() {
43 return sorted_roots_;
44 }
45
46 const std::string& camel_name(RootIndex idx) { return camel_names_.at(idx); }
47
48 private:
49 std::map<Tagged_t, std::list<RootIndex>> sorted_roots_;
50 std::unordered_map<RootIndex, std::string> camel_names_;
51};
52
53void StaticRootsTableGen::write(Isolate* isolate, const char* file) {
55 "Re-generating the table of roots is only supported in builds "
56 "with v8_enable_static_roots disabled");
58 CHECK(file);
59 static_assert(static_cast<int>(RootIndex::kFirstReadOnlyRoot) == 0);
60
61 std::ofstream out(file, std::ios::binary);
62
63 out << "// Copyright 2022 the V8 project authors. All rights reserved.\n"
64 << "// Use of this source code is governed by a BSD-style license "
65 "that can be\n"
66 << "// found in the LICENSE file.\n"
67 << "\n"
68 << "// This file is automatically generated by "
69 "`tools/dev/gen-static-roots.py`. Do\n// not edit manually.\n"
70 << "\n"
71 << "#ifndef V8_ROOTS_STATIC_ROOTS_H_\n"
72 << "#define V8_ROOTS_STATIC_ROOTS_H_\n"
73 << "\n"
74 << "#include \"src/common/globals.h\"\n"
75 << "\n"
76 << "#if V8_STATIC_ROOTS_BOOL\n"
77 << "\n"
78 << "#include \"src/roots/roots.h\"\n"
79 << "\n"
80 << "// Disabling Wasm or Intl invalidates the contents of "
81 "static-roots.h.\n"
82 << "// TODO(olivf): To support static roots for multiple build "
83 "configurations we\n"
84 << "// will need to generate target specific versions of "
85 "this file.\n"
86 << "static_assert(V8_ENABLE_WEBASSEMBLY);\n"
87 << "static_assert(V8_INTL_SUPPORT);\n"
88 << "\n"
89 << "namespace v8 {\n"
90 << "namespace internal {\n"
91 << "\n"
92 << "struct StaticReadOnlyRoot {\n";
93
94 // Output a symbol for every root. Ordered by ptr to make it easier to see the
95 // memory layout of the read only page.
96 const auto size = static_cast<int>(RootIndex::kReadOnlyRootsCount);
98
99 for (auto& entry : gen.sorted_roots()) {
100 Tagged_t ptr = entry.first;
102 const std::list<RootIndex>& roots = entry.second;
103
104 for (RootIndex root : roots) {
105 static const char* kPreString = " static constexpr Tagged_t k";
106 const std::string& name = gen.camel_name(root);
107 size_t ptr_len = ceil(log2(ptr) / 4.0);
108 // Full line is: "kPreString|name = 0x.....;"
109 size_t len = strlen(kPreString) + name.length() + 5 + ptr_len + 1;
110 out << kPreString << name << " =";
111 if (len > 80) out << "\n ";
112 out << " 0x" << std::hex << ptr << std::dec << ";\n";
113 }
114 }
115
116 out << "\n";
117 out << " static constexpr Tagged_t kFirstAllocatedRoot = 0x" << std::hex
118 << gen.sorted_roots().cbegin()->first << std::dec << ";\n";
119 out << " static constexpr Tagged_t kLastAllocatedRoot = 0x" << std::hex
120 << gen.sorted_roots().crbegin()->first << std::dec << ";\n";
121 out << "};\n";
122
123 // Output in order of roots table
124 out << "\nstatic constexpr std::array<Tagged_t, " << size
125 << "> StaticReadOnlyRootsPointerTable = {\n";
126
127 {
128#define ENTRY(_1, _2, CamelName) \
129 out << " StaticReadOnlyRoot::k" << #CamelName << ",\n";
131#undef ENTRY
132 out << "};\n";
133 }
134 out << "\n"
135 << "} // namespace internal\n"
136 << "} // namespace v8\n"
137 << "#endif // V8_STATIC_ROOTS_BOOL\n"
138 << "#endif // V8_ROOTS_STATIC_ROOTS_H_\n";
139}
140
141} // namespace internal
142} // namespace v8
constexpr int kRegularPageSize
#define ENTRY(Name,...)
SourcePosition pos
const std::map< Tagged_t, std::list< RootIndex > > & sorted_roots()
const std::string & camel_name(RootIndex idx)
std::map< Tagged_t, std::list< RootIndex > > sorted_roots_
std::unordered_map< RootIndex, std::string > camel_names_
static void write(Isolate *isolate, const char *file)
#define V8_STATIC_ROOTS_GENERATION_BOOL
Definition globals.h:135
Address Tagged_t
Definition globals.h:547
BodyGen * gen
#define READ_ONLY_ROOT_LIST(V)
Definition roots.h:468
#define CHECK(condition)
Definition logging.h:124
#define CHECK_LT(lhs, rhs)
#define CHECK_WITH_MSG(condition, message)
Definition logging.h:118
#define ADD_ROOT(_, value, CamelName)
#define V8_STATIC_ROOTS_BOOL
Definition v8config.h:1001