1 /*
  2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_CODE_NMETHOD_HPP
 26 #define SHARE_CODE_NMETHOD_HPP
 27 
 28 #include "code/compiledMethod.hpp"
 29 #include "compiler/compilerDefinitions.hpp"
 30 
 31 class DepChange;
 32 class DirectiveSet;
 33 class DebugInformationRecorder;
 34 class JvmtiThreadState;
 35 
 36 // nmethods (native methods) are the compiled code versions of Java methods.
 37 //
 38 // An nmethod contains:
 39 //  - header                 (the nmethod structure)
 40 //  [Relocation]
 41 //  - relocation information
 42 //  - constant part          (doubles, longs and floats used in nmethod)
 43 //  - oop table
 44 //  [Code]
 45 //  - code body
 46 //  - exception handler
 47 //  - stub code
 48 //  [Debugging information]
 49 //  - oop array
 50 //  - data array
 51 //  - pcs
 52 //  [Exception handler table]
 53 //  - handler entry point array
 54 //  [Implicit Null Pointer exception table]
 55 //  - implicit null table array
 56 //  [Speculations]
 57 //  - encoded speculations array
 58 //  [JVMCINMethodData]
 59 //  - meta data for JVMCI compiled nmethod
 60 
 61 #if INCLUDE_JVMCI
 62 class FailedSpeculation;
 63 class JVMCINMethodData;
 64 #endif
 65 
 66 class nmethod : public CompiledMethod {
 67   friend class VMStructs;
 68   friend class JVMCIVMStructs;
 69   friend class NMethodSweeper;
 70   friend class CodeCache;  // scavengable oops
 71   friend class JVMCINMethodData;
 72 
 73  private:
 74   // Shared fields for all nmethod's
 75   int       _entry_bci;        // != InvocationEntryBci if this nmethod is an on-stack replacement method
 76 
 77   // To support simple linked-list chaining of nmethods:
 78   nmethod*  _osr_link;         // from InstanceKlass::osr_nmethods_head
 79 
 80   // STW two-phase nmethod root processing helpers.
 81   //
 82   // When determining liveness of a given nmethod to do code cache unloading,
 83   // some collectors need to do different things depending on whether the nmethods
 84   // need to absolutely be kept alive during root processing; "strong"ly reachable
 85   // nmethods are known to be kept alive at root processing, but the liveness of
 86   // "weak"ly reachable ones is to be determined later.
 87   //
 88   // We want to allow strong and weak processing of nmethods by different threads
 89   // at the same time without heavy synchronization. Additional constraints are
 90   // to make sure that every nmethod is processed a minimal amount of time, and
 91   // nmethods themselves are always iterated at most once at a particular time.
 92   //
 93   // Note that strong processing work must be a superset of weak processing work
 94   // for this code to work.
 95   //
 96   // We store state and claim information in the _oops_do_mark_link member, using
 97   // the two LSBs for the state and the remaining upper bits for linking together
 98   // nmethods that were already visited.
 99   // The last element is self-looped, i.e. points to itself to avoid some special
100   // "end-of-list" sentinel value.
101   //
102   // _oops_do_mark_link special values:
103   //
104   //   _oops_do_mark_link == NULL: the nmethod has not been visited at all yet, i.e.
105   //      is Unclaimed.
106   //
107   // For other values, its lowest two bits indicate the following states of the nmethod:
108   //
109   //   weak_request (WR): the nmethod has been claimed by a thread for weak processing
110   //   weak_done (WD): weak processing has been completed for this nmethod.
111   //   strong_request (SR): the nmethod has been found to need strong processing while
112   //       being weak processed.
113   //   strong_done (SD): strong processing has been completed for this nmethod .
114   //
115   // The following shows the _only_ possible progressions of the _oops_do_mark_link
116   // pointer.
117   //
118   // Given
119   //   N as the nmethod
120   //   X the current next value of _oops_do_mark_link
121   //
122   // Unclaimed (C)-> N|WR (C)-> X|WD: the nmethod has been processed weakly by
123   //   a single thread.
124   // Unclaimed (C)-> N|WR (C)-> X|WD (O)-> X|SD: after weak processing has been
125   //   completed (as above) another thread found that the nmethod needs strong
126   //   processing after all.
127   // Unclaimed (C)-> N|WR (O)-> N|SR (C)-> X|SD: during weak processing another
128   //   thread finds that the nmethod needs strong processing, marks it as such and
129   //   terminates. The original thread completes strong processing.
130   // Unclaimed (C)-> N|SD (C)-> X|SD: the nmethod has been processed strongly from
131   //   the beginning by a single thread.
132   //
133   // "|" describes the concatentation of bits in _oops_do_mark_link.
134   //
135   // The diagram also describes the threads responsible for changing the nmethod to
136   // the next state by marking the _transition_ with (C) and (O), which mean "current"
137   // and "other" thread respectively.
138   //
139   struct oops_do_mark_link; // Opaque data type.
140 
141   // States used for claiming nmethods during root processing.
142   static const uint claim_weak_request_tag = 0;
143   static const uint claim_weak_done_tag = 1;
144   static const uint claim_strong_request_tag = 2;
145   static const uint claim_strong_done_tag = 3;
146 
147   static oops_do_mark_link* mark_link(nmethod* nm, uint tag) {
148     assert(tag <= claim_strong_done_tag, "invalid tag %u", tag);
149     assert(is_aligned(nm, 4), "nmethod pointer must have zero lower two LSB");
150     return (oops_do_mark_link*)(((uintptr_t)nm & ~0x3) | tag);
151   }
152 
153   static uint extract_state(oops_do_mark_link* link) {
154     return (uint)((uintptr_t)link & 0x3);
155   }
156 
157   static nmethod* extract_nmethod(oops_do_mark_link* link) {
158     return (nmethod*)((uintptr_t)link & ~0x3);
159   }
160 
161   void oops_do_log_change(const char* state);
162 
163   static bool oops_do_has_weak_request(oops_do_mark_link* next) {
164     return extract_state(next) == claim_weak_request_tag;
165   }
166 
167   static bool oops_do_has_any_strong_state(oops_do_mark_link* next) {
168     return extract_state(next) >= claim_strong_request_tag;
169   }
170 
171   // Attempt Unclaimed -> N|WR transition. Returns true if successful.
172   bool oops_do_try_claim_weak_request();
173 
174   // Attempt Unclaimed -> N|SD transition. Returns the current link.
175   oops_do_mark_link* oops_do_try_claim_strong_done();
176   // Attempt N|WR -> X|WD transition. Returns NULL if successful, X otherwise.
177   nmethod* oops_do_try_add_to_list_as_weak_done();
178 
179   // Attempt X|WD -> N|SR transition. Returns the current link.
180   oops_do_mark_link* oops_do_try_add_strong_request(oops_do_mark_link* next);
181   // Attempt X|WD -> X|SD transition. Returns true if successful.
182   bool oops_do_try_claim_weak_done_as_strong_done(oops_do_mark_link* next);
183 
184   // Do the N|SD -> X|SD transition.
185   void oops_do_add_to_list_as_strong_done();
186 
187   // Sets this nmethod as strongly claimed (as part of N|SD -> X|SD and N|SR -> X|SD
188   // transitions).
189   void oops_do_set_strong_done(nmethod* old_head);
190 
191   static nmethod* volatile _oops_do_mark_nmethods;
192   oops_do_mark_link* volatile _oops_do_mark_link;
193 
194   // offsets for entry points
195   address _entry_point;                      // entry point with class check
196   address _verified_entry_point;             // entry point without class check
197   address _inline_entry_point;               // inline type entry point (unpack all inline type args) with class check
198   address _verified_inline_entry_point;      // inline type entry point (unpack all inline type args) without class check
199   address _verified_inline_ro_entry_point;   // inline type entry point (unpack receiver only) without class check
200   address _osr_entry_point;                  // entry point for on stack replacement
201 
202   // Offsets for different nmethod parts
203   int  _exception_offset;
204   // Offset of the unwind handler if it exists
205   int _unwind_handler_offset;
206 
207   int _consts_offset;
208   int _stub_offset;
209   int _oops_offset;                       // offset to where embedded oop table begins (inside data)
210   int _metadata_offset;                   // embedded meta data table
211   int _scopes_data_offset;
212   int _scopes_pcs_offset;
213   int _dependencies_offset;
214   int _native_invokers_offset;
215   int _handler_table_offset;
216   int _nul_chk_table_offset;
217 #if INCLUDE_JVMCI
218   int _speculations_offset;
219   int _jvmci_data_offset;
220 #endif
221   int _nmethod_end_offset;
222 
223   int code_offset() const { return (address) code_begin() - header_begin(); }
224 
225   // location in frame (offset for sp) that deopt can store the original
226   // pc during a deopt.
227   int _orig_pc_offset;
228 
229   int _compile_id;                           // which compilation made this nmethod
230   int _comp_level;                           // compilation level
231 
232   // protected by CodeCache_lock
233   bool _has_flushed_dependencies;            // Used for maintenance of dependencies (CodeCache_lock)
234 
235   // used by jvmti to track if an event has been posted for this nmethod.
236   bool _unload_reported;
237   bool _load_reported;
238 
239   // Protected by CompiledMethod_lock
240   volatile signed char _state;               // {not_installed, in_use, not_entrant, zombie, unloaded}
241 
242 #ifdef ASSERT
243   bool _oops_are_stale;  // indicates that it's no longer safe to access oops section
244 #endif
245 
246 #if INCLUDE_RTM_OPT
247   // RTM state at compile time. Used during deoptimization to decide
248   // whether to restart collecting RTM locking abort statistic again.
249   RTMState _rtm_state;
250 #endif
251 
252   // Nmethod Flushing lock. If non-zero, then the nmethod is not removed
253   // and is not made into a zombie. However, once the nmethod is made into
254   // a zombie, it will be locked one final time if CompiledMethodUnload
255   // event processing needs to be done.
256   volatile jint _lock_count;
257 
258   // not_entrant method removal. Each mark_sweep pass will update
259   // this mark to current sweep invocation count if it is seen on the
260   // stack.  An not_entrant method can be removed when there are no
261   // more activations, i.e., when the _stack_traversal_mark is less than
262   // current sweep traversal index.
263   volatile long _stack_traversal_mark;
264 
265   // The _hotness_counter indicates the hotness of a method. The higher
266   // the value the hotter the method. The hotness counter of a nmethod is
267   // set to [(ReservedCodeCacheSize / (1024 * 1024)) * 2] each time the method
268   // is active while stack scanning (do_stack_scanning()). The hotness
269   // counter is decreased (by 1) while sweeping.
270   int _hotness_counter;
271 
272   // Local state used to keep track of whether unloading is happening or not
273   volatile uint8_t _is_unloading_state;
274 
275   // These are used for compiled synchronized native methods to
276   // locate the owner and stack slot for the BasicLock. They are
277   // needed because there is no debug information for compiled native
278   // wrappers and the oop maps are insufficient to allow
279   // frame::retrieve_receiver() to work. Currently they are expected
280   // to be byte offsets from the Java stack pointer for maximum code
281   // sharing between platforms. JVMTI's GetLocalInstance() uses these
282   // offsets to find the receiver for non-static native wrapper frames.
283   ByteSize _native_receiver_sp_offset;
284   ByteSize _native_basic_lock_sp_offset;
285 
286   friend class nmethodLocker;
287 
288   // For native wrappers
289   nmethod(Method* method,
290           CompilerType type,
291           int nmethod_size,
292           int compile_id,
293           CodeOffsets* offsets,
294           CodeBuffer *code_buffer,
295           int frame_size,
296           ByteSize basic_lock_owner_sp_offset, /* synchronized natives only */
297           ByteSize basic_lock_sp_offset,       /* synchronized natives only */
298           OopMapSet* oop_maps);
299 
300   // Creation support
301   nmethod(Method* method,
302           CompilerType type,
303           int nmethod_size,
304           int compile_id,
305           int entry_bci,
306           CodeOffsets* offsets,
307           int orig_pc_offset,
308           DebugInformationRecorder *recorder,
309           Dependencies* dependencies,
310           CodeBuffer *code_buffer,
311           int frame_size,
312           OopMapSet* oop_maps,
313           ExceptionHandlerTable* handler_table,
314           ImplicitExceptionTable* nul_chk_table,
315           AbstractCompiler* compiler,
316           int comp_level,
317           const GrowableArrayView<RuntimeStub*>& native_invokers
318 #if INCLUDE_JVMCI
319           , char* speculations,
320           int speculations_len,
321           int jvmci_data_size
322 #endif
323           );
324 
325   // helper methods
326   void* operator new(size_t size, int nmethod_size, int comp_level) throw();
327 
328   const char* reloc_string_for(u_char* begin, u_char* end);
329 
330   bool try_transition(int new_state);
331 
332   // Returns true if this thread changed the state of the nmethod or
333   // false if another thread performed the transition.
334   bool make_not_entrant_or_zombie(int state);
335   bool make_entrant() { Unimplemented(); return false; }
336   void inc_decompile_count();
337 
338   // Inform external interfaces that a compiled method has been unloaded
339   void post_compiled_method_unload();
340 
341   // Initailize fields to their default values
342   void init_defaults();
343 
344   // Offsets
345   int content_offset() const                  { return content_begin() - header_begin(); }
346   int data_offset() const                     { return _data_offset; }
347 
348   address header_end() const                  { return (address)    header_begin() + header_size(); }
349 
350  public:
351   // create nmethod with entry_bci
352   static nmethod* new_nmethod(const methodHandle& method,
353                               int compile_id,
354                               int entry_bci,
355                               CodeOffsets* offsets,
356                               int orig_pc_offset,
357                               DebugInformationRecorder* recorder,
358                               Dependencies* dependencies,
359                               CodeBuffer *code_buffer,
360                               int frame_size,
361                               OopMapSet* oop_maps,
362                               ExceptionHandlerTable* handler_table,
363                               ImplicitExceptionTable* nul_chk_table,
364                               AbstractCompiler* compiler,
365                               int comp_level,
366                               const GrowableArrayView<RuntimeStub*>& native_invokers = GrowableArrayView<RuntimeStub*>::EMPTY
367 #if INCLUDE_JVMCI
368                               , char* speculations = NULL,
369                               int speculations_len = 0,
370                               int nmethod_mirror_index = -1,
371                               const char* nmethod_mirror_name = NULL,
372                               FailedSpeculation** failed_speculations = NULL
373 #endif
374   );
375 
376   // Only used for unit tests.
377   nmethod()
378     : CompiledMethod(),
379       _is_unloading_state(0),
380       _native_receiver_sp_offset(in_ByteSize(-1)),
381       _native_basic_lock_sp_offset(in_ByteSize(-1)) {}
382 
383 
384   static nmethod* new_native_nmethod(const methodHandle& method,
385                                      int compile_id,
386                                      CodeBuffer *code_buffer,
387                                      int vep_offset,
388                                      int frame_complete,
389                                      int frame_size,
390                                      ByteSize receiver_sp_offset,
391                                      ByteSize basic_lock_sp_offset,
392                                      OopMapSet* oop_maps);
393 
394   // type info
395   bool is_nmethod() const                         { return true; }
396   bool is_osr_method() const                      { return _entry_bci != InvocationEntryBci; }
397 
398   // boundaries for different parts
399   address consts_begin          () const          { return           header_begin() + _consts_offset        ; }
400   address consts_end            () const          { return           code_begin()                           ; }
401   address stub_begin            () const          { return           header_begin() + _stub_offset          ; }
402   address stub_end              () const          { return           header_begin() + _oops_offset          ; }
403   address exception_begin       () const          { return           header_begin() + _exception_offset     ; }
404   address unwind_handler_begin  () const          { return _unwind_handler_offset != -1 ? (header_begin() + _unwind_handler_offset) : NULL; }
405   oop*    oops_begin            () const          { return (oop*)   (header_begin() + _oops_offset)         ; }
406   oop*    oops_end              () const          { return (oop*)   (header_begin() + _metadata_offset)     ; }
407 
408   Metadata** metadata_begin   () const            { return (Metadata**)  (header_begin() + _metadata_offset)     ; }
409   Metadata** metadata_end     () const            { return (Metadata**)  _scopes_data_begin; }
410 
411   address scopes_data_end       () const          { return           header_begin() + _scopes_pcs_offset    ; }
412   PcDesc* scopes_pcs_begin      () const          { return (PcDesc*)(header_begin() + _scopes_pcs_offset   ); }
413   PcDesc* scopes_pcs_end        () const          { return (PcDesc*)(header_begin() + _dependencies_offset) ; }
414   address dependencies_begin    () const          { return           header_begin() + _dependencies_offset  ; }
415   address dependencies_end      () const          { return           header_begin() + _native_invokers_offset ; }
416   RuntimeStub** native_invokers_begin() const     { return (RuntimeStub**)(header_begin() + _native_invokers_offset) ; }
417   RuntimeStub** native_invokers_end  () const     { return (RuntimeStub**)(header_begin() + _handler_table_offset); }
418   address handler_table_begin   () const          { return           header_begin() + _handler_table_offset ; }
419   address handler_table_end     () const          { return           header_begin() + _nul_chk_table_offset ; }
420   address nul_chk_table_begin   () const          { return           header_begin() + _nul_chk_table_offset ; }
421 #if INCLUDE_JVMCI
422   address nul_chk_table_end     () const          { return           header_begin() + _speculations_offset  ; }
423   address speculations_begin    () const          { return           header_begin() + _speculations_offset  ; }
424   address speculations_end      () const          { return           header_begin() + _jvmci_data_offset   ; }
425   address jvmci_data_begin      () const          { return           header_begin() + _jvmci_data_offset    ; }
426   address jvmci_data_end        () const          { return           header_begin() + _nmethod_end_offset   ; }
427 #else
428   address nul_chk_table_end     () const          { return           header_begin() + _nmethod_end_offset   ; }
429 #endif
430 
431   // Sizes
432   int oops_size         () const                  { return (address)  oops_end         () - (address)  oops_begin         (); }
433   int metadata_size     () const                  { return (address)  metadata_end     () - (address)  metadata_begin     (); }
434   int dependencies_size () const                  { return            dependencies_end () -            dependencies_begin (); }
435 #if INCLUDE_JVMCI
436   int speculations_size () const                  { return            speculations_end () -            speculations_begin (); }
437   int jvmci_data_size   () const                  { return            jvmci_data_end   () -            jvmci_data_begin   (); }
438 #endif
439 
440   int     oops_count() const { assert(oops_size() % oopSize == 0, "");  return (oops_size() / oopSize) + 1; }
441   int metadata_count() const { assert(metadata_size() % wordSize == 0, ""); return (metadata_size() / wordSize) + 1; }
442 
443   int total_size        () const;
444 
445   void dec_hotness_counter()        { _hotness_counter--; }
446   void set_hotness_counter(int val) { _hotness_counter = val; }
447   int  hotness_counter() const      { return _hotness_counter; }
448 
449   // Containment
450   bool oops_contains         (oop*    addr) const { return oops_begin         () <= addr && addr < oops_end         (); }
451   bool metadata_contains     (Metadata** addr) const   { return metadata_begin     () <= addr && addr < metadata_end     (); }
452   bool scopes_data_contains  (address addr) const { return scopes_data_begin  () <= addr && addr < scopes_data_end  (); }
453   bool scopes_pcs_contains   (PcDesc* addr) const { return scopes_pcs_begin   () <= addr && addr < scopes_pcs_end   (); }
454 
455   // entry points
456   address entry_point() const                     { return _entry_point;             }        // normal entry point
457   address verified_entry_point() const            { return _verified_entry_point;    }        // normal entry point without class check
458   address inline_entry_point() const              { return _inline_entry_point; }             // inline type entry point (unpack all inline type args)
459   address verified_inline_entry_point() const     { return _verified_inline_entry_point; }    // inline type entry point (unpack all inline type args) without class check
460   address verified_inline_ro_entry_point() const  { return _verified_inline_ro_entry_point; } // inline type entry point (only unpack receiver) without class check
461 
462   // flag accessing and manipulation
463   bool  is_not_installed() const                  { return _state == not_installed; }
464   bool  is_in_use() const                         { return _state <= in_use; }
465   bool  is_alive() const                          { return _state < unloaded; }
466   bool  is_not_entrant() const                    { return _state == not_entrant; }
467   bool  is_zombie() const                         { return _state == zombie; }
468   bool  is_unloaded() const                       { return _state == unloaded; }
469 
470   void clear_unloading_state();
471   virtual bool is_unloading();
472   virtual void do_unloading(bool unloading_occurred);
473 
474 #if INCLUDE_RTM_OPT
475   // rtm state accessing and manipulating
476   RTMState  rtm_state() const                     { return _rtm_state; }
477   void set_rtm_state(RTMState state)              { _rtm_state = state; }
478 #endif
479 
480   bool make_in_use() {
481     return try_transition(in_use);
482   }
483   // Make the nmethod non entrant. The nmethod will continue to be
484   // alive.  It is used when an uncommon trap happens.  Returns true
485   // if this thread changed the state of the nmethod or false if
486   // another thread performed the transition.
487   bool  make_not_entrant() {
488     assert(!method()->is_method_handle_intrinsic(), "Cannot make MH intrinsic not entrant");
489     return make_not_entrant_or_zombie(not_entrant);
490   }
491   bool  make_not_used()    { return make_not_entrant(); }
492   bool  make_zombie()      { return make_not_entrant_or_zombie(zombie); }
493 
494   int get_state() const {
495     return _state;
496   }
497 
498   void  make_unloaded();
499 
500   bool has_dependencies()                         { return dependencies_size() != 0; }
501   void print_dependencies()                       PRODUCT_RETURN;
502   void flush_dependencies(bool delete_immediately);
503   bool has_flushed_dependencies()                 { return _has_flushed_dependencies; }
504   void set_has_flushed_dependencies()             {
505     assert(!has_flushed_dependencies(), "should only happen once");
506     _has_flushed_dependencies = 1;
507   }
508 
509   int   comp_level() const                        { return _comp_level; }
510 
511   void unlink_from_method();
512 
513   // Support for oops in scopes and relocs:
514   // Note: index 0 is reserved for null.
515   oop   oop_at(int index) const;
516   oop   oop_at_phantom(int index) const; // phantom reference
517   oop*  oop_addr_at(int index) const {  // for GC
518     // relocation indexes are biased by 1 (because 0 is reserved)
519     assert(index > 0 && index <= oops_count(), "must be a valid non-zero index");
520     assert(!_oops_are_stale, "oops are stale");
521     return &oops_begin()[index - 1];
522   }
523 
524   // Support for meta data in scopes and relocs:
525   // Note: index 0 is reserved for null.
526   Metadata*     metadata_at(int index) const      { return index == 0 ? NULL: *metadata_addr_at(index); }
527   Metadata**  metadata_addr_at(int index) const {  // for GC
528     // relocation indexes are biased by 1 (because 0 is reserved)
529     assert(index > 0 && index <= metadata_count(), "must be a valid non-zero index");
530     return &metadata_begin()[index - 1];
531   }
532 
533   void copy_values(GrowableArray<jobject>* oops);
534   void copy_values(GrowableArray<Metadata*>* metadata);
535 
536   void free_native_invokers();
537 
538   // Relocation support
539 private:
540   void fix_oop_relocations(address begin, address end, bool initialize_immediates);
541   inline void initialize_immediate_oop(oop* dest, jobject handle);
542 
543 public:
544   void fix_oop_relocations(address begin, address end) { fix_oop_relocations(begin, end, false); }
545   void fix_oop_relocations()                           { fix_oop_relocations(NULL, NULL, false); }
546 
547   // Sweeper support
548   long  stack_traversal_mark()                    { return _stack_traversal_mark; }
549   void  set_stack_traversal_mark(long l)          { _stack_traversal_mark = l; }
550 
551   // On-stack replacement support
552   int   osr_entry_bci() const                     { assert(is_osr_method(), "wrong kind of nmethod"); return _entry_bci; }
553   address  osr_entry() const                      { assert(is_osr_method(), "wrong kind of nmethod"); return _osr_entry_point; }
554   void  invalidate_osr_method();
555   nmethod* osr_link() const                       { return _osr_link; }
556   void     set_osr_link(nmethod *n)               { _osr_link = n; }
557 
558   // Verify calls to dead methods have been cleaned.
559   void verify_clean_inline_caches();
560 
561   // unlink and deallocate this nmethod
562   // Only NMethodSweeper class is expected to use this. NMethodSweeper is not
563   // expected to use any other private methods/data in this class.
564 
565  protected:
566   void flush();
567 
568  public:
569   // When true is returned, it is unsafe to remove this nmethod even if
570   // it is a zombie, since the VM or the ServiceThread might still be
571   // using it.
572   bool is_locked_by_vm() const                    { return _lock_count >0; }
573 
574   // See comment at definition of _last_seen_on_stack
575   void mark_as_seen_on_stack();
576   bool can_convert_to_zombie();
577 
578   // Evolution support. We make old (discarded) compiled methods point to new Method*s.
579   void set_method(Method* method) { _method = method; }
580 
581 #if INCLUDE_JVMCI
582   // Gets the JVMCI name of this nmethod.
583   const char* jvmci_name();
584 
585   // Records the pending failed speculation in the
586   // JVMCI speculation log associated with this nmethod.
587   void update_speculation(JavaThread* thread);
588 
589   // Gets the data specific to a JVMCI compiled method.
590   // This returns a non-NULL value iff this nmethod was
591   // compiled by the JVMCI compiler.
592   JVMCINMethodData* jvmci_nmethod_data() const {
593     return jvmci_data_size() == 0 ? NULL : (JVMCINMethodData*) jvmci_data_begin();
594   }
595 #endif
596 
597  public:
598   void oops_do(OopClosure* f) { oops_do(f, false); }
599   void oops_do(OopClosure* f, bool allow_dead);
600 
601   // All-in-one claiming of nmethods: returns true if the caller successfully claimed that
602   // nmethod.
603   bool oops_do_try_claim();
604 
605   // Class containing callbacks for the oops_do_process_weak/strong() methods
606   // below.
607   class OopsDoProcessor {
608   public:
609     // Process the oops of the given nmethod based on whether it has been called
610     // in a weak or strong processing context, i.e. apply either weak or strong
611     // work on it.
612     virtual void do_regular_processing(nmethod* nm) = 0;
613     // Assuming that the oops of the given nmethod has already been its weak
614     // processing applied, apply the remaining strong processing part.
615     virtual void do_remaining_strong_processing(nmethod* nm) = 0;
616   };
617 
618   // The following two methods do the work corresponding to weak/strong nmethod
619   // processing.
620   void oops_do_process_weak(OopsDoProcessor* p);
621   void oops_do_process_strong(OopsDoProcessor* p);
622 
623   static void oops_do_marking_prologue();
624   static void oops_do_marking_epilogue();
625 
626  private:
627   ScopeDesc* scope_desc_in(address begin, address end);
628 
629   address* orig_pc_addr(const frame* fr);
630 
631   // used by jvmti to track if the load and unload events has been reported
632   bool  unload_reported() const                   { return _unload_reported; }
633   void  set_unload_reported()                     { _unload_reported = true; }
634   bool  load_reported() const                     { return _load_reported; }
635   void  set_load_reported()                       { _load_reported = true; }
636 
637  public:
638   // copying of debugging information
639   void copy_scopes_pcs(PcDesc* pcs, int count);
640   void copy_scopes_data(address buffer, int size);
641 
642   // Accessor/mutator for the original pc of a frame before a frame was deopted.
643   address get_original_pc(const frame* fr) { return *orig_pc_addr(fr); }
644   void    set_original_pc(const frame* fr, address pc) { *orig_pc_addr(fr) = pc; }
645 
646   // jvmti support:
647   void post_compiled_method_load_event(JvmtiThreadState* state = NULL);
648 
649   // verify operations
650   void verify();
651   void verify_scopes();
652   void verify_interrupt_point(address interrupt_point);
653 
654   // Disassemble this nmethod with additional debug information, e.g. information about blocks.
655   void decode2(outputStream* st) const;
656   void print_constant_pool(outputStream* st);
657 
658   // Avoid hiding of parent's 'decode(outputStream*)' method.
659   void decode(outputStream* st) const { decode2(st); } // just delegate here.
660 
661   // printing support
662   void print()                          const;
663   void print(outputStream* st)          const;
664   void print_code();
665 
666 #if defined(SUPPORT_DATA_STRUCTS)
667   // print output in opt build for disassembler library
668   void print_relocations()                        PRODUCT_RETURN;
669   void print_pcs() { print_pcs_on(tty); }
670   void print_pcs_on(outputStream* st);
671   void print_scopes() { print_scopes_on(tty); }
672   void print_scopes_on(outputStream* st)          PRODUCT_RETURN;
673   void print_value_on(outputStream* st) const;
674   void print_native_invokers();
675   void print_handler_table();
676   void print_nul_chk_table();
677   void print_recorded_oop(int log_n, int index);
678   void print_recorded_oops();
679   void print_recorded_metadata();
680 
681   void print_oops(outputStream* st);     // oops from the underlying CodeBlob.
682   void print_metadata(outputStream* st); // metadata in metadata pool.
683 #else
684   // void print_pcs()                             PRODUCT_RETURN;
685   void print_pcs()                                { return; }
686 #endif
687 
688   void print_calls(outputStream* st)              PRODUCT_RETURN;
689   static void print_statistics()                  PRODUCT_RETURN;
690 
691   void maybe_print_nmethod(DirectiveSet* directive);
692   void print_nmethod(bool print_code);
693 
694   // need to re-define this from CodeBlob else the overload hides it
695   virtual void print_on(outputStream* st) const { CodeBlob::print_on(st); }
696   void print_on(outputStream* st, const char* msg) const;
697 
698   // Logging
699   void log_identity(xmlStream* log) const;
700   void log_new_nmethod() const;
701   void log_state_change() const;
702 
703   // Prints block-level comments, including nmethod specific block labels:
704   virtual void print_block_comment(outputStream* stream, address block_begin) const {
705 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
706     print_nmethod_labels(stream, block_begin);
707     CodeBlob::print_block_comment(stream, block_begin);
708 #endif
709   }
710 
711   void print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels=true) const;
712   const char* nmethod_section_label(address pos) const;
713 
714   // returns whether this nmethod has code comments.
715   bool has_code_comment(address begin, address end);
716   // Prints a comment for one native instruction (reloc info, pc desc)
717   void print_code_comment_on(outputStream* st, int column, address begin, address end);
718 
719   // Compiler task identification.  Note that all OSR methods
720   // are numbered in an independent sequence if CICountOSR is true,
721   // and native method wrappers are also numbered independently if
722   // CICountNative is true.
723   virtual int compile_id() const { return _compile_id; }
724   const char* compile_kind() const;
725 
726   // tells if any of this method's dependencies have been invalidated
727   // (this is expensive!)
728   static void check_all_dependencies(DepChange& changes);
729 
730   // tells if this compiled method is dependent on the given changes,
731   // and the changes have invalidated it
732   bool check_dependency_on(DepChange& changes);
733 
734   // Fast breakpoint support. Tells if this compiled method is
735   // dependent on the given method. Returns true if this nmethod
736   // corresponds to the given method as well.
737   virtual bool is_dependent_on_method(Method* dependee);
738 
739   // is it ok to patch at address?
740   bool is_patchable_at(address instr_address);
741 
742   // JVMTI's GetLocalInstance() support
743   ByteSize native_receiver_sp_offset() {
744     return _native_receiver_sp_offset;
745   }
746   ByteSize native_basic_lock_sp_offset() {
747     return _native_basic_lock_sp_offset;
748   }
749 
750   // support for code generation
751   static int verified_entry_point_offset()        { return offset_of(nmethod, _verified_entry_point); }
752   static int osr_entry_point_offset()             { return offset_of(nmethod, _osr_entry_point); }
753   static int state_offset()                       { return offset_of(nmethod, _state); }
754 
755   virtual void metadata_do(MetadataClosure* f);
756 
757   NativeCallWrapper* call_wrapper_at(address call) const;
758   NativeCallWrapper* call_wrapper_before(address return_pc) const;
759   address call_instruction_address(address pc) const;
760 
761   virtual CompiledStaticCall* compiledStaticCall_at(Relocation* call_site) const;
762   virtual CompiledStaticCall* compiledStaticCall_at(address addr) const;
763   virtual CompiledStaticCall* compiledStaticCall_before(address addr) const;
764 };
765 
766 // Locks an nmethod so its code will not get removed and it will not
767 // be made into a zombie, even if it is a not_entrant method. After the
768 // nmethod becomes a zombie, if CompiledMethodUnload event processing
769 // needs to be done, then lock_nmethod() is used directly to keep the
770 // generated code from being reused too early.
771 class nmethodLocker : public StackObj {
772   CompiledMethod* _nm;
773 
774  public:
775 
776   // note: nm can be NULL
777   // Only JvmtiDeferredEvent::compiled_method_unload_event()
778   // should pass zombie_ok == true.
779   static void lock_nmethod(CompiledMethod* nm, bool zombie_ok = false);
780   static void unlock_nmethod(CompiledMethod* nm); // (ditto)
781 
782   nmethodLocker(address pc); // derive nm from pc
783   nmethodLocker(nmethod *nm) { _nm = nm; lock_nmethod(_nm); }
784   nmethodLocker(CompiledMethod *nm) {
785     _nm = nm;
786     lock(_nm);
787   }
788 
789   static void lock(CompiledMethod* method, bool zombie_ok = false) {
790     if (method == NULL) return;
791     lock_nmethod(method, zombie_ok);
792   }
793 
794   static void unlock(CompiledMethod* method) {
795     if (method == NULL) return;
796     unlock_nmethod(method);
797   }
798 
799   nmethodLocker() { _nm = NULL; }
800   ~nmethodLocker() {
801     unlock(_nm);
802   }
803 
804   CompiledMethod* code() { return _nm; }
805   void set_code(CompiledMethod* new_nm, bool zombie_ok = false) {
806     unlock(_nm);   // note:  This works even if _nm==new_nm.
807     _nm = new_nm;
808     lock(_nm, zombie_ok);
809   }
810 };
811 
812 #endif // SHARE_CODE_NMETHOD_HPP