1 /*
   2  * Copyright (c) 2011, 2017, 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/javaClasses.inline.hpp"
  26 #include "code/codeCache.hpp"
  27 #include "code/scopeDesc.hpp"
  28 #include "interpreter/linkResolver.hpp"
  29 #include "memory/oopFactory.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/generateOopMap.hpp"
  32 #include "oops/fieldStreams.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "oops/objArrayOop.inline.hpp"
  35 #include "runtime/fieldDescriptor.hpp"
  36 #include "runtime/javaCalls.hpp"
  37 #include "jvmci/jvmciRuntime.hpp"
  38 #include "compiler/abstractCompiler.hpp"
  39 #include "compiler/compileBroker.hpp"
  40 #include "compiler/compilerOracle.hpp"
  41 #include "compiler/disassembler.hpp"
  42 #include "compiler/oopMap.hpp"
  43 #include "jvmci/jvmciCompilerToVM.hpp"
  44 #include "jvmci/jvmciCompiler.hpp"
  45 #include "jvmci/jvmciEnv.hpp"
  46 #include "jvmci/jvmciJavaClasses.hpp"
  47 #include "jvmci/jvmciCodeInstaller.hpp"
  48 #include "jvmci/vmStructs_jvmci.hpp"
  49 #include "gc/g1/heapRegion.hpp"
  50 #include "runtime/javaCalls.hpp"
  51 #include "runtime/deoptimization.hpp"
  52 #include "runtime/timerTrace.hpp"
  53 #include "runtime/vframe.hpp"
  54 #include "runtime/vframe_hp.hpp"
  55 #include "runtime/vmStructs.hpp"
  56 #include "utilities/resourceHash.hpp"
  57 
  58 
  59 void JNIHandleMark::push_jni_handle_block() {
  60   JavaThread* thread = JavaThread::current();
  61   if (thread != NULL) {
  62     // Allocate a new block for JNI handles.
  63     // Inlined code from jni_PushLocalFrame()
  64     JNIHandleBlock* java_handles = ((JavaThread*)thread)->active_handles();
  65     JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread);
  66     assert(compile_handles != NULL && java_handles != NULL, "should not be NULL");
  67     compile_handles->set_pop_frame_link(java_handles);
  68     thread->set_active_handles(compile_handles);
  69   }
  70 }
  71 
  72 void JNIHandleMark::pop_jni_handle_block() {
  73   JavaThread* thread = JavaThread::current();
  74   if (thread != NULL) {
  75     // Release our JNI handle block
  76     JNIHandleBlock* compile_handles = thread->active_handles();
  77     JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
  78     thread->set_active_handles(java_handles);
  79     compile_handles->set_pop_frame_link(NULL);
  80     JNIHandleBlock::release_block(compile_handles, thread); // may block
  81   }
  82 }
  83 
  84 // Entry to native method implementation that transitions current thread to '_thread_in_vm'.
  85 #define C2V_VMENTRY(result_type, name, signature) \
  86   JNIEXPORT result_type JNICALL c2v_ ## name signature { \
  87   TRACE_jvmci_1("CompilerToVM::" #name); \
  88   TRACE_CALL(result_type, jvmci_ ## name signature) \
  89   JVMCI_VM_ENTRY_MARK; \
  90 
  91 #define C2V_END }
  92 
  93 oop CompilerToVM::get_jvmci_method(const methodHandle& method, TRAPS) {
  94   if (method() != NULL) {
  95     JavaValue result(T_OBJECT);
  96     JavaCallArguments args;
  97     args.push_long((jlong) (address) method());
  98     JavaCalls::call_static(&result, SystemDictionary::HotSpotResolvedJavaMethodImpl_klass(), vmSymbols::fromMetaspace_name(), vmSymbols::method_fromMetaspace_signature(), &args, CHECK_NULL);
  99 
 100     return (oop)result.get_jobject();
 101   }
 102   return NULL;
 103 }
 104 
 105 oop CompilerToVM::get_jvmci_type(Klass* klass, TRAPS) {
 106   if (klass != NULL) {
 107     JavaValue result(T_OBJECT);
 108     JavaCallArguments args;
 109     args.push_oop(Handle(THREAD, klass->java_mirror()));
 110     JavaCalls::call_static(&result, SystemDictionary::HotSpotResolvedObjectTypeImpl_klass(), vmSymbols::fromMetaspace_name(), vmSymbols::klass_fromMetaspace_signature(), &args, CHECK_NULL);
 111 
 112     return (oop)result.get_jobject();
 113   }
 114   return NULL;
 115 }
 116 
 117 
 118 int CompilerToVM::Data::Klass_vtable_start_offset;
 119 int CompilerToVM::Data::Klass_vtable_length_offset;
 120 
 121 int CompilerToVM::Data::Method_extra_stack_entries;
 122 
 123 address CompilerToVM::Data::SharedRuntime_ic_miss_stub;
 124 address CompilerToVM::Data::SharedRuntime_handle_wrong_method_stub;
 125 address CompilerToVM::Data::SharedRuntime_deopt_blob_unpack;
 126 address CompilerToVM::Data::SharedRuntime_deopt_blob_uncommon_trap;
 127 
 128 size_t CompilerToVM::Data::ThreadLocalAllocBuffer_alignment_reserve;
 129 
 130 CollectedHeap* CompilerToVM::Data::Universe_collectedHeap;
 131 int CompilerToVM::Data::Universe_base_vtable_size;
 132 address CompilerToVM::Data::Universe_narrow_oop_base;
 133 int CompilerToVM::Data::Universe_narrow_oop_shift;
 134 address CompilerToVM::Data::Universe_narrow_klass_base;
 135 int CompilerToVM::Data::Universe_narrow_klass_shift;
 136 void* CompilerToVM::Data::Universe_non_oop_bits;
 137 uintptr_t CompilerToVM::Data::Universe_verify_oop_mask;
 138 uintptr_t CompilerToVM::Data::Universe_verify_oop_bits;
 139 
 140 bool       CompilerToVM::Data::_supports_inline_contig_alloc;
 141 HeapWord** CompilerToVM::Data::_heap_end_addr;
 142 HeapWord* volatile* CompilerToVM::Data::_heap_top_addr;
 143 int CompilerToVM::Data::_max_oop_map_stack_offset;
 144 
 145 jbyte* CompilerToVM::Data::cardtable_start_address;
 146 int CompilerToVM::Data::cardtable_shift;
 147 
 148 int CompilerToVM::Data::vm_page_size;
 149 
 150 int CompilerToVM::Data::sizeof_vtableEntry = sizeof(vtableEntry);
 151 int CompilerToVM::Data::sizeof_ExceptionTableElement = sizeof(ExceptionTableElement);
 152 int CompilerToVM::Data::sizeof_LocalVariableTableElement = sizeof(LocalVariableTableElement);
 153 int CompilerToVM::Data::sizeof_ConstantPool = sizeof(ConstantPool);
 154 int CompilerToVM::Data::sizeof_SymbolPointer = sizeof(Symbol*);
 155 int CompilerToVM::Data::sizeof_narrowKlass = sizeof(narrowKlass);
 156 int CompilerToVM::Data::sizeof_arrayOopDesc = sizeof(arrayOopDesc);
 157 int CompilerToVM::Data::sizeof_BasicLock = sizeof(BasicLock);
 158 
 159 address CompilerToVM::Data::dsin;
 160 address CompilerToVM::Data::dcos;
 161 address CompilerToVM::Data::dtan;
 162 address CompilerToVM::Data::dexp;
 163 address CompilerToVM::Data::dlog;
 164 address CompilerToVM::Data::dlog10;
 165 address CompilerToVM::Data::dpow;
 166 
 167 address CompilerToVM::Data::symbol_init;
 168 address CompilerToVM::Data::symbol_clinit;
 169 
 170 void CompilerToVM::Data::initialize(TRAPS) {
 171   Klass_vtable_start_offset = in_bytes(Klass::vtable_start_offset());
 172   Klass_vtable_length_offset = in_bytes(Klass::vtable_length_offset());
 173 
 174   Method_extra_stack_entries = Method::extra_stack_entries();
 175 
 176   SharedRuntime_ic_miss_stub = SharedRuntime::get_ic_miss_stub();
 177   SharedRuntime_handle_wrong_method_stub = SharedRuntime::get_handle_wrong_method_stub();
 178   SharedRuntime_deopt_blob_unpack = SharedRuntime::deopt_blob()->unpack();
 179   SharedRuntime_deopt_blob_uncommon_trap = SharedRuntime::deopt_blob()->uncommon_trap();
 180 
 181   ThreadLocalAllocBuffer_alignment_reserve = ThreadLocalAllocBuffer::alignment_reserve();
 182 
 183   Universe_collectedHeap = Universe::heap();
 184   Universe_base_vtable_size = Universe::base_vtable_size();
 185   Universe_narrow_oop_base = Universe::narrow_oop_base();
 186   Universe_narrow_oop_shift = Universe::narrow_oop_shift();
 187   Universe_narrow_klass_base = Universe::narrow_klass_base();
 188   Universe_narrow_klass_shift = Universe::narrow_klass_shift();
 189   Universe_non_oop_bits = Universe::non_oop_word();
 190   Universe_verify_oop_mask = Universe::verify_oop_mask();
 191   Universe_verify_oop_bits = Universe::verify_oop_bits();
 192 
 193   _supports_inline_contig_alloc = Universe::heap()->supports_inline_contig_alloc();
 194   _heap_end_addr = _supports_inline_contig_alloc ? Universe::heap()->end_addr() : (HeapWord**) -1;
 195   _heap_top_addr = _supports_inline_contig_alloc ? Universe::heap()->top_addr() : (HeapWord* volatile*) -1;
 196 
 197   _max_oop_map_stack_offset = (OopMapValue::register_mask - VMRegImpl::stack2reg(0)->value()) * VMRegImpl::stack_slot_size;
 198   int max_oop_map_stack_index = _max_oop_map_stack_offset / VMRegImpl::stack_slot_size;
 199   assert(OopMapValue::legal_vm_reg_name(VMRegImpl::stack2reg(max_oop_map_stack_index)), "should be valid");
 200   assert(!OopMapValue::legal_vm_reg_name(VMRegImpl::stack2reg(max_oop_map_stack_index + 1)), "should be invalid");
 201 
 202   symbol_init = (address) vmSymbols::object_initializer_name();
 203   symbol_clinit = (address) vmSymbols::class_initializer_name();
 204 
 205   BarrierSet* bs = Universe::heap()->barrier_set();
 206   if (bs->is_a(BarrierSet::CardTableModRef)) {
 207     jbyte* base = barrier_set_cast<CardTableModRefBS>(bs)->byte_map_base;
 208     assert(base != 0, "unexpected byte_map_base");
 209     cardtable_start_address = base;
 210     cardtable_shift = CardTableModRefBS::card_shift;
 211   } else {
 212     // No card mark barriers
 213     cardtable_start_address = 0;
 214     cardtable_shift = 0;
 215   }
 216 
 217   vm_page_size = os::vm_page_size();
 218 
 219 #define SET_TRIGFUNC(name)                                      \
 220   if (StubRoutines::name() != NULL) {                           \
 221     name = StubRoutines::name();                                \
 222   } else {                                                      \
 223     name = CAST_FROM_FN_PTR(address, SharedRuntime::name);      \
 224   }
 225 
 226   SET_TRIGFUNC(dsin);
 227   SET_TRIGFUNC(dcos);
 228   SET_TRIGFUNC(dtan);
 229   SET_TRIGFUNC(dexp);
 230   SET_TRIGFUNC(dlog10);
 231   SET_TRIGFUNC(dlog);
 232   SET_TRIGFUNC(dpow);
 233 
 234 #undef SET_TRIGFUNC
 235 }
 236 
 237 objArrayHandle CompilerToVM::initialize_intrinsics(TRAPS) {
 238   objArrayHandle vmIntrinsics = oopFactory::new_objArray_handle(VMIntrinsicMethod::klass(), (vmIntrinsics::ID_LIMIT - 1), CHECK_(objArrayHandle()));
 239   int index = 0;
 240   // The intrinsics for a class are usually adjacent to each other.
 241   // When they are, the string for the class name can be reused.
 242   vmSymbols::SID kls_sid = vmSymbols::NO_SID;
 243   Handle kls_str;
 244 #define SID_ENUM(n) vmSymbols::VM_SYMBOL_ENUM_NAME(n)
 245 #define VM_SYMBOL_TO_STRING(s) \
 246   java_lang_String::create_from_symbol(vmSymbols::symbol_at(SID_ENUM(s)), CHECK_(objArrayHandle()))
 247 #define VM_INTRINSIC_INFO(id, kls, name, sig, ignore_fcode) {             \
 248     instanceHandle vmIntrinsicMethod = InstanceKlass::cast(VMIntrinsicMethod::klass())->allocate_instance_handle(CHECK_(objArrayHandle())); \
 249     if (kls_sid != SID_ENUM(kls)) {                                       \
 250       kls_str = VM_SYMBOL_TO_STRING(kls);                                 \
 251       kls_sid = SID_ENUM(kls);                                            \
 252     }                                                                     \
 253     Handle name_str = VM_SYMBOL_TO_STRING(name);                          \
 254     Handle sig_str = VM_SYMBOL_TO_STRING(sig);                            \
 255     VMIntrinsicMethod::set_declaringClass(vmIntrinsicMethod, kls_str());  \
 256     VMIntrinsicMethod::set_name(vmIntrinsicMethod, name_str());           \
 257     VMIntrinsicMethod::set_descriptor(vmIntrinsicMethod, sig_str());      \
 258     VMIntrinsicMethod::set_id(vmIntrinsicMethod, vmIntrinsics::id);       \
 259       vmIntrinsics->obj_at_put(index++, vmIntrinsicMethod());             \
 260   }
 261 
 262   VM_INTRINSICS_DO(VM_INTRINSIC_INFO, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE)
 263 #undef SID_ENUM
 264 #undef VM_SYMBOL_TO_STRING
 265 #undef VM_INTRINSIC_INFO
 266   assert(index == vmIntrinsics::ID_LIMIT - 1, "must be");
 267 
 268   return vmIntrinsics;
 269 }
 270 
 271 /**
 272  * The set of VM flags known to be used.
 273  */
 274 #define PREDEFINED_CONFIG_FLAGS(do_bool_flag, do_intx_flag, do_uintx_flag) \
 275   do_intx_flag(AllocateInstancePrefetchLines)                              \
 276   do_intx_flag(AllocatePrefetchDistance)                                   \
 277   do_intx_flag(AllocatePrefetchInstr)                                      \
 278   do_intx_flag(AllocatePrefetchLines)                                      \
 279   do_intx_flag(AllocatePrefetchStepSize)                                   \
 280   do_intx_flag(AllocatePrefetchStyle)                                      \
 281   do_intx_flag(BciProfileWidth)                                            \
 282   do_bool_flag(BootstrapJVMCI)                                             \
 283   do_bool_flag(CITime)                                                     \
 284   do_bool_flag(CITimeEach)                                                 \
 285   do_uintx_flag(CodeCacheSegmentSize)                                      \
 286   do_intx_flag(CodeEntryAlignment)                                         \
 287   do_bool_flag(CompactFields)                                              \
 288   NOT_PRODUCT(do_intx_flag(CompileTheWorldStartAt))                        \
 289   NOT_PRODUCT(do_intx_flag(CompileTheWorldStopAt))                         \
 290   do_intx_flag(ContendedPaddingWidth)                                      \
 291   do_bool_flag(DontCompileHugeMethods)                                     \
 292   do_bool_flag(EnableContended)                                            \
 293   do_intx_flag(FieldsAllocationStyle)                                      \
 294   do_bool_flag(FoldStableValues)                                           \
 295   do_bool_flag(ForceUnreachable)                                           \
 296   do_intx_flag(HugeMethodLimit)                                            \
 297   do_bool_flag(Inline)                                                     \
 298   do_intx_flag(JVMCICounterSize)                                           \
 299   do_bool_flag(JVMCIPrintProperties)                                       \
 300   do_bool_flag(JVMCIUseFastLocking)                                        \
 301   do_intx_flag(MethodProfileWidth)                                         \
 302   do_intx_flag(ObjectAlignmentInBytes)                                     \
 303   do_bool_flag(PrintInlining)                                              \
 304   do_bool_flag(ReduceInitialCardMarks)                                     \
 305   do_bool_flag(RestrictContended)                                          \
 306   do_intx_flag(StackReservedPages)                                         \
 307   do_intx_flag(StackShadowPages)                                           \
 308   do_bool_flag(TLABStats)                                                  \
 309   do_uintx_flag(TLABWasteIncrement)                                        \
 310   do_intx_flag(TypeProfileWidth)                                           \
 311   do_bool_flag(UseAESIntrinsics)                                           \
 312   X86_ONLY(do_intx_flag(UseAVX))                                           \
 313   do_bool_flag(UseBiasedLocking)                                           \
 314   do_bool_flag(UseCRC32Intrinsics)                                         \
 315   do_bool_flag(UseCompressedClassPointers)                                 \
 316   do_bool_flag(UseCompressedOops)                                          \
 317   do_bool_flag(UseConcMarkSweepGC)                                         \
 318   X86_ONLY(do_bool_flag(UseCountLeadingZerosInstruction))                  \
 319   X86_ONLY(do_bool_flag(UseCountTrailingZerosInstruction))                 \
 320   do_bool_flag(UseG1GC)                                                    \
 321   COMPILER2_PRESENT(do_bool_flag(UseMontgomeryMultiplyIntrinsic))          \
 322   COMPILER2_PRESENT(do_bool_flag(UseMontgomerySquareIntrinsic))            \
 323   COMPILER2_PRESENT(do_bool_flag(UseMulAddIntrinsic))                      \
 324   COMPILER2_PRESENT(do_bool_flag(UseMultiplyToLenIntrinsic))               \
 325   do_bool_flag(UsePopCountInstruction)                                     \
 326   do_bool_flag(UseSHA1Intrinsics)                                          \
 327   do_bool_flag(UseSHA256Intrinsics)                                        \
 328   do_bool_flag(UseSHA512Intrinsics)                                        \
 329   do_intx_flag(UseSSE)                                                     \
 330   COMPILER2_PRESENT(do_bool_flag(UseSquareToLenIntrinsic))                 \
 331   do_bool_flag(UseStackBanging)                                            \
 332   do_bool_flag(UseTLAB)                                                    \
 333   do_bool_flag(VerifyOops)                                                 \
 334 
 335 #define BOXED_BOOLEAN(name, value) oop name = ((jboolean)(value) ? boxedTrue() : boxedFalse())
 336 #define BOXED_DOUBLE(name, value) oop name; do { jvalue p; p.d = (jdouble) (value); name = java_lang_boxing_object::create(T_DOUBLE, &p, CHECK_NULL);} while(0)
 337 #define BOXED_LONG(name, value) \
 338   oop name; \
 339   do { \
 340     jvalue p; p.j = (jlong) (value); \
 341     Handle* e = longs.get(p.j); \
 342     if (e == NULL) { \
 343       oop o = java_lang_boxing_object::create(T_LONG, &p, CHECK_NULL); \
 344       Handle h(THREAD, o); \
 345       longs.put(p.j, h); \
 346       name = h(); \
 347     } else { \
 348       name = (*e)(); \
 349     } \
 350   } while (0)
 351 
 352 #define CSTRING_TO_JSTRING(name, value) \
 353   Handle name; \
 354   do { \
 355     if (value != NULL) { \
 356       Handle* e = strings.get(value); \
 357       if (e == NULL) { \
 358         Handle h = java_lang_String::create_from_str(value, CHECK_NULL); \
 359         strings.put(value, h); \
 360         name = h; \
 361       } else { \
 362         name = (*e); \
 363       } \
 364     } \
 365   } while (0)
 366 
 367 C2V_VMENTRY(jobjectArray, readConfiguration, (JNIEnv *env))
 368   ResourceMark rm;
 369   HandleMark hm;
 370 
 371   // Used to canonicalize Long and String values.
 372   ResourceHashtable<jlong, Handle> longs;
 373   ResourceHashtable<const char*, Handle, &CompilerToVM::cstring_hash, &CompilerToVM::cstring_equals> strings;
 374 
 375   jvalue prim;
 376   prim.z = true;  oop boxedTrueOop =  java_lang_boxing_object::create(T_BOOLEAN, &prim, CHECK_NULL);
 377   Handle boxedTrue(THREAD, boxedTrueOop);
 378   prim.z = false; oop boxedFalseOop = java_lang_boxing_object::create(T_BOOLEAN, &prim, CHECK_NULL);
 379   Handle boxedFalse(THREAD, boxedFalseOop);
 380 
 381   CompilerToVM::Data::initialize(CHECK_NULL);
 382 
 383   VMField::klass()->initialize(CHECK_NULL);
 384   VMFlag::klass()->initialize(CHECK_NULL);
 385   VMIntrinsicMethod::klass()->initialize(CHECK_NULL);
 386 
 387   int len = JVMCIVMStructs::localHotSpotVMStructs_count();
 388   objArrayHandle vmFields = oopFactory::new_objArray_handle(VMField::klass(), len, CHECK_NULL);
 389   for (int i = 0; i < len ; i++) {
 390     VMStructEntry vmField = JVMCIVMStructs::localHotSpotVMStructs[i];
 391     instanceHandle vmFieldObj = InstanceKlass::cast(VMField::klass())->allocate_instance_handle(CHECK_NULL);
 392     size_t name_buf_len = strlen(vmField.typeName) + strlen(vmField.fieldName) + 2 /* "::" */;
 393     char* name_buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, name_buf_len + 1);
 394     sprintf(name_buf, "%s::%s", vmField.typeName, vmField.fieldName);
 395     CSTRING_TO_JSTRING(name, name_buf);
 396     CSTRING_TO_JSTRING(type, vmField.typeString);
 397     VMField::set_name(vmFieldObj, name());
 398     VMField::set_type(vmFieldObj, type());
 399     VMField::set_offset(vmFieldObj, vmField.offset);
 400     VMField::set_address(vmFieldObj, (jlong) vmField.address);
 401     if (vmField.isStatic && vmField.typeString != NULL) {
 402       if (strcmp(vmField.typeString, "bool") == 0) {
 403         BOXED_BOOLEAN(box, *(jbyte*) vmField.address);
 404         VMField::set_value(vmFieldObj, box);
 405       } else if (strcmp(vmField.typeString, "int") == 0 ||
 406                  strcmp(vmField.typeString, "jint") == 0) {
 407         BOXED_LONG(box, *(jint*) vmField.address);
 408         VMField::set_value(vmFieldObj, box);
 409       } else if (strcmp(vmField.typeString, "uint64_t") == 0) {
 410         BOXED_LONG(box, *(uint64_t*) vmField.address);
 411         VMField::set_value(vmFieldObj, box);
 412       } else if (strcmp(vmField.typeString, "address") == 0 ||
 413                  strcmp(vmField.typeString, "intptr_t") == 0 ||
 414                  strcmp(vmField.typeString, "uintptr_t") == 0 ||
 415                  strcmp(vmField.typeString, "OopHandle") == 0 ||
 416                  strcmp(vmField.typeString, "size_t") == 0 ||
 417                  // All foo* types are addresses.
 418                  vmField.typeString[strlen(vmField.typeString) - 1] == '*') {
 419         BOXED_LONG(box, *((address*) vmField.address));
 420         VMField::set_value(vmFieldObj, box);
 421       } else {
 422         JVMCI_ERROR_NULL("VM field %s has unsupported type %s", name_buf, vmField.typeString);
 423       }
 424     }
 425     vmFields->obj_at_put(i, vmFieldObj());
 426   }
 427 
 428   int ints_len = JVMCIVMStructs::localHotSpotVMIntConstants_count();
 429   int longs_len = JVMCIVMStructs::localHotSpotVMLongConstants_count();
 430   len = ints_len + longs_len;
 431   objArrayHandle vmConstants = oopFactory::new_objArray_handle(SystemDictionary::Object_klass(), len * 2, CHECK_NULL);
 432   int insert = 0;
 433   for (int i = 0; i < ints_len ; i++) {
 434     VMIntConstantEntry c = JVMCIVMStructs::localHotSpotVMIntConstants[i];
 435     CSTRING_TO_JSTRING(name, c.name);
 436     BOXED_LONG(value, c.value);
 437     vmConstants->obj_at_put(insert++, name());
 438     vmConstants->obj_at_put(insert++, value);
 439   }
 440   for (int i = 0; i < longs_len ; i++) {
 441     VMLongConstantEntry c = JVMCIVMStructs::localHotSpotVMLongConstants[i];
 442     CSTRING_TO_JSTRING(name, c.name);
 443     BOXED_LONG(value, c.value);
 444     vmConstants->obj_at_put(insert++, name());
 445     vmConstants->obj_at_put(insert++, value);
 446   }
 447   assert(insert == len * 2, "must be");
 448 
 449   len = JVMCIVMStructs::localHotSpotVMAddresses_count();
 450   objArrayHandle vmAddresses = oopFactory::new_objArray_handle(SystemDictionary::Object_klass(), len * 2, CHECK_NULL);
 451   for (int i = 0; i < len ; i++) {
 452     VMAddressEntry a = JVMCIVMStructs::localHotSpotVMAddresses[i];
 453     CSTRING_TO_JSTRING(name, a.name);
 454     BOXED_LONG(value, a.value);
 455     vmAddresses->obj_at_put(i * 2, name());
 456     vmAddresses->obj_at_put(i * 2 + 1, value);
 457   }
 458 
 459 #define COUNT_FLAG(ignore) +1
 460 #ifdef ASSERT
 461 #define CHECK_FLAG(type, name) { \
 462   Flag* flag = Flag::find_flag(#name, strlen(#name), /*allow_locked*/ true, /* return_flag */ true); \
 463   assert(flag != NULL, "No such flag named " #name); \
 464   assert(flag->is_##type(), "Flag " #name " is not of type " #type); \
 465 }
 466 #else
 467 #define CHECK_FLAG(type, name)
 468 #endif
 469 
 470 #define ADD_FLAG(type, name, convert) { \
 471   CHECK_FLAG(type, name) \
 472   instanceHandle vmFlagObj = InstanceKlass::cast(VMFlag::klass())->allocate_instance_handle(CHECK_NULL); \
 473   CSTRING_TO_JSTRING(fname, #name); \
 474   CSTRING_TO_JSTRING(ftype, #type); \
 475   VMFlag::set_name(vmFlagObj, fname()); \
 476   VMFlag::set_type(vmFlagObj, ftype()); \
 477   convert(value, name); \
 478   VMFlag::set_value(vmFlagObj, value); \
 479   vmFlags->obj_at_put(i++, vmFlagObj()); \
 480 }
 481 #define ADD_BOOL_FLAG(name)  ADD_FLAG(bool, name, BOXED_BOOLEAN)
 482 #define ADD_INTX_FLAG(name)  ADD_FLAG(intx, name, BOXED_LONG)
 483 #define ADD_UINTX_FLAG(name) ADD_FLAG(uintx, name, BOXED_LONG)
 484 
 485   len = 0 + PREDEFINED_CONFIG_FLAGS(COUNT_FLAG, COUNT_FLAG, COUNT_FLAG);
 486   objArrayHandle vmFlags = oopFactory::new_objArray_handle(VMFlag::klass(), len, CHECK_NULL);
 487   int i = 0;
 488   PREDEFINED_CONFIG_FLAGS(ADD_BOOL_FLAG, ADD_INTX_FLAG, ADD_UINTX_FLAG)
 489 
 490   objArrayHandle vmIntrinsics = CompilerToVM::initialize_intrinsics(CHECK_NULL);
 491 
 492   objArrayOop data = oopFactory::new_objArray(SystemDictionary::Object_klass(), 5, CHECK_NULL);
 493   data->obj_at_put(0, vmFields());
 494   data->obj_at_put(1, vmConstants());
 495   data->obj_at_put(2, vmAddresses());
 496   data->obj_at_put(3, vmFlags());
 497   data->obj_at_put(4, vmIntrinsics());
 498 
 499   return (jobjectArray) JNIHandles::make_local(THREAD, data);
 500 #undef COUNT_FLAG
 501 #undef ADD_FLAG
 502 #undef ADD_BOOL_FLAG
 503 #undef ADD_INTX_FLAG
 504 #undef ADD_UINTX_FLAG
 505 #undef CHECK_FLAG
 506 C2V_END
 507 
 508 C2V_VMENTRY(jobject, getFlagValue, (JNIEnv *, jobject c2vm, jobject name_handle))
 509 #define RETURN_BOXED_LONG(value) oop box; jvalue p; p.j = (jlong) (value); box = java_lang_boxing_object::create(T_LONG, &p, CHECK_NULL); return JNIHandles::make_local(THREAD, box);
 510 #define RETURN_BOXED_DOUBLE(value) oop box; jvalue p; p.d = (jdouble) (value); box = java_lang_boxing_object::create(T_DOUBLE, &p, CHECK_NULL); return JNIHandles::make_local(THREAD, box);
 511   Handle name(THREAD, JNIHandles::resolve(name_handle));
 512   if (name.is_null()) {
 513     THROW_0(vmSymbols::java_lang_NullPointerException());
 514   }
 515   ResourceMark rm;
 516   const char* cstring = java_lang_String::as_utf8_string(name());
 517   Flag* flag = Flag::find_flag(cstring, strlen(cstring), /* allow_locked */ true, /* return_flag */ true);
 518   if (flag == NULL) {
 519     return c2vm;
 520   }
 521   if (flag->is_bool()) {
 522     jvalue prim;
 523     prim.z = flag->get_bool();
 524     oop box = java_lang_boxing_object::create(T_BOOLEAN, &prim, CHECK_NULL);
 525     return JNIHandles::make_local(THREAD, box);
 526   } else if (flag->is_ccstr()) {
 527     Handle value = java_lang_String::create_from_str(flag->get_ccstr(), CHECK_NULL);
 528     return JNIHandles::make_local(THREAD, value());
 529   } else if (flag->is_intx()) {
 530     RETURN_BOXED_LONG(flag->get_intx());
 531   } else if (flag->is_int()) {
 532     RETURN_BOXED_LONG(flag->get_int());
 533   } else if (flag->is_uint()) {
 534     RETURN_BOXED_LONG(flag->get_uint());
 535   } else if (flag->is_uint64_t()) {
 536     RETURN_BOXED_LONG(flag->get_uint64_t());
 537   } else if (flag->is_size_t()) {
 538     RETURN_BOXED_LONG(flag->get_size_t());
 539   } else if (flag->is_uintx()) {
 540     RETURN_BOXED_LONG(flag->get_uintx());
 541   } else if (flag->is_double()) {
 542     RETURN_BOXED_DOUBLE(flag->get_double());
 543   } else {
 544     JVMCI_ERROR_NULL("VM flag %s has unsupported type %s", flag->_name, flag->_type);
 545   }
 546 C2V_END
 547 
 548 #undef BOXED_LONG
 549 #undef BOXED_DOUBLE
 550 #undef CSTRING_TO_JSTRING
 551 
 552 C2V_VMENTRY(jbyteArray, getBytecode, (JNIEnv *, jobject, jobject jvmci_method))
 553   methodHandle method = CompilerToVM::asMethod(jvmci_method);
 554   ResourceMark rm;
 555 
 556   int code_size = method->code_size();
 557   typeArrayOop reconstituted_code = oopFactory::new_byteArray(code_size, CHECK_NULL);
 558 
 559   guarantee(method->method_holder()->is_rewritten(), "Method's holder should be rewritten");
 560   // iterate over all bytecodes and replace non-Java bytecodes
 561 
 562   for (BytecodeStream s(method); s.next() != Bytecodes::_illegal; ) {
 563     Bytecodes::Code code = s.code();
 564     Bytecodes::Code raw_code = s.raw_code();
 565     int bci = s.bci();
 566     int len = s.instruction_size();
 567 
 568     // Restore original byte code.
 569     reconstituted_code->byte_at_put(bci, (jbyte) (s.is_wide()? Bytecodes::_wide : code));
 570     if (len > 1) {
 571       memcpy(reconstituted_code->byte_at_addr(bci + 1), s.bcp()+1, len-1);
 572     }
 573 
 574     if (len > 1) {
 575       // Restore the big-endian constant pool indexes.
 576       // Cf. Rewriter::scan_method
 577       switch (code) {
 578         case Bytecodes::_getstatic:
 579         case Bytecodes::_putstatic:
 580         case Bytecodes::_getfield:
 581         case Bytecodes::_putfield:
 582         case Bytecodes::_invokevirtual:
 583         case Bytecodes::_invokespecial:
 584         case Bytecodes::_invokestatic:
 585         case Bytecodes::_invokeinterface:
 586         case Bytecodes::_invokehandle: {
 587           int cp_index = Bytes::get_native_u2((address) reconstituted_code->byte_at_addr(bci + 1));
 588           Bytes::put_Java_u2((address) reconstituted_code->byte_at_addr(bci + 1), (u2) cp_index);
 589           break;
 590         }
 591 
 592         case Bytecodes::_invokedynamic: {
 593           int cp_index = Bytes::get_native_u4((address) reconstituted_code->byte_at_addr(bci + 1));
 594           Bytes::put_Java_u4((address) reconstituted_code->byte_at_addr(bci + 1), (u4) cp_index);
 595           break;
 596         }
 597 
 598         default:
 599           break;
 600       }
 601 
 602       // Not all ldc byte code are rewritten.
 603       switch (raw_code) {
 604         case Bytecodes::_fast_aldc: {
 605           int cpc_index = reconstituted_code->byte_at(bci + 1) & 0xff;
 606           int cp_index = method->constants()->object_to_cp_index(cpc_index);
 607           assert(cp_index < method->constants()->length(), "sanity check");
 608           reconstituted_code->byte_at_put(bci + 1, (jbyte) cp_index);
 609           break;
 610         }
 611 
 612         case Bytecodes::_fast_aldc_w: {
 613           int cpc_index = Bytes::get_native_u2((address) reconstituted_code->byte_at_addr(bci + 1));
 614           int cp_index = method->constants()->object_to_cp_index(cpc_index);
 615           assert(cp_index < method->constants()->length(), "sanity check");
 616           Bytes::put_Java_u2((address) reconstituted_code->byte_at_addr(bci + 1), (u2) cp_index);
 617           break;
 618         }
 619 
 620         default:
 621           break;
 622       }
 623     }
 624   }
 625 
 626   return (jbyteArray) JNIHandles::make_local(THREAD, reconstituted_code);
 627 C2V_END
 628 
 629 C2V_VMENTRY(jint, getExceptionTableLength, (JNIEnv *, jobject, jobject jvmci_method))
 630   ResourceMark rm;
 631   methodHandle method = CompilerToVM::asMethod(jvmci_method);
 632   return method->exception_table_length();
 633 C2V_END
 634 
 635 C2V_VMENTRY(jlong, getExceptionTableStart, (JNIEnv *, jobject, jobject jvmci_method))
 636   ResourceMark rm;
 637   methodHandle method = CompilerToVM::asMethod(jvmci_method);
 638   if (method->exception_table_length() == 0) {
 639     return 0L;
 640   }
 641   return (jlong) (address) method->exception_table_start();
 642 C2V_END
 643 
 644 C2V_VMENTRY(jobject, asResolvedJavaMethod, (JNIEnv *, jobject, jobject executable_handle))
 645   oop executable = JNIHandles::resolve(executable_handle);
 646   oop mirror = NULL;
 647   int slot = 0;
 648 
 649   if (executable->klass() == SystemDictionary::reflect_Constructor_klass()) {
 650     mirror = java_lang_reflect_Constructor::clazz(executable);
 651     slot = java_lang_reflect_Constructor::slot(executable);
 652   } else {
 653     assert(executable->klass() == SystemDictionary::reflect_Method_klass(), "wrong type");
 654     mirror = java_lang_reflect_Method::clazz(executable);
 655     slot = java_lang_reflect_Method::slot(executable);
 656   }
 657   Klass* holder = java_lang_Class::as_Klass(mirror);
 658   methodHandle method = InstanceKlass::cast(holder)->method_with_idnum(slot);
 659   oop result = CompilerToVM::get_jvmci_method(method, CHECK_NULL);
 660   return JNIHandles::make_local(THREAD, result);
 661 }
 662 
 663 C2V_VMENTRY(jobject, getResolvedJavaMethod, (JNIEnv *, jobject, jobject base, jlong offset))
 664   methodHandle method;
 665   oop base_object = JNIHandles::resolve(base);
 666   if (base_object == NULL) {
 667     method = *((Method**)(offset));
 668   } else if (base_object->is_a(SystemDictionary::ResolvedMethodName_klass())) {
 669     method = (Method*) (intptr_t) base_object->long_field(offset);
 670   } else if (base_object->is_a(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass())) {
 671     method = *((Method**)(HotSpotResolvedJavaMethodImpl::metaspaceMethod(base_object) + offset));
 672   } else {
 673     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
 674                 err_msg("Unexpected type: %s", base_object->klass()->external_name()));
 675   }
 676   assert (method.is_null() || method->is_method(), "invalid read");
 677   oop result = CompilerToVM::get_jvmci_method(method, CHECK_NULL);
 678   return JNIHandles::make_local(THREAD, result);
 679 }
 680 
 681 C2V_VMENTRY(jobject, getConstantPool, (JNIEnv *, jobject, jobject object_handle))
 682   constantPoolHandle cp;
 683   oop object = JNIHandles::resolve(object_handle);
 684   if (object == NULL) {
 685     THROW_0(vmSymbols::java_lang_NullPointerException());
 686   }
 687   if (object->is_a(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass())) {
 688     cp = CompilerToVM::asMethod(object)->constMethod()->constants();
 689   } else if (object->is_a(SystemDictionary::HotSpotResolvedObjectTypeImpl_klass())) {
 690     cp = InstanceKlass::cast(CompilerToVM::asKlass(object))->constants();
 691   } else {
 692     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
 693                 err_msg("Unexpected type: %s", object->klass()->external_name()));
 694   }
 695   assert(!cp.is_null(), "npe");
 696   JavaValue method_result(T_OBJECT);
 697   JavaCallArguments args;
 698   args.push_long((jlong) (address) cp());
 699   JavaCalls::call_static(&method_result, SystemDictionary::HotSpotConstantPool_klass(), vmSymbols::fromMetaspace_name(), vmSymbols::constantPool_fromMetaspace_signature(), &args, CHECK_NULL);
 700   return JNIHandles::make_local(THREAD, (oop)method_result.get_jobject());
 701 }
 702 
 703 C2V_VMENTRY(jobject, getResolvedJavaType, (JNIEnv *, jobject, jobject base, jlong offset, jboolean compressed))
 704   Klass* klass = NULL;
 705   oop base_object = JNIHandles::resolve(base);
 706   jlong base_address = 0;
 707   if (base_object != NULL && offset == oopDesc::klass_offset_in_bytes()) {
 708     klass = base_object->klass();
 709   } else if (!compressed) {
 710     if (base_object != NULL) {
 711       if (base_object->is_a(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass())) {
 712         base_address = HotSpotResolvedJavaMethodImpl::metaspaceMethod(base_object);
 713       } else if (base_object->is_a(SystemDictionary::HotSpotConstantPool_klass())) {
 714         base_address = HotSpotConstantPool::metaspaceConstantPool(base_object);
 715       } else if (base_object->is_a(SystemDictionary::HotSpotResolvedObjectTypeImpl_klass())) {
 716         base_address = (jlong) CompilerToVM::asKlass(base_object);
 717       } else if (base_object->is_a(SystemDictionary::Class_klass())) {
 718         base_address = (jlong) (address) base_object;
 719       } else {
 720         THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
 721                     err_msg("Unexpected arguments: %s " JLONG_FORMAT " %s", base_object->klass()->external_name(), offset, compressed ? "true" : "false"));
 722       }
 723     }
 724     klass = *((Klass**) (intptr_t) (base_address + offset));
 725   } else {
 726     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
 727                 err_msg("Unexpected arguments: %s " JLONG_FORMAT " %s", base_object->klass()->external_name(), offset, compressed ? "true" : "false"));
 728   }
 729   assert (klass == NULL || klass->is_klass(), "invalid read");
 730   oop result = CompilerToVM::get_jvmci_type(klass, CHECK_NULL);
 731   return JNIHandles::make_local(THREAD, result);
 732 }
 733 
 734 C2V_VMENTRY(jobject, findUniqueConcreteMethod, (JNIEnv *, jobject, jobject jvmci_type, jobject jvmci_method))
 735   ResourceMark rm;
 736   methodHandle method = CompilerToVM::asMethod(jvmci_method);
 737   Klass* holder = CompilerToVM::asKlass(jvmci_type);
 738   if (holder->is_interface()) {
 739     THROW_MSG_0(vmSymbols::java_lang_InternalError(), err_msg("Interface %s should be handled in Java code", holder->external_name()));
 740   }
 741 
 742   methodHandle ucm;
 743   {
 744     MutexLocker locker(Compile_lock);
 745     ucm = Dependencies::find_unique_concrete_method(holder, method());
 746   }
 747   oop result = CompilerToVM::get_jvmci_method(ucm, CHECK_NULL);
 748   return JNIHandles::make_local(THREAD, result);
 749 C2V_END
 750 
 751 C2V_VMENTRY(jobject, getImplementor, (JNIEnv *, jobject, jobject jvmci_type))
 752   InstanceKlass* klass = (InstanceKlass*) CompilerToVM::asKlass(jvmci_type);
 753   oop implementor = CompilerToVM::get_jvmci_type(klass->implementor(), CHECK_NULL);
 754   return JNIHandles::make_local(THREAD, implementor);
 755 C2V_END
 756 
 757 C2V_VMENTRY(jboolean, methodIsIgnoredBySecurityStackWalk,(JNIEnv *, jobject, jobject jvmci_method))
 758   methodHandle method = CompilerToVM::asMethod(jvmci_method);
 759   return method->is_ignored_by_security_stack_walk();
 760 C2V_END
 761 
 762 C2V_VMENTRY(jboolean, isCompilable,(JNIEnv *, jobject, jobject jvmci_method))
 763   methodHandle method = CompilerToVM::asMethod(jvmci_method);
 764   return !method->is_not_compilable(CompLevel_full_optimization);
 765 C2V_END
 766 
 767 C2V_VMENTRY(jboolean, hasNeverInlineDirective,(JNIEnv *, jobject, jobject jvmci_method))
 768   methodHandle method = CompilerToVM::asMethod(jvmci_method);
 769   return !Inline || CompilerOracle::should_not_inline(method) || method->dont_inline();
 770 C2V_END
 771 
 772 C2V_VMENTRY(jboolean, shouldInlineMethod,(JNIEnv *, jobject, jobject jvmci_method))
 773   methodHandle method = CompilerToVM::asMethod(jvmci_method);
 774   return CompilerOracle::should_inline(method) || method->force_inline();
 775 C2V_END
 776 
 777 C2V_VMENTRY(jobject, lookupType, (JNIEnv*, jobject, jstring jname, jclass accessing_class, jboolean resolve))
 778   ResourceMark rm;
 779   Handle name(THREAD, JNIHandles::resolve(jname));
 780   Symbol* class_name = java_lang_String::as_symbol(name(), CHECK_0);
 781   if (java_lang_String::length(name()) <= 1) {
 782     THROW_MSG_0(vmSymbols::java_lang_InternalError(), err_msg("Primitive type %s should be handled in Java code", class_name->as_C_string()));
 783   }
 784 
 785   Klass* resolved_klass = NULL;
 786   if (JNIHandles::resolve(accessing_class) == NULL) {
 787     THROW_0(vmSymbols::java_lang_NullPointerException());
 788   }
 789   Klass* accessing_klass = java_lang_Class::as_Klass(JNIHandles::resolve(accessing_class));
 790   Handle class_loader(THREAD, accessing_klass->class_loader());
 791   Handle protection_domain(THREAD, accessing_klass->protection_domain());
 792 
 793   if (resolve) {
 794     resolved_klass = SystemDictionary::resolve_or_null(class_name, class_loader, protection_domain, CHECK_0);
 795   } else {
 796     if (class_name->byte_at(0) == 'L' &&
 797       class_name->byte_at(class_name->utf8_length()-1) == ';') {
 798       // This is a name from a signature.  Strip off the trimmings.
 799       // Call recursive to keep scope of strippedsym.
 800       TempNewSymbol strippedsym = SymbolTable::new_symbol(class_name->as_utf8()+1,
 801                                                           class_name->utf8_length()-2,
 802                                                           CHECK_0);
 803       resolved_klass = SystemDictionary::find(strippedsym, class_loader, protection_domain, CHECK_0);
 804     } else if (FieldType::is_array(class_name)) {
 805       FieldArrayInfo fd;
 806       // dimension and object_key in FieldArrayInfo are assigned as a side-effect
 807       // of this call
 808       BasicType t = FieldType::get_array_info(class_name, fd, CHECK_0);
 809       if (t == T_OBJECT) {
 810         TempNewSymbol strippedsym = SymbolTable::new_symbol(class_name->as_utf8()+1+fd.dimension(),
 811                                                             class_name->utf8_length()-2-fd.dimension(),
 812                                                             CHECK_0);
 813         // naked oop "k" is OK here -- we assign back into it
 814         resolved_klass = SystemDictionary::find(strippedsym,
 815                                                              class_loader,
 816                                                              protection_domain,
 817                                                              CHECK_0);
 818         if (resolved_klass != NULL) {
 819           resolved_klass = resolved_klass->array_klass(fd.dimension(), CHECK_0);
 820         }
 821       } else {
 822         resolved_klass = Universe::typeArrayKlassObj(t);
 823         resolved_klass = TypeArrayKlass::cast(resolved_klass)->array_klass(fd.dimension(), CHECK_0);
 824       }
 825     }
 826   }
 827   oop result = CompilerToVM::get_jvmci_type(resolved_klass, CHECK_NULL);
 828   return JNIHandles::make_local(THREAD, result);
 829 C2V_END
 830 
 831 C2V_VMENTRY(jobject, resolveConstantInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index))
 832   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 833   oop result = cp->resolve_constant_at(index, CHECK_NULL);
 834   return JNIHandles::make_local(THREAD, result);
 835 C2V_END
 836 
 837 C2V_VMENTRY(jobject, resolvePossiblyCachedConstantInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index))
 838   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 839   oop result = cp->resolve_possibly_cached_constant_at(index, CHECK_NULL);
 840   return JNIHandles::make_local(THREAD, result);
 841 C2V_END
 842 
 843 C2V_VMENTRY(jint, lookupNameAndTypeRefIndexInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index))
 844   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 845   return cp->name_and_type_ref_index_at(index);
 846 C2V_END
 847 
 848 C2V_VMENTRY(jobject, lookupNameInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint which))
 849   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 850   Handle sym = java_lang_String::create_from_symbol(cp->name_ref_at(which), CHECK_NULL);
 851   return JNIHandles::make_local(THREAD, sym());
 852 C2V_END
 853 
 854 C2V_VMENTRY(jobject, lookupSignatureInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint which))
 855   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 856   Handle sym = java_lang_String::create_from_symbol(cp->signature_ref_at(which), CHECK_NULL);
 857   return JNIHandles::make_local(THREAD, sym());
 858 C2V_END
 859 
 860 C2V_VMENTRY(jint, lookupKlassRefIndexInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index))
 861   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 862   return cp->klass_ref_index_at(index);
 863 C2V_END
 864 
 865 C2V_VMENTRY(jobject, resolveTypeInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index))
 866   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 867   Klass* resolved_klass = cp->klass_at(index, CHECK_NULL);
 868   oop klass = CompilerToVM::get_jvmci_type(resolved_klass, CHECK_NULL);
 869   return JNIHandles::make_local(THREAD, klass);
 870 C2V_END
 871 
 872 C2V_VMENTRY(jobject, lookupKlassInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index, jbyte opcode))
 873   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 874   Klass* loading_klass = cp->pool_holder();
 875   bool is_accessible = false;
 876   Klass* klass = JVMCIEnv::get_klass_by_index(cp, index, is_accessible, loading_klass);
 877   Symbol* symbol = NULL;
 878   if (klass == NULL) {
 879     symbol = cp->klass_name_at(index);
 880   }
 881   oop result_oop;
 882   if (klass != NULL) {
 883     result_oop = CompilerToVM::get_jvmci_type(klass, CHECK_NULL);
 884   } else {
 885     Handle result = java_lang_String::create_from_symbol(symbol, CHECK_NULL);
 886     result_oop = result();
 887   }
 888   return JNIHandles::make_local(THREAD, result_oop);
 889 C2V_END
 890 
 891 C2V_VMENTRY(jobject, lookupAppendixInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index))
 892   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 893   oop appendix_oop = ConstantPool::appendix_at_if_loaded(cp, index);
 894   return JNIHandles::make_local(THREAD, appendix_oop);
 895 C2V_END
 896 
 897 C2V_VMENTRY(jobject, lookupMethodInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index, jbyte opcode))
 898   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 899   InstanceKlass* pool_holder = cp->pool_holder();
 900   Bytecodes::Code bc = (Bytecodes::Code) (((int) opcode) & 0xFF);
 901   methodHandle method = JVMCIEnv::get_method_by_index(cp, index, bc, pool_holder);
 902   oop result = CompilerToVM::get_jvmci_method(method, CHECK_NULL);
 903   return JNIHandles::make_local(THREAD, result);
 904 C2V_END
 905 
 906 C2V_VMENTRY(jint, constantPoolRemapInstructionOperandFromCache, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index))
 907   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 908   return cp->remap_instruction_operand_from_cache(index);
 909 C2V_END
 910 
 911 C2V_VMENTRY(jobject, resolveFieldInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index, jobject jvmci_method, jbyte opcode, jintArray info_handle))
 912   ResourceMark rm;
 913   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
 914   Bytecodes::Code code = (Bytecodes::Code)(((int) opcode) & 0xFF);
 915   fieldDescriptor fd;
 916   LinkInfo link_info(cp, index, (jvmci_method != NULL) ? CompilerToVM::asMethod(jvmci_method) : NULL, CHECK_0);
 917   LinkResolver::resolve_field(fd, link_info, Bytecodes::java_code(code), false, CHECK_0);
 918   typeArrayOop info = (typeArrayOop) JNIHandles::resolve(info_handle);
 919   if (info == NULL || info->length() != 3) {
 920     JVMCI_ERROR_NULL("info must not be null and have a length of 3");
 921   }
 922   info->int_at_put(0, fd.access_flags().as_int());
 923   info->int_at_put(1, fd.offset());
 924   info->int_at_put(2, fd.index());
 925   oop field_holder = CompilerToVM::get_jvmci_type(fd.field_holder(), CHECK_NULL);
 926   return JNIHandles::make_local(THREAD, field_holder);
 927 C2V_END
 928 
 929 C2V_VMENTRY(jint, getVtableIndexForInterfaceMethod, (JNIEnv *, jobject, jobject jvmci_type, jobject jvmci_method))
 930   ResourceMark rm;
 931   Klass* klass = CompilerToVM::asKlass(jvmci_type);
 932   Method* method = CompilerToVM::asMethod(jvmci_method);
 933   if (klass->is_interface()) {
 934     THROW_MSG_0(vmSymbols::java_lang_InternalError(), err_msg("Interface %s should be handled in Java code", klass->external_name()));
 935   }
 936   if (!method->method_holder()->is_interface()) {
 937     THROW_MSG_0(vmSymbols::java_lang_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()));
 938   }
 939   if (!InstanceKlass::cast(klass)->is_linked()) {
 940     THROW_MSG_0(vmSymbols::java_lang_InternalError(), err_msg("Class %s must be linked", klass->external_name()));
 941   }
 942   return LinkResolver::vtable_index_of_interface_method(klass, method);
 943 C2V_END
 944 
 945 C2V_VMENTRY(jobject, resolveMethod, (JNIEnv *, jobject, jobject receiver_jvmci_type, jobject jvmci_method, jobject caller_jvmci_type))
 946   Klass* recv_klass = CompilerToVM::asKlass(receiver_jvmci_type);
 947   Klass* caller_klass = CompilerToVM::asKlass(caller_jvmci_type);
 948   methodHandle method = CompilerToVM::asMethod(jvmci_method);
 949 
 950   Klass* resolved     = method->method_holder();
 951   Symbol* h_name      = method->name();
 952   Symbol* h_signature = method->signature();
 953 
 954   if (MethodHandles::is_signature_polymorphic_method(method())) {
 955       // Signature polymorphic methods are already resolved, JVMCI just returns NULL in this case.
 956       return NULL;
 957   }
 958 
 959   LinkInfo link_info(resolved, h_name, h_signature, caller_klass);
 960   methodHandle m;
 961   // Only do exact lookup if receiver klass has been linked.  Otherwise,
 962   // the vtable has not been setup, and the LinkResolver will fail.
 963   if (recv_klass->is_array_klass() ||
 964       (InstanceKlass::cast(recv_klass)->is_linked() && !recv_klass->is_interface())) {
 965     if (resolved->is_interface()) {
 966       m = LinkResolver::resolve_interface_call_or_null(recv_klass, link_info);
 967     } else {
 968       m = LinkResolver::resolve_virtual_call_or_null(recv_klass, link_info);
 969     }
 970   }
 971 
 972   if (m.is_null()) {
 973     // Return NULL if there was a problem with lookup (uninitialized class, etc.)
 974     return NULL;
 975   }
 976 
 977   oop result = CompilerToVM::get_jvmci_method(m, CHECK_NULL);
 978   return JNIHandles::make_local(THREAD, result);
 979 C2V_END
 980 
 981 C2V_VMENTRY(jboolean, hasFinalizableSubclass,(JNIEnv *, jobject, jobject jvmci_type))
 982   Klass* klass = CompilerToVM::asKlass(jvmci_type);
 983   assert(klass != NULL, "method must not be called for primitive types");
 984   return Dependencies::find_finalizable_subclass(klass) != NULL;
 985 C2V_END
 986 
 987 C2V_VMENTRY(jobject, getClassInitializer, (JNIEnv *, jobject, jobject jvmci_type))
 988   InstanceKlass* klass = (InstanceKlass*) CompilerToVM::asKlass(jvmci_type);
 989   oop result = CompilerToVM::get_jvmci_method(klass->class_initializer(), CHECK_NULL);
 990   return JNIHandles::make_local(THREAD, result);
 991 C2V_END
 992 
 993 C2V_VMENTRY(jlong, getMaxCallTargetOffset, (JNIEnv*, jobject, jlong addr))
 994   address target_addr = (address) addr;
 995   if (target_addr != 0x0) {
 996     int64_t off_low = (int64_t)target_addr - ((int64_t)CodeCache::low_bound() + sizeof(int));
 997     int64_t off_high = (int64_t)target_addr - ((int64_t)CodeCache::high_bound() + sizeof(int));
 998     return MAX2(ABS(off_low), ABS(off_high));
 999   }
