src/hotspot/share/jvmci/jvmciRuntime.cpp
changeset 54669 ad45b3802d4e
parent 54647 c0d9bc9b4e1f
child 54732 2d012a75d35c
equal deleted inserted replaced
54668:0bda2308eded 54669:ad45b3802d4e
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 #include "precompiled.hpp"
    24 #include "precompiled.hpp"
    25 #include "jvm.h"
       
    26 #include "asm/codeBuffer.hpp"
       
    27 #include "classfile/javaClasses.inline.hpp"
       
    28 #include "classfile/moduleEntry.hpp"
       
    29 #include "code/codeCache.hpp"
       
    30 #include "code/compiledMethod.inline.hpp"
       
    31 #include "compiler/compileBroker.hpp"
    25 #include "compiler/compileBroker.hpp"
    32 #include "compiler/disassembler.hpp"
    26 #include "jvmci/jniAccessMark.inline.hpp"
       
    27 #include "jvmci/jvmciCompilerToVM.hpp"
    33 #include "jvmci/jvmciRuntime.hpp"
    28 #include "jvmci/jvmciRuntime.hpp"
    34 #include "jvmci/jvmciCompilerToVM.hpp"
       
    35 #include "jvmci/jvmciCompiler.hpp"
       
    36 #include "jvmci/jvmciJavaClasses.hpp"
       
    37 #include "jvmci/jvmciEnv.hpp"
       
    38 #include "logging/log.hpp"
    29 #include "logging/log.hpp"
    39 #include "memory/allocation.inline.hpp"
       
    40 #include "memory/oopFactory.hpp"
    30 #include "memory/oopFactory.hpp"
    41 #include "memory/resourceArea.hpp"
    31 #include "oops/constantPool.inline.hpp"
       
    32 #include "oops/method.inline.hpp"
    42 #include "oops/oop.inline.hpp"
    33 #include "oops/oop.inline.hpp"
    43 #include "oops/objArrayOop.inline.hpp"
       
    44 #include "runtime/biasedLocking.hpp"
    34 #include "runtime/biasedLocking.hpp"
       
    35 #include "runtime/deoptimization.hpp"
       
    36 #include "runtime/fieldDescriptor.inline.hpp"
    45 #include "runtime/frame.inline.hpp"
    37 #include "runtime/frame.inline.hpp"
    46 #include "runtime/handles.inline.hpp"
       
    47 #include "runtime/interfaceSupport.inline.hpp"
       
    48 #include "runtime/jniHandles.inline.hpp"
       
    49 #include "runtime/reflection.hpp"
       
    50 #include "runtime/sharedRuntime.hpp"
    38 #include "runtime/sharedRuntime.hpp"
    51 #include "runtime/threadSMR.hpp"
       
    52 #include "utilities/debug.hpp"
       
    53 #include "utilities/defaultStream.hpp"
       
    54 #include "utilities/macros.hpp"
       
    55 #if INCLUDE_G1GC
    39 #if INCLUDE_G1GC
    56 #include "gc/g1/g1ThreadLocalData.hpp"
    40 #include "gc/g1/g1ThreadLocalData.hpp"
    57 #endif // INCLUDE_G1GC
    41 #endif // INCLUDE_G1GC
    58 
       
    59 #if defined(_MSC_VER)
       
    60 #define strtoll _strtoi64
       
    61 #endif
       
    62 
       
    63 jobject JVMCIRuntime::_HotSpotJVMCIRuntime_instance = NULL;
       
    64 bool JVMCIRuntime::_well_known_classes_initialized = false;
       
    65 bool JVMCIRuntime::_shutdown_called = false;
       
    66 
       
    67 BasicType JVMCIRuntime::kindToBasicType(Handle kind, TRAPS) {
       
    68   if (kind.is_null()) {
       
    69     THROW_(vmSymbols::java_lang_NullPointerException(), T_ILLEGAL);
       
    70   }
       
    71   jchar ch = JavaKind::typeChar(kind);
       
    72   switch(ch) {
       
    73     case 'Z': return T_BOOLEAN;
       
    74     case 'B': return T_BYTE;
       
    75     case 'S': return T_SHORT;
       
    76     case 'C': return T_CHAR;
       
    77     case 'I': return T_INT;
       
    78     case 'F': return T_FLOAT;
       
    79     case 'J': return T_LONG;
       
    80     case 'D': return T_DOUBLE;
       
    81     case 'A': return T_OBJECT;
       
    82     case '-': return T_ILLEGAL;
       
    83     default:
       
    84       JVMCI_ERROR_(T_ILLEGAL, "unexpected Kind: %c", ch);
       
    85   }
       
    86 }
       
    87 
    42 
    88 // Simple helper to see if the caller of a runtime stub which
    43 // Simple helper to see if the caller of a runtime stub which
    89 // entered the VM has been deoptimized
    44 // entered the VM has been deoptimized
    90 
    45 
    91 static bool caller_is_deopted() {
    46 static bool caller_is_deopted() {
   149 
   104 
   150 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_instance_common(JavaThread* thread, Klass* klass, bool null_on_fail))
   105 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_instance_common(JavaThread* thread, Klass* klass, bool null_on_fail))
   151   JRT_BLOCK;
   106   JRT_BLOCK;
   152   assert(klass->is_klass(), "not a class");
   107   assert(klass->is_klass(), "not a class");
   153   Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
   108   Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
   154   InstanceKlass* ik = InstanceKlass::cast(klass);
   109   InstanceKlass* h = InstanceKlass::cast(klass);
   155   {
   110   {
   156     RetryableAllocationMark ram(thread, null_on_fail);
   111     RetryableAllocationMark ram(thread, null_on_fail);
   157     ik->check_valid_for_instantiation(true, CHECK);
   112     h->check_valid_for_instantiation(true, CHECK);
   158     oop obj;
   113     oop obj;
   159     if (null_on_fail) {
   114     if (null_on_fail) {
   160       if (!ik->is_initialized()) {
   115       if (!h->is_initialized()) {
   161         // Cannot re-execute class initialization without side effects
   116         // Cannot re-execute class initialization without side effects
   162         // so return without attempting the initialization
   117         // so return without attempting the initialization
   163         return;
   118         return;
   164       }
   119       }
   165     } else {
   120     } else {
   166       // make sure klass is initialized
   121       // make sure klass is initialized
   167       ik->initialize(CHECK);
   122       h->initialize(CHECK);
   168     }
   123     }
   169     // allocate instance and return via TLS
   124     // allocate instance and return via TLS
   170     obj = ik->allocate_instance(CHECK);
   125     obj = h->allocate_instance(CHECK);
   171     thread->set_vm_result(obj);
   126     thread->set_vm_result(obj);
   172   }
   127   }
   173   JRT_BLOCK_END;
   128   JRT_BLOCK_END;
   174   SharedRuntime::on_slowpath_allocation_exit(thread);
   129   SharedRuntime::on_slowpath_allocation_exit(thread);
   175 JRT_END
   130 JRT_END
   287     assert(exception_frame.is_deoptimized_frame(), "must be deopted");
   242     assert(exception_frame.is_deoptimized_frame(), "must be deopted");
   288     pc = exception_frame.pc();
   243     pc = exception_frame.pc();
   289   }
   244   }
   290 #ifdef ASSERT
   245 #ifdef ASSERT
   291   assert(exception.not_null(), "NULL exceptions should be handled by throw_exception");
   246   assert(exception.not_null(), "NULL exceptions should be handled by throw_exception");
       
   247   assert(oopDesc::is_oop(exception()), "just checking");
   292   // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
   248   // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
   293   if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
   249   if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
   294     if (ExitVMOnVerifyError) vm_exit(-1);
   250     if (ExitVMOnVerifyError) vm_exit(-1);
   295     ShouldNotReachHere();
   251     ShouldNotReachHere();
   296   }
   252   }
   367     continuation = SharedRuntime::compute_compiled_exc_handler(cm, pc, exception, false, false, recursive_exception);
   323     continuation = SharedRuntime::compute_compiled_exc_handler(cm, pc, exception, false, false, recursive_exception);
   368     // If an exception was thrown during exception dispatch, the exception oop may have changed
   324     // If an exception was thrown during exception dispatch, the exception oop may have changed
   369     thread->set_exception_oop(exception());
   325     thread->set_exception_oop(exception());
   370     thread->set_exception_pc(pc);
   326     thread->set_exception_pc(pc);
   371 
   327 
   372     // the exception cache is used only by non-implicit exceptions
   328     // The exception cache is used only for non-implicit exceptions
   373     // Update the exception cache only when there didn't happen
   329     // Update the exception cache only when another exception did
   374     // another exception during the computation of the compiled
   330     // occur during the computation of the compiled exception handler
   375     // exception handler. Checking for exception oop equality is not
   331     // (e.g., when loading the class of the catch type).
       
   332     // Checking for exception oop equality is not
   376     // sufficient because some exceptions are pre-allocated and reused.
   333     // sufficient because some exceptions are pre-allocated and reused.
   377     if (continuation != NULL && !recursive_exception && !SharedRuntime::deopt_blob()->contains(continuation)) {
   334     if (continuation != NULL && !recursive_exception && !SharedRuntime::deopt_blob()->contains(continuation)) {
   378       cm->add_handler_for_exception_and_pc(exception, pc, continuation);
   335       cm->add_handler_for_exception_and_pc(exception, pc, continuation);
   379     }
   336     }
   380   }
   337   }
   427     obj->klass()->name()->as_C_string(type, O_BUFLEN);
   384     obj->klass()->name()->as_C_string(type, O_BUFLEN);
   428     markOop mark = obj->mark();
   385     markOop mark = obj->mark();
   429     TRACE_jvmci_3("%s: entered locking slow case with obj=" INTPTR_FORMAT ", type=%s, mark=" INTPTR_FORMAT ", lock=" INTPTR_FORMAT, thread->name(), p2i(obj), type, p2i(mark), p2i(lock));
   386     TRACE_jvmci_3("%s: entered locking slow case with obj=" INTPTR_FORMAT ", type=%s, mark=" INTPTR_FORMAT ", lock=" INTPTR_FORMAT, thread->name(), p2i(obj), type, p2i(mark), p2i(lock));
   430     tty->flush();
   387     tty->flush();
   431   }
   388   }
   432 #ifdef ASSERT
       
   433   if (PrintBiasedLockingStatistics) {
   389   if (PrintBiasedLockingStatistics) {
   434     Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
   390     Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
   435   }
   391   }
   436 #endif
       
   437   Handle h_obj(thread, obj);
   392   Handle h_obj(thread, obj);
       
   393   assert(oopDesc::is_oop(h_obj()), "must be NULL or an object");
   438   if (UseBiasedLocking) {
   394   if (UseBiasedLocking) {
   439     // Retry fast entry if bias is revoked to avoid unnecessary inflation
   395     // Retry fast entry if bias is revoked to avoid unnecessary inflation
   440     ObjectSynchronizer::fast_enter(h_obj, lock, true, CHECK);
   396     ObjectSynchronizer::fast_enter(h_obj, lock, true, CHECK);
   441   } else {
   397   } else {
   442     if (JVMCIUseFastLocking) {
   398     if (JVMCIUseFastLocking) {
   453   assert(thread == JavaThread::current(), "threads must correspond");
   409   assert(thread == JavaThread::current(), "threads must correspond");
   454   assert(thread->last_Java_sp(), "last_Java_sp must be set");
   410   assert(thread->last_Java_sp(), "last_Java_sp must be set");
   455   // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown
   411   // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown
   456   EXCEPTION_MARK;
   412   EXCEPTION_MARK;
   457 
   413 
   458 #ifdef DEBUG
   414 #ifdef ASSERT
   459   if (!oopDesc::is_oop(obj)) {
   415   if (!oopDesc::is_oop(obj)) {
   460     ResetNoHandleMark rhm;
   416     ResetNoHandleMark rhm;
   461     nmethod* method = thread->last_frame().cb()->as_nmethod_or_null();
   417     nmethod* method = thread->last_frame().cb()->as_nmethod_or_null();
   462     if (method != NULL) {
   418     if (method != NULL) {
   463       tty->print_cr("ERROR in monitorexit in method %s wrong obj " INTPTR_FORMAT, method->name(), p2i(obj));
   419       tty->print_cr("ERROR in monitorexit in method %s wrong obj " INTPTR_FORMAT, method->name(), p2i(obj));
   584   if (format != 0L) {
   540   if (format != 0L) {
   585     const char* buf = (char*) (address) format;
   541     const char* buf = (char*) (address) format;
   586     size_t detail_msg_length = strlen(buf) * 2;
   542     size_t detail_msg_length = strlen(buf) * 2;
   587     detail_msg = (char *) NEW_RESOURCE_ARRAY(u_char, detail_msg_length);
   543     detail_msg = (char *) NEW_RESOURCE_ARRAY(u_char, detail_msg_length);
   588     jio_snprintf(detail_msg, detail_msg_length, buf, value);
   544     jio_snprintf(detail_msg, detail_msg_length, buf, value);
   589     report_vm_error(__FILE__, __LINE__, error_msg, "%s", detail_msg);
   545   }
   590   } else {
   546   report_vm_error(__FILE__, __LINE__, error_msg, "%s", detail_msg);
   591     report_vm_error(__FILE__, __LINE__, error_msg);
       
   592   }
       
   593 JRT_END
   547 JRT_END
   594 
   548 
   595 JRT_LEAF(oopDesc*, JVMCIRuntime::load_and_clear_exception(JavaThread* thread))
   549 JRT_LEAF(oopDesc*, JVMCIRuntime::load_and_clear_exception(JavaThread* thread))
   596   oop exception = thread->exception_oop();
   550   oop exception = thread->exception_oop();
   597   assert(exception != NULL, "npe");
   551   assert(exception != NULL, "npe");
   691   } else {
   645   } else {
   692     return (jint) Thread::is_interrupted(receiverThread, clear_interrupted != 0);
   646     return (jint) Thread::is_interrupted(receiverThread, clear_interrupted != 0);
   693   }
   647   }
   694 JRT_END
   648 JRT_END
   695 
   649 
   696 JRT_ENTRY(int, JVMCIRuntime::test_deoptimize_call_int(JavaThread* thread, int value))
   650 JRT_ENTRY(jint, JVMCIRuntime::test_deoptimize_call_int(JavaThread* thread, int value))
   697   deopt_caller();
   651   deopt_caller();
   698   return value;
   652   return (jint) value;
   699 JRT_END
   653 JRT_END
   700 
   654 
   701 void JVMCIRuntime::force_initialization(TRAPS) {
       
   702   JVMCIRuntime::initialize_well_known_classes(CHECK);
       
   703 
       
   704   ResourceMark rm;
       
   705   TempNewSymbol getCompiler = SymbolTable::new_symbol("getCompiler", CHECK);
       
   706   TempNewSymbol sig = SymbolTable::new_symbol("()Ljdk/vm/ci/runtime/JVMCICompiler;", CHECK);
       
   707   Handle jvmciRuntime = JVMCIRuntime::get_HotSpotJVMCIRuntime(CHECK);
       
   708   JavaValue result(T_OBJECT);
       
   709   JavaCalls::call_virtual(&result, jvmciRuntime, HotSpotJVMCIRuntime::klass(), getCompiler, sig, CHECK);
       
   710 }
       
   711 
   655 
   712 // private static JVMCIRuntime JVMCI.initializeRuntime()
   656 // private static JVMCIRuntime JVMCI.initializeRuntime()
   713 JVM_ENTRY(jobject, JVM_GetJVMCIRuntime(JNIEnv *env, jclass c))
   657 JVM_ENTRY_NO_ENV(jobject, JVM_GetJVMCIRuntime(JNIEnv *env, jclass c))
       
   658   JNI_JVMCIENV(env);
   714   if (!EnableJVMCI) {
   659   if (!EnableJVMCI) {
   715     THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVMCI is not enabled")
   660     JVMCI_THROW_MSG_NULL(InternalError, "JVMCI is not enabled");
   716   }
   661   }
   717   JVMCIRuntime::initialize_HotSpotJVMCIRuntime(CHECK_NULL);
   662   JVMCIENV->runtime()->initialize_HotSpotJVMCIRuntime(JVMCI_CHECK_NULL);
   718   jobject ret = JVMCIRuntime::get_HotSpotJVMCIRuntime_jobject(CHECK_NULL);
   663   JVMCIObject runtime = JVMCIENV->runtime()->get_HotSpotJVMCIRuntime(JVMCI_CHECK_NULL);
   719   return ret;
   664   return JVMCIENV->get_jobject(runtime);
   720 JVM_END
   665 JVM_END
   721 
   666 
   722 Handle JVMCIRuntime::callStatic(const char* className, const char* methodName, const char* signature, JavaCallArguments* args, TRAPS) {
   667 void JVMCIRuntime::call_getCompiler(TRAPS) {
   723   TempNewSymbol name = SymbolTable::new_symbol(className, CHECK_(Handle()));
   668   THREAD_JVMCIENV(JavaThread::current());
   724   Klass* klass = SystemDictionary::resolve_or_fail(name, true, CHECK_(Handle()));
   669   JVMCIObject jvmciRuntime = JVMCIRuntime::get_HotSpotJVMCIRuntime(JVMCI_CHECK);
   725   TempNewSymbol runtime = SymbolTable::new_symbol(methodName, CHECK_(Handle()));
   670   initialize(JVMCIENV);
   726   TempNewSymbol sig = SymbolTable::new_symbol(signature, CHECK_(Handle()));
   671   JVMCIENV->call_HotSpotJVMCIRuntime_getCompiler(jvmciRuntime, JVMCI_CHECK);
   727   JavaValue result(T_OBJECT);
   672 }
   728   if (args == NULL) {
   673 
   729     JavaCalls::call_static(&result, klass, runtime, sig, CHECK_(Handle()));
   674 void JVMCINMethodData::initialize(
       
   675   int nmethod_mirror_index,
       
   676   const char* name,
       
   677   FailedSpeculation** failed_speculations)
       
   678 {
       
   679   _failed_speculations = failed_speculations;
       
   680   _nmethod_mirror_index = nmethod_mirror_index;
       
   681   if (name != NULL) {
       
   682     _has_name = true;
       
   683     char* dest = (char*) this->name();
       
   684     strcpy(dest, name);
   730   } else {
   685   } else {
   731     JavaCalls::call_static(&result, klass, runtime, sig, args, CHECK_(Handle()));
   686     _has_name = false;
   732   }
   687   }
   733   return Handle(THREAD, (oop)result.get_jobject());
   688 }
   734 }
   689 
   735 
   690 void JVMCINMethodData::add_failed_speculation(nmethod* nm, jlong speculation) {
   736 Handle JVMCIRuntime::get_HotSpotJVMCIRuntime(TRAPS) {
   691   uint index = (speculation >> 32) & 0xFFFFFFFF;
   737   initialize_JVMCI(CHECK_(Handle()));
   692   int length = (int) speculation;
   738   return Handle(THREAD, JNIHandles::resolve_non_null(_HotSpotJVMCIRuntime_instance));
   693   if (index + length > (uint) nm->speculations_size()) {
   739 }
   694     fatal(INTPTR_FORMAT "[index: %d, length: %d] out of bounds wrt encoded speculations of length %u", speculation, index, length, nm->speculations_size());
   740 
   695   }
   741 void JVMCIRuntime::initialize_HotSpotJVMCIRuntime(TRAPS) {
   696   address data = nm->speculations_begin() + index;
   742   guarantee(!is_HotSpotJVMCIRuntime_initialized(), "cannot reinitialize HotSpotJVMCIRuntime");
   697   FailedSpeculation::add_failed_speculation(nm, _failed_speculations, data, length);
   743   JVMCIRuntime::initialize_well_known_classes(CHECK);
   698 }
       
   699 
       
   700 oop JVMCINMethodData::get_nmethod_mirror(nmethod* nm) {
       
   701   if (_nmethod_mirror_index == -1) {
       
   702     return NULL;
       
   703   }
       
   704   return nm->oop_at(_nmethod_mirror_index);
       
   705 }
       
   706 
       
   707 void JVMCINMethodData::set_nmethod_mirror(nmethod* nm, oop new_mirror) {
       
   708   assert(_nmethod_mirror_index != -1, "cannot set JVMCI mirror for nmethod");
       
   709   oop* addr = nm->oop_addr_at(_nmethod_mirror_index);
       
   710   assert(new_mirror != NULL, "use clear_nmethod_mirror to clear the mirror");
       
   711   assert(*addr == NULL, "cannot overwrite non-null mirror");
       
   712 
       
   713   *addr = new_mirror;
       
   714 
       
   715   // Since we've patched some oops in the nmethod,
       
   716   // (re)register it with the heap.
       
   717   Universe::heap()->register_nmethod(nm);
       
   718 }
       
   719 
       
   720 void JVMCINMethodData::clear_nmethod_mirror(nmethod* nm) {
       
   721   if (_nmethod_mirror_index != -1) {
       
   722     oop* addr = nm->oop_addr_at(_nmethod_mirror_index);
       
   723     *addr = NULL;
       
   724   }
       
   725 }
       
   726 
       
   727 void JVMCINMethodData::invalidate_nmethod_mirror(nmethod* nm) {
       
   728   oop nmethod_mirror = get_nmethod_mirror(nm);
       
   729   if (nmethod_mirror == NULL) {
       
   730     return;
       
   731   }
       
   732 
       
   733   // Update the values in the mirror if it still refers to nm.
       
   734   // We cannot use JVMCIObject to wrap the mirror as this is called
       
   735   // during GC, forbidding the creation of JNIHandles.
       
   736   JVMCIEnv* jvmciEnv = NULL;
       
   737   nmethod* current = (nmethod*) HotSpotJVMCI::InstalledCode::address(jvmciEnv, nmethod_mirror);
       
   738   if (nm == current) {
       
   739     if (!nm->is_alive()) {
       
   740       // Break the link from the mirror to nm such that
       
   741       // future invocations via the mirror will result in
       
   742       // an InvalidInstalledCodeException.
       
   743       HotSpotJVMCI::InstalledCode::set_address(jvmciEnv, nmethod_mirror, 0);
       
   744       HotSpotJVMCI::InstalledCode::set_entryPoint(jvmciEnv, nmethod_mirror, 0);
       
   745     } else if (nm->is_not_entrant()) {
       
   746       // Zero the entry point so any new invocation will fail but keep
       
   747       // the address link around that so that existing activations can
       
   748       // be deoptimized via the mirror (i.e. JVMCIEnv::invalidate_installed_code).
       
   749       HotSpotJVMCI::InstalledCode::set_entryPoint(jvmciEnv, nmethod_mirror, 0);
       
   750     }
       
   751   }
       
   752 }
       
   753 
       
   754 void JVMCIRuntime::initialize_HotSpotJVMCIRuntime(JVMCI_TRAPS) {
       
   755   if (is_HotSpotJVMCIRuntime_initialized()) {
       
   756     if (JVMCIENV->is_hotspot() && UseJVMCINativeLibrary) {
       
   757       JVMCI_THROW_MSG(InternalError, "JVMCI has already been enabled in the JVMCI shared library");
       
   758     }
       
   759   }
       
   760 
       
   761   initialize(JVMCIENV);
       
   762 
   744   // This should only be called in the context of the JVMCI class being initialized
   763   // This should only be called in the context of the JVMCI class being initialized
   745   InstanceKlass* klass = SystemDictionary::JVMCI_klass();
   764   JVMCIObject result = JVMCIENV->call_HotSpotJVMCIRuntime_runtime(JVMCI_CHECK);
   746   guarantee(klass->is_being_initialized() && klass->is_reentrant_initialization(THREAD),
   765 
   747          "HotSpotJVMCIRuntime initialization should only be triggered through JVMCI initialization");
   766   _HotSpotJVMCIRuntime_instance = JVMCIENV->make_global(result);
   748 
   767 }
   749   Handle result = callStatic("jdk/vm/ci/hotspot/HotSpotJVMCIRuntime",
   768 
   750                              "runtime",
   769 void JVMCIRuntime::initialize(JVMCIEnv* JVMCIENV) {
   751                              "()Ljdk/vm/ci/hotspot/HotSpotJVMCIRuntime;", NULL, CHECK);
   770   assert(this != NULL, "sanity");
   752   _HotSpotJVMCIRuntime_instance = JNIHandles::make_global(result);
   771   // Check first without JVMCI_lock
   753 }
   772   if (_initialized) {
   754 
       
   755 void JVMCIRuntime::initialize_JVMCI(TRAPS) {
       
   756   if (JNIHandles::resolve(_HotSpotJVMCIRuntime_instance) == NULL) {
       
   757     callStatic("jdk/vm/ci/runtime/JVMCI",
       
   758                "getRuntime",
       
   759                "()Ljdk/vm/ci/runtime/JVMCIRuntime;", NULL, CHECK);
       
   760   }
       
   761   assert(is_HotSpotJVMCIRuntime_initialized(), "what?");
       
   762 }
       
   763 
       
   764 bool JVMCIRuntime::can_initialize_JVMCI() {
       
   765   // Initializing JVMCI requires the module system to be initialized past phase 3.
       
   766   // The JVMCI API itself isn't available until phase 2 and ServiceLoader (which
       
   767   // JVMCI initialization requires) isn't usable until after phase 3. Testing
       
   768   // whether the system loader is initialized satisfies all these invariants.
       
   769   if (SystemDictionary::java_system_loader() == NULL) {
       
   770     return false;
       
   771   }
       
   772   assert(Universe::is_module_initialized(), "must be");
       
   773   return true;
       
   774 }
       
   775 
       
   776 void JVMCIRuntime::initialize_well_known_classes(TRAPS) {
       
   777   if (JVMCIRuntime::_well_known_classes_initialized == false) {
       
   778     guarantee(can_initialize_JVMCI(), "VM is not yet sufficiently booted to initialize JVMCI");
       
   779     SystemDictionary::WKID scan = SystemDictionary::FIRST_JVMCI_WKID;
       
   780     SystemDictionary::resolve_wk_klasses_through(SystemDictionary::LAST_JVMCI_WKID, scan, CHECK);
       
   781     JVMCIJavaClasses::compute_offsets(CHECK);
       
   782     JVMCIRuntime::_well_known_classes_initialized = true;
       
   783   }
       
   784 }
       
   785 
       
   786 void JVMCIRuntime::metadata_do(void f(Metadata*)) {
       
   787   // For simplicity, the existence of HotSpotJVMCIMetaAccessContext in
       
   788   // the SystemDictionary well known classes should ensure the other
       
   789   // classes have already been loaded, so make sure their order in the
       
   790   // table enforces that.
       
   791   assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotResolvedJavaMethodImpl) <
       
   792          SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier");
       
   793   assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotConstantPool) <
       
   794          SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier");
       
   795   assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotResolvedObjectTypeImpl) <
       
   796          SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier");
       
   797 
       
   798   if (HotSpotJVMCIMetaAccessContext::klass() == NULL ||
       
   799       !HotSpotJVMCIMetaAccessContext::klass()->is_linked()) {
       
   800     // Nothing could be registered yet
       
   801     return;
   773     return;
   802   }
   774   }
   803 
   775 
   804   // WeakReference<HotSpotJVMCIMetaAccessContext>[]
   776   MutexLocker locker(JVMCI_lock);
   805   objArrayOop allContexts = HotSpotJVMCIMetaAccessContext::allContexts();
   777   // Check again under JVMCI_lock
   806   if (allContexts == NULL) {
   778   if (_initialized) {
   807     return;
   779     return;
   808   }
   780   }
   809 
   781 
   810   // These must be loaded at this point but the linking state doesn't matter.
   782   while (_being_initialized) {
   811   assert(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass() != NULL, "must be loaded");
   783     JVMCI_lock->wait();
   812   assert(SystemDictionary::HotSpotConstantPool_klass() != NULL, "must be loaded");
   784     if (_initialized) {
   813   assert(SystemDictionary::HotSpotResolvedObjectTypeImpl_klass() != NULL, "must be loaded");
   785       return;
   814 
   786     }
   815   for (int i = 0; i < allContexts->length(); i++) {
   787   }
   816     oop ref = allContexts->obj_at(i);
   788 
   817     if (ref != NULL) {
   789   _being_initialized = true;
   818       oop referent = java_lang_ref_Reference::referent(ref);
   790 
   819       if (referent != NULL) {
   791   {
   820         // Chunked Object[] with last element pointing to next chunk
   792     MutexUnlocker unlock(JVMCI_lock);
   821         objArrayOop metadataRoots = HotSpotJVMCIMetaAccessContext::metadataRoots(referent);
   793 
   822         while (metadataRoots != NULL) {
   794     HandleMark hm;
   823           for (int typeIndex = 0; typeIndex < metadataRoots->length() - 1; typeIndex++) {
   795     ResourceMark rm;
   824             oop reference = metadataRoots->obj_at(typeIndex);
   796     JavaThread* THREAD = JavaThread::current();
   825             if (reference == NULL) {
   797     if (JVMCIENV->is_hotspot()) {
   826               continue;
   798       HotSpotJVMCI::compute_offsets(CHECK_EXIT);
   827             }
   799     } else {
   828             oop metadataRoot = java_lang_ref_Reference::referent(reference);
   800       JNIAccessMark jni(JVMCIENV);
   829             if (metadataRoot == NULL) {
   801 
   830               continue;
   802       JNIJVMCI::initialize_ids(jni.env());
   831             }
   803       if (jni()->ExceptionCheck()) {
   832             if (metadataRoot->is_a(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass())) {
   804         jni()->ExceptionDescribe();
   833               Method* method = CompilerToVM::asMethod(metadataRoot);
   805         fatal("JNI exception during init");
   834               f(method);
       
   835             } else if (metadataRoot->is_a(SystemDictionary::HotSpotConstantPool_klass())) {
       
   836               ConstantPool* constantPool = CompilerToVM::asConstantPool(metadataRoot);
       
   837               f(constantPool);
       
   838             } else if (metadataRoot->is_a(SystemDictionary::HotSpotResolvedObjectTypeImpl_klass())) {
       
   839               Klass* klass = CompilerToVM::asKlass(metadataRoot);
       
   840               f(klass);
       
   841             } else {
       
   842               metadataRoot->print();
       
   843               ShouldNotReachHere();
       
   844             }
       
   845           }
       
   846           metadataRoots = (objArrayOop)metadataRoots->obj_at(metadataRoots->length() - 1);
       
   847           assert(metadataRoots == NULL || metadataRoots->is_objArray(), "wrong type");
       
   848         }
       
   849       }
   806       }
   850     }
   807     }
   851   }
   808     create_jvmci_primitive_type(T_BOOLEAN, JVMCI_CHECK_EXIT_((void)0));
   852 }
   809     create_jvmci_primitive_type(T_BYTE, JVMCI_CHECK_EXIT_((void)0));
   853 
   810     create_jvmci_primitive_type(T_CHAR, JVMCI_CHECK_EXIT_((void)0));
   854 // private static void CompilerToVM.registerNatives()
   811     create_jvmci_primitive_type(T_SHORT, JVMCI_CHECK_EXIT_((void)0));
   855 JVM_ENTRY(void, JVM_RegisterJVMCINatives(JNIEnv *env, jclass c2vmClass))
   812     create_jvmci_primitive_type(T_INT, JVMCI_CHECK_EXIT_((void)0));
   856   if (!EnableJVMCI) {
   813     create_jvmci_primitive_type(T_LONG, JVMCI_CHECK_EXIT_((void)0));
   857     THROW_MSG(vmSymbols::java_lang_InternalError(), "JVMCI is not enabled");
   814     create_jvmci_primitive_type(T_FLOAT, JVMCI_CHECK_EXIT_((void)0));
   858   }
   815     create_jvmci_primitive_type(T_DOUBLE, JVMCI_CHECK_EXIT_((void)0));
       
   816     create_jvmci_primitive_type(T_VOID, JVMCI_CHECK_EXIT_((void)0));
       
   817 
       
   818     if (!JVMCIENV->is_hotspot()) {
       
   819       JVMCIENV->copy_saved_properties();
       
   820     }
       
   821   }
       
   822 
       
   823   _initialized = true;
       
   824   _being_initialized = false;
       
   825   JVMCI_lock->notify_all();
       
   826 }
       
   827 
       
   828 JVMCIObject JVMCIRuntime::create_jvmci_primitive_type(BasicType type, JVMCI_TRAPS) {
       
   829   Thread* THREAD = Thread::current();
       
   830   // These primitive types are long lived and are created before the runtime is fully set up
       
   831   // so skip registering them for scanning.
       
   832   JVMCIObject mirror = JVMCIENV->get_object_constant(java_lang_Class::primitive_mirror(type), false, true);
       
   833   if (JVMCIENV->is_hotspot()) {
       
   834     JavaValue result(T_OBJECT);
       
   835     JavaCallArguments args;
       
   836     args.push_oop(Handle(THREAD, HotSpotJVMCI::resolve(mirror)));
       
   837     args.push_int(type2char(type));
       
   838     JavaCalls::call_static(&result, HotSpotJVMCI::HotSpotResolvedPrimitiveType::klass(), vmSymbols::fromMetaspace_name(), vmSymbols::primitive_fromMetaspace_signature(), &args, CHECK_(JVMCIObject()));
       
   839 
       
   840     return JVMCIENV->wrap(JNIHandles::make_local((oop)result.get_jobject()));
       
   841   } else {
       
   842     JNIAccessMark jni(JVMCIENV);
       
   843     jobject result = jni()->CallStaticObjectMethod(JNIJVMCI::HotSpotResolvedPrimitiveType::clazz(),
       
   844                                            JNIJVMCI::HotSpotResolvedPrimitiveType_fromMetaspace_method(),
       
   845                                            mirror.as_jobject(), type2char(type));
       
   846     if (jni()->ExceptionCheck()) {
       
   847       return JVMCIObject();
       
   848     }
       
   849     return JVMCIENV->wrap(result);
       
   850   }
       
   851 }
       
   852 
       
   853 void JVMCIRuntime::initialize_JVMCI(JVMCI_TRAPS) {
       
   854   if (!is_HotSpotJVMCIRuntime_initialized()) {
       
   855     initialize(JVMCI_CHECK);
       
   856     JVMCIENV->call_JVMCI_getRuntime(JVMCI_CHECK);
       
   857   }
       
   858 }
       
   859 
       
   860 JVMCIObject JVMCIRuntime::get_HotSpotJVMCIRuntime(JVMCI_TRAPS) {
       
   861   initialize(JVMCIENV);
       
   862   initialize_JVMCI(JVMCI_CHECK_(JVMCIObject()));
       
   863   return _HotSpotJVMCIRuntime_instance;
       
   864 }
       
   865 
       
   866 
       
   867 // private void CompilerToVM.registerNatives()
       
   868 JVM_ENTRY_NO_ENV(void, JVM_RegisterJVMCINatives(JNIEnv *env, jclass c2vmClass))
   859 
   869 
   860 #ifdef _LP64
   870 #ifdef _LP64
   861 #ifndef SPARC
   871 #ifndef TARGET_ARCH_sparc
   862   uintptr_t heap_end = (uintptr_t) Universe::heap()->reserved_region().end();
   872   uintptr_t heap_end = (uintptr_t) Universe::heap()->reserved_region().end();
   863   uintptr_t allocation_end = heap_end + ((uintptr_t)16) * 1024 * 1024 * 1024;
   873   uintptr_t allocation_end = heap_end + ((uintptr_t)16) * 1024 * 1024 * 1024;
   864   guarantee(heap_end < allocation_end, "heap end too close to end of address space (might lead to erroneous TLAB allocations)");
   874   guarantee(heap_end < allocation_end, "heap end too close to end of address space (might lead to erroneous TLAB allocations)");
   865 #endif // !SPARC
   875 #endif // TARGET_ARCH_sparc
   866 #else
   876 #else
   867   fatal("check TLAB allocation code for address space conflicts");
   877   fatal("check TLAB allocation code for address space conflicts");
   868 #endif // _LP64
   878 #endif
   869 
   879 
   870   JVMCIRuntime::initialize_well_known_classes(CHECK);
   880   JNI_JVMCIENV(env);
       
   881 
       
   882   if (!EnableJVMCI) {
       
   883     JVMCI_THROW_MSG(InternalError, "JVMCI is not enabled");
       
   884   }
       
   885 
       
   886   JVMCIENV->runtime()->initialize(JVMCIENV);
   871 
   887 
   872   {
   888   {
       
   889     ResourceMark rm;
       
   890     HandleMark hm(thread);
   873     ThreadToNativeFromVM trans(thread);
   891     ThreadToNativeFromVM trans(thread);
   874     env->RegisterNatives(c2vmClass, CompilerToVM::methods, CompilerToVM::methods_count());
   892 
       
   893     // Ensure _non_oop_bits is initialized
       
   894     Universe::non_oop_word();
       
   895 
       
   896     if (JNI_OK != env->RegisterNatives(c2vmClass, CompilerToVM::methods, CompilerToVM::methods_count())) {
       
   897       if (!env->ExceptionCheck()) {
       
   898         for (int i = 0; i < CompilerToVM::methods_count(); i++) {
       
   899           if (JNI_OK != env->RegisterNatives(c2vmClass, CompilerToVM::methods + i, 1)) {
       
   900             guarantee(false, "Error registering JNI method %s%s", CompilerToVM::methods[i].name, CompilerToVM::methods[i].signature);
       
   901             break;
       
   902           }
       
   903         }
       
   904       } else {
       
   905         env->ExceptionDescribe();
       
   906       }
       
   907       guarantee(false, "Failed registering CompilerToVM native methods");
       
   908     }
   875   }
   909   }
   876 JVM_END
   910 JVM_END
   877 
   911 
   878 void JVMCIRuntime::shutdown(TRAPS) {
   912 
   879   if (_HotSpotJVMCIRuntime_instance != NULL) {
   913 void JVMCIRuntime::shutdown() {
       
   914   if (is_HotSpotJVMCIRuntime_initialized()) {
   880     _shutdown_called = true;
   915     _shutdown_called = true;
   881     HandleMark hm(THREAD);
   916 
   882     Handle receiver = get_HotSpotJVMCIRuntime(CHECK);
   917     THREAD_JVMCIENV(JavaThread::current());
   883     JavaValue result(T_VOID);
   918     JVMCIENV->call_HotSpotJVMCIRuntime_shutdown(_HotSpotJVMCIRuntime_instance);
   884     JavaCallArguments args;
       
   885     args.push_oop(receiver);
       
   886     JavaCalls::call_special(&result, receiver->klass(), vmSymbols::shutdown_method_name(), vmSymbols::void_method_signature(), &args, CHECK);
       
   887   }
   919   }
   888 }
   920 }
   889 
   921 
   890 void JVMCIRuntime::bootstrap_finished(TRAPS) {
   922 void JVMCIRuntime::bootstrap_finished(TRAPS) {
   891   HandleMark hm(THREAD);
   923   if (is_HotSpotJVMCIRuntime_initialized()) {
   892   Handle receiver = get_HotSpotJVMCIRuntime(CHECK);
   924     THREAD_JVMCIENV(JavaThread::current());
   893   JavaValue result(T_VOID);
   925     JVMCIENV->call_HotSpotJVMCIRuntime_bootstrapFinished(_HotSpotJVMCIRuntime_instance, JVMCIENV);
   894   JavaCallArguments args;
   926   }
   895   args.push_oop(receiver);
   927 }
   896   JavaCalls::call_special(&result, receiver->klass(), vmSymbols::bootstrapFinished_method_name(), vmSymbols::void_method_signature(), &args, CHECK);
   928 
   897 }
   929 void JVMCIRuntime::describe_pending_hotspot_exception(JavaThread* THREAD, bool clear) {
       
   930   if (HAS_PENDING_EXCEPTION) {
       
   931     Handle exception(THREAD, PENDING_EXCEPTION);
       
   932     const char* exception_file = THREAD->exception_file();
       
   933     int exception_line = THREAD->exception_line();
       
   934     CLEAR_PENDING_EXCEPTION;
       
   935     if (exception->is_a(SystemDictionary::ThreadDeath_klass())) {
       
   936       // Don't print anything if we are being killed.
       
   937     } else {
       
   938       java_lang_Throwable::print(exception(), tty);
       
   939       tty->cr();
       
   940       java_lang_Throwable::print_stack_trace(exception, tty);
       
   941 
       
   942       // Clear and ignore any exceptions raised during printing
       
   943       CLEAR_PENDING_EXCEPTION;
       
   944     }
       
   945     if (!clear) {
       
   946       THREAD->set_pending_exception(exception(), exception_file, exception_line);
       
   947     }
       
   948   }
       
   949 }
       
   950 
       
   951 
       
   952 void JVMCIRuntime::exit_on_pending_exception(JVMCIEnv* JVMCIENV, const char* message) {
       
   953   JavaThread* THREAD = JavaThread::current();
       
   954 
       
   955   static volatile int report_error = 0;
       
   956   if (!report_error && Atomic::cmpxchg(1, &report_error, 0) == 0) {
       
   957     // Only report an error once
       
   958     tty->print_raw_cr(message);
       
   959     if (JVMCIENV != NULL) {
       
   960       JVMCIENV->describe_pending_exception(true);
       
   961     } else {
       
   962       describe_pending_hotspot_exception(THREAD, true);
       
   963     }
       
   964   } else {
       
   965     // Allow error reporting thread to print the stack trace.  Windows
       
   966     // doesn't allow uninterruptible wait for JavaThreads
       
   967     const bool interruptible = true;
       
   968     os::sleep(THREAD, 200, interruptible);
       
   969   }
       
   970 
       
   971   before_exit(THREAD);
       
   972   vm_exit(-1);
       
   973 }
       
   974 
       
   975 // ------------------------------------------------------------------
       
   976 // Note: the logic of this method should mirror the logic of
       
   977 // constantPoolOopDesc::verify_constant_pool_resolve.
       
   978 bool JVMCIRuntime::check_klass_accessibility(Klass* accessing_klass, Klass* resolved_klass) {
       
   979   if (accessing_klass->is_objArray_klass()) {
       
   980     accessing_klass = ObjArrayKlass::cast(accessing_klass)->bottom_klass();
       
   981   }
       
   982   if (!accessing_klass->is_instance_klass()) {
       
   983     return true;
       
   984   }
       
   985 
       
   986   if (resolved_klass->is_objArray_klass()) {
       
   987     // Find the element klass, if this is an array.
       
   988     resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass();
       
   989   }
       
   990   if (resolved_klass->is_instance_klass()) {
       
   991     Reflection::VerifyClassAccessResults result =
       
   992       Reflection::verify_class_access(accessing_klass, InstanceKlass::cast(resolved_klass), true);
       
   993     return result == Reflection::ACCESS_OK;
       
   994   }
       
   995   return true;
       
   996 }
       
   997 
       
   998 // ------------------------------------------------------------------
       
   999 Klass* JVMCIRuntime::get_klass_by_name_impl(Klass*& accessing_klass,
       
  1000                                           const constantPoolHandle& cpool,
       
  1001                                           Symbol* sym,
       
  1002                                           bool require_local) {
       
  1003   JVMCI_EXCEPTION_CONTEXT;
       
  1004 
       
  1005   // Now we need to check the SystemDictionary
       
  1006   if (sym->char_at(0) == 'L' &&
       
  1007     sym->char_at(sym->utf8_length()-1) == ';') {
       
  1008     // This is a name from a signature.  Strip off the trimmings.
       
  1009     // Call recursive to keep scope of strippedsym.
       
  1010     TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1,
       
  1011                     sym->utf8_length()-2,
       
  1012                     CHECK_NULL);
       
  1013     return get_klass_by_name_impl(accessing_klass, cpool, strippedsym, require_local);
       
  1014   }
       
  1015 
       
  1016   Handle loader(THREAD, (oop)NULL);
       
  1017   Handle domain(THREAD, (oop)NULL);
       
  1018   if (accessing_klass != NULL) {
       
  1019     loader = Handle(THREAD, accessing_klass->class_loader());
       
  1020     domain = Handle(THREAD, accessing_klass->protection_domain());
       
  1021   }
       
  1022 
       
  1023   Klass* found_klass;
       
  1024   {
       
  1025     ttyUnlocker ttyul;  // release tty lock to avoid ordering problems
       
  1026     MutexLocker ml(Compile_lock);
       
  1027     if (!require_local) {
       
  1028       found_klass = SystemDictionary::find_constrained_instance_or_array_klass(sym, loader, CHECK_NULL);
       
  1029     } else {
       
  1030       found_klass = SystemDictionary::find_instance_or_array_klass(sym, loader, domain, CHECK_NULL);
       
  1031     }
       
  1032   }
       
  1033 
       
  1034   // If we fail to find an array klass, look again for its element type.
       
  1035   // The element type may be available either locally or via constraints.
       
  1036   // In either case, if we can find the element type in the system dictionary,
       
  1037   // we must build an array type around it.  The CI requires array klasses
       
  1038   // to be loaded if their element klasses are loaded, except when memory
       
  1039   // is exhausted.
       
  1040   if (sym->char_at(0) == '[' &&
       
  1041       (sym->char_at(1) == '[' || sym->char_at(1) == 'L')) {
       
  1042     // We have an unloaded array.
       
  1043     // Build it on the fly if the element class exists.
       
  1044     TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1,
       
  1045                                                  sym->utf8_length()-1,
       
  1046                                                  CHECK_NULL);
       
  1047 
       
  1048     // Get element Klass recursively.
       
  1049     Klass* elem_klass =
       
  1050       get_klass_by_name_impl(accessing_klass,
       
  1051                              cpool,
       
  1052                              elem_sym,
       
  1053                              require_local);
       
  1054     if (elem_klass != NULL) {
       
  1055       // Now make an array for it
       
  1056       return elem_klass->array_klass(THREAD);
       
  1057     }
       
  1058   }
       
  1059 
       
  1060   if (found_klass == NULL && !cpool.is_null() && cpool->has_preresolution()) {
       
  1061     // Look inside the constant pool for pre-resolved class entries.
       
  1062     for (int i = cpool->length() - 1; i >= 1; i--) {
       
  1063       if (cpool->tag_at(i).is_klass()) {
       
  1064         Klass*  kls = cpool->resolved_klass_at(i);
       
  1065         if (kls->name() == sym) {
       
  1066           return kls;
       
  1067         }
       
  1068       }
       
  1069     }
       
  1070   }
       
  1071 
       
  1072   return found_klass;
       
  1073 }
       
  1074 
       
  1075 // ------------------------------------------------------------------
       
  1076 Klass* JVMCIRuntime::get_klass_by_name(Klass* accessing_klass,
       
  1077                                   Symbol* klass_name,
       
  1078                                   bool require_local) {
       
  1079   ResourceMark rm;
       
  1080   constantPoolHandle cpool;
       
  1081   return get_klass_by_name_impl(accessing_klass,
       
  1082                                                  cpool,
       
  1083                                                  klass_name,
       
  1084                                                  require_local);
       
  1085 }
       
  1086 
       
  1087 // ------------------------------------------------------------------
       
  1088 // Implementation of get_klass_by_index.
       
  1089 Klass* JVMCIRuntime::get_klass_by_index_impl(const constantPoolHandle& cpool,
       
  1090                                         int index,
       
  1091                                         bool& is_accessible,
       
  1092                                         Klass* accessor) {
       
  1093   JVMCI_EXCEPTION_CONTEXT;
       
  1094   Klass* klass = ConstantPool::klass_at_if_loaded(cpool, index);
       
  1095   Symbol* klass_name = NULL;
       
  1096   if (klass == NULL) {
       
  1097     klass_name = cpool->klass_name_at(index);
       
  1098   }
       
  1099 
       
  1100   if (klass == NULL) {
       
  1101     // Not found in constant pool.  Use the name to do the lookup.
       
  1102     Klass* k = get_klass_by_name_impl(accessor,
       
  1103                                         cpool,
       
  1104                                         klass_name,
       
  1105                                         false);
       
  1106     // Calculate accessibility the hard way.
       
  1107     if (k == NULL) {
       
  1108       is_accessible = false;
       
  1109     } else if (k->class_loader() != accessor->class_loader() &&
       
  1110                get_klass_by_name_impl(accessor, cpool, k->name(), true) == NULL) {
       
  1111       // Loaded only remotely.  Not linked yet.
       
  1112       is_accessible = false;
       
  1113     } else {
       
  1114       // Linked locally, and we must also check public/private, etc.
       
  1115       is_accessible = check_klass_accessibility(accessor, k);
       
  1116     }
       
  1117     if (!is_accessible) {
       
  1118       return NULL;
       
  1119     }
       
  1120     return k;
       
  1121   }
       
  1122 
       
  1123   // It is known to be accessible, since it was found in the constant pool.
       
  1124   is_accessible = true;
       
  1125   return klass;
       
  1126 }
       
  1127 
       
  1128 // ------------------------------------------------------------------
       
  1129 // Get a klass from the constant pool.
       
  1130 Klass* JVMCIRuntime::get_klass_by_index(const constantPoolHandle& cpool,
       
  1131                                    int index,
       
  1132                                    bool& is_accessible,
       
  1133                                    Klass* accessor) {
       
  1134   ResourceMark rm;
       
  1135   Klass* result = get_klass_by_index_impl(cpool, index, is_accessible, accessor);
       
  1136   return result;
       
  1137 }
       
  1138 
       
  1139 // ------------------------------------------------------------------
       
  1140 // Implementation of get_field_by_index.
       
  1141 //
       
  1142 // Implementation note: the results of field lookups are cached
       
  1143 // in the accessor klass.
       
  1144 void JVMCIRuntime::get_field_by_index_impl(InstanceKlass* klass, fieldDescriptor& field_desc,
       
  1145                                         int index) {
       
  1146   JVMCI_EXCEPTION_CONTEXT;
       
  1147 
       
  1148   assert(klass->is_linked(), "must be linked before using its constant-pool");
       
  1149 
       
  1150   constantPoolHandle cpool(thread, klass->constants());
       
  1151 
       
  1152   // Get the field's name, signature, and type.
       
  1153   Symbol* name  = cpool->name_ref_at(index);
       
  1154 
       
  1155   int nt_index = cpool->name_and_type_ref_index_at(index);
       
  1156   int sig_index = cpool->signature_ref_index_at(nt_index);
       
  1157   Symbol* signature = cpool->symbol_at(sig_index);
       
  1158 
       
  1159   // Get the field's declared holder.
       
  1160   int holder_index = cpool->klass_ref_index_at(index);
       
  1161   bool holder_is_accessible;
       
  1162   Klass* declared_holder = get_klass_by_index(cpool, holder_index,
       
  1163                                                holder_is_accessible,
       
  1164                                                klass);
       
  1165 
       
  1166   // The declared holder of this field may not have been loaded.
       
  1167   // Bail out with partial field information.
       
  1168   if (!holder_is_accessible) {
       
  1169     return;
       
  1170   }
       
  1171 
       
  1172 
       
  1173   // Perform the field lookup.
       
  1174   Klass*  canonical_holder =
       
  1175     InstanceKlass::cast(declared_holder)->find_field(name, signature, &field_desc);
       
  1176   if (canonical_holder == NULL) {
       
  1177     return;
       
  1178   }
       
  1179 
       
  1180   assert(canonical_holder == field_desc.field_holder(), "just checking");
       
  1181 }
       
  1182 
       
  1183 // ------------------------------------------------------------------
       
  1184 // Get a field by index from a klass's constant pool.
       
  1185 void JVMCIRuntime::get_field_by_index(InstanceKlass* accessor, fieldDescriptor& fd, int index) {
       
  1186   ResourceMark rm;
       
  1187   return get_field_by_index_impl(accessor, fd, index);
       
  1188 }
       
  1189 
       
  1190 // ------------------------------------------------------------------
       
  1191 // Perform an appropriate method lookup based on accessor, holder,
       
  1192 // name, signature, and bytecode.
       
  1193 methodHandle JVMCIRuntime::lookup_method(InstanceKlass* accessor,
       
  1194                                Klass*        holder,
       
  1195                                Symbol*       name,
       
  1196                                Symbol*       sig,
       
  1197                                Bytecodes::Code bc,
       
  1198                                constantTag   tag) {
       
  1199   // Accessibility checks are performed in JVMCIEnv::get_method_by_index_impl().
       
  1200   assert(check_klass_accessibility(accessor, holder), "holder not accessible");
       
  1201 
       
  1202   methodHandle dest_method;
       
  1203   LinkInfo link_info(holder, name, sig, accessor, LinkInfo::needs_access_check, tag);
       
  1204   switch (bc) {
       
  1205   case Bytecodes::_invokestatic:
       
  1206     dest_method =
       
  1207       LinkResolver::resolve_static_call_or_null(link_info);
       
  1208     break;
       
  1209   case Bytecodes::_invokespecial:
       
  1210     dest_method =
       
  1211       LinkResolver::resolve_special_call_or_null(link_info);
       
  1212     break;
       
  1213   case Bytecodes::_invokeinterface:
       
  1214     dest_method =
       
  1215       LinkResolver::linktime_resolve_interface_method_or_null(link_info);
       
  1216     break;
       
  1217   case Bytecodes::_invokevirtual:
       
  1218     dest_method =
       
  1219       LinkResolver::linktime_resolve_virtual_method_or_null(link_info);
       
  1220     break;
       
  1221   default: ShouldNotReachHere();
       
  1222   }
       
  1223 
       
  1224   return dest_method;
       
  1225 }
       
  1226 
       
  1227 
       
  1228 // ------------------------------------------------------------------
       
  1229 methodHandle JVMCIRuntime::get_method_by_index_impl(const constantPoolHandle& cpool,
       
  1230                                           int index, Bytecodes::Code bc,
       
  1231                                           InstanceKlass* accessor) {
       
  1232   if (bc == Bytecodes::_invokedynamic) {
       
  1233     ConstantPoolCacheEntry* cpce = cpool->invokedynamic_cp_cache_entry_at(index);
       
  1234     bool is_resolved = !cpce->is_f1_null();
       
  1235     if (is_resolved) {
       
  1236       // Get the invoker Method* from the constant pool.
       
  1237       // (The appendix argument, if any, will be noted in the method's signature.)
       
  1238       Method* adapter = cpce->f1_as_method();
       
  1239       return methodHandle(adapter);
       
  1240     }
       
  1241 
       
  1242     return NULL;
       
  1243   }
       
  1244 
       
  1245   int holder_index = cpool->klass_ref_index_at(index);
       
  1246   bool holder_is_accessible;
       
  1247   Klass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);
       
  1248 
       
  1249   // Get the method's name and signature.
       
  1250   Symbol* name_sym = cpool->name_ref_at(index);
       
  1251   Symbol* sig_sym  = cpool->signature_ref_at(index);
       
  1252 
       
  1253   if (cpool->has_preresolution()
       
  1254       || ((holder == SystemDictionary::MethodHandle_klass() || holder == SystemDictionary::VarHandle_klass()) &&
       
  1255           MethodHandles::is_signature_polymorphic_name(holder, name_sym))) {
       
  1256     // Short-circuit lookups for JSR 292-related call sites.
       
  1257     // That is, do not rely only on name-based lookups, because they may fail
       
  1258     // if the names are not resolvable in the boot class loader (7056328).
       
  1259     switch (bc) {
       
  1260     case Bytecodes::_invokevirtual:
       
  1261     case Bytecodes::_invokeinterface:
       
  1262     case Bytecodes::_invokespecial:
       
  1263     case Bytecodes::_invokestatic:
       
  1264       {
       
  1265         Method* m = ConstantPool::method_at_if_loaded(cpool, index);
       
  1266         if (m != NULL) {
       
  1267           return m;
       
  1268         }
       
  1269       }
       
  1270       break;
       
  1271     default:
       
  1272       break;
       
  1273     }
       
  1274   }
       
  1275 
       
  1276   if (holder_is_accessible) { // Our declared holder is loaded.
       
  1277     constantTag tag = cpool->tag_ref_at(index);
       
  1278     methodHandle m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
       
  1279     if (!m.is_null()) {
       
  1280       // We found the method.
       
  1281       return m;
       
  1282     }
       
  1283   }
       
  1284 
       
  1285   // Either the declared holder was not loaded, or the method could
       
  1286   // not be found.
       
  1287 
       
  1288   return NULL;
       
  1289 }
       
  1290 
       
  1291 // ------------------------------------------------------------------
       
  1292 InstanceKlass* JVMCIRuntime::get_instance_klass_for_declared_method_holder(Klass* method_holder) {
       
  1293   // For the case of <array>.clone(), the method holder can be an ArrayKlass*
       
  1294   // instead of an InstanceKlass*.  For that case simply pretend that the
       
  1295   // declared holder is Object.clone since that's where the call will bottom out.
       
  1296   if (method_holder->is_instance_klass()) {
       
  1297     return InstanceKlass::cast(method_holder);
       
  1298   } else if (method_holder->is_array_klass()) {
       
  1299     return InstanceKlass::cast(SystemDictionary::Object_klass());
       
  1300   } else {
       
  1301     ShouldNotReachHere();
       
  1302   }
       
  1303   return NULL;
       
  1304 }
       
  1305 
       
  1306 
       
  1307 // ------------------------------------------------------------------
       
  1308 methodHandle JVMCIRuntime::get_method_by_index(const constantPoolHandle& cpool,
       
  1309                                      int index, Bytecodes::Code bc,
       
  1310                                      InstanceKlass* accessor) {
       
  1311   ResourceMark rm;
       
  1312   return get_method_by_index_impl(cpool, index, bc, accessor);
       
  1313 }
       
  1314 
       
  1315 // ------------------------------------------------------------------
       
  1316 // Check for changes to the system dictionary during compilation
       
  1317 // class loads, evolution, breakpoints
       
  1318 JVMCI::CodeInstallResult JVMCIRuntime::validate_compile_task_dependencies(Dependencies* dependencies, JVMCICompileState* compile_state, char** failure_detail) {
       
  1319   // If JVMTI capabilities were enabled during compile, the compilation is invalidated.
       
  1320   if (compile_state != NULL && compile_state->jvmti_state_changed()) {
       
  1321     *failure_detail = (char*) "Jvmti state change during compilation invalidated dependencies";
       
  1322     return JVMCI::dependencies_failed;
       
  1323   }
       
  1324 
       
  1325   // Dependencies must be checked when the system dictionary changes
       
  1326   // or if we don't know whether it has changed (i.e., compile_state == NULL).
       
  1327   bool counter_changed = compile_state == NULL || compile_state->system_dictionary_modification_counter() != SystemDictionary::number_of_modifications();
       
  1328   CompileTask* task = compile_state == NULL ? NULL : compile_state->task();
       
  1329   Dependencies::DepType result = dependencies->validate_dependencies(task, counter_changed, failure_detail);
       
  1330   if (result == Dependencies::end_marker) {
       
  1331     return JVMCI::ok;
       
  1332   }
       
  1333 
       
  1334   if (!Dependencies::is_klass_type(result) || counter_changed) {
       
  1335     return JVMCI::dependencies_failed;
       
  1336   }
       
  1337   // The dependencies were invalid at the time of installation
       
  1338   // without any intervening modification of the system
       
  1339   // dictionary.  That means they were invalidly constructed.
       
  1340   return JVMCI::dependencies_invalid;
       
  1341 }
       
  1342 
       
  1343 
       
  1344 void JVMCIRuntime::compile_method(JVMCIEnv* JVMCIENV, JVMCICompiler* compiler, const methodHandle& method, int entry_bci) {
       
  1345   JVMCI_EXCEPTION_CONTEXT
       
  1346 
       
  1347   JVMCICompileState* compile_state = JVMCIENV->compile_state();
       
  1348 
       
  1349   bool is_osr = entry_bci != InvocationEntryBci;
       
  1350   if (compiler->is_bootstrapping() && is_osr) {
       
  1351     // no OSR compilations during bootstrap - the compiler is just too slow at this point,
       
  1352     // and we know that there are no endless loops
       
  1353     compile_state->set_failure(true, "No OSR during boostrap");
       
  1354     return;
       
  1355   }
       
  1356 
       
  1357   HandleMark hm;
       
  1358   JVMCIObject receiver = get_HotSpotJVMCIRuntime(JVMCIENV);
       
  1359   if (JVMCIENV->has_pending_exception()) {
       
  1360     JVMCIENV->describe_pending_exception(true);
       
  1361     compile_state->set_failure(false, "exception getting HotSpotJVMCIRuntime object");
       
  1362     return;
       
  1363   }
       
  1364   JVMCIObject jvmci_method = JVMCIENV->get_jvmci_method(method, JVMCIENV);
       
  1365   if (JVMCIENV->has_pending_exception()) {
       
  1366     JVMCIENV->describe_pending_exception(true);
       
  1367     compile_state->set_failure(false, "exception getting JVMCI wrapper method");
       
  1368     return;
       
  1369   }
       
  1370 
       
  1371   JVMCIObject result_object = JVMCIENV->call_HotSpotJVMCIRuntime_compileMethod(receiver, jvmci_method, entry_bci,
       
  1372                                                                      (jlong) compile_state, compile_state->task()->compile_id());
       
  1373   if (!JVMCIENV->has_pending_exception()) {
       
  1374     if (result_object.is_non_null()) {
       
  1375       JVMCIObject failure_message = JVMCIENV->get_HotSpotCompilationRequestResult_failureMessage(result_object);
       
  1376       if (failure_message.is_non_null()) {
       
  1377         // Copy failure reason into resource memory first ...
       
  1378         const char* failure_reason = JVMCIENV->as_utf8_string(failure_message);
       
  1379         // ... and then into the C heap.
       
  1380         failure_reason = os::strdup(failure_reason, mtJVMCI);
       
  1381         bool retryable = JVMCIENV->get_HotSpotCompilationRequestResult_retry(result_object) != 0;
       
  1382         compile_state->set_failure(retryable, failure_reason, true);
       
  1383       } else {
       
  1384         if (compile_state->task()->code() == NULL) {
       
  1385           compile_state->set_failure(true, "no nmethod produced");
       
  1386         } else {
       
  1387           compile_state->task()->set_num_inlined_bytecodes(JVMCIENV->get_HotSpotCompilationRequestResult_inlinedBytecodes(result_object));
       
  1388           compiler->inc_methods_compiled();
       
  1389         }
       
  1390       }
       
  1391     } else {
       
  1392       assert(false, "JVMCICompiler.compileMethod should always return non-null");
       
  1393     }
       
  1394   } else {
       
  1395     // An uncaught exception was thrown during compilation. Generally these
       
  1396     // should be handled by the Java code in some useful way but if they leak
       
  1397     // through to here report them instead of dying or silently ignoring them.
       
  1398     JVMCIENV->describe_pending_exception(true);
       
  1399     compile_state->set_failure(false, "unexpected exception thrown");
       
  1400   }
       
  1401   if (compiler->is_bootstrapping()) {
       
  1402     compiler->set_bootstrap_compilation_request_handled();
       
  1403   }
       
  1404 }
       
  1405 
       
  1406 
       
  1407 // ------------------------------------------------------------------
       
  1408 JVMCI::CodeInstallResult JVMCIRuntime::register_method(JVMCIEnv* JVMCIENV,
       
  1409                                 const methodHandle& method,
       
  1410                                 nmethod*& nm,
       
  1411                                 int entry_bci,
       
  1412                                 CodeOffsets* offsets,
       
  1413                                 int orig_pc_offset,
       
  1414                                 CodeBuffer* code_buffer,
       
  1415                                 int frame_words,
       
  1416                                 OopMapSet* oop_map_set,
       
  1417                                 ExceptionHandlerTable* handler_table,
       
  1418                                 AbstractCompiler* compiler,
       
  1419                                 DebugInformationRecorder* debug_info,
       
  1420                                 Dependencies* dependencies,
       
  1421                                 int compile_id,
       
  1422                                 bool has_unsafe_access,
       
  1423                                 bool has_wide_vector,
       
  1424                                 JVMCIObject compiled_code,
       
  1425                                 JVMCIObject nmethod_mirror,
       
  1426                                 FailedSpeculation** failed_speculations,
       
  1427                                 char* speculations,
       
  1428                                 int speculations_len) {
       
  1429   JVMCI_EXCEPTION_CONTEXT;
       
  1430   nm = NULL;
       
  1431   int comp_level = CompLevel_full_optimization;
       
  1432   char* failure_detail = NULL;
       
  1433 
       
  1434   bool install_default = JVMCIENV->get_HotSpotNmethod_isDefault(nmethod_mirror) != 0;
       
  1435   assert(JVMCIENV->isa_HotSpotNmethod(nmethod_mirror), "must be");
       
  1436   JVMCIObject name = JVMCIENV->get_InstalledCode_name(nmethod_mirror);
       
  1437   const char* nmethod_mirror_name = name.is_null() ? NULL : JVMCIENV->as_utf8_string(name);
       
  1438   int nmethod_mirror_index;
       
  1439   if (!install_default) {
       
  1440     // Reserve or initialize mirror slot in the oops table.
       
  1441     OopRecorder* oop_recorder = debug_info->oop_recorder();
       
  1442     nmethod_mirror_index = oop_recorder->allocate_oop_index(nmethod_mirror.is_hotspot() ? nmethod_mirror.as_jobject() : NULL);
       
  1443   } else {
       
  1444     // A default HotSpotNmethod mirror is never tracked by the nmethod
       
  1445     nmethod_mirror_index = -1;
       
  1446   }
       
  1447 
       
  1448   JVMCI::CodeInstallResult result;
       
  1449   {
       
  1450     // To prevent compile queue updates.
       
  1451     MutexLocker locker(MethodCompileQueue_lock, THREAD);
       
  1452 
       
  1453     // Prevent SystemDictionary::add_to_hierarchy from running
       
  1454     // and invalidating our dependencies until we install this method.
       
  1455     MutexLocker ml(Compile_lock);
       
  1456 
       
  1457     // Encode the dependencies now, so we can check them right away.
       
  1458     dependencies->encode_content_bytes();
       
  1459 
       
  1460     // Record the dependencies for the current compile in the log
       
  1461     if (LogCompilation) {
       
  1462       for (Dependencies::DepStream deps(dependencies); deps.next(); ) {
       
  1463         deps.log_dependency();
       
  1464       }
       
  1465     }
       
  1466 
       
  1467     // Check for {class loads, evolution, breakpoints} during compilation
       
  1468     result = validate_compile_task_dependencies(dependencies, JVMCIENV->compile_state(), &failure_detail);
       
  1469     if (result != JVMCI::ok) {
       
  1470       // While not a true deoptimization, it is a preemptive decompile.
       
  1471       MethodData* mdp = method()->method_data();
       
  1472       if (mdp != NULL) {
       
  1473         mdp->inc_decompile_count();
       
  1474 #ifdef ASSERT
       
  1475         if (mdp->decompile_count() > (uint)PerMethodRecompilationCutoff) {
       
  1476           ResourceMark m;
       
  1477           tty->print_cr("WARN: endless recompilation of %s. Method was set to not compilable.", method()->name_and_sig_as_C_string());
       
  1478         }
       
  1479 #endif
       
  1480       }
       
  1481 
       
  1482       // All buffers in the CodeBuffer are allocated in the CodeCache.
       
  1483       // If the code buffer is created on each compile attempt
       
  1484       // as in C2, then it must be freed.
       
  1485       //code_buffer->free_blob();
       
  1486     } else {
       
  1487       ImplicitExceptionTable implicit_tbl;
       
  1488       nm =  nmethod::new_nmethod(method,
       
  1489                                  compile_id,
       
  1490                                  entry_bci,
       
  1491                                  offsets,
       
  1492                                  orig_pc_offset,
       
  1493                                  debug_info, dependencies, code_buffer,
       
  1494                                  frame_words, oop_map_set,
       
  1495                                  handler_table, &implicit_tbl,
       
  1496                                  compiler, comp_level,
       
  1497                                  speculations, speculations_len,
       
  1498                                  nmethod_mirror_index, nmethod_mirror_name, failed_speculations);
       
  1499 
       
  1500 
       
  1501       // Free codeBlobs
       
  1502       if (nm == NULL) {
       
  1503         // The CodeCache is full.  Print out warning and disable compilation.
       
  1504         {
       
  1505           MutexUnlocker ml(Compile_lock);
       
  1506           MutexUnlocker locker(MethodCompileQueue_lock);
       
  1507           CompileBroker::handle_full_code_cache(CodeCache::get_code_blob_type(comp_level));
       
  1508         }
       
  1509       } else {
       
  1510         nm->set_has_unsafe_access(has_unsafe_access);
       
  1511         nm->set_has_wide_vectors(has_wide_vector);
       
  1512 
       
  1513         // Record successful registration.
       
  1514         // (Put nm into the task handle *before* publishing to the Java heap.)
       
  1515         if (JVMCIENV->compile_state() != NULL) {
       
  1516           JVMCIENV->compile_state()->task()->set_code(nm);
       
  1517         }
       
  1518 
       
  1519         JVMCINMethodData* data = nm->jvmci_nmethod_data();
       
  1520         assert(data != NULL, "must be");
       
  1521         if (install_default) {
       
  1522           assert(!nmethod_mirror.is_hotspot() || data->get_nmethod_mirror(nm) == NULL, "must be");
       
  1523           if (entry_bci == InvocationEntryBci) {
       
  1524             if (TieredCompilation) {
       
  1525               // If there is an old version we're done with it
       
  1526               CompiledMethod* old = method->code();
       
  1527               if (TraceMethodReplacement && old != NULL) {
       
  1528                 ResourceMark rm;
       
  1529                 char *method_name = method->name_and_sig_as_C_string();
       
  1530                 tty->print_cr("Replacing method %s", method_name);
       
  1531               }
       
  1532               if (old != NULL ) {
       
  1533                 old->make_not_entrant();
       
  1534               }
       
  1535             }
       
  1536             if (TraceNMethodInstalls) {
       
  1537               ResourceMark rm;
       
  1538               char *method_name = method->name_and_sig_as_C_string();
       
  1539               ttyLocker ttyl;
       
  1540               tty->print_cr("Installing method (%d) %s [entry point: %p]",
       
  1541                             comp_level,
       
  1542                             method_name, nm->entry_point());
       
  1543             }
       
  1544             // Allow the code to be executed
       
  1545             method->set_code(method, nm);
       
  1546           } else {
       
  1547             if (TraceNMethodInstalls ) {
       
  1548               ResourceMark rm;
       
  1549               char *method_name = method->name_and_sig_as_C_string();
       
  1550               ttyLocker ttyl;
       
  1551               tty->print_cr("Installing osr method (%d) %s @ %d",
       
  1552                             comp_level,
       
  1553                             method_name,
       
  1554                             entry_bci);
       
  1555             }
       
  1556             InstanceKlass::cast(method->method_holder())->add_osr_nmethod(nm);
       
  1557           }
       
  1558         } else {
       
  1559           assert(!nmethod_mirror.is_hotspot() || data->get_nmethod_mirror(nm) == HotSpotJVMCI::resolve(nmethod_mirror), "must be");
       
  1560         }
       
  1561         nm->make_in_use();
       
  1562       }
       
  1563       result = nm != NULL ? JVMCI::ok :JVMCI::cache_full;
       
  1564     }
       
  1565   }
       
  1566 
       
  1567   // String creation must be done outside lock
       
  1568   if (failure_detail != NULL) {
       
  1569     // A failure to allocate the string is silently ignored.
       
  1570     JVMCIObject message = JVMCIENV->create_string(failure_detail, JVMCIENV);
       
  1571     JVMCIENV->set_HotSpotCompiledNmethod_installationFailureMessage(compiled_code, message);
       
  1572   }
       
  1573 
       
  1574   // JVMTI -- compiled method notification (must be done outside lock)
       
  1575   if (nm != NULL) {
       
  1576     nm->post_compiled_method_load_event();
       
  1577   }
       
  1578 
       
  1579   return result;
       
  1580 }