< prev index next >

src/hotspot/share/code/nmethod.cpp

Print this page

 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);

1229   _compiled_ic_data           = nullptr;
1230 
1231   _is_unloading_state         = 0;
1232   _state                      = not_installed;
1233 
1234   _has_flushed_dependencies   = false;
1235   _is_unlinked                = false;
1236   _load_reported              = false; // jvmti state
1237 
1238   _deoptimization_status      = not_marked;
1239 
1240   // SECT_CONSTS is first in code buffer so the offset should be 0.
1241   int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1242   assert(consts_offset == 0, "const_offset: %d", consts_offset);
1243 
1244   _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1245 
1246   CHECKED_CAST(_entry_offset,              uint16_t, (offsets->value(CodeOffsets::Entry)));
1247   CHECKED_CAST(_verified_entry_offset,     uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1248 




1249   _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1250 }
1251 
1252 // Post initialization
1253 void nmethod::post_init() {
1254   clear_unloading_state();
1255 
1256   finalize_relocations();
1257 
1258   // Flush generated code
1259   ICache::invalidate_range(code_begin(), code_size());
1260 
1261   Universe::heap()->register_nmethod(this);
1262 
1263 #ifdef COMPILER2
1264   HotCodeCollector::register_nmethod(this);
1265 #endif // COMPILER2
1266 
1267   DEBUG_ONLY(Universe::heap()->verify_nmethod(this));
1268 

1276   int nmethod_size,
1277   int compile_id,
1278   CodeOffsets* offsets,
1279   CodeBuffer* code_buffer,
1280   int frame_size,
1281   ByteSize basic_lock_owner_sp_offset,
1282   ByteSize basic_lock_sp_offset,
1283   OopMapSet* oop_maps,
1284   int mutable_data_size)
1285   : CodeBlob("native nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1286              offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1287   _deoptimization_generation(0),
1288   _gc_epoch(CodeCache::gc_epoch()),
1289   _method(method),
1290   _native_receiver_sp_offset(basic_lock_owner_sp_offset),
1291   _native_basic_lock_sp_offset(basic_lock_sp_offset)
1292 {
1293   {
1294     DEBUG_ONLY(NoSafepointVerifier nsv;)
1295     assert_locked_or_safepoint(CodeCache_lock);
1296 
1297     init_defaults(code_buffer, offsets);
1298 
1299     _osr_entry_point         = nullptr;
1300     _pc_desc_container       = nullptr;
1301     _entry_bci               = InvocationEntryBci;
1302     _compile_id              = compile_id;
1303     _comp_level              = CompLevel_none;
1304     _compiler_type           = type;
1305     _orig_pc_offset          = 0;
1306     _num_stack_arg_slots     = 0;
1307 
1308     if (offsets->value(CodeOffsets::Exceptions) != -1) {
1309       // Continuation enter intrinsic
1310       _exception_offset      = code_offset() + offsets->value(CodeOffsets::Exceptions);
1311     } else {
1312       _exception_offset      = 0;
1313     }
1314     // Native wrappers do not have deopt handlers. Make the values
1315     // something that will never match a pc like the nmethod vtable entry
1316     _deopt_handler_entry_offset    = 0;

1421     _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
1422     if (_mutable_data == nullptr) {
1423       vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for mutable data");
1424     }
1425     memcpy(mutable_data_begin(), nm.mutable_data_begin(), nm.mutable_data_size());
1426   } else {
1427     _mutable_data               = nullptr;
1428   }
1429 
1430   _deoptimization_generation    = 0;
1431   _gc_epoch                     = CodeCache::gc_epoch();
1432   _method                       = nm._method;
1433   _osr_link                     = nullptr;
1434 
1435   _exception_cache              = nullptr;
1436   _gc_data                      = nullptr;
1437   _oops_do_mark_nmethods        = nullptr;
1438   _oops_do_mark_link            = nullptr;
1439   _compiled_ic_data             = nullptr;
1440 
1441   if (nm._osr_entry_point != nullptr) {
1442     _osr_entry_point            = (nm._osr_entry_point - (address) &nm) + (address) this;

1443   } else {
1444     _osr_entry_point            = nullptr;



1445   }
1446 
1447   _entry_offset                 = nm._entry_offset;
1448   _verified_entry_offset        = nm._verified_entry_offset;




1449   _entry_bci                    = nm._entry_bci;
1450   _immutable_data_size          = nm._immutable_data_size;
1451 
1452   _skipped_instructions_size    = nm._skipped_instructions_size;
1453   _stub_offset                  = nm._stub_offset;
1454   _exception_offset             = nm._exception_offset;
1455   _deopt_handler_entry_offset   = nm._deopt_handler_entry_offset;
1456   _unwind_handler_offset        = nm._unwind_handler_offset;
1457   _num_stack_arg_slots          = nm._num_stack_arg_slots;
1458 #if INCLUDE_JVMCI
1459   _metadata_size                = nm._metadata_size;
1460 #endif
1461   _nul_chk_table_offset         = nm._nul_chk_table_offset;
1462   _handler_table_offset         = nm._handler_table_offset;
1463   _scopes_pcs_offset            = nm._scopes_pcs_offset;
1464   _scopes_data_offset           = nm._scopes_data_offset;
1465 #if INCLUDE_JVMCI
1466   _speculations_offset          = nm._speculations_offset;
1467 #endif
1468   _immutable_data_ref_count_offset = nm._immutable_data_ref_count_offset;

1739         _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions);
1740       } else {
1741         _exception_offset = -1;
1742       }
1743 
1744       _deopt_handler_entry_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
1745     }
1746     if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
1747       // C1 generates UnwindHandler at the end of instructions section.
1748       // Calculate positive offset as distance between the start of stubs section
1749       // (which is also the end of instructions section) and the start of the handler.
1750       int unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
1751       CHECKED_CAST(_unwind_handler_offset, int16_t, (_stub_offset - unwind_handler_offset));
1752     } else {
1753       _unwind_handler_offset = -1;
1754     }
1755 
1756     int metadata_size = align_up(code_buffer->total_metadata_size(), wordSize);
1757     JVMCI_ONLY( _metadata_size = metadata_size; )
1758     int jvmci_data_size = 0 JVMCI_ONLY( + align_up(compiler->is_jvmci() ? jvmci_data->size() : 0, oopSize));










