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