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