671
672 // handle the case of an anchor explicitly set in continuation code that doesn't have a callee
673 JavaThread* thread = reg_map->thread();
674 if ((thread->has_last_Java_frame() && fr.sp() == thread->last_Java_sp())
675 JVMTI_ONLY(|| (method()->is_continuation_enter_intrinsic() && thread->on_monitor_waited_event()))) {
676 return;
677 }
678
679 if (!method()->is_native()) {
680 address pc = fr.pc();
681 bool has_receiver, has_appendix;
682 Symbol* signature;
683
684 // The method attached by JIT-compilers should be used, if present.
685 // Bytecode can be inaccurate in such case.
686 Method* callee = attached_method_before_pc(pc);
687 if (callee != nullptr) {
688 has_receiver = !(callee->access_flags().is_static());
689 has_appendix = false;
690 signature = callee->signature();
691 } else {
692 SimpleScopeDesc ssd(this, pc);
693
694 Bytecode_invoke call(methodHandle(Thread::current(), ssd.method()), ssd.bci());
695 has_receiver = call.has_receiver();
696 has_appendix = call.has_appendix();
697 signature = call.signature();
698 }
699
700 fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f);
701 } else if (method()->is_continuation_enter_intrinsic()) {
702 // This method only calls Continuation.enter()
703 Symbol* signature = vmSymbols::continuationEnter_signature();
704 fr.oops_compiled_arguments_do(signature, false, false, reg_map, f);
705 }
706 }
707
708 Method* nmethod::attached_method(address call_instr) {
709 assert(code_contains(call_instr), "not part of the nmethod");
710 RelocIterator iter(this, call_instr, call_instr + 1);
1170 _compiled_ic_data = nullptr;
1171
1172 _is_unloading_state = 0;
1173 _state = not_installed;
1174
1175 _has_flushed_dependencies = false;
1176 _is_unlinked = false;
1177 _load_reported = false; // jvmti state
1178
1179 _deoptimization_status = not_marked;
1180
1181 // SECT_CONSTS is first in code buffer so the offset should be 0.
1182 int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1183 assert(consts_offset == 0, "const_offset: %d", consts_offset);
1184
1185 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1186
1187 CHECKED_CAST(_entry_offset, uint16_t, (offsets->value(CodeOffsets::Entry)));
1188 CHECKED_CAST(_verified_entry_offset, uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1189
1190 _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1191 }
1192
1193 // Post initialization
1194 void nmethod::post_init() {
1195 clear_unloading_state();
1196
1197 finalize_relocations();
1198
1199 // Flush generated code
1200 ICache::invalidate_range(code_begin(), code_size());
1201
1202 Universe::heap()->register_nmethod(this);
1203
1204 #ifdef COMPILER2
1205 HotCodeCollector::register_nmethod(this);
1206 #endif // COMPILER2
1207
1208 DEBUG_ONLY(Universe::heap()->verify_nmethod(this));
1209
1217 int nmethod_size,
1218 int compile_id,
1219 CodeOffsets* offsets,
1220 CodeBuffer* code_buffer,
1221 int frame_size,
1222 ByteSize basic_lock_owner_sp_offset,
1223 ByteSize basic_lock_sp_offset,
1224 OopMapSet* oop_maps,
1225 int mutable_data_size)
1226 : CodeBlob("native nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1227 offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1228 _deoptimization_generation(0),
1229 _gc_epoch(CodeCache::gc_epoch()),
1230 _method(method),
1231 _native_receiver_sp_offset(basic_lock_owner_sp_offset),
1232 _native_basic_lock_sp_offset(basic_lock_sp_offset)
1233 {
1234 {
1235 DEBUG_ONLY(NoSafepointVerifier nsv;)
1236 assert_locked_or_safepoint(CodeCache_lock);
1237
1238 init_defaults(code_buffer, offsets);
1239
1240 _osr_entry_point = nullptr;
1241 _pc_desc_container = nullptr;
1242 _entry_bci = InvocationEntryBci;
1243 _compile_id = compile_id;
1244 _comp_level = CompLevel_none;
1245 _compiler_type = type;
1246 _orig_pc_offset = 0;
1247 _num_stack_arg_slots = 0;
1248
1249 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1250 // Continuation enter intrinsic
1251 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1252 } else {
1253 _exception_offset = 0;
1254 }
1255 // Native wrappers do not have deopt handlers. Make the values
1256 // something that will never match a pc like the nmethod vtable entry
1257 _deopt_handler_entry_offset = 0;
1358 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
1359 if (_mutable_data == nullptr) {
1360 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for mutable data");
1361 }
1362 memcpy(mutable_data_begin(), nm.mutable_data_begin(), nm.mutable_data_size());
1363 } else {
1364 _mutable_data = nullptr;
1365 }
1366
1367 _deoptimization_generation = 0;
1368 _gc_epoch = CodeCache::gc_epoch();
1369 _method = nm._method;
1370 _osr_link = nullptr;
1371
1372 _exception_cache = nullptr;
1373 _gc_data = nullptr;
1374 _oops_do_mark_nmethods = nullptr;
1375 _oops_do_mark_link = nullptr;
1376 _compiled_ic_data = nullptr;
1377
1378 if (nm._osr_entry_point != nullptr) {
1379 _osr_entry_point = (nm._osr_entry_point - (address) &nm) + (address) this;
1380 } else {
1381 _osr_entry_point = nullptr;
1382 }
1383
1384 _entry_offset = nm._entry_offset;
1385 _verified_entry_offset = nm._verified_entry_offset;
1386 _entry_bci = nm._entry_bci;
1387 _immutable_data_size = nm._immutable_data_size;
1388
1389 _skipped_instructions_size = nm._skipped_instructions_size;
1390 _stub_offset = nm._stub_offset;
1391 _exception_offset = nm._exception_offset;
1392 _deopt_handler_entry_offset = nm._deopt_handler_entry_offset;
1393 _unwind_handler_offset = nm._unwind_handler_offset;
1394 _num_stack_arg_slots = nm._num_stack_arg_slots;
1395 _nul_chk_table_offset = nm._nul_chk_table_offset;
1396 _handler_table_offset = nm._handler_table_offset;
1397 _scopes_pcs_offset = nm._scopes_pcs_offset;
1398 _scopes_data_offset = nm._scopes_data_offset;
1399 _immutable_data_ref_count_offset = nm._immutable_data_ref_count_offset;
1400
1401 // Increment number of references to immutable data to share it between nmethods
1402 if (_immutable_data_size > 0) {
1403 _immutable_data = nm._immutable_data;
1404 inc_immutable_data_ref_count();
1405 } else {
1640 "C2 compiler doesn't provide exception handler stub code.");
1641 if (has_exception_handler) {
1642 _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions);
1643 } else {
1644 _exception_offset = -1;
1645 }
1646
1647 _deopt_handler_entry_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
1648
1649 if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
1650 // C1 generates UnwindHandler at the end of instructions section.
1651 // Calculate positive offset as distance between the start of stubs section
1652 // (which is also the end of instructions section) and the start of the handler.
1653 int unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
1654 CHECKED_CAST(_unwind_handler_offset, int16_t, (_stub_offset - unwind_handler_offset));
1655 } else {
1656 _unwind_handler_offset = -1;
1657 }
1658
1659 int metadata_size = align_up(code_buffer->total_metadata_size(), wordSize);
1660 assert(_mutable_data_size == _relocation_size + metadata_size,
1661 "wrong mutable data size: %d != %d + %d",
1662 _mutable_data_size, _relocation_size, metadata_size);
1663 assert(nmethod_size == data_end() - header_begin(), "wrong nmethod size: %d != %d",
1664 nmethod_size, (int)(code_end() - header_begin()));
1665
1666 _immutable_data_size = immutable_data_size;
1667 if (immutable_data_size > 0) {
1668 assert(immutable_data != nullptr, "required");
1669 _immutable_data = immutable_data;
1670 } else {
1671 // We need unique not null address
1672 _immutable_data = blob_end();
1673 }
1674 CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
1675 CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
1676 _scopes_pcs_offset = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
1677 _scopes_data_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size());
1678
1679 _immutable_data_ref_count_offset = _scopes_data_offset + align_up(debug_info->data_size(), oopSize);
3062 }
3063
3064 bool nmethod::check_dependency_on(DepChange& changes) {
3065 // What has happened:
3066 // 1) a new class dependee has been added
3067 // 2) dependee and all its super classes have been marked
3068 bool found_check = false; // set true if we are upset
3069 for (Dependencies::DepStream deps(this); deps.next(); ) {
3070 // Evaluate only relevant dependencies.
3071 if (deps.spot_check_dependency_at(changes) != nullptr) {
3072 found_check = true;
3073 NOT_DEBUG(break);
3074 }
3075 }
3076 return found_check;
3077 }
3078
3079 // Called from mark_for_deoptimization, when dependee is invalidated.
3080 bool nmethod::is_dependent_on_method(Method* dependee) {
3081 for (Dependencies::DepStream deps(this); deps.next(); ) {
3082 if (deps.type() != Dependencies::evol_method)
3083 continue;
3084 Method* method = deps.method_argument(0);
3085 if (method == dependee) return true;
3086 }
3087 return false;
3088 }
3089
3090 void nmethod_init() {
3091 // make sure you didn't forget to adjust the filler fields
3092 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
3093 }
3094
3095 // -----------------------------------------------------------------------------
3096 // Verification
3097
3098 class VerifyOopsClosure: public OopClosure {
3099 nmethod* _nm;
3100 bool _ok;
3101 public:
3102 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
3103 bool ok() { return _ok; }
3104 virtual void do_oop(oop* p) {
3105 if (oopDesc::is_oop_or_null(*p)) return;
3261 if (size () > 0) st->print_cr(" total in heap [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3262 p2i(this),
3263 p2i(this) + size(),
3264 size());
3265 if (consts_size () > 0) st->print_cr(" constants [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3266 p2i(consts_begin()),
3267 p2i(consts_end()),
3268 consts_size());
3269 if (insts_size () > 0) st->print_cr(" main code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3270 p2i(insts_begin()),
3271 p2i(insts_end()),
3272 insts_size());
3273 if (stub_size () > 0) st->print_cr(" stub code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3274 p2i(stub_begin()),
3275 p2i(stub_end()),
3276 stub_size());
3277 if (oops_size () > 0) st->print_cr(" oops [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3278 p2i(oops_begin()),
3279 p2i(oops_end()),
3280 oops_size());
3281 if (mutable_data_size() > 0) st->print_cr(" mutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3282 p2i(mutable_data_begin()),
3283 p2i(mutable_data_end()),
3284 mutable_data_size());
3285 if (relocation_size() > 0) st->print_cr(" relocation [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3286 p2i(relocation_begin()),
3287 p2i(relocation_end()),
3288 relocation_size());
3289 if (metadata_size () > 0) st->print_cr(" metadata [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3290 p2i(metadata_begin()),
3291 p2i(metadata_end()),
3292 metadata_size());
3293 if (immutable_data_size() > 0) st->print_cr(" immutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3294 p2i(immutable_data_begin()),
3295 p2i(immutable_data_end()),
3296 immutable_data_size());
3297 if (dependencies_size () > 0) st->print_cr(" dependencies [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3298 p2i(dependencies_begin()),
3299 p2i(dependencies_end()),
3300 dependencies_size());
3301 if (nul_chk_table_size() > 0) st->print_cr(" nul chk table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3302 p2i(nul_chk_table_begin()),
3303 p2i(nul_chk_table_end()),
3304 nul_chk_table_size());
3305 if (handler_table_size() > 0) st->print_cr(" handler table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3865 return st.as_string();
3866 }
3867 }
3868 }
3869 return have_one ? "other" : nullptr;
3870 }
3871
3872 // Return the last scope in (begin..end]
3873 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
3874 PcDesc* p = pc_desc_near(begin+1);
3875 if (p != nullptr && p->real_pc(this) <= end) {
3876 return new ScopeDesc(this, p);
3877 }
3878 return nullptr;
3879 }
3880
3881 const char* nmethod::nmethod_section_label(address pos) const {
3882 const char* label = nullptr;
3883 if (pos == code_begin()) label = "[Instructions begin]";
3884 if (pos == entry_point()) label = "[Entry Point]";
3885 if (pos == verified_entry_point()) label = "[Verified Entry Point]";
3886 if (pos == consts_begin() && pos != insts_begin()) label = "[Constants]";
3887 // Check stub_code before checking exception_handler or deopt_handler.
3888 if (pos == this->stub_begin()) label = "[Stub Code]";
3889 if (pos == exception_begin()) label = "[Exception Handler]";
3890 if (pos == deopt_handler_entry()) label = "[Deopt Handler Entry Point]";
3891 return label;
3892 }
3893
3894 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
3895 if (print_section_labels) {
3896 const char* label = nmethod_section_label(block_begin);
3897 if (label != nullptr) {
3898 stream->bol();
3899 stream->print_cr("%s", label);
3900 }
3901 }
3902
3903 if (block_begin == entry_point()) {
3904 Method* m = method();
3905 if (m != nullptr) {
3906 stream->print(" # ");
3907 m->print_value_on(stream);
3908 stream->cr();
3909 }
3910 if (m != nullptr && !is_osr_method()) {
3911 ResourceMark rm;
3912 int sizeargs = m->size_of_parameters();
3913 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
3914 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
3915 {
3916 int sig_index = 0;
3917 if (!m->is_static())
3918 sig_bt[sig_index++] = T_OBJECT; // 'this'
3919 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
3920 BasicType t = ss.type();
3921 sig_bt[sig_index++] = t;
3922 if (type2size[t] == 2) {
3923 sig_bt[sig_index++] = T_VOID;
3924 } else {
3925 assert(type2size[t] == 1, "size is 1 or 2");
3926 }
3927 }
3928 assert(sig_index == sizeargs, "");
3929 }
3930 const char* spname = "sp"; // make arch-specific?
3931 SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs);
3932 int stack_slot_offset = this->frame_size() * wordSize;
3933 int tab1 = 14, tab2 = 24;
3934 int sig_index = 0;
3935 int arg_index = (m->is_static() ? 0 : -1);
3936 bool did_old_sp = false;
3937 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
3938 bool at_this = (arg_index == -1);
3939 bool at_old_sp = false;
3940 BasicType t = (at_this ? T_OBJECT : ss.type());
3941 assert(t == sig_bt[sig_index], "sigs in sync");
3942 if (at_this)
3943 stream->print(" # this: ");
3944 else
3945 stream->print(" # parm%d: ", arg_index);
3946 stream->move_to(tab1);
3947 VMReg fst = regs[sig_index].first();
3948 VMReg snd = regs[sig_index].second();
3949 if (fst->is_reg()) {
3950 stream->print("%s", fst->name());
3951 if (snd->is_valid()) {
3952 stream->print(":%s", snd->name());
3953 }
3954 } else if (fst->is_stack()) {
3955 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
3956 if (offset == stack_slot_offset) at_old_sp = true;
3957 stream->print("[%s+0x%x]", spname, offset);
3958 } else {
3959 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
3960 }
3961 stream->print(" ");
3962 stream->move_to(tab2);
3963 stream->print("= ");
3964 if (at_this) {
3965 m->method_holder()->print_value_on(stream);
3966 } else {
3967 bool did_name = false;
3968 if (!at_this && ss.is_reference()) {
3969 Symbol* name = ss.as_symbol();
3970 name->print_value_on(stream);
3971 did_name = true;
3972 }
3973 if (!did_name)
3974 stream->print("%s", type2name(t));
3975 }
3976 if (at_old_sp) {
3977 stream->print(" (%s of caller)", spname);
3978 did_old_sp = true;
3979 }
3980 stream->cr();
3981 sig_index += type2size[t];
3982 arg_index += 1;
3983 if (!at_this) ss.next();
3984 }
3985 if (!did_old_sp) {
3986 stream->print(" # ");
3987 stream->move_to(tab1);
3988 stream->print("[%s+0x%x]", spname, stack_slot_offset);
3989 stream->print(" (%s of caller)", spname);
3990 stream->cr();
3991 }
3992 }
3993 }
3994 }
3995
3996 // Returns whether this nmethod has code comments.
3997 bool nmethod::has_code_comment(address begin, address end) {
3998 // scopes?
3999 ScopeDesc* sd = scope_desc_in(begin, end);
4000 if (sd != nullptr) return true;
4001
4002 // relocations?
4003 const char* str = reloc_string_for(begin, end);
4004 if (str != nullptr) return true;
4005
4006 // implicit exceptions?
4007 int cont_offset = ImplicitExceptionTable(this).continuation_offset((uint)(begin - code_begin()));
4008 if (cont_offset != 0) return true;
4009
4010 return false;
4011 }
4012
4091 else
4092 st->print("<UNKNOWN>");
4093 break;
4094 }
4095 case Bytecodes::_getfield:
4096 case Bytecodes::_putfield:
4097 case Bytecodes::_getstatic:
4098 case Bytecodes::_putstatic:
4099 {
4100 Bytecode_field field(methodHandle(thread, sd->method()), sd->bci());
4101 st->print(" ");
4102 if (field.name() != nullptr)
4103 field.name()->print_symbol_on(st);
4104 else
4105 st->print("<UNKNOWN>");
4106 }
4107 default:
4108 break;
4109 }
4110 }
4111 st->print(" {reexecute=%d rethrow=%d return_oop=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop());
4112 }
4113
4114 // Print all scopes
4115 for (;sd != nullptr; sd = sd->sender()) {
4116 st->move_to(column, 6, 0);
4117 st->print("; -");
4118 if (sd->should_reexecute()) {
4119 st->print(" (reexecute)");
4120 }
4121 if (sd->method() == nullptr) {
4122 st->print("method is nullptr");
4123 } else {
4124 sd->method()->print_short_name(st);
4125 }
4126 int lineno = sd->method()->line_number_from_bci(sd->bci());
4127 if (lineno != -1) {
4128 st->print("@%d (line %d)", sd->bci(), lineno);
4129 } else {
4130 st->print("@%d", sd->bci());
4131 }
|
671
672 // handle the case of an anchor explicitly set in continuation code that doesn't have a callee
673 JavaThread* thread = reg_map->thread();
674 if ((thread->has_last_Java_frame() && fr.sp() == thread->last_Java_sp())
675 JVMTI_ONLY(|| (method()->is_continuation_enter_intrinsic() && thread->on_monitor_waited_event()))) {
676 return;
677 }
678
679 if (!method()->is_native()) {
680 address pc = fr.pc();
681 bool has_receiver, has_appendix;
682 Symbol* signature;
683
684 // The method attached by JIT-compilers should be used, if present.
685 // Bytecode can be inaccurate in such case.
686 Method* callee = attached_method_before_pc(pc);
687 if (callee != nullptr) {
688 has_receiver = !(callee->access_flags().is_static());
689 has_appendix = false;
690 signature = callee->signature();
691
692 // If inline types are passed as fields, use the extended signature
693 // which contains the types of all (oop) fields of the inline type.
694 if (is_compiled_by_c2() && callee->has_scalarized_args()) {
695 const GrowableArray<SigEntry>* sig = callee->adapter()->get_sig_cc();
696 assert(sig != nullptr, "sig should never be null");
697 TempNewSymbol tmp_sig = SigEntry::create_symbol(sig);
698 has_receiver = false; // The extended signature contains the receiver type
699 fr.oops_compiled_arguments_do(tmp_sig, has_receiver, has_appendix, reg_map, f);
700 return;
701 }
702 } else {
703 SimpleScopeDesc ssd(this, pc);
704
705 Bytecode_invoke call(methodHandle(Thread::current(), ssd.method()), ssd.bci());
706 has_receiver = call.has_receiver();
707 has_appendix = call.has_appendix();
708 signature = call.signature();
709 }
710
711 fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f);
712 } else if (method()->is_continuation_enter_intrinsic()) {
713 // This method only calls Continuation.enter()
714 Symbol* signature = vmSymbols::continuationEnter_signature();
715 fr.oops_compiled_arguments_do(signature, false, false, reg_map, f);
716 }
717 }
718
719 Method* nmethod::attached_method(address call_instr) {
720 assert(code_contains(call_instr), "not part of the nmethod");
721 RelocIterator iter(this, call_instr, call_instr + 1);
1181 _compiled_ic_data = nullptr;
1182
1183 _is_unloading_state = 0;
1184 _state = not_installed;
1185
1186 _has_flushed_dependencies = false;
1187 _is_unlinked = false;
1188 _load_reported = false; // jvmti state
1189
1190 _deoptimization_status = not_marked;
1191
1192 // SECT_CONSTS is first in code buffer so the offset should be 0.
1193 int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1194 assert(consts_offset == 0, "const_offset: %d", consts_offset);
1195
1196 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1197
1198 CHECKED_CAST(_entry_offset, uint16_t, (offsets->value(CodeOffsets::Entry)));
1199 CHECKED_CAST(_verified_entry_offset, uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1200
1201 _inline_entry_offset = _entry_offset;
1202 _verified_inline_entry_offset = _verified_entry_offset;
1203 _verified_inline_ro_entry_offset = _verified_entry_offset;
1204
1205 _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1206 }
1207
1208 // Post initialization
1209 void nmethod::post_init() {
1210 clear_unloading_state();
1211
1212 finalize_relocations();
1213
1214 // Flush generated code
1215 ICache::invalidate_range(code_begin(), code_size());
1216
1217 Universe::heap()->register_nmethod(this);
1218
1219 #ifdef COMPILER2
1220 HotCodeCollector::register_nmethod(this);
1221 #endif // COMPILER2
1222
1223 DEBUG_ONLY(Universe::heap()->verify_nmethod(this));
1224
1232 int nmethod_size,
1233 int compile_id,
1234 CodeOffsets* offsets,
1235 CodeBuffer* code_buffer,
1236 int frame_size,
1237 ByteSize basic_lock_owner_sp_offset,
1238 ByteSize basic_lock_sp_offset,
1239 OopMapSet* oop_maps,
1240 int mutable_data_size)
1241 : CodeBlob("native nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1242 offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1243 _deoptimization_generation(0),
1244 _gc_epoch(CodeCache::gc_epoch()),
1245 _method(method),
1246 _native_receiver_sp_offset(basic_lock_owner_sp_offset),
1247 _native_basic_lock_sp_offset(basic_lock_sp_offset)
1248 {
1249 {
1250 DEBUG_ONLY(NoSafepointVerifier nsv;)
1251 assert_locked_or_safepoint(CodeCache_lock);
1252 assert(!method->has_scalarized_args(), "scalarized native wrappers not supported yet");
1253 init_defaults(code_buffer, offsets);
1254
1255 _osr_entry_point = nullptr;
1256 _pc_desc_container = nullptr;
1257 _entry_bci = InvocationEntryBci;
1258 _compile_id = compile_id;
1259 _comp_level = CompLevel_none;
1260 _compiler_type = type;
1261 _orig_pc_offset = 0;
1262 _num_stack_arg_slots = 0;
1263
1264 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1265 // Continuation enter intrinsic
1266 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1267 } else {
1268 _exception_offset = 0;
1269 }
1270 // Native wrappers do not have deopt handlers. Make the values
1271 // something that will never match a pc like the nmethod vtable entry
1272 _deopt_handler_entry_offset = 0;
1373 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
1374 if (_mutable_data == nullptr) {
1375 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for mutable data");
1376 }
1377 memcpy(mutable_data_begin(), nm.mutable_data_begin(), nm.mutable_data_size());
1378 } else {
1379 _mutable_data = nullptr;
1380 }
1381
1382 _deoptimization_generation = 0;
1383 _gc_epoch = CodeCache::gc_epoch();
1384 _method = nm._method;
1385 _osr_link = nullptr;
1386
1387 _exception_cache = nullptr;
1388 _gc_data = nullptr;
1389 _oops_do_mark_nmethods = nullptr;
1390 _oops_do_mark_link = nullptr;
1391 _compiled_ic_data = nullptr;
1392
1393 // Relocate the OSR entry point from nm to the new nmethod.
1394 if (nm._osr_entry_point == nullptr) {
1395 _osr_entry_point = nullptr;
1396 } else {
1397 address new_addr = nm._osr_entry_point - (address) &nm + (address) this;
1398 assert(new_addr >= code_begin() && new_addr < code_end(),
1399 "relocated address must be within code bounds");
1400 _osr_entry_point = new_addr;
1401 }
1402 _entry_offset = nm._entry_offset;
1403 _verified_entry_offset = nm._verified_entry_offset;
1404 _inline_entry_offset = nm._inline_entry_offset;
1405 _verified_inline_entry_offset = nm._verified_inline_entry_offset;
1406 _verified_inline_ro_entry_offset = nm._verified_inline_ro_entry_offset;
1407
1408 _entry_bci = nm._entry_bci;
1409 _immutable_data_size = nm._immutable_data_size;
1410
1411 _skipped_instructions_size = nm._skipped_instructions_size;
1412 _stub_offset = nm._stub_offset;
1413 _exception_offset = nm._exception_offset;
1414 _deopt_handler_entry_offset = nm._deopt_handler_entry_offset;
1415 _unwind_handler_offset = nm._unwind_handler_offset;
1416 _num_stack_arg_slots = nm._num_stack_arg_slots;
1417 _nul_chk_table_offset = nm._nul_chk_table_offset;
1418 _handler_table_offset = nm._handler_table_offset;
1419 _scopes_pcs_offset = nm._scopes_pcs_offset;
1420 _scopes_data_offset = nm._scopes_data_offset;
1421 _immutable_data_ref_count_offset = nm._immutable_data_ref_count_offset;
1422
1423 // Increment number of references to immutable data to share it between nmethods
1424 if (_immutable_data_size > 0) {
1425 _immutable_data = nm._immutable_data;
1426 inc_immutable_data_ref_count();
1427 } else {
1662 "C2 compiler doesn't provide exception handler stub code.");
1663 if (has_exception_handler) {
1664 _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions);
1665 } else {
1666 _exception_offset = -1;
1667 }
1668
1669 _deopt_handler_entry_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
1670
1671 if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
1672 // C1 generates UnwindHandler at the end of instructions section.
1673 // Calculate positive offset as distance between the start of stubs section
1674 // (which is also the end of instructions section) and the start of the handler.
1675 int unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
1676 CHECKED_CAST(_unwind_handler_offset, int16_t, (_stub_offset - unwind_handler_offset));
1677 } else {
1678 _unwind_handler_offset = -1;
1679 }
1680
1681 int metadata_size = align_up(code_buffer->total_metadata_size(), wordSize);
1682 if (offsets->value(CodeOffsets::Inline_Entry) != CodeOffsets::no_such_entry_point) {
1683 CHECKED_CAST(_inline_entry_offset , uint16_t, offsets->value(CodeOffsets::Inline_Entry));
1684 }
1685 if (offsets->value(CodeOffsets::Verified_Inline_Entry) != CodeOffsets::no_such_entry_point) {
1686 CHECKED_CAST(_verified_inline_entry_offset , uint16_t, offsets->value(CodeOffsets::Verified_Inline_Entry));
1687 }
1688 if (offsets->value(CodeOffsets::Verified_Inline_Entry_RO) != CodeOffsets::no_such_entry_point) {
1689 CHECKED_CAST(_verified_inline_ro_entry_offset, uint16_t, offsets->value(CodeOffsets::Verified_Inline_Entry_RO));
1690 }
1691
1692 assert(_mutable_data_size == _relocation_size + metadata_size,
1693 "wrong mutable data size: %d != %d + %d",
1694 _mutable_data_size, _relocation_size, metadata_size);
1695 assert(nmethod_size == data_end() - header_begin(), "wrong nmethod size: %d != %d",
1696 nmethod_size, (int)(code_end() - header_begin()));
1697
1698 _immutable_data_size = immutable_data_size;
1699 if (immutable_data_size > 0) {
1700 assert(immutable_data != nullptr, "required");
1701 _immutable_data = immutable_data;
1702 } else {
1703 // We need unique not null address
1704 _immutable_data = blob_end();
1705 }
1706 CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
1707 CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
1708 _scopes_pcs_offset = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
1709 _scopes_data_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size());
1710
1711 _immutable_data_ref_count_offset = _scopes_data_offset + align_up(debug_info->data_size(), oopSize);
3094 }
3095
3096 bool nmethod::check_dependency_on(DepChange& changes) {
3097 // What has happened:
3098 // 1) a new class dependee has been added
3099 // 2) dependee and all its super classes have been marked
3100 bool found_check = false; // set true if we are upset
3101 for (Dependencies::DepStream deps(this); deps.next(); ) {
3102 // Evaluate only relevant dependencies.
3103 if (deps.spot_check_dependency_at(changes) != nullptr) {
3104 found_check = true;
3105 NOT_DEBUG(break);
3106 }
3107 }
3108 return found_check;
3109 }
3110
3111 // Called from mark_for_deoptimization, when dependee is invalidated.
3112 bool nmethod::is_dependent_on_method(Method* dependee) {
3113 for (Dependencies::DepStream deps(this); deps.next(); ) {
3114 if (Dependencies::has_method_dep(deps.type())) {
3115 Method* method = deps.method_argument(0);
3116 if (method == dependee) return true;
3117 }
3118 }
3119 return false;
3120 }
3121
3122 void nmethod_init() {
3123 // make sure you didn't forget to adjust the filler fields
3124 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
3125 }
3126
3127 // -----------------------------------------------------------------------------
3128 // Verification
3129
3130 class VerifyOopsClosure: public OopClosure {
3131 nmethod* _nm;
3132 bool _ok;
3133 public:
3134 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
3135 bool ok() { return _ok; }
3136 virtual void do_oop(oop* p) {
3137 if (oopDesc::is_oop_or_null(*p)) return;
3293 if (size () > 0) st->print_cr(" total in heap [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3294 p2i(this),
3295 p2i(this) + size(),
3296 size());
3297 if (consts_size () > 0) st->print_cr(" constants [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3298 p2i(consts_begin()),
3299 p2i(consts_end()),
3300 consts_size());
3301 if (insts_size () > 0) st->print_cr(" main code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3302 p2i(insts_begin()),
3303 p2i(insts_end()),
3304 insts_size());
3305 if (stub_size () > 0) st->print_cr(" stub code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3306 p2i(stub_begin()),
3307 p2i(stub_end()),
3308 stub_size());
3309 if (oops_size () > 0) st->print_cr(" oops [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3310 p2i(oops_begin()),
3311 p2i(oops_end()),
3312 oops_size());
3313 if (mutable_data_size () > 0) st->print_cr(" mutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3314 p2i(mutable_data_begin()),
3315 p2i(mutable_data_end()),
3316 mutable_data_size());
3317 if (relocation_size () > 0) st->print_cr(" relocation [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3318 p2i(relocation_begin()),
3319 p2i(relocation_end()),
3320 relocation_size());
3321 if (metadata_size () > 0) st->print_cr(" metadata [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3322 p2i(metadata_begin()),
3323 p2i(metadata_end()),
3324 metadata_size());
3325 if (immutable_data_size() > 0) st->print_cr(" immutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3326 p2i(immutable_data_begin()),
3327 p2i(immutable_data_end()),
3328 immutable_data_size());
3329 if (dependencies_size () > 0) st->print_cr(" dependencies [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3330 p2i(dependencies_begin()),
3331 p2i(dependencies_end()),
3332 dependencies_size());
3333 if (nul_chk_table_size() > 0) st->print_cr(" nul chk table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3334 p2i(nul_chk_table_begin()),
3335 p2i(nul_chk_table_end()),
3336 nul_chk_table_size());
3337 if (handler_table_size() > 0) st->print_cr(" handler table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3897 return st.as_string();
3898 }
3899 }
3900 }
3901 return have_one ? "other" : nullptr;
3902 }
3903
3904 // Return the last scope in (begin..end]
3905 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
3906 PcDesc* p = pc_desc_near(begin+1);
3907 if (p != nullptr && p->real_pc(this) <= end) {
3908 return new ScopeDesc(this, p);
3909 }
3910 return nullptr;
3911 }
3912
3913 const char* nmethod::nmethod_section_label(address pos) const {
3914 const char* label = nullptr;
3915 if (pos == code_begin()) label = "[Instructions begin]";
3916 if (pos == entry_point()) label = "[Entry Point]";
3917 if (pos == inline_entry_point()) label = "[Inline Entry Point]";
3918 if (pos == verified_entry_point()) label = "[Verified Entry Point]";
3919 if (pos == verified_inline_entry_point()) label = "[Verified Inline Entry Point]";
3920 if (pos == verified_inline_ro_entry_point()) label = "[Verified Inline Entry Point (RO)]";
3921 if (pos == consts_begin() && pos != insts_begin()) label = "[Constants]";
3922 // Check stub_code before checking exception_handler or deopt_handler.
3923 if (pos == this->stub_begin()) label = "[Stub Code]";
3924 if (pos == exception_begin()) label = "[Exception Handler]";
3925 if (pos == deopt_handler_entry()) label = "[Deopt Handler Entry Point]";
3926 return label;
3927 }
3928
3929 static int maybe_print_entry_label(outputStream* stream, address pos, address entry, const char* label) {
3930 if (pos == entry) {
3931 stream->bol();
3932 stream->print_cr("%s", label);
3933 return 1;
3934 } else {
3935 return 0;
3936 }
3937 }
3938
3939 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
3940 if (print_section_labels) {
3941 int n = 0;
3942 // Multiple entry points may be at the same position. Print them all.
3943 n += maybe_print_entry_label(stream, block_begin, entry_point(), "[Entry Point]");
3944 n += maybe_print_entry_label(stream, block_begin, inline_entry_point(), "[Inline Entry Point]");
3945 n += maybe_print_entry_label(stream, block_begin, verified_entry_point(), "[Verified Entry Point]");
3946 n += maybe_print_entry_label(stream, block_begin, verified_inline_entry_point(), "[Verified Inline Entry Point]");
3947 n += maybe_print_entry_label(stream, block_begin, verified_inline_ro_entry_point(), "[Verified Inline Entry Point (RO)]");
3948 if (n == 0) {
3949 const char* label = nmethod_section_label(block_begin);
3950 if (label != nullptr) {
3951 stream->bol();
3952 stream->print_cr("%s", label);
3953 }
3954 }
3955 }
3956
3957 Method* m = method();
3958 if (m == nullptr || is_osr_method()) {
3959 return;
3960 }
3961
3962 // Print the name of the method (only once)
3963 address low = MIN3(entry_point(),
3964 verified_entry_point(),
3965 inline_entry_point());
3966 // The verified inline entry point and verified inline RO entry point are not always
3967 // used. When they are unused. CodeOffsets::Verified_Inline_Entry(_RO) is -1. Hence,
3968 // the calculated entry point is smaller than the block they are offsetting into.
3969 if (verified_inline_entry_point() >= block_begin) {
3970 low = MIN2(low, verified_inline_entry_point());
3971 }
3972 if (verified_inline_ro_entry_point() >= block_begin) {
3973 low = MIN2(low, verified_inline_ro_entry_point());
3974 }
3975 assert(low != nullptr, "sanity");
3976 if (block_begin == low) {
3977 stream->print(" # ");
3978 m->print_value_on(stream);
3979 stream->cr();
3980 }
3981
3982 // Print the arguments for the 3 types of verified entry points
3983 CompiledEntrySignature ces(m);
3984 ces.compute_calling_conventions(false);
3985 const GrowableArray<SigEntry>* sig_cc;
3986 const VMRegPair* regs;
3987 if (block_begin == verified_entry_point()) {
3988 sig_cc = ces.sig_cc();
3989 regs = ces.regs_cc();
3990 } else if (block_begin == verified_inline_entry_point()) {
3991 sig_cc = ces.sig();
3992 regs = ces.regs();
3993 } else if (block_begin == verified_inline_ro_entry_point()) {
3994 sig_cc = ces.sig_cc_ro();
3995 regs = ces.regs_cc_ro();
3996 } else {
3997 return;
3998 }
3999
4000 bool has_this = !m->is_static();
4001 if (ces.has_inline_recv() && block_begin == verified_entry_point()) {
4002 // <this> argument is scalarized for verified_entry_point()
4003 has_this = false;
4004 }
4005 const char* spname = "sp"; // make arch-specific?
4006 int stack_slot_offset = this->frame_size() * wordSize;
4007 int tab1 = 14, tab2 = 24;
4008 int sig_index = 0;
4009 int arg_index = has_this ? -1 : 0;
4010 bool did_old_sp = false;
4011 for (ExtendedSignature sig = ExtendedSignature(sig_cc, SigEntryFilter()); !sig.at_end(); ++sig) {
4012 bool at_this = (arg_index == -1);
4013 bool at_old_sp = false;
4014 BasicType t = (*sig)._bt;
4015 if (at_this) {
4016 stream->print(" # this: ");
4017 } else {
4018 stream->print(" # parm%d: ", arg_index);
4019 }
4020 stream->move_to(tab1);
4021 VMReg fst = regs[sig_index].first();
4022 VMReg snd = regs[sig_index].second();
4023 if (fst->is_reg()) {
4024 stream->print("%s", fst->name());
4025 if (snd->is_valid()) {
4026 stream->print(":%s", snd->name());
4027 }
4028 } else if (fst->is_stack()) {
4029 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
4030 if (offset == stack_slot_offset) at_old_sp = true;
4031 stream->print("[%s+0x%x]", spname, offset);
4032 } else {
4033 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
4034 }
4035 stream->print(" ");
4036 stream->move_to(tab2);
4037 stream->print("= ");
4038 if (at_this) {
4039 m->method_holder()->print_value_on(stream);
4040 } else {
4041 bool did_name = false;
4042 if (is_reference_type(t) && !(*sig)._vt_oop) {
4043 Symbol* name = (*sig)._name;
4044 name->print_value_on(stream);
4045 did_name = true;
4046 }
4047 if (!did_name)
4048 stream->print("%s", type2name(t));
4049 if ((*sig)._null_marker) {
4050 stream->print(" (null marker)");
4051 }
4052 if ((*sig)._vt_oop) {
4053 stream->print(" (VT OOP)");
4054 }
4055 }
4056 if (at_old_sp) {
4057 stream->print(" (%s of caller)", spname);
4058 did_old_sp = true;
4059 }
4060 stream->cr();
4061 sig_index += type2size[t];
4062 arg_index += 1;
4063 }
4064 if (!did_old_sp) {
4065 stream->print(" # ");
4066 stream->move_to(tab1);
4067 stream->print("[%s+0x%x]", spname, stack_slot_offset);
4068 stream->print(" (%s of caller)", spname);
4069 stream->cr();
4070 }
4071 }
4072
4073 // Returns whether this nmethod has code comments.
4074 bool nmethod::has_code_comment(address begin, address end) {
4075 // scopes?
4076 ScopeDesc* sd = scope_desc_in(begin, end);
4077 if (sd != nullptr) return true;
4078
4079 // relocations?
4080 const char* str = reloc_string_for(begin, end);
4081 if (str != nullptr) return true;
4082
4083 // implicit exceptions?
4084 int cont_offset = ImplicitExceptionTable(this).continuation_offset((uint)(begin - code_begin()));
4085 if (cont_offset != 0) return true;
4086
4087 return false;
4088 }
4089
4168 else
4169 st->print("<UNKNOWN>");
4170 break;
4171 }
4172 case Bytecodes::_getfield:
4173 case Bytecodes::_putfield:
4174 case Bytecodes::_getstatic:
4175 case Bytecodes::_putstatic:
4176 {
4177 Bytecode_field field(methodHandle(thread, sd->method()), sd->bci());
4178 st->print(" ");
4179 if (field.name() != nullptr)
4180 field.name()->print_symbol_on(st);
4181 else
4182 st->print("<UNKNOWN>");
4183 }
4184 default:
4185 break;
4186 }
4187 }
4188 st->print(" {reexecute=%d rethrow=%d return_oop=%d return_scalarized=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop(), sd->return_scalarized());
4189 }
4190
4191 // Print all scopes
4192 for (;sd != nullptr; sd = sd->sender()) {
4193 st->move_to(column, 6, 0);
4194 st->print("; -");
4195 if (sd->should_reexecute()) {
4196 st->print(" (reexecute)");
4197 }
4198 if (sd->method() == nullptr) {
4199 st->print("method is nullptr");
4200 } else {
4201 sd->method()->print_short_name(st);
4202 }
4203 int lineno = sd->method()->line_number_from_bci(sd->bci());
4204 if (lineno != -1) {
4205 st->print("@%d (line %d)", sd->bci(), lineno);
4206 } else {
4207 st->print("@%d", sd->bci());
4208 }
|