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