700
701 // handle the case of an anchor explicitly set in continuation code that doesn't have a callee
702 JavaThread* thread = reg_map->thread();
703 if ((thread->has_last_Java_frame() && fr.sp() == thread->last_Java_sp())
704 JVMTI_ONLY(|| (method()->is_continuation_enter_intrinsic() && thread->on_monitor_waited_event()))) {
705 return;
706 }
707
708 if (!method()->is_native()) {
709 address pc = fr.pc();
710 bool has_receiver, has_appendix;
711 Symbol* signature;
712
713 // The method attached by JIT-compilers should be used, if present.
714 // Bytecode can be inaccurate in such case.
715 Method* callee = attached_method_before_pc(pc);
716 if (callee != nullptr) {
717 has_receiver = !(callee->access_flags().is_static());
718 has_appendix = false;
719 signature = callee->signature();
720 } else {
721 SimpleScopeDesc ssd(this, pc);
722
723 Bytecode_invoke call(methodHandle(Thread::current(), ssd.method()), ssd.bci());
724 has_receiver = call.has_receiver();
725 has_appendix = call.has_appendix();
726 signature = call.signature();
727 }
728
729 fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f);
730 } else if (method()->is_continuation_enter_intrinsic()) {
731 // This method only calls Continuation.enter()
732 Symbol* signature = vmSymbols::continuationEnter_signature();
733 fr.oops_compiled_arguments_do(signature, false, false, reg_map, f);
734 }
735 }
736
737 Method* nmethod::attached_method(address call_instr) {
738 assert(code_contains(call_instr), "not part of the nmethod");
739 RelocIterator iter(this, call_instr, call_instr + 1);
1231
1232 _has_unsafe_access = 0;
1233 _has_wide_vectors = 0;
1234 _has_monitors = 0;
1235 _has_scoped_access = 0;
1236 _has_flushed_dependencies = 0;
1237 _is_unlinked = 0;
1238 _load_reported = 0; // jvmti state
1239
1240 _deoptimization_status = not_marked;
1241
1242 // SECT_CONSTS is first in code buffer so the offset should be 0.
1243 int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1244 assert(consts_offset == 0, "const_offset: %d", consts_offset);
1245
1246 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1247
1248 CHECKED_CAST(_entry_offset, uint16_t, (offsets->value(CodeOffsets::Entry)));
1249 CHECKED_CAST(_verified_entry_offset, uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1250
1251 _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1252 }
1253
1254 // Post initialization
1255 void nmethod::post_init() {
1256 clear_unloading_state();
1257
1258 finalize_relocations();
1259
1260 // Flush generated code
1261 ICache::invalidate_range(code_begin(), code_size());
1262
1263 Universe::heap()->register_nmethod(this);
1264
1265 #ifdef COMPILER2
1266 HotCodeCollector::register_nmethod(this);
1267 #endif // COMPILER2
1268
1269 DEBUG_ONLY(Universe::heap()->verify_nmethod(this));
1270
1278 int nmethod_size,
1279 int compile_id,
1280 CodeOffsets* offsets,
1281 CodeBuffer* code_buffer,
1282 int frame_size,
1283 ByteSize basic_lock_owner_sp_offset,
1284 ByteSize basic_lock_sp_offset,
1285 OopMapSet* oop_maps,
1286 int mutable_data_size)
1287 : CodeBlob("native nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1288 offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1289 _deoptimization_generation(0),
1290 _gc_epoch(CodeCache::gc_epoch()),
1291 _method(method),
1292 _native_receiver_sp_offset(basic_lock_owner_sp_offset),
1293 _native_basic_lock_sp_offset(basic_lock_sp_offset)
1294 {
1295 {
1296 DEBUG_ONLY(NoSafepointVerifier nsv;)
1297 assert_locked_or_safepoint(CodeCache_lock);
1298
1299 init_defaults(code_buffer, offsets);
1300
1301 _osr_entry_point = nullptr;
1302 _pc_desc_container = nullptr;
1303 _entry_bci = InvocationEntryBci;
1304 _compile_id = compile_id;
1305 _comp_level = CompLevel_none;
1306 _compiler_type = type;
1307 _orig_pc_offset = 0;
1308 _num_stack_arg_slots = 0;
1309
1310 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1311 // Continuation enter intrinsic
1312 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1313 } else {
1314 _exception_offset = 0;
1315 }
1316 // Native wrappers do not have deopt handlers. Make the values
1317 // something that will never match a pc like the nmethod vtable entry
1318 _deopt_handler_entry_offset = 0;
1422 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
1423 if (_mutable_data == nullptr) {
1424 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for mutable data");
1425 }
1426 memcpy(mutable_data_begin(), nm.mutable_data_begin(), nm.mutable_data_size());
1427 } else {
1428 _mutable_data = nullptr;
1429 }
1430
1431 _deoptimization_generation = 0;
1432 _gc_epoch = CodeCache::gc_epoch();
1433 _method = nm._method;
1434 _osr_link = nullptr;
1435
1436 _exception_cache = nullptr;
1437 _gc_data = nullptr;
1438 _oops_do_mark_nmethods = nullptr;
1439 _oops_do_mark_link = nullptr;
1440 _compiled_ic_data = nullptr;
1441
1442 if (nm._osr_entry_point != nullptr) {
1443 _osr_entry_point = (nm._osr_entry_point - (address) &nm) + (address) this;
1444 } else {
1445 _osr_entry_point = nullptr;
1446 }
1447
1448 _entry_offset = nm._entry_offset;
1449 _verified_entry_offset = nm._verified_entry_offset;
1450 _entry_bci = nm._entry_bci;
1451 _immutable_data_size = nm._immutable_data_size;
1452
1453 _skipped_instructions_size = nm._skipped_instructions_size;
1454 _stub_offset = nm._stub_offset;
1455 _exception_offset = nm._exception_offset;
1456 _deopt_handler_entry_offset = nm._deopt_handler_entry_offset;
1457 _unwind_handler_offset = nm._unwind_handler_offset;
1458 _num_stack_arg_slots = nm._num_stack_arg_slots;
1459 #if INCLUDE_JVMCI
1460 _metadata_size = nm._metadata_size;
1461 #endif
1462 _nul_chk_table_offset = nm._nul_chk_table_offset;
1463 _handler_table_offset = nm._handler_table_offset;
1464 _scopes_pcs_offset = nm._scopes_pcs_offset;
1465 _scopes_data_offset = nm._scopes_data_offset;
1466 #if INCLUDE_JVMCI
1467 _speculations_offset = nm._speculations_offset;
1468 #endif
1469 _immutable_data_ref_count_offset = nm._immutable_data_ref_count_offset;
1742 _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions);
1743 } else {
1744 _exception_offset = -1;
1745 }
1746
1747 _deopt_handler_entry_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
1748 }
1749 if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
1750 // C1 generates UnwindHandler at the end of instructions section.
1751 // Calculate positive offset as distance between the start of stubs section
1752 // (which is also the end of instructions section) and the start of the handler.
1753 int unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
1754 CHECKED_CAST(_unwind_handler_offset, int16_t, (_stub_offset - unwind_handler_offset));
1755 } else {
1756 _unwind_handler_offset = -1;
1757 }
1758
1759 int metadata_size = align_up(code_buffer->total_metadata_size(), wordSize);
1760 JVMCI_ONLY( _metadata_size = metadata_size; )
1761 int jvmci_data_size = 0 JVMCI_ONLY( + align_up(compiler->is_jvmci() ? jvmci_data->size() : 0, oopSize));
1762 assert(_mutable_data_size == _relocation_size + metadata_size + jvmci_data_size,
1763 "wrong mutable data size: %d != %d + %d + %d",
1764 _mutable_data_size, _relocation_size, metadata_size, jvmci_data_size);
1765 assert(nmethod_size == data_end() - header_begin(), "wrong nmethod size: %d != %d",
1766 nmethod_size, (int)(code_end() - header_begin()));
1767
1768 _immutable_data_size = immutable_data_size;
1769 if (immutable_data_size > 0) {
1770 assert(immutable_data != nullptr, "required");
1771 _immutable_data = immutable_data;
1772 } else {
1773 // We need unique not null address
1774 _immutable_data = blob_end();
1775 }
1776 CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
1777 CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
1778 _scopes_pcs_offset = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
1779 _scopes_data_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size());
1780
1781 #if INCLUDE_JVMCI
3218 }
3219
3220 bool nmethod::check_dependency_on(DepChange& changes) {
3221 // What has happened:
3222 // 1) a new class dependee has been added
3223 // 2) dependee and all its super classes have been marked
3224 bool found_check = false; // set true if we are upset
3225 for (Dependencies::DepStream deps(this); deps.next(); ) {
3226 // Evaluate only relevant dependencies.
3227 if (deps.spot_check_dependency_at(changes) != nullptr) {
3228 found_check = true;
3229 NOT_DEBUG(break);
3230 }
3231 }
3232 return found_check;
3233 }
3234
3235 // Called from mark_for_deoptimization, when dependee is invalidated.
3236 bool nmethod::is_dependent_on_method(Method* dependee) {
3237 for (Dependencies::DepStream deps(this); deps.next(); ) {
3238 if (deps.type() != Dependencies::evol_method)
3239 continue;
3240 Method* method = deps.method_argument(0);
3241 if (method == dependee) return true;
3242 }
3243 return false;
3244 }
3245
3246 void nmethod_init() {
3247 // make sure you didn't forget to adjust the filler fields
3248 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
3249 }
3250
3251 // -----------------------------------------------------------------------------
3252 // Verification
3253
3254 class VerifyOopsClosure: public OopClosure {
3255 nmethod* _nm;
3256 bool _ok;
3257 public:
3258 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
3259 bool ok() { return _ok; }
3260 virtual void do_oop(oop* p) {
3261 if (oopDesc::is_oop_or_null(*p)) return;
3443 if (size () > 0) st->print_cr(" total in heap [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3444 p2i(this),
3445 p2i(this) + size(),
3446 size());
3447 if (consts_size () > 0) st->print_cr(" constants [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3448 p2i(consts_begin()),
3449 p2i(consts_end()),
3450 consts_size());
3451 if (insts_size () > 0) st->print_cr(" main code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3452 p2i(insts_begin()),
3453 p2i(insts_end()),
3454 insts_size());
3455 if (stub_size () > 0) st->print_cr(" stub code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3456 p2i(stub_begin()),
3457 p2i(stub_end()),
3458 stub_size());
3459 if (oops_size () > 0) st->print_cr(" oops [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3460 p2i(oops_begin()),
3461 p2i(oops_end()),
3462 oops_size());
3463 if (mutable_data_size() > 0) st->print_cr(" mutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3464 p2i(mutable_data_begin()),
3465 p2i(mutable_data_end()),
3466 mutable_data_size());
3467 if (relocation_size() > 0) st->print_cr(" relocation [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3468 p2i(relocation_begin()),
3469 p2i(relocation_end()),
3470 relocation_size());
3471 if (metadata_size () > 0) st->print_cr(" metadata [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3472 p2i(metadata_begin()),
3473 p2i(metadata_end()),
3474 metadata_size());
3475 #if INCLUDE_JVMCI
3476 if (jvmci_data_size () > 0) st->print_cr(" JVMCI data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3477 p2i(jvmci_data_begin()),
3478 p2i(jvmci_data_end()),
3479 jvmci_data_size());
3480 #endif
3481 if (immutable_data_size() > 0) st->print_cr(" immutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3482 p2i(immutable_data_begin()),
3483 p2i(immutable_data_end()),
3484 immutable_data_size());
3485 if (dependencies_size () > 0) st->print_cr(" dependencies [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3486 p2i(dependencies_begin()),
3487 p2i(dependencies_end()),
4059 return st.as_string();
4060 }
4061 }
4062 }
4063 return have_one ? "other" : nullptr;
4064 }
4065
4066 // Return the last scope in (begin..end]
4067 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
4068 PcDesc* p = pc_desc_near(begin+1);
4069 if (p != nullptr && p->real_pc(this) <= end) {
4070 return new ScopeDesc(this, p);
4071 }
4072 return nullptr;
4073 }
4074
4075 const char* nmethod::nmethod_section_label(address pos) const {
4076 const char* label = nullptr;
4077 if (pos == code_begin()) label = "[Instructions begin]";
4078 if (pos == entry_point()) label = "[Entry Point]";
4079 if (pos == verified_entry_point()) label = "[Verified Entry Point]";
4080 if (pos == consts_begin() && pos != insts_begin()) label = "[Constants]";
4081 // Check stub_code before checking exception_handler or deopt_handler.
4082 if (pos == this->stub_begin()) label = "[Stub Code]";
4083 if (JVMCI_ONLY(_exception_offset >= 0 &&) pos == exception_begin()) label = "[Exception Handler]";
4084 if (JVMCI_ONLY(_deopt_handler_entry_offset != -1 &&) pos == deopt_handler_entry()) label = "[Deopt Handler Entry Point]";
4085 return label;
4086 }
4087
4088 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
4089 if (print_section_labels) {
4090 const char* label = nmethod_section_label(block_begin);
4091 if (label != nullptr) {
4092 stream->bol();
4093 stream->print_cr("%s", label);
4094 }
4095 }
4096
4097 if (block_begin == entry_point()) {
4098 Method* m = method();
4099 if (m != nullptr) {
4100 stream->print(" # ");
4101 m->print_value_on(stream);
4102 stream->cr();
4103 }
4104 if (m != nullptr && !is_osr_method()) {
4105 ResourceMark rm;
4106 int sizeargs = m->size_of_parameters();
4107 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
4108 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
4109 {
4110 int sig_index = 0;
4111 if (!m->is_static())
4112 sig_bt[sig_index++] = T_OBJECT; // 'this'
4113 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
4114 BasicType t = ss.type();
4115 sig_bt[sig_index++] = t;
4116 if (type2size[t] == 2) {
4117 sig_bt[sig_index++] = T_VOID;
4118 } else {
4119 assert(type2size[t] == 1, "size is 1 or 2");
4120 }
4121 }
4122 assert(sig_index == sizeargs, "");
4123 }
4124 const char* spname = "sp"; // make arch-specific?
4125 SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs);
4126 int stack_slot_offset = this->frame_size() * wordSize;
4127 int tab1 = 14, tab2 = 24;
4128 int sig_index = 0;
4129 int arg_index = (m->is_static() ? 0 : -1);
4130 bool did_old_sp = false;
4131 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
4132 bool at_this = (arg_index == -1);
4133 bool at_old_sp = false;
4134 BasicType t = (at_this ? T_OBJECT : ss.type());
4135 assert(t == sig_bt[sig_index], "sigs in sync");
4136 if (at_this)
4137 stream->print(" # this: ");
4138 else
4139 stream->print(" # parm%d: ", arg_index);
4140 stream->move_to(tab1);
4141 VMReg fst = regs[sig_index].first();
4142 VMReg snd = regs[sig_index].second();
4143 if (fst->is_reg()) {
4144 stream->print("%s", fst->name());
4145 if (snd->is_valid()) {
4146 stream->print(":%s", snd->name());
4147 }
4148 } else if (fst->is_stack()) {
4149 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
4150 if (offset == stack_slot_offset) at_old_sp = true;
4151 stream->print("[%s+0x%x]", spname, offset);
4152 } else {
4153 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
4154 }
4155 stream->print(" ");
4156 stream->move_to(tab2);
4157 stream->print("= ");
4158 if (at_this) {
4159 m->method_holder()->print_value_on(stream);
4160 } else {
4161 bool did_name = false;
4162 if (!at_this && ss.is_reference()) {
4163 Symbol* name = ss.as_symbol();
4164 name->print_value_on(stream);
4165 did_name = true;
4166 }
4167 if (!did_name)
4168 stream->print("%s", type2name(t));
4169 }
4170 if (at_old_sp) {
4171 stream->print(" (%s of caller)", spname);
4172 did_old_sp = true;
4173 }
4174 stream->cr();
4175 sig_index += type2size[t];
4176 arg_index += 1;
4177 if (!at_this) ss.next();
4178 }
4179 if (!did_old_sp) {
4180 stream->print(" # ");
4181 stream->move_to(tab1);
4182 stream->print("[%s+0x%x]", spname, stack_slot_offset);
4183 stream->print(" (%s of caller)", spname);
4184 stream->cr();
4185 }
4186 }
4187 }
4188 }
4189
4190 // Returns whether this nmethod has code comments.
4191 bool nmethod::has_code_comment(address begin, address end) {
4192 // scopes?
4193 ScopeDesc* sd = scope_desc_in(begin, end);
4194 if (sd != nullptr) return true;
4195
4196 // relocations?
4197 const char* str = reloc_string_for(begin, end);
4198 if (str != nullptr) return true;
4199
4200 // implicit exceptions?
4201 int cont_offset = ImplicitExceptionTable(this).continuation_offset((uint)(begin - code_begin()));
4202 if (cont_offset != 0) return true;
4203
4204 return false;
4205 }
4206
4290 else
4291 st->print("<UNKNOWN>");
4292 break;
4293 }
4294 case Bytecodes::_getfield:
4295 case Bytecodes::_putfield:
4296 case Bytecodes::_getstatic:
4297 case Bytecodes::_putstatic:
4298 {
4299 Bytecode_field field(methodHandle(thread, sd->method()), sd->bci());
4300 st->print(" ");
4301 if (field.name() != nullptr)
4302 field.name()->print_symbol_on(st);
4303 else
4304 st->print("<UNKNOWN>");
4305 }
4306 default:
4307 break;
4308 }
4309 }
4310 st->print(" {reexecute=%d rethrow=%d return_oop=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop());
4311 }
4312
4313 // Print all scopes
4314 for (;sd != nullptr; sd = sd->sender()) {
4315 st->move_to(column, 6, 0);
4316 st->print("; -");
4317 if (sd->should_reexecute()) {
4318 st->print(" (reexecute)");
4319 }
4320 if (sd->method() == nullptr) {
4321 st->print("method is nullptr");
4322 } else {
4323 sd->method()->print_short_name(st);
4324 }
4325 int lineno = sd->method()->line_number_from_bci(sd->bci());
4326 if (lineno != -1) {
4327 st->print("@%d (line %d)", sd->bci(), lineno);
4328 } else {
4329 st->print("@%d", sd->bci());
4330 }
|
700
701 // handle the case of an anchor explicitly set in continuation code that doesn't have a callee
702 JavaThread* thread = reg_map->thread();
703 if ((thread->has_last_Java_frame() && fr.sp() == thread->last_Java_sp())
704 JVMTI_ONLY(|| (method()->is_continuation_enter_intrinsic() && thread->on_monitor_waited_event()))) {
705 return;
706 }
707
708 if (!method()->is_native()) {
709 address pc = fr.pc();
710 bool has_receiver, has_appendix;
711 Symbol* signature;
712
713 // The method attached by JIT-compilers should be used, if present.
714 // Bytecode can be inaccurate in such case.
715 Method* callee = attached_method_before_pc(pc);
716 if (callee != nullptr) {
717 has_receiver = !(callee->access_flags().is_static());
718 has_appendix = false;
719 signature = callee->signature();
720
721 // If inline types are passed as fields, use the extended signature
722 // which contains the types of all (oop) fields of the inline type.
723 if (is_compiled_by_c2() && callee->has_scalarized_args()) {
724 const GrowableArray<SigEntry>* sig = callee->adapter()->get_sig_cc();
725 assert(sig != nullptr, "sig should never be null");
726 TempNewSymbol tmp_sig = SigEntry::create_symbol(sig);
727 has_receiver = false; // The extended signature contains the receiver type
728 fr.oops_compiled_arguments_do(tmp_sig, has_receiver, has_appendix, reg_map, f);
729 return;
730 }
731 } else {
732 SimpleScopeDesc ssd(this, pc);
733
734 Bytecode_invoke call(methodHandle(Thread::current(), ssd.method()), ssd.bci());
735 has_receiver = call.has_receiver();
736 has_appendix = call.has_appendix();
737 signature = call.signature();
738 }
739
740 fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f);
741 } else if (method()->is_continuation_enter_intrinsic()) {
742 // This method only calls Continuation.enter()
743 Symbol* signature = vmSymbols::continuationEnter_signature();
744 fr.oops_compiled_arguments_do(signature, false, false, reg_map, f);
745 }
746 }
747
748 Method* nmethod::attached_method(address call_instr) {
749 assert(code_contains(call_instr), "not part of the nmethod");
750 RelocIterator iter(this, call_instr, call_instr + 1);
1242
1243 _has_unsafe_access = 0;
1244 _has_wide_vectors = 0;
1245 _has_monitors = 0;
1246 _has_scoped_access = 0;
1247 _has_flushed_dependencies = 0;
1248 _is_unlinked = 0;
1249 _load_reported = 0; // jvmti state
1250
1251 _deoptimization_status = not_marked;
1252
1253 // SECT_CONSTS is first in code buffer so the offset should be 0.
1254 int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1255 assert(consts_offset == 0, "const_offset: %d", consts_offset);
1256
1257 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1258
1259 CHECKED_CAST(_entry_offset, uint16_t, (offsets->value(CodeOffsets::Entry)));
1260 CHECKED_CAST(_verified_entry_offset, uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1261
1262 _inline_entry_offset = _entry_offset;
1263 _verified_inline_entry_offset = _verified_entry_offset;
1264 _verified_inline_ro_entry_offset = _verified_entry_offset;
1265
1266 _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1267 }
1268
1269 // Post initialization
1270 void nmethod::post_init() {
1271 clear_unloading_state();
1272
1273 finalize_relocations();
1274
1275 // Flush generated code
1276 ICache::invalidate_range(code_begin(), code_size());
1277
1278 Universe::heap()->register_nmethod(this);
1279
1280 #ifdef COMPILER2
1281 HotCodeCollector::register_nmethod(this);
1282 #endif // COMPILER2
1283
1284 DEBUG_ONLY(Universe::heap()->verify_nmethod(this));
1285
1293 int nmethod_size,
1294 int compile_id,
1295 CodeOffsets* offsets,
1296 CodeBuffer* code_buffer,
1297 int frame_size,
1298 ByteSize basic_lock_owner_sp_offset,
1299 ByteSize basic_lock_sp_offset,
1300 OopMapSet* oop_maps,
1301 int mutable_data_size)
1302 : CodeBlob("native nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1303 offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1304 _deoptimization_generation(0),
1305 _gc_epoch(CodeCache::gc_epoch()),
1306 _method(method),
1307 _native_receiver_sp_offset(basic_lock_owner_sp_offset),
1308 _native_basic_lock_sp_offset(basic_lock_sp_offset)
1309 {
1310 {
1311 DEBUG_ONLY(NoSafepointVerifier nsv;)
1312 assert_locked_or_safepoint(CodeCache_lock);
1313 assert(!method->has_scalarized_args(), "scalarized native wrappers not supported yet");
1314 init_defaults(code_buffer, offsets);
1315
1316 _osr_entry_point = nullptr;
1317 _pc_desc_container = nullptr;
1318 _entry_bci = InvocationEntryBci;
1319 _compile_id = compile_id;
1320 _comp_level = CompLevel_none;
1321 _compiler_type = type;
1322 _orig_pc_offset = 0;
1323 _num_stack_arg_slots = 0;
1324
1325 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1326 // Continuation enter intrinsic
1327 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1328 } else {
1329 _exception_offset = 0;
1330 }
1331 // Native wrappers do not have deopt handlers. Make the values
1332 // something that will never match a pc like the nmethod vtable entry
1333 _deopt_handler_entry_offset = 0;
1437 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
1438 if (_mutable_data == nullptr) {
1439 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for mutable data");
1440 }
1441 memcpy(mutable_data_begin(), nm.mutable_data_begin(), nm.mutable_data_size());
1442 } else {
1443 _mutable_data = nullptr;
1444 }
1445
1446 _deoptimization_generation = 0;
1447 _gc_epoch = CodeCache::gc_epoch();
1448 _method = nm._method;
1449 _osr_link = nullptr;
1450
1451 _exception_cache = nullptr;
1452 _gc_data = nullptr;
1453 _oops_do_mark_nmethods = nullptr;
1454 _oops_do_mark_link = nullptr;
1455 _compiled_ic_data = nullptr;
1456
1457 // Relocate the OSR entry point from nm to the new nmethod.
1458 if (nm._osr_entry_point == nullptr) {
1459 _osr_entry_point = nullptr;
1460 } else {
1461 address new_addr = nm._osr_entry_point - (address) &nm + (address) this;
1462 assert(new_addr >= code_begin() && new_addr < code_end(),
1463 "relocated address must be within code bounds");
1464 _osr_entry_point = new_addr;
1465 }
1466 _entry_offset = nm._entry_offset;
1467 _verified_entry_offset = nm._verified_entry_offset;
1468 _inline_entry_offset = nm._inline_entry_offset;
1469 _verified_inline_entry_offset = nm._verified_inline_entry_offset;
1470 _verified_inline_ro_entry_offset = nm._verified_inline_ro_entry_offset;
1471
1472 _entry_bci = nm._entry_bci;
1473 _immutable_data_size = nm._immutable_data_size;
1474
1475 _skipped_instructions_size = nm._skipped_instructions_size;
1476 _stub_offset = nm._stub_offset;
1477 _exception_offset = nm._exception_offset;
1478 _deopt_handler_entry_offset = nm._deopt_handler_entry_offset;
1479 _unwind_handler_offset = nm._unwind_handler_offset;
1480 _num_stack_arg_slots = nm._num_stack_arg_slots;
1481 #if INCLUDE_JVMCI
1482 _metadata_size = nm._metadata_size;
1483 #endif
1484 _nul_chk_table_offset = nm._nul_chk_table_offset;
1485 _handler_table_offset = nm._handler_table_offset;
1486 _scopes_pcs_offset = nm._scopes_pcs_offset;
1487 _scopes_data_offset = nm._scopes_data_offset;
1488 #if INCLUDE_JVMCI
1489 _speculations_offset = nm._speculations_offset;
1490 #endif
1491 _immutable_data_ref_count_offset = nm._immutable_data_ref_count_offset;
1764 _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions);
1765 } else {
1766 _exception_offset = -1;
1767 }
1768
1769 _deopt_handler_entry_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
1770 }
1771 if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
1772 // C1 generates UnwindHandler at the end of instructions section.
1773 // Calculate positive offset as distance between the start of stubs section
1774 // (which is also the end of instructions section) and the start of the handler.
1775 int unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
1776 CHECKED_CAST(_unwind_handler_offset, int16_t, (_stub_offset - unwind_handler_offset));
1777 } else {
1778 _unwind_handler_offset = -1;
1779 }
1780
1781 int metadata_size = align_up(code_buffer->total_metadata_size(), wordSize);
1782 JVMCI_ONLY( _metadata_size = metadata_size; )
1783 int jvmci_data_size = 0 JVMCI_ONLY( + align_up(compiler->is_jvmci() ? jvmci_data->size() : 0, oopSize));
1784 if (offsets->value(CodeOffsets::Inline_Entry) != CodeOffsets::no_such_entry_point) {
1785 CHECKED_CAST(_inline_entry_offset , uint16_t, offsets->value(CodeOffsets::Inline_Entry));
1786 }
1787 if (offsets->value(CodeOffsets::Verified_Inline_Entry) != CodeOffsets::no_such_entry_point) {
1788 CHECKED_CAST(_verified_inline_entry_offset , uint16_t, offsets->value(CodeOffsets::Verified_Inline_Entry));
1789 }
1790 if (offsets->value(CodeOffsets::Verified_Inline_Entry_RO) != CodeOffsets::no_such_entry_point) {
1791 CHECKED_CAST(_verified_inline_ro_entry_offset, uint16_t, offsets->value(CodeOffsets::Verified_Inline_Entry_RO));
1792 }
1793
1794 assert(_mutable_data_size == _relocation_size + metadata_size + jvmci_data_size,
1795 "wrong mutable data size: %d != %d + %d + %d",
1796 _mutable_data_size, _relocation_size, metadata_size, jvmci_data_size);
1797 assert(nmethod_size == data_end() - header_begin(), "wrong nmethod size: %d != %d",
1798 nmethod_size, (int)(code_end() - header_begin()));
1799
1800 _immutable_data_size = immutable_data_size;
1801 if (immutable_data_size > 0) {
1802 assert(immutable_data != nullptr, "required");
1803 _immutable_data = immutable_data;
1804 } else {
1805 // We need unique not null address
1806 _immutable_data = blob_end();
1807 }
1808 CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
1809 CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
1810 _scopes_pcs_offset = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
1811 _scopes_data_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size());
1812
1813 #if INCLUDE_JVMCI
3250 }
3251
3252 bool nmethod::check_dependency_on(DepChange& changes) {
3253 // What has happened:
3254 // 1) a new class dependee has been added
3255 // 2) dependee and all its super classes have been marked
3256 bool found_check = false; // set true if we are upset
3257 for (Dependencies::DepStream deps(this); deps.next(); ) {
3258 // Evaluate only relevant dependencies.
3259 if (deps.spot_check_dependency_at(changes) != nullptr) {
3260 found_check = true;
3261 NOT_DEBUG(break);
3262 }
3263 }
3264 return found_check;
3265 }
3266
3267 // Called from mark_for_deoptimization, when dependee is invalidated.
3268 bool nmethod::is_dependent_on_method(Method* dependee) {
3269 for (Dependencies::DepStream deps(this); deps.next(); ) {
3270 if (Dependencies::has_method_dep(deps.type())) {
3271 Method* method = deps.method_argument(0);
3272 if (method == dependee) return true;
3273 }
3274 }
3275 return false;
3276 }
3277
3278 void nmethod_init() {
3279 // make sure you didn't forget to adjust the filler fields
3280 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
3281 }
3282
3283 // -----------------------------------------------------------------------------
3284 // Verification
3285
3286 class VerifyOopsClosure: public OopClosure {
3287 nmethod* _nm;
3288 bool _ok;
3289 public:
3290 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
3291 bool ok() { return _ok; }
3292 virtual void do_oop(oop* p) {
3293 if (oopDesc::is_oop_or_null(*p)) return;
3475 if (size () > 0) st->print_cr(" total in heap [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3476 p2i(this),
3477 p2i(this) + size(),
3478 size());
3479 if (consts_size () > 0) st->print_cr(" constants [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3480 p2i(consts_begin()),
3481 p2i(consts_end()),
3482 consts_size());
3483 if (insts_size () > 0) st->print_cr(" main code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3484 p2i(insts_begin()),
3485 p2i(insts_end()),
3486 insts_size());
3487 if (stub_size () > 0) st->print_cr(" stub code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3488 p2i(stub_begin()),
3489 p2i(stub_end()),
3490 stub_size());
3491 if (oops_size () > 0) st->print_cr(" oops [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3492 p2i(oops_begin()),
3493 p2i(oops_end()),
3494 oops_size());
3495 if (mutable_data_size () > 0) st->print_cr(" mutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3496 p2i(mutable_data_begin()),
3497 p2i(mutable_data_end()),
3498 mutable_data_size());
3499 if (relocation_size () > 0) st->print_cr(" relocation [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3500 p2i(relocation_begin()),
3501 p2i(relocation_end()),
3502 relocation_size());
3503 if (metadata_size () > 0) st->print_cr(" metadata [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3504 p2i(metadata_begin()),
3505 p2i(metadata_end()),
3506 metadata_size());
3507 #if INCLUDE_JVMCI
3508 if (jvmci_data_size () > 0) st->print_cr(" JVMCI data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3509 p2i(jvmci_data_begin()),
3510 p2i(jvmci_data_end()),
3511 jvmci_data_size());
3512 #endif
3513 if (immutable_data_size() > 0) st->print_cr(" immutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3514 p2i(immutable_data_begin()),
3515 p2i(immutable_data_end()),
3516 immutable_data_size());
3517 if (dependencies_size () > 0) st->print_cr(" dependencies [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3518 p2i(dependencies_begin()),
3519 p2i(dependencies_end()),
4091 return st.as_string();
4092 }
4093 }
4094 }
4095 return have_one ? "other" : nullptr;
4096 }
4097
4098 // Return the last scope in (begin..end]
4099 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
4100 PcDesc* p = pc_desc_near(begin+1);
4101 if (p != nullptr && p->real_pc(this) <= end) {
4102 return new ScopeDesc(this, p);
4103 }
4104 return nullptr;
4105 }
4106
4107 const char* nmethod::nmethod_section_label(address pos) const {
4108 const char* label = nullptr;
4109 if (pos == code_begin()) label = "[Instructions begin]";
4110 if (pos == entry_point()) label = "[Entry Point]";
4111 if (pos == inline_entry_point()) label = "[Inline Entry Point]";
4112 if (pos == verified_entry_point()) label = "[Verified Entry Point]";
4113 if (pos == verified_inline_entry_point()) label = "[Verified Inline Entry Point]";
4114 if (pos == verified_inline_ro_entry_point()) label = "[Verified Inline Entry Point (RO)]";
4115 if (pos == consts_begin() && pos != insts_begin()) label = "[Constants]";
4116 // Check stub_code before checking exception_handler or deopt_handler.
4117 if (pos == this->stub_begin()) label = "[Stub Code]";
4118 if (JVMCI_ONLY(_exception_offset >= 0 &&) pos == exception_begin()) label = "[Exception Handler]";
4119 if (JVMCI_ONLY(_deopt_handler_entry_offset != -1 &&) pos == deopt_handler_entry()) label = "[Deopt Handler Entry Point]";
4120 return label;
4121 }
4122
4123 static int maybe_print_entry_label(outputStream* stream, address pos, address entry, const char* label) {
4124 if (pos == entry) {
4125 stream->bol();
4126 stream->print_cr("%s", label);
4127 return 1;
4128 } else {
4129 return 0;
4130 }
4131 }
4132
4133 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
4134 if (print_section_labels) {
4135 int n = 0;
4136 // Multiple entry points may be at the same position. Print them all.
4137 n += maybe_print_entry_label(stream, block_begin, entry_point(), "[Entry Point]");
4138 n += maybe_print_entry_label(stream, block_begin, inline_entry_point(), "[Inline Entry Point]");
4139 n += maybe_print_entry_label(stream, block_begin, verified_entry_point(), "[Verified Entry Point]");
4140 n += maybe_print_entry_label(stream, block_begin, verified_inline_entry_point(), "[Verified Inline Entry Point]");
4141 n += maybe_print_entry_label(stream, block_begin, verified_inline_ro_entry_point(), "[Verified Inline Entry Point (RO)]");
4142 if (n == 0) {
4143 const char* label = nmethod_section_label(block_begin);
4144 if (label != nullptr) {
4145 stream->bol();
4146 stream->print_cr("%s", label);
4147 }
4148 }
4149 }
4150
4151 Method* m = method();
4152 if (m == nullptr || is_osr_method()) {
4153 return;
4154 }
4155
4156 // Print the name of the method (only once)
4157 address low = MIN3(entry_point(),
4158 verified_entry_point(),
4159 inline_entry_point());
4160 // The verified inline entry point and verified inline RO entry point are not always
4161 // used. When they are unused. CodeOffsets::Verified_Inline_Entry(_RO) is -1. Hence,
4162 // the calculated entry point is smaller than the block they are offsetting into.
4163 if (verified_inline_entry_point() >= block_begin) {
4164 low = MIN2(low, verified_inline_entry_point());
4165 }
4166 if (verified_inline_ro_entry_point() >= block_begin) {
4167 low = MIN2(low, verified_inline_ro_entry_point());
4168 }
4169 assert(low != nullptr, "sanity");
4170 if (block_begin == low) {
4171 stream->print(" # ");
4172 m->print_value_on(stream);
4173 stream->cr();
4174 }
4175
4176 // Print the arguments for the 3 types of verified entry points
4177 CompiledEntrySignature ces(m);
4178 ces.compute_calling_conventions(false);
4179 const GrowableArray<SigEntry>* sig_cc;
4180 const VMRegPair* regs;
4181 if (block_begin == verified_entry_point()) {
4182 sig_cc = ces.sig_cc();
4183 regs = ces.regs_cc();
4184 } else if (block_begin == verified_inline_entry_point()) {
4185 sig_cc = ces.sig();
4186 regs = ces.regs();
4187 } else if (block_begin == verified_inline_ro_entry_point()) {
4188 sig_cc = ces.sig_cc_ro();
4189 regs = ces.regs_cc_ro();
4190 } else {
4191 return;
4192 }
4193
4194 bool has_this = !m->is_static();
4195 if (ces.has_inline_recv() && block_begin == verified_entry_point()) {
4196 // <this> argument is scalarized for verified_entry_point()
4197 has_this = false;
4198 }
4199 const char* spname = "sp"; // make arch-specific?
4200 int stack_slot_offset = this->frame_size() * wordSize;
4201 int tab1 = 14, tab2 = 24;
4202 int sig_index = 0;
4203 int arg_index = has_this ? -1 : 0;
4204 bool did_old_sp = false;
4205 for (ExtendedSignature sig = ExtendedSignature(sig_cc, SigEntryFilter()); !sig.at_end(); ++sig) {
4206 bool at_this = (arg_index == -1);
4207 bool at_old_sp = false;
4208 BasicType t = (*sig)._bt;
4209 if (at_this) {
4210 stream->print(" # this: ");
4211 } else {
4212 stream->print(" # parm%d: ", arg_index);
4213 }
4214 stream->move_to(tab1);
4215 VMReg fst = regs[sig_index].first();
4216 VMReg snd = regs[sig_index].second();
4217 if (fst->is_reg()) {
4218 stream->print("%s", fst->name());
4219 if (snd->is_valid()) {
4220 stream->print(":%s", snd->name());
4221 }
4222 } else if (fst->is_stack()) {
4223 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
4224 if (offset == stack_slot_offset) at_old_sp = true;
4225 stream->print("[%s+0x%x]", spname, offset);
4226 } else {
4227 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
4228 }
4229 stream->print(" ");
4230 stream->move_to(tab2);
4231 stream->print("= ");
4232 if (at_this) {
4233 m->method_holder()->print_value_on(stream);
4234 } else {
4235 bool did_name = false;
4236 if (is_reference_type(t) && !(*sig)._vt_oop) {
4237 Symbol* name = (*sig)._name;
4238 name->print_value_on(stream);
4239 did_name = true;
4240 }
4241 if (!did_name)
4242 stream->print("%s", type2name(t));
4243 if ((*sig)._null_marker) {
4244 stream->print(" (null marker)");
4245 }
4246 if ((*sig)._vt_oop) {
4247 stream->print(" (VT OOP)");
4248 }
4249 }
4250 if (at_old_sp) {
4251 stream->print(" (%s of caller)", spname);
4252 did_old_sp = true;
4253 }
4254 stream->cr();
4255 sig_index += type2size[t];
4256 arg_index += 1;
4257 }
4258 if (!did_old_sp) {
4259 stream->print(" # ");
4260 stream->move_to(tab1);
4261 stream->print("[%s+0x%x]", spname, stack_slot_offset);
4262 stream->print(" (%s of caller)", spname);
4263 stream->cr();
4264 }
4265 }
4266
4267 // Returns whether this nmethod has code comments.
4268 bool nmethod::has_code_comment(address begin, address end) {
4269 // scopes?
4270 ScopeDesc* sd = scope_desc_in(begin, end);
4271 if (sd != nullptr) return true;
4272
4273 // relocations?
4274 const char* str = reloc_string_for(begin, end);
4275 if (str != nullptr) return true;
4276
4277 // implicit exceptions?
4278 int cont_offset = ImplicitExceptionTable(this).continuation_offset((uint)(begin - code_begin()));
4279 if (cont_offset != 0) return true;
4280
4281 return false;
4282 }
4283
4367 else
4368 st->print("<UNKNOWN>");
4369 break;
4370 }
4371 case Bytecodes::_getfield:
4372 case Bytecodes::_putfield:
4373 case Bytecodes::_getstatic:
4374 case Bytecodes::_putstatic:
4375 {
4376 Bytecode_field field(methodHandle(thread, sd->method()), sd->bci());
4377 st->print(" ");
4378 if (field.name() != nullptr)
4379 field.name()->print_symbol_on(st);
4380 else
4381 st->print("<UNKNOWN>");
4382 }
4383 default:
4384 break;
4385 }
4386 }
4387 st->print(" {reexecute=%d rethrow=%d return_oop=%d return_scalarized=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop(), sd->return_scalarized());
4388 }
4389
4390 // Print all scopes
4391 for (;sd != nullptr; sd = sd->sender()) {
4392 st->move_to(column, 6, 0);
4393 st->print("; -");
4394 if (sd->should_reexecute()) {
4395 st->print(" (reexecute)");
4396 }
4397 if (sd->method() == nullptr) {
4398 st->print("method is nullptr");
4399 } else {
4400 sd->method()->print_short_name(st);
4401 }
4402 int lineno = sd->method()->line_number_from_bci(sd->bci());
4403 if (lineno != -1) {
4404 st->print("@%d (line %d)", sd->bci(), lineno);
4405 } else {
4406 st->print("@%d", sd->bci());
4407 }
|