1759     assert(_mutable_data_size == _relocation_size + metadata_size + jvmci_data_size,
1760            "wrong mutable data size: %d != %d + %d + %d",
1761            _mutable_data_size, _relocation_size, metadata_size, jvmci_data_size);
1762     assert(nmethod_size == data_end() - header_begin(), "wrong nmethod size: %d != %d",
1763            nmethod_size, (int)(code_end() - header_begin()));
1764 
1765     _immutable_data_size  = immutable_data_size;
1766     if (immutable_data_size > 0) {
1767       assert(immutable_data != nullptr, "required");
1768       _immutable_data     = immutable_data;
1769     } else {
1770       // We need unique not null address
1771       _immutable_data     = blob_end();
1772     }
1773     CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
1774     CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
1775     _scopes_pcs_offset    = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
1776     _scopes_data_offset   = _scopes_pcs_offset    + adjust_pcs_size(debug_info->pcs_size());
1777 
1778 #if INCLUDE_JVMCI

3215 }
3216 
3217 bool nmethod::check_dependency_on(DepChange& changes) {
3218   // What has happened:
3219   // 1) a new class dependee has been added
3220   // 2) dependee and all its super classes have been marked
3221   bool found_check = false;  // set true if we are upset
3222   for (Dependencies::DepStream deps(this); deps.next(); ) {
3223     // Evaluate only relevant dependencies.
3224     if (deps.spot_check_dependency_at(changes) != nullptr) {
3225       found_check = true;
3226       NOT_DEBUG(break);
3227     }
3228   }
3229   return found_check;
3230 }
3231 
3232 // Called from mark_for_deoptimization, when dependee is invalidated.
3233 bool nmethod::is_dependent_on_method(Method* dependee) {
3234   for (Dependencies::DepStream deps(this); deps.next(); ) {
3235     if (deps.type() != Dependencies::evol_method)
3236       continue;
3237     Method* method = deps.method_argument(0);
3238     if (method == dependee) return true;
3239   }
3240   return false;
3241 }
3242 
3243 void nmethod_init() {
3244   // make sure you didn't forget to adjust the filler fields
3245   assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
3246 }
3247 
3248 // -----------------------------------------------------------------------------
3249 // Verification
3250 
3251 class VerifyOopsClosure: public OopClosure {
3252   nmethod* _nm;
3253   bool     _ok;
3254 public:
3255   VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
3256   bool ok() { return _ok; }
3257   virtual void do_oop(oop* p) {
3258     if (oopDesc::is_oop_or_null(*p)) return;

3440   if (size              () > 0) st->print_cr(" total in heap  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3441                                              p2i(this),
3442                                              p2i(this) + size(),
3443                                              size());
3444   if (consts_size       () > 0) st->print_cr(" constants      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3445                                              p2i(consts_begin()),
3446                                              p2i(consts_end()),
3447                                              consts_size());
3448   if (insts_size        () > 0) st->print_cr(" main code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3449                                              p2i(insts_begin()),
3450                                              p2i(insts_end()),
3451                                              insts_size());
3452   if (stub_size         () > 0) st->print_cr(" stub code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3453                                              p2i(stub_begin()),
3454                                              p2i(stub_end()),
3455                                              stub_size());
3456   if (oops_size         () > 0) st->print_cr(" oops           [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3457                                              p2i(oops_begin()),
3458                                              p2i(oops_end()),
3459                                              oops_size());
3460   if (mutable_data_size() > 0) st->print_cr(" mutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3461                                              p2i(mutable_data_begin()),
3462                                              p2i(mutable_data_end()),
3463                                              mutable_data_size());
3464   if (relocation_size() > 0)   st->print_cr(" relocation     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3465                                              p2i(relocation_begin()),
3466                                              p2i(relocation_end()),
3467                                              relocation_size());
3468   if (metadata_size     () > 0) st->print_cr(" metadata       [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3469                                              p2i(metadata_begin()),
3470                                              p2i(metadata_end()),
3471                                              metadata_size());
3472 #if INCLUDE_JVMCI
3473   if (jvmci_data_size   () > 0) st->print_cr(" JVMCI data     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3474                                              p2i(jvmci_data_begin()),
3475                                              p2i(jvmci_data_end()),
3476                                              jvmci_data_size());
3477 #endif
3478   if (immutable_data_size() > 0) st->print_cr(" immutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3479                                              p2i(immutable_data_begin()),
3480                                              p2i(immutable_data_end()),
3481                                              immutable_data_size());
3482   if (dependencies_size () > 0) st->print_cr(" dependencies   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3483                                              p2i(dependencies_begin()),
3484                                              p2i(dependencies_end()),

4056           return st.as_string();
4057         }
4058     }
4059   }
4060   return have_one ? "other" : nullptr;
4061 }
4062 
4063 // Return the last scope in (begin..end]
4064 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
4065   PcDesc* p = pc_desc_near(begin+1);
4066   if (p != nullptr && p->real_pc(this) <= end) {
4067     return new ScopeDesc(this, p);
4068   }
4069   return nullptr;
4070 }
4071 
4072 const char* nmethod::nmethod_section_label(address pos) const {
4073   const char* label = nullptr;
4074   if (pos == code_begin())                                              label = "[Instructions begin]";
4075   if (pos == entry_point())                                             label = "[Entry Point]";

4076   if (pos == verified_entry_point())                                    label = "[Verified Entry Point]";


4077   if (pos == consts_begin() && pos != insts_begin())                    label = "[Constants]";
4078   // Check stub_code before checking exception_handler or deopt_handler.
4079   if (pos == this->stub_begin())                                        label = "[Stub Code]";
4080   if (JVMCI_ONLY(_exception_offset >= 0 &&) pos == exception_begin())          label = "[Exception Handler]";
4081   if (JVMCI_ONLY(_deopt_handler_entry_offset != -1 &&) pos == deopt_handler_entry()) label = "[Deopt Handler Entry Point]";
4082   return label;
4083 }
4084 










