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