| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/flags.h" | 5 #include "vm/flags.h" |
| 6 | 6 |
| 7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
| 8 #include "vm/json_stream.h" | 8 #include "vm/json_stream.h" |
| 9 #include "vm/os.h" | 9 #include "vm/os.h" |
| 10 | 10 |
| 11 namespace dart { | 11 namespace dart { |
| 12 | 12 |
| 13 DEFINE_FLAG(bool, print_flags, false, "Print flags as they are being parsed."); | 13 DEFINE_FLAG(bool, print_flags, false, "Print flags as they are being parsed."); |
| 14 DEFINE_FLAG(bool, ignore_unrecognized_flags, false, | 14 DEFINE_FLAG(bool, |
| 15 "Ignore unrecognized flags."); | 15 ignore_unrecognized_flags, |
| 16 false, |
| 17 "Ignore unrecognized flags."); |
| 16 | 18 |
| 17 #define PRODUCT_FLAG_MARCO(name, type, default_value, comment) \ | 19 #define PRODUCT_FLAG_MARCO(name, type, default_value, comment) \ |
| 18 type FLAG_##name = Flags::Register_##type(&FLAG_##name, \ | 20 type FLAG_##name = \ |
| 19 #name, \ | 21 Flags::Register_##type(&FLAG_##name, #name, default_value, comment); |
| 20 default_value, \ | |
| 21 comment); | |
| 22 | 22 |
| 23 #if defined(DEBUG) | 23 #if defined(DEBUG) |
| 24 #define DEBUG_FLAG_MARCO(name, type, default_value, comment) \ | 24 #define DEBUG_FLAG_MARCO(name, type, default_value, comment) \ |
| 25 type FLAG_##name = Flags::Register_##type(&FLAG_##name, \ | 25 type FLAG_##name = \ |
| 26 #name, \ | 26 Flags::Register_##type(&FLAG_##name, #name, default_value, comment); |
| 27 default_value, \ | |
| 28 comment); | |
| 29 #else // defined(DEBUG) | 27 #else // defined(DEBUG) |
| 30 #define DEBUG_FLAG_MARCO(name, type, default_value, comment) | 28 #define DEBUG_FLAG_MARCO(name, type, default_value, comment) |
| 31 #endif // defined(DEBUG) | 29 #endif // defined(DEBUG) |
| 32 | 30 |
| 33 #if defined(PRODUCT) && defined(DART_PRECOMPILED_RUNTIME) | 31 #if defined(PRODUCT) && defined(DART_PRECOMPILED_RUNTIME) |
| 34 // Nothing to be done for the product flag definitions. | 32 // Nothing to be done for the product flag definitions. |
| 35 #define RELEASE_FLAG_MARCO(name, product_value, type, default_value, comment) | 33 #define RELEASE_FLAG_MARCO(name, product_value, type, default_value, comment) |
| 36 // Nothing to be done for the precompilation flag definitions. | 34 // Nothing to be done for the precompilation flag definitions. |
| 37 #define PRECOMPILE_FLAG_MARCO(name, pre_value, product_value, type, \ | 35 #define PRECOMPILE_FLAG_MARCO(name, pre_value, product_value, type, \ |
| 38 default_value, comment) | 36 default_value, comment) |
| 39 | 37 |
| 40 #elif defined(PRODUCT) // !PRECOMPILED | 38 #elif defined(PRODUCT) // !PRECOMPILED |
| 41 // Nothing to be done for the product flag definitions. | 39 // Nothing to be done for the product flag definitions. |
| 42 #define RELEASE_FLAG_MARCO(name, product_value, type, default_value, comment) | 40 #define RELEASE_FLAG_MARCO(name, product_value, type, default_value, comment) |
| 43 // Nothing to be done for the precompilation flag definitions. | 41 // Nothing to be done for the precompilation flag definitions. |
| 44 #define PRECOMPILE_FLAG_MARCO(name, pre_value, product_value, type, \ | 42 #define PRECOMPILE_FLAG_MARCO(name, pre_value, product_value, type, \ |
| 45 default_value, comment) | 43 default_value, comment) |
| 46 | 44 |
| 47 #elif defined(DART_PRECOMPILED_RUNTIME) // !PRODUCT | 45 #elif defined(DART_PRECOMPILED_RUNTIME) // !PRODUCT |
| 48 #define RELEASE_FLAG_MARCO(name, product_value, type, default_value, comment) \ | 46 #define RELEASE_FLAG_MARCO(name, product_value, type, default_value, comment) \ |
| 49 type FLAG_##name = Flags::Register_##type(&FLAG_##name, \ | 47 type FLAG_##name = \ |
| 50 #name, \ | 48 Flags::Register_##type(&FLAG_##name, #name, default_value, comment); |
| 51 default_value, \ | |
| 52 comment); | |
| 53 // Nothing to be done for the precompilation flag definitions. | 49 // Nothing to be done for the precompilation flag definitions. |
| 54 #define PRECOMPILE_FLAG_MARCO(name, pre_value, product_value, type, \ | 50 #define PRECOMPILE_FLAG_MARCO(name, pre_value, product_value, type, \ |
| 55 default_value, comment) | 51 default_value, comment) |
| 56 | 52 |
| 57 #else // !PRODUCT && !PRECOMPILED | 53 #else // !PRODUCT && !PRECOMPILED |
| 58 #define RELEASE_FLAG_MARCO(name, product_value, type, default_value, comment) \ | 54 #define RELEASE_FLAG_MARCO(name, product_value, type, default_value, comment) \ |
| 59 type FLAG_##name = Flags::Register_##type(&FLAG_##name, \ | 55 type FLAG_##name = \ |
| 60 #name, \ | 56 Flags::Register_##type(&FLAG_##name, #name, default_value, comment); |
| 61 default_value, \ | |
| 62 comment); | |
| 63 #define PRECOMPILE_FLAG_MARCO(name, pre_value, product_value, type, \ | 57 #define PRECOMPILE_FLAG_MARCO(name, pre_value, product_value, type, \ |
| 64 default_value, comment) \ | 58 default_value, comment) \ |
| 65 type FLAG_##name = Flags::Register_##type(&FLAG_##name, \ | 59 type FLAG_##name = \ |
| 66 #name, \ | 60 Flags::Register_##type(&FLAG_##name, #name, default_value, comment); |
| 67 default_value, \ | |
| 68 comment); | |
| 69 #endif | 61 #endif |
| 70 | 62 |
| 71 | 63 |
| 72 // Define all of the non-product flags here. | 64 // Define all of the non-product flags here. |
| 73 FLAG_LIST(PRODUCT_FLAG_MARCO, | 65 FLAG_LIST(PRODUCT_FLAG_MARCO, |
| 74 RELEASE_FLAG_MARCO, | 66 RELEASE_FLAG_MARCO, |
| 75 DEBUG_FLAG_MARCO, | 67 DEBUG_FLAG_MARCO, |
| 76 PRECOMPILE_FLAG_MARCO) | 68 PRECOMPILE_FLAG_MARCO) |
| 77 | 69 |
| 78 #undef RELEASE_FLAG_MARCO | 70 #undef RELEASE_FLAG_MARCO |
| 79 #undef DEBUG_FLAG_MARCO | 71 #undef DEBUG_FLAG_MARCO |
| 80 #undef PRODUCT_FLAG_MARCO | 72 #undef PRODUCT_FLAG_MARCO |
| 81 #undef PRECOMPILE_FLAG_MARCO | 73 #undef PRECOMPILE_FLAG_MARCO |
| 82 | 74 |
| 83 | 75 |
| 84 bool Flags::initialized_ = false; | 76 bool Flags::initialized_ = false; |
| 85 | 77 |
| 86 // List of registered flags. | 78 // List of registered flags. |
| 87 Flag** Flags::flags_ = NULL; | 79 Flag** Flags::flags_ = NULL; |
| 88 intptr_t Flags::capacity_ = 0; | 80 intptr_t Flags::capacity_ = 0; |
| 89 intptr_t Flags::num_flags_ = 0; | 81 intptr_t Flags::num_flags_ = 0; |
| 90 | 82 |
| 91 class Flag { | 83 class Flag { |
| 92 public: | 84 public: |
| 93 enum FlagType { | 85 enum FlagType { kBoolean, kInteger, kUint64, kString, kFunc, kNumFlagTypes }; |
| 94 kBoolean, | |
| 95 kInteger, | |
| 96 kUint64, | |
| 97 kString, | |
| 98 kFunc, | |
| 99 kNumFlagTypes | |
| 100 }; | |
| 101 | 86 |
| 102 Flag(const char* name, const char* comment, void* addr, FlagType type) | 87 Flag(const char* name, const char* comment, void* addr, FlagType type) |
| 103 : name_(name), comment_(comment), addr_(addr), type_(type) { | 88 : name_(name), comment_(comment), addr_(addr), type_(type) {} |
| 104 } | |
| 105 Flag(const char* name, const char* comment, FlagHandler handler) | 89 Flag(const char* name, const char* comment, FlagHandler handler) |
| 106 : name_(name), comment_(comment), handler_(handler), type_(kFunc) { | 90 : name_(name), comment_(comment), handler_(handler), type_(kFunc) {} |
| 107 } | |
| 108 | 91 |
| 109 void Print() { | 92 void Print() { |
| 110 if (IsUnrecognized()) { | 93 if (IsUnrecognized()) { |
| 111 OS::Print("%s: unrecognized\n", name_); | 94 OS::Print("%s: unrecognized\n", name_); |
| 112 return; | 95 return; |
| 113 } | 96 } |
| 114 switch (type_) { | 97 switch (type_) { |
| 115 case kBoolean: { | 98 case kBoolean: { |
| 116 OS::Print("%s: %s (%s)\n", | 99 OS::Print("%s: %s (%s)\n", name_, *this->bool_ptr_ ? "true" : "false", |
| 117 name_, *this->bool_ptr_ ? "true" : "false", comment_); | 100 comment_); |
| 118 break; | 101 break; |
| 119 } | 102 } |
| 120 case kInteger: { | 103 case kInteger: { |
| 121 OS::Print("%s: %d (%s)\n", name_, *this->int_ptr_, comment_); | 104 OS::Print("%s: %d (%s)\n", name_, *this->int_ptr_, comment_); |
| 122 break; | 105 break; |
| 123 } | 106 } |
| 124 case kUint64: { | 107 case kUint64: { |
| 125 OS::Print("%s: %" Pu64 " (%s)\n", name_, *this->uint64_ptr_, comment_); | 108 OS::Print("%s: %" Pu64 " (%s)\n", name_, *this->uint64_ptr_, comment_); |
| 126 break; | 109 break; |
| 127 } | 110 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 if (strcmp(flag->name_, name) == 0) { | 151 if (strcmp(flag->name_, name) == 0) { |
| 169 return flag; | 152 return flag; |
| 170 } | 153 } |
| 171 } | 154 } |
| 172 return NULL; | 155 return NULL; |
| 173 } | 156 } |
| 174 | 157 |
| 175 | 158 |
| 176 bool Flags::IsSet(const char* name) { | 159 bool Flags::IsSet(const char* name) { |
| 177 Flag* flag = Lookup(name); | 160 Flag* flag = Lookup(name); |
| 178 return (flag != NULL) && | 161 return (flag != NULL) && (flag->type_ == Flag::kBoolean) && |
| 179 (flag->type_ == Flag::kBoolean) && | 162 (flag->bool_ptr_ != NULL) && (*flag->bool_ptr_ == true); |
| 180 (flag->bool_ptr_ != NULL) && | |
| 181 (*flag->bool_ptr_ == true); | |
| 182 } | 163 } |
| 183 | 164 |
| 184 | 165 |
| 185 void Flags::AddFlag(Flag* flag) { | 166 void Flags::AddFlag(Flag* flag) { |
| 186 ASSERT(!initialized_); | 167 ASSERT(!initialized_); |
| 187 if (num_flags_ == capacity_) { | 168 if (num_flags_ == capacity_) { |
| 188 if (flags_ == NULL) { | 169 if (flags_ == NULL) { |
| 189 capacity_ = 256; | 170 capacity_ = 256; |
| 190 flags_ = new Flag*[capacity_]; | 171 flags_ = new Flag*[capacity_]; |
| 191 } else { | 172 } else { |
| 192 intptr_t new_capacity = capacity_ * 2; | 173 intptr_t new_capacity = capacity_ * 2; |
| 193 Flag** new_flags = new Flag*[new_capacity]; | 174 Flag** new_flags = new Flag*[new_capacity]; |
| 194 for (intptr_t i = 0; i < num_flags_; i++) { | 175 for (intptr_t i = 0; i < num_flags_; i++) { |
| 195 new_flags[i] = flags_[i]; | 176 new_flags[i] = flags_[i]; |
| 196 } | 177 } |
| 197 delete [] flags_; | 178 delete[] flags_; |
| 198 flags_ = new_flags; | 179 flags_ = new_flags; |
| 199 capacity_ = new_capacity; | 180 capacity_ = new_capacity; |
| 200 } | 181 } |
| 201 } | 182 } |
| 202 flags_[num_flags_++] = flag; | 183 flags_[num_flags_++] = flag; |
| 203 } | 184 } |
| 204 | 185 |
| 205 | 186 |
| 206 bool Flags::Register_bool(bool* addr, | 187 bool Flags::Register_bool(bool* addr, |
| 207 const char* name, | 188 const char* name, |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 static bool IsValidFlag(const char* name, | 386 static bool IsValidFlag(const char* name, |
| 406 const char* prefix, | 387 const char* prefix, |
| 407 intptr_t prefix_length) { | 388 intptr_t prefix_length) { |
| 408 intptr_t name_length = strlen(name); | 389 intptr_t name_length = strlen(name); |
| 409 return ((name_length > prefix_length) && | 390 return ((name_length > prefix_length) && |
| 410 (strncmp(name, prefix, prefix_length) == 0)); | 391 (strncmp(name, prefix, prefix_length) == 0)); |
| 411 } | 392 } |
| 412 | 393 |
| 413 | 394 |
| 414 int Flags::CompareFlagNames(const void* left, const void* right) { | 395 int Flags::CompareFlagNames(const void* left, const void* right) { |
| 415 const Flag* left_flag = *reinterpret_cast<const Flag* const *>(left); | 396 const Flag* left_flag = *reinterpret_cast<const Flag* const*>(left); |
| 416 const Flag* right_flag = *reinterpret_cast<const Flag* const *>(right); | 397 const Flag* right_flag = *reinterpret_cast<const Flag* const*>(right); |
| 417 return strcmp(left_flag->name_, right_flag->name_); | 398 return strcmp(left_flag->name_, right_flag->name_); |
| 418 } | 399 } |
| 419 | 400 |
| 420 | 401 |
| 421 bool Flags::ProcessCommandLineFlags(int number_of_vm_flags, | 402 bool Flags::ProcessCommandLineFlags(int number_of_vm_flags, |
| 422 const char** vm_flags) { | 403 const char** vm_flags) { |
| 423 if (initialized_) { | 404 if (initialized_) { |
| 424 return false; | 405 return false; |
| 425 } | 406 } |
| 426 | 407 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 456 } | 437 } |
| 457 } | 438 } |
| 458 if (FLAG_print_flags) { | 439 if (FLAG_print_flags) { |
| 459 PrintFlags(); | 440 PrintFlags(); |
| 460 } | 441 } |
| 461 | 442 |
| 462 initialized_ = true; | 443 initialized_ = true; |
| 463 return true; | 444 return true; |
| 464 } | 445 } |
| 465 | 446 |
| 466 bool Flags::SetFlag(const char* name, | 447 bool Flags::SetFlag(const char* name, const char* value, const char** error) { |
| 467 const char* value, | |
| 468 const char** error) { | |
| 469 Flag* flag = Lookup(name); | 448 Flag* flag = Lookup(name); |
| 470 if (flag == NULL) { | 449 if (flag == NULL) { |
| 471 *error = "Cannot set flag: flag not found"; | 450 *error = "Cannot set flag: flag not found"; |
| 472 return false; | 451 return false; |
| 473 } | 452 } |
| 474 if (!SetFlagFromString(flag, value)) { | 453 if (!SetFlagFromString(flag, value)) { |
| 475 *error = "Cannot set flag: invalid value"; | 454 *error = "Cannot set flag: invalid value"; |
| 476 return false; | 455 return false; |
| 477 } | 456 } |
| 478 return true; | 457 return true; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 JSONObject jsobj(js); | 518 JSONObject jsobj(js); |
| 540 jsobj.AddProperty("type", "FlagList"); | 519 jsobj.AddProperty("type", "FlagList"); |
| 541 JSONArray jsarr(&jsobj, "flags"); | 520 JSONArray jsarr(&jsobj, "flags"); |
| 542 for (intptr_t i = 0; i < num_flags_; ++i) { | 521 for (intptr_t i = 0; i < num_flags_; ++i) { |
| 543 PrintFlagToJSONArray(&jsarr, flags_[i]); | 522 PrintFlagToJSONArray(&jsarr, flags_[i]); |
| 544 } | 523 } |
| 545 } | 524 } |
| 546 #endif // !PRODUCT | 525 #endif // !PRODUCT |
| 547 | 526 |
| 548 } // namespace dart | 527 } // namespace dart |
| OLD | NEW |