1000   return -1;
1001 C2V_END
1002 
1003 C2V_VMENTRY(void, setNotInlineableOrCompileable,(JNIEnv *, jobject,  jobject jvmci_method))
1004   methodHandle method = CompilerToVM::asMethod(jvmci_method);
1005   method->set_not_c1_compilable();
1006   method->set_not_c2_compilable();
1007   method->set_dont_inline(true);
1008 C2V_END
1009 
1010 C2V_VMENTRY(jint, installCode, (JNIEnv *jniEnv, jobject, jobject target, jobject compiled_code, jobject installed_code, jobject speculation_log))
1011   ResourceMark rm;
1012   HandleMark hm;
1013   JNIHandleMark jni_hm;
1014 
1015   Handle target_handle(THREAD, JNIHandles::resolve(target));
1016   Handle compiled_code_handle(THREAD, JNIHandles::resolve(compiled_code));
1017   CodeBlob* cb = NULL;
1018   Handle installed_code_handle(THREAD, JNIHandles::resolve(installed_code));
1019   Handle speculation_log_handle(THREAD, JNIHandles::resolve(speculation_log));
1020 
1021   JVMCICompiler* compiler = JVMCICompiler::instance(true, CHECK_JNI_ERR);
1022 
1023   TraceTime install_time("installCode", JVMCICompiler::codeInstallTimer());
1024   bool is_immutable_PIC = HotSpotCompiledCode::isImmutablePIC(compiled_code_handle) > 0;
1025   CodeInstaller installer(is_immutable_PIC);
1026   JVMCIEnv::CodeInstallResult result = installer.install(compiler, target_handle, compiled_code_handle, cb, installed_code_handle, speculation_log_handle, CHECK_0);
1027 
1028   if (PrintCodeCacheOnCompilation) {
1029     stringStream s;
1030     // Dump code cache  into a buffer before locking the tty,
1031     {
1032       MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1033       CodeCache::print_summary(&s, false);
1034     }
1035     ttyLocker ttyl;
1036     tty->print_raw_cr(s.as_string());
1037   }
1038 
1039   if (result != JVMCIEnv::ok) {
1040     assert(cb == NULL, "should be");
1041   } else {
1042     if (!installed_code_handle.is_null()) {
1043       assert(installed_code_handle->is_a(InstalledCode::klass()), "wrong type");
1044       nmethod::invalidate_installed_code(installed_code_handle, CHECK_0);
1045       {
1046         // Ensure that all updates to the InstalledCode fields are consistent.
1047         MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
1048         InstalledCode::set_address(installed_code_handle, (jlong) cb);
1049         InstalledCode::set_version(installed_code_handle, InstalledCode::version(installed_code_handle) + 1);
1050         if (cb->is_nmethod()) {
1051           InstalledCode::set_entryPoint(installed_code_handle, (jlong) cb->as_nmethod_or_null()->verified_entry_point());
1052         } else {
1053           InstalledCode::set_entryPoint(installed_code_handle, (jlong) cb->code_begin());
1054         }
1055         if (installed_code_handle->is_a(HotSpotInstalledCode::klass())) {
1056           HotSpotInstalledCode::set_size(installed_code_handle, cb->size());
1057           HotSpotInstalledCode::set_codeStart(installed_code_handle, (jlong) cb->code_begin());
1058           HotSpotInstalledCode::set_codeSize(installed_code_handle, cb->code_size());
1059         }
1060       }
1061       nmethod* nm = cb->as_nmethod_or_null();
1062       if (nm != NULL && installed_code_handle->is_scavengable()) {
1063         assert(nm->detect_scavenge_root_oops(), "nm should be scavengable if installed_code is scavengable");
1064         if (!UseG1GC) {
1065           assert(nm->on_scavenge_root_list(), "nm should be on scavengable list");
1066         }
1067       }
1068     }
1069   }
1070   return result;
1071 C2V_END
1072 
1073 C2V_VMENTRY(jint, getMetadata, (JNIEnv *jniEnv, jobject, jobject target, jobject compiled_code, jobject metadata))
1074   ResourceMark rm;
1075   HandleMark hm;
1076 
1077   Handle target_handle(THREAD, JNIHandles::resolve(target));
1078   Handle compiled_code_handle(THREAD, JNIHandles::resolve(compiled_code));
1079   Handle metadata_handle(THREAD, JNIHandles::resolve(metadata));
1080 
1081   CodeMetadata code_metadata;
1082   CodeBlob *cb = NULL;
1083   CodeInstaller installer(true /* immutable PIC compilation */);
1084 
1085   JVMCIEnv::CodeInstallResult result = installer.gather_metadata(target_handle, compiled_code_handle, code_metadata, CHECK_0);
1086   if (result != JVMCIEnv::ok) {
1087     return result;
1088   }
1089 
1090   if (code_metadata.get_nr_pc_desc() > 0) {
1091     typeArrayHandle pcArrayOop = oopFactory::new_byteArray_handle(sizeof(PcDesc) * code_metadata.get_nr_pc_desc(), CHECK_(JVMCIEnv::cache_full));
1092     memcpy(pcArrayOop->byte_at_addr(0), code_metadata.get_pc_desc(), sizeof(PcDesc) * code_metadata.get_nr_pc_desc());
1093     HotSpotMetaData::set_pcDescBytes(metadata_handle, pcArrayOop());
1094   }
1095 
1096   if (code_metadata.get_scopes_size() > 0) {
1097     typeArrayHandle scopesArrayOop = oopFactory::new_byteArray_handle(code_metadata.get_scopes_size(), CHECK_(JVMCIEnv::cache_full));
1098     memcpy(scopesArrayOop->byte_at_addr(0), code_metadata.get_scopes_desc(), code_metadata.get_scopes_size());
1099     HotSpotMetaData::set_scopesDescBytes(metadata_handle, scopesArrayOop());
1100   }
1101 
1102   RelocBuffer* reloc_buffer = code_metadata.get_reloc_buffer();
1103   typeArrayHandle relocArrayOop = oopFactory::new_byteArray_handle((int) reloc_buffer->size(), CHECK_(JVMCIEnv::cache_full));
1104   if (reloc_buffer->size() > 0) {
1105     memcpy(relocArrayOop->byte_at_addr(0), reloc_buffer->begin(), reloc_buffer->size());
1106   }
1107   HotSpotMetaData::set_relocBytes(metadata_handle, relocArrayOop());
1108 
1109   const OopMapSet* oopMapSet = installer.oopMapSet();
1110   {
1111     ResourceMark mark;
1112     ImmutableOopMapBuilder builder(oopMapSet);
1113     int oopmap_size = builder.heap_size();
1114     typeArrayHandle oopMapArrayHandle = oopFactory::new_byteArray_handle(oopmap_size, CHECK_(JVMCIEnv::cache_full));
1115     builder.generate_into((address) oopMapArrayHandle->byte_at_addr(0));
1116     HotSpotMetaData::set_oopMaps(metadata_handle, oopMapArrayHandle());
1117   }
1118 
1119   AOTOopRecorder* recorder = code_metadata.get_oop_recorder();
1120 
1121   int nr_meta_refs = recorder->nr_meta_refs();
1122   objArrayOop metadataArray = oopFactory::new_objectArray(nr_meta_refs, CHECK_(JVMCIEnv::cache_full));
1123   objArrayHandle metadataArrayHandle(THREAD, metadataArray);
1124   for (int i = 0; i < nr_meta_refs; ++i) {
1125     jobject element = recorder->meta_element(i);
1126     if (element == NULL) {
1127       return JVMCIEnv::cache_full;
1128     }
1129     metadataArrayHandle->obj_at_put(i, JNIHandles::resolve(element));
1130   }
1131   HotSpotMetaData::set_metadata(metadata_handle, metadataArrayHandle());
1132 
1133   ExceptionHandlerTable* handler = code_metadata.get_exception_table();
1134   int table_size = handler->size_in_bytes();
1135   typeArrayHandle exceptionArrayOop = oopFactory::new_byteArray_handle(table_size, CHECK_(JVMCIEnv::cache_full));
1136 
1137   if (table_size > 0) {
1138     handler->copy_bytes_to((address) exceptionArrayOop->byte_at_addr(0));
1139   }
1140   HotSpotMetaData::set_exceptionBytes(metadata_handle, exceptionArrayOop());
1141 
1142   return result;
1143 C2V_END
1144 
1145 C2V_VMENTRY(void, resetCompilationStatistics, (JNIEnv *jniEnv, jobject))
1146   JVMCICompiler* compiler = JVMCICompiler::instance(true, CHECK);
1147   CompilerStatistics* stats = compiler->stats();
1148   stats->_standard.reset();
1149   stats->_osr.reset();
1150 C2V_END
1151 
1152 C2V_VMENTRY(jobject, disassembleCodeBlob, (JNIEnv *jniEnv, jobject, jobject installedCode))
1153   ResourceMark rm;
1154   HandleMark hm;
1155 
1156   if (installedCode == NULL) {
1157     THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(), "installedCode is null");
1158   }
1159 
1160   jlong codeBlob = InstalledCode::address(installedCode);
1161   if (codeBlob == 0L) {
1162     return NULL;
1163   }
1164 
1165   CodeBlob* cb = (CodeBlob*) (address) codeBlob;
1166   if (cb == NULL) {
1167     return NULL;
1168   }
1169 
1170   // We don't want the stringStream buffer to resize during disassembly as it
1171   // uses scoped resource memory. If a nested function called during disassembly uses
1172   // a ResourceMark and the buffer expands within the scope of the mark,
1173   // the buffer becomes garbage when that scope is exited. Experience shows that
1174   // the disassembled code is typically about 10x the code size so a fixed buffer
1175   // sized to 20x code size plus a fixed amount for header info should be sufficient.
1176   int bufferSize = cb->code_size() * 20 + 1024;
1177   char* buffer = NEW_RESOURCE_ARRAY(char, bufferSize);
1178   stringStream st(buffer, bufferSize);
1179   if (cb->is_nmethod()) {
1180     nmethod* nm = (nmethod*) cb;
1181     if (!nm->is_alive()) {
1182       return NULL;
1183     }
1184   }
1185   Disassembler::decode(cb, &st);
1186   if (st.size() <= 0) {
1187     return NULL;
1188   }
1189 
1190   Handle result = java_lang_String::create_from_platform_dependent_str(st.as_string(), CHECK_NULL);
1191   return JNIHandles::make_local(THREAD, result());
1192 C2V_END
1193 
1194 C2V_VMENTRY(jobject, getStackTraceElement, (JNIEnv*, jobject, jobject jvmci_method, int bci))
1195   ResourceMark rm;
1196   HandleMark hm;
1197 
1198   methodHandle method = CompilerToVM::asMethod(jvmci_method);
1199   oop element = java_lang_StackTraceElement::create(method, bci, CHECK_NULL);
1200   return JNIHandles::make_local(THREAD, element);
1201 C2V_END
1202 
1203 C2V_VMENTRY(jobject, executeInstalledCode, (JNIEnv*, jobject, jobject args, jobject hotspotInstalledCode))
1204   ResourceMark rm;
1205   HandleMark hm;
1206 
1207   jlong nmethodValue = InstalledCode::address(hotspotInstalledCode);
1208   if (nmethodValue == 0L) {
1209     THROW_NULL(vmSymbols::jdk_vm_ci_code_InvalidInstalledCodeException());
1210   }
1211   nmethod* nm = (nmethod*) (address) nmethodValue;
1212   methodHandle mh = nm->method();
1213   Symbol* signature = mh->signature();
1214   JavaCallArguments jca(mh->size_of_parameters());
1215 
1216   JavaArgumentUnboxer jap(signature, &jca, (arrayOop) JNIHandles::resolve(args), mh->is_static());
1217   JavaValue result(jap.get_ret_type());
1218   jca.set_alternative_target(nm);
1219   JavaCalls::call(&result, mh, &jca, CHECK_NULL);
1220 
1221   if (jap.get_ret_type() == T_VOID) {
1222     return NULL;
1223   } else if (jap.get_ret_type() == T_OBJECT || jap.get_ret_type() == T_ARRAY) {
1224     return JNIHandles::make_local(THREAD, (oop) result.get_jobject());
1225   } else {
1226     jvalue *value = (jvalue *) result.get_value_addr();
1227     // Narrow the value down if required (Important on big endian machines)
1228     switch (jap.get_ret_type()) {
1229       case T_BOOLEAN:
1230        value->z = (jboolean) value->i;
1231        break;
1232       case T_BYTE:
1233        value->b = (jbyte) value->i;
1234        break;
1235       case T_CHAR:
1236        value->c = (jchar) value->i;
1237        break;
1238       case T_SHORT:
1239        value->s = (jshort) value->i;
1240        break;
1241       default:
1242         break;
1243     }
1244     oop o = java_lang_boxing_object::create(jap.get_ret_type(), value, CHECK_NULL);
1245     return JNIHandles::make_local(THREAD, o);
1246   }
1247 C2V_END
1248 
1249 C2V_VMENTRY(jlongArray, getLineNumberTable, (JNIEnv *, jobject, jobject jvmci_method))
1250   Method* method = CompilerToVM::asMethod(jvmci_method);
1251   if (!method->has_linenumber_table()) {
1252     return NULL;
1253   }
1254   u2 num_entries = 0;
1255   CompressedLineNumberReadStream streamForSize(method->compressed_linenumber_table());
1256   while (streamForSize.read_pair()) {
1257     num_entries++;
1258   }
1259 
1260   CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
1261   typeArrayOop result = oopFactory::new_longArray(2 * num_entries, CHECK_NULL);
1262 
1263   int i = 0;
1264   jlong value;
1265   while (stream.read_pair()) {
1266     value = ((long) stream.bci());
1267     result->long_at_put(i, value);
1268     value = ((long) stream.line());
1269     result->long_at_put(i + 1, value);
1270     i += 2;
1271   }
1272 
1273   return (jlongArray) JNIHandles::make_local(THREAD, result);
1274 C2V_END
1275 
1276 C2V_VMENTRY(jlong, getLocalVariableTableStart, (JNIEnv *, jobject, jobject jvmci_method))
1277   ResourceMark rm;
1278   Method* method = CompilerToVM::asMethod(jvmci_method);
1279   if (!method->has_localvariable_table()) {
1280     return 0;
1281   }
1282   return (jlong) (address) method->localvariable_table_start();
1283 C2V_END
1284 
1285 C2V_VMENTRY(jint, getLocalVariableTableLength, (JNIEnv *, jobject, jobject jvmci_method))
1286   ResourceMark rm;
1287   Method* method = CompilerToVM::asMethod(jvmci_method);
1288   return method->localvariable_table_length();
1289 C2V_END
1290 
1291 C2V_VMENTRY(void, reprofile, (JNIEnv*, jobject, jobject jvmci_method))
1292   Method* method = CompilerToVM::asMethod(jvmci_method);
1293   MethodCounters* mcs = method->method_counters();
1294   if (mcs != NULL) {
1295     mcs->clear_counters();
1296   }
1297   NOT_PRODUCT(method->set_compiled_invocation_count(0));
1298 
1299   CompiledMethod* code = method->code();
1300   if (code != NULL) {
1301     code->make_not_entrant();
1302   }
1303 
1304   MethodData* method_data = method->method_data();
1305   if (method_data == NULL) {
1306     ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
1307     method_data = MethodData::allocate(loader_data, method, CHECK);
1308     method->set_method_data(method_data);
1309   } else {
1310     method_data->initialize();
1311   }
1312 C2V_END
1313 
1314 
1315 C2V_VMENTRY(void, invalidateInstalledCode, (JNIEnv*, jobject, jobject installed_code))
1316   Handle installed_code_handle(THREAD, JNIHandles::resolve(installed_code));
1317   nmethod::invalidate_installed_code(installed_code_handle, CHECK);
1318 C2V_END
1319 
1320 C2V_VMENTRY(jlongArray, collectCounters, (JNIEnv*, jobject))
1321   typeArrayOop arrayOop = oopFactory::new_longArray(JVMCICounterSize, CHECK_NULL);
1322   JavaThread::collect_counters(arrayOop);
1323   return (jlongArray) JNIHandles::make_local(THREAD, arrayOop);
1324 C2V_END
1325 
1326 C2V_VMENTRY(int, allocateCompileId, (JNIEnv*, jobject, jobject jvmci_method, int entry_bci))
1327   HandleMark hm;
1328   ResourceMark rm;
1329   if (JNIHandles::resolve(jvmci_method) == NULL) {
1330     THROW_0(vmSymbols::java_lang_NullPointerException());
1331   }
1332   Method* method = CompilerToVM::asMethod(jvmci_method);
1333   if (entry_bci >= method->code_size() || entry_bci < -1) {
1334     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), err_msg("Unexpected bci %d", entry_bci));
1335   }
1336   return CompileBroker::assign_compile_id_unlocked(THREAD, method, entry_bci);
1337 C2V_END
1338 
1339 
1340 C2V_VMENTRY(jboolean, isMature, (JNIEnv*, jobject, jlong metaspace_method_data))
1341   MethodData* mdo = CompilerToVM::asMethodData(metaspace_method_data);
1342   return mdo != NULL && mdo->is_mature();
1343 C2V_END
1344 
1345 C2V_VMENTRY(jboolean, hasCompiledCodeForOSR, (JNIEnv*, jobject, jobject jvmci_method, int entry_bci, int comp_level))
1346   Method* method = CompilerToVM::asMethod(jvmci_method);
1347   return method->lookup_osr_nmethod_for(entry_bci, comp_level, true) != NULL;
1348 C2V_END
1349 
1350 C2V_VMENTRY(jobject, getSymbol, (JNIEnv*, jobject, jlong symbol))
1351   Handle sym = java_lang_String::create_from_symbol((Symbol*)(address)symbol, CHECK_NULL);
1352   return JNIHandles::make_local(THREAD, sym());
1353 C2V_END
1354 
1355 bool matches(jobjectArray methods, Method* method) {
1356   objArrayOop methods_oop = (objArrayOop) JNIHandles::resolve(methods);
1357 
1358   for (int i = 0; i < methods_oop->length(); i++) {
1359     oop resolved = methods_oop->obj_at(i);
1360     if (resolved->is_a(HotSpotResolvedJavaMethodImpl::klass()) && CompilerToVM::asMethod(resolved) == method) {
1361       return true;
1362     }
1363   }
1364   return false;
1365 }
1366 
1367 C2V_VMENTRY(jobject, getNextStackFrame, (JNIEnv*, jobject compilerToVM, jobject hs_frame, jobjectArray methods, jint initialSkip))
1368   ResourceMark rm;
1369 
1370   if (!thread->has_last_Java_frame()) return NULL;
1371   Handle result = HotSpotStackFrameReference::klass()->allocate_instance_handle(CHECK_NULL);
1372   HotSpotStackFrameReference::klass()->initialize(CHECK_NULL);
1373 
1374   StackFrameStream fst(thread);
1375   if (hs_frame != NULL) {
1376     // look for the correct stack frame if one is given
1377     intptr_t* stack_pointer = (intptr_t*) HotSpotStackFrameReference::stackPointer(hs_frame);
1378     while (fst.current()->sp() != stack_pointer && !fst.is_done()) {
1379       fst.next();
1380     }
1381     if (fst.current()->sp() != stack_pointer) {
1382       THROW_MSG_NULL(vmSymbols::java_lang_IllegalStateException(), "stack frame not found")
1383     }
1384   }
1385 
1386   int frame_number = 0;
1387   vframe* vf = vframe::new_vframe(fst.current(), fst.register_map(), thread);
1388   if (hs_frame != NULL) {
1389     // look for the correct vframe within the stack frame if one is given
1390     int last_frame_number = HotSpotStackFrameReference::frameNumber(hs_frame);
1391     while (frame_number < last_frame_number) {
1392       if (vf->is_top()) {
1393         THROW_MSG_NULL(vmSymbols::java_lang_IllegalStateException(), "invalid frame number")
1394       }
1395       vf = vf->sender();
1396       frame_number ++;
1397     }
1398     // move one frame forward
1399     if (vf->is_top()) {
1400       if (fst.is_done()) {
1401         return NULL;
1402       }
1403       fst.next();
1404       vf = vframe::new_vframe(fst.current(), fst.register_map(), thread);
1405       frame_number = 0;
1406     } else {
1407       vf = vf->sender();
1408       frame_number++;
1409     }
1410   }
1411 
1412   while (true) {
1413     // look for the given method
1414     while (true) {
1415       StackValueCollection* locals = NULL;
1416       if (vf->is_compiled_frame()) {
1417         // compiled method frame
1418         compiledVFrame* cvf = compiledVFrame::cast(vf);
1419         if (methods == NULL || matches(methods, cvf->method())) {
1420           if (initialSkip > 0) {
1421             initialSkip --;
1422           } else {
1423             ScopeDesc* scope = cvf->scope();
1424             // native wrappers do not have a scope
1425             if (scope != NULL && scope->objects() != NULL) {
1426               bool realloc_failures = Deoptimization::realloc_objects(thread, fst.current(), scope->objects(), CHECK_NULL);
1427               Deoptimization::reassign_fields(fst.current(), fst.register_map(), scope->objects(), realloc_failures, false);
1428 
1429               GrowableArray<ScopeValue*>* local_values = scope->locals();
1430               typeArrayOop array_oop = oopFactory::new_boolArray(local_values->length(), CHECK_NULL);
1431               typeArrayHandle array(THREAD, array_oop);
1432               for (int i = 0; i < local_values->length(); i++) {
1433                 ScopeValue* value = local_values->at(i);
1434                 if (value->is_object()) {
1435                   array->bool_at_put(i, true);
1436                 }
1437               }
1438               HotSpotStackFrameReference::set_localIsVirtual(result, array());
1439             } else {
1440               HotSpotStackFrameReference::set_localIsVirtual(result, NULL);
1441             }
1442 
1443             locals = cvf->locals();
1444             HotSpotStackFrameReference::set_bci(result, cvf->bci());
1445             oop method = CompilerToVM::get_jvmci_method(cvf->method(), CHECK_NULL);
1446             HotSpotStackFrameReference::set_method(result, method);
1447           }
1448         }
1449       } else if (vf->is_interpreted_frame()) {
1450         // interpreted method frame
1451         interpretedVFrame* ivf = interpretedVFrame::cast(vf);
1452         if (methods == NULL || matches(methods, ivf->method())) {
1453           if (initialSkip > 0) {
1454             initialSkip --;
1455           } else {
1456             locals = ivf->locals();
1457             HotSpotStackFrameReference::set_bci(result, ivf->bci());
1458             oop method = CompilerToVM::get_jvmci_method(ivf->method(), CHECK_NULL);
1459             HotSpotStackFrameReference::set_method(result, method);
1460             HotSpotStackFrameReference::set_localIsVirtual(result, NULL);
1461           }
1462         }
1463       }
1464 
1465       // locals != NULL means that we found a matching frame and result is already partially initialized
1466       if (locals != NULL) {
1467         HotSpotStackFrameReference::set_compilerToVM(result, JNIHandles::resolve(compilerToVM));
1468         HotSpotStackFrameReference::set_stackPointer(result, (jlong) fst.current()->sp());
1469         HotSpotStackFrameReference::set_frameNumber(result, frame_number);
1470 
1471         // initialize the locals array
1472         objArrayOop array_oop = oopFactory::new_objectArray(locals->size(), CHECK_NULL);
1473         objArrayHandle array(THREAD, array_oop);
1474         for (int i = 0; i < locals->size(); i++) {
1475           StackValue* var = locals->at(i);
1476           if (var->type() == T_OBJECT) {
1477             array->obj_at_put(i, locals->at(i)->get_obj()());
1478           }
1479         }
1480         HotSpotStackFrameReference::set_locals(result, array());
1481 
1482         return JNIHandles::make_local(thread, result());
1483       }
1484 
1485       if (vf->is_top()) {
1486         break;
1487       }
1488       frame_number++;
1489       vf = vf->sender();
1490     } // end of vframe loop
1491 
1492     if (fst.is_done()) {
1493       break;
1494     }
1495     fst.next();
1496     vf = vframe::new_vframe(fst.current(), fst.register_map(), thread);
1497     frame_number = 0;
1498   } // end of frame loop
1499 
1500   // the end was reached without finding a matching method
1501   return NULL;
1502 C2V_END
1503 
1504 C2V_VMENTRY(void, resolveInvokeDynamicInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index))
1505   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
1506   CallInfo callInfo;
1507   LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, Bytecodes::_invokedynamic, CHECK);
1508   ConstantPoolCacheEntry* cp_cache_entry = cp->invokedynamic_cp_cache_entry_at(index);
1509   cp_cache_entry->set_dynamic_call(cp, callInfo);
1510 C2V_END
1511 
1512 C2V_VMENTRY(void, resolveInvokeHandleInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index))
1513   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
1514   Klass* holder = cp->klass_ref_at(index, CHECK);
1515   Symbol* name = cp->name_ref_at(index);
1516   if (MethodHandles::is_signature_polymorphic_name(holder, name)) {
1517     CallInfo callInfo;
1518     LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, Bytecodes::_invokehandle, CHECK);
1519     ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index));
1520     cp_cache_entry->set_method_handle(cp, callInfo);
1521   }
1522 C2V_END
1523 
1524 C2V_VMENTRY(jint, isResolvedInvokeHandleInPool, (JNIEnv*, jobject, jobject jvmci_constant_pool, jint index))
1525   constantPoolHandle cp = CompilerToVM::asConstantPool(jvmci_constant_pool);
1526   ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index));
1527   if (cp_cache_entry->is_resolved(Bytecodes::_invokehandle)) {
1528     // MethodHandle.invoke* --> LambdaForm?
1529     ResourceMark rm;
1530 
1531     LinkInfo link_info(cp, index, CATCH);
1532 
1533     Klass* resolved_klass = link_info.resolved_klass();
1534 
1535     Symbol* name_sym = cp->name_ref_at(index);
1536 
1537     vmassert(MethodHandles::is_method_handle_invoke_name(resolved_klass, name_sym), "!");
1538     vmassert(MethodHandles::is_signature_polymorphic_name(resolved_klass, name_sym), "!");
1539 
1540     methodHandle adapter_method(cp_cache_entry->f1_as_method());
1541 
1542     methodHandle resolved_method(adapter_method);
1543 
1544     // Can we treat it as a regular invokevirtual?
1545     if (resolved_method->method_holder() == resolved_klass && resolved_method->name() == name_sym) {
1546       vmassert(!resolved_method->is_static(),"!");
1547       vmassert(MethodHandles::is_signature_polymorphic_method(resolved_method()),"!");
1548       vmassert(!MethodHandles::is_signature_polymorphic_static(resolved_method->intrinsic_id()), "!");
1549       vmassert(cp_cache_entry->appendix_if_resolved(cp) == NULL, "!");
1550       vmassert(cp_cache_entry->method_type_if_resolved(cp) == NULL, "!");
1551 
1552       methodHandle m(LinkResolver::linktime_resolve_virtual_method_or_null(link_info));
1553       vmassert(m == resolved_method, "!!");
1554       return -1;
1555     }
1556 
1557     return Bytecodes::_invokevirtual;
1558   }
1559   if (cp_cache_entry->is_resolved(Bytecodes::_invokedynamic)) {
1560     return Bytecodes::_invokedynamic;
1561   }
1562   return -1;
1563 C2V_END
1564 
1565 
1566 C2V_VMENTRY(jobject, getSignaturePolymorphicHolders, (JNIEnv*, jobject))
1567   objArrayHandle holders = oopFactory::new_objArray_handle(SystemDictionary::String_klass(), 2, CHECK_NULL);
1568   Handle mh = java_lang_String::create_from_str("Ljava/lang/invoke/MethodHandle;", CHECK_NULL);
1569   Handle vh = java_lang_String::create_from_str("Ljava/lang/invoke/VarHandle;", CHECK_NULL);
1570   holders->obj_at_put(0, mh());
1571   holders->obj_at_put(1, vh());
1572   return JNIHandles::make_local(THREAD, holders());
1573 C2V_END
1574 
1575 C2V_VMENTRY(jboolean, shouldDebugNonSafepoints, (JNIEnv*, jobject))
1576   //see compute_recording_non_safepoints in debugInfroRec.cpp
1577   if (JvmtiExport::should_post_compiled_method_load() && FLAG_IS_DEFAULT(DebugNonSafepoints)) {
1578     return true;
1579   }
1580   return DebugNonSafepoints;
1581 C2V_END
1582 
1583 // public native void materializeVirtualObjects(HotSpotStackFrameReference stackFrame, boolean invalidate);
1584 C2V_VMENTRY(void, materializeVirtualObjects, (JNIEnv*, jobject, jobject hs_frame, bool invalidate))
1585   ResourceMark rm;
1586 
1587   if (hs_frame == NULL) {
1588     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "stack frame is null")
1589   }
1590 
1591   HotSpotStackFrameReference::klass()->initialize(CHECK);
1592 
1593   // look for the given stack frame
1594   StackFrameStream fst(thread);
1595   intptr_t* stack_pointer = (intptr_t*) HotSpotStackFrameReference::stackPointer(hs_frame);
1596   while (fst.current()->sp() != stack_pointer && !fst.is_done()) {
1597     fst.next();
1598   }
1599   if (fst.current()->sp() != stack_pointer) {
1600     THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "stack frame not found")
1601   }
1602 
1603   if (invalidate) {
1604     if (!fst.current()->is_compiled_frame()) {
1605       THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "compiled stack frame expected")
1606     }
1607     assert(fst.current()->cb()->is_nmethod(), "nmethod expected");
1608     ((nmethod*) fst.current()->cb())->make_not_entrant();
1609   }
1610   Deoptimization::deoptimize(thread, *fst.current(), fst.register_map(), Deoptimization::Reason_none);
1611   // look for the frame again as it has been updated by deopt (pc, deopt state...)
1612   StackFrameStream fstAfterDeopt(thread);
1613   while (fstAfterDeopt.current()->sp() != stack_pointer && !fstAfterDeopt.is_done()) {
1614     fstAfterDeopt.next();
1615   }
1616   if (fstAfterDeopt.current()->sp() != stack_pointer) {
1617     THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "stack frame not found after deopt")
1618   }
1619 
1620   vframe* vf = vframe::new_vframe(fstAfterDeopt.current(), fstAfterDeopt.register_map(), thread);
1621   if (!vf->is_compiled_frame()) {
1622     THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "compiled stack frame expected")
1623   }
1624 
1625   GrowableArray<compiledVFrame*>* virtualFrames = new GrowableArray<compiledVFrame*>(10);
1626   while (true) {
1627     assert(vf->is_compiled_frame(), "Wrong frame type");
1628     virtualFrames->push(compiledVFrame::cast(vf));
1629     if (vf->is_top()) {
1630       break;
1631     }
1632     vf = vf->sender();
1633   }
1634 
1635   int last_frame_number = HotSpotStackFrameReference::frameNumber(hs_frame);
1636   if (last_frame_number >= virtualFrames->length()) {
1637     THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "invalid frame number")
1638   }
1639 
1640   // Reallocate the non-escaping objects and restore their fields.
1641   assert (virtualFrames->at(last_frame_number)->scope() != NULL,"invalid scope");
1642   GrowableArray<ScopeValue*>* objects = virtualFrames->at(last_frame_number)->scope()->objects();
1643 
1644   if (objects == NULL) {
1645     // no objects to materialize
1646     return;
1647   }
1648 
1649   bool realloc_failures = Deoptimization::realloc_objects(thread, fstAfterDeopt.current(), objects, CHECK);
1650   Deoptimization::reassign_fields(fstAfterDeopt.current(), fstAfterDeopt.register_map(), objects, realloc_failures, false);
1651 
1652   for (int frame_index = 0; frame_index < virtualFrames->length(); frame_index++) {
1653     compiledVFrame* cvf = virtualFrames->at(frame_index);
1654 
1655     GrowableArray<ScopeValue*>* scopeLocals = cvf->scope()->locals();
1656     StackValueCollection* locals = cvf->locals();
1657 
1658     if (locals != NULL) {
1659       for (int i2 = 0; i2 < locals->size(); i2++) {
1660         StackValue* var = locals->at(i2);
1661         if (var->type() == T_OBJECT && scopeLocals->at(i2)->is_object()) {
1662           jvalue val;
1663           val.l = (jobject) locals->at(i2)->get_obj()();
1664           cvf->update_local(T_OBJECT, i2, val);
1665         }
1666       }
1667     }
1668   }
1669 
1670   // all locals are materialized by now
1671   HotSpotStackFrameReference::set_localIsVirtual(hs_frame, NULL);
1672 
1673   // update the locals array
1674   objArrayHandle array(THREAD, HotSpotStackFrameReference::locals(hs_frame));
1675   StackValueCollection* locals = virtualFrames->at(last_frame_number)->locals();
1676   for (int i = 0; i < locals->size(); i++) {
1677     StackValue* var = locals->at(i);
1678     if (var->type() == T_OBJECT) {
1679       array->obj_at_put(i, locals->at(i)->get_obj()());
1680     }
1681   }
1682 C2V_END
1683 
1684 C2V_VMENTRY(void, writeDebugOutput, (JNIEnv*, jobject, jbyteArray bytes, jint offset, jint length))
1685   if (bytes == NULL) {
1686     THROW(vmSymbols::java_lang_NullPointerException());
1687   }
1688   typeArrayOop array = (typeArrayOop) JNIHandles::resolve(bytes);
1689 
1690   // Check if offset and length are non negative.
1691   if (offset < 0 || length < 0) {
1692     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
1693   }
1694   // Check if the range is valid.
1695   if ((((unsigned int) length + (unsigned int) offset) > (unsigned int) array->length())) {
1696     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
1697   }
1698   while (length > 0) {
1699     jbyte* start = array->byte_at_addr(offset);
1700     tty->write((char*) start, MIN2(length, (jint)O_BUFLEN));
1701     length -= O_BUFLEN;
1702     offset += O_BUFLEN;
1703   }
1704 C2V_END
1705 
1706 C2V_VMENTRY(void, flushDebugOutput, (JNIEnv*, jobject))
1707   tty->flush();
1708 C2V_END
1709 
1710 C2V_VMENTRY(int, methodDataProfileDataSize, (JNIEnv*, jobject, jlong metaspace_method_data, jint position))
1711   ResourceMark rm;
1712   MethodData* mdo = CompilerToVM::asMethodData(metaspace_method_data);
1713   ProfileData* profile_data = mdo->data_at(position);
1714   if (mdo->is_valid(profile_data)) {
1715     return profile_data->size_in_bytes();
1716   }
1717   DataLayout* data    = mdo->extra_data_base();
1718   DataLayout* end   = mdo->extra_data_limit();
1719   for (;; data = mdo->next_extra(data)) {
1720     assert(data < end, "moved past end of extra data");
1721     profile_data = data->data_in();
1722     if (mdo->dp_to_di(profile_data->dp()) == position) {
1723       return profile_data->size_in_bytes();
1724     }
1725   }
1726   THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), err_msg("Invalid profile data position %d", position));
1727 C2V_END
1728 
1729 C2V_VMENTRY(jlong, getFingerprint, (JNIEnv*, jobject, jlong metaspace_klass))
1730   Klass *k = CompilerToVM::asKlass(metaspace_klass);
1731   if (k->is_instance_klass()) {
1732     return InstanceKlass::cast(k)->get_stored_fingerprint();
1733   } else {
1734     return 0;
1735   }
1736 C2V_END
1737 
1738 C2V_VMENTRY(jobject, getHostClass, (JNIEnv*, jobject, jobject jvmci_type))
1739   InstanceKlass* k = InstanceKlass::cast(CompilerToVM::asKlass(jvmci_type));
1740   InstanceKlass* host = k->host_klass();
1741   oop result = CompilerToVM::get_jvmci_type(host, CHECK_NULL);
1742   return JNIHandles::make_local(THREAD, result);
1743 C2V_END
1744 
1745 C2V_VMENTRY(int, interpreterFrameSize, (JNIEnv*, jobject, jobject bytecode_frame_handle))
1746   if (bytecode_frame_handle == NULL) {
1747     THROW_0(vmSymbols::java_lang_NullPointerException());
1748   }
1749 
1750   oop top_bytecode_frame = JNIHandles::resolve_non_null(bytecode_frame_handle);
1751   oop bytecode_frame = top_bytecode_frame;
1752   int size = 0;
1753   int callee_parameters = 0;
1754   int callee_locals = 0;
1755   Method* method = getMethodFromHotSpotMethod(BytecodePosition::method(bytecode_frame));
1756   int extra_args = method->max_stack() - BytecodeFrame::numStack(bytecode_frame);
1757 
1758   while (bytecode_frame != NULL) {
1759     int locks = BytecodeFrame::numLocks(bytecode_frame);
1760     int temps = BytecodeFrame::numStack(bytecode_frame);
1761     bool is_top_frame = (bytecode_frame == top_bytecode_frame);
1762     Method* method = getMethodFromHotSpotMethod(BytecodePosition::method(bytecode_frame));
1763 
1764     int frame_size = BytesPerWord * Interpreter::size_activation(method->max_stack(),
1765                                                                  temps + callee_parameters,
1766                                                                  extra_args,
1767                                                                  locks,
1768                                                                  callee_parameters,
1769                                                                  callee_locals,
1770                                                                  is_top_frame);
1771     size += frame_size;
1772 
1773     callee_parameters = method->size_of_parameters();
1774     callee_locals = method->max_locals();
1775     extra_args = 0;
1776     bytecode_frame = BytecodePosition::caller(bytecode_frame);
1777   }
1778   return size + Deoptimization::last_frame_adjust(0, callee_locals) * BytesPerWord;
1779 C2V_END
1780 
1781 C2V_VMENTRY(void, compileToBytecode, (JNIEnv*, jobject, jobject lambda_form_handle))
1782   Handle lambda_form(THREAD, JNIHandles::resolve_non_null(lambda_form_handle));
1783   if (lambda_form->is_a(SystemDictionary::LambdaForm_klass())) {
1784     TempNewSymbol compileToBytecode = SymbolTable::new_symbol("compileToBytecode", CHECK);
1785     JavaValue result(T_VOID);
1786     JavaCalls::call_special(&result, lambda_form, SystemDictionary::LambdaForm_klass(), compileToBytecode, vmSymbols::void_method_signature(), CHECK);
1787   } else {
1788     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1789                 err_msg("Unexpected type: %s", lambda_form->klass()->external_name()));
1790   }
1791 C2V_END
1792 
1793 #define CC (char*)  /*cast a literal from (const char*)*/
1794 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &(c2v_ ## f))
1795 
1796 #define STRING                "Ljava/lang/String;"
1797 #define OBJECT                "Ljava/lang/Object;"
1798 #define CLASS                 "Ljava/lang/Class;"
1799 #define EXECUTABLE            "Ljava/lang/reflect/Executable;"
1800 #define STACK_TRACE_ELEMENT   "Ljava/lang/StackTraceElement;"
1801 #define INSTALLED_CODE        "Ljdk/vm/ci/code/InstalledCode;"
1802 #define TARGET_DESCRIPTION    "Ljdk/vm/ci/code/TargetDescription;"
1803 #define BYTECODE_FRAME        "Ljdk/vm/ci/code/BytecodeFrame;"
1804 #define RESOLVED_METHOD       "Ljdk/vm/ci/meta/ResolvedJavaMethod;"
1805 #define HS_RESOLVED_METHOD    "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaMethodImpl;"
1806 #define HS_RESOLVED_KLASS     "Ljdk/vm/ci/hotspot/HotSpotResolvedObjectTypeImpl;"
1807 #define HS_CONSTANT_POOL      "Ljdk/vm/ci/hotspot/HotSpotConstantPool;"
1808 #define HS_COMPILED_CODE      "Ljdk/vm/ci/hotspot/HotSpotCompiledCode;"
1809 #define HS_CONFIG             "Ljdk/vm/ci/hotspot/HotSpotVMConfig;"
1810 #define HS_METADATA           "Ljdk/vm/ci/hotspot/HotSpotMetaData;"
1811 #define HS_STACK_FRAME_REF    "Ljdk/vm/ci/hotspot/HotSpotStackFrameReference;"
1812 #define HS_SPECULATION_LOG    "Ljdk/vm/ci/hotspot/HotSpotSpeculationLog;"
1813 #define METASPACE_METHOD_DATA "J"
1814 
1815 JNINativeMethod CompilerToVM::methods[] = {
1816   {CC "getBytecode",                                  CC "(" HS_RESOLVED_METHOD ")[B",                                                      FN_PTR(getBytecode)},
1817   {CC "getExceptionTableStart",                       CC "(" HS_RESOLVED_METHOD ")J",                                                       FN_PTR(getExceptionTableStart)},
1818   {CC "getExceptionTableLength",                      CC "(" HS_RESOLVED_METHOD ")I",                                                       FN_PTR(getExceptionTableLength)},
1819   {CC "findUniqueConcreteMethod",                     CC "(" HS_RESOLVED_KLASS HS_RESOLVED_METHOD ")" HS_RESOLVED_METHOD,                   FN_PTR(findUniqueConcreteMethod)},
1820   {CC "getImplementor",                               CC "(" HS_RESOLVED_KLASS ")" HS_RESOLVED_KLASS,                                       FN_PTR(getImplementor)},
1821   {CC "getStackTraceElement",                         CC "(" HS_RESOLVED_METHOD "I)" STACK_TRACE_ELEMENT,                                   FN_PTR(getStackTraceElement)},
1822   {CC "methodIsIgnoredBySecurityStackWalk",           CC "(" HS_RESOLVED_METHOD ")Z",                                                       FN_PTR(methodIsIgnoredBySecurityStackWalk)},
1823   {CC "setNotInlineableOrCompileable",                CC "(" HS_RESOLVED_METHOD ")V",                                                       FN_PTR(setNotInlineableOrCompileable)},
1824   {CC "isCompilable",                                 CC "(" HS_RESOLVED_METHOD ")Z",                                                       FN_PTR(isCompilable)},
1825   {CC "hasNeverInlineDirective",                      CC "(" HS_RESOLVED_METHOD ")Z",                                                       FN_PTR(hasNeverInlineDirective)},
1826   {CC "shouldInlineMethod",                           CC "(" HS_RESOLVED_METHOD ")Z",                                                       FN_PTR(shouldInlineMethod)},
1827   {CC "lookupType",                                   CC "(" STRING CLASS "Z)" HS_RESOLVED_KLASS,                                           FN_PTR(lookupType)},
1828   {CC "lookupNameInPool",                             CC "(" HS_CONSTANT_POOL "I)" STRING,                                                  FN_PTR(lookupNameInPool)},
1829   {CC "lookupNameAndTypeRefIndexInPool",              CC "(" HS_CONSTANT_POOL "I)I",                                                        FN_PTR(lookupNameAndTypeRefIndexInPool)},
1830   {CC "lookupSignatureInPool",                        CC "(" HS_CONSTANT_POOL "I)" STRING,                                                  FN_PTR(lookupSignatureInPool)},
1831   {CC "lookupKlassRefIndexInPool",                    CC "(" HS_CONSTANT_POOL "I)I",                                                        FN_PTR(lookupKlassRefIndexInPool)},
1832   {CC "lookupKlassInPool",                            CC "(" HS_CONSTANT_POOL "I)Ljava/lang/Object;",                                       FN_PTR(lookupKlassInPool)},
1833   {CC "lookupAppendixInPool",                         CC "(" HS_CONSTANT_POOL "I)" OBJECT,                                                  FN_PTR(lookupAppendixInPool)},
1834   {CC "lookupMethodInPool",                           CC "(" HS_CONSTANT_POOL "IB)" HS_RESOLVED_METHOD,                                     FN_PTR(lookupMethodInPool)},
1835   {CC "constantPoolRemapInstructionOperandFromCache", CC "(" HS_CONSTANT_POOL "I)I",                                                        FN_PTR(constantPoolRemapInstructionOperandFromCache)},
1836   {CC "resolveConstantInPool",                        CC "(" HS_CONSTANT_POOL "I)" OBJECT,                                                  FN_PTR(resolveConstantInPool)},
1837   {CC "resolvePossiblyCachedConstantInPool",          CC "(" HS_CONSTANT_POOL "I)" OBJECT,                                                  FN_PTR(resolvePossiblyCachedConstantInPool)},
1838   {CC "resolveTypeInPool",                            CC "(" HS_CONSTANT_POOL "I)" HS_RESOLVED_KLASS,                                       FN_PTR(resolveTypeInPool)},
1839   {CC "resolveFieldInPool",                           CC "(" HS_CONSTANT_POOL "I" HS_RESOLVED_METHOD "B[I)" HS_RESOLVED_KLASS,              FN_PTR(resolveFieldInPool)},
1840   {CC "resolveInvokeDynamicInPool",                   CC "(" HS_CONSTANT_POOL "I)V",                                                        FN_PTR(resolveInvokeDynamicInPool)},
1841   {CC "resolveInvokeHandleInPool",                    CC "(" HS_CONSTANT_POOL "I)V",                                                        FN_PTR(resolveInvokeHandleInPool)},
1842   {CC "isResolvedInvokeHandleInPool",                 CC "(" HS_CONSTANT_POOL "I)I",                                                        FN_PTR(isResolvedInvokeHandleInPool)},
1843   {CC "resolveMethod",                                CC "(" HS_RESOLVED_KLASS HS_RESOLVED_METHOD HS_RESOLVED_KLASS ")" HS_RESOLVED_METHOD, FN_PTR(resolveMethod)},
1844   {CC "getSignaturePolymorphicHolders",               CC "()[" STRING,                                                                      FN_PTR(getSignaturePolymorphicHolders)},
1845   {CC "getVtableIndexForInterfaceMethod",             CC "(" HS_RESOLVED_KLASS HS_RESOLVED_METHOD ")I",                                     FN_PTR(getVtableIndexForInterfaceMethod)},
1846   {CC "getClassInitializer",                          CC "(" HS_RESOLVED_KLASS ")" HS_RESOLVED_METHOD,                                      FN_PTR(getClassInitializer)},
1847   {CC "hasFinalizableSubclass",                       CC "(" HS_RESOLVED_KLASS ")Z",                                                        FN_PTR(hasFinalizableSubclass)},
1848   {CC "getMaxCallTargetOffset",                       CC "(J)J",                                                                            FN_PTR(getMaxCallTargetOffset)},
1849   {CC "asResolvedJavaMethod",                         CC "(" EXECUTABLE ")" HS_RESOLVED_METHOD,                                             FN_PTR(asResolvedJavaMethod)},
1850   {CC "getResolvedJavaMethod",                        CC "(Ljava/lang/Object;J)" HS_RESOLVED_METHOD,                                        FN_PTR(getResolvedJavaMethod)},
1851   {CC "getConstantPool",                              CC "(Ljava/lang/Object;)" HS_CONSTANT_POOL,                                           FN_PTR(getConstantPool)},
1852   {CC "getResolvedJavaType",                          CC "(Ljava/lang/Object;JZ)" HS_RESOLVED_KLASS,                                        FN_PTR(getResolvedJavaType)},
1853   {CC "readConfiguration",                            CC "()[" OBJECT,                                                                      FN_PTR(readConfiguration)},
1854   {CC "installCode",                                  CC "(" TARGET_DESCRIPTION HS_COMPILED_CODE INSTALLED_CODE HS_SPECULATION_LOG ")I",    FN_PTR(installCode)},
1855   {CC "getMetadata",                                  CC "(" TARGET_DESCRIPTION HS_COMPILED_CODE HS_METADATA ")I",                          FN_PTR(getMetadata)},
1856   {CC "resetCompilationStatistics",                   CC "()V",                                                                             FN_PTR(resetCompilationStatistics)},
1857   {CC "disassembleCodeBlob",                          CC "(" INSTALLED_CODE ")" STRING,                                                     FN_PTR(disassembleCodeBlob)},
1858   {CC "executeInstalledCode",                         CC "([" OBJECT INSTALLED_CODE ")" OBJECT,                                             FN_PTR(executeInstalledCode)},
1859   {CC "getLineNumberTable",                           CC "(" HS_RESOLVED_METHOD ")[J",                                                      FN_PTR(getLineNumberTable)},
1860   {CC "getLocalVariableTableStart",                   CC "(" HS_RESOLVED_METHOD ")J",                                                       FN_PTR(getLocalVariableTableStart)},
1861   {CC "getLocalVariableTableLength",                  CC "(" HS_RESOLVED_METHOD ")I",                                                       FN_PTR(getLocalVariableTableLength)},
1862   {CC "reprofile",                                    CC "(" HS_RESOLVED_METHOD ")V",                                                       FN_PTR(reprofile)},
1863   {CC "invalidateInstalledCode",                      CC "(" INSTALLED_CODE ")V",                                                           FN_PTR(invalidateInstalledCode)},
1864   {CC "collectCounters",                              CC "()[J",                                                                            FN_PTR(collectCounters)},
1865   {CC "allocateCompileId",                            CC "(" HS_RESOLVED_METHOD "I)I",                                                      FN_PTR(allocateCompileId)},
1866   {CC "isMature",                                     CC "(" METASPACE_METHOD_DATA ")Z",                                                    FN_PTR(isMature)},
1867   {CC "hasCompiledCodeForOSR",                        CC "(" HS_RESOLVED_METHOD "II)Z",                                                     FN_PTR(hasCompiledCodeForOSR)},
1868   {CC "getSymbol",                                    CC "(J)" STRING,                                                                      FN_PTR(getSymbol)},
1869   {CC "getNextStackFrame",                            CC "(" HS_STACK_FRAME_REF "[" RESOLVED_METHOD "I)" HS_STACK_FRAME_REF,                FN_PTR(getNextStackFrame)},
1870   {CC "materializeVirtualObjects",                    CC "(" HS_STACK_FRAME_REF "Z)V",                                                      FN_PTR(materializeVirtualObjects)},
1871   {CC "shouldDebugNonSafepoints",                     CC "()Z",                                                                             FN_PTR(shouldDebugNonSafepoints)},
1872   {CC "writeDebugOutput",                             CC "([BII)V",                                                                         FN_PTR(writeDebugOutput)},
1873   {CC "flushDebugOutput",                             CC "()V",                                                                             FN_PTR(flushDebugOutput)},
1874   {CC "methodDataProfileDataSize",                    CC "(JI)I",                                                                           FN_PTR(methodDataProfileDataSize)},
1875   {CC "getFingerprint",                               CC "(J)J",                                                                            FN_PTR(getFingerprint)},
1876   {CC "getHostClass",                                 CC "(" HS_RESOLVED_KLASS ")" HS_RESOLVED_KLASS,                                       FN_PTR(getHostClass)},
1877   {CC "interpreterFrameSize",                         CC "(" BYTECODE_FRAME ")I",                                                           FN_PTR(interpreterFrameSize)},
1878   {CC "compileToBytecode",                            CC "(" OBJECT ")V",                                                                   FN_PTR(compileToBytecode)},
1879   {CC "getFlagValue",                                 CC "(" STRING ")" OBJECT,                                                             FN_PTR(getFlagValue)},
1880 };
1881 
1882 int CompilerToVM::methods_count() {
1883   return sizeof(methods) / sizeof(JNINativeMethod);
1884 }