1 /*
   2  * Copyright (c) 1997, 2025, 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 #ifndef SHARE_OOPS_METHOD_HPP
  26 #define SHARE_OOPS_METHOD_HPP
  27 
  28 #include "code/compressedStream.hpp"
  29 #include "compiler/compilerDefinitions.hpp"
  30 #include "oops/annotations.hpp"
  31 #include "oops/constantPool.hpp"
  32 #include "oops/methodFlags.hpp"
  33 #include "oops/instanceKlass.hpp"
  34 #include "oops/oop.hpp"
  35 #include "utilities/accessFlags.hpp"
  36 #include "utilities/align.hpp"
  37 #include "utilities/growableArray.hpp"
  38 #include "utilities/macros.hpp"
  39 #include "utilities/vmEnums.hpp"
  40 #if INCLUDE_JFR
  41 #include "jfr/support/jfrTraceIdExtension.hpp"
  42 #endif
  43 
  44 
  45 // A Method represents a Java method.
  46 //
  47 // Note that most applications load thousands of methods, so keeping the size of this
  48 // class small has a big impact on footprint.
  49 //
  50 // Note that native_function and signature_handler have to be at fixed offsets
  51 // (required by the interpreter)
  52 //
  53 //  Method embedded field layout (after declared fields):
  54 //   [EMBEDDED native_function       (present only if native) ]
  55 //   [EMBEDDED signature_handler     (present only if native) ]
  56 
  57 class CheckedExceptionElement;
  58 class LocalVariableTableElement;
  59 class AdapterHandlerEntry;
  60 class MethodData;
  61 class MethodCounters;
  62 class ConstMethod;
  63 class InlineTableSizes;
  64 class nmethod;
  65 class InterpreterOopMap;
  66 
  67 class Method : public Metadata {
  68  friend class VMStructs;
  69  friend class JVMCIVMStructs;
  70  friend class MethodTest;
  71  private:
  72   // If you add a new field that points to any metaspace object, you
  73   // must add this field to Method::metaspace_pointers_do().
  74   ConstMethod*      _constMethod;                // Method read-only data.
  75   MethodData*       _method_data;
  76   MethodCounters*   _method_counters;
  77   AdapterHandlerEntry* _adapter;
  78   int               _vtable_index;               // vtable index of this method (see VtableIndexFlag)
  79   AccessFlags       _access_flags;               // Access flags
  80   MethodFlags       _flags;
  81 
  82   u2                _intrinsic_id;               // vmSymbols::intrinsic_id (0 == _none)
  83 
  84   JFR_ONLY(DEFINE_TRACE_FLAG;)
  85 
  86 #ifndef PRODUCT
  87   int64_t _compiled_invocation_count;
  88 
  89   Symbol* _name;
  90 #endif
  91   // Entry point for calling both from and to the interpreter.
  92   address _i2i_entry;           // All-args-on-stack calling convention
  93   // Entry point for calling from compiled code, to compiled code if it exists
  94   // or else the interpreter.
  95   volatile address _from_compiled_entry;           // Cache of: _code ? _code->entry_point() : _adapter->c2i_entry()
  96   volatile address _from_compiled_inline_ro_entry; // Cache of: _code ? _code->verified_inline_ro_entry_point() : _adapter->c2i_inline_ro_entry()
  97   volatile address _from_compiled_inline_entry;    // Cache of: _code ? _code->verified_inline_entry_point()    : _adapter->c2i_inline_entry()
  98   // The entry point for calling both from and to compiled code is
  99   // "_code->entry_point()".  Because of tiered compilation and de-opt, this
 100   // field can come and go.  It can transition from null to not-null at any
 101   // time (whenever a compile completes).  It can transition from not-null to
 102   // null only at safepoints (because of a de-opt).
 103   nmethod* volatile _code;                   // Points to the corresponding piece of native code
 104   volatile address  _from_interpreted_entry; // Cache of _code ? _adapter->i2c_entry() : _i2i_entry
 105 
 106   // Constructor
 107   Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name);
 108  public:
 109 
 110   static Method* allocate(ClassLoaderData* loader_data,
 111                           int byte_code_size,
 112                           AccessFlags access_flags,
 113                           InlineTableSizes* sizes,
 114                           ConstMethod::MethodType method_type,
 115                           Symbol* name,
 116                           TRAPS);
 117 
 118   // CDS and vtbl checking can create an empty Method to get vtbl pointer.
 119   Method(){}
 120 
 121   virtual bool is_method() const { return true; }
 122 
 123 #if INCLUDE_CDS
 124   void remove_unshareable_info();
 125   void restore_unshareable_info(TRAPS);
 126   static void restore_archived_method_handle_intrinsic(methodHandle m, TRAPS);
 127 #endif
 128 
 129   // accessors for instance variables
 130 
 131   ConstMethod* constMethod() const             { return _constMethod; }
 132   void set_constMethod(ConstMethod* xconst)    { _constMethod = xconst; }
 133 
 134 
 135   static address make_adapters(const methodHandle& mh, TRAPS);
 136   address from_compiled_entry() const;
 137   address from_compiled_inline_ro_entry() const;
 138   address from_compiled_inline_entry() const;
 139   address from_interpreted_entry() const;
 140 
 141   // access flag
 142   AccessFlags access_flags() const               { return _access_flags;  }
 143   void set_access_flags(AccessFlags flags)       { _access_flags = flags; }
 144 
 145   // name
 146   Symbol* name() const                           { return constants()->symbol_at(name_index()); }
 147   u2 name_index() const                          { return constMethod()->name_index();         }
 148   void set_name_index(int index)                 { constMethod()->set_name_index(index);       }
 149 
 150   // signature
 151   Symbol* signature() const                      { return constants()->symbol_at(signature_index()); }
 152   u2 signature_index() const                     { return constMethod()->signature_index();         }
 153   void set_signature_index(int index)            { constMethod()->set_signature_index(index);       }
 154 
 155   // generics support
 156   Symbol* generic_signature() const              { int idx = generic_signature_index(); return ((idx != 0) ? constants()->symbol_at(idx) : nullptr); }
 157   u2 generic_signature_index() const             { return constMethod()->generic_signature_index(); }
 158 
 159   // annotations support
 160   AnnotationArray* annotations() const           {
 161     return constMethod()->method_annotations();
 162   }
 163   AnnotationArray* parameter_annotations() const {
 164     return constMethod()->parameter_annotations();
 165   }
 166   AnnotationArray* annotation_default() const    {
 167     return constMethod()->default_annotations();
 168   }
 169   AnnotationArray* type_annotations() const      {
 170     return constMethod()->type_annotations();
 171   }
 172 
 173   // Helper routine: get klass name + "." + method name + signature as
 174   // C string, for the purpose of providing more useful
 175   // fatal error handling. The string is allocated in resource
 176   // area if a buffer is not provided by the caller.
 177   char* name_and_sig_as_C_string() const;
 178   char* name_and_sig_as_C_string(char* buf, int size) const;
 179 
 180   // Static routine in the situations we don't have a Method*
 181   static char* name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature);
 182   static char* name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size);
 183 
 184   // Get return type + klass name + "." + method name + ( parameters types )
 185   // as a C string or print it to an outputStream.
 186   // This is to be used to assemble strings passed to Java, so that
 187   // the text more resembles Java code. Used in exception messages.
 188   // Memory is allocated in the resource area; the caller needs
 189   // a ResourceMark.
 190   const char* external_name() const;
 191   void  print_external_name(outputStream *os) const;
 192 
 193   static const char* external_name(                  Klass* klass, Symbol* method_name, Symbol* signature);
 194   static void  print_external_name(outputStream *os, Klass* klass, Symbol* method_name, Symbol* signature);
 195 
 196   Bytecodes::Code java_code_at(int bci) const {
 197     return Bytecodes::java_code_at(this, bcp_from(bci));
 198   }
 199   Bytecodes::Code code_at(int bci) const {
 200     return Bytecodes::code_at(this, bcp_from(bci));
 201   }
 202 
 203   // JVMTI breakpoints
 204 #if !INCLUDE_JVMTI
 205   Bytecodes::Code orig_bytecode_at(int bci) const {
 206     ShouldNotReachHere();
 207     return Bytecodes::_shouldnotreachhere;
 208   }
 209   void set_orig_bytecode_at(int bci, Bytecodes::Code code) {
 210     ShouldNotReachHere();
 211   };
 212   u2   number_of_breakpoints() const {return 0;}
 213 #else // !INCLUDE_JVMTI
 214   Bytecodes::Code orig_bytecode_at(int bci) const;
 215   void set_orig_bytecode_at(int bci, Bytecodes::Code code);
 216   void set_breakpoint(int bci);
 217   void clear_breakpoint(int bci);
 218   void clear_all_breakpoints();
 219   // Tracking number of breakpoints, for fullspeed debugging.
 220   // Only mutated by VM thread.
 221   inline u2 number_of_breakpoints() const;
 222   inline void incr_number_of_breakpoints(Thread* current);
 223   inline void decr_number_of_breakpoints(Thread* current);
 224   // Initialization only
 225   inline void clear_number_of_breakpoints();
 226 #endif // !INCLUDE_JVMTI
 227 
 228   // index into InstanceKlass methods() array
 229   // note: also used by jfr
 230   u2 method_idnum() const           { return constMethod()->method_idnum(); }
 231   void set_method_idnum(u2 idnum)   { constMethod()->set_method_idnum(idnum); }
 232 
 233   u2 orig_method_idnum() const           { return constMethod()->orig_method_idnum(); }
 234   void set_orig_method_idnum(u2 idnum)   { constMethod()->set_orig_method_idnum(idnum); }
 235 
 236   // code size
 237   u2 code_size() const                   { return constMethod()->code_size(); }
 238 
 239   // method size in words
 240   int method_size() const                { return sizeof(Method)/wordSize + ( is_native() ? 2 : 0 ); }
 241 
 242   // constant pool for Klass* holding this method
 243   ConstantPool* constants() const              { return constMethod()->constants(); }
 244   void set_constants(ConstantPool* c)          { constMethod()->set_constants(c); }
 245 
 246   // max stack
 247   // return original max stack size for method verification
 248   u2  verifier_max_stack() const               { return constMethod()->max_stack(); }
 249   int          max_stack() const               { return constMethod()->max_stack() + extra_stack_entries(); }
 250   void      set_max_stack(int size)            {        constMethod()->set_max_stack(size); }
 251 
 252   // max locals
 253   u2  max_locals() const                       { return constMethod()->max_locals(); }
 254   void set_max_locals(int size)                { constMethod()->set_max_locals(size); }
 255 
 256   void set_deprecated() { constMethod()->set_deprecated(); }
 257   bool deprecated() const { return constMethod()->deprecated(); }
 258 
 259   void set_deprecated_for_removal() { constMethod()->set_deprecated_for_removal(); }
 260   bool deprecated_for_removal() const { return constMethod()->deprecated_for_removal(); }
 261 
 262   int highest_comp_level() const;
 263   void set_highest_comp_level(int level);
 264   int highest_osr_comp_level() const;
 265   void set_highest_osr_comp_level(int level);
 266 
 267 #if COMPILER2_OR_JVMCI
 268   // Count of times method was exited via exception while interpreting
 269   inline void interpreter_throwout_increment(Thread* current);
 270 #endif
 271 
 272   inline int interpreter_throwout_count() const;
 273 
 274   u2 size_of_parameters() const { return constMethod()->size_of_parameters(); }
 275 
 276   bool has_stackmap_table() const {
 277     return constMethod()->has_stackmap_table();
 278   }
 279 
 280   Array<u1>* stackmap_data() const {
 281     return constMethod()->stackmap_data();
 282   }
 283 
 284   void set_stackmap_data(Array<u1>* sd) {
 285     constMethod()->set_stackmap_data(sd);
 286   }
 287 
 288   // exception handler table
 289   bool has_exception_handler() const
 290                              { return constMethod()->has_exception_table(); }
 291   u2 exception_table_length() const
 292                              { return constMethod()->exception_table_length(); }
 293   ExceptionTableElement* exception_table_start() const
 294                              { return constMethod()->exception_table_start(); }
 295 
 296   // Finds the first entry point bci of an exception handler for an
 297   // exception of klass ex_klass thrown at throw_bci. A value of null
 298   // for ex_klass indicates that the exception klass is not known; in
 299   // this case it matches any constraint class. Returns -1 if the
 300   // exception cannot be handled in this method. The handler
 301   // constraint classes are loaded if necessary. Note that this may
 302   // throw an exception if loading of the constraint classes causes
 303   // an IllegalAccessError (bugid 4307310) or an OutOfMemoryError.
 304   // If an exception is thrown, returns the bci of the
 305   // exception handler which caused the exception to be thrown, which
 306   // is needed for proper retries. See, for example,
 307   // InterpreterRuntime::exception_handler_for_exception.
 308   static int fast_exception_handler_bci_for(const methodHandle& mh, Klass* ex_klass, int throw_bci, TRAPS);
 309 
 310   static bool register_native(Klass* k,
 311                               Symbol* name,
 312                               Symbol* signature,
 313                               address entry,
 314                               TRAPS);
 315 
 316   // method data access
 317   MethodData* method_data() const              {
 318     return _method_data;
 319   }
 320 
 321   // mark an exception handler as entered (used to prune dead catch blocks in C2)
 322   void set_exception_handler_entered(int handler_bci);
 323 
 324   MethodCounters* method_counters() const {
 325     return _method_counters;
 326   }
 327 
 328   void clear_method_counters() {
 329     _method_counters = nullptr;
 330   }
 331 
 332   bool init_method_counters(MethodCounters* counters);
 333 
 334   inline int prev_event_count() const;
 335   inline void set_prev_event_count(int count);
 336   inline jlong prev_time() const;
 337   inline void set_prev_time(jlong time);
 338   inline float rate() const;
 339   inline void set_rate(float rate);
 340 
 341   int invocation_count() const;
 342   int backedge_count() const;
 343 
 344   bool was_executed_more_than(int n);
 345   bool was_never_executed()                     { return !was_executed_more_than(0);  }
 346 
 347   static void build_profiling_method_data(const methodHandle& method, TRAPS);
 348 
 349   static MethodCounters* build_method_counters(Thread* current, Method* m);
 350 
 351   int interpreter_invocation_count()            { return invocation_count();          }
 352 
 353 #ifndef PRODUCT
 354   int64_t  compiled_invocation_count() const    { return _compiled_invocation_count;}
 355   void set_compiled_invocation_count(int count) { _compiled_invocation_count = (int64_t)count; }
 356 #else
 357   // for PrintMethodData in a product build
 358   int64_t  compiled_invocation_count() const    { return 0; }
 359 #endif // not PRODUCT
 360 
 361   // nmethod/verified compiler entry
 362   address verified_code_entry();
 363   address verified_inline_code_entry();
 364   address verified_inline_ro_code_entry();
 365   bool check_code() const;      // Not inline to avoid circular ref
 366   nmethod* code() const;
 367 
 368   // Locks NMethodState_lock if not held.
 369   void unlink_code(nmethod *compare);
 370   // Locks NMethodState_lock if not held.
 371   void unlink_code();
 372 
 373 private:
 374   // Either called with NMethodState_lock held or from constructor.
 375   void clear_code();
 376 
 377   void clear_method_data() {
 378     _method_data = nullptr;
 379   }
 380 
 381 public:
 382   static void set_code(const methodHandle& mh, nmethod* code);
 383   void set_adapter_entry(AdapterHandlerEntry* adapter) {
 384     _adapter = adapter;
 385   }
 386   void set_from_compiled_entry(address entry) {
 387     _from_compiled_entry = entry;
 388   }
 389   void set_from_compiled_inline_ro_entry(address entry) {
 390     _from_compiled_inline_ro_entry = entry;
 391   }
 392   void set_from_compiled_inline_entry(address entry) {
 393     _from_compiled_inline_entry = entry;
 394   }
 395 
 396   address get_i2c_entry();
 397   address get_c2i_entry();
 398   address get_c2i_inline_entry();
 399   address get_c2i_unverified_entry();
 400   address get_c2i_unverified_inline_entry();
 401   address get_c2i_no_clinit_check_entry();
 402   AdapterHandlerEntry* adapter() const {
 403     return _adapter;
 404   }
 405   // setup entry points
 406   void link_method(const methodHandle& method, TRAPS);
 407   // clear entry points. Used by sharing code during dump time
 408   void unlink_method() NOT_CDS_RETURN;
 409   void remove_unshareable_flags() NOT_CDS_RETURN;
 410 
 411   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
 412   virtual MetaspaceObj::Type type() const { return MethodType; }
 413 
 414   // vtable index
 415   enum VtableIndexFlag {
 416     // Valid vtable indexes are non-negative (>= 0).
 417     // These few negative values are used as sentinels.
 418     itable_index_max        = -10, // first itable index, growing downward
 419     pending_itable_index    = -9,  // itable index will be assigned
 420     invalid_vtable_index    = -4,  // distinct from any valid vtable index
 421     garbage_vtable_index    = -3,  // not yet linked; no vtable layout yet
 422     nonvirtual_vtable_index = -2   // there is no need for vtable dispatch
 423     // 6330203 Note:  Do not use -1, which was overloaded with many meanings.
 424   };
 425   DEBUG_ONLY(bool valid_vtable_index() const     { return _vtable_index >= nonvirtual_vtable_index; })
 426   bool has_vtable_index() const                  { return _vtable_index >= 0; }
 427   int  vtable_index() const                      { return _vtable_index; }
 428   void set_vtable_index(int index);
 429   DEBUG_ONLY(bool valid_itable_index() const     { return _vtable_index <= pending_itable_index; })
 430   bool has_itable_index() const                  { return _vtable_index <= itable_index_max; }
 431   int  itable_index() const                      { assert(valid_itable_index(), "");
 432                                                    return itable_index_max - _vtable_index; }
 433   void set_itable_index(int index);
 434 
 435   // interpreter entry
 436   address interpreter_entry() const              { return _i2i_entry; }
 437   // Only used when first initialize so we can set _i2i_entry and _from_interpreted_entry
 438   void set_interpreter_entry(address entry) {
 439     if (_i2i_entry != entry) {
 440       _i2i_entry = entry;
 441     }
 442     if (_from_interpreted_entry != entry) {
 443       _from_interpreted_entry = entry;
 444     }
 445   }
 446 
 447   // native function (used for native methods only)
 448   enum {
 449     native_bind_event_is_interesting = true
 450   };
 451   address native_function() const                { return *(native_function_addr()); }
 452 
 453   // Must specify a real function (not null).
 454   // Use clear_native_function() to unregister.
 455   void set_native_function(address function, bool post_event_flag);
 456   bool has_native_function() const;
 457   void clear_native_function();
 458 
 459   // signature handler (used for native methods only)
 460   address signature_handler() const              { return *(signature_handler_addr()); }
 461   void set_signature_handler(address handler);
 462 
 463   // Interpreter oopmap support.
 464   // If handle is already available, call with it for better performance.
 465   void mask_for(int bci, InterpreterOopMap* mask);
 466   void mask_for(const methodHandle& this_mh, int bci, InterpreterOopMap* mask);
 467 
 468   // operations on invocation counter
 469   void print_invocation_count(outputStream* st);
 470 
 471   // byte codes
 472   void    set_code(address code)      { return constMethod()->set_code(code); }
 473   address code_base() const           { return constMethod()->code_base(); }
 474   bool    contains(address bcp) const { return constMethod()->contains(bcp); }
 475 
 476   // prints byte codes
 477   void print_codes(int flags = 0) const { print_codes_on(tty, flags); }
 478   void print_codes_on(outputStream* st, int flags = 0) const;
 479   void print_codes_on(int from, int to, outputStream* st, int flags = 0) const;
 480 
 481   // method parameters
 482   bool has_method_parameters() const
 483                          { return constMethod()->has_method_parameters(); }
 484   int method_parameters_length() const
 485                          { return constMethod()->method_parameters_length(); }
 486   MethodParametersElement* method_parameters_start() const
 487                          { return constMethod()->method_parameters_start(); }
 488 
 489   // checked exceptions
 490   u2 checked_exceptions_length() const
 491                          { return constMethod()->checked_exceptions_length(); }
 492   CheckedExceptionElement* checked_exceptions_start() const
 493                          { return constMethod()->checked_exceptions_start(); }
 494 
 495   // localvariable table
 496   bool has_localvariable_table() const
 497                           { return constMethod()->has_localvariable_table(); }
 498   u2 localvariable_table_length() const
 499                         { return constMethod()->localvariable_table_length(); }
 500   LocalVariableTableElement* localvariable_table_start() const
 501                          { return constMethod()->localvariable_table_start(); }
 502 
 503   bool has_linenumber_table() const
 504                               { return constMethod()->has_linenumber_table(); }
 505   u_char* compressed_linenumber_table() const
 506                        { return constMethod()->compressed_linenumber_table(); }
 507 
 508   // method holder (the Klass* holding this method)
 509   InstanceKlass* method_holder() const         { return constants()->pool_holder(); }
 510 
 511   Symbol* klass_name() const;                    // returns the name of the method holder
 512   BasicType result_type() const                  { return constMethod()->result_type(); }
 513   bool is_returning_oop() const                  { BasicType r = result_type(); return is_reference_type(r); }
 514   InlineKlass* returns_inline_type(Thread* thread) const;
 515 
 516   // Checked exceptions thrown by this method (resolved to mirrors)
 517   objArrayHandle resolved_checked_exceptions(TRAPS) { return resolved_checked_exceptions_impl(this, THREAD); }
 518 
 519   // Access flags
 520   bool is_public() const                         { return access_flags().is_public();      }
 521   bool is_private() const                        { return access_flags().is_private();     }
 522   bool is_protected() const                      { return access_flags().is_protected();   }
 523   bool is_package_private() const                { return !is_public() && !is_private() && !is_protected(); }
 524   bool is_static() const                         { return access_flags().is_static();      }
 525   bool is_final() const                          { return access_flags().is_final();       }
 526   bool is_synchronized() const                   { return access_flags().is_synchronized();}
 527   bool is_native() const                         { return access_flags().is_native();      }
 528   bool is_abstract() const                       { return access_flags().is_abstract();    }
 529   bool is_synthetic() const                      { return access_flags().is_synthetic();   }
 530 
 531   // returns true if contains only return operation
 532   bool is_empty_method() const;
 533 
 534   // returns true if this is a vanilla constructor
 535   bool is_vanilla_constructor() const;
 536 
 537   // checks method and its method holder
 538   bool is_final_method() const;
 539   bool is_final_method(AccessFlags class_access_flags) const;
 540   // interface method declared with 'default' - excludes private interface methods
 541   bool is_default_method() const;
 542 
 543   // true if method needs no dynamic dispatch (final and/or no vtable entry)
 544   bool can_be_statically_bound() const;
 545   bool can_be_statically_bound(InstanceKlass* context) const;
 546   bool can_be_statically_bound(AccessFlags class_access_flags) const;
 547 
 548   // true if method can omit stack trace in throw in compiled code.
 549   bool can_omit_stack_trace();
 550 
 551   // Flags getting and setting.
 552 #define M_STATUS_GET_SET(name, ignore)          \
 553   bool name() const { return _flags.name(); }   \
 554   void set_##name(bool x) { _flags.set_##name(x); } \
 555   void set_##name() { _flags.set_##name(true); }
 556   M_STATUS_DO(M_STATUS_GET_SET)
 557 #undef M_STATUS_GET_SET
 558 
 559   // returns true if the method has any backward branches.
 560   bool has_loops() {
 561     return has_loops_flag_init() ? has_loops_flag() : compute_has_loops_flag();
 562   };
 563 
 564   bool compute_has_loops_flag();
 565   bool set_has_loops() {
 566     // set both the flags and that it's been initialized.
 567     set_has_loops_flag();
 568     set_has_loops_flag_init();
 569     return true;
 570   }
 571 
 572   // returns true if the method has any monitors.
 573   bool has_monitors() const                      { return is_synchronized() || has_monitor_bytecodes(); }
 574 
 575   // monitor matching. This returns a conservative estimate of whether the monitorenter/monitorexit bytecodes
 576   // properly nest in the method. It might return false, even though they actually nest properly, since the info.
 577   // has not been computed yet.
 578   bool guaranteed_monitor_matching() const       { return monitor_matching(); }
 579   void set_guaranteed_monitor_matching()         { set_monitor_matching(); }
 580 
 581   // returns true if the method is an accessor function (setter/getter).
 582   bool is_accessor() const;
 583 
 584   // returns true if the method is a getter
 585   bool is_getter() const;
 586 
 587   // returns true if the method is a setter
 588   bool is_setter() const;
 589 
 590   // returns true if the method does nothing but return a constant of primitive type
 591   bool is_constant_getter() const;
 592 
 593   // returns true if the method name is <clinit> and the method has
 594   // valid static initializer flags.
 595   bool is_class_initializer() const;
 596 
 597   // returns true if the method name is <init>
 598   bool is_object_constructor() const;
 599 
 600   // returns true if the method name is wait0
 601   bool is_object_wait0() const;
 602 
 603   // compiled code support
 604   // NOTE: code() is inherently racy as deopt can be clearing code
 605   // simultaneously. Use with caution.
 606   bool has_compiled_code() const;
 607 
 608   bool needs_clinit_barrier() const;
 609 
 610   // sizing
 611   static int header_size()                       {
 612     return align_up((int)sizeof(Method), wordSize) / wordSize;
 613   }
 614   static int size(bool is_native);
 615   int size() const                               { return method_size(); }
 616   void log_touched(Thread* current);
 617   static void print_touched_methods(outputStream* out);
 618 
 619   // interpreter support
 620   static ByteSize const_offset()                 { return byte_offset_of(Method, _constMethod       ); }
 621   static ByteSize access_flags_offset()          { return byte_offset_of(Method, _access_flags      ); }
 622   static ByteSize from_compiled_offset()         { return byte_offset_of(Method, _from_compiled_entry); }
 623   static ByteSize from_compiled_inline_offset()  { return byte_offset_of(Method, _from_compiled_inline_entry); }
 624   static ByteSize from_compiled_inline_ro_offset(){ return byte_offset_of(Method, _from_compiled_inline_ro_entry); }
 625   static ByteSize code_offset()                  { return byte_offset_of(Method, _code); }
 626   static ByteSize flags_offset()                 { return byte_offset_of(Method, _flags); }
 627 
 628   static ByteSize method_counters_offset()       {
 629     return byte_offset_of(Method, _method_counters);
 630   }
 631 #ifndef PRODUCT
 632   static ByteSize compiled_invocation_counter_offset() { return byte_offset_of(Method, _compiled_invocation_count); }
 633 #endif // not PRODUCT
 634   static ByteSize native_function_offset()       { return in_ByteSize(sizeof(Method));                 }
 635   static ByteSize from_interpreted_offset()      { return byte_offset_of(Method, _from_interpreted_entry ); }
 636   static ByteSize interpreter_entry_offset()     { return byte_offset_of(Method, _i2i_entry ); }
 637   static ByteSize signature_handler_offset()     { return in_ByteSize(sizeof(Method) + wordSize);      }
 638   static ByteSize itable_index_offset()          { return byte_offset_of(Method, _vtable_index ); }
 639 
 640   // for code generation
 641   static ByteSize method_data_offset()  { return byte_offset_of(Method, _method_data); }
 642   static ByteSize intrinsic_id_offset() { return byte_offset_of(Method, _intrinsic_id); }
 643   static int intrinsic_id_size_in_bytes()        { return sizeof(u2); }
 644 
 645   // Static methods that are used to implement member methods where an exposed this pointer
 646   // is needed due to possible GCs
 647   static objArrayHandle resolved_checked_exceptions_impl(Method* method, TRAPS);
 648 
 649   // Returns the byte code index from the byte code pointer
 650   int     bci_from(address bcp) const;
 651   address bcp_from(int bci) const;
 652   address bcp_from(address bcp) const;
 653   int validate_bci_from_bcp(address bcp) const;
 654   int validate_bci(int bci) const;
 655 
 656   // Returns the line number for a bci if debugging information for the method is prowided,
 657   // -1 is returned otherwise.
 658   int line_number_from_bci(int bci) const;
 659 
 660   // Reflection support
 661   bool is_overridden_in(Klass* k) const;
 662 
 663   // Stack walking support
 664   bool is_ignored_by_security_stack_walk() const;
 665 
 666   // JSR 292 support
 667   bool is_method_handle_intrinsic() const;          // MethodHandles::is_signature_polymorphic_intrinsic(intrinsic_id)
 668   bool is_compiled_lambda_form() const;             // intrinsic_id() == vmIntrinsics::_compiledLambdaForm
 669   bool has_member_arg() const;                      // intrinsic_id() == vmIntrinsics::_linkToSpecial, etc.
 670   static methodHandle make_method_handle_intrinsic(vmIntrinsicID iid, // _invokeBasic, _linkToVirtual
 671                                                    Symbol* signature, //anything at all
 672                                                    TRAPS);
 673   // Some special methods don't need to be findable by nmethod iterators and are permanent.
 674   bool can_be_allocated_in_NonNMethod_space() const { return is_method_handle_intrinsic(); }
 675 
 676   // Continuation
 677   inline bool is_continuation_enter_intrinsic() const;
 678   inline bool is_continuation_yield_intrinsic() const;
 679   inline bool is_continuation_native_intrinsic() const;
 680   inline bool is_special_native_intrinsic() const;
 681 
 682   static Klass* check_non_bcp_klass(Klass* klass);
 683 
 684   enum {
 685     // How many extra stack entries for invokedynamic
 686     extra_stack_entries_for_jsr292 = 1
 687   };
 688 
 689   // this operates only on invoke methods:
 690   // presize interpreter frames for extra interpreter stack entries, if needed
 691   // Account for the extra appendix argument for invokehandle/invokedynamic
 692   static int extra_stack_entries() { return extra_stack_entries_for_jsr292; }
 693   static int extra_stack_words();  // = extra_stack_entries() * Interpreter::stackElementSize
 694 
 695   // RedefineClasses() support:
 696   bool on_stack() const                             { return on_stack_flag(); }
 697   void set_on_stack(const bool value);
 698 
 699   void record_gc_epoch();
 700 
 701   // see the definition in Method*.cpp for the gory details
 702   bool should_not_be_cached() const;
 703 
 704   // Rewriting support
 705   static methodHandle clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,
 706                                           u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS);
 707 
 708   // jmethodID handling
 709   // Because the useful life-span of a jmethodID cannot be determined,
 710   // once created they are never reclaimed.  The methods to which they refer,
 711   // however, can be GC'ed away if the class is unloaded or if the method is
 712   // made obsolete or deleted -- in these cases, the jmethodID
 713   // refers to null (as is the case for any weak reference).
 714   static jmethodID make_jmethod_id(ClassLoaderData* cld, Method* mh);
 715 
 716   // Ensure there is enough capacity in the internal tracking data
 717   // structures to hold the number of jmethodIDs you plan to generate.
 718   // This saves substantial time doing allocations.
 719   static void ensure_jmethod_ids(ClassLoaderData* cld, int capacity);
 720 
 721   // Use resolve_jmethod_id() in situations where the caller is expected
 722   // to provide a valid jmethodID; the only sanity checks are in asserts;
 723   // result guaranteed not to be null.
 724   inline static Method* resolve_jmethod_id(jmethodID mid) {
 725     assert(mid != nullptr, "JNI method id should not be null");
 726     return *((Method**)mid);
 727   }
 728 
 729   // Use checked_resolve_jmethod_id() in situations where the caller
 730   // should provide a valid jmethodID, but might not. Null is returned
 731   // when the jmethodID does not refer to a valid method.
 732   static Method* checked_resolve_jmethod_id(jmethodID mid);
 733 
 734   static void change_method_associated_with_jmethod_id(jmethodID old_jmid_ptr, Method* new_method);
 735   static bool is_method_id(jmethodID mid);
 736 
 737   // Clear methods
 738   static void clear_jmethod_ids(ClassLoaderData* loader_data);
 739   void clear_jmethod_id();
 740   static void print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) PRODUCT_RETURN;
 741 
 742   // Get this method's jmethodID -- allocate if it doesn't exist
 743   jmethodID jmethod_id();
 744 
 745   // Lookup the jmethodID for this method.  Return null if not found.
 746   // NOTE that this function can be called from a signal handler
 747   // (see AsyncGetCallTrace support for Forte Analyzer) and this
 748   // needs to be async-safe. No allocation should be done and
 749   // so handles are not used to avoid deadlock.
 750   jmethodID find_jmethod_id_or_null()               { return method_holder()->jmethod_id_or_null(this); }
 751 
 752   // Support for inlining of intrinsic methods
 753   vmIntrinsicID intrinsic_id() const          { return (vmIntrinsicID) _intrinsic_id;           }
 754   void     set_intrinsic_id(vmIntrinsicID id) {                           _intrinsic_id = (u2) id; }
 755 
 756   // Helper routines for intrinsic_id() and vmIntrinsics::method().
 757   void init_intrinsic_id(vmSymbolID klass_id);     // updates from _none if a match
 758   static vmSymbolID klass_id_for_intrinsics(const Klass* holder);
 759 
 760   bool caller_sensitive() const     { return constMethod()->caller_sensitive(); }
 761   void set_caller_sensitive() { constMethod()->set_caller_sensitive(); }
 762 
 763   bool changes_current_thread() const { return constMethod()->changes_current_thread(); }
 764   void set_changes_current_thread() { constMethod()->set_changes_current_thread(); }
 765 
 766   bool jvmti_hide_events() const { return constMethod()->jvmti_hide_events(); }
 767   void set_jvmti_hide_events() { constMethod()->set_jvmti_hide_events(); }
 768 
 769   bool jvmti_mount_transition() const { return constMethod()->jvmti_mount_transition(); }
 770   void set_jvmti_mount_transition() { constMethod()->set_jvmti_mount_transition(); }
 771 
 772   bool is_hidden() const { return constMethod()->is_hidden(); }
 773   void set_is_hidden() { constMethod()->set_is_hidden(); }
 774 
 775   bool is_scoped() const { return constMethod()->is_scoped(); }
 776   void set_scoped() { constMethod()->set_is_scoped(); }
 777 
 778   bool intrinsic_candidate() const { return constMethod()->intrinsic_candidate(); }
 779   void set_intrinsic_candidate() { constMethod()->set_intrinsic_candidate(); }
 780 
 781   bool has_injected_profile() const { return constMethod()->has_injected_profile(); }
 782   void set_has_injected_profile() { constMethod()->set_has_injected_profile(); }
 783 
 784   bool has_reserved_stack_access() const { return constMethod()->reserved_stack_access(); }
 785   void set_has_reserved_stack_access() { constMethod()->set_reserved_stack_access(); }
 786 
 787   bool is_scalarized_arg(int idx) const;
 788 
 789   bool c1_needs_stack_repair() const { return constMethod()->c1_needs_stack_repair(); }
 790   void set_c1_needs_stack_repair() { constMethod()->set_c1_needs_stack_repair(); }
 791 
 792   bool c2_needs_stack_repair() const { return constMethod()->c2_needs_stack_repair(); }
 793   void set_c2_needs_stack_repair() { constMethod()->set_c2_needs_stack_repair(); }
 794 
 795   bool mismatch() const { return constMethod()->mismatch(); }
 796   void set_mismatch() { constMethod()->set_mismatch(); }
 797 
 798   JFR_ONLY(DEFINE_TRACE_FLAG_ACCESSOR;)
 799 
 800   ConstMethod::MethodType method_type() const {
 801       return _constMethod->method_type();
 802   }
 803   bool is_overpass() const { return method_type() == ConstMethod::OVERPASS; }
 804 
 805   // On-stack replacement support
 806   bool has_osr_nmethod(int level, bool match_level) {
 807    return method_holder()->lookup_osr_nmethod(this, InvocationEntryBci, level, match_level) != nullptr;
 808   }
 809 
 810   nmethod* lookup_osr_nmethod_for(int bci, int level, bool match_level) {
 811     return method_holder()->lookup_osr_nmethod(this, bci, level, match_level);
 812   }
 813 
 814   // Find if klass for method is loaded
 815   bool is_klass_loaded_by_klass_index(int klass_index) const;
 816   bool is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved = false) const;
 817 
 818   // Indicates whether compilation failed earlier for this method, or
 819   // whether it is not compilable for another reason like having a
 820   // breakpoint set in it.
 821   bool  is_not_compilable(int comp_level = CompLevel_any) const;
 822   void set_not_compilable(const char* reason, int comp_level = CompLevel_all, bool report = true);
 823   void set_not_compilable_quietly(const char* reason, int comp_level = CompLevel_all) {
 824     set_not_compilable(reason, comp_level, false);
 825   }
 826   bool  is_not_osr_compilable(int comp_level = CompLevel_any) const;
 827   void set_not_osr_compilable(const char* reason, int comp_level = CompLevel_all, bool report = true);
 828   void set_not_osr_compilable_quietly(const char* reason, int comp_level = CompLevel_all) {
 829     set_not_osr_compilable(reason, comp_level, false);
 830   }
 831   bool is_always_compilable() const;
 832 
 833  private:
 834   void print_made_not_compilable(int comp_level, bool is_osr, bool report, const char* reason);
 835 
 836  public:
 837   MethodCounters* get_method_counters(Thread* current) {
 838     if (_method_counters == nullptr) {
 839       build_method_counters(current, this);
 840     }
 841     return _method_counters;
 842   }
 843 
 844   void clear_is_not_c1_compilable()           { set_is_not_c1_compilable(false); }
 845   void clear_is_not_c2_compilable()           { set_is_not_c2_compilable(false); }
 846   void clear_is_not_c2_osr_compilable()       { set_is_not_c2_osr_compilable(false); }
 847 
 848   // not_c1_osr_compilable == not_c1_compilable
 849   bool is_not_c1_osr_compilable() const       { return is_not_c1_compilable(); }
 850   void set_is_not_c1_osr_compilable()         { set_is_not_c1_compilable(); }
 851   void clear_is_not_c1_osr_compilable()       { clear_is_not_c1_compilable(); }
 852 
 853   // Background compilation support
 854   void clear_queued_for_compilation()  { set_queued_for_compilation(false);   }
 855 
 856   // Resolve all classes in signature, return 'true' if successful
 857   static bool load_signature_classes(const methodHandle& m, TRAPS);
 858 
 859   // Printing
 860   void print_short_name(outputStream* st = tty) const; // prints as klassname::methodname; Exposed so field engineers can debug VM
 861 #if INCLUDE_JVMTI
 862   void print_name(outputStream* st = tty) const; // prints as "virtual void foo(int)"; exposed for -Xlog:redefine+class
 863 #else
 864   void print_name(outputStream* st = tty) const  PRODUCT_RETURN; // prints as "virtual void foo(int)"
 865 #endif
 866 
 867   typedef int (*method_comparator_func)(Method* a, Method* b);
 868 
 869   // Helper routine used for method sorting
 870   static void sort_methods(Array<Method*>* methods, bool set_idnums = true, method_comparator_func func = nullptr);
 871 
 872   // Deallocation function for redefine classes or if an error occurs
 873   void deallocate_contents(ClassLoaderData* loader_data);
 874 
 875   void release_C_heap_structures();
 876 
 877   Method* get_new_method() const {
 878     InstanceKlass* holder = method_holder();
 879     Method* new_method = holder->method_with_idnum(orig_method_idnum());
 880 
 881     assert(new_method != nullptr, "method_with_idnum() should not be null");
 882     assert(this != new_method, "sanity check");
 883     return new_method;
 884   }
 885 
 886   // Printing
 887 #ifndef PRODUCT
 888   void print_on(outputStream* st) const;
 889 #endif
 890   void print_value_on(outputStream* st) const;
 891   void print_linkage_flags(outputStream* st) PRODUCT_RETURN;
 892 
 893   const char* internal_name() const { return "{method}"; }
 894 
 895   // Check for valid method pointer
 896   static bool has_method_vptr(const void* ptr);
 897   static bool is_valid_method(const Method* m);
 898 
 899   // Verify
 900   void verify() { verify_on(tty); }
 901   void verify_on(outputStream* st);
 902 
 903  private:
 904 
 905   // Inlined elements
 906   address* native_function_addr() const          { assert(is_native(), "must be native"); return (address*) (this+1); }
 907   address* signature_handler_addr() const        { return native_function_addr() + 1; }
 908 };
 909 
 910 
 911 // Utility class for compressing line number tables
 912 
 913 class CompressedLineNumberWriteStream: public CompressedWriteStream {
 914  private:
 915   int _bci;
 916   int _line;
 917  public:
 918   // Constructor
 919   CompressedLineNumberWriteStream(int initial_size) : CompressedWriteStream(initial_size), _bci(0), _line(0) {}
 920   CompressedLineNumberWriteStream(u_char* buffer, int initial_size) : CompressedWriteStream(buffer, initial_size), _bci(0), _line(0) {}
 921 
 922   // Write (bci, line number) pair to stream
 923   void write_pair_regular(int bci_delta, int line_delta);
 924 
 925   // If (bci delta, line delta) fits in (5-bit unsigned, 3-bit unsigned)
 926   // we save it as one byte, otherwise we write a 0xFF escape character
 927   // and use regular compression. 0x0 is used as end-of-stream terminator.
 928   void write_pair_inline(int bci, int line);
 929 
 930   void write_pair(int bci, int line);
 931 
 932   // Write end-of-stream marker
 933   void write_terminator()                        { write_byte(0); }
 934 };
 935 
 936 
 937 // Utility class for decompressing line number tables
 938 
 939 class CompressedLineNumberReadStream: public CompressedReadStream {
 940  private:
 941   int _bci;
 942   int _line;
 943  public:
 944   // Constructor
 945   CompressedLineNumberReadStream(u_char* buffer);
 946   // Read (bci, line number) pair from stream. Returns false at end-of-stream.
 947   bool read_pair();
 948   // Accessing bci and line number (after calling read_pair)
 949   int bci() const                               { return _bci; }
 950   int line() const                              { return _line; }
 951 };
 952 
 953 
 954 #if INCLUDE_JVMTI
 955 
 956 /// Fast Breakpoints.
 957 
 958 // If this structure gets more complicated (because bpts get numerous),
 959 // move it into its own header.
 960 
 961 // There is presently no provision for concurrent access
 962 // to breakpoint lists, which is only OK for JVMTI because
 963 // breakpoints are written only at safepoints, and are read
 964 // concurrently only outside of safepoints.
 965 
 966 class BreakpointInfo : public CHeapObj<mtClass> {
 967   friend class VMStructs;
 968  private:
 969   Bytecodes::Code  _orig_bytecode;
 970   int              _bci;
 971   u2               _name_index;       // of method
 972   u2               _signature_index;  // of method
 973   BreakpointInfo*  _next;             // simple storage allocation
 974 
 975  public:
 976   BreakpointInfo(Method* m, int bci);
 977 
 978   // accessors
 979   Bytecodes::Code orig_bytecode()                     { return _orig_bytecode; }
 980   void        set_orig_bytecode(Bytecodes::Code code) { _orig_bytecode = code; }
 981   int         bci()                                   { return _bci; }
 982 
 983   BreakpointInfo*          next() const               { return _next; }
 984   void                 set_next(BreakpointInfo* n)    { _next = n; }
 985 
 986   // helps for searchers
 987   bool match(const Method* m, int bci) {
 988     return bci == _bci && match(m);
 989   }
 990 
 991   bool match(const Method* m) {
 992     return _name_index == m->name_index() &&
 993       _signature_index == m->signature_index();
 994   }
 995 
 996   void set(Method* method);
 997   void clear(Method* method);
 998 };
 999 
1000 #endif // INCLUDE_JVMTI
1001 
1002 // Utility class for access exception handlers
1003 class ExceptionTable : public StackObj {
1004  private:
1005   ExceptionTableElement* _table;
1006   u2  _length;
1007 
1008  public:
1009   ExceptionTable(const Method* m) {
1010     if (m->has_exception_handler()) {
1011       _table = m->exception_table_start();
1012       _length = m->exception_table_length();
1013     } else {
1014       _table = nullptr;
1015       _length = 0;
1016     }
1017   }
1018 
1019   u2 length() const {
1020     return _length;
1021   }
1022 
1023   u2 start_pc(int idx) const {
1024     assert(idx < _length, "out of bounds");
1025     return _table[idx].start_pc;
1026   }
1027 
1028   void set_start_pc(int idx, u2 value) {
1029     assert(idx < _length, "out of bounds");
1030     _table[idx].start_pc = value;
1031   }
1032 
1033   u2 end_pc(int idx) const {
1034     assert(idx < _length, "out of bounds");
1035     return _table[idx].end_pc;
1036   }
1037 
1038   void set_end_pc(int idx, u2 value) {
1039     assert(idx < _length, "out of bounds");
1040     _table[idx].end_pc = value;
1041   }
1042 
1043   u2 handler_pc(int idx) const {
1044     assert(idx < _length, "out of bounds");
1045     return _table[idx].handler_pc;
1046   }
1047 
1048   void set_handler_pc(int idx, u2 value) {
1049     assert(idx < _length, "out of bounds");
1050     _table[idx].handler_pc = value;
1051   }
1052 
1053   u2 catch_type_index(int idx) const {
1054     assert(idx < _length, "out of bounds");
1055     return _table[idx].catch_type_index;
1056   }
1057 
1058   void set_catch_type_index(int idx, u2 value) {
1059     assert(idx < _length, "out of bounds");
1060     _table[idx].catch_type_index = value;
1061   }
1062 };
1063 
1064 #endif // SHARE_OOPS_METHOD_HPP