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 "smart-pointers.h"
12 #include "string-stream.h"
14 #if V8_TARGET_ARCH_ARM
15 #include "arm/assembler-arm-inl.h"
21 // Define all of our flags.
22 #define FLAG_MODE_DEFINE
23 #include "flag-definitions.h"
25 // Define all of our flags default values.
26 #define FLAG_MODE_DEFINE_DEFAULTS
27 #include "flag-definitions.h"
31 // This structure represents a single entry in the flag system, with a pointer
32 // to the actual flag, default value, comment, etc. This is designed to be POD
33 // initialized as to avoid requiring static constructors.
35 enum FlagType { TYPE_BOOL, TYPE_MAYBE_BOOL, TYPE_INT, TYPE_FLOAT,
36 TYPE_STRING, TYPE_ARGS };
38 FlagType type_; // What type of flag, bool, int, or string.
39 const char* name_; // Name of the flag, ex "my_flag".
40 void* valptr_; // Pointer to the global flag variable.
41 const void* defptr_; // Pointer to the default value.
42 const char* cmt_; // A comment about the flags purpose.
43 bool owns_ptr_; // Does the flag own its string value?
45 FlagType type() const { return type_; }
47 const char* name() const { return name_; }
49 const char* comment() const { return cmt_; }
51 bool* bool_variable() const {
52 ASSERT(type_ == TYPE_BOOL);
53 return reinterpret_cast<bool*>(valptr_);
56 MaybeBoolFlag* maybe_bool_variable() const {
57 ASSERT(type_ == TYPE_MAYBE_BOOL);
58 return reinterpret_cast<MaybeBoolFlag*>(valptr_);
61 int* int_variable() const {
62 ASSERT(type_ == TYPE_INT);
63 return reinterpret_cast<int*>(valptr_);
66 double* float_variable() const {
67 ASSERT(type_ == TYPE_FLOAT);
68 return reinterpret_cast<double*>(valptr_);
71 const char* string_value() const {
72 ASSERT(type_ == TYPE_STRING);
73 return *reinterpret_cast<const char**>(valptr_);
76 void set_string_value(const char* value, bool owns_ptr) {
77 ASSERT(type_ == TYPE_STRING);
78 const char** ptr = reinterpret_cast<const char**>(valptr_);
79 if (owns_ptr_ && *ptr != NULL) DeleteArray(*ptr);
84 JSArguments* args_variable() const {
85 ASSERT(type_ == TYPE_ARGS);
86 return reinterpret_cast<JSArguments*>(valptr_);
89 bool bool_default() const {
90 ASSERT(type_ == TYPE_BOOL);
91 return *reinterpret_cast<const bool*>(defptr_);
94 int int_default() const {
95 ASSERT(type_ == TYPE_INT);
96 return *reinterpret_cast<const int*>(defptr_);
99 double float_default() const {
100 ASSERT(type_ == TYPE_FLOAT);
101 return *reinterpret_cast<const double*>(defptr_);
104 const char* string_default() const {
105 ASSERT(type_ == TYPE_STRING);
106 return *reinterpret_cast<const char* const *>(defptr_);
109 JSArguments args_default() const {
110 ASSERT(type_ == TYPE_ARGS);
111 return *reinterpret_cast<const JSArguments*>(defptr_);
114 // Compare this flag's current value against the default.
115 bool IsDefault() const {
118 return *bool_variable() == bool_default();
119 case TYPE_MAYBE_BOOL:
120 return maybe_bool_variable()->has_value == false;
122 return *int_variable() == int_default();
124 return *float_variable() == float_default();
126 const char* str1 = string_value();
127 const char* str2 = string_default();
128 if (str2 == NULL) return str1 == NULL;
129 if (str1 == NULL) return str2 == NULL;
130 return strcmp(str1, str2) == 0;
133 return args_variable()->argc == 0;
139 // Set a flag back to it's default value.
143 *bool_variable() = bool_default();
145 case TYPE_MAYBE_BOOL:
146 *maybe_bool_variable() = MaybeBoolFlag::Create(false, false);
149 *int_variable() = int_default();
152 *float_variable() = float_default();
155 set_string_value(string_default(), false);
158 *args_variable() = args_default();
165 #define FLAG_MODE_META
166 #include "flag-definitions.h"
169 const size_t num_flags = sizeof(flags) / sizeof(*flags);
174 static const char* Type2String(Flag::FlagType type) {
176 case Flag::TYPE_BOOL: return "bool";
177 case Flag::TYPE_MAYBE_BOOL: return "maybe_bool";
178 case Flag::TYPE_INT: return "int";
179 case Flag::TYPE_FLOAT: return "float";
180 case Flag::TYPE_STRING: return "string";
181 case Flag::TYPE_ARGS: return "arguments";
188 static SmartArrayPointer<const char> ToString(Flag* flag) {
189 HeapStringAllocator string_allocator;
190 StringStream buffer(&string_allocator);
191 switch (flag->type()) {
192 case Flag::TYPE_BOOL:
193 buffer.Add("%s", (*flag->bool_variable() ? "true" : "false"));
195 case Flag::TYPE_MAYBE_BOOL:
196 buffer.Add("%s", flag->maybe_bool_variable()->has_value
197 ? (flag->maybe_bool_variable()->value ? "true" : "false")
201 buffer.Add("%d", *flag->int_variable());
203 case Flag::TYPE_FLOAT:
204 buffer.Add("%f", FmtElm(*flag->float_variable()));
206 case Flag::TYPE_STRING: {
207 const char* str = flag->string_value();
208 buffer.Add("%s", str ? str : "NULL");
211 case Flag::TYPE_ARGS: {
212 JSArguments args = *flag->args_variable();
214 buffer.Add("%s", args[0]);
215 for (int i = 1; i < args.argc; i++) {
216 buffer.Add(" %s", args[i]);
222 return buffer.ToCString();
227 List<const char*>* FlagList::argv() {
228 List<const char*>* args = new List<const char*>(8);
229 Flag* args_flag = NULL;
230 for (size_t i = 0; i < num_flags; ++i) {
232 if (!f->IsDefault()) {
233 if (f->type() == Flag::TYPE_ARGS) {
234 ASSERT(args_flag == NULL);
235 args_flag = f; // Must be last in arguments.
238 HeapStringAllocator string_allocator;
239 StringStream buffer(&string_allocator);
240 if (f->type() != Flag::TYPE_BOOL || *(f->bool_variable())) {
241 buffer.Add("--%s", f->name());
243 buffer.Add("--no%s", f->name());
245 args->Add(buffer.ToCString().Detach());
246 if (f->type() != Flag::TYPE_BOOL) {
247 args->Add(ToString(f).Detach());
251 if (args_flag != NULL) {
252 HeapStringAllocator string_allocator;
253 StringStream buffer(&string_allocator);
254 buffer.Add("--%s", args_flag->name());
255 args->Add(buffer.ToCString().Detach());
256 JSArguments jsargs = *args_flag->args_variable();
257 for (int j = 0; j < jsargs.argc; j++) {
258 args->Add(StrDup(jsargs[j]));
265 inline char NormalizeChar(char ch) {
266 return ch == '_' ? '-' : ch;
270 // Helper function to parse flags: Takes an argument arg and splits it into
271 // a flag name and flag value (or NULL if they are missing). is_bool is set
272 // if the arg started with "-no" or "--no". The buffer may be used to NUL-
273 // terminate the name, it must be large enough to hold any possible name.
274 static void SplitArgument(const char* arg,
284 if (arg != NULL && *arg == '-') {
285 // find the begin of the flag name
286 arg++; // remove 1st '-'
288 arg++; // remove 2nd '-'
289 if (arg[0] == '\0') {
290 const char* kJSArgumentsFlagName = "js_arguments";
291 *name = kJSArgumentsFlagName;
295 if (arg[0] == 'n' && arg[1] == 'o') {
296 arg += 2; // remove "no"
297 if (NormalizeChar(arg[0]) == '-') arg++; // remove dash after "no".
302 // find the end of the flag name
303 while (*arg != '\0' && *arg != '=')
306 // get the value if any
308 // make a copy so we can NUL-terminate flag name
309 size_t n = arg - *name;
310 CHECK(n < static_cast<size_t>(buffer_size)); // buffer is too small
311 OS::MemCopy(buffer, *name, n);
321 static bool EqualNames(const char* a, const char* b) {
322 for (int i = 0; NormalizeChar(a[i]) == NormalizeChar(b[i]); i++) {
331 static Flag* FindFlag(const char* name) {
332 for (size_t i = 0; i < num_flags; ++i) {
333 if (EqualNames(name, flags[i].name()))
340 bool FlagList::serializer_enabled_ = false;
344 int FlagList::SetFlagsFromCommandLine(int* argc,
347 bool serializer_enabled) {
348 serializer_enabled_ = serializer_enabled;
351 for (int i = 1; i < *argc;) {
353 const char* arg = argv[i++];
355 // split arg into flag components
360 SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
364 Flag* flag = FindFlag(name);
367 // We don't recognize this flag but since we're removing
368 // the flags we recognize we assume that the remaining flags
369 // will be processed somewhere else so this flag might make
373 PrintF(stderr, "Error: unrecognized flag %s\n"
374 "Try --help for options\n", arg);
380 // if we still need a flag value, use the next argument if available
381 if (flag->type() != Flag::TYPE_BOOL &&
382 flag->type() != Flag::TYPE_MAYBE_BOOL &&
383 flag->type() != Flag::TYPE_ARGS &&
388 PrintF(stderr, "Error: missing value for flag %s of type %s\n"
389 "Try --help for options\n",
390 arg, Type2String(flag->type()));
397 char* endp = const_cast<char*>(""); // *endp is only read
398 switch (flag->type()) {
399 case Flag::TYPE_BOOL:
400 *flag->bool_variable() = !is_bool;
402 case Flag::TYPE_MAYBE_BOOL:
403 *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !is_bool);
406 *flag->int_variable() = strtol(value, &endp, 10); // NOLINT
408 case Flag::TYPE_FLOAT:
409 *flag->float_variable() = strtod(value, &endp);
411 case Flag::TYPE_STRING:
412 flag->set_string_value(value ? StrDup(value) : NULL, true);
414 case Flag::TYPE_ARGS: {
415 int start_pos = (value == NULL) ? i : i - 1;
416 int js_argc = *argc - start_pos;
417 const char** js_argv = NewArray<const char*>(js_argc);
419 js_argv[0] = StrDup(value);
421 for (int k = i; k < *argc; k++) {
422 js_argv[k - start_pos] = StrDup(argv[k]);
424 *flag->args_variable() = JSArguments::Create(js_argc, js_argv);
425 i = *argc; // Consume all arguments
431 bool is_bool_type = flag->type() == Flag::TYPE_BOOL ||
432 flag->type() == Flag::TYPE_MAYBE_BOOL;
433 if ((is_bool_type && value != NULL) || (!is_bool_type && is_bool) ||
435 PrintF(stderr, "Error: illegal value for flag %s of type %s\n"
436 "Try --help for options\n",
437 arg, Type2String(flag->type()));
442 // remove the flag & value from the command
451 // shrink the argument list
454 for (int i = 1; i < *argc; i++) {
465 // parsed all flags successfully
470 static char* SkipWhiteSpace(char* p) {
471 while (*p != '\0' && isspace(*p) != 0) p++;
476 static char* SkipBlackSpace(char* p) {
477 while (*p != '\0' && isspace(*p) == 0) p++;
483 int FlagList::SetFlagsFromString(const char* str, int len) {
484 // make a 0-terminated copy of str
485 ScopedVector<char> copy0(len + 1);
486 OS::MemCopy(copy0.start(), str, len);
489 // strip leading white space
490 char* copy = SkipWhiteSpace(copy0.start());
492 // count the number of 'arguments'
493 int argc = 1; // be compatible with SetFlagsFromCommandLine()
494 for (char* p = copy; *p != '\0'; argc++) {
495 p = SkipBlackSpace(p);
496 p = SkipWhiteSpace(p);
499 // allocate argument array
500 ScopedVector<char*> argv(argc);
502 // split the flags string into arguments
503 argc = 1; // be compatible with SetFlagsFromCommandLine()
504 for (char* p = copy; *p != '\0'; argc++) {
506 p = SkipBlackSpace(p);
507 if (*p != '\0') *p++ = '\0'; // 0-terminate argument
508 p = SkipWhiteSpace(p);
512 int result = SetFlagsFromCommandLine(&argc, argv.start(), false);
519 void FlagList::ResetAllFlags() {
520 for (size_t i = 0; i < num_flags; ++i) {
527 void FlagList::PrintHelp() {
528 #if V8_TARGET_ARCH_ARM
529 CpuFeatures::PrintTarget();
530 CpuFeatures::Probe(serializer_enabled_);
531 CpuFeatures::PrintFeatures();
532 #endif // V8_TARGET_ARCH_ARM
535 printf(" shell [options] -e string\n");
536 printf(" execute string in V8\n");
537 printf(" shell [options] file1 file2 ... filek\n");
538 printf(" run JavaScript scripts in file1, file2, ..., filek\n");
539 printf(" shell [options]\n");
540 printf(" shell [options] --shell [file1 file2 ... filek]\n");
541 printf(" run an interactive JavaScript shell\n");
542 printf(" d8 [options] file1 file2 ... filek\n");
543 printf(" d8 [options]\n");
544 printf(" d8 [options] --shell [file1 file2 ... filek]\n");
545 printf(" run the new debugging shell\n\n");
546 printf("Options:\n");
547 for (size_t i = 0; i < num_flags; ++i) {
549 SmartArrayPointer<const char> value = ToString(f);
550 printf(" --%s (%s)\n type: %s default: %s\n",
551 f->name(), f->comment(), Type2String(f->type()), value.get());
557 void FlagList::EnforceFlagImplications() {
558 #define FLAG_MODE_DEFINE_IMPLICATIONS
559 #include "flag-definitions.h"
560 #undef FLAG_MODE_DEFINE_IMPLICATIONS
563 } } // namespace v8::internal