flags.cc 17.2 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 8 9
#include <cctype>
#include <cstdlib>
#include <sstream>
10

11
#include "src/allocation.h"
12
#include "src/assembler.h"
13
#include "src/base/functional.h"
14
#include "src/base/platform/platform.h"
15
#include "src/list-inl.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 42 43 44 45
  enum FlagType {
    TYPE_BOOL,
    TYPE_MAYBE_BOOL,
    TYPE_INT,
    TYPE_UINT,
    TYPE_FLOAT,
    TYPE_STRING,
    TYPE_ARGS
  };
46 47 48 49 50 51

  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.
52
  bool owns_ptr_;           // Does the flag own its string value?
53 54

  FlagType type() const { return type_; }
55

56
  const char* name() const { return name_; }
57

58 59 60
  const char* comment() const { return cmt_; }

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

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

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

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

80
  double* float_variable() const {
81
    DCHECK(type_ == TYPE_FLOAT);
82
    return reinterpret_cast<double*>(valptr_);
83 84
  }

85
  const char* string_value() const {
86
    DCHECK(type_ == TYPE_STRING);
87 88 89
    return *reinterpret_cast<const char**>(valptr_);
  }

90
  void set_string_value(const char* value, bool owns_ptr) {
91
    DCHECK(type_ == TYPE_STRING);
92
    const char** ptr = reinterpret_cast<const char**>(valptr_);
93 94 95
    if (owns_ptr_ && *ptr != NULL) DeleteArray(*ptr);
    *ptr = value;
    owns_ptr_ = owns_ptr;
96
  }
97

98
  JSArguments* args_variable() const {
99
    DCHECK(type_ == TYPE_ARGS);
100 101 102
    return reinterpret_cast<JSArguments*>(valptr_);
  }

103
  bool bool_default() const {
104
    DCHECK(type_ == TYPE_BOOL);
105 106
    return *reinterpret_cast<const bool*>(defptr_);
  }
107

108
  int int_default() const {
109
    DCHECK(type_ == TYPE_INT);
110 111
    return *reinterpret_cast<const int*>(defptr_);
  }
112

113 114 115 116 117
  unsigned int uint_default() const {
    DCHECK(type_ == TYPE_UINT);
    return *reinterpret_cast<const unsigned int*>(defptr_);
  }

118
  double float_default() const {
119
    DCHECK(type_ == TYPE_FLOAT);
120 121
    return *reinterpret_cast<const double*>(defptr_);
  }
122

123
  const char* string_default() const {
124
    DCHECK(type_ == TYPE_STRING);
125
    return *reinterpret_cast<const char* const *>(defptr_);
126 127
  }

128
  JSArguments args_default() const {
129
    DCHECK(type_ == TYPE_ARGS);
130 131 132
    return *reinterpret_cast<const JSArguments*>(defptr_);
  }

133 134 135
  // Compare this flag's current value against the default.
  bool IsDefault() const {
    switch (type_) {
136 137
      case TYPE_BOOL:
        return *bool_variable() == bool_default();
138 139
      case TYPE_MAYBE_BOOL:
        return maybe_bool_variable()->has_value == false;
140 141
      case TYPE_INT:
        return *int_variable() == int_default();
142 143
      case TYPE_UINT:
        return *uint_variable() == uint_default();
144 145
      case TYPE_FLOAT:
        return *float_variable() == float_default();
146
      case TYPE_STRING: {
147
        const char* str1 = string_value();
148 149 150 151
        const char* str2 = string_default();
        if (str2 == NULL) return str1 == NULL;
        if (str1 == NULL) return str2 == NULL;
        return strcmp(str1, str2) == 0;
152 153
      }
      case TYPE_ARGS:
154
        return args_variable()->argc == 0;
155
    }
156 157
    UNREACHABLE();
    return true;
158
  }
159

160 161 162
  // Set a flag back to it's default value.
  void Reset() {
    switch (type_) {
163 164 165
      case TYPE_BOOL:
        *bool_variable() = bool_default();
        break;
166 167 168
      case TYPE_MAYBE_BOOL:
        *maybe_bool_variable() = MaybeBoolFlag::Create(false, false);
        break;
169 170 171
      case TYPE_INT:
        *int_variable() = int_default();
        break;
172 173 174
      case TYPE_UINT:
        *uint_variable() = uint_default();
        break;
175 176 177 178
      case TYPE_FLOAT:
        *float_variable() = float_default();
        break;
      case TYPE_STRING:
179
        set_string_value(string_default(), false);
180
        break;
181 182 183
      case TYPE_ARGS:
        *args_variable() = args_default();
        break;
184
    }
185
  }
