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/compiledMethod.hpp"
 29 #include "compiler/compilerDefinitions.hpp"
 30 
 31 class CompiledICData;
 32 class CompileTask;
 33 class DepChange;
 34 class DirectiveSet;
 35 class DebugInformationRecorder;
 36 class JvmtiThreadState;
 37 class OopIterateClosure;
 38 
 39 // nmethods (native methods) are the compiled code versions of Java methods.
 40 //
 41 // An nmethod contains:
 42 //  - header                 (the nmethod structure)
 43 //  [Relocation]
 44 //  - relocation information
 45 //  - constant part          (doubles, longs and floats used in nmethod)
 46 //  - oop table
 47 //  [Code]
 48 //  - code body
 49 //  - exception handler
 50 //  - stub code
 51 //  [Debugging information]
 52 //  - oop array
 53 //  - data array
 54 //  - pcs
 55 //  [Exception handler table]
 56 //  - handler entry point array
 57 //  [Implicit Null Pointer exception table]
 58 //  - implicit null table array
 59 //  [Speculations]
 60 //  - encoded speculations array
 61 //  [JVMCINMethodData]
 62 //  - meta data for JVMCI compiled nmethod
 63 
 64 #if INCLUDE_JVMCI
 65 class FailedSpeculation;
 66 class JVMCINMethodData;
 67 #endif
 68 
 69 class nmethod : public CompiledMethod {
 70   friend class VMStructs;
 71   friend class JVMCIVMStructs;
 72   friend class CodeCache;  // scavengable oops
 73   friend class JVMCINMethodData;
 74 
 75  private:
 76 
 77   uint64_t  _gc_epoch;
 78 
 79   // To support simple linked-list chaining of nmethods:
 80   nmethod*  _osr_link;         // from InstanceKlass::osr_nmethods_head
 81 
 82   // STW two-phase nmethod root processing helpers.
 83   //
 84   // When determining liveness of a given nmethod to do code cache unloading,
 85   // some collectors need to do different things depending on whether the nmethods
 86   // need to absolutely be kept alive during root processing; "strong"ly reachable
 87   // nmethods are known to be kept alive at root processing, but the liveness of
 88   // "weak"ly reachable ones is to be determined later.
 89   //
 90   // We want to allow strong and weak processing of nmethods by different threads
 91   // at the same time without heavy synchronization. Additional constraints are
 92   // to make sure that every nmethod is processed a minimal amount of time, and
 93   // nmethods themselves are always iterated at most once at a particular time.
 94   //
 95   // Note that strong processing work must be a superset of weak processing work
 96   // for this code to work.
 97   //
 98   // We store state and claim information in the _oops_do_mark_link member, using
 99   // the two LSBs for the state and the remaining upper bits for linking together
100   // nmethods that were already visited.
101   // The last element is self-looped, i.e. points to itself to avoid some special
102   // "end-of-list" sentinel value.
103   //
104   // _oops_do_mark_link special values:
105   //
106   //   _oops_do_mark_link == nullptr: the nmethod has not been visited at all yet, i.e.
107   //      is Unclaimed.
108   //
109   // For other values, its lowest two bits indicate the following states of the nmethod:
110   //
111   //   weak_request (WR): the nmethod has been claimed by a thread for weak processing
112   //   weak_done (WD): weak processing has been completed for this nmethod.
113   //   strong_request (SR): the nmethod has been found to need strong processing while
114   //       being weak processed.
115   //   strong_done (SD): strong processing has been completed for this nmethod .
116   //
117   // The following shows the _only_ possible progressions of the _oops_do_mark_link
118   // pointer.
119   //
120   // Given
121   //   N as the nmethod
122   //   X the current next value of _oops_do_mark_link
123   //
124   // Unclaimed (C)-> N|WR (C)-> X|WD: the nmethod has been processed weakly by
125   //   a single thread.
126   // Unclaimed (C)-> N|WR (C)-> X|WD (O)-> X|SD: after weak processing has been
127   //   completed (as above) another thread found that the nmethod needs strong
128   //   processing after all.
129   // Unclaimed (C)-> N|WR (O)-> N|SR (C)-> X|SD: during weak processing another
130   //   thread finds that the nmethod needs strong processing, marks it as such and
131   //   terminates. The original thread completes strong processing.
132   // Unclaimed (C)-> N|SD (C)-> X|SD: the nmethod has been processed strongly from
133   //   the beginning by a single thread.
134   //
135   // "|" describes the concatenation of bits in _oops_do_mark_link.
136   //
137   // The diagram also describes the threads responsible for changing the nmethod to
138   // the next state by marking the _transition_ with (C) and (O), which mean "current"
139   // and "other" thread respectively.
140   //
141   struct oops_do_mark_link; // Opaque data type.
142 
143   // States used for claiming nmethods during root processing.
144   static const uint claim_weak_request_tag = 0;
145   static const uint claim_weak_done_tag = 1;
146   static const uint claim_strong_request_tag = 2;
147   static const uint claim_strong_done_tag = 3;
148 
149   static oops_do_mark_link* mark_link(nmethod* nm, uint tag) {
150     assert(tag <= claim_strong_done_tag, "invalid tag %u", tag);
151     assert(is_aligned(nm, 4), "nmethod pointer must have zero lower two LSB");
152     return (oops_do_mark_link*)(((uintptr_t)nm & ~0x3) | tag);
153   }
154 
155   static uint extract_state(oops_do_mark_link* link) {
156     return (uint)((uintptr_t)link & 0x3);
157   }
158 
159   static nmethod* extract_nmethod(oops_do_mark_link* link) {
160     return (nmethod*)((uintptr_t)link & ~0x3);
161   }
162 
163   void oops_do_log_change(const char* state);
164 
165   static bool oops_do_has_weak_request(oops_do_mark_link* next) {
166     return extract_state(next) == claim_weak_request_tag;
167   }
168 
169   static bool oops_do_has_any_strong_state(oops_do_mark_link* next) {
170     return extract_state(next) >= claim_strong_request_tag;
171   }
172 
173   // Attempt Unclaimed -> N|WR transition. Returns true if successful.
174   bool oops_do_try_claim_weak_request();
175 
176   // Attempt Unclaimed -> N|SD transition. Returns the current link.
177   oops_do_mark_link* oops_do_try_claim_strong_done();
178   // Attempt N|WR -> X|WD transition. Returns nullptr if successful, X otherwise.
179   nmethod* oops_do_try_add_to_list_as_weak_done();
180 
181   // Attempt X|WD -> N|SR transition. Returns the current link.
182   oops_do_mark_link* oops_do_try_add_strong_request(oops_do_mark_link* next);
183   // Attempt X|WD -> X|SD transition. Returns true if successful.
184   bool oops_do_try_claim_weak_done_as_strong_done(oops_do_mark_link* next);
185 
186   // Do the N|SD -> X|SD transition.
187   void oops_do_add_to_list_as_strong_done();
188 
189   // Sets this nmethod as strongly claimed (as part of N|SD -> X|SD and N|SR -> X|SD
190   // transitions).
191   void oops_do_set_strong_done(nmethod* old_head);
192 
193   static nmethod* volatile _oops_do_mark_nmethods;
194   oops_do_mark_link* volatile _oops_do_mark_link;
195 
196   // offsets for entry points
197   address _entry_point;                      // entry point with class check
198   address _verified_entry_point;             // entry point without class check
199   address _inline_entry_point;               // inline type entry point (unpack all inline type args) with class check
200   address _verified_inline_entry_point;      // inline type entry point (unpack all inline type args) without class check
201   address _verified_inline_ro_entry_point;   // inline type entry point (unpack receiver only) without class check
202   address _osr_entry_point;                  // entry point for on stack replacement
203 
204   CompiledICData* _compiled_ic_data;
205   bool _is_unlinked;
206 
207   // Shared fields for all nmethod's
208   int _entry_bci;      // != InvocationEntryBci if this nmethod is an on-stack replacement method
209 
210   // Offsets for different nmethod parts
211   int  _exception_offset;
212   // Offset of the unwind handler if it exists
213   int _unwind_handler_offset;
214 
215   int _consts_offset;
216   int _stub_offset;
217   int _oops_offset;                       // offset to where embedded oop table begins (inside data)
218   int _metadata_offset;                   // embedded meta data table
219   int _scopes_data_offset;
220   int _scopes_pcs_offset;
221   int _dependencies_offset;
222   int _handler_table_offset;
223   int _nul_chk_table_offset;
224 #if INCLUDE_JVMCI
225   int _speculations_offset;
226   int _jvmci_data_offset;
227 #endif
228   int _nmethod_end_offset;
229 
230   int code_offset() const { return int(code_begin() - header_begin()); }
231 
232   // location in frame (offset for sp) that deopt can store the original
233   // pc during a deopt.
234   int _orig_pc_offset;
235 
236   int _compile_id;                           // which compilation made this nmethod
237 
238 #if INCLUDE_RTM_OPT
239   // RTM state at compile time. Used during deoptimization to decide
240   // whether to restart collecting RTM locking abort statistic again.
241   RTMState _rtm_state;
242 #endif
243 
244   // These are used for compiled synchronized native methods to
245   // locate the owner and stack slot for the BasicLock. They are
246   // needed because there is no debug information for compiled native
247   // wrappers and the oop maps are insufficient to allow
248   // frame::retrieve_receiver() to work. Currently they are expected
249   // to be byte offsets from the Java stack pointer for maximum code
250   // sharing between platforms. JVMTI's GetLocalInstance() uses these
251   // offsets to find the receiver for non-static native wrapper frames.
252   ByteSize _native_receiver_sp_offset;
253   ByteSize _native_basic_lock_sp_offset;
254 
255   CompLevel _comp_level;               // compilation level
256 
257   // Local state used to keep track of whether unloading is happening or not
258   volatile uint8_t _is_unloading_state;
259 
260   // protected by CodeCache_lock
261   bool _has_flushed_dependencies;      // Used for maintenance of dependencies (CodeCache_lock)
262 
263   // used by jvmti to track if an event has been posted for this nmethod.
264   bool _load_reported;
265 
266   // Protected by CompiledMethod_lock
267   volatile signed char _state;         // {not_installed, in_use, not_used, not_entrant}
268 
269   int _skipped_instructions_size;
270 
271   // For native wrappers
272   nmethod(Method* method,
273           CompilerType type,
274           int nmethod_size,
275           int compile_id,
276           CodeOffsets* offsets,
277           CodeBuffer *code_buffer,
278           int frame_size,
279           ByteSize basic_lock_owner_sp_offset, /* synchronized natives only */
280           ByteSize basic_lock_sp_offset,       /* synchronized natives only */
281           OopMapSet* oop_maps);
282 
283   // Creation support
284   nmethod(Method* method,
285           CompilerType type,
286           int nmethod_size,
287           int compile_id,
288           int entry_bci,
289           CodeOffsets* offsets,
290           int orig_pc_offset,
291           DebugInformationRecorder *recorder,
292           Dependencies* dependencies,
293           CodeBuffer *code_buffer,
294           int frame_size,
295           OopMapSet* oop_maps,
296           ExceptionHandlerTable* handler_table,
297           ImplicitExceptionTable* nul_chk_table,
298           AbstractCompiler* compiler,
299           CompLevel comp_level
300 #if INCLUDE_JVMCI
301           , char* speculations = nullptr,
302           int speculations_len = 0,
303           JVMCINMethodData* jvmci_data = nullptr
304 #endif
305           );
306 
307   // helper methods
308   void* operator new(size_t size, int nmethod_size, int comp_level) throw();
309   // For method handle intrinsics: Try MethodNonProfiled, MethodProfiled and NonNMethod.
310   // Attention: Only allow NonNMethod space for special nmethods which don't need to be
311   // findable by nmethod iterators! In particular, they must not contain oops!
312   void* operator new(size_t size, int nmethod_size, bool allow_NonNMethod_space) throw();
313 
314   const char* reloc_string_for(u_char* begin, u_char* end);
315 
316   bool try_transition(signed char new_state);
317 
318   // Returns true if this thread changed the state of the nmethod or
319   // false if another thread performed the transition.
320   bool make_entrant() { Unimplemented(); return false; }
321   void inc_decompile_count();
322 
323   // Inform external interfaces that a compiled method has been unloaded
324   void post_compiled_method_unload();
325 
326   // Initialize fields to their default values
327   void init_defaults();
328 
329   // Offsets
330   int content_offset() const                  { return int(content_begin() - header_begin()); }
331   int data_offset() const                     { return _data_offset; }
332 
333   address header_end() const                  { return (address)    header_begin() + header_size(); }
334 
335  public:
336   // create nmethod with entry_bci
337   static nmethod* new_nmethod(const methodHandle& method,
338                               int compile_id,
339                               int entry_bci,
340                               CodeOffsets* offsets,
341                               int orig_pc_offset,
342                               DebugInformationRecorder* recorder,
343                               Dependencies* dependencies,
344                               CodeBuffer *code_buffer,
345                               int frame_size,
346                               OopMapSet* oop_maps,
347                               ExceptionHandlerTable* handler_table,
348                               ImplicitExceptionTable* nul_chk_table,
349                               AbstractCompiler* compiler,
350                               CompLevel comp_level
351 #if INCLUDE_JVMCI
352                               , char* speculations = nullptr,
353                               int speculations_len = 0,
354                               JVMCINMethodData* jvmci_data = nullptr
355 #endif
356   );
357 
358   // Only used for unit tests.
359   nmethod()
360     : CompiledMethod(),
361       _native_receiver_sp_offset(in_ByteSize(-1)),
362       _native_basic_lock_sp_offset(in_ByteSize(-1)),
363       _is_unloading_state(0) {}
364 
365 
366   static nmethod* new_native_nmethod(const methodHandle& method,
367                                      int compile_id,
368                                      CodeBuffer *code_buffer,
369                                      int vep_offset,
370                                      int frame_complete,
371                                      int frame_size,
372                                      ByteSize receiver_sp_offset,
373                                      ByteSize basic_lock_sp_offset,
374                                      OopMapSet* oop_maps,
375                                      int exception_handler = -1);
376 
377   // type info
378   bool is_nmethod() const                         { return true; }
379   bool is_osr_method() const                      { return _entry_bci != InvocationEntryBci; }
380 
381   // boundaries for different parts
382   address consts_begin          () const          { return           header_begin() + _consts_offset        ; }
383   address consts_end            () const          { return           code_begin()                           ; }
384   address stub_begin            () const          { return           header_begin() + _stub_offset          ; }
385   address stub_end              () const          { return           header_begin() + _oops_offset          ; }
386   address exception_begin       () const          { return           header_begin() + _exception_offset     ; }
387   address unwind_handler_begin  () const          { return _unwind_handler_offset != -1 ? (header_begin() + _unwind_handler_offset) : nullptr; }
388   oop*    oops_begin            () const          { return (oop*)   (header_begin() + _oops_offset)         ; }
389   oop*    oops_end              () const          { return (oop*)   (header_begin() + _metadata_offset)     ; }
390 
391   Metadata** metadata_begin   () const            { return (Metadata**)  (header_begin() + _metadata_offset)     ; }
392   Metadata** metadata_end     () const            { return (Metadata**)  _scopes_data_begin; }
393 
394   address scopes_data_end       () const          { return           header_begin() + _scopes_pcs_offset    ; }
395   PcDesc* scopes_pcs_begin      () const          { return (PcDesc*)(header_begin() + _scopes_pcs_offset   ); }
396   PcDesc* scopes_pcs_end        () const          { return (PcDesc*)(header_begin() + _dependencies_offset) ; }
397   address dependencies_begin    () const          { return           header_begin() + _dependencies_offset  ; }
398   address dependencies_end      () const          { return           header_begin() + _handler_table_offset ; }
399   address handler_table_begin   () const          { return           header_begin() + _handler_table_offset ; }
400   address handler_table_end     () const          { return           header_begin() + _nul_chk_table_offset ; }
401   address nul_chk_table_begin   () const          { return           header_begin() + _nul_chk_table_offset ; }
402 
403   int skipped_instructions_size () const          { return           _skipped_instructions_size             ; }
404 
405 #if INCLUDE_JVMCI
406   address nul_chk_table_end     () const          { return           header_begin() + _speculations_offset  ; }
407   address speculations_begin    () const          { return           header_begin() + _speculations_offset  ; }
408   address speculations_end      () const          { return           header_begin() + _jvmci_data_offset   ; }
409   address jvmci_data_begin      () const          { return           header_begin() + _jvmci_data_offset    ; }
410   address jvmci_data_end        () const          { return           header_begin() + _nmethod_end_offset   ; }
411 #else
412   address nul_chk_table_end     () const          { return           header_begin() + _nmethod_end_offset   ; }
413 #endif
414 
415   // Sizes
416   int oops_size         () const                  { return int((address)  oops_end         () - (address)  oops_begin         ()); }
417   int metadata_size     () const                  { return int((address)  metadata_end     () - (address)  metadata_begin     ()); }
418   int dependencies_size () const                  { return int(           dependencies_end () -            dependencies_begin ()); }
419 #if INCLUDE_JVMCI
420   int speculations_size () const                  { return int(           speculations_end () -            speculations_begin ()); }
421   int jvmci_data_size   () const                  { return int(           jvmci_data_end   () -            jvmci_data_begin   ()); }
422 #endif
423 
424   int     oops_count() const { assert(oops_size() % oopSize == 0, "");  return (oops_size() / oopSize) + 1; }
425   int metadata_count() const { assert(metadata_size() % wordSize == 0, ""); return (metadata_size() / wordSize) + 1; }
426 
427   int total_size        () const;
428 
429   // Containment
430   bool oops_contains         (oop*    addr) const { return oops_begin         () <= addr && addr < oops_end         (); }
431   bool metadata_contains     (Metadata** addr) const   { return metadata_begin     () <= addr && addr < metadata_end     (); }
432   bool scopes_data_contains  (address addr) const { return scopes_data_begin  () <= addr && addr < scopes_data_end  (); }
433   bool scopes_pcs_contains   (PcDesc* addr) const { return scopes_pcs_begin   () <= addr && addr < scopes_pcs_end   (); }
434 
435   // entry points
436   address entry_point() const                     { return _entry_point;             }        // normal entry point
437   address verified_entry_point() const            { return _verified_entry_point;    }        // normal entry point without class check
438   address inline_entry_point() const              { return _inline_entry_point; }             // inline type entry point (unpack all inline type args)
439   address verified_inline_entry_point() const     { return _verified_inline_entry_point; }    // inline type entry point (unpack all inline type args) without class check
440   address verified_inline_ro_entry_point() const  { return _verified_inline_ro_entry_point; } // inline type entry point (only unpack receiver) without class check
441 
442   // flag accessing and manipulation
443   bool  is_not_installed() const                  { return _state == not_installed; }
444   bool  is_in_use() const                         { return _state <= in_use; }
445   bool  is_not_entrant() const                    { return _state == not_entrant; }
446 
447   void clear_unloading_state();
448   // Heuristically deduce an nmethod isn't worth keeping around
449   bool is_cold();
450   virtual bool is_unloading();
451   virtual void do_unloading(bool unloading_occurred);
452 
453   bool is_unlinked() const                        { return _is_unlinked; }
454   void set_is_unlinked()                          { assert(!_is_unlinked, "already unlinked"); _is_unlinked = true; }
455 
456 #if INCLUDE_RTM_OPT
457   // rtm state accessing and manipulating
458   RTMState  rtm_state() const                     { return _rtm_state; }
459   void set_rtm_state(RTMState state)              { _rtm_state = state; }
460 #endif
461 
462   bool make_in_use() {
463     return try_transition(in_use);
464   }
465   // Make the nmethod non entrant. The nmethod will continue to be
466   // alive.  It is used when an uncommon trap happens.  Returns true
467   // if this thread changed the state of the nmethod or false if
468   // another thread performed the transition.
469   bool  make_not_entrant();
470   bool  make_not_used()    { return make_not_entrant(); }
471 
472   int get_state() const {
473     return _state;
474   }
475 
476   bool has_dependencies()                         { return dependencies_size() != 0; }
477   void print_dependencies_on(outputStream* out) PRODUCT_RETURN;
478   void flush_dependencies();
479   bool has_flushed_dependencies()                 { return _has_flushed_dependencies; }
480   void set_has_flushed_dependencies()             {
481     assert(!has_flushed_dependencies(), "should only happen once");
482     _has_flushed_dependencies = 1;
483   }
484 
485   int   comp_level() const                        { return _comp_level; }
486 
487   void unlink_from_method();
488 
489   // Support for oops in scopes and relocs:
490   // Note: index 0 is reserved for null.
491   oop   oop_at(int index) const;
492   oop   oop_at_phantom(int index) const; // phantom reference
493   oop*  oop_addr_at(int index) const {  // for GC
494     // relocation indexes are biased by 1 (because 0 is reserved)
495     assert(index > 0 && index <= oops_count(), "must be a valid non-zero index");
496     return &oops_begin()[index - 1];
497   }
498 
499   // Support for meta data in scopes and relocs:
500   // Note: index 0 is reserved for null.
501   Metadata*     metadata_at(int index) const      { return index == 0 ? nullptr: *metadata_addr_at(index); }
502   Metadata**  metadata_addr_at(int index) const {  // for GC
503     // relocation indexes are biased by 1 (because 0 is reserved)
504     assert(index > 0 && index <= metadata_count(), "must be a valid non-zero index");
505     return &metadata_begin()[index - 1];
506   }
507 
508   void copy_values(GrowableArray<jobject>* oops);
509   void copy_values(GrowableArray<Metadata*>* metadata);
510 
511   // Relocation support
512 private:
513   void fix_oop_relocations(address begin, address end, bool initialize_immediates);
514   inline void initialize_immediate_oop(oop* dest, jobject handle);
515 
516 public:
517   void fix_oop_relocations(address begin, address end) { fix_oop_relocations(begin, end, false); }
518   void fix_oop_relocations()                           { fix_oop_relocations(nullptr, nullptr, false); }
519 
520   // On-stack replacement support
521   int   osr_entry_bci() const                     { assert(is_osr_method(), "wrong kind of nmethod"); return _entry_bci; }
522   address  osr_entry() const                      { assert(is_osr_method(), "wrong kind of nmethod"); return _osr_entry_point; }
523   void  invalidate_osr_method();
524   nmethod* osr_link() const                       { return _osr_link; }
525   void     set_osr_link(nmethod *n)               { _osr_link = n; }
526 
527   // Verify calls to dead methods have been cleaned.
528   void verify_clean_inline_caches();
529 
530   // Unlink this nmethod from the system
531   void unlink();
532 
533   // Deallocate this nmethod - called by the GC
534   void purge(bool free_code_cache_data, bool unregister_nmethod);
535 
536   // See comment at definition of _last_seen_on_stack
537   void mark_as_maybe_on_stack();
538   bool is_maybe_on_stack();
539 
540   // Evolution support. We make old (discarded) compiled methods point to new Method*s.
541   void set_method(Method* method) { _method = method; }
542 
543 #if INCLUDE_JVMCI
544   // Gets the JVMCI name of this nmethod.
545   const char* jvmci_name();
546 
547   // Records the pending failed speculation in the
548   // JVMCI speculation log associated with this nmethod.
549   void update_speculation(JavaThread* thread);
550 
551   // Gets the data specific to a JVMCI compiled method.
552   // This returns a non-nullptr value iff this nmethod was
553   // compiled by the JVMCI compiler.
554   JVMCINMethodData* jvmci_nmethod_data() const {
555     return jvmci_data_size() == 0 ? nullptr : (JVMCINMethodData*) jvmci_data_begin();
556   }
557 #endif
558 
559  public:
560   void oops_do(OopClosure* f) { oops_do(f, false); }
561   void oops_do(OopClosure* f, bool allow_dead);
562 
563   // All-in-one claiming of nmethods: returns true if the caller successfully claimed that
564   // nmethod.
565   bool oops_do_try_claim();
566 
567   // Loom support for following nmethods on the stack
568   void follow_nmethod(OopIterateClosure* cl);
569 
570   // Class containing callbacks for the oops_do_process_weak/strong() methods
571   // below.
572   class OopsDoProcessor {
573   public:
574     // Process the oops of the given nmethod based on whether it has been called
575     // in a weak or strong processing context, i.e. apply either weak or strong
576     // work on it.
577     virtual void do_regular_processing(nmethod* nm) = 0;
578     // Assuming that the oops of the given nmethod has already been its weak
579     // processing applied, apply the remaining strong processing part.
580     virtual void do_remaining_strong_processing(nmethod* nm) = 0;
581   };
582 
583   // The following two methods do the work corresponding to weak/strong nmethod
584   // processing.
585   void oops_do_process_weak(OopsDoProcessor* p);
586   void oops_do_process_strong(OopsDoProcessor* p);
587 
588   static void oops_do_marking_prologue();
589   static void oops_do_marking_epilogue();
590 
591  private:
592   ScopeDesc* scope_desc_in(address begin, address end);
593 
594   address* orig_pc_addr(const frame* fr);
595 
596   // used by jvmti to track if the load events has been reported
597   bool  load_reported() const                     { return _load_reported; }
598   void  set_load_reported()                       { _load_reported = true; }
599 
600  public:
601   // copying of debugging information
602   void copy_scopes_pcs(PcDesc* pcs, int count);
603   void copy_scopes_data(address buffer, int size);
604 
605   int orig_pc_offset() { return _orig_pc_offset; }
606 
607   // Post successful compilation
608   void post_compiled_method(CompileTask* task);
609 
610   // jvmti support:
611   void post_compiled_method_load_event(JvmtiThreadState* state = nullptr);
612 
613   // verify operations
614   void verify();
615   void verify_scopes();
616   void verify_interrupt_point(address interrupt_point, bool is_inline_cache);
617 
618   // Disassemble this nmethod with additional debug information, e.g. information about blocks.
619   void decode2(outputStream* st) const;
620   void print_constant_pool(outputStream* st);
621 
622   // Avoid hiding of parent's 'decode(outputStream*)' method.
623   void decode(outputStream* st) const { decode2(st); } // just delegate here.
624 
625   // printing support
626   void print()                          const;
627   void print(outputStream* st)          const;
628   void print_code();
629 
630 #if defined(SUPPORT_DATA_STRUCTS)
631   // print output in opt build for disassembler library
632   void print_relocations()                        PRODUCT_RETURN;
633   void print_pcs_on(outputStream* st);
634   void print_scopes() { print_scopes_on(tty); }
635   void print_scopes_on(outputStream* st)          PRODUCT_RETURN;
636   void print_value_on(outputStream* st) const;
637   void print_handler_table();
638   void print_nul_chk_table();
639   void print_recorded_oop(int log_n, int index);
640   void print_recorded_oops();
641   void print_recorded_metadata();
642 
643   void print_oops(outputStream* st);     // oops from the underlying CodeBlob.
644   void print_metadata(outputStream* st); // metadata in metadata pool.
645 #else
646   void print_pcs_on(outputStream* st) { return; }
647 #endif
648 
649   void print_calls(outputStream* st)              PRODUCT_RETURN;
650   static void print_statistics()                  PRODUCT_RETURN;
651 
652   void maybe_print_nmethod(const DirectiveSet* directive);
653   void print_nmethod(bool print_code);
654 
655   // need to re-define this from CodeBlob else the overload hides it
656   virtual void print_on(outputStream* st) const { CodeBlob::print_on(st); }
657   void print_on(outputStream* st, const char* msg) const;
658 
659   // Logging
660   void log_identity(xmlStream* log) const;
661   void log_new_nmethod() const;
662   void log_state_change() const;
663 
664   // Prints block-level comments, including nmethod specific block labels:
665   virtual void print_block_comment(outputStream* stream, address block_begin) const {
666 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
667     print_nmethod_labels(stream, block_begin);
668     CodeBlob::print_block_comment(stream, block_begin);
669 #endif
670   }
671 
672   void print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels=true) const;
673   const char* nmethod_section_label(address pos) const;
674 
675   // returns whether this nmethod has code comments.
676   bool has_code_comment(address begin, address end);
677   // Prints a comment for one native instruction (reloc info, pc desc)
678   void print_code_comment_on(outputStream* st, int column, address begin, address end);
679 
680   // Compiler task identification.  Note that all OSR methods
681   // are numbered in an independent sequence if CICountOSR is true,
682   // and native method wrappers are also numbered independently if
683   // CICountNative is true.
684   virtual int compile_id() const { return _compile_id; }
685   const char* compile_kind() const;
686 
687   // tells if this compiled method is dependent on the given changes,
688   // and the changes have invalidated it
689   bool check_dependency_on(DepChange& changes);
690 
691   // Fast breakpoint support. Tells if this compiled method is
692   // dependent on the given method. Returns true if this nmethod
693   // corresponds to the given method as well.
694   virtual bool is_dependent_on_method(Method* dependee);
695 
696   // JVMTI's GetLocalInstance() support
697   ByteSize native_receiver_sp_offset() {
698     return _native_receiver_sp_offset;
699   }
700   ByteSize native_basic_lock_sp_offset() {
701     return _native_basic_lock_sp_offset;
702   }
703 
704   // support for code generation
705   static ByteSize verified_entry_point_offset() { return byte_offset_of(nmethod, _verified_entry_point); }
706   static ByteSize osr_entry_point_offset()      { return byte_offset_of(nmethod, _osr_entry_point); }
707   static ByteSize state_offset()                { return byte_offset_of(nmethod, _state); }
708 
709   virtual void metadata_do(MetadataClosure* f);
710 
711   address call_instruction_address(address pc) const;
712 
713   virtual void  make_deoptimized();
714   void finalize_relocations();
715 };
716 
717 #endif // SHARE_CODE_NMETHOD_HPP