4085 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
4086   if (print_section_labels) {
4087     const char* label = nmethod_section_label(block_begin);
4088     if (label != nullptr) {
4089       stream->bol();
4090       stream->print_cr("%s", label);









4091     }
4092   }
4093 
4094   if (block_begin == entry_point()) {
4095     Method* m = method();
4096     if (m != nullptr) {
4097       stream->print("  # ");
4098       m->print_value_on(stream);
4099       stream->cr();
























































4100     }
4101     if (m != nullptr && !is_osr_method()) {
4102       ResourceMark rm;
4103       int sizeargs = m->size_of_parameters();
4104       BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
4105       VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
4106       {
4107         int sig_index = 0;
4108         if (!m->is_static())
4109           sig_bt[sig_index++] = T_OBJECT; // 'this'
4110         for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
4111           BasicType t = ss.type();
4112           sig_bt[sig_index++] = t;
4113           if (type2size[t] == 2) {
4114             sig_bt[sig_index++] = T_VOID;
4115           } else {
4116             assert(type2size[t] == 1, "size is 1 or 2");
4117           }
4118         }
4119         assert(sig_index == sizeargs, "");
4120       }
4121       const char* spname = "sp"; // make arch-specific?
4122       SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs);
4123       int stack_slot_offset = this->frame_size() * wordSize;
4124       int tab1 = 14, tab2 = 24;
4125       int sig_index = 0;
4126       int arg_index = (m->is_static() ? 0 : -1);
4127       bool did_old_sp = false;
4128       for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
4129         bool at_this = (arg_index == -1);
4130         bool at_old_sp = false;
4131         BasicType t = (at_this ? T_OBJECT : ss.type());
4132         assert(t == sig_bt[sig_index], "sigs in sync");
4133         if (at_this)
4134           stream->print("  # this: ");
4135         else
4136           stream->print("  # parm%d: ", arg_index);
4137         stream->move_to(tab1);
4138         VMReg fst = regs[sig_index].first();
4139         VMReg snd = regs[sig_index].second();
4140         if (fst->is_reg()) {
4141           stream->print("%s", fst->name());
4142           if (snd->is_valid())  {
4143             stream->print(":%s", snd->name());
4144           }
4145         } else if (fst->is_stack()) {
4146           int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
4147           if (offset == stack_slot_offset)  at_old_sp = true;
4148           stream->print("[%s+0x%x]", spname, offset);
4149         } else {
4150           stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
4151         }
4152         stream->print(" ");
4153         stream->move_to(tab2);
4154         stream->print("= ");
4155         if (at_this) {
4156           m->method_holder()->print_value_on(stream);
4157         } else {
4158           bool did_name = false;
4159           if (!at_this && ss.is_reference()) {
4160             Symbol* name = ss.as_symbol();
4161             name->print_value_on(stream);
4162             did_name = true;
4163           }
4164           if (!did_name)
4165             stream->print("%s", type2name(t));
4166         }
4167         if (at_old_sp) {
4168           stream->print("  (%s of caller)", spname);
4169           did_old_sp = true;
4170         }
4171         stream->cr();
4172         sig_index += type2size[t];
4173         arg_index += 1;
4174         if (!at_this)  ss.next();
4175       }
4176       if (!did_old_sp) {
4177         stream->print("  # ");
4178         stream->move_to(tab1);
4179         stream->print("[%s+0x%x]", spname, stack_slot_offset);
4180         stream->print("  (%s of caller)", spname);
4181         stream->cr();
4182       }
4183     }














