v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
flags-impl.h
Go to the documentation of this file.
1// Copyright 2024 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_FLAGS_FLAGS_IMPL_H_
6#define V8_FLAGS_FLAGS_IMPL_H_
7
8#include <optional>
9#include <unordered_set>
10
11#include "src/base/macros.h"
12#include "src/base/vector.h"
13#include "src/flags/flags.h"
14
15namespace v8::internal {
16
18 public:
19 static char NormalizeChar(char ch);
20
21 static int FlagNamesCmp(const char* a, const char* b);
22
23 static bool EqualNames(const char* a, const char* b);
24 static bool EqualNameWithSuffix(const char* a, const char* b);
25};
26
27struct Flag;
28Flag* FindFlagByPointer(const void* ptr);
29V8_EXPORT_PRIVATE Flag* FindFlagByName(const char* name);
31
33
34// Helper struct for printing normalized flag names.
35struct FlagName {
36 const char* name;
37 bool negated;
38
39 constexpr FlagName(const char* name, bool negated)
40 : name(name), negated(negated) {
41 DCHECK_NE('\0', name[0]);
42 DCHECK_NE('!', name[0]);
43 }
44
45 constexpr explicit FlagName(const char* name)
46 : FlagName(name[0] == '!' ? name + 1 : name, name[0] == '!') {}
47};
48
49std::ostream& operator<<(std::ostream& os, FlagName flag_name);
50
51// This structure represents a single entry in the flag system, with a pointer
52// to the actual flag, default value, comment, etc. This is designed to be POD
53// initialized as to avoid requiring static constructors.
54struct Flag {
65
67
68 constexpr bool IsAnyImplication(Flag::SetBy set_by) {
69 return set_by == SetBy::kWeakImplication || set_by == SetBy::kImplication;
70 }
71
72 FlagType type_; // What type of flag, bool, int, or string.
73 const char* name_; // Name of the flag, ex "my_flag".
74 void* valptr_; // Pointer to the global flag variable.
75 const void* defptr_; // Pointer to the default value.
76 const char* cmt_; // A comment about the flags purpose.
77 bool owns_ptr_; // Does the flag own its string value?
79 // Name of the flag implying this flag, if any.
80 const char* implied_by_ = nullptr;
81#ifdef DEBUG
82 // Pointer to the flag implying this flag, if any.
83 const Flag* implied_by_ptr_ = nullptr;
84#endif
85
86 FlagType type() const { return type_; }
87
88 const char* name() const { return name_; }
89
90 const char* comment() const { return cmt_; }
91
92 bool PointsTo(const void* ptr) const { return valptr_ == ptr; }
93
94#ifdef DEBUG
95 bool ImpliedBy(const void* ptr) const {
96 const Flag* current = this->implied_by_ptr_;
97 std::unordered_set<const Flag*> visited_flags;
98 while (current != nullptr) {
99 visited_flags.insert(current);
100 if (current->PointsTo(ptr)) return true;
101 current = current->implied_by_ptr_;
102 if (visited_flags.contains(current)) break;
103 }
104 return false;
105 }
106#endif
107
108 bool bool_variable() const { return GetValue<TYPE_BOOL, bool>(); }
109
110 void set_bool_variable(bool value, SetBy set_by) {
111 SetValue<TYPE_BOOL, bool>(value, set_by);
112 }
113
114 std::optional<bool> maybe_bool_variable() const {
116 }
117
118 void set_maybe_bool_variable(std::optional<bool> value, SetBy set_by) {
120 }
121
122 int int_variable() const { return GetValue<TYPE_INT, int>(); }
123
124 void set_int_variable(int value, SetBy set_by) {
125 SetValue<TYPE_INT, int>(value, set_by);
126 }
127
128 unsigned int uint_variable() const {
130 }
131
132 void set_uint_variable(unsigned int value, SetBy set_by) {
134 }
135
137
138 void set_uint64_variable(uint64_t value, SetBy set_by) {
139 SetValue<TYPE_UINT64, uint64_t>(value, set_by);
140 }
141
142 double float_variable() const { return GetValue<TYPE_FLOAT, double>(); }
143
144 void set_float_variable(double value, SetBy set_by) {
145 SetValue<TYPE_FLOAT, double>(value, set_by);
146 }
147
149
150 void set_size_t_variable(size_t value, SetBy set_by) {
151 SetValue<TYPE_SIZE_T, size_t>(value, set_by);
152 }
153
154 const char* string_value() const {
156 }
157
158 void set_string_value(const char* new_value, bool owns_new_value,
159 SetBy set_by);
160
161 template <typename T>
162 T GetDefaultValue() const {
163 return *reinterpret_cast<const T*>(defptr_);
164 }
165
166 bool bool_default() const {
168 return GetDefaultValue<bool>();
169 }
170
171 int int_default() const {
173 return GetDefaultValue<int>();
174 }
175
176 unsigned int uint_default() const {
179 }
180
181 uint64_t uint64_default() const {
184 }
185
186 double float_default() const {
189 }
190
191 size_t size_t_default() const {
194 }
195
196 const char* string_default() const {
199 }
200
202
203 // {change_flag} indicates if we're going to change the flag value.
204 // Returns an updated value for {change_flag}, which is changed to false if a
205 // weak implication is being ignored beause a flag is already set by a normal
206 // implication or from the command-line.
207 bool CheckFlagChange(SetBy new_set_by, bool change_flag,
208 const char* implied_by = nullptr);
209
210 bool IsReadOnly() const {
211 // See the FLAG_READONLY definition for FLAG_MODE_META.
212 return valptr_ == nullptr;
213 }
214
215 template <FlagType flag_type, typename T>
216 T GetValue() const {
217 DCHECK_EQ(flag_type, type_);
218 if (IsReadOnly()) return GetDefaultValue<T>();
219 return *reinterpret_cast<const FlagValue<T>*>(valptr_);
220 }
221
222 template <FlagType flag_type, typename T>
223 void SetValue(T new_value, SetBy set_by) {
224 DCHECK_EQ(flag_type, type_);
225 bool change_flag = GetValue<flag_type, T>() != new_value;
227 if (change_flag) {
228 DCHECK(!IsReadOnly());
229 *reinterpret_cast<FlagValue<T>*>(valptr_) = new_value;
230 }
231 }
232
233 // Compare this flag's current value against the default.
234 bool IsDefault() const;
235
237
238 // Set a flag back to its default value.
240
242};
243
244} // namespace v8::internal
245
246#endif // V8_FLAGS_FLAGS_IMPL_H_
static bool EqualNames(const char *a, const char *b)
static char NormalizeChar(char ch)
static int FlagNamesCmp(const char *a, const char *b)
static bool EqualNameWithSuffix(const char *a, const char *b)
V8_EXPORT_PRIVATE base::Vector< Flag > Flags()
Definition flags.cc:300
V8_EXPORT_PRIVATE Flag * FindFlagByName(const char *name)
Definition flags.cc:353
return change_flag
Definition flags.cc:3733
Flag * FindFlagByPointer(const void *ptr)
Definition flags.cc:369
std::ostream & operator<<(std::ostream &os, AtomicMemoryOrder order)
V8_EXPORT_PRIVATE Flag * FindImplicationFlagByName(const char *name)
Definition flags.cc:344
return value
Definition map-inl.h:893
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
constexpr FlagName(const char *name, bool negated)
Definition flags-impl.h:39
constexpr FlagName(const char *name)
Definition flags-impl.h:45
void set_bool_variable(bool value, SetBy set_by)
Definition flags-impl.h:110
uint64_t uint64_default() const
Definition flags-impl.h:181
double float_variable() const
Definition flags-impl.h:142
int int_variable() const
Definition flags-impl.h:122
std::optional< bool > maybe_bool_variable() const
Definition flags-impl.h:114
double float_default() const
Definition flags-impl.h:186
V8_EXPORT_PRIVATE void Reset()
Definition flags.cc:3768
FlagType type() const
Definition flags-impl.h:86
const char * cmt_
Definition flags-impl.h:76
const char * string_value() const
Definition flags-impl.h:154
int int_default() const
Definition flags-impl.h:171
const char * name_
Definition flags-impl.h:73
const char * name() const
Definition flags-impl.h:88
size_t size_t_default() const
Definition flags-impl.h:191
void set_uint64_variable(uint64_t value, SetBy set_by)
Definition flags-impl.h:138
bool PointsTo(const void *ptr) const
Definition flags-impl.h:92
void set_size_t_variable(size_t value, SetBy set_by)
Definition flags-impl.h:150
bool bool_variable() const
Definition flags-impl.h:108
const char * implied_by_
Definition flags-impl.h:80
bool bool_default() const
Definition flags-impl.h:166
T GetDefaultValue() const
Definition flags-impl.h:162
void set_float_variable(double value, SetBy set_by)
Definition flags-impl.h:144
const void * defptr_
Definition flags-impl.h:75
bool IsDefault() const
Definition flags.cc:3736
unsigned int uint_variable() const
Definition flags-impl.h:128
size_t size_t_variable() const
Definition flags-impl.h:148
constexpr bool IsAnyImplication(Flag::SetBy set_by)
Definition flags-impl.h:68
void set_maybe_bool_variable(std::optional< bool > value, SetBy set_by)
Definition flags-impl.h:118
void set_uint_variable(unsigned int value, SetBy set_by)
Definition flags-impl.h:132
bool IsReadOnly() const
Definition flags-impl.h:210
void ReleaseDynamicAllocations()
Definition flags.cc:3763
void set_string_value(const char *new_value, bool owns_new_value, SetBy set_by)
void SetValue(T new_value, SetBy set_by)
Definition flags-impl.h:223
unsigned int uint_default() const
Definition flags-impl.h:176
bool CheckFlagChange(SetBy new_set_by, bool change_flag, const char *implied_by=nullptr)
void set_int_variable(int value, SetBy set_by)
Definition flags-impl.h:124
static bool ShouldCheckFlagContradictions()
uint64_t uint64_variable() const
Definition flags-impl.h:136
const char * comment() const
Definition flags-impl.h:90
const char * string_default() const
Definition flags-impl.h:196