186 187 188 189
};

Flag flags[] = {
#define FLAG_MODE_META
190
#include "src/flag-definitions.h"  // NOLINT(build/include)
191 192 193 194 195
};

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

}  // namespace
196 197


198
static const char* Type2String(Flag::FlagType type) {
199
  switch (type) {
200
    case Flag::TYPE_BOOL: return "bool";
201
    case Flag::TYPE_MAYBE_BOOL: return "maybe_bool";
202
    case Flag::TYPE_INT: return "int";
203 204
    case Flag::TYPE_UINT:
      return "uint";
205 206
    case Flag::TYPE_FLOAT: return "float";
    case Flag::TYPE_STRING: return "string";
207
    case Flag::TYPE_ARGS: return "arguments";
208 209 210 211 212 213
  }
  UNREACHABLE();
  return NULL;
}


214
std::ostream& operator<<(std::ostream& os, const Flag& flag) {  // NOLINT
215
  switch (flag.type()) {
216
    case Flag::TYPE_BOOL:
217
      os << (*flag.bool_variable() ? "true" : "false");
218
      break;
219
    case Flag::TYPE_MAYBE_BOOL:
220 221 222
      os << (flag.maybe_bool_variable()->has_value
                 ? (flag.maybe_bool_variable()->value ? "true" : "false")
                 : "unset");
223
      break;
224
    case Flag::TYPE_INT:
225
      os << *flag.int_variable();
226
      break;
227 228 229
    case Flag::TYPE_UINT:
      os << *flag.uint_variable();
      break;
230
    case Flag::TYPE_FLOAT:
231
      os << *flag.float_variable();
232
      break;
233
    case Flag::TYPE_STRING: {
234 235
      const char* str = flag.string_value();
      os << (str ? str : "NULL");
236 237 238
      break;
    }
    case Flag::TYPE_ARGS: {
239
      JSArguments args = *flag.args_variable();
240
      if (args.argc > 0) {
241
        os << args[0];
242
        for (int i = 1; i < args.argc; i++) {
243
          os << args[i];
244
        }
245 246
      }
      break;
247
    }
248
  }
249
  return os;
250 251 252
}


253
// static
254 255 256
List<const char*>* FlagList::argv() {
  List<const char*>* args = new List<const char*>(8);
  Flag* args_flag = NULL;
257 258
  for (size_t i = 0; i < num_flags; ++i) {
    Flag* f = &flags[i];
259
    if (!f->IsDefault()) {
260
      if (f->type() == Flag::TYPE_ARGS) {
261
        DCHECK(args_flag == NULL);
262 263 264
        args_flag = f;  // Must be last in arguments.
        continue;
      }
265 266
      {
        bool disabled = f->type() == Flag::TYPE_BOOL && !*f->bool_variable();
267
        std::ostringstream os;
268
        os << (disabled ? "--no" : "--") << f->name();
269
        args->Add(StrDup(os.str().c_str()));
270
      }
271
      if (f->type() != Flag::TYPE_BOOL) {
272
        std::ostringstream os;
273
        os << *f;
274
        args->Add(StrDup(os.str().c_str()));
275 276 277
      }
    }
  }
278
  if (args_flag != NULL) {
279
    std::ostringstream os;
280
    os << "--" << args_flag->name();
281
    args->Add(StrDup(os.str().c_str()));
282
    JSArguments jsargs = *args_flag->args_variable();
283
    for (int j = 0; j < jsargs.argc; j++) {
284
      args->Add(StrDup(jsargs[j]));
285 286
    }
  }
287
  return args;
288 289 290
}


291 292 293 294 295
inline char NormalizeChar(char ch) {
  return ch == '_' ? '-' : ch;
}