4184   }
4185 }
4186 
4187 // Returns whether this nmethod has code comments.
4188 bool nmethod::has_code_comment(address begin, address end) {
4189   // scopes?
4190   ScopeDesc* sd  = scope_desc_in(begin, end);
4191   if (sd != nullptr) return true;
4192 
4193   // relocations?
4194   const char* str = reloc_string_for(begin, end);
4195   if (str != nullptr) return true;
4196 
4197   // implicit exceptions?
4198   int cont_offset = ImplicitExceptionTable(this).continuation_offset((uint)(begin - code_begin()));
4199   if (cont_offset != 0) return true;
4200 
4201   return false;
4202 }
4203 

4287             else
4288               st->print("<UNKNOWN>");
4289             break;
4290           }
4291         case Bytecodes::_getfield:
4292         case Bytecodes::_putfield:
4293         case Bytecodes::_getstatic:
4294         case Bytecodes::_putstatic:
4295           {
4296             Bytecode_field field(methodHandle(thread, sd->method()), sd->bci());
4297             st->print(" ");
4298             if (field.name() != nullptr)
4299               field.name()->print_symbol_on(st);
4300             else
4301               st->print("<UNKNOWN>");
4302           }
4303         default:
4304           break;
4305         }
4306       }
4307       st->print(" {reexecute=%d rethrow=%d return_oop=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop());
4308     }
4309 
4310     // Print all scopes
4311     for (;sd != nullptr; sd = sd->sender()) {
4312       st->move_to(column, 6, 0);
4313       st->print("; -");
4314       if (sd->should_reexecute()) {
4315         st->print(" (reexecute)");
4316       }
4317       if (sd->method() == nullptr) {
4318         st->print("method is nullptr");
4319       } else {
4320         sd->method()->print_short_name(st);
4321       }
4322       int lineno = sd->method()->line_number_from_bci(sd->bci());
4323       if (lineno != -1) {
4324         st->print("@%d (line %d)", sd->bci(), lineno);
4325       } else {
4326         st->print("@%d", sd->bci());
4327       }

 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);

