1 /*
2 * Copyright (c) 1997, 2025, 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 #include "runtime/mutexLocker.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 ICacheInvalidationContext;
45 class ImplicitExceptionTable;
46 class JvmtiThreadState;
47 class MetadataClosure;
48 class NativeCallWrapper;
49 class OopIterateClosure;
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 _skipped_instructions_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_entry_offset;
234 // Offset (from insts_end) of the unwind handler if it exists
235 int16_t _unwind_handler_offset;
236 // Number of arguments passed on the stack
237 uint16_t _num_stack_arg_slots;
238
239 #if INCLUDE_JVMCI
708 int skipped_instructions_size () const { return _skipped_instructions_size; }
709 int total_size() const;
710
711 // Containment
712 bool consts_contains (address addr) const { return consts_begin () <= addr && addr < consts_end (); }
713 // Returns true if a given address is in the 'insts' section. The method
714 // insts_contains_inclusive() is end-inclusive.
715 bool insts_contains (address addr) const { return insts_begin () <= addr && addr < insts_end (); }
716 bool insts_contains_inclusive(address addr) const { return insts_begin () <= addr && addr <= insts_end (); }
717 bool stub_contains (address addr) const { return stub_begin () <= addr && addr < stub_end (); }
718 bool oops_contains (oop* addr) const { return oops_begin () <= addr && addr < oops_end (); }
719 bool metadata_contains (Metadata** addr) const { return metadata_begin () <= addr && addr < metadata_end (); }
720 bool scopes_data_contains (address addr) const { return scopes_data_begin () <= addr && addr < scopes_data_end (); }
721 bool scopes_pcs_contains (PcDesc* addr) const { return scopes_pcs_begin () <= addr && addr < scopes_pcs_end (); }
722 bool handler_table_contains (address addr) const { return handler_table_begin() <= addr && addr < handler_table_end(); }
723 bool nul_chk_table_contains (address addr) const { return nul_chk_table_begin() <= addr && addr < nul_chk_table_end(); }
724
725 // entry points
726 address entry_point() const { return code_begin() + _entry_offset; } // normal entry point
727 address verified_entry_point() const { return code_begin() + _verified_entry_offset; } // if klass is correct
728
729 enum : signed char { not_installed = -1, // in construction, only the owner doing the construction is
730 // allowed to advance state
731 in_use = 0, // executable nmethod
732 not_entrant = 1 // marked for deoptimization but activations may still exist
733 };
734
735 // flag accessing and manipulation
736 bool is_not_installed() const { return _state == not_installed; }
737 bool is_in_use() const { return _state <= in_use; }
738 bool is_not_entrant() const { return _state == not_entrant; }
739 int get_state() const { return _state; }
740
741 void clear_unloading_state();
742 // Heuristically deduce an nmethod isn't worth keeping around
743 bool is_cold();
744 bool is_unloading();
745 void do_unloading(bool unloading_occurred);
746
747 bool make_in_use() {
767 }
768
769 // tells whether frames described by this nmethod can be deoptimized
770 // note: native wrappers cannot be deoptimized.
771 bool can_be_deoptimized() const { return is_java_method(); }
772
773 bool has_dependencies() { return dependencies_size() != 0; }
774 void print_dependencies_on(outputStream* out) PRODUCT_RETURN;
775 void flush_dependencies();
776
777 template<typename T>
778 T* gc_data() const { return reinterpret_cast<T*>(_gc_data); }
779 template<typename T>
780 void set_gc_data(T* gc_data) { _gc_data = reinterpret_cast<void*>(gc_data); }
781
782 bool has_unsafe_access() const { return _flags._has_unsafe_access; }
783 bool has_monitors() const { return _flags._has_monitors; }
784 bool has_scoped_access() const { return _flags._has_scoped_access; }
785 bool has_wide_vectors() const { return _flags._has_wide_vectors; }
786
787 bool has_flushed_dependencies() const { return _has_flushed_dependencies; }
788 void set_has_flushed_dependencies(bool z) {
789 assert(!has_flushed_dependencies(), "should only happen once");
790 _has_flushed_dependencies = z;
791 }
792
793 bool is_unlinked() const { return _is_unlinked; }
794 void set_is_unlinked() {
795 assert(!_is_unlinked, "already unlinked");
796 _is_unlinked = true;
797 }
798
799 int comp_level() const { return _comp_level; }
800
801 // Support for oops in scopes and relocs:
802 // Note: index 0 is reserved for null.
803 oop oop_at(int index) const;
804 oop oop_at_phantom(int index) const; // phantom reference
805 oop* oop_addr_at(int index) const { // for GC
806 // relocation indexes are biased by 1 (because 0 is reserved)
846 address handler_for_exception_and_pc(Handle exception, address pc);
847 void add_handler_for_exception_and_pc(Handle exception, address pc, address handler);
848 void clean_exception_cache();
849
850 void add_exception_cache_entry(ExceptionCache* new_entry);
851 ExceptionCache* exception_cache_entry_for_exception(Handle exception);
852
853
854 // Deopt
855 // Return true is the PC is one would expect if the frame is being deopted.
856 inline bool is_deopt_pc(address pc);
857 inline bool is_deopt_entry(address pc);
858
859 // Accessor/mutator for the original pc of a frame before a frame was deopted.
860 address get_original_pc(const frame* fr) { return *orig_pc_addr(fr); }
861 void set_original_pc(const frame* fr, address pc) { *orig_pc_addr(fr) = pc; }
862
863 const char* state() const;
864
865 bool inlinecache_check_contains(address addr) const {
866 return (addr >= code_begin() && addr < verified_entry_point());
867 }
868
869 void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f);
870
871 // implicit exceptions support
872 address continuation_for_implicit_div0_exception(address pc) { return continuation_for_implicit_exception(pc, true); }
873 address continuation_for_implicit_null_exception(address pc) { return continuation_for_implicit_exception(pc, false); }
874
875 // Inline cache support for class unloading and nmethod unloading
876 private:
877 void cleanup_inline_caches_impl(bool unloading_occurred, bool clean_all);
878
879 address continuation_for_implicit_exception(address pc, bool for_div0_check);
880
881 public:
882 // Serial version used by whitebox test
883 void cleanup_inline_caches_whitebox();
884
885 void clear_inline_caches();
886
1079
1080 // Logging
1081 void log_identity(xmlStream* log) const;
1082 void log_new_nmethod() const;
1083 void log_relocated_nmethod(nmethod* original) const;
1084 void log_state_change(InvalidationReason invalidation_reason) const;
1085
1086 // Prints block-level comments, including nmethod specific block labels:
1087 void print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels=true) const;
1088 const char* nmethod_section_label(address pos) const;
1089
1090 // returns whether this nmethod has code comments.
1091 bool has_code_comment(address begin, address end);
1092 // Prints a comment for one native instruction (reloc info, pc desc)
1093 void print_code_comment_on(outputStream* st, int column, address begin, address end);
1094
1095 // tells if this compiled method is dependent on the given changes,
1096 // and the changes have invalidated it
1097 bool check_dependency_on(DepChange& changes);
1098
1099 // Fast breakpoint support. Tells if this compiled method is
1100 // dependent on the given method. Returns true if this nmethod
1101 // corresponds to the given method as well.
1102 bool is_dependent_on_method(Method* dependee);
1103
1104 // JVMTI's GetLocalInstance() support
1105 ByteSize native_receiver_sp_offset() {
1106 assert(is_native_method(), "sanity");
1107 return _native_receiver_sp_offset;
1108 }
1109 ByteSize native_basic_lock_sp_offset() {
1110 assert(is_native_method(), "sanity");
1111 return _native_basic_lock_sp_offset;
1112 }
1113
1114 // support for code generation
1115 static ByteSize osr_entry_point_offset() { return byte_offset_of(nmethod, _osr_entry_point); }
1116 static ByteSize state_offset() { return byte_offset_of(nmethod, _state); }
1117
1118 void metadata_do(MetadataClosure* f);
1119
1120 address call_instruction_address(address pc) const;
1121
|
1 /*
2 * Copyright (c) 1997, 2026, 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 #include "runtime/mutexLocker.hpp"
34
35 class AbstractCompiler;
36 class CompiledDirectCall;
37 class CompiledIC;
38 class CompiledICData;
39 class CompileTask;
40 class DepChange;
41 class Dependencies;
42 class DirectiveSet;
43 class DebugInformationRecorder;
44 class ExceptionHandlerTable;
45 class ICacheInvalidationContext;
46 class ImplicitExceptionTable;
47 class JvmtiThreadState;
48 class MetadataClosure;
49 class NativeCallWrapper;
50 class OopIterateClosure;
201 };
202
203 // nmethod's read-only data
204 address _immutable_data;
205
206 PcDescContainer* _pc_desc_container;
207 ExceptionCache* volatile _exception_cache;
208
209 void* _gc_data;
210
211 struct oops_do_mark_link; // Opaque data type.
212 static nmethod* volatile _oops_do_mark_nmethods;
213 oops_do_mark_link* volatile _oops_do_mark_link;
214
215 CompiledICData* _compiled_ic_data;
216
217 // offsets for entry points
218 address _osr_entry_point; // entry point for on stack replacement
219 uint16_t _entry_offset; // entry point with class check
220 uint16_t _verified_entry_offset; // entry point without class check
221 uint16_t _inline_entry_offset; // inline type entry point (unpack all inline type args) with class check
222 uint16_t _verified_inline_entry_offset; // inline type entry point (unpack all inline type args) without class check
223 uint16_t _verified_inline_ro_entry_offset; // inline type entry point (unpack receiver only) without class check
224 int _entry_bci; // != InvocationEntryBci if this nmethod is an on-stack replacement method
225 int _immutable_data_size;
226
227 // _consts_offset == _content_offset because SECT_CONSTS is first in code buffer
228
229 int _skipped_instructions_size;
230
231 int _stub_offset;
232
233 // Offsets for different stubs section parts
234 int _exception_offset;
235 // All deoptee's will resume execution at this location described by
236 // this offset.
237 int _deopt_handler_entry_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 #if INCLUDE_JVMCI
712 int skipped_instructions_size () const { return _skipped_instructions_size; }
713 int total_size() const;
714
715 // Containment
716 bool consts_contains (address addr) const { return consts_begin () <= addr && addr < consts_end (); }
717 // Returns true if a given address is in the 'insts' section. The method
718 // insts_contains_inclusive() is end-inclusive.
719 bool insts_contains (address addr) const { return insts_begin () <= addr && addr < insts_end (); }
720 bool insts_contains_inclusive(address addr) const { return insts_begin () <= addr && addr <= insts_end (); }
721 bool stub_contains (address addr) const { return stub_begin () <= addr && addr < stub_end (); }
722 bool oops_contains (oop* addr) const { return oops_begin () <= addr && addr < oops_end (); }
723 bool metadata_contains (Metadata** addr) const { return metadata_begin () <= addr && addr < metadata_end (); }
724 bool scopes_data_contains (address addr) const { return scopes_data_begin () <= addr && addr < scopes_data_end (); }
725 bool scopes_pcs_contains (PcDesc* addr) const { return scopes_pcs_begin () <= addr && addr < scopes_pcs_end (); }
726 bool handler_table_contains (address addr) const { return handler_table_begin() <= addr && addr < handler_table_end(); }
727 bool nul_chk_table_contains (address addr) const { return nul_chk_table_begin() <= addr && addr < nul_chk_table_end(); }
728
729 // entry points
730 address entry_point() const { return code_begin() + _entry_offset; } // normal entry point
731 address verified_entry_point() const { return code_begin() + _verified_entry_offset; } // if klass is correct
732 address inline_entry_point() const { return code_begin() + _inline_entry_offset; } // inline type entry point (unpack all inline type args)
733 address verified_inline_entry_point() const { return code_begin() + _verified_inline_entry_offset; } // inline type entry point (unpack all inline type args) without class check
734 address verified_inline_ro_entry_point() const { return code_begin() + _verified_inline_ro_entry_offset; } // inline type entry point (only unpack receiver) without class check
735
736 enum : signed char { not_installed = -1, // in construction, only the owner doing the construction is
737 // allowed to advance state
738 in_use = 0, // executable nmethod
739 not_entrant = 1 // marked for deoptimization but activations may still exist
740 };
741
742 // flag accessing and manipulation
743 bool is_not_installed() const { return _state == not_installed; }
744 bool is_in_use() const { return _state <= in_use; }
745 bool is_not_entrant() const { return _state == not_entrant; }
746 int get_state() const { return _state; }
747
748 void clear_unloading_state();
749 // Heuristically deduce an nmethod isn't worth keeping around
750 bool is_cold();
751 bool is_unloading();
752 void do_unloading(bool unloading_occurred);
753
754 bool make_in_use() {
774 }
775
776 // tells whether frames described by this nmethod can be deoptimized
777 // note: native wrappers cannot be deoptimized.
778 bool can_be_deoptimized() const { return is_java_method(); }
779
780 bool has_dependencies() { return dependencies_size() != 0; }
781 void print_dependencies_on(outputStream* out) PRODUCT_RETURN;
782 void flush_dependencies();
783
784 template<typename T>
785 T* gc_data() const { return reinterpret_cast<T*>(_gc_data); }
786 template<typename T>
787 void set_gc_data(T* gc_data) { _gc_data = reinterpret_cast<void*>(gc_data); }
788
789 bool has_unsafe_access() const { return _flags._has_unsafe_access; }
790 bool has_monitors() const { return _flags._has_monitors; }
791 bool has_scoped_access() const { return _flags._has_scoped_access; }
792 bool has_wide_vectors() const { return _flags._has_wide_vectors; }
793
794 bool needs_stack_repair() const {
795 if (is_compiled_by_c1()) {
796 return method()->c1_needs_stack_repair();
797 } else if (is_compiled_by_c2()) {
798 return method()->c2_needs_stack_repair();
799 } else {
800 return false;
801 }
802 }
803
804 bool has_flushed_dependencies() const { return _has_flushed_dependencies; }
805 void set_has_flushed_dependencies(bool z) {
806 assert(!has_flushed_dependencies(), "should only happen once");
807 _has_flushed_dependencies = z;
808 }
809
810 bool is_unlinked() const { return _is_unlinked; }
811 void set_is_unlinked() {
812 assert(!_is_unlinked, "already unlinked");
813 _is_unlinked = true;
814 }
815
816 int comp_level() const { return _comp_level; }
817
818 // Support for oops in scopes and relocs:
819 // Note: index 0 is reserved for null.
820 oop oop_at(int index) const;
821 oop oop_at_phantom(int index) const; // phantom reference
822 oop* oop_addr_at(int index) const { // for GC
823 // relocation indexes are biased by 1 (because 0 is reserved)
863 address handler_for_exception_and_pc(Handle exception, address pc);
864 void add_handler_for_exception_and_pc(Handle exception, address pc, address handler);
865 void clean_exception_cache();
866
867 void add_exception_cache_entry(ExceptionCache* new_entry);
868 ExceptionCache* exception_cache_entry_for_exception(Handle exception);
869
870
871 // Deopt
872 // Return true is the PC is one would expect if the frame is being deopted.
873 inline bool is_deopt_pc(address pc);
874 inline bool is_deopt_entry(address pc);
875
876 // Accessor/mutator for the original pc of a frame before a frame was deopted.
877 address get_original_pc(const frame* fr) { return *orig_pc_addr(fr); }
878 void set_original_pc(const frame* fr, address pc) { *orig_pc_addr(fr) = pc; }
879
880 const char* state() const;
881
882 bool inlinecache_check_contains(address addr) const {
883 return (addr >= code_begin() && (addr < verified_entry_point() || addr < verified_inline_entry_point()));
884 }
885
886 void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f);
887
888 // implicit exceptions support
889 address continuation_for_implicit_div0_exception(address pc) { return continuation_for_implicit_exception(pc, true); }
890 address continuation_for_implicit_null_exception(address pc) { return continuation_for_implicit_exception(pc, false); }
891
892 // Inline cache support for class unloading and nmethod unloading
893 private:
894 void cleanup_inline_caches_impl(bool unloading_occurred, bool clean_all);
895
896 address continuation_for_implicit_exception(address pc, bool for_div0_check);
897
898 public:
899 // Serial version used by whitebox test
900 void cleanup_inline_caches_whitebox();
901
902 void clear_inline_caches();
903
1096
1097 // Logging
1098 void log_identity(xmlStream* log) const;
1099 void log_new_nmethod() const;
1100 void log_relocated_nmethod(nmethod* original) const;
1101 void log_state_change(InvalidationReason invalidation_reason) const;
1102
1103 // Prints block-level comments, including nmethod specific block labels:
1104 void print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels=true) const;
1105 const char* nmethod_section_label(address pos) const;
1106
1107 // returns whether this nmethod has code comments.
1108 bool has_code_comment(address begin, address end);
1109 // Prints a comment for one native instruction (reloc info, pc desc)
1110 void print_code_comment_on(outputStream* st, int column, address begin, address end);
1111
1112 // tells if this compiled method is dependent on the given changes,
1113 // and the changes have invalidated it
1114 bool check_dependency_on(DepChange& changes);
1115
1116 // Tells if this compiled method is dependent on the given method.
1117 // Returns true if this nmethod corresponds to the given method as well.
1118 // It is used for fast breakpoint support and updating the calling convention
1119 // in case of mismatch.
1120 bool is_dependent_on_method(Method* dependee);
1121
1122 // JVMTI's GetLocalInstance() support
1123 ByteSize native_receiver_sp_offset() {
1124 assert(is_native_method(), "sanity");
1125 return _native_receiver_sp_offset;
1126 }
1127 ByteSize native_basic_lock_sp_offset() {
1128 assert(is_native_method(), "sanity");
1129 return _native_basic_lock_sp_offset;
1130 }
1131
1132 // support for code generation
1133 static ByteSize osr_entry_point_offset() { return byte_offset_of(nmethod, _osr_entry_point); }
1134 static ByteSize state_offset() { return byte_offset_of(nmethod, _state); }
1135
1136 void metadata_do(MetadataClosure* f);
1137
1138 address call_instruction_address(address pc) const;
1139
|