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