1240   _compiled_ic_data           = nullptr;
1241 
1242   _is_unloading_state         = 0;
1243   _state                      = not_installed;
1244 
1245   _has_flushed_dependencies   = false;
1246   _is_unlinked                = false;
1247   _load_reported              = false; // jvmti state
1248 
1249   _deoptimization_status      = not_marked;
1250 
1251   // SECT_CONSTS is first in code buffer so the offset should be 0.
1252   int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1253   assert(consts_offset == 0, "const_offset: %d", consts_offset);
1254 
1255   _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1256 
1257   CHECKED_CAST(_entry_offset,              uint16_t, (offsets->value(CodeOffsets::Entry)));
1258   CHECKED_CAST(_verified_entry_offset,     uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1259 
1260   _inline_entry_offset             = _entry_offset;
1261   _verified_inline_entry_offset    = _verified_entry_offset;
1262   _verified_inline_ro_entry_offset = _verified_entry_offset;
1263 
1264   _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1265 }
1266 
1267 // Post initialization
1268 void nmethod::post_init() {
1269   clear_unloading_state();
1270 
1271   finalize_relocations();
1272 
1273   // Flush generated code
1274   ICache::invalidate_range(code_begin(), code_size());
1275 
1276   Universe::heap()->register_nmethod(this);
1277 
1278 #ifdef COMPILER2
1279   HotCodeCollector::register_nmethod(this);
1280 #endif // COMPILER2
1281 
1282   DEBUG_ONLY(Universe::heap()->verify_nmethod(this));
1283 

1291   int nmethod_size,
1292   int compile_id,
1293   CodeOffsets* offsets,
1294   CodeBuffer* code_buffer,
1295   int frame_size,
1296   ByteSize basic_lock_owner_sp_offset,
1297   ByteSize basic_lock_sp_offset,
1298   OopMapSet* oop_maps,
1299   int mutable_data_size)
1300   : CodeBlob("native nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1301              offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1302   _deoptimization_generation(0),
1303   _gc_epoch(CodeCache::gc_epoch()),
1304   _method(method),
1305   _native_receiver_sp_offset(basic_lock_owner_sp_offset),
1306   _native_basic_lock_sp_offset(basic_lock_sp_offset)
1307 {
1308   {
1309     DEBUG_ONLY(NoSafepointVerifier nsv;)
1310     assert_locked_or_safepoint(CodeCache_lock);
1311     assert(!method->has_scalarized_args(), "scalarized native wrappers not supported yet");
1312     init_defaults(code_buffer, offsets);
1313 
1314     _osr_entry_point         = nullptr;
1315     _pc_desc_container       = nullptr;
1316     _entry_bci               = InvocationEntryBci;
1317     _compile_id              = compile_id;
1318     _comp_level              = CompLevel_none;
1319     _compiler_type           = type;
1320     _orig_pc_offset          = 0;
1321     _num_stack_arg_slots     = 0;
1322 
1323     if (offsets->value(CodeOffsets::Exceptions) != -1) {
1324       // Continuation enter intrinsic
1325       _exception_offset      = code_offset() + offsets->value(CodeOffsets::Exceptions);
1326     } else {
1327       _exception_offset      = 0;
1328     }
1329     // Native wrappers do not have deopt handlers. Make the values
1330     // something that will never match a pc like the nmethod vtable entry
1331     _deopt_handler_entry_offset    = 0;

1436     _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
1437     if (_mutable_data == nullptr) {
1438       vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for mutable data");
1439     }
1440     memcpy(mutable_data_begin(), nm.mutable_data_begin(), nm.mutable_data_size());
1441   } else {
1442     _mutable_data               = nullptr;
1443   }
1444 
1445   _deoptimization_generation    = 0;
1446   _gc_epoch                     = CodeCache::gc_epoch();
1447   _method                       = nm._method;
1448   _osr_link                     = nullptr;
1449 
1450   _exception_cache              = nullptr;
1451   _gc_data                      = nullptr;
1452   _oops_do_mark_nmethods        = nullptr;
1453   _oops_do_mark_link            = nullptr;
1454   _compiled_ic_data             = nullptr;
1455 
1456   // Relocate the OSR entry point from nm to the new nmethod.
1457   if (nm._osr_entry_point == nullptr) {
1458     _osr_entry_point = nullptr;
1459   } else {
1460     address new_addr = nm._osr_entry_point - (address) &nm + (address) this;
1461     assert(new_addr >= code_begin() && new_addr < code_end(),
1462            "relocated address must be within code bounds");
1463     _osr_entry_point = new_addr;
1464   }

1465   _entry_offset                 = nm._entry_offset;
1466   _verified_entry_offset        = nm._verified_entry_offset;
1467   _inline_entry_offset             = nm._inline_entry_offset;
1468   _verified_inline_entry_offset    = nm._verified_inline_entry_offset;
1469   _verified_inline_ro_entry_offset = nm._verified_inline_ro_entry_offset;
1470 
1471   _entry_bci                    = nm._entry_bci;
1472   _immutable_data_size          = nm._immutable_data_size;
1473 
1474   _skipped_instructions_size    = nm._skipped_instructions_size;
1475   _stub_offset                  = nm._stub_offset;
1476   _exception_offset             = nm._exception_offset;
1477   _deopt_handler_entry_offset   = nm._deopt_handler_entry_offset;
1478   _unwind_handler_offset        = nm._unwind_handler_offset;
1479   _num_stack_arg_slots          = nm._num_stack_arg_slots;
1480 #if INCLUDE_JVMCI
1481   _metadata_size                = nm._metadata_size;
1482 #endif
1483   _nul_chk_table_offset         = nm._nul_chk_table_offset;
1484   _handler_table_offset         = nm._handler_table_offset;
1485   _scopes_pcs_offset            = nm._scopes_pcs_offset;
1486   _scopes_data_offset           = nm._scopes_data_offset;
1487 #if INCLUDE_JVMCI
1488   _speculations_offset          = nm._speculations_offset;
1489 #endif
1490   _immutable_data_ref_count_offset = nm._immutable_data_ref_count_offset;

1761         _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions);
1762       } else {
1763         _exception_offset = -1;
1764       }
1765 
1766       _deopt_handler_entry_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
1767     }
1768     if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
1769       // C1 generates UnwindHandler at the end of instructions section.
1770       // Calculate positive offset as distance between the start of stubs section
1771       // (which is also the end of instructions section) and the start of the handler.
1772       int unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
1773       CHECKED_CAST(_unwind_handler_offset, int16_t, (_stub_offset - unwind_handler_offset));
1774     } else {
1775       _unwind_handler_offset = -1;
1776     }
1777 
1778     int metadata_size = align_up(code_buffer->total_metadata_size(), wordSize);
1779     JVMCI_ONLY( _metadata_size = metadata_size; )
1780     int jvmci_data_size = 0 JVMCI_ONLY( + align_up(compiler->is_jvmci() ? jvmci_data->size() : 0, oopSize));
1781     if (offsets->value(CodeOffsets::Inline_Entry) != CodeOffsets::no_such_entry_point) {
1782       CHECKED_CAST(_inline_entry_offset            , uint16_t, offsets->value(CodeOffsets::Inline_Entry));
1783     }
1784     if (offsets->value(CodeOffsets::Verified_Inline_Entry) != CodeOffsets::no_such_entry_point) {
1785       CHECKED_CAST(_verified_inline_entry_offset   , uint16_t, offsets->value(CodeOffsets::Verified_Inline_Entry));
1786     }
1787     if (offsets->value(CodeOffsets::Verified_Inline_Entry_RO) != CodeOffsets::no_such_entry_point) {
1788       CHECKED_CAST(_verified_inline_ro_entry_offset, uint16_t, offsets->value(CodeOffsets::Verified_Inline_Entry_RO));
1789     }
1790 
1791     assert(_mutable_data_size == _relocation_size + metadata_size + jvmci_data_size,
1792            "wrong mutable data size: %d != %d + %d + %d",
1793            _mutable_data_size, _relocation_size, metadata_size, jvmci_data_size);
1794     assert(nmethod_size == data_end() - header_begin(), "wrong nmethod size: %d != %d",
1795            nmethod_size, (int)(code_end() - header_begin()));
1796 
1797     _immutable_data_size  = immutable_data_size;
1798     if (immutable_data_size > 0) {
1799       assert(immutable_data != nullptr, "required");
1800       _immutable_data     = immutable_data;
1801     } else {
1802       // We need unique not null address
1803       _immutable_data     = blob_end();
1804     }
1805     CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
1806     CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
1807     _scopes_pcs_offset    = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
1808     _scopes_data_offset   = _scopes_pcs_offset    + adjust_pcs_size(debug_info->pcs_size());
1809 
1810 #if INCLUDE_JVMCI

