ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <ctype.h> |
| 29 | #include <stdlib.h> |
| 30 | |
| 31 | #include "v8.h" |
| 32 | |
| 33 | #include "platform.h" |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 34 | #include "smart-pointers.h" |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 35 | #include "string-stream.h" |
| 36 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 37 | #if V8_TARGET_ARCH_ARM |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 38 | #include "arm/assembler-arm-inl.h" |
| 39 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 41 | namespace v8 { |
| 42 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 44 | // Define all of our flags. |
| 45 | #define FLAG_MODE_DEFINE |
| 46 | #include "flag-definitions.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 48 | // Define all of our flags default values. |
| 49 | #define FLAG_MODE_DEFINE_DEFAULTS |
| 50 | #include "flag-definitions.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 52 | namespace { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 54 | // This structure represents a single entry in the flag system, with a pointer |
| 55 | // to the actual flag, default value, comment, etc. This is designed to be POD |
| 56 | // initialized as to avoid requiring static constructors. |
| 57 | struct Flag { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 58 | enum FlagType { TYPE_BOOL, TYPE_MAYBE_BOOL, TYPE_INT, TYPE_FLOAT, |
| 59 | TYPE_STRING, TYPE_ARGS }; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 60 | |
| 61 | FlagType type_; // What type of flag, bool, int, or string. |
| 62 | const char* name_; // Name of the flag, ex "my_flag". |
| 63 | void* valptr_; // Pointer to the global flag variable. |
| 64 | const void* defptr_; // Pointer to the default value. |
| 65 | const char* cmt_; // A comment about the flags purpose. |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 66 | bool owns_ptr_; // Does the flag own its string value? |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 67 | |
| 68 | FlagType type() const { return type_; } |
| 69 | |
| 70 | const char* name() const { return name_; } |
| 71 | |
| 72 | const char* comment() const { return cmt_; } |
| 73 | |
| 74 | bool* bool_variable() const { |
| 75 | ASSERT(type_ == TYPE_BOOL); |
| 76 | return reinterpret_cast<bool*>(valptr_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 79 | MaybeBoolFlag* maybe_bool_variable() const { |
| 80 | ASSERT(type_ == TYPE_MAYBE_BOOL); |
| 81 | return reinterpret_cast<MaybeBoolFlag*>(valptr_); |
| 82 | } |
| 83 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 84 | int* int_variable() const { |
| 85 | ASSERT(type_ == TYPE_INT); |
| 86 | return reinterpret_cast<int*>(valptr_); |
| 87 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 89 | double* float_variable() const { |
| 90 | ASSERT(type_ == TYPE_FLOAT); |
| 91 | return reinterpret_cast<double*>(valptr_); |
| 92 | } |
| 93 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 94 | const char* string_value() const { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 95 | ASSERT(type_ == TYPE_STRING); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 96 | return *reinterpret_cast<const char**>(valptr_); |
| 97 | } |
| 98 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 99 | void set_string_value(const char* value, bool owns_ptr) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 100 | ASSERT(type_ == TYPE_STRING); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 101 | const char** ptr = reinterpret_cast<const char**>(valptr_); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 102 | if (owns_ptr_ && *ptr != NULL) DeleteArray(*ptr); |
| 103 | *ptr = value; |
| 104 | owns_ptr_ = owns_ptr; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 105 | } |
| 106 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 107 | JSArguments* args_variable() const { |
| 108 | ASSERT(type_ == TYPE_ARGS); |
| 109 | return reinterpret_cast<JSArguments*>(valptr_); |
| 110 | } |
| 111 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 112 | bool bool_default() const { |
| 113 | ASSERT(type_ == TYPE_BOOL); |
| 114 | return *reinterpret_cast<const bool*>(defptr_); |
| 115 | } |
| 116 | |
| 117 | int int_default() const { |
| 118 | ASSERT(type_ == TYPE_INT); |
| 119 | return *reinterpret_cast<const int*>(defptr_); |
| 120 | } |
| 121 | |
| 122 | double float_default() const { |
| 123 | ASSERT(type_ == TYPE_FLOAT); |
| 124 | return *reinterpret_cast<const double*>(defptr_); |
| 125 | } |
| 126 | |
| 127 | const char* string_default() const { |
| 128 | ASSERT(type_ == TYPE_STRING); |
| 129 | return *reinterpret_cast<const char* const *>(defptr_); |
| 130 | } |
| 131 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 132 | JSArguments args_default() const { |
| 133 | ASSERT(type_ == TYPE_ARGS); |
| 134 | return *reinterpret_cast<const JSArguments*>(defptr_); |
| 135 | } |
| 136 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 137 | // Compare this flag's current value against the default. |
| 138 | bool IsDefault() const { |
| 139 | switch (type_) { |
| 140 | case TYPE_BOOL: |
| 141 | return *bool_variable() == bool_default(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 142 | case TYPE_MAYBE_BOOL: |
| 143 | return maybe_bool_variable()->has_value == false; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 144 | case TYPE_INT: |
| 145 | return *int_variable() == int_default(); |
| 146 | case TYPE_FLOAT: |
| 147 | return *float_variable() == float_default(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 148 | case TYPE_STRING: { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 149 | const char* str1 = string_value(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 150 | const char* str2 = string_default(); |
| 151 | if (str2 == NULL) return str1 == NULL; |
| 152 | if (str1 == NULL) return str2 == NULL; |
| 153 | return strcmp(str1, str2) == 0; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 154 | } |
| 155 | case TYPE_ARGS: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 156 | return args_variable()->argc == 0; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 157 | } |
| 158 | UNREACHABLE(); |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | // Set a flag back to it's default value. |
| 163 | void Reset() { |
| 164 | switch (type_) { |
| 165 | case TYPE_BOOL: |
| 166 | *bool_variable() = bool_default(); |
| 167 | break; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 168 | case TYPE_MAYBE_BOOL: |
| 169 | *maybe_bool_variable() = MaybeBoolFlag::Create(false, false); |
| 170 | break; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 171 | case TYPE_INT: |
| 172 | *int_variable() = int_default(); |
| 173 | break; |
| 174 | case TYPE_FLOAT: |
| 175 | *float_variable() = float_default(); |
| 176 | break; |
| 177 | case TYPE_STRING: |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 178 | set_string_value(string_default(), false); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 179 | break; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 180 | case TYPE_ARGS: |
| 181 | *args_variable() = args_default(); |
| 182 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | Flag flags[] = { |
| 188 | #define FLAG_MODE_META |
| 189 | #include "flag-definitions.h" |
| 190 | }; |
| 191 | |
| 192 | const size_t num_flags = sizeof(flags) / sizeof(*flags); |
| 193 | |
| 194 | } // namespace |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 195 | |
| 196 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 197 | static const char* Type2String(Flag::FlagType type) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 198 | switch (type) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 199 | case Flag::TYPE_BOOL: return "bool"; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 200 | case Flag::TYPE_MAYBE_BOOL: return "maybe_bool"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 201 | case Flag::TYPE_INT: return "int"; |
| 202 | case Flag::TYPE_FLOAT: return "float"; |
| 203 | case Flag::TYPE_STRING: return "string"; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 204 | case Flag::TYPE_ARGS: return "arguments"; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 205 | } |
| 206 | UNREACHABLE(); |
| 207 | return NULL; |
| 208 | } |
| 209 | |
| 210 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 211 | static SmartArrayPointer<const char> ToString(Flag* flag) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 212 | HeapStringAllocator string_allocator; |
| 213 | StringStream buffer(&string_allocator); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 214 | switch (flag->type()) { |
| 215 | case Flag::TYPE_BOOL: |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 216 | buffer.Add("%s", (*flag->bool_variable() ? "true" : "false")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | break; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 218 | case Flag::TYPE_MAYBE_BOOL: |
| 219 | buffer.Add("%s", flag->maybe_bool_variable()->has_value |
| 220 | ? (flag->maybe_bool_variable()->value ? "true" : "false") |
| 221 | : "unset"); |
| 222 | break; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 223 | case Flag::TYPE_INT: |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 224 | buffer.Add("%d", *flag->int_variable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | break; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 226 | case Flag::TYPE_FLOAT: |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 227 | buffer.Add("%f", FmtElm(*flag->float_variable())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | break; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 229 | case Flag::TYPE_STRING: { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 230 | const char* str = flag->string_value(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 231 | buffer.Add("%s", str ? str : "NULL"); |
| 232 | break; |
| 233 | } |
| 234 | case Flag::TYPE_ARGS: { |
| 235 | JSArguments args = *flag->args_variable(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 236 | if (args.argc > 0) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 237 | buffer.Add("%s", args[0]); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 238 | for (int i = 1; i < args.argc; i++) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 239 | buffer.Add(" %s", args[i]); |
| 240 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 241 | } |
| 242 | break; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 243 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 245 | return buffer.ToCString(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 249 | // static |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 250 | List<const char*>* FlagList::argv() { |
| 251 | List<const char*>* args = new List<const char*>(8); |
| 252 | Flag* args_flag = NULL; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 253 | for (size_t i = 0; i < num_flags; ++i) { |
| 254 | Flag* f = &flags[i]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 255 | if (!f->IsDefault()) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 256 | if (f->type() == Flag::TYPE_ARGS) { |
| 257 | ASSERT(args_flag == NULL); |
| 258 | args_flag = f; // Must be last in arguments. |
| 259 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 261 | HeapStringAllocator string_allocator; |
| 262 | StringStream buffer(&string_allocator); |
| 263 | if (f->type() != Flag::TYPE_BOOL || *(f->bool_variable())) { |
| 264 | buffer.Add("--%s", f->name()); |
| 265 | } else { |
| 266 | buffer.Add("--no%s", f->name()); |
| 267 | } |
| 268 | args->Add(buffer.ToCString().Detach()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 269 | if (f->type() != Flag::TYPE_BOOL) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 270 | args->Add(ToString(f).Detach()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 274 | if (args_flag != NULL) { |
| 275 | HeapStringAllocator string_allocator; |
| 276 | StringStream buffer(&string_allocator); |
| 277 | buffer.Add("--%s", args_flag->name()); |
| 278 | args->Add(buffer.ToCString().Detach()); |
| 279 | JSArguments jsargs = *args_flag->args_variable(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 280 | for (int j = 0; j < jsargs.argc; j++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 281 | args->Add(StrDup(jsargs[j])); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | return args; |
| 285 | } |
| 286 | |
| 287 | |
verwaest@chromium.org | ec6855e | 2013-08-22 12:26:58 +0000 | [diff] [blame] | 288 | inline char NormalizeChar(char ch) { |
| 289 | return ch == '_' ? '-' : ch; |
| 290 | } |
| 291 | |
| 292 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 293 | // Helper function to parse flags: Takes an argument arg and splits it into |
| 294 | // a flag name and flag value (or NULL if they are missing). is_bool is set |
| 295 | // if the arg started with "-no" or "--no". The buffer may be used to NUL- |
| 296 | // terminate the name, it must be large enough to hold any possible name. |
| 297 | static void SplitArgument(const char* arg, |
| 298 | char* buffer, |
| 299 | int buffer_size, |
| 300 | const char** name, |
| 301 | const char** value, |
| 302 | bool* is_bool) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 303 | *name = NULL; |
| 304 | *value = NULL; |
| 305 | *is_bool = false; |
| 306 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 307 | if (arg != NULL && *arg == '-') { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 308 | // find the begin of the flag name |
| 309 | arg++; // remove 1st '-' |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 310 | if (*arg == '-') { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | arg++; // remove 2nd '-' |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 312 | if (arg[0] == '\0') { |
| 313 | const char* kJSArgumentsFlagName = "js_arguments"; |
| 314 | *name = kJSArgumentsFlagName; |
| 315 | return; |
| 316 | } |
| 317 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 318 | if (arg[0] == 'n' && arg[1] == 'o') { |
| 319 | arg += 2; // remove "no" |
verwaest@chromium.org | ec6855e | 2013-08-22 12:26:58 +0000 | [diff] [blame] | 320 | if (NormalizeChar(arg[0]) == '-') arg++; // remove dash after "no". |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 321 | *is_bool = true; |
| 322 | } |
| 323 | *name = arg; |
| 324 | |
| 325 | // find the end of the flag name |
| 326 | while (*arg != '\0' && *arg != '=') |
| 327 | arg++; |
| 328 | |
| 329 | // get the value if any |
| 330 | if (*arg == '=') { |
| 331 | // make a copy so we can NUL-terminate flag name |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 332 | size_t n = arg - *name; |
| 333 | CHECK(n < static_cast<size_t>(buffer_size)); // buffer is too small |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 334 | OS::MemCopy(buffer, *name, n); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 335 | buffer[n] = '\0'; |
| 336 | *name = buffer; |
| 337 | // get the value |
| 338 | *value = arg + 1; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 344 | static bool EqualNames(const char* a, const char* b) { |
| 345 | for (int i = 0; NormalizeChar(a[i]) == NormalizeChar(b[i]); i++) { |
| 346 | if (a[i] == '\0') { |
| 347 | return true; |
| 348 | } |
| 349 | } |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | static Flag* FindFlag(const char* name) { |
| 355 | for (size_t i = 0; i < num_flags; ++i) { |
| 356 | if (EqualNames(name, flags[i].name())) |
| 357 | return &flags[i]; |
| 358 | } |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | // static |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 364 | int FlagList::SetFlagsFromCommandLine(int* argc, |
| 365 | char** argv, |
| 366 | bool remove_flags) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 367 | int return_code = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 368 | // parse arguments |
| 369 | for (int i = 1; i < *argc;) { |
| 370 | int j = i; // j > 0 |
| 371 | const char* arg = argv[i++]; |
| 372 | |
| 373 | // split arg into flag components |
| 374 | char buffer[1*KB]; |
| 375 | const char* name; |
| 376 | const char* value; |
| 377 | bool is_bool; |
| 378 | SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool); |
| 379 | |
| 380 | if (name != NULL) { |
| 381 | // lookup the flag |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 382 | Flag* flag = FindFlag(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 383 | if (flag == NULL) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 384 | if (remove_flags) { |
| 385 | // We don't recognize this flag but since we're removing |
| 386 | // the flags we recognize we assume that the remaining flags |
| 387 | // will be processed somewhere else so this flag might make |
| 388 | // sense there. |
| 389 | continue; |
| 390 | } else { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 391 | PrintF(stderr, "Error: unrecognized flag %s\n" |
| 392 | "Try --help for options\n", arg); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 393 | return_code = j; |
| 394 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 395 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // if we still need a flag value, use the next argument if available |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 399 | if (flag->type() != Flag::TYPE_BOOL && |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 400 | flag->type() != Flag::TYPE_MAYBE_BOOL && |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 401 | flag->type() != Flag::TYPE_ARGS && |
| 402 | value == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 403 | if (i < *argc) { |
| 404 | value = argv[i++]; |
| 405 | } else { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 406 | PrintF(stderr, "Error: missing value for flag %s of type %s\n" |
| 407 | "Try --help for options\n", |
| 408 | arg, Type2String(flag->type())); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 409 | return_code = j; |
| 410 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | // set the flag |
| 415 | char* endp = const_cast<char*>(""); // *endp is only read |
| 416 | switch (flag->type()) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 417 | case Flag::TYPE_BOOL: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 418 | *flag->bool_variable() = !is_bool; |
| 419 | break; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 420 | case Flag::TYPE_MAYBE_BOOL: |
| 421 | *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !is_bool); |
| 422 | break; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 423 | case Flag::TYPE_INT: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 424 | *flag->int_variable() = strtol(value, &endp, 10); // NOLINT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 425 | break; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 426 | case Flag::TYPE_FLOAT: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 427 | *flag->float_variable() = strtod(value, &endp); |
| 428 | break; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 429 | case Flag::TYPE_STRING: |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 430 | flag->set_string_value(value ? StrDup(value) : NULL, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 431 | break; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 432 | case Flag::TYPE_ARGS: { |
| 433 | int start_pos = (value == NULL) ? i : i - 1; |
| 434 | int js_argc = *argc - start_pos; |
| 435 | const char** js_argv = NewArray<const char*>(js_argc); |
| 436 | if (value != NULL) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 437 | js_argv[0] = StrDup(value); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 438 | } |
| 439 | for (int k = i; k < *argc; k++) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 440 | js_argv[k - start_pos] = StrDup(argv[k]); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 441 | } |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 442 | *flag->args_variable() = JSArguments::Create(js_argc, js_argv); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 443 | i = *argc; // Consume all arguments |
| 444 | break; |
| 445 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // handle errors |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 449 | bool is_bool_type = flag->type() == Flag::TYPE_BOOL || |
| 450 | flag->type() == Flag::TYPE_MAYBE_BOOL; |
| 451 | if ((is_bool_type && value != NULL) || (!is_bool_type && is_bool) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 452 | *endp != '\0') { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 453 | PrintF(stderr, "Error: illegal value for flag %s of type %s\n" |
| 454 | "Try --help for options\n", |
| 455 | arg, Type2String(flag->type())); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 456 | return_code = j; |
| 457 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // remove the flag & value from the command |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 461 | if (remove_flags) { |
| 462 | while (j < i) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 463 | argv[j++] = NULL; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | // shrink the argument list |
| 470 | if (remove_flags) { |
| 471 | int j = 1; |
| 472 | for (int i = 1; i < *argc; i++) { |
| 473 | if (argv[i] != NULL) |
| 474 | argv[j++] = argv[i]; |
| 475 | } |
| 476 | *argc = j; |
| 477 | } |
| 478 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 479 | if (FLAG_help) { |
| 480 | PrintHelp(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 481 | exit(0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 482 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 483 | // parsed all flags successfully |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 484 | return return_code; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
| 488 | static char* SkipWhiteSpace(char* p) { |
| 489 | while (*p != '\0' && isspace(*p) != 0) p++; |
| 490 | return p; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | static char* SkipBlackSpace(char* p) { |
| 495 | while (*p != '\0' && isspace(*p) == 0) p++; |
| 496 | return p; |
| 497 | } |
| 498 | |
| 499 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 500 | // static |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 501 | int FlagList::SetFlagsFromString(const char* str, int len) { |
| 502 | // make a 0-terminated copy of str |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 503 | ScopedVector<char> copy0(len + 1); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 504 | OS::MemCopy(copy0.start(), str, len); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 505 | copy0[len] = '\0'; |
| 506 | |
| 507 | // strip leading white space |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 508 | char* copy = SkipWhiteSpace(copy0.start()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 509 | |
| 510 | // count the number of 'arguments' |
| 511 | int argc = 1; // be compatible with SetFlagsFromCommandLine() |
| 512 | for (char* p = copy; *p != '\0'; argc++) { |
| 513 | p = SkipBlackSpace(p); |
| 514 | p = SkipWhiteSpace(p); |
| 515 | } |
| 516 | |
| 517 | // allocate argument array |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 518 | ScopedVector<char*> argv(argc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 519 | |
| 520 | // split the flags string into arguments |
| 521 | argc = 1; // be compatible with SetFlagsFromCommandLine() |
| 522 | for (char* p = copy; *p != '\0'; argc++) { |
| 523 | argv[argc] = p; |
| 524 | p = SkipBlackSpace(p); |
| 525 | if (*p != '\0') *p++ = '\0'; // 0-terminate argument |
| 526 | p = SkipWhiteSpace(p); |
| 527 | } |
| 528 | |
| 529 | // set the flags |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 530 | int result = SetFlagsFromCommandLine(&argc, argv.start(), false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 531 | |
| 532 | return result; |
| 533 | } |
| 534 | |
| 535 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 536 | // static |
| 537 | void FlagList::ResetAllFlags() { |
| 538 | for (size_t i = 0; i < num_flags; ++i) { |
| 539 | flags[i].Reset(); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | |
| 544 | // static |
| 545 | void FlagList::PrintHelp() { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 546 | #if V8_TARGET_ARCH_ARM |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 547 | CpuFeatures::PrintTarget(); |
| 548 | CpuFeatures::Probe(); |
| 549 | CpuFeatures::PrintFeatures(); |
| 550 | #endif // V8_TARGET_ARCH_ARM |
| 551 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 552 | printf("Usage:\n"); |
| 553 | printf(" shell [options] -e string\n"); |
| 554 | printf(" execute string in V8\n"); |
| 555 | printf(" shell [options] file1 file2 ... filek\n"); |
| 556 | printf(" run JavaScript scripts in file1, file2, ..., filek\n"); |
| 557 | printf(" shell [options]\n"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 558 | printf(" shell [options] --shell [file1 file2 ... filek]\n"); |
| 559 | printf(" run an interactive JavaScript shell\n"); |
| 560 | printf(" d8 [options] file1 file2 ... filek\n"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 561 | printf(" d8 [options]\n"); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 562 | printf(" d8 [options] --shell [file1 file2 ... filek]\n"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 563 | printf(" run the new debugging shell\n\n"); |
| 564 | printf("Options:\n"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 565 | for (size_t i = 0; i < num_flags; ++i) { |
| 566 | Flag* f = &flags[i]; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 567 | SmartArrayPointer<const char> value = ToString(f); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 568 | printf(" --%s (%s)\n type: %s default: %s\n", |
| 569 | f->name(), f->comment(), Type2String(f->type()), *value); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 570 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | } |
| 572 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 573 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 574 | // static |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 575 | void FlagList::EnforceFlagImplications() { |
| 576 | #define FLAG_MODE_DEFINE_IMPLICATIONS |
| 577 | #include "flag-definitions.h" |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 578 | #undef FLAG_MODE_DEFINE_IMPLICATIONS |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 579 | } |
| 580 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 581 | } } // namespace v8::internal |