Path: blob/master/src/hotspot/share/jvmci/jvmciCompilerToVM.cpp
64440 views
/*1* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "classfile/classLoaderData.inline.hpp"25#include "classfile/javaClasses.inline.hpp"26#include "classfile/stringTable.hpp"27#include "classfile/symbolTable.hpp"28#include "classfile/systemDictionary.hpp"29#include "classfile/vmClasses.hpp"30#include "code/scopeDesc.hpp"31#include "compiler/compileBroker.hpp"32#include "compiler/compilerEvent.hpp"33#include "compiler/disassembler.hpp"34#include "compiler/oopMap.hpp"35#include "interpreter/linkResolver.hpp"36#include "interpreter/bytecodeStream.hpp"37#include "jfr/jfrEvents.hpp"38#include "jvmci/jvmciCompilerToVM.hpp"39#include "jvmci/jvmciCodeInstaller.hpp"40#include "jvmci/jvmciRuntime.hpp"41#include "logging/log.hpp"42#include "logging/logTag.hpp"43#include "memory/oopFactory.hpp"44#include "memory/universe.hpp"45#include "oops/constantPool.inline.hpp"46#include "oops/instanceMirrorKlass.hpp"47#include "oops/instanceKlass.inline.hpp"48#include "oops/method.inline.hpp"49#include "oops/typeArrayOop.inline.hpp"50#include "prims/jvmtiExport.hpp"51#include "prims/methodHandles.hpp"52#include "prims/nativeLookup.hpp"53#include "runtime/atomic.hpp"54#include "runtime/deoptimization.hpp"55#include "runtime/fieldDescriptor.inline.hpp"56#include "runtime/frame.inline.hpp"57#include "runtime/globals_extension.hpp"58#include "runtime/interfaceSupport.inline.hpp"59#include "runtime/jniHandles.inline.hpp"60#include "runtime/reflectionUtils.hpp"61#include "runtime/stackFrameStream.inline.hpp"62#include "runtime/timerTrace.hpp"63#include "runtime/vframe_hp.hpp"6465JVMCIKlassHandle::JVMCIKlassHandle(Thread* thread, Klass* klass) {66_thread = thread;67_klass = klass;68if (klass != NULL) {69_holder = Handle(_thread, klass->klass_holder());70}71}7273JVMCIKlassHandle& JVMCIKlassHandle::operator=(Klass* klass) {74_klass = klass;75if (klass != NULL) {76_holder = Handle(_thread, klass->klass_holder());77}78return *this;79}8081static void requireInHotSpot(const char* caller, JVMCI_TRAPS) {82if (!JVMCIENV->is_hotspot()) {83JVMCI_THROW_MSG(IllegalStateException, err_msg("Cannot call %s from JVMCI shared library", caller));84}85}8687void JNIHandleMark::push_jni_handle_block(JavaThread* thread) {88if (thread != NULL) {89// Allocate a new block for JNI handles.90// Inlined code from jni_PushLocalFrame()91JNIHandleBlock* java_handles = thread->active_handles();92JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread);93assert(compile_handles != NULL && java_handles != NULL, "should not be NULL");94compile_handles->set_pop_frame_link(java_handles);95thread->set_active_handles(compile_handles);96}97}9899void JNIHandleMark::pop_jni_handle_block(JavaThread* thread) {100if (thread != NULL) {101// Release our JNI handle block102JNIHandleBlock* compile_handles = thread->active_handles();103JNIHandleBlock* java_handles = compile_handles->pop_frame_link();104thread->set_active_handles(java_handles);105compile_handles->set_pop_frame_link(NULL);106JNIHandleBlock::release_block(compile_handles, thread); // may block107}108}109110class JVMCITraceMark : public StackObj {111const char* _msg;112public:113JVMCITraceMark(const char* msg) {114_msg = msg;115JVMCI_event_2("Enter %s", _msg);116}117~JVMCITraceMark() {118JVMCI_event_2(" Exit %s", _msg);119}120};121122123Handle JavaArgumentUnboxer::next_arg(BasicType expectedType) {124assert(_index < _args->length(), "out of bounds");125oop arg=((objArrayOop) (_args))->obj_at(_index++);126assert(expectedType == T_OBJECT || java_lang_boxing_object::is_instance(arg, expectedType), "arg type mismatch");127return Handle(Thread::current(), arg);128}129130// Bring the JVMCI compiler thread into the VM state.131#define JVMCI_VM_ENTRY_MARK \132MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \133ThreadInVMfromNative __tiv(thread); \134HandleMarkCleaner __hm(thread); \135JavaThread* THREAD = thread; \136debug_only(VMNativeEntryWrapper __vew;)137138// Native method block that transitions current thread to '_thread_in_vm'.139#define C2V_BLOCK(result_type, name, signature) \140JVMCI_VM_ENTRY_MARK; \141ResourceMark rm; \142JNI_JVMCIENV(JVMCI::compilation_tick(thread), env);143144static JavaThread* get_current_thread(bool allow_null=true) {145Thread* thread = Thread::current_or_null_safe();146if (thread == NULL) {147assert(allow_null, "npe");148return NULL;149}150return thread->as_Java_thread();151}152153// Entry to native method implementation that transitions154// current thread to '_thread_in_vm'.155#define C2V_VMENTRY(result_type, name, signature) \156JNIEXPORT result_type JNICALL c2v_ ## name signature { \157JavaThread* thread = get_current_thread(); \158if (thread == NULL) { \159env->ThrowNew(JNIJVMCI::InternalError::clazz(), \160err_msg("Cannot call into HotSpot from JVMCI shared library without attaching current thread")); \161return; \162} \163JVMCITraceMark jtm("CompilerToVM::" #name); \164C2V_BLOCK(result_type, name, signature)165166#define C2V_VMENTRY_(result_type, name, signature, result) \167JNIEXPORT result_type JNICALL c2v_ ## name signature { \168JavaThread* thread = get_current_thread(); \169if (thread == NULL) { \170env->ThrowNew(JNIJVMCI::InternalError::clazz(), \171err_msg("Cannot call into HotSpot from JVMCI shared library without attaching current thread")); \172return result; \173} \174JVMCITraceMark jtm("CompilerToVM::" #name); \175C2V_BLOCK(result_type, name, signature)176177#define C2V_VMENTRY_NULL(result_type, name, signature) C2V_VMENTRY_(result_type, name, signature, NULL)178#define C2V_VMENTRY_0(result_type, name, signature) C2V_VMENTRY_(result_type, name, signature, 0)179180// Entry to native method implementation that does not transition181// current thread to '_thread_in_vm'.182#define C2V_VMENTRY_PREFIX(result_type, name, signature) \183JNIEXPORT result_type JNICALL c2v_ ## name signature { \184JavaThread* thread = get_current_thread();185186#define C2V_END }187188#define JNI_THROW(caller, name, msg) do { \189jint __throw_res = env->ThrowNew(JNIJVMCI::name::clazz(), msg); \190if (__throw_res != JNI_OK) { \191tty->print_cr("Throwing " #name " in " caller " returned %d", __throw_res); \192} \193return; \194} while (0);195196#define JNI_THROW_(caller, name, msg, result) do { \197jint __throw_res = env->ThrowNew(JNIJVMCI::name::clazz(), msg); \198if (__throw_res != JNI_OK) { \199tty->print_cr("Throwing " #name " in " caller " returned %d", __throw_res); \200} \201return result; \202} while (0)203204jobjectArray readConfiguration0(JNIEnv *env, JVMCI_TRAPS);205206C2V_VMENTRY_NULL(jobjectArray, readConfiguration, (JNIEnv* env))207jobjectArray config = readConfiguration0(env, JVMCI_CHECK_NULL);208return config;209}210211C2V_VMENTRY_NULL(jobject, getFlagValue, (JNIEnv* env, jobject c2vm, jobject name_handle))212#define RETURN_BOXED_LONG(value) jvalue p; p.j = (jlong) (value); JVMCIObject box = JVMCIENV->create_box(T_LONG, &p, JVMCI_CHECK_NULL); return box.as_jobject();213#define RETURN_BOXED_DOUBLE(value) jvalue p; p.d = (jdouble) (value); JVMCIObject box = JVMCIENV->create_box(T_DOUBLE, &p, JVMCI_CHECK_NULL); return box.as_jobject();214JVMCIObject name = JVMCIENV->wrap(name_handle);215if (name.is_null()) {216JVMCI_THROW_NULL(NullPointerException);217}218const char* cstring = JVMCIENV->as_utf8_string(name);219const JVMFlag* flag = JVMFlag::find_declared_flag(cstring);220if (flag == NULL) {221return c2vm;222}223if (flag->is_bool()) {224jvalue prim;225prim.z = flag->get_bool();226JVMCIObject box = JVMCIENV->create_box(T_BOOLEAN, &prim, JVMCI_CHECK_NULL);227return JVMCIENV->get_jobject(box);228} else if (flag->is_ccstr()) {229JVMCIObject value = JVMCIENV->create_string(flag->get_ccstr(), JVMCI_CHECK_NULL);230return JVMCIENV->get_jobject(value);231} else if (flag->is_intx()) {232RETURN_BOXED_LONG(flag->get_intx());233} else if (flag->is_int()) {234RETURN_BOXED_LONG(flag->get_int());235} else if (flag->is_uint()) {236RETURN_BOXED_LONG(flag->get_uint());237} else if (flag->is_uint64_t()) {238RETURN_BOXED_LONG(flag->get_uint64_t());239} else if (flag->is_size_t()) {240RETURN_BOXED_LONG(flag->get_size_t());241} else if (flag->is_uintx()) {242RETURN_BOXED_LONG(flag->get_uintx());243} else if (flag->is_double()) {244RETURN_BOXED_DOUBLE(flag->get_double());245} else {246JVMCI_ERROR_NULL("VM flag %s has unsupported type %s", flag->name(), flag->type_string());247}248#undef RETURN_BOXED_LONG249#undef RETURN_BOXED_DOUBLE250C2V_END251252C2V_VMENTRY_NULL(jbyteArray, getBytecode, (JNIEnv* env, jobject, jobject jvmci_method))253methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));254255int code_size = method->code_size();256jbyte* reconstituted_code = NEW_RESOURCE_ARRAY(jbyte, code_size);257258guarantee(method->method_holder()->is_rewritten(), "Method's holder should be rewritten");259// iterate over all bytecodes and replace non-Java bytecodes260261for (BytecodeStream s(method); s.next() != Bytecodes::_illegal; ) {262Bytecodes::Code code = s.code();263Bytecodes::Code raw_code = s.raw_code();264int bci = s.bci();265int len = s.instruction_size();266267// Restore original byte code.268reconstituted_code[bci] = (jbyte) (s.is_wide()? Bytecodes::_wide : code);269if (len > 1) {270memcpy(reconstituted_code + (bci + 1), s.bcp()+1, len-1);271}272273if (len > 1) {274// Restore the big-endian constant pool indexes.275// Cf. Rewriter::scan_method276switch (code) {277case Bytecodes::_getstatic:278case Bytecodes::_putstatic:279case Bytecodes::_getfield:280case Bytecodes::_putfield:281case Bytecodes::_invokevirtual:282case Bytecodes::_invokespecial:283case Bytecodes::_invokestatic:284case Bytecodes::_invokeinterface:285case Bytecodes::_invokehandle: {286int cp_index = Bytes::get_native_u2((address) reconstituted_code + (bci + 1));287Bytes::put_Java_u2((address) reconstituted_code + (bci + 1), (u2) cp_index);288break;289}290291case Bytecodes::_invokedynamic: {292int cp_index = Bytes::get_native_u4((address) reconstituted_code + (bci + 1));293Bytes::put_Java_u4((address) reconstituted_code + (bci + 1), (u4) cp_index);294break;295}296297default:298break;299}300301// Not all ldc byte code are rewritten.302switch (raw_code) {303case Bytecodes::_fast_aldc: {304int cpc_index = reconstituted_code[bci + 1] & 0xff;305int cp_index = method->constants()->object_to_cp_index(cpc_index);306assert(cp_index < method->constants()->length(), "sanity check");307reconstituted_code[bci + 1] = (jbyte) cp_index;308break;309}310311case Bytecodes::_fast_aldc_w: {312int cpc_index = Bytes::get_native_u2((address) reconstituted_code + (bci + 1));313int cp_index = method->constants()->object_to_cp_index(cpc_index);314assert(cp_index < method->constants()->length(), "sanity check");315Bytes::put_Java_u2((address) reconstituted_code + (bci + 1), (u2) cp_index);316break;317}318319default:320break;321}322}323}324325JVMCIPrimitiveArray result = JVMCIENV->new_byteArray(code_size, JVMCI_CHECK_NULL);326JVMCIENV->copy_bytes_from(reconstituted_code, result, 0, code_size);327return JVMCIENV->get_jbyteArray(result);328C2V_END329330C2V_VMENTRY_0(jint, getExceptionTableLength, (JNIEnv* env, jobject, jobject jvmci_method))331Method* method = JVMCIENV->asMethod(jvmci_method);332return method->exception_table_length();333C2V_END334335C2V_VMENTRY_0(jlong, getExceptionTableStart, (JNIEnv* env, jobject, jobject jvmci_method))336Method* method = JVMCIENV->asMethod(jvmci_method);337if (method->exception_table_length() == 0) {338return 0L;339}340return (jlong) (address) method->exception_table_start();341C2V_END342343C2V_VMENTRY_NULL(jobject, asResolvedJavaMethod, (JNIEnv* env, jobject, jobject executable_handle))344requireInHotSpot("asResolvedJavaMethod", JVMCI_CHECK_NULL);345oop executable = JNIHandles::resolve(executable_handle);346oop mirror = NULL;347int slot = 0;348349if (executable->klass() == vmClasses::reflect_Constructor_klass()) {350mirror = java_lang_reflect_Constructor::clazz(executable);351slot = java_lang_reflect_Constructor::slot(executable);352} else {353assert(executable->klass() == vmClasses::reflect_Method_klass(), "wrong type");354mirror = java_lang_reflect_Method::clazz(executable);355slot = java_lang_reflect_Method::slot(executable);356}357Klass* holder = java_lang_Class::as_Klass(mirror);358methodHandle method (THREAD, InstanceKlass::cast(holder)->method_with_idnum(slot));359JVMCIObject result = JVMCIENV->get_jvmci_method(method, JVMCI_CHECK_NULL);360return JVMCIENV->get_jobject(result);361}362363C2V_VMENTRY_NULL(jobject, getResolvedJavaMethod, (JNIEnv* env, jobject, jobject base, jlong offset))364Method* method = NULL;365JVMCIObject base_object = JVMCIENV->wrap(base);366if (base_object.is_null()) {367method = *((Method**)(offset));368} else if (JVMCIENV->isa_HotSpotObjectConstantImpl(base_object)) {369Handle obj = JVMCIENV->asConstant(base_object, JVMCI_CHECK_NULL);370if (obj->is_a(vmClasses::ResolvedMethodName_klass())) {371method = (Method*) (intptr_t) obj->long_field(offset);372} else {373JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Unexpected type: %s", obj->klass()->external_name()));374}375} else if (JVMCIENV->isa_HotSpotResolvedJavaMethodImpl(base_object)) {376method = JVMCIENV->asMethod(base_object);377}378if (method == NULL) {379JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Unexpected type: %s", JVMCIENV->klass_name(base_object)));380}381assert (method->is_method(), "invalid read");382JVMCIObject result = JVMCIENV->get_jvmci_method(methodHandle(THREAD, method), JVMCI_CHECK_NULL);383return JVMCIENV->get_jobject(result);384}385386C2V_VMENTRY_NULL(jobject, getConstantPool, (JNIEnv* env, jobject, jobject object_handle))387ConstantPool* cp = NULL;388JVMCIObject object = JVMCIENV->wrap(object_handle);389if (object.is_null()) {390JVMCI_THROW_NULL(NullPointerException);391}392if (JVMCIENV->isa_HotSpotResolvedJavaMethodImpl(object)) {393cp = JVMCIENV->asMethod(object)->constMethod()->constants();394} else if (JVMCIENV->isa_HotSpotResolvedObjectTypeImpl(object)) {395cp = InstanceKlass::cast(JVMCIENV->asKlass(object))->constants();396} else {397JVMCI_THROW_MSG_NULL(IllegalArgumentException,398err_msg("Unexpected type: %s", JVMCIENV->klass_name(object)));399}400assert(cp != NULL, "npe");401402JVMCIObject result = JVMCIENV->get_jvmci_constant_pool(constantPoolHandle(THREAD, cp), JVMCI_CHECK_NULL);403return JVMCIENV->get_jobject(result);404}405406C2V_VMENTRY_NULL(jobject, getResolvedJavaType0, (JNIEnv* env, jobject, jobject base, jlong offset, jboolean compressed))407JVMCIKlassHandle klass(THREAD);408JVMCIObject base_object = JVMCIENV->wrap(base);409jlong base_address = 0;410if (base_object.is_non_null() && offset == oopDesc::klass_offset_in_bytes()) {411// klass = JVMCIENV->unhandle(base_object)->klass();412if (JVMCIENV->isa_HotSpotObjectConstantImpl(base_object)) {413Handle base_oop = JVMCIENV->asConstant(base_object, JVMCI_CHECK_NULL);414klass = base_oop->klass();415} else {416assert(false, "What types are we actually expecting here?");417}418} else if (!compressed) {419if (base_object.is_non_null()) {420if (JVMCIENV->isa_HotSpotResolvedJavaMethodImpl(base_object)) {421base_address = (intptr_t) JVMCIENV->asMethod(base_object);422} else if (JVMCIENV->isa_HotSpotConstantPool(base_object)) {423base_address = (intptr_t) JVMCIENV->asConstantPool(base_object);424} else if (JVMCIENV->isa_HotSpotResolvedObjectTypeImpl(base_object)) {425base_address = (intptr_t) JVMCIENV->asKlass(base_object);426} else if (JVMCIENV->isa_HotSpotObjectConstantImpl(base_object)) {427Handle base_oop = JVMCIENV->asConstant(base_object, JVMCI_CHECK_NULL);428if (base_oop->is_a(vmClasses::Class_klass())) {429base_address = cast_from_oop<jlong>(base_oop());430}431}432if (base_address == 0) {433JVMCI_THROW_MSG_NULL(IllegalArgumentException,434err_msg("Unexpected arguments: %s " JLONG_FORMAT " %s", JVMCIENV->klass_name(base_object), offset, compressed ? "true" : "false"));435}436}437klass = *((Klass**) (intptr_t) (base_address + offset));438} else {439JVMCI_THROW_MSG_NULL(IllegalArgumentException,440err_msg("Unexpected arguments: %s " JLONG_FORMAT " %s",441base_object.is_non_null() ? JVMCIENV->klass_name(base_object) : "null",442offset, compressed ? "true" : "false"));443}444assert (klass == NULL || klass->is_klass(), "invalid read");445JVMCIObject result = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL);446return JVMCIENV->get_jobject(result);447}448449C2V_VMENTRY_NULL(jobject, findUniqueConcreteMethod, (JNIEnv* env, jobject, jobject jvmci_type, jobject jvmci_method))450methodHandle method (THREAD, JVMCIENV->asMethod(jvmci_method));451InstanceKlass* holder = InstanceKlass::cast(JVMCIENV->asKlass(jvmci_type));452if (holder->is_interface()) {453JVMCI_THROW_MSG_NULL(InternalError, err_msg("Interface %s should be handled in Java code", holder->external_name()));454}455if (method->can_be_statically_bound()) {456JVMCI_THROW_MSG_NULL(InternalError, err_msg("Effectively static method %s.%s should be handled in Java code", method->method_holder()->external_name(), method->external_name()));457}458459methodHandle ucm;460{461MutexLocker locker(Compile_lock);462ucm = methodHandle(THREAD, Dependencies::find_unique_concrete_method(holder, method()));463}464JVMCIObject result = JVMCIENV->get_jvmci_method(ucm, JVMCI_CHECK_NULL);465return JVMCIENV->get_jobject(result);466C2V_END467468C2V_VMENTRY_NULL(jobject, getImplementor, (JNIEnv* env, jobject, jobject jvmci_type))469Klass* klass = JVMCIENV->asKlass(jvmci_type);470if (!klass->is_interface()) {471THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),472err_msg("Expected interface type, got %s", klass->external_name()));473}474InstanceKlass* iklass = InstanceKlass::cast(klass);475JVMCIKlassHandle handle(THREAD);476{477// Need Compile_lock around implementor()478MutexLocker locker(Compile_lock);479handle = iklass->implementor();480}481JVMCIObject implementor = JVMCIENV->get_jvmci_type(handle, JVMCI_CHECK_NULL);482return JVMCIENV->get_jobject(implementor);483C2V_END484485C2V_VMENTRY_0(jboolean, methodIsIgnoredBySecurityStackWalk,(JNIEnv* env, jobject, jobject jvmci_method))486Method* method = JVMCIENV->asMethod(jvmci_method);487return method->is_ignored_by_security_stack_walk();488C2V_END489490C2V_VMENTRY_0(jboolean, isCompilable,(JNIEnv* env, jobject, jobject jvmci_method))491Method* method = JVMCIENV->asMethod(jvmci_method);492// Skip redefined methods493if (method->is_old()) {494return false;495}496return !method->is_not_compilable(CompLevel_full_optimization);497C2V_END498499C2V_VMENTRY_0(jboolean, hasNeverInlineDirective,(JNIEnv* env, jobject, jobject jvmci_method))500methodHandle method (THREAD, JVMCIENV->asMethod(jvmci_method));501return !Inline || CompilerOracle::should_not_inline(method) || method->dont_inline();502C2V_END503504C2V_VMENTRY_0(jboolean, shouldInlineMethod,(JNIEnv* env, jobject, jobject jvmci_method))505methodHandle method (THREAD, JVMCIENV->asMethod(jvmci_method));506return CompilerOracle::should_inline(method) || method->force_inline();507C2V_END508509C2V_VMENTRY_NULL(jobject, lookupType, (JNIEnv* env, jobject, jstring jname, jclass accessing_class, jboolean resolve))510JVMCIObject name = JVMCIENV->wrap(jname);511const char* str = JVMCIENV->as_utf8_string(name);512TempNewSymbol class_name = SymbolTable::new_symbol(str);513514if (class_name->utf8_length() <= 1) {515JVMCI_THROW_MSG_0(InternalError, err_msg("Primitive type %s should be handled in Java code", class_name->as_C_string()));516}517518JVMCIKlassHandle resolved_klass(THREAD);519Klass* accessing_klass = NULL;520Handle class_loader;521Handle protection_domain;522if (accessing_class != NULL) {523accessing_klass = JVMCIENV->asKlass(accessing_class);524class_loader = Handle(THREAD, accessing_klass->class_loader());525protection_domain = Handle(THREAD, accessing_klass->protection_domain());526} else {527// Use the System class loader528class_loader = Handle(THREAD, SystemDictionary::java_system_loader());529JVMCIENV->runtime()->initialize(JVMCIENV);530}531532if (resolve) {533resolved_klass = SystemDictionary::resolve_or_null(class_name, class_loader, protection_domain, CHECK_NULL);534if (resolved_klass == NULL) {535JVMCI_THROW_MSG_NULL(ClassNotFoundException, str);536}537} else {538if (Signature::has_envelope(class_name)) {539// This is a name from a signature. Strip off the trimmings.540// Call recursive to keep scope of strippedsym.541TempNewSymbol strippedsym = Signature::strip_envelope(class_name);542resolved_klass = SystemDictionary::find_instance_klass(strippedsym,543class_loader,544protection_domain);545} else if (Signature::is_array(class_name)) {546SignatureStream ss(class_name, false);547int ndim = ss.skip_array_prefix();548if (ss.type() == T_OBJECT) {549Symbol* strippedsym = ss.as_symbol();550resolved_klass = SystemDictionary::find_instance_klass(strippedsym,551class_loader,552protection_domain);553if (!resolved_klass.is_null()) {554resolved_klass = resolved_klass->array_klass(ndim, CHECK_NULL);555}556} else {557resolved_klass = TypeArrayKlass::cast(Universe::typeArrayKlassObj(ss.type()))->array_klass(ndim, CHECK_NULL);558}559} else {560resolved_klass = SystemDictionary::find_instance_klass(class_name,561class_loader,562protection_domain);563}564}565JVMCIObject result = JVMCIENV->get_jvmci_type(resolved_klass, JVMCI_CHECK_NULL);566return JVMCIENV->get_jobject(result);567C2V_END568569C2V_VMENTRY_NULL(jobject, getArrayType, (JNIEnv* env, jobject, jobject jvmci_type))570if (jvmci_type == NULL) {571JVMCI_THROW_0(NullPointerException);572}573574JVMCIObject jvmci_type_object = JVMCIENV->wrap(jvmci_type);575JVMCIKlassHandle array_klass(THREAD);576if (JVMCIENV->isa_HotSpotResolvedPrimitiveType(jvmci_type_object)) {577BasicType type = JVMCIENV->kindToBasicType(JVMCIENV->get_HotSpotResolvedPrimitiveType_kind(jvmci_type_object), JVMCI_CHECK_0);578if (type == T_VOID) {579return NULL;580}581array_klass = Universe::typeArrayKlassObj(type);582if (array_klass == NULL) {583JVMCI_THROW_MSG_NULL(InternalError, err_msg("No array klass for primitive type %s", type2name(type)));584}585} else {586Klass* klass = JVMCIENV->asKlass(jvmci_type);587if (klass == NULL) {588JVMCI_THROW_0(NullPointerException);589}590array_klass = klass->array_klass(CHECK_NULL);591}592JVMCIObject result = JVMCIENV->get_jvmci_type(array_klass, JVMCI_CHECK_NULL);593return JVMCIENV->get_jobject(result);594C2V_END595596C2V_VMENTRY_NULL(jobject, lookupClass, (JNIEnv* env, jobject, jclass mirror))597requireInHotSpot("lookupClass", JVMCI_CHECK_NULL);598if (mirror == NULL) {599return NULL;600}601JVMCIKlassHandle klass(THREAD);602klass = java_lang_Class::as_Klass(JNIHandles::resolve(mirror));603if (klass == NULL) {604JVMCI_THROW_MSG_NULL(IllegalArgumentException, "Primitive classes are unsupported");605}606JVMCIObject result = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL);607return JVMCIENV->get_jobject(result);608}609610C2V_VMENTRY_NULL(jobject, resolvePossiblyCachedConstantInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))611constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));612oop obj = cp->resolve_possibly_cached_constant_at(index, CHECK_NULL);613constantTag tag = cp->tag_at(index);614if (tag.is_dynamic_constant() || tag.is_dynamic_constant_in_error()) {615if (obj == Universe::the_null_sentinel()) {616return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_NULL_POINTER());617}618BasicType bt = Signature::basic_type(cp->uncached_signature_ref_at(index));619if (!is_reference_type(bt)) {620if (!is_java_primitive(bt)) {621return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_ILLEGAL());622}623624// Convert standard box (e.g. java.lang.Integer) to JVMCI box (e.g. jdk.vm.ci.meta.PrimitiveConstant)625jvalue value;626jlong raw_value;627JVMCIObject kind;628BasicType bt2 = java_lang_boxing_object::get_value(obj, &value);629assert(bt2 == bt, "");630switch (bt2) {631case T_LONG: kind = JVMCIENV->get_JavaKind_Long(); raw_value = value.j; break;632case T_DOUBLE: kind = JVMCIENV->get_JavaKind_Double(); raw_value = value.j; break;633case T_FLOAT: kind = JVMCIENV->get_JavaKind_Float(); raw_value = value.i; break;634case T_INT: kind = JVMCIENV->get_JavaKind_Int(); raw_value = value.i; break;635case T_SHORT: kind = JVMCIENV->get_JavaKind_Short(); raw_value = value.s; break;636case T_BYTE: kind = JVMCIENV->get_JavaKind_Byte(); raw_value = value.b; break;637case T_CHAR: kind = JVMCIENV->get_JavaKind_Char(); raw_value = value.c; break;638case T_BOOLEAN: kind = JVMCIENV->get_JavaKind_Boolean(); raw_value = value.z; break;639default: return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_ILLEGAL());640}641642JVMCIObject result = JVMCIENV->call_JavaConstant_forPrimitive(kind, raw_value, JVMCI_CHECK_NULL);643return JVMCIENV->get_jobject(result);644}645}646return JVMCIENV->get_jobject(JVMCIENV->get_object_constant(obj));647C2V_END648649C2V_VMENTRY_0(jint, lookupNameAndTypeRefIndexInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))650constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));651return cp->name_and_type_ref_index_at(index);652C2V_END653654C2V_VMENTRY_NULL(jobject, lookupNameInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint which))655constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));656JVMCIObject sym = JVMCIENV->create_string(cp->name_ref_at(which), JVMCI_CHECK_NULL);657return JVMCIENV->get_jobject(sym);658C2V_END659660C2V_VMENTRY_NULL(jobject, lookupSignatureInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint which))661constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));662JVMCIObject sym = JVMCIENV->create_string(cp->signature_ref_at(which), JVMCI_CHECK_NULL);663return JVMCIENV->get_jobject(sym);664C2V_END665666C2V_VMENTRY_0(jint, lookupKlassRefIndexInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))667constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));668return cp->klass_ref_index_at(index);669C2V_END670671C2V_VMENTRY_NULL(jobject, resolveTypeInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))672constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));673Klass* klass = cp->klass_at(index, CHECK_NULL);674JVMCIKlassHandle resolved_klass(THREAD, klass);675if (resolved_klass->is_instance_klass()) {676InstanceKlass::cast(resolved_klass())->link_class(CHECK_NULL);677if (!InstanceKlass::cast(resolved_klass())->is_linked()) {678// link_class() should not return here if there is an issue.679JVMCI_THROW_MSG_NULL(InternalError, err_msg("Class %s must be linked", resolved_klass()->external_name()));680}681}682JVMCIObject klassObject = JVMCIENV->get_jvmci_type(resolved_klass, JVMCI_CHECK_NULL);683return JVMCIENV->get_jobject(klassObject);684C2V_END685686C2V_VMENTRY_NULL(jobject, lookupKlassInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index, jbyte opcode))687constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));688Klass* loading_klass = cp->pool_holder();689bool is_accessible = false;690JVMCIKlassHandle klass(THREAD, JVMCIRuntime::get_klass_by_index(cp, index, is_accessible, loading_klass));691Symbol* symbol = NULL;692if (klass.is_null()) {693constantTag tag = cp->tag_at(index);694if (tag.is_klass()) {695// The klass has been inserted into the constant pool696// very recently.697klass = cp->resolved_klass_at(index);698} else if (tag.is_symbol()) {699symbol = cp->symbol_at(index);700} else {701assert(cp->tag_at(index).is_unresolved_klass(), "wrong tag");702symbol = cp->klass_name_at(index);703}704}705JVMCIObject result;706if (!klass.is_null()) {707result = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL);708} else {709result = JVMCIENV->create_string(symbol, JVMCI_CHECK_NULL);710}711return JVMCIENV->get_jobject(result);712C2V_END713714C2V_VMENTRY_NULL(jobject, lookupAppendixInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))715constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));716oop appendix_oop = ConstantPool::appendix_at_if_loaded(cp, index);717return JVMCIENV->get_jobject(JVMCIENV->get_object_constant(appendix_oop));718C2V_END719720C2V_VMENTRY_NULL(jobject, lookupMethodInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index, jbyte opcode))721constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));722InstanceKlass* pool_holder = cp->pool_holder();723Bytecodes::Code bc = (Bytecodes::Code) (((int) opcode) & 0xFF);724methodHandle method(THREAD, JVMCIRuntime::get_method_by_index(cp, index, bc, pool_holder));725JVMCIObject result = JVMCIENV->get_jvmci_method(method, JVMCI_CHECK_NULL);726return JVMCIENV->get_jobject(result);727C2V_END728729C2V_VMENTRY_0(jint, constantPoolRemapInstructionOperandFromCache, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))730constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));731return cp->remap_instruction_operand_from_cache(index);732C2V_END733734C2V_VMENTRY_NULL(jobject, resolveFieldInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index, jobject jvmci_method, jbyte opcode, jintArray info_handle))735constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));736Bytecodes::Code code = (Bytecodes::Code)(((int) opcode) & 0xFF);737fieldDescriptor fd;738methodHandle mh(THREAD, (jvmci_method != NULL) ? JVMCIENV->asMethod(jvmci_method) : NULL);739LinkInfo link_info(cp, index, mh, CHECK_NULL);740LinkResolver::resolve_field(fd, link_info, Bytecodes::java_code(code), false, CHECK_NULL);741JVMCIPrimitiveArray info = JVMCIENV->wrap(info_handle);742if (info.is_null() || JVMCIENV->get_length(info) != 3) {743JVMCI_ERROR_NULL("info must not be null and have a length of 3");744}745JVMCIENV->put_int_at(info, 0, fd.access_flags().as_int());746JVMCIENV->put_int_at(info, 1, fd.offset());747JVMCIENV->put_int_at(info, 2, fd.index());748JVMCIKlassHandle handle(THREAD, fd.field_holder());749JVMCIObject field_holder = JVMCIENV->get_jvmci_type(handle, JVMCI_CHECK_NULL);750return JVMCIENV->get_jobject(field_holder);751C2V_END752753C2V_VMENTRY_0(jint, getVtableIndexForInterfaceMethod, (JNIEnv* env, jobject, jobject jvmci_type, jobject jvmci_method))754Klass* klass = JVMCIENV->asKlass(jvmci_type);755methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));756InstanceKlass* holder = method->method_holder();757if (klass->is_interface()) {758JVMCI_THROW_MSG_0(InternalError, err_msg("Interface %s should be handled in Java code", klass->external_name()));759}760if (!holder->is_interface()) {761JVMCI_THROW_MSG_0(InternalError, err_msg("Method %s is not held by an interface, this case should be handled in Java code", method->name_and_sig_as_C_string()));762}763if (!klass->is_instance_klass()) {764JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s must be instance klass", klass->external_name()));765}766if (!InstanceKlass::cast(klass)->is_linked()) {767JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s must be linked", klass->external_name()));768}769if (!klass->is_subtype_of(holder)) {770JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s does not implement interface %s", klass->external_name(), holder->external_name()));771}772return LinkResolver::vtable_index_of_interface_method(klass, method);773C2V_END774775C2V_VMENTRY_NULL(jobject, resolveMethod, (JNIEnv* env, jobject, jobject receiver_jvmci_type, jobject jvmci_method, jobject caller_jvmci_type))776Klass* recv_klass = JVMCIENV->asKlass(receiver_jvmci_type);777Klass* caller_klass = JVMCIENV->asKlass(caller_jvmci_type);778methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));779780Klass* resolved = method->method_holder();781Symbol* h_name = method->name();782Symbol* h_signature = method->signature();783784if (MethodHandles::is_signature_polymorphic_method(method())) {785// Signature polymorphic methods are already resolved, JVMCI just returns NULL in this case.786return NULL;787}788789if (method->name() == vmSymbols::clone_name() &&790resolved == vmClasses::Object_klass() &&791recv_klass->is_array_klass()) {792// Resolution of the clone method on arrays always returns Object.clone even though that method793// has protected access. There's some trickery in the access checking to make this all work out794// so it's necessary to pass in the array class as the resolved class to properly trigger this.795// Otherwise it's impossible to resolve the array clone methods through JVMCI. See796// LinkResolver::check_method_accessability for the matching logic.797resolved = recv_klass;798}799800LinkInfo link_info(resolved, h_name, h_signature, caller_klass);801Method* m = NULL;802// Only do exact lookup if receiver klass has been linked. Otherwise,803// the vtable has not been setup, and the LinkResolver will fail.804if (recv_klass->is_array_klass() ||805(InstanceKlass::cast(recv_klass)->is_linked() && !recv_klass->is_interface())) {806if (resolved->is_interface()) {807m = LinkResolver::resolve_interface_call_or_null(recv_klass, link_info);808} else {809m = LinkResolver::resolve_virtual_call_or_null(recv_klass, link_info);810}811}812813if (m == NULL) {814// Return NULL if there was a problem with lookup (uninitialized class, etc.)815return NULL;816}817818JVMCIObject result = JVMCIENV->get_jvmci_method(methodHandle(THREAD, m), JVMCI_CHECK_NULL);819return JVMCIENV->get_jobject(result);820C2V_END821822C2V_VMENTRY_0(jboolean, hasFinalizableSubclass,(JNIEnv* env, jobject, jobject jvmci_type))823Klass* klass = JVMCIENV->asKlass(jvmci_type);824assert(klass != NULL, "method must not be called for primitive types");825if (!klass->is_instance_klass()) {826return false;827}828InstanceKlass* iklass = InstanceKlass::cast(klass);829return Dependencies::find_finalizable_subclass(iklass) != NULL;830C2V_END831832C2V_VMENTRY_NULL(jobject, getClassInitializer, (JNIEnv* env, jobject, jobject jvmci_type))833Klass* klass = JVMCIENV->asKlass(jvmci_type);834if (!klass->is_instance_klass()) {835return NULL;836}837InstanceKlass* iklass = InstanceKlass::cast(klass);838methodHandle clinit(THREAD, iklass->class_initializer());839JVMCIObject result = JVMCIENV->get_jvmci_method(clinit, JVMCI_CHECK_NULL);840return JVMCIENV->get_jobject(result);841C2V_END842843C2V_VMENTRY_0(jlong, getMaxCallTargetOffset, (JNIEnv* env, jobject, jlong addr))844address target_addr = (address) addr;845if (target_addr != 0x0) {846int64_t off_low = (int64_t)target_addr - ((int64_t)CodeCache::low_bound() + sizeof(int));847int64_t off_high = (int64_t)target_addr - ((int64_t)CodeCache::high_bound() + sizeof(int));848return MAX2(ABS(off_low), ABS(off_high));849}850return -1;851C2V_END852853C2V_VMENTRY(void, setNotInlinableOrCompilable,(JNIEnv* env, jobject, jobject jvmci_method))854methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));855method->set_not_c1_compilable();856method->set_not_c2_compilable();857method->set_dont_inline(true);858C2V_END859860C2V_VMENTRY_0(jint, installCode, (JNIEnv *env, jobject, jobject target, jobject compiled_code,861jobject installed_code, jlong failed_speculations_address, jbyteArray speculations_obj))862HandleMark hm(THREAD);863JNIHandleMark jni_hm(thread);864865JVMCIObject target_handle = JVMCIENV->wrap(target);866JVMCIObject compiled_code_handle = JVMCIENV->wrap(compiled_code);867CodeBlob* cb = NULL;868JVMCIObject installed_code_handle = JVMCIENV->wrap(installed_code);869JVMCIPrimitiveArray speculations_handle = JVMCIENV->wrap(speculations_obj);870871int speculations_len = JVMCIENV->get_length(speculations_handle);872char* speculations = NEW_RESOURCE_ARRAY(char, speculations_len);873JVMCIENV->copy_bytes_to(speculations_handle, (jbyte*) speculations, 0, speculations_len);874875JVMCICompiler* compiler = JVMCICompiler::instance(true, CHECK_JNI_ERR);876877TraceTime install_time("installCode", JVMCICompiler::codeInstallTimer(!thread->is_Compiler_thread()));878879nmethodLocker nmethod_handle;880CodeInstaller installer(JVMCIENV);881JVMCI::CodeInstallResult result = installer.install(compiler,882target_handle,883compiled_code_handle,884cb,885nmethod_handle,886installed_code_handle,887(FailedSpeculation**)(address) failed_speculations_address,888speculations,889speculations_len,890JVMCI_CHECK_0);891892if (PrintCodeCacheOnCompilation) {893stringStream s;894// Dump code cache into a buffer before locking the tty,895{896MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);897CodeCache::print_summary(&s, false);898}899ttyLocker ttyl;900tty->print_raw_cr(s.as_string());901}902903if (result != JVMCI::ok) {904assert(cb == NULL, "should be");905} else {906if (installed_code_handle.is_non_null()) {907if (cb->is_nmethod()) {908assert(JVMCIENV->isa_HotSpotNmethod(installed_code_handle), "wrong type");909// Clear the link to an old nmethod first910JVMCIObject nmethod_mirror = installed_code_handle;911JVMCIENV->invalidate_nmethod_mirror(nmethod_mirror, JVMCI_CHECK_0);912} else {913assert(JVMCIENV->isa_InstalledCode(installed_code_handle), "wrong type");914}915// Initialize the link to the new code blob916JVMCIENV->initialize_installed_code(installed_code_handle, cb, JVMCI_CHECK_0);917}918}919return result;920C2V_END921922C2V_VMENTRY_0(jint, getMetadata, (JNIEnv *env, jobject, jobject target, jobject compiled_code, jobject metadata))923JVMCI_THROW_MSG_0(InternalError, "unimplemented");924C2V_END925926C2V_VMENTRY(void, resetCompilationStatistics, (JNIEnv* env, jobject))927JVMCICompiler* compiler = JVMCICompiler::instance(true, CHECK);928CompilerStatistics* stats = compiler->stats();929stats->_standard.reset();930stats->_osr.reset();931C2V_END932933C2V_VMENTRY_NULL(jobject, disassembleCodeBlob, (JNIEnv* env, jobject, jobject installedCode))934HandleMark hm(THREAD);935936if (installedCode == NULL) {937JVMCI_THROW_MSG_NULL(NullPointerException, "installedCode is null");938}939940JVMCIObject installedCodeObject = JVMCIENV->wrap(installedCode);941nmethodLocker locker;942CodeBlob* cb = JVMCIENV->get_code_blob(installedCodeObject, locker);943if (cb == NULL) {944return NULL;945}946947// We don't want the stringStream buffer to resize during disassembly as it948// uses scoped resource memory. If a nested function called during disassembly uses949// a ResourceMark and the buffer expands within the scope of the mark,950// the buffer becomes garbage when that scope is exited. Experience shows that951// the disassembled code is typically about 10x the code size so a fixed buffer952// sized to 20x code size plus a fixed amount for header info should be sufficient.953int bufferSize = cb->code_size() * 20 + 1024;954char* buffer = NEW_RESOURCE_ARRAY(char, bufferSize);955stringStream st(buffer, bufferSize);956if (cb->is_nmethod()) {957nmethod* nm = (nmethod*) cb;958if (!nm->is_alive()) {959return NULL;960}961}962Disassembler::decode(cb, &st);963if (st.size() <= 0) {964return NULL;965}966967JVMCIObject result = JVMCIENV->create_string(st.as_string(), JVMCI_CHECK_NULL);968return JVMCIENV->get_jobject(result);969C2V_END970971C2V_VMENTRY_NULL(jobject, getStackTraceElement, (JNIEnv* env, jobject, jobject jvmci_method, int bci))972HandleMark hm(THREAD);973974methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));975JVMCIObject element = JVMCIENV->new_StackTraceElement(method, bci, JVMCI_CHECK_NULL);976return JVMCIENV->get_jobject(element);977C2V_END978979C2V_VMENTRY_NULL(jobject, executeHotSpotNmethod, (JNIEnv* env, jobject, jobject args, jobject hs_nmethod))980// The incoming arguments array would have to contain JavaConstants instead of regular objects981// and the return value would have to be wrapped as a JavaConstant.982requireInHotSpot("executeHotSpotNmethod", JVMCI_CHECK_NULL);983984HandleMark hm(THREAD);985986JVMCIObject nmethod_mirror = JVMCIENV->wrap(hs_nmethod);987nmethodLocker locker;988nmethod* nm = JVMCIENV->get_nmethod(nmethod_mirror, locker);989if (nm == NULL || !nm->is_in_use()) {990JVMCI_THROW_NULL(InvalidInstalledCodeException);991}992methodHandle mh(THREAD, nm->method());993Symbol* signature = mh->signature();994JavaCallArguments jca(mh->size_of_parameters());995996JavaArgumentUnboxer jap(signature, &jca, (arrayOop) JNIHandles::resolve(args), mh->is_static());997JavaValue result(jap.return_type());998jca.set_alternative_target(Handle(THREAD, JNIHandles::resolve(nmethod_mirror.as_jobject())));999JavaCalls::call(&result, mh, &jca, CHECK_NULL);10001001if (jap.return_type() == T_VOID) {1002return NULL;1003} else if (is_reference_type(jap.return_type())) {1004return JNIHandles::make_local(THREAD, result.get_oop());1005} else {1006jvalue *value = (jvalue *) result.get_value_addr();1007// Narrow the value down if required (Important on big endian machines)1008switch (jap.return_type()) {1009case T_BOOLEAN:1010value->z = (jboolean) value->i;1011break;1012case T_BYTE:1013value->b = (jbyte) value->i;1014break;1015case T_CHAR:1016value->c = (jchar) value->i;1017break;1018case T_SHORT:1019value->s = (jshort) value->i;1020break;1021default:1022break;1023}1024JVMCIObject o = JVMCIENV->create_box(jap.return_type(), value, JVMCI_CHECK_NULL);1025return JVMCIENV->get_jobject(o);1026}1027C2V_END10281029C2V_VMENTRY_NULL(jlongArray, getLineNumberTable, (JNIEnv* env, jobject, jobject jvmci_method))1030Method* method = JVMCIENV->asMethod(jvmci_method);1031if (!method->has_linenumber_table()) {1032return NULL;1033}1034u2 num_entries = 0;1035CompressedLineNumberReadStream streamForSize(method->compressed_linenumber_table());1036while (streamForSize.read_pair()) {1037num_entries++;1038}10391040CompressedLineNumberReadStream stream(method->compressed_linenumber_table());1041JVMCIPrimitiveArray result = JVMCIENV->new_longArray(2 * num_entries, JVMCI_CHECK_NULL);10421043int i = 0;1044jlong value;1045while (stream.read_pair()) {1046value = ((long) stream.bci());1047JVMCIENV->put_long_at(result, i, value);1048value = ((long) stream.line());1049JVMCIENV->put_long_at(result, i + 1, value);1050i += 2;1051}10521053return (jlongArray) JVMCIENV->get_jobject(result);1054C2V_END10551056C2V_VMENTRY_0(jlong, getLocalVariableTableStart, (JNIEnv* env, jobject, jobject jvmci_method))1057Method* method = JVMCIENV->asMethod(jvmci_method);1058if (!method->has_localvariable_table()) {1059return 0;1060}1061return (jlong) (address) method->localvariable_table_start();1062C2V_END10631064C2V_VMENTRY_0(jint, getLocalVariableTableLength, (JNIEnv* env, jobject, jobject jvmci_method))1065Method* method = JVMCIENV->asMethod(jvmci_method);1066return method->localvariable_table_length();1067C2V_END10681069C2V_VMENTRY(void, reprofile, (JNIEnv* env, jobject, jobject jvmci_method))1070methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));1071MethodCounters* mcs = method->method_counters();1072if (mcs != NULL) {1073mcs->clear_counters();1074}1075NOT_PRODUCT(method->set_compiled_invocation_count(0));10761077CompiledMethod* code = method->code();1078if (code != NULL) {1079code->make_not_entrant();1080}10811082MethodData* method_data = method->method_data();1083if (method_data == NULL) {1084ClassLoaderData* loader_data = method->method_holder()->class_loader_data();1085method_data = MethodData::allocate(loader_data, method, CHECK);1086method->set_method_data(method_data);1087} else {1088method_data->initialize();1089}1090C2V_END109110921093C2V_VMENTRY(void, invalidateHotSpotNmethod, (JNIEnv* env, jobject, jobject hs_nmethod))1094JVMCIObject nmethod_mirror = JVMCIENV->wrap(hs_nmethod);1095JVMCIENV->invalidate_nmethod_mirror(nmethod_mirror, JVMCI_CHECK);1096C2V_END10971098C2V_VMENTRY_NULL(jlongArray, collectCounters, (JNIEnv* env, jobject))1099// Returns a zero length array if counters aren't enabled1100JVMCIPrimitiveArray array = JVMCIENV->new_longArray(JVMCICounterSize, JVMCI_CHECK_NULL);1101if (JVMCICounterSize > 0) {1102jlong* temp_array = NEW_RESOURCE_ARRAY(jlong, JVMCICounterSize);1103JavaThread::collect_counters(temp_array, JVMCICounterSize);1104JVMCIENV->copy_longs_from(temp_array, array, 0, JVMCICounterSize);1105}1106return (jlongArray) JVMCIENV->get_jobject(array);1107C2V_END11081109C2V_VMENTRY_0(jint, getCountersSize, (JNIEnv* env, jobject))1110return (jint) JVMCICounterSize;1111C2V_END11121113C2V_VMENTRY_0(jboolean, setCountersSize, (JNIEnv* env, jobject, jint new_size))1114return JavaThread::resize_all_jvmci_counters(new_size);1115C2V_END11161117C2V_VMENTRY_0(jint, allocateCompileId, (JNIEnv* env, jobject, jobject jvmci_method, int entry_bci))1118HandleMark hm(THREAD);1119if (jvmci_method == NULL) {1120JVMCI_THROW_0(NullPointerException);1121}1122methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));1123if (entry_bci >= method->code_size() || entry_bci < -1) {1124JVMCI_THROW_MSG_0(IllegalArgumentException, err_msg("Unexpected bci %d", entry_bci));1125}1126return CompileBroker::assign_compile_id_unlocked(THREAD, method, entry_bci);1127C2V_END112811291130C2V_VMENTRY_0(jboolean, isMature, (JNIEnv* env, jobject, jlong metaspace_method_data))1131MethodData* mdo = JVMCIENV->asMethodData(metaspace_method_data);1132return mdo != NULL && mdo->is_mature();1133C2V_END11341135C2V_VMENTRY_0(jboolean, hasCompiledCodeForOSR, (JNIEnv* env, jobject, jobject jvmci_method, int entry_bci, int comp_level))1136Method* method = JVMCIENV->asMethod(jvmci_method);1137return method->lookup_osr_nmethod_for(entry_bci, comp_level, true) != NULL;1138C2V_END11391140C2V_VMENTRY_NULL(jobject, getSymbol, (JNIEnv* env, jobject, jlong symbol))1141JVMCIObject sym = JVMCIENV->create_string((Symbol*)(address)symbol, JVMCI_CHECK_NULL);1142return JVMCIENV->get_jobject(sym);1143C2V_END11441145bool matches(jobjectArray methods, Method* method, JVMCIEnv* JVMCIENV) {1146objArrayOop methods_oop = (objArrayOop) JNIHandles::resolve(methods);11471148for (int i = 0; i < methods_oop->length(); i++) {1149oop resolved = methods_oop->obj_at(i);1150if ((resolved->klass() == HotSpotJVMCI::HotSpotResolvedJavaMethodImpl::klass()) && HotSpotJVMCI::asMethod(JVMCIENV, resolved) == method) {1151return true;1152}1153}1154return false;1155}11561157void call_interface(JavaValue* result, Klass* spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS) {1158CallInfo callinfo;1159Handle receiver = args->receiver();1160Klass* recvrKlass = receiver.is_null() ? (Klass*)NULL : receiver->klass();1161LinkInfo link_info(spec_klass, name, signature);1162LinkResolver::resolve_interface_call(1163callinfo, receiver, recvrKlass, link_info, true, CHECK);1164methodHandle method(THREAD, callinfo.selected_method());1165assert(method.not_null(), "should have thrown exception");11661167// Invoke the method1168JavaCalls::call(result, method, args, CHECK);1169}11701171C2V_VMENTRY_NULL(jobject, iterateFrames, (JNIEnv* env, jobject compilerToVM, jobjectArray initial_methods, jobjectArray match_methods, jint initialSkip, jobject visitor_handle))11721173if (!thread->has_last_Java_frame()) {1174return NULL;1175}1176Handle visitor(THREAD, JNIHandles::resolve_non_null(visitor_handle));11771178requireInHotSpot("iterateFrames", JVMCI_CHECK_NULL);11791180HotSpotJVMCI::HotSpotStackFrameReference::klass()->initialize(CHECK_NULL);1181Handle frame_reference = HotSpotJVMCI::HotSpotStackFrameReference::klass()->allocate_instance_handle(CHECK_NULL);11821183StackFrameStream fst(thread, true /* update */, true /* process_frames */);1184jobjectArray methods = initial_methods;11851186int frame_number = 0;1187vframe* vf = vframe::new_vframe(fst, thread);11881189while (true) {1190// look for the given method1191bool realloc_called = false;1192while (true) {1193StackValueCollection* locals = NULL;1194if (vf->is_compiled_frame()) {1195// compiled method frame1196compiledVFrame* cvf = compiledVFrame::cast(vf);1197if (methods == NULL || matches(methods, cvf->method(), JVMCIENV)) {1198if (initialSkip > 0) {1199initialSkip--;1200} else {1201ScopeDesc* scope = cvf->scope();1202// native wrappers do not have a scope1203if (scope != NULL && scope->objects() != NULL) {1204GrowableArray<ScopeValue*>* objects;1205if (!realloc_called) {1206objects = scope->objects();1207} else {1208// some object might already have been re-allocated, only reallocate the non-allocated ones1209objects = new GrowableArray<ScopeValue*>(scope->objects()->length());1210for (int i = 0; i < scope->objects()->length(); i++) {1211ObjectValue* sv = (ObjectValue*) scope->objects()->at(i);1212if (sv->value().is_null()) {1213objects->append(sv);1214}1215}1216}1217bool realloc_failures = Deoptimization::realloc_objects(thread, fst.current(), fst.register_map(), objects, CHECK_NULL);1218Deoptimization::reassign_fields(fst.current(), fst.register_map(), objects, realloc_failures, false);1219realloc_called = true;12201221GrowableArray<ScopeValue*>* local_values = scope->locals();1222assert(local_values != NULL, "NULL locals");1223typeArrayOop array_oop = oopFactory::new_boolArray(local_values->length(), CHECK_NULL);1224typeArrayHandle array(THREAD, array_oop);1225for (int i = 0; i < local_values->length(); i++) {1226ScopeValue* value = local_values->at(i);1227if (value->is_object()) {1228array->bool_at_put(i, true);1229}1230}1231HotSpotJVMCI::HotSpotStackFrameReference::set_localIsVirtual(JVMCIENV, frame_reference(), array());1232} else {1233HotSpotJVMCI::HotSpotStackFrameReference::set_localIsVirtual(JVMCIENV, frame_reference(), NULL);1234}12351236locals = cvf->locals();1237HotSpotJVMCI::HotSpotStackFrameReference::set_bci(JVMCIENV, frame_reference(), cvf->bci());1238methodHandle mh(THREAD, cvf->method());1239JVMCIObject method = JVMCIENV->get_jvmci_method(mh, JVMCI_CHECK_NULL);1240HotSpotJVMCI::HotSpotStackFrameReference::set_method(JVMCIENV, frame_reference(), JNIHandles::resolve(method.as_jobject()));1241}1242}1243} else if (vf->is_interpreted_frame()) {1244// interpreted method frame1245interpretedVFrame* ivf = interpretedVFrame::cast(vf);1246if (methods == NULL || matches(methods, ivf->method(), JVMCIENV)) {1247if (initialSkip > 0) {1248initialSkip--;1249} else {1250locals = ivf->locals();1251HotSpotJVMCI::HotSpotStackFrameReference::set_bci(JVMCIENV, frame_reference(), ivf->bci());1252methodHandle mh(THREAD, ivf->method());1253JVMCIObject method = JVMCIENV->get_jvmci_method(mh, JVMCI_CHECK_NULL);1254HotSpotJVMCI::HotSpotStackFrameReference::set_method(JVMCIENV, frame_reference(), JNIHandles::resolve(method.as_jobject()));1255HotSpotJVMCI::HotSpotStackFrameReference::set_localIsVirtual(JVMCIENV, frame_reference(), NULL);1256}1257}1258}12591260// locals != NULL means that we found a matching frame and result is already partially initialized1261if (locals != NULL) {1262methods = match_methods;1263HotSpotJVMCI::HotSpotStackFrameReference::set_compilerToVM(JVMCIENV, frame_reference(), JNIHandles::resolve(compilerToVM));1264HotSpotJVMCI::HotSpotStackFrameReference::set_stackPointer(JVMCIENV, frame_reference(), (jlong) fst.current()->sp());1265HotSpotJVMCI::HotSpotStackFrameReference::set_frameNumber(JVMCIENV, frame_reference(), frame_number);12661267// initialize the locals array1268objArrayOop array_oop = oopFactory::new_objectArray(locals->size(), CHECK_NULL);1269objArrayHandle array(THREAD, array_oop);1270for (int i = 0; i < locals->size(); i++) {1271StackValue* var = locals->at(i);1272if (var->type() == T_OBJECT) {1273array->obj_at_put(i, locals->at(i)->get_obj()());1274}1275}1276HotSpotJVMCI::HotSpotStackFrameReference::set_locals(JVMCIENV, frame_reference(), array());1277HotSpotJVMCI::HotSpotStackFrameReference::set_objectsMaterialized(JVMCIENV, frame_reference(), JNI_FALSE);12781279JavaValue result(T_OBJECT);1280JavaCallArguments args(visitor);1281args.push_oop(frame_reference);1282call_interface(&result, HotSpotJVMCI::InspectedFrameVisitor::klass(), vmSymbols::visitFrame_name(), vmSymbols::visitFrame_signature(), &args, CHECK_NULL);1283if (result.get_oop() != NULL) {1284return JNIHandles::make_local(thread, result.get_oop());1285}1286assert(initialSkip == 0, "There should be no match before initialSkip == 0");1287if (HotSpotJVMCI::HotSpotStackFrameReference::objectsMaterialized(JVMCIENV, frame_reference()) == JNI_TRUE) {1288// the frame has been deoptimized, we need to re-synchronize the frame and vframe1289intptr_t* stack_pointer = (intptr_t*) HotSpotJVMCI::HotSpotStackFrameReference::stackPointer(JVMCIENV, frame_reference());1290fst = StackFrameStream(thread, true /* update */, true /* process_frames */);1291while (fst.current()->sp() != stack_pointer && !fst.is_done()) {1292fst.next();1293}1294if (fst.current()->sp() != stack_pointer) {1295THROW_MSG_NULL(vmSymbols::java_lang_IllegalStateException(), "stack frame not found after deopt")1296}1297vf = vframe::new_vframe(fst, thread);1298if (!vf->is_compiled_frame()) {1299THROW_MSG_NULL(vmSymbols::java_lang_IllegalStateException(), "compiled stack frame expected")1300}1301for (int i = 0; i < frame_number; i++) {1302if (vf->is_top()) {1303THROW_MSG_NULL(vmSymbols::java_lang_IllegalStateException(), "vframe not found after deopt")1304}1305vf = vf->sender();1306assert(vf->is_compiled_frame(), "Wrong frame type");1307}1308}1309frame_reference = HotSpotJVMCI::HotSpotStackFrameReference::klass()->allocate_instance_handle(CHECK_NULL);1310HotSpotJVMCI::HotSpotStackFrameReference::klass()->initialize(CHECK_NULL);1311}13121313if (vf->is_top()) {1314break;1315}1316frame_number++;1317vf = vf->sender();1318} // end of vframe loop13191320if (fst.is_done()) {1321break;1322}1323fst.next();1324vf = vframe::new_vframe(fst, thread);1325frame_number = 0;1326} // end of frame loop13271328// the end was reached without finding a matching method1329return NULL;1330C2V_END13311332C2V_VMENTRY(void, resolveInvokeDynamicInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))1333constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));1334CallInfo callInfo;1335LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, Bytecodes::_invokedynamic, CHECK);1336ConstantPoolCacheEntry* cp_cache_entry = cp->invokedynamic_cp_cache_entry_at(index);1337cp_cache_entry->set_dynamic_call(cp, callInfo);1338C2V_END13391340C2V_VMENTRY(void, resolveInvokeHandleInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))1341constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));1342Klass* holder = cp->klass_ref_at(index, CHECK);1343Symbol* name = cp->name_ref_at(index);1344if (MethodHandles::is_signature_polymorphic_name(holder, name)) {1345CallInfo callInfo;1346LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, Bytecodes::_invokehandle, CHECK);1347ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index));1348cp_cache_entry->set_method_handle(cp, callInfo);1349}1350C2V_END13511352C2V_VMENTRY_0(jint, isResolvedInvokeHandleInPool, (JNIEnv* env, jobject, jobject jvmci_constant_pool, jint index))1353constantPoolHandle cp(THREAD, JVMCIENV->asConstantPool(jvmci_constant_pool));1354ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index));1355if (cp_cache_entry->is_resolved(Bytecodes::_invokehandle)) {1356// MethodHandle.invoke* --> LambdaForm?1357ResourceMark rm;13581359LinkInfo link_info(cp, index, CATCH);13601361Klass* resolved_klass = link_info.resolved_klass();13621363Symbol* name_sym = cp->name_ref_at(index);13641365vmassert(MethodHandles::is_method_handle_invoke_name(resolved_klass, name_sym), "!");1366vmassert(MethodHandles::is_signature_polymorphic_name(resolved_klass, name_sym), "!");13671368methodHandle adapter_method(THREAD, cp_cache_entry->f1_as_method());13691370methodHandle resolved_method(adapter_method);13711372// Can we treat it as a regular invokevirtual?1373if (resolved_method->method_holder() == resolved_klass && resolved_method->name() == name_sym) {1374vmassert(!resolved_method->is_static(),"!");1375vmassert(MethodHandles::is_signature_polymorphic_method(resolved_method()),"!");1376vmassert(!MethodHandles::is_signature_polymorphic_static(resolved_method->intrinsic_id()), "!");1377vmassert(cp_cache_entry->appendix_if_resolved(cp) == NULL, "!");13781379methodHandle m(THREAD, LinkResolver::linktime_resolve_virtual_method_or_null(link_info));1380vmassert(m == resolved_method, "!!");1381return -1;1382}13831384return Bytecodes::_invokevirtual;1385}1386if (cp_cache_entry->is_resolved(Bytecodes::_invokedynamic)) {1387return Bytecodes::_invokedynamic;1388}1389return -1;1390C2V_END139113921393C2V_VMENTRY_NULL(jobject, getSignaturePolymorphicHolders, (JNIEnv* env, jobject))1394JVMCIObjectArray holders = JVMCIENV->new_String_array(2, JVMCI_CHECK_NULL);1395JVMCIObject mh = JVMCIENV->create_string("Ljava/lang/invoke/MethodHandle;", JVMCI_CHECK_NULL);1396JVMCIObject vh = JVMCIENV->create_string("Ljava/lang/invoke/VarHandle;", JVMCI_CHECK_NULL);1397JVMCIENV->put_object_at(holders, 0, mh);1398JVMCIENV->put_object_at(holders, 1, vh);1399return JVMCIENV->get_jobject(holders);1400C2V_END14011402C2V_VMENTRY_0(jboolean, shouldDebugNonSafepoints, (JNIEnv* env, jobject))1403//see compute_recording_non_safepoints in debugInfroRec.cpp1404if (JvmtiExport::should_post_compiled_method_load() && FLAG_IS_DEFAULT(DebugNonSafepoints)) {1405return true;1406}1407return DebugNonSafepoints;1408C2V_END14091410// public native void materializeVirtualObjects(HotSpotStackFrameReference stackFrame, boolean invalidate);1411C2V_VMENTRY(void, materializeVirtualObjects, (JNIEnv* env, jobject, jobject _hs_frame, bool invalidate))1412JVMCIObject hs_frame = JVMCIENV->wrap(_hs_frame);1413if (hs_frame.is_null()) {1414JVMCI_THROW_MSG(NullPointerException, "stack frame is null");1415}14161417requireInHotSpot("materializeVirtualObjects", JVMCI_CHECK);14181419JVMCIENV->HotSpotStackFrameReference_initialize(JVMCI_CHECK);14201421// look for the given stack frame1422StackFrameStream fst(thread, false /* update */, true /* process_frames */);1423intptr_t* stack_pointer = (intptr_t*) JVMCIENV->get_HotSpotStackFrameReference_stackPointer(hs_frame);1424while (fst.current()->sp() != stack_pointer && !fst.is_done()) {1425fst.next();1426}1427if (fst.current()->sp() != stack_pointer) {1428JVMCI_THROW_MSG(IllegalStateException, "stack frame not found");1429}14301431if (invalidate) {1432if (!fst.current()->is_compiled_frame()) {1433JVMCI_THROW_MSG(IllegalStateException, "compiled stack frame expected");1434}1435assert(fst.current()->cb()->is_nmethod(), "nmethod expected");1436((nmethod*) fst.current()->cb())->make_not_entrant();1437}1438Deoptimization::deoptimize(thread, *fst.current(), Deoptimization::Reason_none);1439// look for the frame again as it has been updated by deopt (pc, deopt state...)1440StackFrameStream fstAfterDeopt(thread, true /* update */, true /* process_frames */);1441while (fstAfterDeopt.current()->sp() != stack_pointer && !fstAfterDeopt.is_done()) {1442fstAfterDeopt.next();1443}1444if (fstAfterDeopt.current()->sp() != stack_pointer) {1445JVMCI_THROW_MSG(IllegalStateException, "stack frame not found after deopt");1446}14471448vframe* vf = vframe::new_vframe(fstAfterDeopt.current(), fstAfterDeopt.register_map(), thread);1449if (!vf->is_compiled_frame()) {1450JVMCI_THROW_MSG(IllegalStateException, "compiled stack frame expected");1451}14521453GrowableArray<compiledVFrame*>* virtualFrames = new GrowableArray<compiledVFrame*>(10);1454while (true) {1455assert(vf->is_compiled_frame(), "Wrong frame type");1456virtualFrames->push(compiledVFrame::cast(vf));1457if (vf->is_top()) {1458break;1459}1460vf = vf->sender();1461}14621463int last_frame_number = JVMCIENV->get_HotSpotStackFrameReference_frameNumber(hs_frame);1464if (last_frame_number >= virtualFrames->length()) {1465JVMCI_THROW_MSG(IllegalStateException, "invalid frame number");1466}14671468// Reallocate the non-escaping objects and restore their fields.1469assert (virtualFrames->at(last_frame_number)->scope() != NULL,"invalid scope");1470GrowableArray<ScopeValue*>* objects = virtualFrames->at(last_frame_number)->scope()->objects();14711472if (objects == NULL) {1473// no objects to materialize1474return;1475}14761477bool realloc_failures = Deoptimization::realloc_objects(thread, fstAfterDeopt.current(), fstAfterDeopt.register_map(), objects, CHECK);1478Deoptimization::reassign_fields(fstAfterDeopt.current(), fstAfterDeopt.register_map(), objects, realloc_failures, false);14791480for (int frame_index = 0; frame_index < virtualFrames->length(); frame_index++) {1481compiledVFrame* cvf = virtualFrames->at(frame_index);14821483GrowableArray<ScopeValue*>* scopeLocals = cvf->scope()->locals();1484StackValueCollection* locals = cvf->locals();1485if (locals != NULL) {1486for (int i2 = 0; i2 < locals->size(); i2++) {1487StackValue* var = locals->at(i2);1488if (var->type() == T_OBJECT && scopeLocals->at(i2)->is_object()) {1489jvalue val;1490val.l = cast_from_oop<jobject>(locals->at(i2)->get_obj()());1491cvf->update_local(T_OBJECT, i2, val);1492}1493}1494}14951496GrowableArray<ScopeValue*>* scopeExpressions = cvf->scope()->expressions();1497StackValueCollection* expressions = cvf->expressions();1498if (expressions != NULL) {1499for (int i2 = 0; i2 < expressions->size(); i2++) {1500StackValue* var = expressions->at(i2);1501if (var->type() == T_OBJECT && scopeExpressions->at(i2)->is_object()) {1502jvalue val;1503val.l = cast_from_oop<jobject>(expressions->at(i2)->get_obj()());1504cvf->update_stack(T_OBJECT, i2, val);1505}1506}1507}15081509GrowableArray<MonitorValue*>* scopeMonitors = cvf->scope()->monitors();1510GrowableArray<MonitorInfo*>* monitors = cvf->monitors();1511if (monitors != NULL) {1512for (int i2 = 0; i2 < monitors->length(); i2++) {1513cvf->update_monitor(i2, monitors->at(i2));1514}1515}1516}15171518// all locals are materialized by now1519JVMCIENV->set_HotSpotStackFrameReference_localIsVirtual(hs_frame, NULL);1520// update the locals array1521JVMCIObjectArray array = JVMCIENV->get_HotSpotStackFrameReference_locals(hs_frame);1522StackValueCollection* locals = virtualFrames->at(last_frame_number)->locals();1523for (int i = 0; i < locals->size(); i++) {1524StackValue* var = locals->at(i);1525if (var->type() == T_OBJECT) {1526JVMCIENV->put_object_at(array, i, HotSpotJVMCI::wrap(locals->at(i)->get_obj()()));1527}1528}1529HotSpotJVMCI::HotSpotStackFrameReference::set_objectsMaterialized(JVMCIENV, hs_frame, JNI_TRUE);1530C2V_END15311532// Use of tty does not require the current thread to be attached to the VM1533// so no need for a full C2V_VMENTRY transition.1534C2V_VMENTRY_PREFIX(void, writeDebugOutput, (JNIEnv* env, jobject, jlong buffer, jint length, bool flush))1535if (length <= 8) {1536tty->write((char*) &buffer, length);1537} else {1538tty->write((char*) buffer, length);1539}1540if (flush) {1541tty->flush();1542}1543C2V_END15441545// Use of tty does not require the current thread to be attached to the VM1546// so no need for a full C2V_VMENTRY transition.1547C2V_VMENTRY_PREFIX(void, flushDebugOutput, (JNIEnv* env, jobject))1548tty->flush();1549C2V_END15501551C2V_VMENTRY_0(jint, methodDataProfileDataSize, (JNIEnv* env, jobject, jlong metaspace_method_data, jint position))1552MethodData* mdo = JVMCIENV->asMethodData(metaspace_method_data);1553ProfileData* profile_data = mdo->data_at(position);1554if (mdo->is_valid(profile_data)) {1555return profile_data->size_in_bytes();1556}1557DataLayout* data = mdo->extra_data_base();1558DataLayout* end = mdo->extra_data_limit();1559for (;; data = mdo->next_extra(data)) {1560assert(data < end, "moved past end of extra data");1561profile_data = data->data_in();1562if (mdo->dp_to_di(profile_data->dp()) == position) {1563return profile_data->size_in_bytes();1564}1565}1566JVMCI_THROW_MSG_0(IllegalArgumentException, err_msg("Invalid profile data position %d", position));1567C2V_END15681569C2V_VMENTRY_0(jlong, getFingerprint, (JNIEnv* env, jobject, jlong metaspace_klass))1570JVMCI_THROW_MSG_0(InternalError, "unimplemented");1571C2V_END15721573C2V_VMENTRY_NULL(jobject, getInterfaces, (JNIEnv* env, jobject, jobject jvmci_type))1574if (jvmci_type == NULL) {1575JVMCI_THROW_0(NullPointerException);1576}15771578Klass* klass = JVMCIENV->asKlass(jvmci_type);1579if (klass == NULL) {1580JVMCI_THROW_0(NullPointerException);1581}1582if (!klass->is_instance_klass()) {1583JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s must be instance klass", klass->external_name()));1584}1585InstanceKlass* iklass = InstanceKlass::cast(klass);15861587// Regular instance klass, fill in all local interfaces1588int size = iklass->local_interfaces()->length();1589JVMCIObjectArray interfaces = JVMCIENV->new_HotSpotResolvedObjectTypeImpl_array(size, JVMCI_CHECK_NULL);1590for (int index = 0; index < size; index++) {1591JVMCIKlassHandle klass(THREAD);1592Klass* k = iklass->local_interfaces()->at(index);1593klass = k;1594JVMCIObject type = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL);1595JVMCIENV->put_object_at(interfaces, index, type);1596}1597return JVMCIENV->get_jobject(interfaces);1598C2V_END15991600C2V_VMENTRY_NULL(jobject, getComponentType, (JNIEnv* env, jobject, jobject jvmci_type))1601if (jvmci_type == NULL) {1602JVMCI_THROW_0(NullPointerException);1603}16041605Klass* klass = JVMCIENV->asKlass(jvmci_type);1606oop mirror = klass->java_mirror();1607if (java_lang_Class::is_primitive(mirror) ||1608!java_lang_Class::as_Klass(mirror)->is_array_klass()) {1609return NULL;1610}16111612oop component_mirror = java_lang_Class::component_mirror(mirror);1613if (component_mirror == NULL) {1614return NULL;1615}1616Klass* component_klass = java_lang_Class::as_Klass(component_mirror);1617if (component_klass != NULL) {1618JVMCIKlassHandle klass_handle(THREAD);1619klass_handle = component_klass;1620JVMCIObject result = JVMCIENV->get_jvmci_type(klass_handle, JVMCI_CHECK_NULL);1621return JVMCIENV->get_jobject(result);1622}1623BasicType type = java_lang_Class::primitive_type(component_mirror);1624JVMCIObject result = JVMCIENV->get_jvmci_primitive_type(type);1625return JVMCIENV->get_jobject(result);1626C2V_END16271628C2V_VMENTRY(void, ensureInitialized, (JNIEnv* env, jobject, jobject jvmci_type))1629if (jvmci_type == NULL) {1630JVMCI_THROW(NullPointerException);1631}16321633Klass* klass = JVMCIENV->asKlass(jvmci_type);1634if (klass != NULL && klass->should_be_initialized()) {1635InstanceKlass* k = InstanceKlass::cast(klass);1636k->initialize(CHECK);1637}1638C2V_END16391640C2V_VMENTRY(void, ensureLinked, (JNIEnv* env, jobject, jobject jvmci_type))1641if (jvmci_type == NULL) {1642JVMCI_THROW(NullPointerException);1643}16441645Klass* klass = JVMCIENV->asKlass(jvmci_type);1646if (klass != NULL && klass->is_instance_klass()) {1647InstanceKlass* k = InstanceKlass::cast(klass);1648k->link_class(CHECK);1649}1650C2V_END16511652C2V_VMENTRY_0(jint, interpreterFrameSize, (JNIEnv* env, jobject, jobject bytecode_frame_handle))1653if (bytecode_frame_handle == NULL) {1654JVMCI_THROW_0(NullPointerException);1655}16561657JVMCIObject top_bytecode_frame = JVMCIENV->wrap(bytecode_frame_handle);1658JVMCIObject bytecode_frame = top_bytecode_frame;1659int size = 0;1660int callee_parameters = 0;1661int callee_locals = 0;1662Method* method = JVMCIENV->asMethod(JVMCIENV->get_BytecodePosition_method(bytecode_frame));1663int extra_args = method->max_stack() - JVMCIENV->get_BytecodeFrame_numStack(bytecode_frame);16641665while (bytecode_frame.is_non_null()) {1666int locks = JVMCIENV->get_BytecodeFrame_numLocks(bytecode_frame);1667int temps = JVMCIENV->get_BytecodeFrame_numStack(bytecode_frame);1668bool is_top_frame = (JVMCIENV->equals(bytecode_frame, top_bytecode_frame));1669Method* method = JVMCIENV->asMethod(JVMCIENV->get_BytecodePosition_method(bytecode_frame));16701671int frame_size = BytesPerWord * Interpreter::size_activation(method->max_stack(),1672temps + callee_parameters,1673extra_args,1674locks,1675callee_parameters,1676callee_locals,1677is_top_frame);1678size += frame_size;16791680callee_parameters = method->size_of_parameters();1681callee_locals = method->max_locals();1682extra_args = 0;1683bytecode_frame = JVMCIENV->get_BytecodePosition_caller(bytecode_frame);1684}1685return size + Deoptimization::last_frame_adjust(0, callee_locals) * BytesPerWord;1686C2V_END16871688C2V_VMENTRY(void, compileToBytecode, (JNIEnv* env, jobject, jobject lambda_form_handle))1689Handle lambda_form = JVMCIENV->asConstant(JVMCIENV->wrap(lambda_form_handle), JVMCI_CHECK);1690if (lambda_form->is_a(vmClasses::LambdaForm_klass())) {1691TempNewSymbol compileToBytecode = SymbolTable::new_symbol("compileToBytecode");1692JavaValue result(T_VOID);1693JavaCalls::call_special(&result, lambda_form, vmClasses::LambdaForm_klass(), compileToBytecode, vmSymbols::void_method_signature(), CHECK);1694} else {1695JVMCI_THROW_MSG(IllegalArgumentException,1696err_msg("Unexpected type: %s", lambda_form->klass()->external_name()))1697}1698C2V_END16991700C2V_VMENTRY_0(jint, getIdentityHashCode, (JNIEnv* env, jobject, jobject object))1701Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_0);1702return obj->identity_hash();1703C2V_END17041705C2V_VMENTRY_0(jboolean, isInternedString, (JNIEnv* env, jobject, jobject object))1706Handle str = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_0);1707if (!java_lang_String::is_instance(str())) {1708return false;1709}1710int len;1711jchar* name = java_lang_String::as_unicode_string(str(), len, CHECK_false);1712return (StringTable::lookup(name, len) != NULL);1713C2V_END171417151716C2V_VMENTRY_NULL(jobject, unboxPrimitive, (JNIEnv* env, jobject, jobject object))1717if (object == NULL) {1718JVMCI_THROW_0(NullPointerException);1719}1720Handle box = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_NULL);1721BasicType type = java_lang_boxing_object::basic_type(box());1722jvalue result;1723if (java_lang_boxing_object::get_value(box(), &result) == T_ILLEGAL) {1724return NULL;1725}1726JVMCIObject boxResult = JVMCIENV->create_box(type, &result, JVMCI_CHECK_NULL);1727return JVMCIENV->get_jobject(boxResult);1728C2V_END17291730C2V_VMENTRY_NULL(jobject, boxPrimitive, (JNIEnv* env, jobject, jobject object))1731if (object == NULL) {1732JVMCI_THROW_0(NullPointerException);1733}1734JVMCIObject box = JVMCIENV->wrap(object);1735BasicType type = JVMCIENV->get_box_type(box);1736if (type == T_ILLEGAL) {1737return NULL;1738}1739jvalue value = JVMCIENV->get_boxed_value(type, box);1740JavaValue box_result(T_OBJECT);1741JavaCallArguments jargs;1742Klass* box_klass = NULL;1743Symbol* box_signature = NULL;1744#define BOX_CASE(bt, v, argtype, name) \1745case bt: \1746jargs.push_##argtype(value.v); \1747box_klass = vmClasses::name##_klass(); \1748box_signature = vmSymbols::name##_valueOf_signature(); \1749break17501751switch (type) {1752BOX_CASE(T_BOOLEAN, z, int, Boolean);1753BOX_CASE(T_BYTE, b, int, Byte);1754BOX_CASE(T_CHAR, c, int, Character);1755BOX_CASE(T_SHORT, s, int, Short);1756BOX_CASE(T_INT, i, int, Integer);1757BOX_CASE(T_LONG, j, long, Long);1758BOX_CASE(T_FLOAT, f, float, Float);1759BOX_CASE(T_DOUBLE, d, double, Double);1760default:1761ShouldNotReachHere();1762}1763#undef BOX_CASE17641765JavaCalls::call_static(&box_result,1766box_klass,1767vmSymbols::valueOf_name(),1768box_signature, &jargs, CHECK_NULL);1769oop hotspot_box = box_result.get_oop();1770JVMCIObject result = JVMCIENV->get_object_constant(hotspot_box, false);1771return JVMCIENV->get_jobject(result);1772C2V_END17731774C2V_VMENTRY_NULL(jobjectArray, getDeclaredConstructors, (JNIEnv* env, jobject, jobject holder))1775if (holder == NULL) {1776JVMCI_THROW_0(NullPointerException);1777}1778Klass* klass = JVMCIENV->asKlass(holder);1779if (!klass->is_instance_klass()) {1780JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(0, JVMCI_CHECK_NULL);1781return JVMCIENV->get_jobjectArray(methods);1782}17831784InstanceKlass* iklass = InstanceKlass::cast(klass);1785// Ensure class is linked1786iklass->link_class(CHECK_NULL);17871788GrowableArray<Method*> constructors_array;1789for (int i = 0; i < iklass->methods()->length(); i++) {1790Method* m = iklass->methods()->at(i);1791if (m->is_initializer() && !m->is_static()) {1792constructors_array.append(m);1793}1794}1795JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(constructors_array.length(), JVMCI_CHECK_NULL);1796for (int i = 0; i < constructors_array.length(); i++) {1797methodHandle ctor(THREAD, constructors_array.at(i));1798JVMCIObject method = JVMCIENV->get_jvmci_method(ctor, JVMCI_CHECK_NULL);1799JVMCIENV->put_object_at(methods, i, method);1800}1801return JVMCIENV->get_jobjectArray(methods);1802C2V_END18031804C2V_VMENTRY_NULL(jobjectArray, getDeclaredMethods, (JNIEnv* env, jobject, jobject holder))1805if (holder == NULL) {1806JVMCI_THROW_0(NullPointerException);1807}1808Klass* klass = JVMCIENV->asKlass(holder);1809if (!klass->is_instance_klass()) {1810JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(0, JVMCI_CHECK_NULL);1811return JVMCIENV->get_jobjectArray(methods);1812}18131814InstanceKlass* iklass = InstanceKlass::cast(klass);1815// Ensure class is linked1816iklass->link_class(CHECK_NULL);18171818GrowableArray<Method*> methods_array;1819for (int i = 0; i < iklass->methods()->length(); i++) {1820Method* m = iklass->methods()->at(i);1821if (!m->is_initializer() && !m->is_overpass()) {1822methods_array.append(m);1823}1824}1825JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(methods_array.length(), JVMCI_CHECK_NULL);1826for (int i = 0; i < methods_array.length(); i++) {1827methodHandle mh(THREAD, methods_array.at(i));1828JVMCIObject method = JVMCIENV->get_jvmci_method(mh, JVMCI_CHECK_NULL);1829JVMCIENV->put_object_at(methods, i, method);1830}1831return JVMCIENV->get_jobjectArray(methods);1832C2V_END18331834C2V_VMENTRY_NULL(jobject, readFieldValue, (JNIEnv* env, jobject, jobject object, jobject expected_type, long displacement, jobject kind_object))1835if (object == NULL || kind_object == NULL) {1836JVMCI_THROW_0(NullPointerException);1837}18381839JVMCIObject kind = JVMCIENV->wrap(kind_object);1840BasicType basic_type = JVMCIENV->kindToBasicType(kind, JVMCI_CHECK_NULL);18411842InstanceKlass* holder = NULL;1843if (expected_type != NULL) {1844holder = InstanceKlass::cast(JVMCIENV->asKlass(JVMCIENV->wrap(expected_type)));1845}18461847bool is_static = false;1848Handle obj;1849JVMCIObject base = JVMCIENV->wrap(object);1850if (JVMCIENV->isa_HotSpotObjectConstantImpl(base)) {1851obj = JVMCIENV->asConstant(base, JVMCI_CHECK_NULL);1852// asConstant will throw an NPE if a constant contains NULL18531854if (holder != NULL && !obj->is_a(holder)) {1855// Not a subtype of field holder1856return NULL;1857}1858is_static = false;1859if (holder == NULL && java_lang_Class::is_instance(obj()) && displacement >= InstanceMirrorKlass::offset_of_static_fields()) {1860is_static = true;1861}1862} else if (JVMCIENV->isa_HotSpotResolvedObjectTypeImpl(base)) {1863is_static = true;1864Klass* klass = JVMCIENV->asKlass(base);1865if (holder != NULL && holder != klass) {1866return NULL;1867}1868obj = Handle(THREAD, klass->java_mirror());1869} else {1870// The Java code is expected to guard against this path1871ShouldNotReachHere();1872}18731874int basic_type_elemsize = type2aelembytes(basic_type);1875if (displacement < 0 || ((long) displacement + basic_type_elemsize > HeapWordSize * obj->size())) {1876// Reading outside of the object bounds1877JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading outside object bounds");1878}18791880// Perform basic sanity checks on the read. Primitive reads are permitted to read outside the1881// bounds of their fields but object reads must map exactly onto the underlying oop slot.1882bool aligned = (displacement % basic_type_elemsize) == 0;1883if (!aligned) {1884JVMCI_THROW_MSG_NULL(IllegalArgumentException, "read is unaligned");1885}1886if (basic_type == T_OBJECT) {1887if (obj->is_objArray()) {1888if (displacement < arrayOopDesc::base_offset_in_bytes(T_OBJECT)) {1889JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading from array header");1890}1891if (displacement + heapOopSize > arrayOopDesc::base_offset_in_bytes(T_OBJECT) + arrayOop(obj())->length() * heapOopSize) {1892JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading after last array element");1893}1894if (((displacement - arrayOopDesc::base_offset_in_bytes(T_OBJECT)) % heapOopSize) != 0) {1895JVMCI_THROW_MSG_NULL(IllegalArgumentException, "misaligned object read from array");1896}1897} else if (obj->is_instance()) {1898InstanceKlass* klass = InstanceKlass::cast(is_static ? java_lang_Class::as_Klass(obj()) : obj->klass());1899fieldDescriptor fd;1900if (!klass->find_field_from_offset(displacement, is_static, &fd)) {1901JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Can't find field at displacement %d in object of type %s", (int) displacement, klass->external_name()));1902}1903if (fd.field_type() != T_OBJECT && fd.field_type() != T_ARRAY) {1904JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Field at displacement %d in object of type %s is %s but expected %s", (int) displacement,1905klass->external_name(), type2name(fd.field_type()), type2name(basic_type)));1906}1907} else if (obj->is_typeArray()) {1908JVMCI_THROW_MSG_NULL(IllegalArgumentException, "Can't read objects from primitive array");1909} else {1910ShouldNotReachHere();1911}1912} else {1913if (obj->is_objArray()) {1914JVMCI_THROW_MSG_NULL(IllegalArgumentException, "Reading primitive from object array");1915} else if (obj->is_typeArray()) {1916if (displacement < arrayOopDesc::base_offset_in_bytes(ArrayKlass::cast(obj->klass())->element_type())) {1917JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading from array header");1918}1919}1920}19211922jlong value = 0;19231924// Treat all reads as volatile for simplicity as this function can be used1925// both for reading Java fields declared as volatile as well as for constant1926// folding Unsafe.get* methods with volatile semantics.19271928switch (basic_type) {1929case T_BOOLEAN: value = obj->bool_field_acquire(displacement); break;1930case T_BYTE: value = obj->byte_field_acquire(displacement); break;1931case T_SHORT: value = obj->short_field_acquire(displacement); break;1932case T_CHAR: value = obj->char_field_acquire(displacement); break;1933case T_FLOAT:1934case T_INT: value = obj->int_field_acquire(displacement); break;1935case T_DOUBLE:1936case T_LONG: value = obj->long_field_acquire(displacement); break;19371938case T_OBJECT: {1939if (displacement == java_lang_Class::component_mirror_offset() && java_lang_Class::is_instance(obj()) &&1940(java_lang_Class::as_Klass(obj()) == NULL || !java_lang_Class::as_Klass(obj())->is_array_klass())) {1941// Class.componentType for non-array classes can transiently contain an int[] that's1942// used for locking so always return null to mimic Class.getComponentType()1943return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_NULL_POINTER());1944}19451946oop value = obj->obj_field_acquire(displacement);19471948if (value == NULL) {1949return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_NULL_POINTER());1950} else {1951if (value != NULL && !oopDesc::is_oop(value)) {1952// Throw an exception to improve debuggability. This check isn't totally reliable because1953// is_oop doesn't try to be completety safe but for most invalid values it provides a good1954// enough answer. It possible to crash in the is_oop call but that just means the crash happens1955// closer to where things went wrong.1956JVMCI_THROW_MSG_NULL(InternalError, err_msg("Read bad oop " INTPTR_FORMAT " at offset " JLONG_FORMAT " in object " INTPTR_FORMAT " of type %s",1957p2i(value), displacement, p2i(obj()), obj->klass()->external_name()));1958}19591960JVMCIObject result = JVMCIENV->get_object_constant(value);1961return JVMCIENV->get_jobject(result);1962}1963}19641965default:1966ShouldNotReachHere();1967}1968JVMCIObject result = JVMCIENV->call_JavaConstant_forPrimitive(kind, value, JVMCI_CHECK_NULL);1969return JVMCIENV->get_jobject(result);1970C2V_END19711972C2V_VMENTRY_0(jboolean, isInstance, (JNIEnv* env, jobject, jobject holder, jobject object))1973if (object == NULL || holder == NULL) {1974JVMCI_THROW_0(NullPointerException);1975}1976Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_0);1977Klass* klass = JVMCIENV->asKlass(JVMCIENV->wrap(holder));1978return obj->is_a(klass);1979C2V_END19801981C2V_VMENTRY_0(jboolean, isAssignableFrom, (JNIEnv* env, jobject, jobject holder, jobject otherHolder))1982if (holder == NULL || otherHolder == NULL) {1983JVMCI_THROW_0(NullPointerException);1984}1985Klass* klass = JVMCIENV->asKlass(JVMCIENV->wrap(holder));1986Klass* otherKlass = JVMCIENV->asKlass(JVMCIENV->wrap(otherHolder));1987return otherKlass->is_subtype_of(klass);1988C2V_END19891990C2V_VMENTRY_0(jboolean, isTrustedForIntrinsics, (JNIEnv* env, jobject, jobject holder))1991if (holder == NULL) {1992JVMCI_THROW_0(NullPointerException);1993}1994InstanceKlass* ik = InstanceKlass::cast(JVMCIENV->asKlass(JVMCIENV->wrap(holder)));1995if (ik->class_loader_data()->is_boot_class_loader_data() || ik->class_loader_data()->is_platform_class_loader_data()) {1996return true;1997}1998return false;1999C2V_END20002001C2V_VMENTRY_NULL(jobject, asJavaType, (JNIEnv* env, jobject, jobject object))2002if (object == NULL) {2003JVMCI_THROW_0(NullPointerException);2004}2005Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_NULL);2006if (java_lang_Class::is_instance(obj())) {2007if (java_lang_Class::is_primitive(obj())) {2008JVMCIObject type = JVMCIENV->get_jvmci_primitive_type(java_lang_Class::primitive_type(obj()));2009return JVMCIENV->get_jobject(type);2010}2011Klass* klass = java_lang_Class::as_Klass(obj());2012JVMCIKlassHandle klass_handle(THREAD);2013klass_handle = klass;2014JVMCIObject type = JVMCIENV->get_jvmci_type(klass_handle, JVMCI_CHECK_NULL);2015return JVMCIENV->get_jobject(type);2016}2017return NULL;2018C2V_END201920202021C2V_VMENTRY_NULL(jobject, asString, (JNIEnv* env, jobject, jobject object))2022if (object == NULL) {2023JVMCI_THROW_0(NullPointerException);2024}2025Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_NULL);2026const char* str = java_lang_String::as_utf8_string(obj());2027JVMCIObject result = JVMCIENV->create_string(str, JVMCI_CHECK_NULL);2028return JVMCIENV->get_jobject(result);2029C2V_END203020312032C2V_VMENTRY_0(jboolean, equals, (JNIEnv* env, jobject, jobject x, jlong xHandle, jobject y, jlong yHandle))2033if (x == NULL || y == NULL) {2034JVMCI_THROW_0(NullPointerException);2035}2036return JVMCIENV->resolve_handle(xHandle) == JVMCIENV->resolve_handle(yHandle);2037C2V_END20382039C2V_VMENTRY_NULL(jobject, getJavaMirror, (JNIEnv* env, jobject, jobject object))2040if (object == NULL) {2041JVMCI_THROW_0(NullPointerException);2042}2043JVMCIObject base_object = JVMCIENV->wrap(object);2044Handle mirror;2045if (JVMCIENV->isa_HotSpotResolvedObjectTypeImpl(base_object)) {2046mirror = Handle(THREAD, JVMCIENV->asKlass(base_object)->java_mirror());2047} else if (JVMCIENV->isa_HotSpotResolvedPrimitiveType(base_object)) {2048mirror = JVMCIENV->asConstant(JVMCIENV->get_HotSpotResolvedPrimitiveType_mirror(base_object), JVMCI_CHECK_NULL);2049} else {2050JVMCI_THROW_MSG_NULL(IllegalArgumentException,2051err_msg("Unexpected type: %s", JVMCIENV->klass_name(base_object)));2052}2053JVMCIObject result = JVMCIENV->get_object_constant(mirror());2054return JVMCIENV->get_jobject(result);2055C2V_END205620572058C2V_VMENTRY_0(jint, getArrayLength, (JNIEnv* env, jobject, jobject x))2059if (x == NULL) {2060JVMCI_THROW_0(NullPointerException);2061}2062Handle xobj = JVMCIENV->asConstant(JVMCIENV->wrap(x), JVMCI_CHECK_0);2063if (xobj->klass()->is_array_klass()) {2064return arrayOop(xobj())->length();2065}2066return -1;2067C2V_END206820692070C2V_VMENTRY_NULL(jobject, readArrayElement, (JNIEnv* env, jobject, jobject x, int index))2071if (x == NULL) {2072JVMCI_THROW_0(NullPointerException);2073}2074Handle xobj = JVMCIENV->asConstant(JVMCIENV->wrap(x), JVMCI_CHECK_NULL);2075if (xobj->klass()->is_array_klass()) {2076arrayOop array = arrayOop(xobj());2077BasicType element_type = ArrayKlass::cast(array->klass())->element_type();2078if (index < 0 || index >= array->length()) {2079return NULL;2080}2081JVMCIObject result;20822083if (element_type == T_OBJECT) {2084result = JVMCIENV->get_object_constant(objArrayOop(xobj())->obj_at(index));2085if (result.is_null()) {2086result = JVMCIENV->get_JavaConstant_NULL_POINTER();2087}2088} else {2089jvalue value;2090switch (element_type) {2091case T_DOUBLE: value.d = typeArrayOop(xobj())->double_at(index); break;2092case T_FLOAT: value.f = typeArrayOop(xobj())->float_at(index); break;2093case T_LONG: value.j = typeArrayOop(xobj())->long_at(index); break;2094case T_INT: value.i = typeArrayOop(xobj())->int_at(index); break;2095case T_SHORT: value.s = typeArrayOop(xobj())->short_at(index); break;2096case T_CHAR: value.c = typeArrayOop(xobj())->char_at(index); break;2097case T_BYTE: value.b = typeArrayOop(xobj())->byte_at(index); break;2098case T_BOOLEAN: value.z = typeArrayOop(xobj())->byte_at(index) & 1; break;2099default: ShouldNotReachHere();2100}2101result = JVMCIENV->create_box(element_type, &value, JVMCI_CHECK_NULL);2102}2103assert(!result.is_null(), "must have a value");2104return JVMCIENV->get_jobject(result);2105}2106return NULL;;2107C2V_END210821092110C2V_VMENTRY_0(jint, arrayBaseOffset, (JNIEnv* env, jobject, jobject kind))2111if (kind == NULL) {2112JVMCI_THROW_0(NullPointerException);2113}2114BasicType type = JVMCIENV->kindToBasicType(JVMCIENV->wrap(kind), JVMCI_CHECK_0);2115return arrayOopDesc::header_size(type) * HeapWordSize;2116C2V_END21172118C2V_VMENTRY_0(jint, arrayIndexScale, (JNIEnv* env, jobject, jobject kind))2119if (kind == NULL) {2120JVMCI_THROW_0(NullPointerException);2121}2122BasicType type = JVMCIENV->kindToBasicType(JVMCIENV->wrap(kind), JVMCI_CHECK_0);2123return type2aelembytes(type);2124C2V_END21252126C2V_VMENTRY(void, deleteGlobalHandle, (JNIEnv* env, jobject, jlong h))2127jobject handle = (jobject)(address)h;2128if (handle != NULL) {2129JVMCIENV->runtime()->destroy_global(handle);2130}2131}21322133static void requireJVMCINativeLibrary(JVMCI_TRAPS) {2134if (!UseJVMCINativeLibrary) {2135JVMCI_THROW_MSG(UnsupportedOperationException, "JVMCI shared library is not enabled (requires -XX:+UseJVMCINativeLibrary)");2136}2137}21382139C2V_VMENTRY_NULL(jlongArray, registerNativeMethods, (JNIEnv* env, jobject, jclass mirror))2140requireJVMCINativeLibrary(JVMCI_CHECK_NULL);2141requireInHotSpot("registerNativeMethods", JVMCI_CHECK_NULL);2142char* sl_path;2143void* sl_handle;2144JVMCIRuntime* runtime = JVMCI::compiler_runtime();2145{2146// Ensure the JVMCI shared library runtime is initialized.2147JVMCIEnv __peer_jvmci_env__(thread, false, __FILE__, __LINE__);2148JVMCIEnv* peerEnv = &__peer_jvmci_env__;2149HandleMark hm(THREAD);2150JVMCIObject receiver = runtime->get_HotSpotJVMCIRuntime(peerEnv);2151if (peerEnv->has_pending_exception()) {2152peerEnv->describe_pending_exception(true);2153}2154sl_handle = JVMCI::get_shared_library(sl_path, false);2155if (sl_handle == NULL) {2156JVMCI_THROW_MSG_0(InternalError, err_msg("Error initializing JVMCI runtime %d", runtime->id()));2157}2158}21592160if (mirror == NULL) {2161JVMCI_THROW_0(NullPointerException);2162}2163Klass* klass = java_lang_Class::as_Klass(JNIHandles::resolve(mirror));2164if (klass == NULL || !klass->is_instance_klass()) {2165JVMCI_THROW_MSG_0(IllegalArgumentException, "clazz is for primitive type");2166}21672168InstanceKlass* iklass = InstanceKlass::cast(klass);2169for (int i = 0; i < iklass->methods()->length(); i++) {2170methodHandle method(THREAD, iklass->methods()->at(i));2171if (method->is_native()) {21722173// Compute argument size2174int args_size = 1 // JNIEnv2175+ (method->is_static() ? 1 : 0) // class for static methods2176+ method->size_of_parameters(); // actual parameters21772178// 1) Try JNI short style2179stringStream st;2180char* pure_name = NativeLookup::pure_jni_name(method);2181guarantee(pure_name != NULL, "Illegal native method name encountered");2182os::print_jni_name_prefix_on(&st, args_size);2183st.print_raw(pure_name);2184os::print_jni_name_suffix_on(&st, args_size);2185char* jni_name = st.as_string();21862187address entry = (address) os::dll_lookup(sl_handle, jni_name);2188if (entry == NULL) {2189// 2) Try JNI long style2190st.reset();2191char* long_name = NativeLookup::long_jni_name(method);2192guarantee(long_name != NULL, "Illegal native method name encountered");2193os::print_jni_name_prefix_on(&st, args_size);2194st.print_raw(pure_name);2195st.print_raw(long_name);2196os::print_jni_name_suffix_on(&st, args_size);2197char* jni_long_name = st.as_string();2198entry = (address) os::dll_lookup(sl_handle, jni_long_name);2199if (entry == NULL) {2200JVMCI_THROW_MSG_0(UnsatisfiedLinkError, err_msg("%s [neither %s nor %s exist in %s]",2201method->name_and_sig_as_C_string(),2202jni_name, jni_long_name, sl_path));2203}2204}22052206if (method->has_native_function() && entry != method->native_function()) {2207JVMCI_THROW_MSG_0(UnsatisfiedLinkError, err_msg("%s [cannot re-link from " PTR_FORMAT " to " PTR_FORMAT "]",2208method->name_and_sig_as_C_string(), p2i(method->native_function()), p2i(entry)));2209}2210method->set_native_function(entry, Method::native_bind_event_is_interesting);2211log_debug(jni, resolve)("[Dynamic-linking native method %s.%s ... JNI] @ " PTR_FORMAT,2212method->method_holder()->external_name(),2213method->name()->as_C_string(),2214p2i((void*) entry));2215}2216}22172218typeArrayOop info_oop = oopFactory::new_longArray(4, CHECK_0);2219jlongArray info = (jlongArray) JNIHandles::make_local(THREAD, info_oop);2220runtime->init_JavaVM_info(info, JVMCI_CHECK_0);2221return info;2222}22232224C2V_VMENTRY_PREFIX(jboolean, isCurrentThreadAttached, (JNIEnv* env, jobject c2vm))2225if (thread == NULL) {2226// Called from unattached JVMCI shared library thread2227return false;2228}2229JVMCITraceMark jtm("isCurrentThreadAttached");2230if (thread->jni_environment() == env) {2231C2V_BLOCK(jboolean, isCurrentThreadAttached, (JNIEnv* env, jobject))2232requireJVMCINativeLibrary(JVMCI_CHECK_0);2233JVMCIRuntime* runtime = JVMCI::compiler_runtime();2234if (runtime == NULL || !runtime->has_shared_library_javavm()) {2235JVMCI_THROW_MSG_0(IllegalStateException, "Require JVMCI shared library JavaVM to be initialized in isCurrentThreadAttached");2236}2237JNIEnv* peerEnv;2238return runtime->GetEnv(thread, (void**) &peerEnv, JNI_VERSION_1_2) == JNI_OK;2239}2240return true;2241C2V_END22422243C2V_VMENTRY_PREFIX(jlong, getCurrentJavaThread, (JNIEnv* env, jobject c2vm))2244if (thread == NULL) {2245// Called from unattached JVMCI shared library thread2246return 0L;2247}2248JVMCITraceMark jtm("getCurrentJavaThread");2249return (jlong) p2i(thread);2250C2V_END22512252C2V_VMENTRY_PREFIX(jboolean, attachCurrentThread, (JNIEnv* env, jobject c2vm, jbyteArray name, jboolean as_daemon))2253if (thread == NULL) {2254// Called from unattached JVMCI shared library thread2255guarantee(name != NULL, "libjvmci caller must pass non-null name");22562257extern struct JavaVM_ main_vm;2258JNIEnv* hotspotEnv;22592260int name_len = env->GetArrayLength(name);2261char name_buf[64]; // Cannot use Resource heap as it requires a current thread2262int to_copy = MIN2(name_len, (int) sizeof(name_buf) - 1);2263env->GetByteArrayRegion(name, 0, to_copy, (jbyte*) name_buf);2264name_buf[to_copy] = '\0';2265JavaVMAttachArgs attach_args;2266attach_args.version = JNI_VERSION_1_2;2267attach_args.name = name_buf;2268attach_args.group = NULL;2269jint res = as_daemon ? main_vm.AttachCurrentThreadAsDaemon((void**) &hotspotEnv, &attach_args) :2270main_vm.AttachCurrentThread((void**) &hotspotEnv, &attach_args);2271if (res != JNI_OK) {2272JNI_THROW_("attachCurrentThread", InternalError, err_msg("Trying to attach thread returned %d", res), false);2273}2274return true;2275}2276JVMCITraceMark jtm("attachCurrentThread");2277if (thread->jni_environment() == env) {2278// Called from HotSpot2279C2V_BLOCK(jboolean, attachCurrentThread, (JNIEnv* env, jobject, jboolean))2280requireJVMCINativeLibrary(JVMCI_CHECK_0);2281JVMCIRuntime* runtime = JVMCI::compiler_runtime();2282if (runtime == NULL || !runtime->has_shared_library_javavm()) {2283JVMCI_THROW_MSG_0(IllegalStateException, "Require JVMCI shared library JavaVM to be initialized in attachCurrentThread");2284}22852286JavaVMAttachArgs attach_args;2287attach_args.version = JNI_VERSION_1_2;2288attach_args.name = thread->name();2289attach_args.group = NULL;2290JNIEnv* peerJNIEnv;2291if (runtime->GetEnv(thread, (void**) &peerJNIEnv, JNI_VERSION_1_2) == JNI_OK) {2292return false;2293}2294jint res = as_daemon ? runtime->AttachCurrentThreadAsDaemon(thread, (void**) &peerJNIEnv, &attach_args) :2295runtime->AttachCurrentThread(thread, (void**) &peerJNIEnv, &attach_args);22962297if (res == JNI_OK) {2298guarantee(peerJNIEnv != NULL, "must be");2299JVMCI_event_1("attached to JavaVM for JVMCI runtime %d", runtime->id());2300return true;2301}2302JVMCI_THROW_MSG_0(InternalError, err_msg("Error %d while attaching %s", res, attach_args.name));2303}2304// Called from JVMCI shared library2305return false;2306C2V_END23072308C2V_VMENTRY_PREFIX(void, detachCurrentThread, (JNIEnv* env, jobject c2vm))2309if (thread == NULL) {2310// Called from unattached JVMCI shared library thread2311JNI_THROW("detachCurrentThread", IllegalStateException, "Cannot detach non-attached thread");2312}2313JVMCITraceMark jtm("detachCurrentThread");2314if (thread->jni_environment() == env) {2315// Called from HotSpot2316C2V_BLOCK(void, detachCurrentThread, (JNIEnv* env, jobject))2317requireJVMCINativeLibrary(JVMCI_CHECK);2318requireInHotSpot("detachCurrentThread", JVMCI_CHECK);2319JVMCIRuntime* runtime = JVMCI::compiler_runtime();2320if (runtime == NULL || !runtime->has_shared_library_javavm()) {2321JVMCI_THROW_MSG(IllegalStateException, "Require JVMCI shared library JavaVM to be initialized in detachCurrentThread");2322}2323JNIEnv* peerJNIEnv;2324if (runtime->GetEnv(thread, (void**) &peerJNIEnv, JNI_VERSION_1_2) != JNI_OK) {2325JVMCI_THROW_MSG(IllegalStateException, err_msg("Cannot detach non-attached thread: %s", thread->name()));2326}2327jint res = runtime->DetachCurrentThread(thread);2328if (res != JNI_OK) {2329JVMCI_THROW_MSG(InternalError, err_msg("Error %d while attaching %s", res, thread->name()));2330}2331} else {2332// Called from attached JVMCI shared library thread2333extern struct JavaVM_ main_vm;2334jint res = main_vm.DetachCurrentThread();2335if (res != JNI_OK) {2336JNI_THROW("detachCurrentThread", InternalError, "Cannot detach non-attached thread");2337}2338}2339C2V_END23402341C2V_VMENTRY_0(jlong, translate, (JNIEnv* env, jobject, jobject obj_handle, jboolean callPostTranslation))2342requireJVMCINativeLibrary(JVMCI_CHECK_0);2343if (obj_handle == NULL) {2344return 0L;2345}2346JVMCIEnv __peer_jvmci_env__(thread, !JVMCIENV->is_hotspot(), __FILE__, __LINE__);2347JVMCIEnv* peerEnv = &__peer_jvmci_env__;2348JVMCIEnv* thisEnv = JVMCIENV;23492350JVMCIObject obj = thisEnv->wrap(obj_handle);2351JVMCIObject result;2352if (thisEnv->isa_HotSpotResolvedJavaMethodImpl(obj)) {2353methodHandle method(THREAD, thisEnv->asMethod(obj));2354result = peerEnv->get_jvmci_method(method, JVMCI_CHECK_0);2355} else if (thisEnv->isa_HotSpotResolvedObjectTypeImpl(obj)) {2356Klass* klass = thisEnv->asKlass(obj);2357JVMCIKlassHandle klass_handle(THREAD);2358klass_handle = klass;2359result = peerEnv->get_jvmci_type(klass_handle, JVMCI_CHECK_0);2360} else if (thisEnv->isa_HotSpotResolvedPrimitiveType(obj)) {2361BasicType type = JVMCIENV->kindToBasicType(JVMCIENV->get_HotSpotResolvedPrimitiveType_kind(obj), JVMCI_CHECK_0);2362result = peerEnv->get_jvmci_primitive_type(type);2363} else if (thisEnv->isa_IndirectHotSpotObjectConstantImpl(obj) ||2364thisEnv->isa_DirectHotSpotObjectConstantImpl(obj)) {2365Handle constant = thisEnv->asConstant(obj, JVMCI_CHECK_0);2366result = peerEnv->get_object_constant(constant());2367} else if (thisEnv->isa_HotSpotNmethod(obj)) {2368nmethodLocker locker;2369nmethod* nm = JVMCIENV->get_nmethod(obj, locker);2370if (nm != NULL) {2371JVMCINMethodData* data = nm->jvmci_nmethod_data();2372if (data != NULL) {2373if (peerEnv->is_hotspot()) {2374// Only the mirror in the HotSpot heap is accessible2375// through JVMCINMethodData2376oop nmethod_mirror = data->get_nmethod_mirror(nm, /* phantom_ref */ true);2377if (nmethod_mirror != NULL) {2378result = HotSpotJVMCI::wrap(nmethod_mirror);2379}2380}2381}2382}2383if (result.is_null()) {2384JVMCIObject methodObject = thisEnv->get_HotSpotNmethod_method(obj);2385methodHandle mh(THREAD, thisEnv->asMethod(methodObject));2386jboolean isDefault = thisEnv->get_HotSpotNmethod_isDefault(obj);2387jlong compileIdSnapshot = thisEnv->get_HotSpotNmethod_compileIdSnapshot(obj);2388JVMCIObject name_string = thisEnv->get_InstalledCode_name(obj);2389const char* cstring = name_string.is_null() ? NULL : thisEnv->as_utf8_string(name_string);2390// Create a new HotSpotNmethod instance in the peer runtime2391result = peerEnv->new_HotSpotNmethod(mh, cstring, isDefault, compileIdSnapshot, JVMCI_CHECK_0);2392if (result.is_null()) {2393// exception occurred (e.g. OOME) creating a new HotSpotNmethod2394} else if (nm == NULL) {2395// nmethod must have been unloaded2396} else {2397// Link the new HotSpotNmethod to the nmethod2398peerEnv->initialize_installed_code(result, nm, JVMCI_CHECK_0);2399// Only HotSpotNmethod instances in the HotSpot heap are tracked directly by the runtime.2400if (peerEnv->is_hotspot()) {2401JVMCINMethodData* data = nm->jvmci_nmethod_data();2402if (data == NULL) {2403JVMCI_THROW_MSG_0(IllegalArgumentException, "Cannot set HotSpotNmethod mirror for default nmethod");2404}2405if (data->get_nmethod_mirror(nm, /* phantom_ref */ false) != NULL) {2406JVMCI_THROW_MSG_0(IllegalArgumentException, "Cannot overwrite existing HotSpotNmethod mirror for nmethod");2407}2408oop nmethod_mirror = HotSpotJVMCI::resolve(result);2409data->set_nmethod_mirror(nm, nmethod_mirror);2410}2411}2412}2413} else {2414JVMCI_THROW_MSG_0(IllegalArgumentException,2415err_msg("Cannot translate object of type: %s", thisEnv->klass_name(obj)));2416}2417if (callPostTranslation) {2418peerEnv->call_HotSpotJVMCIRuntime_postTranslation(result, JVMCI_CHECK_0);2419}2420// Propagate any exception that occurred while creating the translated object2421if (peerEnv->transfer_pending_exception(thread, thisEnv)) {2422return 0L;2423}2424return (jlong) peerEnv->make_global(result).as_jobject();2425}24262427C2V_VMENTRY_NULL(jobject, unhand, (JNIEnv* env, jobject, jlong obj_handle))2428requireJVMCINativeLibrary(JVMCI_CHECK_NULL);2429if (obj_handle == 0L) {2430return NULL;2431}2432jobject global_handle = (jobject) obj_handle;2433JVMCIObject global_handle_obj = JVMCIENV->wrap((jobject) obj_handle);2434jobject result = JVMCIENV->make_local(global_handle_obj).as_jobject();24352436JVMCIENV->destroy_global(global_handle_obj);2437return result;2438}24392440C2V_VMENTRY(void, updateHotSpotNmethod, (JNIEnv* env, jobject, jobject code_handle))2441JVMCIObject code = JVMCIENV->wrap(code_handle);2442// Execute this operation for the side effect of updating the InstalledCode state2443nmethodLocker locker;2444JVMCIENV->get_nmethod(code, locker);2445}24462447C2V_VMENTRY_NULL(jbyteArray, getCode, (JNIEnv* env, jobject, jobject code_handle))2448JVMCIObject code = JVMCIENV->wrap(code_handle);2449nmethodLocker locker;2450CodeBlob* cb = JVMCIENV->get_code_blob(code, locker);2451if (cb == NULL) {2452return NULL;2453}2454int code_size = cb->code_size();2455JVMCIPrimitiveArray result = JVMCIENV->new_byteArray(code_size, JVMCI_CHECK_NULL);2456JVMCIENV->copy_bytes_from((jbyte*) cb->code_begin(), result, 0, code_size);2457return JVMCIENV->get_jbyteArray(result);2458}24592460C2V_VMENTRY_NULL(jobject, asReflectionExecutable, (JNIEnv* env, jobject, jobject jvmci_method))2461requireInHotSpot("asReflectionExecutable", JVMCI_CHECK_NULL);2462methodHandle m(THREAD, JVMCIENV->asMethod(jvmci_method));2463oop executable;2464if (m->is_initializer()) {2465if (m->is_static_initializer()) {2466JVMCI_THROW_MSG_NULL(IllegalArgumentException,2467"Cannot create java.lang.reflect.Method for class initializer");2468}2469executable = Reflection::new_constructor(m, CHECK_NULL);2470} else {2471executable = Reflection::new_method(m, false, CHECK_NULL);2472}2473return JNIHandles::make_local(THREAD, executable);2474}24752476C2V_VMENTRY_NULL(jobject, asReflectionField, (JNIEnv* env, jobject, jobject jvmci_type, jint index))2477requireInHotSpot("asReflectionField", JVMCI_CHECK_NULL);2478Klass* klass = JVMCIENV->asKlass(jvmci_type);2479if (!klass->is_instance_klass()) {2480JVMCI_THROW_MSG_NULL(IllegalArgumentException,2481err_msg("Expected non-primitive type, got %s", klass->external_name()));2482}2483InstanceKlass* iklass = InstanceKlass::cast(klass);2484Array<u2>* fields = iklass->fields();2485if (index < 0 ||index > fields->length()) {2486JVMCI_THROW_MSG_NULL(IllegalArgumentException,2487err_msg("Field index %d out of bounds for %s", index, klass->external_name()));2488}2489fieldDescriptor fd(iklass, index);2490oop reflected = Reflection::new_field(&fd, CHECK_NULL);2491return JNIHandles::make_local(THREAD, reflected);2492}24932494C2V_VMENTRY_NULL(jobjectArray, getFailedSpeculations, (JNIEnv* env, jobject, jlong failed_speculations_address, jobjectArray current))2495FailedSpeculation* head = *((FailedSpeculation**)(address) failed_speculations_address);2496int result_length = 0;2497for (FailedSpeculation* fs = head; fs != NULL; fs = fs->next()) {2498result_length++;2499}2500int current_length = 0;2501JVMCIObjectArray current_array = NULL;2502if (current != NULL) {2503current_array = JVMCIENV->wrap(current);2504current_length = JVMCIENV->get_length(current_array);2505if (current_length == result_length) {2506// No new failures2507return current;2508}2509}2510JVMCIObjectArray result = JVMCIENV->new_byte_array_array(result_length, JVMCI_CHECK_NULL);2511int result_index = 0;2512for (FailedSpeculation* fs = head; result_index < result_length; fs = fs->next()) {2513assert(fs != NULL, "npe");2514JVMCIPrimitiveArray entry;2515if (result_index < current_length) {2516entry = (JVMCIPrimitiveArray) JVMCIENV->get_object_at(current_array, result_index);2517} else {2518entry = JVMCIENV->new_byteArray(fs->data_len(), JVMCI_CHECK_NULL);2519JVMCIENV->copy_bytes_from((jbyte*) fs->data(), entry, 0, fs->data_len());2520}2521JVMCIENV->put_object_at(result, result_index++, entry);2522}2523return JVMCIENV->get_jobjectArray(result);2524}25252526C2V_VMENTRY_0(jlong, getFailedSpeculationsAddress, (JNIEnv* env, jobject, jobject jvmci_method))2527methodHandle method(THREAD, JVMCIENV->asMethod(jvmci_method));2528MethodData* method_data = method->method_data();2529if (method_data == NULL) {2530ClassLoaderData* loader_data = method->method_holder()->class_loader_data();2531method_data = MethodData::allocate(loader_data, method, CHECK_0);2532method->set_method_data(method_data);2533}2534return (jlong) method_data->get_failed_speculations_address();2535}25362537C2V_VMENTRY(void, releaseFailedSpeculations, (JNIEnv* env, jobject, jlong failed_speculations_address))2538FailedSpeculation::free_failed_speculations((FailedSpeculation**)(address) failed_speculations_address);2539}25402541C2V_VMENTRY_0(jboolean, addFailedSpeculation, (JNIEnv* env, jobject, jlong failed_speculations_address, jbyteArray speculation_obj))2542JVMCIPrimitiveArray speculation_handle = JVMCIENV->wrap(speculation_obj);2543int speculation_len = JVMCIENV->get_length(speculation_handle);2544char* speculation = NEW_RESOURCE_ARRAY(char, speculation_len);2545JVMCIENV->copy_bytes_to(speculation_handle, (jbyte*) speculation, 0, speculation_len);2546return FailedSpeculation::add_failed_speculation(NULL, (FailedSpeculation**)(address) failed_speculations_address, (address) speculation, speculation_len);2547}25482549C2V_VMENTRY(void, callSystemExit, (JNIEnv* env, jobject, jint status))2550JavaValue result(T_VOID);2551JavaCallArguments jargs(1);2552jargs.push_int(status);2553JavaCalls::call_static(&result,2554vmClasses::System_klass(),2555vmSymbols::exit_method_name(),2556vmSymbols::int_void_signature(),2557&jargs,2558CHECK);2559}25602561C2V_VMENTRY_0(jlong, ticksNow, (JNIEnv* env, jobject))2562return CompilerEvent::ticksNow();2563}25642565C2V_VMENTRY_0(jint, registerCompilerPhase, (JNIEnv* env, jobject, jstring jphase_name))2566#if INCLUDE_JFR2567JVMCIObject phase_name = JVMCIENV->wrap(jphase_name);2568const char *name = JVMCIENV->as_utf8_string(phase_name);2569return CompilerEvent::PhaseEvent::get_phase_id(name, true, true, true);2570#else2571return -1;2572#endif // !INCLUDE_JFR2573}25742575C2V_VMENTRY(void, notifyCompilerPhaseEvent, (JNIEnv* env, jobject, jlong startTime, jint phase, jint compileId, jint level))2576EventCompilerPhase event;2577if (event.should_commit()) {2578CompilerEvent::PhaseEvent::post(event, startTime, phase, compileId, level);2579}2580}25812582C2V_VMENTRY(void, notifyCompilerInliningEvent, (JNIEnv* env, jobject, jint compileId, jobject caller, jobject callee, jboolean succeeded, jstring jmessage, jint bci))2583EventCompilerInlining event;2584if (event.should_commit()) {2585Method* caller_method = JVMCIENV->asMethod(caller);2586Method* callee_method = JVMCIENV->asMethod(callee);2587JVMCIObject message = JVMCIENV->wrap(jmessage);2588CompilerEvent::InlineEvent::post(event, compileId, caller_method, callee_method, succeeded, JVMCIENV->as_utf8_string(message), bci);2589}2590}25912592C2V_VMENTRY(void, setThreadLocalObject, (JNIEnv* env, jobject, jint id, jobject value))2593requireInHotSpot("setThreadLocalObject", JVMCI_CHECK);2594if (id == 0) {2595thread->set_jvmci_reserved_oop0(JNIHandles::resolve(value));2596return;2597}2598THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),2599err_msg("%d is not a valid thread local id", id));2600}26012602C2V_VMENTRY_NULL(jobject, getThreadLocalObject, (JNIEnv* env, jobject, jint id))2603requireInHotSpot("getThreadLocalObject", JVMCI_CHECK_NULL);2604if (id == 0) {2605return JNIHandles::make_local(thread->get_jvmci_reserved_oop0());2606}2607THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),2608err_msg("%d is not a valid thread local id", id));2609}26102611C2V_VMENTRY(void, setThreadLocalLong, (JNIEnv* env, jobject, jint id, jlong value))2612requireInHotSpot("setThreadLocalLong", JVMCI_CHECK);2613if (id == 0) {2614thread->set_jvmci_reserved0(value);2615} else if (id == 1) {2616thread->set_jvmci_reserved1(value);2617} else {2618THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),2619err_msg("%d is not a valid thread local id", id));2620}2621}26222623C2V_VMENTRY_0(jlong, getThreadLocalLong, (JNIEnv* env, jobject, jint id))2624requireInHotSpot("getThreadLocalLong", JVMCI_CHECK_0);2625if (id == 0) {2626return thread->get_jvmci_reserved0();2627} else if (id == 1) {2628return thread->get_jvmci_reserved1();2629} else {2630THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),2631err_msg("%d is not a valid thread local id", id));2632}2633}26342635#define CC (char*) /*cast a literal from (const char*)*/2636#define FN_PTR(f) CAST_FROM_FN_PTR(void*, &(c2v_ ## f))26372638#define STRING "Ljava/lang/String;"2639#define OBJECT "Ljava/lang/Object;"2640#define CLASS "Ljava/lang/Class;"2641#define OBJECTCONSTANT "Ljdk/vm/ci/hotspot/HotSpotObjectConstantImpl;"2642#define HANDLECONSTANT "Ljdk/vm/ci/hotspot/IndirectHotSpotObjectConstantImpl;"2643#define EXECUTABLE "Ljava/lang/reflect/Executable;"2644#define STACK_TRACE_ELEMENT "Ljava/lang/StackTraceElement;"2645#define INSTALLED_CODE "Ljdk/vm/ci/code/InstalledCode;"2646#define TARGET_DESCRIPTION "Ljdk/vm/ci/code/TargetDescription;"2647#define BYTECODE_FRAME "Ljdk/vm/ci/code/BytecodeFrame;"2648#define JAVACONSTANT "Ljdk/vm/ci/meta/JavaConstant;"2649#define INSPECTED_FRAME_VISITOR "Ljdk/vm/ci/code/stack/InspectedFrameVisitor;"2650#define RESOLVED_METHOD "Ljdk/vm/ci/meta/ResolvedJavaMethod;"2651#define HS_RESOLVED_METHOD "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaMethodImpl;"2652#define HS_RESOLVED_KLASS "Ljdk/vm/ci/hotspot/HotSpotResolvedObjectTypeImpl;"2653#define HS_RESOLVED_TYPE "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaType;"2654#define HS_RESOLVED_FIELD "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaField;"2655#define HS_INSTALLED_CODE "Ljdk/vm/ci/hotspot/HotSpotInstalledCode;"2656#define HS_NMETHOD "Ljdk/vm/ci/hotspot/HotSpotNmethod;"2657#define HS_CONSTANT_POOL "Ljdk/vm/ci/hotspot/HotSpotConstantPool;"2658#define HS_COMPILED_CODE "Ljdk/vm/ci/hotspot/HotSpotCompiledCode;"2659#define HS_CONFIG "Ljdk/vm/ci/hotspot/HotSpotVMConfig;"2660#define HS_METADATA "Ljdk/vm/ci/hotspot/HotSpotMetaData;"2661#define HS_STACK_FRAME_REF "Ljdk/vm/ci/hotspot/HotSpotStackFrameReference;"2662#define HS_SPECULATION_LOG "Ljdk/vm/ci/hotspot/HotSpotSpeculationLog;"2663#define METASPACE_OBJECT "Ljdk/vm/ci/hotspot/MetaspaceObject;"2664#define REFLECTION_EXECUTABLE "Ljava/lang/reflect/Executable;"2665#define REFLECTION_FIELD "Ljava/lang/reflect/Field;"2666#define METASPACE_METHOD_DATA "J"26672668JNINativeMethod CompilerToVM::methods[] = {2669{CC "getBytecode", CC "(" HS_RESOLVED_METHOD ")[B", FN_PTR(getBytecode)},2670{CC "getExceptionTableStart", CC "(" HS_RESOLVED_METHOD ")J", FN_PTR(getExceptionTableStart)},2671{CC "getExceptionTableLength", CC "(" HS_RESOLVED_METHOD ")I", FN_PTR(getExceptionTableLength)},2672{CC "findUniqueConcreteMethod", CC "(" HS_RESOLVED_KLASS HS_RESOLVED_METHOD ")" HS_RESOLVED_METHOD, FN_PTR(findUniqueConcreteMethod)},2673{CC "getImplementor", CC "(" HS_RESOLVED_KLASS ")" HS_RESOLVED_KLASS, FN_PTR(getImplementor)},2674{CC "getStackTraceElement", CC "(" HS_RESOLVED_METHOD "I)" STACK_TRACE_ELEMENT, FN_PTR(getStackTraceElement)},2675{CC "methodIsIgnoredBySecurityStackWalk", CC "(" HS_RESOLVED_METHOD ")Z", FN_PTR(methodIsIgnoredBySecurityStackWalk)},2676{CC "setNotInlinableOrCompilable", CC "(" HS_RESOLVED_METHOD ")V", FN_PTR(setNotInlinableOrCompilable)},2677{CC "isCompilable", CC "(" HS_RESOLVED_METHOD ")Z", FN_PTR(isCompilable)},2678{CC "hasNeverInlineDirective", CC "(" HS_RESOLVED_METHOD ")Z", FN_PTR(hasNeverInlineDirective)},2679{CC "shouldInlineMethod", CC "(" HS_RESOLVED_METHOD ")Z", FN_PTR(shouldInlineMethod)},2680{CC "lookupType", CC "(" STRING HS_RESOLVED_KLASS "Z)" HS_RESOLVED_TYPE, FN_PTR(lookupType)},2681{CC "getArrayType", CC "(" HS_RESOLVED_TYPE ")" HS_RESOLVED_KLASS, FN_PTR(getArrayType)},2682{CC "lookupClass", CC "(" CLASS ")" HS_RESOLVED_TYPE, FN_PTR(lookupClass)},2683{CC "lookupNameInPool", CC "(" HS_CONSTANT_POOL "I)" STRING, FN_PTR(lookupNameInPool)},2684{CC "lookupNameAndTypeRefIndexInPool", CC "(" HS_CONSTANT_POOL "I)I", FN_PTR(lookupNameAndTypeRefIndexInPool)},2685{CC "lookupSignatureInPool", CC "(" HS_CONSTANT_POOL "I)" STRING, FN_PTR(lookupSignatureInPool)},2686{CC "lookupKlassRefIndexInPool", CC "(" HS_CONSTANT_POOL "I)I", FN_PTR(lookupKlassRefIndexInPool)},2687{CC "lookupKlassInPool", CC "(" HS_CONSTANT_POOL "I)Ljava/lang/Object;", FN_PTR(lookupKlassInPool)},2688{CC "lookupAppendixInPool", CC "(" HS_CONSTANT_POOL "I)" OBJECTCONSTANT, FN_PTR(lookupAppendixInPool)},2689{CC "lookupMethodInPool", CC "(" HS_CONSTANT_POOL "IB)" HS_RESOLVED_METHOD, FN_PTR(lookupMethodInPool)},2690{CC "constantPoolRemapInstructionOperandFromCache", CC "(" HS_CONSTANT_POOL "I)I", FN_PTR(constantPoolRemapInstructionOperandFromCache)},2691{CC "resolvePossiblyCachedConstantInPool", CC "(" HS_CONSTANT_POOL "I)" JAVACONSTANT, FN_PTR(resolvePossiblyCachedConstantInPool)},2692{CC "resolveTypeInPool", CC "(" HS_CONSTANT_POOL "I)" HS_RESOLVED_KLASS, FN_PTR(resolveTypeInPool)},2693{CC "resolveFieldInPool", CC "(" HS_CONSTANT_POOL "I" HS_RESOLVED_METHOD "B[I)" HS_RESOLVED_KLASS, FN_PTR(resolveFieldInPool)},2694{CC "resolveInvokeDynamicInPool", CC "(" HS_CONSTANT_POOL "I)V", FN_PTR(resolveInvokeDynamicInPool)},2695{CC "resolveInvokeHandleInPool", CC "(" HS_CONSTANT_POOL "I)V", FN_PTR(resolveInvokeHandleInPool)},2696{CC "isResolvedInvokeHandleInPool", CC "(" HS_CONSTANT_POOL "I)I", FN_PTR(isResolvedInvokeHandleInPool)},2697{CC "resolveMethod", CC "(" HS_RESOLVED_KLASS HS_RESOLVED_METHOD HS_RESOLVED_KLASS ")" HS_RESOLVED_METHOD, FN_PTR(resolveMethod)},2698{CC "getSignaturePolymorphicHolders", CC "()[" STRING, FN_PTR(getSignaturePolymorphicHolders)},2699{CC "getVtableIndexForInterfaceMethod", CC "(" HS_RESOLVED_KLASS HS_RESOLVED_METHOD ")I", FN_PTR(getVtableIndexForInterfaceMethod)},2700{CC "getClassInitializer", CC "(" HS_RESOLVED_KLASS ")" HS_RESOLVED_METHOD, FN_PTR(getClassInitializer)},2701{CC "hasFinalizableSubclass", CC "(" HS_RESOLVED_KLASS ")Z", FN_PTR(hasFinalizableSubclass)},2702{CC "getMaxCallTargetOffset", CC "(J)J", FN_PTR(getMaxCallTargetOffset)},2703{CC "asResolvedJavaMethod", CC "(" EXECUTABLE ")" HS_RESOLVED_METHOD, FN_PTR(asResolvedJavaMethod)},2704{CC "getResolvedJavaMethod", CC "(" OBJECTCONSTANT "J)" HS_RESOLVED_METHOD, FN_PTR(getResolvedJavaMethod)},2705{CC "getConstantPool", CC "(" METASPACE_OBJECT ")" HS_CONSTANT_POOL, FN_PTR(getConstantPool)},2706{CC "getResolvedJavaType0", CC "(Ljava/lang/Object;JZ)" HS_RESOLVED_KLASS, FN_PTR(getResolvedJavaType0)},2707{CC "readConfiguration", CC "()[" OBJECT, FN_PTR(readConfiguration)},2708{CC "installCode", CC "(" TARGET_DESCRIPTION HS_COMPILED_CODE INSTALLED_CODE "J[B)I", FN_PTR(installCode)},2709{CC "getMetadata", CC "(" TARGET_DESCRIPTION HS_COMPILED_CODE HS_METADATA ")I", FN_PTR(getMetadata)},2710{CC "resetCompilationStatistics", CC "()V", FN_PTR(resetCompilationStatistics)},2711{CC "disassembleCodeBlob", CC "(" INSTALLED_CODE ")" STRING, FN_PTR(disassembleCodeBlob)},2712{CC "executeHotSpotNmethod", CC "([" OBJECT HS_NMETHOD ")" OBJECT, FN_PTR(executeHotSpotNmethod)},2713{CC "getLineNumberTable", CC "(" HS_RESOLVED_METHOD ")[J", FN_PTR(getLineNumberTable)},2714{CC "getLocalVariableTableStart", CC "(" HS_RESOLVED_METHOD ")J", FN_PTR(getLocalVariableTableStart)},2715{CC "getLocalVariableTableLength", CC "(" HS_RESOLVED_METHOD ")I", FN_PTR(getLocalVariableTableLength)},2716{CC "reprofile", CC "(" HS_RESOLVED_METHOD ")V", FN_PTR(reprofile)},2717{CC "invalidateHotSpotNmethod", CC "(" HS_NMETHOD ")V", FN_PTR(invalidateHotSpotNmethod)},2718{CC "collectCounters", CC "()[J", FN_PTR(collectCounters)},2719{CC "getCountersSize", CC "()I", FN_PTR(getCountersSize)},2720{CC "setCountersSize", CC "(I)Z", FN_PTR(setCountersSize)},2721{CC "allocateCompileId", CC "(" HS_RESOLVED_METHOD "I)I", FN_PTR(allocateCompileId)},2722{CC "isMature", CC "(" METASPACE_METHOD_DATA ")Z", FN_PTR(isMature)},2723{CC "hasCompiledCodeForOSR", CC "(" HS_RESOLVED_METHOD "II)Z", FN_PTR(hasCompiledCodeForOSR)},2724{CC "getSymbol", CC "(J)" STRING, FN_PTR(getSymbol)},2725{CC "iterateFrames", CC "([" RESOLVED_METHOD "[" RESOLVED_METHOD "I" INSPECTED_FRAME_VISITOR ")" OBJECT, FN_PTR(iterateFrames)},2726{CC "materializeVirtualObjects", CC "(" HS_STACK_FRAME_REF "Z)V", FN_PTR(materializeVirtualObjects)},2727{CC "shouldDebugNonSafepoints", CC "()Z", FN_PTR(shouldDebugNonSafepoints)},2728{CC "writeDebugOutput", CC "(JIZ)V", FN_PTR(writeDebugOutput)},2729{CC "flushDebugOutput", CC "()V", FN_PTR(flushDebugOutput)},2730{CC "methodDataProfileDataSize", CC "(JI)I", FN_PTR(methodDataProfileDataSize)},2731{CC "getFingerprint", CC "(J)J", FN_PTR(getFingerprint)},2732{CC "interpreterFrameSize", CC "(" BYTECODE_FRAME ")I", FN_PTR(interpreterFrameSize)},2733{CC "compileToBytecode", CC "(" OBJECTCONSTANT ")V", FN_PTR(compileToBytecode)},2734{CC "getFlagValue", CC "(" STRING ")" OBJECT, FN_PTR(getFlagValue)},2735{CC "getInterfaces", CC "(" HS_RESOLVED_KLASS ")[" HS_RESOLVED_KLASS, FN_PTR(getInterfaces)},2736{CC "getComponentType", CC "(" HS_RESOLVED_KLASS ")" HS_RESOLVED_TYPE, FN_PTR(getComponentType)},2737{CC "ensureInitialized", CC "(" HS_RESOLVED_KLASS ")V", FN_PTR(ensureInitialized)},2738{CC "ensureLinked", CC "(" HS_RESOLVED_KLASS ")V", FN_PTR(ensureLinked)},2739{CC "getIdentityHashCode", CC "(" OBJECTCONSTANT ")I", FN_PTR(getIdentityHashCode)},2740{CC "isInternedString", CC "(" OBJECTCONSTANT ")Z", FN_PTR(isInternedString)},2741{CC "unboxPrimitive", CC "(" OBJECTCONSTANT ")" OBJECT, FN_PTR(unboxPrimitive)},2742{CC "boxPrimitive", CC "(" OBJECT ")" OBJECTCONSTANT, FN_PTR(boxPrimitive)},2743{CC "getDeclaredConstructors", CC "(" HS_RESOLVED_KLASS ")[" RESOLVED_METHOD, FN_PTR(getDeclaredConstructors)},2744{CC "getDeclaredMethods", CC "(" HS_RESOLVED_KLASS ")[" RESOLVED_METHOD, FN_PTR(getDeclaredMethods)},2745{CC "readFieldValue", CC "(" HS_RESOLVED_KLASS HS_RESOLVED_KLASS "JLjdk/vm/ci/meta/JavaKind;)" JAVACONSTANT, FN_PTR(readFieldValue)},2746{CC "readFieldValue", CC "(" OBJECTCONSTANT HS_RESOLVED_KLASS "JLjdk/vm/ci/meta/JavaKind;)" JAVACONSTANT, FN_PTR(readFieldValue)},2747{CC "isInstance", CC "(" HS_RESOLVED_KLASS OBJECTCONSTANT ")Z", FN_PTR(isInstance)},2748{CC "isAssignableFrom", CC "(" HS_RESOLVED_KLASS HS_RESOLVED_KLASS ")Z", FN_PTR(isAssignableFrom)},2749{CC "isTrustedForIntrinsics", CC "(" HS_RESOLVED_KLASS ")Z", FN_PTR(isTrustedForIntrinsics)},2750{CC "asJavaType", CC "(" OBJECTCONSTANT ")" HS_RESOLVED_TYPE, FN_PTR(asJavaType)},2751{CC "asString", CC "(" OBJECTCONSTANT ")" STRING, FN_PTR(asString)},2752{CC "equals", CC "(" OBJECTCONSTANT "J" OBJECTCONSTANT "J)Z", FN_PTR(equals)},2753{CC "getJavaMirror", CC "(" HS_RESOLVED_TYPE ")" OBJECTCONSTANT, FN_PTR(getJavaMirror)},2754{CC "getArrayLength", CC "(" OBJECTCONSTANT ")I", FN_PTR(getArrayLength)},2755{CC "readArrayElement", CC "(" OBJECTCONSTANT "I)Ljava/lang/Object;", FN_PTR(readArrayElement)},2756{CC "arrayBaseOffset", CC "(Ljdk/vm/ci/meta/JavaKind;)I", FN_PTR(arrayBaseOffset)},2757{CC "arrayIndexScale", CC "(Ljdk/vm/ci/meta/JavaKind;)I", FN_PTR(arrayIndexScale)},2758{CC "deleteGlobalHandle", CC "(J)V", FN_PTR(deleteGlobalHandle)},2759{CC "registerNativeMethods", CC "(" CLASS ")[J", FN_PTR(registerNativeMethods)},2760{CC "isCurrentThreadAttached", CC "()Z", FN_PTR(isCurrentThreadAttached)},2761{CC "getCurrentJavaThread", CC "()J", FN_PTR(getCurrentJavaThread)},2762{CC "attachCurrentThread", CC "([BZ)Z", FN_PTR(attachCurrentThread)},2763{CC "detachCurrentThread", CC "()V", FN_PTR(detachCurrentThread)},2764{CC "translate", CC "(" OBJECT "Z)J", FN_PTR(translate)},2765{CC "unhand", CC "(J)" OBJECT, FN_PTR(unhand)},2766{CC "updateHotSpotNmethod", CC "(" HS_NMETHOD ")V", FN_PTR(updateHotSpotNmethod)},2767{CC "getCode", CC "(" HS_INSTALLED_CODE ")[B", FN_PTR(getCode)},2768{CC "asReflectionExecutable", CC "(" HS_RESOLVED_METHOD ")" REFLECTION_EXECUTABLE, FN_PTR(asReflectionExecutable)},2769{CC "asReflectionField", CC "(" HS_RESOLVED_KLASS "I)" REFLECTION_FIELD, FN_PTR(asReflectionField)},2770{CC "getFailedSpeculations", CC "(J[[B)[[B", FN_PTR(getFailedSpeculations)},2771{CC "getFailedSpeculationsAddress", CC "(" HS_RESOLVED_METHOD ")J", FN_PTR(getFailedSpeculationsAddress)},2772{CC "releaseFailedSpeculations", CC "(J)V", FN_PTR(releaseFailedSpeculations)},2773{CC "addFailedSpeculation", CC "(J[B)Z", FN_PTR(addFailedSpeculation)},2774{CC "callSystemExit", CC "(I)V", FN_PTR(callSystemExit)},2775{CC "ticksNow", CC "()J", FN_PTR(ticksNow)},2776{CC "getThreadLocalObject", CC "(I)" OBJECT, FN_PTR(getThreadLocalObject)},2777{CC "setThreadLocalObject", CC "(I" OBJECT ")V", FN_PTR(setThreadLocalObject)},2778{CC "getThreadLocalLong", CC "(I)J", FN_PTR(getThreadLocalLong)},2779{CC "setThreadLocalLong", CC "(IJ)V", FN_PTR(setThreadLocalLong)},2780{CC "registerCompilerPhase", CC "(" STRING ")I", FN_PTR(registerCompilerPhase)},2781{CC "notifyCompilerPhaseEvent", CC "(JIII)V", FN_PTR(notifyCompilerPhaseEvent)},2782{CC "notifyCompilerInliningEvent", CC "(I" HS_RESOLVED_METHOD HS_RESOLVED_METHOD "ZLjava/lang/String;I)V", FN_PTR(notifyCompilerInliningEvent)},2783};27842785int CompilerToVM::methods_count() {2786return sizeof(methods) / sizeof(JNINativeMethod);2787}278827892790