< prev index next >

src/hotspot/share/oops/method.cpp

Print this page

  40 #include "interpreter/interpreter.hpp"
  41 #include "interpreter/oopMapCache.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logTag.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "memory/allocation.inline.hpp"
  46 #include "memory/metadataFactory.hpp"
  47 #include "memory/metaspaceClosure.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"
  51 #include "oops/constMethod.hpp"
  52 #include "oops/constantPool.hpp"
  53 #include "oops/klass.inline.hpp"
  54 #include "oops/method.inline.hpp"
  55 #include "oops/methodData.hpp"
  56 #include "oops/objArrayKlass.hpp"
  57 #include "oops/objArrayOop.inline.hpp"
  58 #include "oops/oop.inline.hpp"
  59 #include "oops/symbol.hpp"

  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/methodHandles.hpp"
  62 #include "runtime/arguments.hpp"
  63 #include "runtime/atomic.hpp"
  64 #include "runtime/continuationEntry.hpp"
  65 #include "runtime/frame.inline.hpp"
  66 #include "runtime/handles.inline.hpp"
  67 #include "runtime/init.hpp"
  68 #include "runtime/orderAccess.hpp"
  69 #include "runtime/relocator.hpp"
  70 #include "runtime/safepointVerifiers.hpp"
  71 #include "runtime/sharedRuntime.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "runtime/vm_version.hpp"
  74 #include "services/memTracker.hpp"
  75 #include "utilities/align.hpp"
  76 #include "utilities/quickSort.hpp"
  77 #include "utilities/vmError.hpp"
  78 #include "utilities/xmlstream.hpp"
  79 

 103   set_access_flags(access_flags);
 104   set_intrinsic_id(vmIntrinsics::_none);
 105   set_force_inline(false);
 106   set_hidden(false);
 107   set_dont_inline(false);
 108   set_changes_current_thread(false);
 109   set_has_injected_profile(false);
 110   set_method_data(NULL);
 111   clear_method_counters();
 112   set_vtable_index(Method::garbage_vtable_index);
 113 
 114   // Fix and bury in Method*
 115   set_interpreter_entry(NULL); // sets i2i entry and from_int
 116   set_adapter_entry(NULL);
 117   Method::clear_code(); // from_c/from_i get set to c2i/i2i
 118 
 119   if (access_flags.is_native()) {
 120     clear_native_function();
 121     set_signature_handler(NULL);
 122   }
 123 
 124   NOT_PRODUCT(set_compiled_invocation_count(0);)
 125   // Name is very useful for debugging.
 126   NOT_PRODUCT(_name = name;)
 127 }
 128 
 129 // Release Method*.  The nmethod will be gone when we get here because
 130 // we've walked the code cache.
 131 void Method::deallocate_contents(ClassLoaderData* loader_data) {
 132   MetadataFactory::free_metadata(loader_data, constMethod());
 133   set_constMethod(NULL);
 134   MetadataFactory::free_metadata(loader_data, method_data());
 135   set_method_data(NULL);
 136   MetadataFactory::free_metadata(loader_data, method_counters());
 137   clear_method_counters();
 138   // The nmethod will be gone when we get here.
 139   if (code() != NULL) _code = NULL;
 140 }
 141 
 142 void Method::release_C_heap_structures() {
 143   if (method_data()) {
 144 #if INCLUDE_JVMCI
 145     FailedSpeculation::free_failed_speculations(method_data()->get_failed_speculations_address());
 146 #endif
 147     // Destroy MethodData
 148     method_data()->~MethodData();
 149   }
 150 }
 151 
 152 address Method::get_i2c_entry() {
 153   assert(adapter() != NULL, "must have");
 154   return adapter()->get_i2c_entry();
 155 }
 156 
 157 address Method::get_c2i_entry() {
 158   assert(adapter() != NULL, "must have");
 159   return adapter()->get_c2i_entry();
 160 }
 161 





 162 address Method::get_c2i_unverified_entry() {
 163   assert(adapter() != NULL, "must have");
 164   return adapter()->get_c2i_unverified_entry();
 165 }
 166 





 167 address Method::get_c2i_no_clinit_check_entry() {
 168   assert(VM_Version::supports_fast_class_init_checks(), "");
 169   assert(adapter() != NULL, "must have");
 170   return adapter()->get_c2i_no_clinit_check_entry();
 171 }
 172 
 173 char* Method::name_and_sig_as_C_string() const {
 174   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
 175 }
 176 
 177 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
 178   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
 179 }
 180 
 181 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
 182   const char* klass_name = klass->external_name();
 183   int klass_name_len  = (int)strlen(klass_name);
 184   int method_name_len = method_name->utf8_length();
 185   int len             = klass_name_len + 1 + method_name_len + signature->utf8_length();
 186   char* dest          = NEW_RESOURCE_ARRAY(char, len + 1);

 654 }
 655 
 656 int Method::extra_stack_words() {
 657   // not an inline function, to avoid a header dependency on Interpreter
 658   return extra_stack_entries() * Interpreter::stackElementSize;
 659 }
 660 
 661 // Derive size of parameters, return type, and fingerprint,
 662 // all in one pass, which is run at load time.
 663 // We need the first two, and might as well grab the third.
 664 void Method::compute_from_signature(Symbol* sig) {
 665   // At this point, since we are scanning the signature,
 666   // we might as well compute the whole fingerprint.
 667   Fingerprinter fp(sig, is_static());
 668   set_size_of_parameters(fp.size_of_parameters());
 669   set_num_stack_arg_slots(fp.num_stack_arg_slots());
 670   constMethod()->set_result_type(fp.return_type());
 671   constMethod()->set_fingerprint(fp.fingerprint());
 672 }
 673 













 674 bool Method::is_vanilla_constructor() const {
 675   // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
 676   // which only calls the superclass vanilla constructor and possibly does stores of
 677   // zero constants to local fields:
 678   //
 679   //   aload_0
 680   //   invokespecial
 681   //   indexbyte1
 682   //   indexbyte2
 683   //
 684   // followed by an (optional) sequence of:
 685   //
 686   //   aload_0
 687   //   aconst_null / iconst_0 / fconst_0 / dconst_0
 688   //   putfield
 689   //   indexbyte1
 690   //   indexbyte2
 691   //
 692   // followed by:
 693   //
 694   //   return
 695 
 696   assert(name() == vmSymbols::object_initializer_name(),    "Should only be called for default constructors");
 697   assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
 698   int size = code_size();
 699   // Check if size match
 700   if (size == 0 || size % 5 != 0) return false;
 701   address cb = code_base();
 702   int last = size - 1;
 703   if (cb[0] != Bytecodes::_aload_0 || cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {

 704     // Does not call superclass default constructor
 705     return false;
 706   }
 707   // Check optional sequence
 708   for (int i = 4; i < last; i += 5) {
 709     if (cb[i] != Bytecodes::_aload_0) return false;
 710     if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
 711     if (cb[i+2] != Bytecodes::_putfield) return false;
 712   }
 713   return true;
 714 }
 715 
 716 
 717 bool Method::compute_has_loops_flag() {
 718   BytecodeStream bcs(methodHandle(Thread::current(), this));
 719   Bytecodes::Code bc;
 720 
 721   while ((bc = bcs.next()) >= 0) {
 722     switch (bc) {
 723       case Bytecodes::_ifeq:

 841 
 842 bool Method::is_accessor() const {
 843   return is_getter() || is_setter();
 844 }
 845 
 846 bool Method::is_getter() const {
 847   if (code_size() != 5) return false;
 848   if (size_of_parameters() != 1) return false;
 849   if (java_code_at(0) != Bytecodes::_aload_0)  return false;
 850   if (java_code_at(1) != Bytecodes::_getfield) return false;
 851   switch (java_code_at(4)) {
 852     case Bytecodes::_ireturn:
 853     case Bytecodes::_lreturn:
 854     case Bytecodes::_freturn:
 855     case Bytecodes::_dreturn:
 856     case Bytecodes::_areturn:
 857       break;
 858     default:
 859       return false;
 860   }





 861   return true;
 862 }
 863 
 864 bool Method::is_setter() const {
 865   if (code_size() != 6) return false;
 866   if (java_code_at(0) != Bytecodes::_aload_0) return false;
 867   switch (java_code_at(1)) {
 868     case Bytecodes::_iload_1:
 869     case Bytecodes::_aload_1:
 870     case Bytecodes::_fload_1:
 871       if (size_of_parameters() != 2) return false;
 872       break;
 873     case Bytecodes::_dload_1:
 874     case Bytecodes::_lload_1:
 875       if (size_of_parameters() != 3) return false;
 876       break;
 877     default:
 878       return false;
 879   }
 880   if (java_code_at(2) != Bytecodes::_putfield) return false;
 881   if (java_code_at(5) != Bytecodes::_return)   return false;





 882   return true;
 883 }
 884 
 885 bool Method::is_constant_getter() const {
 886   int last_index = code_size() - 1;
 887   // Check if the first 1-3 bytecodes are a constant push
 888   // and the last bytecode is a return.
 889   return (2 <= code_size() && code_size() <= 4 &&
 890           Bytecodes::is_const(java_code_at(0)) &&
 891           Bytecodes::length_for(java_code_at(0)) == last_index &&
 892           Bytecodes::is_return(java_code_at(last_index)));
 893 }
 894 
 895 bool Method::is_initializer() const {
 896   return is_object_initializer() || is_static_initializer();
 897 }
 898 
 899 bool Method::has_valid_initializer_flags() const {
 900   return (is_static() ||
 901           method_holder()->major_version() < 51);
 902 }
 903 
 904 bool Method::is_static_initializer() const {
 905   // For classfiles version 51 or greater, ensure that the clinit method is
 906   // static.  Non-static methods with the name "<clinit>" are not static
 907   // initializers. (older classfiles exempted for backward compatibility)
 908   return name() == vmSymbols::class_initializer_name() &&
 909          has_valid_initializer_flags();

 910 }
 911 
 912 bool Method::is_object_initializer() const {
 913    return name() == vmSymbols::object_initializer_name();






 914 }
 915 
 916 bool Method::needs_clinit_barrier() const {
 917   return is_static() && !method_holder()->is_initialized();
 918 }
 919 
 920 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
 921   int length = method->checked_exceptions_length();
 922   if (length == 0) {  // common case
 923     return objArrayHandle(THREAD, Universe::the_empty_class_array());
 924   } else {
 925     methodHandle h_this(THREAD, method);
 926     objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
 927     objArrayHandle mirrors (THREAD, m_oop);
 928     for (int i = 0; i < length; i++) {
 929       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 930       Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
 931       if (log_is_enabled(Warning, exceptions) &&
 932           !k->is_subclass_of(vmClasses::Throwable_klass())) {
 933         ResourceMark rm(THREAD);

 951     // Not necessarily sorted and not necessarily one-to-one.
 952     CompressedLineNumberReadStream stream(compressed_linenumber_table());
 953     while (stream.read_pair()) {
 954       if (stream.bci() == bci) {
 955         // perfect match
 956         return stream.line();
 957       } else {
 958         // update best_bci/line
 959         if (stream.bci() < bci && stream.bci() >= best_bci) {
 960           best_bci  = stream.bci();
 961           best_line = stream.line();
 962         }
 963       }
 964     }
 965   }
 966   return best_line;
 967 }
 968 
 969 
 970 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
 971   if( constants()->tag_at(klass_index).is_unresolved_klass() ) {
 972     Thread *thread = Thread::current();
 973     Symbol* klass_name = constants()->klass_name_at(klass_index);
 974     Handle loader(thread, method_holder()->class_loader());
 975     Handle prot  (thread, method_holder()->protection_domain());
 976     return SystemDictionary::find_instance_klass(thread, klass_name, loader, prot) != NULL;
 977   } else {
 978     return true;
 979   }
 980 }
 981 
 982 
 983 bool Method::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
 984   int klass_index = constants()->klass_ref_index_at(refinfo_index);
 985   if (must_be_resolved) {
 986     // Make sure klass is resolved in constantpool.
 987     if (constants()->tag_at(klass_index).is_unresolved_klass()) return false;


 988   }
 989   return is_klass_loaded_by_klass_index(klass_index);
 990 }
 991 
 992 
 993 void Method::set_native_function(address function, bool post_event_flag) {
 994   assert(function != NULL, "use clear_native_function to unregister natives");
 995   assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
 996   address* native_function = native_function_addr();
 997 
 998   // We can see racers trying to place the same native function into place. Once
 999   // is plenty.
1000   address current = *native_function;
1001   if (current == function) return;
1002   if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
1003       function != NULL) {
1004     // native_method_throw_unsatisfied_link_error_entry() should only
1005     // be passed when post_event_flag is false.
1006     assert(function !=
1007       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),

1136 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1137   print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1138   if (comp_level == CompLevel_all) {
1139     set_not_c1_osr_compilable();
1140     set_not_c2_osr_compilable();
1141   } else {
1142     if (is_c1_compile(comp_level))
1143       set_not_c1_osr_compilable();
1144     if (is_c2_compile(comp_level))
1145       set_not_c2_osr_compilable();
1146   }
1147   assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1148 }
1149 
1150 // Revert to using the interpreter and clear out the nmethod
1151 void Method::clear_code() {
1152   // this may be NULL if c2i adapters have not been made yet
1153   // Only should happen at allocate time.
1154   if (adapter() == NULL) {
1155     _from_compiled_entry    = NULL;


1156   } else {
1157     _from_compiled_entry    = adapter()->get_c2i_entry();


1158   }
1159   OrderAccess::storestore();
1160   _from_interpreted_entry = _i2i_entry;
1161   OrderAccess::storestore();
1162   _code = NULL;
1163 }
1164 
1165 void Method::unlink_code(CompiledMethod *compare) {
1166   MutexLocker ml(CompiledMethod_lock->owned_by_self() ? NULL : CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
1167   // We need to check if either the _code or _from_compiled_code_entry_point
1168   // refer to this nmethod because there is a race in setting these two fields
1169   // in Method* as seen in bugid 4947125.
1170   if (code() == compare ||
1171       from_compiled_entry() == compare->verified_entry_point()) {
1172     clear_code();
1173   }
1174 }
1175 
1176 void Method::unlink_code() {
1177   MutexLocker ml(CompiledMethod_lock->owned_by_self() ? NULL : CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
1178   clear_code();
1179 }
1180 
1181 #if INCLUDE_CDS
1182 // Called by class data sharing to remove any entry points (which are not shared)
1183 void Method::unlink_method() {
1184   Arguments::assert_is_dumping_archive();
1185   _code = NULL;
1186   _adapter = NULL;
1187   _i2i_entry = NULL;
1188   _from_compiled_entry = NULL;


1189   _from_interpreted_entry = NULL;
1190 
1191   if (is_native()) {
1192     *native_function_addr() = NULL;
1193     set_signature_handler(NULL);
1194   }
1195   NOT_PRODUCT(set_compiled_invocation_count(0);)
1196 
1197   set_method_data(NULL);
1198   clear_method_counters();
1199 }
1200 #endif
1201 
1202 // Called when the method_holder is getting linked. Setup entrypoints so the method
1203 // is ready to be called from interpreter, compiler, and vtables.
1204 void Method::link_method(const methodHandle& h_method, TRAPS) {
1205   // If the code cache is full, we may reenter this function for the
1206   // leftover methods that weren't linked.
1207   if (adapter() != NULL) {
1208     return;
1209   }
1210   assert( _code == NULL, "nothing compiled yet" );
1211 
1212   // Setup interpreter entrypoint
1213   assert(this == h_method(), "wrong h_method()" );
1214 
1215   assert(adapter() == NULL, "init'd to NULL");
1216   address entry = Interpreter::entry_for_method(h_method);
1217   assert(entry != NULL, "interpreter entry must be non-null");
1218   // Sets both _i2i_entry and _from_interpreted_entry
1219   set_interpreter_entry(entry);
1220 
1221   // Don't overwrite already registered native entries.
1222   if (is_native() && !has_native_function()) {
1223     set_native_function(
1224       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1225       !native_bind_event_is_interesting);
1226   }



1227 
1228   // Setup compiler entrypoint.  This is made eagerly, so we do not need
1229   // special handling of vtables.  An alternative is to make adapters more
1230   // lazily by calling make_adapter() from from_compiled_entry() for the
1231   // normal calls.  For vtable calls life gets more complicated.  When a
1232   // call-site goes mega-morphic we need adapters in all methods which can be
1233   // called from the vtable.  We need adapters on such methods that get loaded
1234   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
1235   // problem we'll make these lazily later.
1236   (void) make_adapters(h_method, CHECK);
1237 
1238   // ONLY USE the h_method now as make_adapter may have blocked
1239 
1240   if (h_method->is_continuation_native_intrinsic()) {
1241     // the entry points to this method will be set in set_code, called when first resolving this method
1242     _from_interpreted_entry = NULL;
1243     _from_compiled_entry = NULL;
1244     _i2i_entry = NULL;
1245   }
1246 }
1247 
1248 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1249   // Adapters for compiled code are made eagerly here.  They are fairly
1250   // small (generally < 100 bytes) and quick to make (and cached and shared)
1251   // so making them eagerly shouldn't be too expensive.
1252   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1253   if (adapter == NULL ) {
1254     if (!is_init_completed()) {
1255       // Don't throw exceptions during VM initialization because java.lang.* classes
1256       // might not have been initialized, causing problems when constructing the
1257       // Java exception object.
1258       vm_exit_during_initialization("Out of space in CodeCache for adapters");
1259     } else {
1260       THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "Out of space in CodeCache for adapters");
1261     }
1262   }
1263 
1264   mh->set_adapter_entry(adapter);
1265   mh->_from_compiled_entry = adapter->get_c2i_entry();


1266   return adapter->get_c2i_entry();
1267 }
1268 
1269 address Method::from_compiled_entry_no_trampoline() const {
1270   CompiledMethod *code = Atomic::load_acquire(&_code);
1271   if (code) {
1272     return code->verified_entry_point();





1273   } else {
1274     return adapter()->get_c2i_entry();





1275   }
1276 }
1277 
1278 // The verified_code_entry() must be called when a invoke is resolved
1279 // on this method.
1280 
1281 // It returns the compiled code entry point, after asserting not null.
1282 // This function is called after potential safepoints so that nmethod
1283 // or adapter that it points to is still live and valid.
1284 // This function must not hit a safepoint!
1285 address Method::verified_code_entry() {
1286   debug_only(NoSafepointVerifier nsv;)
1287   assert(_from_compiled_entry != NULL, "must be set");
1288   return _from_compiled_entry;
1289 }
1290 












1291 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1292 // (could be racing a deopt).
1293 // Not inline to avoid circular ref.
1294 bool Method::check_code() const {
1295   // cached in a register or local.  There's a race on the value of the field.
1296   CompiledMethod *code = Atomic::load_acquire(&_code);
1297   return code == NULL || (code->method() == NULL) || (code->method() == (Method*)this && !code->is_osr_method());
1298 }
1299 
1300 // Install compiled code.  Instantly it can execute.
1301 void Method::set_code(const methodHandle& mh, CompiledMethod *code) {
1302   assert_lock_strong(CompiledMethod_lock);
1303   assert( code, "use clear_code to remove code" );
1304   assert( mh->check_code(), "" );
1305 
1306   guarantee(mh->adapter() != NULL, "Adapter blob must already exist!");
1307 
1308   // These writes must happen in this order, because the interpreter will
1309   // directly jump to from_interpreted_entry which jumps to an i2c adapter
1310   // which jumps to _from_compiled_entry.
1311   mh->_code = code;             // Assign before allowing compiled code to exec
1312 
1313   int comp_level = code->comp_level();
1314   // In theory there could be a race here. In practice it is unlikely
1315   // and not worth worrying about.
1316   if (comp_level > mh->highest_comp_level()) {
1317     mh->set_highest_comp_level(comp_level);
1318   }
1319 
1320   OrderAccess::storestore();
1321   mh->_from_compiled_entry = code->verified_entry_point();


1322   OrderAccess::storestore();
1323 
1324   if (mh->is_continuation_native_intrinsic()) {
1325     assert(mh->_from_interpreted_entry == NULL, "initialized incorrectly"); // see link_method
1326 
1327     if (mh->is_continuation_enter_intrinsic()) {
1328       // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1329       mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1330     } else if (mh->is_continuation_yield_intrinsic()) {
1331       mh->_i2i_entry = mh->get_i2c_entry();
1332     } else {
1333       guarantee(false, "Unknown Continuation native intrinsic");
1334     }
1335     // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1336     Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1337   } else if (!mh->is_method_handle_intrinsic()) {
1338     // Instantly compiled code can execute.
1339     mh->_from_interpreted_entry = mh->get_i2c_entry();
1340   }
1341 }

2315   // This assumes that the vtbl pointer is the first word of a C++ object.
2316   return dereference_vptr(&m) == dereference_vptr(ptr);
2317 }
2318 
2319 // Check that this pointer is valid by checking that the vtbl pointer matches
2320 bool Method::is_valid_method(const Method* m) {
2321   if (m == NULL) {
2322     return false;
2323   } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2324     // Quick sanity check on pointer.
2325     return false;
2326   } else if (m->is_shared()) {
2327     return CppVtables::is_valid_shared_method(m);
2328   } else if (Metaspace::contains_non_shared(m)) {
2329     return has_method_vptr((const void*)m);
2330   } else {
2331     return false;
2332   }
2333 }
2334 

























2335 #ifndef PRODUCT
2336 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2337   out->print("%d", loader_data->jmethod_ids()->count_methods());
2338 }
2339 #endif // PRODUCT
2340 
2341 
2342 // Printing
2343 
2344 #ifndef PRODUCT
2345 
2346 void Method::print_on(outputStream* st) const {
2347   ResourceMark rm;
2348   assert(is_method(), "must be method");
2349   st->print_cr("%s", internal_name());
2350   st->print_cr(" - this oop:          " PTR_FORMAT, p2i(this));
2351   st->print   (" - method holder:     "); method_holder()->print_value_on(st); st->cr();
2352   st->print   (" - constants:         " PTR_FORMAT " ", p2i(constants()));
2353   constants()->print_value_on(st); st->cr();
2354   st->print   (" - access:            0x%x  ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2355   st->print   (" - name:              ");    name()->print_value_on(st); st->cr();
2356   st->print   (" - signature:         ");    signature()->print_value_on(st); st->cr();
2357   st->print_cr(" - max stack:         %d",   max_stack());
2358   st->print_cr(" - max locals:        %d",   max_locals());
2359   st->print_cr(" - size of params:    %d",   size_of_parameters());
2360   st->print_cr(" - method size:       %d",   method_size());
2361   if (intrinsic_id() != vmIntrinsics::_none)
2362     st->print_cr(" - intrinsic id:      %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2363   if (highest_comp_level() != CompLevel_none)
2364     st->print_cr(" - highest level:     %d", highest_comp_level());
2365   st->print_cr(" - vtable index:      %d",   _vtable_index);




2366   st->print_cr(" - i2i entry:         " PTR_FORMAT, p2i(interpreter_entry()));
2367   st->print(   " - adapters:          ");
2368   AdapterHandlerEntry* a = ((Method*)this)->adapter();
2369   if (a == NULL)
2370     st->print_cr(PTR_FORMAT, p2i(a));
2371   else
2372     a->print_adapter_on(st);
2373   st->print_cr(" - compiled entry     " PTR_FORMAT, p2i(from_compiled_entry()));


2374   st->print_cr(" - code size:         %d",   code_size());
2375   if (code_size() != 0) {
2376     st->print_cr(" - code start:        " PTR_FORMAT, p2i(code_base()));
2377     st->print_cr(" - code end (excl):   " PTR_FORMAT, p2i(code_base() + code_size()));
2378   }
2379   if (method_data() != NULL) {
2380     st->print_cr(" - method data:       " PTR_FORMAT, p2i(method_data()));
2381   }
2382   st->print_cr(" - checked ex length: %d",   checked_exceptions_length());
2383   if (checked_exceptions_length() > 0) {
2384     CheckedExceptionElement* table = checked_exceptions_start();
2385     st->print_cr(" - checked ex start:  " PTR_FORMAT, p2i(table));
2386     if (Verbose) {
2387       for (int i = 0; i < checked_exceptions_length(); i++) {
2388         st->print_cr("   - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2389       }
2390     }
2391   }
2392   if (has_linenumber_table()) {
2393     u_char* table = compressed_linenumber_table();

2423     st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2424   }
2425 }
2426 
2427 void Method::print_linkage_flags(outputStream* st) {
2428   access_flags().print_on(st);
2429   if (is_default_method()) {
2430     st->print("default ");
2431   }
2432   if (is_overpass()) {
2433     st->print("overpass ");
2434   }
2435 }
2436 #endif //PRODUCT
2437 
2438 void Method::print_value_on(outputStream* st) const {
2439   assert(is_method(), "must be method");
2440   st->print("%s", internal_name());
2441   print_address_on(st);
2442   st->print(" ");

2443   name()->print_value_on(st);
2444   st->print(" ");
2445   signature()->print_value_on(st);
2446   st->print(" in ");
2447   method_holder()->print_value_on(st);
2448   if (WizardMode) st->print("#%d", _vtable_index);
2449   if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2450   if (WizardMode && code() != NULL) st->print(" ((nmethod*)%p)", code());
2451 }
2452 
2453 // Verification
2454 
2455 void Method::verify_on(outputStream* st) {
2456   guarantee(is_method(), "object must be method");
2457   guarantee(constants()->is_constantPool(), "should be constant pool");
2458   MethodData* md = method_data();
2459   guarantee(md == NULL ||
2460       md->is_methodData(), "should be method data");
2461 }

  40 #include "interpreter/interpreter.hpp"
  41 #include "interpreter/oopMapCache.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logTag.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "memory/allocation.inline.hpp"
  46 #include "memory/metadataFactory.hpp"
  47 #include "memory/metaspaceClosure.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"
  51 #include "oops/constMethod.hpp"
  52 #include "oops/constantPool.hpp"
  53 #include "oops/klass.inline.hpp"
  54 #include "oops/method.inline.hpp"
  55 #include "oops/methodData.hpp"
  56 #include "oops/objArrayKlass.hpp"
  57 #include "oops/objArrayOop.inline.hpp"
  58 #include "oops/oop.inline.hpp"
  59 #include "oops/symbol.hpp"
  60 #include "oops/inlineKlass.inline.hpp"
  61 #include "prims/jvmtiExport.hpp"
  62 #include "prims/methodHandles.hpp"
  63 #include "runtime/arguments.hpp"
  64 #include "runtime/atomic.hpp"
  65 #include "runtime/continuationEntry.hpp"
  66 #include "runtime/frame.inline.hpp"
  67 #include "runtime/handles.inline.hpp"
  68 #include "runtime/init.hpp"
  69 #include "runtime/orderAccess.hpp"
  70 #include "runtime/relocator.hpp"
  71 #include "runtime/safepointVerifiers.hpp"
  72 #include "runtime/sharedRuntime.hpp"
  73 #include "runtime/signature.hpp"
  74 #include "runtime/vm_version.hpp"
  75 #include "services/memTracker.hpp"
  76 #include "utilities/align.hpp"
  77 #include "utilities/quickSort.hpp"
  78 #include "utilities/vmError.hpp"
  79 #include "utilities/xmlstream.hpp"
  80 

 104   set_access_flags(access_flags);
 105   set_intrinsic_id(vmIntrinsics::_none);
 106   set_force_inline(false);
 107   set_hidden(false);
 108   set_dont_inline(false);
 109   set_changes_current_thread(false);
 110   set_has_injected_profile(false);
 111   set_method_data(NULL);
 112   clear_method_counters();
 113   set_vtable_index(Method::garbage_vtable_index);
 114 
 115   // Fix and bury in Method*
 116   set_interpreter_entry(NULL); // sets i2i entry and from_int
 117   set_adapter_entry(NULL);
 118   Method::clear_code(); // from_c/from_i get set to c2i/i2i
 119 
 120   if (access_flags.is_native()) {
 121     clear_native_function();
 122     set_signature_handler(NULL);
 123   }

 124   NOT_PRODUCT(set_compiled_invocation_count(0);)
 125   // Name is very useful for debugging.
 126   NOT_PRODUCT(_name = name;)
 127 }
 128 
 129 // Release Method*.  The nmethod will be gone when we get here because
 130 // we've walked the code cache.
 131 void Method::deallocate_contents(ClassLoaderData* loader_data) {
 132   MetadataFactory::free_metadata(loader_data, constMethod());
 133   set_constMethod(NULL);
 134   MetadataFactory::free_metadata(loader_data, method_data());
 135   set_method_data(NULL);
 136   MetadataFactory::free_metadata(loader_data, method_counters());
 137   clear_method_counters();
 138   // The nmethod will be gone when we get here.
 139   if (code() != NULL) _code = NULL;
 140 }
 141 
 142 void Method::release_C_heap_structures() {
 143   if (method_data()) {
 144 #if INCLUDE_JVMCI
 145     FailedSpeculation::free_failed_speculations(method_data()->get_failed_speculations_address());
 146 #endif
 147     // Destroy MethodData
 148     method_data()->~MethodData();
 149   }
 150 }
 151 
 152 address Method::get_i2c_entry() {
 153   assert(adapter() != NULL, "must have");
 154   return adapter()->get_i2c_entry();
 155 }
 156 
 157 address Method::get_c2i_entry() {
 158   assert(adapter() != NULL, "must have");
 159   return adapter()->get_c2i_entry();
 160 }
 161 
 162 address Method::get_c2i_inline_entry() {
 163   assert(adapter() != NULL, "must have");
 164   return adapter()->get_c2i_inline_entry();
 165 }
 166 
 167 address Method::get_c2i_unverified_entry() {
 168   assert(adapter() != NULL, "must have");
 169   return adapter()->get_c2i_unverified_entry();
 170 }
 171 
 172 address Method::get_c2i_unverified_inline_entry() {
 173   assert(adapter() != NULL, "must have");
 174   return adapter()->get_c2i_unverified_inline_entry();
 175 }
 176 
 177 address Method::get_c2i_no_clinit_check_entry() {
 178   assert(VM_Version::supports_fast_class_init_checks(), "");
 179   assert(adapter() != NULL, "must have");
 180   return adapter()->get_c2i_no_clinit_check_entry();
 181 }
 182 
 183 char* Method::name_and_sig_as_C_string() const {
 184   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
 185 }
 186 
 187 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
 188   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
 189 }
 190 
 191 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
 192   const char* klass_name = klass->external_name();
 193   int klass_name_len  = (int)strlen(klass_name);
 194   int method_name_len = method_name->utf8_length();
 195   int len             = klass_name_len + 1 + method_name_len + signature->utf8_length();
 196   char* dest          = NEW_RESOURCE_ARRAY(char, len + 1);

 664 }
 665 
 666 int Method::extra_stack_words() {
 667   // not an inline function, to avoid a header dependency on Interpreter
 668   return extra_stack_entries() * Interpreter::stackElementSize;
 669 }
 670 
 671 // Derive size of parameters, return type, and fingerprint,
 672 // all in one pass, which is run at load time.
 673 // We need the first two, and might as well grab the third.
 674 void Method::compute_from_signature(Symbol* sig) {
 675   // At this point, since we are scanning the signature,
 676   // we might as well compute the whole fingerprint.
 677   Fingerprinter fp(sig, is_static());
 678   set_size_of_parameters(fp.size_of_parameters());
 679   set_num_stack_arg_slots(fp.num_stack_arg_slots());
 680   constMethod()->set_result_type(fp.return_type());
 681   constMethod()->set_fingerprint(fp.fingerprint());
 682 }
 683 
 684 // InlineKlass the method is declared to return. This must not
 685 // safepoint as it is called with references live on the stack at
 686 // locations the GC is unaware of.
 687 InlineKlass* Method::returns_inline_type(Thread* thread) const {
 688   assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
 689   NoSafepointVerifier nsv;
 690   SignatureStream ss(signature());
 691   while (!ss.at_return_type()) {
 692     ss.next();
 693   }
 694   return ss.as_inline_klass(method_holder());
 695 }
 696 
 697 bool Method::is_vanilla_constructor() const {
 698   // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
 699   // which only calls the superclass vanilla constructor and possibly does stores of
 700   // zero constants to local fields:
 701   //
 702   //   aload_0, _fast_aload_0, or _nofast_aload_0
 703   //   invokespecial
 704   //   indexbyte1
 705   //   indexbyte2
 706   //
 707   // followed by an (optional) sequence of:
 708   //
 709   //   aload_0
 710   //   aconst_null / iconst_0 / fconst_0 / dconst_0
 711   //   putfield
 712   //   indexbyte1
 713   //   indexbyte2
 714   //
 715   // followed by:
 716   //
 717   //   return
 718 
 719   assert(name() == vmSymbols::object_initializer_name(),    "Should only be called for default constructors");
 720   assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
 721   int size = code_size();
 722   // Check if size match
 723   if (size == 0 || size % 5 != 0) return false;
 724   address cb = code_base();
 725   int last = size - 1;
 726   if ((cb[0] != Bytecodes::_aload_0 && cb[0] != Bytecodes::_fast_aload_0 && cb[0] != Bytecodes::_nofast_aload_0) ||
 727        cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {
 728     // Does not call superclass default constructor
 729     return false;
 730   }
 731   // Check optional sequence
 732   for (int i = 4; i < last; i += 5) {
 733     if (cb[i] != Bytecodes::_aload_0) return false;
 734     if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
 735     if (cb[i+2] != Bytecodes::_putfield) return false;
 736   }
 737   return true;
 738 }
 739 
 740 
 741 bool Method::compute_has_loops_flag() {
 742   BytecodeStream bcs(methodHandle(Thread::current(), this));
 743   Bytecodes::Code bc;
 744 
 745   while ((bc = bcs.next()) >= 0) {
 746     switch (bc) {
 747       case Bytecodes::_ifeq:

 865 
 866 bool Method::is_accessor() const {
 867   return is_getter() || is_setter();
 868 }
 869 
 870 bool Method::is_getter() const {
 871   if (code_size() != 5) return false;
 872   if (size_of_parameters() != 1) return false;
 873   if (java_code_at(0) != Bytecodes::_aload_0)  return false;
 874   if (java_code_at(1) != Bytecodes::_getfield) return false;
 875   switch (java_code_at(4)) {
 876     case Bytecodes::_ireturn:
 877     case Bytecodes::_lreturn:
 878     case Bytecodes::_freturn:
 879     case Bytecodes::_dreturn:
 880     case Bytecodes::_areturn:
 881       break;
 882     default:
 883       return false;
 884   }
 885   if (has_scalarized_return()) {
 886     // Don't treat this as (trivial) getter method because the
 887     // inline type should be returned in a scalarized form.
 888     return false;
 889   }
 890   return true;
 891 }
 892 
 893 bool Method::is_setter() const {
 894   if (code_size() != 6) return false;
 895   if (java_code_at(0) != Bytecodes::_aload_0) return false;
 896   switch (java_code_at(1)) {
 897     case Bytecodes::_iload_1:
 898     case Bytecodes::_aload_1:
 899     case Bytecodes::_fload_1:
 900       if (size_of_parameters() != 2) return false;
 901       break;
 902     case Bytecodes::_dload_1:
 903     case Bytecodes::_lload_1:
 904       if (size_of_parameters() != 3) return false;
 905       break;
 906     default:
 907       return false;
 908   }
 909   if (java_code_at(2) != Bytecodes::_putfield) return false;
 910   if (java_code_at(5) != Bytecodes::_return)   return false;
 911   if (has_scalarized_args()) {
 912     // Don't treat this as (trivial) setter method because the
 913     // inline type argument should be passed in a scalarized form.
 914     return false;
 915   }
 916   return true;
 917 }
 918 
 919 bool Method::is_constant_getter() const {
 920   int last_index = code_size() - 1;
 921   // Check if the first 1-3 bytecodes are a constant push
 922   // and the last bytecode is a return.
 923   return (2 <= code_size() && code_size() <= 4 &&
 924           Bytecodes::is_const(java_code_at(0)) &&
 925           Bytecodes::length_for(java_code_at(0)) == last_index &&
 926           Bytecodes::is_return(java_code_at(last_index)) &&
 927           !has_scalarized_args());



 928 }
 929 
 930 bool Method::is_object_constructor_or_class_initializer() const {
 931   return (is_object_constructor() || is_class_initializer());

 932 }
 933 
 934 bool Method::is_class_initializer() const {
 935   // For classfiles version 51 or greater, ensure that the clinit method is
 936   // static.  Non-static methods with the name "<clinit>" are not static
 937   // initializers. (older classfiles exempted for backward compatibility)
 938   return (name() == vmSymbols::class_initializer_name() &&
 939           (is_static() ||
 940            method_holder()->major_version() < 51));
 941 }
 942 
 943 // A method named <init>, if non-static, is a classic object constructor.
 944 bool Method::is_object_constructor() const {
 945    return name() == vmSymbols::object_initializer_name() && !is_static();
 946 }
 947 
 948 // A static method named <init> is a factory for an inline class.
 949 bool Method::is_static_init_factory() const {
 950    return name() == vmSymbols::object_initializer_name() && is_static();
 951 }
 952 
 953 bool Method::needs_clinit_barrier() const {
 954   return is_static() && !method_holder()->is_initialized();
 955 }
 956 
 957 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
 958   int length = method->checked_exceptions_length();
 959   if (length == 0) {  // common case
 960     return objArrayHandle(THREAD, Universe::the_empty_class_array());
 961   } else {
 962     methodHandle h_this(THREAD, method);
 963     objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
 964     objArrayHandle mirrors (THREAD, m_oop);
 965     for (int i = 0; i < length; i++) {
 966       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 967       Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
 968       if (log_is_enabled(Warning, exceptions) &&
 969           !k->is_subclass_of(vmClasses::Throwable_klass())) {
 970         ResourceMark rm(THREAD);

 988     // Not necessarily sorted and not necessarily one-to-one.
 989     CompressedLineNumberReadStream stream(compressed_linenumber_table());
 990     while (stream.read_pair()) {
 991       if (stream.bci() == bci) {
 992         // perfect match
 993         return stream.line();
 994       } else {
 995         // update best_bci/line
 996         if (stream.bci() < bci && stream.bci() >= best_bci) {
 997           best_bci  = stream.bci();
 998           best_line = stream.line();
 999         }
1000       }
1001     }
1002   }
1003   return best_line;
1004 }
1005 
1006 
1007 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
1008   if( constants()->tag_at(klass_index).is_unresolved_klass()) {
1009     Thread *thread = Thread::current();
1010     Symbol* klass_name = constants()->klass_name_at(klass_index);
1011     Handle loader(thread, method_holder()->class_loader());
1012     Handle prot  (thread, method_holder()->protection_domain());
1013     return SystemDictionary::find_instance_klass(thread, klass_name, loader, prot) != NULL;
1014   } else {
1015     return true;
1016   }
1017 }
1018 
1019 
1020 bool Method::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
1021   int klass_index = constants()->klass_ref_index_at(refinfo_index);
1022   if (must_be_resolved) {
1023     // Make sure klass is resolved in constantpool.
1024     if (constants()->tag_at(klass_index).is_unresolved_klass()) {
1025       return false;
1026     }
1027   }
1028   return is_klass_loaded_by_klass_index(klass_index);
1029 }
1030 
1031 
1032 void Method::set_native_function(address function, bool post_event_flag) {
1033   assert(function != NULL, "use clear_native_function to unregister natives");
1034   assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
1035   address* native_function = native_function_addr();
1036 
1037   // We can see racers trying to place the same native function into place. Once
1038   // is plenty.
1039   address current = *native_function;
1040   if (current == function) return;
1041   if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
1042       function != NULL) {
1043     // native_method_throw_unsatisfied_link_error_entry() should only
1044     // be passed when post_event_flag is false.
1045     assert(function !=
1046       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),

1175 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1176   print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1177   if (comp_level == CompLevel_all) {
1178     set_not_c1_osr_compilable();
1179     set_not_c2_osr_compilable();
1180   } else {
1181     if (is_c1_compile(comp_level))
1182       set_not_c1_osr_compilable();
1183     if (is_c2_compile(comp_level))
1184       set_not_c2_osr_compilable();
1185   }
1186   assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1187 }
1188 
1189 // Revert to using the interpreter and clear out the nmethod
1190 void Method::clear_code() {
1191   // this may be NULL if c2i adapters have not been made yet
1192   // Only should happen at allocate time.
1193   if (adapter() == NULL) {
1194     _from_compiled_entry    = NULL;
1195     _from_compiled_inline_entry = NULL;
1196     _from_compiled_inline_ro_entry = NULL;
1197   } else {
1198     _from_compiled_entry    = adapter()->get_c2i_entry();
1199     _from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1200     _from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1201   }
1202   OrderAccess::storestore();
1203   _from_interpreted_entry = _i2i_entry;
1204   OrderAccess::storestore();
1205   _code = NULL;
1206 }
1207 
1208 void Method::unlink_code(CompiledMethod *compare) {
1209   MutexLocker ml(CompiledMethod_lock->owned_by_self() ? NULL : CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
1210   // We need to check if either the _code or _from_compiled_code_entry_point
1211   // refer to this nmethod because there is a race in setting these two fields
1212   // in Method* as seen in bugid 4947125.
1213   if (code() == compare ||
1214       from_compiled_entry() == compare->verified_entry_point()) {
1215     clear_code();
1216   }
1217 }
1218 
1219 void Method::unlink_code() {
1220   MutexLocker ml(CompiledMethod_lock->owned_by_self() ? NULL : CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
1221   clear_code();
1222 }
1223 
1224 #if INCLUDE_CDS
1225 // Called by class data sharing to remove any entry points (which are not shared)
1226 void Method::unlink_method() {
1227   Arguments::assert_is_dumping_archive();
1228   _code = NULL;
1229   _adapter = NULL;
1230   _i2i_entry = NULL;
1231   _from_compiled_entry = NULL;
1232   _from_compiled_inline_entry = NULL;
1233   _from_compiled_inline_ro_entry = NULL;
1234   _from_interpreted_entry = NULL;
1235 
1236   if (is_native()) {
1237     *native_function_addr() = NULL;
1238     set_signature_handler(NULL);
1239   }
1240   NOT_PRODUCT(set_compiled_invocation_count(0);)
1241 
1242   set_method_data(NULL);
1243   clear_method_counters();
1244 }
1245 #endif
1246 
1247 // Called when the method_holder is getting linked. Setup entrypoints so the method
1248 // is ready to be called from interpreter, compiler, and vtables.
1249 void Method::link_method(const methodHandle& h_method, TRAPS) {
1250   // If the code cache is full, we may reenter this function for the
1251   // leftover methods that weren't linked.
1252   if (adapter() != NULL) {
1253     return;
1254   }
1255   assert( _code == NULL, "nothing compiled yet" );
1256 
1257   // Setup interpreter entrypoint
1258   assert(this == h_method(), "wrong h_method()" );
1259 
1260   assert(adapter() == NULL, "init'd to NULL");
1261   address entry = Interpreter::entry_for_method(h_method);
1262   assert(entry != NULL, "interpreter entry must be non-null");
1263   // Sets both _i2i_entry and _from_interpreted_entry
1264   set_interpreter_entry(entry);
1265 
1266   // Don't overwrite already registered native entries.
1267   if (is_native() && !has_native_function()) {
1268     set_native_function(
1269       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1270       !native_bind_event_is_interesting);
1271   }
1272   if (InlineTypeReturnedAsFields && returns_inline_type(THREAD)) {
1273     set_has_scalarized_return(true);
1274   }
1275 
1276   // Setup compiler entrypoint.  This is made eagerly, so we do not need
1277   // special handling of vtables.  An alternative is to make adapters more
1278   // lazily by calling make_adapter() from from_compiled_entry() for the
1279   // normal calls.  For vtable calls life gets more complicated.  When a
1280   // call-site goes mega-morphic we need adapters in all methods which can be
1281   // called from the vtable.  We need adapters on such methods that get loaded
1282   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
1283   // problem we'll make these lazily later.
1284   (void) make_adapters(h_method, CHECK);
1285 
1286   // ONLY USE the h_method now as make_adapter may have blocked
1287 
1288   if (h_method->is_continuation_native_intrinsic()) {
1289     // the entry points to this method will be set in set_code, called when first resolving this method
1290     _from_interpreted_entry = NULL;
1291     _from_compiled_entry = NULL;
1292     _i2i_entry = NULL;
1293   }
1294 }
1295 
1296 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1297   // Adapters for compiled code are made eagerly here.  They are fairly
1298   // small (generally < 100 bytes) and quick to make (and cached and shared)
1299   // so making them eagerly shouldn't be too expensive.
1300   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1301   if (adapter == NULL ) {
1302     if (!is_init_completed()) {
1303       // Don't throw exceptions during VM initialization because java.lang.* classes
1304       // might not have been initialized, causing problems when constructing the
1305       // Java exception object.
1306       vm_exit_during_initialization("Out of space in CodeCache for adapters");
1307     } else {
1308       THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "Out of space in CodeCache for adapters");
1309     }
1310   }
1311 
1312   mh->set_adapter_entry(adapter);
1313   mh->_from_compiled_entry = adapter->get_c2i_entry();
1314   mh->_from_compiled_inline_entry = adapter->get_c2i_inline_entry();
1315   mh->_from_compiled_inline_ro_entry = adapter->get_c2i_inline_ro_entry();
1316   return adapter->get_c2i_entry();
1317 }
1318 
1319 address Method::from_compiled_entry_no_trampoline(bool caller_is_c1) const {
1320   CompiledMethod *code = Atomic::load_acquire(&_code);
1321   if (caller_is_c1) {
1322     // C1 - inline type arguments are passed as objects
1323     if (code) {
1324       return code->verified_inline_entry_point();
1325     } else {
1326       return adapter()->get_c2i_inline_entry();
1327     }
1328   } else {
1329     // C2 - inline type arguments may be passed as fields
1330     if (code) {
1331       return code->verified_entry_point();
1332     } else {
1333       return adapter()->get_c2i_entry();
1334     }
1335   }
1336 }
1337 
1338 // The verified_code_entry() must be called when a invoke is resolved
1339 // on this method.
1340 
1341 // It returns the compiled code entry point, after asserting not null.
1342 // This function is called after potential safepoints so that nmethod
1343 // or adapter that it points to is still live and valid.
1344 // This function must not hit a safepoint!
1345 address Method::verified_code_entry() {
1346   debug_only(NoSafepointVerifier nsv;)
1347   assert(_from_compiled_entry != NULL, "must be set");
1348   return _from_compiled_entry;
1349 }
1350 
1351 address Method::verified_inline_code_entry() {
1352   debug_only(NoSafepointVerifier nsv;)
1353   assert(_from_compiled_inline_entry != NULL, "must be set");
1354   return _from_compiled_inline_entry;
1355 }
1356 
1357 address Method::verified_inline_ro_code_entry() {
1358   debug_only(NoSafepointVerifier nsv;)
1359   assert(_from_compiled_inline_ro_entry != NULL, "must be set");
1360   return _from_compiled_inline_ro_entry;
1361 }
1362 
1363 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1364 // (could be racing a deopt).
1365 // Not inline to avoid circular ref.
1366 bool Method::check_code() const {
1367   // cached in a register or local.  There's a race on the value of the field.
1368   CompiledMethod *code = Atomic::load_acquire(&_code);
1369   return code == NULL || (code->method() == NULL) || (code->method() == (Method*)this && !code->is_osr_method());
1370 }
1371 
1372 // Install compiled code.  Instantly it can execute.
1373 void Method::set_code(const methodHandle& mh, CompiledMethod *code) {
1374   assert_lock_strong(CompiledMethod_lock);
1375   assert( code, "use clear_code to remove code" );
1376   assert( mh->check_code(), "" );
1377 
1378   guarantee(mh->adapter() != NULL, "Adapter blob must already exist!");
1379 
1380   // These writes must happen in this order, because the interpreter will
1381   // directly jump to from_interpreted_entry which jumps to an i2c adapter
1382   // which jumps to _from_compiled_entry.
1383   mh->_code = code;             // Assign before allowing compiled code to exec
1384 
1385   int comp_level = code->comp_level();
1386   // In theory there could be a race here. In practice it is unlikely
1387   // and not worth worrying about.
1388   if (comp_level > mh->highest_comp_level()) {
1389     mh->set_highest_comp_level(comp_level);
1390   }
1391 
1392   OrderAccess::storestore();
1393   mh->_from_compiled_entry = code->verified_entry_point();
1394   mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1395   mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1396   OrderAccess::storestore();
1397 
1398   if (mh->is_continuation_native_intrinsic()) {
1399     assert(mh->_from_interpreted_entry == NULL, "initialized incorrectly"); // see link_method
1400 
1401     if (mh->is_continuation_enter_intrinsic()) {
1402       // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1403       mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1404     } else if (mh->is_continuation_yield_intrinsic()) {
1405       mh->_i2i_entry = mh->get_i2c_entry();
1406     } else {
1407       guarantee(false, "Unknown Continuation native intrinsic");
1408     }
1409     // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1410     Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1411   } else if (!mh->is_method_handle_intrinsic()) {
1412     // Instantly compiled code can execute.
1413     mh->_from_interpreted_entry = mh->get_i2c_entry();
1414   }
1415 }

2389   // This assumes that the vtbl pointer is the first word of a C++ object.
2390   return dereference_vptr(&m) == dereference_vptr(ptr);
2391 }
2392 
2393 // Check that this pointer is valid by checking that the vtbl pointer matches
2394 bool Method::is_valid_method(const Method* m) {
2395   if (m == NULL) {
2396     return false;
2397   } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2398     // Quick sanity check on pointer.
2399     return false;
2400   } else if (m->is_shared()) {
2401     return CppVtables::is_valid_shared_method(m);
2402   } else if (Metaspace::contains_non_shared(m)) {
2403     return has_method_vptr((const void*)m);
2404   } else {
2405     return false;
2406   }
2407 }
2408 
2409 bool Method::is_scalarized_arg(int idx) const {
2410   if (!has_scalarized_args()) {
2411     return false;
2412   }
2413   // Search through signature and check if argument is wrapped in T_PRIMITIVE_OBJECT/T_VOID
2414   int depth = 0;
2415   const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2416   for (int i = 0; i < sig->length(); i++) {
2417     BasicType bt = sig->at(i)._bt;
2418     if (bt == T_PRIMITIVE_OBJECT) {
2419       depth++;
2420     }
2421     if (idx == 0) {
2422       break; // Argument found
2423     }
2424     if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2425       depth--;
2426     }
2427     if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2428       idx--; // Advance to next argument
2429     }
2430   }
2431   return depth != 0;
2432 }
2433 
2434 #ifndef PRODUCT
2435 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2436   out->print("%d", loader_data->jmethod_ids()->count_methods());
2437 }
2438 #endif // PRODUCT
2439 
2440 
2441 // Printing
2442 
2443 #ifndef PRODUCT
2444 
2445 void Method::print_on(outputStream* st) const {
2446   ResourceMark rm;
2447   assert(is_method(), "must be method");
2448   st->print_cr("%s", internal_name());
2449   st->print_cr(" - this oop:          " PTR_FORMAT, p2i(this));
2450   st->print   (" - method holder:     "); method_holder()->print_value_on(st); st->cr();
2451   st->print   (" - constants:         " PTR_FORMAT " ", p2i(constants()));
2452   constants()->print_value_on(st); st->cr();
2453   st->print   (" - access:            0x%x  ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2454   st->print   (" - name:              ");    name()->print_value_on(st); st->cr();
2455   st->print   (" - signature:         ");    signature()->print_value_on(st); st->cr();
2456   st->print_cr(" - max stack:         %d",   max_stack());
2457   st->print_cr(" - max locals:        %d",   max_locals());
2458   st->print_cr(" - size of params:    %d",   size_of_parameters());
2459   st->print_cr(" - method size:       %d",   method_size());
2460   if (intrinsic_id() != vmIntrinsics::_none)
2461     st->print_cr(" - intrinsic id:      %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2462   if (highest_comp_level() != CompLevel_none)
2463     st->print_cr(" - highest level:     %d", highest_comp_level());
2464   st->print_cr(" - vtable index:      %d",   _vtable_index);
2465 #ifdef ASSERT
2466   if (valid_itable_index())
2467     st->print_cr(" - itable index:      %d",   itable_index());
2468 #endif
2469   st->print_cr(" - i2i entry:         " PTR_FORMAT, p2i(interpreter_entry()));
2470   st->print(   " - adapters:          ");
2471   AdapterHandlerEntry* a = ((Method*)this)->adapter();
2472   if (a == NULL)
2473     st->print_cr(PTR_FORMAT, p2i(a));
2474   else
2475     a->print_adapter_on(st);
2476   st->print_cr(" - compiled entry           " PTR_FORMAT, p2i(from_compiled_entry()));
2477   st->print_cr(" - compiled inline entry    " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2478   st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2479   st->print_cr(" - code size:         %d",   code_size());
2480   if (code_size() != 0) {
2481     st->print_cr(" - code start:        " PTR_FORMAT, p2i(code_base()));
2482     st->print_cr(" - code end (excl):   " PTR_FORMAT, p2i(code_base() + code_size()));
2483   }
2484   if (method_data() != NULL) {
2485     st->print_cr(" - method data:       " PTR_FORMAT, p2i(method_data()));
2486   }
2487   st->print_cr(" - checked ex length: %d",   checked_exceptions_length());
2488   if (checked_exceptions_length() > 0) {
2489     CheckedExceptionElement* table = checked_exceptions_start();
2490     st->print_cr(" - checked ex start:  " PTR_FORMAT, p2i(table));
2491     if (Verbose) {
2492       for (int i = 0; i < checked_exceptions_length(); i++) {
2493         st->print_cr("   - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2494       }
2495     }
2496   }
2497   if (has_linenumber_table()) {
2498     u_char* table = compressed_linenumber_table();

2528     st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2529   }
2530 }
2531 
2532 void Method::print_linkage_flags(outputStream* st) {
2533   access_flags().print_on(st);
2534   if (is_default_method()) {
2535     st->print("default ");
2536   }
2537   if (is_overpass()) {
2538     st->print("overpass ");
2539   }
2540 }
2541 #endif //PRODUCT
2542 
2543 void Method::print_value_on(outputStream* st) const {
2544   assert(is_method(), "must be method");
2545   st->print("%s", internal_name());
2546   print_address_on(st);
2547   st->print(" ");
2548   if (WizardMode) access_flags().print_on(st);
2549   name()->print_value_on(st);
2550   st->print(" ");
2551   signature()->print_value_on(st);
2552   st->print(" in ");
2553   method_holder()->print_value_on(st);
2554   if (WizardMode) st->print("#%d", _vtable_index);
2555   if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2556   if (WizardMode && code() != NULL) st->print(" ((nmethod*)%p)", code());
2557 }
2558 
2559 // Verification
2560 
2561 void Method::verify_on(outputStream* st) {
2562   guarantee(is_method(), "object must be method");
2563   guarantee(constants()->is_constantPool(), "should be constant pool");
2564   MethodData* md = method_data();
2565   guarantee(md == NULL ||
2566       md->is_methodData(), "should be method data");
2567 }
< prev index next >