1 /*
   2  * Copyright (c) 1997, 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 "cds/cdsConfig.hpp"
  27 #include "cds/cppVtables.hpp"
  28 #include "cds/metaspaceShared.hpp"
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/classLoaderDataGraph.hpp"
  31 #include "classfile/metadataOnStackMark.hpp"
  32 #include "classfile/symbolTable.hpp"
  33 #include "classfile/systemDictionary.hpp"
  34 #include "classfile/vmClasses.hpp"
  35 #include "code/codeCache.hpp"
  36 #include "code/debugInfoRec.hpp"
  37 #include "compiler/compilationPolicy.hpp"
  38 #include "gc/shared/collectedHeap.inline.hpp"
  39 #include "interpreter/bytecodeStream.hpp"
  40 #include "interpreter/bytecodeTracer.hpp"
  41 #include "interpreter/bytecodes.hpp"
  42 #include "interpreter/interpreter.hpp"
  43 #include "interpreter/oopMapCache.hpp"
  44 #include "logging/log.hpp"
  45 #include "logging/logStream.hpp"
  46 #include "logging/logTag.hpp"
  47 #include "memory/allocation.inline.hpp"
  48 #include "memory/metadataFactory.hpp"
  49 #include "memory/metaspaceClosure.hpp"
  50 #include "memory/oopFactory.hpp"
  51 #include "memory/resourceArea.hpp"
  52 #include "memory/universe.hpp"
  53 #include "nmt/memTracker.hpp"
  54 #include "oops/constMethod.hpp"
  55 #include "oops/constantPool.hpp"
  56 #include "oops/klass.inline.hpp"
  57 #include "oops/method.inline.hpp"
  58 #include "oops/methodData.hpp"
  59 #include "oops/objArrayKlass.hpp"
  60 #include "oops/objArrayOop.inline.hpp"
  61 #include "oops/oop.inline.hpp"
  62 #include "oops/symbol.hpp"
  63 #include "oops/inlineKlass.inline.hpp"
  64 #include "prims/jvmtiExport.hpp"
  65 #include "prims/methodHandles.hpp"
  66 #include "runtime/atomic.hpp"
  67 #include "runtime/arguments.hpp"
  68 #include "runtime/continuationEntry.hpp"
  69 #include "runtime/frame.inline.hpp"
  70 #include "runtime/handles.inline.hpp"
  71 #include "runtime/init.hpp"
  72 #include "runtime/java.hpp"
  73 #include "runtime/orderAccess.hpp"
  74 #include "runtime/perfData.hpp"
  75 #include "runtime/relocator.hpp"
  76 #include "runtime/safepointVerifiers.hpp"
  77 #include "runtime/sharedRuntime.hpp"
  78 #include "runtime/signature.hpp"
  79 #include "runtime/threads.hpp"
  80 #include "runtime/vm_version.hpp"
  81 #include "utilities/align.hpp"
  82 #include "utilities/quickSort.hpp"
  83 #include "utilities/vmError.hpp"
  84 #include "utilities/xmlstream.hpp"
  85 
  86 // Implementation of Method
  87 
  88 Method* Method::allocate(ClassLoaderData* loader_data,
  89                          int byte_code_size,
  90                          AccessFlags access_flags,
  91                          InlineTableSizes* sizes,
  92                          ConstMethod::MethodType method_type,
  93                          Symbol* name,
  94                          TRAPS) {
  95   assert(!access_flags.is_native() || byte_code_size == 0,
  96          "native methods should not contain byte codes");
  97   ConstMethod* cm = ConstMethod::allocate(loader_data,
  98                                           byte_code_size,
  99                                           sizes,
 100                                           method_type,
 101                                           CHECK_NULL);
 102   int size = Method::size(access_flags.is_native());
 103   return new (loader_data, size, MetaspaceObj::MethodType, THREAD) Method(cm, access_flags, name);
 104 }
 105 
 106 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
 107   NoSafepointVerifier no_safepoint;
 108   set_constMethod(xconst);
 109   set_access_flags(access_flags);
 110   set_intrinsic_id(vmIntrinsics::_none);
 111   clear_method_data();
 112   clear_method_counters();
 113   set_vtable_index(Method::garbage_vtable_index);
 114 
 115   // Fix and bury in Method*
 116   set_interpreter_entry(nullptr); // sets i2i entry and from_int
 117   set_adapter_entry(nullptr);
 118   Method::clear_code(); // from_c/from_i get set to c2i/i2i
 119 
 120   if (access_flags.is_native()) {
 121     clear_native_function();
 122     set_signature_handler(nullptr);
 123   }

 124   NOT_PRODUCT(set_compiled_invocation_count(0);)
 125   // Name is very useful for debugging.
 126   NOT_PRODUCT(_name = name;)
 127 }
 128 
 129 // Release Method*.  The nmethod will be gone when we get here because
 130 // we've walked the code cache.
 131 void Method::deallocate_contents(ClassLoaderData* loader_data) {
 132   MetadataFactory::free_metadata(loader_data, constMethod());
 133   set_constMethod(nullptr);
 134   MetadataFactory::free_metadata(loader_data, method_data());
 135   clear_method_data();
 136   MetadataFactory::free_metadata(loader_data, method_counters());
 137   clear_method_counters();
 138   // The nmethod will be gone when we get here.
 139   if (code() != nullptr) _code = nullptr;
 140 }
 141 
 142 void Method::release_C_heap_structures() {
 143   if (method_data()) {
 144     method_data()->release_C_heap_structures();
 145 
 146     // Destroy MethodData embedded lock
 147     method_data()->~MethodData();
 148   }
 149 }
 150 
 151 address Method::get_i2c_entry() {
 152   assert(adapter() != nullptr, "must have");
 153   return adapter()->get_i2c_entry();
 154 }
 155 
 156 address Method::get_c2i_entry() {
 157   assert(adapter() != nullptr, "must have");
 158   return adapter()->get_c2i_entry();
 159 }
 160 
 161 address Method::get_c2i_inline_entry() {
 162   assert(adapter() != nullptr, "must have");
 163   return adapter()->get_c2i_inline_entry();
 164 }
 165 
 166 address Method::get_c2i_unverified_entry() {
 167   assert(adapter() != nullptr, "must have");
 168   return adapter()->get_c2i_unverified_entry();
 169 }
 170 
 171 address Method::get_c2i_unverified_inline_entry() {
 172   assert(adapter() != nullptr, "must have");
 173   return adapter()->get_c2i_unverified_inline_entry();
 174 }
 175 
 176 address Method::get_c2i_no_clinit_check_entry() {
 177   assert(VM_Version::supports_fast_class_init_checks(), "");
 178   assert(adapter() != nullptr, "must have");
 179   return adapter()->get_c2i_no_clinit_check_entry();
 180 }
 181 
 182 char* Method::name_and_sig_as_C_string() const {
 183   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
 184 }
 185 
 186 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
 187   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
 188 }
 189 
 190 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
 191   const char* klass_name = klass->external_name();
 192   int klass_name_len  = (int)strlen(klass_name);
 193   int method_name_len = method_name->utf8_length();
 194   int len             = klass_name_len + 1 + method_name_len + signature->utf8_length();
 195   char* dest          = NEW_RESOURCE_ARRAY(char, len + 1);
 196   strcpy(dest, klass_name);
 197   dest[klass_name_len] = '.';
 198   strcpy(&dest[klass_name_len + 1], method_name->as_C_string());
 199   strcpy(&dest[klass_name_len + 1 + method_name_len], signature->as_C_string());
 200   dest[len] = 0;
 201   return dest;
 202 }
 203 
 204 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size) {
 205   Symbol* klass_name = klass->name();
 206   klass_name->as_klass_external_name(buf, size);
 207   int len = (int)strlen(buf);
 208 
 209   if (len < size - 1) {
 210     buf[len++] = '.';
 211 
 212     method_name->as_C_string(&(buf[len]), size - len);
 213     len = (int)strlen(buf);
 214 
 215     signature->as_C_string(&(buf[len]), size - len);
 216   }
 217 
 218   return buf;
 219 }
 220 
 221 const char* Method::external_name() const {
 222   return external_name(constants()->pool_holder(), name(), signature());
 223 }
 224 
 225 void Method::print_external_name(outputStream *os) const {
 226   print_external_name(os, constants()->pool_holder(), name(), signature());
 227 }
 228 
 229 const char* Method::external_name(Klass* klass, Symbol* method_name, Symbol* signature) {
 230   stringStream ss;
 231   print_external_name(&ss, klass, method_name, signature);
 232   return ss.as_string();
 233 }
 234 
 235 void Method::print_external_name(outputStream *os, Klass* klass, Symbol* method_name, Symbol* signature) {
 236   signature->print_as_signature_external_return_type(os);
 237   os->print(" %s.%s(", klass->external_name(), method_name->as_C_string());
 238   signature->print_as_signature_external_parameters(os);
 239   os->print(")");
 240 }
 241 
 242 int Method::fast_exception_handler_bci_for(const methodHandle& mh, Klass* ex_klass, int throw_bci, TRAPS) {
 243   if (log_is_enabled(Debug, exceptions)) {
 244     ResourceMark rm(THREAD);
 245     log_debug(exceptions)("Looking for catch handler for exception of type \"%s\" in method \"%s\"",
 246                           ex_klass == nullptr ? "null" : ex_klass->external_name(), mh->name()->as_C_string());
 247   }
 248   // exception table holds quadruple entries of the form (beg_bci, end_bci, handler_bci, klass_index)
 249   // access exception table
 250   ExceptionTable table(mh());
 251   int length = table.length();
 252   // iterate through all entries sequentially
 253   constantPoolHandle pool(THREAD, mh->constants());
 254   for (int i = 0; i < length; i ++) {
 255     //reacquire the table in case a GC happened
 256     ExceptionTable table(mh());
 257     int beg_bci = table.start_pc(i);
 258     int end_bci = table.end_pc(i);
 259     assert(beg_bci <= end_bci, "inconsistent exception table");
 260     log_debug(exceptions)("  - checking exception table entry for BCI %d to %d",
 261                          beg_bci, end_bci);
 262 
 263     if (beg_bci <= throw_bci && throw_bci < end_bci) {
 264       // exception handler bci range covers throw_bci => investigate further
 265       log_debug(exceptions)("    - entry covers throw point BCI %d", throw_bci);
 266 
 267       int handler_bci = table.handler_pc(i);
 268       int klass_index = table.catch_type_index(i);
 269       if (klass_index == 0) {
 270         if (log_is_enabled(Info, exceptions)) {
 271           ResourceMark rm(THREAD);
 272           log_info(exceptions)("Found catch-all handler for exception of type \"%s\" in method \"%s\" at BCI: %d",
 273                                ex_klass == nullptr ? "null" : ex_klass->external_name(), mh->name()->as_C_string(), handler_bci);
 274         }
 275         return handler_bci;
 276       } else if (ex_klass == nullptr) {
 277         // Is this even possible?
 278         if (log_is_enabled(Info, exceptions)) {
 279           ResourceMark rm(THREAD);
 280           log_info(exceptions)("null exception class is implicitly caught by handler in method \"%s\" at BCI: %d",
 281                                mh()->name()->as_C_string(), handler_bci);
 282         }
 283         return handler_bci;
 284       } else {
 285         if (log_is_enabled(Debug, exceptions)) {
 286           ResourceMark rm(THREAD);
 287           log_debug(exceptions)("    - resolving catch type \"%s\"",
 288                                pool->klass_name_at(klass_index)->as_C_string());
 289         }
 290         // we know the exception class => get the constraint class
 291         // this may require loading of the constraint class; if verification
 292         // fails or some other exception occurs, return handler_bci
 293         Klass* k = pool->klass_at(klass_index, THREAD);
 294         if (HAS_PENDING_EXCEPTION) {
 295           if (log_is_enabled(Debug, exceptions)) {
 296             ResourceMark rm(THREAD);
 297             log_debug(exceptions)("    - exception \"%s\" occurred resolving catch type",
 298                                  PENDING_EXCEPTION->klass()->external_name());
 299           }
 300           return handler_bci;
 301         }
 302         assert(k != nullptr, "klass not loaded");
 303         if (ex_klass->is_subtype_of(k)) {
 304           if (log_is_enabled(Info, exceptions)) {
 305             ResourceMark rm(THREAD);
 306             log_info(exceptions)("Found matching handler for exception of type \"%s\" in method \"%s\" at BCI: %d",
 307                                  ex_klass == nullptr ? "null" : ex_klass->external_name(), mh->name()->as_C_string(), handler_bci);
 308           }
 309           return handler_bci;
 310         }
 311       }
 312     }
 313   }
 314 
 315   if (log_is_enabled(Debug, exceptions)) {
 316     ResourceMark rm(THREAD);
 317     log_debug(exceptions)("No catch handler found for exception of type \"%s\" in method \"%s\"",
 318                           ex_klass->external_name(), mh->name()->as_C_string());
 319   }
 320 
 321   return -1;
 322 }
 323 
 324 void Method::mask_for(int bci, InterpreterOopMap* mask) {
 325   methodHandle h_this(Thread::current(), this);
 326   mask_for(h_this, bci, mask);
 327 }
 328 
 329 void Method::mask_for(const methodHandle& this_mh, int bci, InterpreterOopMap* mask) {
 330   assert(this_mh() == this, "Sanity");
 331   method_holder()->mask_for(this_mh, bci, mask);
 332 }
 333 
 334 int Method::bci_from(address bcp) const {
 335   if (is_native() && bcp == nullptr) {
 336     return 0;
 337   }
 338   // Do not have a ResourceMark here because AsyncGetCallTrace stack walking code
 339   // may call this after interrupting a nested ResourceMark.
 340   assert((is_native() && bcp == code_base()) || contains(bcp) || VMError::is_error_reported(),
 341          "bcp doesn't belong to this method. bcp: " PTR_FORMAT, p2i(bcp));
 342 
 343   return int(bcp - code_base());
 344 }
 345 
 346 
 347 int Method::validate_bci(int bci) const {
 348   return (bci == 0 || bci < code_size()) ? bci : -1;
 349 }
 350 
 351 // Return bci if it appears to be a valid bcp
 352 // Return -1 otherwise.
 353 // Used by profiling code, when invalid data is a possibility.
 354 // The caller is responsible for validating the Method* itself.
 355 int Method::validate_bci_from_bcp(address bcp) const {
 356   // keep bci as -1 if not a valid bci
 357   int bci = -1;
 358   if (bcp == nullptr || bcp == code_base()) {
 359     // code_size() may return 0 and we allow 0 here
 360     // the method may be native
 361     bci = 0;
 362   } else if (contains(bcp)) {
 363     bci = int(bcp - code_base());
 364   }
 365   // Assert that if we have dodged any asserts, bci is negative.
 366   assert(bci == -1 || bci == bci_from(bcp_from(bci)), "sane bci if >=0");
 367   return bci;
 368 }
 369 
 370 address Method::bcp_from(int bci) const {
 371   assert((is_native() && bci == 0) || (!is_native() && 0 <= bci && bci < code_size()),
 372          "illegal bci: %d for %s method", bci, is_native() ? "native" : "non-native");
 373   address bcp = code_base() + bci;
 374   assert((is_native() && bcp == code_base()) || contains(bcp), "bcp doesn't belong to this method");
 375   return bcp;
 376 }
 377 
 378 address Method::bcp_from(address bcp) const {
 379   if (is_native() && bcp == nullptr) {
 380     return code_base();
 381   } else {
 382     return bcp;
 383   }
 384 }
 385 
 386 int Method::size(bool is_native) {
 387   // If native, then include pointers for native_function and signature_handler
 388   int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
 389   int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
 390   return align_metadata_size(header_size() + extra_words);
 391 }
 392 
 393 Symbol* Method::klass_name() const {
 394   return method_holder()->name();
 395 }
 396 
 397 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
 398   log_trace(cds)("Iter(Method): %p", this);
 399 
 400   if (!method_holder()->is_rewritten() || CDSConfig::is_valhalla_preview()) {
 401     it->push(&_constMethod, MetaspaceClosure::_writable);
 402   } else {
 403     it->push(&_constMethod);
 404   }
 405   it->push(&_method_data);
 406   it->push(&_method_counters);
 407   NOT_PRODUCT(it->push(&_name);)
 408 }
 409 
 410 #if INCLUDE_CDS
 411 // Attempt to return method to original state.  Clear any pointers
 412 // (to objects outside the shared spaces).  We won't be able to predict
 413 // where they should point in a new JVM.  Further initialize some
 414 // entries now in order allow them to be write protected later.
 415 
 416 void Method::remove_unshareable_info() {
 417   unlink_method();
 418   JFR_ONLY(REMOVE_METHOD_ID(this);)
 419 }
 420 
 421 void Method::restore_unshareable_info(TRAPS) {
 422   assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored");
 423   assert(!queued_for_compilation(), "method's queued_for_compilation flag should not be set");
 424 }
 425 #endif
 426 
 427 void Method::set_vtable_index(int index) {
 428   if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
 429     // At runtime initialize_vtable is rerun as part of link_class_impl()
 430     // for a shared class loaded by the non-boot loader to obtain the loader
 431     // constraints based on the runtime classloaders' context.
 432     return; // don't write into the shared class
 433   } else {
 434     _vtable_index = index;
 435   }
 436 }
 437 
 438 void Method::set_itable_index(int index) {
 439   if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
 440     // At runtime initialize_itable is rerun as part of link_class_impl()
 441     // for a shared class loaded by the non-boot loader to obtain the loader
 442     // constraints based on the runtime classloaders' context. The dumptime
 443     // itable index should be the same as the runtime index.
 444     assert(_vtable_index == itable_index_max - index,
 445            "archived itable index is different from runtime index");
 446     return; // don’t write into the shared class
 447   } else {
 448     _vtable_index = itable_index_max - index;
 449   }
 450   assert(valid_itable_index(), "");
 451 }
 452 
 453 // The RegisterNatives call being attempted tried to register with a method that
 454 // is not native.  Ask JVM TI what prefixes have been specified.  Then check
 455 // to see if the native method is now wrapped with the prefixes.  See the
 456 // SetNativeMethodPrefix(es) functions in the JVM TI Spec for details.
 457 static Method* find_prefixed_native(Klass* k, Symbol* name, Symbol* signature, TRAPS) {
 458 #if INCLUDE_JVMTI
 459   ResourceMark rm(THREAD);
 460   Method* method;
 461   int name_len = name->utf8_length();
 462   char* name_str = name->as_utf8();
 463   int prefix_count;
 464   char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
 465   for (int i = 0; i < prefix_count; i++) {
 466     char* prefix = prefixes[i];
 467     int prefix_len = (int)strlen(prefix);
 468 
 469     // try adding this prefix to the method name and see if it matches another method name
 470     int trial_len = name_len + prefix_len;
 471     char* trial_name_str = NEW_RESOURCE_ARRAY(char, trial_len + 1);
 472     strcpy(trial_name_str, prefix);
 473     strcat(trial_name_str, name_str);
 474     TempNewSymbol trial_name = SymbolTable::probe(trial_name_str, trial_len);
 475     if (trial_name == nullptr) {
 476       continue; // no such symbol, so this prefix wasn't used, try the next prefix
 477     }
 478     method = k->lookup_method(trial_name, signature);
 479     if (method == nullptr) {
 480       continue; // signature doesn't match, try the next prefix
 481     }
 482     if (method->is_native()) {
 483       method->set_is_prefixed_native();
 484       return method; // wahoo, we found a prefixed version of the method, return it
 485     }
 486     // found as non-native, so prefix is good, add it, probably just need more prefixes
 487     name_len = trial_len;
 488     name_str = trial_name_str;
 489   }
 490 #endif // INCLUDE_JVMTI
 491   return nullptr; // not found
 492 }
 493 
 494 bool Method::register_native(Klass* k, Symbol* name, Symbol* signature, address entry, TRAPS) {
 495   Method* method = k->lookup_method(name, signature);
 496   if (method == nullptr) {
 497     ResourceMark rm(THREAD);
 498     stringStream st;
 499     st.print("Method '");
 500     print_external_name(&st, k, name, signature);
 501     st.print("' name or signature does not match");
 502     THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false);
 503   }
 504   if (!method->is_native()) {
 505     // trying to register to a non-native method, see if a JVM TI agent has added prefix(es)
 506     method = find_prefixed_native(k, name, signature, THREAD);
 507     if (method == nullptr) {
 508       ResourceMark rm(THREAD);
 509       stringStream st;
 510       st.print("Method '");
 511       print_external_name(&st, k, name, signature);
 512       st.print("' is not declared as native");
 513       THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false);
 514     }
 515   }
 516 
 517   if (entry != nullptr) {
 518     method->set_native_function(entry, native_bind_event_is_interesting);
 519   } else {
 520     method->clear_native_function();
 521   }
 522   if (log_is_enabled(Debug, jni, resolve)) {
 523     ResourceMark rm(THREAD);
 524     log_debug(jni, resolve)("[Registering JNI native method %s.%s]",
 525                             method->method_holder()->external_name(),
 526                             method->name()->as_C_string());
 527   }
 528   return true;
 529 }
 530 
 531 bool Method::was_executed_more_than(int n) {
 532   // Invocation counter is reset when the Method* is compiled.
 533   // If the method has compiled code we therefore assume it has
 534   // be executed more than n times.
 535   if (is_accessor() || is_empty_method() || (code() != nullptr)) {
 536     // interpreter doesn't bump invocation counter of trivial methods
 537     // compiler does not bump invocation counter of compiled methods
 538     return true;
 539   }
 540   else if ((method_counters() != nullptr &&
 541             method_counters()->invocation_counter()->carry()) ||
 542            (method_data() != nullptr &&
 543             method_data()->invocation_counter()->carry())) {
 544     // The carry bit is set when the counter overflows and causes
 545     // a compilation to occur.  We don't know how many times
 546     // the counter has been reset, so we simply assume it has
 547     // been executed more than n times.
 548     return true;
 549   } else {
 550     return invocation_count() > n;
 551   }
 552 }
 553 
 554 void Method::print_invocation_count(outputStream* st) {
 555   //---<  compose+print method return type, klass, name, and signature  >---
 556   if (is_static())       { st->print("static "); }
 557   if (is_final())        { st->print("final "); }
 558   if (is_synchronized()) { st->print("synchronized "); }
 559   if (is_native())       { st->print("native "); }
 560   st->print("%s::", method_holder()->external_name());
 561   name()->print_symbol_on(st);
 562   signature()->print_symbol_on(st);
 563 
 564   if (WizardMode) {
 565     // dump the size of the byte codes
 566     st->print(" {%d}", code_size());
 567   }
 568   st->cr();
 569 
 570   // Counting based on signed int counters tends to overflow with
 571   // longer-running workloads on fast machines. The counters under
 572   // consideration here, however, are limited in range by counting
 573   // logic. See InvocationCounter:count_limit for example.
 574   // No "overflow precautions" need to be implemented here.
 575   st->print_cr ("  interpreter_invocation_count: " INT32_FORMAT_W(11), interpreter_invocation_count());
 576   st->print_cr ("  invocation_counter:           " INT32_FORMAT_W(11), invocation_count());
 577   st->print_cr ("  backedge_counter:             " INT32_FORMAT_W(11), backedge_count());
 578 
 579   if (method_data() != nullptr) {
 580     st->print_cr ("  decompile_count:              " UINT32_FORMAT_W(11), method_data()->decompile_count());
 581   }
 582 
 583 #ifndef PRODUCT
 584   if (CountCompiledCalls) {
 585     st->print_cr ("  compiled_invocation_count:    " INT64_FORMAT_W(11), compiled_invocation_count());
 586   }
 587 #endif
 588 }
 589 
 590 // Build a MethodData* object to hold profiling information collected on this
 591 // method when requested.
 592 void Method::build_profiling_method_data(const methodHandle& method, TRAPS) {
 593   // Do not profile the method if metaspace has hit an OOM previously
 594   // allocating profiling data. Callers clear pending exception so don't
 595   // add one here.
 596   if (ClassLoaderDataGraph::has_metaspace_oom()) {
 597     return;
 598   }
 599 
 600   ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
 601   MethodData* method_data = MethodData::allocate(loader_data, method, THREAD);
 602   if (HAS_PENDING_EXCEPTION) {
 603     CompileBroker::log_metaspace_failure();
 604     ClassLoaderDataGraph::set_metaspace_oom(true);
 605     return;   // return the exception (which is cleared)
 606   }
 607 
 608   if (!Atomic::replace_if_null(&method->_method_data, method_data)) {
 609     MetadataFactory::free_metadata(loader_data, method_data);
 610     return;
 611   }
 612 
 613   if (PrintMethodData && (Verbose || WizardMode)) {
 614     ResourceMark rm(THREAD);
 615     tty->print("build_profiling_method_data for ");
 616     method->print_name(tty);
 617     tty->cr();
 618     // At the end of the run, the MDO, full of data, will be dumped.
 619   }
 620 }
 621 
 622 MethodCounters* Method::build_method_counters(Thread* current, Method* m) {
 623   // Do not profile the method if metaspace has hit an OOM previously
 624   if (ClassLoaderDataGraph::has_metaspace_oom()) {
 625     return nullptr;
 626   }
 627 
 628   methodHandle mh(current, m);
 629   MethodCounters* counters;
 630   if (current->is_Java_thread()) {
 631     JavaThread* THREAD = JavaThread::cast(current); // For exception macros.
 632     // Use the TRAPS version for a JavaThread so it will adjust the GC threshold
 633     // if needed.
 634     counters = MethodCounters::allocate_with_exception(mh, THREAD);
 635     if (HAS_PENDING_EXCEPTION) {
 636       CLEAR_PENDING_EXCEPTION;
 637     }
 638   } else {
 639     // Call metaspace allocation that doesn't throw exception if the
 640     // current thread isn't a JavaThread, ie. the VMThread.
 641     counters = MethodCounters::allocate_no_exception(mh);
 642   }
 643 
 644   if (counters == nullptr) {
 645     CompileBroker::log_metaspace_failure();
 646     ClassLoaderDataGraph::set_metaspace_oom(true);
 647     return nullptr;
 648   }
 649 
 650   if (!mh->init_method_counters(counters)) {
 651     MetadataFactory::free_metadata(mh->method_holder()->class_loader_data(), counters);
 652   }
 653 
 654   return mh->method_counters();
 655 }
 656 
 657 bool Method::init_method_counters(MethodCounters* counters) {
 658   // Try to install a pointer to MethodCounters, return true on success.
 659   return Atomic::replace_if_null(&_method_counters, counters);
 660 }
 661 
 662 void Method::set_exception_handler_entered(int handler_bci) {
 663   if (ProfileExceptionHandlers) {
 664     MethodData* mdo = method_data();
 665     if (mdo != nullptr) {
 666       BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
 667       handler_data.set_exception_handler_entered();
 668     }
 669   }
 670 }
 671 
 672 int Method::extra_stack_words() {
 673   // not an inline function, to avoid a header dependency on Interpreter
 674   return extra_stack_entries() * Interpreter::stackElementSize;
 675 }
 676 
 677 // InlineKlass the method is declared to return. This must not
 678 // safepoint as it is called with references live on the stack at
 679 // locations the GC is unaware of.
 680 InlineKlass* Method::returns_inline_type(Thread* thread) const {
 681   assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
 682   if (is_native()) {
 683     return nullptr;
 684   }
 685   NoSafepointVerifier nsv;
 686   SignatureStream ss(signature());
 687   while (!ss.at_return_type()) {
 688     ss.next();
 689   }
 690   return ss.as_inline_klass(method_holder());
 691 }
 692 
 693 bool Method::compute_has_loops_flag() {
 694   BytecodeStream bcs(methodHandle(Thread::current(), this));
 695   Bytecodes::Code bc;
 696 
 697   while ((bc = bcs.next()) >= 0) {
 698     switch (bc) {
 699       case Bytecodes::_ifeq:
 700       case Bytecodes::_ifnull:
 701       case Bytecodes::_iflt:
 702       case Bytecodes::_ifle:
 703       case Bytecodes::_ifne:
 704       case Bytecodes::_ifnonnull:
 705       case Bytecodes::_ifgt:
 706       case Bytecodes::_ifge:
 707       case Bytecodes::_if_icmpeq:
 708       case Bytecodes::_if_icmpne:
 709       case Bytecodes::_if_icmplt:
 710       case Bytecodes::_if_icmpgt:
 711       case Bytecodes::_if_icmple:
 712       case Bytecodes::_if_icmpge:
 713       case Bytecodes::_if_acmpeq:
 714       case Bytecodes::_if_acmpne:
 715       case Bytecodes::_goto:
 716       case Bytecodes::_jsr:
 717         if (bcs.dest() < bcs.next_bci()) {
 718           return set_has_loops();
 719         }
 720         break;
 721 
 722       case Bytecodes::_goto_w:
 723       case Bytecodes::_jsr_w:
 724         if (bcs.dest_w() < bcs.next_bci()) {
 725           return set_has_loops();
 726         }
 727         break;
 728 
 729       case Bytecodes::_lookupswitch: {
 730         Bytecode_lookupswitch lookupswitch(this, bcs.bcp());
 731         if (lookupswitch.default_offset() < 0) {
 732           return set_has_loops();
 733         } else {
 734           for (int i = 0; i < lookupswitch.number_of_pairs(); ++i) {
 735             LookupswitchPair pair = lookupswitch.pair_at(i);
 736             if (pair.offset() < 0) {
 737               return set_has_loops();
 738             }
 739           }
 740         }
 741         break;
 742       }
 743       case Bytecodes::_tableswitch: {
 744         Bytecode_tableswitch tableswitch(this, bcs.bcp());
 745         if (tableswitch.default_offset() < 0) {
 746           return set_has_loops();
 747         } else {
 748           for (int i = 0; i < tableswitch.length(); ++i) {
 749             if (tableswitch.dest_offset_at(i) < 0) {
 750               return set_has_loops();
 751             }
 752           }
 753         }
 754         break;
 755       }
 756       default:
 757         break;
 758     }
 759   }
 760 
 761   _flags.set_has_loops_flag_init(true);
 762   return false;
 763 }
 764 
 765 bool Method::is_final_method(AccessFlags class_access_flags) const {
 766   // or "does_not_require_vtable_entry"
 767   // default method or overpass can occur, is not final (reuses vtable entry)
 768   // private methods in classes get vtable entries for backward class compatibility.
 769   if (is_overpass() || is_default_method())  return false;
 770   return is_final() || class_access_flags.is_final();
 771 }
 772 
 773 bool Method::is_final_method() const {
 774   return is_final_method(method_holder()->access_flags());
 775 }
 776 
 777 bool Method::is_default_method() const {
 778   if (method_holder() != nullptr &&
 779       method_holder()->is_interface() &&
 780       !is_abstract() && !is_private()) {
 781     return true;
 782   } else {
 783     return false;
 784   }
 785 }
 786 
 787 bool Method::can_be_statically_bound(AccessFlags class_access_flags) const {
 788   if (is_final_method(class_access_flags))  return true;
 789 #ifdef ASSERT
 790   bool is_nonv = (vtable_index() == nonvirtual_vtable_index);
 791   if (class_access_flags.is_interface()) {
 792       ResourceMark rm;
 793       assert(is_nonv == is_static() || is_nonv == is_private(),
 794              "nonvirtual unexpected for non-static, non-private: %s",
 795              name_and_sig_as_C_string());
 796   }
 797 #endif
 798   assert(valid_vtable_index() || valid_itable_index(), "method must be linked before we ask this question");
 799   return vtable_index() == nonvirtual_vtable_index;
 800 }
 801 
 802 bool Method::can_be_statically_bound() const {
 803   return can_be_statically_bound(method_holder()->access_flags());
 804 }
 805 
 806 bool Method::can_be_statically_bound(InstanceKlass* context) const {
 807   return (method_holder() == context) && can_be_statically_bound();
 808 }
 809 
 810 /**
 811  *  Returns false if this is one of specially treated methods for
 812  *  which we have to provide stack trace in throw in compiled code.
 813  *  Returns true otherwise.
 814  */
 815 bool Method::can_omit_stack_trace() {
 816   if (klass_name() == vmSymbols::sun_invoke_util_ValueConversions()) {
 817     return false; // All methods in sun.invoke.util.ValueConversions
 818   }
 819   return true;
 820 }
 821 
 822 bool Method::is_accessor() const {
 823   return is_getter() || is_setter();
 824 }
 825 
 826 bool Method::is_getter() const {
 827   if (code_size() != 5) return false;
 828   if (size_of_parameters() != 1) return false;
 829   if (java_code_at(0) != Bytecodes::_aload_0)  return false;
 830   if (java_code_at(1) != Bytecodes::_getfield) return false;
 831   switch (java_code_at(4)) {
 832     case Bytecodes::_ireturn:
 833     case Bytecodes::_lreturn:
 834     case Bytecodes::_freturn:
 835     case Bytecodes::_dreturn:
 836     case Bytecodes::_areturn:
 837       break;
 838     default:
 839       return false;
 840   }
 841   if (has_scalarized_return()) {
 842     // Don't treat this as (trivial) getter method because the
 843     // inline type should be returned in a scalarized form.
 844     return false;
 845   }
 846   return true;
 847 }
 848 
 849 bool Method::is_setter() const {
 850   if (code_size() != 6) return false;
 851   if (java_code_at(0) != Bytecodes::_aload_0) return false;
 852   switch (java_code_at(1)) {
 853     case Bytecodes::_iload_1:
 854     case Bytecodes::_aload_1:
 855     case Bytecodes::_fload_1:
 856       if (size_of_parameters() != 2) return false;
 857       break;
 858     case Bytecodes::_dload_1:
 859     case Bytecodes::_lload_1:
 860       if (size_of_parameters() != 3) return false;
 861       break;
 862     default:
 863       return false;
 864   }
 865   if (java_code_at(2) != Bytecodes::_putfield) return false;
 866   if (java_code_at(5) != Bytecodes::_return)   return false;
 867   if (has_scalarized_args()) {
 868     // Don't treat this as (trivial) setter method because the
 869     // inline type argument should be passed in a scalarized form.
 870     return false;
 871   }
 872   return true;
 873 }
 874 
 875 bool Method::is_constant_getter() const {
 876   int last_index = code_size() - 1;
 877   // Check if the first 1-3 bytecodes are a constant push
 878   // and the last bytecode is a return.
 879   return (2 <= code_size() && code_size() <= 4 &&
 880           Bytecodes::is_const(java_code_at(0)) &&
 881           Bytecodes::length_for(java_code_at(0)) == last_index &&
 882           Bytecodes::is_return(java_code_at(last_index)) &&
 883           !has_scalarized_args());
 884 }
 885 
 886 bool Method::is_class_initializer() const {





 887   // For classfiles version 51 or greater, ensure that the clinit method is
 888   // static.  Non-static methods with the name "<clinit>" are not static
 889   // initializers. (older classfiles exempted for backward compatibility)
 890   return (name() == vmSymbols::class_initializer_name() &&
 891           (is_static() ||
 892            method_holder()->major_version() < 51));
 893 }
 894 
 895 // A method named <init>, is a classic object constructor.
 896 bool Method::is_object_constructor() const {
 897   return name() == vmSymbols::object_initializer_name();
 898 }
 899 
 900 bool Method::needs_clinit_barrier() const {
 901   return is_static() && !method_holder()->is_initialized();
 902 }
 903 
 904 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
 905   int length = method->checked_exceptions_length();
 906   if (length == 0) {  // common case
 907     return objArrayHandle(THREAD, Universe::the_empty_class_array());
 908   } else {
 909     methodHandle h_this(THREAD, method);
 910     objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
 911     objArrayHandle mirrors (THREAD, m_oop);
 912     for (int i = 0; i < length; i++) {
 913       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 914       Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
 915       if (log_is_enabled(Warning, exceptions) &&
 916           !k->is_subclass_of(vmClasses::Throwable_klass())) {
 917         ResourceMark rm(THREAD);
 918         log_warning(exceptions)(
 919           "Class %s in throws clause of method %s is not a subtype of class java.lang.Throwable",
 920           k->external_name(), method->external_name());
 921       }
 922       mirrors->obj_at_put(i, k->java_mirror());
 923     }
 924     return mirrors;
 925   }
 926 };
 927 
 928 
 929 int Method::line_number_from_bci(int bci) const {
 930   int best_bci  =  0;
 931   int best_line = -1;
 932   if (bci == SynchronizationEntryBCI) bci = 0;
 933   if (0 <= bci && bci < code_size() && has_linenumber_table()) {
 934     // The line numbers are a short array of 2-tuples [start_pc, line_number].
 935     // Not necessarily sorted and not necessarily one-to-one.
 936     CompressedLineNumberReadStream stream(compressed_linenumber_table());
 937     while (stream.read_pair()) {
 938       if (stream.bci() == bci) {
 939         // perfect match
 940         return stream.line();
 941       } else {
 942         // update best_bci/line
 943         if (stream.bci() < bci && stream.bci() >= best_bci) {
 944           best_bci  = stream.bci();
 945           best_line = stream.line();
 946         }
 947       }
 948     }
 949   }
 950   return best_line;
 951 }
 952 
 953 
 954 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
 955   if( constants()->tag_at(klass_index).is_unresolved_klass()) {
 956     Thread *thread = Thread::current();
 957     Symbol* klass_name = constants()->klass_name_at(klass_index);
 958     Handle loader(thread, method_holder()->class_loader());
 959     Handle prot  (thread, method_holder()->protection_domain());
 960     return SystemDictionary::find_instance_klass(thread, klass_name, loader, prot) != nullptr;
 961   } else {
 962     return true;
 963   }
 964 }
 965 
 966 
 967 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
 968   int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
 969   if (must_be_resolved) {
 970     // Make sure klass is resolved in constantpool.
 971     if (constants()->tag_at(klass_index).is_unresolved_klass()) {
 972       return false;
 973     }
 974   }
 975   return is_klass_loaded_by_klass_index(klass_index);
 976 }
 977 
 978 
 979 void Method::set_native_function(address function, bool post_event_flag) {
 980   assert(function != nullptr, "use clear_native_function to unregister natives");
 981   assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
 982   address* native_function = native_function_addr();
 983 
 984   // We can see racers trying to place the same native function into place. Once
 985   // is plenty.
 986   address current = *native_function;
 987   if (current == function) return;
 988   if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
 989       function != nullptr) {
 990     // native_method_throw_unsatisfied_link_error_entry() should only
 991     // be passed when post_event_flag is false.
 992     assert(function !=
 993       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
 994       "post_event_flag mismatch");
 995 
 996     // post the bind event, and possible change the bind function
 997     JvmtiExport::post_native_method_bind(this, &function);
 998   }
 999   *native_function = function;
