jrose@1145: /* coleenp@4037: * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. jrose@1145: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jrose@1145: * jrose@1145: * This code is free software; you can redistribute it and/or modify it jrose@1145: * under the terms of the GNU General Public License version 2 only, as jrose@1145: * published by the Free Software Foundation. jrose@1145: * jrose@1145: * This code is distributed in the hope that it will be useful, but WITHOUT jrose@1145: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jrose@1145: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jrose@1145: * version 2 for more details (a copy is included in the LICENSE file that jrose@1145: * accompanied this code). jrose@1145: * jrose@1145: * You should have received a copy of the GNU General Public License version jrose@1145: * 2 along with this work; if not, write to the Free Software Foundation, jrose@1145: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jrose@1145: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. jrose@1145: * jrose@1145: */ jrose@1145: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/symbolTable.hpp" jrose@2982: #include "compiler/compileBroker.hpp" stefank@2314: #include "interpreter/interpreter.hpp" never@2920: #include "interpreter/oopMapCache.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "memory/oopFactory.hpp" sspitsyn@4965: #include "prims/jvmtiRedefineClassesTrace.hpp" stefank@2314: #include "prims/methodHandles.hpp" jrose@2982: #include "runtime/compilationPolicy.hpp" stefank@2314: #include "runtime/javaCalls.hpp" stefank@2314: #include "runtime/reflection.hpp" stefank@2314: #include "runtime/signature.hpp" stefank@2314: #include "runtime/stubRoutines.hpp" stefank@2314: twisti@3969: jrose@1145: /* jrose@1145: * JSR 292 reference implementation: method handles twisti@3969: * The JDK 7 reference implementation represented method handle twisti@3969: * combinations as chains. Each link in the chain had a "vmentry" twisti@3969: * field which pointed at a bit of assembly code which performed twisti@3969: * one transformation before dispatching to the next link in the chain. twisti@3969: * twisti@3969: * The current reference implementation pushes almost all code generation twisti@3969: * responsibility to (trusted) Java code. A method handle contains a twisti@3969: * pointer to its "LambdaForm", which embodies all details of the method twisti@3969: * handle's behavior. The LambdaForm is a normal Java object, managed twisti@3969: * by a runtime coded in Java. jrose@1145: */ jrose@1145: jrose@1145: bool MethodHandles::_enabled = false; // set true after successful native linkage never@2950: MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL; twisti@1734: twisti@1734: //------------------------------------------------------------------------------ twisti@1734: // MethodHandles::generate_adapters twisti@1734: // twisti@2436: void MethodHandles::generate_adapters() { twisti@2698: if (!EnableInvokeDynamic || SystemDictionary::MethodHandle_klass() == NULL) return; twisti@1734: twisti@1734: assert(_adapter_code == NULL, "generate only once"); twisti@1734: twisti@1734: ResourceMark rm; twisti@1734: TraceTime timer("MethodHandles adapters generation", TraceStartupTime); never@2950: _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size); twisti@1734: if (_adapter_code == NULL) ccheung@4993: vm_exit_out_of_memory(adapter_code_size, OOM_MALLOC_ERROR, ccheung@4993: "CodeCache: no room for MethodHandles adapters"); never@3255: { never@3255: CodeBuffer code(_adapter_code); never@3255: MethodHandlesAdapterGenerator g(&code); never@3255: g.generate(); never@3255: code.log_section_sizes("MethodHandlesAdapterBlob"); never@3255: } twisti@1734: } twisti@1734: twisti@1734: //------------------------------------------------------------------------------ twisti@1734: // MethodHandlesAdapterGenerator::generate twisti@1734: // twisti@2436: void MethodHandlesAdapterGenerator::generate() { twisti@1734: // Generate generic method handle adapters. twisti@3969: // Generate interpreter entries twisti@3969: for (Interpreter::MethodKind mk = Interpreter::method_handle_invoke_FIRST; twisti@3969: mk <= Interpreter::method_handle_invoke_LAST; twisti@3969: mk = Interpreter::MethodKind(1 + (int)mk)) { twisti@3969: vmIntrinsics::ID iid = Interpreter::method_handle_intrinsic(mk); twisti@3969: StubCodeMark mark(this, "MethodHandle::interpreter_entry", vmIntrinsics::name_at(iid)); twisti@3969: address entry = MethodHandles::generate_method_handle_interpreter_entry(_masm, iid); twisti@3969: if (entry != NULL) { twisti@3969: Interpreter::set_entry_for_kind(mk, entry); never@2895: } twisti@3969: // If the entry is not set, it will throw AbstractMethodError. twisti@1734: } twisti@1734: } twisti@1734: jrose@1145: void MethodHandles::set_enabled(bool z) { jrose@1145: if (_enabled != z) { twisti@2698: guarantee(z && EnableInvokeDynamic, "can only enable once, and only if -XX:+EnableInvokeDynamic"); jrose@1145: _enabled = z; jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // MemberName support jrose@1145: jrose@2639: // import java_lang_invoke_MemberName.* jrose@1145: enum { twisti@4866: IS_METHOD = java_lang_invoke_MemberName::MN_IS_METHOD, twisti@4866: IS_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR, twisti@4866: IS_FIELD = java_lang_invoke_MemberName::MN_IS_FIELD, twisti@4866: IS_TYPE = java_lang_invoke_MemberName::MN_IS_TYPE, twisti@4866: CALLER_SENSITIVE = java_lang_invoke_MemberName::MN_CALLER_SENSITIVE, twisti@3969: REFERENCE_KIND_SHIFT = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT, twisti@3969: REFERENCE_KIND_MASK = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK, twisti@4866: SEARCH_SUPERCLASSES = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES, twisti@4866: SEARCH_INTERFACES = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES, twisti@3969: ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE jrose@1145: }; jrose@1145: jrose@1862: Handle MethodHandles::new_MemberName(TRAPS) { jrose@1862: Handle empty; jrose@1862: instanceKlassHandle k(THREAD, SystemDictionary::MemberName_klass()); jrose@1862: if (!k->is_initialized()) k->initialize(CHECK_(empty)); jrose@1862: return Handle(THREAD, k->allocate_instance(THREAD)); jrose@1862: } jrose@1862: sspitsyn@4965: oop MethodHandles::init_MemberName(Handle mname, Handle target) { sspitsyn@4965: Thread* thread = Thread::current(); sspitsyn@4965: oop target_oop = target(); coleenp@4037: Klass* target_klass = target_oop->klass(); twisti@3969: if (target_klass == SystemDictionary::reflect_Field_klass()) { jrose@1145: oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder() jrose@1145: int slot = java_lang_reflect_Field::slot(target_oop); // fd.index() jrose@1145: int mods = java_lang_reflect_Field::modifiers(target_oop); twisti@3969: oop type = java_lang_reflect_Field::type(target_oop); twisti@3969: oop name = java_lang_reflect_Field::name(target_oop); sspitsyn@4965: KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); sspitsyn@4965: intptr_t offset = InstanceKlass::cast(k())->field_offset(slot); sspitsyn@4965: return init_field_MemberName(mname, k, accessFlags_from(mods), type, name, offset); twisti@3969: } else if (target_klass == SystemDictionary::reflect_Method_klass()) { twisti@3969: oop clazz = java_lang_reflect_Method::clazz(target_oop); twisti@3969: int slot = java_lang_reflect_Method::slot(target_oop); sspitsyn@4965: KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); sspitsyn@4965: if (!k.is_null() && k->oop_is_instance()) { sspitsyn@4965: Method* m = InstanceKlass::cast(k())->method_with_idnum(slot); sspitsyn@4965: return init_method_MemberName(mname, m, true, k); twisti@3969: } twisti@3969: } else if (target_klass == SystemDictionary::reflect_Constructor_klass()) { twisti@3969: oop clazz = java_lang_reflect_Constructor::clazz(target_oop); twisti@3969: int slot = java_lang_reflect_Constructor::slot(target_oop); sspitsyn@4965: KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); sspitsyn@4965: if (!k.is_null() && k->oop_is_instance()) { sspitsyn@4965: Method* m = InstanceKlass::cast(k())->method_with_idnum(slot); sspitsyn@4965: return init_method_MemberName(mname, m, false, k); twisti@3969: } twisti@3969: } else if (target_klass == SystemDictionary::MemberName_klass()) { twisti@3969: // Note: This only works if the MemberName has already been resolved. twisti@3969: oop clazz = java_lang_invoke_MemberName::clazz(target_oop); twisti@3969: int flags = java_lang_invoke_MemberName::flags(target_oop); coleenp@4037: Metadata* vmtarget=java_lang_invoke_MemberName::vmtarget(target_oop); twisti@3969: intptr_t vmindex = java_lang_invoke_MemberName::vmindex(target_oop); sspitsyn@4965: KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); twisti@3969: int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; twisti@3969: if (vmtarget == NULL) return NULL; // not resolved twisti@3969: if ((flags & IS_FIELD) != 0) { coleenp@4037: assert(vmtarget->is_klass(), "field vmtarget is Klass*"); twisti@3969: int basic_mods = (ref_kind_is_static(ref_kind) ? JVM_ACC_STATIC : 0); twisti@3969: // FIXME: how does k (receiver_limit) contribute? sspitsyn@4965: KlassHandle k_vmtarget(thread, (Klass*)vmtarget); sspitsyn@4965: return init_field_MemberName(mname, k_vmtarget, accessFlags_from(basic_mods), NULL, NULL, vmindex); twisti@3969: } else if ((flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) { coleenp@4037: assert(vmtarget->is_method(), "method or constructor vmtarget is Method*"); sspitsyn@4965: return init_method_MemberName(mname, (Method*)vmtarget, ref_kind_does_dispatch(ref_kind), k); twisti@3969: } else { twisti@3969: return NULL; twisti@3969: } jrose@1145: } twisti@3969: return NULL; jrose@1145: } jrose@1145: sspitsyn@4965: oop MethodHandles::init_method_MemberName(Handle mname, Method* m, bool do_dispatch, sspitsyn@4965: KlassHandle receiver_limit_h) { sspitsyn@4965: Klass* receiver_limit = receiver_limit_h(); twisti@3969: AccessFlags mods = m->access_flags(); twisti@3969: int flags = (jushort)( mods.as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS ); coleenp@4037: int vmindex = Method::nonvirtual_vtable_index; // implies never any dispatch coleenp@4037: Klass* mklass = m->method_holder(); twisti@3969: if (receiver_limit == NULL) twisti@3969: receiver_limit = mklass; twisti@3969: if (m->is_initializer()) { twisti@3969: flags |= IS_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); twisti@3969: } else if (mods.is_static()) { twisti@3969: flags |= IS_METHOD | (JVM_REF_invokeStatic << REFERENCE_KIND_SHIFT); bharadwaj@4969: // Get vindex from itable if method holder is an interface. bharadwaj@4969: if (m->method_holder()->is_interface()) { bharadwaj@4960: vmindex = klassItable::compute_itable_index(m); bharadwaj@4960: } twisti@3969: } else if (receiver_limit != mklass && hseigel@4278: !receiver_limit->is_subtype_of(mklass)) { twisti@3969: return NULL; // bad receiver limit hseigel@4278: } else if (receiver_limit->is_interface() && hseigel@4278: mklass->is_interface()) { twisti@3969: flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT); twisti@3969: receiver_limit = mklass; // ignore passed-in limit; interfaces are interconvertible twisti@3969: vmindex = klassItable::compute_itable_index(m); hseigel@4278: } else if (mklass != receiver_limit && mklass->is_interface()) { twisti@3969: flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); twisti@3969: // it is a miranda method, so m->vtable_index is not what we want twisti@3969: ResourceMark rm; coleenp@4037: klassVtable* vt = InstanceKlass::cast(receiver_limit)->vtable(); twisti@3969: vmindex = vt->index_of_miranda(m->name(), m->signature()); twisti@3969: } else if (!do_dispatch || m->can_be_statically_bound()) { twisti@3969: flags |= IS_METHOD | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); twisti@3969: } else { twisti@3969: flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); twisti@3969: vmindex = m->vtable_index(); twisti@3969: } twisti@3969: twisti@4866: // @CallerSensitive annotation detected twisti@4866: if (m->caller_sensitive()) { twisti@4866: flags |= CALLER_SENSITIVE; twisti@4866: } twisti@4866: sspitsyn@4965: oop mname_oop = mname(); twisti@4866: java_lang_invoke_MemberName::set_flags( mname_oop, flags); twisti@3969: java_lang_invoke_MemberName::set_vmtarget(mname_oop, m); twisti@4866: java_lang_invoke_MemberName::set_vmindex( mname_oop, vmindex); // vtable/itable index twisti@4866: java_lang_invoke_MemberName::set_clazz( mname_oop, receiver_limit->java_mirror()); twisti@3969: // Note: name and type can be lazily computed by resolve_MemberName, twisti@3969: // if Java code needs them as resolved String and MethodType objects. twisti@3969: // The clazz must be eagerly stored, because it provides a GC coleenp@4037: // root to help keep alive the Method*. twisti@3969: // If relevant, the vtable or itable value is stored as vmindex. twisti@3969: // This is done eagerly, since it is readily available without twisti@3969: // constructing any new objects. twisti@3969: // TO DO: maybe intern mname_oop sspitsyn@4965: m->method_holder()->add_member_name(mname); sspitsyn@4965: return mname(); twisti@3969: } twisti@3969: sspitsyn@4965: Handle MethodHandles::init_method_MemberName(Handle mname, CallInfo& info, TRAPS) { twisti@3969: Handle empty; twisti@3969: if (info.resolved_appendix().not_null()) { twisti@3969: // The resolved MemberName must not be accompanied by an appendix argument, twisti@3969: // since there is no way to bind this value into the MemberName. twisti@3969: // Caller is responsible to prevent this from happening. twisti@3969: THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty); twisti@3969: } twisti@3969: methodHandle m = info.resolved_method(); twisti@3969: KlassHandle defc = info.resolved_klass(); twisti@3969: int vmindex = -1; coleenp@4251: if (defc->is_interface() && m->method_holder()->is_interface()) { twisti@3969: // LinkResolver does not report itable indexes! (fix this?) twisti@3969: vmindex = klassItable::compute_itable_index(m()); twisti@3969: } else if (m->can_be_statically_bound()) { twisti@3969: // LinkResolver reports vtable index even for final methods! coleenp@4037: vmindex = Method::nonvirtual_vtable_index; twisti@3969: } else { twisti@3969: vmindex = info.vtable_index(); twisti@3969: } sspitsyn@4965: oop res = init_method_MemberName(mname, m(), (vmindex >= 0), defc()); twisti@3969: assert(res == NULL || (java_lang_invoke_MemberName::vmindex(res) == vmindex), ""); twisti@3969: return Handle(THREAD, res); twisti@3969: } twisti@3969: sspitsyn@4965: oop MethodHandles::init_field_MemberName(Handle mname, KlassHandle field_holder, twisti@3969: AccessFlags mods, oop type, oop name, twisti@3969: intptr_t offset, bool is_setter) { twisti@3969: int flags = (jushort)( mods.as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS ); twisti@3969: flags |= IS_FIELD | ((mods.is_static() ? JVM_REF_getStatic : JVM_REF_getField) << REFERENCE_KIND_SHIFT); twisti@3969: if (is_setter) flags += ((JVM_REF_putField - JVM_REF_getField) << REFERENCE_KIND_SHIFT); sspitsyn@4965: Metadata* vmtarget = field_holder(); twisti@3969: int vmindex = offset; // determines the field uniquely when combined with static bit sspitsyn@4965: oop mname_oop = mname(); twisti@3969: java_lang_invoke_MemberName::set_flags(mname_oop, flags); jrose@2639: java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget); jrose@2639: java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); hseigel@4278: java_lang_invoke_MemberName::set_clazz(mname_oop, field_holder->java_mirror()); twisti@3969: if (name != NULL) twisti@3969: java_lang_invoke_MemberName::set_name(mname_oop, name); twisti@3969: if (type != NULL) twisti@3969: java_lang_invoke_MemberName::set_type(mname_oop, type); twisti@3969: // Note: name and type can be lazily computed by resolve_MemberName, twisti@3969: // if Java code needs them as resolved String and Class objects. twisti@3969: // Note that the incoming type oop might be pre-resolved (non-null). twisti@3969: // The base clazz and field offset (vmindex) must be eagerly stored, twisti@3969: // because they unambiguously identify the field. twisti@3969: // Although the fieldDescriptor::_index would also identify the field, twisti@3969: // we do not use it, because it is harder to decode. twisti@3969: // TO DO: maybe intern mname_oop sspitsyn@4965: InstanceKlass::cast(field_holder())->add_member_name(mname); sspitsyn@4965: return mname(); jrose@1145: } jrose@1145: sspitsyn@4965: Handle MethodHandles::init_field_MemberName(Handle mname, FieldAccessInfo& info, TRAPS) { twisti@3969: return Handle(); coleenp@4037: #if 0 // FIXME twisti@3969: KlassHandle field_holder = info.klass(); twisti@3969: intptr_t field_offset = info.field_offset(); twisti@3969: return init_field_MemberName(mname_oop, field_holder(), twisti@3969: info.access_flags(), twisti@3969: type, name, twisti@3969: field_offset, false /*is_setter*/); twisti@3969: #endif jrose@1145: } jrose@1145: jrose@1145: twisti@3969: // JVM 2.9 Special Methods: twisti@3969: // A method is signature polymorphic if and only if all of the following conditions hold : twisti@3969: // * It is declared in the java.lang.invoke.MethodHandle class. twisti@3969: // * It has a single formal parameter of type Object[]. twisti@3969: // * It has a return type of Object. twisti@3969: // * It has the ACC_VARARGS and ACC_NATIVE flags set. coleenp@4037: bool MethodHandles::is_method_handle_invoke_name(Klass* klass, Symbol* name) { twisti@3969: if (klass == NULL) twisti@3969: return false; twisti@3969: // The following test will fail spuriously during bootstrap of MethodHandle itself: twisti@3969: // if (klass != SystemDictionary::MethodHandle_klass()) twisti@3969: // Test the name instead: hseigel@4278: if (klass->name() != vmSymbols::java_lang_invoke_MethodHandle()) twisti@3969: return false; twisti@3969: Symbol* poly_sig = vmSymbols::object_array_object_signature(); coleenp@4037: Method* m = InstanceKlass::cast(klass)->find_method(name, poly_sig); twisti@3969: if (m == NULL) return false; twisti@3969: int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS; twisti@3969: int flags = m->access_flags().as_int(); twisti@3969: return (flags & required) == required; jrose@1145: } jrose@1145: twisti@3969: twisti@3969: Symbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) { twisti@3969: assert(is_signature_polymorphic_intrinsic(iid), err_msg("iid=%d", iid)); twisti@3969: switch (iid) { twisti@3969: case vmIntrinsics::_invokeBasic: return vmSymbols::invokeBasic_name(); twisti@3969: case vmIntrinsics::_linkToVirtual: return vmSymbols::linkToVirtual_name(); twisti@3969: case vmIntrinsics::_linkToStatic: return vmSymbols::linkToStatic_name(); twisti@3969: case vmIntrinsics::_linkToSpecial: return vmSymbols::linkToSpecial_name(); twisti@3969: case vmIntrinsics::_linkToInterface: return vmSymbols::linkToInterface_name(); twisti@3969: } twisti@3969: assert(false, ""); twisti@3969: return 0; twisti@3969: } twisti@3969: twisti@3969: int MethodHandles::signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid) { twisti@3969: switch (iid) { twisti@3969: case vmIntrinsics::_invokeBasic: return 0; twisti@3969: case vmIntrinsics::_linkToVirtual: return JVM_REF_invokeVirtual; twisti@3969: case vmIntrinsics::_linkToStatic: return JVM_REF_invokeStatic; twisti@3969: case vmIntrinsics::_linkToSpecial: return JVM_REF_invokeSpecial; twisti@3969: case vmIntrinsics::_linkToInterface: return JVM_REF_invokeInterface; twisti@3969: } twisti@3969: assert(false, err_msg("iid=%d", iid)); twisti@3969: return 0; twisti@3969: } twisti@3969: twisti@3969: vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Symbol* name) { twisti@3969: vmSymbols::SID name_id = vmSymbols::find_sid(name); twisti@3969: switch (name_id) { twisti@3969: // The ID _invokeGeneric stands for all non-static signature-polymorphic methods, except built-ins. twisti@3969: case vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name): return vmIntrinsics::_invokeGeneric; twisti@3969: // The only built-in non-static signature-polymorphic method is MethodHandle.invokeBasic: twisti@3969: case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeBasic_name): return vmIntrinsics::_invokeBasic; twisti@3969: twisti@3969: // There is one static signature-polymorphic method for each JVM invocation mode. twisti@3969: case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToVirtual_name): return vmIntrinsics::_linkToVirtual; twisti@3969: case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToStatic_name): return vmIntrinsics::_linkToStatic; twisti@3969: case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToSpecial_name): return vmIntrinsics::_linkToSpecial; twisti@3969: case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToInterface_name): return vmIntrinsics::_linkToInterface; twisti@3969: } twisti@3969: twisti@3969: // Cover the case of invokeExact and any future variants of invokeFoo. coleenp@4037: Klass* mh_klass = SystemDictionary::well_known_klass( twisti@3969: SystemDictionary::WK_KLASS_ENUM_NAME(MethodHandle_klass) ); twisti@3969: if (mh_klass != NULL && is_method_handle_invoke_name(mh_klass, name)) twisti@3969: return vmIntrinsics::_invokeGeneric; twisti@3969: twisti@3969: // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against. twisti@3969: // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic. twisti@3969: return vmIntrinsics::_none; twisti@3969: } twisti@3969: coleenp@4037: vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) { twisti@3969: if (klass != NULL && hseigel@4278: klass->name() == vmSymbols::java_lang_invoke_MethodHandle()) { twisti@3969: vmIntrinsics::ID iid = signature_polymorphic_name_id(name); twisti@3969: if (iid != vmIntrinsics::_none) twisti@3969: return iid; twisti@3969: if (is_method_handle_invoke_name(klass, name)) twisti@3969: return vmIntrinsics::_invokeGeneric; twisti@3969: } twisti@3969: return vmIntrinsics::_none; twisti@3969: } twisti@3969: twisti@3969: coleenp@2497: // convert the external string or reflective type to an internal signature twisti@3969: Symbol* MethodHandles::lookup_signature(oop type_str, bool intern_if_not_found, TRAPS) { jrose@2639: if (java_lang_invoke_MethodType::is_instance(type_str)) { twisti@3969: return java_lang_invoke_MethodType::as_signature(type_str, intern_if_not_found, CHECK_NULL); coleenp@2497: } else if (java_lang_Class::is_instance(type_str)) { coleenp@2497: return java_lang_Class::as_signature(type_str, false, CHECK_NULL); coleenp@2497: } else if (java_lang_String::is_instance(type_str)) { twisti@3969: if (intern_if_not_found) { coleenp@2497: return java_lang_String::as_symbol(type_str, CHECK_NULL); coleenp@2497: } else { coleenp@2497: return java_lang_String::as_symbol_or_null(type_str); coleenp@2497: } coleenp@2497: } else { coleenp@2497: THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized type", NULL); coleenp@2497: } coleenp@2497: } coleenp@2497: twisti@3969: static const char OBJ_SIG[] = "Ljava/lang/Object;"; twisti@3969: enum { OBJ_SIG_LEN = 18 }; twisti@3969: twisti@3969: bool MethodHandles::is_basic_type_signature(Symbol* sig) { twisti@3969: assert(vmSymbols::object_signature()->utf8_length() == (int)OBJ_SIG_LEN, ""); twisti@3969: assert(vmSymbols::object_signature()->equals(OBJ_SIG), ""); twisti@3969: const int len = sig->utf8_length(); twisti@3969: for (int i = 0; i < len; i++) { twisti@3969: switch (sig->byte_at(i)) { twisti@3969: case 'L': twisti@3969: // only java/lang/Object is valid here twisti@3969: if (sig->index_of_at(i, OBJ_SIG, OBJ_SIG_LEN) != i) twisti@3969: return false; twisti@3969: i += OBJ_SIG_LEN-1; //-1 because of i++ in loop twisti@3969: continue; twisti@3969: case '(': case ')': case 'V': twisti@3969: case 'I': case 'J': case 'F': case 'D': twisti@3969: continue; twisti@3969: //case '[': twisti@3969: //case 'Z': case 'B': case 'C': case 'S': twisti@3969: default: twisti@3969: return false; twisti@3969: } twisti@3969: } twisti@3969: return true; twisti@3969: } twisti@3969: twisti@3969: Symbol* MethodHandles::lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS) { twisti@3969: Symbol* bsig = NULL; twisti@3969: if (sig == NULL) { twisti@3969: return sig; twisti@3969: } else if (is_basic_type_signature(sig)) { twisti@3969: sig->increment_refcount(); twisti@3969: return sig; // that was easy twisti@3969: } else if (sig->byte_at(0) != '(') { twisti@3969: BasicType bt = char2type(sig->byte_at(0)); twisti@3969: if (is_subword_type(bt)) { twisti@3969: bsig = vmSymbols::int_signature(); twisti@3969: } else { twisti@3969: assert(bt == T_OBJECT || bt == T_ARRAY, "is_basic_type_signature was false"); twisti@3969: bsig = vmSymbols::object_signature(); twisti@3969: } twisti@3969: } else { twisti@3969: ResourceMark rm; twisti@3969: stringStream buffer(128); twisti@3969: buffer.put('('); twisti@3969: int arg_pos = 0, keep_arg_pos = -1; twisti@3969: if (keep_last_arg) twisti@3969: keep_arg_pos = ArgumentCount(sig).size() - 1; twisti@3969: for (SignatureStream ss(sig); !ss.is_done(); ss.next()) { twisti@3969: BasicType bt = ss.type(); twisti@3969: size_t this_arg_pos = buffer.size(); twisti@3969: if (ss.at_return_type()) { twisti@3969: buffer.put(')'); twisti@3969: } twisti@3969: if (arg_pos == keep_arg_pos) { twisti@3969: buffer.write((char*) ss.raw_bytes(), twisti@3969: (int) ss.raw_length()); twisti@3969: } else if (bt == T_OBJECT || bt == T_ARRAY) { twisti@3969: buffer.write(OBJ_SIG, OBJ_SIG_LEN); twisti@3969: } else { twisti@3969: if (is_subword_type(bt)) twisti@3969: bt = T_INT; twisti@3969: buffer.put(type2char(bt)); twisti@3969: } twisti@3969: arg_pos++; twisti@3969: } twisti@3969: const char* sigstr = buffer.base(); twisti@3969: int siglen = (int) buffer.size(); twisti@3969: bsig = SymbolTable::new_symbol(sigstr, siglen, THREAD); twisti@3969: } twisti@3969: assert(is_basic_type_signature(bsig) || twisti@3969: // detune assert in case the injected argument is not a basic type: twisti@3969: keep_last_arg, ""); twisti@3969: return bsig; twisti@3969: } twisti@3969: twisti@3969: void MethodHandles::print_as_basic_type_signature_on(outputStream* st, twisti@3969: Symbol* sig, twisti@3969: bool keep_arrays, twisti@3969: bool keep_basic_names) { twisti@3969: st = st ? st : tty; twisti@3969: int len = sig->utf8_length(); twisti@3969: int array = 0; twisti@3969: bool prev_type = false; twisti@3969: for (int i = 0; i < len; i++) { twisti@3969: char ch = sig->byte_at(i); twisti@3969: switch (ch) { twisti@3969: case '(': case ')': twisti@3969: prev_type = false; twisti@3969: st->put(ch); twisti@3969: continue; twisti@3969: case '[': twisti@3969: if (!keep_basic_names && keep_arrays) twisti@3969: st->put(ch); twisti@3969: array++; twisti@3969: continue; twisti@3969: case 'L': twisti@3969: { twisti@3969: if (prev_type) st->put(','); twisti@3969: int start = i+1, slash = start; twisti@3969: while (++i < len && (ch = sig->byte_at(i)) != ';') { twisti@3969: if (ch == '/' || ch == '.' || ch == '$') slash = i+1; twisti@3969: } twisti@3969: if (slash < i) start = slash; twisti@3969: if (!keep_basic_names) { twisti@3969: st->put('L'); twisti@3969: } else { twisti@3969: for (int j = start; j < i; j++) twisti@3969: st->put(sig->byte_at(j)); twisti@3969: prev_type = true; twisti@3969: } twisti@3969: break; twisti@3969: } twisti@3969: default: twisti@3969: { twisti@3969: if (array && char2type(ch) != T_ILLEGAL && !keep_arrays) { twisti@3969: ch = '['; twisti@3969: array = 0; twisti@3969: } twisti@3969: if (prev_type) st->put(','); twisti@3969: const char* n = NULL; twisti@3969: if (keep_basic_names) twisti@3969: n = type2name(char2type(ch)); twisti@3969: if (n == NULL) { twisti@3969: // unknown letter, or we don't want to know its name twisti@3969: st->put(ch); twisti@3969: } else { twisti@3969: st->print(n); twisti@3969: prev_type = true; twisti@3969: } twisti@3969: break; twisti@3969: } twisti@3969: } twisti@3969: // Switch break goes here to take care of array suffix: twisti@3969: if (prev_type) { twisti@3969: while (array > 0) { twisti@3969: st->print("[]"); twisti@3969: --array; twisti@3969: } twisti@3969: } twisti@3969: array = 0; twisti@3969: } twisti@3969: } twisti@3969: twisti@3969: twisti@3969: twisti@3969: static oop object_java_mirror() { hseigel@4278: return SystemDictionary::Object_klass()->java_mirror(); twisti@3969: } twisti@3969: twisti@3969: static oop field_name_or_null(Symbol* s) { twisti@3969: if (s == NULL) return NULL; twisti@3969: return StringTable::lookup(s); twisti@3969: } twisti@3969: twisti@3969: static oop field_signature_type_or_null(Symbol* s) { twisti@3969: if (s == NULL) return NULL; twisti@3969: BasicType bt = FieldType::basic_type(s); twisti@3969: if (is_java_primitive(bt)) { twisti@3969: assert(s->utf8_length() == 1, ""); twisti@3969: return java_lang_Class::primitive_mirror(bt); twisti@3969: } twisti@3969: // Here are some more short cuts for common types. twisti@3969: // They are optional, since reference types can be resolved lazily. twisti@3969: if (bt == T_OBJECT) { twisti@3969: if (s == vmSymbols::object_signature()) { twisti@3969: return object_java_mirror(); twisti@3969: } else if (s == vmSymbols::class_signature()) { hseigel@4278: return SystemDictionary::Class_klass()->java_mirror(); twisti@3969: } else if (s == vmSymbols::string_signature()) { hseigel@4278: return SystemDictionary::String_klass()->java_mirror(); twisti@3969: } twisti@3969: } twisti@3969: return NULL; twisti@3969: } twisti@3969: coleenp@4037: jrose@1145: // An unresolved member name is a mere symbolic reference. jrose@1145: // Resolving it plants a vmtarget/vmindex in it, coleenp@4037: // which refers directly to JVM internals. twisti@3969: Handle MethodHandles::resolve_MemberName(Handle mname, TRAPS) { twisti@3969: Handle empty; jrose@2639: assert(java_lang_invoke_MemberName::is_instance(mname()), ""); twisti@3969: twisti@3969: if (java_lang_invoke_MemberName::vmtarget(mname()) != NULL) { twisti@3969: // Already resolved. twisti@3969: DEBUG_ONLY(int vmindex = java_lang_invoke_MemberName::vmindex(mname())); coleenp@4037: assert(vmindex >= Method::nonvirtual_vtable_index, ""); twisti@3969: return mname; twisti@3969: } twisti@3969: twisti@2806: Handle defc_oop(THREAD, java_lang_invoke_MemberName::clazz(mname())); twisti@2806: Handle name_str(THREAD, java_lang_invoke_MemberName::name( mname())); twisti@2806: Handle type_str(THREAD, java_lang_invoke_MemberName::type( mname())); twisti@2806: int flags = java_lang_invoke_MemberName::flags(mname()); twisti@3969: int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; twisti@3969: if (!ref_kind_is_valid(ref_kind)) { twisti@3969: THROW_MSG_(vmSymbols::java_lang_InternalError(), "obsolete MemberName format", empty); twisti@3969: } twisti@3969: twisti@3969: DEBUG_ONLY(int old_vmindex); twisti@3969: assert((old_vmindex = java_lang_invoke_MemberName::vmindex(mname())) == 0, "clean input"); jrose@1145: twisti@2806: if (defc_oop.is_null() || name_str.is_null() || type_str.is_null()) { twisti@3969: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve", empty); jrose@1145: } twisti@2806: twisti@2806: instanceKlassHandle defc; twisti@2806: { coleenp@4037: Klass* defc_klass = java_lang_Class::as_Klass(defc_oop()); coleenp@4037: if (defc_klass == NULL) return empty; // a primitive; no resolution possible hseigel@4278: if (!defc_klass->oop_is_instance()) { hseigel@4278: if (!defc_klass->oop_is_array()) return empty; coleenp@4037: defc_klass = SystemDictionary::Object_klass(); twisti@2806: } coleenp@4037: defc = instanceKlassHandle(THREAD, defc_klass); jrose@1145: } jrose@1145: if (defc.is_null()) { twisti@3969: THROW_MSG_(vmSymbols::java_lang_InternalError(), "primitive class", empty); jrose@1145: } twisti@3969: defc->link_class(CHECK_(empty)); // possible safepoint jrose@1145: jrose@1145: // convert the external string name to an internal symbol twisti@2806: TempNewSymbol name = java_lang_String::as_symbol_or_null(name_str()); twisti@3969: if (name == NULL) return empty; // no such name never@2978: if (name == vmSymbols::class_initializer_name()) twisti@3969: return empty; // illegal name jrose@1145: twisti@3969: vmIntrinsics::ID mh_invoke_id = vmIntrinsics::_none; jrose@1863: if ((flags & ALL_KINDS) == IS_METHOD && twisti@3969: (defc() == SystemDictionary::MethodHandle_klass()) && twisti@3969: (ref_kind == JVM_REF_invokeVirtual || twisti@3969: ref_kind == JVM_REF_invokeSpecial || twisti@3969: // static invocation mode is required for _linkToVirtual, etc.: twisti@3969: ref_kind == JVM_REF_invokeStatic)) { twisti@3969: vmIntrinsics::ID iid = signature_polymorphic_name_id(name); twisti@3969: if (iid != vmIntrinsics::_none && twisti@3969: ((ref_kind == JVM_REF_invokeStatic) == is_signature_polymorphic_static(iid))) { twisti@3969: // Virtual methods invoke and invokeExact, plus internal invokers like _invokeBasic. twisti@3969: // For a static reference it could an internal linkage routine like _linkToVirtual, etc. twisti@3969: mh_invoke_id = iid; twisti@3969: } twisti@2806: } jrose@1863: jrose@1145: // convert the external string or reflective type to an internal signature twisti@3969: TempNewSymbol type = lookup_signature(type_str(), (mh_invoke_id != vmIntrinsics::_none), CHECK_(empty)); twisti@3969: if (type == NULL) return empty; // no such signature exists in the VM jrose@1145: jrose@1145: // Time to do the lookup. jrose@1145: switch (flags & ALL_KINDS) { jrose@1145: case IS_METHOD: jrose@1145: { jrose@1145: CallInfo result; twisti@3969: bool do_dispatch = true; // default, neutral setting jrose@1145: { twisti@3969: assert(!HAS_PENDING_EXCEPTION, ""); twisti@3969: if (ref_kind == JVM_REF_invokeStatic) { twisti@3969: //do_dispatch = false; // no need, since statics are never dispatched jrose@1145: LinkResolver::resolve_static_call(result, jrose@1145: defc, name, type, KlassHandle(), false, false, THREAD); twisti@3969: } else if (ref_kind == JVM_REF_invokeInterface) { jrose@1145: LinkResolver::resolve_interface_call(result, Handle(), defc, jrose@1145: defc, name, type, KlassHandle(), false, false, THREAD); twisti@3969: } else if (mh_invoke_id != vmIntrinsics::_none) { twisti@3969: assert(!is_signature_polymorphic_static(mh_invoke_id), ""); twisti@3969: LinkResolver::resolve_handle_call(result, twisti@3969: defc, name, type, KlassHandle(), THREAD); twisti@3969: } else if (ref_kind == JVM_REF_invokeSpecial) { twisti@3969: do_dispatch = false; // force non-virtual linkage twisti@3969: LinkResolver::resolve_special_call(result, twisti@3969: defc, name, type, KlassHandle(), false, THREAD); twisti@3969: } else if (ref_kind == JVM_REF_invokeVirtual) { jrose@1145: LinkResolver::resolve_virtual_call(result, Handle(), defc, jrose@1145: defc, name, type, KlassHandle(), false, false, THREAD); twisti@3969: } else { twisti@3969: assert(false, err_msg("ref_kind=%d", ref_kind)); jrose@1145: } jrose@1145: if (HAS_PENDING_EXCEPTION) { twisti@3969: return empty; jrose@1145: } jrose@1145: } sspitsyn@4965: return init_method_MemberName(mname, result, THREAD); jrose@1145: } jrose@1145: case IS_CONSTRUCTOR: jrose@1145: { jrose@1145: CallInfo result; jrose@1145: { twisti@3969: assert(!HAS_PENDING_EXCEPTION, ""); coleenp@2497: if (name == vmSymbols::object_initializer_name()) { jrose@1145: LinkResolver::resolve_special_call(result, jrose@1145: defc, name, type, KlassHandle(), false, THREAD); jrose@1145: } else { jrose@1145: break; // will throw after end of switch jrose@1145: } jrose@1145: if (HAS_PENDING_EXCEPTION) { twisti@3969: return empty; jrose@1145: } jrose@1145: } jrose@1145: assert(result.is_statically_bound(), ""); sspitsyn@4965: return init_method_MemberName(mname, result, THREAD); jrose@1145: } jrose@1145: case IS_FIELD: jrose@1145: { jrose@1145: // This is taken from LinkResolver::resolve_field, sans access checks. jrose@1145: fieldDescriptor fd; // find_field initializes fd if found coleenp@4037: KlassHandle sel_klass(THREAD, InstanceKlass::cast(defc())->find_field(name, type, &fd)); jrose@1145: // check if field exists; i.e., if a klass containing the field def has been selected twisti@3969: if (sel_klass.is_null()) return empty; // should not happen twisti@3969: oop type = field_signature_type_or_null(fd.signature()); twisti@3969: oop name = field_name_or_null(fd.name()); twisti@3969: bool is_setter = (ref_kind_is_valid(ref_kind) && ref_kind_is_setter(ref_kind)); twisti@3969: mname = Handle(THREAD, sspitsyn@4965: init_field_MemberName(mname, sel_klass, twisti@3969: fd.access_flags(), type, name, fd.offset(), is_setter)); twisti@3969: return mname; jrose@1145: } jrose@1863: default: twisti@3969: THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty); jrose@1145: } jrose@1863: twisti@3969: return empty; jrose@1145: } jrose@1145: jrose@1145: // Conversely, a member name which is only initialized from JVM internals jrose@1145: // may have null defc, name, and type fields. jrose@1145: // Resolving it plants a vmtarget/vmindex in it, jrose@1145: // which refers directly to JVM internals. jrose@1145: void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) { jrose@2639: assert(java_lang_invoke_MemberName::is_instance(mname()), ""); coleenp@4037: Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); jrose@2639: int vmindex = java_lang_invoke_MemberName::vmindex(mname()); twisti@3969: if (vmtarget == NULL) { jrose@1145: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand"); jrose@1145: } jrose@1145: jrose@2639: bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL); jrose@2639: bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL); jrose@2639: bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL); jrose@2639: int flags = java_lang_invoke_MemberName::flags(mname()); jrose@1145: jrose@1145: if (suppress != 0) { jrose@1145: if (suppress & _suppress_defc) have_defc = true; jrose@1145: if (suppress & _suppress_name) have_name = true; jrose@1145: if (suppress & _suppress_type) have_type = true; jrose@1145: } jrose@1145: jrose@1145: if (have_defc && have_name && have_type) return; // nothing needed jrose@1145: jrose@1145: switch (flags & ALL_KINDS) { jrose@1145: case IS_METHOD: jrose@1145: case IS_CONSTRUCTOR: jrose@1145: { coleenp@4037: assert(vmtarget->is_method(), "method or constructor vmtarget is Method*"); coleenp@4037: methodHandle m(THREAD, (Method*)vmtarget); twisti@3969: DEBUG_ONLY(vmtarget = NULL); // safety jrose@1145: if (m.is_null()) break; jrose@1145: if (!have_defc) { coleenp@4251: InstanceKlass* defc = m->method_holder(); coleenp@4251: java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); jrose@1145: } jrose@1145: if (!have_name) { jrose@1145: //not java_lang_String::create_from_symbol; let's intern member names jrose@1145: Handle name = StringTable::intern(m->name(), CHECK); jrose@2639: java_lang_invoke_MemberName::set_name(mname(), name()); jrose@1145: } jrose@1145: if (!have_type) { jrose@1145: Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK); jrose@2639: java_lang_invoke_MemberName::set_type(mname(), type()); jrose@1145: } jrose@1145: return; jrose@1145: } jrose@1145: case IS_FIELD: jrose@1145: { jrose@1145: // This is taken from LinkResolver::resolve_field, sans access checks. coleenp@4037: assert(vmtarget->is_klass(), "field vmtarget is Klass*"); hseigel@4278: if (!((Klass*) vmtarget)->oop_is_instance()) break; coleenp@4037: instanceKlassHandle defc(THREAD, (Klass*) vmtarget); twisti@3969: DEBUG_ONLY(vmtarget = NULL); // safety jrose@1145: bool is_static = ((flags & JVM_ACC_STATIC) != 0); jrose@1145: fieldDescriptor fd; // find_field initializes fd if found jrose@1145: if (!defc->find_field_from_offset(vmindex, is_static, &fd)) jrose@1145: break; // cannot expand jrose@1145: if (!have_defc) { jrose@2639: java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); jrose@1145: } jrose@1145: if (!have_name) { jrose@1145: //not java_lang_String::create_from_symbol; let's intern member names jrose@1145: Handle name = StringTable::intern(fd.name(), CHECK); jrose@2639: java_lang_invoke_MemberName::set_name(mname(), name()); jrose@1145: } jrose@1145: if (!have_type) { twisti@3969: // If it is a primitive field type, don't mess with short strings like "I". twisti@3969: Handle type = field_signature_type_or_null(fd.signature()); twisti@3969: if (type.is_null()) { twisti@3969: java_lang_String::create_from_symbol(fd.signature(), CHECK); twisti@3969: } jrose@2639: java_lang_invoke_MemberName::set_type(mname(), type()); jrose@1145: } jrose@1145: return; jrose@1145: } jrose@1145: } jrose@1145: THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); jrose@1145: } jrose@1145: sspitsyn@4965: int MethodHandles::find_MemberNames(KlassHandle k, coleenp@2497: Symbol* name, Symbol* sig, sspitsyn@4965: int mflags, KlassHandle caller, sspitsyn@4965: int skip, objArrayHandle results) { jrose@1145: // %%% take caller into account! jrose@1145: sspitsyn@4965: Thread* thread = Thread::current(); sspitsyn@4965: sspitsyn@4965: if (k.is_null() || !k->oop_is_instance()) return -1; jrose@1145: jrose@1145: int rfill = 0, rlimit = results->length(), rskip = skip; jrose@1145: // overflow measurement: jrose@1145: int overflow = 0, overflow_limit = MAX2(1000, rlimit); jrose@1145: jrose@1145: int match_flags = mflags; jrose@1145: bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0); jrose@1145: bool search_intfc = ((match_flags & SEARCH_INTERFACES) != 0); jrose@1145: bool local_only = !(search_superc | search_intfc); jrose@1145: bool classes_only = false; jrose@1145: jrose@1145: if (name != NULL) { jrose@1145: if (name->utf8_length() == 0) return 0; // a match is not possible jrose@1145: } jrose@1145: if (sig != NULL) { jrose@1145: if (sig->utf8_length() == 0) return 0; // a match is not possible jrose@1145: if (sig->byte_at(0) == '(') jrose@1145: match_flags &= ~(IS_FIELD | IS_TYPE); jrose@1145: else jrose@1145: match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD); jrose@1145: } jrose@1145: jrose@1145: if ((match_flags & IS_TYPE) != 0) { jrose@1145: // NYI, and Core Reflection works quite well for this query jrose@1145: } jrose@1145: jrose@1145: if ((match_flags & IS_FIELD) != 0) { sspitsyn@4965: for (FieldStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) { jrose@1145: if (name != NULL && st.name() != name) jrose@1145: continue; jrose@1145: if (sig != NULL && st.signature() != sig) jrose@1145: continue; jrose@1145: // passed the filters jrose@1145: if (rskip > 0) { jrose@1145: --rskip; jrose@1145: } else if (rfill < rlimit) { sspitsyn@4965: Handle result(thread, results->obj_at(rfill++)); sspitsyn@4965: if (!java_lang_invoke_MemberName::is_instance(result())) jrose@1145: return -99; // caller bug! twisti@3969: oop type = field_signature_type_or_null(st.signature()); twisti@3969: oop name = field_name_or_null(st.name()); sspitsyn@4965: oop saved = MethodHandles::init_field_MemberName(result, st.klass(), twisti@3969: st.access_flags(), type, name, twisti@3969: st.offset()); sspitsyn@4965: if (saved != result()) twisti@3969: results->obj_at_put(rfill-1, saved); // show saved instance to user jrose@1145: } else if (++overflow >= overflow_limit) { jrose@1145: match_flags = 0; break; // got tired of looking at overflow jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) { jrose@1145: // watch out for these guys: coleenp@2497: Symbol* init_name = vmSymbols::object_initializer_name(); coleenp@2497: Symbol* clinit_name = vmSymbols::class_initializer_name(); jrose@1145: if (name == clinit_name) clinit_name = NULL; // hack for exposing jrose@1145: bool negate_name_test = false; jrose@1145: // fix name so that it captures the intention of IS_CONSTRUCTOR jrose@1145: if (!(match_flags & IS_METHOD)) { jrose@1145: // constructors only jrose@1145: if (name == NULL) { jrose@1145: name = init_name; jrose@1145: } else if (name != init_name) { jrose@1145: return 0; // no constructors of this method name jrose@1145: } jrose@1145: } else if (!(match_flags & IS_CONSTRUCTOR)) { jrose@1145: // methods only jrose@1145: if (name == NULL) { jrose@1145: name = init_name; jrose@1145: negate_name_test = true; // if we see the name, we *omit* the entry jrose@1145: } else if (name == init_name) { jrose@1145: return 0; // no methods of this constructor name jrose@1145: } jrose@1145: } else { jrose@1145: // caller will accept either sort; no need to adjust name jrose@1145: } sspitsyn@4965: for (MethodStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) { coleenp@4037: Method* m = st.method(); coleenp@2497: Symbol* m_name = m->name(); jrose@1145: if (m_name == clinit_name) jrose@1145: continue; jrose@1145: if (name != NULL && ((m_name != name) ^ negate_name_test)) jrose@1145: continue; jrose@1145: if (sig != NULL && m->signature() != sig) jrose@1145: continue; jrose@1145: // passed the filters jrose@1145: if (rskip > 0) { jrose@1145: --rskip; jrose@1145: } else if (rfill < rlimit) { sspitsyn@4965: Handle result(thread, results->obj_at(rfill++)); sspitsyn@4965: if (!java_lang_invoke_MemberName::is_instance(result())) jrose@1145: return -99; // caller bug! twisti@3969: oop saved = MethodHandles::init_method_MemberName(result, m, true, NULL); sspitsyn@4965: if (saved != result()) twisti@3969: results->obj_at_put(rfill-1, saved); // show saved instance to user jrose@1145: } else if (++overflow >= overflow_limit) { jrose@1145: match_flags = 0; break; // got tired of looking at overflow jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: jrose@1145: // return number of elements we at leasted wanted to initialize jrose@1145: return rfill + overflow; jrose@1145: } sspitsyn@4965: sspitsyn@4965: //------------------------------------------------------------------------------ sspitsyn@4965: // MemberNameTable sspitsyn@4965: // sspitsyn@4965: sspitsyn@4965: MemberNameTable::MemberNameTable() : GrowableArray(10, true) { sspitsyn@4965: assert_locked_or_safepoint(MemberNameTable_lock); sspitsyn@4965: } sspitsyn@4965: sspitsyn@4965: MemberNameTable::~MemberNameTable() { sspitsyn@4965: assert_locked_or_safepoint(MemberNameTable_lock); sspitsyn@4965: int len = this->length(); sspitsyn@4965: sspitsyn@4965: for (int idx = 0; idx < len; idx++) { sspitsyn@4965: jweak ref = this->at(idx); sspitsyn@4965: JNIHandles::destroy_weak_global(ref); sspitsyn@4965: } sspitsyn@4965: } sspitsyn@4965: sspitsyn@4965: // Return entry index if found, return -1 otherwise. sspitsyn@4965: int MemberNameTable::find_member_name(oop mem_name) { sspitsyn@4965: assert_locked_or_safepoint(MemberNameTable_lock); sspitsyn@4965: int len = this->length(); sspitsyn@4965: sspitsyn@4965: for (int idx = 0; idx < len; idx++) { sspitsyn@4965: jweak ref = this->at(idx); sspitsyn@4965: oop entry = JNIHandles::resolve(ref); sspitsyn@4965: if (entry == mem_name) { sspitsyn@4965: return idx; sspitsyn@4965: } sspitsyn@4965: } sspitsyn@4965: return -1; sspitsyn@4965: } sspitsyn@4965: sspitsyn@4965: void MemberNameTable::add_member_name(jweak mem_name_wref) { sspitsyn@4965: assert_locked_or_safepoint(MemberNameTable_lock); sspitsyn@4965: oop mem_name = JNIHandles::resolve(mem_name_wref); sspitsyn@4965: sspitsyn@4965: // Each member name may appear just once: add only if not found sspitsyn@4965: if (find_member_name(mem_name) == -1) { sspitsyn@4965: this->append(mem_name_wref); sspitsyn@4965: } sspitsyn@4965: } sspitsyn@4965: sspitsyn@4965: #if INCLUDE_JVMTI sspitsyn@4965: oop MemberNameTable::find_member_name_by_method(Method* old_method) { sspitsyn@4965: assert_locked_or_safepoint(MemberNameTable_lock); sspitsyn@4965: oop found = NULL; sspitsyn@4965: int len = this->length(); sspitsyn@4965: sspitsyn@4965: for (int idx = 0; idx < len; idx++) { sspitsyn@4965: oop mem_name = JNIHandles::resolve(this->at(idx)); sspitsyn@4965: if (mem_name == NULL) { sspitsyn@4965: continue; sspitsyn@4965: } sspitsyn@4965: Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mem_name); sspitsyn@4965: if (method == old_method) { sspitsyn@4965: found = mem_name; sspitsyn@4965: break; sspitsyn@4965: } sspitsyn@4965: } sspitsyn@4965: return found; sspitsyn@4965: } sspitsyn@4965: sspitsyn@4965: // It is called at safepoint only sspitsyn@4965: void MemberNameTable::adjust_method_entries(Method** old_methods, Method** new_methods, sspitsyn@4965: int methods_length, bool *trace_name_printed) { sspitsyn@4965: assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); sspitsyn@4965: // search the MemberNameTable for uses of either obsolete or EMCP methods sspitsyn@4965: for (int j = 0; j < methods_length; j++) { sspitsyn@4965: Method* old_method = old_methods[j]; sspitsyn@4965: Method* new_method = new_methods[j]; sspitsyn@4965: oop mem_name = find_member_name_by_method(old_method); sspitsyn@4965: if (mem_name != NULL) { sspitsyn@4965: java_lang_invoke_MemberName::adjust_vmtarget(mem_name, new_method); sspitsyn@4965: sspitsyn@4965: if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { sspitsyn@4965: if (!(*trace_name_printed)) { sspitsyn@4965: // RC_TRACE_MESG macro has an embedded ResourceMark sspitsyn@4965: RC_TRACE_MESG(("adjust: name=%s", sspitsyn@4965: old_method->method_holder()->external_name())); sspitsyn@4965: *trace_name_printed = true; sspitsyn@4965: } sspitsyn@4965: // RC_TRACE macro has an embedded ResourceMark sspitsyn@4965: RC_TRACE(0x00400000, ("MemberName method update: %s(%s)", sspitsyn@4965: new_method->name()->as_C_string(), sspitsyn@4965: new_method->signature()->as_C_string())); sspitsyn@4965: } sspitsyn@4965: } sspitsyn@4965: } sspitsyn@4965: } sspitsyn@4965: #endif // INCLUDE_JVMTI sspitsyn@4965: jrose@1145: // twisti@3969: // Here are the native methods in java.lang.invoke.MethodHandleNatives jrose@1145: // They are the private interface between this JVM and the HotSpot-specific jrose@1145: // Java code that implements JSR 292 method handles. jrose@1145: // jrose@1145: // Note: We use a JVM_ENTRY macro to define each of these, for this is the way jrose@1145: // that intrinsic (non-JNI) native methods are defined in HotSpot. jrose@1145: // coleenp@4037: jrose@2639: JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) { jrose@1145: switch (which) { never@3105: case MethodHandles::GC_COUNT_GWT: never@3105: #ifdef COMPILER2 never@3105: return true; never@3105: #else never@3105: return false; never@3105: #endif jrose@1145: } jrose@1145: return 0; jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: #ifndef PRODUCT twisti@3969: #define EACH_NAMED_CON(template, requirement) \ twisti@3969: template(MethodHandles,GC_COUNT_GWT) \ jrose@2639: template(java_lang_invoke_MemberName,MN_IS_METHOD) \ jrose@2639: template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \ jrose@2639: template(java_lang_invoke_MemberName,MN_IS_FIELD) \ jrose@2639: template(java_lang_invoke_MemberName,MN_IS_TYPE) \ twisti@4866: template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \ jrose@2639: template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \ jrose@2639: template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \ twisti@3969: template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \ twisti@3969: template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \ twisti@3969: template(MethodHandles,GC_LAMBDA_SUPPORT) \ jrose@1145: /*end*/ jrose@1145: twisti@3969: #define IGNORE_REQ(req_expr) /* req_expr */ jrose@1145: #define ONE_PLUS(scope,value) 1+ twisti@3969: static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0; jrose@1145: #define VALUE_COMMA(scope,value) scope::value, twisti@3969: static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA, IGNORE_REQ) 0 }; jrose@1145: #define STRING_NULL(scope,value) #value "\0" twisti@3969: static const char con_names[] = { EACH_NAMED_CON(STRING_NULL, IGNORE_REQ) }; twisti@3969: twisti@3969: static bool advertise_con_value(int which) { twisti@3969: if (which < 0) return false; twisti@3969: bool ok = true; twisti@3969: int count = 0; twisti@3969: #define INC_COUNT(scope,value) \ twisti@3969: ++count; twisti@3969: #define CHECK_REQ(req_expr) \ twisti@3969: if (which < count) return ok; \ twisti@3969: ok = (req_expr); twisti@3969: EACH_NAMED_CON(INC_COUNT, CHECK_REQ); twisti@3969: #undef INC_COUNT twisti@3969: #undef CHECK_REQ twisti@3969: assert(count == con_value_count, ""); twisti@3969: if (which < count) return ok; twisti@3969: return false; twisti@3969: } jrose@1145: jrose@1145: #undef ONE_PLUS jrose@1145: #undef VALUE_COMMA jrose@1145: #undef STRING_NULL jrose@1145: #undef EACH_NAMED_CON twisti@3969: #endif // PRODUCT jrose@1145: jrose@2639: JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) { jrose@1145: #ifndef PRODUCT twisti@3969: if (advertise_con_value(which)) { twisti@3969: assert(which >= 0 && which < con_value_count, ""); jrose@1145: int con = con_values[which]; twisti@2806: objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh)); twisti@2806: if (box.not_null() && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) { jrose@1145: const char* str = &con_names[0]; jrose@1145: for (int i = 0; i < which; i++) jrose@1145: str += strlen(str) + 1; // skip name and null twisti@2806: oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint jrose@1145: box->obj_at_put(0, name); jrose@1145: } jrose@1145: return con; jrose@1145: } jrose@1145: #endif jrose@1145: return 0; jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // void init(MemberName self, AccessibleObject ref) jrose@2639: JVM_ENTRY(void, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) { never@2937: if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } never@2937: if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); } jrose@1145: Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); sspitsyn@4965: Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); sspitsyn@4965: MethodHandles::init_MemberName(mname, target); jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // void expand(MemberName self) jrose@2639: JVM_ENTRY(void, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) { never@2937: if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } jrose@1145: Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); jrose@1145: MethodHandles::expand_MemberName(mname, 0, CHECK); jrose@1145: } jrose@1145: JVM_END jrose@1145: jrose@1145: // void resolve(MemberName self, Class caller) twisti@3969: JVM_ENTRY(jobject, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) { twisti@3969: if (mname_jh == NULL) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); } jrose@1145: Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); jrose@1862: jrose@1862: // The trusted Java code that calls this method should already have performed jrose@1862: // access checks on behalf of the given caller. But, we can verify this. twisti@3969: if (VerifyMethodHandles && caller_jh != NULL && twisti@3969: java_lang_invoke_MemberName::clazz(mname()) != NULL) { coleenp@4037: Klass* reference_klass = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname())); jrose@1862: if (reference_klass != NULL) { jrose@1862: // Emulate LinkResolver::check_klass_accessability. coleenp@4037: Klass* caller = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh)); jrose@1862: if (!Reflection::verify_class_access(caller, jrose@1862: reference_klass, jrose@1862: true)) { hseigel@4278: THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), reference_klass->external_name()); jrose@1862: } jrose@1862: } jrose@1862: } jrose@1862: twisti@3969: Handle resolved = MethodHandles::resolve_MemberName(mname, CHECK_NULL); twisti@3969: if (resolved.is_null()) { twisti@3969: int flags = java_lang_invoke_MemberName::flags(mname()); twisti@3969: int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; twisti@3969: if (!MethodHandles::ref_kind_is_valid(ref_kind)) { twisti@3969: THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "obsolete MemberName format"); twisti@3969: } twisti@3969: if ((flags & ALL_KINDS) == IS_FIELD) { twisti@3969: THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), "field resolution failed"); twisti@3969: } else if ((flags & ALL_KINDS) == IS_METHOD || twisti@3969: (flags & ALL_KINDS) == IS_CONSTRUCTOR) { twisti@3969: THROW_MSG_NULL(vmSymbols::java_lang_NoSuchFieldError(), "method resolution failed"); twisti@3969: } else { twisti@3969: THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "resolution failed"); twisti@3969: } twisti@3969: } twisti@3969: twisti@3969: return JNIHandles::make_local(THREAD, resolved()); jrose@1145: } jrose@1145: JVM_END jrose@1145: twisti@3969: static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) { twisti@3969: if (mname == NULL || twisti@3969: java_lang_invoke_MemberName::vmtarget(mname) == NULL) { twisti@3969: THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved"); twisti@3969: } else { twisti@3969: int flags = java_lang_invoke_MemberName::flags(mname); twisti@3969: if ((flags & IS_FIELD) != 0 && twisti@3969: (must_be_static twisti@3969: ? (flags & JVM_ACC_STATIC) != 0 twisti@3969: : (flags & JVM_ACC_STATIC) == 0)) { twisti@3969: int vmindex = java_lang_invoke_MemberName::vmindex(mname); twisti@3969: return (jlong) vmindex; twisti@3969: } twisti@3969: } twisti@3969: const char* msg = (must_be_static ? "static field required" : "non-static field required"); twisti@3969: THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg); twisti@3969: return 0; twisti@3969: } twisti@3969: twisti@3969: JVM_ENTRY(jlong, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { twisti@3969: return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD); twisti@3969: } twisti@3969: JVM_END twisti@3969: twisti@3969: JVM_ENTRY(jlong, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { twisti@3969: return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD); twisti@3969: } twisti@3969: JVM_END twisti@3969: twisti@3969: JVM_ENTRY(jobject, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) { twisti@3969: // use the other function to perform sanity checks: twisti@3969: jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL); twisti@3969: oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh)); twisti@3969: return JNIHandles::make_local(THREAD, clazz); twisti@3969: } twisti@3969: JVM_END twisti@3969: twisti@3969: JVM_ENTRY(jobject, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) { twisti@3969: if (mname_jh == NULL) return NULL; twisti@3969: Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); twisti@3969: intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname()); coleenp@4037: Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); twisti@3969: objArrayHandle result = oopFactory::new_objArray(SystemDictionary::Object_klass(), 2, CHECK_NULL); twisti@3969: jvalue vmindex_value; vmindex_value.j = (long)vmindex; twisti@3969: oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL); twisti@3969: result->obj_at_put(0, x); twisti@3969: x = NULL; coleenp@4037: if (vmtarget == NULL) { coleenp@4037: x = NULL; twisti@3969: } else if (vmtarget->is_klass()) { hseigel@4278: x = ((Klass*) vmtarget)->java_mirror(); coleenp@4037: } else if (vmtarget->is_method()) { twisti@3969: Handle mname2 = MethodHandles::new_MemberName(CHECK_NULL); sspitsyn@4965: x = MethodHandles::init_method_MemberName(mname2, (Method*)vmtarget, false, NULL); twisti@3969: } twisti@3969: result->obj_at_put(1, x); twisti@3969: return JNIHandles::make_local(env, result()); twisti@3969: } twisti@3969: JVM_END twisti@3969: twisti@3969: twisti@3969: jrose@1145: // static native int getMembers(Class defc, String matchName, String matchSig, jrose@1145: // int matchFlags, Class caller, int skip, MemberName[] results); jrose@2639: JVM_ENTRY(jint, MHN_getMembers(JNIEnv *env, jobject igcls, jrose@1145: jclass clazz_jh, jstring name_jh, jstring sig_jh, jrose@1145: int mflags, jclass caller_jh, jint skip, jobjectArray results_jh)) { jrose@1145: if (clazz_jh == NULL || results_jh == NULL) return -1; coleenp@4037: KlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz_jh))); jrose@1145: twisti@2806: objArrayHandle results(THREAD, (objArrayOop) JNIHandles::resolve(results_jh)); twisti@2806: if (results.is_null() || !results->is_objArray()) return -1; jrose@1145: coleenp@2497: TempNewSymbol name = NULL; coleenp@2497: TempNewSymbol sig = NULL; jrose@1145: if (name_jh != NULL) { jrose@1145: name = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(name_jh)); jrose@1145: if (name == NULL) return 0; // a match is not possible jrose@1145: } jrose@1145: if (sig_jh != NULL) { jrose@1145: sig = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(sig_jh)); jrose@1145: if (sig == NULL) return 0; // a match is not possible jrose@1145: } jrose@1145: twisti@2806: KlassHandle caller; jrose@1145: if (caller_jh != NULL) { jrose@1145: oop caller_oop = JNIHandles::resolve_non_null(caller_jh); jrose@1145: if (!java_lang_Class::is_instance(caller_oop)) return -1; coleenp@4037: caller = KlassHandle(THREAD, java_lang_Class::as_Klass(caller_oop)); jrose@1145: } jrose@1145: twisti@2806: if (name != NULL && sig != NULL && results.not_null()) { jrose@1145: // try a direct resolve jrose@1145: // %%% TO DO jrose@1145: } jrose@1145: sspitsyn@4965: int res = MethodHandles::find_MemberNames(k, name, sig, mflags, sspitsyn@4965: caller, skip, results); jrose@1145: // TO DO: expand at least some of the MemberNames, to avoid massive callbacks jrose@1145: return res; jrose@1145: } jrose@1145: JVM_END jrose@1145: twisti@3131: JVM_ENTRY(void, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { twisti@3239: Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh)); twisti@3239: Handle target (THREAD, JNIHandles::resolve(target_jh)); twisti@3131: { twisti@3131: // Walk all nmethods depending on this call site. twisti@3131: MutexLocker mu(Compile_lock, thread); twisti@3131: Universe::flush_dependents_on(call_site, target); twisti@4354: java_lang_invoke_CallSite::set_target(call_site(), target()); twisti@3131: } twisti@3131: } twisti@3131: JVM_END twisti@3131: twisti@3131: JVM_ENTRY(void, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { twisti@3239: Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh)); twisti@3239: Handle target (THREAD, JNIHandles::resolve(target_jh)); twisti@3131: { twisti@3131: // Walk all nmethods depending on this call site. twisti@3131: MutexLocker mu(Compile_lock, thread); twisti@3131: Universe::flush_dependents_on(call_site, target); twisti@4354: java_lang_invoke_CallSite::set_target_volatile(call_site(), target()); twisti@3131: } twisti@3131: } twisti@3131: JVM_END twisti@3131: jrose@1145: /// JVM_RegisterMethodHandleMethods jrose@1145: twisti@3131: #undef CS // Solaris builds complain twisti@3131: jrose@1145: #define LANG "Ljava/lang/" jrose@2742: #define JLINV "Ljava/lang/invoke/" jrose@1145: jrose@1145: #define OBJ LANG"Object;" jrose@1145: #define CLS LANG"Class;" jrose@1145: #define STRG LANG"String;" twisti@3131: #define CS JLINV"CallSite;" jrose@2742: #define MT JLINV"MethodType;" jrose@2742: #define MH JLINV"MethodHandle;" jrose@2742: #define MEM JLINV"MemberName;" jrose@1145: jrose@1145: #define CC (char*) /*cast a literal from (const char*)*/ jrose@1145: #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) jrose@1145: twisti@3131: // These are the native methods on java.lang.invoke.MethodHandleNatives. twisti@3969: static JNINativeMethod required_methods_JDK8[] = { twisti@3131: {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)}, twisti@3131: {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)}, twisti@3969: {CC"resolve", CC"("MEM""CLS")"MEM, FN_PTR(MHN_resolve_Mem)}, twisti@3131: {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)}, jrose@1145: // static native int getNamedCon(int which, Object[] name) twisti@3131: {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)}, jrose@1145: // static native int getMembers(Class defc, String matchName, String matchSig, jrose@1145: // int matchFlags, Class caller, int skip, MemberName[] results); twisti@3969: {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)}, twisti@3969: {CC"objectFieldOffset", CC"("MEM")J", FN_PTR(MHN_objectFieldOffset)}, twisti@3131: {CC"setCallSiteTargetNormal", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetNormal)}, twisti@3969: {CC"setCallSiteTargetVolatile", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetVolatile)}, twisti@3969: {CC"staticFieldOffset", CC"("MEM")J", FN_PTR(MHN_staticFieldOffset)}, twisti@3969: {CC"staticFieldBase", CC"("MEM")"OBJ, FN_PTR(MHN_staticFieldBase)}, twisti@3969: {CC"getMemberVMInfo", CC"("MEM")"OBJ, FN_PTR(MHN_getMemberVMInfo)} jrose@1145: }; jrose@1145: jrose@1145: // This one function is exported, used by NativeLookup. jrose@1145: jrose@1145: JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { twisti@2698: if (!EnableInvokeDynamic) { twisti@2698: warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable."); jrose@1145: return; // bind nothing jrose@1145: } jrose@1145: twisti@3131: assert(!MethodHandles::enabled(), "must not be enabled"); jrose@1474: bool enable_MH = true; jrose@1474: twisti@3969: jclass MH_class = NULL; twisti@3969: if (SystemDictionary::MethodHandle_klass() == NULL) { twisti@3969: enable_MH = false; twisti@3969: } else { hseigel@4278: oop mirror = SystemDictionary::MethodHandle_klass()->java_mirror(); twisti@3969: MH_class = (jclass) JNIHandles::make_local(env, mirror); twisti@3969: } twisti@3969: twisti@3969: int status; twisti@3969: twisti@3969: if (enable_MH) { jrose@1145: ThreadToNativeFromVM ttnfv(thread); twisti@3969: twisti@3969: status = env->RegisterNatives(MHN_class, required_methods_JDK8, sizeof(required_methods_JDK8)/sizeof(JNINativeMethod)); twisti@3969: if (status != JNI_OK || env->ExceptionOccurred()) { jrose@2742: warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); jrose@2742: enable_MH = false; jrose@1145: env->ExceptionClear(); jrose@1145: } jrose@1145: } jrose@1161: twisti@3969: if (TraceInvokeDynamic) { twisti@3969: tty->print_cr("MethodHandle support loaded (using LambdaForms)"); jrose@1474: } jrose@1474: jrose@1474: if (enable_MH) { twisti@2436: MethodHandles::generate_adapters(); jrose@1474: MethodHandles::set_enabled(true); jrose@1474: } jrose@1145: } jrose@1145: JVM_END