1 /*
   2  * Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "classfile/classLoaderData.inline.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/stringTable.hpp"
  28 #include "classfile/symbolTable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmClasses.hpp"
  31 #include "code/scopeDesc.hpp"
  32 #include "compiler/compileBroker.hpp"
  33 #include "compiler/compilerEvent.hpp"
  34 #include "compiler/disassembler.hpp"
  35 #include "compiler/oopMap.hpp"
  36 #include "interpreter/linkResolver.hpp"
  37 #include "interpreter/bytecodeStream.hpp"
  38 #include "jfr/jfrEvents.hpp"
  39 #include "jvmci/jvmciCompilerToVM.hpp"
  40 #include "jvmci/jvmciCodeInstaller.hpp"
  41 #include "jvmci/jvmciRuntime.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logTag.hpp"
  44 #include "memory/oopFactory.hpp"
  45 #include "memory/universe.hpp"
  46 #include "oops/constantPool.inline.hpp"
  47 #include "oops/instanceMirrorKlass.hpp"
  48 #include "oops/instanceKlass.inline.hpp"
  49 #include "oops/method.inline.hpp"
  50 #include "oops/typeArrayOop.inline.hpp"
  51 #include "prims/jvmtiExport.hpp"
  52 #include "prims/methodHandles.hpp"
  53 #include "prims/nativeLookup.hpp"
  54 #include "runtime/atomic.hpp"
  55 #include "runtime/deoptimization.hpp"
  56 #include "runtime/fieldDescriptor.inline.hpp"
  57 #include "runtime/frame.inline.hpp"
  58 #include "runtime/globals_extension.hpp"
  59 #include "runtime/interfaceSupport.inline.hpp"
  60 #include "runtime/jniHandles.inline.hpp"
  61 #include "runtime/reflectionUtils.hpp"
  62 #include "runtime/stackFrameStream.inline.hpp"
  63 #include "runtime/timerTrace.hpp"
  64 #include "runtime/vframe_hp.hpp"
  65 #include "runtime/vframe.inline.hpp"
  66 
  67 JVMCIKlassHandle::JVMCIKlassHandle(Thread* thread, Klass* klass) {
  68   _thread = thread;
  69   _klass = klass;
  70   if (klass != NULL) {
  71     _holder = Handle(_thread, klass->klass_holder());
  72   }
  73 }
  74 
  75 JVMCIKlassHandle& JVMCIKlassHandle::operator=(Klass* klass) {
  76   _klass = klass;
  77   if (klass != NULL) {
  78     _holder = Handle(_thread, klass->klass_holder());
  79   }
  80   return *this;
  81 }
  82 
  83 static void requireInHotSpot(const char* caller, JVMCI_TRAPS) {
  84   if (!JVMCIENV->is_hotspot()) {
  85     JVMCI_THROW_MSG(IllegalStateException, err_msg("Cannot call %s from JVMCI shared library", caller));
  86   }
  87 }
  88 
  89 void JNIHandleMark::push_jni_handle_block(JavaThread* thread) {
  90   if (thread != NULL) {
  91     // Allocate a new block for JNI handles.
  92     // Inlined code from jni_PushLocalFrame()
  93     JNIHandleBlock* java_handles = thread->active_handles();
  94     JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread);
  95     assert(compile_handles != NULL && java_handles != NULL, "should not be NULL");
  96     compile_handles->set_pop_frame_link(java_handles);
  97     thread->set_active_handles(compile_handles);
  98   }
  99 }
 100 
 101 void JNIHandleMark::pop_jni_handle_block(JavaThread* thread) {
 102   if (thread != NULL) {
 103     // Release our JNI handle block
 104     JNIHandleBlock* compile_handles = thread->active_handles();
 105     JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
 106     thread->set_active_handles(java_handles);
 107     compile_handles->set_pop_frame_link(NULL);
 108     JNIHandleBlock::release_block(compile_handles, thread); // may block
 109   }
 110 }
 111 
 112 class JVMCITraceMark : public StackObj {
 113   const char* _msg;
 114  public:
 115   JVMCITraceMark(const char* msg) {
 116     _msg = msg;
 117     JVMCI_event_2("Enter %s", _msg);
 118   }
 119   ~JVMCITraceMark() {
 120     JVMCI_event_2(" Exit %s", _msg);
 121   }
 122 };
 123 
 124 
 125 Handle JavaArgumentUnboxer::next_arg(BasicType expectedType) {
 126   assert(_index < _args->length(), "out of bounds");
 127   oop arg=((objArrayOop) (_args))->obj_at(_index++);
 128   assert(expectedType == T_OBJECT || java_lang_boxing_object::is_instance(arg, expectedType), "arg type mismatch");
 129   return Handle(Thread::current(), arg);
 130 }
 131 
 132 // Bring the JVMCI compiler thread into the VM state.
 133 #define JVMCI_VM_ENTRY_MARK                                       \
 134   MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));       \
 135   ThreadInVMfromNative __tiv(thread);                             \
 136   HandleMarkCleaner __hm(thread);                                 \
 137   JavaThread* THREAD = thread;                                        \
 138   debug_only(VMNativeEntryWrapper __vew;)
 139 
 140 // Native method block that transitions current thread to '_thread_in_vm'.
 141 #define C2V_BLOCK(result_type, name, signature)      \
 142   JVMCI_VM_ENTRY_MARK;                               \
 143   ResourceMark rm;                                   \
 144   JNI_JVMCIENV(JVMCI::compilation_tick(thread), env);
 145 
 146 static JavaThread* get_current_thread(bool allow_null=true) {
 147   Thread* thread = Thread::current_or_null_safe();
 148   if (thread == NULL) {
 149     assert(allow_null, "npe");
 150     return NULL;
 151   }
 152   return JavaThread::cast(thread);
 153 }
 154 
 155 // Entry to native method implementation that transitions
 156 // current thread to '_thread_in_vm'.
 157 #define C2V_VMENTRY(result_type, name, signature)        \
 158   JNIEXPORT result_type JNICALL c2v_ ## name signature { \
 159   JavaThread* thread = get_current_thread();             \
 160   if (thread == NULL) {                                  \
 161     env->ThrowNew(JNIJVMCI::InternalError::clazz(),      \
 162         err_msg("Cannot call into HotSpot from JVMCI shared library without attaching current thread")); \
 163     return;                                              \
 164   }                                                      \
 165   JVMCITraceMark jtm("CompilerToVM::" #name);            \
 166   C2V_BLOCK(result_type, name, signature)
 167 
 168 #define C2V_VMENTRY_(result_type, name, signature, result) \
 169   JNIEXPORT result_type JNICALL c2v_ ## name signature { \
 170   JavaThread* thread = get_current_thread();             \
 171   if (thread == NULL) {                                  \
 172     env->ThrowNew(JNIJVMCI::InternalError::clazz(),      \
 173         err_msg("Cannot call into HotSpot from JVMCI shared library without attaching current thread")); \
 174     return result;                                       \
 175   }                                                      \
 176   JVMCITraceMark jtm("CompilerToVM::" #name);            \
 177   C2V_BLOCK(result_type, name, signature)
 178 
 179 #define C2V_VMENTRY_NULL(result_type, name, signature) C2V_VMENTRY_(result_type, name, signature, NULL)
 180 #define C2V_VMENTRY_0(result_type, name, signature) C2V_VMENTRY_(result_type, name, signature, 0)
 181 
 182 // Entry to native method implementation that does not transition
 183 // current thread to '_thread_in_vm'.
 184 #define C2V_VMENTRY_PREFIX(result_type, name, signature) \
 185   JNIEXPORT result_type JNICALL c2v_ ## name signature { \
 186   JavaThread* thread = get_current_thread();
 187 
 188 #define C2V_END }
 189 
 190 #define JNI_THROW(caller, name, msg) do {                                         \
 191     jint __throw_res = env->ThrowNew(JNIJVMCI::name::clazz(), msg);               \
 192     if (__throw_res != JNI_OK) {                                                  \
 193       tty->print_cr("Throwing " #name " in " caller " returned %d", __throw_res); \
 194     }                                                                             \
 195     return;                                                                       \
 196   } while (0);
 197 
 198 #define JNI_THROW_(caller, name, msg, result) do {                                \
 199     jint __throw_res = env->ThrowNew(JNIJVMCI::name::clazz(), msg);               \
 200     if (__throw_res != JNI_OK) {                                                  \
 201       tty->print_cr("Throwing " #name " in " caller " returned %d", __throw_res); \
 202     }                                                                             \
 203     return result;                                                                \
 204   } while (0)
 205 
 206 jobjectArray readConfiguration0(JNIEnv *env, JVMCI_TRAPS);
 207 
 208 C2V_VMENTRY_NULL(jobjectArray, readConfiguration, (JNIEnv* env))
 209   jobjectArray config = readConfiguration0(env, JVMCI_CHECK_NULL);
 210   return config;
 211 }
 212 
 213 C2V_VMENTRY_NULL(jobject, getFlagValue, (JNIEnv* env, jobject c2vm, jobject name_handle))
 214 #define RETURN_BOXED_LONG(value) jvalue p; p.j = (jlong) (value); JVMCIObject box = JVMCIENV->create_box(T_LONG, &p, JVMCI_CHECK_NULL); return box.as_jobject();
 215 #define RETURN_BOXED_DOUBLE(value) jvalue p; p.d = (jdouble) (value); JVMCIObject box = JVMCIENV->create_box(T_DOUBLE, &p, JVMCI_CHECK_NULL); return box.as_jobject();
 216   JVMCIObject name = JVMCIENV->wrap(name_handle);
 217   if (name.is_null()) {
 218     JVMCI_THROW_NULL(NullPointerException);
 219   }
 220   const char* cstring = JVMCIENV->as_utf8_string(name);
 221   const JVMFlag* flag = JVMFlag::find_declared_flag(cstring);
 222   if (flag == NULL) {
 223     return c2vm;
 224   }
 225   if (flag->is_bool()) {
 226     jvalue prim;
 227     prim.z = flag->get_bool();
 228     JVMCIObject box = JVMCIENV->create_box(T_BOOLEAN, &prim, JVMCI_CHECK_NULL);
 229     return JVMCIENV->get_jobject(box);
 230   } else if (flag->is_ccstr()) {
 231     JVMCIObject value = JVMCIENV->create_string(flag->get_ccstr(), JVMCI_CHECK_NULL);
 232     return JVMCIENV->get_jobject(value);
 233   } else if (flag->is_intx()) {
 234     RETURN_BOXED_LONG(flag->get_intx());
 235   } else if (flag->is_int()) {
 236     RETURN_BOXED_LONG(flag->get_int());
 237   } else if (flag->is_uint()) {
 238     RETURN_BOXED_LONG(flag->get_uint());
 239   } else if (flag->is_uint64_t()) {
 240     RETURN_BOXED_LONG(flag->get_uint64_t());
 241   } else if (flag->is_size_t()) {
 242     RETURN_BOXED_LONG(flag->get_size_t());
 243   } else if (flag->is_uintx()) {
 244     RETURN_BOXED_LONG(flag->get_uintx());
 245   } else if (flag->is_double()) {
 246     RETURN_BOXED_DOUBLE(flag->get_double());
 247   } else {
 248     JVMCI_ERROR_NULL("VM flag %s has unsupported type %s", flag->name(), flag->type_string());
 249   }
 250 #undef RETURN_BOXED_LONG
 251 #undef RETURN_BOXED_DOUBLE
 252 C2V_END
 253 
 254 C2V_VMENTRY_NULL(jbyteArray, getBytecode, (JNIEnv* env, jobject, jobject jvmci_method))
 255   methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));
 256 
 257   int code_size = method->code_size();
 258   jbyte* reconstituted_code = NEW_RESOURCE_ARRAY(jbyte, code_size);
 259 
 260   guarantee(method->method_holder()->is_rewritten(), "Method's holder should be rewritten");
 261   // iterate over all bytecodes and replace non-Java bytecodes
 262 
 263   for (BytecodeStream s(method); s.next() != Bytecodes::_illegal; ) {
 264     Bytecodes::Code code = s.code();
 265     Bytecodes::Code raw_code = s.raw_code();
 266     int bci = s.bci();
 267     int len = s.instruction_size();
 268 
 269     // Restore original byte code.
 270     reconstituted_code[bci] =  (jbyte) (s.is_wide()? Bytecodes::_wide : code);
 271     if (len > 1) {
 272       memcpy(reconstituted_code + (bci + 1), s.bcp()+1, len-1);
 273     }
 274 
 275     if (len > 1) {
 276       // Restore the big-endian constant pool indexes.
 277       // Cf. Rewriter::scan_method
 278       switch (code) {
 279         case Bytecodes::_getstatic:
 280         case Bytecodes::_putstatic:
 281         case Bytecodes::_getfield:
 282         case Bytecodes::_putfield:
 283         case Bytecodes::_invokevirtual:
 284         case Bytecodes::_invokespecial:
 285         case Bytecodes::_invokestatic:
 286         case Bytecodes::_invokeinterface:
 287         case Bytecodes::_invokehandle: {
 288           int cp_index = Bytes::get_native_u2((address) reconstituted_code + (bci + 1));
 289           Bytes::put_Java_u2((address) reconstituted_code + (bci + 1), (u2) cp_index);
 290           break;
 291         }
 292 
 293         case Bytecodes::_invokedynamic: {
 294           int cp_index = Bytes::get_native_u4((address) reconstituted_code + (bci + 1));
 295           Bytes::put_Java_u4((address) reconstituted_code + (bci + 1), (u4) cp_index);
 296           break;
 297         }
 298 
 299         default:
 300           break;
 301       }
 302 
 303       // Not all ldc byte code are rewritten.
 304       switch (raw_code) {
 305         case Bytecodes::_fast_aldc: {
 306           int cpc_index = reconstituted_code[bci + 1] & 0xff;
 307           int cp_index = method->constants()->object_to_cp_index(cpc_index);
 308           assert(cp_index < method->constants()->length(), "sanity check");
 309           reconstituted_code[bci + 1] = (jbyte) cp_index;
 310           break;
 311         }
 312 
 313         case Bytecodes::_fast_aldc_w: {
 314           int cpc_index = Bytes::get_native_u2((address) reconstituted_code + (bci + 1));
 315           int cp_index = method->constants()->object_to_cp_index(cpc_index);
 316           assert(cp_index < method->constants()->length(), "sanity check");
 317           Bytes::put_Java_u2((address) reconstituted_code + (bci + 1), (u2) cp_index);
 318           break;
 319         }
 320 
 321         default:
 322           break;
 323       }
 324     }
 325   }
 326 
 327   JVMCIPrimitiveArray result = JVMCIENV->new_byteArray(code_size, JVMCI_CHECK_NULL);
 328   JVMCIENV->copy_bytes_from(reconstituted_code, result, 0, code_size);
 329   return JVMCIENV->get_jbyteArray(result);
 330 C2V_END
 331 
 332 C2V_VMENTRY_0(jint, getExceptionTableLength, (JNIEnv* env, jobject, jobject jvmci_method))
 333   Method* method = JVMCIENV->asMethod(jvmci_method);
 334   return method->exception_table_length();
 335 C2V_END
 336 
 337 C2V_VMENTRY_0(jlong, getExceptionTableStart, (JNIEnv* env, jobject, jobject jvmci_method))
 338   Method* method = JVMCIENV->asMethod(jvmci_method);
 339   if (method->exception_table_length() == 0) {
 340     return 0L;
 341   }
 342   return (jlong) (address) method->exception_table_start();
 343 C2V_END
 344 
 345 C2V_VMENTRY_NULL(jobject, asResolvedJavaMethod, (JNIEnv* env, jobject, jobject executable_handle))
 346   requireInHotSpot("asResolvedJavaMethod", JVMCI_CHECK_NULL);
 347   oop executable = JNIHandles::resolve(executable_handle);
 348   oop mirror = NULL;
 349   int slot = 0;
 350 
 351   if (executable->klass() == vmClasses::reflect_Constructor_klass()) {
 352     mirror = java_lang_reflect_Constructor::clazz(executable);
 353     slot = java_lang_reflect_Constructor::slot(executable);
 354   } else {
 355     assert(executable->klass() == vmClasses::reflect_Method_klass(), "wrong type");
 356     mirror = java_lang_reflect_Method::clazz(executable);
 357     slot = java_lang_reflect_Method::slot(executable);
 358   }
 359   Klass* holder = java_lang_Class::as_Klass(mirror);
 360   methodHandle method (THREAD, InstanceKlass::cast(holder)->method_with_idnum(slot));
 361   JVMCIObject result = JVMCIENV->get_jvmci_method(method, JVMCI_CHECK_NULL);
 362   return JVMCIENV->get_jobject(result);
 363 }
 364 
 365 C2V_VMENTRY_NULL(jobject, getResolvedJavaMethod, (JNIEnv* env, jobject, jobject base, jlong offset))
 366   Method* method = NULL;
 367   JVMCIObject base_object = JVMCIENV->wrap(base);
 368   if (base_object.is_null()) {
 369     method = *((Method**)(offset));
 370   } else if (JVMCIENV->isa_HotSpotObjectConstantImpl(base_object)) {
 371     Handle obj = JVMCIENV->asConstant(base_object, JVMCI_CHECK_NULL);
 372     if (obj->is_a(vmClasses::ResolvedMethodName_klass())) {
 373       method = (Method*) (intptr_t) obj->long_field(offset);
 374     } else {
 375       JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Unexpected type: %s", obj->klass()->external_name()));
 376     }
 377   } else if (JVMCIENV->isa_HotSpotResolvedJavaMethodImpl(base_object)) {
 378     method = JVMCIENV->asMethod(base_object);
 379   }
 380   if (method == NULL) {
 381     JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Unexpected type: %s", JVMCIENV->klass_name(base_object)));
 382   }
 383   assert (method->is_method(), "invalid read");
 384   JVMCIObject result = JVMCIENV->get_jvmci_method(methodHandle(THREAD, method), JVMCI_CHECK_NULL);
 385   return JVMCIENV->get_jobject(result);
 386 }
 387 
 388 C2V_VMENTRY_NULL(jobject, getConstantPool, (JNIEnv* env, jobject, jobject object_handle))
 389   ConstantPool* cp = NULL;
 390   JVMCIObject object = JVMCIENV->wrap(object_handle);
 391   if (object.is_null()) {
 392     JVMCI_THROW_NULL(NullPointerException);
 393   }
 394   if (JVMCIENV->isa_HotSpotResolvedJavaMethodImpl(object)) {
 395     cp = JVMCIENV->asMethod(object)->constMethod()->constants();
 396   } else if (JVMCIENV->isa_HotSpotResolvedObjectTypeImpl(object)) {
 397     cp = InstanceKlass::cast(JVMCIENV->asKlass(object))->constants();
 398   } else {
 399     JVMCI_THROW_MSG_NULL(IllegalArgumentException,
 400                 err_msg("Unexpected type: %s", JVMCIENV->klass_name(object)));
 401   }
 402   assert(cp != NULL, "npe");
 403 
 404   JVMCIObject result = JVMCIENV->get_jvmci_constant_pool(constantPoolHandle(THREAD, cp), JVMCI_CHECK_NULL);
 405   return JVMCIENV->get_jobject(result);
 406 }
 407 
 408 C2V_VMENTRY_NULL(jobject, getResolvedJavaType0, (JNIEnv* env, jobject, jobject base, jlong offset, jboolean compressed))
 409   JVMCIKlassHandle klass(THREAD);
 410   JVMCIObject base_object = JVMCIENV->wrap(base);
 411   jlong base_address = 0;
 412   if (base_object.is_non_null() && offset == oopDesc::klass_offset_in_bytes()) {
 413     // klass = JVMCIENV->unhandle(base_object)->klass();
 414     if (JVMCIENV->isa_HotSpotObjectConstantImpl(base_object)) {
 415       Handle base_oop = JVMCIENV->asConstant(base_object, JVMCI_CHECK_NULL);
 416       klass = base_oop->klass();
 417     } else {
 418       assert(false, "What types are we actually expecting here?");
 419     }
 420   } else if (!compressed) {
 421     if (base_object.is_non_null()) {
 422       if (JVMCIENV->isa_HotSpotResolvedJavaMethodImpl(base_object)) {
 423         base_address = (intptr_t) JVMCIENV->asMethod(base_object);
 424       } else if (JVMCIENV->isa_HotSpotConstantPool(base_object)) {
 425         base_address = (intptr_t) JVMCIENV->asConstantPool(base_object);
 426       } else if (JVMCIENV->isa_HotSpotResolvedObjectTypeImpl(base_object)) {
 427         base_address = (intptr_t) JVMCIENV->asKlass(base_object);
 428       } else if (JVMCIENV->isa_HotSpotObjectConstantImpl(base_object)) {
 429         Handle base_oop = JVMCIENV->asConstant(base_object, JVMCI_CHECK_NULL);
 430         if (base_oop->is_a(vmClasses::Class_klass())) {
 431           base_address = cast_from_oop<jlong>(base_oop());
 432         }
 433       }
 434       if (base_address == 0) {
 435         JVMCI_THROW_MSG_NULL(IllegalArgumentException,
 436                     err_msg("Unexpected arguments: %s " JLONG_FORMAT " %s", JVMCIENV->klass_name(base_object), offset, compressed ? "true" : "false"));
 437       }
 438     }
 439     klass = *((Klass**) (intptr_t) (base_address + offset));
 440   } else {
 441     JVMCI_THROW_MSG_NULL(IllegalArgumentException,
 442                 err_msg("Unexpected arguments: %s " JLONG_FORMAT " %s",
 443                         base_object.is_non_null() ? JVMCIENV->klass_name(base_object) : "null",
 444                         offset, compressed ? "true" : "false"));
 445   }
 446   assert (klass == NULL || klass->is_klass(), "invalid read");
 447   JVMCIObject result = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL);
 448   return JVMCIENV->get_jobject(result);
 449 }
 450 
 451 C2V_VMENTRY_NULL(jobject, findUniqueConcreteMethod, (JNIEnv* env, jobject, jobject jvmci_type, jobject jvmci_method))
 452   methodHandle method (THREAD, JVMCIENV->asMethod(jvmci_method));
 453   InstanceKlass* holder = InstanceKlass::cast(JVMCIENV->asKlass(jvmci_type));
 454   if (holder->is_interface()) {
 455     JVMCI_THROW_MSG_NULL(InternalError, err_msg("Interface %s should be handled in Java code", holder->external_name()));
 456   }
 457   if (method->can_be_statically_bound()) {
 458     JVMCI_THROW_MSG_NULL(InternalError, err_msg("Effectively static method %s.%s should be handled in Java code", method->method_holder()->external_name(), method->external_name()));
 459   }
 460 
 461   methodHandle ucm;
 462   {
 463     MutexLocker locker(Compile_lock);
 464     ucm = methodHandle(THREAD, Dependencies::find_unique_concrete_method(holder, method()));
 465   }
 466   JVMCIObject result = JVMCIENV->get_jvmci_method(ucm, JVMCI_CHECK_NULL);
 467   return JVMCIENV->get_jobject(result);
 468 C2V_END
 469 
 470 C2V_VMENTRY_NULL(jobject, getImplementor, (JNIEnv* env, jobject, jobject jvmci_type))
 471   Klass* klass = JVMCIENV->asKlass(jvmci_type);
 472   if (!klass->is_interface()) {
 473     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
 474         err_msg("Expected interface type, got %s", klass->external_name()));
 475   }
 476   InstanceKlass* iklass = InstanceKlass::cast(klass);
 477   JVMCIKlassHandle handle(THREAD);
 478   {
 479     // Need Compile_lock around implementor()
 480     MutexLocker locker(Compile_lock);
 481     handle = iklass->implementor();
 482   }
 483   JVMCIObject implementor = JVMCIENV->get_jvmci_type(handle, JVMCI_CHECK_NULL);
 484   return JVMCIENV->get_jobject(implementor);
 485 C2V_END
 486 
 487 C2V_VMENTRY_0(jboolean, methodIsIgnoredBySecurityStackWalk,(JNIEnv* env, jobject, jobject jvmci_method))
 488   Method* method = JVMCIENV->asMethod(jvmci_method);
 489   return method->is_ignored_by_security_stack_walk();
 490 C2V_END
 491 
 492 C2V_VMENTRY_0(jboolean, isCompilable,(JNIEnv* env, jobject, jobject jvmci_method))
 493   Method* method = JVMCIENV->asMethod(jvmci_method);
 494   // Skip redefined methods
 495   if (method->is_old()) {
 496     return false;
 497   }
 498   return !method->is_not_compilable(CompLevel_full_optimization);
 499 C2V_END
 500 
 501 C2V_VMENTRY_0(jboolean, hasNeverInlineDirective,(JNIEnv* env, jobject, jobject jvmci_method))
 502   methodHandle method (THREAD, JVMCIENV->asMethod(jvmci_method));
 503   return !Inline || CompilerOracle::should_not_inline(method) || method->dont_inline();
 504 C2V_END
 505 
 506 C2V_VMENTRY_0(jboolean, shouldInlineMethod,(JNIEnv* env, jobject, jobject jvmci_method))
 507   methodHandle method (THREAD, JVMCIENV->asMethod(jvmci_method));
 508   return CompilerOracle::should_inline(method) || method->force_inline();
 509 C2V_END
 510 
 511 C2V_VMENTRY_NULL(jobject, lookupType, (JNIEnv* env, jobject, jstring jname, jclass accessing_class, jboolean resolve))
 512   JVMCIObject name = JVMCIENV->wrap(jname);
 513   const char* str = JVMCIENV->as_utf8_string(name);
 514   TempNewSymbol class_name = SymbolTable::new_symbol(str);
 515 
 516   if (class_name->utf8_length() <= 1) {
 517     JVMCI_THROW_MSG_0(InternalError, err_msg("Primitive type %s should be handled in Java code", class_name->as_C_string()));
 518   }
 519 
 520   JVMCIKlassHandle resolved_klass(THREAD);
 521   Klass* accessing_klass = NULL;
 522   Handle class_loader;
 523   Handle protection_domain;
 524   if (accessing_class != NULL) {
 525     accessing_klass = JVMCIENV->asKlass(accessing_class);
 526     class_loader = Handle(THREAD, accessing_klass->class_loader());
 527     protection_domain = Handle(THREAD, accessing_klass->protection_domain());
 528   } else {
 529     // Use the System class loader
 530     class_loader = Handle(THREAD, SystemDictionary::java_system_loader());
 531     JVMCIENV->runtime()->initialize(JVMCIENV);
 532   }
 533 
 534   if (resolve) {
 535     resolved_klass = SystemDictionary::resolve_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
 536     if (resolved_klass == NULL) {
 537       JVMCI_THROW_MSG_NULL(ClassNotFoundException, str);
 538     }
 539   } else {
 540     if (Signature::has_envelope(class_name)) {
 541       // This is a name from a signature.  Strip off the trimmings.
 542       // Call recursive to keep scope of strippedsym.
 543       TempNewSymbol strippedsym = Signature::strip_envelope(class_name);
 544       resolved_klass = SystemDictionary::find_instance_klass(strippedsym,
 545                                                              class_loader,
 546                                                              protection_domain);
 547     } else if (Signature::is_array(class_name)) {
 548       SignatureStream ss(class_name, false);
 549       int ndim = ss.skip_array_prefix();
 550       if (ss.type() == T_OBJECT) {
 551         Symbol* strippedsym = ss.as_symbol();
 552         resolved_klass = SystemDictionary::find_instance_klass(strippedsym,
 553                                                                class_loader,
 554                                                                protection_domain);
 555         if (!resolved_klass.is_null()) {
 556           resolved_klass = resolved_klass->array_klass(ndim, CHECK_NULL);
 557         }
 558       } else {
 559         resolved_klass = TypeArrayKlass::cast(Universe::typeArrayKlassObj(ss.type()))->array_klass(ndim, CHECK_NULL);
 560       }
 561     } else {
 562       resolved_klass = SystemDictionary::find_instance_klass(class_name,
 563                                                              class_loader,
 564                                                              protection_domain);
 565     }
 566   }
 567   JVMCIObject result = JVMCIENV->get_jvmci_type(resolved_klass, JVMCI_CHECK_NULL);
 568   return JVMCIENV->get_jobject(result);
 569 C2V_END
 570 
 571 C2V_VMENTRY_NULL(jobject, getArrayType, (JNIEnv* env, jobject, jobject jvmci_type))
 572   if (jvmci_type == NULL) {
 573     JVMCI_THROW_0(NullPointerException);
 574   }
 575 
 576   JVMCIObject jvmci_type_object = JVMCIENV->wrap(jvmci_type);
 577   JVMCIKlassHandle array_klass(THREAD);
 578   if (JVMCIENV->isa_HotSpotResolvedPrimitiveType(jvmci_type_object)) {
 579     BasicType type = JVMCIENV->kindToBasicType(JVMCIENV->get_HotSpotResolvedPrimitiveType_kind(jvmci_type_object), JVMCI_CHECK_0);
 580     if (type == T_VOID) {
 581       return NULL;
 582     }
 583     array_klass = Universe::typeArrayKlassObj(type);
 584     if (array_klass == NULL) {
 585       JVMCI_THROW_MSG_NULL(InternalError, err_msg("No array klass for primitive type %s", type2name(type)));
 586     }
 587   } else {
 588     Klass* klass = JVMCIENV->asKlass(jvmci_type);
 589     if (klass == NULL) {
 590       JVMCI_THROW_0(NullPointerException);
 591     }
 592     array_klass = klass->array_klass(CHECK_NULL);
 593   }
 594   JVMCIObject result = JVMCIENV->get_jvmci_type(array_klass, JVMCI_CHECK_NULL);
 595   return JVMCIENV->get_jobject(result);
 596 C2V_END
 597 
 598 C2V_VMENTRY_NULL(jobject, lookupClass, (JNIEnv* env, jobject, jclass mirror))
 599   requireInHotSpot("lookupClass", JVMCI_CHECK_NULL);
 600   if (mirror == NULL) {
 601     return NULL;
 602   }
 603   JVMCIKlassHandle klass(THREAD);
 604   klass = java_lang_Class::as_Klass(JNIHandles::resolve(mirror));
 605   if (klass == NULL) {
 606     JVMCI_THROW_MSG_NULL(IllegalArgumentException, "Primitive classes are unsupported");
 607   }
 608   JVMCIObject result = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL);
 609   return JVMCIENV->get_jobject(result);
 610 }
 611 
 612 C2V_VMENTRY_NULL(jobject, resolvePossiblyCachedConstantInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))
 613   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 614   oop obj = cp->resolve_possibly_cached_constant_at(index, CHECK_NULL);
 615   constantTag tag = cp->tag_at(index);
 616   if (tag.is_dynamic_constant() || tag.is_dynamic_constant_in_error()) {
 617     if (obj == Universe::the_null_sentinel()) {
 618       return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_NULL_POINTER());
 619     }
 620     BasicType bt = Signature::basic_type(cp->uncached_signature_ref_at(index));
 621     if (!is_reference_type(bt)) {
 622       if (!is_java_primitive(bt)) {
 623         return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_ILLEGAL());
 624       }
 625 
 626       // Convert standard box (e.g. java.lang.Integer) to JVMCI box (e.g. jdk.vm.ci.meta.PrimitiveConstant)
 627       jvalue value;
 628       jlong raw_value;
 629       JVMCIObject kind;
 630       BasicType bt2 = java_lang_boxing_object::get_value(obj, &value);
 631       assert(bt2 == bt, "");
 632       switch (bt2) {
 633         case T_LONG:    kind = JVMCIENV->get_JavaKind_Long();    raw_value = value.j; break;
 634         case T_DOUBLE:  kind = JVMCIENV->get_JavaKind_Double();  raw_value = value.j; break;
 635         case T_FLOAT:   kind = JVMCIENV->get_JavaKind_Float();   raw_value = value.i; break;
 636         case T_INT:     kind = JVMCIENV->get_JavaKind_Int();     raw_value = value.i; break;
 637         case T_SHORT:   kind = JVMCIENV->get_JavaKind_Short();   raw_value = value.s; break;
 638         case T_BYTE:    kind = JVMCIENV->get_JavaKind_Byte();    raw_value = value.b; break;
 639         case T_CHAR:    kind = JVMCIENV->get_JavaKind_Char();    raw_value = value.c; break;
 640         case T_BOOLEAN: kind = JVMCIENV->get_JavaKind_Boolean(); raw_value = value.z; break;
 641         default:        return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_ILLEGAL());
 642       }
 643 
 644       JVMCIObject result = JVMCIENV->call_JavaConstant_forPrimitive(kind, raw_value, JVMCI_CHECK_NULL);
 645       return JVMCIENV->get_jobject(result);
 646     }
 647   }
 648   return JVMCIENV->get_jobject(JVMCIENV->get_object_constant(obj));
 649 C2V_END
 650 
 651 C2V_VMENTRY_0(jint, lookupNameAndTypeRefIndexInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))
 652   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 653   return cp->name_and_type_ref_index_at(index);
 654 C2V_END
 655 
 656 C2V_VMENTRY_NULL(jobject, lookupNameInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint which))
 657   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 658   JVMCIObject sym = JVMCIENV->create_string(cp->name_ref_at(which), JVMCI_CHECK_NULL);
 659   return JVMCIENV->get_jobject(sym);
 660 C2V_END
 661 
 662 C2V_VMENTRY_NULL(jobject, lookupSignatureInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint which))
 663   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 664   JVMCIObject sym = JVMCIENV->create_string(cp->signature_ref_at(which), JVMCI_CHECK_NULL);
 665   return JVMCIENV->get_jobject(sym);
 666 C2V_END
 667 
 668 C2V_VMENTRY_0(jint, lookupKlassRefIndexInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))
 669   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 670   return cp->klass_ref_index_at(index);
 671 C2V_END
 672 
 673 C2V_VMENTRY_NULL(jobject, resolveTypeInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))
 674   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 675   Klass* klass = cp->klass_at(index, CHECK_NULL);
 676   JVMCIKlassHandle resolved_klass(THREAD, klass);
 677   if (resolved_klass->is_instance_klass()) {
 678     InstanceKlass::cast(resolved_klass())->link_class(CHECK_NULL);
 679     if (!InstanceKlass::cast(resolved_klass())->is_linked()) {
 680       // link_class() should not return here if there is an issue.
 681       JVMCI_THROW_MSG_NULL(InternalError, err_msg("Class %s must be linked", resolved_klass()->external_name()));
 682     }
 683   }
 684   JVMCIObject klassObject = JVMCIENV->get_jvmci_type(resolved_klass, JVMCI_CHECK_NULL);
 685   return JVMCIENV->get_jobject(klassObject);
 686 C2V_END
 687 
 688 C2V_VMENTRY_NULL(jobject, lookupKlassInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index, jbyte opcode))
 689   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 690   Klass* loading_klass = cp->pool_holder();
 691   bool is_accessible = false;
 692   JVMCIKlassHandle klass(THREAD, JVMCIRuntime::get_klass_by_index(cp, index, is_accessible, loading_klass));
 693   Symbol* symbol = NULL;
 694   if (klass.is_null()) {
 695     constantTag tag = cp->tag_at(index);
 696     if (tag.is_klass()) {
 697       // The klass has been inserted into the constant pool
 698       // very recently.
 699       klass = cp->resolved_klass_at(index);
 700     } else if (tag.is_symbol()) {
 701       symbol = cp->symbol_at(index);
 702     } else {
 703       assert(cp->tag_at(index).is_unresolved_klass(), "wrong tag");
 704       symbol = cp->klass_name_at(index);
 705     }
 706   }
 707   JVMCIObject result;
 708   if (!klass.is_null()) {
 709     result = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL);
 710   } else {
 711     result = JVMCIENV->create_string(symbol, JVMCI_CHECK_NULL);
 712   }
 713   return JVMCIENV->get_jobject(result);
 714 C2V_END
 715 
 716 C2V_VMENTRY_NULL(jobject, lookupAppendixInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))
 717   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 718   oop appendix_oop = ConstantPool::appendix_at_if_loaded(cp, index);
 719   return JVMCIENV->get_jobject(JVMCIENV->get_object_constant(appendix_oop));
 720 C2V_END
 721 
 722 C2V_VMENTRY_NULL(jobject, lookupMethodInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index, jbyte opcode))
 723   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 724   InstanceKlass* pool_holder = cp->pool_holder();
 725   Bytecodes::Code bc = (Bytecodes::Code) (((int) opcode) & 0xFF);
 726   methodHandle method(THREAD, JVMCIRuntime::get_method_by_index(cp, index, bc, pool_holder));
 727   JVMCIObject result = JVMCIENV->get_jvmci_method(method, JVMCI_CHECK_NULL);
 728   return JVMCIENV->get_jobject(result);
 729 C2V_END
 730 
 731 C2V_VMENTRY_0(jint, constantPoolRemapInstructionOperandFromCache, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))
 732   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 733   return cp->remap_instruction_operand_from_cache(index);
 734 C2V_END
 735 
 736 C2V_VMENTRY_NULL(jobject, resolveFieldInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index, jobject jvmci_method, jbyte opcode, jintArray info_handle))
 737   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
 738   Bytecodes::Code code = (Bytecodes::Code)(((int) opcode) & 0xFF);
 739   fieldDescriptor fd;
 740   methodHandle mh(THREAD, (jvmci_method != NULL) ? JVMCIENV->asMethod(jvmci_method) : NULL);
 741   LinkInfo link_info(cp, index, mh, CHECK_NULL);
 742   LinkResolver::resolve_field(fd, link_info, Bytecodes::java_code(code), false, CHECK_NULL);
 743   JVMCIPrimitiveArray info = JVMCIENV->wrap(info_handle);
 744   if (info.is_null() || JVMCIENV->get_length(info) != 3) {
 745     JVMCI_ERROR_NULL("info must not be null and have a length of 3");
 746   }
 747   JVMCIENV->put_int_at(info, 0, fd.access_flags().as_int());
 748   JVMCIENV->put_int_at(info, 1, fd.offset());
 749   JVMCIENV->put_int_at(info, 2, fd.index());
 750   JVMCIKlassHandle handle(THREAD, fd.field_holder());
 751   JVMCIObject field_holder = JVMCIENV->get_jvmci_type(handle, JVMCI_CHECK_NULL);
 752   return JVMCIENV->get_jobject(field_holder);
 753 C2V_END
 754 
 755 C2V_VMENTRY_0(jint, getVtableIndexForInterfaceMethod, (JNIEnv* env, jobject, jobject jvmci_type, jobject jvmci_method))
 756   Klass* klass = JVMCIENV->asKlass(jvmci_type);
 757   methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));
 758   InstanceKlass* holder = method->method_holder();
 759   if (klass->is_interface()) {
 760     JVMCI_THROW_MSG_0(InternalError, err_msg("Interface %s should be handled in Java code", klass->external_name()));
 761   }
 762   if (!holder->is_interface()) {
 763     JVMCI_THROW_MSG_0(InternalError, err_msg("Method %s is not held by an interface, this case should be handled in Java code", method->name_and_sig_as_C_string()));
 764   }
 765   if (!klass->is_instance_klass()) {
 766     JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s must be instance klass", klass->external_name()));
 767   }
 768   if (!InstanceKlass::cast(klass)->is_linked()) {
 769     JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s must be linked", klass->external_name()));
 770   }
 771   if (!klass->is_subtype_of(holder)) {
 772     JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s does not implement interface %s", klass->external_name(), holder->external_name()));
 773   }
 774   return LinkResolver::vtable_index_of_interface_method(klass, method);
 775 C2V_END
 776 
 777 C2V_VMENTRY_NULL(jobject, resolveMethod, (JNIEnv* env, jobject, jobject receiver_jvmci_type, jobject jvmci_method, jobject caller_jvmci_type))
 778   Klass* recv_klass = JVMCIENV->asKlass(receiver_jvmci_type);
 779   Klass* caller_klass = JVMCIENV->asKlass(caller_jvmci_type);
 780   methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));
 781 
 782   Klass* resolved     = method->method_holder();
 783   Symbol* h_name      = method->name();
 784   Symbol* h_signature = method->signature();
 785 
 786   if (MethodHandles::is_signature_polymorphic_method(method())) {
 787       // Signature polymorphic methods are already resolved, JVMCI just returns NULL in this case.
 788       return NULL;
 789   }
 790 
 791   if (method->name() == vmSymbols::clone_name() &&
 792       resolved == vmClasses::Object_klass() &&
 793       recv_klass->is_array_klass()) {
 794     // Resolution of the clone method on arrays always returns Object.clone even though that method
 795     // has protected access.  There's some trickery in the access checking to make this all work out
 796     // so it's necessary to pass in the array class as the resolved class to properly trigger this.
 797     // Otherwise it's impossible to resolve the array clone methods through JVMCI.  See
 798     // LinkResolver::check_method_accessability for the matching logic.
 799     resolved = recv_klass;
 800   }
 801 
 802   LinkInfo link_info(resolved, h_name, h_signature, caller_klass);
 803   Method* m = NULL;
 804   // Only do exact lookup if receiver klass has been linked.  Otherwise,
 805   // the vtable has not been setup, and the LinkResolver will fail.
 806   if (recv_klass->is_array_klass() ||
 807       (InstanceKlass::cast(recv_klass)->is_linked() && !recv_klass->is_interface())) {
 808     if (resolved->is_interface()) {
 809       m = LinkResolver::resolve_interface_call_or_null(recv_klass, link_info);
 810     } else {
 811       m = LinkResolver::resolve_virtual_call_or_null(recv_klass, link_info);
 812     }
 813   }
 814 
 815   if (m == NULL) {
 816     // Return NULL if there was a problem with lookup (uninitialized class, etc.)
 817     return NULL;
 818   }
 819 
 820   JVMCIObject result = JVMCIENV->get_jvmci_method(methodHandle(THREAD, m), JVMCI_CHECK_NULL);
 821   return JVMCIENV->get_jobject(result);
 822 C2V_END
 823 
 824 C2V_VMENTRY_0(jboolean, hasFinalizableSubclass,(JNIEnv* env, jobject, jobject jvmci_type))
 825   Klass* klass = JVMCIENV->asKlass(jvmci_type);
 826   assert(klass != NULL, "method must not be called for primitive types");
 827   if (!klass->is_instance_klass()) {
 828     return false;
 829   }
 830   InstanceKlass* iklass = InstanceKlass::cast(klass);
 831   return Dependencies::find_finalizable_subclass(iklass) != NULL;
 832 C2V_END
 833 
 834 C2V_VMENTRY_NULL(jobject, getClassInitializer, (JNIEnv* env, jobject, jobject jvmci_type))
 835   Klass* klass = JVMCIENV->asKlass(jvmci_type);
 836   if (!klass->is_instance_klass()) {
 837     return NULL;
 838   }
 839   InstanceKlass* iklass = InstanceKlass::cast(klass);
 840   methodHandle clinit(THREAD, iklass->class_initializer());
 841   JVMCIObject result = JVMCIENV->get_jvmci_method(clinit, JVMCI_CHECK_NULL);
 842   return JVMCIENV->get_jobject(result);
 843 C2V_END
 844 
 845 C2V_VMENTRY_0(jlong, getMaxCallTargetOffset, (JNIEnv* env, jobject, jlong addr))
 846   address target_addr = (address) addr;
 847   if (target_addr != 0x0) {
 848     int64_t off_low = (int64_t)target_addr - ((int64_t)CodeCache::low_bound() + sizeof(int));
 849     int64_t off_high = (int64_t)target_addr - ((int64_t)CodeCache::high_bound() + sizeof(int));
 850     return MAX2(ABS(off_low), ABS(off_high));
 851   }
 852   return -1;
 853 C2V_END
 854 
 855 C2V_VMENTRY(void, setNotInlinableOrCompilable,(JNIEnv* env, jobject,  jobject jvmci_method))
 856   methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));
 857   method->set_not_c1_compilable();
 858   method->set_not_c2_compilable();
 859   method->set_dont_inline(true);
 860 C2V_END
 861 
 862 C2V_VMENTRY_0(jint, installCode, (JNIEnv *env, jobject, jobject target, jobject compiled_code,
 863             jobject installed_code, jlong failed_speculations_address, jbyteArray speculations_obj))
 864   HandleMark hm(THREAD);
 865   JNIHandleMark jni_hm(thread);
 866 
 867   JVMCIObject target_handle = JVMCIENV->wrap(target);
 868   JVMCIObject compiled_code_handle = JVMCIENV->wrap(compiled_code);
 869   CodeBlob* cb = NULL;
 870   JVMCIObject installed_code_handle = JVMCIENV->wrap(installed_code);
 871   JVMCIPrimitiveArray speculations_handle = JVMCIENV->wrap(speculations_obj);
 872 
 873   int speculations_len = JVMCIENV->get_length(speculations_handle);
 874   char* speculations = NEW_RESOURCE_ARRAY(char, speculations_len);
 875   JVMCIENV->copy_bytes_to(speculations_handle, (jbyte*) speculations, 0, speculations_len);
 876 
 877   JVMCICompiler* compiler = JVMCICompiler::instance(true, CHECK_JNI_ERR);
 878 
 879   TraceTime install_time("installCode", JVMCICompiler::codeInstallTimer(!thread->is_Compiler_thread()));
 880 
 881   nmethodLocker nmethod_handle;
 882   CodeInstaller installer(JVMCIENV);
 883   JVMCI::CodeInstallResult result = installer.install(compiler,
 884       target_handle,
 885       compiled_code_handle,
 886       cb,
 887       nmethod_handle,
 888       installed_code_handle,
 889       (FailedSpeculation**)(address) failed_speculations_address,
 890       speculations,
 891       speculations_len,
 892       JVMCI_CHECK_0);
 893 
 894   if (PrintCodeCacheOnCompilation) {
 895     stringStream s;
 896     // Dump code cache into a buffer before locking the tty,
 897     {
 898       MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 899       CodeCache::print_summary(&s, false);
 900     }
 901     ttyLocker ttyl;
 902     tty->print_raw_cr(s.as_string());
 903   }
 904 
 905   if (result != JVMCI::ok) {
 906     assert(cb == NULL, "should be");
 907   } else {
 908     if (installed_code_handle.is_non_null()) {
 909       if (cb->is_nmethod()) {
 910         assert(JVMCIENV->isa_HotSpotNmethod(installed_code_handle), "wrong type");
 911         // Clear the link to an old nmethod first
 912         JVMCIObject nmethod_mirror = installed_code_handle;
 913         JVMCIENV->invalidate_nmethod_mirror(nmethod_mirror, JVMCI_CHECK_0);
 914       } else {
 915         assert(JVMCIENV->isa_InstalledCode(installed_code_handle), "wrong type");
 916       }
 917       // Initialize the link to the new code blob
 918       JVMCIENV->initialize_installed_code(installed_code_handle, cb, JVMCI_CHECK_0);
 919     }
 920   }
 921   return result;
 922 C2V_END
 923 
 924 C2V_VMENTRY_0(jint, getMetadata, (JNIEnv *env, jobject, jobject target, jobject compiled_code, jobject metadata))
 925   JVMCI_THROW_MSG_0(InternalError, "unimplemented");
 926 C2V_END
 927 
 928 C2V_VMENTRY(void, resetCompilationStatistics, (JNIEnv* env, jobject))
 929   JVMCICompiler* compiler = JVMCICompiler::instance(true, CHECK);
 930   CompilerStatistics* stats = compiler->stats();
 931   stats->_standard.reset();
 932   stats->_osr.reset();
 933 C2V_END
 934 
 935 C2V_VMENTRY_NULL(jobject, disassembleCodeBlob, (JNIEnv* env, jobject, jobject installedCode))
 936   HandleMark hm(THREAD);
 937 
 938   if (installedCode == NULL) {
 939     JVMCI_THROW_MSG_NULL(NullPointerException, "installedCode is null");
 940   }
 941 
 942   JVMCIObject installedCodeObject = JVMCIENV->wrap(installedCode);
 943   nmethodLocker locker;
 944   CodeBlob* cb = JVMCIENV->get_code_blob(installedCodeObject, locker);
 945   if (cb == NULL) {
 946     return NULL;
 947   }
 948 
 949   // We don't want the stringStream buffer to resize during disassembly as it
 950   // uses scoped resource memory. If a nested function called during disassembly uses
 951   // a ResourceMark and the buffer expands within the scope of the mark,
 952   // the buffer becomes garbage when that scope is exited. Experience shows that
 953   // the disassembled code is typically about 10x the code size so a fixed buffer
 954   // sized to 20x code size plus a fixed amount for header info should be sufficient.
 955   int bufferSize = cb->code_size() * 20 + 1024;
 956   char* buffer = NEW_RESOURCE_ARRAY(char, bufferSize);
 957   stringStream st(buffer, bufferSize);
 958   if (cb->is_nmethod()) {
 959     nmethod* nm = (nmethod*) cb;
 960     if (!nm->is_alive()) {
 961       return NULL;
 962     }
 963   }
 964   Disassembler::decode(cb, &st);
 965   if (st.size() <= 0) {
 966     return NULL;
 967   }
 968 
 969   JVMCIObject result = JVMCIENV->create_string(st.as_string(), JVMCI_CHECK_NULL);
 970   return JVMCIENV->get_jobject(result);
 971 C2V_END
 972 
 973 C2V_VMENTRY_NULL(jobject, getStackTraceElement, (JNIEnv* env, jobject, jobject jvmci_method, int bci))
 974   HandleMark hm(THREAD);
 975 
 976   methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));
 977   JVMCIObject element = JVMCIENV->new_StackTraceElement(method, bci, JVMCI_CHECK_NULL);
 978   return JVMCIENV->get_jobject(element);
 979 C2V_END
 980 
 981 C2V_VMENTRY_NULL(jobject, executeHotSpotNmethod, (JNIEnv* env, jobject, jobject args, jobject hs_nmethod))
 982   // The incoming arguments array would have to contain JavaConstants instead of regular objects
 983   // and the return value would have to be wrapped as a JavaConstant.
 984   requireInHotSpot("executeHotSpotNmethod", JVMCI_CHECK_NULL);
 985 
 986   HandleMark hm(THREAD);
 987 
 988   JVMCIObject nmethod_mirror = JVMCIENV->wrap(hs_nmethod);
 989   nmethodLocker locker;
 990   nmethod* nm = JVMCIENV->get_nmethod(nmethod_mirror, locker);
 991   if (nm == NULL || !nm->is_in_use()) {
 992     JVMCI_THROW_NULL(InvalidInstalledCodeException);
 993   }
 994   methodHandle mh(THREAD, nm->method());
 995   Symbol* signature = mh->signature();
 996   JavaCallArguments jca(mh->size_of_parameters());
 997 
 998   JavaArgumentUnboxer jap(signature, &jca, (arrayOop) JNIHandles::resolve(args), mh->is_static());
 999   JavaValue result(jap.return_type());
1000   jca.set_alternative_target(Handle(THREAD, JNIHandles::resolve(nmethod_mirror.as_jobject())));
1001   JavaCalls::call(&result, mh, &jca, CHECK_NULL);
1002 
1003   if (jap.return_type() == T_VOID) {
1004     return NULL;
1005   } else if (is_reference_type(jap.return_type())) {
1006     return JNIHandles::make_local(THREAD, result.get_oop());
1007   } else {
1008     jvalue *value = (jvalue *) result.get_value_addr();
1009     // Narrow the value down if required (Important on big endian machines)
1010     switch (jap.return_type()) {
1011       case T_BOOLEAN:
1012        value->z = (jboolean) value->i;
1013        break;
1014       case T_BYTE:
1015        value->b = (jbyte) value->i;
1016        break;
1017       case T_CHAR:
1018        value->c = (jchar) value->i;
1019        break;
1020       case T_SHORT:
1021        value->s = (jshort) value->i;
1022        break;
1023       default:
1024         break;
1025     }
1026     JVMCIObject o = JVMCIENV->create_box(jap.return_type(), value, JVMCI_CHECK_NULL);
1027     return JVMCIENV->get_jobject(o);
1028   }
1029 C2V_END
1030 
1031 C2V_VMENTRY_NULL(jlongArray, getLineNumberTable, (JNIEnv* env, jobject, jobject jvmci_method))
1032   Method* method = JVMCIENV->asMethod(jvmci_method);
1033   if (!method->has_linenumber_table()) {
1034     return NULL;
1035   }
1036   u2 num_entries = 0;
1037   CompressedLineNumberReadStream streamForSize(method->compressed_linenumber_table());
1038   while (streamForSize.read_pair()) {
1039     num_entries++;
1040   }
1041 
1042   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
1043   JVMCIPrimitiveArray result = JVMCIENV->new_longArray(2 * num_entries, JVMCI_CHECK_NULL);
1044 
1045   int i = 0;
1046   jlong value;
1047   while (stream.read_pair()) {
1048     value = ((long) stream.bci());
1049     JVMCIENV->put_long_at(result, i, value);
1050     value = ((long) stream.line());
1051     JVMCIENV->put_long_at(result, i + 1, value);
1052     i += 2;
1053   }
1054 
1055   return (jlongArray) JVMCIENV->get_jobject(result);
1056 C2V_END
1057 
1058 C2V_VMENTRY_0(jlong, getLocalVariableTableStart, (JNIEnv* env, jobject, jobject jvmci_method))
1059   Method* method = JVMCIENV->asMethod(jvmci_method);
1060   if (!method->has_localvariable_table()) {
1061     return 0;
1062   }
1063   return (jlong) (address) method->localvariable_table_start();
1064 C2V_END
1065 
1066 C2V_VMENTRY_0(jint, getLocalVariableTableLength, (JNIEnv* env, jobject, jobject jvmci_method))
1067   Method* method = JVMCIENV->asMethod(jvmci_method);
1068   return method->localvariable_table_length();
1069 C2V_END
1070 
1071 C2V_VMENTRY(void, reprofile, (JNIEnv* env, jobject, jobject jvmci_method))
1072   methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));
1073   MethodCounters* mcs = method->method_counters();
1074   if (mcs != NULL) {
1075     mcs->clear_counters();
1076   }
1077   NOT_PRODUCT(method->set_compiled_invocation_count(0));
1078 
1079   CompiledMethod* code = method->code();
1080   if (code != NULL) {
1081     code->make_not_entrant();
1082   }
1083 
1084   MethodData* method_data = method->method_data();
1085   if (method_data == NULL) {
1086     ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
1087     method_data = MethodData::allocate(loader_data, method, CHECK);
1088     method->set_method_data(method_data);
1089   } else {
1090     method_data->initialize();
1091   }
1092 C2V_END
1093 
1094 
1095 C2V_VMENTRY(void, invalidateHotSpotNmethod, (JNIEnv* env, jobject, jobject hs_nmethod))
1096   JVMCIObject nmethod_mirror = JVMCIENV->wrap(hs_nmethod);
1097   JVMCIENV->invalidate_nmethod_mirror(nmethod_mirror, JVMCI_CHECK);
1098 C2V_END
1099 
1100 C2V_VMENTRY_NULL(jlongArray, collectCounters, (JNIEnv* env, jobject))
1101   // Returns a zero length array if counters aren't enabled
1102   JVMCIPrimitiveArray array = JVMCIENV->new_longArray(JVMCICounterSize, JVMCI_CHECK_NULL);
1103   if (JVMCICounterSize > 0) {
1104     jlong* temp_array = NEW_RESOURCE_ARRAY(jlong, JVMCICounterSize);
1105     JavaThread::collect_counters(temp_array, JVMCICounterSize);
1106     JVMCIENV->copy_longs_from(temp_array, array, 0, JVMCICounterSize);
1107   }
1108   return (jlongArray) JVMCIENV->get_jobject(array);
1109 C2V_END
1110 
1111 C2V_VMENTRY_0(jint, getCountersSize, (JNIEnv* env, jobject))
1112   return (jint) JVMCICounterSize;
1113 C2V_END
1114 
1115 C2V_VMENTRY_0(jboolean, setCountersSize, (JNIEnv* env, jobject, jint new_size))
1116   return JavaThread::resize_all_jvmci_counters(new_size);
1117 C2V_END
1118 
1119 C2V_VMENTRY_0(jint, allocateCompileId, (JNIEnv* env, jobject, jobject jvmci_method, int entry_bci))
1120   HandleMark hm(THREAD);
1121   if (jvmci_method == NULL) {
1122     JVMCI_THROW_0(NullPointerException);
1123   }
1124   methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));
1125   if (entry_bci >= method->code_size() || entry_bci < -1) {
1126     JVMCI_THROW_MSG_0(IllegalArgumentException, err_msg("Unexpected bci %d", entry_bci));
1127   }
1128   return CompileBroker::assign_compile_id_unlocked(THREAD, method, entry_bci);
1129 C2V_END
1130 
1131 
1132 C2V_VMENTRY_0(jboolean, isMature, (JNIEnv* env, jobject, jlong metaspace_method_data))
1133   MethodData* mdo = JVMCIENV->asMethodData(metaspace_method_data);
1134   return mdo != NULL && mdo->is_mature();
1135 C2V_END
1136 
1137 C2V_VMENTRY_0(jboolean, hasCompiledCodeForOSR, (JNIEnv* env, jobject, jobject jvmci_method, int entry_bci, int comp_level))
1138   Method* method = JVMCIENV->asMethod(jvmci_method);
1139   return method->lookup_osr_nmethod_for(entry_bci, comp_level, true) != NULL;
1140 C2V_END
1141 
1142 C2V_VMENTRY_NULL(jobject, getSymbol, (JNIEnv* env, jobject, jlong symbol))
1143   JVMCIObject sym = JVMCIENV->create_string((Symbol*)(address)symbol, JVMCI_CHECK_NULL);
1144   return JVMCIENV->get_jobject(sym);
1145 C2V_END
1146 
1147 /*
1148  * Used by matches() to convert a ResolvedJavaMethod[] to an array of Method*.
1149  */
1150 GrowableArray<Method*>* init_resolved_methods(jobjectArray methods, JVMCIEnv* JVMCIENV) {
1151   objArrayOop methods_oop = (objArrayOop) JNIHandles::resolve(methods);
1152   GrowableArray<Method*>* resolved_methods = new GrowableArray<Method*>(methods_oop->length());
1153   for (int i = 0; i < methods_oop->length(); i++) {
1154     oop resolved = methods_oop->obj_at(i);
1155     Method* resolved_method = NULL;
1156     if (resolved->klass() == HotSpotJVMCI::HotSpotResolvedJavaMethodImpl::klass()) {
1157       resolved_method = HotSpotJVMCI::asMethod(JVMCIENV, resolved);
1158     }
1159     resolved_methods->append(resolved_method);
1160   }
1161   return resolved_methods;
1162 }
1163 
1164 /*
1165  * Used by c2v_iterateFrames to check if `method` matches one of the ResolvedJavaMethods in the `methods` array.
1166  * The ResolvedJavaMethod[] array is converted to a Method* array that is then cached in the resolved_methods_ref in/out parameter.
1167  * In case of a match, the matching ResolvedJavaMethod is returned in matched_jvmci_method_ref.
1168  */
1169 bool matches(jobjectArray methods, Method* method, GrowableArray<Method*>** resolved_methods_ref, Handle* matched_jvmci_method_ref, Thread* THREAD, JVMCIEnv* JVMCIENV) {
1170   GrowableArray<Method*>* resolved_methods = *resolved_methods_ref;
1171   if (resolved_methods == NULL) {
1172     resolved_methods = init_resolved_methods(methods, JVMCIENV);
1173     *resolved_methods_ref = resolved_methods;
1174   }
1175   assert(method != NULL, "method should not be NULL");
1176   assert(resolved_methods->length() == ((objArrayOop) JNIHandles::resolve(methods))->length(), "arrays must have the same length");
1177   for (int i = 0; i < resolved_methods->length(); i++) {
1178     Method* m = resolved_methods->at(i);
1179     if (m == method) {
1180       *matched_jvmci_method_ref = Handle(THREAD, ((objArrayOop) JNIHandles::resolve(methods))->obj_at(i));
1181       return true;
1182     }
1183   }
1184   return false;
1185 }
1186 
1187 /*
1188  * Resolves an interface call to a concrete method handle.
1189  */
1190 methodHandle resolve_interface_call(Klass* spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS) {
1191   CallInfo callinfo;
1192   Handle receiver = args->receiver();
1193   Klass* recvrKlass = receiver.is_null() ? (Klass*)NULL : receiver->klass();
1194   LinkInfo link_info(spec_klass, name, signature);
1195   LinkResolver::resolve_interface_call(
1196           callinfo, receiver, recvrKlass, link_info, true, CHECK_(methodHandle()));
1197   methodHandle method(THREAD, callinfo.selected_method());
1198   assert(method.not_null(), "should have thrown exception");
1199   return method;
1200 }
1201 
1202 /*
1203  * Used by c2v_iterateFrames to make a new vframeStream at the given compiled frame id (stack pointer) and vframe id.
1204  */
1205 void resync_vframestream_to_compiled_frame(vframeStream& vfst, intptr_t* stack_pointer, int vframe_id, JavaThread* thread, TRAPS) {
1206   vfst = vframeStream(thread);
1207   while (vfst.frame_id() != stack_pointer && !vfst.at_end()) {
1208     vfst.next();
1209   }
1210   if (vfst.frame_id() != stack_pointer) {
1211     THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "stack frame not found after deopt")
1212   }
1213   if (vfst.is_interpreted_frame()) {
1214     THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "compiled stack frame expected")
1215   }
1216   while (vfst.vframe_id() != vframe_id) {
1217     if (vfst.at_end()) {
1218       THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "vframe not found after deopt")
1219     }
1220     vfst.next();
1221     assert(!vfst.is_interpreted_frame(), "Wrong frame type");
1222   }
1223 }
1224 
1225 /*
1226  * Used by c2v_iterateFrames. Returns an array of any unallocated scope objects or NULL if none.
1227  */
1228 GrowableArray<ScopeValue*>* get_unallocated_objects_or_null(GrowableArray<ScopeValue*>* scope_objects) {
1229   GrowableArray<ScopeValue*>* unallocated = NULL;
1230   for (int i = 0; i < scope_objects->length(); i++) {
1231     ObjectValue* sv = (ObjectValue*) scope_objects->at(i);
1232     if (sv->value().is_null()) {
1233       if (unallocated == NULL) {
1234         unallocated = new GrowableArray<ScopeValue*>(scope_objects->length());
1235       }
1236       unallocated->append(sv);
1237     }
1238   }
1239   return unallocated;
1240 }
1241 
1242 C2V_VMENTRY_NULL(jobject, iterateFrames, (JNIEnv* env, jobject compilerToVM, jobjectArray initial_methods, jobjectArray match_methods, jint initialSkip, jobject visitor_handle))
1243 
1244   if (!thread->has_last_Java_frame()) {
1245     return NULL;
1246   }
1247   Handle visitor(THREAD, JNIHandles::resolve_non_null(visitor_handle));
1248 
1249   requireInHotSpot("iterateFrames", JVMCI_CHECK_NULL);
1250 
1251   HotSpotJVMCI::HotSpotStackFrameReference::klass()->initialize(CHECK_NULL);
1252 
1253   vframeStream vfst(thread);
1254   jobjectArray methods = initial_methods;
1255   methodHandle visitor_method;
1256   GrowableArray<Method*>* resolved_methods = NULL;
1257 
1258   while (!vfst.at_end()) { // frame loop
1259     bool realloc_called = false;
1260     intptr_t* frame_id = vfst.frame_id();
1261 
1262     // Previous compiledVFrame of this frame; use with at_scope() to reuse scope object pool.
1263     compiledVFrame* prev_cvf = NULL;
1264 
1265     for (; !vfst.at_end() && vfst.frame_id() == frame_id; vfst.next()) { // vframe loop
1266       int frame_number = 0;
1267       Method *method = vfst.method();
1268       int bci = vfst.bci();
1269 
1270       Handle matched_jvmci_method;
1271       if (methods == NULL || matches(methods, method, &resolved_methods, &matched_jvmci_method, THREAD, JVMCIENV)) {
1272         if (initialSkip > 0) {
1273           initialSkip--;
1274           continue;
1275         }
1276         javaVFrame* vf;
1277         if (prev_cvf != NULL && prev_cvf->frame_pointer()->id() == frame_id) {
1278           assert(prev_cvf->is_compiled_frame(), "expected compiled Java frame");
1279           vf = prev_cvf->at_scope(vfst.decode_offset(), vfst.vframe_id());
1280         } else {
1281           vf = vfst.asJavaVFrame();
1282         }
1283 
1284         StackValueCollection* locals = NULL;
1285         typeArrayHandle localIsVirtual_h;
1286         if (vf->is_compiled_frame()) {
1287           // compiled method frame
1288           compiledVFrame* cvf = compiledVFrame::cast(vf);
1289 
1290           ScopeDesc* scope = cvf->scope();
1291           // native wrappers do not have a scope
1292           if (scope != NULL && scope->objects() != NULL) {
1293             prev_cvf = cvf;
1294 
1295             GrowableArray<ScopeValue*>* objects = NULL;
1296             if (!realloc_called) {
1297               objects = scope->objects();
1298             } else {
1299               // some object might already have been re-allocated, only reallocate the non-allocated ones
1300               objects = get_unallocated_objects_or_null(scope->objects());
1301             }
1302 
1303             if (objects != NULL) {
1304               RegisterMap reg_map(vf->register_map());
1305               bool realloc_failures = Deoptimization::realloc_objects(thread, vf->frame_pointer(), &reg_map, objects, CHECK_NULL);
1306               Deoptimization::reassign_fields(vf->frame_pointer(), &reg_map, objects, realloc_failures, false, CHECK_NULL);
1307               realloc_called = true;
1308             }
1309 
1310             GrowableArray<ScopeValue*>* local_values = scope->locals();
1311             for (int i = 0; i < local_values->length(); i++) {
1312               ScopeValue* value = local_values->at(i);
1313               if (value->is_object()) {
1314                 if (localIsVirtual_h.is_null()) {
1315                   typeArrayOop array_oop = oopFactory::new_boolArray(local_values->length(), CHECK_NULL);
1316                   localIsVirtual_h = typeArrayHandle(THREAD, array_oop);
1317                 }
1318                 localIsVirtual_h->bool_at_put(i, true);
1319               }
1320             }
1321           }
1322 
1323           locals = cvf->locals();
1324           frame_number = cvf->vframe_id();
1325         } else {
1326           // interpreted method frame
1327           interpretedVFrame* ivf = interpretedVFrame::cast(vf);
1328 
1329           locals = ivf->locals();
1330         }
1331         assert(bci == vf->bci(), "wrong bci");
1332         assert(method == vf->method(), "wrong method");
1333 
1334         Handle frame_reference = HotSpotJVMCI::HotSpotStackFrameReference::klass()->allocate_instance_handle(CHECK_NULL);
1335         HotSpotJVMCI::HotSpotStackFrameReference::set_bci(JVMCIENV, frame_reference(), bci);
1336         if (matched_jvmci_method.is_null()) {
1337           methodHandle mh(THREAD, method);
1338           JVMCIObject jvmci_method = JVMCIENV->get_jvmci_method(mh, JVMCI_CHECK_NULL);
1339           matched_jvmci_method = Handle(THREAD, JNIHandles::resolve(jvmci_method.as_jobject()));
1340         }
1341         HotSpotJVMCI::HotSpotStackFrameReference::set_method(JVMCIENV, frame_reference(), matched_jvmci_method());
1342         HotSpotJVMCI::HotSpotStackFrameReference::set_localIsVirtual(JVMCIENV, frame_reference(), localIsVirtual_h());
1343 
1344         HotSpotJVMCI::HotSpotStackFrameReference::set_compilerToVM(JVMCIENV, frame_reference(), JNIHandles::resolve(compilerToVM));
1345         HotSpotJVMCI::HotSpotStackFrameReference::set_stackPointer(JVMCIENV, frame_reference(), (jlong) frame_id);
1346         HotSpotJVMCI::HotSpotStackFrameReference::set_frameNumber(JVMCIENV, frame_reference(), frame_number);
1347 
1348         // initialize the locals array
1349         objArrayOop array_oop = oopFactory::new_objectArray(locals->size(), CHECK_NULL);
1350         objArrayHandle array(THREAD, array_oop);
1351         for (int i = 0; i < locals->size(); i++) {
1352           StackValue* var = locals->at(i);
1353           if (var->type() == T_OBJECT) {
1354             array->obj_at_put(i, locals->at(i)->get_obj()());
1355           }
1356         }
1357         HotSpotJVMCI::HotSpotStackFrameReference::set_locals(JVMCIENV, frame_reference(), array());
1358         HotSpotJVMCI::HotSpotStackFrameReference::set_objectsMaterialized(JVMCIENV, frame_reference(), JNI_FALSE);
1359 
1360         JavaValue result(T_OBJECT);
1361         JavaCallArguments args(visitor);
1362         if (visitor_method.is_null()) {
1363           visitor_method = resolve_interface_call(HotSpotJVMCI::InspectedFrameVisitor::klass(), vmSymbols::visitFrame_name(), vmSymbols::visitFrame_signature(), &args, CHECK_NULL);
1364         }
1365 
1366         args.push_oop(frame_reference);
1367         JavaCalls::call(&result, visitor_method, &args, CHECK_NULL);
1368         if (result.get_oop() != NULL) {
1369           return JNIHandles::make_local(thread, result.get_oop());
1370         }
1371         if (methods == initial_methods) {
1372           methods = match_methods;
1373           if (resolved_methods != NULL && JNIHandles::resolve(match_methods) != JNIHandles::resolve(initial_methods)) {
1374             resolved_methods = NULL;
1375           }
1376         }
1377         assert(initialSkip == 0, "There should be no match before initialSkip == 0");
1378         if (HotSpotJVMCI::HotSpotStackFrameReference::objectsMaterialized(JVMCIENV, frame_reference()) == JNI_TRUE) {
1379           // the frame has been deoptimized, we need to re-synchronize the frame and vframe
1380           prev_cvf = NULL;
1381           intptr_t* stack_pointer = (intptr_t*) HotSpotJVMCI::HotSpotStackFrameReference::stackPointer(JVMCIENV, frame_reference());
1382           resync_vframestream_to_compiled_frame(vfst, stack_pointer, frame_number, thread, CHECK_NULL);
1383         }
1384       }
1385     } // end of vframe loop
1386   } // end of frame loop
1387 
1388   // the end was reached without finding a matching method
1389   return NULL;
1390 C2V_END
1391 
1392 C2V_VMENTRY(void, resolveInvokeDynamicInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))
1393   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
1394   CallInfo callInfo;
1395   LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, Bytecodes::_invokedynamic, CHECK);
1396   ConstantPoolCacheEntry* cp_cache_entry = cp->invokedynamic_cp_cache_entry_at(index);
1397   cp_cache_entry->set_dynamic_call(cp, callInfo);
1398 C2V_END
1399 
1400 C2V_VMENTRY(void, resolveInvokeHandleInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))
1401   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
1402   Klass* holder = cp->klass_ref_at(index, CHECK);
1403   Symbol* name = cp->name_ref_at(index);
1404   if (MethodHandles::is_signature_polymorphic_name(holder, name)) {
1405     CallInfo callInfo;
1406     LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, Bytecodes::_invokehandle, CHECK);
1407     ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index));
1408     cp_cache_entry->set_method_handle(cp, callInfo);
1409   }
1410 C2V_END
1411 
1412 C2V_VMENTRY_0(jint, isResolvedInvokeHandleInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))
1413   constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));
1414   ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index));
1415   if (cp_cache_entry->is_resolved(Bytecodes::_invokehandle)) {
1416     // MethodHandle.invoke* --> LambdaForm?
1417     ResourceMark rm;
1418 
1419     LinkInfo link_info(cp, index, CATCH);
1420 
1421     Klass* resolved_klass = link_info.resolved_klass();
1422 
1423     Symbol* name_sym = cp->name_ref_at(index);
1424 
1425     vmassert(MethodHandles::is_method_handle_invoke_name(resolved_klass, name_sym), "!");
1426     vmassert(MethodHandles::is_signature_polymorphic_name(resolved_klass, name_sym), "!");
1427 
1428     methodHandle adapter_method(THREAD, cp_cache_entry->f1_as_method());
1429 
1430     methodHandle resolved_method(adapter_method);
1431 
1432     // Can we treat it as a regular invokevirtual?
1433     if (resolved_method->method_holder() == resolved_klass && resolved_method->name() == name_sym) {
1434       vmassert(!resolved_method->is_static(),"!");
1435       vmassert(MethodHandles::is_signature_polymorphic_method(resolved_method()),"!");
1436       vmassert(!MethodHandles::is_signature_polymorphic_static(resolved_method->intrinsic_id()), "!");
1437       vmassert(cp_cache_entry->appendix_if_resolved(cp) == NULL, "!");
1438 
1439       methodHandle m(THREAD, LinkResolver::linktime_resolve_virtual_method_or_null(link_info));
1440       vmassert(m == resolved_method, "!!");
1441       return -1;
1442     }
1443 
1444     return Bytecodes::_invokevirtual;
1445   }
1446   if (cp_cache_entry->is_resolved(Bytecodes::_invokedynamic)) {
1447     return Bytecodes::_invokedynamic;
1448   }
1449   return -1;
1450 C2V_END
1451 
1452 
1453 C2V_VMENTRY_NULL(jobject, getSignaturePolymorphicHolders, (JNIEnv* env, jobject))
1454   JVMCIObjectArray holders = JVMCIENV->new_String_array(2, JVMCI_CHECK_NULL);
1455   JVMCIObject mh = JVMCIENV->create_string("Ljava/lang/invoke/MethodHandle;", JVMCI_CHECK_NULL);
1456   JVMCIObject vh = JVMCIENV->create_string("Ljava/lang/invoke/VarHandle;", JVMCI_CHECK_NULL);
1457   JVMCIENV->put_object_at(holders, 0, mh);
1458   JVMCIENV->put_object_at(holders, 1, vh);
1459   return JVMCIENV->get_jobject(holders);
1460 C2V_END
1461 
1462 C2V_VMENTRY_0(jboolean, shouldDebugNonSafepoints, (JNIEnv* env, jobject))
1463   //see compute_recording_non_safepoints in debugInfroRec.cpp
1464   if (JvmtiExport::should_post_compiled_method_load() && FLAG_IS_DEFAULT(DebugNonSafepoints)) {
1465     return true;
1466   }
1467   return DebugNonSafepoints;
1468 C2V_END
1469 
1470 // public native void materializeVirtualObjects(HotSpotStackFrameReference stackFrame, boolean invalidate);
1471 C2V_VMENTRY(void, materializeVirtualObjects, (JNIEnv* env, jobject, jobject _hs_frame, bool invalidate))
1472   JVMCIObject hs_frame = JVMCIENV->wrap(_hs_frame);
1473   if (hs_frame.is_null()) {
1474     JVMCI_THROW_MSG(NullPointerException, "stack frame is null");
1475   }
1476 
1477   requireInHotSpot("materializeVirtualObjects", JVMCI_CHECK);
1478 
1479   JVMCIENV->HotSpotStackFrameReference_initialize(JVMCI_CHECK);
1480 
1481   // look for the given stack frame
1482   StackFrameStream fst(thread, false /* update */, true /* process_frames */);
1483   intptr_t* stack_pointer = (intptr_t*) JVMCIENV->get_HotSpotStackFrameReference_stackPointer(hs_frame);
1484   while (fst.current()->id() != stack_pointer && !fst.is_done()) {
1485     fst.next();
1486   }
1487   if (fst.current()->id() != stack_pointer) {
1488     JVMCI_THROW_MSG(IllegalStateException, "stack frame not found");
1489   }
1490 
1491   if (invalidate) {
1492     if (!fst.current()->is_compiled_frame()) {
1493       JVMCI_THROW_MSG(IllegalStateException, "compiled stack frame expected");
1494     }
1495     assert(fst.current()->cb()->is_nmethod(), "nmethod expected");
1496     ((nmethod*) fst.current()->cb())->make_not_entrant();
1497   }
1498   Deoptimization::deoptimize(thread, *fst.current(), Deoptimization::Reason_none);
1499   // look for the frame again as it has been updated by deopt (pc, deopt state...)
1500   StackFrameStream fstAfterDeopt(thread, true /* update */, true /* process_frames */);
1501   while (fstAfterDeopt.current()->id() != stack_pointer && !fstAfterDeopt.is_done()) {
1502     fstAfterDeopt.next();
1503   }
1504   if (fstAfterDeopt.current()->id() != stack_pointer) {
1505     JVMCI_THROW_MSG(IllegalStateException, "stack frame not found after deopt");
1506   }
1507 
1508   vframe* vf = vframe::new_vframe(fstAfterDeopt.current(), fstAfterDeopt.register_map(), thread);
1509   if (!vf->is_compiled_frame()) {
1510     JVMCI_THROW_MSG(IllegalStateException, "compiled stack frame expected");
1511   }
1512 
1513   GrowableArray<compiledVFrame*>* virtualFrames = new GrowableArray<compiledVFrame*>(10);
1514   while (true) {
1515     assert(vf->is_compiled_frame(), "Wrong frame type");
1516     virtualFrames->push(compiledVFrame::cast(vf));
1517     if (vf->is_top()) {
1518       break;
1519     }
1520     vf = vf->sender();
1521   }
1522 
1523   int last_frame_number = JVMCIENV->get_HotSpotStackFrameReference_frameNumber(hs_frame);
1524   if (last_frame_number >= virtualFrames->length()) {
1525     JVMCI_THROW_MSG(IllegalStateException, "invalid frame number");
1526   }
1527 
1528   // Reallocate the non-escaping objects and restore their fields.
1529   assert (virtualFrames->at(last_frame_number)->scope() != NULL,"invalid scope");
1530   GrowableArray<ScopeValue*>* objects = virtualFrames->at(last_frame_number)->scope()->objects();
1531 
1532   if (objects == NULL) {
1533     // no objects to materialize
1534     return;
1535   }
1536 
1537   bool realloc_failures = Deoptimization::realloc_objects(thread, fstAfterDeopt.current(), fstAfterDeopt.register_map(), objects, CHECK);
1538   Deoptimization::reassign_fields(fstAfterDeopt.current(), fstAfterDeopt.register_map(), objects, realloc_failures, false, THREAD);
1539 
1540   for (int frame_index = 0; frame_index < virtualFrames->length(); frame_index++) {
1541     compiledVFrame* cvf = virtualFrames->at(frame_index);
1542 
1543     GrowableArray<ScopeValue*>* scopeLocals = cvf->scope()->locals();
1544     StackValueCollection* locals = cvf->locals();
1545     if (locals != NULL) {
1546       for (int i2 = 0; i2 < locals->size(); i2++) {
1547         StackValue* var = locals->at(i2);
1548         if (var->type() == T_OBJECT && scopeLocals->at(i2)->is_object()) {
1549           jvalue val;
1550           val.l = cast_from_oop<jobject>(locals->at(i2)->get_obj()());
1551           cvf->update_local(T_OBJECT, i2, val);
1552         }
1553       }
1554     }
1555 
1556     GrowableArray<ScopeValue*>* scopeExpressions = cvf->scope()->expressions();
1557     StackValueCollection* expressions = cvf->expressions();
1558     if (expressions != NULL) {
1559       for (int i2 = 0; i2 < expressions->size(); i2++) {
1560         StackValue* var = expressions->at(i2);
1561         if (var->type() == T_OBJECT && scopeExpressions->at(i2)->is_object()) {
1562           jvalue val;
1563           val.l = cast_from_oop<jobject>(expressions->at(i2)->get_obj()());
1564           cvf->update_stack(T_OBJECT, i2, val);
1565         }
1566       }
1567     }
1568 
1569     GrowableArray<MonitorValue*>* scopeMonitors = cvf->scope()->monitors();
1570     GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
1571     if (monitors != NULL) {
1572       for (int i2 = 0; i2 < monitors->length(); i2++) {
1573         cvf->update_monitor(i2, monitors->at(i2));
1574       }
1575     }
1576   }
1577 
1578   // all locals are materialized by now
1579   JVMCIENV->set_HotSpotStackFrameReference_localIsVirtual(hs_frame, NULL);
1580   // update the locals array
1581   JVMCIObjectArray array = JVMCIENV->get_HotSpotStackFrameReference_locals(hs_frame);
1582   StackValueCollection* locals = virtualFrames->at(last_frame_number)->locals();
1583   for (int i = 0; i < locals->size(); i++) {
1584     StackValue* var = locals->at(i);
1585     if (var->type() == T_OBJECT) {
1586       JVMCIENV->put_object_at(array, i, HotSpotJVMCI::wrap(locals->at(i)->get_obj()()));
1587     }
1588   }
1589   HotSpotJVMCI::HotSpotStackFrameReference::set_objectsMaterialized(JVMCIENV, hs_frame, JNI_TRUE);
1590 C2V_END
1591 
1592 // Use of tty does not require the current thread to be attached to the VM
1593 // so no need for a full C2V_VMENTRY transition.
1594 C2V_VMENTRY_PREFIX(void, writeDebugOutput, (JNIEnv* env, jobject, jlong buffer, jint length, bool flush))
1595   if (length <= 8) {
1596     tty->write((char*) &buffer, length);
1597   } else {
1598     tty->write((char*) buffer, length);
1599   }
1600   if (flush) {
1601     tty->flush();
1602   }
1603 C2V_END
1604 
1605 // Use of tty does not require the current thread to be attached to the VM
1606 // so no need for a full C2V_VMENTRY transition.
1607 C2V_VMENTRY_PREFIX(void, flushDebugOutput, (JNIEnv* env, jobject))
1608   tty->flush();
1609 C2V_END
1610 
1611 C2V_VMENTRY_0(jint, methodDataProfileDataSize, (JNIEnv* env, jobject, jlong metaspace_method_data, jint position))
1612   MethodData* mdo = JVMCIENV->asMethodData(metaspace_method_data);
1613   ProfileData* profile_data = mdo->data_at(position);
1614   if (mdo->is_valid(profile_data)) {
1615     return profile_data->size_in_bytes();
1616   }
1617   DataLayout* data    = mdo->extra_data_base();
1618   DataLayout* end   = mdo->extra_data_limit();
1619   for (;; data = mdo->next_extra(data)) {
1620     assert(data < end, "moved past end of extra data");
1621     profile_data = data->data_in();
1622     if (mdo->dp_to_di(profile_data->dp()) == position) {
1623       return profile_data->size_in_bytes();
1624     }
1625   }
1626   JVMCI_THROW_MSG_0(IllegalArgumentException, err_msg("Invalid profile data position %d", position));
1627 C2V_END
1628 
1629 C2V_VMENTRY_0(jlong, getFingerprint, (JNIEnv* env, jobject, jlong metaspace_klass))
1630   JVMCI_THROW_MSG_0(InternalError, "unimplemented");
1631 C2V_END
1632 
1633 C2V_VMENTRY_NULL(jobject, getInterfaces, (JNIEnv* env, jobject, jobject jvmci_type))
1634   if (jvmci_type == NULL) {
1635     JVMCI_THROW_0(NullPointerException);
1636   }
1637 
1638   Klass* klass = JVMCIENV->asKlass(jvmci_type);
1639   if (klass == NULL) {
1640     JVMCI_THROW_0(NullPointerException);
1641   }
1642   if (!klass->is_instance_klass()) {
1643     JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s must be instance klass", klass->external_name()));
1644   }
1645   InstanceKlass* iklass = InstanceKlass::cast(klass);
1646 
1647   // Regular instance klass, fill in all local interfaces
1648   int size = iklass->local_interfaces()->length();
1649   JVMCIObjectArray interfaces = JVMCIENV->new_HotSpotResolvedObjectTypeImpl_array(size, JVMCI_CHECK_NULL);
1650   for (int index = 0; index < size; index++) {
1651     JVMCIKlassHandle klass(THREAD);
1652     Klass* k = iklass->local_interfaces()->at(index);
1653     klass = k;
1654     JVMCIObject type = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL);
1655     JVMCIENV->put_object_at(interfaces, index, type);
1656   }
1657   return JVMCIENV->get_jobject(interfaces);
1658 C2V_END
1659 
1660 C2V_VMENTRY_NULL(jobject, getComponentType, (JNIEnv* env, jobject, jobject jvmci_type))
1661   if (jvmci_type == NULL) {
1662     JVMCI_THROW_0(NullPointerException);
1663   }
1664 
1665   Klass* klass = JVMCIENV->asKlass(jvmci_type);
1666   oop mirror = klass->java_mirror();
1667   if (java_lang_Class::is_primitive(mirror) ||
1668       !java_lang_Class::as_Klass(mirror)->is_array_klass()) {
1669     return NULL;
1670   }
1671 
1672   oop component_mirror = java_lang_Class::component_mirror(mirror);
1673   if (component_mirror == NULL) {
1674     return NULL;
1675   }
1676   Klass* component_klass = java_lang_Class::as_Klass(component_mirror);
1677   if (component_klass != NULL) {
1678     JVMCIKlassHandle klass_handle(THREAD);
1679     klass_handle = component_klass;
1680     JVMCIObject result = JVMCIENV->get_jvmci_type(klass_handle, JVMCI_CHECK_NULL);
1681     return JVMCIENV->get_jobject(result);
1682   }
1683   BasicType type = java_lang_Class::primitive_type(component_mirror);
1684   JVMCIObject result = JVMCIENV->get_jvmci_primitive_type(type);
1685   return JVMCIENV->get_jobject(result);
1686 C2V_END
1687 
1688 C2V_VMENTRY(void, ensureInitialized, (JNIEnv* env, jobject, jobject jvmci_type))
1689   if (jvmci_type == NULL) {
1690     JVMCI_THROW(NullPointerException);
1691   }
1692 
1693   Klass* klass = JVMCIENV->asKlass(jvmci_type);
1694   if (klass != NULL && klass->should_be_initialized()) {
1695     InstanceKlass* k = InstanceKlass::cast(klass);
1696     k->initialize(CHECK);
1697   }
1698 C2V_END
1699 
1700 C2V_VMENTRY(void, ensureLinked, (JNIEnv* env, jobject, jobject jvmci_type))
1701   if (jvmci_type == NULL) {
1702     JVMCI_THROW(NullPointerException);
1703   }
1704 
1705   Klass* klass = JVMCIENV->asKlass(jvmci_type);
1706   if (klass != NULL && klass->is_instance_klass()) {
1707     InstanceKlass* k = InstanceKlass::cast(klass);
1708     k->link_class(CHECK);
1709   }
1710 C2V_END
1711 
1712 C2V_VMENTRY_0(jint, interpreterFrameSize, (JNIEnv* env, jobject, jobject bytecode_frame_handle))
1713   if (bytecode_frame_handle == NULL) {
1714     JVMCI_THROW_0(NullPointerException);
1715   }
1716 
1717   JVMCIObject top_bytecode_frame = JVMCIENV->wrap(bytecode_frame_handle);
1718   JVMCIObject bytecode_frame = top_bytecode_frame;
1719   int size = 0;
1720   int callee_parameters = 0;
1721   int callee_locals = 0;
1722   Method* method = JVMCIENV->asMethod(JVMCIENV->get_BytecodePosition_method(bytecode_frame));
1723   int extra_args = method->max_stack() - JVMCIENV->get_BytecodeFrame_numStack(bytecode_frame);
1724 
1725   while (bytecode_frame.is_non_null()) {
1726     int locks = JVMCIENV->get_BytecodeFrame_numLocks(bytecode_frame);
1727     int temps = JVMCIENV->get_BytecodeFrame_numStack(bytecode_frame);
1728     bool is_top_frame = (JVMCIENV->equals(bytecode_frame, top_bytecode_frame));
1729     Method* method = JVMCIENV->asMethod(JVMCIENV->get_BytecodePosition_method(bytecode_frame));
1730 
1731     int frame_size = BytesPerWord * Interpreter::size_activation(method->max_stack(),
1732                                                                  temps + callee_parameters,
1733                                                                  extra_args,
1734                                                                  locks,
1735                                                                  callee_parameters,
1736                                                                  callee_locals,
1737                                                                  is_top_frame);
1738     size += frame_size;
1739 
1740     callee_parameters = method->size_of_parameters();
1741     callee_locals = method->max_locals();
1742     extra_args = 0;
1743     bytecode_frame = JVMCIENV->get_BytecodePosition_caller(bytecode_frame);
1744   }
1745   return size + Deoptimization::last_frame_adjust(0, callee_locals) * BytesPerWord;
1746 C2V_END
1747 
1748 C2V_VMENTRY(void, compileToBytecode, (JNIEnv* env, jobject, jobject lambda_form_handle))
1749   Handle lambda_form = JVMCIENV->asConstant(JVMCIENV->wrap(lambda_form_handle), JVMCI_CHECK);
1750   if (lambda_form->is_a(vmClasses::LambdaForm_klass())) {
1751     TempNewSymbol compileToBytecode = SymbolTable::new_symbol("compileToBytecode");
1752     JavaValue result(T_VOID);
1753     JavaCalls::call_special(&result, lambda_form, vmClasses::LambdaForm_klass(), compileToBytecode, vmSymbols::void_method_signature(), CHECK);
1754   } else {
1755     JVMCI_THROW_MSG(IllegalArgumentException,
1756                     err_msg("Unexpected type: %s", lambda_form->klass()->external_name()))
1757   }
1758 C2V_END
1759 
1760 C2V_VMENTRY_0(jint, getIdentityHashCode, (JNIEnv* env, jobject, jobject object))
1761   Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_0);
1762   return obj->identity_hash();
1763 C2V_END
1764 
1765 C2V_VMENTRY_0(jboolean, isInternedString, (JNIEnv* env, jobject, jobject object))
1766   Handle str = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_0);
1767   if (!java_lang_String::is_instance(str())) {
1768     return false;
1769   }
1770   int len;
1771   jchar* name = java_lang_String::as_unicode_string(str(), len, CHECK_false);
1772   return (StringTable::lookup(name, len) != NULL);
1773 C2V_END
1774 
1775 
1776 C2V_VMENTRY_NULL(jobject, unboxPrimitive, (JNIEnv* env, jobject, jobject object))
1777   if (object == NULL) {
1778     JVMCI_THROW_0(NullPointerException);
1779   }
1780   Handle box = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_NULL);
1781   BasicType type = java_lang_boxing_object::basic_type(box());
1782   jvalue result;
1783   if (java_lang_boxing_object::get_value(box(), &result) == T_ILLEGAL) {
1784     return NULL;
1785   }
1786   JVMCIObject boxResult = JVMCIENV->create_box(type, &result, JVMCI_CHECK_NULL);
1787   return JVMCIENV->get_jobject(boxResult);
1788 C2V_END
1789 
1790 C2V_VMENTRY_NULL(jobject, boxPrimitive, (JNIEnv* env, jobject, jobject object))
1791   if (object == NULL) {
1792     JVMCI_THROW_0(NullPointerException);
1793   }
1794   JVMCIObject box = JVMCIENV->wrap(object);
1795   BasicType type = JVMCIENV->get_box_type(box);
1796   if (type == T_ILLEGAL) {
1797     return NULL;
1798   }
1799   jvalue value = JVMCIENV->get_boxed_value(type, box);
1800   JavaValue box_result(T_OBJECT);
1801   JavaCallArguments jargs;
1802   Klass* box_klass = NULL;
1803   Symbol* box_signature = NULL;
1804 #define BOX_CASE(bt, v, argtype, name)           \
1805   case bt: \
1806     jargs.push_##argtype(value.v); \
1807     box_klass = vmClasses::name##_klass(); \
1808     box_signature = vmSymbols::name##_valueOf_signature(); \
1809     break
1810 
1811   switch (type) {
1812     BOX_CASE(T_BOOLEAN, z, int, Boolean);
1813     BOX_CASE(T_BYTE, b, int, Byte);
1814     BOX_CASE(T_CHAR, c, int, Character);
1815     BOX_CASE(T_SHORT, s, int, Short);
1816     BOX_CASE(T_INT, i, int, Integer);
1817     BOX_CASE(T_LONG, j, long, Long);
1818     BOX_CASE(T_FLOAT, f, float, Float);
1819     BOX_CASE(T_DOUBLE, d, double, Double);
1820     default:
1821       ShouldNotReachHere();
1822   }
1823 #undef BOX_CASE
1824 
1825   JavaCalls::call_static(&box_result,
1826                          box_klass,
1827                          vmSymbols::valueOf_name(),
1828                          box_signature, &jargs, CHECK_NULL);
1829   oop hotspot_box = box_result.get_oop();
1830   JVMCIObject result = JVMCIENV->get_object_constant(hotspot_box, false);
1831   return JVMCIENV->get_jobject(result);
1832 C2V_END
1833 
1834 C2V_VMENTRY_NULL(jobjectArray, getDeclaredConstructors, (JNIEnv* env, jobject, jobject holder))
1835   if (holder == NULL) {
1836     JVMCI_THROW_0(NullPointerException);
1837   }
1838   Klass* klass = JVMCIENV->asKlass(holder);
1839   if (!klass->is_instance_klass()) {
1840     JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(0, JVMCI_CHECK_NULL);
1841     return JVMCIENV->get_jobjectArray(methods);
1842   }
1843 
1844   InstanceKlass* iklass = InstanceKlass::cast(klass);
1845   // Ensure class is linked
1846   iklass->link_class(CHECK_NULL);
1847 
1848   GrowableArray<Method*> constructors_array;
1849   for (int i = 0; i < iklass->methods()->length(); i++) {
1850     Method* m = iklass->methods()->at(i);
1851     if (m->is_object_constructor()) {
1852       constructors_array.append(m);
1853     }
1854   }
1855   JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(constructors_array.length(), JVMCI_CHECK_NULL);
1856   for (int i = 0; i < constructors_array.length(); i++) {
1857     methodHandle ctor(THREAD, constructors_array.at(i));
1858     JVMCIObject method = JVMCIENV->get_jvmci_method(ctor, JVMCI_CHECK_NULL);
1859     JVMCIENV->put_object_at(methods, i, method);
1860   }
1861   return JVMCIENV->get_jobjectArray(methods);
1862 C2V_END
1863 
1864 C2V_VMENTRY_NULL(jobjectArray, getDeclaredMethods, (JNIEnv* env, jobject, jobject holder))
1865   if (holder == NULL) {
1866     JVMCI_THROW_0(NullPointerException);
1867   }
1868   Klass* klass = JVMCIENV->asKlass(holder);
1869   if (!klass->is_instance_klass()) {
1870     JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(0, JVMCI_CHECK_NULL);
1871     return JVMCIENV->get_jobjectArray(methods);
1872   }
1873 
1874   InstanceKlass* iklass = InstanceKlass::cast(klass);
1875   // Ensure class is linked
1876   iklass->link_class(CHECK_NULL);
1877 
1878   GrowableArray<Method*> methods_array;
1879   for (int i = 0; i < iklass->methods()->length(); i++) {
1880     Method* m = iklass->methods()->at(i);
1881     if (!(m->is_object_constructor() || m->is_class_initializer()) && !m->is_overpass()) {
1882       methods_array.append(m);
1883     }
1884   }
1885   JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(methods_array.length(), JVMCI_CHECK_NULL);
1886   for (int i = 0; i < methods_array.length(); i++) {
1887     methodHandle mh(THREAD, methods_array.at(i));
1888     JVMCIObject method = JVMCIENV->get_jvmci_method(mh, JVMCI_CHECK_NULL);
1889     JVMCIENV->put_object_at(methods, i, method);
1890   }
1891   return JVMCIENV->get_jobjectArray(methods);
1892 C2V_END
1893 
1894 C2V_VMENTRY_NULL(jobject, readFieldValue, (JNIEnv* env, jobject, jobject object, jobject expected_type, long displacement, jboolean is_volatile, jobject kind_object))
1895   if (object == NULL || kind_object == NULL) {
1896     JVMCI_THROW_0(NullPointerException);
1897   }
1898 
1899   JVMCIObject kind = JVMCIENV->wrap(kind_object);
1900   BasicType basic_type = JVMCIENV->kindToBasicType(kind, JVMCI_CHECK_NULL);
1901 
1902   InstanceKlass* holder = NULL;
1903   if (expected_type != NULL) {
1904     holder = InstanceKlass::cast(JVMCIENV->asKlass(JVMCIENV->wrap(expected_type)));
1905   }
1906 
1907   bool is_static = false;
1908   Handle obj;
1909   JVMCIObject base = JVMCIENV->wrap(object);
1910   if (JVMCIENV->isa_HotSpotObjectConstantImpl(base)) {
1911     obj = JVMCIENV->asConstant(base, JVMCI_CHECK_NULL);
1912     // asConstant will throw an NPE if a constant contains NULL
1913 
1914     if (holder != NULL && !obj->is_a(holder)) {
1915       // Not a subtype of field holder
1916       return NULL;
1917     }
1918     is_static = false;
1919     if (holder == NULL && java_lang_Class::is_instance(obj()) && displacement >= InstanceMirrorKlass::offset_of_static_fields()) {
1920       is_static = true;
1921     }
1922   } else if (JVMCIENV->isa_HotSpotResolvedObjectTypeImpl(base)) {
1923     is_static = true;
1924     Klass* klass = JVMCIENV->asKlass(base);
1925     if (holder != NULL && holder != klass) {
1926       return NULL;
1927     }
1928     obj = Handle(THREAD, klass->java_mirror());
1929   } else {
1930     // The Java code is expected to guard against this path
1931     ShouldNotReachHere();
1932   }
1933 
1934   if (displacement < 0 || ((long) displacement + type2aelembytes(basic_type) > HeapWordSize * obj->size())) {
1935     // Reading outside of the object bounds
1936     JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading outside object bounds");
1937   }
1938 
1939   // Perform basic sanity checks on the read.  Primitive reads are permitted to read outside the
1940   // bounds of their fields but object reads must map exactly onto the underlying oop slot.
1941   if (basic_type == T_OBJECT) {
1942     if (obj->is_objArray()) {
1943       if (displacement < arrayOopDesc::base_offset_in_bytes(T_OBJECT)) {
1944         JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading from array header");
1945       }
1946       if (displacement + heapOopSize > arrayOopDesc::base_offset_in_bytes(T_OBJECT) + arrayOop(obj())->length() * heapOopSize) {
1947         JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading after last array element");
1948       }
1949       if (((displacement - arrayOopDesc::base_offset_in_bytes(T_OBJECT)) % heapOopSize) != 0) {
1950         JVMCI_THROW_MSG_NULL(IllegalArgumentException, "misaligned object read from array");
1951       }
1952     } else if (obj->is_instance()) {
1953       InstanceKlass* klass = InstanceKlass::cast(is_static ? java_lang_Class::as_Klass(obj()) : obj->klass());
1954       fieldDescriptor fd;
1955       if (!klass->find_field_from_offset(displacement, is_static, &fd)) {
1956         JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Can't find field at displacement %d in object of type %s", (int) displacement, klass->external_name()));
1957       }
1958       if (fd.field_type() != T_OBJECT && fd.field_type() != T_ARRAY) {
1959         JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Field at displacement %d in object of type %s is %s but expected %s", (int) displacement,
1960                                                                klass->external_name(), type2name(fd.field_type()), type2name(basic_type)));
1961       }
1962     } else if (obj->is_typeArray()) {
1963       JVMCI_THROW_MSG_NULL(IllegalArgumentException, "Can't read objects from primitive array");
1964     } else {
1965       ShouldNotReachHere();
1966     }
1967   } else {
1968     if (obj->is_objArray()) {
1969       JVMCI_THROW_MSG_NULL(IllegalArgumentException, "Reading primitive from object array");
1970     } else if (obj->is_typeArray()) {
1971       if (displacement < arrayOopDesc::base_offset_in_bytes(ArrayKlass::cast(obj->klass())->element_type())) {
1972         JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading from array header");
1973       }
1974     }
1975   }
1976 
1977   jlong value = 0;
1978   switch (basic_type) {
1979     case T_BOOLEAN: value = is_volatile ? obj->bool_field_acquire(displacement)   : obj->bool_field(displacement);  break;
1980     case T_BYTE:    value = is_volatile ? obj->byte_field_acquire(displacement)   : obj->byte_field(displacement);  break;
1981     case T_SHORT:   value = is_volatile ? obj->short_field_acquire(displacement)  : obj->short_field(displacement); break;
1982     case T_CHAR:    value = is_volatile ? obj->char_field_acquire(displacement)   : obj->char_field(displacement);  break;
1983     case T_FLOAT:
1984     case T_INT:     value = is_volatile ? obj->int_field_acquire(displacement)    : obj->int_field(displacement);   break;
1985     case T_DOUBLE:
1986     case T_LONG:    value = is_volatile ? obj->long_field_acquire(displacement)   : obj->long_field(displacement);  break;
1987 
1988     case T_OBJECT: {
1989       if (displacement == java_lang_Class::component_mirror_offset() && java_lang_Class::is_instance(obj()) &&
1990           (java_lang_Class::as_Klass(obj()) == NULL || !java_lang_Class::as_Klass(obj())->is_array_klass())) {
1991         // Class.componentType for non-array classes can transiently contain an int[] that's
1992         // used for locking so always return null to mimic Class.getComponentType()
1993         return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_NULL_POINTER());
1994       }
1995 
1996       oop value = is_volatile ? obj->obj_field_acquire(displacement) : obj->obj_field(displacement);
1997       if (value == NULL) {
1998         return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_NULL_POINTER());
1999       } else {
2000         if (value != NULL && !oopDesc::is_oop(value)) {
2001           // Throw an exception to improve debuggability.  This check isn't totally reliable because
2002           // is_oop doesn't try to be completety safe but for most invalid values it provides a good
2003           // enough answer.  It possible to crash in the is_oop call but that just means the crash happens
2004           // closer to where things went wrong.
2005           JVMCI_THROW_MSG_NULL(InternalError, err_msg("Read bad oop " INTPTR_FORMAT " at offset " JLONG_FORMAT " in object " INTPTR_FORMAT " of type %s",
2006                                                       p2i(value), displacement, p2i(obj()), obj->klass()->external_name()));
2007         }
2008 
2009         JVMCIObject result = JVMCIENV->get_object_constant(value);
2010         return JVMCIENV->get_jobject(result);
2011       }
2012     }
2013 
2014     default:
2015       ShouldNotReachHere();
2016   }
2017   JVMCIObject result = JVMCIENV->call_JavaConstant_forPrimitive(kind, value, JVMCI_CHECK_NULL);
2018   return JVMCIENV->get_jobject(result);
2019 C2V_END
2020 
2021 C2V_VMENTRY_0(jboolean, isInstance, (JNIEnv* env, jobject, jobject holder, jobject object))
2022   if (object == NULL || holder == NULL) {
2023     JVMCI_THROW_0(NullPointerException);
2024   }
2025   Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_0);
2026   Klass* klass = JVMCIENV->asKlass(JVMCIENV->wrap(holder));
2027   return obj->is_a(klass);
2028 C2V_END
2029 
2030 C2V_VMENTRY_0(jboolean, isAssignableFrom, (JNIEnv* env, jobject, jobject holder, jobject otherHolder))
2031   if (holder == NULL || otherHolder == NULL) {
2032     JVMCI_THROW_0(NullPointerException);
2033   }
2034   Klass* klass = JVMCIENV->asKlass(JVMCIENV->wrap(holder));
2035   Klass* otherKlass = JVMCIENV->asKlass(JVMCIENV->wrap(otherHolder));
2036   return otherKlass->is_subtype_of(klass);
2037 C2V_END
2038 
2039 C2V_VMENTRY_0(jboolean, isTrustedForIntrinsics, (JNIEnv* env, jobject, jobject holder))
2040   if (holder == NULL) {
2041     JVMCI_THROW_0(NullPointerException);
2042   }
2043   InstanceKlass* ik = InstanceKlass::cast(JVMCIENV->asKlass(JVMCIENV->wrap(holder)));
2044   if (ik->class_loader_data()->is_boot_class_loader_data() || ik->class_loader_data()->is_platform_class_loader_data()) {
2045     return true;
2046   }
2047   return false;
2048 C2V_END
2049 
2050 C2V_VMENTRY_NULL(jobject, asJavaType, (JNIEnv* env, jobject, jobject object))
2051   if (object == NULL) {
2052     JVMCI_THROW_0(NullPointerException);
2053   }
2054   Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_NULL);
2055   if (java_lang_Class::is_instance(obj())) {
2056     if (java_lang_Class::is_primitive(obj())) {
2057       JVMCIObject type = JVMCIENV->get_jvmci_primitive_type(java_lang_Class::primitive_type(obj()));
2058       return JVMCIENV->get_jobject(type);
2059     }
2060     Klass* klass = java_lang_Class::as_Klass(obj());
2061     JVMCIKlassHandle klass_handle(THREAD);
2062     klass_handle = klass;
2063     JVMCIObject type = JVMCIENV->get_jvmci_type(klass_handle, JVMCI_CHECK_NULL);
2064     return JVMCIENV->get_jobject(type);
2065   }
2066   return NULL;
2067 C2V_END
2068 
2069 
2070 C2V_VMENTRY_NULL(jobject, asString, (JNIEnv* env, jobject, jobject object))
2071   if (object == NULL) {
2072     JVMCI_THROW_0(NullPointerException);
2073   }
2074   Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_NULL);
2075   const char* str = java_lang_String::as_utf8_string(obj());
2076   JVMCIObject result = JVMCIENV->create_string(str, JVMCI_CHECK_NULL);
2077   return JVMCIENV->get_jobject(result);
2078 C2V_END
2079 
2080 
2081 C2V_VMENTRY_0(jboolean, equals, (JNIEnv* env, jobject, jobject x, jlong xHandle, jobject y, jlong yHandle))
2082   if (x == NULL || y == NULL) {
2083     JVMCI_THROW_0(NullPointerException);
2084   }
2085   return JVMCIENV->resolve_handle(xHandle) == JVMCIENV->resolve_handle(yHandle);
2086 C2V_END
2087 
2088 C2V_VMENTRY_NULL(jobject, getJavaMirror, (JNIEnv* env, jobject, jobject object))
2089   if (object == NULL) {
2090     JVMCI_THROW_0(NullPointerException);
2091   }
2092   JVMCIObject base_object = JVMCIENV->wrap(object);
2093   Handle mirror;
2094   if (JVMCIENV->isa_HotSpotResolvedObjectTypeImpl(base_object)) {
2095     mirror = Handle(THREAD, JVMCIENV->asKlass(base_object)->java_mirror());
2096   } else if (JVMCIENV->isa_HotSpotResolvedPrimitiveType(base_object)) {
2097     mirror = JVMCIENV->asConstant(JVMCIENV->get_HotSpotResolvedPrimitiveType_mirror(base_object), JVMCI_CHECK_NULL);
2098   } else {
2099     JVMCI_THROW_MSG_NULL(IllegalArgumentException,
2100                          err_msg("Unexpected type: %s", JVMCIENV->klass_name(base_object)));
2101  }
2102   JVMCIObject result = JVMCIENV->get_object_constant(mirror());
2103   return JVMCIENV->get_jobject(result);
2104 C2V_END
2105 
2106 
2107 C2V_VMENTRY_0(jint, getArrayLength, (JNIEnv* env, jobject, jobject x))
2108   if (x == NULL) {
2109     JVMCI_THROW_0(NullPointerException);
2110   }
2111   Handle xobj = JVMCIENV->asConstant(JVMCIENV->wrap(x), JVMCI_CHECK_0);
2112   if (xobj->klass()->is_array_klass()) {
2113     return arrayOop(xobj())->length();
2114   }
2115   return -1;
2116  C2V_END
2117 
2118 
2119 C2V_VMENTRY_NULL(jobject, readArrayElement, (JNIEnv* env, jobject, jobject x, int index))
2120   if (x == NULL) {
2121     JVMCI_THROW_0(NullPointerException);
2122   }
2123   Handle xobj = JVMCIENV->asConstant(JVMCIENV->wrap(x), JVMCI_CHECK_NULL);
2124   if (xobj->klass()->is_array_klass()) {
2125     arrayOop array = arrayOop(xobj());
2126     BasicType element_type = ArrayKlass::cast(array->klass())->element_type();
2127     if (index < 0 || index >= array->length()) {
2128       return NULL;
2129     }
2130     JVMCIObject result;
2131 
2132     if (element_type == T_OBJECT) {
2133       result = JVMCIENV->get_object_constant(objArrayOop(xobj())->obj_at(index));
2134       if (result.is_null()) {
2135         result = JVMCIENV->get_JavaConstant_NULL_POINTER();
2136       }
2137     } else {
2138       jvalue value;
2139       switch (element_type) {
2140         case T_DOUBLE:        value.d = typeArrayOop(xobj())->double_at(index);        break;
2141         case T_FLOAT:         value.f = typeArrayOop(xobj())->float_at(index);         break;
2142         case T_LONG:          value.j = typeArrayOop(xobj())->long_at(index);          break;
2143         case T_INT:           value.i = typeArrayOop(xobj())->int_at(index);            break;
2144         case T_SHORT:         value.s = typeArrayOop(xobj())->short_at(index);          break;
2145         case T_CHAR:          value.c = typeArrayOop(xobj())->char_at(index);           break;
2146         case T_BYTE:          value.b = typeArrayOop(xobj())->byte_at(index);           break;
2147         case T_BOOLEAN:       value.z = typeArrayOop(xobj())->byte_at(index) & 1;       break;
2148         default:              ShouldNotReachHere();
2149       }
2150       result = JVMCIENV->create_box(element_type, &value, JVMCI_CHECK_NULL);
2151     }
2152     assert(!result.is_null(), "must have a value");
2153     return JVMCIENV->get_jobject(result);
2154   }
2155   return NULL;;
2156 C2V_END
2157 
2158 
2159 C2V_VMENTRY_0(jint, arrayBaseOffset, (JNIEnv* env, jobject, jobject kind))
2160   if (kind == NULL) {
2161     JVMCI_THROW_0(NullPointerException);
2162   }
2163   BasicType type = JVMCIENV->kindToBasicType(JVMCIENV->wrap(kind), JVMCI_CHECK_0);
2164   return arrayOopDesc::header_size(type) * HeapWordSize;
2165 C2V_END
2166 
2167 C2V_VMENTRY_0(jint, arrayIndexScale, (JNIEnv* env, jobject, jobject kind))
2168   if (kind == NULL) {
2169     JVMCI_THROW_0(NullPointerException);
2170   }
2171   BasicType type = JVMCIENV->kindToBasicType(JVMCIENV->wrap(kind), JVMCI_CHECK_0);
2172   return type2aelembytes(type);
2173 C2V_END
2174 
2175 C2V_VMENTRY(void, deleteGlobalHandle, (JNIEnv* env, jobject, jlong h))
2176   jobject handle = (jobject)(address)h;
2177   if (handle != NULL) {
2178     JVMCIENV->runtime()->destroy_global(handle);
2179   }
2180 }
2181 
2182 static void requireJVMCINativeLibrary(JVMCI_TRAPS) {
2183   if (!UseJVMCINativeLibrary) {
2184     JVMCI_THROW_MSG(UnsupportedOperationException, "JVMCI shared library is not enabled (requires -XX:+UseJVMCINativeLibrary)");
2185   }
2186 }
2187 
2188 C2V_VMENTRY_NULL(jlongArray, registerNativeMethods, (JNIEnv* env, jobject, jclass mirror))
2189   requireJVMCINativeLibrary(JVMCI_CHECK_NULL);
2190   requireInHotSpot("registerNativeMethods", JVMCI_CHECK_NULL);
2191   char* sl_path;
2192   void* sl_handle;
2193   JVMCIRuntime* runtime = JVMCI::compiler_runtime();
2194   {
2195     // Ensure the JVMCI shared library runtime is initialized.
2196     JVMCIEnv __peer_jvmci_env__(thread, false, __FILE__, __LINE__);
2197     JVMCIEnv* peerEnv = &__peer_jvmci_env__;
2198     HandleMark hm(THREAD);
2199     JVMCIObject receiver = runtime->get_HotSpotJVMCIRuntime(peerEnv);
2200     if (peerEnv->has_pending_exception()) {
2201       peerEnv->describe_pending_exception(true);
2202     }
2203     sl_handle = JVMCI::get_shared_library(sl_path, false);
2204     if (sl_handle == NULL) {
2205       JVMCI_THROW_MSG_0(InternalError, err_msg("Error initializing JVMCI runtime %d", runtime->id()));
2206     }
2207   }
2208 
2209   if (mirror == NULL) {
2210     JVMCI_THROW_0(NullPointerException);
2211   }
2212   Klass* klass = java_lang_Class::as_Klass(JNIHandles::resolve(mirror));
2213   if (klass == NULL || !klass->is_instance_klass()) {
2214     JVMCI_THROW_MSG_0(IllegalArgumentException, "clazz is for primitive type");
2215   }
2216 
2217   InstanceKlass* iklass = InstanceKlass::cast(klass);
2218   for (int i = 0; i < iklass->methods()->length(); i++) {
2219     methodHandle method(THREAD, iklass->methods()->at(i));
2220     if (method->is_native()) {
2221 
2222       // Compute argument size
2223       int args_size = 1                             // JNIEnv
2224                     + (method->is_static() ? 1 : 0) // class for static methods
2225                     + method->size_of_parameters(); // actual parameters
2226 
2227       // 1) Try JNI short style
2228       stringStream st;
2229       char* pure_name = NativeLookup::pure_jni_name(method);
2230       guarantee(pure_name != NULL, "Illegal native method name encountered");
2231       os::print_jni_name_prefix_on(&st, args_size);
2232       st.print_raw(pure_name);
2233       os::print_jni_name_suffix_on(&st, args_size);
2234       char* jni_name = st.as_string();
2235 
2236       address entry = (address) os::dll_lookup(sl_handle, jni_name);
2237       if (entry == NULL) {
2238         // 2) Try JNI long style
2239         st.reset();
2240         char* long_name = NativeLookup::long_jni_name(method);
2241         guarantee(long_name != NULL, "Illegal native method name encountered");
2242         os::print_jni_name_prefix_on(&st, args_size);
2243         st.print_raw(pure_name);
2244         st.print_raw(long_name);
2245         os::print_jni_name_suffix_on(&st, args_size);
2246         char* jni_long_name = st.as_string();
2247         entry = (address) os::dll_lookup(sl_handle, jni_long_name);
2248         if (entry == NULL) {
2249           JVMCI_THROW_MSG_0(UnsatisfiedLinkError, err_msg("%s [neither %s nor %s exist in %s]",
2250               method->name_and_sig_as_C_string(),
2251               jni_name, jni_long_name, sl_path));
2252         }
2253       }
2254 
2255       if (method->has_native_function() && entry != method->native_function()) {
2256         JVMCI_THROW_MSG_0(UnsatisfiedLinkError, err_msg("%s [cannot re-link from " PTR_FORMAT " to " PTR_FORMAT "]",
2257             method->name_and_sig_as_C_string(), p2i(method->native_function()), p2i(entry)));
2258       }
2259       method->set_native_function(entry, Method::native_bind_event_is_interesting);
2260       log_debug(jni, resolve)("[Dynamic-linking native method %s.%s ... JNI] @ " PTR_FORMAT,
2261                               method->method_holder()->external_name(),
2262                               method->name()->as_C_string(),
2263                               p2i((void*) entry));
2264     }
2265   }
2266 
2267   typeArrayOop info_oop = oopFactory::new_longArray(4, CHECK_0);
2268   jlongArray info = (jlongArray) JNIHandles::make_local(THREAD, info_oop);
2269   runtime->init_JavaVM_info(info, JVMCI_CHECK_0);
2270   return info;
2271 }
2272 
2273 C2V_VMENTRY_PREFIX(jboolean, isCurrentThreadAttached, (JNIEnv* env, jobject c2vm))
2274   if (thread == NULL) {
2275     // Called from unattached JVMCI shared library thread
2276     return false;
2277   }
2278   JVMCITraceMark jtm("isCurrentThreadAttached");
2279   if (thread->jni_environment() == env) {
2280     C2V_BLOCK(jboolean, isCurrentThreadAttached, (JNIEnv* env, jobject))
2281     requireJVMCINativeLibrary(JVMCI_CHECK_0);
2282     JVMCIRuntime* runtime = JVMCI::compiler_runtime();
2283     if (runtime == NULL || !runtime->has_shared_library_javavm()) {
2284       JVMCI_THROW_MSG_0(IllegalStateException, "Require JVMCI shared library JavaVM to be initialized in isCurrentThreadAttached");
2285     }
2286     JNIEnv* peerEnv;
2287     return runtime->GetEnv(thread, (void**) &peerEnv, JNI_VERSION_1_2) == JNI_OK;
2288   }
2289   return true;
2290 C2V_END
2291 
2292 C2V_VMENTRY_PREFIX(jlong, getCurrentJavaThread, (JNIEnv* env, jobject c2vm))
2293   if (thread == NULL) {
2294     // Called from unattached JVMCI shared library thread
2295     return 0L;
2296   }
2297   JVMCITraceMark jtm("getCurrentJavaThread");
2298   return (jlong) p2i(thread);
2299 C2V_END
2300 
2301 C2V_VMENTRY_PREFIX(jboolean, attachCurrentThread, (JNIEnv* env, jobject c2vm, jbyteArray name, jboolean as_daemon))
2302   if (thread == NULL) {
2303     // Called from unattached JVMCI shared library thread
2304     guarantee(name != NULL, "libjvmci caller must pass non-null name");
2305 
2306     extern struct JavaVM_ main_vm;
2307     JNIEnv* hotspotEnv;
2308 
2309     int name_len = env->GetArrayLength(name);
2310     char name_buf[64]; // Cannot use Resource heap as it requires a current thread
2311     int to_copy = MIN2(name_len, (int) sizeof(name_buf) - 1);
2312     env->GetByteArrayRegion(name, 0, to_copy, (jbyte*) name_buf);
2313     name_buf[to_copy] = '\0';
2314     JavaVMAttachArgs attach_args;
2315     attach_args.version = JNI_VERSION_1_2;
2316     attach_args.name = name_buf;
2317     attach_args.group = NULL;
2318     jint res = as_daemon ? main_vm.AttachCurrentThreadAsDaemon((void**) &hotspotEnv, &attach_args) :
2319                            main_vm.AttachCurrentThread((void**) &hotspotEnv, &attach_args);
2320     if (res != JNI_OK) {
2321       JNI_THROW_("attachCurrentThread", InternalError, err_msg("Trying to attach thread returned %d", res), false);
2322     }
2323     return true;
2324   }
2325   JVMCITraceMark jtm("attachCurrentThread");
2326   if (thread->jni_environment() == env) {
2327     // Called from HotSpot
2328     C2V_BLOCK(jboolean, attachCurrentThread, (JNIEnv* env, jobject, jboolean))
2329     requireJVMCINativeLibrary(JVMCI_CHECK_0);
2330     JVMCIRuntime* runtime = JVMCI::compiler_runtime();
2331     if (runtime == NULL || !runtime->has_shared_library_javavm()) {
2332         JVMCI_THROW_MSG_0(IllegalStateException, "Require JVMCI shared library JavaVM to be initialized in attachCurrentThread");
2333     }
2334 
2335     JavaVMAttachArgs attach_args;
2336     attach_args.version = JNI_VERSION_1_2;
2337     attach_args.name = const_cast<char*>(thread->name());
2338     attach_args.group = NULL;
2339     JNIEnv* peerJNIEnv;
2340     if (runtime->GetEnv(thread, (void**) &peerJNIEnv, JNI_VERSION_1_2) == JNI_OK) {
2341       return false;
2342     }
2343     jint res = as_daemon ? runtime->AttachCurrentThreadAsDaemon(thread, (void**) &peerJNIEnv, &attach_args) :
2344                            runtime->AttachCurrentThread(thread, (void**) &peerJNIEnv, &attach_args);
2345 
2346     if (res == JNI_OK) {
2347       guarantee(peerJNIEnv != NULL, "must be");
2348       JVMCI_event_1("attached to JavaVM for JVMCI runtime %d", runtime->id());
2349       return true;
2350     }
2351     JVMCI_THROW_MSG_0(InternalError, err_msg("Error %d while attaching %s", res, attach_args.name));
2352   }
2353   // Called from JVMCI shared library
2354   return false;
2355 C2V_END
2356 
2357 C2V_VMENTRY_PREFIX(void, detachCurrentThread, (JNIEnv* env, jobject c2vm))
2358   if (thread == NULL) {
2359     // Called from unattached JVMCI shared library thread
2360     JNI_THROW("detachCurrentThread", IllegalStateException, "Cannot detach non-attached thread");
2361   }
2362   JVMCITraceMark jtm("detachCurrentThread");
2363   if (thread->jni_environment() == env) {
2364     // Called from HotSpot
2365     C2V_BLOCK(void, detachCurrentThread, (JNIEnv* env, jobject))
2366     requireJVMCINativeLibrary(JVMCI_CHECK);
2367     requireInHotSpot("detachCurrentThread", JVMCI_CHECK);
2368     JVMCIRuntime* runtime = JVMCI::compiler_runtime();
2369     if (runtime == NULL || !runtime->has_shared_library_javavm()) {
2370       JVMCI_THROW_MSG(IllegalStateException, "Require JVMCI shared library JavaVM to be initialized in detachCurrentThread");
2371     }
2372     JNIEnv* peerJNIEnv;
2373     if (runtime->GetEnv(thread, (void**) &peerJNIEnv, JNI_VERSION_1_2) != JNI_OK) {
2374       JVMCI_THROW_MSG(IllegalStateException, err_msg("Cannot detach non-attached thread: %s", thread->name()));
2375     }
2376     jint res = runtime->DetachCurrentThread(thread);
2377     if (res != JNI_OK) {
2378       JVMCI_THROW_MSG(InternalError, err_msg("Error %d while attaching %s", res, thread->name()));
2379     }
2380   } else {
2381     // Called from attached JVMCI shared library thread
2382     extern struct JavaVM_ main_vm;
2383     jint res = main_vm.DetachCurrentThread();
2384     if (res != JNI_OK) {
2385       JNI_THROW("detachCurrentThread", InternalError, "Cannot detach non-attached thread");
2386     }
2387   }
2388 C2V_END
2389 
2390 C2V_VMENTRY_0(jlong, translate, (JNIEnv* env, jobject, jobject obj_handle))
2391   requireJVMCINativeLibrary(JVMCI_CHECK_0);
2392   if (obj_handle == NULL) {
2393     return 0L;
2394   }
2395   JVMCIEnv __peer_jvmci_env__(thread, !JVMCIENV->is_hotspot(), __FILE__, __LINE__);
2396   JVMCIEnv* peerEnv = &__peer_jvmci_env__;
2397   JVMCIEnv* thisEnv = JVMCIENV;
2398 
2399   JVMCIObject obj = thisEnv->wrap(obj_handle);
2400   JVMCIObject result;
2401   if (thisEnv->isa_HotSpotResolvedJavaMethodImpl(obj)) {
2402     methodHandle method(THREAD, thisEnv->asMethod(obj));
2403     result = peerEnv->get_jvmci_method(method, JVMCI_CHECK_0);
2404   } else if (thisEnv->isa_HotSpotResolvedObjectTypeImpl(obj)) {
2405     Klass* klass = thisEnv->asKlass(obj);
2406     JVMCIKlassHandle klass_handle(THREAD);
2407     klass_handle = klass;
2408     result = peerEnv->get_jvmci_type(klass_handle, JVMCI_CHECK_0);
2409   } else if (thisEnv->isa_HotSpotResolvedPrimitiveType(obj)) {
2410     BasicType type = JVMCIENV->kindToBasicType(JVMCIENV->get_HotSpotResolvedPrimitiveType_kind(obj), JVMCI_CHECK_0);
2411     result = peerEnv->get_jvmci_primitive_type(type);
2412   } else if (thisEnv->isa_IndirectHotSpotObjectConstantImpl(obj) ||
2413              thisEnv->isa_DirectHotSpotObjectConstantImpl(obj)) {
2414     Handle constant = thisEnv->asConstant(obj, JVMCI_CHECK_0);
2415     result = peerEnv->get_object_constant(constant());
2416   } else if (thisEnv->isa_HotSpotNmethod(obj)) {
2417     nmethodLocker locker;
2418     nmethod* nm = JVMCIENV->get_nmethod(obj, locker);
2419     if (nm != NULL) {
2420       JVMCINMethodData* data = nm->jvmci_nmethod_data();
2421       if (data != NULL) {
2422         if (peerEnv->is_hotspot()) {
2423           // Only the mirror in the HotSpot heap is accessible
2424           // through JVMCINMethodData
2425           oop nmethod_mirror = data->get_nmethod_mirror(nm, /* phantom_ref */ true);
2426           if (nmethod_mirror != NULL) {
2427             result = HotSpotJVMCI::wrap(nmethod_mirror);
2428           }
2429         }
2430       }
2431     }
2432     if (result.is_null()) {
2433       JVMCIObject methodObject = thisEnv->get_HotSpotNmethod_method(obj);
2434       methodHandle mh(THREAD, thisEnv->asMethod(methodObject));
2435       jboolean isDefault = thisEnv->get_HotSpotNmethod_isDefault(obj);
2436       jlong compileIdSnapshot = thisEnv->get_HotSpotNmethod_compileIdSnapshot(obj);
2437       JVMCIObject name_string = thisEnv->get_InstalledCode_name(obj);
2438       const char* cstring = name_string.is_null() ? NULL : thisEnv->as_utf8_string(name_string);
2439       // Create a new HotSpotNmethod instance in the peer runtime
2440       result = peerEnv->new_HotSpotNmethod(mh, cstring, isDefault, compileIdSnapshot, JVMCI_CHECK_0);
2441       if (nm == NULL) {
2442         // nmethod must have been unloaded
2443       } else {
2444         // Link the new HotSpotNmethod to the nmethod
2445         peerEnv->initialize_installed_code(result, nm, JVMCI_CHECK_0);
2446         // Only HotSpotNmethod instances in the HotSpot heap are tracked directly by the runtime.
2447         if (peerEnv->is_hotspot()) {
2448           JVMCINMethodData* data = nm->jvmci_nmethod_data();
2449           if (data == NULL) {
2450             JVMCI_THROW_MSG_0(IllegalArgumentException, "Cannot set HotSpotNmethod mirror for default nmethod");
2451           }
2452           if (data->get_nmethod_mirror(nm, /* phantom_ref */ false) != NULL) {
2453             JVMCI_THROW_MSG_0(IllegalArgumentException, "Cannot overwrite existing HotSpotNmethod mirror for nmethod");
2454           }
2455           oop nmethod_mirror = HotSpotJVMCI::resolve(result);
2456           data->set_nmethod_mirror(nm, nmethod_mirror);
2457         }
2458       }
2459     }
2460   } else {
2461     JVMCI_THROW_MSG_0(IllegalArgumentException,
2462                 err_msg("Cannot translate object of type: %s", thisEnv->klass_name(obj)));
2463   }
2464   return (jlong) peerEnv->make_global(result).as_jobject();
2465 }
2466 
2467 C2V_VMENTRY_NULL(jobject, unhand, (JNIEnv* env, jobject, jlong obj_handle))
2468   requireJVMCINativeLibrary(JVMCI_CHECK_NULL);
2469   if (obj_handle == 0L) {
2470     return NULL;
2471   }
2472   jobject global_handle = (jobject) obj_handle;
2473   JVMCIObject global_handle_obj = JVMCIENV->wrap((jobject) obj_handle);
2474   jobject result = JVMCIENV->make_local(global_handle_obj).as_jobject();
2475 
2476   JVMCIENV->destroy_global(global_handle_obj);
2477   return result;
2478 }
2479 
2480 C2V_VMENTRY(void, updateHotSpotNmethod, (JNIEnv* env, jobject, jobject code_handle))
2481   JVMCIObject code = JVMCIENV->wrap(code_handle);
2482   // Execute this operation for the side effect of updating the InstalledCode state
2483   nmethodLocker locker;
2484   JVMCIENV->get_nmethod(code, locker);
2485 }
2486 
2487 C2V_VMENTRY_NULL(jbyteArray, getCode, (JNIEnv* env, jobject, jobject code_handle))
2488   JVMCIObject code = JVMCIENV->wrap(code_handle);
2489   nmethodLocker locker;
2490   CodeBlob* cb = JVMCIENV->get_code_blob(code, locker);
2491   if (cb == NULL) {
2492     return NULL;
2493   }
2494   int code_size = cb->code_size();
2495   JVMCIPrimitiveArray result = JVMCIENV->new_byteArray(code_size, JVMCI_CHECK_NULL);
2496   JVMCIENV->copy_bytes_from((jbyte*) cb->code_begin(), result, 0, code_size);
2497   return JVMCIENV->get_jbyteArray(result);
2498 }
2499 
2500 C2V_VMENTRY_NULL(jobject, asReflectionExecutable, (JNIEnv* env, jobject, jobject jvmci_method))
2501   requireInHotSpot("asReflectionExecutable", JVMCI_CHECK_NULL);
2502   methodHandle m(THREAD, JVMCIENV->asMethod(jvmci_method));
2503   oop executable;
2504   if (m->is_class_initializer()) {
2505       JVMCI_THROW_MSG_NULL(IllegalArgumentException,
2506           "Cannot create java.lang.reflect.Method for class initializer");
2507   }
2508   else if (m->is_object_constructor() || m->is_static_init_factory()) {
2509     executable = Reflection::new_constructor(m, CHECK_NULL);
2510   } else {
2511     executable = Reflection::new_method(m, false, CHECK_NULL);
2512   }
2513   return JNIHandles::make_local(THREAD, executable);
2514 }
2515 
2516 C2V_VMENTRY_NULL(jobject, asReflectionField, (JNIEnv* env, jobject, jobject jvmci_type, jint index))
2517   requireInHotSpot("asReflectionField", JVMCI_CHECK_NULL);
2518   Klass* klass = JVMCIENV->asKlass(jvmci_type);
2519   if (!klass->is_instance_klass()) {
2520     JVMCI_THROW_MSG_NULL(IllegalArgumentException,
2521         err_msg("Expected non-primitive type, got %s", klass->external_name()));
2522   }
2523   InstanceKlass* iklass = InstanceKlass::cast(klass);
2524   Array<u2>* fields = iklass->fields();
2525   if (index < 0 ||index > fields->length()) {
2526     JVMCI_THROW_MSG_NULL(IllegalArgumentException,
2527         err_msg("Field index %d out of bounds for %s", index, klass->external_name()));
2528   }
2529   fieldDescriptor fd(iklass, index);
2530   oop reflected = Reflection::new_field(&fd, CHECK_NULL);
2531   return JNIHandles::make_local(THREAD, reflected);
2532 }
2533 
2534 C2V_VMENTRY_NULL(jobjectArray, getFailedSpeculations, (JNIEnv* env, jobject, jlong failed_speculations_address, jobjectArray current))
2535   FailedSpeculation* head = *((FailedSpeculation**)(address) failed_speculations_address);
2536   int result_length = 0;
2537   for (FailedSpeculation* fs = head; fs != NULL; fs = fs->next()) {
2538     result_length++;
2539   }
2540   int current_length = 0;
2541   JVMCIObjectArray current_array = NULL;
2542   if (current != NULL) {
2543     current_array = JVMCIENV->wrap(current);
2544     current_length = JVMCIENV->get_length(current_array);
2545     if (current_length == result_length) {
2546       // No new failures
2547       return current;
2548     }
2549   }
2550   JVMCIObjectArray result = JVMCIENV->new_byte_array_array(result_length, JVMCI_CHECK_NULL);
2551   int result_index = 0;
2552   for (FailedSpeculation* fs = head; result_index < result_length; fs = fs->next()) {
2553     assert(fs != NULL, "npe");
2554     JVMCIPrimitiveArray entry;
2555     if (result_index < current_length) {
2556       entry = (JVMCIPrimitiveArray) JVMCIENV->get_object_at(current_array, result_index);
2557     } else {
2558       entry = JVMCIENV->new_byteArray(fs->data_len(), JVMCI_CHECK_NULL);
2559       JVMCIENV->copy_bytes_from((jbyte*) fs->data(), entry, 0, fs->data_len());
2560     }
2561     JVMCIENV->put_object_at(result, result_index++, entry);
2562   }
2563   return JVMCIENV->get_jobjectArray(result);
2564 }
2565 
2566 C2V_VMENTRY_0(jlong, getFailedSpeculationsAddress, (JNIEnv* env, jobject, jobject jvmci_method))
2567   methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));
2568   MethodData* method_data = method->method_data();
2569   if (method_data == NULL) {
2570     ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
2571     method_data = MethodData::allocate(loader_data, method, CHECK_0);
2572     method->set_method_data(method_data);
2573   }
2574   return (jlong) method_data->get_failed_speculations_address();
2575 }
2576 
2577 C2V_VMENTRY(void, releaseFailedSpeculations, (JNIEnv* env, jobject, jlong failed_speculations_address))
2578   FailedSpeculation::free_failed_speculations((FailedSpeculation**)(address) failed_speculations_address);
2579 }
2580 
2581 C2V_VMENTRY_0(jboolean, addFailedSpeculation, (JNIEnv* env, jobject, jlong failed_speculations_address, jbyteArray speculation_obj))
2582   JVMCIPrimitiveArray speculation_handle = JVMCIENV->wrap(speculation_obj);
2583   int speculation_len = JVMCIENV->get_length(speculation_handle);
2584   char* speculation = NEW_RESOURCE_ARRAY(char, speculation_len);
2585   JVMCIENV->copy_bytes_to(speculation_handle, (jbyte*) speculation, 0, speculation_len);
2586   return FailedSpeculation::add_failed_speculation(NULL, (FailedSpeculation**)(address) failed_speculations_address, (address) speculation, speculation_len);
2587 }
2588 
2589 C2V_VMENTRY(void, callSystemExit, (JNIEnv* env, jobject, jint status))
2590   JavaValue result(T_VOID);
2591   JavaCallArguments jargs(1);
2592   jargs.push_int(status);
2593   JavaCalls::call_static(&result,
2594                        vmClasses::System_klass(),
2595                        vmSymbols::exit_method_name(),
2596                        vmSymbols::int_void_signature(),
2597                        &jargs,
2598                        CHECK);
2599 }
2600 
2601 C2V_VMENTRY_0(jlong, ticksNow, (JNIEnv* env, jobject))
2602   return CompilerEvent::ticksNow();
2603 }
2604 
2605 C2V_VMENTRY_0(jint, registerCompilerPhase, (JNIEnv* env, jobject, jstring jphase_name))
2606 #if INCLUDE_JFR
2607   JVMCIObject phase_name = JVMCIENV->wrap(jphase_name);
2608   const char *name = JVMCIENV->as_utf8_string(phase_name);
2609   return CompilerEvent::PhaseEvent::get_phase_id(name, true, true, true);
2610 #else
2611   return -1;
2612 #endif // !INCLUDE_JFR
2613 }
2614 
2615 C2V_VMENTRY(void, notifyCompilerPhaseEvent, (JNIEnv* env, jobject, jlong startTime, jint phase, jint compileId, jint level))
2616   EventCompilerPhase event;
2617   if (event.should_commit()) {
2618     CompilerEvent::PhaseEvent::post(event, startTime, phase, compileId, level);
2619   }
2620 }
2621 
2622 C2V_VMENTRY(void, notifyCompilerInliningEvent, (JNIEnv* env, jobject, jint compileId, jobject caller, jobject callee, jboolean succeeded, jstring jmessage, jint bci))
2623   EventCompilerInlining event;
2624   if (event.should_commit()) {
2625     Method* caller_method = JVMCIENV->asMethod(caller);
2626     Method* callee_method = JVMCIENV->asMethod(callee);
2627     JVMCIObject message = JVMCIENV->wrap(jmessage);
2628     CompilerEvent::InlineEvent::post(event, compileId, caller_method, callee_method, succeeded, JVMCIENV->as_utf8_string(message), bci);
2629   }
2630 }
2631 
2632 C2V_VMENTRY(void, setThreadLocalObject, (JNIEnv* env, jobject, jint id, jobject value))
2633   requireInHotSpot("setThreadLocalObject", JVMCI_CHECK);
2634   if (id == 0) {
2635     thread->set_jvmci_reserved_oop0(JNIHandles::resolve(value));
2636     return;
2637   }
2638   THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
2639             err_msg("%d is not a valid thread local id", id));
2640 }
2641 
2642 C2V_VMENTRY_NULL(jobject, getThreadLocalObject, (JNIEnv* env, jobject, jint id))
2643   requireInHotSpot("getThreadLocalObject", JVMCI_CHECK_NULL);
2644   if (id == 0) {
2645     return JNIHandles::make_local(thread->get_jvmci_reserved_oop0());
2646   }
2647   THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
2648               err_msg("%d is not a valid thread local id", id));
2649 }
2650 
2651 C2V_VMENTRY(void, setThreadLocalLong, (JNIEnv* env, jobject, jint id, jlong value))
2652   requireInHotSpot("setThreadLocalLong", JVMCI_CHECK);
2653   if (id == 0) {
2654     thread->set_jvmci_reserved0(value);
2655   } else if (id == 1) {
2656     thread->set_jvmci_reserved1(value);
2657   } else {
2658     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
2659               err_msg("%d is not a valid thread local id", id));
2660   }
2661 }
2662 
2663 C2V_VMENTRY_0(jlong, getThreadLocalLong, (JNIEnv* env, jobject, jint id))
2664   requireInHotSpot("getThreadLocalLong", JVMCI_CHECK_0);
2665   if (id == 0) {
2666     return thread->get_jvmci_reserved0();
2667   } else if (id == 1) {
2668     return thread->get_jvmci_reserved1();
2669   } else {
2670     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
2671                 err_msg("%d is not a valid thread local id", id));
2672   }
2673 }
2674 
2675 #define CC (char*)  /*cast a literal from (const char*)*/
2676 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &(c2v_ ## f))
2677 
2678 #define STRING                  "Ljava/lang/String;"
2679 #define OBJECT                  "Ljava/lang/Object;"
2680 #define CLASS                   "Ljava/lang/Class;"
2681 #define OBJECTCONSTANT          "Ljdk/vm/ci/hotspot/HotSpotObjectConstantImpl;"
2682 #define HANDLECONSTANT          "Ljdk/vm/ci/hotspot/IndirectHotSpotObjectConstantImpl;"
2683 #define EXECUTABLE              "Ljava/lang/reflect/Executable;"
2684 #define STACK_TRACE_ELEMENT     "Ljava/lang/StackTraceElement;"
2685 #define INSTALLED_CODE          "Ljdk/vm/ci/code/InstalledCode;"
2686 #define TARGET_DESCRIPTION      "Ljdk/vm/ci/code/TargetDescription;"
2687 #define BYTECODE_FRAME          "Ljdk/vm/ci/code/BytecodeFrame;"
2688 #define JAVACONSTANT            "Ljdk/vm/ci/meta/JavaConstant;"
2689 #define INSPECTED_FRAME_VISITOR "Ljdk/vm/ci/code/stack/InspectedFrameVisitor;"
2690 #define RESOLVED_METHOD         "Ljdk/vm/ci/meta/ResolvedJavaMethod;"
2691 #define HS_RESOLVED_METHOD      "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaMethodImpl;"
2692 #define HS_RESOLVED_KLASS       "Ljdk/vm/ci/hotspot/HotSpotResolvedObjectTypeImpl;"
2693 #define HS_RESOLVED_TYPE        "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaType;"
2694 #define HS_RESOLVED_FIELD       "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaField;"
2695 #define HS_INSTALLED_CODE       "Ljdk/vm/ci/hotspot/HotSpotInstalledCode;"
2696 #define HS_NMETHOD              "Ljdk/vm/ci/hotspot/HotSpotNmethod;"
2697 #define HS_CONSTANT_POOL        "Ljdk/vm/ci/hotspot/HotSpotConstantPool;"
2698 #define HS_COMPILED_CODE        "Ljdk/vm/ci/hotspot/HotSpotCompiledCode;"
2699 #define HS_CONFIG               "Ljdk/vm/ci/hotspot/HotSpotVMConfig;"
2700 #define HS_METADATA             "Ljdk/vm/ci/hotspot/HotSpotMetaData;"
2701 #define HS_STACK_FRAME_REF      "Ljdk/vm/ci/hotspot/HotSpotStackFrameReference;"
2702 #define HS_SPECULATION_LOG      "Ljdk/vm/ci/hotspot/HotSpotSpeculationLog;"
2703 #define METASPACE_OBJECT        "Ljdk/vm/ci/hotspot/MetaspaceObject;"
2704 #define REFLECTION_EXECUTABLE   "Ljava/lang/reflect/Executable;"
2705 #define REFLECTION_FIELD        "Ljava/lang/reflect/Field;"
2706 #define METASPACE_METHOD_DATA   "J"
2707 
2708 JNINativeMethod CompilerToVM::methods[] = {
2709   {CC "getBytecode",                                  CC "(" HS_RESOLVED_METHOD ")[B",                                                      FN_PTR(getBytecode)},
2710   {CC "getExceptionTableStart",                       CC "(" HS_RESOLVED_METHOD ")J",                                                       FN_PTR(getExceptionTableStart)},
2711   {CC "getExceptionTableLength",                      CC "(" HS_RESOLVED_METHOD ")I",                                                       FN_PTR(getExceptionTableLength)},
2712   {CC "findUniqueConcreteMethod",                     CC "(" HS_RESOLVED_KLASS HS_RESOLVED_METHOD ")" HS_RESOLVED_METHOD,                   FN_PTR(findUniqueConcreteMethod)},
2713   {CC "getImplementor",                               CC "(" HS_RESOLVED_KLASS ")" HS_RESOLVED_KLASS,                                       FN_PTR(getImplementor)},
2714   {CC "getStackTraceElement",                         CC "(" HS_RESOLVED_METHOD "I)" STACK_TRACE_ELEMENT,                                   FN_PTR(getStackTraceElement)},
2715   {CC "methodIsIgnoredBySecurityStackWalk",           CC "(" HS_RESOLVED_METHOD ")Z",                                                       FN_PTR(methodIsIgnoredBySecurityStackWalk)},
2716   {CC "setNotInlinableOrCompilable",                  CC "(" HS_RESOLVED_METHOD ")V",                                                       FN_PTR(setNotInlinableOrCompilable)},
2717   {CC "isCompilable",                                 CC "(" HS_RESOLVED_METHOD ")Z",                                                       FN_PTR(isCompilable)},
2718   {CC "hasNeverInlineDirective",                      CC "(" HS_RESOLVED_METHOD ")Z",                                                       FN_PTR(hasNeverInlineDirective)},
2719   {CC "shouldInlineMethod",                           CC "(" HS_RESOLVED_METHOD ")Z",                                                       FN_PTR(shouldInlineMethod)},
2720   {CC "lookupType",                                   CC "(" STRING HS_RESOLVED_KLASS "Z)" HS_RESOLVED_TYPE,                                FN_PTR(lookupType)},
2721   {CC "getArrayType",                                 CC "(" HS_RESOLVED_TYPE ")" HS_RESOLVED_KLASS,                                        FN_PTR(getArrayType)},
2722   {CC "lookupClass",                                  CC "(" CLASS ")" HS_RESOLVED_TYPE,                                                    FN_PTR(lookupClass)},
2723   {CC "lookupNameInPool",                             CC "(" HS_CONSTANT_POOL "I)" STRING,                                                  FN_PTR(lookupNameInPool)},
2724   {CC "lookupNameAndTypeRefIndexInPool",              CC "(" HS_CONSTANT_POOL "I)I",                                                        FN_PTR(lookupNameAndTypeRefIndexInPool)},
2725   {CC "lookupSignatureInPool",                        CC "(" HS_CONSTANT_POOL "I)" STRING,                                                  FN_PTR(lookupSignatureInPool)},
2726   {CC "lookupKlassRefIndexInPool",                    CC "(" HS_CONSTANT_POOL "I)I",                                                        FN_PTR(lookupKlassRefIndexInPool)},
2727   {CC "lookupKlassInPool",                            CC "(" HS_CONSTANT_POOL "I)Ljava/lang/Object;",                                       FN_PTR(lookupKlassInPool)},
2728   {CC "lookupAppendixInPool",                         CC "(" HS_CONSTANT_POOL "I)" OBJECTCONSTANT,                                          FN_PTR(lookupAppendixInPool)},
2729   {CC "lookupMethodInPool",                           CC "(" HS_CONSTANT_POOL "IB)" HS_RESOLVED_METHOD,                                     FN_PTR(lookupMethodInPool)},
2730   {CC "constantPoolRemapInstructionOperandFromCache", CC "(" HS_CONSTANT_POOL "I)I",                                                        FN_PTR(constantPoolRemapInstructionOperandFromCache)},
2731   {CC "resolvePossiblyCachedConstantInPool",          CC "(" HS_CONSTANT_POOL "I)" JAVACONSTANT,                                            FN_PTR(resolvePossiblyCachedConstantInPool)},
2732   {CC "resolveTypeInPool",                            CC "(" HS_CONSTANT_POOL "I)" HS_RESOLVED_KLASS,                                       FN_PTR(resolveTypeInPool)},
2733   {CC "resolveFieldInPool",                           CC "(" HS_CONSTANT_POOL "I" HS_RESOLVED_METHOD "B[I)" HS_RESOLVED_KLASS,              FN_PTR(resolveFieldInPool)},
2734   {CC "resolveInvokeDynamicInPool",                   CC "(" HS_CONSTANT_POOL "I)V",                                                        FN_PTR(resolveInvokeDynamicInPool)},
2735   {CC "resolveInvokeHandleInPool",                    CC "(" HS_CONSTANT_POOL "I)V",                                                        FN_PTR(resolveInvokeHandleInPool)},
2736   {CC "isResolvedInvokeHandleInPool",                 CC "(" HS_CONSTANT_POOL "I)I",                                                        FN_PTR(isResolvedInvokeHandleInPool)},
2737   {CC "resolveMethod",                                CC "(" HS_RESOLVED_KLASS HS_RESOLVED_METHOD HS_RESOLVED_KLASS ")" HS_RESOLVED_METHOD, FN_PTR(resolveMethod)},
2738   {CC "getSignaturePolymorphicHolders",               CC "()[" STRING,                                                                      FN_PTR(getSignaturePolymorphicHolders)},
2739   {CC "getVtableIndexForInterfaceMethod",             CC "(" HS_RESOLVED_KLASS HS_RESOLVED_METHOD ")I",                                     FN_PTR(getVtableIndexForInterfaceMethod)},
2740   {CC "getClassInitializer",                          CC "(" HS_RESOLVED_KLASS ")" HS_RESOLVED_METHOD,                                      FN_PTR(getClassInitializer)},
2741   {CC "hasFinalizableSubclass",                       CC "(" HS_RESOLVED_KLASS ")Z",                                                        FN_PTR(hasFinalizableSubclass)},
2742   {CC "getMaxCallTargetOffset",                       CC "(J)J",                                                                            FN_PTR(getMaxCallTargetOffset)},
2743   {CC "asResolvedJavaMethod",                         CC "(" EXECUTABLE ")" HS_RESOLVED_METHOD,                                             FN_PTR(asResolvedJavaMethod)},
2744   {CC "getResolvedJavaMethod",                        CC "(" OBJECTCONSTANT "J)" HS_RESOLVED_METHOD,                                        FN_PTR(getResolvedJavaMethod)},
2745   {CC "getConstantPool",                              CC "(" METASPACE_OBJECT ")" HS_CONSTANT_POOL,                                         FN_PTR(getConstantPool)},
2746   {CC "getResolvedJavaType0",                         CC "(Ljava/lang/Object;JZ)" HS_RESOLVED_KLASS,                                        FN_PTR(getResolvedJavaType0)},
2747   {CC "readConfiguration",                            CC "()[" OBJECT,                                                                      FN_PTR(readConfiguration)},
2748   {CC "installCode",                                  CC "(" TARGET_DESCRIPTION HS_COMPILED_CODE INSTALLED_CODE "J[B)I",                    FN_PTR(installCode)},
2749   {CC "getMetadata",                                  CC "(" TARGET_DESCRIPTION HS_COMPILED_CODE HS_METADATA ")I",                          FN_PTR(getMetadata)},
2750   {CC "resetCompilationStatistics",                   CC "()V",                                                                             FN_PTR(resetCompilationStatistics)},
2751   {CC "disassembleCodeBlob",                          CC "(" INSTALLED_CODE ")" STRING,                                                     FN_PTR(disassembleCodeBlob)},
2752   {CC "executeHotSpotNmethod",                        CC "([" OBJECT HS_NMETHOD ")" OBJECT,                                                 FN_PTR(executeHotSpotNmethod)},
2753   {CC "getLineNumberTable",                           CC "(" HS_RESOLVED_METHOD ")[J",                                                      FN_PTR(getLineNumberTable)},
2754   {CC "getLocalVariableTableStart",                   CC "(" HS_RESOLVED_METHOD ")J",                                                       FN_PTR(getLocalVariableTableStart)},
2755   {CC "getLocalVariableTableLength",                  CC "(" HS_RESOLVED_METHOD ")I",                                                       FN_PTR(getLocalVariableTableLength)},
2756   {CC "reprofile",                                    CC "(" HS_RESOLVED_METHOD ")V",                                                       FN_PTR(reprofile)},
2757   {CC "invalidateHotSpotNmethod",                     CC "(" HS_NMETHOD ")V",                                                               FN_PTR(invalidateHotSpotNmethod)},
2758   {CC "collectCounters",                              CC "()[J",                                                                            FN_PTR(collectCounters)},
2759   {CC "getCountersSize",                              CC "()I",                                                                             FN_PTR(getCountersSize)},
2760   {CC "setCountersSize",                              CC "(I)Z",                                                                            FN_PTR(setCountersSize)},
2761   {CC "allocateCompileId",                            CC "(" HS_RESOLVED_METHOD "I)I",                                                      FN_PTR(allocateCompileId)},
2762   {CC "isMature",                                     CC "(" METASPACE_METHOD_DATA ")Z",                                                    FN_PTR(isMature)},
2763   {CC "hasCompiledCodeForOSR",                        CC "(" HS_RESOLVED_METHOD "II)Z",                                                     FN_PTR(hasCompiledCodeForOSR)},
2764   {CC "getSymbol",                                    CC "(J)" STRING,                                                                      FN_PTR(getSymbol)},
2765   {CC "iterateFrames",                                CC "([" RESOLVED_METHOD "[" RESOLVED_METHOD "I" INSPECTED_FRAME_VISITOR ")" OBJECT,   FN_PTR(iterateFrames)},
2766   {CC "materializeVirtualObjects",                    CC "(" HS_STACK_FRAME_REF "Z)V",                                                      FN_PTR(materializeVirtualObjects)},
2767   {CC "shouldDebugNonSafepoints",                     CC "()Z",                                                                             FN_PTR(shouldDebugNonSafepoints)},
2768   {CC "writeDebugOutput",                             CC "(JIZ)V",                                                                          FN_PTR(writeDebugOutput)},
2769   {CC "flushDebugOutput",                             CC "()V",                                                                             FN_PTR(flushDebugOutput)},
2770   {CC "methodDataProfileDataSize",                    CC "(JI)I",                                                                           FN_PTR(methodDataProfileDataSize)},
2771   {CC "getFingerprint",                               CC "(J)J",                                                                            FN_PTR(getFingerprint)},
2772   {CC "interpreterFrameSize",                         CC "(" BYTECODE_FRAME ")I",                                                           FN_PTR(interpreterFrameSize)},
2773   {CC "compileToBytecode",                            CC "(" OBJECTCONSTANT ")V",                                                           FN_PTR(compileToBytecode)},
2774   {CC "getFlagValue",                                 CC "(" STRING ")" OBJECT,                                                             FN_PTR(getFlagValue)},
2775   {CC "getInterfaces",                                CC "(" HS_RESOLVED_KLASS ")[" HS_RESOLVED_KLASS,                                      FN_PTR(getInterfaces)},
2776   {CC "getComponentType",                             CC "(" HS_RESOLVED_KLASS ")" HS_RESOLVED_TYPE,                                        FN_PTR(getComponentType)},
2777   {CC "ensureInitialized",                            CC "(" HS_RESOLVED_KLASS ")V",                                                        FN_PTR(ensureInitialized)},
2778   {CC "ensureLinked",                                 CC "(" HS_RESOLVED_KLASS ")V",                                                        FN_PTR(ensureLinked)},
2779   {CC "getIdentityHashCode",                          CC "(" OBJECTCONSTANT ")I",                                                           FN_PTR(getIdentityHashCode)},
2780   {CC "isInternedString",                             CC "(" OBJECTCONSTANT ")Z",                                                           FN_PTR(isInternedString)},
2781   {CC "unboxPrimitive",                               CC "(" OBJECTCONSTANT ")" OBJECT,                                                     FN_PTR(unboxPrimitive)},
2782   {CC "boxPrimitive",                                 CC "(" OBJECT ")" OBJECTCONSTANT,                                                     FN_PTR(boxPrimitive)},
2783   {CC "getDeclaredConstructors",                      CC "(" HS_RESOLVED_KLASS ")[" RESOLVED_METHOD,                                        FN_PTR(getDeclaredConstructors)},
2784   {CC "getDeclaredMethods",                           CC "(" HS_RESOLVED_KLASS ")[" RESOLVED_METHOD,                                        FN_PTR(getDeclaredMethods)},
2785   {CC "readFieldValue",                               CC "(" HS_RESOLVED_KLASS HS_RESOLVED_KLASS "JZLjdk/vm/ci/meta/JavaKind;)" JAVACONSTANT, FN_PTR(readFieldValue)},
2786   {CC "readFieldValue",                               CC "(" OBJECTCONSTANT HS_RESOLVED_KLASS "JZLjdk/vm/ci/meta/JavaKind;)" JAVACONSTANT,  FN_PTR(readFieldValue)},
2787   {CC "isInstance",                                   CC "(" HS_RESOLVED_KLASS OBJECTCONSTANT ")Z",                                         FN_PTR(isInstance)},
2788   {CC "isAssignableFrom",                             CC "(" HS_RESOLVED_KLASS HS_RESOLVED_KLASS ")Z",                                      FN_PTR(isAssignableFrom)},
2789   {CC "isTrustedForIntrinsics",                       CC "(" HS_RESOLVED_KLASS ")Z",                                                        FN_PTR(isTrustedForIntrinsics)},
2790   {CC "asJavaType",                                   CC "(" OBJECTCONSTANT ")" HS_RESOLVED_TYPE,                                           FN_PTR(asJavaType)},
2791   {CC "asString",                                     CC "(" OBJECTCONSTANT ")" STRING,                                                     FN_PTR(asString)},
2792   {CC "equals",                                       CC "(" OBJECTCONSTANT "J" OBJECTCONSTANT "J)Z",                                       FN_PTR(equals)},
2793   {CC "getJavaMirror",                                CC "(" HS_RESOLVED_TYPE ")" OBJECTCONSTANT,                                           FN_PTR(getJavaMirror)},
2794   {CC "getArrayLength",                               CC "(" OBJECTCONSTANT ")I",                                                           FN_PTR(getArrayLength)},
2795   {CC "readArrayElement",                             CC "(" OBJECTCONSTANT "I)Ljava/lang/Object;",                                         FN_PTR(readArrayElement)},
2796   {CC "arrayBaseOffset",                              CC "(Ljdk/vm/ci/meta/JavaKind;)I",                                                    FN_PTR(arrayBaseOffset)},
2797   {CC "arrayIndexScale",                              CC "(Ljdk/vm/ci/meta/JavaKind;)I",                                                    FN_PTR(arrayIndexScale)},
2798   {CC "deleteGlobalHandle",                           CC "(J)V",                                                                            FN_PTR(deleteGlobalHandle)},
2799   {CC "registerNativeMethods",                        CC "(" CLASS ")[J",                                                                   FN_PTR(registerNativeMethods)},
2800   {CC "isCurrentThreadAttached",                      CC "()Z",                                                                             FN_PTR(isCurrentThreadAttached)},
2801   {CC "getCurrentJavaThread",                         CC "()J",                                                                             FN_PTR(getCurrentJavaThread)},
2802   {CC "attachCurrentThread",                          CC "([BZ)Z",                                                                          FN_PTR(attachCurrentThread)},
2803   {CC "detachCurrentThread",                          CC "()V",                                                                             FN_PTR(detachCurrentThread)},
2804   {CC "translate",                                    CC "(" OBJECT ")J",                                                                   FN_PTR(translate)},
2805   {CC "unhand",                                       CC "(J)" OBJECT,                                                                      FN_PTR(unhand)},
2806   {CC "updateHotSpotNmethod",                         CC "(" HS_NMETHOD ")V",                                                               FN_PTR(updateHotSpotNmethod)},
2807   {CC "getCode",                                      CC "(" HS_INSTALLED_CODE ")[B",                                                       FN_PTR(getCode)},
2808   {CC "asReflectionExecutable",                       CC "(" HS_RESOLVED_METHOD ")" REFLECTION_EXECUTABLE,                                  FN_PTR(asReflectionExecutable)},
2809   {CC "asReflectionField",                            CC "(" HS_RESOLVED_KLASS "I)" REFLECTION_FIELD,                                       FN_PTR(asReflectionField)},
2810   {CC "getFailedSpeculations",                        CC "(J[[B)[[B",                                                                       FN_PTR(getFailedSpeculations)},
2811   {CC "getFailedSpeculationsAddress",                 CC "(" HS_RESOLVED_METHOD ")J",                                                       FN_PTR(getFailedSpeculationsAddress)},
2812   {CC "releaseFailedSpeculations",                    CC "(J)V",                                                                            FN_PTR(releaseFailedSpeculations)},
2813   {CC "addFailedSpeculation",                         CC "(J[B)Z",                                                                          FN_PTR(addFailedSpeculation)},
2814   {CC "callSystemExit",                               CC "(I)V",                                                                            FN_PTR(callSystemExit)},
2815   {CC "ticksNow",                                     CC "()J",                                                                             FN_PTR(ticksNow)},
2816   {CC "getThreadLocalObject",                         CC "(I)" OBJECT,                                                                      FN_PTR(getThreadLocalObject)},
2817   {CC "setThreadLocalObject",                         CC "(I" OBJECT ")V",                                                                  FN_PTR(setThreadLocalObject)},
2818   {CC "getThreadLocalLong",                           CC "(I)J",                                                                            FN_PTR(getThreadLocalLong)},
2819   {CC "setThreadLocalLong",                           CC "(IJ)V",                                                                           FN_PTR(setThreadLocalLong)},
2820   {CC "registerCompilerPhase",                        CC "(" STRING ")I",                                                                   FN_PTR(registerCompilerPhase)},
2821   {CC "notifyCompilerPhaseEvent",                     CC "(JIII)V",                                                                         FN_PTR(notifyCompilerPhaseEvent)},
2822   {CC "notifyCompilerInliningEvent",                  CC "(I" HS_RESOLVED_METHOD HS_RESOLVED_METHOD "ZLjava/lang/String;I)V",               FN_PTR(notifyCompilerInliningEvent)},
2823 };
2824 
2825 int CompilerToVM::methods_count() {
2826   return sizeof(methods) / sizeof(JNINativeMethod);
2827 }