3247 }
3248 
3249 bool nmethod::check_dependency_on(DepChange& changes) {
3250   // What has happened:
3251   // 1) a new class dependee has been added
3252   // 2) dependee and all its super classes have been marked
3253   bool found_check = false;  // set true if we are upset
3254   for (Dependencies::DepStream deps(this); deps.next(); ) {
3255     // Evaluate only relevant dependencies.
3256     if (deps.spot_check_dependency_at(changes) != nullptr) {
3257       found_check = true;
3258       NOT_DEBUG(break);
3259     }
3260   }
3261   return found_check;
3262 }
3263 
3264 // Called from mark_for_deoptimization, when dependee is invalidated.
3265 bool nmethod::is_dependent_on_method(Method* dependee) {
3266   for (Dependencies::DepStream deps(this); deps.next(); ) {
3267     if (Dependencies::has_method_dep(deps.type())) {
3268       Method* method = deps.method_argument(0);
3269       if (method == dependee) return true;
3270     }
3271   }
3272   return false;
3273 }
3274 
3275 void nmethod_init() {
3276   // make sure you didn't forget to adjust the filler fields
3277   assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
3278 }
3279 
3280 // -----------------------------------------------------------------------------
3281 // Verification
3282 
3283 class VerifyOopsClosure: public OopClosure {
3284   nmethod* _nm;
3285   bool     _ok;
3286 public:
3287   VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
3288   bool ok() { return _ok; }
3289   virtual void do_oop(oop* p) {
3290     if (oopDesc::is_oop_or_null(*p)) return;

3472   if (size              () > 0) st->print_cr(" total in heap  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3473                                              p2i(this),
3474                                              p2i(this) + size(),
3475                                              size());
3476   if (consts_size       () > 0) st->print_cr(" constants      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3477                                              p2i(consts_begin()),
3478                                              p2i(consts_end()),
3479                                              consts_size());
3480   if (insts_size        () > 0) st->print_cr(" main code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3481                                              p2i(insts_begin()),
3482                                              p2i(insts_end()),
3483                                              insts_size());
3484   if (stub_size         () > 0) st->print_cr(" stub code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3485                                              p2i(stub_begin()),
3486                                              p2i(stub_end()),
3487                                              stub_size());
3488   if (oops_size         () > 0) st->print_cr(" oops           [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3489                                              p2i(oops_begin()),
3490                                              p2i(oops_end()),
3491                                              oops_size());
3492   if (mutable_data_size () > 0) st->print_cr(" mutable data   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3493                                              p2i(mutable_data_begin()),
3494                                              p2i(mutable_data_end()),
3495                                              mutable_data_size());
3496   if (relocation_size   () > 0) st->print_cr(" relocation     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3497                                              p2i(relocation_begin()),
3498                                              p2i(relocation_end()),
3499                                              relocation_size());
3500   if (metadata_size     () > 0) st->print_cr(" metadata       [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3501                                              p2i(metadata_begin()),
3502                                              p2i(metadata_end()),
3503                                              metadata_size());
3504 #if INCLUDE_JVMCI
3505   if (jvmci_data_size   () > 0) st->print_cr(" JVMCI data     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3506                                              p2i(jvmci_data_begin()),
3507                                              p2i(jvmci_data_end()),
3508                                              jvmci_data_size());
3509 #endif
3510   if (immutable_data_size() > 0) st->print_cr(" immutable data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3511                                              p2i(immutable_data_begin()),
3512                                              p2i(immutable_data_end()),
3513                                              immutable_data_size());
3514   if (dependencies_size () > 0) st->print_cr(" dependencies   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3515                                              p2i(dependencies_begin()),
3516                                              p2i(dependencies_end()),

4088           return st.as_string();
4089         }
4090     }
4091   }
4092   return have_one ? "other" : nullptr;
4093 }
4094 
4095 // Return the last scope in (begin..end]
4096 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
4097   PcDesc* p = pc_desc_near(begin+1);
4098   if (p != nullptr && p->real_pc(this) <= end) {
4099     return new ScopeDesc(this, p);
4100   }
4101   return nullptr;
4102 }
4103 
4104 const char* nmethod::nmethod_section_label(address pos) const {
4105   const char* label = nullptr;
4106   if (pos == code_begin())                                              label = "[Instructions begin]";
4107   if (pos == entry_point())                                             label = "[Entry Point]";
4108   if (pos == inline_entry_point())                                      label = "[Inline Entry Point]";
4109   if (pos == verified_entry_point())                                    label = "[Verified Entry Point]";
4110   if (pos == verified_inline_entry_point())                             label = "[Verified Inline Entry Point]";
4111   if (pos == verified_inline_ro_entry_point())                          label = "[Verified Inline Entry Point (RO)]";
4112   if (pos == consts_begin() && pos != insts_begin())                    label = "[Constants]";
4113   // Check stub_code before checking exception_handler or deopt_handler.
4114   if (pos == this->stub_begin())                                        label = "[Stub Code]";
4115   if (JVMCI_ONLY(_exception_offset >= 0 &&) pos == exception_begin())          label = "[Exception Handler]";
4116   if (JVMCI_ONLY(_deopt_handler_entry_offset != -1 &&) pos == deopt_handler_entry()) label = "[Deopt Handler Entry Point]";
4117   return label;
4118 }
4119 
4120 static int maybe_print_entry_label(outputStream* stream, address pos, address entry, const char* label) {
4121   if (pos == entry) {
4122     stream->bol();
4123     stream->print_cr("%s", label);
4124     return 1;
4125   } else {
4126     return 0;
4127   }
4128 }
4129 
4130 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
4131   if (print_section_labels) {
4132     int n = 0;
4133     // Multiple entry points may be at the same position. Print them all.
4134     n += maybe_print_entry_label(stream, block_begin, entry_point(),                    "[Entry Point]");
4135     n += maybe_print_entry_label(stream, block_begin, inline_entry_point(),             "[Inline Entry Point]");
4136     n += maybe_print_entry_label(stream, block_begin, verified_entry_point(),           "[Verified Entry Point]");
4137     n += maybe_print_entry_label(stream, block_begin, verified_inline_entry_point(),    "[Verified Inline Entry Point]");
4138     n += maybe_print_entry_label(stream, block_begin, verified_inline_ro_entry_point(), "[Verified Inline Entry Point (RO)]");
4139     if (n == 0) {
4140       const char* label = nmethod_section_label(block_begin);
4141       if (label != nullptr) {
4142         stream->bol();
4143         stream->print_cr("%s", label);
4144       }
4145     }
4146   }
4147 
4148   Method* m = method();
4149   if (m == nullptr || is_osr_method()) {
4150     return;
4151   }
4152 
4153   // Print the name of the method (only once)
4154   address low = MIN3(entry_point(),
4155                      verified_entry_point(),
4156                      inline_entry_point());
4157   // The verified inline entry point and verified inline RO entry point are not always
4158   // used. When they are unused. CodeOffsets::Verified_Inline_Entry(_RO) is -1. Hence,
4159   // the calculated entry point is smaller than the block they are offsetting into.
4160   if (verified_inline_entry_point() >= block_begin) {
4161     low = MIN2(low, verified_inline_entry_point());
4162   }
4163   if (verified_inline_ro_entry_point() >= block_begin) {
4164     low = MIN2(low, verified_inline_ro_entry_point());
4165   }
4166   assert(low != nullptr, "sanity");
4167   if (block_begin == low) {
4168     stream->print("  # ");
4169     m->print_value_on(stream);
4170     stream->cr();
4171   }
4172 
4173   // Print the arguments for the 3 types of verified entry points
4174   CompiledEntrySignature ces(m);
4175   ces.compute_calling_conventions(false);
4176   const GrowableArray<SigEntry>* sig_cc;
4177   const VMRegPair* regs;
4178   if (block_begin == verified_entry_point()) {
4179     sig_cc = ces.sig_cc();
4180     regs = ces.regs_cc();
4181   } else if (block_begin == verified_inline_entry_point()) {
4182     sig_cc = ces.sig();
4183     regs = ces.regs();
4184   } else if (block_begin == verified_inline_ro_entry_point()) {
4185     sig_cc = ces.sig_cc_ro();
4186     regs = ces.regs_cc_ro();
4187   } else {
4188     return;
4189   }
4190 
4191   bool has_this = !m->is_static();
4192   if (ces.has_inline_recv() && block_begin == verified_entry_point()) {
4193     // <this> argument is scalarized for verified_entry_point()
4194     has_this = false;
4195   }
4196   const char* spname = "sp"; // make arch-specific?
4197   int stack_slot_offset = this->frame_size() * wordSize;
4198   int tab1 = 14, tab2 = 24;
4199   int sig_index = 0;
4200   int arg_index = has_this ? -1 : 0;
4201   bool did_old_sp = false;
4202   for (ExtendedSignature sig = ExtendedSignature(sig_cc, SigEntryFilter()); !sig.at_end(); ++sig) {
4203     bool at_this = (arg_index == -1);
4204     bool at_old_sp = false;
4205     BasicType t = (*sig)._bt;
4206     if (at_this) {
4207       stream->print("  # this: ");
4208     } else {
4209       stream->print("  # parm%d: ", arg_index);
4210     }
4211     stream->move_to(tab1);
4212     VMReg fst = regs[sig_index].first();
4213     VMReg snd = regs[sig_index].second();
4214     if (fst->is_reg()) {
4215       stream->print("%s", fst->name());
4216       if (snd->is_valid())  {
4217         stream->print(":%s", snd->name());












4218       }
4219     } else if (fst->is_stack()) {
4220       int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
4221       if (offset == stack_slot_offset)  at_old_sp = true;
4222       stream->print("[%s+0x%x]", spname, offset);
4223     } else {
4224       stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
4225     }
4226     stream->print(" ");
4227     stream->move_to(tab2);
4228     stream->print("= ");
4229     if (at_this) {
4230       m->method_holder()->print_value_on(stream);
4231     } else {
4232       bool did_name = false;
4233       if (is_reference_type(t) && !(*sig)._vt_oop) {
4234         Symbol* name = (*sig)._name;
4235         name->print_value_on(stream);
4236         did_name = true;
4237       }
4238       if (!did_name)
4239         stream->print("%s", type2name(t));
4240       if ((*sig)._null_marker) {
4241         stream->print(" (null marker)");































4242       }
4243       if ((*sig)._vt_oop) {
4244         stream->print(" (VT OOP)");




4245       }
4246     }
4247     if (at_old_sp) {
4248       stream->print("  (%s of caller)", spname);
4249       did_old_sp = true;
4250     }
4251     stream->cr();
4252     sig_index += type2size[t];
4253     arg_index += 1;
4254   }
4255   if (!did_old_sp) {
4256     stream->print("  # ");
4257     stream->move_to(tab1);
4258     stream->print("[%s+0x%x]", spname, stack_slot_offset);
4259     stream->print("  (%s of caller)", spname);
4260     stream->cr();
4261   }
4262 }
4263 
4264 // Returns whether this nmethod has code comments.
4265 bool nmethod::has_code_comment(address begin, address end) {
4266   // scopes?
4267   ScopeDesc* sd  = scope_desc_in(begin, end);
4268   if (sd != nullptr) return true;
4269 
4270   // relocations?
4271   const char* str = reloc_string_for(begin, end);
4272   if (str != nullptr) return true;
4273 
4274   // implicit exceptions?
4275   int cont_offset = ImplicitExceptionTable(this).continuation_offset((uint)(begin - code_begin()));
4276   if (cont_offset != 0) return true;
4277 
4278   return false;
4279 }
4280 

