1 // Copyright 2006-2008 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.
11 #include "src/assembler.h"
12 #include "src/base/functional.h"
13 #include "src/base/platform/platform.h"
14 #include "src/ostreams.h"
19 // Define all of our flags.
20 #define FLAG_MODE_DEFINE
21 #include "src/flag-definitions.h" // NOLINT
23 // Define all of our flags default values.
24 #define FLAG_MODE_DEFINE_DEFAULTS
25 #include "src/flag-definitions.h" // NOLINT
29 // This structure represents a single entry in the flag system, with a pointer
30 // to the actual flag, default value, comment, etc. This is designed to be POD
31 // initialized as to avoid requiring static constructors.
33 enum FlagType { TYPE_BOOL, TYPE_MAYBE_BOOL, TYPE_INT, TYPE_FLOAT,
34 TYPE_STRING, TYPE_ARGS };
36 FlagType type_; // What type of flag, bool, int, or string.
37 const char* name_; // Name of the flag, ex "my_flag".
38 void* valptr_; // Pointer to the global flag variable.
39 const void* defptr_; // Pointer to the default value.
40 const char* cmt_; // A comment about the flags purpose.
41 bool owns_ptr_; // Does the flag own its string value?
43 FlagType type() const { return type_; }
45 const char* name() const { return name_; }
47 const char* comment() const { return cmt_; }
49 bool* bool_variable() const {
50 DCHECK(type_ == TYPE_BOOL);
51 return reinterpret_cast<bool*>(valptr_);
54 MaybeBoolFlag* maybe_bool_variable() const {
55 DCHECK(type_ == TYPE_MAYBE_BOOL);
56 return reinterpret_cast<MaybeBoolFlag*>(valptr_);
59 int* int_variable() const {
60 DCHECK(type_ == TYPE_INT);
61 return reinterpret_cast<int*>(valptr_);
64 double* float_variable() const {
65 DCHECK(type_ == TYPE_FLOAT);
66 return reinterpret_cast<double*>(valptr_);
69 const char* string_value() const {
70 DCHECK(type_ == TYPE_STRING);
71 return *reinterpret_cast<const char**>(valptr_);
74 void set_string_value(const char* value, bool owns_ptr) {
75 DCHECK(type_ == TYPE_STRING);
76 const char** ptr = reinterpret_cast<const char**>(valptr_);
77 if (owns_ptr_ && *ptr != NULL) DeleteArray(*ptr);
82 JSArguments* args_variable() const {
83 DCHECK(type_ == TYPE_ARGS);
84 return reinterpret_cast<JSArguments*>(valptr_);
87 bool bool_default() const {
88 DCHECK(type_ == TYPE_BOOL);
89 return *reinterpret_cast<const bool*>(defptr_);
92 int int_default() const {
93 DCHECK(type_ == TYPE_INT);
94 return *reinterpret_cast<const int*>(defptr_);
97 double float_default() const {
98 DCHECK(type_ == TYPE_FLOAT);
99 return *reinterpret_cast<const double*>(defptr_);
102 const char* string_default() const {
103 DCHECK(type_ == TYPE_STRING);
104 return *reinterpret_cast<const char* const *>(defptr_);
107 JSArguments args_default() const {
108 DCHECK(type_ == TYPE_ARGS);
109 return *reinterpret_cast<const JSArguments*>(defptr_);
112 // Compare this flag's current value against the default.
113 bool IsDefault() const {
116 return *bool_variable() == bool_default();
117 case TYPE_MAYBE_BOOL:
118 return maybe_bool_variable()->has_value == false;
120 return *int_variable() == int_default();
122 return *float_variable() == float_default();
124 const char* str1 = string_value();
125 const char* str2 = string_default();
126 if (str2 == NULL) return str1 == NULL;
127 if (str1 == NULL) return str2 == NULL;
128 return strcmp(str1, str2) == 0;
131 return args_variable()->argc == 0;
137 // Set a flag back to it's default value.
141 *bool_variable() = bool_default();
143 case TYPE_MAYBE_BOOL:
144 *maybe_bool_variable() = MaybeBoolFlag::Create(false, false);
147 *int_variable() = int_default();
150 *float_variable() = float_default();
153 set_string_value(string_default(), false);
156 *args_variable() = args_default();
163 #define FLAG_MODE_META
164 #include "src/flag-definitions.h"
167 const size_t num_flags = sizeof(flags) / sizeof(*flags);
172 static const char* Type2String(Flag::FlagType type) {
174 case Flag::TYPE_BOOL: return "bool";
175 case Flag::TYPE_MAYBE_BOOL: return "maybe_bool";
176 case Flag::TYPE_INT: return "int";
177 case Flag::TYPE_FLOAT: return "float";
178 case Flag::TYPE_STRING: return "string";
179 case Flag::TYPE_ARGS: return "arguments";
186 std::ostream& operator<<(std::ostream& os, const Flag& flag) { // NOLINT
187 switch (flag.type()) {
188 case Flag::TYPE_BOOL:
189 os << (*flag.bool_variable() ? "true" : "false");
191 case Flag::TYPE_MAYBE_BOOL:
192 os << (flag.maybe_bool_variable()->has_value
193 ? (flag.maybe_bool_variable()->value ? "true" : "false")
197 os << *flag.int_variable();
199 case Flag::TYPE_FLOAT:
200 os << *flag.float_variable();
202 case Flag::TYPE_STRING: {
203 const char* str = flag.string_value();
204 os << (str ? str : "NULL");
207 case Flag::TYPE_ARGS: {
208 JSArguments args = *flag.args_variable();
211 for (int i = 1; i < args.argc; i++) {
223 List<const char*>* FlagList::argv() {
224 List<const char*>* args = new List<const char*>(8);
225 Flag* args_flag = NULL;
226 for (size_t i = 0; i < num_flags; ++i) {
228 if (!f->IsDefault()) {
229 if (f->type() == Flag::TYPE_ARGS) {
230 DCHECK(args_flag == NULL);
231 args_flag = f; // Must be last in arguments.
235 bool disabled = f->type() == Flag::TYPE_BOOL && !*f->bool_variable();
236 std::ostringstream os;
237 os << (disabled ? "--no" : "--") << f->name();
238 args->Add(StrDup(os.str().c_str()));
240 if (f->type() != Flag::TYPE_BOOL) {
241 std::ostringstream os;
243 args->Add(StrDup(os.str().c_str()));
247 if (args_flag != NULL) {
248 std::ostringstream os;
249 os << "--" << args_flag->name();
250 args->Add(StrDup(os.str().c_str()));
251 JSArguments jsargs = *args_flag->args_variable();
252 for (int j = 0; j < jsargs.argc; j++) {
253 args->Add(StrDup(jsargs[j]));
260 inline char NormalizeChar(char ch) {
261 return ch == '_' ? '-' : ch;
265 // Helper function to parse flags: Takes an argument arg and splits it into
266 // a flag name and flag value (or NULL if they are missing). is_bool is set
267 // if the arg started with "-no" or "--no". The buffer may be used to NUL-
268 // terminate the name, it must be large enough to hold any possible name.
269 static void SplitArgument(const char* arg,
279 if (arg != NULL && *arg == '-') {
280 // find the begin of the flag name
281 arg++; // remove 1st '-'
283 arg++; // remove 2nd '-'
284 if (arg[0] == '\0') {
285 const char* kJSArgumentsFlagName = "js_arguments";
286 *name = kJSArgumentsFlagName;
290 if (arg[0] == 'n' && arg[1] == 'o') {
291 arg += 2; // remove "no"
292 if (NormalizeChar(arg[0]) == '-') arg++; // remove dash after "no".
297 // find the end of the flag name
298 while (*arg != '\0' && *arg != '=')
301 // get the value if any
303 // make a copy so we can NUL-terminate flag name
304 size_t n = arg - *name;
305 CHECK(n < static_cast<size_t>(buffer_size)); // buffer is too small
306 MemCopy(buffer, *name, n);
316 static bool EqualNames(const char* a, const char* b) {
317 for (int i = 0; NormalizeChar(a[i]) == NormalizeChar(b[i]); i++) {
326 static Flag* FindFlag(const char* name) {
327 for (size_t i = 0; i < num_flags; ++i) {
328 if (EqualNames(name, flags[i].name()))
336 int FlagList::SetFlagsFromCommandLine(int* argc,
341 for (int i = 1; i < *argc;) {
343 const char* arg = argv[i++];
345 // split arg into flag components
350 SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
354 Flag* flag = FindFlag(name);
357 // We don't recognize this flag but since we're removing
358 // the flags we recognize we assume that the remaining flags
359 // will be processed somewhere else so this flag might make
363 PrintF(stderr, "Error: unrecognized flag %s\n"
364 "Try --help for options\n", arg);
370 // if we still need a flag value, use the next argument if available
371 if (flag->type() != Flag::TYPE_BOOL &&
372 flag->type() != Flag::TYPE_MAYBE_BOOL &&
373 flag->type() != Flag::TYPE_ARGS &&
379 PrintF(stderr, "Error: missing value for flag %s of type %s\n"
380 "Try --help for options\n",
381 arg, Type2String(flag->type()));
388 char* endp = const_cast<char*>(""); // *endp is only read
389 switch (flag->type()) {
390 case Flag::TYPE_BOOL:
391 *flag->bool_variable() = !is_bool;
393 case Flag::TYPE_MAYBE_BOOL:
394 *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !is_bool);
397 *flag->int_variable() = strtol(value, &endp, 10); // NOLINT
399 case Flag::TYPE_FLOAT:
400 *flag->float_variable() = strtod(value, &endp);
402 case Flag::TYPE_STRING:
403 flag->set_string_value(value ? StrDup(value) : NULL, true);
405 case Flag::TYPE_ARGS: {
406 int start_pos = (value == NULL) ? i : i - 1;
407 int js_argc = *argc - start_pos;
408 const char** js_argv = NewArray<const char*>(js_argc);
410 js_argv[0] = StrDup(value);
412 for (int k = i; k < *argc; k++) {
413 js_argv[k - start_pos] = StrDup(argv[k]);
415 *flag->args_variable() = JSArguments::Create(js_argc, js_argv);
416 i = *argc; // Consume all arguments
422 bool is_bool_type = flag->type() == Flag::TYPE_BOOL ||
423 flag->type() == Flag::TYPE_MAYBE_BOOL;
424 if ((is_bool_type && value != NULL) || (!is_bool_type && is_bool) ||
426 PrintF(stderr, "Error: illegal value for flag %s of type %s\n"
427 "Try --help for options\n",
428 arg, Type2String(flag->type()));
433 // remove the flag & value from the command
442 // shrink the argument list
445 for (int i = 1; i < *argc; i++) {
456 // parsed all flags successfully
461 static char* SkipWhiteSpace(char* p) {
462 while (*p != '\0' && isspace(*p) != 0) p++;
467 static char* SkipBlackSpace(char* p) {
468 while (*p != '\0' && isspace(*p) == 0) p++;
474 int FlagList::SetFlagsFromString(const char* str, int len) {
475 // make a 0-terminated copy of str
476 ScopedVector<char> copy0(len + 1);
477 MemCopy(copy0.start(), str, len);
480 // strip leading white space
481 char* copy = SkipWhiteSpace(copy0.start());
483 // count the number of 'arguments'
484 int argc = 1; // be compatible with SetFlagsFromCommandLine()
485 for (char* p = copy; *p != '\0'; argc++) {
486 p = SkipBlackSpace(p);
487 p = SkipWhiteSpace(p);
490 // allocate argument array
491 ScopedVector<char*> argv(argc);
493 // split the flags string into arguments
494 argc = 1; // be compatible with SetFlagsFromCommandLine()
495 for (char* p = copy; *p != '\0'; argc++) {
497 p = SkipBlackSpace(p);
498 if (*p != '\0') *p++ = '\0'; // 0-terminate argument
499 p = SkipWhiteSpace(p);
503 int result = SetFlagsFromCommandLine(&argc, argv.start(), false);
510 void FlagList::ResetAllFlags() {
511 for (size_t i = 0; i < num_flags; ++i) {
518 void FlagList::PrintHelp() {
519 CpuFeatures::Probe(false);
520 CpuFeatures::PrintTarget();
521 CpuFeatures::PrintFeatures();
525 << " shell [options] -e string\n"
526 << " execute string in V8\n"
527 << " shell [options] file1 file2 ... filek\n"
528 << " run JavaScript scripts in file1, file2, ..., filek\n"
529 << " shell [options]\n"
530 << " shell [options] --shell [file1 file2 ... filek]\n"
531 << " run an interactive JavaScript shell\n"
532 << " d8 [options] file1 file2 ... filek\n"
534 << " d8 [options] --shell [file1 file2 ... filek]\n"
535 << " run the new debugging shell\n\n"
537 for (size_t i = 0; i < num_flags; ++i) {
539 os << " --" << f->name() << " (" << f->comment() << ")\n"
540 << " type: " << Type2String(f->type()) << " default: " << *f
546 static uint32_t flag_hash = 0;
549 void ComputeFlagListHash() {
550 std::ostringstream modified_args_as_string;
552 modified_args_as_string << "debug";
554 for (size_t i = 0; i < num_flags; ++i) {
555 Flag* current = &flags[i];
556 if (!current->IsDefault()) {
557 modified_args_as_string << i;
558 modified_args_as_string << *current;
561 std::string args(modified_args_as_string.str());
562 flag_hash = static_cast<uint32_t>(
563 base::hash_range(args.c_str(), args.c_str() + args.length()));
568 void FlagList::EnforceFlagImplications() {
569 #define FLAG_MODE_DEFINE_IMPLICATIONS
570 #include "src/flag-definitions.h"
571 #undef FLAG_MODE_DEFINE_IMPLICATIONS
572 ComputeFlagListHash();
576 uint32_t FlagList::Hash() { return flag_hash; }
577 } } // namespace v8::internal