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