flags.cc 19 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#include "src/flags.h"

7
#include <cctype>
8
#include <cerrno>
9 10
#include <cstdlib>
#include <sstream>
11

12
#include "src/allocation.h"
13
#include "src/assembler.h"
14
#include "src/base/functional.h"
15
#include "src/base/platform/platform.h"
16
#include "src/ostreams.h"
17
#include "src/utils.h"
18
#include "src/wasm/wasm-limits.h"
19

20 21
namespace v8 {
namespace internal {
22

23 24
// Define all of our flags.
#define FLAG_MODE_DEFINE
25
#include "src/flag-definitions.h"  // NOLINT(build/include)
26

27 28
// Define all of our flags default values.
#define FLAG_MODE_DEFINE_DEFAULTS
29
#include "src/flag-definitions.h"  // NOLINT(build/include)
30 31 32 33 34 35 36

namespace {

// This structure represents a single entry in the flag system, with a pointer
// to the actual flag, default value, comment, etc.  This is designed to be POD
// initialized as to avoid requiring static constructors.
struct Flag {
37 38 39 40 41
  enum FlagType {
    TYPE_BOOL,
    TYPE_MAYBE_BOOL,
    TYPE_INT,
    TYPE_UINT,
Yang Guo's avatar
Yang Guo committed
42
    TYPE_UINT64,
43
    TYPE_FLOAT,
44
    TYPE_SIZE_T,
45 46 47
    TYPE_STRING,
    TYPE_ARGS
  };
48 49 50 51 52 53

  FlagType type_;           // What type of flag, bool, int, or string.
  const char* name_;        // Name of the flag, ex "my_flag".
  void* valptr_;            // Pointer to the global flag variable.
  const void* defptr_;      // Pointer to the default value.
  const char* cmt_;         // A comment about the flags purpose.
54
  bool owns_ptr_;           // Does the flag own its string value?
55 56

  FlagType type() const { return type_; }
57

58
  const char* name() const { return name_; }
59

60 61 62
  const char* comment() const { return cmt_; }

