1 /*
   2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_CODE_NMETHOD_HPP
  26 #define SHARE_CODE_NMETHOD_HPP
  27 
  28 #include "code/codeBlob.hpp"
  29 #include "code/pcDesc.hpp"
  30 #include "oops/metadata.hpp"
  31 #include "oops/method.hpp"
  32 
  33 class AbstractCompiler;
  34 class CompiledDirectCall;
  35 class CompiledIC;
  36 class CompiledICData;
  37 class CompileTask;
  38 class DepChange;
  39 class Dependencies;
  40 class DirectiveSet;
  41 class DebugInformationRecorder;
  42 class ExceptionHandlerTable;
  43 class ImplicitExceptionTable;
  44 class JvmtiThreadState;
  45 class MetadataClosure;
  46 class NativeCallWrapper;
  47 class OopIterateClosure;
  48 class ScopeDesc;
  49 class xmlStream;
  50 
  51 // This class is used internally by nmethods, to cache
  52 // exception/pc/handler information.
  53 
  54 class ExceptionCache : public CHeapObj<mtCode> {
  55   friend class VMStructs;
  56  private:
  57   enum { cache_size = 16 };
  58   Klass*   _exception_type;
  59   address  _pc[cache_size];
  60   address  _handler[cache_size];
  61   volatile int _count;
  62   ExceptionCache* volatile _next;
  63   ExceptionCache* _purge_list_next;
  64 
  65   inline address pc_at(int index);
  66   void set_pc_at(int index, address a)      { assert(index >= 0 && index < cache_size,""); _pc[index] = a; }
  67 
  68   inline address handler_at(int index);
  69   void set_handler_at(int index, address a) { assert(index >= 0 && index < cache_size,""); _handler[index] = a; }
  70 
  71   inline int count();
  72   // increment_count is only called under lock, but there may be concurrent readers.
  73   void increment_count();
  74 
  75  public:
  76 
  77   ExceptionCache(Handle exception, address pc, address handler);
  78 
  79   Klass*    exception_type()                { return _exception_type; }
  80   ExceptionCache* next();
  81   void      set_next(ExceptionCache *ec);
  82   ExceptionCache* purge_list_next()                 { return _purge_list_next; }
  83   void      set_purge_list_next(ExceptionCache *ec) { _purge_list_next = ec; }
  84 
  85   address match(Handle exception, address pc);
  86   bool    match_exception_with_space(Handle exception) ;
  87   address test_address(address addr);
  88   bool    add_address_and_handler(address addr, address handler) ;
  89 };
  90 
  91 // cache pc descs found in earlier inquiries
  92 class PcDescCache {
  93   friend class VMStructs;
  94  private:
  95   enum { cache_size = 4 };
  96   // The array elements MUST be volatile! Several threads may modify
  97   // and read from the cache concurrently. find_pc_desc_internal has
  98   // returned wrong results. C++ compiler (namely xlC12) may duplicate
  99   // C++ field accesses if the elements are not volatile.
 100   typedef PcDesc* PcDescPtr;
 101   volatile PcDescPtr _pc_descs[cache_size]; // last cache_size pc_descs found
 102  public:
 103   PcDescCache() { debug_only(_pc_descs[0] = nullptr); }
 104   void    init_to(PcDesc* initial_pc_desc);
 105   PcDesc* find_pc_desc(int pc_offset, bool approximate);
 106   void    add_pc_desc(PcDesc* pc_desc);
 107   PcDesc* last_pc_desc() { return _pc_descs[0]; }
 108 };
 109 
 110 class PcDescContainer : public CHeapObj<mtCode> {
 111 private:
 112   PcDescCache _pc_desc_cache;
 113 public:
 114   PcDescContainer(PcDesc* initial_pc_desc) { _pc_desc_cache.init_to(initial_pc_desc); }
 115 
 116   PcDesc* find_pc_desc_internal(address pc, bool approximate, address code_begin,
 117                                 PcDesc* lower, PcDesc* upper);
 118 
 119   PcDesc* find_pc_desc(address pc, bool approximate, address code_begin, PcDesc* lower, PcDesc* upper)
 120 #ifdef PRODUCT
 121   {
 122     PcDesc* desc = _pc_desc_cache.last_pc_desc();
 123     assert(desc != nullptr, "PcDesc cache should be initialized already");
 124     if (desc->pc_offset() == (pc - code_begin)) {
 125       // Cached value matched
 126       return desc;
 127     }
 128     return find_pc_desc_internal(pc, approximate, code_begin, lower, upper);
 129   }
 130 #endif
 131   ;
 132 };
 133 
 134 // nmethods (native methods) are the compiled code versions of Java methods.
 135 //
 136 // An nmethod contains:
 137 //  - header                 (the nmethod structure)
 138 //  [Relocation]
 139 //  - relocation information
 140 //  - constant part          (doubles, longs and floats used in nmethod)
 141 //  - oop table
 142 //  [Code]
 143 //  - code body
 144 //  - exception handler
 145 //  - stub code
 146 //  [Debugging information]
 147 //  - oop array
 148 //  - data array
 149 //  - pcs
 150 //  [Exception handler table]
 151 //  - handler entry point array
 152 //  [Implicit Null Pointer exception table]
 153 //  - implicit null table array
 154 //  [Speculations]
 155 //  - encoded speculations array
 156 //  [JVMCINMethodData]
 157 //  - meta data for JVMCI compiled nmethod
 158 
 159 #if INCLUDE_JVMCI
 160 class FailedSpeculation;
 161 class JVMCINMethodData;
 162 #endif
 163 
 164 class nmethod : public CodeBlob {
 165   friend class VMStructs;
 166   friend class JVMCIVMStructs;
 167   friend class CodeCache;  // scavengable oops
 168   friend class JVMCINMethodData;
 169   friend class DeoptimizationScope;
 170 
 171  private:
 172 
 173   // Used to track in which deoptimize handshake this method will be deoptimized.
 174   uint64_t  _deoptimization_generation;
 175 
 176   uint64_t  _gc_epoch;
 177 
 178   Method*   _method;
 179 
 180   // To reduce header size union fields which usages do not overlap.
 181   union {
 182     // To support simple linked-list chaining of nmethods:
 183     nmethod*  _osr_link; // from InstanceKlass::osr_nmethods_head
 184     struct {
 185       // These are used for compiled synchronized native methods to
 186       // locate the owner and stack slot for the BasicLock. They are
 187       // needed because there is no debug information for compiled native
 188       // wrappers and the oop maps are insufficient to allow
 189       // frame::retrieve_receiver() to work. Currently they are expected
 190       // to be byte offsets from the Java stack pointer for maximum code
 191       // sharing between platforms. JVMTI's GetLocalInstance() uses these
 192       // offsets to find the receiver for non-static native wrapper frames.
 193       ByteSize _native_receiver_sp_offset;
 194       ByteSize _native_basic_lock_sp_offset;
 195     };
 196   };
 197 
 198   // nmethod's read-only data
 199   address _immutable_data;
 200 
 201   PcDescContainer* _pc_desc_container;
 202   ExceptionCache* volatile _exception_cache;
 203 
 204   void* _gc_data;
 205 
 206   struct oops_do_mark_link; // Opaque data type.
 207   static nmethod*    volatile _oops_do_mark_nmethods;
 208   oops_do_mark_link* volatile _oops_do_mark_link;
 209 
 210   CompiledICData* _compiled_ic_data;
 211 
 212   // offsets for entry points
 213   address  _osr_entry_point;       // entry point for on stack replacement
 214   uint16_t _entry_offset;          // entry point with class check
 215   uint16_t _verified_entry_offset; // entry point without class check
 216   int      _entry_bci;             // != InvocationEntryBci if this nmethod is an on-stack replacement method
 217   int      _immutable_data_size;
 218 
 219   // _consts_offset == _content_offset because SECT_CONSTS is first in code buffer
 220 
 221   int _inline_insts_size;
 222 
 223   int _stub_offset;
 224 
 225   // Offsets for different stubs section parts
 226   int _exception_offset;
 227   // All deoptee's will resume execution at this location described by
 228   // this offset.
 229   int _deopt_handler_offset;
 230   // All deoptee's at a MethodHandle call site will resume execution
 231   // at this location described by this offset.
 232   int _deopt_mh_handler_offset;
 233   // Offset (from insts_end) of the unwind handler if it exists
 234   int16_t  _unwind_handler_offset;
 235   // Number of arguments passed on the stack
 236   uint16_t _num_stack_arg_slots;
 237 
 238   // Offsets in mutable data section
 239   // _oops_offset == _data_offset,  offset where embedded oop table begins (inside data)
 240   uint16_t _metadata_offset; // embedded meta data table
 241 #if INCLUDE_JVMCI
 242   uint16_t _jvmci_data_offset;
 243 #endif
 244 
 245   // Offset in immutable data section
 246   // _dependencies_offset == 0
 247   uint16_t _nul_chk_table_offset;
 248   uint16_t _handler_table_offset; // This table could be big in C1 code
 249   int      _scopes_pcs_offset;
 250   int      _scopes_data_offset;
 251 #if INCLUDE_JVMCI
 252   int      _speculations_offset;
 253 #endif
 254 
 255   // location in frame (offset for sp) that deopt can store the original
 256   // pc during a deopt.
 257   int _orig_pc_offset;
 258 
 259   int          _compile_id;            // which compilation made this nmethod
 260   CompLevel    _comp_level;            // compilation level (s1)
 261   CompilerType _compiler_type;         // which compiler made this nmethod (u1)
 262 
 263 #if INCLUDE_RTM_OPT
 264   // RTM state at compile time. Used during deoptimization to decide
 265   // whether to restart collecting RTM locking abort statistic again.
 266   RTMState _rtm_state;
 267 #endif
 268 
 269   // Local state used to keep track of whether unloading is happening or not
 270   volatile uint8_t _is_unloading_state;
 271 
 272   // Protected by NMethodState_lock
 273   volatile signed char _state;         // {not_installed, in_use, not_entrant}
 274 
 275   // set during construction
 276   uint8_t _has_unsafe_access:1,        // May fault due to unsafe access.
 277           _has_method_handle_invokes:1,// Has this method MethodHandle invokes?
 278           _has_wide_vectors:1,         // Preserve wide vectors at safepoints
 279           _has_monitors:1,             // Fastpath monitor detection for continuations
 280           _has_flushed_dependencies:1, // Used for maintenance of dependencies (under CodeCache_lock)
 281           _is_unlinked:1,              // mark during class unloading
 282           _load_reported:1;            // used by jvmti to track if an event has been posted for this nmethod
 283 
 284   enum DeoptimizationStatus : u1 {
 285     not_marked,
 286     deoptimize,
 287     deoptimize_noupdate,
 288     deoptimize_done
 289   };
 290 
 291   volatile DeoptimizationStatus _deoptimization_status; // Used for stack deoptimization
 292 
 293   DeoptimizationStatus deoptimization_status() const {
 294     return Atomic::load(&_deoptimization_status);
 295   }
 296 
 297   // Initialize fields to their default values
 298   void init_defaults(CodeBuffer *code_buffer, CodeOffsets* offsets);
 299 
 300   // Post initialization
 301   void post_init();
 302 
 303   // For native wrappers
 304   nmethod(Method* method,
 305           CompilerType type,
 306           int nmethod_size,
 307           int compile_id,
 308           CodeOffsets* offsets,
 309           CodeBuffer *code_buffer,
 310           int frame_size,
 311           ByteSize basic_lock_owner_sp_offset, /* synchronized natives only */
 312           ByteSize basic_lock_sp_offset,       /* synchronized natives only */
 313           OopMapSet* oop_maps);
 314 
 315   // For normal JIT compiled code
 316   nmethod(Method* method,
 317           CompilerType type,
 318           int nmethod_size,
 319           int immutable_data_size,
 320           int compile_id,
 321           int entry_bci,
 322           address immutable_data,
 323           CodeOffsets* offsets,
 324           int orig_pc_offset,
 325           DebugInformationRecorder *recorder,
 326           Dependencies* dependencies,
 327           CodeBuffer *code_buffer,
 328           int frame_size,
 329           OopMapSet* oop_maps,
 330           ExceptionHandlerTable* handler_table,
 331           ImplicitExceptionTable* nul_chk_table,
 332           AbstractCompiler* compiler,
 333           CompLevel comp_level
 334 #if INCLUDE_JVMCI
 335           , char* speculations = nullptr,
 336           int speculations_len = 0,
 337           JVMCINMethodData* jvmci_data = nullptr
 338 #endif
 339           );
 340 
 341   // helper methods
 342   void* operator new(size_t size, int nmethod_size, int comp_level) throw();
 343 
 344   // For method handle intrinsics: Try MethodNonProfiled, MethodProfiled and NonNMethod.
 345   // Attention: Only allow NonNMethod space for special nmethods which don't need to be
 346   // findable by nmethod iterators! In particular, they must not contain oops!
 347   void* operator new(size_t size, int nmethod_size, bool allow_NonNMethod_space) throw();
 348 
 349   const char* reloc_string_for(u_char* begin, u_char* end);
 350 
 351   bool try_transition(signed char new_state);
 352 
 353   // Returns true if this thread changed the state of the nmethod or
 354   // false if another thread performed the transition.
 355   bool make_entrant() { Unimplemented(); return false; }
 356   void inc_decompile_count();
 357 
 358   // Inform external interfaces that a compiled method has been unloaded
 359   void post_compiled_method_unload();
 360 
 361   PcDesc* find_pc_desc(address pc, bool approximate) {
 362     if (_pc_desc_container == nullptr) return nullptr; // native method
 363     return _pc_desc_container->find_pc_desc(pc, approximate, code_begin(), scopes_pcs_begin(), scopes_pcs_end());
 364   }
 365 
 366   // STW two-phase nmethod root processing helpers.
 367   //
 368   // When determining liveness of a given nmethod to do code cache unloading,
 369   // some collectors need to do different things depending on whether the nmethods
 370   // need to absolutely be kept alive during root processing; "strong"ly reachable
 371   // nmethods are known to be kept alive at root processing, but the liveness of
 372   // "weak"ly reachable ones is to be determined later.
 373   //
 374   // We want to allow strong and weak processing of nmethods by different threads
 375   // at the same time without heavy synchronization. Additional constraints are
 376   // to make sure that every nmethod is processed a minimal amount of time, and
 377   // nmethods themselves are always iterated at most once at a particular time.
 378   //
 379   // Note that strong processing work must be a superset of weak processing work
 380   // for this code to work.
 381   //
 382   // We store state and claim information in the _oops_do_mark_link member, using
 383   // the two LSBs for the state and the remaining upper bits for linking together
 384   // nmethods that were already visited.
 385   // The last element is self-looped, i.e. points to itself to avoid some special
 386   // "end-of-list" sentinel value.
 387   //
 388   // _oops_do_mark_link special values:
 389   //
 390   //   _oops_do_mark_link == nullptr: the nmethod has not been visited at all yet, i.e.
 391   //      is Unclaimed.
 392   //
 393   // For other values, its lowest two bits indicate the following states of the nmethod:
 394   //
 395   //   weak_request (WR): the nmethod has been claimed by a thread for weak processing
 396   //   weak_done (WD): weak processing has been completed for this nmethod.
 397   //   strong_request (SR): the nmethod has been found to need strong processing while
 398   //       being weak processed.
 399   //   strong_done (SD): strong processing has been completed for this nmethod .
 400   //
 401   // The following shows the _only_ possible progressions of the _oops_do_mark_link
 402   // pointer.
 403   //
 404   // Given
 405   //   N as the nmethod
 406   //   X the current next value of _oops_do_mark_link
 407   //
 408   // Unclaimed (C)-> N|WR (C)-> X|WD: the nmethod has been processed weakly by
 409   //   a single thread.
 410   // Unclaimed (C)-> N|WR (C)-> X|WD (O)-> X|SD: after weak processing has been
 411   //   completed (as above) another thread found that the nmethod needs strong
 412   //   processing after all.
 413   // Unclaimed (C)-> N|WR (O)-> N|SR (C)-> X|SD: during weak processing another
 414   //   thread finds that the nmethod needs strong processing, marks it as such and
 415   //   terminates. The original thread completes strong processing.
 416   // Unclaimed (C)-> N|SD (C)-> X|SD: the nmethod has been processed strongly from
 417   //   the beginning by a single thread.
 418   //
 419   // "|" describes the concatenation of bits in _oops_do_mark_link.
 420   //
 421   // The diagram also describes the threads responsible for changing the nmethod to
 422   // the next state by marking the _transition_ with (C) and (O), which mean "current"
 423   // and "other" thread respectively.
 424   //
 425 
 426   // States used for claiming nmethods during root processing.
 427   static const uint claim_weak_request_tag = 0;
 428   static const uint claim_weak_done_tag = 1;
 429   static const uint claim_strong_request_tag = 2;
 430   static const uint claim_strong_done_tag = 3;
 431 
 432   static oops_do_mark_link* mark_link(nmethod* nm, uint tag) {
 433     assert(tag <= claim_strong_done_tag, "invalid tag %u", tag);
 434     assert(is_aligned(nm, 4), "nmethod pointer must have zero lower two LSB");
 435     return (oops_do_mark_link*)(((uintptr_t)nm & ~0x3) | tag);
 436   }
 437 
 438   static uint extract_state(oops_do_mark_link* link) {
 439     return (uint)((uintptr_t)link & 0x3);
 440   }
 441 
 442   static nmethod* extract_nmethod(oops_do_mark_link* link) {
 443     return (nmethod*)((uintptr_t)link & ~0x3);
 444   }
 445 
 446   void oops_do_log_change(const char* state);
 447 
 448   static bool oops_do_has_weak_request(oops_do_mark_link* next) {
 449     return extract_state(next) == claim_weak_request_tag;
 450   }
 451 
 452   static bool oops_do_has_any_strong_state(oops_do_mark_link* next) {
 453     return extract_state(next) >= claim_strong_request_tag;
 454   }
 455 
 456   // Attempt Unclaimed -> N|WR transition. Returns true if successful.
 457   bool oops_do_try_claim_weak_request();
 458 
 459   // Attempt Unclaimed -> N|SD transition. Returns the current link.
 460   oops_do_mark_link* oops_do_try_claim_strong_done();
 461   // Attempt N|WR -> X|WD transition. Returns nullptr if successful, X otherwise.
 462   nmethod* oops_do_try_add_to_list_as_weak_done();
 463 
 464   // Attempt X|WD -> N|SR transition. Returns the current link.
 465   oops_do_mark_link* oops_do_try_add_strong_request(oops_do_mark_link* next);
 466   // Attempt X|WD -> X|SD transition. Returns true if successful.
 467   bool oops_do_try_claim_weak_done_as_strong_done(oops_do_mark_link* next);
 468 
 469   // Do the N|SD -> X|SD transition.
 470   void oops_do_add_to_list_as_strong_done();
 471 
 472   // Sets this nmethod as strongly claimed (as part of N|SD -> X|SD and N|SR -> X|SD
 473   // transitions).
 474   void oops_do_set_strong_done(nmethod* old_head);
 475 
 476 public:
 477   // create nmethod with entry_bci
 478   static nmethod* new_nmethod(const methodHandle& method,
 479                               int compile_id,
 480                               int entry_bci,
 481                               CodeOffsets* offsets,
 482                               int orig_pc_offset,
 483                               DebugInformationRecorder* recorder,
 484                               Dependencies* dependencies,
 485                               CodeBuffer *code_buffer,
 486                               int frame_size,
 487                               OopMapSet* oop_maps,
 488                               ExceptionHandlerTable* handler_table,
 489                               ImplicitExceptionTable* nul_chk_table,
 490                               AbstractCompiler* compiler,
 491                               CompLevel comp_level
 492 #if INCLUDE_JVMCI
 493                               , char* speculations = nullptr,
 494                               int speculations_len = 0,
 495                               JVMCINMethodData* jvmci_data = nullptr
 496 #endif
 497   );
 498 
 499   static nmethod* new_native_nmethod(const methodHandle& method,
 500                                      int compile_id,
 501                                      CodeBuffer *code_buffer,
 502                                      int vep_offset,
 503                                      int frame_complete,
 504                                      int frame_size,
 505                                      ByteSize receiver_sp_offset,
 506                                      ByteSize basic_lock_sp_offset,
 507                                      OopMapSet* oop_maps,
 508                                      int exception_handler = -1);
 509 
 510   Method* method       () const { return _method; }
 511   bool is_native_method() const { return _method != nullptr && _method->is_native(); }
 512   bool is_java_method  () const { return _method != nullptr && !_method->is_native(); }
 513   bool is_osr_method   () const { return _entry_bci != InvocationEntryBci; }
 514 
 515   // Compiler task identification.  Note that all OSR methods
 516   // are numbered in an independent sequence if CICountOSR is true,
 517   // and native method wrappers are also numbered independently if
 518   // CICountNative is true.
 519   int compile_id() const { return _compile_id; }
 520   const char* compile_kind() const;
 521 
 522   inline bool  is_compiled_by_c1   () const { return _compiler_type == compiler_c1; }
 523   inline bool  is_compiled_by_c2   () const { return _compiler_type == compiler_c2; }
 524   inline bool  is_compiled_by_jvmci() const { return _compiler_type == compiler_jvmci; }
 525   CompilerType compiler_type       () const { return _compiler_type; }
 526   const char*  compiler_name       () const;
 527 
 528   // boundaries for different parts
 529   address consts_begin          () const { return           content_begin(); }
 530   address consts_end            () const { return           code_begin()   ; }
 531   address insts_begin           () const { return           code_begin()   ; }
 532   address insts_end             () const { return           header_begin() + _stub_offset             ; }
 533   address stub_begin            () const { return           header_begin() + _stub_offset             ; }
 534   address stub_end              () const { return           data_begin()   ; }
 535   address exception_begin       () const { return           header_begin() + _exception_offset        ; }
 536   address deopt_handler_begin   () const { return           header_begin() + _deopt_handler_offset    ; }
 537   address deopt_mh_handler_begin() const { return           header_begin() + _deopt_mh_handler_offset ; }
 538   address unwind_handler_begin  () const { return _unwind_handler_offset != -1 ? (insts_end() - _unwind_handler_offset) : nullptr; }
 539 
 540   // mutable data
 541   oop*    oops_begin            () const { return (oop*)        data_begin(); }
 542   oop*    oops_end              () const { return (oop*)       (data_begin() + _metadata_offset)      ; }
 543   Metadata** metadata_begin     () const { return (Metadata**) (data_begin() + _metadata_offset)      ; }
 544 #if INCLUDE_JVMCI
 545   Metadata** metadata_end       () const { return (Metadata**) (data_begin() + _jvmci_data_offset)    ; }
 546   address jvmci_data_begin      () const { return               data_begin() + _jvmci_data_offset     ; }
 547   address jvmci_data_end        () const { return               data_end(); }
 548 #else
 549   Metadata** metadata_end       () const { return (Metadata**)  data_end(); }
 550 #endif
 551 
 552   // immutable data
 553   address immutable_data_begin  () const { return           _immutable_data; }
 554   address immutable_data_end    () const { return           _immutable_data + _immutable_data_size ; }
 555   address dependencies_begin    () const { return           _immutable_data; }
 556   address dependencies_end      () const { return           _immutable_data + _nul_chk_table_offset; }
 557   address nul_chk_table_begin   () const { return           _immutable_data + _nul_chk_table_offset; }
 558   address nul_chk_table_end     () const { return           _immutable_data + _handler_table_offset; }
 559   address handler_table_begin   () const { return           _immutable_data + _handler_table_offset; }
 560   address handler_table_end     () const { return           _immutable_data + _scopes_pcs_offset   ; }
 561   PcDesc* scopes_pcs_begin      () const { return (PcDesc*)(_immutable_data + _scopes_pcs_offset)  ; }
 562   PcDesc* scopes_pcs_end        () const { return (PcDesc*)(_immutable_data + _scopes_data_offset) ; }
 563   address scopes_data_begin     () const { return           _immutable_data + _scopes_data_offset  ; }
 564 
 565 #if INCLUDE_JVMCI
 566   address scopes_data_end       () const { return           _immutable_data + _speculations_offset ; }
 567   address speculations_begin    () const { return           _immutable_data + _speculations_offset ; }
 568   address speculations_end      () const { return            immutable_data_end(); }
 569 #else
 570   address scopes_data_end       () const { return            immutable_data_end(); }
 571 #endif
 572 
 573   // Sizes
 574   int immutable_data_size() const { return _immutable_data_size; }
 575   int consts_size        () const { return int(          consts_end       () -           consts_begin       ()); }
 576   int insts_size         () const { return int(          insts_end        () -           insts_begin        ()); }
 577   int stub_size          () const { return int(          stub_end         () -           stub_begin         ()); }
 578   int oops_size          () const { return int((address) oops_end         () - (address) oops_begin         ()); }
 579   int metadata_size      () const { return int((address) metadata_end     () - (address) metadata_begin     ()); }
 580   int scopes_data_size   () const { return int(          scopes_data_end  () -           scopes_data_begin  ()); }
 581   int scopes_pcs_size    () const { return int((intptr_t)scopes_pcs_end   () - (intptr_t)scopes_pcs_begin   ()); }
 582   int dependencies_size  () const { return int(          dependencies_end () -           dependencies_begin ()); }
 583   int handler_table_size () const { return int(          handler_table_end() -           handler_table_begin()); }
 584   int nul_chk_table_size () const { return int(          nul_chk_table_end() -           nul_chk_table_begin()); }
 585 #if INCLUDE_JVMCI
 586   int speculations_size  () const { return int(          speculations_end () -           speculations_begin ()); }
 587   int jvmci_data_size    () const { return int(          jvmci_data_end   () -           jvmci_data_begin   ()); }
 588 #endif
 589 
 590   int     oops_count() const { assert(oops_size() % oopSize == 0, "");  return (oops_size() / oopSize) + 1; }
 591   int metadata_count() const { assert(metadata_size() % wordSize == 0, ""); return (metadata_size() / wordSize) + 1; }
 592 
 593   int inline_insts_size() const { return _inline_insts_size; }
 594   int total_size() const;
 595 
 596   // Containment
 597   bool consts_contains         (address addr) const { return consts_begin       () <= addr && addr < consts_end       (); }
 598   // Returns true if a given address is in the 'insts' section. The method
 599   // insts_contains_inclusive() is end-inclusive.
 600   bool insts_contains          (address addr) const { return insts_begin        () <= addr && addr < insts_end        (); }
 601   bool insts_contains_inclusive(address addr) const { return insts_begin        () <= addr && addr <= insts_end       (); }
 602   bool stub_contains           (address addr) const { return stub_begin         () <= addr && addr < stub_end         (); }
 603   bool oops_contains           (oop*    addr) const { return oops_begin         () <= addr && addr < oops_end         (); }
 604   bool metadata_contains       (Metadata** addr) const { return metadata_begin  () <= addr && addr < metadata_end     (); }
 605   bool scopes_data_contains    (address addr) const { return scopes_data_begin  () <= addr && addr < scopes_data_end  (); }
 606   bool scopes_pcs_contains     (PcDesc* addr) const { return scopes_pcs_begin   () <= addr && addr < scopes_pcs_end   (); }
 607   bool handler_table_contains  (address addr) const { return handler_table_begin() <= addr && addr < handler_table_end(); }
 608   bool nul_chk_table_contains  (address addr) const { return nul_chk_table_begin() <= addr && addr < nul_chk_table_end(); }
 609 
 610   // entry points
 611   address entry_point() const          { return code_begin() + _entry_offset;          } // normal entry point
 612   address verified_entry_point() const { return code_begin() + _verified_entry_offset; } // if klass is correct
 613 
 614   enum : signed char { not_installed = -1, // in construction, only the owner doing the construction is
 615                                            // allowed to advance state
 616                        in_use        = 0,  // executable nmethod
 617                        not_entrant   = 1   // marked for deoptimization but activations may still exist
 618   };
 619 
 620   // flag accessing and manipulation
 621   bool is_not_installed() const        { return _state == not_installed; }
 622   bool is_in_use() const               { return _state <= in_use; }
 623   bool is_not_entrant() const          { return _state == not_entrant; }
 624   int  get_state() const               { return _state; }
 625 
 626   void clear_unloading_state();
 627   // Heuristically deduce an nmethod isn't worth keeping around
 628   bool is_cold();
 629   bool is_unloading();
 630   void do_unloading(bool unloading_occurred);
 631 
 632 #if INCLUDE_RTM_OPT
 633   // rtm state accessing and manipulating
 634   RTMState  rtm_state() const          { return _rtm_state; }
 635   void set_rtm_state(RTMState state)   { _rtm_state = state; }
 636 #endif
 637 
 638   bool make_in_use() {
 639     return try_transition(in_use);
 640   }
 641   // Make the nmethod non entrant. The nmethod will continue to be
 642   // alive.  It is used when an uncommon trap happens.  Returns true
 643   // if this thread changed the state of the nmethod or false if
 644   // another thread performed the transition.
 645   bool  make_not_entrant();
 646   bool  make_not_used()    { return make_not_entrant(); }
 647 
 648   bool  is_marked_for_deoptimization() const { return deoptimization_status() != not_marked; }
 649   bool  has_been_deoptimized() const { return deoptimization_status() == deoptimize_done; }
 650   void  set_deoptimized_done();
 651 
 652   bool update_recompile_counts() const {
 653     // Update recompile counts when either the update is explicitly requested (deoptimize)
 654     // or the nmethod is not marked for deoptimization at all (not_marked).
 655     // The latter happens during uncommon traps when deoptimized nmethod is made not entrant.
 656     DeoptimizationStatus status = deoptimization_status();
 657     return status != deoptimize_noupdate && status != deoptimize_done;
 658   }
 659 
 660   // tells whether frames described by this nmethod can be deoptimized
 661   // note: native wrappers cannot be deoptimized.
 662   bool can_be_deoptimized() const { return is_java_method(); }
 663 
 664   bool has_dependencies()                         { return dependencies_size() != 0; }
 665   void print_dependencies_on(outputStream* out) PRODUCT_RETURN;
 666   void flush_dependencies();
 667 
 668   template<typename T>
 669   T* gc_data() const                              { return reinterpret_cast<T*>(_gc_data); }
 670   template<typename T>
 671   void set_gc_data(T* gc_data)                    { _gc_data = reinterpret_cast<void*>(gc_data); }
 672 
 673   bool  has_unsafe_access() const                 { return _has_unsafe_access; }
 674   void  set_has_unsafe_access(bool z)             { _has_unsafe_access = z; }
 675 
 676   bool  has_monitors() const                      { return _has_monitors; }
 677   void  set_has_monitors(bool z)                  { _has_monitors = z; }
 678 
 679   bool  has_method_handle_invokes() const         { return _has_method_handle_invokes; }
 680   void  set_has_method_handle_invokes(bool z)     { _has_method_handle_invokes = z; }
 681 
 682   bool  has_wide_vectors() const                  { return _has_wide_vectors; }
 683   void  set_has_wide_vectors(bool z)              { _has_wide_vectors = z; }
 684 
 685   bool  has_flushed_dependencies() const          { return _has_flushed_dependencies; }
 686   void  set_has_flushed_dependencies(bool z)      {
 687     assert(!has_flushed_dependencies(), "should only happen once");
 688     _has_flushed_dependencies = z;
 689   }
 690 
 691   bool  is_unlinked() const                       { return _is_unlinked; }
 692   void  set_is_unlinked()                         {
 693      assert(!_is_unlinked, "already unlinked");
 694       _is_unlinked = true;
 695   }
 696 
 697   int   comp_level() const                        { return _comp_level; }
 698 
 699   // Support for oops in scopes and relocs:
 700   // Note: index 0 is reserved for null.
 701   oop   oop_at(int index) const;
 702   oop   oop_at_phantom(int index) const; // phantom reference
 703   oop*  oop_addr_at(int index) const {  // for GC
 704     // relocation indexes are biased by 1 (because 0 is reserved)
 705     assert(index > 0 && index <= oops_count(), "must be a valid non-zero index");
 706     return &oops_begin()[index - 1];
 707   }
 708 
 709   // Support for meta data in scopes and relocs:
 710   // Note: index 0 is reserved for null.
 711   Metadata*   metadata_at(int index) const      { return index == 0 ? nullptr: *metadata_addr_at(index); }
 712   Metadata**  metadata_addr_at(int index) const {  // for GC
 713     // relocation indexes are biased by 1 (because 0 is reserved)
 714     assert(index > 0 && index <= metadata_count(), "must be a valid non-zero index");
 715     return &metadata_begin()[index - 1];
 716   }
 717 
 718   void copy_values(GrowableArray<jobject>* oops);
 719   void copy_values(GrowableArray<Metadata*>* metadata);
 720 
 721   // Relocation support
 722 private:
 723   void fix_oop_relocations(address begin, address end, bool initialize_immediates);
 724   inline void initialize_immediate_oop(oop* dest, jobject handle);
 725 
 726 protected:
 727   address oops_reloc_begin() const;
 728 
 729 public:
 730   void fix_oop_relocations(address begin, address end) { fix_oop_relocations(begin, end, false); }
 731   void fix_oop_relocations()                           { fix_oop_relocations(nullptr, nullptr, false); }
 732 
 733   bool is_at_poll_return(address pc);
 734   bool is_at_poll_or_poll_return(address pc);
 735 
 736 protected:
 737   // Exception cache support
 738   // Note: _exception_cache may be read and cleaned concurrently.
 739   ExceptionCache* exception_cache() const         { return _exception_cache; }
 740   ExceptionCache* exception_cache_acquire() const;
 741 
 742 public:
 743   address handler_for_exception_and_pc(Handle exception, address pc);
 744   void add_handler_for_exception_and_pc(Handle exception, address pc, address handler);
 745   void clean_exception_cache();
 746 
 747   void add_exception_cache_entry(ExceptionCache* new_entry);
 748   ExceptionCache* exception_cache_entry_for_exception(Handle exception);
 749 
 750 
 751   // MethodHandle
 752   bool is_method_handle_return(address return_pc);
 753   // Deopt
 754   // Return true is the PC is one would expect if the frame is being deopted.
 755   inline bool is_deopt_pc(address pc);
 756   inline bool is_deopt_mh_entry(address pc);
 757   inline bool is_deopt_entry(address pc);
 758 
 759   // Accessor/mutator for the original pc of a frame before a frame was deopted.
 760   address get_original_pc(const frame* fr) { return *orig_pc_addr(fr); }
 761   void    set_original_pc(const frame* fr, address pc) { *orig_pc_addr(fr) = pc; }
 762 
 763   const char* state() const;
 764 
 765   bool inlinecache_check_contains(address addr) const {
 766     return (addr >= code_begin() && addr < verified_entry_point());
 767   }
 768 
 769   void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f);
 770 
 771   // implicit exceptions support
 772   address continuation_for_implicit_div0_exception(address pc) { return continuation_for_implicit_exception(pc, true); }
 773   address continuation_for_implicit_null_exception(address pc) { return continuation_for_implicit_exception(pc, false); }
 774 
 775   // Inline cache support for class unloading and nmethod unloading
 776  private:
 777   void cleanup_inline_caches_impl(bool unloading_occurred, bool clean_all);
 778 
 779   address continuation_for_implicit_exception(address pc, bool for_div0_check);
 780 
 781  public:
 782   // Serial version used by whitebox test
 783   void cleanup_inline_caches_whitebox();
 784 
 785   void clear_inline_caches();
 786 
 787   // Execute nmethod barrier code, as if entering through nmethod call.
 788   void run_nmethod_entry_barrier();
 789 
 790   void verify_oop_relocations();
 791 
 792   bool has_evol_metadata();
 793 
 794   Method* attached_method(address call_pc);
 795   Method* attached_method_before_pc(address pc);
 796 
 797   // GC unloading support
 798   // Cleans unloaded klasses and unloaded nmethods in inline caches
 799 
 800   void unload_nmethod_caches(bool class_unloading_occurred);
 801 
 802   void unlink_from_method();
 803 
 804   // On-stack replacement support
 805   int      osr_entry_bci()    const { assert(is_osr_method(), "wrong kind of nmethod"); return _entry_bci; }
 806   address  osr_entry()        const { assert(is_osr_method(), "wrong kind of nmethod"); return _osr_entry_point; }
 807   nmethod* osr_link()         const { return _osr_link; }
 808   void     set_osr_link(nmethod *n) { _osr_link = n; }
 809   void     invalidate_osr_method();
 810 
 811   int num_stack_arg_slots(bool rounded = true) const {
 812     return rounded ? align_up(_num_stack_arg_slots, 2) : _num_stack_arg_slots;
 813   }
 814 
 815   // Verify calls to dead methods have been cleaned.
 816   void verify_clean_inline_caches();
 817 
 818   // Unlink this nmethod from the system
 819   void unlink();
 820 
 821   // Deallocate this nmethod - called by the GC
 822   void purge(bool unregister_nmethod);
 823 
 824   // See comment at definition of _last_seen_on_stack
 825   void mark_as_maybe_on_stack();
 826   bool is_maybe_on_stack();
 827 
 828   // Evolution support. We make old (discarded) compiled methods point to new Method*s.
 829   void set_method(Method* method) { _method = method; }
 830 
 831 #if INCLUDE_JVMCI
 832   // Gets the JVMCI name of this nmethod.
 833   const char* jvmci_name();
 834 
 835   // Records the pending failed speculation in the
 836   // JVMCI speculation log associated with this nmethod.
 837   void update_speculation(JavaThread* thread);
 838 
 839   // Gets the data specific to a JVMCI compiled method.
 840   // This returns a non-nullptr value iff this nmethod was
 841   // compiled by the JVMCI compiler.
 842   JVMCINMethodData* jvmci_nmethod_data() const {
 843     return jvmci_data_size() == 0 ? nullptr : (JVMCINMethodData*) jvmci_data_begin();
 844   }
 845 #endif
 846 
 847   void oops_do(OopClosure* f) { oops_do(f, false); }
 848   void oops_do(OopClosure* f, bool allow_dead);
 849 
 850   // All-in-one claiming of nmethods: returns true if the caller successfully claimed that
 851   // nmethod.
 852   bool oops_do_try_claim();
 853 
 854   // Loom support for following nmethods on the stack
 855   void follow_nmethod(OopIterateClosure* cl);
 856 
 857   // Class containing callbacks for the oops_do_process_weak/strong() methods
 858   // below.
 859   class OopsDoProcessor {
 860   public:
 861     // Process the oops of the given nmethod based on whether it has been called
 862     // in a weak or strong processing context, i.e. apply either weak or strong
 863     // work on it.
 864     virtual void do_regular_processing(nmethod* nm) = 0;
 865     // Assuming that the oops of the given nmethod has already been its weak
 866     // processing applied, apply the remaining strong processing part.
 867     virtual void do_remaining_strong_processing(nmethod* nm) = 0;
 868   };
 869 
 870   // The following two methods do the work corresponding to weak/strong nmethod
 871   // processing.
 872   void oops_do_process_weak(OopsDoProcessor* p);
 873   void oops_do_process_strong(OopsDoProcessor* p);
 874 
 875   static void oops_do_marking_prologue();
 876   static void oops_do_marking_epilogue();
 877 
 878  private:
 879   ScopeDesc* scope_desc_in(address begin, address end);
 880 
 881   address* orig_pc_addr(const frame* fr);
 882 
 883   // used by jvmti to track if the load events has been reported
 884   bool  load_reported() const                     { return _load_reported; }
 885   void  set_load_reported()                       { _load_reported = true; }
 886 
 887  public:
 888   // ScopeDesc retrieval operation
 889   PcDesc* pc_desc_at(address pc)   { return find_pc_desc(pc, false); }
 890   // pc_desc_near returns the first PcDesc at or after the given pc.
 891   PcDesc* pc_desc_near(address pc) { return find_pc_desc(pc, true); }
 892 
 893   // ScopeDesc for an instruction
 894   ScopeDesc* scope_desc_at(address pc);
 895   ScopeDesc* scope_desc_near(address pc);
 896 
 897   // copying of debugging information
 898   void copy_scopes_pcs(PcDesc* pcs, int count);
 899   void copy_scopes_data(address buffer, int size);
 900 
 901   int orig_pc_offset() { return _orig_pc_offset; }
 902 
 903   // Post successful compilation
 904   void post_compiled_method(CompileTask* task);
 905 
 906   // jvmti support:
 907   void post_compiled_method_load_event(JvmtiThreadState* state = nullptr);
 908 
 909   // verify operations
 910   void verify() override;
 911   void verify_scopes();
 912   void verify_interrupt_point(address interrupt_point, bool is_inline_cache);
 913 
 914   // Disassemble this nmethod with additional debug information, e.g. information about blocks.
 915   void decode2(outputStream* st) const;
 916   void print_constant_pool(outputStream* st);
 917 
 918   // Avoid hiding of parent's 'decode(outputStream*)' method.
 919   void decode(outputStream* st) const { decode2(st); } // just delegate here.
 920 
 921   // printing support
 922   void print()                 const override;
 923   void print(outputStream* st) const;
 924   void print_code();
 925 
 926 #if defined(SUPPORT_DATA_STRUCTS)
 927   // print output in opt build for disassembler library
 928   void print_relocations()                        PRODUCT_RETURN;
 929   void print_pcs_on(outputStream* st);
 930   void print_scopes() { print_scopes_on(tty); }
 931   void print_scopes_on(outputStream* st)          PRODUCT_RETURN;
 932   void print_value_on(outputStream* st) const override;
 933   void print_handler_table();
 934   void print_nul_chk_table();
 935   void print_recorded_oop(int log_n, int index);
 936   void print_recorded_oops();
 937   void print_recorded_metadata();
 938 
 939   void print_oops(outputStream* st);     // oops from the underlying CodeBlob.
 940   void print_metadata(outputStream* st); // metadata in metadata pool.
 941 #else
 942   void print_pcs_on(outputStream* st) { return; }
 943 #endif
 944 
 945   void print_calls(outputStream* st)              PRODUCT_RETURN;
 946   static void print_statistics()                  PRODUCT_RETURN;
 947 
 948   void maybe_print_nmethod(const DirectiveSet* directive);
 949   void print_nmethod(bool print_code);
 950 
 951   // need to re-define this from CodeBlob else the overload hides it
 952   void print_on(outputStream* st) const override { CodeBlob::print_on(st); }
 953   void print_on(outputStream* st, const char* msg) const;
 954 
 955   // Logging
 956   void log_identity(xmlStream* log) const;
 957   void log_new_nmethod() const;
 958   void log_state_change() const;
 959 
 960   // Prints block-level comments, including nmethod specific block labels:
 961   void print_block_comment(outputStream* stream, address block_begin) const override {
 962 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
 963     print_nmethod_labels(stream, block_begin);
 964     CodeBlob::print_block_comment(stream, block_begin);
 965 #endif
 966   }
 967 
 968   void print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels=true) const;
 969   const char* nmethod_section_label(address pos) const;
 970 
 971   // returns whether this nmethod has code comments.
 972   bool has_code_comment(address begin, address end);
 973   // Prints a comment for one native instruction (reloc info, pc desc)
 974   void print_code_comment_on(outputStream* st, int column, address begin, address end);
 975 
 976   // tells if this compiled method is dependent on the given changes,
 977   // and the changes have invalidated it
 978   bool check_dependency_on(DepChange& changes);
 979 
 980   // Fast breakpoint support. Tells if this compiled method is
 981   // dependent on the given method. Returns true if this nmethod
 982   // corresponds to the given method as well.
 983   bool is_dependent_on_method(Method* dependee);
 984 
 985   // JVMTI's GetLocalInstance() support
 986   ByteSize native_receiver_sp_offset() {
 987     assert(is_native_method(), "sanity");
 988     return _native_receiver_sp_offset;
 989   }
 990   ByteSize native_basic_lock_sp_offset() {
 991     assert(is_native_method(), "sanity");
 992     return _native_basic_lock_sp_offset;
 993   }
 994 
 995   // support for code generation
 996   static ByteSize osr_entry_point_offset() { return byte_offset_of(nmethod, _osr_entry_point); }
 997   static ByteSize state_offset()           { return byte_offset_of(nmethod, _state); }
 998 
 999   void metadata_do(MetadataClosure* f);
1000 
1001   address call_instruction_address(address pc) const;
1002 
1003   void make_deoptimized();
1004   void finalize_relocations();
1005 };
1006 
1007 #endif // SHARE_CODE_NMETHOD_HPP