1 /*
   2  * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "compiler/compilerDirectives.hpp"
  28 #include "compiler/compilerOracle.hpp"
  29 #include "compiler/methodMatcher.hpp"
  30 #include "jvm.h"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/oopFactory.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "oops/klass.hpp"
  35 #include "oops/method.inline.hpp"
  36 #include "oops/symbol.hpp"
  37 #include "opto/phasetype.hpp"
  38 #include "opto/traceAutoVectorizationTag.hpp"
  39 #include "runtime/globals_extension.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 #include "runtime/jniHandles.hpp"
  42 #include "runtime/os.hpp"
  43 #include "utilities/istream.hpp"
  44 #include "utilities/parseInteger.hpp"
  45 
  46 // Default compile commands, if defined, are parsed before any of the
  47 // explicitly defined compile commands. Thus, explicitly defined compile
  48 // commands take precedence over default compile commands. The effect is
  49 // as if the default compile commands had been specified at the start of
  50 // the command line.
  51 static const char* const default_compile_commands[] = {
  52 #ifdef ASSERT
  53     // In debug builds, impose a (generous) per-compilation memory limit
  54     // to catch pathological compilations during testing. The suboption
  55     // "crash" will cause the JVM to assert.
  56     //
  57     // Note: to disable the default limit at the command line,
  58     // set a limit of 0 (e.g. -XX:CompileCommand=MemLimit,*.*,0).
  59     "MemLimit,*.*,1G~crash",
  60 #endif
  61     nullptr };
  62 
  63 static const char* optiontype_names[] = {
  64 #define enum_of_types(type, name) name,
  65         OPTION_TYPES(enum_of_types)
  66 #undef enum_of_types
  67 };
  68 
  69 static const char* optiontype2name(enum OptionType type) {
  70   return optiontype_names[static_cast<int>(type)];
  71 }
  72 
  73 static enum OptionType option_types[] = {
  74 #define enum_of_options(option, name, ctype) OptionType::ctype,
  75         COMPILECOMMAND_OPTIONS(enum_of_options)
  76 #undef enum_of_options
  77 };
  78 
  79 static enum OptionType option2type(CompileCommandEnum option) {
  80   return option_types[static_cast<int>(option)];
  81 }
  82 
  83 static const char* option_names[] = {
  84 #define enum_of_options(option, name, ctype) name,
  85         COMPILECOMMAND_OPTIONS(enum_of_options)
  86 #undef enum_of_options
  87 };
  88 
  89 static const char* option2name(CompileCommandEnum option) {
  90   return option_names[static_cast<int>(option)];
  91 }
  92 
  93 /* Methods to map real type names to OptionType */
  94 template<typename T>
  95 static OptionType get_type_for() {
  96   return OptionType::Unknown;
  97 };
  98 
  99 template<> OptionType get_type_for<intx>() {
 100   return OptionType::Intx;
 101 }
 102 
 103 template<> OptionType get_type_for<uintx>() {
 104   return OptionType::Uintx;
 105 }
 106 
 107 template<> OptionType get_type_for<bool>() {
 108   return OptionType::Bool;
 109 }
 110 
 111 template<> OptionType get_type_for<ccstr>() {
 112   return OptionType::Ccstr;
 113 }
 114 
 115 template<> OptionType get_type_for<double>() {
 116   return OptionType::Double;
 117 }
 118 
 119 class MethodMatcher;
 120 class TypedMethodOptionMatcher;
 121 
 122 static TypedMethodOptionMatcher* option_list = nullptr;
 123 static bool any_set = false;
 124 static bool print_final_memstat_report = false;
 125 
 126 // A filter for quick lookup if an option is set
 127 static bool option_filter[static_cast<int>(CompileCommandEnum::Unknown) + 1] = { 0 };
 128 
 129 static void command_set_in_filter(CompileCommandEnum option) {
 130   assert(option != CompileCommandEnum::Unknown, "sanity");
 131   assert(option2type(option) != OptionType::Unknown, "sanity");
 132 
 133   if ((option != CompileCommandEnum::DontInline) &&
 134       (option != CompileCommandEnum::Inline) &&
 135       (option != CompileCommandEnum::Log)) {
 136     any_set = true;
 137   }
 138   option_filter[static_cast<int>(option)] = true;
 139 }
 140 
 141 static bool has_command(CompileCommandEnum option) {
 142   return option_filter[static_cast<int>(option)];
 143 }
 144 
 145 class TypedMethodOptionMatcher : public MethodMatcher {
 146  private:
 147   TypedMethodOptionMatcher* _next;
 148   CompileCommandEnum _option;
 149  public:
 150 
 151   union {
 152     bool bool_value;
 153     intx intx_value;
 154     uintx uintx_value;
 155     double double_value;
 156     ccstr ccstr_value;
 157   } _u;
 158 
 159   TypedMethodOptionMatcher() : MethodMatcher(),
 160     _next(nullptr),
 161     _option(CompileCommandEnum::Unknown) {
 162       memset(&_u, 0, sizeof(_u));
 163   }
 164 
 165   ~TypedMethodOptionMatcher();
 166   static TypedMethodOptionMatcher* parse_method_pattern(char*& line, char* errorbuf, const int buf_size);
 167   TypedMethodOptionMatcher* match(const methodHandle &method, CompileCommandEnum option);
 168 
 169   void init(CompileCommandEnum option, TypedMethodOptionMatcher* next) {
 170     _next = next;
 171     _option = option;
 172   }
 173 
 174   void init_matcher(Symbol* class_name, Mode class_mode,
 175                     Symbol* method_name, Mode method_mode,
 176                     Symbol* signature) {
 177     MethodMatcher::init(class_name, class_mode, method_name, method_mode, signature);
 178   }
 179 
 180   void set_next(TypedMethodOptionMatcher* next) {_next = next; }
 181   TypedMethodOptionMatcher* next() { return _next; }
 182   CompileCommandEnum option() { return _option; }
 183   template<typename T> T value();
 184   template<typename T> void set_value(T value);
 185   void print();
 186   void print_all();
 187   TypedMethodOptionMatcher* clone();
 188 };
 189 
 190 // A few templated accessors instead of a full template class.
 191 template<> intx TypedMethodOptionMatcher::value<intx>() {
 192   return _u.intx_value;
 193 }
 194 
 195 template<> uintx TypedMethodOptionMatcher::value<uintx>() {
 196   return _u.uintx_value;
 197 }
 198 
 199 template<> bool TypedMethodOptionMatcher::value<bool>() {
 200   return _u.bool_value;
 201 }
 202 
 203 template<> double TypedMethodOptionMatcher::value<double>() {
 204   return _u.double_value;
 205 }
 206 
 207 template<> ccstr TypedMethodOptionMatcher::value<ccstr>() {
 208   return _u.ccstr_value;
 209 }
 210 
 211 template<> void TypedMethodOptionMatcher::set_value(intx value) {
 212   _u.intx_value = value;
 213 }
 214 
 215 template<> void TypedMethodOptionMatcher::set_value(uintx value) {
 216   _u.uintx_value = value;
 217 }
 218 
 219 template<> void TypedMethodOptionMatcher::set_value(double value) {
 220   _u.double_value = value;
 221 }
 222 
 223 template<> void TypedMethodOptionMatcher::set_value(bool value) {
 224   _u.bool_value = value;
 225 }
 226 
 227 template<> void TypedMethodOptionMatcher::set_value(ccstr value) {
 228   _u.ccstr_value = (ccstr)os::strdup_check_oom(value);
 229 }
 230 
 231 void TypedMethodOptionMatcher::print() {
 232   ttyLocker ttyl;
 233   print_base(tty);
 234   const char* name = option2name(_option);
 235   enum OptionType type = option2type(_option);
 236   switch (type) {
 237     case OptionType::Intx:
 238     tty->print_cr(" intx %s = " INTX_FORMAT, name, value<intx>());
 239     break;
 240     case OptionType::Uintx:
 241     tty->print_cr(" uintx %s = " UINTX_FORMAT, name, value<uintx>());
 242     break;
 243     case OptionType::Bool:
 244     tty->print_cr(" bool %s = %s", name, value<bool>() ? "true" : "false");
 245     break;
 246     case OptionType::Double:
 247     tty->print_cr(" double %s = %f", name, value<double>());
 248     break;
 249     case OptionType::Ccstr:
 250     case OptionType::Ccstrlist:
 251     tty->print_cr(" const char* %s = '%s'", name, value<ccstr>());
 252     break;
 253   default:
 254     ShouldNotReachHere();
 255   }
 256 }
 257 
 258 void TypedMethodOptionMatcher::print_all() {
 259    print();
 260    if (_next != nullptr) {
 261      tty->print(" ");
 262      _next->print_all();
 263    }
 264  }
 265 
 266 TypedMethodOptionMatcher* TypedMethodOptionMatcher::clone() {
 267   TypedMethodOptionMatcher* m = new TypedMethodOptionMatcher();
 268   m->_class_mode = _class_mode;
 269   m->_class_name = _class_name;
 270   m->_method_mode = _method_mode;
 271   m->_method_name = _method_name;
 272   m->_signature = _signature;
 273   // Need to ref count the symbols
 274   if (_class_name != nullptr) {
 275     _class_name->increment_refcount();
 276   }
 277   if (_method_name != nullptr) {
 278     _method_name->increment_refcount();
 279   }
 280   if (_signature != nullptr) {
 281     _signature->increment_refcount();
 282   }
 283   return m;
 284 }
 285 
 286 TypedMethodOptionMatcher::~TypedMethodOptionMatcher() {
 287   enum OptionType type = option2type(_option);
 288   if (type == OptionType::Ccstr || type == OptionType::Ccstrlist) {
 289     ccstr v = value<ccstr>();
 290     os::free((void*)v);
 291   }
 292 }
 293 
 294 TypedMethodOptionMatcher* TypedMethodOptionMatcher::parse_method_pattern(char*& line, char* errorbuf, const int buf_size) {
 295   assert(*errorbuf == '\0', "Dont call here with error_msg already set");
 296   const char* error_msg = nullptr;
 297   TypedMethodOptionMatcher* tom = new TypedMethodOptionMatcher();
 298   MethodMatcher::parse_method_pattern(line, error_msg, tom);
 299   if (error_msg != nullptr) {
 300     jio_snprintf(errorbuf, buf_size, error_msg);
 301     delete tom;
 302     return nullptr;
 303   }
 304   return tom;
 305 }
 306 
 307 TypedMethodOptionMatcher* TypedMethodOptionMatcher::match(const methodHandle& method, CompileCommandEnum option) {
 308   TypedMethodOptionMatcher* current = this;
 309   while (current != nullptr) {
 310     if (current->_option == option) {
 311       if (current->matches(method)) {
 312         return current;
 313       }
 314     }
 315     current = current->next();
 316   }
 317   return nullptr;
 318 }
 319 
 320 template<typename T>
 321 static void register_command(TypedMethodOptionMatcher* matcher,
 322                              CompileCommandEnum option,
 323                              T value) {
 324   assert(matcher != option_list, "No circular lists please");
 325   if (option == CompileCommandEnum::Log && !LogCompilation) {
 326     tty->print_cr("Warning:  +LogCompilation must be enabled in order for individual methods to be logged with ");
 327     tty->print_cr("          CompileCommand=log,<method pattern>");
 328   }
 329   assert(CompilerOracle::option_matches_type(option, value), "Value must match option type");
 330 
 331   if (option == CompileCommandEnum::Blackhole && !UnlockExperimentalVMOptions) {
 332     warning("Blackhole compile option is experimental and must be enabled via -XX:+UnlockExperimentalVMOptions");
 333     // Delete matcher as we don't keep it
 334     delete matcher;
 335     return;
 336   }
 337 
 338   matcher->init(option, option_list);
 339   matcher->set_value<T>(value);
 340   option_list = matcher;
 341   command_set_in_filter(option);
 342 
 343   if (!CompilerOracle::be_quiet()) {
 344     // Print out the successful registration of a compile command
 345     ttyLocker ttyl;
 346     tty->print("CompileCommand: %s ", option2name(option));
 347     matcher->print();
 348   }
 349 
 350   return;
 351 }
 352 
 353 template<typename T>
 354 bool CompilerOracle::has_option_value(const methodHandle& method, CompileCommandEnum option, T& value) {
 355   assert(option_matches_type(option, value), "Value must match option type");
 356   if (!has_command(option)) {
 357     return false;
 358   }
 359   if (option_list != nullptr) {
 360     TypedMethodOptionMatcher* m = option_list->match(method, option);
 361     if (m != nullptr) {
 362       value = m->value<T>();
 363       return true;
 364     }
 365   }
 366   return false;
 367 }
 368 
 369 static bool resolve_inlining_predicate(CompileCommandEnum option, const methodHandle& method) {
 370   assert(option == CompileCommandEnum::Inline || option == CompileCommandEnum::DontInline, "Sanity");
 371   bool v1 = false;
 372   bool v2 = false;
 373   bool has_inline = CompilerOracle::has_option_value(method, CompileCommandEnum::Inline, v1);
 374   bool has_dnotinline = CompilerOracle::has_option_value(method, CompileCommandEnum::DontInline, v2);
 375   if (has_inline && has_dnotinline) {
 376     if (v1 && v2) {
 377       // Conflict options detected
 378       // Find the last one for that method and return the predicate accordingly
 379       // option_list lists options in reverse order. So the first option we find is the last which was specified.
 380       CompileCommandEnum last_one = CompileCommandEnum::Unknown;
 381       TypedMethodOptionMatcher* current = option_list;
 382       while (current != nullptr) {
 383         last_one = current->option();
 384         if (last_one == CompileCommandEnum::Inline || last_one == CompileCommandEnum::DontInline) {
 385           if (current->matches(method)) {
 386             return last_one == option;
 387           }
 388         }
 389         current = current->next();
 390       }
 391       ShouldNotReachHere();
 392       return false;
 393     } else {
 394       // No conflicts
 395       return option == CompileCommandEnum::Inline ? v1 : v2;
 396     }
 397   } else {
 398     if (option == CompileCommandEnum::Inline) {
 399       return has_inline ? v1 : false;
 400     } else {
 401       return has_dnotinline ? v2 : false;
 402     }
 403   }
 404 }
 405 
 406 static bool check_predicate(CompileCommandEnum option, const methodHandle& method) {
 407   // Special handling for Inline and DontInline since conflict options may be specified
 408   if (option == CompileCommandEnum::Inline || option == CompileCommandEnum::DontInline) {
 409     return resolve_inlining_predicate(option, method);
 410   }
 411 
 412   bool value = false;
 413   if (CompilerOracle::has_option_value(method, option, value)) {
 414     return value;
 415   }
 416   return false;
 417 }
 418 
 419 bool CompilerOracle::has_any_command_set() {
 420   return any_set;
 421 }
 422 
 423 // Explicit instantiation for all OptionTypes supported.
 424 template bool CompilerOracle::has_option_value<intx>(const methodHandle& method, CompileCommandEnum option, intx& value);
 425 template bool CompilerOracle::has_option_value<uintx>(const methodHandle& method, CompileCommandEnum option, uintx& value);
 426 template bool CompilerOracle::has_option_value<bool>(const methodHandle& method, CompileCommandEnum option, bool& value);
 427 template bool CompilerOracle::has_option_value<ccstr>(const methodHandle& method, CompileCommandEnum option, ccstr& value);
 428 template bool CompilerOracle::has_option_value<double>(const methodHandle& method, CompileCommandEnum option, double& value);
 429 
 430 template<typename T>
 431 bool CompilerOracle::option_matches_type(CompileCommandEnum option, T& value) {
 432   enum OptionType option_type = option2type(option);
 433   if (option_type == OptionType::Unknown) {
 434     return false; // Can't query options with type Unknown.
 435   }
 436   if (option_type == OptionType::Ccstrlist) {
 437     option_type = OptionType::Ccstr; // CCstrList type options are stored as Ccstr
 438   }
 439   return (get_type_for<T>() == option_type);
 440 }
 441 
 442 template bool CompilerOracle::option_matches_type<intx>(CompileCommandEnum option, intx& value);
 443 template bool CompilerOracle::option_matches_type<uintx>(CompileCommandEnum option, uintx& value);
 444 template bool CompilerOracle::option_matches_type<bool>(CompileCommandEnum option, bool& value);
 445 template bool CompilerOracle::option_matches_type<ccstr>(CompileCommandEnum option, ccstr& value);
 446 template bool CompilerOracle::option_matches_type<double>(CompileCommandEnum option, double& value);
 447 
 448 bool CompilerOracle::has_option(const methodHandle& method, CompileCommandEnum option) {
 449   bool value = false;
 450   has_option_value(method, option, value);
 451   return value;
 452 }
 453 
 454 bool CompilerOracle::should_exclude(const methodHandle& method) {
 455   if (check_predicate(CompileCommandEnum::Exclude, method)) {
 456     return true;
 457   }
 458   if (has_command(CompileCommandEnum::CompileOnly)) {
 459     return !check_predicate(CompileCommandEnum::CompileOnly, method);
 460   }
 461   return false;
 462 }
 463 
 464 bool CompilerOracle::should_inline(const methodHandle& method) {
 465   return (check_predicate(CompileCommandEnum::Inline, method));
 466 }
 467 
 468 bool CompilerOracle::should_not_inline(const methodHandle& method) {
 469   return check_predicate(CompileCommandEnum::DontInline, method) || check_predicate(CompileCommandEnum::Exclude, method);
 470 }
 471 
 472 bool CompilerOracle::should_print(const methodHandle& method) {
 473   return check_predicate(CompileCommandEnum::Print, method);
 474 }
 475 
 476 bool CompilerOracle::should_print_methods() {
 477   return has_command(CompileCommandEnum::Print);
 478 }
 479 
 480 // Tells whether there are any methods to collect memory statistics for
 481 bool CompilerOracle::should_collect_memstat() {
 482   return has_command(CompileCommandEnum::MemStat) || has_command(CompileCommandEnum::MemLimit);
 483 }
 484 
 485 bool CompilerOracle::should_print_final_memstat_report() {
 486   return print_final_memstat_report;
 487 }
 488 
 489 bool CompilerOracle::should_log(const methodHandle& method) {
 490   if (!LogCompilation) return false;
 491   if (!has_command(CompileCommandEnum::Log)) {
 492     return true;  // by default, log all
 493   }
 494   return (check_predicate(CompileCommandEnum::Log, method));
 495 }
 496 
 497 bool CompilerOracle::should_break_at(const methodHandle& method) {
 498   return check_predicate(CompileCommandEnum::Break, method);
 499 }
 500 
 501 void CompilerOracle::tag_blackhole_if_possible(const methodHandle& method) {
 502   if (!check_predicate(CompileCommandEnum::Blackhole, method)) {
 503     return;
 504   }
 505   guarantee(UnlockExperimentalVMOptions, "Checked during initial parsing");
 506   if (method->result_type() != T_VOID) {
 507     warning("Blackhole compile option only works for methods with void type: %s",
 508             method->name_and_sig_as_C_string());
 509     return;
 510   }
 511   if (!method->is_empty_method()) {
 512     warning("Blackhole compile option only works for empty methods: %s",
 513             method->name_and_sig_as_C_string());
 514     return;
 515   }
 516   if (!method->is_static()) {
 517     warning("Blackhole compile option only works for static methods: %s",
 518             method->name_and_sig_as_C_string());
 519     return;
 520   }
 521   if (method->intrinsic_id() == vmIntrinsics::_blackhole) {
 522     return;
 523   }
 524   if (method->intrinsic_id() != vmIntrinsics::_none) {
 525     warning("Blackhole compile option only works for methods that do not have intrinsic set: %s, %s",
 526             method->name_and_sig_as_C_string(), vmIntrinsics::name_at(method->intrinsic_id()));
 527     return;
 528   }
 529   method->set_intrinsic_id(vmIntrinsics::_blackhole);
 530 }
 531 
 532 static CompileCommandEnum match_option_name(const char* line, int* bytes_read, char* errorbuf, int bufsize) {
 533   assert(ARRAY_SIZE(option_names) == static_cast<int>(CompileCommandEnum::Count), "option_names size mismatch");
 534 
 535   *bytes_read = 0;
 536   char option_buf[256];
 537   int matches = sscanf(line, "%255[a-zA-Z0-9]%n", option_buf, bytes_read);
 538   if (matches > 0 && strcasecmp(option_buf, "unknown") != 0) {
 539     for (uint i = 0; i < ARRAY_SIZE(option_names); i++) {
 540       if (strcasecmp(option_buf, option_names[i]) == 0) {
 541         return static_cast<CompileCommandEnum>(i);
 542       }
 543     }
 544   }
 545   jio_snprintf(errorbuf, bufsize, "Unrecognized option '%s'", option_buf);
 546   return CompileCommandEnum::Unknown;
 547 }
 548 
 549 // match exactly and don't mess with errorbuf
 550 CompileCommandEnum CompilerOracle::parse_option_name(const char* line) {
 551   for (uint i = 0; i < ARRAY_SIZE(option_names); i++) {
 552     if (strcasecmp(line, option_names[i]) == 0) {
 553       return static_cast<CompileCommandEnum>(i);
 554     }
 555   }
 556   return CompileCommandEnum::Unknown;
 557 }
 558 
 559 enum OptionType CompilerOracle::parse_option_type(const char* type_str) {
 560   for (uint i = 0; i < ARRAY_SIZE(optiontype_names); i++) {
 561     if (strcasecmp(type_str, optiontype_names[i]) == 0) {
 562       return static_cast<enum OptionType>(i);
 563     }
 564   }
 565   return OptionType::Unknown;
 566 }
 567 
 568 static void print_tip() { // CMH Update info
 569   tty->cr();
 570   tty->print_cr("Usage: '-XX:CompileCommand=<option>,<method pattern>' - to set boolean option to true");
 571   tty->print_cr("Usage: '-XX:CompileCommand=<option>,<method pattern>,<value>'");
 572   tty->print_cr("Use:   '-XX:CompileCommand=help' for more information and to list all option.");
 573   tty->cr();
 574 }
 575 
 576 static void print_option(CompileCommandEnum option, const char* name, enum OptionType type) {
 577   if (type != OptionType::Unknown) {
 578     tty->print_cr("    %s (%s)", name, optiontype2name(type));
 579   }
 580 }
 581 
 582 static void print_commands() {
 583   tty->cr();
 584   tty->print_cr("All available options:");
 585 #define enum_of_options(option, name, ctype) print_option(CompileCommandEnum::option, name, OptionType::ctype);
 586   COMPILECOMMAND_OPTIONS(enum_of_options)
 587 #undef enum_of_options
 588   tty->cr();
 589 }
 590 
 591 static void usage() {
 592   tty->cr();
 593   tty->print_cr("The CompileCommand option enables the user of the JVM to control specific");
 594   tty->print_cr("behavior of the dynamic compilers.");
 595   tty->cr();
 596   tty->print_cr("Compile commands has this general form:");
 597   tty->print_cr("-XX:CompileCommand=<option><method pattern><value>");
 598   tty->print_cr("    Sets <option> to the specified value for methods matching <method pattern>");
 599   tty->print_cr("    All options are typed");
 600   tty->cr();
 601   tty->print_cr("-XX:CompileCommand=<option><method pattern>");
 602   tty->print_cr("    Sets <option> to true for methods matching <method pattern>");
 603   tty->print_cr("    Only applies to boolean options.");
 604   tty->cr();
 605   tty->print_cr("-XX:CompileCommand=quiet");
 606   tty->print_cr("    Silence the compile command output");
 607   tty->cr();
 608   tty->print_cr("-XX:CompileCommand=help");
 609   tty->print_cr("    Prints this help text");
 610   tty->cr();
 611   print_commands();
 612   tty->cr();
 613     tty->print_cr("Method patterns has the format:");
 614   tty->print_cr("  package/Class.method()");
 615   tty->cr();
 616   tty->print_cr("For backward compatibility this form is also allowed:");
 617   tty->print_cr("  package.Class::method()");
 618   tty->cr();
 619   tty->print_cr("The signature can be separated by an optional whitespace or comma:");
 620   tty->print_cr("  package/Class.method ()");
 621   tty->cr();
 622   tty->print_cr("The class and method identifier can be used together with leading or");
 623   tty->print_cr("trailing *'s for wildcard matching:");
 624   tty->print_cr("  *ackage/Clas*.*etho*()");
 625   tty->cr();
 626   tty->print_cr("It is possible to use more than one CompileCommand on the command line:");
 627   tty->print_cr("  -XX:CompileCommand=exclude,java/*.* -XX:CompileCommand=log,java*.*");
 628   tty->cr();
 629   tty->print_cr("The CompileCommands can be loaded from a file with the flag");
 630   tty->print_cr("-XX:CompileCommandFile=<file> or be added to the file '.hotspot_compiler'");
 631   tty->print_cr("Use the same format in the file as the argument to the CompileCommand flag.");
 632   tty->print_cr("Add one command on each line.");
 633   tty->print_cr("  exclude java/*.*");
 634   tty->print_cr("  option java/*.* ReplayInline");
 635   tty->cr();
 636   tty->print_cr("The following commands have conflicting behavior: 'exclude', 'inline', 'dontinline',");
 637   tty->print_cr("and 'compileonly'. There is no priority of commands. Applying (a subset of) these");
 638   tty->print_cr("commands to the same method results in undefined behavior.");
 639   tty->cr();
 640 };
 641 
 642 static int skip_whitespace(char* &line) {
 643   // Skip any leading spaces
 644   int whitespace_read = 0;
 645   sscanf(line, "%*[ \t]%n", &whitespace_read);
 646   line += whitespace_read;
 647   return whitespace_read;
 648 }
 649 
 650 static void skip_comma(char* &line) {
 651   // Skip any leading spaces
 652   if (*line == ',') {
 653     line++;
 654   }
 655 }
 656 
 657 static bool parseMemLimit(const char* line, intx& value, int& bytes_read, char* errorbuf, const int buf_size) {
 658   // Format:
 659   // "<memory size>['~' <suboption>]"
 660   // <memory size> can have units, e.g. M
 661   // <suboption> one of "crash" "stop", if omitted, "stop" is implied.
 662   //
 663   // Examples:
 664   // -XX:CompileCommand='memlimit,*.*,20m'
 665   // -XX:CompileCommand='memlimit,*.*,20m~stop'
 666   // -XX:CompileCommand='memlimit,Option::toString,1m~crash'
 667   //
 668   // The resulting intx carries the size and whether we are to stop or crash:
 669   // - neg. value means crash
 670   // - pos. value (default) means stop
 671   size_t s = 0;
 672   char* end;
 673   if (!parse_integer<size_t>(line, &end, &s)) {
 674     jio_snprintf(errorbuf, buf_size, "MemLimit: invalid value");
 675     return false;
 676   }
 677   bytes_read = (int)(end - line);
 678 
 679   intx v = (intx)s;
 680   if ((*end) != '\0') {
 681     if (strncasecmp(end, "~crash", 6) == 0) {
 682       v = -v;
 683       bytes_read += 6;
 684     } else if (strncasecmp(end, "~stop", 5) == 0) {
 685       // ok, this is the default
 686       bytes_read += 5;
 687     } else {
 688       jio_snprintf(errorbuf, buf_size, "MemLimit: invalid option");
 689       return false;
 690     }
 691   }
 692   value = v;
 693   return true;
 694 }
 695 
 696 static bool parseMemStat(const char* line, uintx& value, int& bytes_read, char* errorbuf, const int buf_size) {
 697 
 698 #define IF_ENUM_STRING(S, CMD)                \
 699   if (strncasecmp(line, S, strlen(S)) == 0) { \
 700     bytes_read += (int)strlen(S);             \
 701     CMD                                       \
 702     return true;                              \
 703   }
 704 
 705   IF_ENUM_STRING("collect", {
 706     value = (uintx)MemStatAction::collect;
 707   });
 708   IF_ENUM_STRING("print", {
 709     value = (uintx)MemStatAction::print;
 710     print_final_memstat_report = true;
 711   });
 712 #undef IF_ENUM_STRING
 713 
 714   jio_snprintf(errorbuf, buf_size, "MemStat: invalid option");
 715 
 716   return false;
 717 }
 718 
 719 static void scan_value(enum OptionType type, char* line, int& total_bytes_read,
 720         TypedMethodOptionMatcher* matcher, CompileCommandEnum option, char* errorbuf, const int buf_size) {
 721   int bytes_read = 0;
 722   const char* ccname = option2name(option);
 723   const char* type_str = optiontype2name(type);
 724   int skipped = skip_whitespace(line);
 725   total_bytes_read += skipped;
 726   if (type == OptionType::Intx) {
 727     intx value;
 728     bool success = false;
 729     if (option == CompileCommandEnum::MemLimit) {
 730       // Special parsing for MemLimit
 731       success = parseMemLimit(line, value, bytes_read, errorbuf, buf_size);
 732     } else {
 733       // Is it a raw number?
 734       success = sscanf(line, "" INTX_FORMAT "%n", &value, &bytes_read) == 1;
 735     }
 736     if (success) {
 737       total_bytes_read += bytes_read;
 738       line += bytes_read;
 739       register_command(matcher, option, value);
 740       return;
 741     } else {
 742       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 743     }
 744   } else if (type == OptionType::Uintx) {
 745     uintx value;
 746     bool success = false;
 747     if (option == CompileCommandEnum::MemStat) {
 748       // Special parsing for MemStat
 749       success = parseMemStat(line, value, bytes_read, errorbuf, buf_size);
 750     } else {
 751       // parse as raw number
 752       success = sscanf(line, "" UINTX_FORMAT "%n", &value, &bytes_read) == 1;
 753     }
 754     if (success) {
 755       total_bytes_read += bytes_read;
 756       line += bytes_read;
 757       register_command(matcher, option, value);
 758     } else {
 759       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 760     }
 761   } else if (type == OptionType::Ccstr) {
 762     ResourceMark rm;
 763     char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 764     if (sscanf(line, "%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) {
 765       total_bytes_read += bytes_read;
 766       line += bytes_read;
 767       register_command(matcher, option, (ccstr) value);
 768       return;
 769     } else {
 770       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 771     }
 772   } else if (type == OptionType::Ccstrlist) {
 773     // Accumulates several strings into one. The internal type is ccstr.
 774     ResourceMark rm;
 775     char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 776     char* next_value = value;
 777     if (sscanf(line, "%255[_a-zA-Z0-9+\\-]%n", next_value, &bytes_read) == 1) {
 778       total_bytes_read += bytes_read;
 779       line += bytes_read;
 780       next_value += bytes_read + 1;
 781       char* end_value = next_value - 1;
 782       while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9+\\-]%n", next_value, &bytes_read) == 1) {
 783         total_bytes_read += bytes_read;
 784         line += bytes_read;
 785         *end_value = ' '; // override '\0'
 786         next_value += bytes_read;
 787         end_value = next_value-1;
 788       }
 789 
 790       if (option == CompileCommandEnum::ControlIntrinsic || option == CompileCommandEnum::DisableIntrinsic) {
 791         ControlIntrinsicValidator validator(value, (option == CompileCommandEnum::DisableIntrinsic));
 792 
 793         if (!validator.is_valid()) {
 794           jio_snprintf(errorbuf, buf_size, "Unrecognized intrinsic detected in %s: %s", option2name(option), validator.what());
 795         }
 796       }
 797 #if !defined(PRODUCT) && defined(COMPILER2)
 798       else if (option == CompileCommandEnum::TraceAutoVectorization) {
 799         TraceAutoVectorizationTagValidator validator(value, true);
 800 
 801         if (!validator.is_valid()) {
 802           jio_snprintf(errorbuf, buf_size, "Unrecognized tag name in %s: %s", option2name(option), validator.what());
 803         }
 804       } else if (option == CompileCommandEnum::PrintIdealPhase) {
 805         PhaseNameValidator validator(value);
 806 
 807         if (!validator.is_valid()) {
 808           jio_snprintf(errorbuf, buf_size, "Unrecognized phase name in %s: %s", option2name(option), validator.what());
 809         }
 810       } else if (option == CompileCommandEnum::TestOptionList) {
 811         // all values are ok
 812       }
 813 #endif
 814       else {
 815         assert(false, "Ccstrlist type option missing validator");
 816       }
 817 
 818       register_command(matcher, option, (ccstr) value);
 819       return;
 820     } else {
 821       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 822     }
 823   } else if (type == OptionType::Bool) {
 824     char value[256];
 825     if (*line == '\0') {
 826       // Short version of a CompileCommand sets a boolean Option to true
 827       // -XXCompileCommand=<Option>,<method pattern>
 828       register_command(matcher, option, true);
 829       return;
 830     }
 831     if (sscanf(line, "%255[a-zA-Z]%n", value, &bytes_read) == 1) {
 832       if (strcasecmp(value, "true") == 0) {
 833         total_bytes_read += bytes_read;
 834         line += bytes_read;
 835         register_command(matcher, option, true);
 836         return;
 837       } else if (strcasecmp(value, "false") == 0) {
 838         total_bytes_read += bytes_read;
 839         line += bytes_read;
 840         register_command(matcher, option, false);
 841         return;
 842       } else {
 843         jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 844       }
 845     } else {
 846       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 847     }
 848   } else if (type == OptionType::Double) {
 849     char buffer[2][256];
 850     // Decimal separator '.' has been replaced with ' ' or '/' earlier,
 851     // so read integer and fraction part of double value separately.
 852     if (sscanf(line, "%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) {
 853       char value[512] = "";
 854       jio_snprintf(value, sizeof(value), "%s.%s", buffer[0], buffer[1]);
 855       total_bytes_read += bytes_read;
 856       line += bytes_read;
 857       register_command(matcher, option, atof(value));
 858       return;
 859     } else {
 860       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 861     }
 862   } else {
 863     jio_snprintf(errorbuf, buf_size, "Type '%s' not supported ", type_str);
 864   }
 865 }
 866 
 867 // Scan next option and value in line, return MethodMatcher object on success, nullptr on failure.
 868 // On failure, error_msg contains description for the first error.
 869 // For future extensions: set error_msg on first error.
 870 static void scan_option_and_value(enum OptionType type, char* line, int& total_bytes_read,
 871                                 TypedMethodOptionMatcher* matcher,
 872                                 char* errorbuf, const int buf_size) {
 873   total_bytes_read = 0;
 874   int bytes_read = 0;
 875   char option_buf[256];
 876 
 877   // Read option name.
 878   if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", option_buf, &bytes_read) == 1) {
 879     line += bytes_read;
 880     total_bytes_read += bytes_read;
 881     int bytes_read2 = 0;
 882     total_bytes_read += skip_whitespace(line);
 883     CompileCommandEnum option = match_option_name(option_buf, &bytes_read2, errorbuf, buf_size);
 884     if (option == CompileCommandEnum::Unknown) {
 885       assert(*errorbuf != '\0', "error must have been set");
 886       return;
 887     }
 888     enum OptionType optiontype = option2type(option);
 889     if (option2type(option) != type) {
 890       const char* optiontype_name = optiontype2name(optiontype);
 891       const char* type_name = optiontype2name(type);
 892       jio_snprintf(errorbuf, buf_size, "Option '%s' with type '%s' doesn't match supplied type '%s'", option_buf, optiontype_name, type_name);
 893       return;
 894     }
 895     scan_value(type, line, total_bytes_read, matcher, option, errorbuf, buf_size);
 896   } else {
 897     const char* type_str = optiontype2name(type);
 898     jio_snprintf(errorbuf, buf_size, "Option name for type '%s' should be alphanumeric ", type_str);
 899   }
 900   return;
 901 }
 902 
 903 void CompilerOracle::print_parse_error(char* error_msg, char* original_line) {
 904   assert(*error_msg != '\0', "Must have error_message");
 905   ttyLocker ttyl;
 906   tty->print_cr("CompileCommand: An error occurred during parsing");
 907   tty->print_cr("Error: %s", error_msg);
 908   tty->print_cr("Line: '%s'", original_line);
 909   print_tip();
 910 }
 911 
 912 class LineCopy : StackObj {
 913   const char* _copy;
 914 public:
 915     LineCopy(char* line) {
 916       _copy = os::strdup(line, mtInternal);
 917     }
 918     ~LineCopy() {
 919       os::free((void*)_copy);
 920     }
 921     char* get() {
 922       return (char*)_copy;
 923     }
 924 };
 925 
 926 bool CompilerOracle::parse_from_line_quietly(char* line) {
 927   const bool quiet0 = _quiet;
 928   _quiet = true;
 929   const bool result = parse_from_line(line);
 930   _quiet = quiet0;
 931   return result;
 932 }
 933 
 934 bool CompilerOracle::parse_from_line(char* line) {
 935   if ((line[0] == '\0') || (line[0] == '#')) {
 936     return true;
 937   }
 938 
 939   LineCopy original(line);
 940   int bytes_read;
 941   char error_buf[1024] = {0};
 942 
 943   CompileCommandEnum option = match_option_name(line, &bytes_read, error_buf, sizeof(error_buf));
 944   line += bytes_read;
 945   ResourceMark rm;
 946 
 947   if (option == CompileCommandEnum::Unknown) {
 948     print_parse_error(error_buf, original.get());
 949     return false;
 950   }
 951 
 952   if (option == CompileCommandEnum::Quiet) {
 953     _quiet = true;
 954     return true;
 955   }
 956 
 957   if (option == CompileCommandEnum::Help) {
 958     usage();
 959     return true;
 960   }
 961 
 962   if (option == CompileCommandEnum::Option) {
 963     // Look for trailing options.
 964     //
 965     // Two types of trailing options are
 966     // supported:
 967     //
 968     // (1) CompileCommand=option,Klass::method,option
 969     // (2) CompileCommand=option,Klass::method,type,option,value
 970     //
 971     // Type (1) is used to enable a boolean option for a method.
 972     //
 973     // Type (2) is used to support options with a value. Values can have the
 974     // the following types: intx, uintx, bool, ccstr, ccstrlist, and double.
 975 
 976     char option_type[256]; // stores option for Type (1) and type of Type (2)
 977     skip_comma(line);
 978     TypedMethodOptionMatcher* archetype = TypedMethodOptionMatcher::parse_method_pattern(line, error_buf, sizeof(error_buf));
 979     if (archetype == nullptr) {
 980       print_parse_error(error_buf, original.get());
 981       return false;
 982     }
 983 
 984     skip_whitespace(line);
 985 
 986     // This is unnecessarily complex. Should retire multi-option lines and skip while loop
 987     while (sscanf(line, "%255[a-zA-Z0-9]%n", option_type, &bytes_read) == 1) {
 988       line += bytes_read;
 989 
 990       // typed_matcher is used as a blueprint for each option, deleted at the end
 991       TypedMethodOptionMatcher* typed_matcher = archetype->clone();
 992       enum OptionType type = parse_option_type(option_type);
 993       if (type != OptionType::Unknown) {
 994         // Type (2) option: parse option name and value.
 995         scan_option_and_value(type, line, bytes_read, typed_matcher, error_buf, sizeof(error_buf));
 996         if (*error_buf != '\0') {
 997           print_parse_error(error_buf, original.get());
 998           return false;
 999         }
1000         line += bytes_read;
1001       } else {
1002         // Type (1) option - option_type contains the option name -> bool value = true is implied
1003         int bytes_read;
1004         CompileCommandEnum option = match_option_name(option_type, &bytes_read, error_buf, sizeof(error_buf));
1005         if (option == CompileCommandEnum::Unknown) {
1006           print_parse_error(error_buf, original.get());
1007           return false;
1008         }
1009         if (option2type(option) == OptionType::Bool) {
1010           register_command(typed_matcher, option, true);
1011         } else {
1012           jio_snprintf(error_buf, sizeof(error_buf), "  Missing type '%s' before option '%s'",
1013                        optiontype2name(option2type(option)), option2name(option));
1014           print_parse_error(error_buf, original.get());
1015           return false;
1016         }
1017       }
1018       assert(typed_matcher != nullptr, "sanity");
1019       assert(*error_buf == '\0', "No error here");
1020       skip_whitespace(line);
1021     } // while(
1022     delete archetype;
1023   } else {  // not an OptionCommand
1024     // Command has the following form:
1025     // CompileCommand=<option>,<method pattern><value>
1026     // CompileCommand=<option>,<method pattern>     (implies option is bool and value is true)
1027     assert(*error_buf == '\0', "Don't call here with error_buf already set");
1028     enum OptionType type = option2type(option);
1029     int bytes_read = 0;
1030     skip_comma(line);
1031     TypedMethodOptionMatcher* matcher = TypedMethodOptionMatcher::parse_method_pattern(line, error_buf, sizeof(error_buf));
1032     if (matcher == nullptr) {
1033       print_parse_error(error_buf, original.get());
1034       return false;
1035     }
1036     skip_whitespace(line);
1037     if (*line == '\0') {
1038       if (option2type(option) == OptionType::Bool) {
1039         // if this is a bool option this implies true
1040         register_command(matcher, option, true);
1041         return true;
1042       } else if (option == CompileCommandEnum::MemStat) {
1043         // MemStat default action is to collect data but to not print
1044         register_command(matcher, option, (uintx)MemStatAction::collect);
1045         return true;
1046       } else {
1047         jio_snprintf(error_buf, sizeof(error_buf), "  Option '%s' is not followed by a value", option2name(option));
1048         print_parse_error(error_buf, original.get());
1049         return false;
1050       }
1051     }
1052     scan_value(type, line, bytes_read, matcher, option, error_buf, sizeof(error_buf));
1053     if (*error_buf != '\0') {
1054       print_parse_error(error_buf, original.get());
1055       return false;
1056     }
1057     assert(matcher != nullptr, "consistency");
1058   }
1059   return true;
1060 }
1061 
1062 static const char* default_cc_file = ".hotspot_compiler";
1063 
1064 static const char* cc_file() {
1065 #ifdef ASSERT
1066   if (CompileCommandFile == nullptr)
1067     return default_cc_file;
1068 #endif
1069   return CompileCommandFile;
1070 }
1071 
1072 bool CompilerOracle::has_command_file() {
1073   return cc_file() != nullptr;
1074 }
1075 
1076 bool CompilerOracle::_quiet = false;
1077 
1078 bool CompilerOracle::parse_from_file() {
1079   assert(has_command_file(), "command file must be specified");
1080   FILE* stream = os::fopen(cc_file(), "rt");
1081   if (stream == nullptr) {
1082     return true;
1083   }
1084 
1085   FileInput input(stream, /*need_close=*/ true);
1086   return parse_from_input(&input, parse_from_line);
1087 }
1088 
1089 bool CompilerOracle::parse_from_input(inputStream::Input* input,
1090                                       CompilerOracle::
1091                                       parse_from_line_fn_t* parse_from_line) {
1092   bool success = true;
1093   for (inputStream in(input); !in.done(); in.next()) {
1094     if (!parse_from_line(in.current_line())) {
1095       success = false;
1096     }
1097   }
1098   return success;
1099 }
1100 
1101 bool CompilerOracle::parse_from_string(const char* str,
1102                                        CompilerOracle::
1103                                        parse_from_line_fn_t* parse_from_line) {
1104   MemoryInput input(str, strlen(str));
1105   return parse_from_input(&input, parse_from_line);
1106 }
1107 
1108 bool compilerOracle_init() {
1109   bool success = true;
1110   // Register default compile commands first - any commands specified via CompileCommand will
1111   // supersede these default commands.
1112   for (int i = 0; default_compile_commands[i] != nullptr; i ++) {
1113     char* s = os::strdup(default_compile_commands[i]);
1114     success = CompilerOracle::parse_from_line_quietly(s);
1115     os::free(s);
1116     assert(success, "default compile command \"%s\" failed to parse", default_compile_commands[i]);
1117   }
1118   if (!CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line)) {
1119     success = false;
1120   }
1121   if (!CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only)) {
1122     success = false;
1123   }
1124   if (CompilerOracle::has_command_file()) {
1125     if (!CompilerOracle::parse_from_file()) {
1126       success = false;
1127     }
1128   } else {
1129     struct stat buf;
1130     if (os::stat(default_cc_file, &buf) == 0) {
1131       warning("%s file is present but has been ignored.  "
1132               "Run with -XX:CompileCommandFile=%s to load the file.",
1133               default_cc_file, default_cc_file);
1134     }
1135   }
1136   if (has_command(CompileCommandEnum::Print)) {
1137     if (PrintAssembly) {
1138       warning("CompileCommand and/or %s file contains 'print' commands, but PrintAssembly is also enabled", default_cc_file);
1139     }
1140   }
1141   return success;
1142 }
1143 
1144 bool CompilerOracle::parse_compile_only(char* line) {
1145   if (line[0] == '\0') {
1146     return true;
1147   }
1148   ResourceMark rm;
1149   char error_buf[1024] = {0};
1150   LineCopy original(line);
1151   char* method_pattern;
1152   do {
1153     if (line[0] == '\0') {
1154       break;
1155     }
1156     method_pattern = strtok_r(line, ",", &line);
1157     if (method_pattern != nullptr) {
1158       TypedMethodOptionMatcher* matcher = TypedMethodOptionMatcher::parse_method_pattern(method_pattern, error_buf, sizeof(error_buf));
1159       if (matcher != nullptr) {
1160         register_command(matcher, CompileCommandEnum::CompileOnly, true);
1161         continue;
1162       }
1163     }
1164     ttyLocker ttyl;
1165     tty->print_cr("CompileOnly: An error occurred during parsing");
1166     if (*error_buf != '\0') {
1167       tty->print_cr("Error: %s", error_buf);
1168     }
1169     tty->print_cr("Line: '%s'", original.get());
1170     return false;
1171   } while (method_pattern != nullptr && line != nullptr);
1172   return true;
1173 }
1174 
1175 CompileCommandEnum CompilerOracle::string_to_option(const char* name) {
1176   int bytes_read = 0;
1177   char errorbuf[1024] = {0};
1178   return match_option_name(name, &bytes_read, errorbuf, sizeof(errorbuf));
1179 }