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