1000   // This function can be called more than once. We must make sure that we always
1001   // use the latest registered method -> check if a stub already has been generated.
1002   // If so, we have to make it not_entrant.
1003   nmethod* nm = code(); // Put it into local variable to guard against concurrent updates
1004   if (nm != nullptr) {
1005     nm->make_not_entrant();
1006   }
1007 }
1008 
1009 
1010 bool Method::has_native_function() const {
1011   if (is_special_native_intrinsic())
1012     return false;  // special-cased in SharedRuntime::generate_native_wrapper
1013   address func = native_function();
1014   return (func != nullptr && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
1015 }
1016 
1017 
1018 void Method::clear_native_function() {
1019   // Note: is_method_handle_intrinsic() is allowed here.
1020   set_native_function(
1021     SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1022     !native_bind_event_is_interesting);
1023   this->unlink_code();
1024 }
1025 
1026 
1027 void Method::set_signature_handler(address handler) {
1028   address* signature_handler =  signature_handler_addr();
1029   *signature_handler = handler;
1030 }
1031 
1032 
1033 void Method::print_made_not_compilable(int comp_level, bool is_osr, bool report, const char* reason) {
1034   assert(reason != nullptr, "must provide a reason");
1035   if (PrintCompilation && report) {
1036     ttyLocker ttyl;
1037     tty->print("made not %scompilable on ", is_osr ? "OSR " : "");
1038     if (comp_level == CompLevel_all) {
1039       tty->print("all levels ");
1040     } else {
1041       tty->print("level %d ", comp_level);
1042     }
1043     this->print_short_name(tty);
1044     int size = this->code_size();
1045     if (size > 0) {
1046       tty->print(" (%d bytes)", size);
1047     }
1048     if (reason != nullptr) {
1049       tty->print("   %s", reason);
1050     }
1051     tty->cr();
1052   }
1053   if ((TraceDeoptimization || LogCompilation) && (xtty != nullptr)) {
1054     ttyLocker ttyl;
1055     xtty->begin_elem("make_not_compilable thread='" UINTX_FORMAT "' osr='%d' level='%d'",
1056                      os::current_thread_id(), is_osr, comp_level);
1057     if (reason != nullptr) {
1058       xtty->print(" reason=\'%s\'", reason);
1059     }
1060     xtty->method(this);
1061     xtty->stamp();
1062     xtty->end_elem();
1063   }
1064 }
1065 
1066 bool Method::is_always_compilable() const {
1067   // Generated adapters must be compiled
1068   if (is_special_native_intrinsic() && is_synthetic()) {
1069     assert(!is_not_c1_compilable(), "sanity check");
1070     assert(!is_not_c2_compilable(), "sanity check");
1071     return true;
1072   }
1073 
1074   return false;
1075 }
1076 
1077 bool Method::is_not_compilable(int comp_level) const {
1078   if (number_of_breakpoints() > 0)
1079     return true;
1080   if (is_always_compilable())
1081     return false;
1082   if (comp_level == CompLevel_any)
1083     return is_not_c1_compilable() && is_not_c2_compilable();
1084   if (is_c1_compile(comp_level))
1085     return is_not_c1_compilable();
1086   if (is_c2_compile(comp_level))
1087     return is_not_c2_compilable();
1088   return false;
1089 }
1090 
1091 // call this when compiler finds that this method is not compilable
1092 void Method::set_not_compilable(const char* reason, int comp_level, bool report) {
1093   if (is_always_compilable()) {
1094     // Don't mark a method which should be always compilable
1095     return;
1096   }
1097   print_made_not_compilable(comp_level, /*is_osr*/ false, report, reason);
1098   if (comp_level == CompLevel_all) {
1099     set_is_not_c1_compilable();
1100     set_is_not_c2_compilable();
1101   } else {
1102     if (is_c1_compile(comp_level))
1103       set_is_not_c1_compilable();
1104     if (is_c2_compile(comp_level))
1105       set_is_not_c2_compilable();
1106   }
1107   assert(!CompilationPolicy::can_be_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1108 }
1109 
1110 bool Method::is_not_osr_compilable(int comp_level) const {
1111   if (is_not_compilable(comp_level))
1112     return true;
1113   if (comp_level == CompLevel_any)
1114     return is_not_c1_osr_compilable() && is_not_c2_osr_compilable();
1115   if (is_c1_compile(comp_level))
1116     return is_not_c1_osr_compilable();
1117   if (is_c2_compile(comp_level))
1118     return is_not_c2_osr_compilable();
1119   return false;
1120 }
1121 
1122 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1123   print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1124   if (comp_level == CompLevel_all) {
1125     set_is_not_c1_osr_compilable();
1126     set_is_not_c2_osr_compilable();
1127   } else {
1128     if (is_c1_compile(comp_level))
1129       set_is_not_c1_osr_compilable();
1130     if (is_c2_compile(comp_level))
1131       set_is_not_c2_osr_compilable();
1132   }
1133   assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1134 }
1135 
1136 // Revert to using the interpreter and clear out the nmethod
1137 void Method::clear_code() {
1138   // this may be null if c2i adapters have not been made yet
1139   // Only should happen at allocate time.
1140   if (adapter() == nullptr) {
1141     _from_compiled_entry    = nullptr;
1142     _from_compiled_inline_entry = nullptr;
1143     _from_compiled_inline_ro_entry = nullptr;
1144   } else {
1145     _from_compiled_entry    = adapter()->get_c2i_entry();
1146     _from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1147     _from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1148   }
1149   OrderAccess::storestore();
1150   _from_interpreted_entry = _i2i_entry;
1151   OrderAccess::storestore();
1152   _code = nullptr;
1153 }
1154 
1155 void Method::unlink_code(nmethod *compare) {
1156   ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1157   // We need to check if either the _code or _from_compiled_code_entry_point
1158   // refer to this nmethod because there is a race in setting these two fields
1159   // in Method* as seen in bugid 4947125.
1160   if (code() == compare ||
1161       from_compiled_entry() == compare->verified_entry_point()) {
1162     clear_code();
1163   }
1164 }
1165 
1166 void Method::unlink_code() {
1167   ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1168   clear_code();
1169 }
1170 
1171 #if INCLUDE_CDS
1172 // Called by class data sharing to remove any entry points (which are not shared)
1173 void Method::unlink_method() {
1174   assert(CDSConfig::is_dumping_archive(), "sanity");
1175   _code = nullptr;
1176   _adapter = nullptr;
1177   _i2i_entry = nullptr;
1178   _from_compiled_entry = nullptr;
1179   _from_compiled_inline_entry = nullptr;
1180   _from_compiled_inline_ro_entry = nullptr;
1181   _from_interpreted_entry = nullptr;
1182 
1183   if (is_native()) {
1184     *native_function_addr() = nullptr;
1185     set_signature_handler(nullptr);
1186   }
1187   NOT_PRODUCT(set_compiled_invocation_count(0);)
1188 
1189   clear_method_data();
1190   clear_method_counters();
1191   remove_unshareable_flags();
1192 }
1193 
1194 void Method::remove_unshareable_flags() {
1195   // clear all the flags that shouldn't be in the archived version
1196   assert(!is_old(), "must be");
1197   assert(!is_obsolete(), "must be");
1198   assert(!is_deleted(), "must be");
1199 
1200   set_is_prefixed_native(false);
1201   set_queued_for_compilation(false);
1202   set_is_not_c2_compilable(false);
1203   set_is_not_c1_compilable(false);
1204   set_is_not_c2_osr_compilable(false);
1205   set_on_stack_flag(false);
1206   set_has_scalarized_args(false);
1207   set_has_scalarized_return(false);
1208 }
1209 #endif
1210 
1211 // Called when the method_holder is getting linked. Setup entrypoints so the method
1212 // is ready to be called from interpreter, compiler, and vtables.
1213 void Method::link_method(const methodHandle& h_method, TRAPS) {
1214   if (log_is_enabled(Info, perf, class, link)) {
1215     ClassLoader::perf_ik_link_methods_count()->inc();
1216   }
1217 
1218   // If the code cache is full, we may reenter this function for the
1219   // leftover methods that weren't linked.
1220   if (adapter() != nullptr) {
1221     return;
1222   }
1223   assert( _code == nullptr, "nothing compiled yet" );
1224 
1225   // Setup interpreter entrypoint
1226   assert(this == h_method(), "wrong h_method()" );
1227 
1228   assert(adapter() == nullptr, "init'd to null");
1229   address entry = Interpreter::entry_for_method(h_method);
1230   assert(entry != nullptr, "interpreter entry must be non-null");
1231   // Sets both _i2i_entry and _from_interpreted_entry
1232   set_interpreter_entry(entry);
1233 
1234   // Don't overwrite already registered native entries.
1235   if (is_native() && !has_native_function()) {
1236     set_native_function(
1237       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1238       !native_bind_event_is_interesting);
1239   }
1240   if (InlineTypeReturnedAsFields && returns_inline_type(THREAD) && !has_scalarized_return()) {
1241     set_has_scalarized_return();
1242   }
1243 
1244   // Setup compiler entrypoint.  This is made eagerly, so we do not need
1245   // special handling of vtables.  An alternative is to make adapters more
1246   // lazily by calling make_adapter() from from_compiled_entry() for the
1247   // normal calls.  For vtable calls life gets more complicated.  When a
1248   // call-site goes mega-morphic we need adapters in all methods which can be
1249   // called from the vtable.  We need adapters on such methods that get loaded
1250   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
1251   // problem we'll make these lazily later.
1252   (void) make_adapters(h_method, CHECK);
1253 
1254   // ONLY USE the h_method now as make_adapter may have blocked
1255 
1256   if (h_method->is_continuation_native_intrinsic()) {
1257     _from_interpreted_entry = nullptr;
1258     _from_compiled_entry = nullptr;
1259     _i2i_entry = nullptr;
1260     if (Continuations::enabled()) {
1261       assert(!Threads::is_vm_complete(), "should only be called during vm init");
1262       AdapterHandlerLibrary::create_native_wrapper(h_method);
1263       if (!h_method->has_compiled_code()) {
1264         THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Initial size of CodeCache is too small");
1265       }
1266       assert(_from_interpreted_entry == get_i2c_entry(), "invariant");
1267     }
1268   }
1269 }
1270 
1271 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1272   PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1273 
1274   // Adapters for compiled code are made eagerly here.  They are fairly
1275   // small (generally < 100 bytes) and quick to make (and cached and shared)
1276   // so making them eagerly shouldn't be too expensive.
1277   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1278   if (adapter == nullptr ) {
1279     if (!is_init_completed()) {
1280       // Don't throw exceptions during VM initialization because java.lang.* classes
1281       // might not have been initialized, causing problems when constructing the
1282       // Java exception object.
1283       vm_exit_during_initialization("Out of space in CodeCache for adapters");
1284     } else {
1285       THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1286     }
1287   }
1288 
1289   mh->set_adapter_entry(adapter);
1290   mh->_from_compiled_entry = adapter->get_c2i_entry();
1291   mh->_from_compiled_inline_entry = adapter->get_c2i_inline_entry();
1292   mh->_from_compiled_inline_ro_entry = adapter->get_c2i_inline_ro_entry();
1293   return adapter->get_c2i_entry();
1294 }
1295 
1296 // The verified_code_entry() must be called when a invoke is resolved
1297 // on this method.
1298 
1299 // It returns the compiled code entry point, after asserting not null.
1300 // This function is called after potential safepoints so that nmethod
1301 // or adapter that it points to is still live and valid.
1302 // This function must not hit a safepoint!
1303 address Method::verified_code_entry() {
1304   debug_only(NoSafepointVerifier nsv;)
1305   assert(_from_compiled_entry != nullptr, "must be set");
1306   return _from_compiled_entry;
1307 }
1308 
1309 address Method::verified_inline_code_entry() {
1310   debug_only(NoSafepointVerifier nsv;)
1311   assert(_from_compiled_inline_entry != nullptr, "must be set");
1312   return _from_compiled_inline_entry;
1313 }
1314 
1315 address Method::verified_inline_ro_code_entry() {
1316   debug_only(NoSafepointVerifier nsv;)
1317   assert(_from_compiled_inline_ro_entry != nullptr, "must be set");
1318   return _from_compiled_inline_ro_entry;
1319 }
1320 
1321 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1322 // (could be racing a deopt).
1323 // Not inline to avoid circular ref.
1324 bool Method::check_code() const {
1325   // cached in a register or local.  There's a race on the value of the field.
1326   nmethod *code = Atomic::load_acquire(&_code);
1327   return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1328 }
1329 
1330 // Install compiled code.  Instantly it can execute.
1331 void Method::set_code(const methodHandle& mh, nmethod *code) {
1332   assert_lock_strong(NMethodState_lock);
1333   assert( code, "use clear_code to remove code" );
1334   assert( mh->check_code(), "" );
1335 
1336   guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1337 
1338   // These writes must happen in this order, because the interpreter will
1339   // directly jump to from_interpreted_entry which jumps to an i2c adapter
1340   // which jumps to _from_compiled_entry.
1341   mh->_code = code;             // Assign before allowing compiled code to exec
1342 
1343   int comp_level = code->comp_level();
1344   // In theory there could be a race here. In practice it is unlikely
1345   // and not worth worrying about.
1346   if (comp_level > mh->highest_comp_level()) {
1347     mh->set_highest_comp_level(comp_level);
1348   }
1349 
1350   OrderAccess::storestore();
1351   mh->_from_compiled_entry = code->verified_entry_point();
1352   mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1353   mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1354   OrderAccess::storestore();
1355 
1356   if (mh->is_continuation_native_intrinsic()) {
1357     assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1358 
1359     if (mh->is_continuation_enter_intrinsic()) {
1360       // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1361       mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1362     } else if (mh->is_continuation_yield_intrinsic()) {
1363       mh->_i2i_entry = mh->get_i2c_entry();
1364     } else {
1365       guarantee(false, "Unknown Continuation native intrinsic");
1366     }
1367     // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1368     Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1369   } else if (!mh->is_method_handle_intrinsic()) {
1370     // Instantly compiled code can execute.
1371     mh->_from_interpreted_entry = mh->get_i2c_entry();
1372   }
1373 }
1374 
1375 
1376 bool Method::is_overridden_in(Klass* k) const {
1377   InstanceKlass* ik = InstanceKlass::cast(k);
1378 
1379   if (ik->is_interface()) return false;
1380 
1381   // If method is an interface, we skip it - except if it
1382   // is a miranda method
1383   if (method_holder()->is_interface()) {
1384     // Check that method is not a miranda method
1385     if (ik->lookup_method(name(), signature()) == nullptr) {
1386       // No implementation exist - so miranda method
1387       return false;
1388     }
1389     return true;
1390   }
1391 
1392   assert(ik->is_subclass_of(method_holder()), "should be subklass");
1393   if (!has_vtable_index()) {
1394     return false;
1395   } else {
1396     Method* vt_m = ik->method_at_vtable(vtable_index());
1397     return vt_m != this;
1398   }
1399 }
1400 
1401 
1402 // give advice about whether this Method* should be cached or not
1403 bool Method::should_not_be_cached() const {
1404   if (is_old()) {
1405     // This method has been redefined. It is either EMCP or obsolete
1406     // and we don't want to cache it because that would pin the method
1407     // down and prevent it from being collectible if and when it
1408     // finishes executing.
1409     return true;
1410   }
1411 
1412   // caching this method should be just fine
1413   return false;
1414 }
1415 
1416 
1417 /**
1418  *  Returns true if this is one of the specially treated methods for
1419  *  security related stack walks (like Reflection.getCallerClass).
1420  */
1421 bool Method::is_ignored_by_security_stack_walk() const {
1422   if (intrinsic_id() == vmIntrinsics::_invoke) {
1423     // This is Method.invoke() -- ignore it
1424     return true;
1425   }
1426   if (method_holder()->is_subclass_of(vmClasses::reflect_MethodAccessorImpl_klass())) {
1427     // This is an auxiliary frame -- ignore it
1428     return true;
1429   }
1430   if (is_method_handle_intrinsic() || is_compiled_lambda_form()) {
1431     // This is an internal adapter frame for method handles -- ignore it
1432     return true;
1433   }
1434   return false;
1435 }
1436 
1437 
1438 // Constant pool structure for invoke methods:
1439 enum {
1440   _imcp_invoke_name = 1,        // utf8: 'invokeExact', etc.
1441   _imcp_invoke_signature,       // utf8: (variable Symbol*)
1442   _imcp_limit
1443 };
1444 
1445 // Test if this method is an MH adapter frame generated by Java code.
1446 // Cf. java/lang/invoke/InvokerBytecodeGenerator
1447 bool Method::is_compiled_lambda_form() const {
1448   return intrinsic_id() == vmIntrinsics::_compiledLambdaForm;
1449 }
1450 
1451 // Test if this method is an internal MH primitive method.
1452 bool Method::is_method_handle_intrinsic() const {
1453   vmIntrinsics::ID iid = intrinsic_id();
1454   return (MethodHandles::is_signature_polymorphic(iid) &&
1455           MethodHandles::is_signature_polymorphic_intrinsic(iid));
1456 }
1457 
1458 bool Method::has_member_arg() const {
1459   vmIntrinsics::ID iid = intrinsic_id();
1460   return (MethodHandles::is_signature_polymorphic(iid) &&
1461           MethodHandles::has_member_arg(iid));
1462 }
1463 
1464 // Make an instance of a signature-polymorphic internal MH primitive.
1465 methodHandle Method::make_method_handle_intrinsic(vmIntrinsics::ID iid,
1466                                                          Symbol* signature,
1467                                                          TRAPS) {
1468   ResourceMark rm(THREAD);
1469   methodHandle empty;
1470 
1471   InstanceKlass* holder = vmClasses::MethodHandle_klass();
1472   Symbol* name = MethodHandles::signature_polymorphic_intrinsic_name(iid);
1473   assert(iid == MethodHandles::signature_polymorphic_name_id(name), "");
1474 
1475   log_info(methodhandles)("make_method_handle_intrinsic MH.%s%s", name->as_C_string(), signature->as_C_string());
1476 
1477   // invariant:   cp->symbol_at_put is preceded by a refcount increment (more usually a lookup)
1478   name->increment_refcount();
1479   signature->increment_refcount();
1480 
1481   int cp_length = _imcp_limit;
1482   ClassLoaderData* loader_data = holder->class_loader_data();
1483   constantPoolHandle cp;
1484   {
1485     ConstantPool* cp_oop = ConstantPool::allocate(loader_data, cp_length, CHECK_(empty));
1486     cp = constantPoolHandle(THREAD, cp_oop);
1487   }
1488   cp->copy_fields(holder->constants());
1489   cp->set_pool_holder(holder);
1490   cp->symbol_at_put(_imcp_invoke_name,       name);
1491   cp->symbol_at_put(_imcp_invoke_signature,  signature);
1492   cp->set_has_preresolution();
1493 
1494   // decide on access bits:  public or not?
1495   int flags_bits = (JVM_ACC_NATIVE | JVM_ACC_SYNTHETIC | JVM_ACC_FINAL);
1496   bool must_be_static = MethodHandles::is_signature_polymorphic_static(iid);
1497   if (must_be_static)  flags_bits |= JVM_ACC_STATIC;
1498   assert((flags_bits & JVM_ACC_PUBLIC) == 0, "do not expose these methods");
1499 
1500   methodHandle m;
1501   {
1502     InlineTableSizes sizes;
1503     Method* m_oop = Method::allocate(loader_data, 0,
1504                                      accessFlags_from(flags_bits), &sizes,
1505                                      ConstMethod::NORMAL,
1506                                      name,
1507                                      CHECK_(empty));
1508     m = methodHandle(THREAD, m_oop);
1509   }
1510   m->set_constants(cp());
1511   m->set_name_index(_imcp_invoke_name);
1512   m->set_signature_index(_imcp_invoke_signature);
1513   assert(MethodHandles::is_signature_polymorphic_name(m->name()), "");
1514   assert(m->signature() == signature, "");
1515   m->constMethod()->compute_from_signature(signature, must_be_static);
1516   m->init_intrinsic_id(klass_id_for_intrinsics(m->method_holder()));
1517   assert(m->is_method_handle_intrinsic(), "");
1518 #ifdef ASSERT
1519   if (!MethodHandles::is_signature_polymorphic(m->intrinsic_id()))  m->print();
1520   assert(MethodHandles::is_signature_polymorphic(m->intrinsic_id()), "must be an invoker");
1521   assert(m->intrinsic_id() == iid, "correctly predicted iid");
1522 #endif //ASSERT
1523 
1524   // Finally, set up its entry points.
1525   assert(m->can_be_statically_bound(), "");
1526   m->set_vtable_index(Method::nonvirtual_vtable_index);
1527   m->link_method(m, CHECK_(empty));
1528 
1529   if (iid == vmIntrinsics::_linkToNative) {
1530     m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1531   }
1532   if (log_is_enabled(Debug, methodhandles)) {
1533     LogTarget(Debug, methodhandles) lt;
1534     LogStream ls(lt);
1535     m->print_on(&ls);
1536   }
1537 
1538   return m;
1539 }
1540 
1541 Klass* Method::check_non_bcp_klass(Klass* klass) {
1542   if (klass != nullptr && klass->class_loader() != nullptr) {
1543     if (klass->is_objArray_klass())
1544       klass = ObjArrayKlass::cast(klass)->bottom_klass();
1545     return klass;
1546   }
1547   return nullptr;
1548 }
1549 
1550 
1551 methodHandle Method::clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,
1552                                                 u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS) {
1553   // Code below does not work for native methods - they should never get rewritten anyway
1554   assert(!m->is_native(), "cannot rewrite native methods");
1555   // Allocate new Method*
1556   AccessFlags flags = m->access_flags();
1557 
1558   ConstMethod* cm = m->constMethod();
1559   int checked_exceptions_len = cm->checked_exceptions_length();
1560   int localvariable_len = cm->localvariable_table_length();
1561   int exception_table_len = cm->exception_table_length();
1562   int method_parameters_len = cm->method_parameters_length();
1563   int method_annotations_len = cm->method_annotations_length();
1564   int parameter_annotations_len = cm->parameter_annotations_length();
1565   int type_annotations_len = cm->type_annotations_length();
1566   int default_annotations_len = cm->default_annotations_length();
1567 
1568   InlineTableSizes sizes(
1569       localvariable_len,
1570       new_compressed_linenumber_size,
1571       exception_table_len,
1572       checked_exceptions_len,
1573       method_parameters_len,
1574       cm->generic_signature_index(),
1575       method_annotations_len,
1576       parameter_annotations_len,
1577       type_annotations_len,
1578       default_annotations_len,
1579       0);
1580 
1581   ClassLoaderData* loader_data = m->method_holder()->class_loader_data();
1582   Method* newm_oop = Method::allocate(loader_data,
1583                                       new_code_length,
1584                                       flags,
1585                                       &sizes,
1586                                       m->method_type(),
1587                                       m->name(),
1588                                       CHECK_(methodHandle()));
1589   methodHandle newm (THREAD, newm_oop);
1590 
1591   // Create a shallow copy of Method part, but be careful to preserve the new ConstMethod*
1592   ConstMethod* newcm = newm->constMethod();
1593   int new_const_method_size = newm->constMethod()->size();
1594 
1595   // This works because the source and target are both Methods. Some compilers
1596   // (e.g., clang) complain that the target vtable pointer will be stomped,
1597   // so cast away newm()'s and m()'s Methodness.
1598   memcpy((void*)newm(), (void*)m(), sizeof(Method));
1599 
1600   // Create shallow copy of ConstMethod.
1601   memcpy(newcm, m->constMethod(), sizeof(ConstMethod));
1602 
1603   // Reset correct method/const method, method size, and parameter info
1604   newm->set_constMethod(newcm);
1605   newm->constMethod()->set_code_size(new_code_length);
1606   newm->constMethod()->set_constMethod_size(new_const_method_size);
1607   assert(newm->code_size() == new_code_length, "check");
1608   assert(newm->method_parameters_length() == method_parameters_len, "check");
1609   assert(newm->checked_exceptions_length() == checked_exceptions_len, "check");
1610   assert(newm->exception_table_length() == exception_table_len, "check");
1611   assert(newm->localvariable_table_length() == localvariable_len, "check");
1612   // Copy new byte codes
1613   memcpy(newm->code_base(), new_code, new_code_length);
1614   // Copy line number table
1615   if (new_compressed_linenumber_size > 0) {
1616     memcpy(newm->compressed_linenumber_table(),
1617            new_compressed_linenumber_table,
1618            new_compressed_linenumber_size);
1619   }
1620   // Copy method_parameters
1621   if (method_parameters_len > 0) {
1622     memcpy(newm->method_parameters_start(),
1623            m->method_parameters_start(),
1624            method_parameters_len * sizeof(MethodParametersElement));
1625   }
1626   // Copy checked_exceptions
1627   if (checked_exceptions_len > 0) {
1628     memcpy(newm->checked_exceptions_start(),
1629            m->checked_exceptions_start(),
1630            checked_exceptions_len * sizeof(CheckedExceptionElement));
1631   }
1632   // Copy exception table
1633   if (exception_table_len > 0) {
1634     memcpy(newm->exception_table_start(),
1635            m->exception_table_start(),
1636            exception_table_len * sizeof(ExceptionTableElement));
1637   }
1638   // Copy local variable number table
1639   if (localvariable_len > 0) {
1640     memcpy(newm->localvariable_table_start(),
1641            m->localvariable_table_start(),
1642            localvariable_len * sizeof(LocalVariableTableElement));
1643   }
1644   // Copy stackmap table
1645   if (m->has_stackmap_table()) {
1646     int code_attribute_length = m->stackmap_data()->length();
1647     Array<u1>* stackmap_data =
1648       MetadataFactory::new_array<u1>(loader_data, code_attribute_length, 0, CHECK_(methodHandle()));
1649     memcpy((void*)stackmap_data->adr_at(0),
1650            (void*)m->stackmap_data()->adr_at(0), code_attribute_length);
1651     newm->set_stackmap_data(stackmap_data);
1652   }
1653 
1654   // copy annotations over to new method
1655   newcm->copy_annotations_from(loader_data, cm, CHECK_(methodHandle()));
1656   return newm;
1657 }
1658 
1659 vmSymbolID Method::klass_id_for_intrinsics(const Klass* holder) {
1660   // if loader is not the default loader (i.e., non-null), we can't know the intrinsics
1661   // because we are not loading from core libraries
1662   // exception: the AES intrinsics come from lib/ext/sunjce_provider.jar
1663   // which does not use the class default class loader so we check for its loader here
1664   const InstanceKlass* ik = InstanceKlass::cast(holder);
1665   if ((ik->class_loader() != nullptr) && !SystemDictionary::is_platform_class_loader(ik->class_loader())) {
1666     return vmSymbolID::NO_SID;   // regardless of name, no intrinsics here
1667   }
1668 
1669   // see if the klass name is well-known:
1670   Symbol* klass_name = ik->name();
1671   vmSymbolID id = vmSymbols::find_sid(klass_name);
1672   if (id != vmSymbolID::NO_SID && vmIntrinsics::class_has_intrinsics(id)) {
1673     return id;
1674   } else {
1675     return vmSymbolID::NO_SID;
1676   }
1677 }
1678 
1679 void Method::init_intrinsic_id(vmSymbolID klass_id) {
1680   assert(_intrinsic_id == static_cast<int>(vmIntrinsics::_none), "do this just once");
1681   const uintptr_t max_id_uint = right_n_bits((int)(sizeof(_intrinsic_id) * BitsPerByte));
1682   assert((uintptr_t)vmIntrinsics::ID_LIMIT <= max_id_uint, "else fix size");
1683   assert(intrinsic_id_size_in_bytes() == sizeof(_intrinsic_id), "");
1684 
1685   // the klass name is well-known:
1686   assert(klass_id == klass_id_for_intrinsics(method_holder()), "must be");
1687   assert(klass_id != vmSymbolID::NO_SID, "caller responsibility");
1688 
1689   // ditto for method and signature:
1690   vmSymbolID name_id = vmSymbols::find_sid(name());
1691   if (klass_id != VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle)
1692       && klass_id != VM_SYMBOL_ENUM_NAME(java_lang_invoke_VarHandle)
1693       && name_id == vmSymbolID::NO_SID) {
1694     return;
1695   }
1696   vmSymbolID sig_id = vmSymbols::find_sid(signature());
1697   if (klass_id != VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle)
1698       && klass_id != VM_SYMBOL_ENUM_NAME(java_lang_invoke_VarHandle)
1699       && sig_id == vmSymbolID::NO_SID) {
1700     return;
1701   }
1702   jshort flags = access_flags().as_short();
1703 
1704   vmIntrinsics::ID id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags);
1705   if (id != vmIntrinsics::_none) {
1706     set_intrinsic_id(id);
1707     if (id == vmIntrinsics::_Class_cast) {
1708       // Even if the intrinsic is rejected, we want to inline this simple method.
1709       set_force_inline();
1710     }
1711     return;
1712   }
1713 
1714   // A few slightly irregular cases:
1715   switch (klass_id) {
1716   // Signature-polymorphic methods: MethodHandle.invoke*, InvokeDynamic.*., VarHandle
1717   case VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle):
1718   case VM_SYMBOL_ENUM_NAME(java_lang_invoke_VarHandle):
1719     if (!is_native())  break;
1720     id = MethodHandles::signature_polymorphic_name_id(method_holder(), name());
1721     if (is_static() != MethodHandles::is_signature_polymorphic_static(id))
1722       id = vmIntrinsics::_none;
1723     break;
1724 
1725   default:
1726     break;
1727   }
1728 
1729   if (id != vmIntrinsics::_none) {
1730     // Set up its iid.  It is an alias method.
1731     set_intrinsic_id(id);
1732     return;
1733   }
1734 }
1735 
1736 bool Method::load_signature_classes(const methodHandle& m, TRAPS) {
1737   if (!THREAD->can_call_java()) {
1738     // There is nothing useful this routine can do from within the Compile thread.
1739     // Hopefully, the signature contains only well-known classes.
1740     // We could scan for this and return true/false, but the caller won't care.
1741     return false;
1742   }
1743   bool sig_is_loaded = true;
1744   ResourceMark rm(THREAD);
1745   for (ResolvingSignatureStream ss(m()); !ss.is_done(); ss.next()) {
1746     if (ss.is_reference()) {
1747       // load everything, including arrays "[Lfoo;"
1748       Klass* klass = ss.as_klass(SignatureStream::ReturnNull, THREAD);
1749       // We are loading classes eagerly. If a ClassNotFoundException or
1750       // a LinkageError was generated, be sure to ignore it.
1751       if (HAS_PENDING_EXCEPTION) {
1752         if (PENDING_EXCEPTION->is_a(vmClasses::ClassNotFoundException_klass()) ||
1753             PENDING_EXCEPTION->is_a(vmClasses::LinkageError_klass())) {
1754           CLEAR_PENDING_EXCEPTION;
1755         } else {
1756           return false;
1757         }
1758       }
1759       if( klass == nullptr) { sig_is_loaded = false; }
1760     }
1761   }
1762   return sig_is_loaded;
1763 }
1764 
1765 // Exposed so field engineers can debug VM
1766 void Method::print_short_name(outputStream* st) const {
1767   ResourceMark rm;
1768 #ifdef PRODUCT
1769   st->print(" %s::", method_holder()->external_name());
1770 #else
1771   st->print(" %s::", method_holder()->internal_name());
1772 #endif
1773   name()->print_symbol_on(st);
1774   if (WizardMode) signature()->print_symbol_on(st);
1775   else if (MethodHandles::is_signature_polymorphic(intrinsic_id()))
1776     MethodHandles::print_as_basic_type_signature_on(st, signature());
1777 }
1778 
1779 // Comparer for sorting an object array containing
1780 // Method*s.
1781 static int method_comparator(Method* a, Method* b) {
1782   return a->name()->fast_compare(b->name());
1783 }
1784 
1785 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array
1786 // default_methods also uses this without the ordering for fast find_method
1787 void Method::sort_methods(Array<Method*>* methods, bool set_idnums, method_comparator_func func) {
1788   int length = methods->length();
1789   if (length > 1) {
1790     if (func == nullptr) {
1791       func = method_comparator;
1792     }
1793     {
1794       NoSafepointVerifier nsv;
1795       QuickSort::sort(methods->data(), length, func);
1796     }
1797     // Reset method ordering
1798     if (set_idnums) {
1799       for (u2 i = 0; i < length; i++) {
1800         Method* m = methods->at(i);
1801         m->set_method_idnum(i);
1802         m->set_orig_method_idnum(i);
1803       }
1804     }
1805   }
1806 }
1807 
1808 //-----------------------------------------------------------------------------------
1809 // Non-product code unless JVM/TI needs it
1810 
1811 #if !defined(PRODUCT) || INCLUDE_JVMTI
1812 class SignatureTypePrinter : public SignatureTypeNames {
1813  private:
1814   outputStream* _st;
1815   bool _use_separator;
1816 
1817   void type_name(const char* name) {
1818     if (_use_separator) _st->print(", ");
1819     _st->print("%s", name);
1820     _use_separator = true;
1821   }
1822 
1823  public:
1824   SignatureTypePrinter(Symbol* signature, outputStream* st) : SignatureTypeNames(signature) {
1825     _st = st;
1826     _use_separator = false;
1827   }
1828 
1829   void print_parameters()              { _use_separator = false; do_parameters_on(this); }
1830   void print_returntype()              { _use_separator = false; do_type(return_type()); }
1831 };
1832 
1833 
1834 void Method::print_name(outputStream* st) const {
1835   Thread *thread = Thread::current();
1836   ResourceMark rm(thread);
1837   st->print("%s ", is_static() ? "static" : "virtual");
1838   if (WizardMode) {
1839     st->print("%s.", method_holder()->internal_name());
1840     name()->print_symbol_on(st);
1841     signature()->print_symbol_on(st);
1842   } else {
1843     SignatureTypePrinter sig(signature(), st);
1844     sig.print_returntype();
1845     st->print(" %s.", method_holder()->internal_name());
1846     name()->print_symbol_on(st);
1847     st->print("(");
1848     sig.print_parameters();
1849     st->print(")");
1850   }
1851 }
1852 #endif // !PRODUCT || INCLUDE_JVMTI
1853 
1854 
1855 void Method::print_codes_on(outputStream* st, int flags) const {
1856   print_codes_on(0, code_size(), st, flags);
1857 }
1858 
1859 void Method::print_codes_on(int from, int to, outputStream* st, int flags) const {
1860   Thread *thread = Thread::current();
1861   ResourceMark rm(thread);
1862   methodHandle mh (thread, (Method*)this);
1863   BytecodeTracer::print_method_codes(mh, from, to, st, flags);
1864 }
1865 
1866 CompressedLineNumberReadStream::CompressedLineNumberReadStream(u_char* buffer) : CompressedReadStream(buffer) {
1867   _bci = 0;
1868   _line = 0;
1869 };
1870 
1871 bool CompressedLineNumberReadStream::read_pair() {
1872   jubyte next = read_byte();
1873   // Check for terminator
1874   if (next == 0) return false;
1875   if (next == 0xFF) {
1876     // Escape character, regular compression used
1877     _bci  += read_signed_int();
1878     _line += read_signed_int();
1879   } else {
1880     // Single byte compression used
1881     _bci  += next >> 3;
1882     _line += next & 0x7;
1883   }
1884   return true;
1885 }
1886 
1887 #if INCLUDE_JVMTI
1888 
1889 Bytecodes::Code Method::orig_bytecode_at(int bci) const {
1890   BreakpointInfo* bp = method_holder()->breakpoints();
1891   for (; bp != nullptr; bp = bp->next()) {
1892     if (bp->match(this, bci)) {
1893       return bp->orig_bytecode();
1894     }
1895   }
1896   {
1897     ResourceMark rm;
1898     fatal("no original bytecode found in %s at bci %d", name_and_sig_as_C_string(), bci);
1899   }
1900   return Bytecodes::_shouldnotreachhere;
1901 }
1902 
1903 void Method::set_orig_bytecode_at(int bci, Bytecodes::Code code) {
1904   assert(code != Bytecodes::_breakpoint, "cannot patch breakpoints this way");
1905   BreakpointInfo* bp = method_holder()->breakpoints();
1906   for (; bp != nullptr; bp = bp->next()) {
1907     if (bp->match(this, bci)) {
1908       bp->set_orig_bytecode(code);
1909       // and continue, in case there is more than one
1910     }
1911   }
1912 }
1913 
1914 void Method::set_breakpoint(int bci) {
1915   InstanceKlass* ik = method_holder();
1916   BreakpointInfo *bp = new BreakpointInfo(this, bci);
1917   bp->set_next(ik->breakpoints());
1918   ik->set_breakpoints(bp);
1919   // do this last:
1920   bp->set(this);
1921 }
1922 
1923 static void clear_matches(Method* m, int bci) {
1924   InstanceKlass* ik = m->method_holder();
1925   BreakpointInfo* prev_bp = nullptr;
1926   BreakpointInfo* next_bp;
1927   for (BreakpointInfo* bp = ik->breakpoints(); bp != nullptr; bp = next_bp) {
1928     next_bp = bp->next();
1929     // bci value of -1 is used to delete all breakpoints in method m (ex: clear_all_breakpoint).
1930     if (bci >= 0 ? bp->match(m, bci) : bp->match(m)) {
1931       // do this first:
1932       bp->clear(m);
1933       // unhook it
1934       if (prev_bp != nullptr)
1935         prev_bp->set_next(next_bp);
1936       else
1937         ik->set_breakpoints(next_bp);
1938       delete bp;
1939       // When class is redefined JVMTI sets breakpoint in all versions of EMCP methods
1940       // at same location. So we have multiple matching (method_index and bci)
1941       // BreakpointInfo nodes in BreakpointInfo list. We should just delete one
1942       // breakpoint for clear_breakpoint request and keep all other method versions
1943       // BreakpointInfo for future clear_breakpoint request.
1944       // bcivalue of -1 is used to clear all breakpoints (see clear_all_breakpoints)
1945       // which is being called when class is unloaded. We delete all the Breakpoint
1946       // information for all versions of method. We may not correctly restore the original
1947       // bytecode in all method versions, but that is ok. Because the class is being unloaded
1948       // so these methods won't be used anymore.
1949       if (bci >= 0) {
1950         break;
1951       }
1952     } else {
1953       // This one is a keeper.
1954       prev_bp = bp;
1955     }
1956   }
1957 }
1958 
1959 void Method::clear_breakpoint(int bci) {
1960   assert(bci >= 0, "");
1961   clear_matches(this, bci);
1962 }
1963 
1964 void Method::clear_all_breakpoints() {
1965   clear_matches(this, -1);
1966 }
1967 
1968 #endif // INCLUDE_JVMTI
1969 
1970 int Method::invocation_count() const {
1971   MethodCounters* mcs = method_counters();
1972   MethodData* mdo = method_data();
1973   if (((mcs != nullptr) ? mcs->invocation_counter()->carry() : false) ||
1974       ((mdo != nullptr) ? mdo->invocation_counter()->carry() : false)) {
1975     return InvocationCounter::count_limit;
1976   } else {
1977     return ((mcs != nullptr) ? mcs->invocation_counter()->count() : 0) +
1978            ((mdo != nullptr) ? mdo->invocation_counter()->count() : 0);
1979   }
1980 }
1981 
1982 int Method::backedge_count() const {
1983   MethodCounters* mcs = method_counters();
1984   MethodData* mdo = method_data();
1985   if (((mcs != nullptr) ? mcs->backedge_counter()->carry() : false) ||
1986       ((mdo != nullptr) ? mdo->backedge_counter()->carry() : false)) {
1987     return InvocationCounter::count_limit;
1988   } else {
1989     return ((mcs != nullptr) ? mcs->backedge_counter()->count() : 0) +
1990            ((mdo != nullptr) ? mdo->backedge_counter()->count() : 0);
1991   }
1992 }
1993 
1994 int Method::highest_comp_level() const {
1995   const MethodCounters* mcs = method_counters();
1996   if (mcs != nullptr) {
1997     return mcs->highest_comp_level();
1998   } else {
1999     return CompLevel_none;
2000   }
2001 }
2002 
2003 int Method::highest_osr_comp_level() const {
2004   const MethodCounters* mcs = method_counters();
2005   if (mcs != nullptr) {
2006     return mcs->highest_osr_comp_level();
2007   } else {
2008     return CompLevel_none;
2009   }
2010 }
2011 
2012 void Method::set_highest_comp_level(int level) {
2013   MethodCounters* mcs = method_counters();
2014   if (mcs != nullptr) {
2015     mcs->set_highest_comp_level(level);
2016   }
2017 }
2018 
2019 void Method::set_highest_osr_comp_level(int level) {
2020   MethodCounters* mcs = method_counters();
2021   if (mcs != nullptr) {
2022     mcs->set_highest_osr_comp_level(level);
2023   }
2024 }
2025 
2026 #if INCLUDE_JVMTI
2027 
2028 BreakpointInfo::BreakpointInfo(Method* m, int bci) {
2029   _bci = bci;
2030   _name_index = m->name_index();
2031   _signature_index = m->signature_index();
2032   _orig_bytecode = (Bytecodes::Code) *m->bcp_from(_bci);
2033   if (_orig_bytecode == Bytecodes::_breakpoint)
2034     _orig_bytecode = m->orig_bytecode_at(_bci);
2035   _next = nullptr;
2036 }
2037 
2038 void BreakpointInfo::set(Method* method) {
2039 #ifdef ASSERT
2040   {
2041     Bytecodes::Code code = (Bytecodes::Code) *method->bcp_from(_bci);
2042     if (code == Bytecodes::_breakpoint)
2043       code = method->orig_bytecode_at(_bci);
2044     assert(orig_bytecode() == code, "original bytecode must be the same");
2045   }
2046 #endif
2047   Thread *thread = Thread::current();
2048   *method->bcp_from(_bci) = Bytecodes::_breakpoint;
2049   method->incr_number_of_breakpoints(thread);
2050   {
2051     // Deoptimize all dependents on this method
2052     HandleMark hm(thread);
2053     methodHandle mh(thread, method);
2054     CodeCache::mark_dependents_on_method_for_breakpoint(mh);
2055   }
2056 }
2057 
2058 void BreakpointInfo::clear(Method* method) {
2059   *method->bcp_from(_bci) = orig_bytecode();
2060   assert(method->number_of_breakpoints() > 0, "must not go negative");
2061   method->decr_number_of_breakpoints(Thread::current());
2062 }
2063 
2064 #endif // INCLUDE_JVMTI
2065 
2066 // jmethodID handling
2067 
2068 // This is a block allocating object, sort of like JNIHandleBlock, only a
2069 // lot simpler.
2070 // It's allocated on the CHeap because once we allocate a jmethodID, we can
2071 // never get rid of it.
2072 
2073 static const int min_block_size = 8;
2074 
2075 class JNIMethodBlockNode : public CHeapObj<mtClass> {
2076   friend class JNIMethodBlock;
2077   Method**        _methods;
2078   int             _number_of_methods;
2079   int             _top;
2080   JNIMethodBlockNode* _next;
2081 
2082  public:
2083 
2084   JNIMethodBlockNode(int num_methods = min_block_size);
2085 
2086   ~JNIMethodBlockNode() { FREE_C_HEAP_ARRAY(Method*, _methods); }
2087 
2088   void ensure_methods(int num_addl_methods) {
2089     if (_top < _number_of_methods) {
2090       num_addl_methods -= _number_of_methods - _top;
2091       if (num_addl_methods <= 0) {
2092         return;
2093       }
2094     }
2095     if (_next == nullptr) {
2096       _next = new JNIMethodBlockNode(MAX2(num_addl_methods, min_block_size));
2097     } else {
2098       _next->ensure_methods(num_addl_methods);
2099     }
2100   }
2101 };
2102 
2103 class JNIMethodBlock : public CHeapObj<mtClass> {
2104   JNIMethodBlockNode _head;
2105   JNIMethodBlockNode *_last_free;
2106  public:
2107   static Method* const _free_method;
2108 
2109   JNIMethodBlock(int initial_capacity = min_block_size)
2110       : _head(initial_capacity), _last_free(&_head) {}
2111 
2112   void ensure_methods(int num_addl_methods) {
2113     _last_free->ensure_methods(num_addl_methods);
2114   }
2115 
2116   Method** add_method(Method* m) {
2117     for (JNIMethodBlockNode* b = _last_free; b != nullptr; b = b->_next) {
2118       if (b->_top < b->_number_of_methods) {
2119         // top points to the next free entry.
2120         int i = b->_top;
2121         b->_methods[i] = m;
2122         b->_top++;
2123         _last_free = b;
2124         return &(b->_methods[i]);
2125       } else if (b->_top == b->_number_of_methods) {
2126         // if the next free entry ran off the block see if there's a free entry
2127         for (int i = 0; i < b->_number_of_methods; i++) {
2128           if (b->_methods[i] == _free_method) {
2129             b->_methods[i] = m;
2130             _last_free = b;
2131             return &(b->_methods[i]);
2132           }
2133         }
2134         // Only check each block once for frees.  They're very unlikely.
2135         // Increment top past the end of the block.
2136         b->_top++;
2137       }
2138       // need to allocate a next block.
2139       if (b->_next == nullptr) {
2140         b->_next = _last_free = new JNIMethodBlockNode();
2141       }
2142     }
2143     guarantee(false, "Should always allocate a free block");
2144     return nullptr;
2145   }
2146 
2147   bool contains(Method** m) {
2148     if (m == nullptr) return false;
2149     for (JNIMethodBlockNode* b = &_head; b != nullptr; b = b->_next) {
2150       if (b->_methods <= m && m < b->_methods + b->_number_of_methods) {
2151         // This is a bit of extra checking, for two reasons.  One is
2152         // that contains() deals with pointers that are passed in by
2153         // JNI code, so making sure that the pointer is aligned
2154         // correctly is valuable.  The other is that <= and > are
2155         // technically not defined on pointers, so the if guard can
2156         // pass spuriously; no modern compiler is likely to make that
2157         // a problem, though (and if one did, the guard could also
2158         // fail spuriously, which would be bad).
2159         ptrdiff_t idx = m - b->_methods;
2160         if (b->_methods + idx == m) {
2161           return true;
2162         }
2163       }
2164     }
2165     return false;  // not found
2166   }
2167 
2168   // During class unloading the methods are cleared, which is different
2169   // than freed.
2170   void clear_all_methods() {
2171     for (JNIMethodBlockNode* b = &_head; b != nullptr; b = b->_next) {
2172       for (int i = 0; i< b->_number_of_methods; i++) {
2173         b->_methods[i] = nullptr;
2174       }
2175     }
2176   }
2177 #ifndef PRODUCT
2178   int count_methods() {
2179     // count all allocated methods
2180     int count = 0;
2181     for (JNIMethodBlockNode* b = &_head; b != nullptr; b = b->_next) {
2182       for (int i = 0; i< b->_number_of_methods; i++) {
2183         if (b->_methods[i] != _free_method) count++;
2184       }
2185     }
2186     return count;
2187   }
2188 #endif // PRODUCT
2189 };
2190 
2191 // Something that can't be mistaken for an address or a markWord
2192 Method* const JNIMethodBlock::_free_method = (Method*)55;
2193 
2194 JNIMethodBlockNode::JNIMethodBlockNode(int num_methods) : _top(0), _next(nullptr) {
2195   _number_of_methods = MAX2(num_methods, min_block_size);
2196   _methods = NEW_C_HEAP_ARRAY(Method*, _number_of_methods, mtInternal);
2197   for (int i = 0; i < _number_of_methods; i++) {
2198     _methods[i] = JNIMethodBlock::_free_method;
2199   }
2200 }
2201 
2202 void Method::ensure_jmethod_ids(ClassLoaderData* cld, int capacity) {
2203   // Have to add jmethod_ids() to class loader data thread-safely.
2204   // Also have to add the method to the list safely, which the lock
2205   // protects as well.
2206   MutexLocker ml(JmethodIdCreation_lock,  Mutex::_no_safepoint_check_flag);
2207   if (cld->jmethod_ids() == nullptr) {
2208     cld->set_jmethod_ids(new JNIMethodBlock(capacity));
2209   } else {
2210     cld->jmethod_ids()->ensure_methods(capacity);
2211   }
2212 }
2213 
2214 // Add a method id to the jmethod_ids
2215 jmethodID Method::make_jmethod_id(ClassLoaderData* cld, Method* m) {
2216   // Have to add jmethod_ids() to class loader data thread-safely.
2217   // Also have to add the method to the list safely, which the lock
2218   // protects as well.
2219   assert(JmethodIdCreation_lock->owned_by_self(), "sanity check");
2220 
2221   ResourceMark rm;
2222   log_debug(jmethod)("Creating jmethodID for Method %s", m->external_name());
2223   if (cld->jmethod_ids() == nullptr) {
2224     cld->set_jmethod_ids(new JNIMethodBlock());
2225   }
2226   // jmethodID is a pointer to Method*
2227   return (jmethodID)cld->jmethod_ids()->add_method(m);
2228 }
2229 
2230 jmethodID Method::jmethod_id() {
2231   methodHandle mh(Thread::current(), this);
2232   return method_holder()->get_jmethod_id(mh);
2233 }
2234 
2235 void Method::change_method_associated_with_jmethod_id(jmethodID jmid, Method* new_method) {
2236   // Can't assert the method_holder is the same because the new method has the
2237   // scratch method holder.
2238   assert(resolve_jmethod_id(jmid)->method_holder()->class_loader()
2239            == new_method->method_holder()->class_loader() ||
2240            new_method->method_holder()->class_loader() == nullptr, // allow Unsafe substitution
2241          "changing to a different class loader");
2242   // Just change the method in place, jmethodID pointer doesn't change.
2243   *((Method**)jmid) = new_method;
2244 }
2245 
2246 bool Method::is_method_id(jmethodID mid) {
2247   Method* m = resolve_jmethod_id(mid);
2248   assert(m != nullptr, "should be called with non-null method");
2249   InstanceKlass* ik = m->method_holder();
2250   ClassLoaderData* cld = ik->class_loader_data();
2251   if (cld->jmethod_ids() == nullptr) return false;
2252   return (cld->jmethod_ids()->contains((Method**)mid));
2253 }
2254 
2255 Method* Method::checked_resolve_jmethod_id(jmethodID mid) {
2256   if (mid == nullptr) return nullptr;
2257   Method* o = resolve_jmethod_id(mid);
2258   if (o == nullptr || o == JNIMethodBlock::_free_method) {
2259     return nullptr;
2260   }
2261   // Method should otherwise be valid. Assert for testing.
2262   assert(is_valid_method(o), "should be valid jmethodid");
2263   // If the method's class holder object is unreferenced, but not yet marked as
2264   // unloaded, we need to return null here too because after a safepoint, its memory
2265   // will be reclaimed.
2266   return o->method_holder()->is_loader_alive() ? o : nullptr;
2267 };
2268 
2269 void Method::set_on_stack(const bool value) {
2270   // Set both the method itself and its constant pool.  The constant pool
2271   // on stack means some method referring to it is also on the stack.
2272   constants()->set_on_stack(value);
2273 
2274   bool already_set = on_stack_flag();
2275   set_on_stack_flag(value);
2276   if (value && !already_set) {
2277     MetadataOnStackMark::record(this);
2278   }
2279 }
2280 
2281 void Method::record_gc_epoch() {
2282   // If any method is on the stack in continuations, none of them can be reclaimed,
2283   // so save the marking cycle to check for the whole class in the cpCache.
2284   // The cpCache is writeable.
2285   constants()->cache()->record_gc_epoch();
2286 }
2287 
2288 // Called when the class loader is unloaded to make all methods weak.
2289 void Method::clear_jmethod_ids(ClassLoaderData* loader_data) {
2290   loader_data->jmethod_ids()->clear_all_methods();
2291 }
2292 
2293 void Method::clear_jmethod_id() {
2294   // Being at a safepoint prevents racing against other class redefinitions
2295   assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
2296   // The jmethodID is not stored in the Method instance, we need to look it up first
2297   jmethodID methodid = find_jmethod_id_or_null();
2298   // We need to make sure that jmethodID actually resolves to this method
2299   // - multiple redefined versions may share jmethodID slots and if a method
2300   //   has already been rewired to a newer version we could be removing reference
2301   //   to a still existing method instance
2302   if (methodid != nullptr && *((Method**)methodid) == this) {
2303     *((Method**)methodid) = nullptr;
2304   }
2305 }
2306 
2307 bool Method::has_method_vptr(const void* ptr) {
2308   Method m;
2309   // This assumes that the vtbl pointer is the first word of a C++ object.
2310   return dereference_vptr(&m) == dereference_vptr(ptr);
2311 }
2312 
2313 // Check that this pointer is valid by checking that the vtbl pointer matches
2314 bool Method::is_valid_method(const Method* m) {
2315   if (m == nullptr) {
2316     return false;
2317   } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2318     // Quick sanity check on pointer.
2319     return false;
2320   } else if (!os::is_readable_range(m, m + 1)) {
2321     return false;
2322   } else if (m->is_shared()) {
2323     return CppVtables::is_valid_shared_method(m);
2324   } else if (Metaspace::contains_non_shared(m)) {
2325     return has_method_vptr((const void*)m);
2326   } else {
2327     return false;
2328   }
2329 }
2330 
2331 bool Method::is_scalarized_arg(int idx) const {
2332   if (!has_scalarized_args()) {
2333     return false;
2334   }
2335   // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2336   int depth = 0;
2337   const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2338   for (int i = 0; i < sig->length(); i++) {
2339     BasicType bt = sig->at(i)._bt;
2340     if (bt == T_METADATA) {
2341       depth++;
2342     }
2343     if (idx == 0) {
2344       break; // Argument found
2345     }
2346     if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2347       depth--;
2348     }
2349     if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2350       idx--; // Advance to next argument
2351     }
2352   }
2353   return depth != 0;
2354 }
2355 
2356 #ifndef PRODUCT
2357 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2358   out->print("%d", loader_data->jmethod_ids()->count_methods());
2359 }
2360 #endif // PRODUCT
2361 
2362 
2363 // Printing
2364 
2365 #ifndef PRODUCT
2366 
2367 void Method::print_on(outputStream* st) const {
2368   ResourceMark rm;
2369   assert(is_method(), "must be method");
2370   st->print_cr("%s", internal_name());
2371   st->print_cr(" - this oop:          " PTR_FORMAT, p2i(this));
2372   st->print   (" - method holder:     "); method_holder()->print_value_on(st); st->cr();
2373   st->print   (" - constants:         " PTR_FORMAT " ", p2i(constants()));
2374   constants()->print_value_on(st); st->cr();
2375   st->print   (" - access:            0x%x  ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2376   st->print   (" - flags:             0x%x  ", _flags.as_int()); _flags.print_on(st); st->cr();
2377   st->print   (" - name:              ");    name()->print_value_on(st); st->cr();
2378   st->print   (" - signature:         ");    signature()->print_value_on(st); st->cr();
2379   st->print_cr(" - max stack:         %d",   max_stack());
2380   st->print_cr(" - max locals:        %d",   max_locals());
2381   st->print_cr(" - size of params:    %d",   size_of_parameters());
2382   st->print_cr(" - method size:       %d",   method_size());
2383   if (intrinsic_id() != vmIntrinsics::_none)
2384     st->print_cr(" - intrinsic id:      %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2385   if (highest_comp_level() != CompLevel_none)
2386     st->print_cr(" - highest level:     %d", highest_comp_level());
2387   st->print_cr(" - vtable index:      %d",   _vtable_index);
2388 #ifdef ASSERT
2389   if (valid_itable_index())
2390     st->print_cr(" - itable index:      %d",   itable_index());
2391 #endif
2392   st->print_cr(" - i2i entry:         " PTR_FORMAT, p2i(interpreter_entry()));
2393   st->print(   " - adapters:          ");
2394   AdapterHandlerEntry* a = ((Method*)this)->adapter();
2395   if (a == nullptr)
2396     st->print_cr(PTR_FORMAT, p2i(a));
2397   else
2398     a->print_adapter_on(st);
2399   st->print_cr(" - compiled entry           " PTR_FORMAT, p2i(from_compiled_entry()));
2400   st->print_cr(" - compiled inline entry    " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2401   st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2402   st->print_cr(" - code size:         %d",   code_size());
2403   if (code_size() != 0) {
2404     st->print_cr(" - code start:        " PTR_FORMAT, p2i(code_base()));
2405     st->print_cr(" - code end (excl):   " PTR_FORMAT, p2i(code_base() + code_size()));
2406   }
2407   if (method_data() != nullptr) {
2408     st->print_cr(" - method data:       " PTR_FORMAT, p2i(method_data()));
2409   }
2410   st->print_cr(" - checked ex length: %d",   checked_exceptions_length());
2411   if (checked_exceptions_length() > 0) {
2412     CheckedExceptionElement* table = checked_exceptions_start();
2413     st->print_cr(" - checked ex start:  " PTR_FORMAT, p2i(table));
2414     if (Verbose) {
2415       for (int i = 0; i < checked_exceptions_length(); i++) {
2416         st->print_cr("   - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2417       }
2418     }
2419   }
2420   if (has_linenumber_table()) {
2421     u_char* table = compressed_linenumber_table();
2422     st->print_cr(" - linenumber start:  " PTR_FORMAT, p2i(table));
2423     if (Verbose) {
2424       CompressedLineNumberReadStream stream(table);
2425       while (stream.read_pair()) {
2426         st->print_cr("   - line %d: %d", stream.line(), stream.bci());
2427       }
2428     }
2429   }
2430   st->print_cr(" - localvar length:   %d",   localvariable_table_length());
2431   if (localvariable_table_length() > 0) {
2432     LocalVariableTableElement* table = localvariable_table_start();
2433     st->print_cr(" - localvar start:    " PTR_FORMAT, p2i(table));
2434     if (Verbose) {
2435       for (int i = 0; i < localvariable_table_length(); i++) {
2436         int bci = table[i].start_bci;
2437         int len = table[i].length;
2438         const char* name = constants()->printable_name_at(table[i].name_cp_index);
2439         const char* desc = constants()->printable_name_at(table[i].descriptor_cp_index);
2440         int slot = table[i].slot;
2441         st->print_cr("   - %s %s bci=%d len=%d slot=%d", desc, name, bci, len, slot);
2442       }
2443     }
2444   }
2445   if (code() != nullptr) {
2446     st->print   (" - compiled code: ");
2447     code()->print_value_on(st);
2448   }
2449   if (is_native()) {
2450     st->print_cr(" - native function:   " PTR_FORMAT, p2i(native_function()));
2451     st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2452   }
2453 }
2454 
2455 void Method::print_linkage_flags(outputStream* st) {
2456   access_flags().print_on(st);
2457   if (is_default_method()) {
2458     st->print("default ");
2459   }
2460   if (is_overpass()) {
2461     st->print("overpass ");
2462   }
2463 }
2464 #endif //PRODUCT
2465 
2466 void Method::print_value_on(outputStream* st) const {
2467   assert(is_method(), "must be method");
2468   st->print("%s", internal_name());
2469   print_address_on(st);
2470   st->print(" ");
2471   if (WizardMode) access_flags().print_on(st);
2472   name()->print_value_on(st);
2473   st->print(" ");
2474   signature()->print_value_on(st);
2475   st->print(" in ");
2476   method_holder()->print_value_on(st);
2477   if (WizardMode) st->print("#%d", _vtable_index);
2478   if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2479   if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2480 }
2481 
2482 // Verification
2483 
2484 void Method::verify_on(outputStream* st) {
2485   guarantee(is_method(), "object must be method");
2486   guarantee(constants()->is_constantPool(), "should be constant pool");
2487   MethodData* md = method_data();
2488   guarantee(md == nullptr ||
2489       md->is_methodData(), "should be method data");
2490 }
--- EOF ---