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