697
698 // handle the case of an anchor explicitly set in continuation code that doesn't have a callee
699 JavaThread* thread = reg_map->thread();
700 if ((thread->has_last_Java_frame() && fr.sp() == thread->last_Java_sp())
701 JVMTI_ONLY(|| (method()->is_continuation_enter_intrinsic() && thread->on_monitor_waited_event()))) {
702 return;
703 }
704
705 if (!method()->is_native()) {
706 address pc = fr.pc();
707 bool has_receiver, has_appendix;
708 Symbol* signature;
709
710 // The method attached by JIT-compilers should be used, if present.
711 // Bytecode can be inaccurate in such case.
712 Method* callee = attached_method_before_pc(pc);
713 if (callee != nullptr) {
714 has_receiver = !(callee->access_flags().is_static());
715 has_appendix = false;
716 signature = callee->signature();
717 } else {
718 SimpleScopeDesc ssd(this, pc);
719
720 Bytecode_invoke call(methodHandle(Thread::current(), ssd.method()), ssd.bci());
721 has_receiver = call.has_receiver();
722 has_appendix = call.has_appendix();
723 signature = call.signature();
724 }
725
726 fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f);
727 } else if (method()->is_continuation_enter_intrinsic()) {
728 // This method only calls Continuation.enter()
729 Symbol* signature = vmSymbols::continuationEnter_signature();
730 fr.oops_compiled_arguments_do(signature, false, false, reg_map, f);
731 }
732 }
733
734 Method* nmethod::attached_method(address call_instr) {
735 assert(code_contains(call_instr), "not part of the nmethod");
736 RelocIterator iter(this, call_instr, call_instr + 1);
1228
1229 _has_unsafe_access = 0;
1230 _has_wide_vectors = 0;
1231 _has_monitors = 0;
1232 _has_scoped_access = 0;
1233 _has_flushed_dependencies = 0;
1234 _is_unlinked = 0;
1235 _load_reported = 0; // jvmti state
1236
1237 _deoptimization_status = not_marked;
1238
1239 // SECT_CONSTS is first in code buffer so the offset should be 0.
1240 int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1241 assert(consts_offset == 0, "const_offset: %d", consts_offset);
1242
1243 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1244
1245 CHECKED_CAST(_entry_offset, uint16_t, (offsets->value(CodeOffsets::Entry)));
1246 CHECKED_CAST(_verified_entry_offset, uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1247
1248 _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1249 }
1250
1251 // Post initialization
1252 void nmethod::post_init() {
1253 clear_unloading_state();
1254
1255 finalize_relocations();
1256
1257 // Flush generated code
1258 ICache::invalidate_range(code_begin(), code_size());
1259
1260 Universe::heap()->register_nmethod(this);
1261 DEBUG_ONLY(Universe::heap()->verify_nmethod(this));
1262
1263 CodeCache::commit(this);
1264 }
1265
1266 // For native wrappers
1267 nmethod::nmethod(
1270 int nmethod_size,
1271 int compile_id,
1272 CodeOffsets* offsets,
1273 CodeBuffer* code_buffer,
1274 int frame_size,
1275 ByteSize basic_lock_owner_sp_offset,
1276 ByteSize basic_lock_sp_offset,
1277 OopMapSet* oop_maps,
1278 int mutable_data_size)
1279 : CodeBlob("native nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1280 offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1281 _deoptimization_generation(0),
1282 _gc_epoch(CodeCache::gc_epoch()),
1283 _method(method),
1284 _native_receiver_sp_offset(basic_lock_owner_sp_offset),
1285 _native_basic_lock_sp_offset(basic_lock_sp_offset)
1286 {
1287 {
1288 DEBUG_ONLY(NoSafepointVerifier nsv;)
1289 assert_locked_or_safepoint(CodeCache_lock);
1290
1291 init_defaults(code_buffer, offsets);
1292
1293 _osr_entry_point = nullptr;
1294 _pc_desc_container = nullptr;
1295 _entry_bci = InvocationEntryBci;
1296 _compile_id = compile_id;
1297 _comp_level = CompLevel_none;
1298 _compiler_type = type;
1299 _orig_pc_offset = 0;
1300 _num_stack_arg_slots = 0;
1301
1302 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1303 // Continuation enter intrinsic
1304 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1305 } else {
1306 _exception_offset = 0;
1307 }
1308 // Native wrappers do not have deopt handlers. Make the values
1309 // something that will never match a pc like the nmethod vtable entry
1310 _deopt_handler_entry_offset = 0;
1414 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
1415 if (_mutable_data == nullptr) {
1416 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for mutable data");
1417 }
1418 memcpy(mutable_data_begin(), nm.mutable_data_begin(), nm.mutable_data_size());
1419 } else {
1420 _mutable_data = nullptr;
1421 }
1422
1423 _deoptimization_generation = 0;
1424 _gc_epoch = CodeCache::gc_epoch();
1425 _method = nm._method;
1426 _osr_link = nullptr;
1427
1428 _exception_cache = nullptr;
1429 _gc_data = nullptr;
1430 _oops_do_mark_nmethods = nullptr;
1431 _oops_do_mark_link = nullptr;
1432 _compiled_ic_data = nullptr;
1433
1434 if (nm._osr_entry_point != nullptr) {
1435 _osr_entry_point = (nm._osr_entry_point - (address) &nm) + (address) this;
1436 } else {
1437 _osr_entry_point = nullptr;
1438 }
1439
1440 _entry_offset = nm._entry_offset;
1441 _verified_entry_offset = nm._verified_entry_offset;
1442 _entry_bci = nm._entry_bci;
1443 _immutable_data_size = nm._immutable_data_size;
1444
1445 _skipped_instructions_size = nm._skipped_instructions_size;
1446 _stub_offset = nm._stub_offset;
1447 _exception_offset = nm._exception_offset;
1448 _deopt_handler_entry_offset = nm._deopt_handler_entry_offset;
1449 _unwind_handler_offset = nm._unwind_handler_offset;
1450 _num_stack_arg_slots = nm._num_stack_arg_slots;
1451 #if INCLUDE_JVMCI
1452 _metadata_size = nm._metadata_size;
1453 #endif
1454 _nul_chk_table_offset = nm._nul_chk_table_offset;
1455 _handler_table_offset = nm._handler_table_offset;
1456 _scopes_pcs_offset = nm._scopes_pcs_offset;
1457 _scopes_data_offset = nm._scopes_data_offset;
1458 #if INCLUDE_JVMCI
1459 _speculations_offset = nm._speculations_offset;
1460 #endif
1461 _immutable_data_ref_count_offset = nm._immutable_data_ref_count_offset;
1734 _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions);
1735 } else {
1736 _exception_offset = -1;
1737 }
1738
1739 _deopt_handler_entry_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
1740 }
1741 if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
1742 // C1 generates UnwindHandler at the end of instructions section.
1743 // Calculate positive offset as distance between the start of stubs section
1744 // (which is also the end of instructions section) and the start of the handler.
1745 int unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
1746 CHECKED_CAST(_unwind_handler_offset, int16_t, (_stub_offset - unwind_handler_offset));
1747 } else {
1748 _unwind_handler_offset = -1;
1749 }
1750
1751 int metadata_size = align_up(code_buffer->total_metadata_size(), wordSize);
1752 JVMCI_ONLY( _metadata_size = metadata_size; )
1753 int jvmci_data_size = 0 JVMCI_ONLY( + align_up(compiler->is_jvmci() ? jvmci_data->size() : 0, oopSize));
1754 assert(_mutable_data_size == _relocation_size + metadata_size + jvmci_data_size,
1755 "wrong mutable data size: %d != %d + %d + %d",
1756 _mutable_data_size, _relocation_size, metadata_size, jvmci_data_size);
1757 assert(nmethod_size == data_end() - header_begin(), "wrong nmethod size: %d != %d",
1758 nmethod_size, (int)(code_end() - header_begin()));
1759
1760 _immutable_data_size = immutable_data_size;
1761 if (immutable_data_size > 0) {
1762 assert(immutable_data != nullptr, "required");
1763 _immutable_data = immutable_data;
1764 } else {
1765 // We need unique not null address
1766 _immutable_data = blob_end();
1767 }
1768 CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
1769 CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
1770 _scopes_pcs_offset = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
1771 _scopes_data_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size());
1772
1773 #if INCLUDE_JVMCI
3187 }
3188
3189 bool nmethod::check_dependency_on(DepChange& changes) {
3190 // What has happened:
3191 // 1) a new class dependee has been added
3192 // 2) dependee and all its super classes have been marked
3193 bool found_check = false; // set true if we are upset
3194 for (Dependencies::DepStream deps(this); deps.next(); ) {
3195 // Evaluate only relevant dependencies.
3196 if (deps.spot_check_dependency_at(changes) != nullptr) {
3197 found_check = true;
3198 NOT_DEBUG(break);
3199 }
3200 }
3201 return found_check;
3202 }
3203
3204 // Called from mark_for_deoptimization, when dependee is invalidated.
3205 bool nmethod::is_dependent_on_method(Method* dependee) {
3206 for (Dependencies::DepStream deps(this); deps.next(); ) {
3207 if (deps.type() != Dependencies::evol_method)
3208 continue;
3209 Method* method = deps.method_argument(0);
3210 if (method == dependee) return true;
3211 }
3212 return false;
3213 }
3214
3215 void nmethod_init() {
3216 // make sure you didn't forget to adjust the filler fields
3217 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
3218 }
3219
3220 // -----------------------------------------------------------------------------
3221 // Verification
3222
3223 class VerifyOopsClosure: public OopClosure {
3224 nmethod* _nm;
3225 bool _ok;
3226 public:
3227 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
3228 bool ok() { return _ok; }
3229 virtual void do_oop(oop* p) {
3230 if (oopDesc::is_oop_or_null(*p)) return;
4028 return st.as_string();
4029 }
4030 }
4031 }
4032 return have_one ? "other" : nullptr;
4033 }
4034
4035 // Return the last scope in (begin..end]
4036 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
4037 PcDesc* p = pc_desc_near(begin+1);
4038 if (p != nullptr && p->real_pc(this) <= end) {
4039 return new ScopeDesc(this, p);
4040 }
4041 return nullptr;
4042 }
4043
4044 const char* nmethod::nmethod_section_label(address pos) const {
4045 const char* label = nullptr;
4046 if (pos == code_begin()) label = "[Instructions begin]";
4047 if (pos == entry_point()) label = "[Entry Point]";
4048 if (pos == verified_entry_point()) label = "[Verified Entry Point]";
4049 if (pos == consts_begin() && pos != insts_begin()) label = "[Constants]";
4050 // Check stub_code before checking exception_handler or deopt_handler.
4051 if (pos == this->stub_begin()) label = "[Stub Code]";
4052 if (JVMCI_ONLY(_exception_offset >= 0 &&) pos == exception_begin()) label = "[Exception Handler]";
4053 if (JVMCI_ONLY(_deopt_handler_entry_offset != -1 &&) pos == deopt_handler_entry()) label = "[Deopt Handler Entry Point]";
4054 return label;
4055 }
4056
4057 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
4058 if (print_section_labels) {
4059 const char* label = nmethod_section_label(block_begin);
4060 if (label != nullptr) {
4061 stream->bol();
4062 stream->print_cr("%s", label);
4063 }
4064 }
4065
4066 if (block_begin == entry_point()) {
4067 Method* m = method();
4068 if (m != nullptr) {
4069 stream->print(" # ");
4070 m->print_value_on(stream);
4071 stream->cr();
4072 }
4073 if (m != nullptr && !is_osr_method()) {
4074 ResourceMark rm;
4075 int sizeargs = m->size_of_parameters();
4076 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
4077 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
4078 {
4079 int sig_index = 0;
4080 if (!m->is_static())
4081 sig_bt[sig_index++] = T_OBJECT; // 'this'
4082 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
4083 BasicType t = ss.type();
4084 sig_bt[sig_index++] = t;
4085 if (type2size[t] == 2) {
4086 sig_bt[sig_index++] = T_VOID;
4087 } else {
4088 assert(type2size[t] == 1, "size is 1 or 2");
4089 }
4090 }
4091 assert(sig_index == sizeargs, "");
4092 }
4093 const char* spname = "sp"; // make arch-specific?
4094 SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs);
4095 int stack_slot_offset = this->frame_size() * wordSize;
4096 int tab1 = 14, tab2 = 24;
4097 int sig_index = 0;
4098 int arg_index = (m->is_static() ? 0 : -1);
4099 bool did_old_sp = false;
4100 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
4101 bool at_this = (arg_index == -1);
4102 bool at_old_sp = false;
4103 BasicType t = (at_this ? T_OBJECT : ss.type());
4104 assert(t == sig_bt[sig_index], "sigs in sync");
4105 if (at_this)
4106 stream->print(" # this: ");
4107 else
4108 stream->print(" # parm%d: ", arg_index);
4109 stream->move_to(tab1);
4110 VMReg fst = regs[sig_index].first();
4111 VMReg snd = regs[sig_index].second();
4112 if (fst->is_reg()) {
4113 stream->print("%s", fst->name());
4114 if (snd->is_valid()) {
4115 stream->print(":%s", snd->name());
4116 }
4117 } else if (fst->is_stack()) {
4118 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
4119 if (offset == stack_slot_offset) at_old_sp = true;
4120 stream->print("[%s+0x%x]", spname, offset);
4121 } else {
4122 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
4123 }
4124 stream->print(" ");
4125 stream->move_to(tab2);
4126 stream->print("= ");
4127 if (at_this) {
4128 m->method_holder()->print_value_on(stream);
4129 } else {
4130 bool did_name = false;
4131 if (!at_this && ss.is_reference()) {
4132 Symbol* name = ss.as_symbol();
4133 name->print_value_on(stream);
4134 did_name = true;
4135 }
4136 if (!did_name)
4137 stream->print("%s", type2name(t));
4138 }
4139 if (at_old_sp) {
4140 stream->print(" (%s of caller)", spname);
4141 did_old_sp = true;
4142 }
4143 stream->cr();
4144 sig_index += type2size[t];
4145 arg_index += 1;
4146 if (!at_this) ss.next();
4147 }
4148 if (!did_old_sp) {
4149 stream->print(" # ");
4150 stream->move_to(tab1);
4151 stream->print("[%s+0x%x]", spname, stack_slot_offset);
4152 stream->print(" (%s of caller)", spname);
4153 stream->cr();
4154 }
4155 }
4156 }
4157 }
4158
4159 // Returns whether this nmethod has code comments.
4160 bool nmethod::has_code_comment(address begin, address end) {
4161 // scopes?
4162 ScopeDesc* sd = scope_desc_in(begin, end);
4163 if (sd != nullptr) return true;
4164
4165 // relocations?
4166 const char* str = reloc_string_for(begin, end);
4167 if (str != nullptr) return true;
4168
4169 // implicit exceptions?
4170 int cont_offset = ImplicitExceptionTable(this).continuation_offset((uint)(begin - code_begin()));
4171 if (cont_offset != 0) return true;
4172
4173 return false;
4174 }
4175
4259 else
4260 st->print("<UNKNOWN>");
4261 break;
4262 }
4263 case Bytecodes::_getfield:
4264 case Bytecodes::_putfield:
4265 case Bytecodes::_getstatic:
4266 case Bytecodes::_putstatic:
4267 {
4268 Bytecode_field field(methodHandle(thread, sd->method()), sd->bci());
4269 st->print(" ");
4270 if (field.name() != nullptr)
4271 field.name()->print_symbol_on(st);
4272 else
4273 st->print("<UNKNOWN>");
4274 }
4275 default:
4276 break;
4277 }
4278 }
4279 st->print(" {reexecute=%d rethrow=%d return_oop=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop());
4280 }
4281
4282 // Print all scopes
4283 for (;sd != nullptr; sd = sd->sender()) {
4284 st->move_to(column, 6, 0);
4285 st->print("; -");
4286 if (sd->should_reexecute()) {
4287 st->print(" (reexecute)");
4288 }
4289 if (sd->method() == nullptr) {
4290 st->print("method is nullptr");
4291 } else {
4292 sd->method()->print_short_name(st);
4293 }
4294 int lineno = sd->method()->line_number_from_bci(sd->bci());
4295 if (lineno != -1) {
4296 st->print("@%d (line %d)", sd->bci(), lineno);
4297 } else {
4298 st->print("@%d", sd->bci());
4299 }
|
697
698 // handle the case of an anchor explicitly set in continuation code that doesn't have a callee
699 JavaThread* thread = reg_map->thread();
700 if ((thread->has_last_Java_frame() && fr.sp() == thread->last_Java_sp())
701 JVMTI_ONLY(|| (method()->is_continuation_enter_intrinsic() && thread->on_monitor_waited_event()))) {
702 return;
703 }
704
705 if (!method()->is_native()) {
706 address pc = fr.pc();
707 bool has_receiver, has_appendix;
708 Symbol* signature;
709
710 // The method attached by JIT-compilers should be used, if present.
711 // Bytecode can be inaccurate in such case.
712 Method* callee = attached_method_before_pc(pc);
713 if (callee != nullptr) {
714 has_receiver = !(callee->access_flags().is_static());
715 has_appendix = false;
716 signature = callee->signature();
717
718 // If inline types are passed as fields, use the extended signature
719 // which contains the types of all (oop) fields of the inline type.
720 if (is_compiled_by_c2() && callee->has_scalarized_args()) {
721 const GrowableArray<SigEntry>* sig = callee->adapter()->get_sig_cc();
722 assert(sig != nullptr, "sig should never be null");
723 TempNewSymbol tmp_sig = SigEntry::create_symbol(sig);
724 has_receiver = false; // The extended signature contains the receiver type
725 fr.oops_compiled_arguments_do(tmp_sig, has_receiver, has_appendix, reg_map, f);
726 return;
727 }
728 } else {
729 SimpleScopeDesc ssd(this, pc);
730
731 Bytecode_invoke call(methodHandle(Thread::current(), ssd.method()), ssd.bci());
732 has_receiver = call.has_receiver();
733 has_appendix = call.has_appendix();
734 signature = call.signature();
735 }
736
737 fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f);
738 } else if (method()->is_continuation_enter_intrinsic()) {
739 // This method only calls Continuation.enter()
740 Symbol* signature = vmSymbols::continuationEnter_signature();
741 fr.oops_compiled_arguments_do(signature, false, false, reg_map, f);
742 }
743 }
744
745 Method* nmethod::attached_method(address call_instr) {
746 assert(code_contains(call_instr), "not part of the nmethod");
747 RelocIterator iter(this, call_instr, call_instr + 1);
1239
1240 _has_unsafe_access = 0;
1241 _has_wide_vectors = 0;
1242 _has_monitors = 0;
1243 _has_scoped_access = 0;
1244 _has_flushed_dependencies = 0;
1245 _is_unlinked = 0;
1246 _load_reported = 0; // jvmti state
1247
1248 _deoptimization_status = not_marked;
1249
1250 // SECT_CONSTS is first in code buffer so the offset should be 0.
1251 int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1252 assert(consts_offset == 0, "const_offset: %d", consts_offset);
1253
1254 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1255
1256 CHECKED_CAST(_entry_offset, uint16_t, (offsets->value(CodeOffsets::Entry)));
1257 CHECKED_CAST(_verified_entry_offset, uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1258
1259 _inline_entry_offset = _entry_offset;
1260 _verified_inline_entry_offset = _verified_entry_offset;
1261 _verified_inline_ro_entry_offset = _verified_entry_offset;
1262
1263 _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1264 }
1265
1266 // Post initialization
1267 void nmethod::post_init() {
1268 clear_unloading_state();
1269
1270 finalize_relocations();
1271
1272 // Flush generated code
1273 ICache::invalidate_range(code_begin(), code_size());
1274
1275 Universe::heap()->register_nmethod(this);
1276 DEBUG_ONLY(Universe::heap()->verify_nmethod(this));
1277
1278 CodeCache::commit(this);
1279 }
1280
1281 // For native wrappers
1282 nmethod::nmethod(
1285 int nmethod_size,
1286 int compile_id,
1287 CodeOffsets* offsets,
1288 CodeBuffer* code_buffer,
1289 int frame_size,
1290 ByteSize basic_lock_owner_sp_offset,
1291 ByteSize basic_lock_sp_offset,
1292 OopMapSet* oop_maps,
1293 int mutable_data_size)
1294 : CodeBlob("native nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1295 offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1296 _deoptimization_generation(0),
1297 _gc_epoch(CodeCache::gc_epoch()),
1298 _method(method),
1299 _native_receiver_sp_offset(basic_lock_owner_sp_offset),
1300 _native_basic_lock_sp_offset(basic_lock_sp_offset)
1301 {
1302 {
1303 DEBUG_ONLY(NoSafepointVerifier nsv;)
1304 assert_locked_or_safepoint(CodeCache_lock);
1305 assert(!method->has_scalarized_args(), "scalarized native wrappers not supported yet");
1306 init_defaults(code_buffer, offsets);
1307
1308 _osr_entry_point = nullptr;
1309 _pc_desc_container = nullptr;
1310 _entry_bci = InvocationEntryBci;
1311 _compile_id = compile_id;
1312 _comp_level = CompLevel_none;
1313 _compiler_type = type;
1314 _orig_pc_offset = 0;
1315 _num_stack_arg_slots = 0;
1316
1317 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1318 // Continuation enter intrinsic
1319 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1320 } else {
1321 _exception_offset = 0;
1322 }
1323 // Native wrappers do not have deopt handlers. Make the values
1324 // something that will never match a pc like the nmethod vtable entry
1325 _deopt_handler_entry_offset = 0;
1429 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
1430 if (_mutable_data == nullptr) {
1431 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for mutable data");
1432 }
1433 memcpy(mutable_data_begin(), nm.mutable_data_begin(), nm.mutable_data_size());
1434 } else {
1435 _mutable_data = nullptr;
1436 }
1437
1438 _deoptimization_generation = 0;
1439 _gc_epoch = CodeCache::gc_epoch();
1440 _method = nm._method;
1441 _osr_link = nullptr;
1442
1443 _exception_cache = nullptr;
1444 _gc_data = nullptr;
1445 _oops_do_mark_nmethods = nullptr;
1446 _oops_do_mark_link = nullptr;
1447 _compiled_ic_data = nullptr;
1448
1449 // Relocate the OSR entry point from nm to the new nmethod.
1450 if (nm._osr_entry_point == nullptr) {
1451 _osr_entry_point = nullptr;
1452 } else {
1453 address new_addr = nm._osr_entry_point - (address) &nm + (address) this;
1454 assert(new_addr >= code_begin() && new_addr < code_end(),
1455 "relocated address must be within code bounds");
1456 _osr_entry_point = new_addr;
1457 }
1458 _entry_offset = nm._entry_offset;
1459 _verified_entry_offset = nm._verified_entry_offset;
1460 _inline_entry_offset = nm._inline_entry_offset;
1461 _verified_inline_entry_offset = nm._verified_inline_entry_offset;
1462 _verified_inline_ro_entry_offset = nm._verified_inline_ro_entry_offset;
1463
1464 _entry_bci = nm._entry_bci;
1465 _immutable_data_size = nm._immutable_data_size;
1466
1467 _skipped_instructions_size = nm._skipped_instructions_size;
1468 _stub_offset = nm._stub_offset;
1469 _exception_offset = nm._exception_offset;
1470 _deopt_handler_entry_offset = nm._deopt_handler_entry_offset;
1471 _unwind_handler_offset = nm._unwind_handler_offset;
1472 _num_stack_arg_slots = nm._num_stack_arg_slots;
1473 #if INCLUDE_JVMCI
1474 _metadata_size = nm._metadata_size;
1475 #endif
1476 _nul_chk_table_offset = nm._nul_chk_table_offset;
1477 _handler_table_offset = nm._handler_table_offset;
1478 _scopes_pcs_offset = nm._scopes_pcs_offset;
1479 _scopes_data_offset = nm._scopes_data_offset;
1480 #if INCLUDE_JVMCI
1481 _speculations_offset = nm._speculations_offset;
1482 #endif
1483 _immutable_data_ref_count_offset = nm._immutable_data_ref_count_offset;
1756 _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions);
1757 } else {
1758 _exception_offset = -1;
1759 }
1760
1761 _deopt_handler_entry_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
1762 }
1763 if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
1764 // C1 generates UnwindHandler at the end of instructions section.
1765 // Calculate positive offset as distance between the start of stubs section
1766 // (which is also the end of instructions section) and the start of the handler.
1767 int unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
1768 CHECKED_CAST(_unwind_handler_offset, int16_t, (_stub_offset - unwind_handler_offset));
1769 } else {
1770 _unwind_handler_offset = -1;
1771 }
1772
1773 int metadata_size = align_up(code_buffer->total_metadata_size(), wordSize);
1774 JVMCI_ONLY( _metadata_size = metadata_size; )
1775 int jvmci_data_size = 0 JVMCI_ONLY( + align_up(compiler->is_jvmci() ? jvmci_data->size() : 0, oopSize));
1776 if (offsets->value(CodeOffsets::Inline_Entry) != CodeOffsets::no_such_entry_point) {
1777 CHECKED_CAST(_inline_entry_offset , uint16_t, offsets->value(CodeOffsets::Inline_Entry));
1778 }
1779 if (offsets->value(CodeOffsets::Verified_Inline_Entry) != CodeOffsets::no_such_entry_point) {
1780 CHECKED_CAST(_verified_inline_entry_offset , uint16_t, offsets->value(CodeOffsets::Verified_Inline_Entry));
1781 }
1782 if (offsets->value(CodeOffsets::Verified_Inline_Entry_RO) != CodeOffsets::no_such_entry_point) {
1783 CHECKED_CAST(_verified_inline_ro_entry_offset, uint16_t, offsets->value(CodeOffsets::Verified_Inline_Entry_RO));
1784 }
1785
1786 assert(_mutable_data_size == _relocation_size + metadata_size + jvmci_data_size,
1787 "wrong mutable data size: %d != %d + %d + %d",
1788 _mutable_data_size, _relocation_size, metadata_size, jvmci_data_size);
1789 assert(nmethod_size == data_end() - header_begin(), "wrong nmethod size: %d != %d",
1790 nmethod_size, (int)(code_end() - header_begin()));
1791
1792 _immutable_data_size = immutable_data_size;
1793 if (immutable_data_size > 0) {
1794 assert(immutable_data != nullptr, "required");
1795 _immutable_data = immutable_data;
1796 } else {
1797 // We need unique not null address
1798 _immutable_data = blob_end();
1799 }
1800 CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
1801 CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
1802 _scopes_pcs_offset = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
1803 _scopes_data_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size());
1804
1805 #if INCLUDE_JVMCI
3219 }
3220
3221 bool nmethod::check_dependency_on(DepChange& changes) {
3222 // What has happened:
3223 // 1) a new class dependee has been added
3224 // 2) dependee and all its super classes have been marked
3225 bool found_check = false; // set true if we are upset
3226 for (Dependencies::DepStream deps(this); deps.next(); ) {
3227 // Evaluate only relevant dependencies.
3228 if (deps.spot_check_dependency_at(changes) != nullptr) {
3229 found_check = true;
3230 NOT_DEBUG(break);
3231 }
3232 }
3233 return found_check;
3234 }
3235
3236 // Called from mark_for_deoptimization, when dependee is invalidated.
3237 bool nmethod::is_dependent_on_method(Method* dependee) {
3238 for (Dependencies::DepStream deps(this); deps.next(); ) {
3239 if (Dependencies::has_method_dep(deps.type())) {
3240 Method* method = deps.method_argument(0);
3241 if (method == dependee) return true;
3242 }
3243 }
3244 return false;
3245 }
3246
3247 void nmethod_init() {
3248 // make sure you didn't forget to adjust the filler fields
3249 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
3250 }
3251
3252 // -----------------------------------------------------------------------------
3253 // Verification
3254
3255 class VerifyOopsClosure: public OopClosure {
3256 nmethod* _nm;
3257 bool _ok;
3258 public:
3259 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
3260 bool ok() { return _ok; }
3261 virtual void do_oop(oop* p) {
3262 if (oopDesc::is_oop_or_null(*p)) return;
4060 return st.as_string();
4061 }
4062 }
4063 }
4064 return have_one ? "other" : nullptr;
4065 }
4066
4067 // Return the last scope in (begin..end]
4068 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
4069 PcDesc* p = pc_desc_near(begin+1);
4070 if (p != nullptr && p->real_pc(this) <= end) {
4071 return new ScopeDesc(this, p);
4072 }
4073 return nullptr;
4074 }
4075
4076 const char* nmethod::nmethod_section_label(address pos) const {
4077 const char* label = nullptr;
4078 if (pos == code_begin()) label = "[Instructions begin]";
4079 if (pos == entry_point()) label = "[Entry Point]";
4080 if (pos == inline_entry_point()) label = "[Inline Entry Point]";
4081 if (pos == verified_entry_point()) label = "[Verified Entry Point]";
4082 if (pos == verified_inline_entry_point()) label = "[Verified Inline Entry Point]";
4083 if (pos == verified_inline_ro_entry_point()) label = "[Verified Inline Entry Point (RO)]";
4084 if (pos == consts_begin() && pos != insts_begin()) label = "[Constants]";
4085 // Check stub_code before checking exception_handler or deopt_handler.
4086 if (pos == this->stub_begin()) label = "[Stub Code]";
4087 if (JVMCI_ONLY(_exception_offset >= 0 &&) pos == exception_begin()) label = "[Exception Handler]";
4088 if (JVMCI_ONLY(_deopt_handler_entry_offset != -1 &&) pos == deopt_handler_entry()) label = "[Deopt Handler Entry Point]";
4089 return label;
4090 }
4091
4092 static int maybe_print_entry_label(outputStream* stream, address pos, address entry, const char* label) {
4093 if (pos == entry) {
4094 stream->bol();
4095 stream->print_cr("%s", label);
4096 return 1;
4097 } else {
4098 return 0;
4099 }
4100 }
4101
4102 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
4103 if (print_section_labels) {
4104 int n = 0;
4105 // Multiple entry points may be at the same position. Print them all.
4106 n += maybe_print_entry_label(stream, block_begin, entry_point(), "[Entry Point]");
4107 n += maybe_print_entry_label(stream, block_begin, inline_entry_point(), "[Inline Entry Point]");
4108 n += maybe_print_entry_label(stream, block_begin, verified_entry_point(), "[Verified Entry Point]");
4109 n += maybe_print_entry_label(stream, block_begin, verified_inline_entry_point(), "[Verified Inline Entry Point]");
4110 n += maybe_print_entry_label(stream, block_begin, verified_inline_ro_entry_point(), "[Verified Inline Entry Point (RO)]");
4111 if (n == 0) {
4112 const char* label = nmethod_section_label(block_begin);
4113 if (label != nullptr) {
4114 stream->bol();
4115 stream->print_cr("%s", label);
4116 }
4117 }
4118 }
4119
4120 Method* m = method();
4121 if (m == nullptr || is_osr_method()) {
4122 return;
4123 }
4124
4125 // Print the name of the method (only once)
4126 address low = MIN3(entry_point(),
4127 verified_entry_point(),
4128 inline_entry_point());
4129 // The verified inline entry point and verified inline RO entry point are not always
4130 // used. When they are unused. CodeOffsets::Verified_Inline_Entry(_RO) is -1. Hence,
4131 // the calculated entry point is smaller than the block they are offsetting into.
4132 if (verified_inline_entry_point() >= block_begin) {
4133 low = MIN2(low, verified_inline_entry_point());
4134 }
4135 if (verified_inline_ro_entry_point() >= block_begin) {
4136 low = MIN2(low, verified_inline_ro_entry_point());
4137 }
4138 assert(low != nullptr, "sanity");
4139 if (block_begin == low) {
4140 stream->print(" # ");
4141 m->print_value_on(stream);
4142 stream->cr();
4143 }
4144
4145 // Print the arguments for the 3 types of verified entry points
4146 CompiledEntrySignature ces(m);
4147 ces.compute_calling_conventions(false);
4148 const GrowableArray<SigEntry>* sig_cc;
4149 const VMRegPair* regs;
4150 if (block_begin == verified_entry_point()) {
4151 sig_cc = ces.sig_cc();
4152 regs = ces.regs_cc();
4153 } else if (block_begin == verified_inline_entry_point()) {
4154 sig_cc = ces.sig();
4155 regs = ces.regs();
4156 } else if (block_begin == verified_inline_ro_entry_point()) {
4157 sig_cc = ces.sig_cc_ro();
4158 regs = ces.regs_cc_ro();
4159 } else {
4160 return;
4161 }
4162
4163 bool has_this = !m->is_static();
4164 if (ces.has_inline_recv() && block_begin == verified_entry_point()) {
4165 // <this> argument is scalarized for verified_entry_point()
4166 has_this = false;
4167 }
4168 const char* spname = "sp"; // make arch-specific?
4169 int stack_slot_offset = this->frame_size() * wordSize;
4170 int tab1 = 14, tab2 = 24;
4171 int sig_index = 0;
4172 int arg_index = has_this ? -1 : 0;
4173 bool did_old_sp = false;
4174 for (ExtendedSignature sig = ExtendedSignature(sig_cc, SigEntryFilter()); !sig.at_end(); ++sig) {
4175 bool at_this = (arg_index == -1);
4176 bool at_old_sp = false;
4177 BasicType t = (*sig)._bt;
4178 if (at_this) {
4179 stream->print(" # this: ");
4180 } else {
4181 stream->print(" # parm%d: ", arg_index);
4182 }
4183 stream->move_to(tab1);
4184 VMReg fst = regs[sig_index].first();
4185 VMReg snd = regs[sig_index].second();
4186 if (fst->is_reg()) {
4187 stream->print("%s", fst->name());
4188 if (snd->is_valid()) {
4189 stream->print(":%s", snd->name());
4190 }
4191 } else if (fst->is_stack()) {
4192 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
4193 if (offset == stack_slot_offset) at_old_sp = true;
4194 stream->print("[%s+0x%x]", spname, offset);
4195 } else {
4196 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
4197 }
4198 stream->print(" ");
4199 stream->move_to(tab2);
4200 stream->print("= ");
4201 if (at_this) {
4202 m->method_holder()->print_value_on(stream);
4203 } else {
4204 bool did_name = false;
4205 if (is_reference_type(t) && !(*sig)._vt_oop) {
4206 Symbol* name = (*sig)._name;
4207 name->print_value_on(stream);
4208 did_name = true;
4209 }
4210 if (!did_name)
4211 stream->print("%s", type2name(t));
4212 if ((*sig)._null_marker) {
4213 stream->print(" (null marker)");
4214 }
4215 if ((*sig)._vt_oop) {
4216 stream->print(" (VT OOP)");
4217 }
4218 }
4219 if (at_old_sp) {
4220 stream->print(" (%s of caller)", spname);
4221 did_old_sp = true;
4222 }
4223 stream->cr();
4224 sig_index += type2size[t];
4225 arg_index += 1;
4226 }
4227 if (!did_old_sp) {
4228 stream->print(" # ");
4229 stream->move_to(tab1);
4230 stream->print("[%s+0x%x]", spname, stack_slot_offset);
4231 stream->print(" (%s of caller)", spname);
4232 stream->cr();
4233 }
4234 }
4235
4236 // Returns whether this nmethod has code comments.
4237 bool nmethod::has_code_comment(address begin, address end) {
4238 // scopes?
4239 ScopeDesc* sd = scope_desc_in(begin, end);
4240 if (sd != nullptr) return true;
4241
4242 // relocations?
4243 const char* str = reloc_string_for(begin, end);
4244 if (str != nullptr) return true;
4245
4246 // implicit exceptions?
4247 int cont_offset = ImplicitExceptionTable(this).continuation_offset((uint)(begin - code_begin()));
4248 if (cont_offset != 0) return true;
4249
4250 return false;
4251 }
4252
4336 else
4337 st->print("<UNKNOWN>");
4338 break;
4339 }
4340 case Bytecodes::_getfield:
4341 case Bytecodes::_putfield:
4342 case Bytecodes::_getstatic:
4343 case Bytecodes::_putstatic:
4344 {
4345 Bytecode_field field(methodHandle(thread, sd->method()), sd->bci());
4346 st->print(" ");
4347 if (field.name() != nullptr)
4348 field.name()->print_symbol_on(st);
4349 else
4350 st->print("<UNKNOWN>");
4351 }
4352 default:
4353 break;
4354 }
4355 }
4356 st->print(" {reexecute=%d rethrow=%d return_oop=%d return_scalarized=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop(), sd->return_scalarized());
4357 }
4358
4359 // Print all scopes
4360 for (;sd != nullptr; sd = sd->sender()) {
4361 st->move_to(column, 6, 0);
4362 st->print("; -");
4363 if (sd->should_reexecute()) {
4364 st->print(" (reexecute)");
4365 }
4366 if (sd->method() == nullptr) {
4367 st->print("method is nullptr");
4368 } else {
4369 sd->method()->print_short_name(st);
4370 }
4371 int lineno = sd->method()->line_number_from_bci(sd->bci());
4372 if (lineno != -1) {
4373 st->print("@%d (line %d)", sd->bci(), lineno);
4374 } else {
4375 st->print("@%d", sd->bci());
4376 }
|