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