4364             else
4365               st->print("<UNKNOWN>");
4366             break;
4367           }
4368         case Bytecodes::_getfield:
4369         case Bytecodes::_putfield:
4370         case Bytecodes::_getstatic:
4371         case Bytecodes::_putstatic:
4372           {
4373             Bytecode_field field(methodHandle(thread, sd->method()), sd->bci());
4374             st->print(" ");
4375             if (field.name() != nullptr)
4376               field.name()->print_symbol_on(st);
4377             else
4378               st->print("<UNKNOWN>");
4379           }
4380         default:
4381           break;
4382         }
4383       }
4384       st->print(" {reexecute=%d rethrow=%d return_oop=%d return_scalarized=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop(), sd->return_scalarized());
4385     }
4386 
4387     // Print all scopes
4388     for (;sd != nullptr; sd = sd->sender()) {
4389       st->move_to(column, 6, 0);
4390       st->print("; -");
4391       if (sd->should_reexecute()) {
4392         st->print(" (reexecute)");
4393       }
4394       if (sd->method() == nullptr) {
4395         st->print("method is nullptr");
4396       } else {
4397         sd->method()->print_short_name(st);
4398       }
4399       int lineno = sd->method()->line_number_from_bci(sd->bci());
4400       if (lineno != -1) {
4401         st->print("@%d (line %d)", sd->bci(), lineno);
4402       } else {
4403         st->print("@%d", sd->bci());
4404       }
< prev index next >