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