296 297 298 299 300 301 302 303 304 305
// Helper function to parse flags: Takes an argument arg and splits it into
// a flag name and flag value (or NULL if they are missing). is_bool is set
// 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.
static void SplitArgument(const char* arg,
                          char* buffer,
                          int buffer_size,
                          const char** name,
                          const char** value,
                          bool* is_bool) {
306 307 308 309
  *name = NULL;
  *value = NULL;
  *is_bool = false;

310
  if (arg != NULL && *arg == '-') {
311 312
    // find the begin of the flag name
    arg++;  // remove 1st '-'
313
    if (*arg == '-') {
314
      arg++;  // remove 2nd '-'
315 316 317 318 319 320
      if (arg[0] == '\0') {
        const char* kJSArgumentsFlagName = "js_arguments";
        *name = kJSArgumentsFlagName;
        return;
      }
    }
321 322
    if (arg[0] == 'n' && arg[1] == 'o') {
      arg += 2;  // remove "no"
323
      if (NormalizeChar(arg[0]) == '-') arg++;  // remove dash after "no".
324 325 326 327 328 329 330 331 332 333 334
      *is_bool = true;
    }
    *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
335 336
      size_t n = arg - *name;
      CHECK(n < static_cast<size_t>(buffer_size));  // buffer is too small
337
      MemCopy(buffer, *name, n);
338 339 340 341 342 343 344 345 346
      buffer[n] = '\0';
      *name = buffer;
      // get the value
      *value = arg + 1;
    }
  }
}


347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
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];
  }
  return NULL;
}


// static
367 368
int FlagList::SetFlagsFromCommandLine(int* argc,
                                      char** argv,
369
                                      bool remove_flags) {
370
  int return_code = 0;
371 372 373 374 375 376 377 378 379 380 381 382 383 384
  // 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;
    bool is_bool;
    SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);

    if (name != NULL) {
      // lookup the flag
385
      Flag* flag = FindFlag(name);
386
      if (flag == NULL) {
387 388 389 390 391 392 393
        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 {
394 395
          PrintF(stderr, "Error: unrecognized flag %s\n"
                 "Try --help for options\n", arg);
396 397
          return_code = j;
          break;
398
        }
399 400 401
      }

      // if we still need a flag value, use the next argument if available
402
      if (flag->type() != Flag::TYPE_BOOL &&
403
          flag->type() != Flag::TYPE_MAYBE_BOOL &&
404 405
          flag->type() != Flag::TYPE_ARGS &&
          value == NULL) {
406 407
        if (i < *argc) {
          value = argv[i++];
408 409
        }
        if (!value) {
410 411 412
          PrintF(stderr, "Error: missing value for flag %s of type %s\n"
                 "Try --help for options\n",
                 arg, Type2String(flag->type()));
413 414
          return_code = j;
          break;
415 416 417 418 419 420
        }
      }

      // set the flag
      char* endp = const_cast<char*>("");  // *endp is only read
      switch (flag->type()) {
421
        case Flag::TYPE_BOOL:
422 423
          *flag->bool_variable() = !is_bool;
          break;
424 425 426
        case Flag::TYPE_MAYBE_BOOL:
          *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !is_bool);
          break;
427
        case Flag::TYPE_INT:
428
          *flag->int_variable() = static_cast<int>(strtol(value, &endp, 10));
429
          break;
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
        case Flag::TYPE_UINT: {
          // We do not use strtoul because it accepts negative numbers.
          int64_t val = static_cast<int64_t>(strtoll(value, &endp, 10));
          if (val < 0 || val > std::numeric_limits<unsigned int>::max()) {
            PrintF(stderr,
                   "Error: Value for flag %s of type %s is out of bounds "
                   "[0-%" PRIu64
                   "]\n"
                   "Try --help for options\n",
                   arg, Type2String(flag->type()),
                   static_cast<uint64_t>(
                       std::numeric_limits<unsigned int>::max()));
            return_code = j;
            break;
          }
          *flag->uint_variable() = static_cast<unsigned int>(val);
          break;
        }
448
        case Flag::TYPE_FLOAT:
449 450
          *flag->float_variable() = strtod(value, &endp);
          break;
451
        case Flag::TYPE_STRING:
452
          flag->set_string_value(value ? StrDup(value) : NULL, true);
453
          break;
454 455 456 457 458
        case Flag::TYPE_ARGS: {
          int start_pos = (value == NULL) ? i : i - 1;
          int js_argc = *argc - start_pos;
          const char** js_argv = NewArray<const char*>(js_argc);
          if (value != NULL) {
459
            js_argv[0] = StrDup(value);
460 461
          }
          for (int k = i; k < *argc; k++) {
462
            js_argv[k - start_pos] = StrDup(argv[k]);
463
          }
464
          *flag->args_variable() = JSArguments::Create(js_argc, js_argv);
465 466 467
          i = *argc;  // Consume all arguments
          break;
        }
468 469 470
      }

      // handle errors
