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