  bool* bool_variable() const {
63
    DCHECK(type_ == TYPE_BOOL);
64
    return reinterpret_cast<bool*>(valptr_);
65 66
  }

67
  MaybeBoolFlag* maybe_bool_variable() const {
68
    DCHECK(type_ == TYPE_MAYBE_BOOL);
69 70 71
    return reinterpret_cast<MaybeBoolFlag*>(valptr_);
  }

72
  int* int_variable() const {
73
    DCHECK(type_ == TYPE_INT);
74 75
    return reinterpret_cast<int*>(valptr_);
  }
76

77 78 79 80 81
  unsigned int* uint_variable() const {
    DCHECK(type_ == TYPE_UINT);
    return reinterpret_cast<unsigned int*>(valptr_);
  }

Yang Guo's avatar
Yang Guo committed
82 83 84 85 86
  uint64_t* uint64_variable() const {
    DCHECK(type_ == TYPE_UINT64);
    return reinterpret_cast<uint64_t*>(valptr_);
  }

87
  double* float_variable() const {
88
    DCHECK(type_ == TYPE_FLOAT);
89
    return reinterpret_cast<double*>(valptr_);
90 91
  }

92 93 94 95 96
  size_t* size_t_variable() const {
    DCHECK(type_ == TYPE_SIZE_T);
    return reinterpret_cast<size_t*>(valptr_);
  }

97
  const char* string_value() const {
98
    DCHECK(type_ == TYPE_STRING);
99 100 101
    return *reinterpret_cast<const char**>(valptr_);
  }

102
  void set_string_value(const char* value, bool owns_ptr) {
103
    DCHECK(type_ == TYPE_STRING);
104
    const char** ptr = reinterpret_cast<const char**>(valptr_);
105
    if (owns_ptr_ && *ptr != nullptr) DeleteArray(*ptr);
106 107
    *ptr = value;
    owns_ptr_ = owns_ptr;
108
  }
109

110
  JSArguments* args_variable() const {
111
    DCHECK(type_ == TYPE_ARGS);
112 113 114
    return reinterpret_cast<JSArguments*>(valptr_);
  }

115
  bool bool_default() const {
116
    DCHECK(type_ == TYPE_BOOL);
117 118
    return *reinterpret_cast<const bool*>(defptr_);
  }
119

120
  int int_default() const {
121
    DCHECK(type_ == TYPE_INT);
122 123
    return *reinterpret_cast<const int*>(defptr_);
  }
124

125 126 127 128 129
  unsigned int uint_default() const {
    DCHECK(type_ == TYPE_UINT);
    return *reinterpret_cast<const unsigned int*>(defptr_);
  }

Yang Guo's avatar
Yang Guo committed
130 131 132 133 134
  uint64_t uint64_default() const {
    DCHECK(type_ == TYPE_UINT64);
    return *reinterpret_cast<const uint64_t*>(defptr_);
  }

135
  double float_default() const {
136
    DCHECK(type_ == TYPE_FLOAT);
137 138
    return *reinterpret_cast<const double*>(defptr_);
  }
139

140 141 142 143 144
  size_t size_t_default() const {
    DCHECK(type_ == TYPE_SIZE_T);
    return *reinterpret_cast<const size_t*>(defptr_);
  }

145
  const char* string_default() const {
146
    DCHECK(type_ == TYPE_STRING);
147
    return *reinterpret_cast<const char* const *>(defptr_);
148 149
  }

150
  JSArguments args_default() const {
151
    DCHECK(type_ == TYPE_ARGS);
152 153 154
    return *reinterpret_cast<const JSArguments*>(defptr_);
  }

155 156 157
  // Compare this flag's current value against the default.
  bool IsDefault() const {
    switch (type_) {
158 159
      case TYPE_BOOL:
        return *bool_variable() == bool_default();
160 161
      case TYPE_MAYBE_BOOL:
        return maybe_bool_variable()->has_value == false;
162 163
      case TYPE_INT:
        return *int_variable() == int_default();
164 165
      case TYPE_UINT:
        return *uint_variable() == uint_default();
Yang Guo's avatar
Yang Guo committed
166 167
      case TYPE_UINT64:
        return *uint64_variable() == uint64_default();
168 169
      case TYPE_FLOAT:
        return *float_variable() == float_default();
170 171
      case TYPE_SIZE_T:
        return *size_t_variable() == size_t_default();
172
      case TYPE_STRING: {
173
        const char* str1 = string_value();
174
        const char* str2 = string_default();
175 176
        if (str2 == nullptr) return str1 == nullptr;
        if (str1 == nullptr) return str2 == nullptr;
177
        return strcmp(str1, str2) == 0;
178 179
      }
      case TYPE_ARGS:
180
        return args_variable()->argc == 0;
181
    }
182
    UNREACHABLE();
183
  }
184

185 186 187
  // Set a flag back to it's default value.
  void Reset() {
    switch (type_) {
188 189 190
      case TYPE_BOOL:
        *bool_variable() = bool_default();
        break;
191 192 193
      case TYPE_MAYBE_BOOL:
        *maybe_bool_variable() = MaybeBoolFlag::Create(false, false);
        break;
194 195 196
      case TYPE_INT:
        *int_variable() = int_default();
        break;
197 198 199
      case TYPE_UINT:
        *uint_variable() = uint_default();
        break;
Yang Guo's avatar
Yang Guo committed
200 201 202
      case TYPE_UINT64:
        *uint64_variable() = uint64_default();
        break;
203 204 205
      case TYPE_FLOAT:
        *float_variable() = float_default();
        break;
206 207 208
      case TYPE_SIZE_T:
        *size_t_variable() = size_t_default();
        break;
209
      case TYPE_STRING:
210
        set_string_value(string_default(), false);
211
        break;
212 213 214
      case TYPE_ARGS:
        *args_variable() = args_default();
        break;
215
    }
216
  }
217 218 219 220
};

Flag flags[] = {
#define FLAG_MODE_META
221
#include "src/flag-definitions.h"  // NOLINT(build/include)
222 223 224 225 226
};

const size_t num_flags = sizeof(flags) / sizeof(*flags);

}  // namespace
227 228


229
static const char* Type2String(Flag::FlagType type) {
230
  switch (type) {
231
    case Flag::TYPE_BOOL: return "bool";
232
    case Flag::TYPE_MAYBE_BOOL: return "maybe_bool";
233
    case Flag::TYPE_INT: return "int";
234 235
    case Flag::TYPE_UINT:
      return "uint";
Yang Guo's avatar
Yang Guo committed
236 237
    case Flag::TYPE_UINT64:
      return "uint64";
238
    case Flag::TYPE_FLOAT: return "float";
239 240
    case Flag::TYPE_SIZE_T:
      return "size_t";
241
    case Flag::TYPE_STRING: return "string";
242
    case Flag::TYPE_ARGS: return "arguments";
243 244 245 246 247
  }
  UNREACHABLE();
}


248
std::ostream& operator<<(std::ostream& os, const Flag& flag) {  // NOLINT
249
  switch (flag.type()) {
250
    case Flag::TYPE_BOOL:
251
      os << (*flag.bool_variable() ? "true" : "false");
252
      break;
253
    case Flag::TYPE_MAYBE_BOOL:
254 255 256
      os << (flag.maybe_bool_variable()->has_value
                 ? (flag.maybe_bool_variable()->value ? "true" : "false")
                 : "unset");
257
      break;
258
    case Flag::TYPE_INT:
259
      os << *flag.int_variable();
260
      break;
261 262 263
    case Flag::TYPE_UINT:
      os << *flag.uint_variable();
      break;
Yang Guo's avatar
Yang Guo committed
264 265 266
    case Flag::TYPE_UINT64:
      os << *flag.uint64_variable();
      break;
267
    case Flag::TYPE_FLOAT:
268
      os << *flag.float_variable();
269
      break;
270 271 272
    case Flag::TYPE_SIZE_T:
      os << *flag.size_t_variable();
      break;
273
    case Flag::TYPE_STRING: {
274
      const char* str = flag.string_value();
275
      os << (str ? str : "nullptr");
276 277 278
      break;
    }
    case Flag::TYPE_ARGS: {
279
      JSArguments args = *flag.args_variable();
280
      if (args.argc > 0) {
281
        os << args[0];
282
        for (int i = 1; i < args.argc; i++) {
283
          os << args[i];
284
        }
285 286
      }
      break;
287
    }
288
  }
289
  return os;
290 291 292
}


293
// static
294 295
std::vector<const char*>* FlagList::argv() {
  std::vector<const char*>* args = new std::vector<const char*>(8);
296
  Flag* args_flag = nullptr;
297 298
  for (size_t i = 0; i < num_flags; ++i) {
    Flag* f = &flags[i];
299
    if (!f->IsDefault()) {
300
      if (f->type() == Flag::TYPE_ARGS) {
301
        DCHECK_NULL(args_flag);
302 303 304
        args_flag = f;  // Must be last in arguments.
        continue;
      }
305 306
      {
        bool disabled = f->type() == Flag::TYPE_BOOL && !*f->bool_variable();
307
        std::ostringstream os;
308
        os << (disabled ? "--no" : "--") << f->name();
309
        args->push_back(StrDup(os.str().c_str()));
310
      }
311
      if (f->type() != Flag::TYPE_BOOL) {
312
        std::ostringstream os;
313
        os << *f;
314
        args->push_back(StrDup(os.str().c_str()));
315 316 317
      }
    }
  }
318
  if (args_flag != nullptr) {
319
    std::ostringstream os;
320
    os << "--" << args_flag->name();
321
    args->push_back(StrDup(os.str().c_str()));
322
    JSArguments jsargs = *args_flag->args_variable();
323
    for (int j = 0; j < jsargs.argc; j++) {
324
      args->push_back(StrDup(jsargs[j]));
325 326
    }
  }
327
  return args;
328 329 330
}


331 332 333 334
inline char NormalizeChar(char ch) {
  return ch == '_' ? '-' : ch;
}

335
// Helper function to parse flags: Takes an argument arg and splits it into
336
// a flag name and flag value (or nullptr if they are missing). negated is set
337 338
// if the arg started with "-no" or "--no". The buffer may be used to NUL-
// terminate the name, it must be large enough to hold any possible name.
339 340 341
static void SplitArgument(const char* arg, char* buffer, int buffer_size,
                          const char** name, const char** value,
                          bool* negated) {
342 343
  *name = nullptr;
  *value = nullptr;
344
  *negated = false;
345

346
  if (arg != nullptr && *arg == '-') {
347 348
    // find the begin of the flag name
    arg++;  // remove 1st '-'
349
    if (*arg == '-') {
350
      arg++;  // remove 2nd '-'
351 352 353 354 355 356
      if (arg[0] == '\0') {
        const char* kJSArgumentsFlagName = "js_arguments";
        *name = kJSArgumentsFlagName;
        return;
      }
    }
357 358
    if (arg[0] == 'n' && arg[1] == 'o') {
      arg += 2;  // remove "no"
359
      if (NormalizeChar(arg[0]) == '-') arg++;  // remove dash after "no".
360
      *negated = true;
361 362 363 364 365 366 367 368 369 370
    }
    *name = arg;

    // find the end of the flag name
    while (*arg != '\0' && *arg != '=')
      arg++;

    // get the value if any
    if (*arg == '=') {
      // make a copy so we can NUL-terminate flag name
371 372
      size_t n = arg - *name;
      CHECK(n < static_cast<size_t>(buffer_size));  // buffer is too small
373
      MemCopy(buffer, *name, n);
374 375 376 377 378 379 380 381 382
      buffer[n] = '\0';
      *name = buffer;
      // get the value
      *value = arg + 1;
    }
  }
}


383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
static bool EqualNames(const char* a, const char* b) {
  for (int i = 0; NormalizeChar(a[i]) == NormalizeChar(b[i]); i++) {
    if (a[i] == '\0') {
      return true;
    }
  }
  return false;
}


static Flag* FindFlag(const char* name) {
  for (size_t i = 0; i < num_flags; ++i) {
    if (EqualNames(name, flags[i].name()))
      return &flags[i];
  }
398
  return nullptr;
399 400
}

401 402 403 404 405 406 407 408 409 410 411 412
template <typename T>
bool TryParseUnsigned(Flag* flag, const char* arg, const char* value,
                      char** endp, T* out_val) {
  // We do not use strtoul because it accepts negative numbers.
  // Rejects values >= 2**63 when T is 64 bits wide but that
  // seems like an acceptable trade-off.
  uint64_t max = static_cast<uint64_t>(std::numeric_limits<T>::max());
  errno = 0;
  int64_t val = static_cast<int64_t>(strtoll(value, endp, 10));
  if (val < 0 || static_cast<uint64_t>(val) > max || errno != 0) {
    PrintF(stderr,
           "Error: Value for flag %s of type %s is out of bounds "
413
           "[0-%" PRIu64 "]\n",
414 415 416 417 418 419
           arg, Type2String(flag->type()), max);
    return false;
  }
  *out_val = static_cast<T>(val);
  return true;
}
420 421

// static
422 423
int FlagList::SetFlagsFromCommandLine(int* argc,
                                      char** argv,
424
                                      bool remove_flags) {
425
  int return_code = 0;
426 427 428 429 430 431 432 433 434
  // parse arguments
  for (int i = 1; i < *argc;) {
    int j = i;  // j > 0
    const char* arg = argv[i++];

    // split arg into flag components
    char buffer[1*KB];
    const char* name;
    const char* value;
435 436
    bool negated;
    SplitArgument(arg, buffer, sizeof buffer, &name, &value, &negated);
437

438
    if (name != nullptr) {
439
      // lookup the flag
440
      Flag* flag = FindFlag(name);
441
      if (flag == nullptr) {
442 443 444 445 446 447 448
        if (remove_flags) {
          // We don't recognize this flag but since we're removing
          // the flags we recognize we assume that the remaining flags
          // will be processed somewhere else so this flag might make
          // sense there.
          continue;
        } else {
449
          PrintF(stderr, "Error: unrecognized flag %s\n", arg);
450 451
          return_code = j;
          break;
452
        }
453 454 455
      }

      // if we still need a flag value, use the next argument if available
456
      if (flag->type() != Flag::TYPE_BOOL &&
457
          flag->type() != Flag::TYPE_MAYBE_BOOL &&
458
          flag->type() != Flag::TYPE_ARGS && value == nullptr) {
459 460
        if (i < *argc) {
          value = argv[i++];
461 462
        }
        if (!value) {
463 464
          PrintF(stderr, "Error: missing value for flag %s of type %s\n", arg,
                 Type2String(flag->type()));
465 466
          return_code = j;
          break;
467 468 469 470 471 472
        }
      }

      // set the flag
      char* endp = const_cast<char*>("");  // *endp is only read
      switch (flag->type()) {
473
        case Flag::TYPE_BOOL:
474
          *flag->bool_variable() = !negated;
475
          break;
476
        case Flag::TYPE_MAYBE_BOOL:
477
          *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !negated);
478
          break;
479
        case Flag::TYPE_INT:
480
          *flag->int_variable() = static_cast<int>(strtol(value, &endp, 10));
481
          break;
482 483 484
        case Flag::TYPE_UINT:
          if (!TryParseUnsigned(flag, arg, value, &endp,
                                flag->uint_variable())) {
485 486 487
            return_code = j;
          }
          break;
Yang Guo's avatar
Yang Guo committed
488 489 490 491 492 493
        case Flag::TYPE_UINT64:
          if (!TryParseUnsigned(flag, arg, value, &endp,
                                flag->uint64_variable())) {
            return_code = j;
          }
          break;
494
        case Flag::TYPE_FLOAT:
495 496
          *flag->float_variable() = strtod(value, &endp);
          break;
497 498 499 500 501 502
        case Flag::TYPE_SIZE_T:
          if (!TryParseUnsigned(flag, arg, value, &endp,
                                flag->size_t_variable())) {
            return_code = j;
          }
          break;
503
        case Flag::TYPE_STRING:
504
          flag->set_string_value(value ? StrDup(value) : nullptr, true);
505
          break;
506
        case Flag::TYPE_ARGS: {
507
          int start_pos = (value == nullptr) ? i : i - 1;
508 509
          int js_argc = *argc - start_pos;
          const char** js_argv = NewArray<const char*>(js_argc);
510
          if (value != nullptr) {
511
            js_argv[0] = StrDup(value);
512 513
          }
          for (int k = i; k < *argc; k++) {
514
            js_argv[k - start_pos] = StrDup(argv[k]);
515
          }
516
          *flag->args_variable() = JSArguments::Create(js_argc, js_argv);
517 518 519
          i = *argc;  // Consume all arguments
          break;
        }
520 521 522
      }

      // handle errors
523 524
      bool is_bool_type = flag->type() == Flag::TYPE_BOOL ||
          flag->type() == Flag::TYPE_MAYBE_BOOL;
525
      if ((is_bool_type && value != nullptr) || (!is_bool_type && negated) ||
526
          *endp != '\0') {
527 528 529 530
        // TODO(neis): TryParseUnsigned may return with {*endp == '\0'} even in
        // an error case.
        PrintF(stderr, "Error: illegal value for flag %s of type %s\n", arg,
               Type2String(flag->type()));
531 532 533 534
        if (is_bool_type) {
          PrintF(stderr,
                 "To set or unset a boolean flag, use --flag or --no-flag.\n");
        }
535 536
        return_code = j;
        break;
537 538 539
      }

      // remove the flag & value from the command
540 541
      if (remove_flags) {
        while (j < i) {
542
          argv[j++] = nullptr;
543 544
        }
      }
545 546 547
    }
  }

548 549 550 551 552
  if (FLAG_help) {
    PrintHelp();
    exit(0);
  }

553
  if (remove_flags) {
554
    // shrink the argument list
555 556
    int j = 1;
    for (int i = 1; i < *argc; i++) {
557
      if (argv[i] != nullptr) argv[j++] = argv[i];
558 559
    }
    *argc = j;
560 561 562 563 564 565 566 567
  } else if (return_code != 0) {
    if (return_code + 1 < *argc) {
      PrintF(stderr, "The remaining arguments were ignored:");
      for (int i = return_code + 1; i < *argc; ++i) {
        PrintF(stderr, " %s", argv[i]);
      }
      PrintF(stderr, "\n");
    }
568
  }
569
  if (return_code != 0) PrintF(stderr, "Try --help for options\n");
570

571
  return return_code;
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
}


static char* SkipWhiteSpace(char* p) {
  while (*p != '\0' && isspace(*p) != 0) p++;
  return p;
}


static char* SkipBlackSpace(char* p) {
  while (*p != '\0' && isspace(*p) == 0) p++;
  return p;
}


587
// static
588 589
int FlagList::SetFlagsFromString(const char* str, int len) {
  // make a 0-terminated copy of str
590
  ScopedVector<char> copy0(len + 1);
591
  MemCopy(copy0.start(), str, len);
592 593 594
  copy0[len] = '\0';

  // strip leading white space
595
  char* copy = SkipWhiteSpace(copy0.start());
596 597 598 599 600 601 602 603 604

  // count the number of 'arguments'
  int argc = 1;  // be compatible with SetFlagsFromCommandLine()
  for (char* p = copy; *p != '\0'; argc++) {
    p = SkipBlackSpace(p);
    p = SkipWhiteSpace(p);
  }

  // allocate argument array
605
  ScopedVector<char*> argv(argc);
606 607 608 609 610 611 612 613 614 615

  // split the flags string into arguments
  argc = 1;  // be compatible with SetFlagsFromCommandLine()
  for (char* p = copy; *p != '\0'; argc++) {
    argv[argc] = p;
    p = SkipBlackSpace(p);
    if (*p != '\0') *p++ = '\0';  // 0-terminate argument
    p = SkipWhiteSpace(p);
  }

616
  return SetFlagsFromCommandLine(&argc, argv.start(), false);
617 618 619
}


620 621 622 623 624 625 626 627 628 629
// static
void FlagList::ResetAllFlags() {
  for (size_t i = 0; i < num_flags; ++i) {
    flags[i].Reset();
  }
}


// static
void FlagList::PrintHelp() {
630
  CpuFeatures::Probe(false);
631 632 633
  CpuFeatures::PrintTarget();
  CpuFeatures::PrintFeatures();

634
  StdoutStream os;
635 636 637 638 639 640
  os << "Synopsis:\n"
        "  shell [options] [--shell] [<file>...]\n"
        "  d8 [options] [-e <string>] [--shell] [[--module] <file>...]\n\n"
        "  -e        execute a string in V8\n"
        "  --shell   run an interactive JavaScript shell\n"
        "  --module  execute a file as a JavaScript module\n\n"
641
        "Note: the --module option is implicitly enabled for *.mjs files.\n\n"
642 643
        "Options:\n";

644 645 646 647 648 649 650
  for (const Flag& f : flags) {
    os << "  --";
    for (const char* c = f.name(); *c != '\0'; ++c) {
      os << NormalizeChar(*c);
    }
    os << " (" << f.comment() << ")\n"
       << "        type: " << Type2String(f.type()) << "  default: " << f
651
       << "\n";
652
  }
653 654
}

655

656
static uint32_t flag_hash = 0;
657

658

659
void ComputeFlagListHash() {
660
  std::ostringstream modified_args_as_string;
661 662 663
#ifdef DEBUG
  modified_args_as_string << "debug";
#endif  // DEBUG
664 665 666
  if (FLAG_embedded_builtins) {
    modified_args_as_string << "embedded";
  }
667 668 669
  for (size_t i = 0; i < num_flags; ++i) {
    Flag* current = &flags[i];
    if (!current->IsDefault()) {
670
      modified_args_as_string << i;
671 672 673 674
      modified_args_as_string << *current;
    }
  }
  std::string args(modified_args_as_string.str());
675
  flag_hash = static_cast<uint32_t>(
676 677
      base::hash_range(args.c_str(), args.c_str() + args.length()));
}
678 679 680 681 682


// static
void FlagList::EnforceFlagImplications() {
#define FLAG_MODE_DEFINE_IMPLICATIONS
683
#include "src/flag-definitions.h"  // NOLINT(build/include)
684 685 686 687 688 689
#undef FLAG_MODE_DEFINE_IMPLICATIONS
  ComputeFlagListHash();
}


uint32_t FlagList::Hash() { return flag_hash; }
690 691
}  // namespace internal
}  // namespace v8