471 472 473
      bool is_bool_type = flag->type() == Flag::TYPE_BOOL ||
          flag->type() == Flag::TYPE_MAYBE_BOOL;
      if ((is_bool_type && value != NULL) || (!is_bool_type && is_bool) ||
474
          *endp != '\0') {
475 476 477
        PrintF(stderr, "Error: illegal value for flag %s of type %s\n"
               "Try --help for options\n",
               arg, Type2String(flag->type()));
478 479 480 481
        if (is_bool_type) {
          PrintF(stderr,
                 "To set or unset a boolean flag, use --flag or --no-flag.\n");
        }
482 483
        return_code = j;
        break;
484 485 486
      }

      // remove the flag & value from the command
487 488
      if (remove_flags) {
        while (j < i) {
489
          argv[j++] = NULL;
490 491
        }
      }
492 493 494 495 496 497 498 499 500 501 502 503 504
    }
  }

  // shrink the argument list
  if (remove_flags) {
    int j = 1;
    for (int i = 1; i < *argc; i++) {
      if (argv[i] != NULL)
        argv[j++] = argv[i];
    }
    *argc = j;
  }

505 506
  if (FLAG_help) {
    PrintHelp();
507
    exit(0);
508
  }
509
  // parsed all flags successfully
510
  return return_code;
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
}


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;
}


526
// static
527 528
int FlagList::SetFlagsFromString(const char* str, int len) {
  // make a 0-terminated copy of str
529
  ScopedVector<char> copy0(len + 1);
530
  MemCopy(copy0.start(), str, len);
531 532 533
  copy0[len] = '\0';

  // strip leading white space
534
  char* copy = SkipWhiteSpace(copy0.start());
535 536 537 538 539 540 541 542 543

  // 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
544
  ScopedVector<char*> argv(argc);
545 546 547 548 549 550 551 552 553 554 555

  // 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);
  }

  // set the flags
556
  int result = SetFlagsFromCommandLine(&argc, argv.start(), false);
557 558 559 560 561

  return result;
}


562 563 564 565 566 567 568 569 570 571
// static
void FlagList::ResetAllFlags() {
  for (size_t i = 0; i < num_flags; ++i) {
    flags[i].Reset();
  }
}


// static
void FlagList::PrintHelp() {
572
  CpuFeatures::Probe(false);
573 574 575
  CpuFeatures::PrintTarget();
  CpuFeatures::PrintFeatures();

576 577
  OFStream os(stdout);
  os << "Usage:\n"
578 579 580 581 582 583 584 585 586 587 588 589 590
        "  shell [options] -e string\n"
        "    execute string in V8\n"
        "  shell [options] file1 file2 ... filek\n"
        "    run JavaScript scripts in file1, file2, ..., filek\n"
        "  shell [options]\n"
        "  shell [options] --shell [file1 file2 ... filek]\n"
        "    run an interactive JavaScript shell\n"
        "  d8 [options] file1 file2 ... filek\n"
        "  d8 [options]\n"
        "  d8 [options] --shell [file1 file2 ... filek]\n"
        "    run the new debugging shell\n\n"
        "Options:\n";

591 592
  for (size_t i = 0; i < num_flags; ++i) {
    Flag* f = &flags[i];
593 594 595
    os << "  --" << f->name() << " (" << f->comment() << ")\n"
       << "        type: " << Type2String(f->type()) << "  default: " << *f
       << "\n";
596
  }
597 598
}

599

600
static uint32_t flag_hash = 0;
601

602

603
void ComputeFlagListHash() {
604
  std::ostringstream modified_args_as_string;
605 606 607
#ifdef DEBUG
  modified_args_as_string << "debug";
#endif  // DEBUG
608 609 610
  for (size_t i = 0; i < num_flags; ++i) {
    Flag* current = &flags[i];
    if (!current->IsDefault()) {
611
      modified_args_as_string << i;
612 613 614 615
      modified_args_as_string << *current;
    }
  }
  std::string args(modified_args_as_string.str());
616
  flag_hash = static_cast<uint32_t>(
617 618
      base::hash_range(args.c_str(), args.c_str() + args.length()));
}
619 620 621 622 623


// static
void FlagList::EnforceFlagImplications() {
#define FLAG_MODE_DEFINE_IMPLICATIONS
624
#include "src/flag-definitions.h"  // NOLINT(build/include)
625 626 627 628 629 630
#undef FLAG_MODE_DEFINE_IMPLICATIONS
  ComputeFlagListHash();
}


uint32_t FlagList::Hash() { return flag_hash; }
631 632
}  // namespace internal
}  // namespace v8